blob: 49a255a369108df124765f265efb87f7eb5e32b0 [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"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/Lexer.h"
Richard Smith7a614d82011-06-11 17:19:42 +000025#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000027#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000029#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000031#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000032using namespace clang;
33
Chris Lattner2b334bb2010-04-16 23:34:13 +000034/// isKnownToHaveBooleanValue - Return true if this is an integer expression
35/// that is known to return 0 or 1. This happens for _Bool/bool expressions
36/// but also int expressions which are produced by things like comparisons in
37/// C.
38bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000039 const Expr *E = IgnoreParens();
40
Chris Lattner2b334bb2010-04-16 23:34:13 +000041 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000042 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000043 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000044 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000045
Peter Collingbournef111d932011-04-15 00:35:48 +000046 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000047 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000048 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000049 return UO->getSubExpr()->isKnownToHaveBooleanValue();
50 default:
51 return false;
52 }
53 }
Sean Huntc3021132010-05-05 15:23:54 +000054
John McCall6907fbe2010-06-12 01:56:02 +000055 // Only look through implicit casts. If the user writes
56 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000057 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000058 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000059
Peter Collingbournef111d932011-04-15 00:35:48 +000060 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000061 switch (BO->getOpcode()) {
62 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000063 case BO_LT: // Relational operators.
64 case BO_GT:
65 case BO_LE:
66 case BO_GE:
67 case BO_EQ: // Equality operators.
68 case BO_NE:
69 case BO_LAnd: // AND operator.
70 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000071 return true;
Sean Huntc3021132010-05-05 15:23:54 +000072
John McCall2de56d12010-08-25 11:45:40 +000073 case BO_And: // Bitwise AND operator.
74 case BO_Xor: // Bitwise XOR operator.
75 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000076 // Handle things like (x==2)|(y==12).
77 return BO->getLHS()->isKnownToHaveBooleanValue() &&
78 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000079
John McCall2de56d12010-08-25 11:45:40 +000080 case BO_Comma:
81 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000082 return BO->getRHS()->isKnownToHaveBooleanValue();
83 }
84 }
Sean Huntc3021132010-05-05 15:23:54 +000085
Peter Collingbournef111d932011-04-15 00:35:48 +000086 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000087 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
88 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000089
Chris Lattner2b334bb2010-04-16 23:34:13 +000090 return false;
91}
92
John McCall63c00d72011-02-09 08:16:59 +000093// Amusing macro metaprogramming hack: check whether a class provides
94// a more specific implementation of getExprLoc().
95namespace {
96 /// This implementation is used when a class provides a custom
97 /// implementation of getExprLoc.
98 template <class E, class T>
99 SourceLocation getExprLocImpl(const Expr *expr,
100 SourceLocation (T::*v)() const) {
101 return static_cast<const E*>(expr)->getExprLoc();
102 }
103
104 /// This implementation is used when a class doesn't provide
105 /// a custom implementation of getExprLoc. Overload resolution
106 /// should pick it over the implementation above because it's
107 /// more specialized according to function template partial ordering.
108 template <class E>
109 SourceLocation getExprLocImpl(const Expr *expr,
110 SourceLocation (Expr::*v)() const) {
111 return static_cast<const E*>(expr)->getSourceRange().getBegin();
112 }
113}
114
115SourceLocation Expr::getExprLoc() const {
116 switch (getStmtClass()) {
117 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
118#define ABSTRACT_STMT(type)
119#define STMT(type, base) \
120 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
121#define EXPR(type, base) \
122 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
123#include "clang/AST/StmtNodes.inc"
124 }
125 llvm_unreachable("unknown statement kind");
126 return SourceLocation();
127}
128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129//===----------------------------------------------------------------------===//
130// Primary Expressions.
131//===----------------------------------------------------------------------===//
132
Douglas Gregor561f8122011-07-01 01:22:09 +0000133/// \brief Compute the type-, value-, and instantiation-dependence of a
134/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000135/// based on the declaration being referenced.
136static void computeDeclRefDependence(NamedDecl *D, QualType T,
137 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000138 bool &ValueDependent,
139 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000140 TypeDependent = false;
141 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000142 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000143
144 // (TD) C++ [temp.dep.expr]p3:
145 // An id-expression is type-dependent if it contains:
146 //
Sean Huntc3021132010-05-05 15:23:54 +0000147 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000148 //
149 // (VD) C++ [temp.dep.constexpr]p2:
150 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000151
Douglas Gregor0da76df2009-11-23 11:41:28 +0000152 // (TD) - an identifier that was declared with dependent type
153 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000154 if (T->isDependentType()) {
155 TypeDependent = true;
156 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000157 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000158 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000159 } else if (T->isInstantiationDependentType()) {
160 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000161 }
Douglas Gregord967e312011-01-19 21:52:31 +0000162
Douglas Gregor0da76df2009-11-23 11:41:28 +0000163 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000164 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000165 == DeclarationName::CXXConversionFunctionName) {
166 QualType T = D->getDeclName().getCXXNameType();
167 if (T->isDependentType()) {
168 TypeDependent = true;
169 ValueDependent = true;
170 InstantiationDependent = true;
171 return;
172 }
173
174 if (T->isInstantiationDependentType())
175 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000176 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000177
Douglas Gregor0da76df2009-11-23 11:41:28 +0000178 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000179 if (isa<NonTypeTemplateParmDecl>(D)) {
180 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000181 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000182 return;
183 }
184
Douglas Gregor0da76df2009-11-23 11:41:28 +0000185 // (VD) - a constant with integral or enumeration type and is
186 // initialized with an expression that is value-dependent.
Douglas Gregord967e312011-01-19 21:52:31 +0000187 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000188 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000189 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000190 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000191 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000192 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000193 InstantiationDependent = true;
194 }
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000195 }
Douglas Gregord967e312011-01-19 21:52:31 +0000196
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000197 // (VD) - FIXME: Missing from the standard:
198 // - a member function or a static data member of the current
199 // instantiation
200 else if (Var->isStaticDataMember() &&
Douglas Gregor561f8122011-07-01 01:22:09 +0000201 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000202 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000203 InstantiationDependent = true;
204 }
Douglas Gregord967e312011-01-19 21:52:31 +0000205
206 return;
207 }
208
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000209 // (VD) - FIXME: Missing from the standard:
210 // - a member function or a static data member of the current
211 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000212 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
213 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000214 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000215 return;
216 }
217}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000218
Douglas Gregord967e312011-01-19 21:52:31 +0000219void DeclRefExpr::computeDependence() {
220 bool TypeDependent = false;
221 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000222 bool InstantiationDependent = false;
223 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
224 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000225
226 // (TD) C++ [temp.dep.expr]p3:
227 // An id-expression is type-dependent if it contains:
228 //
229 // and
230 //
231 // (VD) C++ [temp.dep.constexpr]p2:
232 // An identifier is value-dependent if it is:
233 if (!TypeDependent && !ValueDependent &&
234 hasExplicitTemplateArgs() &&
235 TemplateSpecializationType::anyDependentTemplateArguments(
236 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000237 getNumTemplateArgs(),
238 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000239 TypeDependent = true;
240 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000241 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000242 }
243
244 ExprBits.TypeDependent = TypeDependent;
245 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000246 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000247
Douglas Gregor10738d32010-12-23 23:51:58 +0000248 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000249 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000250 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000251}
252
Chandler Carruth3aa81402011-05-01 23:48:14 +0000253DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000254 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000255 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000256 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000257 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000258 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000259 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
260 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000261 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000262 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000263 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
264 if (FoundD)
265 getInternalFoundDecl() = FoundD;
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000266 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Douglas Gregor561f8122011-07-01 01:22:09 +0000267 if (TemplateArgs) {
268 bool Dependent = false;
269 bool InstantiationDependent = false;
270 bool ContainsUnexpandedParameterPack = false;
271 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
272 InstantiationDependent,
273 ContainsUnexpandedParameterPack);
274 if (InstantiationDependent)
275 setInstantiationDependent(true);
276 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000277 DeclRefExprBits.HadMultipleCandidates = 0;
278
Abramo Bagnara25777432010-08-11 22:01:17 +0000279 computeDependence();
280}
281
Douglas Gregora2813ce2009-10-23 18:54:35 +0000282DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000283 NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000284 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000285 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000286 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000287 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000288 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000289 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000290 return Create(Context, QualifierLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000291 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000292 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000293}
294
295DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000296 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000297 ValueDecl *D,
298 const DeclarationNameInfo &NameInfo,
299 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000300 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000301 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000302 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000303 // Filter out cases where the found Decl is the same as the value refenenced.
304 if (D == FoundD)
305 FoundD = 0;
306
Douglas Gregora2813ce2009-10-23 18:54:35 +0000307 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000308 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000309 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000310 if (FoundD)
311 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000312 if (TemplateArgs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000313 Size += ASTTemplateArgumentListInfo::sizeFor(*TemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000314
Chris Lattner32488542010-10-30 05:14:06 +0000315 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth3aa81402011-05-01 23:48:14 +0000316 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
317 T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000318}
319
Chandler Carruth3aa81402011-05-01 23:48:14 +0000320DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000321 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000322 bool HasFoundDecl,
Douglas Gregordef03542011-02-04 12:01:24 +0000323 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000324 unsigned NumTemplateArgs) {
325 std::size_t Size = sizeof(DeclRefExpr);
326 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000327 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000328 if (HasFoundDecl)
329 Size += sizeof(NamedDecl *);
Douglas Gregordef03542011-02-04 12:01:24 +0000330 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000331 Size += ASTTemplateArgumentListInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000332
Chris Lattner32488542010-10-30 05:14:06 +0000333 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000334 return new (Mem) DeclRefExpr(EmptyShell());
335}
336
Douglas Gregora2813ce2009-10-23 18:54:35 +0000337SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000338 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000339 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000340 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000341 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000342 R.setEnd(getRAngleLoc());
343 return R;
344}
345
Anders Carlsson3a082d82009-09-08 18:24:21 +0000346// FIXME: Maybe this should use DeclPrinter with a special "print predefined
347// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000348std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
349 ASTContext &Context = CurrentDecl->getASTContext();
350
Anders Carlsson3a082d82009-09-08 18:24:21 +0000351 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000352 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000353 return FD->getNameAsString();
354
355 llvm::SmallString<256> Name;
356 llvm::raw_svector_ostream Out(Name);
357
358 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000359 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000360 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000361 if (MD->isStatic())
362 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000363 }
364
365 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000366
367 std::string Proto = FD->getQualifiedNameAsString(Policy);
368
John McCall183700f2009-09-21 23:43:11 +0000369 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000370 const FunctionProtoType *FT = 0;
371 if (FD->hasWrittenPrototype())
372 FT = dyn_cast<FunctionProtoType>(AFT);
373
374 Proto += "(";
375 if (FT) {
376 llvm::raw_string_ostream POut(Proto);
377 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
378 if (i) POut << ", ";
379 std::string Param;
380 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
381 POut << Param;
382 }
383
384 if (FT->isVariadic()) {
385 if (FD->getNumParams()) POut << ", ";
386 POut << "...";
387 }
388 }
389 Proto += ")";
390
Sam Weinig4eadcc52009-12-27 01:38:20 +0000391 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
392 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
393 if (ThisQuals.hasConst())
394 Proto += " const";
395 if (ThisQuals.hasVolatile())
396 Proto += " volatile";
397 }
398
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000399 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
400 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000401
402 Out << Proto;
403
404 Out.flush();
405 return Name.str().str();
406 }
407 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
408 llvm::SmallString<256> Name;
409 llvm::raw_svector_ostream Out(Name);
410 Out << (MD->isInstanceMethod() ? '-' : '+');
411 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000412
413 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
414 // a null check to avoid a crash.
415 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000416 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000417
Anders Carlsson3a082d82009-09-08 18:24:21 +0000418 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000419 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
420 Out << '(' << CID << ')';
421
Anders Carlsson3a082d82009-09-08 18:24:21 +0000422 Out << ' ';
423 Out << MD->getSelector().getAsString();
424 Out << ']';
425
426 Out.flush();
427 return Name.str().str();
428 }
429 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
430 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
431 return "top level";
432 }
433 return "";
434}
435
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000436void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
437 if (hasAllocation())
438 C.Deallocate(pVal);
439
440 BitWidth = Val.getBitWidth();
441 unsigned NumWords = Val.getNumWords();
442 const uint64_t* Words = Val.getRawData();
443 if (NumWords > 1) {
444 pVal = new (C) uint64_t[NumWords];
445 std::copy(Words, Words + NumWords, pVal);
446 } else if (NumWords == 1)
447 VAL = Words[0];
448 else
449 VAL = 0;
450}
451
452IntegerLiteral *
453IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
454 QualType type, SourceLocation l) {
455 return new (C) IntegerLiteral(C, V, type, l);
456}
457
458IntegerLiteral *
459IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
460 return new (C) IntegerLiteral(Empty);
461}
462
463FloatingLiteral *
464FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
465 bool isexact, QualType Type, SourceLocation L) {
466 return new (C) FloatingLiteral(C, V, isexact, Type, L);
467}
468
469FloatingLiteral *
470FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
471 return new (C) FloatingLiteral(Empty);
472}
473
Chris Lattnerda8249e2008-06-07 22:13:43 +0000474/// getValueAsApproximateDouble - This returns the value as an inaccurate
475/// double. Note that this may cause loss of precision, but is useful for
476/// debugging dumps, etc.
477double FloatingLiteral::getValueAsApproximateDouble() const {
478 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000479 bool ignored;
480 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
481 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000482 return V.convertToDouble();
483}
484
Chris Lattner5f9e2722011-07-23 10:55:15 +0000485StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000486 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000487 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000488 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000489 // Allocate enough space for the StringLiteral plus an array of locations for
490 // any concatenated string tokens.
491 void *Mem = C.Allocate(sizeof(StringLiteral)+
492 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000493 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000494 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000495
Reid Spencer5f016e22007-07-11 17:01:13 +0000496 // OPTIMIZE: could allocate this appended to the StringLiteral.
Jay Foad65aa6882011-06-21 15:13:30 +0000497 char *AStrData = new (C, 1) char[Str.size()];
498 memcpy(AStrData, Str.data(), Str.size());
Chris Lattner2085fd62009-02-18 06:40:38 +0000499 SL->StrData = AStrData;
Jay Foad65aa6882011-06-21 15:13:30 +0000500 SL->ByteLength = Str.size();
Douglas Gregor5cee1192011-07-27 05:40:30 +0000501 SL->Kind = Kind;
Anders Carlsson3e2193c2011-04-14 00:40:03 +0000502 SL->IsPascal = Pascal;
Chris Lattner2085fd62009-02-18 06:40:38 +0000503 SL->TokLocs[0] = Loc[0];
504 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000505
Chris Lattner726e1682009-02-18 05:49:11 +0000506 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000507 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
508 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000509}
510
Douglas Gregor673ecd62009-04-15 16:35:07 +0000511StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
512 void *Mem = C.Allocate(sizeof(StringLiteral)+
513 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000514 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000515 StringLiteral *SL = new (Mem) StringLiteral(QualType());
516 SL->StrData = 0;
517 SL->ByteLength = 0;
518 SL->NumConcatenated = NumStrs;
519 return SL;
520}
521
Chris Lattner5f9e2722011-07-23 10:55:15 +0000522void StringLiteral::setString(ASTContext &C, StringRef Str) {
Daniel Dunbarb6480232009-09-22 03:27:33 +0000523 char *AStrData = new (C, 1) char[Str.size()];
524 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000525 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000526 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000527}
528
Chris Lattner08f92e32010-11-17 07:37:15 +0000529/// getLocationOfByte - Return a source location that points to the specified
530/// byte of this string literal.
531///
532/// Strings are amazingly complex. They can be formed from multiple tokens and
533/// can have escape sequences in them in addition to the usual trigraph and
534/// escaped newline business. This routine handles this complexity.
535///
536SourceLocation StringLiteral::
537getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
538 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000539 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
540
Chris Lattner08f92e32010-11-17 07:37:15 +0000541 // Loop over all of the tokens in this string until we find the one that
542 // contains the byte we're looking for.
543 unsigned TokNo = 0;
544 while (1) {
545 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
546 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
547
548 // Get the spelling of the string so that we can get the data that makes up
549 // the string literal, not the identifier for the macro it is potentially
550 // expanded through.
551 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
552
553 // Re-lex the token to get its length and original spelling.
554 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
555 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000556 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000557 if (Invalid)
558 return StrTokSpellingLoc;
559
560 const char *StrData = Buffer.data()+LocInfo.second;
561
562 // Create a langops struct and enable trigraphs. This is sufficient for
563 // relexing tokens.
564 LangOptions LangOpts;
565 LangOpts.Trigraphs = true;
566
567 // Create a lexer starting at the beginning of this token.
568 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
569 Buffer.end());
570 Token TheTok;
571 TheLexer.LexFromRawLexer(TheTok);
572
573 // Use the StringLiteralParser to compute the length of the string in bytes.
574 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
575 unsigned TokNumBytes = SLP.GetStringLength();
576
577 // If the byte is in this token, return the location of the byte.
578 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000579 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000580 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
581
582 // Now that we know the offset of the token in the spelling, use the
583 // preprocessor to get the offset in the original source.
584 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
585 }
586
587 // Move to the next string token.
588 ++TokNo;
589 ByteNo -= TokNumBytes;
590 }
591}
592
593
594
Reid Spencer5f016e22007-07-11 17:01:13 +0000595/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
596/// corresponds to, e.g. "sizeof" or "[pre]++".
597const char *UnaryOperator::getOpcodeStr(Opcode Op) {
598 switch (Op) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000599 default: llvm_unreachable("Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000600 case UO_PostInc: return "++";
601 case UO_PostDec: return "--";
602 case UO_PreInc: return "++";
603 case UO_PreDec: return "--";
604 case UO_AddrOf: return "&";
605 case UO_Deref: return "*";
606 case UO_Plus: return "+";
607 case UO_Minus: return "-";
608 case UO_Not: return "~";
609 case UO_LNot: return "!";
610 case UO_Real: return "__real";
611 case UO_Imag: return "__imag";
612 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 }
614}
615
John McCall2de56d12010-08-25 11:45:40 +0000616UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000617UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
618 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000619 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000620 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
621 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
622 case OO_Amp: return UO_AddrOf;
623 case OO_Star: return UO_Deref;
624 case OO_Plus: return UO_Plus;
625 case OO_Minus: return UO_Minus;
626 case OO_Tilde: return UO_Not;
627 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000628 }
629}
630
631OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
632 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000633 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
634 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
635 case UO_AddrOf: return OO_Amp;
636 case UO_Deref: return OO_Star;
637 case UO_Plus: return OO_Plus;
638 case UO_Minus: return OO_Minus;
639 case UO_Not: return OO_Tilde;
640 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000641 default: return OO_None;
642 }
643}
644
645
Reid Spencer5f016e22007-07-11 17:01:13 +0000646//===----------------------------------------------------------------------===//
647// Postfix Operators.
648//===----------------------------------------------------------------------===//
649
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000650CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
651 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000652 SourceLocation rparenloc)
653 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000654 fn->isTypeDependent(),
655 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000656 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000657 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000658 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000660 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000661 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000662 for (unsigned i = 0; i != numargs; ++i) {
663 if (args[i]->isTypeDependent())
664 ExprBits.TypeDependent = true;
665 if (args[i]->isValueDependent())
666 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000667 if (args[i]->isInstantiationDependent())
668 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000669 if (args[i]->containsUnexpandedParameterPack())
670 ExprBits.ContainsUnexpandedParameterPack = true;
671
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000672 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000673 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000674
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000675 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000676 RParenLoc = rparenloc;
677}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000678
Ted Kremenek668bf912009-02-09 20:51:47 +0000679CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000680 QualType t, ExprValueKind VK, SourceLocation rparenloc)
681 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000682 fn->isTypeDependent(),
683 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000684 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000685 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000686 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000687
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000688 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000689 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000690 for (unsigned i = 0; i != numargs; ++i) {
691 if (args[i]->isTypeDependent())
692 ExprBits.TypeDependent = true;
693 if (args[i]->isValueDependent())
694 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000695 if (args[i]->isInstantiationDependent())
696 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000697 if (args[i]->containsUnexpandedParameterPack())
698 ExprBits.ContainsUnexpandedParameterPack = true;
699
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000700 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000701 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000702
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000703 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000704 RParenLoc = rparenloc;
705}
706
Mike Stump1eb44332009-09-09 15:08:12 +0000707CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
708 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000709 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000710 SubExprs = new (C) Stmt*[PREARGS_START];
711 CallExprBits.NumPreArgs = 0;
712}
713
714CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
715 EmptyShell Empty)
716 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
717 // FIXME: Why do we allocate this?
718 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
719 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000720}
721
Nuno Lopesd20254f2009-12-20 23:11:08 +0000722Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000723 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000724
725 while (SubstNonTypeTemplateParmExpr *NTTP
726 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
727 CEE = NTTP->getReplacement()->IgnoreParenCasts();
728 }
729
Sebastian Redl20012152010-09-10 20:55:30 +0000730 // If we're calling a dereference, look at the pointer instead.
731 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
732 if (BO->isPtrMemOp())
733 CEE = BO->getRHS()->IgnoreParenCasts();
734 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
735 if (UO->getOpcode() == UO_Deref)
736 CEE = UO->getSubExpr()->IgnoreParenCasts();
737 }
Chris Lattner6346f962009-07-17 15:46:27 +0000738 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000739 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000740 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
741 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000742
743 return 0;
744}
745
Nuno Lopesd20254f2009-12-20 23:11:08 +0000746FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000747 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000748}
749
Chris Lattnerd18b3292007-12-28 05:25:02 +0000750/// setNumArgs - This changes the number of arguments present in this call.
751/// Any orphaned expressions are deleted by this, and any new operands are set
752/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000753void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000754 // No change, just return.
755 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000756
Chris Lattnerd18b3292007-12-28 05:25:02 +0000757 // If shrinking # arguments, just delete the extras and forgot them.
758 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000759 this->NumArgs = NumArgs;
760 return;
761 }
762
763 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000764 unsigned NumPreArgs = getNumPreArgs();
765 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000766 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000767 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000768 NewSubExprs[i] = SubExprs[i];
769 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000770 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
771 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000772 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Douglas Gregor88c9a462009-04-17 21:46:47 +0000774 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000775 SubExprs = NewSubExprs;
776 this->NumArgs = NumArgs;
777}
778
Chris Lattnercb888962008-10-06 05:00:53 +0000779/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
780/// not, return 0.
Jay Foad4ba2a172011-01-12 09:06:06 +0000781unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000782 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000783 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000784 // ImplicitCastExpr.
785 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
786 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000787 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000788
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000789 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
790 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000791 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Anders Carlssonbcba2012008-01-31 02:13:57 +0000793 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
794 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000795 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000796
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000797 if (!FDecl->getIdentifier())
798 return 0;
799
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000800 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000801}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000802
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000803QualType CallExpr::getCallReturnType() const {
804 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000805 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000806 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000807 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000808 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000809 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
810 // This should never be overloaded and so should never return null.
811 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000812
John McCall864c0412011-04-26 20:42:42 +0000813 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000814 return FnType->getResultType();
815}
Chris Lattnercb888962008-10-06 05:00:53 +0000816
John McCall2882eca2011-02-21 06:23:05 +0000817SourceRange CallExpr::getSourceRange() const {
818 if (isa<CXXOperatorCallExpr>(this))
819 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
820
821 SourceLocation begin = getCallee()->getLocStart();
822 if (begin.isInvalid() && getNumArgs() > 0)
823 begin = getArg(0)->getLocStart();
824 SourceLocation end = getRParenLoc();
825 if (end.isInvalid() && getNumArgs() > 0)
826 end = getArg(getNumArgs() - 1)->getLocEnd();
827 return SourceRange(begin, end);
828}
829
Sean Huntc3021132010-05-05 15:23:54 +0000830OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000831 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000832 TypeSourceInfo *tsi,
833 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000834 Expr** exprsPtr, unsigned numExprs,
835 SourceLocation RParenLoc) {
836 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000837 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000838 sizeof(Expr*) * numExprs);
839
840 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
841 exprsPtr, numExprs, RParenLoc);
842}
843
844OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
845 unsigned numComps, unsigned numExprs) {
846 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
847 sizeof(OffsetOfNode) * numComps +
848 sizeof(Expr*) * numExprs);
849 return new (Mem) OffsetOfExpr(numComps, numExprs);
850}
851
Sean Huntc3021132010-05-05 15:23:54 +0000852OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000853 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000854 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000855 Expr** exprsPtr, unsigned numExprs,
856 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000857 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
858 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000859 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000860 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000861 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000862 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
863 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000864{
865 for(unsigned i = 0; i < numComps; ++i) {
866 setComponent(i, compsPtr[i]);
867 }
Sean Huntc3021132010-05-05 15:23:54 +0000868
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000869 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000870 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
871 ExprBits.ValueDependent = true;
872 if (exprsPtr[i]->containsUnexpandedParameterPack())
873 ExprBits.ContainsUnexpandedParameterPack = true;
874
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000875 setIndexExpr(i, exprsPtr[i]);
876 }
877}
878
879IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
880 assert(getKind() == Field || getKind() == Identifier);
881 if (getKind() == Field)
882 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000883
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000884 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
885}
886
Mike Stump1eb44332009-09-09 15:08:12 +0000887MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000888 NestedNameSpecifierLoc QualifierLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000889 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000890 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000891 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000892 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000893 QualType ty,
894 ExprValueKind vk,
895 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000896 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000897
Douglas Gregor40d96a62011-02-28 21:54:11 +0000898 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000899 founddecl.getDecl() != memberdecl ||
900 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000901 if (hasQualOrFound)
902 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000903
John McCalld5532b62009-11-23 01:53:49 +0000904 if (targs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000905 Size += ASTTemplateArgumentListInfo::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000906
Chris Lattner32488542010-10-30 05:14:06 +0000907 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000908 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
909 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000910
911 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000912 // FIXME: Wrong. We should be looking at the member declaration we found.
913 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000914 E->setValueDependent(true);
915 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +0000916 E->setInstantiationDependent(true);
917 }
918 else if (QualifierLoc &&
919 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
920 E->setInstantiationDependent(true);
921
John McCall6bb80172010-03-30 21:47:33 +0000922 E->HasQualifierOrFoundDecl = true;
923
924 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000925 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000926 NQ->FoundDecl = founddecl;
927 }
928
929 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000930 bool Dependent = false;
931 bool InstantiationDependent = false;
932 bool ContainsUnexpandedParameterPack = false;
John McCall6bb80172010-03-30 21:47:33 +0000933 E->HasExplicitTemplateArgumentList = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000934 E->getExplicitTemplateArgs().initializeFrom(*targs, Dependent,
935 InstantiationDependent,
936 ContainsUnexpandedParameterPack);
937 if (InstantiationDependent)
938 E->setInstantiationDependent(true);
John McCall6bb80172010-03-30 21:47:33 +0000939 }
940
941 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000942}
943
Douglas Gregor75e85042011-03-02 21:06:53 +0000944SourceRange MemberExpr::getSourceRange() const {
945 SourceLocation StartLoc;
946 if (isImplicitAccess()) {
947 if (hasQualifier())
948 StartLoc = getQualifierLoc().getBeginLoc();
949 else
950 StartLoc = MemberLoc;
951 } else {
952 // FIXME: We don't want this to happen. Rather, we should be able to
953 // detect all kinds of implicit accesses more cleanly.
954 StartLoc = getBase()->getLocStart();
955 if (StartLoc.isInvalid())
956 StartLoc = MemberLoc;
957 }
958
959 SourceLocation EndLoc =
960 HasExplicitTemplateArgumentList? getRAngleLoc()
961 : getMemberNameInfo().getEndLoc();
962
963 return SourceRange(StartLoc, EndLoc);
964}
965
John McCall1d9b3b22011-09-09 05:25:32 +0000966void CastExpr::CheckCastConsistency() const {
967 switch (getCastKind()) {
968 case CK_DerivedToBase:
969 case CK_UncheckedDerivedToBase:
970 case CK_DerivedToBaseMemberPointer:
971 case CK_BaseToDerived:
972 case CK_BaseToDerivedMemberPointer:
973 assert(!path_empty() && "Cast kind should have a base path!");
974 break;
975
976 case CK_CPointerToObjCPointerCast:
977 assert(getType()->isObjCObjectPointerType());
978 assert(getSubExpr()->getType()->isPointerType());
979 goto CheckNoBasePath;
980
981 case CK_BlockPointerToObjCPointerCast:
982 assert(getType()->isObjCObjectPointerType());
983 assert(getSubExpr()->getType()->isBlockPointerType());
984 goto CheckNoBasePath;
985
986 case CK_BitCast:
987 // Arbitrary casts to C pointer types count as bitcasts.
988 // Otherwise, we should only have block and ObjC pointer casts
989 // here if they stay within the type kind.
990 if (!getType()->isPointerType()) {
991 assert(getType()->isObjCObjectPointerType() ==
992 getSubExpr()->getType()->isObjCObjectPointerType());
993 assert(getType()->isBlockPointerType() ==
994 getSubExpr()->getType()->isBlockPointerType());
995 }
996 goto CheckNoBasePath;
997
998 case CK_AnyPointerToBlockPointerCast:
999 assert(getType()->isBlockPointerType());
1000 assert(getSubExpr()->getType()->isAnyPointerType() &&
1001 !getSubExpr()->getType()->isBlockPointerType());
1002 goto CheckNoBasePath;
1003
1004 // These should not have an inheritance path.
1005 case CK_Dynamic:
1006 case CK_ToUnion:
1007 case CK_ArrayToPointerDecay:
1008 case CK_FunctionToPointerDecay:
1009 case CK_NullToMemberPointer:
1010 case CK_NullToPointer:
1011 case CK_ConstructorConversion:
1012 case CK_IntegralToPointer:
1013 case CK_PointerToIntegral:
1014 case CK_ToVoid:
1015 case CK_VectorSplat:
1016 case CK_IntegralCast:
1017 case CK_IntegralToFloating:
1018 case CK_FloatingToIntegral:
1019 case CK_FloatingCast:
1020 case CK_ObjCObjectLValueCast:
1021 case CK_FloatingRealToComplex:
1022 case CK_FloatingComplexToReal:
1023 case CK_FloatingComplexCast:
1024 case CK_FloatingComplexToIntegralComplex:
1025 case CK_IntegralRealToComplex:
1026 case CK_IntegralComplexToReal:
1027 case CK_IntegralComplexCast:
1028 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001029 case CK_ARCProduceObject:
1030 case CK_ARCConsumeObject:
1031 case CK_ARCReclaimReturnedObject:
1032 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001033 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1034 goto CheckNoBasePath;
1035
1036 case CK_Dependent:
1037 case CK_LValueToRValue:
1038 case CK_GetObjCProperty:
1039 case CK_NoOp:
1040 case CK_PointerToBoolean:
1041 case CK_IntegralToBoolean:
1042 case CK_FloatingToBoolean:
1043 case CK_MemberPointerToBoolean:
1044 case CK_FloatingComplexToBoolean:
1045 case CK_IntegralComplexToBoolean:
1046 case CK_LValueBitCast: // -> bool&
1047 case CK_UserDefinedConversion: // operator bool()
1048 CheckNoBasePath:
1049 assert(path_empty() && "Cast kind should not have a base path!");
1050 break;
1051 }
1052}
1053
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001054const char *CastExpr::getCastKindName() const {
1055 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001056 case CK_Dependent:
1057 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001058 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001059 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001060 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001061 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001062 case CK_LValueToRValue:
1063 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +00001064 case CK_GetObjCProperty:
1065 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +00001066 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001067 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001068 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001069 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001070 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001071 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001072 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001073 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001074 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001075 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001076 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001077 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001078 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001079 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001080 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001081 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001082 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001083 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001084 case CK_NullToPointer:
1085 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001086 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001087 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001088 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001089 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001090 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001091 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001092 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001093 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001094 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001095 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001096 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001097 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001098 case CK_PointerToBoolean:
1099 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001100 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001101 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001102 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001103 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001104 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001105 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001106 case CK_IntegralToBoolean:
1107 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001108 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001109 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001110 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001111 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001112 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001113 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001114 case CK_FloatingToBoolean:
1115 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001116 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001117 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001118 case CK_CPointerToObjCPointerCast:
1119 return "CPointerToObjCPointerCast";
1120 case CK_BlockPointerToObjCPointerCast:
1121 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001122 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001123 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001124 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001125 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001126 case CK_FloatingRealToComplex:
1127 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001128 case CK_FloatingComplexToReal:
1129 return "FloatingComplexToReal";
1130 case CK_FloatingComplexToBoolean:
1131 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001132 case CK_FloatingComplexCast:
1133 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001134 case CK_FloatingComplexToIntegralComplex:
1135 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001136 case CK_IntegralRealToComplex:
1137 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001138 case CK_IntegralComplexToReal:
1139 return "IntegralComplexToReal";
1140 case CK_IntegralComplexToBoolean:
1141 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001142 case CK_IntegralComplexCast:
1143 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001144 case CK_IntegralComplexToFloatingComplex:
1145 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001146 case CK_ARCConsumeObject:
1147 return "ARCConsumeObject";
1148 case CK_ARCProduceObject:
1149 return "ARCProduceObject";
1150 case CK_ARCReclaimReturnedObject:
1151 return "ARCReclaimReturnedObject";
1152 case CK_ARCExtendBlockObject:
1153 return "ARCCExtendBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001154 }
Mike Stump1eb44332009-09-09 15:08:12 +00001155
John McCall2bb5d002010-11-13 09:02:35 +00001156 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001157 return 0;
1158}
1159
Douglas Gregor6eef5192009-12-14 19:27:10 +00001160Expr *CastExpr::getSubExprAsWritten() {
1161 Expr *SubExpr = 0;
1162 CastExpr *E = this;
1163 do {
1164 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001165
1166 // Skip through reference binding to temporary.
1167 if (MaterializeTemporaryExpr *Materialize
1168 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1169 SubExpr = Materialize->GetTemporaryExpr();
1170
Douglas Gregor6eef5192009-12-14 19:27:10 +00001171 // Skip any temporary bindings; they're implicit.
1172 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1173 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001174
Douglas Gregor6eef5192009-12-14 19:27:10 +00001175 // Conversions by constructor and conversion functions have a
1176 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001177 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001178 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001179 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001180 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001181
Douglas Gregor6eef5192009-12-14 19:27:10 +00001182 // If the subexpression we're left with is an implicit cast, look
1183 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001184 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1185
Douglas Gregor6eef5192009-12-14 19:27:10 +00001186 return SubExpr;
1187}
1188
John McCallf871d0c2010-08-07 06:22:56 +00001189CXXBaseSpecifier **CastExpr::path_buffer() {
1190 switch (getStmtClass()) {
1191#define ABSTRACT_STMT(x)
1192#define CASTEXPR(Type, Base) \
1193 case Stmt::Type##Class: \
1194 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1195#define STMT(Type, Base)
1196#include "clang/AST/StmtNodes.inc"
1197 default:
1198 llvm_unreachable("non-cast expressions not possible here");
1199 return 0;
1200 }
1201}
1202
1203void CastExpr::setCastPath(const CXXCastPath &Path) {
1204 assert(Path.size() == path_size());
1205 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1206}
1207
1208ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1209 CastKind Kind, Expr *Operand,
1210 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001211 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001212 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1213 void *Buffer =
1214 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1215 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001216 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001217 if (PathSize) E->setCastPath(*BasePath);
1218 return E;
1219}
1220
1221ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1222 unsigned PathSize) {
1223 void *Buffer =
1224 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1225 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1226}
1227
1228
1229CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001230 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001231 const CXXCastPath *BasePath,
1232 TypeSourceInfo *WrittenTy,
1233 SourceLocation L, SourceLocation R) {
1234 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1235 void *Buffer =
1236 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1237 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001238 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001239 if (PathSize) E->setCastPath(*BasePath);
1240 return E;
1241}
1242
1243CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1244 void *Buffer =
1245 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1246 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1247}
1248
Reid Spencer5f016e22007-07-11 17:01:13 +00001249/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1250/// corresponds to, e.g. "<<=".
1251const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1252 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001253 case BO_PtrMemD: return ".*";
1254 case BO_PtrMemI: return "->*";
1255 case BO_Mul: return "*";
1256 case BO_Div: return "/";
1257 case BO_Rem: return "%";
1258 case BO_Add: return "+";
1259 case BO_Sub: return "-";
1260 case BO_Shl: return "<<";
1261 case BO_Shr: return ">>";
1262 case BO_LT: return "<";
1263 case BO_GT: return ">";
1264 case BO_LE: return "<=";
1265 case BO_GE: return ">=";
1266 case BO_EQ: return "==";
1267 case BO_NE: return "!=";
1268 case BO_And: return "&";
1269 case BO_Xor: return "^";
1270 case BO_Or: return "|";
1271 case BO_LAnd: return "&&";
1272 case BO_LOr: return "||";
1273 case BO_Assign: return "=";
1274 case BO_MulAssign: return "*=";
1275 case BO_DivAssign: return "/=";
1276 case BO_RemAssign: return "%=";
1277 case BO_AddAssign: return "+=";
1278 case BO_SubAssign: return "-=";
1279 case BO_ShlAssign: return "<<=";
1280 case BO_ShrAssign: return ">>=";
1281 case BO_AndAssign: return "&=";
1282 case BO_XorAssign: return "^=";
1283 case BO_OrAssign: return "|=";
1284 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001286
1287 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001288}
1289
John McCall2de56d12010-08-25 11:45:40 +00001290BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001291BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1292 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001293 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001294 case OO_Plus: return BO_Add;
1295 case OO_Minus: return BO_Sub;
1296 case OO_Star: return BO_Mul;
1297 case OO_Slash: return BO_Div;
1298 case OO_Percent: return BO_Rem;
1299 case OO_Caret: return BO_Xor;
1300 case OO_Amp: return BO_And;
1301 case OO_Pipe: return BO_Or;
1302 case OO_Equal: return BO_Assign;
1303 case OO_Less: return BO_LT;
1304 case OO_Greater: return BO_GT;
1305 case OO_PlusEqual: return BO_AddAssign;
1306 case OO_MinusEqual: return BO_SubAssign;
1307 case OO_StarEqual: return BO_MulAssign;
1308 case OO_SlashEqual: return BO_DivAssign;
1309 case OO_PercentEqual: return BO_RemAssign;
1310 case OO_CaretEqual: return BO_XorAssign;
1311 case OO_AmpEqual: return BO_AndAssign;
1312 case OO_PipeEqual: return BO_OrAssign;
1313 case OO_LessLess: return BO_Shl;
1314 case OO_GreaterGreater: return BO_Shr;
1315 case OO_LessLessEqual: return BO_ShlAssign;
1316 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1317 case OO_EqualEqual: return BO_EQ;
1318 case OO_ExclaimEqual: return BO_NE;
1319 case OO_LessEqual: return BO_LE;
1320 case OO_GreaterEqual: return BO_GE;
1321 case OO_AmpAmp: return BO_LAnd;
1322 case OO_PipePipe: return BO_LOr;
1323 case OO_Comma: return BO_Comma;
1324 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001325 }
1326}
1327
1328OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1329 static const OverloadedOperatorKind OverOps[] = {
1330 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1331 OO_Star, OO_Slash, OO_Percent,
1332 OO_Plus, OO_Minus,
1333 OO_LessLess, OO_GreaterGreater,
1334 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1335 OO_EqualEqual, OO_ExclaimEqual,
1336 OO_Amp,
1337 OO_Caret,
1338 OO_Pipe,
1339 OO_AmpAmp,
1340 OO_PipePipe,
1341 OO_Equal, OO_StarEqual,
1342 OO_SlashEqual, OO_PercentEqual,
1343 OO_PlusEqual, OO_MinusEqual,
1344 OO_LessLessEqual, OO_GreaterGreaterEqual,
1345 OO_AmpEqual, OO_CaretEqual,
1346 OO_PipeEqual,
1347 OO_Comma
1348 };
1349 return OverOps[Opc];
1350}
1351
Ted Kremenek709210f2010-04-13 23:39:13 +00001352InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001353 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001354 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001355 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001356 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001357 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001358 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +00001359 HadArrayRangeDesignator(false)
Sean Huntc3021132010-05-05 15:23:54 +00001360{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001361 for (unsigned I = 0; I != numInits; ++I) {
1362 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001363 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001364 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001365 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001366 if (initExprs[I]->isInstantiationDependent())
1367 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001368 if (initExprs[I]->containsUnexpandedParameterPack())
1369 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001370 }
Sean Huntc3021132010-05-05 15:23:54 +00001371
Ted Kremenek709210f2010-04-13 23:39:13 +00001372 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001373}
Reid Spencer5f016e22007-07-11 17:01:13 +00001374
Ted Kremenek709210f2010-04-13 23:39:13 +00001375void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001376 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001377 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001378}
1379
Ted Kremenek709210f2010-04-13 23:39:13 +00001380void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001381 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001382}
1383
Ted Kremenek709210f2010-04-13 23:39:13 +00001384Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001385 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001386 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001387 InitExprs.back() = expr;
1388 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001389 }
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Douglas Gregor4c678342009-01-28 21:54:33 +00001391 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1392 InitExprs[Init] = expr;
1393 return Result;
1394}
1395
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001396void InitListExpr::setArrayFiller(Expr *filler) {
1397 ArrayFillerOrUnionFieldInit = filler;
1398 // Fill out any "holes" in the array due to designated initializers.
1399 Expr **inits = getInits();
1400 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1401 if (inits[i] == 0)
1402 inits[i] = filler;
1403}
1404
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001405SourceRange InitListExpr::getSourceRange() const {
1406 if (SyntacticForm)
1407 return SyntacticForm->getSourceRange();
1408 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1409 if (Beg.isInvalid()) {
1410 // Find the first non-null initializer.
1411 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1412 E = InitExprs.end();
1413 I != E; ++I) {
1414 if (Stmt *S = *I) {
1415 Beg = S->getLocStart();
1416 break;
1417 }
1418 }
1419 }
1420 if (End.isInvalid()) {
1421 // Find the first non-null initializer from the end.
1422 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1423 E = InitExprs.rend();
1424 I != E; ++I) {
1425 if (Stmt *S = *I) {
1426 End = S->getSourceRange().getEnd();
1427 break;
1428 }
1429 }
1430 }
1431 return SourceRange(Beg, End);
1432}
1433
Steve Naroffbfdcae62008-09-04 15:31:07 +00001434/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001435///
1436const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001437 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001438 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001439}
1440
Mike Stump1eb44332009-09-09 15:08:12 +00001441SourceLocation BlockExpr::getCaretLocation() const {
1442 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001443}
Mike Stump1eb44332009-09-09 15:08:12 +00001444const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001445 return TheBlock->getBody();
1446}
Mike Stump1eb44332009-09-09 15:08:12 +00001447Stmt *BlockExpr::getBody() {
1448 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001449}
Steve Naroff56ee6892008-10-08 17:01:13 +00001450
1451
Reid Spencer5f016e22007-07-11 17:01:13 +00001452//===----------------------------------------------------------------------===//
1453// Generic Expression Routines
1454//===----------------------------------------------------------------------===//
1455
Chris Lattner026dc962009-02-14 07:37:35 +00001456/// isUnusedResultAWarning - Return true if this immediate expression should
1457/// be warned about if the result is unused. If so, fill in Loc and Ranges
1458/// with location to warn on and the source range[s] to report with the
1459/// warning.
1460bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001461 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001462 // Don't warn if the expr is type dependent. The type could end up
1463 // instantiating to void.
1464 if (isTypeDependent())
1465 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001466
Reid Spencer5f016e22007-07-11 17:01:13 +00001467 switch (getStmtClass()) {
1468 default:
John McCall0faede62010-03-12 07:11:26 +00001469 if (getType()->isVoidType())
1470 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001471 Loc = getExprLoc();
1472 R1 = getSourceRange();
1473 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001474 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001475 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001476 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001477 case GenericSelectionExprClass:
1478 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1479 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001480 case UnaryOperatorClass: {
1481 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001482
Reid Spencer5f016e22007-07-11 17:01:13 +00001483 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001484 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001485 case UO_PostInc:
1486 case UO_PostDec:
1487 case UO_PreInc:
1488 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001489 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001490 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001492 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001493 return false;
1494 break;
John McCall2de56d12010-08-25 11:45:40 +00001495 case UO_Real:
1496 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001497 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001498 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1499 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001500 return false;
1501 break;
John McCall2de56d12010-08-25 11:45:40 +00001502 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001503 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001504 }
Chris Lattner026dc962009-02-14 07:37:35 +00001505 Loc = UO->getOperatorLoc();
1506 R1 = UO->getSubExpr()->getSourceRange();
1507 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001509 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001510 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001511 switch (BO->getOpcode()) {
1512 default:
1513 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001514 // Consider the RHS of comma for side effects. LHS was checked by
1515 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001516 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001517 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1518 // lvalue-ness) of an assignment written in a macro.
1519 if (IntegerLiteral *IE =
1520 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1521 if (IE->getValue() == 0)
1522 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001523 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1524 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001525 case BO_LAnd:
1526 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001527 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1528 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1529 return false;
1530 break;
John McCallbf0ee352010-02-16 04:10:53 +00001531 }
Chris Lattner026dc962009-02-14 07:37:35 +00001532 if (BO->isAssignmentOp())
1533 return false;
1534 Loc = BO->getOperatorLoc();
1535 R1 = BO->getLHS()->getSourceRange();
1536 R2 = BO->getRHS()->getSourceRange();
1537 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001538 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001539 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001540 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001541 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001542 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001543
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001544 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001545 // If only one of the LHS or RHS is a warning, the operator might
1546 // be being used for control flow. Only warn if both the LHS and
1547 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001548 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001549 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1550 return false;
1551 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001552 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001553 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001554 }
1555
Reid Spencer5f016e22007-07-11 17:01:13 +00001556 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001557 // If the base pointer or element is to a volatile pointer/field, accessing
1558 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001559 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001560 return false;
1561 Loc = cast<MemberExpr>(this)->getMemberLoc();
1562 R1 = SourceRange(Loc, Loc);
1563 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1564 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Reid Spencer5f016e22007-07-11 17:01:13 +00001566 case ArraySubscriptExprClass:
1567 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001568 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001569 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001570 return false;
1571 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1572 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1573 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1574 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001575
Chandler Carruth9b106832011-08-17 09:49:44 +00001576 case CXXOperatorCallExprClass: {
1577 // We warn about operator== and operator!= even when user-defined operator
1578 // overloads as there is no reasonable way to define these such that they
1579 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1580 // warning: these operators are commonly typo'ed, and so warning on them
1581 // provides additional value as well. If this list is updated,
1582 // DiagnoseUnusedComparison should be as well.
1583 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1584 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001585 Op->getOperator() == OO_ExclaimEqual) {
1586 Loc = Op->getOperatorLoc();
1587 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001588 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001589 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001590
1591 // Fallthrough for generic call handling.
1592 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001593 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001594 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001595 // If this is a direct call, get the callee.
1596 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001597 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001598 // If the callee has attribute pure, const, or warn_unused_result, warn
1599 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001600 //
1601 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1602 // updated to match for QoI.
1603 if (FD->getAttr<WarnUnusedResultAttr>() ||
1604 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1605 Loc = CE->getCallee()->getLocStart();
1606 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001607
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001608 if (unsigned NumArgs = CE->getNumArgs())
1609 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1610 CE->getArg(NumArgs-1)->getLocEnd());
1611 return true;
1612 }
Chris Lattner026dc962009-02-14 07:37:35 +00001613 }
1614 return false;
1615 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001616
1617 case CXXTemporaryObjectExprClass:
1618 case CXXConstructExprClass:
1619 return false;
1620
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001621 case ObjCMessageExprClass: {
1622 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001623 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1624 ME->isInstanceMessage() &&
1625 !ME->getType()->isVoidType() &&
1626 ME->getSelector().getIdentifierInfoForSlot(0) &&
1627 ME->getSelector().getIdentifierInfoForSlot(0)
1628 ->getName().startswith("init")) {
1629 Loc = getExprLoc();
1630 R1 = ME->getSourceRange();
1631 return true;
1632 }
1633
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001634 const ObjCMethodDecl *MD = ME->getMethodDecl();
1635 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1636 Loc = getExprLoc();
1637 return true;
1638 }
Chris Lattner026dc962009-02-14 07:37:35 +00001639 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001640 }
Mike Stump1eb44332009-09-09 15:08:12 +00001641
John McCall12f78a62010-12-02 01:19:52 +00001642 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001643 Loc = getExprLoc();
1644 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001645 return true;
John McCall12f78a62010-12-02 01:19:52 +00001646
Chris Lattner611b2ec2008-07-26 19:51:01 +00001647 case StmtExprClass: {
1648 // Statement exprs don't logically have side effects themselves, but are
1649 // sometimes used in macros in ways that give them a type that is unused.
1650 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1651 // however, if the result of the stmt expr is dead, we don't want to emit a
1652 // warning.
1653 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001654 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001655 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001656 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001657 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1658 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1659 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1660 }
Mike Stump1eb44332009-09-09 15:08:12 +00001661
John McCall0faede62010-03-12 07:11:26 +00001662 if (getType()->isVoidType())
1663 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001664 Loc = cast<StmtExpr>(this)->getLParenLoc();
1665 R1 = getSourceRange();
1666 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001667 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001668 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001669 // If this is an explicit cast to void, allow it. People do this when they
1670 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001671 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001672 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001673 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1674 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1675 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001676 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001677 if (getType()->isVoidType())
1678 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001679 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001680
Anders Carlsson58beed92009-11-17 17:11:23 +00001681 // If this is a cast to void or a constructor conversion, check the operand.
1682 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001683 if (CE->getCastKind() == CK_ToVoid ||
1684 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001685 return (cast<CastExpr>(this)->getSubExpr()
1686 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001687 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1688 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1689 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001690 }
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Eli Friedman4be1f472008-05-19 21:24:43 +00001692 case ImplicitCastExprClass:
1693 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001694 return (cast<ImplicitCastExpr>(this)
1695 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001696
Chris Lattner04421082008-04-08 04:40:51 +00001697 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001698 return (cast<CXXDefaultArgExpr>(this)
1699 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001700
1701 case CXXNewExprClass:
1702 // FIXME: In theory, there might be new expressions that don't have side
1703 // effects (e.g. a placement new with an uninitialized POD).
1704 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001705 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001706 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001707 return (cast<CXXBindTemporaryExpr>(this)
1708 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001709 case ExprWithCleanupsClass:
1710 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001711 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001712 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001713}
1714
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001715/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001716/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001717bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001718 const Expr *E = IgnoreParens();
1719 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001720 default:
1721 return false;
1722 case ObjCIvarRefExprClass:
1723 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001724 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001725 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001726 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001727 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001728 case MaterializeTemporaryExprClass:
1729 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1730 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001731 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001732 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001733 case BlockDeclRefExprClass:
Douglas Gregora2813ce2009-10-23 18:54:35 +00001734 case DeclRefExprClass: {
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001735
1736 const Decl *D;
1737 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1738 D = BDRE->getDecl();
1739 else
1740 D = cast<DeclRefExpr>(E)->getDecl();
1741
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001742 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1743 if (VD->hasGlobalStorage())
1744 return true;
1745 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001746 // dereferencing to a pointer is always a gc'able candidate,
1747 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001748 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001749 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001750 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001751 return false;
1752 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001753 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001754 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001755 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001756 }
1757 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001758 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001759 }
1760}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001761
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001762bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1763 if (isTypeDependent())
1764 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001765 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001766}
1767
John McCall864c0412011-04-26 20:42:42 +00001768QualType Expr::findBoundMemberType(const Expr *expr) {
1769 assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1770
1771 // Bound member expressions are always one of these possibilities:
1772 // x->m x.m x->*y x.*y
1773 // (possibly parenthesized)
1774
1775 expr = expr->IgnoreParens();
1776 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1777 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1778 return mem->getMemberDecl()->getType();
1779 }
1780
1781 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1782 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1783 ->getPointeeType();
1784 assert(type->isFunctionType());
1785 return type;
1786 }
1787
1788 assert(isa<UnresolvedMemberExpr>(expr));
1789 return QualType();
1790}
1791
Sebastian Redl369e51f2010-09-10 20:55:33 +00001792static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1793 Expr::CanThrowResult CT2) {
1794 // CanThrowResult constants are ordered so that the maximum is the correct
1795 // merge result.
1796 return CT1 > CT2 ? CT1 : CT2;
1797}
1798
1799static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1800 Expr *E = const_cast<Expr*>(CE);
1801 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001802 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001803 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1804 }
1805 return R;
1806}
1807
Richard Smith7a614d82011-06-11 17:19:42 +00001808static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1809 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001810 bool NullThrows = true) {
1811 if (!D)
1812 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1813
1814 // See if we can get a function type from the decl somehow.
1815 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1816 if (!VD) // If we have no clue what we're calling, assume the worst.
1817 return Expr::CT_Can;
1818
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001819 // As an extension, we assume that __attribute__((nothrow)) functions don't
1820 // throw.
1821 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1822 return Expr::CT_Cannot;
1823
Sebastian Redl369e51f2010-09-10 20:55:33 +00001824 QualType T = VD->getType();
1825 const FunctionProtoType *FT;
1826 if ((FT = T->getAs<FunctionProtoType>())) {
1827 } else if (const PointerType *PT = T->getAs<PointerType>())
1828 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1829 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1830 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1831 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1832 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1833 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1834 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1835
1836 if (!FT)
1837 return Expr::CT_Can;
1838
Richard Smith7a614d82011-06-11 17:19:42 +00001839 if (FT->getExceptionSpecType() == EST_Delayed) {
1840 assert(isa<CXXConstructorDecl>(D) &&
1841 "only constructor exception specs can be unknown");
1842 Ctx.getDiagnostics().Report(E->getLocStart(),
1843 diag::err_exception_spec_unknown)
1844 << E->getSourceRange();
1845 return Expr::CT_Can;
1846 }
1847
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001848 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001849}
1850
1851static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1852 if (DC->isTypeDependent())
1853 return Expr::CT_Dependent;
1854
Sebastian Redl295995c2010-09-10 20:55:47 +00001855 if (!DC->getTypeAsWritten()->isReferenceType())
1856 return Expr::CT_Cannot;
1857
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001858 if (DC->getSubExpr()->isTypeDependent())
1859 return Expr::CT_Dependent;
1860
Sebastian Redl369e51f2010-09-10 20:55:33 +00001861 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1862}
1863
1864static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1865 const CXXTypeidExpr *DC) {
1866 if (DC->isTypeOperand())
1867 return Expr::CT_Cannot;
1868
1869 Expr *Op = DC->getExprOperand();
1870 if (Op->isTypeDependent())
1871 return Expr::CT_Dependent;
1872
1873 const RecordType *RT = Op->getType()->getAs<RecordType>();
1874 if (!RT)
1875 return Expr::CT_Cannot;
1876
1877 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1878 return Expr::CT_Cannot;
1879
1880 if (Op->Classify(C).isPRValue())
1881 return Expr::CT_Cannot;
1882
1883 return Expr::CT_Can;
1884}
1885
1886Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1887 // C++ [expr.unary.noexcept]p3:
1888 // [Can throw] if in a potentially-evaluated context the expression would
1889 // contain:
1890 switch (getStmtClass()) {
1891 case CXXThrowExprClass:
1892 // - a potentially evaluated throw-expression
1893 return CT_Can;
1894
1895 case CXXDynamicCastExprClass: {
1896 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1897 // where T is a reference type, that requires a run-time check
1898 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1899 if (CT == CT_Can)
1900 return CT;
1901 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1902 }
1903
1904 case CXXTypeidExprClass:
1905 // - a potentially evaluated typeid expression applied to a glvalue
1906 // expression whose type is a polymorphic class type
1907 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1908
1909 // - a potentially evaluated call to a function, member function, function
1910 // pointer, or member function pointer that does not have a non-throwing
1911 // exception-specification
1912 case CallExprClass:
1913 case CXXOperatorCallExprClass:
1914 case CXXMemberCallExprClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001915 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001916 CanThrowResult CT;
1917 if (isTypeDependent())
1918 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001919 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1920 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001921 else
Richard Smith7a614d82011-06-11 17:19:42 +00001922 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001923 if (CT == CT_Can)
1924 return CT;
1925 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1926 }
1927
Sebastian Redl295995c2010-09-10 20:55:47 +00001928 case CXXConstructExprClass:
1929 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00001930 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001931 cast<CXXConstructExpr>(this)->getConstructor());
1932 if (CT == CT_Can)
1933 return CT;
1934 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1935 }
1936
1937 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001938 CanThrowResult CT;
1939 if (isTypeDependent())
1940 CT = CT_Dependent;
1941 else
1942 CT = MergeCanThrow(
Richard Smith7a614d82011-06-11 17:19:42 +00001943 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
1944 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00001945 /*NullThrows*/false));
1946 if (CT == CT_Can)
1947 return CT;
1948 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1949 }
1950
1951 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001952 CanThrowResult CT;
1953 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
1954 if (DTy.isNull() || DTy->isDependentType()) {
1955 CT = CT_Dependent;
1956 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00001957 CT = CanCalleeThrow(C, this,
1958 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001959 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1960 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00001961 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001962 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001963 if (CT == CT_Can)
1964 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001965 }
1966 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1967 }
1968
1969 case CXXBindTemporaryExprClass: {
1970 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00001971 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001972 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1973 if (CT == CT_Can)
1974 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001975 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1976 }
1977
1978 // ObjC message sends are like function calls, but never have exception
1979 // specs.
1980 case ObjCMessageExprClass:
1981 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001982 return CT_Can;
1983
1984 // Many other things have subexpressions, so we have to test those.
1985 // Some are simple:
1986 case ParenExprClass:
1987 case MemberExprClass:
1988 case CXXReinterpretCastExprClass:
1989 case CXXConstCastExprClass:
1990 case ConditionalOperatorClass:
1991 case CompoundLiteralExprClass:
1992 case ExtVectorElementExprClass:
1993 case InitListExprClass:
1994 case DesignatedInitExprClass:
1995 case ParenListExprClass:
1996 case VAArgExprClass:
1997 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00001998 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001999 case ObjCIvarRefExprClass:
2000 case ObjCIsaExprClass:
2001 case ShuffleVectorExprClass:
2002 return CanSubExprsThrow(C, this);
2003
2004 // Some might be dependent for other reasons.
2005 case UnaryOperatorClass:
2006 case ArraySubscriptExprClass:
2007 case ImplicitCastExprClass:
2008 case CStyleCastExprClass:
2009 case CXXStaticCastExprClass:
2010 case CXXFunctionalCastExprClass:
2011 case BinaryOperatorClass:
Douglas Gregor03e80032011-06-21 17:03:29 +00002012 case CompoundAssignOperatorClass:
2013 case MaterializeTemporaryExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002014 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2015 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2016 }
2017
2018 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2019 case StmtExprClass:
2020 return CT_Can;
2021
2022 case ChooseExprClass:
2023 if (isTypeDependent() || isValueDependent())
2024 return CT_Dependent;
2025 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2026
Peter Collingbournef111d932011-04-15 00:35:48 +00002027 case GenericSelectionExprClass:
2028 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2029 return CT_Dependent;
2030 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2031
Sebastian Redl369e51f2010-09-10 20:55:33 +00002032 // Some expressions are always dependent.
2033 case DependentScopeDeclRefExprClass:
2034 case CXXUnresolvedConstructExprClass:
2035 case CXXDependentScopeMemberExprClass:
2036 return CT_Dependent;
2037
2038 default:
2039 // All other expressions don't have subexpressions, or else they are
2040 // unevaluated.
2041 return CT_Cannot;
2042 }
2043}
2044
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002045Expr* Expr::IgnoreParens() {
2046 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002047 while (true) {
2048 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2049 E = P->getSubExpr();
2050 continue;
2051 }
2052 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2053 if (P->getOpcode() == UO_Extension) {
2054 E = P->getSubExpr();
2055 continue;
2056 }
2057 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002058 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2059 if (!P->isResultDependent()) {
2060 E = P->getResultExpr();
2061 continue;
2062 }
2063 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002064 return E;
2065 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002066}
2067
Chris Lattner56f34942008-02-13 01:02:39 +00002068/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2069/// or CastExprs or ImplicitCastExprs, returning their operand.
2070Expr *Expr::IgnoreParenCasts() {
2071 Expr *E = this;
2072 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002073 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002074 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002075 continue;
2076 }
2077 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002078 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002079 continue;
2080 }
2081 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2082 if (P->getOpcode() == UO_Extension) {
2083 E = P->getSubExpr();
2084 continue;
2085 }
2086 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002087 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2088 if (!P->isResultDependent()) {
2089 E = P->getResultExpr();
2090 continue;
2091 }
2092 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002093 if (MaterializeTemporaryExpr *Materialize
2094 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2095 E = Materialize->GetTemporaryExpr();
2096 continue;
2097 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002098 if (SubstNonTypeTemplateParmExpr *NTTP
2099 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2100 E = NTTP->getReplacement();
2101 continue;
2102 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002103 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002104 }
2105}
2106
John McCall9c5d70c2010-12-04 08:24:19 +00002107/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2108/// casts. This is intended purely as a temporary workaround for code
2109/// that hasn't yet been rewritten to do the right thing about those
2110/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002111Expr *Expr::IgnoreParenLValueCasts() {
2112 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002113 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002114 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2115 E = P->getSubExpr();
2116 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002117 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002118 if (P->getCastKind() == CK_LValueToRValue) {
2119 E = P->getSubExpr();
2120 continue;
2121 }
John McCall9c5d70c2010-12-04 08:24:19 +00002122 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2123 if (P->getOpcode() == UO_Extension) {
2124 E = P->getSubExpr();
2125 continue;
2126 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002127 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2128 if (!P->isResultDependent()) {
2129 E = P->getResultExpr();
2130 continue;
2131 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002132 } else if (MaterializeTemporaryExpr *Materialize
2133 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2134 E = Materialize->GetTemporaryExpr();
2135 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002136 } else if (SubstNonTypeTemplateParmExpr *NTTP
2137 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2138 E = NTTP->getReplacement();
2139 continue;
John McCallf6a16482010-12-04 03:47:34 +00002140 }
2141 break;
2142 }
2143 return E;
2144}
2145
John McCall2fc46bf2010-05-05 22:59:52 +00002146Expr *Expr::IgnoreParenImpCasts() {
2147 Expr *E = this;
2148 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002149 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002150 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002151 continue;
2152 }
2153 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002154 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002155 continue;
2156 }
2157 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2158 if (P->getOpcode() == UO_Extension) {
2159 E = P->getSubExpr();
2160 continue;
2161 }
2162 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002163 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2164 if (!P->isResultDependent()) {
2165 E = P->getResultExpr();
2166 continue;
2167 }
2168 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002169 if (MaterializeTemporaryExpr *Materialize
2170 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2171 E = Materialize->GetTemporaryExpr();
2172 continue;
2173 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002174 if (SubstNonTypeTemplateParmExpr *NTTP
2175 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2176 E = NTTP->getReplacement();
2177 continue;
2178 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002179 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002180 }
2181}
2182
Hans Wennborg2f072b42011-06-09 17:06:51 +00002183Expr *Expr::IgnoreConversionOperator() {
2184 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002185 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002186 return MCE->getImplicitObjectArgument();
2187 }
2188 return this;
2189}
2190
Chris Lattnerecdd8412009-03-13 17:28:01 +00002191/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2192/// value (including ptr->int casts of the same size). Strip off any
2193/// ParenExpr or CastExprs, returning their operand.
2194Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2195 Expr *E = this;
2196 while (true) {
2197 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2198 E = P->getSubExpr();
2199 continue;
2200 }
Mike Stump1eb44332009-09-09 15:08:12 +00002201
Chris Lattnerecdd8412009-03-13 17:28:01 +00002202 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2203 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002204 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002205 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002206
Chris Lattnerecdd8412009-03-13 17:28:01 +00002207 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2208 E = SE;
2209 continue;
2210 }
Mike Stump1eb44332009-09-09 15:08:12 +00002211
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002212 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002213 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002214 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002215 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002216 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2217 E = SE;
2218 continue;
2219 }
2220 }
Mike Stump1eb44332009-09-09 15:08:12 +00002221
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002222 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2223 if (P->getOpcode() == UO_Extension) {
2224 E = P->getSubExpr();
2225 continue;
2226 }
2227 }
2228
Peter Collingbournef111d932011-04-15 00:35:48 +00002229 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2230 if (!P->isResultDependent()) {
2231 E = P->getResultExpr();
2232 continue;
2233 }
2234 }
2235
Douglas Gregorc0244c52011-09-08 17:56:33 +00002236 if (SubstNonTypeTemplateParmExpr *NTTP
2237 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2238 E = NTTP->getReplacement();
2239 continue;
2240 }
2241
Chris Lattnerecdd8412009-03-13 17:28:01 +00002242 return E;
2243 }
2244}
2245
Douglas Gregor6eef5192009-12-14 19:27:10 +00002246bool Expr::isDefaultArgument() const {
2247 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002248 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2249 E = M->GetTemporaryExpr();
2250
Douglas Gregor6eef5192009-12-14 19:27:10 +00002251 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2252 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002253
Douglas Gregor6eef5192009-12-14 19:27:10 +00002254 return isa<CXXDefaultArgExpr>(E);
2255}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002256
Douglas Gregor2f599792010-04-02 18:24:57 +00002257/// \brief Skip over any no-op casts and any temporary-binding
2258/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002259static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002260 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2261 E = M->GetTemporaryExpr();
2262
Douglas Gregor2f599792010-04-02 18:24:57 +00002263 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002264 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002265 E = ICE->getSubExpr();
2266 else
2267 break;
2268 }
2269
2270 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2271 E = BE->getSubExpr();
2272
2273 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002274 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002275 E = ICE->getSubExpr();
2276 else
2277 break;
2278 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002279
2280 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002281}
2282
John McCall558d2ab2010-09-15 10:14:12 +00002283/// isTemporaryObject - Determines if this expression produces a
2284/// temporary of the given class type.
2285bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2286 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2287 return false;
2288
Anders Carlssonf8b30152010-11-28 16:40:49 +00002289 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002290
John McCall58277b52010-09-15 20:59:13 +00002291 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002292 if (!E->Classify(C).isPRValue()) {
2293 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002294 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002295 return false;
2296 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002297
John McCall19e60ad2010-09-16 06:57:56 +00002298 // Black-list a few cases which yield pr-values of class type that don't
2299 // refer to temporaries of that type:
2300
2301 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002302 if (isa<ImplicitCastExpr>(E)) {
2303 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2304 case CK_DerivedToBase:
2305 case CK_UncheckedDerivedToBase:
2306 return false;
2307 default:
2308 break;
2309 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002310 }
2311
John McCall19e60ad2010-09-16 06:57:56 +00002312 // - member expressions (all)
2313 if (isa<MemberExpr>(E))
2314 return false;
2315
John McCall56ca35d2011-02-17 10:25:35 +00002316 // - opaque values (all)
2317 if (isa<OpaqueValueExpr>(E))
2318 return false;
2319
John McCall558d2ab2010-09-15 10:14:12 +00002320 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002321}
2322
Douglas Gregor75e85042011-03-02 21:06:53 +00002323bool Expr::isImplicitCXXThis() const {
2324 const Expr *E = this;
2325
2326 // Strip away parentheses and casts we don't care about.
2327 while (true) {
2328 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2329 E = Paren->getSubExpr();
2330 continue;
2331 }
2332
2333 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2334 if (ICE->getCastKind() == CK_NoOp ||
2335 ICE->getCastKind() == CK_LValueToRValue ||
2336 ICE->getCastKind() == CK_DerivedToBase ||
2337 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2338 E = ICE->getSubExpr();
2339 continue;
2340 }
2341 }
2342
2343 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2344 if (UnOp->getOpcode() == UO_Extension) {
2345 E = UnOp->getSubExpr();
2346 continue;
2347 }
2348 }
2349
Douglas Gregor03e80032011-06-21 17:03:29 +00002350 if (const MaterializeTemporaryExpr *M
2351 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2352 E = M->GetTemporaryExpr();
2353 continue;
2354 }
2355
Douglas Gregor75e85042011-03-02 21:06:53 +00002356 break;
2357 }
2358
2359 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2360 return This->isImplicit();
2361
2362 return false;
2363}
2364
Douglas Gregor898574e2008-12-05 23:32:09 +00002365/// hasAnyTypeDependentArguments - Determines if any of the expressions
2366/// in Exprs is type-dependent.
2367bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2368 for (unsigned I = 0; I < NumExprs; ++I)
2369 if (Exprs[I]->isTypeDependent())
2370 return true;
2371
2372 return false;
2373}
2374
2375/// hasAnyValueDependentArguments - Determines if any of the expressions
2376/// in Exprs is value-dependent.
2377bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2378 for (unsigned I = 0; I < NumExprs; ++I)
2379 if (Exprs[I]->isValueDependent())
2380 return true;
2381
2382 return false;
2383}
2384
John McCall4204f072010-08-02 21:13:48 +00002385bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002386 // This function is attempting whether an expression is an initializer
2387 // which can be evaluated at compile-time. isEvaluatable handles most
2388 // of the cases, but it can't deal with some initializer-specific
2389 // expressions, and it can't deal with aggregates; we deal with those here,
2390 // and fall back to isEvaluatable for the other cases.
2391
John McCall4204f072010-08-02 21:13:48 +00002392 // If we ever capture reference-binding directly in the AST, we can
2393 // kill the second parameter.
2394
2395 if (IsForRef) {
2396 EvalResult Result;
2397 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2398 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002399
Anders Carlssone8a32b82008-11-24 05:23:59 +00002400 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002401 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002402 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002403 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002404 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002405 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002406 case CXXTemporaryObjectExprClass:
2407 case CXXConstructExprClass: {
2408 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002409
2410 // Only if it's
2411 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002412 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002413 if (!CE->getNumArgs()) return true;
2414
2415 // 2) an elidable trivial copy construction of an operand which is
2416 // itself a constant initializer. Note that we consider the
2417 // operand on its own, *not* as a reference binding.
2418 return CE->isElidable() &&
2419 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002420 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002421 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002422 // This handles gcc's extension that allows global initializers like
2423 // "struct x {int x;} x = (struct x) {};".
2424 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002425 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002426 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002427 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002428 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002429 // FIXME: This doesn't deal with fields with reference types correctly.
2430 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2431 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002432 const InitListExpr *Exp = cast<InitListExpr>(this);
2433 unsigned numInits = Exp->getNumInits();
2434 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002435 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002436 return false;
2437 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002438 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002439 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002440 case ImplicitValueInitExprClass:
2441 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002442 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002443 return cast<ParenExpr>(this)->getSubExpr()
2444 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002445 case GenericSelectionExprClass:
2446 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2447 return false;
2448 return cast<GenericSelectionExpr>(this)->getResultExpr()
2449 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002450 case ChooseExprClass:
2451 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2452 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002453 case UnaryOperatorClass: {
2454 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002455 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002456 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002457 break;
2458 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002459 case BinaryOperatorClass: {
2460 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2461 // but this handles the common case.
2462 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002463 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002464 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2465 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2466 return true;
2467 break;
2468 }
John McCall4204f072010-08-02 21:13:48 +00002469 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002470 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002471 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002472 case CStyleCastExprClass:
2473 // Handle casts with a destination that's a struct or union; this
2474 // deals with both the gcc no-op struct cast extension and the
2475 // cast-to-union extension.
2476 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002477 return cast<CastExpr>(this)->getSubExpr()
2478 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002479
Chris Lattner430656e2009-10-13 22:12:09 +00002480 // Integer->integer casts can be handled here, which is important for
2481 // things like (int)(&&x-&&y). Scary but true.
2482 if (getType()->isIntegerType() &&
2483 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002484 return cast<CastExpr>(this)->getSubExpr()
2485 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002486
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002487 break;
Douglas Gregor03e80032011-06-21 17:03:29 +00002488
2489 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002490 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002491 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002492 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002493 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002494}
2495
Chandler Carruth82214a82011-02-18 23:54:50 +00002496/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2497/// pointer constant or not, as well as the specific kind of constant detected.
2498/// Null pointer constants can be integer constant expressions with the
2499/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2500/// (a GNU extension).
2501Expr::NullPointerConstantKind
2502Expr::isNullPointerConstant(ASTContext &Ctx,
2503 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002504 if (isValueDependent()) {
2505 switch (NPC) {
2506 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002507 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002508 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002509 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2510 return NPCK_ZeroInteger;
2511 else
2512 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002513
Douglas Gregorce940492009-09-25 04:25:58 +00002514 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002515 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002516 }
2517 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002518
Sebastian Redl07779722008-10-31 14:43:28 +00002519 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002520 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002521 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002522 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002523 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002524 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002525 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002526 Pointee->isVoidType() && // to void*
2527 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002528 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002529 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002530 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002531 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2532 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002533 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002534 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2535 // Accept ((void*)0) as a null pointer constant, as many other
2536 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002537 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002538 } else if (const GenericSelectionExpr *GE =
2539 dyn_cast<GenericSelectionExpr>(this)) {
2540 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002541 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002542 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002543 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002544 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002545 } else if (isa<GNUNullExpr>(this)) {
2546 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002547 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002548 } else if (const MaterializeTemporaryExpr *M
2549 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2550 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002551 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002552
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002553 // C++0x nullptr_t is always a null pointer constant.
2554 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002555 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002556
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002557 if (const RecordType *UT = getType()->getAsUnionType())
2558 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2559 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2560 const Expr *InitExpr = CLE->getInitializer();
2561 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2562 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2563 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002564 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002565 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002566 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002567 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002568
Reid Spencer5f016e22007-07-11 17:01:13 +00002569 // If we have an integer constant expression, we need to *evaluate* it and
2570 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002571 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002572 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2573
2574 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002575}
Steve Naroff31a45842007-07-28 23:10:27 +00002576
John McCallf6a16482010-12-04 03:47:34 +00002577/// \brief If this expression is an l-value for an Objective C
2578/// property, find the underlying property reference expression.
2579const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2580 const Expr *E = this;
2581 while (true) {
2582 assert((E->getValueKind() == VK_LValue &&
2583 E->getObjectKind() == OK_ObjCProperty) &&
2584 "expression is not a property reference");
2585 E = E->IgnoreParenCasts();
2586 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2587 if (BO->getOpcode() == BO_Comma) {
2588 E = BO->getRHS();
2589 continue;
2590 }
2591 }
2592
2593 break;
2594 }
2595
2596 return cast<ObjCPropertyRefExpr>(E);
2597}
2598
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002599FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002600 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002601
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002602 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002603 if (ICE->getCastKind() == CK_LValueToRValue ||
2604 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002605 E = ICE->getSubExpr()->IgnoreParens();
2606 else
2607 break;
2608 }
2609
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002610 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002611 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002612 if (Field->isBitField())
2613 return Field;
2614
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002615 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2616 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2617 if (Field->isBitField())
2618 return Field;
2619
Eli Friedman42068e92011-07-13 02:05:57 +00002620 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002621 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2622 return BinOp->getLHS()->getBitField();
2623
Eli Friedman42068e92011-07-13 02:05:57 +00002624 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2625 return BinOp->getRHS()->getBitField();
2626 }
2627
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002628 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002629}
2630
Anders Carlsson09380262010-01-31 17:18:49 +00002631bool Expr::refersToVectorElement() const {
2632 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002633
Anders Carlsson09380262010-01-31 17:18:49 +00002634 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002635 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002636 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002637 E = ICE->getSubExpr()->IgnoreParens();
2638 else
2639 break;
2640 }
Sean Huntc3021132010-05-05 15:23:54 +00002641
Anders Carlsson09380262010-01-31 17:18:49 +00002642 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2643 return ASE->getBase()->getType()->isVectorType();
2644
2645 if (isa<ExtVectorElementExpr>(E))
2646 return true;
2647
2648 return false;
2649}
2650
Chris Lattner2140e902009-02-16 22:14:05 +00002651/// isArrow - Return true if the base expression is a pointer to vector,
2652/// return false if the base expression is a vector.
2653bool ExtVectorElementExpr::isArrow() const {
2654 return getBase()->getType()->isPointerType();
2655}
2656
Nate Begeman213541a2008-04-18 23:10:10 +00002657unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002658 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002659 return VT->getNumElements();
2660 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002661}
2662
Nate Begeman8a997642008-05-09 06:41:27 +00002663/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002664bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002665 // FIXME: Refactor this code to an accessor on the AST node which returns the
2666 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002667 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002668
2669 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002670 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002671 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002672
Nate Begeman190d6a22009-01-18 02:01:21 +00002673 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002674 if (Comp[0] == 's' || Comp[0] == 'S')
2675 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002676
Daniel Dunbar15027422009-10-17 23:53:04 +00002677 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002678 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002679 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002680
Steve Narofffec0b492007-07-30 03:29:09 +00002681 return false;
2682}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002683
Nate Begeman8a997642008-05-09 06:41:27 +00002684/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002685void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002686 SmallVectorImpl<unsigned> &Elts) const {
2687 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002688 if (Comp[0] == 's' || Comp[0] == 'S')
2689 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002690
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002691 bool isHi = Comp == "hi";
2692 bool isLo = Comp == "lo";
2693 bool isEven = Comp == "even";
2694 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002695
Nate Begeman8a997642008-05-09 06:41:27 +00002696 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2697 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002698
Nate Begeman8a997642008-05-09 06:41:27 +00002699 if (isHi)
2700 Index = e + i;
2701 else if (isLo)
2702 Index = i;
2703 else if (isEven)
2704 Index = 2 * i;
2705 else if (isOdd)
2706 Index = 2 * i + 1;
2707 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002708 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002709
Nate Begeman3b8d1162008-05-13 21:03:02 +00002710 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002711 }
Nate Begeman8a997642008-05-09 06:41:27 +00002712}
2713
Douglas Gregor04badcf2010-04-21 00:45:42 +00002714ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002715 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002716 SourceLocation LBracLoc,
2717 SourceLocation SuperLoc,
2718 bool IsInstanceSuper,
2719 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002720 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002721 ArrayRef<SourceLocation> SelLocs,
2722 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002723 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002724 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002725 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002726 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002727 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002728 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002729 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002730 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2731 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002732 Kind(IsInstanceSuper? SuperInstance : SuperClass),
2733 HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002734 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002735{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002736 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002737 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002738}
2739
Douglas Gregor04badcf2010-04-21 00:45:42 +00002740ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002741 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002742 SourceLocation LBracLoc,
2743 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002744 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002745 ArrayRef<SourceLocation> SelLocs,
2746 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002747 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002748 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002749 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002750 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002751 T->isDependentType(), T->isInstantiationDependentType(),
2752 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002753 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2754 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002755 Kind(Class),
2756 HasMethod(Method != 0), IsDelegateInitCall(false),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002757 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002758{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002759 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002760 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002761}
2762
Douglas Gregor04badcf2010-04-21 00:45:42 +00002763ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002764 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002765 SourceLocation LBracLoc,
2766 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002767 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002768 ArrayRef<SourceLocation> SelLocs,
2769 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002770 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002771 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002772 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002773 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002774 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002775 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002776 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002777 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2778 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002779 Kind(Instance),
2780 HasMethod(Method != 0), IsDelegateInitCall(false),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002781 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002782{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002783 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002784 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002785}
2786
2787void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2788 ArrayRef<SourceLocation> SelLocs,
2789 SelectorLocationsKind SelLocsK) {
2790 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00002791 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002792 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002793 if (Args[I]->isTypeDependent())
2794 ExprBits.TypeDependent = true;
2795 if (Args[I]->isValueDependent())
2796 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002797 if (Args[I]->isInstantiationDependent())
2798 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002799 if (Args[I]->containsUnexpandedParameterPack())
2800 ExprBits.ContainsUnexpandedParameterPack = true;
2801
2802 MyArgs[I] = Args[I];
2803 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002804
2805 SelLocsKind = SelLocsK;
2806 if (SelLocsK == SelLoc_NonStandard)
2807 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
Chris Lattner0389e6b2009-04-26 00:44:05 +00002808}
2809
Douglas Gregor04badcf2010-04-21 00:45:42 +00002810ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002811 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002812 SourceLocation LBracLoc,
2813 SourceLocation SuperLoc,
2814 bool IsInstanceSuper,
2815 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002816 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002817 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002818 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002819 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002820 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002821 SelectorLocationsKind SelLocsK;
2822 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00002823 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002824 SuperType, Sel, SelLocs, SelLocsK,
2825 Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002826}
2827
2828ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002829 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002830 SourceLocation LBracLoc,
2831 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002832 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002833 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002834 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002835 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002836 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002837 SelectorLocationsKind SelLocsK;
2838 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002839 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002840 SelLocs, SelLocsK, Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002841}
2842
2843ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002844 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002845 SourceLocation LBracLoc,
2846 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002847 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002848 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002849 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002850 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002851 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002852 SelectorLocationsKind SelLocsK;
2853 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002854 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002855 SelLocs, SelLocsK, Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002856}
2857
Sean Huntc3021132010-05-05 15:23:54 +00002858ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002859 unsigned NumArgs,
2860 unsigned NumStoredSelLocs) {
2861 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002862 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2863}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002864
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002865ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2866 ArrayRef<Expr *> Args,
2867 SourceLocation RBraceLoc,
2868 ArrayRef<SourceLocation> SelLocs,
2869 Selector Sel,
2870 SelectorLocationsKind &SelLocsK) {
2871 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
2872 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
2873 : 0;
2874 return alloc(C, Args.size(), NumStoredSelLocs);
2875}
2876
2877ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2878 unsigned NumArgs,
2879 unsigned NumStoredSelLocs) {
2880 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2881 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
2882 return (ObjCMessageExpr *)C.Allocate(Size,
2883 llvm::AlignOf<ObjCMessageExpr>::Alignment);
2884}
2885
2886void ObjCMessageExpr::getSelectorLocs(
2887 SmallVectorImpl<SourceLocation> &SelLocs) const {
2888 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
2889 SelLocs.push_back(getSelectorLoc(i));
2890}
2891
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002892SourceRange ObjCMessageExpr::getReceiverRange() const {
2893 switch (getReceiverKind()) {
2894 case Instance:
2895 return getInstanceReceiver()->getSourceRange();
2896
2897 case Class:
2898 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2899
2900 case SuperInstance:
2901 case SuperClass:
2902 return getSuperLoc();
2903 }
2904
2905 return SourceLocation();
2906}
2907
Douglas Gregor04badcf2010-04-21 00:45:42 +00002908Selector ObjCMessageExpr::getSelector() const {
2909 if (HasMethod)
2910 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2911 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002912 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002913}
2914
2915ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2916 switch (getReceiverKind()) {
2917 case Instance:
2918 if (const ObjCObjectPointerType *Ptr
2919 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2920 return Ptr->getInterfaceDecl();
2921 break;
2922
2923 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002924 if (const ObjCObjectType *Ty
2925 = getClassReceiver()->getAs<ObjCObjectType>())
2926 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002927 break;
2928
2929 case SuperInstance:
2930 if (const ObjCObjectPointerType *Ptr
2931 = getSuperType()->getAs<ObjCObjectPointerType>())
2932 return Ptr->getInterfaceDecl();
2933 break;
2934
2935 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002936 if (const ObjCObjectType *Iface
2937 = getSuperType()->getAs<ObjCObjectType>())
2938 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002939 break;
2940 }
2941
2942 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002943}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002944
Chris Lattner5f9e2722011-07-23 10:55:15 +00002945StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00002946 switch (getBridgeKind()) {
2947 case OBC_Bridge:
2948 return "__bridge";
2949 case OBC_BridgeTransfer:
2950 return "__bridge_transfer";
2951 case OBC_BridgeRetained:
2952 return "__bridge_retained";
2953 }
2954
2955 return "__bridge";
2956}
2957
Jay Foad4ba2a172011-01-12 09:06:06 +00002958bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00002959 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002960}
2961
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002962ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2963 QualType Type, SourceLocation BLoc,
2964 SourceLocation RP)
2965 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2966 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002967 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002968 Type->containsUnexpandedParameterPack()),
2969 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2970{
2971 SubExprs = new (C) Stmt*[nexpr];
2972 for (unsigned i = 0; i < nexpr; i++) {
2973 if (args[i]->isTypeDependent())
2974 ExprBits.TypeDependent = true;
2975 if (args[i]->isValueDependent())
2976 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002977 if (args[i]->isInstantiationDependent())
2978 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002979 if (args[i]->containsUnexpandedParameterPack())
2980 ExprBits.ContainsUnexpandedParameterPack = true;
2981
2982 SubExprs[i] = args[i];
2983 }
2984}
2985
Nate Begeman888376a2009-08-12 02:28:50 +00002986void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2987 unsigned NumExprs) {
2988 if (SubExprs) C.Deallocate(SubExprs);
2989
2990 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002991 this->NumExprs = NumExprs;
2992 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002993}
Nate Begeman888376a2009-08-12 02:28:50 +00002994
Peter Collingbournef111d932011-04-15 00:35:48 +00002995GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2996 SourceLocation GenericLoc, Expr *ControllingExpr,
2997 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2998 unsigned NumAssocs, SourceLocation DefaultLoc,
2999 SourceLocation RParenLoc,
3000 bool ContainsUnexpandedParameterPack,
3001 unsigned ResultIndex)
3002 : Expr(GenericSelectionExprClass,
3003 AssocExprs[ResultIndex]->getType(),
3004 AssocExprs[ResultIndex]->getValueKind(),
3005 AssocExprs[ResultIndex]->getObjectKind(),
3006 AssocExprs[ResultIndex]->isTypeDependent(),
3007 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003008 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003009 ContainsUnexpandedParameterPack),
3010 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3011 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3012 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3013 RParenLoc(RParenLoc) {
3014 SubExprs[CONTROLLING] = ControllingExpr;
3015 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3016 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3017}
3018
3019GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3020 SourceLocation GenericLoc, Expr *ControllingExpr,
3021 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3022 unsigned NumAssocs, SourceLocation DefaultLoc,
3023 SourceLocation RParenLoc,
3024 bool ContainsUnexpandedParameterPack)
3025 : Expr(GenericSelectionExprClass,
3026 Context.DependentTy,
3027 VK_RValue,
3028 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003029 /*isTypeDependent=*/true,
3030 /*isValueDependent=*/true,
3031 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003032 ContainsUnexpandedParameterPack),
3033 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3034 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3035 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3036 RParenLoc(RParenLoc) {
3037 SubExprs[CONTROLLING] = ControllingExpr;
3038 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3039 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3040}
3041
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003042//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003043// DesignatedInitExpr
3044//===----------------------------------------------------------------------===//
3045
Chandler Carruthb1138242011-06-16 06:47:06 +00003046IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003047 assert(Kind == FieldDesignator && "Only valid on a field designator");
3048 if (Field.NameOrField & 0x01)
3049 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3050 else
3051 return getField()->getIdentifier();
3052}
3053
Sean Huntc3021132010-05-05 15:23:54 +00003054DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003055 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003056 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003057 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003058 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003059 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003060 unsigned NumIndexExprs,
3061 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003062 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003063 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003064 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003065 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003066 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003067 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3068 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003069 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003070
3071 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003072 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003073 *Child++ = Init;
3074
3075 // Copy the designators and their subexpressions, computing
3076 // value-dependence along the way.
3077 unsigned IndexIdx = 0;
3078 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003079 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003080
3081 if (this->Designators[I].isArrayDesignator()) {
3082 // Compute type- and value-dependence.
3083 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003084 if (Index->isTypeDependent() || Index->isValueDependent())
3085 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003086 if (Index->isInstantiationDependent())
3087 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003088 // Propagate unexpanded parameter packs.
3089 if (Index->containsUnexpandedParameterPack())
3090 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003091
3092 // Copy the index expressions into permanent storage.
3093 *Child++ = IndexExprs[IndexIdx++];
3094 } else if (this->Designators[I].isArrayRangeDesignator()) {
3095 // Compute type- and value-dependence.
3096 Expr *Start = IndexExprs[IndexIdx];
3097 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003098 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003099 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003100 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003101 ExprBits.InstantiationDependent = true;
3102 } else if (Start->isInstantiationDependent() ||
3103 End->isInstantiationDependent()) {
3104 ExprBits.InstantiationDependent = true;
3105 }
3106
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003107 // Propagate unexpanded parameter packs.
3108 if (Start->containsUnexpandedParameterPack() ||
3109 End->containsUnexpandedParameterPack())
3110 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003111
3112 // Copy the start/end expressions into permanent storage.
3113 *Child++ = IndexExprs[IndexIdx++];
3114 *Child++ = IndexExprs[IndexIdx++];
3115 }
3116 }
3117
3118 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003119}
3120
Douglas Gregor05c13a32009-01-22 00:58:24 +00003121DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003122DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003123 unsigned NumDesignators,
3124 Expr **IndexExprs, unsigned NumIndexExprs,
3125 SourceLocation ColonOrEqualLoc,
3126 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003127 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003128 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003129 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003130 ColonOrEqualLoc, UsesColonSyntax,
3131 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003132}
3133
Mike Stump1eb44332009-09-09 15:08:12 +00003134DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003135 unsigned NumIndexExprs) {
3136 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3137 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3138 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3139}
3140
Douglas Gregor319d57f2010-01-06 23:17:19 +00003141void DesignatedInitExpr::setDesignators(ASTContext &C,
3142 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003143 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003144 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003145 NumDesignators = NumDesigs;
3146 for (unsigned I = 0; I != NumDesigs; ++I)
3147 Designators[I] = Desigs[I];
3148}
3149
Abramo Bagnara24f46742011-03-16 15:08:46 +00003150SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3151 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3152 if (size() == 1)
3153 return DIE->getDesignator(0)->getSourceRange();
3154 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3155 DIE->getDesignator(size()-1)->getEndLocation());
3156}
3157
Douglas Gregor05c13a32009-01-22 00:58:24 +00003158SourceRange DesignatedInitExpr::getSourceRange() const {
3159 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003160 Designator &First =
3161 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003162 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003163 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003164 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3165 else
3166 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3167 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003168 StartLoc =
3169 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003170 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3171}
3172
Douglas Gregor05c13a32009-01-22 00:58:24 +00003173Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3174 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3175 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3176 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003177 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3178 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3179}
3180
3181Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003182 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003183 "Requires array range designator");
3184 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3185 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003186 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3187 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3188}
3189
3190Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003191 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003192 "Requires array range designator");
3193 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3194 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003195 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3196 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3197}
3198
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003199/// \brief Replaces the designator at index @p Idx with the series
3200/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003201void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003202 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003203 const Designator *Last) {
3204 unsigned NumNewDesignators = Last - First;
3205 if (NumNewDesignators == 0) {
3206 std::copy_backward(Designators + Idx + 1,
3207 Designators + NumDesignators,
3208 Designators + Idx);
3209 --NumNewDesignators;
3210 return;
3211 } else if (NumNewDesignators == 1) {
3212 Designators[Idx] = *First;
3213 return;
3214 }
3215
Mike Stump1eb44332009-09-09 15:08:12 +00003216 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003217 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003218 std::copy(Designators, Designators + Idx, NewDesignators);
3219 std::copy(First, Last, NewDesignators + Idx);
3220 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3221 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003222 Designators = NewDesignators;
3223 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3224}
3225
Mike Stump1eb44332009-09-09 15:08:12 +00003226ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003227 Expr **exprs, unsigned nexprs,
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003228 SourceLocation rparenloc, QualType T)
3229 : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003230 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003231 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003232 assert(!T.isNull() && "ParenListExpr must have a valid type");
Nate Begeman2ef13e52009-08-10 23:49:36 +00003233 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003234 for (unsigned i = 0; i != nexprs; ++i) {
3235 if (exprs[i]->isTypeDependent())
3236 ExprBits.TypeDependent = true;
3237 if (exprs[i]->isValueDependent())
3238 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003239 if (exprs[i]->isInstantiationDependent())
3240 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003241 if (exprs[i]->containsUnexpandedParameterPack())
3242 ExprBits.ContainsUnexpandedParameterPack = true;
3243
Nate Begeman2ef13e52009-08-10 23:49:36 +00003244 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003245 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003246}
3247
John McCalle996ffd2011-02-16 08:02:54 +00003248const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3249 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3250 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003251 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3252 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003253 e = cast<CXXConstructExpr>(e)->getArg(0);
3254 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3255 e = ice->getSubExpr();
3256 return cast<OpaqueValueExpr>(e);
3257}
3258
Douglas Gregor05c13a32009-01-22 00:58:24 +00003259//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003260// ExprIterator.
3261//===----------------------------------------------------------------------===//
3262
3263Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3264Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3265Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3266const Expr* ConstExprIterator::operator[](size_t idx) const {
3267 return cast<Expr>(I[idx]);
3268}
3269const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3270const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3271
3272//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003273// Child Iterators for iterating over subexpressions/substatements
3274//===----------------------------------------------------------------------===//
3275
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003276// UnaryExprOrTypeTraitExpr
3277Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003278 // If this is of a type and the type is a VLA type (and not a typedef), the
3279 // size expression of the VLA needs to be treated as an executable expression.
3280 // Why isn't this weirdness documented better in StmtIterator?
3281 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003282 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003283 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003284 return child_range(child_iterator(T), child_iterator());
3285 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003286 }
John McCall63c00d72011-02-09 08:16:59 +00003287 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003288}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003289
Steve Naroff563477d2007-09-18 23:55:05 +00003290// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003291Stmt::child_range ObjCMessageExpr::children() {
3292 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003293 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003294 begin = reinterpret_cast<Stmt **>(this + 1);
3295 else
3296 begin = reinterpret_cast<Stmt **>(getArgs());
3297 return child_range(begin,
3298 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003299}
3300
Steve Naroff4eb206b2008-09-03 18:15:37 +00003301// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003302BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003303 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003304 bool constAdded)
Douglas Gregor561f8122011-07-01 01:22:09 +00003305 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003306 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003307 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003308{
Douglas Gregord967e312011-01-19 21:52:31 +00003309 bool TypeDependent = false;
3310 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +00003311 bool InstantiationDependent = false;
3312 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3313 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +00003314 ExprBits.TypeDependent = TypeDependent;
3315 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +00003316 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003317}