blob: 9a08886c3fcebddf29744159d97e7c6a6e11314b [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.
Richard Smithdb1822c2011-11-08 01:31:09 +0000188 // (VD) - a constant with literal type and is initialized with an
189 // expression that is value-dependent [C++11].
190 // (VD) - FIXME: Missing from the standard:
191 // - an entity with reference type and is initialized with an
192 // expression that is value-dependent [C++11]
Douglas Gregord967e312011-01-19 21:52:31 +0000193 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smithdb1822c2011-11-08 01:31:09 +0000194 if ((D->getASTContext().getLangOptions().CPlusPlus0x ?
195 Var->getType()->isLiteralType() :
196 Var->getType()->isIntegralOrEnumerationType()) &&
197 (Var->getType().getCVRQualifiers() == Qualifiers::Const ||
198 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000199 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000200 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000201 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000202 InstantiationDependent = true;
203 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000204 }
205
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000206 // (VD) - FIXME: Missing from the standard:
207 // - a member function or a static data member of the current
208 // instantiation
Richard Smithdb1822c2011-11-08 01:31:09 +0000209 if (Var->isStaticDataMember() &&
210 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000211 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000212 InstantiationDependent = true;
213 }
Douglas Gregord967e312011-01-19 21:52:31 +0000214
215 return;
216 }
217
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000218 // (VD) - FIXME: Missing from the standard:
219 // - a member function or a static data member of the current
220 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000221 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
222 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000223 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000224 }
Douglas Gregord967e312011-01-19 21:52:31 +0000225}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000226
Douglas Gregord967e312011-01-19 21:52:31 +0000227void DeclRefExpr::computeDependence() {
228 bool TypeDependent = false;
229 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000230 bool InstantiationDependent = false;
231 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
232 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000233
234 // (TD) C++ [temp.dep.expr]p3:
235 // An id-expression is type-dependent if it contains:
236 //
237 // and
238 //
239 // (VD) C++ [temp.dep.constexpr]p2:
240 // An identifier is value-dependent if it is:
241 if (!TypeDependent && !ValueDependent &&
242 hasExplicitTemplateArgs() &&
243 TemplateSpecializationType::anyDependentTemplateArguments(
244 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000245 getNumTemplateArgs(),
246 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000247 TypeDependent = true;
248 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000249 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000250 }
251
252 ExprBits.TypeDependent = TypeDependent;
253 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000254 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000255
Douglas Gregor10738d32010-12-23 23:51:58 +0000256 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000257 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000258 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000259}
260
Chandler Carruth3aa81402011-05-01 23:48:14 +0000261DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000262 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000263 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000264 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000265 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000266 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000267 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
268 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000269 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000270 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000271 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
272 if (FoundD)
273 getInternalFoundDecl() = FoundD;
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000274 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Douglas Gregor561f8122011-07-01 01:22:09 +0000275 if (TemplateArgs) {
276 bool Dependent = false;
277 bool InstantiationDependent = false;
278 bool ContainsUnexpandedParameterPack = false;
279 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
280 InstantiationDependent,
281 ContainsUnexpandedParameterPack);
282 if (InstantiationDependent)
283 setInstantiationDependent(true);
284 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000285 DeclRefExprBits.HadMultipleCandidates = 0;
286
Abramo Bagnara25777432010-08-11 22:01:17 +0000287 computeDependence();
288}
289
Douglas Gregora2813ce2009-10-23 18:54:35 +0000290DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000291 NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000292 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000293 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000294 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000295 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000296 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000297 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000298 return Create(Context, QualifierLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000299 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000300 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000301}
302
303DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000304 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000305 ValueDecl *D,
306 const DeclarationNameInfo &NameInfo,
307 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000308 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000309 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000310 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000311 // Filter out cases where the found Decl is the same as the value refenenced.
312 if (D == FoundD)
313 FoundD = 0;
314
Douglas Gregora2813ce2009-10-23 18:54:35 +0000315 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000316 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000317 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000318 if (FoundD)
319 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000320 if (TemplateArgs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000321 Size += ASTTemplateArgumentListInfo::sizeFor(*TemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000322
Chris Lattner32488542010-10-30 05:14:06 +0000323 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth3aa81402011-05-01 23:48:14 +0000324 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
325 T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000326}
327
Chandler Carruth3aa81402011-05-01 23:48:14 +0000328DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000329 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000330 bool HasFoundDecl,
Douglas Gregordef03542011-02-04 12:01:24 +0000331 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000332 unsigned NumTemplateArgs) {
333 std::size_t Size = sizeof(DeclRefExpr);
334 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000335 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000336 if (HasFoundDecl)
337 Size += sizeof(NamedDecl *);
Douglas Gregordef03542011-02-04 12:01:24 +0000338 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000339 Size += ASTTemplateArgumentListInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000340
Chris Lattner32488542010-10-30 05:14:06 +0000341 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000342 return new (Mem) DeclRefExpr(EmptyShell());
343}
344
Douglas Gregora2813ce2009-10-23 18:54:35 +0000345SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000346 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000347 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000348 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000349 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000350 R.setEnd(getRAngleLoc());
351 return R;
352}
353
Anders Carlsson3a082d82009-09-08 18:24:21 +0000354// FIXME: Maybe this should use DeclPrinter with a special "print predefined
355// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000356std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
357 ASTContext &Context = CurrentDecl->getASTContext();
358
Anders Carlsson3a082d82009-09-08 18:24:21 +0000359 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000360 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000361 return FD->getNameAsString();
362
363 llvm::SmallString<256> Name;
364 llvm::raw_svector_ostream Out(Name);
365
366 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000367 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000368 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000369 if (MD->isStatic())
370 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000371 }
372
373 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000374
375 std::string Proto = FD->getQualifiedNameAsString(Policy);
376
John McCall183700f2009-09-21 23:43:11 +0000377 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000378 const FunctionProtoType *FT = 0;
379 if (FD->hasWrittenPrototype())
380 FT = dyn_cast<FunctionProtoType>(AFT);
381
382 Proto += "(";
383 if (FT) {
384 llvm::raw_string_ostream POut(Proto);
385 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
386 if (i) POut << ", ";
387 std::string Param;
388 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
389 POut << Param;
390 }
391
392 if (FT->isVariadic()) {
393 if (FD->getNumParams()) POut << ", ";
394 POut << "...";
395 }
396 }
397 Proto += ")";
398
Sam Weinig4eadcc52009-12-27 01:38:20 +0000399 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
400 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
401 if (ThisQuals.hasConst())
402 Proto += " const";
403 if (ThisQuals.hasVolatile())
404 Proto += " volatile";
405 }
406
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000407 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
408 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000409
410 Out << Proto;
411
412 Out.flush();
413 return Name.str().str();
414 }
415 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
416 llvm::SmallString<256> Name;
417 llvm::raw_svector_ostream Out(Name);
418 Out << (MD->isInstanceMethod() ? '-' : '+');
419 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000420
421 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
422 // a null check to avoid a crash.
423 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000424 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000425
Anders Carlsson3a082d82009-09-08 18:24:21 +0000426 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000427 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
428 Out << '(' << CID << ')';
429
Anders Carlsson3a082d82009-09-08 18:24:21 +0000430 Out << ' ';
431 Out << MD->getSelector().getAsString();
432 Out << ']';
433
434 Out.flush();
435 return Name.str().str();
436 }
437 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
438 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
439 return "top level";
440 }
441 return "";
442}
443
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000444void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
445 if (hasAllocation())
446 C.Deallocate(pVal);
447
448 BitWidth = Val.getBitWidth();
449 unsigned NumWords = Val.getNumWords();
450 const uint64_t* Words = Val.getRawData();
451 if (NumWords > 1) {
452 pVal = new (C) uint64_t[NumWords];
453 std::copy(Words, Words + NumWords, pVal);
454 } else if (NumWords == 1)
455 VAL = Words[0];
456 else
457 VAL = 0;
458}
459
460IntegerLiteral *
461IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
462 QualType type, SourceLocation l) {
463 return new (C) IntegerLiteral(C, V, type, l);
464}
465
466IntegerLiteral *
467IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
468 return new (C) IntegerLiteral(Empty);
469}
470
471FloatingLiteral *
472FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
473 bool isexact, QualType Type, SourceLocation L) {
474 return new (C) FloatingLiteral(C, V, isexact, Type, L);
475}
476
477FloatingLiteral *
478FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
479 return new (C) FloatingLiteral(Empty);
480}
481
Chris Lattnerda8249e2008-06-07 22:13:43 +0000482/// getValueAsApproximateDouble - This returns the value as an inaccurate
483/// double. Note that this may cause loss of precision, but is useful for
484/// debugging dumps, etc.
485double FloatingLiteral::getValueAsApproximateDouble() const {
486 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000487 bool ignored;
488 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
489 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000490 return V.convertToDouble();
491}
492
Jakub Staszak2d5f0952012-01-06 17:44:30 +0000493int StringLiteral::mapCharByteWidth(TargetInfo const &target, StringKind k) {
494 int CharByteWidth = 0;
Eli Friedman64f45a22011-11-01 02:23:42 +0000495 switch(k) {
496 case Ascii:
497 case UTF8:
498 CharByteWidth = target.getCharWidth();
499 break;
500 case Wide:
501 CharByteWidth = target.getWCharWidth();
502 break;
503 case UTF16:
504 CharByteWidth = target.getChar16Width();
505 break;
506 case UTF32:
507 CharByteWidth = target.getChar32Width();
508 }
509 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
510 CharByteWidth /= 8;
511 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
512 && "character byte widths supported are 1, 2, and 4 only");
513 return CharByteWidth;
514}
515
Chris Lattner5f9e2722011-07-23 10:55:15 +0000516StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000517 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000518 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000519 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000520 // Allocate enough space for the StringLiteral plus an array of locations for
521 // any concatenated string tokens.
522 void *Mem = C.Allocate(sizeof(StringLiteral)+
523 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000524 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000525 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000528 SL->setString(C,Str,Kind,Pascal);
529
Chris Lattner2085fd62009-02-18 06:40:38 +0000530 SL->TokLocs[0] = Loc[0];
531 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000532
Chris Lattner726e1682009-02-18 05:49:11 +0000533 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000534 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
535 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000536}
537
Douglas Gregor673ecd62009-04-15 16:35:07 +0000538StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
539 void *Mem = C.Allocate(sizeof(StringLiteral)+
540 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000541 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000542 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000543 SL->CharByteWidth = 0;
544 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000545 SL->NumConcatenated = NumStrs;
546 return SL;
547}
548
Eli Friedman64f45a22011-11-01 02:23:42 +0000549void StringLiteral::setString(ASTContext &C, StringRef Str,
550 StringKind Kind, bool IsPascal) {
551 //FIXME: we assume that the string data comes from a target that uses the same
552 // code unit size and endianess for the type of string.
553 this->Kind = Kind;
554 this->IsPascal = IsPascal;
555
556 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
557 assert((Str.size()%CharByteWidth == 0)
558 && "size of data must be multiple of CharByteWidth");
559 Length = Str.size()/CharByteWidth;
560
561 switch(CharByteWidth) {
562 case 1: {
563 char *AStrData = new (C) char[Length];
564 std::memcpy(AStrData,Str.data(),Str.size());
565 StrData.asChar = AStrData;
566 break;
567 }
568 case 2: {
569 uint16_t *AStrData = new (C) uint16_t[Length];
570 std::memcpy(AStrData,Str.data(),Str.size());
571 StrData.asUInt16 = AStrData;
572 break;
573 }
574 case 4: {
575 uint32_t *AStrData = new (C) uint32_t[Length];
576 std::memcpy(AStrData,Str.data(),Str.size());
577 StrData.asUInt32 = AStrData;
578 break;
579 }
580 default:
581 assert(false && "unsupported CharByteWidth");
582 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000583}
584
Chris Lattner08f92e32010-11-17 07:37:15 +0000585/// getLocationOfByte - Return a source location that points to the specified
586/// byte of this string literal.
587///
588/// Strings are amazingly complex. They can be formed from multiple tokens and
589/// can have escape sequences in them in addition to the usual trigraph and
590/// escaped newline business. This routine handles this complexity.
591///
592SourceLocation StringLiteral::
593getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
594 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000595 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
596
Chris Lattner08f92e32010-11-17 07:37:15 +0000597 // Loop over all of the tokens in this string until we find the one that
598 // contains the byte we're looking for.
599 unsigned TokNo = 0;
600 while (1) {
601 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
602 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
603
604 // Get the spelling of the string so that we can get the data that makes up
605 // the string literal, not the identifier for the macro it is potentially
606 // expanded through.
607 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
608
609 // Re-lex the token to get its length and original spelling.
610 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
611 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000612 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000613 if (Invalid)
614 return StrTokSpellingLoc;
615
616 const char *StrData = Buffer.data()+LocInfo.second;
617
618 // Create a langops struct and enable trigraphs. This is sufficient for
619 // relexing tokens.
620 LangOptions LangOpts;
621 LangOpts.Trigraphs = true;
622
623 // Create a lexer starting at the beginning of this token.
624 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
625 Buffer.end());
626 Token TheTok;
627 TheLexer.LexFromRawLexer(TheTok);
628
629 // Use the StringLiteralParser to compute the length of the string in bytes.
630 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
631 unsigned TokNumBytes = SLP.GetStringLength();
632
633 // If the byte is in this token, return the location of the byte.
634 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000635 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000636 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
637
638 // Now that we know the offset of the token in the spelling, use the
639 // preprocessor to get the offset in the original source.
640 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
641 }
642
643 // Move to the next string token.
644 ++TokNo;
645 ByteNo -= TokNumBytes;
646 }
647}
648
649
650
Reid Spencer5f016e22007-07-11 17:01:13 +0000651/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
652/// corresponds to, e.g. "sizeof" or "[pre]++".
653const char *UnaryOperator::getOpcodeStr(Opcode Op) {
654 switch (Op) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000655 default: llvm_unreachable("Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000656 case UO_PostInc: return "++";
657 case UO_PostDec: return "--";
658 case UO_PreInc: return "++";
659 case UO_PreDec: return "--";
660 case UO_AddrOf: return "&";
661 case UO_Deref: return "*";
662 case UO_Plus: return "+";
663 case UO_Minus: return "-";
664 case UO_Not: return "~";
665 case UO_LNot: return "!";
666 case UO_Real: return "__real";
667 case UO_Imag: return "__imag";
668 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 }
670}
671
John McCall2de56d12010-08-25 11:45:40 +0000672UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000673UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
674 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000675 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000676 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
677 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
678 case OO_Amp: return UO_AddrOf;
679 case OO_Star: return UO_Deref;
680 case OO_Plus: return UO_Plus;
681 case OO_Minus: return UO_Minus;
682 case OO_Tilde: return UO_Not;
683 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000684 }
685}
686
687OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
688 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000689 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
690 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
691 case UO_AddrOf: return OO_Amp;
692 case UO_Deref: return OO_Star;
693 case UO_Plus: return OO_Plus;
694 case UO_Minus: return OO_Minus;
695 case UO_Not: return OO_Tilde;
696 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000697 default: return OO_None;
698 }
699}
700
701
Reid Spencer5f016e22007-07-11 17:01:13 +0000702//===----------------------------------------------------------------------===//
703// Postfix Operators.
704//===----------------------------------------------------------------------===//
705
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000706CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
707 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000708 SourceLocation rparenloc)
709 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000710 fn->isTypeDependent(),
711 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000712 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000713 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000714 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000715
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000716 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000717 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000718 for (unsigned i = 0; i != numargs; ++i) {
719 if (args[i]->isTypeDependent())
720 ExprBits.TypeDependent = true;
721 if (args[i]->isValueDependent())
722 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000723 if (args[i]->isInstantiationDependent())
724 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000725 if (args[i]->containsUnexpandedParameterPack())
726 ExprBits.ContainsUnexpandedParameterPack = true;
727
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000728 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000729 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000730
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000731 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000732 RParenLoc = rparenloc;
733}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000734
Ted Kremenek668bf912009-02-09 20:51:47 +0000735CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000736 QualType t, ExprValueKind VK, SourceLocation rparenloc)
737 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000738 fn->isTypeDependent(),
739 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000740 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000741 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000742 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000743
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000744 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000745 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000746 for (unsigned i = 0; i != numargs; ++i) {
747 if (args[i]->isTypeDependent())
748 ExprBits.TypeDependent = true;
749 if (args[i]->isValueDependent())
750 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000751 if (args[i]->isInstantiationDependent())
752 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000753 if (args[i]->containsUnexpandedParameterPack())
754 ExprBits.ContainsUnexpandedParameterPack = true;
755
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000756 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000757 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000758
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000759 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 RParenLoc = rparenloc;
761}
762
Mike Stump1eb44332009-09-09 15:08:12 +0000763CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
764 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000765 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000766 SubExprs = new (C) Stmt*[PREARGS_START];
767 CallExprBits.NumPreArgs = 0;
768}
769
770CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
771 EmptyShell Empty)
772 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
773 // FIXME: Why do we allocate this?
774 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
775 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000776}
777
Nuno Lopesd20254f2009-12-20 23:11:08 +0000778Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000779 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000780
781 while (SubstNonTypeTemplateParmExpr *NTTP
782 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
783 CEE = NTTP->getReplacement()->IgnoreParenCasts();
784 }
785
Sebastian Redl20012152010-09-10 20:55:30 +0000786 // If we're calling a dereference, look at the pointer instead.
787 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
788 if (BO->isPtrMemOp())
789 CEE = BO->getRHS()->IgnoreParenCasts();
790 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
791 if (UO->getOpcode() == UO_Deref)
792 CEE = UO->getSubExpr()->IgnoreParenCasts();
793 }
Chris Lattner6346f962009-07-17 15:46:27 +0000794 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000795 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000796 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
797 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000798
799 return 0;
800}
801
Nuno Lopesd20254f2009-12-20 23:11:08 +0000802FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000803 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000804}
805
Chris Lattnerd18b3292007-12-28 05:25:02 +0000806/// setNumArgs - This changes the number of arguments present in this call.
807/// Any orphaned expressions are deleted by this, and any new operands are set
808/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000809void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000810 // No change, just return.
811 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Chris Lattnerd18b3292007-12-28 05:25:02 +0000813 // If shrinking # arguments, just delete the extras and forgot them.
814 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000815 this->NumArgs = NumArgs;
816 return;
817 }
818
819 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000820 unsigned NumPreArgs = getNumPreArgs();
821 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000822 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000823 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000824 NewSubExprs[i] = SubExprs[i];
825 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000826 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
827 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000828 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Douglas Gregor88c9a462009-04-17 21:46:47 +0000830 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000831 SubExprs = NewSubExprs;
832 this->NumArgs = NumArgs;
833}
834
Chris Lattnercb888962008-10-06 05:00:53 +0000835/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
836/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +0000837unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000838 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000839 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000840 // ImplicitCastExpr.
841 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
842 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000843 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000845 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
846 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000847 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000848
Anders Carlssonbcba2012008-01-31 02:13:57 +0000849 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
850 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000851 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000852
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000853 if (!FDecl->getIdentifier())
854 return 0;
855
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000856 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000857}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000858
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000859QualType CallExpr::getCallReturnType() const {
860 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000861 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000862 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000863 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000864 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000865 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
866 // This should never be overloaded and so should never return null.
867 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000868
John McCall864c0412011-04-26 20:42:42 +0000869 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000870 return FnType->getResultType();
871}
Chris Lattnercb888962008-10-06 05:00:53 +0000872
John McCall2882eca2011-02-21 06:23:05 +0000873SourceRange CallExpr::getSourceRange() const {
874 if (isa<CXXOperatorCallExpr>(this))
875 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
876
877 SourceLocation begin = getCallee()->getLocStart();
878 if (begin.isInvalid() && getNumArgs() > 0)
879 begin = getArg(0)->getLocStart();
880 SourceLocation end = getRParenLoc();
881 if (end.isInvalid() && getNumArgs() > 0)
882 end = getArg(getNumArgs() - 1)->getLocEnd();
883 return SourceRange(begin, end);
884}
885
Sean Huntc3021132010-05-05 15:23:54 +0000886OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000887 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000888 TypeSourceInfo *tsi,
889 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000890 Expr** exprsPtr, unsigned numExprs,
891 SourceLocation RParenLoc) {
892 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000893 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000894 sizeof(Expr*) * numExprs);
895
896 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
897 exprsPtr, numExprs, RParenLoc);
898}
899
900OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
901 unsigned numComps, unsigned numExprs) {
902 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
903 sizeof(OffsetOfNode) * numComps +
904 sizeof(Expr*) * numExprs);
905 return new (Mem) OffsetOfExpr(numComps, numExprs);
906}
907
Sean Huntc3021132010-05-05 15:23:54 +0000908OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000909 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000910 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000911 Expr** exprsPtr, unsigned numExprs,
912 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000913 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
914 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000915 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000916 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000917 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000918 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
919 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000920{
921 for(unsigned i = 0; i < numComps; ++i) {
922 setComponent(i, compsPtr[i]);
923 }
Sean Huntc3021132010-05-05 15:23:54 +0000924
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000925 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000926 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
927 ExprBits.ValueDependent = true;
928 if (exprsPtr[i]->containsUnexpandedParameterPack())
929 ExprBits.ContainsUnexpandedParameterPack = true;
930
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000931 setIndexExpr(i, exprsPtr[i]);
932 }
933}
934
935IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
936 assert(getKind() == Field || getKind() == Identifier);
937 if (getKind() == Field)
938 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000939
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000940 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
941}
942
Mike Stump1eb44332009-09-09 15:08:12 +0000943MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000944 NestedNameSpecifierLoc QualifierLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000945 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000946 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000947 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000948 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000949 QualType ty,
950 ExprValueKind vk,
951 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000952 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000953
Douglas Gregor40d96a62011-02-28 21:54:11 +0000954 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000955 founddecl.getDecl() != memberdecl ||
956 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000957 if (hasQualOrFound)
958 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000959
John McCalld5532b62009-11-23 01:53:49 +0000960 if (targs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000961 Size += ASTTemplateArgumentListInfo::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000962
Chris Lattner32488542010-10-30 05:14:06 +0000963 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000964 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
965 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000966
967 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000968 // FIXME: Wrong. We should be looking at the member declaration we found.
969 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000970 E->setValueDependent(true);
971 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +0000972 E->setInstantiationDependent(true);
973 }
974 else if (QualifierLoc &&
975 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
976 E->setInstantiationDependent(true);
977
John McCall6bb80172010-03-30 21:47:33 +0000978 E->HasQualifierOrFoundDecl = true;
979
980 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000981 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000982 NQ->FoundDecl = founddecl;
983 }
984
985 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000986 bool Dependent = false;
987 bool InstantiationDependent = false;
988 bool ContainsUnexpandedParameterPack = false;
John McCall6bb80172010-03-30 21:47:33 +0000989 E->HasExplicitTemplateArgumentList = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000990 E->getExplicitTemplateArgs().initializeFrom(*targs, Dependent,
991 InstantiationDependent,
992 ContainsUnexpandedParameterPack);
993 if (InstantiationDependent)
994 E->setInstantiationDependent(true);
John McCall6bb80172010-03-30 21:47:33 +0000995 }
996
997 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000998}
999
Douglas Gregor75e85042011-03-02 21:06:53 +00001000SourceRange MemberExpr::getSourceRange() const {
1001 SourceLocation StartLoc;
1002 if (isImplicitAccess()) {
1003 if (hasQualifier())
1004 StartLoc = getQualifierLoc().getBeginLoc();
1005 else
1006 StartLoc = MemberLoc;
1007 } else {
1008 // FIXME: We don't want this to happen. Rather, we should be able to
1009 // detect all kinds of implicit accesses more cleanly.
1010 StartLoc = getBase()->getLocStart();
1011 if (StartLoc.isInvalid())
1012 StartLoc = MemberLoc;
1013 }
1014
1015 SourceLocation EndLoc =
1016 HasExplicitTemplateArgumentList? getRAngleLoc()
1017 : getMemberNameInfo().getEndLoc();
1018
1019 return SourceRange(StartLoc, EndLoc);
1020}
1021
John McCall1d9b3b22011-09-09 05:25:32 +00001022void CastExpr::CheckCastConsistency() const {
1023 switch (getCastKind()) {
1024 case CK_DerivedToBase:
1025 case CK_UncheckedDerivedToBase:
1026 case CK_DerivedToBaseMemberPointer:
1027 case CK_BaseToDerived:
1028 case CK_BaseToDerivedMemberPointer:
1029 assert(!path_empty() && "Cast kind should have a base path!");
1030 break;
1031
1032 case CK_CPointerToObjCPointerCast:
1033 assert(getType()->isObjCObjectPointerType());
1034 assert(getSubExpr()->getType()->isPointerType());
1035 goto CheckNoBasePath;
1036
1037 case CK_BlockPointerToObjCPointerCast:
1038 assert(getType()->isObjCObjectPointerType());
1039 assert(getSubExpr()->getType()->isBlockPointerType());
1040 goto CheckNoBasePath;
1041
1042 case CK_BitCast:
1043 // Arbitrary casts to C pointer types count as bitcasts.
1044 // Otherwise, we should only have block and ObjC pointer casts
1045 // here if they stay within the type kind.
1046 if (!getType()->isPointerType()) {
1047 assert(getType()->isObjCObjectPointerType() ==
1048 getSubExpr()->getType()->isObjCObjectPointerType());
1049 assert(getType()->isBlockPointerType() ==
1050 getSubExpr()->getType()->isBlockPointerType());
1051 }
1052 goto CheckNoBasePath;
1053
1054 case CK_AnyPointerToBlockPointerCast:
1055 assert(getType()->isBlockPointerType());
1056 assert(getSubExpr()->getType()->isAnyPointerType() &&
1057 !getSubExpr()->getType()->isBlockPointerType());
1058 goto CheckNoBasePath;
1059
1060 // These should not have an inheritance path.
1061 case CK_Dynamic:
1062 case CK_ToUnion:
1063 case CK_ArrayToPointerDecay:
1064 case CK_FunctionToPointerDecay:
1065 case CK_NullToMemberPointer:
1066 case CK_NullToPointer:
1067 case CK_ConstructorConversion:
1068 case CK_IntegralToPointer:
1069 case CK_PointerToIntegral:
1070 case CK_ToVoid:
1071 case CK_VectorSplat:
1072 case CK_IntegralCast:
1073 case CK_IntegralToFloating:
1074 case CK_FloatingToIntegral:
1075 case CK_FloatingCast:
1076 case CK_ObjCObjectLValueCast:
1077 case CK_FloatingRealToComplex:
1078 case CK_FloatingComplexToReal:
1079 case CK_FloatingComplexCast:
1080 case CK_FloatingComplexToIntegralComplex:
1081 case CK_IntegralRealToComplex:
1082 case CK_IntegralComplexToReal:
1083 case CK_IntegralComplexCast:
1084 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001085 case CK_ARCProduceObject:
1086 case CK_ARCConsumeObject:
1087 case CK_ARCReclaimReturnedObject:
1088 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001089 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1090 goto CheckNoBasePath;
1091
1092 case CK_Dependent:
1093 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001094 case CK_NoOp:
1095 case CK_PointerToBoolean:
1096 case CK_IntegralToBoolean:
1097 case CK_FloatingToBoolean:
1098 case CK_MemberPointerToBoolean:
1099 case CK_FloatingComplexToBoolean:
1100 case CK_IntegralComplexToBoolean:
1101 case CK_LValueBitCast: // -> bool&
1102 case CK_UserDefinedConversion: // operator bool()
1103 CheckNoBasePath:
1104 assert(path_empty() && "Cast kind should not have a base path!");
1105 break;
1106 }
1107}
1108
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001109const char *CastExpr::getCastKindName() const {
1110 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001111 case CK_Dependent:
1112 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001113 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001114 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001115 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001116 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001117 case CK_LValueToRValue:
1118 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001119 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001120 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001121 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001122 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001123 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001124 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001125 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001126 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001127 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001128 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001129 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001130 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001131 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001132 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001133 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001134 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001135 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001136 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001137 case CK_NullToPointer:
1138 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001139 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001140 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001141 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001142 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001143 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001144 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001145 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001146 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001147 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001148 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001149 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001150 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001151 case CK_PointerToBoolean:
1152 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001153 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001154 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001155 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001156 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001157 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001158 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001159 case CK_IntegralToBoolean:
1160 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001161 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001162 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001163 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001164 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001165 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001166 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001167 case CK_FloatingToBoolean:
1168 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001169 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001170 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001171 case CK_CPointerToObjCPointerCast:
1172 return "CPointerToObjCPointerCast";
1173 case CK_BlockPointerToObjCPointerCast:
1174 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001175 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001176 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001177 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001178 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001179 case CK_FloatingRealToComplex:
1180 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001181 case CK_FloatingComplexToReal:
1182 return "FloatingComplexToReal";
1183 case CK_FloatingComplexToBoolean:
1184 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001185 case CK_FloatingComplexCast:
1186 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001187 case CK_FloatingComplexToIntegralComplex:
1188 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001189 case CK_IntegralRealToComplex:
1190 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001191 case CK_IntegralComplexToReal:
1192 return "IntegralComplexToReal";
1193 case CK_IntegralComplexToBoolean:
1194 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001195 case CK_IntegralComplexCast:
1196 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001197 case CK_IntegralComplexToFloatingComplex:
1198 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001199 case CK_ARCConsumeObject:
1200 return "ARCConsumeObject";
1201 case CK_ARCProduceObject:
1202 return "ARCProduceObject";
1203 case CK_ARCReclaimReturnedObject:
1204 return "ARCReclaimReturnedObject";
1205 case CK_ARCExtendBlockObject:
1206 return "ARCCExtendBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001207 }
Mike Stump1eb44332009-09-09 15:08:12 +00001208
John McCall2bb5d002010-11-13 09:02:35 +00001209 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001210 return 0;
1211}
1212
Douglas Gregor6eef5192009-12-14 19:27:10 +00001213Expr *CastExpr::getSubExprAsWritten() {
1214 Expr *SubExpr = 0;
1215 CastExpr *E = this;
1216 do {
1217 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001218
1219 // Skip through reference binding to temporary.
1220 if (MaterializeTemporaryExpr *Materialize
1221 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1222 SubExpr = Materialize->GetTemporaryExpr();
1223
Douglas Gregor6eef5192009-12-14 19:27:10 +00001224 // Skip any temporary bindings; they're implicit.
1225 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1226 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001227
Douglas Gregor6eef5192009-12-14 19:27:10 +00001228 // Conversions by constructor and conversion functions have a
1229 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001230 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001231 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001232 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001233 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001234
Douglas Gregor6eef5192009-12-14 19:27:10 +00001235 // If the subexpression we're left with is an implicit cast, look
1236 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001237 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1238
Douglas Gregor6eef5192009-12-14 19:27:10 +00001239 return SubExpr;
1240}
1241
John McCallf871d0c2010-08-07 06:22:56 +00001242CXXBaseSpecifier **CastExpr::path_buffer() {
1243 switch (getStmtClass()) {
1244#define ABSTRACT_STMT(x)
1245#define CASTEXPR(Type, Base) \
1246 case Stmt::Type##Class: \
1247 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1248#define STMT(Type, Base)
1249#include "clang/AST/StmtNodes.inc"
1250 default:
1251 llvm_unreachable("non-cast expressions not possible here");
1252 return 0;
1253 }
1254}
1255
1256void CastExpr::setCastPath(const CXXCastPath &Path) {
1257 assert(Path.size() == path_size());
1258 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1259}
1260
1261ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1262 CastKind Kind, Expr *Operand,
1263 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001264 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001265 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1266 void *Buffer =
1267 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1268 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001269 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001270 if (PathSize) E->setCastPath(*BasePath);
1271 return E;
1272}
1273
1274ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1275 unsigned PathSize) {
1276 void *Buffer =
1277 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1278 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1279}
1280
1281
1282CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001283 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001284 const CXXCastPath *BasePath,
1285 TypeSourceInfo *WrittenTy,
1286 SourceLocation L, SourceLocation R) {
1287 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1288 void *Buffer =
1289 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1290 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001291 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001292 if (PathSize) E->setCastPath(*BasePath);
1293 return E;
1294}
1295
1296CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1297 void *Buffer =
1298 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1299 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1300}
1301
Reid Spencer5f016e22007-07-11 17:01:13 +00001302/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1303/// corresponds to, e.g. "<<=".
1304const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1305 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001306 case BO_PtrMemD: return ".*";
1307 case BO_PtrMemI: return "->*";
1308 case BO_Mul: return "*";
1309 case BO_Div: return "/";
1310 case BO_Rem: return "%";
1311 case BO_Add: return "+";
1312 case BO_Sub: return "-";
1313 case BO_Shl: return "<<";
1314 case BO_Shr: return ">>";
1315 case BO_LT: return "<";
1316 case BO_GT: return ">";
1317 case BO_LE: return "<=";
1318 case BO_GE: return ">=";
1319 case BO_EQ: return "==";
1320 case BO_NE: return "!=";
1321 case BO_And: return "&";
1322 case BO_Xor: return "^";
1323 case BO_Or: return "|";
1324 case BO_LAnd: return "&&";
1325 case BO_LOr: return "||";
1326 case BO_Assign: return "=";
1327 case BO_MulAssign: return "*=";
1328 case BO_DivAssign: return "/=";
1329 case BO_RemAssign: return "%=";
1330 case BO_AddAssign: return "+=";
1331 case BO_SubAssign: return "-=";
1332 case BO_ShlAssign: return "<<=";
1333 case BO_ShrAssign: return ">>=";
1334 case BO_AndAssign: return "&=";
1335 case BO_XorAssign: return "^=";
1336 case BO_OrAssign: return "|=";
1337 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001339
1340 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001341}
1342
John McCall2de56d12010-08-25 11:45:40 +00001343BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001344BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1345 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001346 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001347 case OO_Plus: return BO_Add;
1348 case OO_Minus: return BO_Sub;
1349 case OO_Star: return BO_Mul;
1350 case OO_Slash: return BO_Div;
1351 case OO_Percent: return BO_Rem;
1352 case OO_Caret: return BO_Xor;
1353 case OO_Amp: return BO_And;
1354 case OO_Pipe: return BO_Or;
1355 case OO_Equal: return BO_Assign;
1356 case OO_Less: return BO_LT;
1357 case OO_Greater: return BO_GT;
1358 case OO_PlusEqual: return BO_AddAssign;
1359 case OO_MinusEqual: return BO_SubAssign;
1360 case OO_StarEqual: return BO_MulAssign;
1361 case OO_SlashEqual: return BO_DivAssign;
1362 case OO_PercentEqual: return BO_RemAssign;
1363 case OO_CaretEqual: return BO_XorAssign;
1364 case OO_AmpEqual: return BO_AndAssign;
1365 case OO_PipeEqual: return BO_OrAssign;
1366 case OO_LessLess: return BO_Shl;
1367 case OO_GreaterGreater: return BO_Shr;
1368 case OO_LessLessEqual: return BO_ShlAssign;
1369 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1370 case OO_EqualEqual: return BO_EQ;
1371 case OO_ExclaimEqual: return BO_NE;
1372 case OO_LessEqual: return BO_LE;
1373 case OO_GreaterEqual: return BO_GE;
1374 case OO_AmpAmp: return BO_LAnd;
1375 case OO_PipePipe: return BO_LOr;
1376 case OO_Comma: return BO_Comma;
1377 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001378 }
1379}
1380
1381OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1382 static const OverloadedOperatorKind OverOps[] = {
1383 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1384 OO_Star, OO_Slash, OO_Percent,
1385 OO_Plus, OO_Minus,
1386 OO_LessLess, OO_GreaterGreater,
1387 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1388 OO_EqualEqual, OO_ExclaimEqual,
1389 OO_Amp,
1390 OO_Caret,
1391 OO_Pipe,
1392 OO_AmpAmp,
1393 OO_PipePipe,
1394 OO_Equal, OO_StarEqual,
1395 OO_SlashEqual, OO_PercentEqual,
1396 OO_PlusEqual, OO_MinusEqual,
1397 OO_LessLessEqual, OO_GreaterGreaterEqual,
1398 OO_AmpEqual, OO_CaretEqual,
1399 OO_PipeEqual,
1400 OO_Comma
1401 };
1402 return OverOps[Opc];
1403}
1404
Ted Kremenek709210f2010-04-13 23:39:13 +00001405InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001406 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001407 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001408 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001409 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001410 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001411 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +00001412 HadArrayRangeDesignator(false)
Sean Huntc3021132010-05-05 15:23:54 +00001413{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001414 for (unsigned I = 0; I != numInits; ++I) {
1415 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001416 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001417 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001418 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001419 if (initExprs[I]->isInstantiationDependent())
1420 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001421 if (initExprs[I]->containsUnexpandedParameterPack())
1422 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001423 }
Sean Huntc3021132010-05-05 15:23:54 +00001424
Ted Kremenek709210f2010-04-13 23:39:13 +00001425 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001426}
Reid Spencer5f016e22007-07-11 17:01:13 +00001427
Ted Kremenek709210f2010-04-13 23:39:13 +00001428void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001429 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001430 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001431}
1432
Ted Kremenek709210f2010-04-13 23:39:13 +00001433void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001434 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001435}
1436
Ted Kremenek709210f2010-04-13 23:39:13 +00001437Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001438 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001439 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001440 InitExprs.back() = expr;
1441 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001442 }
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Douglas Gregor4c678342009-01-28 21:54:33 +00001444 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1445 InitExprs[Init] = expr;
1446 return Result;
1447}
1448
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001449void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001450 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001451 ArrayFillerOrUnionFieldInit = filler;
1452 // Fill out any "holes" in the array due to designated initializers.
1453 Expr **inits = getInits();
1454 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1455 if (inits[i] == 0)
1456 inits[i] = filler;
1457}
1458
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001459SourceRange InitListExpr::getSourceRange() const {
1460 if (SyntacticForm)
1461 return SyntacticForm->getSourceRange();
1462 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1463 if (Beg.isInvalid()) {
1464 // Find the first non-null initializer.
1465 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1466 E = InitExprs.end();
1467 I != E; ++I) {
1468 if (Stmt *S = *I) {
1469 Beg = S->getLocStart();
1470 break;
1471 }
1472 }
1473 }
1474 if (End.isInvalid()) {
1475 // Find the first non-null initializer from the end.
1476 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1477 E = InitExprs.rend();
1478 I != E; ++I) {
1479 if (Stmt *S = *I) {
1480 End = S->getSourceRange().getEnd();
1481 break;
1482 }
1483 }
1484 }
1485 return SourceRange(Beg, End);
1486}
1487
Steve Naroffbfdcae62008-09-04 15:31:07 +00001488/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001489///
1490const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001491 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001492 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001493}
1494
Mike Stump1eb44332009-09-09 15:08:12 +00001495SourceLocation BlockExpr::getCaretLocation() const {
1496 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001497}
Mike Stump1eb44332009-09-09 15:08:12 +00001498const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001499 return TheBlock->getBody();
1500}
Mike Stump1eb44332009-09-09 15:08:12 +00001501Stmt *BlockExpr::getBody() {
1502 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001503}
Steve Naroff56ee6892008-10-08 17:01:13 +00001504
1505
Reid Spencer5f016e22007-07-11 17:01:13 +00001506//===----------------------------------------------------------------------===//
1507// Generic Expression Routines
1508//===----------------------------------------------------------------------===//
1509
Chris Lattner026dc962009-02-14 07:37:35 +00001510/// isUnusedResultAWarning - Return true if this immediate expression should
1511/// be warned about if the result is unused. If so, fill in Loc and Ranges
1512/// with location to warn on and the source range[s] to report with the
1513/// warning.
1514bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001515 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001516 // Don't warn if the expr is type dependent. The type could end up
1517 // instantiating to void.
1518 if (isTypeDependent())
1519 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001520
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 switch (getStmtClass()) {
1522 default:
John McCall0faede62010-03-12 07:11:26 +00001523 if (getType()->isVoidType())
1524 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001525 Loc = getExprLoc();
1526 R1 = getSourceRange();
1527 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001529 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001530 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001531 case GenericSelectionExprClass:
1532 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1533 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001534 case UnaryOperatorClass: {
1535 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Reid Spencer5f016e22007-07-11 17:01:13 +00001537 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001538 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001539 case UO_PostInc:
1540 case UO_PostDec:
1541 case UO_PreInc:
1542 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001543 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001544 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001545 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001546 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001547 return false;
1548 break;
John McCall2de56d12010-08-25 11:45:40 +00001549 case UO_Real:
1550 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001552 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1553 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001554 return false;
1555 break;
John McCall2de56d12010-08-25 11:45:40 +00001556 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001557 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 }
Chris Lattner026dc962009-02-14 07:37:35 +00001559 Loc = UO->getOperatorLoc();
1560 R1 = UO->getSubExpr()->getSourceRange();
1561 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001562 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001563 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001564 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001565 switch (BO->getOpcode()) {
1566 default:
1567 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001568 // Consider the RHS of comma for side effects. LHS was checked by
1569 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001570 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001571 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1572 // lvalue-ness) of an assignment written in a macro.
1573 if (IntegerLiteral *IE =
1574 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1575 if (IE->getValue() == 0)
1576 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001577 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1578 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001579 case BO_LAnd:
1580 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001581 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1582 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1583 return false;
1584 break;
John McCallbf0ee352010-02-16 04:10:53 +00001585 }
Chris Lattner026dc962009-02-14 07:37:35 +00001586 if (BO->isAssignmentOp())
1587 return false;
1588 Loc = BO->getOperatorLoc();
1589 R1 = BO->getLHS()->getSourceRange();
1590 R2 = BO->getRHS()->getSourceRange();
1591 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001592 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001593 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001594 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001595 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001596 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001597
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001598 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001599 // If only one of the LHS or RHS is a warning, the operator might
1600 // be being used for control flow. Only warn if both the LHS and
1601 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001602 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001603 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1604 return false;
1605 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001606 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001607 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001608 }
1609
Reid Spencer5f016e22007-07-11 17:01:13 +00001610 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001611 // If the base pointer or element is to a volatile pointer/field, accessing
1612 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001613 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001614 return false;
1615 Loc = cast<MemberExpr>(this)->getMemberLoc();
1616 R1 = SourceRange(Loc, Loc);
1617 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1618 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001619
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 case ArraySubscriptExprClass:
1621 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001622 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001623 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001624 return false;
1625 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1626 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1627 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1628 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001629
Chandler Carruth9b106832011-08-17 09:49:44 +00001630 case CXXOperatorCallExprClass: {
1631 // We warn about operator== and operator!= even when user-defined operator
1632 // overloads as there is no reasonable way to define these such that they
1633 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1634 // warning: these operators are commonly typo'ed, and so warning on them
1635 // provides additional value as well. If this list is updated,
1636 // DiagnoseUnusedComparison should be as well.
1637 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1638 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001639 Op->getOperator() == OO_ExclaimEqual) {
1640 Loc = Op->getOperatorLoc();
1641 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001642 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001643 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001644
1645 // Fallthrough for generic call handling.
1646 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001648 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001649 // If this is a direct call, get the callee.
1650 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001651 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001652 // If the callee has attribute pure, const, or warn_unused_result, warn
1653 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001654 //
1655 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1656 // updated to match for QoI.
1657 if (FD->getAttr<WarnUnusedResultAttr>() ||
1658 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1659 Loc = CE->getCallee()->getLocStart();
1660 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001661
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001662 if (unsigned NumArgs = CE->getNumArgs())
1663 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1664 CE->getArg(NumArgs-1)->getLocEnd());
1665 return true;
1666 }
Chris Lattner026dc962009-02-14 07:37:35 +00001667 }
1668 return false;
1669 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001670
1671 case CXXTemporaryObjectExprClass:
1672 case CXXConstructExprClass:
1673 return false;
1674
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001675 case ObjCMessageExprClass: {
1676 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001677 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1678 ME->isInstanceMessage() &&
1679 !ME->getType()->isVoidType() &&
1680 ME->getSelector().getIdentifierInfoForSlot(0) &&
1681 ME->getSelector().getIdentifierInfoForSlot(0)
1682 ->getName().startswith("init")) {
1683 Loc = getExprLoc();
1684 R1 = ME->getSourceRange();
1685 return true;
1686 }
1687
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001688 const ObjCMethodDecl *MD = ME->getMethodDecl();
1689 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1690 Loc = getExprLoc();
1691 return true;
1692 }
Chris Lattner026dc962009-02-14 07:37:35 +00001693 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001694 }
Mike Stump1eb44332009-09-09 15:08:12 +00001695
John McCall12f78a62010-12-02 01:19:52 +00001696 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001697 Loc = getExprLoc();
1698 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001699 return true;
John McCall12f78a62010-12-02 01:19:52 +00001700
John McCall4b9c2d22011-11-06 09:01:30 +00001701 case PseudoObjectExprClass: {
1702 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1703
1704 // Only complain about things that have the form of a getter.
1705 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1706 isa<BinaryOperator>(PO->getSyntacticForm()))
1707 return false;
1708
1709 Loc = getExprLoc();
1710 R1 = getSourceRange();
1711 return true;
1712 }
1713
Chris Lattner611b2ec2008-07-26 19:51:01 +00001714 case StmtExprClass: {
1715 // Statement exprs don't logically have side effects themselves, but are
1716 // sometimes used in macros in ways that give them a type that is unused.
1717 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1718 // however, if the result of the stmt expr is dead, we don't want to emit a
1719 // warning.
1720 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001721 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001722 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001723 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001724 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1725 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1726 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1727 }
Mike Stump1eb44332009-09-09 15:08:12 +00001728
John McCall0faede62010-03-12 07:11:26 +00001729 if (getType()->isVoidType())
1730 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001731 Loc = cast<StmtExpr>(this)->getLParenLoc();
1732 R1 = getSourceRange();
1733 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001734 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001735 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001736 // If this is an explicit cast to void, allow it. People do this when they
1737 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001738 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001739 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001740 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1741 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1742 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001743 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001744 if (getType()->isVoidType())
1745 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001746 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001747
Anders Carlsson58beed92009-11-17 17:11:23 +00001748 // If this is a cast to void or a constructor conversion, check the operand.
1749 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001750 if (CE->getCastKind() == CK_ToVoid ||
1751 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001752 return (cast<CastExpr>(this)->getSubExpr()
1753 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001754 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1755 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1756 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001757 }
Mike Stump1eb44332009-09-09 15:08:12 +00001758
Eli Friedman4be1f472008-05-19 21:24:43 +00001759 case ImplicitCastExprClass:
1760 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001761 return (cast<ImplicitCastExpr>(this)
1762 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001763
Chris Lattner04421082008-04-08 04:40:51 +00001764 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001765 return (cast<CXXDefaultArgExpr>(this)
1766 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001767
1768 case CXXNewExprClass:
1769 // FIXME: In theory, there might be new expressions that don't have side
1770 // effects (e.g. a placement new with an uninitialized POD).
1771 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001772 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001773 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001774 return (cast<CXXBindTemporaryExpr>(this)
1775 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001776 case ExprWithCleanupsClass:
1777 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001778 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001779 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001780}
1781
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001782/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001783/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001784bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001785 const Expr *E = IgnoreParens();
1786 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001787 default:
1788 return false;
1789 case ObjCIvarRefExprClass:
1790 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001791 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001792 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001793 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001794 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001795 case MaterializeTemporaryExprClass:
1796 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1797 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001798 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001799 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001800 case BlockDeclRefExprClass:
Douglas Gregora2813ce2009-10-23 18:54:35 +00001801 case DeclRefExprClass: {
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001802
1803 const Decl *D;
1804 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1805 D = BDRE->getDecl();
1806 else
1807 D = cast<DeclRefExpr>(E)->getDecl();
1808
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001809 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1810 if (VD->hasGlobalStorage())
1811 return true;
1812 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001813 // dereferencing to a pointer is always a gc'able candidate,
1814 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001815 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001816 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001817 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001818 return false;
1819 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001820 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001821 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001822 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001823 }
1824 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001825 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001826 }
1827}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001828
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001829bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1830 if (isTypeDependent())
1831 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001832 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001833}
1834
John McCall864c0412011-04-26 20:42:42 +00001835QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00001836 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00001837
1838 // Bound member expressions are always one of these possibilities:
1839 // x->m x.m x->*y x.*y
1840 // (possibly parenthesized)
1841
1842 expr = expr->IgnoreParens();
1843 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1844 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1845 return mem->getMemberDecl()->getType();
1846 }
1847
1848 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1849 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1850 ->getPointeeType();
1851 assert(type->isFunctionType());
1852 return type;
1853 }
1854
1855 assert(isa<UnresolvedMemberExpr>(expr));
1856 return QualType();
1857}
1858
Sebastian Redl369e51f2010-09-10 20:55:33 +00001859static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1860 Expr::CanThrowResult CT2) {
1861 // CanThrowResult constants are ordered so that the maximum is the correct
1862 // merge result.
1863 return CT1 > CT2 ? CT1 : CT2;
1864}
1865
1866static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1867 Expr *E = const_cast<Expr*>(CE);
1868 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001869 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001870 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1871 }
1872 return R;
1873}
1874
Richard Smith7a614d82011-06-11 17:19:42 +00001875static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1876 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001877 bool NullThrows = true) {
1878 if (!D)
1879 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1880
1881 // See if we can get a function type from the decl somehow.
1882 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1883 if (!VD) // If we have no clue what we're calling, assume the worst.
1884 return Expr::CT_Can;
1885
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001886 // As an extension, we assume that __attribute__((nothrow)) functions don't
1887 // throw.
1888 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1889 return Expr::CT_Cannot;
1890
Sebastian Redl369e51f2010-09-10 20:55:33 +00001891 QualType T = VD->getType();
1892 const FunctionProtoType *FT;
1893 if ((FT = T->getAs<FunctionProtoType>())) {
1894 } else if (const PointerType *PT = T->getAs<PointerType>())
1895 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1896 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1897 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1898 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1899 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1900 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1901 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1902
1903 if (!FT)
1904 return Expr::CT_Can;
1905
Richard Smith7a614d82011-06-11 17:19:42 +00001906 if (FT->getExceptionSpecType() == EST_Delayed) {
1907 assert(isa<CXXConstructorDecl>(D) &&
1908 "only constructor exception specs can be unknown");
1909 Ctx.getDiagnostics().Report(E->getLocStart(),
1910 diag::err_exception_spec_unknown)
1911 << E->getSourceRange();
1912 return Expr::CT_Can;
1913 }
1914
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001915 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001916}
1917
1918static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1919 if (DC->isTypeDependent())
1920 return Expr::CT_Dependent;
1921
Sebastian Redl295995c2010-09-10 20:55:47 +00001922 if (!DC->getTypeAsWritten()->isReferenceType())
1923 return Expr::CT_Cannot;
1924
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001925 if (DC->getSubExpr()->isTypeDependent())
1926 return Expr::CT_Dependent;
1927
Sebastian Redl369e51f2010-09-10 20:55:33 +00001928 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1929}
1930
1931static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1932 const CXXTypeidExpr *DC) {
1933 if (DC->isTypeOperand())
1934 return Expr::CT_Cannot;
1935
1936 Expr *Op = DC->getExprOperand();
1937 if (Op->isTypeDependent())
1938 return Expr::CT_Dependent;
1939
1940 const RecordType *RT = Op->getType()->getAs<RecordType>();
1941 if (!RT)
1942 return Expr::CT_Cannot;
1943
1944 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1945 return Expr::CT_Cannot;
1946
1947 if (Op->Classify(C).isPRValue())
1948 return Expr::CT_Cannot;
1949
1950 return Expr::CT_Can;
1951}
1952
1953Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1954 // C++ [expr.unary.noexcept]p3:
1955 // [Can throw] if in a potentially-evaluated context the expression would
1956 // contain:
1957 switch (getStmtClass()) {
1958 case CXXThrowExprClass:
1959 // - a potentially evaluated throw-expression
1960 return CT_Can;
1961
1962 case CXXDynamicCastExprClass: {
1963 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1964 // where T is a reference type, that requires a run-time check
1965 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1966 if (CT == CT_Can)
1967 return CT;
1968 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1969 }
1970
1971 case CXXTypeidExprClass:
1972 // - a potentially evaluated typeid expression applied to a glvalue
1973 // expression whose type is a polymorphic class type
1974 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1975
1976 // - a potentially evaluated call to a function, member function, function
1977 // pointer, or member function pointer that does not have a non-throwing
1978 // exception-specification
1979 case CallExprClass:
1980 case CXXOperatorCallExprClass:
1981 case CXXMemberCallExprClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001982 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001983 CanThrowResult CT;
1984 if (isTypeDependent())
1985 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001986 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1987 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001988 else
Richard Smith7a614d82011-06-11 17:19:42 +00001989 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001990 if (CT == CT_Can)
1991 return CT;
1992 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1993 }
1994
Sebastian Redl295995c2010-09-10 20:55:47 +00001995 case CXXConstructExprClass:
1996 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00001997 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001998 cast<CXXConstructExpr>(this)->getConstructor());
1999 if (CT == CT_Can)
2000 return CT;
2001 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2002 }
2003
2004 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002005 CanThrowResult CT;
2006 if (isTypeDependent())
2007 CT = CT_Dependent;
2008 else
2009 CT = MergeCanThrow(
Richard Smith7a614d82011-06-11 17:19:42 +00002010 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
2011 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00002012 /*NullThrows*/false));
2013 if (CT == CT_Can)
2014 return CT;
2015 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2016 }
2017
2018 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002019 CanThrowResult CT;
2020 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
2021 if (DTy.isNull() || DTy->isDependentType()) {
2022 CT = CT_Dependent;
2023 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00002024 CT = CanCalleeThrow(C, this,
2025 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002026 if (const RecordType *RT = DTy->getAs<RecordType>()) {
2027 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00002028 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002029 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002030 if (CT == CT_Can)
2031 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002032 }
2033 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2034 }
2035
2036 case CXXBindTemporaryExprClass: {
2037 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00002038 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002039 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
2040 if (CT == CT_Can)
2041 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00002042 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2043 }
2044
2045 // ObjC message sends are like function calls, but never have exception
2046 // specs.
2047 case ObjCMessageExprClass:
2048 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002049 return CT_Can;
2050
2051 // Many other things have subexpressions, so we have to test those.
2052 // Some are simple:
2053 case ParenExprClass:
2054 case MemberExprClass:
2055 case CXXReinterpretCastExprClass:
2056 case CXXConstCastExprClass:
2057 case ConditionalOperatorClass:
2058 case CompoundLiteralExprClass:
2059 case ExtVectorElementExprClass:
2060 case InitListExprClass:
2061 case DesignatedInitExprClass:
2062 case ParenListExprClass:
2063 case VAArgExprClass:
2064 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00002065 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002066 case ObjCIvarRefExprClass:
2067 case ObjCIsaExprClass:
2068 case ShuffleVectorExprClass:
2069 return CanSubExprsThrow(C, this);
2070
2071 // Some might be dependent for other reasons.
2072 case UnaryOperatorClass:
2073 case ArraySubscriptExprClass:
2074 case ImplicitCastExprClass:
2075 case CStyleCastExprClass:
2076 case CXXStaticCastExprClass:
2077 case CXXFunctionalCastExprClass:
2078 case BinaryOperatorClass:
Douglas Gregor03e80032011-06-21 17:03:29 +00002079 case CompoundAssignOperatorClass:
2080 case MaterializeTemporaryExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002081 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2082 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2083 }
2084
2085 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2086 case StmtExprClass:
2087 return CT_Can;
2088
2089 case ChooseExprClass:
2090 if (isTypeDependent() || isValueDependent())
2091 return CT_Dependent;
2092 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2093
Peter Collingbournef111d932011-04-15 00:35:48 +00002094 case GenericSelectionExprClass:
2095 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2096 return CT_Dependent;
2097 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2098
Sebastian Redl369e51f2010-09-10 20:55:33 +00002099 // Some expressions are always dependent.
2100 case DependentScopeDeclRefExprClass:
2101 case CXXUnresolvedConstructExprClass:
2102 case CXXDependentScopeMemberExprClass:
2103 return CT_Dependent;
2104
2105 default:
2106 // All other expressions don't have subexpressions, or else they are
2107 // unevaluated.
2108 return CT_Cannot;
2109 }
2110}
2111
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002112Expr* Expr::IgnoreParens() {
2113 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002114 while (true) {
2115 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2116 E = P->getSubExpr();
2117 continue;
2118 }
2119 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2120 if (P->getOpcode() == UO_Extension) {
2121 E = P->getSubExpr();
2122 continue;
2123 }
2124 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002125 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2126 if (!P->isResultDependent()) {
2127 E = P->getResultExpr();
2128 continue;
2129 }
2130 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002131 return E;
2132 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002133}
2134
Chris Lattner56f34942008-02-13 01:02:39 +00002135/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2136/// or CastExprs or ImplicitCastExprs, returning their operand.
2137Expr *Expr::IgnoreParenCasts() {
2138 Expr *E = this;
2139 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002140 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002141 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002142 continue;
2143 }
2144 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002145 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002146 continue;
2147 }
2148 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2149 if (P->getOpcode() == UO_Extension) {
2150 E = P->getSubExpr();
2151 continue;
2152 }
2153 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002154 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2155 if (!P->isResultDependent()) {
2156 E = P->getResultExpr();
2157 continue;
2158 }
2159 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002160 if (MaterializeTemporaryExpr *Materialize
2161 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2162 E = Materialize->GetTemporaryExpr();
2163 continue;
2164 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002165 if (SubstNonTypeTemplateParmExpr *NTTP
2166 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2167 E = NTTP->getReplacement();
2168 continue;
2169 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002170 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002171 }
2172}
2173
John McCall9c5d70c2010-12-04 08:24:19 +00002174/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2175/// casts. This is intended purely as a temporary workaround for code
2176/// that hasn't yet been rewritten to do the right thing about those
2177/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002178Expr *Expr::IgnoreParenLValueCasts() {
2179 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002180 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002181 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2182 E = P->getSubExpr();
2183 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002184 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002185 if (P->getCastKind() == CK_LValueToRValue) {
2186 E = P->getSubExpr();
2187 continue;
2188 }
John McCall9c5d70c2010-12-04 08:24:19 +00002189 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2190 if (P->getOpcode() == UO_Extension) {
2191 E = P->getSubExpr();
2192 continue;
2193 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002194 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2195 if (!P->isResultDependent()) {
2196 E = P->getResultExpr();
2197 continue;
2198 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002199 } else if (MaterializeTemporaryExpr *Materialize
2200 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2201 E = Materialize->GetTemporaryExpr();
2202 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002203 } else if (SubstNonTypeTemplateParmExpr *NTTP
2204 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2205 E = NTTP->getReplacement();
2206 continue;
John McCallf6a16482010-12-04 03:47:34 +00002207 }
2208 break;
2209 }
2210 return E;
2211}
2212
John McCall2fc46bf2010-05-05 22:59:52 +00002213Expr *Expr::IgnoreParenImpCasts() {
2214 Expr *E = this;
2215 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002216 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002217 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002218 continue;
2219 }
2220 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002221 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002222 continue;
2223 }
2224 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2225 if (P->getOpcode() == UO_Extension) {
2226 E = P->getSubExpr();
2227 continue;
2228 }
2229 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002230 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2231 if (!P->isResultDependent()) {
2232 E = P->getResultExpr();
2233 continue;
2234 }
2235 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002236 if (MaterializeTemporaryExpr *Materialize
2237 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2238 E = Materialize->GetTemporaryExpr();
2239 continue;
2240 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002241 if (SubstNonTypeTemplateParmExpr *NTTP
2242 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2243 E = NTTP->getReplacement();
2244 continue;
2245 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002246 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002247 }
2248}
2249
Hans Wennborg2f072b42011-06-09 17:06:51 +00002250Expr *Expr::IgnoreConversionOperator() {
2251 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002252 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002253 return MCE->getImplicitObjectArgument();
2254 }
2255 return this;
2256}
2257
Chris Lattnerecdd8412009-03-13 17:28:01 +00002258/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2259/// value (including ptr->int casts of the same size). Strip off any
2260/// ParenExpr or CastExprs, returning their operand.
2261Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2262 Expr *E = this;
2263 while (true) {
2264 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2265 E = P->getSubExpr();
2266 continue;
2267 }
Mike Stump1eb44332009-09-09 15:08:12 +00002268
Chris Lattnerecdd8412009-03-13 17:28:01 +00002269 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2270 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002271 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002272 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002273
Chris Lattnerecdd8412009-03-13 17:28:01 +00002274 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2275 E = SE;
2276 continue;
2277 }
Mike Stump1eb44332009-09-09 15:08:12 +00002278
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002279 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002280 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002281 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002282 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002283 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2284 E = SE;
2285 continue;
2286 }
2287 }
Mike Stump1eb44332009-09-09 15:08:12 +00002288
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002289 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2290 if (P->getOpcode() == UO_Extension) {
2291 E = P->getSubExpr();
2292 continue;
2293 }
2294 }
2295
Peter Collingbournef111d932011-04-15 00:35:48 +00002296 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2297 if (!P->isResultDependent()) {
2298 E = P->getResultExpr();
2299 continue;
2300 }
2301 }
2302
Douglas Gregorc0244c52011-09-08 17:56:33 +00002303 if (SubstNonTypeTemplateParmExpr *NTTP
2304 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2305 E = NTTP->getReplacement();
2306 continue;
2307 }
2308
Chris Lattnerecdd8412009-03-13 17:28:01 +00002309 return E;
2310 }
2311}
2312
Douglas Gregor6eef5192009-12-14 19:27:10 +00002313bool Expr::isDefaultArgument() const {
2314 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002315 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2316 E = M->GetTemporaryExpr();
2317
Douglas Gregor6eef5192009-12-14 19:27:10 +00002318 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2319 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002320
Douglas Gregor6eef5192009-12-14 19:27:10 +00002321 return isa<CXXDefaultArgExpr>(E);
2322}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002323
Douglas Gregor2f599792010-04-02 18:24:57 +00002324/// \brief Skip over any no-op casts and any temporary-binding
2325/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002326static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002327 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2328 E = M->GetTemporaryExpr();
2329
Douglas Gregor2f599792010-04-02 18:24:57 +00002330 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002331 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002332 E = ICE->getSubExpr();
2333 else
2334 break;
2335 }
2336
2337 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2338 E = BE->getSubExpr();
2339
2340 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002341 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002342 E = ICE->getSubExpr();
2343 else
2344 break;
2345 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002346
2347 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002348}
2349
John McCall558d2ab2010-09-15 10:14:12 +00002350/// isTemporaryObject - Determines if this expression produces a
2351/// temporary of the given class type.
2352bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2353 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2354 return false;
2355
Anders Carlssonf8b30152010-11-28 16:40:49 +00002356 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002357
John McCall58277b52010-09-15 20:59:13 +00002358 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002359 if (!E->Classify(C).isPRValue()) {
2360 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002361 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002362 return false;
2363 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002364
John McCall19e60ad2010-09-16 06:57:56 +00002365 // Black-list a few cases which yield pr-values of class type that don't
2366 // refer to temporaries of that type:
2367
2368 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002369 if (isa<ImplicitCastExpr>(E)) {
2370 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2371 case CK_DerivedToBase:
2372 case CK_UncheckedDerivedToBase:
2373 return false;
2374 default:
2375 break;
2376 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002377 }
2378
John McCall19e60ad2010-09-16 06:57:56 +00002379 // - member expressions (all)
2380 if (isa<MemberExpr>(E))
2381 return false;
2382
John McCall56ca35d2011-02-17 10:25:35 +00002383 // - opaque values (all)
2384 if (isa<OpaqueValueExpr>(E))
2385 return false;
2386
John McCall558d2ab2010-09-15 10:14:12 +00002387 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002388}
2389
Douglas Gregor75e85042011-03-02 21:06:53 +00002390bool Expr::isImplicitCXXThis() const {
2391 const Expr *E = this;
2392
2393 // Strip away parentheses and casts we don't care about.
2394 while (true) {
2395 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2396 E = Paren->getSubExpr();
2397 continue;
2398 }
2399
2400 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2401 if (ICE->getCastKind() == CK_NoOp ||
2402 ICE->getCastKind() == CK_LValueToRValue ||
2403 ICE->getCastKind() == CK_DerivedToBase ||
2404 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2405 E = ICE->getSubExpr();
2406 continue;
2407 }
2408 }
2409
2410 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2411 if (UnOp->getOpcode() == UO_Extension) {
2412 E = UnOp->getSubExpr();
2413 continue;
2414 }
2415 }
2416
Douglas Gregor03e80032011-06-21 17:03:29 +00002417 if (const MaterializeTemporaryExpr *M
2418 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2419 E = M->GetTemporaryExpr();
2420 continue;
2421 }
2422
Douglas Gregor75e85042011-03-02 21:06:53 +00002423 break;
2424 }
2425
2426 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2427 return This->isImplicit();
2428
2429 return false;
2430}
2431
Douglas Gregor898574e2008-12-05 23:32:09 +00002432/// hasAnyTypeDependentArguments - Determines if any of the expressions
2433/// in Exprs is type-dependent.
2434bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2435 for (unsigned I = 0; I < NumExprs; ++I)
2436 if (Exprs[I]->isTypeDependent())
2437 return true;
2438
2439 return false;
2440}
2441
2442/// hasAnyValueDependentArguments - Determines if any of the expressions
2443/// in Exprs is value-dependent.
2444bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2445 for (unsigned I = 0; I < NumExprs; ++I)
2446 if (Exprs[I]->isValueDependent())
2447 return true;
2448
2449 return false;
2450}
2451
John McCall4204f072010-08-02 21:13:48 +00002452bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002453 // This function is attempting whether an expression is an initializer
2454 // which can be evaluated at compile-time. isEvaluatable handles most
2455 // of the cases, but it can't deal with some initializer-specific
2456 // expressions, and it can't deal with aggregates; we deal with those here,
2457 // and fall back to isEvaluatable for the other cases.
2458
John McCall4204f072010-08-02 21:13:48 +00002459 // If we ever capture reference-binding directly in the AST, we can
2460 // kill the second parameter.
2461
2462 if (IsForRef) {
2463 EvalResult Result;
2464 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2465 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002466
Anders Carlssone8a32b82008-11-24 05:23:59 +00002467 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002468 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002469 case IntegerLiteralClass:
2470 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002471 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002472 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002473 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002474 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002475 case CXXTemporaryObjectExprClass:
2476 case CXXConstructExprClass: {
2477 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002478
2479 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002480 if (CE->getConstructor()->isTrivial()) {
2481 // 1) an application of the trivial default constructor or
2482 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002483
Richard Smith180f4792011-11-10 06:34:14 +00002484 // 2) an elidable trivial copy construction of an operand which is
2485 // itself a constant initializer. Note that we consider the
2486 // operand on its own, *not* as a reference binding.
2487 if (CE->isElidable() &&
2488 CE->getArg(0)->isConstantInitializer(Ctx, false))
2489 return true;
2490 }
2491
2492 // 3) a foldable constexpr constructor.
2493 break;
John McCallb4b9b152010-08-01 21:51:45 +00002494 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002495 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002496 // This handles gcc's extension that allows global initializers like
2497 // "struct x {int x;} x = (struct x) {};".
2498 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002499 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002500 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002501 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002502 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002503 // FIXME: This doesn't deal with fields with reference types correctly.
2504 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2505 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002506 const InitListExpr *Exp = cast<InitListExpr>(this);
2507 unsigned numInits = Exp->getNumInits();
2508 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002509 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002510 return false;
2511 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002512 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002513 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002514 case ImplicitValueInitExprClass:
2515 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002516 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002517 return cast<ParenExpr>(this)->getSubExpr()
2518 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002519 case GenericSelectionExprClass:
2520 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2521 return false;
2522 return cast<GenericSelectionExpr>(this)->getResultExpr()
2523 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002524 case ChooseExprClass:
2525 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2526 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002527 case UnaryOperatorClass: {
2528 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002529 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002530 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002531 break;
2532 }
John McCall4204f072010-08-02 21:13:48 +00002533 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002534 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002535 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002536 case CStyleCastExprClass: {
2537 const CastExpr *CE = cast<CastExpr>(this);
2538
2539 // Handle bitcasts of vector constants.
2540 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2541 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2542
Eli Friedman6bd97192011-12-21 00:43:02 +00002543 // Handle misc casts we want to ignore.
2544 // FIXME: Is it really safe to ignore all these?
2545 if (CE->getCastKind() == CK_NoOp ||
2546 CE->getCastKind() == CK_LValueToRValue ||
2547 CE->getCastKind() == CK_ToUnion ||
2548 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002549 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2550
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002551 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002552 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002553 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002554 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002555 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002556 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002557 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002558}
2559
Chandler Carruth82214a82011-02-18 23:54:50 +00002560/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2561/// pointer constant or not, as well as the specific kind of constant detected.
2562/// Null pointer constants can be integer constant expressions with the
2563/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2564/// (a GNU extension).
2565Expr::NullPointerConstantKind
2566Expr::isNullPointerConstant(ASTContext &Ctx,
2567 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002568 if (isValueDependent()) {
2569 switch (NPC) {
2570 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002571 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002572 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002573 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2574 return NPCK_ZeroInteger;
2575 else
2576 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002577
Douglas Gregorce940492009-09-25 04:25:58 +00002578 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002579 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002580 }
2581 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002582
Sebastian Redl07779722008-10-31 14:43:28 +00002583 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002584 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002585 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002586 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002587 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002588 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002589 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002590 Pointee->isVoidType() && // to void*
2591 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002592 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002593 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002594 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002595 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2596 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002597 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002598 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2599 // Accept ((void*)0) as a null pointer constant, as many other
2600 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002601 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002602 } else if (const GenericSelectionExpr *GE =
2603 dyn_cast<GenericSelectionExpr>(this)) {
2604 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002605 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002606 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002607 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002608 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002609 } else if (isa<GNUNullExpr>(this)) {
2610 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002611 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002612 } else if (const MaterializeTemporaryExpr *M
2613 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2614 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002615 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2616 if (const Expr *Source = OVE->getSourceExpr())
2617 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002618 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002619
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002620 // C++0x nullptr_t is always a null pointer constant.
2621 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002622 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002623
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002624 if (const RecordType *UT = getType()->getAsUnionType())
2625 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2626 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2627 const Expr *InitExpr = CLE->getInitializer();
2628 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2629 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2630 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002631 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002632 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002633 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002634 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002635
Reid Spencer5f016e22007-07-11 17:01:13 +00002636 // If we have an integer constant expression, we need to *evaluate* it and
2637 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002638 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002639 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2640
2641 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002642}
Steve Naroff31a45842007-07-28 23:10:27 +00002643
John McCallf6a16482010-12-04 03:47:34 +00002644/// \brief If this expression is an l-value for an Objective C
2645/// property, find the underlying property reference expression.
2646const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2647 const Expr *E = this;
2648 while (true) {
2649 assert((E->getValueKind() == VK_LValue &&
2650 E->getObjectKind() == OK_ObjCProperty) &&
2651 "expression is not a property reference");
2652 E = E->IgnoreParenCasts();
2653 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2654 if (BO->getOpcode() == BO_Comma) {
2655 E = BO->getRHS();
2656 continue;
2657 }
2658 }
2659
2660 break;
2661 }
2662
2663 return cast<ObjCPropertyRefExpr>(E);
2664}
2665
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002666FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002667 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002668
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002669 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002670 if (ICE->getCastKind() == CK_LValueToRValue ||
2671 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002672 E = ICE->getSubExpr()->IgnoreParens();
2673 else
2674 break;
2675 }
2676
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002677 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002678 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002679 if (Field->isBitField())
2680 return Field;
2681
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002682 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2683 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2684 if (Field->isBitField())
2685 return Field;
2686
Eli Friedman42068e92011-07-13 02:05:57 +00002687 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002688 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2689 return BinOp->getLHS()->getBitField();
2690
Eli Friedman42068e92011-07-13 02:05:57 +00002691 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2692 return BinOp->getRHS()->getBitField();
2693 }
2694
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002695 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002696}
2697
Anders Carlsson09380262010-01-31 17:18:49 +00002698bool Expr::refersToVectorElement() const {
2699 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002700
Anders Carlsson09380262010-01-31 17:18:49 +00002701 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002702 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002703 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002704 E = ICE->getSubExpr()->IgnoreParens();
2705 else
2706 break;
2707 }
Sean Huntc3021132010-05-05 15:23:54 +00002708
Anders Carlsson09380262010-01-31 17:18:49 +00002709 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2710 return ASE->getBase()->getType()->isVectorType();
2711
2712 if (isa<ExtVectorElementExpr>(E))
2713 return true;
2714
2715 return false;
2716}
2717
Chris Lattner2140e902009-02-16 22:14:05 +00002718/// isArrow - Return true if the base expression is a pointer to vector,
2719/// return false if the base expression is a vector.
2720bool ExtVectorElementExpr::isArrow() const {
2721 return getBase()->getType()->isPointerType();
2722}
2723
Nate Begeman213541a2008-04-18 23:10:10 +00002724unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002725 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002726 return VT->getNumElements();
2727 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002728}
2729
Nate Begeman8a997642008-05-09 06:41:27 +00002730/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002731bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002732 // FIXME: Refactor this code to an accessor on the AST node which returns the
2733 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002734 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002735
2736 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002737 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002738 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002739
Nate Begeman190d6a22009-01-18 02:01:21 +00002740 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002741 if (Comp[0] == 's' || Comp[0] == 'S')
2742 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002743
Daniel Dunbar15027422009-10-17 23:53:04 +00002744 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002745 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002746 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002747
Steve Narofffec0b492007-07-30 03:29:09 +00002748 return false;
2749}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002750
Nate Begeman8a997642008-05-09 06:41:27 +00002751/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002752void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002753 SmallVectorImpl<unsigned> &Elts) const {
2754 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002755 if (Comp[0] == 's' || Comp[0] == 'S')
2756 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002757
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002758 bool isHi = Comp == "hi";
2759 bool isLo = Comp == "lo";
2760 bool isEven = Comp == "even";
2761 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002762
Nate Begeman8a997642008-05-09 06:41:27 +00002763 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2764 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002765
Nate Begeman8a997642008-05-09 06:41:27 +00002766 if (isHi)
2767 Index = e + i;
2768 else if (isLo)
2769 Index = i;
2770 else if (isEven)
2771 Index = 2 * i;
2772 else if (isOdd)
2773 Index = 2 * i + 1;
2774 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002775 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002776
Nate Begeman3b8d1162008-05-13 21:03:02 +00002777 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002778 }
Nate Begeman8a997642008-05-09 06:41:27 +00002779}
2780
Douglas Gregor04badcf2010-04-21 00:45:42 +00002781ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002782 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002783 SourceLocation LBracLoc,
2784 SourceLocation SuperLoc,
2785 bool IsInstanceSuper,
2786 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002787 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002788 ArrayRef<SourceLocation> SelLocs,
2789 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002790 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002791 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002792 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002793 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002794 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002795 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002796 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002797 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2798 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002799 Kind(IsInstanceSuper? SuperInstance : SuperClass),
2800 HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002801 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002802{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002803 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002804 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002805}
2806
Douglas Gregor04badcf2010-04-21 00:45:42 +00002807ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002808 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002809 SourceLocation LBracLoc,
2810 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002811 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002812 ArrayRef<SourceLocation> SelLocs,
2813 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002814 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002815 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002816 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002817 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002818 T->isDependentType(), T->isInstantiationDependentType(),
2819 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002820 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2821 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002822 Kind(Class),
2823 HasMethod(Method != 0), IsDelegateInitCall(false),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002824 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002825{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002826 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002827 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002828}
2829
Douglas Gregor04badcf2010-04-21 00:45:42 +00002830ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002831 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002832 SourceLocation LBracLoc,
2833 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002834 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002835 ArrayRef<SourceLocation> SelLocs,
2836 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002837 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002838 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002839 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002840 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002841 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002842 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002843 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002844 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2845 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002846 Kind(Instance),
2847 HasMethod(Method != 0), IsDelegateInitCall(false),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002848 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002849{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002850 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002851 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002852}
2853
2854void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2855 ArrayRef<SourceLocation> SelLocs,
2856 SelectorLocationsKind SelLocsK) {
2857 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00002858 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002859 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002860 if (Args[I]->isTypeDependent())
2861 ExprBits.TypeDependent = true;
2862 if (Args[I]->isValueDependent())
2863 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002864 if (Args[I]->isInstantiationDependent())
2865 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002866 if (Args[I]->containsUnexpandedParameterPack())
2867 ExprBits.ContainsUnexpandedParameterPack = true;
2868
2869 MyArgs[I] = Args[I];
2870 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002871
2872 SelLocsKind = SelLocsK;
2873 if (SelLocsK == SelLoc_NonStandard)
2874 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
Chris Lattner0389e6b2009-04-26 00:44:05 +00002875}
2876
Douglas Gregor04badcf2010-04-21 00:45:42 +00002877ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002878 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002879 SourceLocation LBracLoc,
2880 SourceLocation SuperLoc,
2881 bool IsInstanceSuper,
2882 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002883 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002884 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002885 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002886 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002887 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002888 SelectorLocationsKind SelLocsK;
2889 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00002890 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002891 SuperType, Sel, SelLocs, SelLocsK,
2892 Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002893}
2894
2895ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002896 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002897 SourceLocation LBracLoc,
2898 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002899 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002900 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002901 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002902 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002903 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002904 SelectorLocationsKind SelLocsK;
2905 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002906 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002907 SelLocs, SelLocsK, Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002908}
2909
2910ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002911 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002912 SourceLocation LBracLoc,
2913 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002914 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002915 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002916 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002917 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002918 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002919 SelectorLocationsKind SelLocsK;
2920 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002921 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002922 SelLocs, SelLocsK, Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002923}
2924
Sean Huntc3021132010-05-05 15:23:54 +00002925ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002926 unsigned NumArgs,
2927 unsigned NumStoredSelLocs) {
2928 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002929 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2930}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002931
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002932ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2933 ArrayRef<Expr *> Args,
2934 SourceLocation RBraceLoc,
2935 ArrayRef<SourceLocation> SelLocs,
2936 Selector Sel,
2937 SelectorLocationsKind &SelLocsK) {
2938 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
2939 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
2940 : 0;
2941 return alloc(C, Args.size(), NumStoredSelLocs);
2942}
2943
2944ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2945 unsigned NumArgs,
2946 unsigned NumStoredSelLocs) {
2947 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2948 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
2949 return (ObjCMessageExpr *)C.Allocate(Size,
2950 llvm::AlignOf<ObjCMessageExpr>::Alignment);
2951}
2952
2953void ObjCMessageExpr::getSelectorLocs(
2954 SmallVectorImpl<SourceLocation> &SelLocs) const {
2955 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
2956 SelLocs.push_back(getSelectorLoc(i));
2957}
2958
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002959SourceRange ObjCMessageExpr::getReceiverRange() const {
2960 switch (getReceiverKind()) {
2961 case Instance:
2962 return getInstanceReceiver()->getSourceRange();
2963
2964 case Class:
2965 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2966
2967 case SuperInstance:
2968 case SuperClass:
2969 return getSuperLoc();
2970 }
2971
2972 return SourceLocation();
2973}
2974
Douglas Gregor04badcf2010-04-21 00:45:42 +00002975Selector ObjCMessageExpr::getSelector() const {
2976 if (HasMethod)
2977 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2978 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002979 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002980}
2981
2982ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2983 switch (getReceiverKind()) {
2984 case Instance:
2985 if (const ObjCObjectPointerType *Ptr
2986 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2987 return Ptr->getInterfaceDecl();
2988 break;
2989
2990 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002991 if (const ObjCObjectType *Ty
2992 = getClassReceiver()->getAs<ObjCObjectType>())
2993 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002994 break;
2995
2996 case SuperInstance:
2997 if (const ObjCObjectPointerType *Ptr
2998 = getSuperType()->getAs<ObjCObjectPointerType>())
2999 return Ptr->getInterfaceDecl();
3000 break;
3001
3002 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00003003 if (const ObjCObjectType *Iface
3004 = getSuperType()->getAs<ObjCObjectType>())
3005 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003006 break;
3007 }
3008
3009 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003010}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003011
Chris Lattner5f9e2722011-07-23 10:55:15 +00003012StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003013 switch (getBridgeKind()) {
3014 case OBC_Bridge:
3015 return "__bridge";
3016 case OBC_BridgeTransfer:
3017 return "__bridge_transfer";
3018 case OBC_BridgeRetained:
3019 return "__bridge_retained";
3020 }
3021
3022 return "__bridge";
3023}
3024
Jay Foad4ba2a172011-01-12 09:06:06 +00003025bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003026 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003027}
3028
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003029ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3030 QualType Type, SourceLocation BLoc,
3031 SourceLocation RP)
3032 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3033 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003034 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003035 Type->containsUnexpandedParameterPack()),
3036 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3037{
3038 SubExprs = new (C) Stmt*[nexpr];
3039 for (unsigned i = 0; i < nexpr; i++) {
3040 if (args[i]->isTypeDependent())
3041 ExprBits.TypeDependent = true;
3042 if (args[i]->isValueDependent())
3043 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003044 if (args[i]->isInstantiationDependent())
3045 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003046 if (args[i]->containsUnexpandedParameterPack())
3047 ExprBits.ContainsUnexpandedParameterPack = true;
3048
3049 SubExprs[i] = args[i];
3050 }
3051}
3052
Nate Begeman888376a2009-08-12 02:28:50 +00003053void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3054 unsigned NumExprs) {
3055 if (SubExprs) C.Deallocate(SubExprs);
3056
3057 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003058 this->NumExprs = NumExprs;
3059 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003060}
Nate Begeman888376a2009-08-12 02:28:50 +00003061
Peter Collingbournef111d932011-04-15 00:35:48 +00003062GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3063 SourceLocation GenericLoc, Expr *ControllingExpr,
3064 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3065 unsigned NumAssocs, SourceLocation DefaultLoc,
3066 SourceLocation RParenLoc,
3067 bool ContainsUnexpandedParameterPack,
3068 unsigned ResultIndex)
3069 : Expr(GenericSelectionExprClass,
3070 AssocExprs[ResultIndex]->getType(),
3071 AssocExprs[ResultIndex]->getValueKind(),
3072 AssocExprs[ResultIndex]->getObjectKind(),
3073 AssocExprs[ResultIndex]->isTypeDependent(),
3074 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003075 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003076 ContainsUnexpandedParameterPack),
3077 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3078 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3079 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3080 RParenLoc(RParenLoc) {
3081 SubExprs[CONTROLLING] = ControllingExpr;
3082 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3083 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3084}
3085
3086GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3087 SourceLocation GenericLoc, Expr *ControllingExpr,
3088 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3089 unsigned NumAssocs, SourceLocation DefaultLoc,
3090 SourceLocation RParenLoc,
3091 bool ContainsUnexpandedParameterPack)
3092 : Expr(GenericSelectionExprClass,
3093 Context.DependentTy,
3094 VK_RValue,
3095 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003096 /*isTypeDependent=*/true,
3097 /*isValueDependent=*/true,
3098 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003099 ContainsUnexpandedParameterPack),
3100 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3101 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3102 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3103 RParenLoc(RParenLoc) {
3104 SubExprs[CONTROLLING] = ControllingExpr;
3105 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3106 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3107}
3108
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003109//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003110// DesignatedInitExpr
3111//===----------------------------------------------------------------------===//
3112
Chandler Carruthb1138242011-06-16 06:47:06 +00003113IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003114 assert(Kind == FieldDesignator && "Only valid on a field designator");
3115 if (Field.NameOrField & 0x01)
3116 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3117 else
3118 return getField()->getIdentifier();
3119}
3120
Sean Huntc3021132010-05-05 15:23:54 +00003121DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003122 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003123 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003124 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003125 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003126 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003127 unsigned NumIndexExprs,
3128 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003129 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003130 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003131 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003132 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003133 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003134 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3135 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003136 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003137
3138 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003139 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003140 *Child++ = Init;
3141
3142 // Copy the designators and their subexpressions, computing
3143 // value-dependence along the way.
3144 unsigned IndexIdx = 0;
3145 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003146 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003147
3148 if (this->Designators[I].isArrayDesignator()) {
3149 // Compute type- and value-dependence.
3150 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003151 if (Index->isTypeDependent() || Index->isValueDependent())
3152 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003153 if (Index->isInstantiationDependent())
3154 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003155 // Propagate unexpanded parameter packs.
3156 if (Index->containsUnexpandedParameterPack())
3157 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003158
3159 // Copy the index expressions into permanent storage.
3160 *Child++ = IndexExprs[IndexIdx++];
3161 } else if (this->Designators[I].isArrayRangeDesignator()) {
3162 // Compute type- and value-dependence.
3163 Expr *Start = IndexExprs[IndexIdx];
3164 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003165 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003166 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003167 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003168 ExprBits.InstantiationDependent = true;
3169 } else if (Start->isInstantiationDependent() ||
3170 End->isInstantiationDependent()) {
3171 ExprBits.InstantiationDependent = true;
3172 }
3173
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003174 // Propagate unexpanded parameter packs.
3175 if (Start->containsUnexpandedParameterPack() ||
3176 End->containsUnexpandedParameterPack())
3177 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003178
3179 // Copy the start/end expressions into permanent storage.
3180 *Child++ = IndexExprs[IndexIdx++];
3181 *Child++ = IndexExprs[IndexIdx++];
3182 }
3183 }
3184
3185 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003186}
3187
Douglas Gregor05c13a32009-01-22 00:58:24 +00003188DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003189DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003190 unsigned NumDesignators,
3191 Expr **IndexExprs, unsigned NumIndexExprs,
3192 SourceLocation ColonOrEqualLoc,
3193 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003194 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003195 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003196 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003197 ColonOrEqualLoc, UsesColonSyntax,
3198 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003199}
3200
Mike Stump1eb44332009-09-09 15:08:12 +00003201DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003202 unsigned NumIndexExprs) {
3203 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3204 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3205 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3206}
3207
Douglas Gregor319d57f2010-01-06 23:17:19 +00003208void DesignatedInitExpr::setDesignators(ASTContext &C,
3209 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003210 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003211 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003212 NumDesignators = NumDesigs;
3213 for (unsigned I = 0; I != NumDesigs; ++I)
3214 Designators[I] = Desigs[I];
3215}
3216
Abramo Bagnara24f46742011-03-16 15:08:46 +00003217SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3218 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3219 if (size() == 1)
3220 return DIE->getDesignator(0)->getSourceRange();
3221 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3222 DIE->getDesignator(size()-1)->getEndLocation());
3223}
3224
Douglas Gregor05c13a32009-01-22 00:58:24 +00003225SourceRange DesignatedInitExpr::getSourceRange() const {
3226 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003227 Designator &First =
3228 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003229 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003230 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003231 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3232 else
3233 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3234 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003235 StartLoc =
3236 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003237 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3238}
3239
Douglas Gregor05c13a32009-01-22 00:58:24 +00003240Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3241 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3242 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3243 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003244 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3245 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3246}
3247
3248Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003249 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003250 "Requires array range designator");
3251 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3252 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003253 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3254 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3255}
3256
3257Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003258 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003259 "Requires array range designator");
3260 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3261 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003262 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3263 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3264}
3265
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003266/// \brief Replaces the designator at index @p Idx with the series
3267/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003268void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003269 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003270 const Designator *Last) {
3271 unsigned NumNewDesignators = Last - First;
3272 if (NumNewDesignators == 0) {
3273 std::copy_backward(Designators + Idx + 1,
3274 Designators + NumDesignators,
3275 Designators + Idx);
3276 --NumNewDesignators;
3277 return;
3278 } else if (NumNewDesignators == 1) {
3279 Designators[Idx] = *First;
3280 return;
3281 }
3282
Mike Stump1eb44332009-09-09 15:08:12 +00003283 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003284 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003285 std::copy(Designators, Designators + Idx, NewDesignators);
3286 std::copy(First, Last, NewDesignators + Idx);
3287 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3288 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003289 Designators = NewDesignators;
3290 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3291}
3292
Mike Stump1eb44332009-09-09 15:08:12 +00003293ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003294 Expr **exprs, unsigned nexprs,
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003295 SourceLocation rparenloc, QualType T)
3296 : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003297 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003298 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003299 assert(!T.isNull() && "ParenListExpr must have a valid type");
Nate Begeman2ef13e52009-08-10 23:49:36 +00003300 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003301 for (unsigned i = 0; i != nexprs; ++i) {
3302 if (exprs[i]->isTypeDependent())
3303 ExprBits.TypeDependent = true;
3304 if (exprs[i]->isValueDependent())
3305 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003306 if (exprs[i]->isInstantiationDependent())
3307 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003308 if (exprs[i]->containsUnexpandedParameterPack())
3309 ExprBits.ContainsUnexpandedParameterPack = true;
3310
Nate Begeman2ef13e52009-08-10 23:49:36 +00003311 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003312 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003313}
3314
John McCalle996ffd2011-02-16 08:02:54 +00003315const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3316 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3317 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003318 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3319 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003320 e = cast<CXXConstructExpr>(e)->getArg(0);
3321 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3322 e = ice->getSubExpr();
3323 return cast<OpaqueValueExpr>(e);
3324}
3325
John McCall4b9c2d22011-11-06 09:01:30 +00003326PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3327 unsigned numSemanticExprs) {
3328 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3329 (1 + numSemanticExprs) * sizeof(Expr*),
3330 llvm::alignOf<PseudoObjectExpr>());
3331 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3332}
3333
3334PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3335 : Expr(PseudoObjectExprClass, shell) {
3336 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3337}
3338
3339PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3340 ArrayRef<Expr*> semantics,
3341 unsigned resultIndex) {
3342 assert(syntax && "no syntactic expression!");
3343 assert(semantics.size() && "no semantic expressions!");
3344
3345 QualType type;
3346 ExprValueKind VK;
3347 if (resultIndex == NoResult) {
3348 type = C.VoidTy;
3349 VK = VK_RValue;
3350 } else {
3351 assert(resultIndex < semantics.size());
3352 type = semantics[resultIndex]->getType();
3353 VK = semantics[resultIndex]->getValueKind();
3354 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3355 }
3356
3357 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3358 (1 + semantics.size()) * sizeof(Expr*),
3359 llvm::alignOf<PseudoObjectExpr>());
3360 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3361 resultIndex);
3362}
3363
3364PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3365 Expr *syntax, ArrayRef<Expr*> semantics,
3366 unsigned resultIndex)
3367 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3368 /*filled in at end of ctor*/ false, false, false, false) {
3369 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3370 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3371
3372 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3373 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3374 getSubExprsBuffer()[i] = E;
3375
3376 if (E->isTypeDependent())
3377 ExprBits.TypeDependent = true;
3378 if (E->isValueDependent())
3379 ExprBits.ValueDependent = true;
3380 if (E->isInstantiationDependent())
3381 ExprBits.InstantiationDependent = true;
3382 if (E->containsUnexpandedParameterPack())
3383 ExprBits.ContainsUnexpandedParameterPack = true;
3384
3385 if (isa<OpaqueValueExpr>(E))
3386 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3387 "opaque-value semantic expressions for pseudo-object "
3388 "operations must have sources");
3389 }
3390}
3391
Douglas Gregor05c13a32009-01-22 00:58:24 +00003392//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003393// ExprIterator.
3394//===----------------------------------------------------------------------===//
3395
3396Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3397Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3398Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3399const Expr* ConstExprIterator::operator[](size_t idx) const {
3400 return cast<Expr>(I[idx]);
3401}
3402const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3403const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3404
3405//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003406// Child Iterators for iterating over subexpressions/substatements
3407//===----------------------------------------------------------------------===//
3408
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003409// UnaryExprOrTypeTraitExpr
3410Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003411 // If this is of a type and the type is a VLA type (and not a typedef), the
3412 // size expression of the VLA needs to be treated as an executable expression.
3413 // Why isn't this weirdness documented better in StmtIterator?
3414 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003415 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003416 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003417 return child_range(child_iterator(T), child_iterator());
3418 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003419 }
John McCall63c00d72011-02-09 08:16:59 +00003420 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003421}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003422
Steve Naroff563477d2007-09-18 23:55:05 +00003423// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003424Stmt::child_range ObjCMessageExpr::children() {
3425 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003426 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003427 begin = reinterpret_cast<Stmt **>(this + 1);
3428 else
3429 begin = reinterpret_cast<Stmt **>(getArgs());
3430 return child_range(begin,
3431 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003432}
3433
Steve Naroff4eb206b2008-09-03 18:15:37 +00003434// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003435BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003436 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003437 bool constAdded)
Douglas Gregor561f8122011-07-01 01:22:09 +00003438 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003439 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003440 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003441{
Douglas Gregord967e312011-01-19 21:52:31 +00003442 bool TypeDependent = false;
3443 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +00003444 bool InstantiationDependent = false;
3445 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3446 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +00003447 ExprBits.TypeDependent = TypeDependent;
3448 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +00003449 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003450}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003451
3452
3453AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3454 QualType t, AtomicOp op, SourceLocation RP)
3455 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3456 false, false, false, false),
3457 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3458{
3459 for (unsigned i = 0; i < nexpr; i++) {
3460 if (args[i]->isTypeDependent())
3461 ExprBits.TypeDependent = true;
3462 if (args[i]->isValueDependent())
3463 ExprBits.ValueDependent = true;
3464 if (args[i]->isInstantiationDependent())
3465 ExprBits.InstantiationDependent = true;
3466 if (args[i]->containsUnexpandedParameterPack())
3467 ExprBits.ContainsUnexpandedParameterPack = true;
3468
3469 SubExprs[i] = args[i];
3470 }
3471}