blob: 323997368879800fc06f6e8edb63ebc7f2f9554a [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>
Eli Friedman64f45a22011-11-01 02:23:42 +000032#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000033using namespace clang;
34
Chris Lattner2b334bb2010-04-16 23:34:13 +000035/// isKnownToHaveBooleanValue - Return true if this is an integer expression
36/// that is known to return 0 or 1. This happens for _Bool/bool expressions
37/// but also int expressions which are produced by things like comparisons in
38/// C.
39bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000040 const Expr *E = IgnoreParens();
41
Chris Lattner2b334bb2010-04-16 23:34:13 +000042 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000043 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000044 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000045 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000046
Peter Collingbournef111d932011-04-15 00:35:48 +000047 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000048 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000049 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000050 return UO->getSubExpr()->isKnownToHaveBooleanValue();
51 default:
52 return false;
53 }
54 }
Sean Huntc3021132010-05-05 15:23:54 +000055
John McCall6907fbe2010-06-12 01:56:02 +000056 // Only look through implicit casts. If the user writes
57 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000058 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000059 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000060
Peter Collingbournef111d932011-04-15 00:35:48 +000061 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000062 switch (BO->getOpcode()) {
63 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000064 case BO_LT: // Relational operators.
65 case BO_GT:
66 case BO_LE:
67 case BO_GE:
68 case BO_EQ: // Equality operators.
69 case BO_NE:
70 case BO_LAnd: // AND operator.
71 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000072 return true;
Sean Huntc3021132010-05-05 15:23:54 +000073
John McCall2de56d12010-08-25 11:45:40 +000074 case BO_And: // Bitwise AND operator.
75 case BO_Xor: // Bitwise XOR operator.
76 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000077 // Handle things like (x==2)|(y==12).
78 return BO->getLHS()->isKnownToHaveBooleanValue() &&
79 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000080
John McCall2de56d12010-08-25 11:45:40 +000081 case BO_Comma:
82 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000083 return BO->getRHS()->isKnownToHaveBooleanValue();
84 }
85 }
Sean Huntc3021132010-05-05 15:23:54 +000086
Peter Collingbournef111d932011-04-15 00:35:48 +000087 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000088 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
89 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000090
Chris Lattner2b334bb2010-04-16 23:34:13 +000091 return false;
92}
93
John McCall63c00d72011-02-09 08:16:59 +000094// Amusing macro metaprogramming hack: check whether a class provides
95// a more specific implementation of getExprLoc().
96namespace {
97 /// This implementation is used when a class provides a custom
98 /// implementation of getExprLoc.
99 template <class E, class T>
100 SourceLocation getExprLocImpl(const Expr *expr,
101 SourceLocation (T::*v)() const) {
102 return static_cast<const E*>(expr)->getExprLoc();
103 }
104
105 /// This implementation is used when a class doesn't provide
106 /// a custom implementation of getExprLoc. Overload resolution
107 /// should pick it over the implementation above because it's
108 /// more specialized according to function template partial ordering.
109 template <class E>
110 SourceLocation getExprLocImpl(const Expr *expr,
111 SourceLocation (Expr::*v)() const) {
112 return static_cast<const E*>(expr)->getSourceRange().getBegin();
113 }
114}
115
116SourceLocation Expr::getExprLoc() const {
117 switch (getStmtClass()) {
118 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
119#define ABSTRACT_STMT(type)
120#define STMT(type, base) \
121 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
122#define EXPR(type, base) \
123 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
124#include "clang/AST/StmtNodes.inc"
125 }
126 llvm_unreachable("unknown statement kind");
127 return SourceLocation();
128}
129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130//===----------------------------------------------------------------------===//
131// Primary Expressions.
132//===----------------------------------------------------------------------===//
133
Douglas Gregor561f8122011-07-01 01:22:09 +0000134/// \brief Compute the type-, value-, and instantiation-dependence of a
135/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000136/// based on the declaration being referenced.
137static void computeDeclRefDependence(NamedDecl *D, QualType T,
138 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000139 bool &ValueDependent,
140 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000141 TypeDependent = false;
142 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000143 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000144
145 // (TD) C++ [temp.dep.expr]p3:
146 // An id-expression is type-dependent if it contains:
147 //
Sean Huntc3021132010-05-05 15:23:54 +0000148 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000149 //
150 // (VD) C++ [temp.dep.constexpr]p2:
151 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000152
Douglas Gregor0da76df2009-11-23 11:41:28 +0000153 // (TD) - an identifier that was declared with dependent type
154 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000155 if (T->isDependentType()) {
156 TypeDependent = true;
157 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000158 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000159 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000160 } else if (T->isInstantiationDependentType()) {
161 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000162 }
Douglas Gregord967e312011-01-19 21:52:31 +0000163
Douglas Gregor0da76df2009-11-23 11:41:28 +0000164 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000165 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000166 == DeclarationName::CXXConversionFunctionName) {
167 QualType T = D->getDeclName().getCXXNameType();
168 if (T->isDependentType()) {
169 TypeDependent = true;
170 ValueDependent = true;
171 InstantiationDependent = true;
172 return;
173 }
174
175 if (T->isInstantiationDependentType())
176 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000177 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000178
Douglas Gregor0da76df2009-11-23 11:41:28 +0000179 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000180 if (isa<NonTypeTemplateParmDecl>(D)) {
181 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000182 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000183 return;
184 }
185
Douglas Gregor0da76df2009-11-23 11:41:28 +0000186 // (VD) - a constant with integral or enumeration type and is
187 // initialized with an expression that is value-dependent.
Douglas Gregord967e312011-01-19 21:52:31 +0000188 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000189 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000190 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000191 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000192 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000193 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000194 InstantiationDependent = true;
195 }
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000196 }
Douglas Gregord967e312011-01-19 21:52:31 +0000197
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000198 // (VD) - FIXME: Missing from the standard:
199 // - a member function or a static data member of the current
200 // instantiation
201 else if (Var->isStaticDataMember() &&
Douglas Gregor561f8122011-07-01 01:22:09 +0000202 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000203 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000204 InstantiationDependent = true;
205 }
Douglas Gregord967e312011-01-19 21:52:31 +0000206
207 return;
208 }
209
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000210 // (VD) - FIXME: Missing from the standard:
211 // - a member function or a static data member of the current
212 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000213 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
214 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000215 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000216 return;
217 }
218}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000219
Douglas Gregord967e312011-01-19 21:52:31 +0000220void DeclRefExpr::computeDependence() {
221 bool TypeDependent = false;
222 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000223 bool InstantiationDependent = false;
224 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
225 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000226
227 // (TD) C++ [temp.dep.expr]p3:
228 // An id-expression is type-dependent if it contains:
229 //
230 // and
231 //
232 // (VD) C++ [temp.dep.constexpr]p2:
233 // An identifier is value-dependent if it is:
234 if (!TypeDependent && !ValueDependent &&
235 hasExplicitTemplateArgs() &&
236 TemplateSpecializationType::anyDependentTemplateArguments(
237 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000238 getNumTemplateArgs(),
239 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000240 TypeDependent = true;
241 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000242 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000243 }
244
245 ExprBits.TypeDependent = TypeDependent;
246 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000247 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000248
Douglas Gregor10738d32010-12-23 23:51:58 +0000249 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000250 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000251 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000252}
253
Chandler Carruth3aa81402011-05-01 23:48:14 +0000254DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000255 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000256 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000257 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000258 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000259 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000260 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
261 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000262 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000263 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000264 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
265 if (FoundD)
266 getInternalFoundDecl() = FoundD;
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000267 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Douglas Gregor561f8122011-07-01 01:22:09 +0000268 if (TemplateArgs) {
269 bool Dependent = false;
270 bool InstantiationDependent = false;
271 bool ContainsUnexpandedParameterPack = false;
272 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
273 InstantiationDependent,
274 ContainsUnexpandedParameterPack);
275 if (InstantiationDependent)
276 setInstantiationDependent(true);
277 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000278 DeclRefExprBits.HadMultipleCandidates = 0;
279
Abramo Bagnara25777432010-08-11 22:01:17 +0000280 computeDependence();
281}
282
Douglas Gregora2813ce2009-10-23 18:54:35 +0000283DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000284 NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000285 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000286 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000287 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000288 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000289 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000290 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000291 return Create(Context, QualifierLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000292 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000293 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000294}
295
296DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000297 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000298 ValueDecl *D,
299 const DeclarationNameInfo &NameInfo,
300 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000301 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000302 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000303 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000304 // Filter out cases where the found Decl is the same as the value refenenced.
305 if (D == FoundD)
306 FoundD = 0;
307
Douglas Gregora2813ce2009-10-23 18:54:35 +0000308 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000309 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000310 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000311 if (FoundD)
312 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000313 if (TemplateArgs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000314 Size += ASTTemplateArgumentListInfo::sizeFor(*TemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000315
Chris Lattner32488542010-10-30 05:14:06 +0000316 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth3aa81402011-05-01 23:48:14 +0000317 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
318 T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000319}
320
Chandler Carruth3aa81402011-05-01 23:48:14 +0000321DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000322 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000323 bool HasFoundDecl,
Douglas Gregordef03542011-02-04 12:01:24 +0000324 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000325 unsigned NumTemplateArgs) {
326 std::size_t Size = sizeof(DeclRefExpr);
327 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000328 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000329 if (HasFoundDecl)
330 Size += sizeof(NamedDecl *);
Douglas Gregordef03542011-02-04 12:01:24 +0000331 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000332 Size += ASTTemplateArgumentListInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000333
Chris Lattner32488542010-10-30 05:14:06 +0000334 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000335 return new (Mem) DeclRefExpr(EmptyShell());
336}
337
Douglas Gregora2813ce2009-10-23 18:54:35 +0000338SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000339 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000340 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000341 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000342 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000343 R.setEnd(getRAngleLoc());
344 return R;
345}
346
Anders Carlsson3a082d82009-09-08 18:24:21 +0000347// FIXME: Maybe this should use DeclPrinter with a special "print predefined
348// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000349std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
350 ASTContext &Context = CurrentDecl->getASTContext();
351
Anders Carlsson3a082d82009-09-08 18:24:21 +0000352 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000353 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000354 return FD->getNameAsString();
355
356 llvm::SmallString<256> Name;
357 llvm::raw_svector_ostream Out(Name);
358
359 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000360 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000361 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000362 if (MD->isStatic())
363 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000364 }
365
366 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000367
368 std::string Proto = FD->getQualifiedNameAsString(Policy);
369
John McCall183700f2009-09-21 23:43:11 +0000370 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000371 const FunctionProtoType *FT = 0;
372 if (FD->hasWrittenPrototype())
373 FT = dyn_cast<FunctionProtoType>(AFT);
374
375 Proto += "(";
376 if (FT) {
377 llvm::raw_string_ostream POut(Proto);
378 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
379 if (i) POut << ", ";
380 std::string Param;
381 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
382 POut << Param;
383 }
384
385 if (FT->isVariadic()) {
386 if (FD->getNumParams()) POut << ", ";
387 POut << "...";
388 }
389 }
390 Proto += ")";
391
Sam Weinig4eadcc52009-12-27 01:38:20 +0000392 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
393 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
394 if (ThisQuals.hasConst())
395 Proto += " const";
396 if (ThisQuals.hasVolatile())
397 Proto += " volatile";
398 }
399
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000400 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
401 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000402
403 Out << Proto;
404
405 Out.flush();
406 return Name.str().str();
407 }
408 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
409 llvm::SmallString<256> Name;
410 llvm::raw_svector_ostream Out(Name);
411 Out << (MD->isInstanceMethod() ? '-' : '+');
412 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000413
414 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
415 // a null check to avoid a crash.
416 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000417 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000418
Anders Carlsson3a082d82009-09-08 18:24:21 +0000419 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000420 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
421 Out << '(' << CID << ')';
422
Anders Carlsson3a082d82009-09-08 18:24:21 +0000423 Out << ' ';
424 Out << MD->getSelector().getAsString();
425 Out << ']';
426
427 Out.flush();
428 return Name.str().str();
429 }
430 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
431 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
432 return "top level";
433 }
434 return "";
435}
436
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000437void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
438 if (hasAllocation())
439 C.Deallocate(pVal);
440
441 BitWidth = Val.getBitWidth();
442 unsigned NumWords = Val.getNumWords();
443 const uint64_t* Words = Val.getRawData();
444 if (NumWords > 1) {
445 pVal = new (C) uint64_t[NumWords];
446 std::copy(Words, Words + NumWords, pVal);
447 } else if (NumWords == 1)
448 VAL = Words[0];
449 else
450 VAL = 0;
451}
452
453IntegerLiteral *
454IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
455 QualType type, SourceLocation l) {
456 return new (C) IntegerLiteral(C, V, type, l);
457}
458
459IntegerLiteral *
460IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
461 return new (C) IntegerLiteral(Empty);
462}
463
464FloatingLiteral *
465FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
466 bool isexact, QualType Type, SourceLocation L) {
467 return new (C) FloatingLiteral(C, V, isexact, Type, L);
468}
469
470FloatingLiteral *
471FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
472 return new (C) FloatingLiteral(Empty);
473}
474
Chris Lattnerda8249e2008-06-07 22:13:43 +0000475/// getValueAsApproximateDouble - This returns the value as an inaccurate
476/// double. Note that this may cause loss of precision, but is useful for
477/// debugging dumps, etc.
478double FloatingLiteral::getValueAsApproximateDouble() const {
479 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000480 bool ignored;
481 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
482 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000483 return V.convertToDouble();
484}
485
Eli Friedman64f45a22011-11-01 02:23:42 +0000486int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
487 int CharByteWidth;
488 switch(k) {
489 case Ascii:
490 case UTF8:
491 CharByteWidth = target.getCharWidth();
492 break;
493 case Wide:
494 CharByteWidth = target.getWCharWidth();
495 break;
496 case UTF16:
497 CharByteWidth = target.getChar16Width();
498 break;
499 case UTF32:
500 CharByteWidth = target.getChar32Width();
501 }
502 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
503 CharByteWidth /= 8;
504 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
505 && "character byte widths supported are 1, 2, and 4 only");
506 return CharByteWidth;
507}
508
Chris Lattner5f9e2722011-07-23 10:55:15 +0000509StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000510 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000511 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000512 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000513 // Allocate enough space for the StringLiteral plus an array of locations for
514 // any concatenated string tokens.
515 void *Mem = C.Allocate(sizeof(StringLiteral)+
516 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000517 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000518 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000521 SL->setString(C,Str,Kind,Pascal);
522
Chris Lattner2085fd62009-02-18 06:40:38 +0000523 SL->TokLocs[0] = Loc[0];
524 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000525
Chris Lattner726e1682009-02-18 05:49:11 +0000526 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000527 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
528 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000529}
530
Douglas Gregor673ecd62009-04-15 16:35:07 +0000531StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
532 void *Mem = C.Allocate(sizeof(StringLiteral)+
533 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000534 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000535 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000536 SL->CharByteWidth = 0;
537 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000538 SL->NumConcatenated = NumStrs;
539 return SL;
540}
541
Eli Friedman64f45a22011-11-01 02:23:42 +0000542void StringLiteral::setString(ASTContext &C, StringRef Str,
543 StringKind Kind, bool IsPascal) {
544 //FIXME: we assume that the string data comes from a target that uses the same
545 // code unit size and endianess for the type of string.
546 this->Kind = Kind;
547 this->IsPascal = IsPascal;
548
549 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
550 assert((Str.size()%CharByteWidth == 0)
551 && "size of data must be multiple of CharByteWidth");
552 Length = Str.size()/CharByteWidth;
553
554 switch(CharByteWidth) {
555 case 1: {
556 char *AStrData = new (C) char[Length];
557 std::memcpy(AStrData,Str.data(),Str.size());
558 StrData.asChar = AStrData;
559 break;
560 }
561 case 2: {
562 uint16_t *AStrData = new (C) uint16_t[Length];
563 std::memcpy(AStrData,Str.data(),Str.size());
564 StrData.asUInt16 = AStrData;
565 break;
566 }
567 case 4: {
568 uint32_t *AStrData = new (C) uint32_t[Length];
569 std::memcpy(AStrData,Str.data(),Str.size());
570 StrData.asUInt32 = AStrData;
571 break;
572 }
573 default:
574 assert(false && "unsupported CharByteWidth");
575 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000576}
577
Chris Lattner08f92e32010-11-17 07:37:15 +0000578/// getLocationOfByte - Return a source location that points to the specified
579/// byte of this string literal.
580///
581/// Strings are amazingly complex. They can be formed from multiple tokens and
582/// can have escape sequences in them in addition to the usual trigraph and
583/// escaped newline business. This routine handles this complexity.
584///
585SourceLocation StringLiteral::
586getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
587 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000588 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
589
Chris Lattner08f92e32010-11-17 07:37:15 +0000590 // Loop over all of the tokens in this string until we find the one that
591 // contains the byte we're looking for.
592 unsigned TokNo = 0;
593 while (1) {
594 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
595 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
596
597 // Get the spelling of the string so that we can get the data that makes up
598 // the string literal, not the identifier for the macro it is potentially
599 // expanded through.
600 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
601
602 // Re-lex the token to get its length and original spelling.
603 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
604 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000605 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000606 if (Invalid)
607 return StrTokSpellingLoc;
608
609 const char *StrData = Buffer.data()+LocInfo.second;
610
611 // Create a langops struct and enable trigraphs. This is sufficient for
612 // relexing tokens.
613 LangOptions LangOpts;
614 LangOpts.Trigraphs = true;
615
616 // Create a lexer starting at the beginning of this token.
617 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
618 Buffer.end());
619 Token TheTok;
620 TheLexer.LexFromRawLexer(TheTok);
621
622 // Use the StringLiteralParser to compute the length of the string in bytes.
623 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
624 unsigned TokNumBytes = SLP.GetStringLength();
625
626 // If the byte is in this token, return the location of the byte.
627 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000628 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000629 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
630
631 // Now that we know the offset of the token in the spelling, use the
632 // preprocessor to get the offset in the original source.
633 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
634 }
635
636 // Move to the next string token.
637 ++TokNo;
638 ByteNo -= TokNumBytes;
639 }
640}
641
642
643
Reid Spencer5f016e22007-07-11 17:01:13 +0000644/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
645/// corresponds to, e.g. "sizeof" or "[pre]++".
646const char *UnaryOperator::getOpcodeStr(Opcode Op) {
647 switch (Op) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000648 default: llvm_unreachable("Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000649 case UO_PostInc: return "++";
650 case UO_PostDec: return "--";
651 case UO_PreInc: return "++";
652 case UO_PreDec: return "--";
653 case UO_AddrOf: return "&";
654 case UO_Deref: return "*";
655 case UO_Plus: return "+";
656 case UO_Minus: return "-";
657 case UO_Not: return "~";
658 case UO_LNot: return "!";
659 case UO_Real: return "__real";
660 case UO_Imag: return "__imag";
661 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 }
663}
664
John McCall2de56d12010-08-25 11:45:40 +0000665UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000666UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
667 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000668 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000669 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
670 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
671 case OO_Amp: return UO_AddrOf;
672 case OO_Star: return UO_Deref;
673 case OO_Plus: return UO_Plus;
674 case OO_Minus: return UO_Minus;
675 case OO_Tilde: return UO_Not;
676 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000677 }
678}
679
680OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
681 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000682 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
683 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
684 case UO_AddrOf: return OO_Amp;
685 case UO_Deref: return OO_Star;
686 case UO_Plus: return OO_Plus;
687 case UO_Minus: return OO_Minus;
688 case UO_Not: return OO_Tilde;
689 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000690 default: return OO_None;
691 }
692}
693
694
Reid Spencer5f016e22007-07-11 17:01:13 +0000695//===----------------------------------------------------------------------===//
696// Postfix Operators.
697//===----------------------------------------------------------------------===//
698
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000699CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
700 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000701 SourceLocation rparenloc)
702 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000703 fn->isTypeDependent(),
704 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000705 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000706 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000707 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000709 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000710 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000711 for (unsigned i = 0; i != numargs; ++i) {
712 if (args[i]->isTypeDependent())
713 ExprBits.TypeDependent = true;
714 if (args[i]->isValueDependent())
715 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000716 if (args[i]->isInstantiationDependent())
717 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000718 if (args[i]->containsUnexpandedParameterPack())
719 ExprBits.ContainsUnexpandedParameterPack = true;
720
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000721 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000722 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000723
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000724 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000725 RParenLoc = rparenloc;
726}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000727
Ted Kremenek668bf912009-02-09 20:51:47 +0000728CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000729 QualType t, ExprValueKind VK, SourceLocation rparenloc)
730 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000731 fn->isTypeDependent(),
732 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000733 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000734 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000735 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000736
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000737 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000738 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000739 for (unsigned i = 0; i != numargs; ++i) {
740 if (args[i]->isTypeDependent())
741 ExprBits.TypeDependent = true;
742 if (args[i]->isValueDependent())
743 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000744 if (args[i]->isInstantiationDependent())
745 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000746 if (args[i]->containsUnexpandedParameterPack())
747 ExprBits.ContainsUnexpandedParameterPack = true;
748
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000749 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000750 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000751
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000752 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 RParenLoc = rparenloc;
754}
755
Mike Stump1eb44332009-09-09 15:08:12 +0000756CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
757 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000758 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000759 SubExprs = new (C) Stmt*[PREARGS_START];
760 CallExprBits.NumPreArgs = 0;
761}
762
763CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
764 EmptyShell Empty)
765 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
766 // FIXME: Why do we allocate this?
767 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
768 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000769}
770
Nuno Lopesd20254f2009-12-20 23:11:08 +0000771Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000772 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000773
774 while (SubstNonTypeTemplateParmExpr *NTTP
775 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
776 CEE = NTTP->getReplacement()->IgnoreParenCasts();
777 }
778
Sebastian Redl20012152010-09-10 20:55:30 +0000779 // If we're calling a dereference, look at the pointer instead.
780 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
781 if (BO->isPtrMemOp())
782 CEE = BO->getRHS()->IgnoreParenCasts();
783 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
784 if (UO->getOpcode() == UO_Deref)
785 CEE = UO->getSubExpr()->IgnoreParenCasts();
786 }
Chris Lattner6346f962009-07-17 15:46:27 +0000787 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000788 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000789 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
790 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000791
792 return 0;
793}
794
Nuno Lopesd20254f2009-12-20 23:11:08 +0000795FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000796 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000797}
798
Chris Lattnerd18b3292007-12-28 05:25:02 +0000799/// setNumArgs - This changes the number of arguments present in this call.
800/// Any orphaned expressions are deleted by this, and any new operands are set
801/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000802void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000803 // No change, just return.
804 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Chris Lattnerd18b3292007-12-28 05:25:02 +0000806 // If shrinking # arguments, just delete the extras and forgot them.
807 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000808 this->NumArgs = NumArgs;
809 return;
810 }
811
812 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000813 unsigned NumPreArgs = getNumPreArgs();
814 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000815 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000816 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000817 NewSubExprs[i] = SubExprs[i];
818 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000819 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
820 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000821 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Douglas Gregor88c9a462009-04-17 21:46:47 +0000823 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000824 SubExprs = NewSubExprs;
825 this->NumArgs = NumArgs;
826}
827
Chris Lattnercb888962008-10-06 05:00:53 +0000828/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
829/// not, return 0.
Jay Foad4ba2a172011-01-12 09:06:06 +0000830unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000831 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000832 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000833 // ImplicitCastExpr.
834 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
835 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000836 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000838 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
839 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000840 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Anders Carlssonbcba2012008-01-31 02:13:57 +0000842 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
843 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000844 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000846 if (!FDecl->getIdentifier())
847 return 0;
848
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000849 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000850}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000851
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000852QualType CallExpr::getCallReturnType() const {
853 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000854 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000855 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000856 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000857 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000858 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
859 // This should never be overloaded and so should never return null.
860 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000861
John McCall864c0412011-04-26 20:42:42 +0000862 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000863 return FnType->getResultType();
864}
Chris Lattnercb888962008-10-06 05:00:53 +0000865
John McCall2882eca2011-02-21 06:23:05 +0000866SourceRange CallExpr::getSourceRange() const {
867 if (isa<CXXOperatorCallExpr>(this))
868 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
869
870 SourceLocation begin = getCallee()->getLocStart();
871 if (begin.isInvalid() && getNumArgs() > 0)
872 begin = getArg(0)->getLocStart();
873 SourceLocation end = getRParenLoc();
874 if (end.isInvalid() && getNumArgs() > 0)
875 end = getArg(getNumArgs() - 1)->getLocEnd();
876 return SourceRange(begin, end);
877}
878
Sean Huntc3021132010-05-05 15:23:54 +0000879OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000880 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000881 TypeSourceInfo *tsi,
882 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000883 Expr** exprsPtr, unsigned numExprs,
884 SourceLocation RParenLoc) {
885 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000886 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000887 sizeof(Expr*) * numExprs);
888
889 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
890 exprsPtr, numExprs, RParenLoc);
891}
892
893OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
894 unsigned numComps, unsigned numExprs) {
895 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
896 sizeof(OffsetOfNode) * numComps +
897 sizeof(Expr*) * numExprs);
898 return new (Mem) OffsetOfExpr(numComps, numExprs);
899}
900
Sean Huntc3021132010-05-05 15:23:54 +0000901OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000902 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000903 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000904 Expr** exprsPtr, unsigned numExprs,
905 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000906 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
907 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000908 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000909 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000910 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000911 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
912 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000913{
914 for(unsigned i = 0; i < numComps; ++i) {
915 setComponent(i, compsPtr[i]);
916 }
Sean Huntc3021132010-05-05 15:23:54 +0000917
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000918 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000919 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
920 ExprBits.ValueDependent = true;
921 if (exprsPtr[i]->containsUnexpandedParameterPack())
922 ExprBits.ContainsUnexpandedParameterPack = true;
923
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000924 setIndexExpr(i, exprsPtr[i]);
925 }
926}
927
928IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
929 assert(getKind() == Field || getKind() == Identifier);
930 if (getKind() == Field)
931 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000932
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000933 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
934}
935
Mike Stump1eb44332009-09-09 15:08:12 +0000936MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000937 NestedNameSpecifierLoc QualifierLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000938 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000939 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000940 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000941 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000942 QualType ty,
943 ExprValueKind vk,
944 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000945 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000946
Douglas Gregor40d96a62011-02-28 21:54:11 +0000947 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000948 founddecl.getDecl() != memberdecl ||
949 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000950 if (hasQualOrFound)
951 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000952
John McCalld5532b62009-11-23 01:53:49 +0000953 if (targs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000954 Size += ASTTemplateArgumentListInfo::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Chris Lattner32488542010-10-30 05:14:06 +0000956 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000957 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
958 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000959
960 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000961 // FIXME: Wrong. We should be looking at the member declaration we found.
962 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000963 E->setValueDependent(true);
964 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +0000965 E->setInstantiationDependent(true);
966 }
967 else if (QualifierLoc &&
968 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
969 E->setInstantiationDependent(true);
970
John McCall6bb80172010-03-30 21:47:33 +0000971 E->HasQualifierOrFoundDecl = true;
972
973 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000974 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000975 NQ->FoundDecl = founddecl;
976 }
977
978 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000979 bool Dependent = false;
980 bool InstantiationDependent = false;
981 bool ContainsUnexpandedParameterPack = false;
John McCall6bb80172010-03-30 21:47:33 +0000982 E->HasExplicitTemplateArgumentList = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000983 E->getExplicitTemplateArgs().initializeFrom(*targs, Dependent,
984 InstantiationDependent,
985 ContainsUnexpandedParameterPack);
986 if (InstantiationDependent)
987 E->setInstantiationDependent(true);
John McCall6bb80172010-03-30 21:47:33 +0000988 }
989
990 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000991}
992
Douglas Gregor75e85042011-03-02 21:06:53 +0000993SourceRange MemberExpr::getSourceRange() const {
994 SourceLocation StartLoc;
995 if (isImplicitAccess()) {
996 if (hasQualifier())
997 StartLoc = getQualifierLoc().getBeginLoc();
998 else
999 StartLoc = MemberLoc;
1000 } else {
1001 // FIXME: We don't want this to happen. Rather, we should be able to
1002 // detect all kinds of implicit accesses more cleanly.
1003 StartLoc = getBase()->getLocStart();
1004 if (StartLoc.isInvalid())
1005 StartLoc = MemberLoc;
1006 }
1007
1008 SourceLocation EndLoc =
1009 HasExplicitTemplateArgumentList? getRAngleLoc()
1010 : getMemberNameInfo().getEndLoc();
1011
1012 return SourceRange(StartLoc, EndLoc);
1013}
1014
John McCall1d9b3b22011-09-09 05:25:32 +00001015void CastExpr::CheckCastConsistency() const {
1016 switch (getCastKind()) {
1017 case CK_DerivedToBase:
1018 case CK_UncheckedDerivedToBase:
1019 case CK_DerivedToBaseMemberPointer:
1020 case CK_BaseToDerived:
1021 case CK_BaseToDerivedMemberPointer:
1022 assert(!path_empty() && "Cast kind should have a base path!");
1023 break;
1024
1025 case CK_CPointerToObjCPointerCast:
1026 assert(getType()->isObjCObjectPointerType());
1027 assert(getSubExpr()->getType()->isPointerType());
1028 goto CheckNoBasePath;
1029
1030 case CK_BlockPointerToObjCPointerCast:
1031 assert(getType()->isObjCObjectPointerType());
1032 assert(getSubExpr()->getType()->isBlockPointerType());
1033 goto CheckNoBasePath;
1034
1035 case CK_BitCast:
1036 // Arbitrary casts to C pointer types count as bitcasts.
1037 // Otherwise, we should only have block and ObjC pointer casts
1038 // here if they stay within the type kind.
1039 if (!getType()->isPointerType()) {
1040 assert(getType()->isObjCObjectPointerType() ==
1041 getSubExpr()->getType()->isObjCObjectPointerType());
1042 assert(getType()->isBlockPointerType() ==
1043 getSubExpr()->getType()->isBlockPointerType());
1044 }
1045 goto CheckNoBasePath;
1046
1047 case CK_AnyPointerToBlockPointerCast:
1048 assert(getType()->isBlockPointerType());
1049 assert(getSubExpr()->getType()->isAnyPointerType() &&
1050 !getSubExpr()->getType()->isBlockPointerType());
1051 goto CheckNoBasePath;
1052
1053 // These should not have an inheritance path.
1054 case CK_Dynamic:
1055 case CK_ToUnion:
1056 case CK_ArrayToPointerDecay:
1057 case CK_FunctionToPointerDecay:
1058 case CK_NullToMemberPointer:
1059 case CK_NullToPointer:
1060 case CK_ConstructorConversion:
1061 case CK_IntegralToPointer:
1062 case CK_PointerToIntegral:
1063 case CK_ToVoid:
1064 case CK_VectorSplat:
1065 case CK_IntegralCast:
1066 case CK_IntegralToFloating:
1067 case CK_FloatingToIntegral:
1068 case CK_FloatingCast:
1069 case CK_ObjCObjectLValueCast:
1070 case CK_FloatingRealToComplex:
1071 case CK_FloatingComplexToReal:
1072 case CK_FloatingComplexCast:
1073 case CK_FloatingComplexToIntegralComplex:
1074 case CK_IntegralRealToComplex:
1075 case CK_IntegralComplexToReal:
1076 case CK_IntegralComplexCast:
1077 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001078 case CK_ARCProduceObject:
1079 case CK_ARCConsumeObject:
1080 case CK_ARCReclaimReturnedObject:
1081 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001082 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1083 goto CheckNoBasePath;
1084
1085 case CK_Dependent:
1086 case CK_LValueToRValue:
1087 case CK_GetObjCProperty:
1088 case CK_NoOp:
1089 case CK_PointerToBoolean:
1090 case CK_IntegralToBoolean:
1091 case CK_FloatingToBoolean:
1092 case CK_MemberPointerToBoolean:
1093 case CK_FloatingComplexToBoolean:
1094 case CK_IntegralComplexToBoolean:
1095 case CK_LValueBitCast: // -> bool&
1096 case CK_UserDefinedConversion: // operator bool()
1097 CheckNoBasePath:
1098 assert(path_empty() && "Cast kind should not have a base path!");
1099 break;
1100 }
1101}
1102
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001103const char *CastExpr::getCastKindName() const {
1104 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001105 case CK_Dependent:
1106 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001107 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001108 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001109 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001110 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001111 case CK_LValueToRValue:
1112 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +00001113 case CK_GetObjCProperty:
1114 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +00001115 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001116 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001117 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001118 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001119 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001120 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001121 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001122 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001123 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001124 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001125 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001126 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001127 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001128 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001129 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001130 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001131 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001132 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001133 case CK_NullToPointer:
1134 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001135 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001136 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001137 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001138 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001139 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001140 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001141 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001142 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001143 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001144 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001145 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001146 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001147 case CK_PointerToBoolean:
1148 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001149 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001150 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001151 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001152 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001153 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001154 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001155 case CK_IntegralToBoolean:
1156 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001157 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001158 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001159 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001160 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001161 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001162 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001163 case CK_FloatingToBoolean:
1164 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001165 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001166 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001167 case CK_CPointerToObjCPointerCast:
1168 return "CPointerToObjCPointerCast";
1169 case CK_BlockPointerToObjCPointerCast:
1170 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001171 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001172 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001173 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001174 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001175 case CK_FloatingRealToComplex:
1176 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001177 case CK_FloatingComplexToReal:
1178 return "FloatingComplexToReal";
1179 case CK_FloatingComplexToBoolean:
1180 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001181 case CK_FloatingComplexCast:
1182 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001183 case CK_FloatingComplexToIntegralComplex:
1184 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001185 case CK_IntegralRealToComplex:
1186 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001187 case CK_IntegralComplexToReal:
1188 return "IntegralComplexToReal";
1189 case CK_IntegralComplexToBoolean:
1190 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001191 case CK_IntegralComplexCast:
1192 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001193 case CK_IntegralComplexToFloatingComplex:
1194 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001195 case CK_ARCConsumeObject:
1196 return "ARCConsumeObject";
1197 case CK_ARCProduceObject:
1198 return "ARCProduceObject";
1199 case CK_ARCReclaimReturnedObject:
1200 return "ARCReclaimReturnedObject";
1201 case CK_ARCExtendBlockObject:
1202 return "ARCCExtendBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001203 }
Mike Stump1eb44332009-09-09 15:08:12 +00001204
John McCall2bb5d002010-11-13 09:02:35 +00001205 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001206 return 0;
1207}
1208
Douglas Gregor6eef5192009-12-14 19:27:10 +00001209Expr *CastExpr::getSubExprAsWritten() {
1210 Expr *SubExpr = 0;
1211 CastExpr *E = this;
1212 do {
1213 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001214
1215 // Skip through reference binding to temporary.
1216 if (MaterializeTemporaryExpr *Materialize
1217 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1218 SubExpr = Materialize->GetTemporaryExpr();
1219
Douglas Gregor6eef5192009-12-14 19:27:10 +00001220 // Skip any temporary bindings; they're implicit.
1221 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1222 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001223
Douglas Gregor6eef5192009-12-14 19:27:10 +00001224 // Conversions by constructor and conversion functions have a
1225 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001226 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001227 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001228 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001229 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001230
Douglas Gregor6eef5192009-12-14 19:27:10 +00001231 // If the subexpression we're left with is an implicit cast, look
1232 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001233 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1234
Douglas Gregor6eef5192009-12-14 19:27:10 +00001235 return SubExpr;
1236}
1237
John McCallf871d0c2010-08-07 06:22:56 +00001238CXXBaseSpecifier **CastExpr::path_buffer() {
1239 switch (getStmtClass()) {
1240#define ABSTRACT_STMT(x)
1241#define CASTEXPR(Type, Base) \
1242 case Stmt::Type##Class: \
1243 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1244#define STMT(Type, Base)
1245#include "clang/AST/StmtNodes.inc"
1246 default:
1247 llvm_unreachable("non-cast expressions not possible here");
1248 return 0;
1249 }
1250}
1251
1252void CastExpr::setCastPath(const CXXCastPath &Path) {
1253 assert(Path.size() == path_size());
1254 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1255}
1256
1257ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1258 CastKind Kind, Expr *Operand,
1259 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001260 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001261 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1262 void *Buffer =
1263 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1264 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001265 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001266 if (PathSize) E->setCastPath(*BasePath);
1267 return E;
1268}
1269
1270ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1271 unsigned PathSize) {
1272 void *Buffer =
1273 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1274 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1275}
1276
1277
1278CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001279 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001280 const CXXCastPath *BasePath,
1281 TypeSourceInfo *WrittenTy,
1282 SourceLocation L, SourceLocation R) {
1283 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1284 void *Buffer =
1285 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1286 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001287 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001288 if (PathSize) E->setCastPath(*BasePath);
1289 return E;
1290}
1291
1292CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1293 void *Buffer =
1294 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1295 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1296}
1297
Reid Spencer5f016e22007-07-11 17:01:13 +00001298/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1299/// corresponds to, e.g. "<<=".
1300const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1301 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001302 case BO_PtrMemD: return ".*";
1303 case BO_PtrMemI: return "->*";
1304 case BO_Mul: return "*";
1305 case BO_Div: return "/";
1306 case BO_Rem: return "%";
1307 case BO_Add: return "+";
1308 case BO_Sub: return "-";
1309 case BO_Shl: return "<<";
1310 case BO_Shr: return ">>";
1311 case BO_LT: return "<";
1312 case BO_GT: return ">";
1313 case BO_LE: return "<=";
1314 case BO_GE: return ">=";
1315 case BO_EQ: return "==";
1316 case BO_NE: return "!=";
1317 case BO_And: return "&";
1318 case BO_Xor: return "^";
1319 case BO_Or: return "|";
1320 case BO_LAnd: return "&&";
1321 case BO_LOr: return "||";
1322 case BO_Assign: return "=";
1323 case BO_MulAssign: return "*=";
1324 case BO_DivAssign: return "/=";
1325 case BO_RemAssign: return "%=";
1326 case BO_AddAssign: return "+=";
1327 case BO_SubAssign: return "-=";
1328 case BO_ShlAssign: return "<<=";
1329 case BO_ShrAssign: return ">>=";
1330 case BO_AndAssign: return "&=";
1331 case BO_XorAssign: return "^=";
1332 case BO_OrAssign: return "|=";
1333 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001334 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001335
1336 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001337}
1338
John McCall2de56d12010-08-25 11:45:40 +00001339BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001340BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1341 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001342 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001343 case OO_Plus: return BO_Add;
1344 case OO_Minus: return BO_Sub;
1345 case OO_Star: return BO_Mul;
1346 case OO_Slash: return BO_Div;
1347 case OO_Percent: return BO_Rem;
1348 case OO_Caret: return BO_Xor;
1349 case OO_Amp: return BO_And;
1350 case OO_Pipe: return BO_Or;
1351 case OO_Equal: return BO_Assign;
1352 case OO_Less: return BO_LT;
1353 case OO_Greater: return BO_GT;
1354 case OO_PlusEqual: return BO_AddAssign;
1355 case OO_MinusEqual: return BO_SubAssign;
1356 case OO_StarEqual: return BO_MulAssign;
1357 case OO_SlashEqual: return BO_DivAssign;
1358 case OO_PercentEqual: return BO_RemAssign;
1359 case OO_CaretEqual: return BO_XorAssign;
1360 case OO_AmpEqual: return BO_AndAssign;
1361 case OO_PipeEqual: return BO_OrAssign;
1362 case OO_LessLess: return BO_Shl;
1363 case OO_GreaterGreater: return BO_Shr;
1364 case OO_LessLessEqual: return BO_ShlAssign;
1365 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1366 case OO_EqualEqual: return BO_EQ;
1367 case OO_ExclaimEqual: return BO_NE;
1368 case OO_LessEqual: return BO_LE;
1369 case OO_GreaterEqual: return BO_GE;
1370 case OO_AmpAmp: return BO_LAnd;
1371 case OO_PipePipe: return BO_LOr;
1372 case OO_Comma: return BO_Comma;
1373 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001374 }
1375}
1376
1377OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1378 static const OverloadedOperatorKind OverOps[] = {
1379 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1380 OO_Star, OO_Slash, OO_Percent,
1381 OO_Plus, OO_Minus,
1382 OO_LessLess, OO_GreaterGreater,
1383 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1384 OO_EqualEqual, OO_ExclaimEqual,
1385 OO_Amp,
1386 OO_Caret,
1387 OO_Pipe,
1388 OO_AmpAmp,
1389 OO_PipePipe,
1390 OO_Equal, OO_StarEqual,
1391 OO_SlashEqual, OO_PercentEqual,
1392 OO_PlusEqual, OO_MinusEqual,
1393 OO_LessLessEqual, OO_GreaterGreaterEqual,
1394 OO_AmpEqual, OO_CaretEqual,
1395 OO_PipeEqual,
1396 OO_Comma
1397 };
1398 return OverOps[Opc];
1399}
1400
Ted Kremenek709210f2010-04-13 23:39:13 +00001401InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001402 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001403 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001404 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001405 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001406 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001407 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +00001408 HadArrayRangeDesignator(false)
Sean Huntc3021132010-05-05 15:23:54 +00001409{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001410 for (unsigned I = 0; I != numInits; ++I) {
1411 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001412 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001413 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001414 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001415 if (initExprs[I]->isInstantiationDependent())
1416 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001417 if (initExprs[I]->containsUnexpandedParameterPack())
1418 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001419 }
Sean Huntc3021132010-05-05 15:23:54 +00001420
Ted Kremenek709210f2010-04-13 23:39:13 +00001421 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001422}
Reid Spencer5f016e22007-07-11 17:01:13 +00001423
Ted Kremenek709210f2010-04-13 23:39:13 +00001424void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001425 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001426 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001427}
1428
Ted Kremenek709210f2010-04-13 23:39:13 +00001429void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001430 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001431}
1432
Ted Kremenek709210f2010-04-13 23:39:13 +00001433Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001434 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001435 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001436 InitExprs.back() = expr;
1437 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001438 }
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Douglas Gregor4c678342009-01-28 21:54:33 +00001440 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1441 InitExprs[Init] = expr;
1442 return Result;
1443}
1444
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001445void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001446 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001447 ArrayFillerOrUnionFieldInit = filler;
1448 // Fill out any "holes" in the array due to designated initializers.
1449 Expr **inits = getInits();
1450 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1451 if (inits[i] == 0)
1452 inits[i] = filler;
1453}
1454
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001455SourceRange InitListExpr::getSourceRange() const {
1456 if (SyntacticForm)
1457 return SyntacticForm->getSourceRange();
1458 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1459 if (Beg.isInvalid()) {
1460 // Find the first non-null initializer.
1461 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1462 E = InitExprs.end();
1463 I != E; ++I) {
1464 if (Stmt *S = *I) {
1465 Beg = S->getLocStart();
1466 break;
1467 }
1468 }
1469 }
1470 if (End.isInvalid()) {
1471 // Find the first non-null initializer from the end.
1472 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1473 E = InitExprs.rend();
1474 I != E; ++I) {
1475 if (Stmt *S = *I) {
1476 End = S->getSourceRange().getEnd();
1477 break;
1478 }
1479 }
1480 }
1481 return SourceRange(Beg, End);
1482}
1483
Steve Naroffbfdcae62008-09-04 15:31:07 +00001484/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001485///
1486const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001487 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001488 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001489}
1490
Mike Stump1eb44332009-09-09 15:08:12 +00001491SourceLocation BlockExpr::getCaretLocation() const {
1492 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001493}
Mike Stump1eb44332009-09-09 15:08:12 +00001494const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001495 return TheBlock->getBody();
1496}
Mike Stump1eb44332009-09-09 15:08:12 +00001497Stmt *BlockExpr::getBody() {
1498 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001499}
Steve Naroff56ee6892008-10-08 17:01:13 +00001500
1501
Reid Spencer5f016e22007-07-11 17:01:13 +00001502//===----------------------------------------------------------------------===//
1503// Generic Expression Routines
1504//===----------------------------------------------------------------------===//
1505
Chris Lattner026dc962009-02-14 07:37:35 +00001506/// isUnusedResultAWarning - Return true if this immediate expression should
1507/// be warned about if the result is unused. If so, fill in Loc and Ranges
1508/// with location to warn on and the source range[s] to report with the
1509/// warning.
1510bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001511 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001512 // Don't warn if the expr is type dependent. The type could end up
1513 // instantiating to void.
1514 if (isTypeDependent())
1515 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Reid Spencer5f016e22007-07-11 17:01:13 +00001517 switch (getStmtClass()) {
1518 default:
John McCall0faede62010-03-12 07:11:26 +00001519 if (getType()->isVoidType())
1520 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001521 Loc = getExprLoc();
1522 R1 = getSourceRange();
1523 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001525 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001526 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001527 case GenericSelectionExprClass:
1528 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1529 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001530 case UnaryOperatorClass: {
1531 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Reid Spencer5f016e22007-07-11 17:01:13 +00001533 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001534 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001535 case UO_PostInc:
1536 case UO_PostDec:
1537 case UO_PreInc:
1538 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001539 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001540 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001541 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001542 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001543 return false;
1544 break;
John McCall2de56d12010-08-25 11:45:40 +00001545 case UO_Real:
1546 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001547 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001548 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1549 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001550 return false;
1551 break;
John McCall2de56d12010-08-25 11:45:40 +00001552 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001553 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001554 }
Chris Lattner026dc962009-02-14 07:37:35 +00001555 Loc = UO->getOperatorLoc();
1556 R1 = UO->getSubExpr()->getSourceRange();
1557 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001559 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001560 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001561 switch (BO->getOpcode()) {
1562 default:
1563 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001564 // Consider the RHS of comma for side effects. LHS was checked by
1565 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001566 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001567 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1568 // lvalue-ness) of an assignment written in a macro.
1569 if (IntegerLiteral *IE =
1570 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1571 if (IE->getValue() == 0)
1572 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001573 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1574 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001575 case BO_LAnd:
1576 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001577 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1578 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1579 return false;
1580 break;
John McCallbf0ee352010-02-16 04:10:53 +00001581 }
Chris Lattner026dc962009-02-14 07:37:35 +00001582 if (BO->isAssignmentOp())
1583 return false;
1584 Loc = BO->getOperatorLoc();
1585 R1 = BO->getLHS()->getSourceRange();
1586 R2 = BO->getRHS()->getSourceRange();
1587 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001588 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001589 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001590 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001591 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001592 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001593
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001594 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001595 // If only one of the LHS or RHS is a warning, the operator might
1596 // be being used for control flow. Only warn if both the LHS and
1597 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001598 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001599 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1600 return false;
1601 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001602 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001603 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001604 }
1605
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001607 // If the base pointer or element is to a volatile pointer/field, accessing
1608 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001609 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001610 return false;
1611 Loc = cast<MemberExpr>(this)->getMemberLoc();
1612 R1 = SourceRange(Loc, Loc);
1613 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1614 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 case ArraySubscriptExprClass:
1617 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001618 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001619 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001620 return false;
1621 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1622 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1623 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1624 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001625
Chandler Carruth9b106832011-08-17 09:49:44 +00001626 case CXXOperatorCallExprClass: {
1627 // We warn about operator== and operator!= even when user-defined operator
1628 // overloads as there is no reasonable way to define these such that they
1629 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1630 // warning: these operators are commonly typo'ed, and so warning on them
1631 // provides additional value as well. If this list is updated,
1632 // DiagnoseUnusedComparison should be as well.
1633 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1634 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001635 Op->getOperator() == OO_ExclaimEqual) {
1636 Loc = Op->getOperatorLoc();
1637 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001638 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001639 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001640
1641 // Fallthrough for generic call handling.
1642 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001643 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001644 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001645 // If this is a direct call, get the callee.
1646 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001647 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001648 // If the callee has attribute pure, const, or warn_unused_result, warn
1649 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001650 //
1651 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1652 // updated to match for QoI.
1653 if (FD->getAttr<WarnUnusedResultAttr>() ||
1654 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1655 Loc = CE->getCallee()->getLocStart();
1656 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001658 if (unsigned NumArgs = CE->getNumArgs())
1659 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1660 CE->getArg(NumArgs-1)->getLocEnd());
1661 return true;
1662 }
Chris Lattner026dc962009-02-14 07:37:35 +00001663 }
1664 return false;
1665 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001666
1667 case CXXTemporaryObjectExprClass:
1668 case CXXConstructExprClass:
1669 return false;
1670
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001671 case ObjCMessageExprClass: {
1672 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001673 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1674 ME->isInstanceMessage() &&
1675 !ME->getType()->isVoidType() &&
1676 ME->getSelector().getIdentifierInfoForSlot(0) &&
1677 ME->getSelector().getIdentifierInfoForSlot(0)
1678 ->getName().startswith("init")) {
1679 Loc = getExprLoc();
1680 R1 = ME->getSourceRange();
1681 return true;
1682 }
1683
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001684 const ObjCMethodDecl *MD = ME->getMethodDecl();
1685 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1686 Loc = getExprLoc();
1687 return true;
1688 }
Chris Lattner026dc962009-02-14 07:37:35 +00001689 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001690 }
Mike Stump1eb44332009-09-09 15:08:12 +00001691
John McCall12f78a62010-12-02 01:19:52 +00001692 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001693 Loc = getExprLoc();
1694 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001695 return true;
John McCall12f78a62010-12-02 01:19:52 +00001696
Chris Lattner611b2ec2008-07-26 19:51:01 +00001697 case StmtExprClass: {
1698 // Statement exprs don't logically have side effects themselves, but are
1699 // sometimes used in macros in ways that give them a type that is unused.
1700 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1701 // however, if the result of the stmt expr is dead, we don't want to emit a
1702 // warning.
1703 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001704 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001705 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001706 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001707 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1708 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1709 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1710 }
Mike Stump1eb44332009-09-09 15:08:12 +00001711
John McCall0faede62010-03-12 07:11:26 +00001712 if (getType()->isVoidType())
1713 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001714 Loc = cast<StmtExpr>(this)->getLParenLoc();
1715 R1 = getSourceRange();
1716 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001717 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001718 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001719 // If this is an explicit cast to void, allow it. People do this when they
1720 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001721 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001722 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001723 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1724 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1725 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001726 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001727 if (getType()->isVoidType())
1728 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001729 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001730
Anders Carlsson58beed92009-11-17 17:11:23 +00001731 // If this is a cast to void or a constructor conversion, check the operand.
1732 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001733 if (CE->getCastKind() == CK_ToVoid ||
1734 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001735 return (cast<CastExpr>(this)->getSubExpr()
1736 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001737 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1738 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1739 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001740 }
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Eli Friedman4be1f472008-05-19 21:24:43 +00001742 case ImplicitCastExprClass:
1743 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001744 return (cast<ImplicitCastExpr>(this)
1745 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001746
Chris Lattner04421082008-04-08 04:40:51 +00001747 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001748 return (cast<CXXDefaultArgExpr>(this)
1749 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001750
1751 case CXXNewExprClass:
1752 // FIXME: In theory, there might be new expressions that don't have side
1753 // effects (e.g. a placement new with an uninitialized POD).
1754 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001755 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001756 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001757 return (cast<CXXBindTemporaryExpr>(this)
1758 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001759 case ExprWithCleanupsClass:
1760 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001761 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001762 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001763}
1764
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001765/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001766/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001767bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001768 const Expr *E = IgnoreParens();
1769 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001770 default:
1771 return false;
1772 case ObjCIvarRefExprClass:
1773 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001774 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001775 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001776 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001777 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001778 case MaterializeTemporaryExprClass:
1779 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1780 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001781 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001782 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001783 case BlockDeclRefExprClass:
Douglas Gregora2813ce2009-10-23 18:54:35 +00001784 case DeclRefExprClass: {
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001785
1786 const Decl *D;
1787 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1788 D = BDRE->getDecl();
1789 else
1790 D = cast<DeclRefExpr>(E)->getDecl();
1791
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001792 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1793 if (VD->hasGlobalStorage())
1794 return true;
1795 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001796 // dereferencing to a pointer is always a gc'able candidate,
1797 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001798 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001799 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001800 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001801 return false;
1802 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001803 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001804 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001805 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001806 }
1807 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001808 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001809 }
1810}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001811
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001812bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1813 if (isTypeDependent())
1814 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001815 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001816}
1817
John McCall864c0412011-04-26 20:42:42 +00001818QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00001819 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00001820
1821 // Bound member expressions are always one of these possibilities:
1822 // x->m x.m x->*y x.*y
1823 // (possibly parenthesized)
1824
1825 expr = expr->IgnoreParens();
1826 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1827 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1828 return mem->getMemberDecl()->getType();
1829 }
1830
1831 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1832 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1833 ->getPointeeType();
1834 assert(type->isFunctionType());
1835 return type;
1836 }
1837
1838 assert(isa<UnresolvedMemberExpr>(expr));
1839 return QualType();
1840}
1841
Sebastian Redl369e51f2010-09-10 20:55:33 +00001842static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1843 Expr::CanThrowResult CT2) {
1844 // CanThrowResult constants are ordered so that the maximum is the correct
1845 // merge result.
1846 return CT1 > CT2 ? CT1 : CT2;
1847}
1848
1849static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1850 Expr *E = const_cast<Expr*>(CE);
1851 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001852 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001853 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1854 }
1855 return R;
1856}
1857
Richard Smith7a614d82011-06-11 17:19:42 +00001858static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1859 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001860 bool NullThrows = true) {
1861 if (!D)
1862 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1863
1864 // See if we can get a function type from the decl somehow.
1865 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1866 if (!VD) // If we have no clue what we're calling, assume the worst.
1867 return Expr::CT_Can;
1868
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001869 // As an extension, we assume that __attribute__((nothrow)) functions don't
1870 // throw.
1871 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1872 return Expr::CT_Cannot;
1873
Sebastian Redl369e51f2010-09-10 20:55:33 +00001874 QualType T = VD->getType();
1875 const FunctionProtoType *FT;
1876 if ((FT = T->getAs<FunctionProtoType>())) {
1877 } else if (const PointerType *PT = T->getAs<PointerType>())
1878 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1879 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1880 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1881 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1882 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1883 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1884 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1885
1886 if (!FT)
1887 return Expr::CT_Can;
1888
Richard Smith7a614d82011-06-11 17:19:42 +00001889 if (FT->getExceptionSpecType() == EST_Delayed) {
1890 assert(isa<CXXConstructorDecl>(D) &&
1891 "only constructor exception specs can be unknown");
1892 Ctx.getDiagnostics().Report(E->getLocStart(),
1893 diag::err_exception_spec_unknown)
1894 << E->getSourceRange();
1895 return Expr::CT_Can;
1896 }
1897
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001898 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001899}
1900
1901static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1902 if (DC->isTypeDependent())
1903 return Expr::CT_Dependent;
1904
Sebastian Redl295995c2010-09-10 20:55:47 +00001905 if (!DC->getTypeAsWritten()->isReferenceType())
1906 return Expr::CT_Cannot;
1907
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001908 if (DC->getSubExpr()->isTypeDependent())
1909 return Expr::CT_Dependent;
1910
Sebastian Redl369e51f2010-09-10 20:55:33 +00001911 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1912}
1913
1914static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1915 const CXXTypeidExpr *DC) {
1916 if (DC->isTypeOperand())
1917 return Expr::CT_Cannot;
1918
1919 Expr *Op = DC->getExprOperand();
1920 if (Op->isTypeDependent())
1921 return Expr::CT_Dependent;
1922
1923 const RecordType *RT = Op->getType()->getAs<RecordType>();
1924 if (!RT)
1925 return Expr::CT_Cannot;
1926
1927 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1928 return Expr::CT_Cannot;
1929
1930 if (Op->Classify(C).isPRValue())
1931 return Expr::CT_Cannot;
1932
1933 return Expr::CT_Can;
1934}
1935
1936Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1937 // C++ [expr.unary.noexcept]p3:
1938 // [Can throw] if in a potentially-evaluated context the expression would
1939 // contain:
1940 switch (getStmtClass()) {
1941 case CXXThrowExprClass:
1942 // - a potentially evaluated throw-expression
1943 return CT_Can;
1944
1945 case CXXDynamicCastExprClass: {
1946 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1947 // where T is a reference type, that requires a run-time check
1948 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1949 if (CT == CT_Can)
1950 return CT;
1951 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1952 }
1953
1954 case CXXTypeidExprClass:
1955 // - a potentially evaluated typeid expression applied to a glvalue
1956 // expression whose type is a polymorphic class type
1957 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1958
1959 // - a potentially evaluated call to a function, member function, function
1960 // pointer, or member function pointer that does not have a non-throwing
1961 // exception-specification
1962 case CallExprClass:
1963 case CXXOperatorCallExprClass:
1964 case CXXMemberCallExprClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001965 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001966 CanThrowResult CT;
1967 if (isTypeDependent())
1968 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001969 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1970 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001971 else
Richard Smith7a614d82011-06-11 17:19:42 +00001972 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001973 if (CT == CT_Can)
1974 return CT;
1975 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1976 }
1977
Sebastian Redl295995c2010-09-10 20:55:47 +00001978 case CXXConstructExprClass:
1979 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00001980 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001981 cast<CXXConstructExpr>(this)->getConstructor());
1982 if (CT == CT_Can)
1983 return CT;
1984 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1985 }
1986
1987 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001988 CanThrowResult CT;
1989 if (isTypeDependent())
1990 CT = CT_Dependent;
1991 else
1992 CT = MergeCanThrow(
Richard Smith7a614d82011-06-11 17:19:42 +00001993 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
1994 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00001995 /*NullThrows*/false));
1996 if (CT == CT_Can)
1997 return CT;
1998 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1999 }
2000
2001 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002002 CanThrowResult CT;
2003 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
2004 if (DTy.isNull() || DTy->isDependentType()) {
2005 CT = CT_Dependent;
2006 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00002007 CT = CanCalleeThrow(C, this,
2008 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002009 if (const RecordType *RT = DTy->getAs<RecordType>()) {
2010 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00002011 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002012 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002013 if (CT == CT_Can)
2014 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002015 }
2016 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2017 }
2018
2019 case CXXBindTemporaryExprClass: {
2020 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00002021 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002022 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
2023 if (CT == CT_Can)
2024 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00002025 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2026 }
2027
2028 // ObjC message sends are like function calls, but never have exception
2029 // specs.
2030 case ObjCMessageExprClass:
2031 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002032 return CT_Can;
2033
2034 // Many other things have subexpressions, so we have to test those.
2035 // Some are simple:
2036 case ParenExprClass:
2037 case MemberExprClass:
2038 case CXXReinterpretCastExprClass:
2039 case CXXConstCastExprClass:
2040 case ConditionalOperatorClass:
2041 case CompoundLiteralExprClass:
2042 case ExtVectorElementExprClass:
2043 case InitListExprClass:
2044 case DesignatedInitExprClass:
2045 case ParenListExprClass:
2046 case VAArgExprClass:
2047 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00002048 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002049 case ObjCIvarRefExprClass:
2050 case ObjCIsaExprClass:
2051 case ShuffleVectorExprClass:
2052 return CanSubExprsThrow(C, this);
2053
2054 // Some might be dependent for other reasons.
2055 case UnaryOperatorClass:
2056 case ArraySubscriptExprClass:
2057 case ImplicitCastExprClass:
2058 case CStyleCastExprClass:
2059 case CXXStaticCastExprClass:
2060 case CXXFunctionalCastExprClass:
2061 case BinaryOperatorClass:
Douglas Gregor03e80032011-06-21 17:03:29 +00002062 case CompoundAssignOperatorClass:
2063 case MaterializeTemporaryExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002064 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2065 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2066 }
2067
2068 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2069 case StmtExprClass:
2070 return CT_Can;
2071
2072 case ChooseExprClass:
2073 if (isTypeDependent() || isValueDependent())
2074 return CT_Dependent;
2075 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2076
Peter Collingbournef111d932011-04-15 00:35:48 +00002077 case GenericSelectionExprClass:
2078 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2079 return CT_Dependent;
2080 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2081
Sebastian Redl369e51f2010-09-10 20:55:33 +00002082 // Some expressions are always dependent.
2083 case DependentScopeDeclRefExprClass:
2084 case CXXUnresolvedConstructExprClass:
2085 case CXXDependentScopeMemberExprClass:
2086 return CT_Dependent;
2087
2088 default:
2089 // All other expressions don't have subexpressions, or else they are
2090 // unevaluated.
2091 return CT_Cannot;
2092 }
2093}
2094
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002095Expr* Expr::IgnoreParens() {
2096 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002097 while (true) {
2098 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2099 E = P->getSubExpr();
2100 continue;
2101 }
2102 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2103 if (P->getOpcode() == UO_Extension) {
2104 E = P->getSubExpr();
2105 continue;
2106 }
2107 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002108 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2109 if (!P->isResultDependent()) {
2110 E = P->getResultExpr();
2111 continue;
2112 }
2113 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002114 return E;
2115 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002116}
2117
Chris Lattner56f34942008-02-13 01:02:39 +00002118/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2119/// or CastExprs or ImplicitCastExprs, returning their operand.
2120Expr *Expr::IgnoreParenCasts() {
2121 Expr *E = this;
2122 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002123 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002124 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002125 continue;
2126 }
2127 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002128 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002129 continue;
2130 }
2131 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2132 if (P->getOpcode() == UO_Extension) {
2133 E = P->getSubExpr();
2134 continue;
2135 }
2136 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002137 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2138 if (!P->isResultDependent()) {
2139 E = P->getResultExpr();
2140 continue;
2141 }
2142 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002143 if (MaterializeTemporaryExpr *Materialize
2144 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2145 E = Materialize->GetTemporaryExpr();
2146 continue;
2147 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002148 if (SubstNonTypeTemplateParmExpr *NTTP
2149 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2150 E = NTTP->getReplacement();
2151 continue;
2152 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002153 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002154 }
2155}
2156
John McCall9c5d70c2010-12-04 08:24:19 +00002157/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2158/// casts. This is intended purely as a temporary workaround for code
2159/// that hasn't yet been rewritten to do the right thing about those
2160/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002161Expr *Expr::IgnoreParenLValueCasts() {
2162 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002163 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002164 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2165 E = P->getSubExpr();
2166 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002167 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002168 if (P->getCastKind() == CK_LValueToRValue) {
2169 E = P->getSubExpr();
2170 continue;
2171 }
John McCall9c5d70c2010-12-04 08:24:19 +00002172 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2173 if (P->getOpcode() == UO_Extension) {
2174 E = P->getSubExpr();
2175 continue;
2176 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002177 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2178 if (!P->isResultDependent()) {
2179 E = P->getResultExpr();
2180 continue;
2181 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002182 } else if (MaterializeTemporaryExpr *Materialize
2183 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2184 E = Materialize->GetTemporaryExpr();
2185 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002186 } else if (SubstNonTypeTemplateParmExpr *NTTP
2187 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2188 E = NTTP->getReplacement();
2189 continue;
John McCallf6a16482010-12-04 03:47:34 +00002190 }
2191 break;
2192 }
2193 return E;
2194}
2195
John McCall2fc46bf2010-05-05 22:59:52 +00002196Expr *Expr::IgnoreParenImpCasts() {
2197 Expr *E = this;
2198 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002199 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002200 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002201 continue;
2202 }
2203 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002204 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002205 continue;
2206 }
2207 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2208 if (P->getOpcode() == UO_Extension) {
2209 E = P->getSubExpr();
2210 continue;
2211 }
2212 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002213 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2214 if (!P->isResultDependent()) {
2215 E = P->getResultExpr();
2216 continue;
2217 }
2218 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002219 if (MaterializeTemporaryExpr *Materialize
2220 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2221 E = Materialize->GetTemporaryExpr();
2222 continue;
2223 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002224 if (SubstNonTypeTemplateParmExpr *NTTP
2225 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2226 E = NTTP->getReplacement();
2227 continue;
2228 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002229 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002230 }
2231}
2232
Hans Wennborg2f072b42011-06-09 17:06:51 +00002233Expr *Expr::IgnoreConversionOperator() {
2234 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002235 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002236 return MCE->getImplicitObjectArgument();
2237 }
2238 return this;
2239}
2240
Chris Lattnerecdd8412009-03-13 17:28:01 +00002241/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2242/// value (including ptr->int casts of the same size). Strip off any
2243/// ParenExpr or CastExprs, returning their operand.
2244Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2245 Expr *E = this;
2246 while (true) {
2247 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2248 E = P->getSubExpr();
2249 continue;
2250 }
Mike Stump1eb44332009-09-09 15:08:12 +00002251
Chris Lattnerecdd8412009-03-13 17:28:01 +00002252 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2253 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002254 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002255 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002256
Chris Lattnerecdd8412009-03-13 17:28:01 +00002257 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2258 E = SE;
2259 continue;
2260 }
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002262 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002263 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002264 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002265 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002266 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2267 E = SE;
2268 continue;
2269 }
2270 }
Mike Stump1eb44332009-09-09 15:08:12 +00002271
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002272 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2273 if (P->getOpcode() == UO_Extension) {
2274 E = P->getSubExpr();
2275 continue;
2276 }
2277 }
2278
Peter Collingbournef111d932011-04-15 00:35:48 +00002279 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2280 if (!P->isResultDependent()) {
2281 E = P->getResultExpr();
2282 continue;
2283 }
2284 }
2285
Douglas Gregorc0244c52011-09-08 17:56:33 +00002286 if (SubstNonTypeTemplateParmExpr *NTTP
2287 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2288 E = NTTP->getReplacement();
2289 continue;
2290 }
2291
Chris Lattnerecdd8412009-03-13 17:28:01 +00002292 return E;
2293 }
2294}
2295
Douglas Gregor6eef5192009-12-14 19:27:10 +00002296bool Expr::isDefaultArgument() const {
2297 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002298 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2299 E = M->GetTemporaryExpr();
2300
Douglas Gregor6eef5192009-12-14 19:27:10 +00002301 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2302 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002303
Douglas Gregor6eef5192009-12-14 19:27:10 +00002304 return isa<CXXDefaultArgExpr>(E);
2305}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002306
Douglas Gregor2f599792010-04-02 18:24:57 +00002307/// \brief Skip over any no-op casts and any temporary-binding
2308/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002309static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002310 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2311 E = M->GetTemporaryExpr();
2312
Douglas Gregor2f599792010-04-02 18:24:57 +00002313 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002314 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002315 E = ICE->getSubExpr();
2316 else
2317 break;
2318 }
2319
2320 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2321 E = BE->getSubExpr();
2322
2323 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002324 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002325 E = ICE->getSubExpr();
2326 else
2327 break;
2328 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002329
2330 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002331}
2332
John McCall558d2ab2010-09-15 10:14:12 +00002333/// isTemporaryObject - Determines if this expression produces a
2334/// temporary of the given class type.
2335bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2336 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2337 return false;
2338
Anders Carlssonf8b30152010-11-28 16:40:49 +00002339 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002340
John McCall58277b52010-09-15 20:59:13 +00002341 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002342 if (!E->Classify(C).isPRValue()) {
2343 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002344 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002345 return false;
2346 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002347
John McCall19e60ad2010-09-16 06:57:56 +00002348 // Black-list a few cases which yield pr-values of class type that don't
2349 // refer to temporaries of that type:
2350
2351 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002352 if (isa<ImplicitCastExpr>(E)) {
2353 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2354 case CK_DerivedToBase:
2355 case CK_UncheckedDerivedToBase:
2356 return false;
2357 default:
2358 break;
2359 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002360 }
2361
John McCall19e60ad2010-09-16 06:57:56 +00002362 // - member expressions (all)
2363 if (isa<MemberExpr>(E))
2364 return false;
2365
John McCall56ca35d2011-02-17 10:25:35 +00002366 // - opaque values (all)
2367 if (isa<OpaqueValueExpr>(E))
2368 return false;
2369
John McCall558d2ab2010-09-15 10:14:12 +00002370 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002371}
2372
Douglas Gregor75e85042011-03-02 21:06:53 +00002373bool Expr::isImplicitCXXThis() const {
2374 const Expr *E = this;
2375
2376 // Strip away parentheses and casts we don't care about.
2377 while (true) {
2378 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2379 E = Paren->getSubExpr();
2380 continue;
2381 }
2382
2383 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2384 if (ICE->getCastKind() == CK_NoOp ||
2385 ICE->getCastKind() == CK_LValueToRValue ||
2386 ICE->getCastKind() == CK_DerivedToBase ||
2387 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2388 E = ICE->getSubExpr();
2389 continue;
2390 }
2391 }
2392
2393 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2394 if (UnOp->getOpcode() == UO_Extension) {
2395 E = UnOp->getSubExpr();
2396 continue;
2397 }
2398 }
2399
Douglas Gregor03e80032011-06-21 17:03:29 +00002400 if (const MaterializeTemporaryExpr *M
2401 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2402 E = M->GetTemporaryExpr();
2403 continue;
2404 }
2405
Douglas Gregor75e85042011-03-02 21:06:53 +00002406 break;
2407 }
2408
2409 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2410 return This->isImplicit();
2411
2412 return false;
2413}
2414
Douglas Gregor898574e2008-12-05 23:32:09 +00002415/// hasAnyTypeDependentArguments - Determines if any of the expressions
2416/// in Exprs is type-dependent.
2417bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2418 for (unsigned I = 0; I < NumExprs; ++I)
2419 if (Exprs[I]->isTypeDependent())
2420 return true;
2421
2422 return false;
2423}
2424
2425/// hasAnyValueDependentArguments - Determines if any of the expressions
2426/// in Exprs is value-dependent.
2427bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2428 for (unsigned I = 0; I < NumExprs; ++I)
2429 if (Exprs[I]->isValueDependent())
2430 return true;
2431
2432 return false;
2433}
2434
John McCall4204f072010-08-02 21:13:48 +00002435bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002436 // This function is attempting whether an expression is an initializer
2437 // which can be evaluated at compile-time. isEvaluatable handles most
2438 // of the cases, but it can't deal with some initializer-specific
2439 // expressions, and it can't deal with aggregates; we deal with those here,
2440 // and fall back to isEvaluatable for the other cases.
2441
John McCall4204f072010-08-02 21:13:48 +00002442 // If we ever capture reference-binding directly in the AST, we can
2443 // kill the second parameter.
2444
2445 if (IsForRef) {
2446 EvalResult Result;
2447 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2448 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002449
Anders Carlssone8a32b82008-11-24 05:23:59 +00002450 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002451 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002452 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002453 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002454 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002455 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002456 case CXXTemporaryObjectExprClass:
2457 case CXXConstructExprClass: {
2458 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002459
2460 // Only if it's
2461 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002462 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002463 if (!CE->getNumArgs()) return true;
2464
2465 // 2) an elidable trivial copy construction of an operand which is
2466 // itself a constant initializer. Note that we consider the
2467 // operand on its own, *not* as a reference binding.
2468 return CE->isElidable() &&
2469 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002470 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002471 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002472 // This handles gcc's extension that allows global initializers like
2473 // "struct x {int x;} x = (struct x) {};".
2474 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002475 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002476 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002477 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002478 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002479 // FIXME: This doesn't deal with fields with reference types correctly.
2480 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2481 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002482 const InitListExpr *Exp = cast<InitListExpr>(this);
2483 unsigned numInits = Exp->getNumInits();
2484 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002485 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002486 return false;
2487 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002488 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002489 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002490 case ImplicitValueInitExprClass:
2491 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002492 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002493 return cast<ParenExpr>(this)->getSubExpr()
2494 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002495 case GenericSelectionExprClass:
2496 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2497 return false;
2498 return cast<GenericSelectionExpr>(this)->getResultExpr()
2499 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002500 case ChooseExprClass:
2501 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2502 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002503 case UnaryOperatorClass: {
2504 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002505 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002506 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002507 break;
2508 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002509 case BinaryOperatorClass: {
2510 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2511 // but this handles the common case.
2512 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002513 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002514 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2515 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2516 return true;
2517 break;
2518 }
John McCall4204f072010-08-02 21:13:48 +00002519 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002520 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002521 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002522 case CStyleCastExprClass:
2523 // Handle casts with a destination that's a struct or union; this
2524 // deals with both the gcc no-op struct cast extension and the
2525 // cast-to-union extension.
2526 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002527 return cast<CastExpr>(this)->getSubExpr()
2528 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002529
Chris Lattner430656e2009-10-13 22:12:09 +00002530 // Integer->integer casts can be handled here, which is important for
2531 // things like (int)(&&x-&&y). Scary but true.
2532 if (getType()->isIntegerType() &&
2533 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002534 return cast<CastExpr>(this)->getSubExpr()
2535 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002536
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002537 break;
Douglas Gregor03e80032011-06-21 17:03:29 +00002538
2539 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002540 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002541 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002542 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002543 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002544}
2545
Chandler Carruth82214a82011-02-18 23:54:50 +00002546/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2547/// pointer constant or not, as well as the specific kind of constant detected.
2548/// Null pointer constants can be integer constant expressions with the
2549/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2550/// (a GNU extension).
2551Expr::NullPointerConstantKind
2552Expr::isNullPointerConstant(ASTContext &Ctx,
2553 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002554 if (isValueDependent()) {
2555 switch (NPC) {
2556 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002557 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002558 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002559 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2560 return NPCK_ZeroInteger;
2561 else
2562 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002563
Douglas Gregorce940492009-09-25 04:25:58 +00002564 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002565 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002566 }
2567 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002568
Sebastian Redl07779722008-10-31 14:43:28 +00002569 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002570 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002571 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002572 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002573 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002574 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002575 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002576 Pointee->isVoidType() && // to void*
2577 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002578 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002579 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002580 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002581 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2582 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002583 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002584 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2585 // Accept ((void*)0) as a null pointer constant, as many other
2586 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002587 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002588 } else if (const GenericSelectionExpr *GE =
2589 dyn_cast<GenericSelectionExpr>(this)) {
2590 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002591 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002592 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002593 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002594 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002595 } else if (isa<GNUNullExpr>(this)) {
2596 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002597 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002598 } else if (const MaterializeTemporaryExpr *M
2599 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2600 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002601 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002602
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002603 // C++0x nullptr_t is always a null pointer constant.
2604 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002605 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002606
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002607 if (const RecordType *UT = getType()->getAsUnionType())
2608 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2609 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2610 const Expr *InitExpr = CLE->getInitializer();
2611 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2612 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2613 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002614 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002615 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002616 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002617 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002618
Reid Spencer5f016e22007-07-11 17:01:13 +00002619 // If we have an integer constant expression, we need to *evaluate* it and
2620 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002621 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002622 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2623
2624 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002625}
Steve Naroff31a45842007-07-28 23:10:27 +00002626
John McCallf6a16482010-12-04 03:47:34 +00002627/// \brief If this expression is an l-value for an Objective C
2628/// property, find the underlying property reference expression.
2629const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2630 const Expr *E = this;
2631 while (true) {
2632 assert((E->getValueKind() == VK_LValue &&
2633 E->getObjectKind() == OK_ObjCProperty) &&
2634 "expression is not a property reference");
2635 E = E->IgnoreParenCasts();
2636 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2637 if (BO->getOpcode() == BO_Comma) {
2638 E = BO->getRHS();
2639 continue;
2640 }
2641 }
2642
2643 break;
2644 }
2645
2646 return cast<ObjCPropertyRefExpr>(E);
2647}
2648
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002649FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002650 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002651
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002652 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002653 if (ICE->getCastKind() == CK_LValueToRValue ||
2654 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002655 E = ICE->getSubExpr()->IgnoreParens();
2656 else
2657 break;
2658 }
2659
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002660 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002661 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002662 if (Field->isBitField())
2663 return Field;
2664
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002665 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2666 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2667 if (Field->isBitField())
2668 return Field;
2669
Eli Friedman42068e92011-07-13 02:05:57 +00002670 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002671 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2672 return BinOp->getLHS()->getBitField();
2673
Eli Friedman42068e92011-07-13 02:05:57 +00002674 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2675 return BinOp->getRHS()->getBitField();
2676 }
2677
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002678 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002679}
2680
Anders Carlsson09380262010-01-31 17:18:49 +00002681bool Expr::refersToVectorElement() const {
2682 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002683
Anders Carlsson09380262010-01-31 17:18:49 +00002684 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002685 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002686 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002687 E = ICE->getSubExpr()->IgnoreParens();
2688 else
2689 break;
2690 }
Sean Huntc3021132010-05-05 15:23:54 +00002691
Anders Carlsson09380262010-01-31 17:18:49 +00002692 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2693 return ASE->getBase()->getType()->isVectorType();
2694
2695 if (isa<ExtVectorElementExpr>(E))
2696 return true;
2697
2698 return false;
2699}
2700
Chris Lattner2140e902009-02-16 22:14:05 +00002701/// isArrow - Return true if the base expression is a pointer to vector,
2702/// return false if the base expression is a vector.
2703bool ExtVectorElementExpr::isArrow() const {
2704 return getBase()->getType()->isPointerType();
2705}
2706
Nate Begeman213541a2008-04-18 23:10:10 +00002707unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002708 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002709 return VT->getNumElements();
2710 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002711}
2712
Nate Begeman8a997642008-05-09 06:41:27 +00002713/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002714bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002715 // FIXME: Refactor this code to an accessor on the AST node which returns the
2716 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002717 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002718
2719 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002720 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002721 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Nate Begeman190d6a22009-01-18 02:01:21 +00002723 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002724 if (Comp[0] == 's' || Comp[0] == 'S')
2725 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002726
Daniel Dunbar15027422009-10-17 23:53:04 +00002727 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002728 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002729 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002730
Steve Narofffec0b492007-07-30 03:29:09 +00002731 return false;
2732}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002733
Nate Begeman8a997642008-05-09 06:41:27 +00002734/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002735void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002736 SmallVectorImpl<unsigned> &Elts) const {
2737 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002738 if (Comp[0] == 's' || Comp[0] == 'S')
2739 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002740
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002741 bool isHi = Comp == "hi";
2742 bool isLo = Comp == "lo";
2743 bool isEven = Comp == "even";
2744 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002745
Nate Begeman8a997642008-05-09 06:41:27 +00002746 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2747 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002748
Nate Begeman8a997642008-05-09 06:41:27 +00002749 if (isHi)
2750 Index = e + i;
2751 else if (isLo)
2752 Index = i;
2753 else if (isEven)
2754 Index = 2 * i;
2755 else if (isOdd)
2756 Index = 2 * i + 1;
2757 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002758 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002759
Nate Begeman3b8d1162008-05-13 21:03:02 +00002760 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002761 }
Nate Begeman8a997642008-05-09 06:41:27 +00002762}
2763
Douglas Gregor04badcf2010-04-21 00:45:42 +00002764ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002765 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002766 SourceLocation LBracLoc,
2767 SourceLocation SuperLoc,
2768 bool IsInstanceSuper,
2769 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002770 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002771 ArrayRef<SourceLocation> SelLocs,
2772 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002773 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002774 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002775 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002776 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002777 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002778 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002779 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002780 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2781 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002782 Kind(IsInstanceSuper? SuperInstance : SuperClass),
2783 HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002784 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002785{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002786 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002787 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002788}
2789
Douglas Gregor04badcf2010-04-21 00:45:42 +00002790ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002791 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002792 SourceLocation LBracLoc,
2793 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002794 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002795 ArrayRef<SourceLocation> SelLocs,
2796 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002797 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002798 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002799 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002800 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002801 T->isDependentType(), T->isInstantiationDependentType(),
2802 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002803 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2804 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002805 Kind(Class),
2806 HasMethod(Method != 0), IsDelegateInitCall(false),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002807 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002808{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002809 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002810 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002811}
2812
Douglas Gregor04badcf2010-04-21 00:45:42 +00002813ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002814 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002815 SourceLocation LBracLoc,
2816 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002817 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002818 ArrayRef<SourceLocation> SelLocs,
2819 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002820 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002821 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002822 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002823 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002824 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002825 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002826 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002827 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2828 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002829 Kind(Instance),
2830 HasMethod(Method != 0), IsDelegateInitCall(false),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002831 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002832{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002833 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002834 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002835}
2836
2837void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2838 ArrayRef<SourceLocation> SelLocs,
2839 SelectorLocationsKind SelLocsK) {
2840 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00002841 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002842 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002843 if (Args[I]->isTypeDependent())
2844 ExprBits.TypeDependent = true;
2845 if (Args[I]->isValueDependent())
2846 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002847 if (Args[I]->isInstantiationDependent())
2848 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002849 if (Args[I]->containsUnexpandedParameterPack())
2850 ExprBits.ContainsUnexpandedParameterPack = true;
2851
2852 MyArgs[I] = Args[I];
2853 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002854
2855 SelLocsKind = SelLocsK;
2856 if (SelLocsK == SelLoc_NonStandard)
2857 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
Chris Lattner0389e6b2009-04-26 00:44:05 +00002858}
2859
Douglas Gregor04badcf2010-04-21 00:45:42 +00002860ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002861 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002862 SourceLocation LBracLoc,
2863 SourceLocation SuperLoc,
2864 bool IsInstanceSuper,
2865 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002866 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002867 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002868 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002869 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002870 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002871 SelectorLocationsKind SelLocsK;
2872 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00002873 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002874 SuperType, Sel, SelLocs, SelLocsK,
2875 Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002876}
2877
2878ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002879 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002880 SourceLocation LBracLoc,
2881 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002882 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002883 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002884 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002885 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002886 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002887 SelectorLocationsKind SelLocsK;
2888 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002889 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002890 SelLocs, SelLocsK, Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002891}
2892
2893ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002894 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002895 SourceLocation LBracLoc,
2896 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002897 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002898 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002899 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002900 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002901 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002902 SelectorLocationsKind SelLocsK;
2903 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002904 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002905 SelLocs, SelLocsK, Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002906}
2907
Sean Huntc3021132010-05-05 15:23:54 +00002908ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002909 unsigned NumArgs,
2910 unsigned NumStoredSelLocs) {
2911 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002912 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2913}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002914
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002915ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2916 ArrayRef<Expr *> Args,
2917 SourceLocation RBraceLoc,
2918 ArrayRef<SourceLocation> SelLocs,
2919 Selector Sel,
2920 SelectorLocationsKind &SelLocsK) {
2921 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
2922 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
2923 : 0;
2924 return alloc(C, Args.size(), NumStoredSelLocs);
2925}
2926
2927ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2928 unsigned NumArgs,
2929 unsigned NumStoredSelLocs) {
2930 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2931 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
2932 return (ObjCMessageExpr *)C.Allocate(Size,
2933 llvm::AlignOf<ObjCMessageExpr>::Alignment);
2934}
2935
2936void ObjCMessageExpr::getSelectorLocs(
2937 SmallVectorImpl<SourceLocation> &SelLocs) const {
2938 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
2939 SelLocs.push_back(getSelectorLoc(i));
2940}
2941
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002942SourceRange ObjCMessageExpr::getReceiverRange() const {
2943 switch (getReceiverKind()) {
2944 case Instance:
2945 return getInstanceReceiver()->getSourceRange();
2946
2947 case Class:
2948 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2949
2950 case SuperInstance:
2951 case SuperClass:
2952 return getSuperLoc();
2953 }
2954
2955 return SourceLocation();
2956}
2957
Douglas Gregor04badcf2010-04-21 00:45:42 +00002958Selector ObjCMessageExpr::getSelector() const {
2959 if (HasMethod)
2960 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2961 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002962 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002963}
2964
2965ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2966 switch (getReceiverKind()) {
2967 case Instance:
2968 if (const ObjCObjectPointerType *Ptr
2969 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2970 return Ptr->getInterfaceDecl();
2971 break;
2972
2973 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002974 if (const ObjCObjectType *Ty
2975 = getClassReceiver()->getAs<ObjCObjectType>())
2976 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002977 break;
2978
2979 case SuperInstance:
2980 if (const ObjCObjectPointerType *Ptr
2981 = getSuperType()->getAs<ObjCObjectPointerType>())
2982 return Ptr->getInterfaceDecl();
2983 break;
2984
2985 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002986 if (const ObjCObjectType *Iface
2987 = getSuperType()->getAs<ObjCObjectType>())
2988 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002989 break;
2990 }
2991
2992 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002993}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002994
Chris Lattner5f9e2722011-07-23 10:55:15 +00002995StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00002996 switch (getBridgeKind()) {
2997 case OBC_Bridge:
2998 return "__bridge";
2999 case OBC_BridgeTransfer:
3000 return "__bridge_transfer";
3001 case OBC_BridgeRetained:
3002 return "__bridge_retained";
3003 }
3004
3005 return "__bridge";
3006}
3007
Jay Foad4ba2a172011-01-12 09:06:06 +00003008bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003009 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003010}
3011
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003012ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3013 QualType Type, SourceLocation BLoc,
3014 SourceLocation RP)
3015 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3016 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003017 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003018 Type->containsUnexpandedParameterPack()),
3019 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3020{
3021 SubExprs = new (C) Stmt*[nexpr];
3022 for (unsigned i = 0; i < nexpr; i++) {
3023 if (args[i]->isTypeDependent())
3024 ExprBits.TypeDependent = true;
3025 if (args[i]->isValueDependent())
3026 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003027 if (args[i]->isInstantiationDependent())
3028 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003029 if (args[i]->containsUnexpandedParameterPack())
3030 ExprBits.ContainsUnexpandedParameterPack = true;
3031
3032 SubExprs[i] = args[i];
3033 }
3034}
3035
Nate Begeman888376a2009-08-12 02:28:50 +00003036void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3037 unsigned NumExprs) {
3038 if (SubExprs) C.Deallocate(SubExprs);
3039
3040 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003041 this->NumExprs = NumExprs;
3042 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003043}
Nate Begeman888376a2009-08-12 02:28:50 +00003044
Peter Collingbournef111d932011-04-15 00:35:48 +00003045GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3046 SourceLocation GenericLoc, Expr *ControllingExpr,
3047 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3048 unsigned NumAssocs, SourceLocation DefaultLoc,
3049 SourceLocation RParenLoc,
3050 bool ContainsUnexpandedParameterPack,
3051 unsigned ResultIndex)
3052 : Expr(GenericSelectionExprClass,
3053 AssocExprs[ResultIndex]->getType(),
3054 AssocExprs[ResultIndex]->getValueKind(),
3055 AssocExprs[ResultIndex]->getObjectKind(),
3056 AssocExprs[ResultIndex]->isTypeDependent(),
3057 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003058 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003059 ContainsUnexpandedParameterPack),
3060 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3061 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3062 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3063 RParenLoc(RParenLoc) {
3064 SubExprs[CONTROLLING] = ControllingExpr;
3065 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3066 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3067}
3068
3069GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3070 SourceLocation GenericLoc, Expr *ControllingExpr,
3071 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3072 unsigned NumAssocs, SourceLocation DefaultLoc,
3073 SourceLocation RParenLoc,
3074 bool ContainsUnexpandedParameterPack)
3075 : Expr(GenericSelectionExprClass,
3076 Context.DependentTy,
3077 VK_RValue,
3078 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003079 /*isTypeDependent=*/true,
3080 /*isValueDependent=*/true,
3081 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003082 ContainsUnexpandedParameterPack),
3083 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3084 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3085 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3086 RParenLoc(RParenLoc) {
3087 SubExprs[CONTROLLING] = ControllingExpr;
3088 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3089 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3090}
3091
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003092//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003093// DesignatedInitExpr
3094//===----------------------------------------------------------------------===//
3095
Chandler Carruthb1138242011-06-16 06:47:06 +00003096IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003097 assert(Kind == FieldDesignator && "Only valid on a field designator");
3098 if (Field.NameOrField & 0x01)
3099 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3100 else
3101 return getField()->getIdentifier();
3102}
3103
Sean Huntc3021132010-05-05 15:23:54 +00003104DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003105 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003106 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003107 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003108 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003109 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003110 unsigned NumIndexExprs,
3111 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003112 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003113 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003114 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003115 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003116 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003117 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3118 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003119 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003120
3121 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003122 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003123 *Child++ = Init;
3124
3125 // Copy the designators and their subexpressions, computing
3126 // value-dependence along the way.
3127 unsigned IndexIdx = 0;
3128 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003129 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003130
3131 if (this->Designators[I].isArrayDesignator()) {
3132 // Compute type- and value-dependence.
3133 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003134 if (Index->isTypeDependent() || Index->isValueDependent())
3135 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003136 if (Index->isInstantiationDependent())
3137 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003138 // Propagate unexpanded parameter packs.
3139 if (Index->containsUnexpandedParameterPack())
3140 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003141
3142 // Copy the index expressions into permanent storage.
3143 *Child++ = IndexExprs[IndexIdx++];
3144 } else if (this->Designators[I].isArrayRangeDesignator()) {
3145 // Compute type- and value-dependence.
3146 Expr *Start = IndexExprs[IndexIdx];
3147 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003148 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003149 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003150 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003151 ExprBits.InstantiationDependent = true;
3152 } else if (Start->isInstantiationDependent() ||
3153 End->isInstantiationDependent()) {
3154 ExprBits.InstantiationDependent = true;
3155 }
3156
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003157 // Propagate unexpanded parameter packs.
3158 if (Start->containsUnexpandedParameterPack() ||
3159 End->containsUnexpandedParameterPack())
3160 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003161
3162 // Copy the start/end expressions into permanent storage.
3163 *Child++ = IndexExprs[IndexIdx++];
3164 *Child++ = IndexExprs[IndexIdx++];
3165 }
3166 }
3167
3168 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003169}
3170
Douglas Gregor05c13a32009-01-22 00:58:24 +00003171DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003172DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003173 unsigned NumDesignators,
3174 Expr **IndexExprs, unsigned NumIndexExprs,
3175 SourceLocation ColonOrEqualLoc,
3176 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003177 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003178 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003179 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003180 ColonOrEqualLoc, UsesColonSyntax,
3181 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003182}
3183
Mike Stump1eb44332009-09-09 15:08:12 +00003184DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003185 unsigned NumIndexExprs) {
3186 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3187 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3188 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3189}
3190
Douglas Gregor319d57f2010-01-06 23:17:19 +00003191void DesignatedInitExpr::setDesignators(ASTContext &C,
3192 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003193 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003194 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003195 NumDesignators = NumDesigs;
3196 for (unsigned I = 0; I != NumDesigs; ++I)
3197 Designators[I] = Desigs[I];
3198}
3199
Abramo Bagnara24f46742011-03-16 15:08:46 +00003200SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3201 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3202 if (size() == 1)
3203 return DIE->getDesignator(0)->getSourceRange();
3204 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3205 DIE->getDesignator(size()-1)->getEndLocation());
3206}
3207
Douglas Gregor05c13a32009-01-22 00:58:24 +00003208SourceRange DesignatedInitExpr::getSourceRange() const {
3209 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003210 Designator &First =
3211 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003212 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003213 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003214 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3215 else
3216 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3217 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003218 StartLoc =
3219 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003220 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3221}
3222
Douglas Gregor05c13a32009-01-22 00:58:24 +00003223Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3224 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3225 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3226 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003227 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3228 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3229}
3230
3231Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003232 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003233 "Requires array range designator");
3234 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3235 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003236 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3237 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3238}
3239
3240Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003241 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003242 "Requires array range designator");
3243 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3244 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003245 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3246 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3247}
3248
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003249/// \brief Replaces the designator at index @p Idx with the series
3250/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003251void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003252 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003253 const Designator *Last) {
3254 unsigned NumNewDesignators = Last - First;
3255 if (NumNewDesignators == 0) {
3256 std::copy_backward(Designators + Idx + 1,
3257 Designators + NumDesignators,
3258 Designators + Idx);
3259 --NumNewDesignators;
3260 return;
3261 } else if (NumNewDesignators == 1) {
3262 Designators[Idx] = *First;
3263 return;
3264 }
3265
Mike Stump1eb44332009-09-09 15:08:12 +00003266 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003267 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003268 std::copy(Designators, Designators + Idx, NewDesignators);
3269 std::copy(First, Last, NewDesignators + Idx);
3270 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3271 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003272 Designators = NewDesignators;
3273 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3274}
3275
Mike Stump1eb44332009-09-09 15:08:12 +00003276ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003277 Expr **exprs, unsigned nexprs,
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003278 SourceLocation rparenloc, QualType T)
3279 : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003280 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003281 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003282 assert(!T.isNull() && "ParenListExpr must have a valid type");
Nate Begeman2ef13e52009-08-10 23:49:36 +00003283 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003284 for (unsigned i = 0; i != nexprs; ++i) {
3285 if (exprs[i]->isTypeDependent())
3286 ExprBits.TypeDependent = true;
3287 if (exprs[i]->isValueDependent())
3288 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003289 if (exprs[i]->isInstantiationDependent())
3290 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003291 if (exprs[i]->containsUnexpandedParameterPack())
3292 ExprBits.ContainsUnexpandedParameterPack = true;
3293
Nate Begeman2ef13e52009-08-10 23:49:36 +00003294 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003295 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003296}
3297
John McCalle996ffd2011-02-16 08:02:54 +00003298const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3299 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3300 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003301 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3302 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003303 e = cast<CXXConstructExpr>(e)->getArg(0);
3304 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3305 e = ice->getSubExpr();
3306 return cast<OpaqueValueExpr>(e);
3307}
3308
Douglas Gregor05c13a32009-01-22 00:58:24 +00003309//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003310// ExprIterator.
3311//===----------------------------------------------------------------------===//
3312
3313Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3314Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3315Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3316const Expr* ConstExprIterator::operator[](size_t idx) const {
3317 return cast<Expr>(I[idx]);
3318}
3319const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3320const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3321
3322//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003323// Child Iterators for iterating over subexpressions/substatements
3324//===----------------------------------------------------------------------===//
3325
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003326// UnaryExprOrTypeTraitExpr
3327Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003328 // If this is of a type and the type is a VLA type (and not a typedef), the
3329 // size expression of the VLA needs to be treated as an executable expression.
3330 // Why isn't this weirdness documented better in StmtIterator?
3331 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003332 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003333 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003334 return child_range(child_iterator(T), child_iterator());
3335 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003336 }
John McCall63c00d72011-02-09 08:16:59 +00003337 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003338}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003339
Steve Naroff563477d2007-09-18 23:55:05 +00003340// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003341Stmt::child_range ObjCMessageExpr::children() {
3342 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003343 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003344 begin = reinterpret_cast<Stmt **>(this + 1);
3345 else
3346 begin = reinterpret_cast<Stmt **>(getArgs());
3347 return child_range(begin,
3348 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003349}
3350
Steve Naroff4eb206b2008-09-03 18:15:37 +00003351// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003352BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003353 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003354 bool constAdded)
Douglas Gregor561f8122011-07-01 01:22:09 +00003355 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003356 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003357 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003358{
Douglas Gregord967e312011-01-19 21:52:31 +00003359 bool TypeDependent = false;
3360 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +00003361 bool InstantiationDependent = false;
3362 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3363 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +00003364 ExprBits.TypeDependent = TypeDependent;
3365 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +00003366 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003367}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003368
3369
3370AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3371 QualType t, AtomicOp op, SourceLocation RP)
3372 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3373 false, false, false, false),
3374 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3375{
3376 for (unsigned i = 0; i < nexpr; i++) {
3377 if (args[i]->isTypeDependent())
3378 ExprBits.TypeDependent = true;
3379 if (args[i]->isValueDependent())
3380 ExprBits.ValueDependent = true;
3381 if (args[i]->isInstantiationDependent())
3382 ExprBits.InstantiationDependent = true;
3383 if (args[i]->containsUnexpandedParameterPack())
3384 ExprBits.ContainsUnexpandedParameterPack = true;
3385
3386 SubExprs[i] = args[i];
3387 }
3388}