blob: 6eb6116b01b6d75b7c0d37518141f8ab224ead2a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/Lexer.h"
Richard Smith7a614d82011-06-11 17:19:42 +000025#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000027#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000029#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000031#include <algorithm>
Eli Friedman64f45a22011-11-01 02:23:42 +000032#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000033using namespace clang;
34
Chris Lattner2b334bb2010-04-16 23:34:13 +000035/// isKnownToHaveBooleanValue - Return true if this is an integer expression
36/// that is known to return 0 or 1. This happens for _Bool/bool expressions
37/// but also int expressions which are produced by things like comparisons in
38/// C.
39bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000040 const Expr *E = IgnoreParens();
41
Chris Lattner2b334bb2010-04-16 23:34:13 +000042 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000043 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000044 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000045 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000046
Peter Collingbournef111d932011-04-15 00:35:48 +000047 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000048 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000049 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000050 return UO->getSubExpr()->isKnownToHaveBooleanValue();
51 default:
52 return false;
53 }
54 }
Sean Huntc3021132010-05-05 15:23:54 +000055
John McCall6907fbe2010-06-12 01:56:02 +000056 // Only look through implicit casts. If the user writes
57 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000058 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000059 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000060
Peter Collingbournef111d932011-04-15 00:35:48 +000061 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000062 switch (BO->getOpcode()) {
63 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000064 case BO_LT: // Relational operators.
65 case BO_GT:
66 case BO_LE:
67 case BO_GE:
68 case BO_EQ: // Equality operators.
69 case BO_NE:
70 case BO_LAnd: // AND operator.
71 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000072 return true;
Sean Huntc3021132010-05-05 15:23:54 +000073
John McCall2de56d12010-08-25 11:45:40 +000074 case BO_And: // Bitwise AND operator.
75 case BO_Xor: // Bitwise XOR operator.
76 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000077 // Handle things like (x==2)|(y==12).
78 return BO->getLHS()->isKnownToHaveBooleanValue() &&
79 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000080
John McCall2de56d12010-08-25 11:45:40 +000081 case BO_Comma:
82 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000083 return BO->getRHS()->isKnownToHaveBooleanValue();
84 }
85 }
Sean Huntc3021132010-05-05 15:23:54 +000086
Peter Collingbournef111d932011-04-15 00:35:48 +000087 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000088 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
89 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000090
Chris Lattner2b334bb2010-04-16 23:34:13 +000091 return false;
92}
93
John McCall63c00d72011-02-09 08:16:59 +000094// Amusing macro metaprogramming hack: check whether a class provides
95// a more specific implementation of getExprLoc().
96namespace {
97 /// This implementation is used when a class provides a custom
98 /// implementation of getExprLoc.
99 template <class E, class T>
100 SourceLocation getExprLocImpl(const Expr *expr,
101 SourceLocation (T::*v)() const) {
102 return static_cast<const E*>(expr)->getExprLoc();
103 }
104
105 /// This implementation is used when a class doesn't provide
106 /// a custom implementation of getExprLoc. Overload resolution
107 /// should pick it over the implementation above because it's
108 /// more specialized according to function template partial ordering.
109 template <class E>
110 SourceLocation getExprLocImpl(const Expr *expr,
111 SourceLocation (Expr::*v)() const) {
112 return static_cast<const E*>(expr)->getSourceRange().getBegin();
113 }
114}
115
116SourceLocation Expr::getExprLoc() const {
117 switch (getStmtClass()) {
118 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
119#define ABSTRACT_STMT(type)
120#define STMT(type, base) \
121 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
122#define EXPR(type, base) \
123 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
124#include "clang/AST/StmtNodes.inc"
125 }
126 llvm_unreachable("unknown statement kind");
127 return SourceLocation();
128}
129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130//===----------------------------------------------------------------------===//
131// Primary Expressions.
132//===----------------------------------------------------------------------===//
133
Douglas Gregor561f8122011-07-01 01:22:09 +0000134/// \brief Compute the type-, value-, and instantiation-dependence of a
135/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000136/// based on the declaration being referenced.
137static void computeDeclRefDependence(NamedDecl *D, QualType T,
138 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000139 bool &ValueDependent,
140 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000141 TypeDependent = false;
142 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000143 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000144
145 // (TD) C++ [temp.dep.expr]p3:
146 // An id-expression is type-dependent if it contains:
147 //
Sean Huntc3021132010-05-05 15:23:54 +0000148 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000149 //
150 // (VD) C++ [temp.dep.constexpr]p2:
151 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000152
Douglas Gregor0da76df2009-11-23 11:41:28 +0000153 // (TD) - an identifier that was declared with dependent type
154 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000155 if (T->isDependentType()) {
156 TypeDependent = true;
157 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000158 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000159 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000160 } else if (T->isInstantiationDependentType()) {
161 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000162 }
Douglas Gregord967e312011-01-19 21:52:31 +0000163
Douglas Gregor0da76df2009-11-23 11:41:28 +0000164 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000165 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000166 == DeclarationName::CXXConversionFunctionName) {
167 QualType T = D->getDeclName().getCXXNameType();
168 if (T->isDependentType()) {
169 TypeDependent = true;
170 ValueDependent = true;
171 InstantiationDependent = true;
172 return;
173 }
174
175 if (T->isInstantiationDependentType())
176 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000177 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000178
Douglas Gregor0da76df2009-11-23 11:41:28 +0000179 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000180 if (isa<NonTypeTemplateParmDecl>(D)) {
181 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000182 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000183 return;
184 }
185
Douglas Gregor0da76df2009-11-23 11:41:28 +0000186 // (VD) - a constant with integral or enumeration type and is
187 // initialized with an expression that is value-dependent.
Douglas Gregord967e312011-01-19 21:52:31 +0000188 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000189 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000190 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000191 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000192 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000193 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000194 InstantiationDependent = true;
195 }
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000196 }
Douglas Gregord967e312011-01-19 21:52:31 +0000197
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000198 // (VD) - FIXME: Missing from the standard:
199 // - a member function or a static data member of the current
200 // instantiation
201 else if (Var->isStaticDataMember() &&
Douglas Gregor561f8122011-07-01 01:22:09 +0000202 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000203 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000204 InstantiationDependent = true;
205 }
Douglas Gregord967e312011-01-19 21:52:31 +0000206
207 return;
208 }
209
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000210 // (VD) - FIXME: Missing from the standard:
211 // - a member function or a static data member of the current
212 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000213 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
214 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000215 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000216 return;
217 }
218}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000219
Douglas Gregord967e312011-01-19 21:52:31 +0000220void DeclRefExpr::computeDependence() {
221 bool TypeDependent = false;
222 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000223 bool InstantiationDependent = false;
224 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
225 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000226
227 // (TD) C++ [temp.dep.expr]p3:
228 // An id-expression is type-dependent if it contains:
229 //
230 // and
231 //
232 // (VD) C++ [temp.dep.constexpr]p2:
233 // An identifier is value-dependent if it is:
234 if (!TypeDependent && !ValueDependent &&
235 hasExplicitTemplateArgs() &&
236 TemplateSpecializationType::anyDependentTemplateArguments(
237 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000238 getNumTemplateArgs(),
239 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000240 TypeDependent = true;
241 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000242 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000243 }
244
245 ExprBits.TypeDependent = TypeDependent;
246 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000247 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000248
Douglas Gregor10738d32010-12-23 23:51:58 +0000249 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000250 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000251 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000252}
253
Chandler Carruth3aa81402011-05-01 23:48:14 +0000254DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000255 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000256 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000257 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000258 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000259 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000260 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
261 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000262 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000263 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000264 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
265 if (FoundD)
266 getInternalFoundDecl() = FoundD;
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000267 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Douglas Gregor561f8122011-07-01 01:22:09 +0000268 if (TemplateArgs) {
269 bool Dependent = false;
270 bool InstantiationDependent = false;
271 bool ContainsUnexpandedParameterPack = false;
272 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
273 InstantiationDependent,
274 ContainsUnexpandedParameterPack);
275 if (InstantiationDependent)
276 setInstantiationDependent(true);
277 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000278 DeclRefExprBits.HadMultipleCandidates = 0;
279
Abramo Bagnara25777432010-08-11 22:01:17 +0000280 computeDependence();
281}
282
Douglas Gregora2813ce2009-10-23 18:54:35 +0000283DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000284 NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000285 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000286 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000287 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000288 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000289 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000290 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000291 return Create(Context, QualifierLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000292 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000293 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000294}
295
296DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000297 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000298 ValueDecl *D,
299 const DeclarationNameInfo &NameInfo,
300 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000301 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000302 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000303 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000304 // Filter out cases where the found Decl is the same as the value refenenced.
305 if (D == FoundD)
306 FoundD = 0;
307
Douglas Gregora2813ce2009-10-23 18:54:35 +0000308 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000309 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000310 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000311 if (FoundD)
312 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000313 if (TemplateArgs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000314 Size += ASTTemplateArgumentListInfo::sizeFor(*TemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000315
Chris Lattner32488542010-10-30 05:14:06 +0000316 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth3aa81402011-05-01 23:48:14 +0000317 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
318 T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000319}
320
Chandler Carruth3aa81402011-05-01 23:48:14 +0000321DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000322 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000323 bool HasFoundDecl,
Douglas Gregordef03542011-02-04 12:01:24 +0000324 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000325 unsigned NumTemplateArgs) {
326 std::size_t Size = sizeof(DeclRefExpr);
327 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000328 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000329 if (HasFoundDecl)
330 Size += sizeof(NamedDecl *);
Douglas Gregordef03542011-02-04 12:01:24 +0000331 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000332 Size += ASTTemplateArgumentListInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000333
Chris Lattner32488542010-10-30 05:14:06 +0000334 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000335 return new (Mem) DeclRefExpr(EmptyShell());
336}
337
Douglas Gregora2813ce2009-10-23 18:54:35 +0000338SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000339 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000340 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000341 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000342 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000343 R.setEnd(getRAngleLoc());
344 return R;
345}
346
Anders Carlsson3a082d82009-09-08 18:24:21 +0000347// FIXME: Maybe this should use DeclPrinter with a special "print predefined
348// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000349std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
350 ASTContext &Context = CurrentDecl->getASTContext();
351
Anders Carlsson3a082d82009-09-08 18:24:21 +0000352 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000353 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000354 return FD->getNameAsString();
355
356 llvm::SmallString<256> Name;
357 llvm::raw_svector_ostream Out(Name);
358
359 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000360 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000361 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000362 if (MD->isStatic())
363 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000364 }
365
366 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000367
368 std::string Proto = FD->getQualifiedNameAsString(Policy);
369
John McCall183700f2009-09-21 23:43:11 +0000370 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000371 const FunctionProtoType *FT = 0;
372 if (FD->hasWrittenPrototype())
373 FT = dyn_cast<FunctionProtoType>(AFT);
374
375 Proto += "(";
376 if (FT) {
377 llvm::raw_string_ostream POut(Proto);
378 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
379 if (i) POut << ", ";
380 std::string Param;
381 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
382 POut << Param;
383 }
384
385 if (FT->isVariadic()) {
386 if (FD->getNumParams()) POut << ", ";
387 POut << "...";
388 }
389 }
390 Proto += ")";
391
Sam Weinig4eadcc52009-12-27 01:38:20 +0000392 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
393 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
394 if (ThisQuals.hasConst())
395 Proto += " const";
396 if (ThisQuals.hasVolatile())
397 Proto += " volatile";
398 }
399
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000400 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
401 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000402
403 Out << Proto;
404
405 Out.flush();
406 return Name.str().str();
407 }
408 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
409 llvm::SmallString<256> Name;
410 llvm::raw_svector_ostream Out(Name);
411 Out << (MD->isInstanceMethod() ? '-' : '+');
412 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000413
414 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
415 // a null check to avoid a crash.
416 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000417 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000418
Anders Carlsson3a082d82009-09-08 18:24:21 +0000419 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000420 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
421 Out << '(' << CID << ')';
422
Anders Carlsson3a082d82009-09-08 18:24:21 +0000423 Out << ' ';
424 Out << MD->getSelector().getAsString();
425 Out << ']';
426
427 Out.flush();
428 return Name.str().str();
429 }
430 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
431 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
432 return "top level";
433 }
434 return "";
435}
436
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000437void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
438 if (hasAllocation())
439 C.Deallocate(pVal);
440
441 BitWidth = Val.getBitWidth();
442 unsigned NumWords = Val.getNumWords();
443 const uint64_t* Words = Val.getRawData();
444 if (NumWords > 1) {
445 pVal = new (C) uint64_t[NumWords];
446 std::copy(Words, Words + NumWords, pVal);
447 } else if (NumWords == 1)
448 VAL = Words[0];
449 else
450 VAL = 0;
451}
452
453IntegerLiteral *
454IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
455 QualType type, SourceLocation l) {
456 return new (C) IntegerLiteral(C, V, type, l);
457}
458
459IntegerLiteral *
460IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
461 return new (C) IntegerLiteral(Empty);
462}
463
464FloatingLiteral *
465FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
466 bool isexact, QualType Type, SourceLocation L) {
467 return new (C) FloatingLiteral(C, V, isexact, Type, L);
468}
469
470FloatingLiteral *
471FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
472 return new (C) FloatingLiteral(Empty);
473}
474
Chris Lattnerda8249e2008-06-07 22:13:43 +0000475/// getValueAsApproximateDouble - This returns the value as an inaccurate
476/// double. Note that this may cause loss of precision, but is useful for
477/// debugging dumps, etc.
478double FloatingLiteral::getValueAsApproximateDouble() const {
479 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000480 bool ignored;
481 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
482 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000483 return V.convertToDouble();
484}
485
Eli Friedman64f45a22011-11-01 02:23:42 +0000486int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
487 int CharByteWidth;
488 switch(k) {
489 case Ascii:
490 case UTF8:
491 CharByteWidth = target.getCharWidth();
492 break;
493 case Wide:
494 CharByteWidth = target.getWCharWidth();
495 break;
496 case UTF16:
497 CharByteWidth = target.getChar16Width();
498 break;
499 case UTF32:
500 CharByteWidth = target.getChar32Width();
501 }
502 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
503 CharByteWidth /= 8;
504 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
505 && "character byte widths supported are 1, 2, and 4 only");
506 return CharByteWidth;
507}
508
Chris Lattner5f9e2722011-07-23 10:55:15 +0000509StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000510 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000511 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000512 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000513 // Allocate enough space for the StringLiteral plus an array of locations for
514 // any concatenated string tokens.
515 void *Mem = C.Allocate(sizeof(StringLiteral)+
516 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000517 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000518 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000521 SL->setString(C,Str,Kind,Pascal);
522
Chris Lattner2085fd62009-02-18 06:40:38 +0000523 SL->TokLocs[0] = Loc[0];
524 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000525
Chris Lattner726e1682009-02-18 05:49:11 +0000526 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000527 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
528 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000529}
530
Douglas Gregor673ecd62009-04-15 16:35:07 +0000531StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
532 void *Mem = C.Allocate(sizeof(StringLiteral)+
533 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000534 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000535 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000536 SL->CharByteWidth = 0;
537 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000538 SL->NumConcatenated = NumStrs;
539 return SL;
540}
541
Eli Friedman64f45a22011-11-01 02:23:42 +0000542void StringLiteral::setString(ASTContext &C, StringRef Str,
543 StringKind Kind, bool IsPascal) {
544 //FIXME: we assume that the string data comes from a target that uses the same
545 // code unit size and endianess for the type of string.
546 this->Kind = Kind;
547 this->IsPascal = IsPascal;
548
549 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
550 assert((Str.size()%CharByteWidth == 0)
551 && "size of data must be multiple of CharByteWidth");
552 Length = Str.size()/CharByteWidth;
553
554 switch(CharByteWidth) {
555 case 1: {
556 char *AStrData = new (C) char[Length];
557 std::memcpy(AStrData,Str.data(),Str.size());
558 StrData.asChar = AStrData;
559 break;
560 }
561 case 2: {
562 uint16_t *AStrData = new (C) uint16_t[Length];
563 std::memcpy(AStrData,Str.data(),Str.size());
564 StrData.asUInt16 = AStrData;
565 break;
566 }
567 case 4: {
568 uint32_t *AStrData = new (C) uint32_t[Length];
569 std::memcpy(AStrData,Str.data(),Str.size());
570 StrData.asUInt32 = AStrData;
571 break;
572 }
573 default:
574 assert(false && "unsupported CharByteWidth");
575 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000576}
577
Chris Lattner08f92e32010-11-17 07:37:15 +0000578/// getLocationOfByte - Return a source location that points to the specified
579/// byte of this string literal.
580///
581/// Strings are amazingly complex. They can be formed from multiple tokens and
582/// can have escape sequences in them in addition to the usual trigraph and
583/// escaped newline business. This routine handles this complexity.
584///
585SourceLocation StringLiteral::
586getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
587 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000588 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
589
Chris Lattner08f92e32010-11-17 07:37:15 +0000590 // Loop over all of the tokens in this string until we find the one that
591 // contains the byte we're looking for.
592 unsigned TokNo = 0;
593 while (1) {
594 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
595 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
596
597 // Get the spelling of the string so that we can get the data that makes up
598 // the string literal, not the identifier for the macro it is potentially
599 // expanded through.
600 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
601
602 // Re-lex the token to get its length and original spelling.
603 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
604 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000605 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000606 if (Invalid)
607 return StrTokSpellingLoc;
608
609 const char *StrData = Buffer.data()+LocInfo.second;
610
611 // Create a langops struct and enable trigraphs. This is sufficient for
612 // relexing tokens.
613 LangOptions LangOpts;
614 LangOpts.Trigraphs = true;
615
616 // Create a lexer starting at the beginning of this token.
617 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
618 Buffer.end());
619 Token TheTok;
620 TheLexer.LexFromRawLexer(TheTok);
621
622 // Use the StringLiteralParser to compute the length of the string in bytes.
623 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
624 unsigned TokNumBytes = SLP.GetStringLength();
625
626 // If the byte is in this token, return the location of the byte.
627 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000628 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000629 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
630
631 // Now that we know the offset of the token in the spelling, use the
632 // preprocessor to get the offset in the original source.
633 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
634 }
635
636 // Move to the next string token.
637 ++TokNo;
638 ByteNo -= TokNumBytes;
639 }
640}
641
642
643
Reid Spencer5f016e22007-07-11 17:01:13 +0000644/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
645/// corresponds to, e.g. "sizeof" or "[pre]++".
646const char *UnaryOperator::getOpcodeStr(Opcode Op) {
647 switch (Op) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000648 default: llvm_unreachable("Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000649 case UO_PostInc: return "++";
650 case UO_PostDec: return "--";
651 case UO_PreInc: return "++";
652 case UO_PreDec: return "--";
653 case UO_AddrOf: return "&";
654 case UO_Deref: return "*";
655 case UO_Plus: return "+";
656 case UO_Minus: return "-";
657 case UO_Not: return "~";
658 case UO_LNot: return "!";
659 case UO_Real: return "__real";
660 case UO_Imag: return "__imag";
661 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 }
663}
664
John McCall2de56d12010-08-25 11:45:40 +0000665UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000666UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
667 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000668 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000669 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
670 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
671 case OO_Amp: return UO_AddrOf;
672 case OO_Star: return UO_Deref;
673 case OO_Plus: return UO_Plus;
674 case OO_Minus: return UO_Minus;
675 case OO_Tilde: return UO_Not;
676 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000677 }
678}
679
680OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
681 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000682 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
683 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
684 case UO_AddrOf: return OO_Amp;
685 case UO_Deref: return OO_Star;
686 case UO_Plus: return OO_Plus;
687 case UO_Minus: return OO_Minus;
688 case UO_Not: return OO_Tilde;
689 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000690 default: return OO_None;
691 }
692}
693
694
Reid Spencer5f016e22007-07-11 17:01:13 +0000695//===----------------------------------------------------------------------===//
696// Postfix Operators.
697//===----------------------------------------------------------------------===//
698
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000699CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
700 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000701 SourceLocation rparenloc)
702 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000703 fn->isTypeDependent(),
704 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000705 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000706 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000707 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000708
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000709 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000710 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000711 for (unsigned i = 0; i != numargs; ++i) {
712 if (args[i]->isTypeDependent())
713 ExprBits.TypeDependent = true;
714 if (args[i]->isValueDependent())
715 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000716 if (args[i]->isInstantiationDependent())
717 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000718 if (args[i]->containsUnexpandedParameterPack())
719 ExprBits.ContainsUnexpandedParameterPack = true;
720
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000721 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000722 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000723
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000724 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000725 RParenLoc = rparenloc;
726}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000727
Ted Kremenek668bf912009-02-09 20:51:47 +0000728CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000729 QualType t, ExprValueKind VK, SourceLocation rparenloc)
730 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000731 fn->isTypeDependent(),
732 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000733 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000734 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000735 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000736
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000737 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000738 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000739 for (unsigned i = 0; i != numargs; ++i) {
740 if (args[i]->isTypeDependent())
741 ExprBits.TypeDependent = true;
742 if (args[i]->isValueDependent())
743 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000744 if (args[i]->isInstantiationDependent())
745 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000746 if (args[i]->containsUnexpandedParameterPack())
747 ExprBits.ContainsUnexpandedParameterPack = true;
748
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000749 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000750 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000751
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000752 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 RParenLoc = rparenloc;
754}
755
Mike Stump1eb44332009-09-09 15:08:12 +0000756CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
757 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000758 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000759 SubExprs = new (C) Stmt*[PREARGS_START];
760 CallExprBits.NumPreArgs = 0;
761}
762
763CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
764 EmptyShell Empty)
765 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
766 // FIXME: Why do we allocate this?
767 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
768 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000769}
770
Nuno Lopesd20254f2009-12-20 23:11:08 +0000771Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000772 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000773
774 while (SubstNonTypeTemplateParmExpr *NTTP
775 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
776 CEE = NTTP->getReplacement()->IgnoreParenCasts();
777 }
778
Sebastian Redl20012152010-09-10 20:55:30 +0000779 // If we're calling a dereference, look at the pointer instead.
780 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
781 if (BO->isPtrMemOp())
782 CEE = BO->getRHS()->IgnoreParenCasts();
783 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
784 if (UO->getOpcode() == UO_Deref)
785 CEE = UO->getSubExpr()->IgnoreParenCasts();
786 }
Chris Lattner6346f962009-07-17 15:46:27 +0000787 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000788 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000789 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
790 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000791
792 return 0;
793}
794
Nuno Lopesd20254f2009-12-20 23:11:08 +0000795FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000796 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000797}
798
Chris Lattnerd18b3292007-12-28 05:25:02 +0000799/// setNumArgs - This changes the number of arguments present in this call.
800/// Any orphaned expressions are deleted by this, and any new operands are set
801/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000802void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000803 // No change, just return.
804 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Chris Lattnerd18b3292007-12-28 05:25:02 +0000806 // If shrinking # arguments, just delete the extras and forgot them.
807 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000808 this->NumArgs = NumArgs;
809 return;
810 }
811
812 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000813 unsigned NumPreArgs = getNumPreArgs();
814 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000815 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000816 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000817 NewSubExprs[i] = SubExprs[i];
818 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000819 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
820 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000821 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Douglas Gregor88c9a462009-04-17 21:46:47 +0000823 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000824 SubExprs = NewSubExprs;
825 this->NumArgs = NumArgs;
826}
827
Chris Lattnercb888962008-10-06 05:00:53 +0000828/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
829/// not, return 0.
Jay Foad4ba2a172011-01-12 09:06:06 +0000830unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000831 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000832 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000833 // ImplicitCastExpr.
834 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
835 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000836 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000837
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000838 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
839 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000840 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Anders Carlssonbcba2012008-01-31 02:13:57 +0000842 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
843 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000844 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000845
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000846 if (!FDecl->getIdentifier())
847 return 0;
848
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000849 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000850}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000851
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000852QualType CallExpr::getCallReturnType() const {
853 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000854 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000855 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000856 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000857 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000858 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
859 // This should never be overloaded and so should never return null.
860 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000861
John McCall864c0412011-04-26 20:42:42 +0000862 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000863 return FnType->getResultType();
864}
Chris Lattnercb888962008-10-06 05:00:53 +0000865
John McCall2882eca2011-02-21 06:23:05 +0000866SourceRange CallExpr::getSourceRange() const {
867 if (isa<CXXOperatorCallExpr>(this))
868 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
869
870 SourceLocation begin = getCallee()->getLocStart();
871 if (begin.isInvalid() && getNumArgs() > 0)
872 begin = getArg(0)->getLocStart();
873 SourceLocation end = getRParenLoc();
874 if (end.isInvalid() && getNumArgs() > 0)
875 end = getArg(getNumArgs() - 1)->getLocEnd();
876 return SourceRange(begin, end);
877}
878
Sean Huntc3021132010-05-05 15:23:54 +0000879OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000880 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000881 TypeSourceInfo *tsi,
882 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000883 Expr** exprsPtr, unsigned numExprs,
884 SourceLocation RParenLoc) {
885 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000886 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000887 sizeof(Expr*) * numExprs);
888
889 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
890 exprsPtr, numExprs, RParenLoc);
891}
892
893OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
894 unsigned numComps, unsigned numExprs) {
895 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
896 sizeof(OffsetOfNode) * numComps +
897 sizeof(Expr*) * numExprs);
898 return new (Mem) OffsetOfExpr(numComps, numExprs);
899}
900
Sean Huntc3021132010-05-05 15:23:54 +0000901OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000902 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000903 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000904 Expr** exprsPtr, unsigned numExprs,
905 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000906 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
907 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000908 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000909 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000910 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000911 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
912 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000913{
914 for(unsigned i = 0; i < numComps; ++i) {
915 setComponent(i, compsPtr[i]);
916 }
Sean Huntc3021132010-05-05 15:23:54 +0000917
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000918 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000919 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
920 ExprBits.ValueDependent = true;
921 if (exprsPtr[i]->containsUnexpandedParameterPack())
922 ExprBits.ContainsUnexpandedParameterPack = true;
923
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000924 setIndexExpr(i, exprsPtr[i]);
925 }
926}
927
928IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
929 assert(getKind() == Field || getKind() == Identifier);
930 if (getKind() == Field)
931 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000932
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000933 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
934}
935
Mike Stump1eb44332009-09-09 15:08:12 +0000936MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000937 NestedNameSpecifierLoc QualifierLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000938 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000939 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000940 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000941 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000942 QualType ty,
943 ExprValueKind vk,
944 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000945 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000946
Douglas Gregor40d96a62011-02-28 21:54:11 +0000947 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000948 founddecl.getDecl() != memberdecl ||
949 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000950 if (hasQualOrFound)
951 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000952
John McCalld5532b62009-11-23 01:53:49 +0000953 if (targs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000954 Size += ASTTemplateArgumentListInfo::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Chris Lattner32488542010-10-30 05:14:06 +0000956 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000957 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
958 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000959
960 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000961 // FIXME: Wrong. We should be looking at the member declaration we found.
962 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000963 E->setValueDependent(true);
964 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +0000965 E->setInstantiationDependent(true);
966 }
967 else if (QualifierLoc &&
968 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
969 E->setInstantiationDependent(true);
970
John McCall6bb80172010-03-30 21:47:33 +0000971 E->HasQualifierOrFoundDecl = true;
972
973 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000974 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000975 NQ->FoundDecl = founddecl;
976 }
977
978 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000979 bool Dependent = false;
980 bool InstantiationDependent = false;
981 bool ContainsUnexpandedParameterPack = false;
John McCall6bb80172010-03-30 21:47:33 +0000982 E->HasExplicitTemplateArgumentList = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000983 E->getExplicitTemplateArgs().initializeFrom(*targs, Dependent,
984 InstantiationDependent,
985 ContainsUnexpandedParameterPack);
986 if (InstantiationDependent)
987 E->setInstantiationDependent(true);
John McCall6bb80172010-03-30 21:47:33 +0000988 }
989
990 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000991}
992
Douglas Gregor75e85042011-03-02 21:06:53 +0000993SourceRange MemberExpr::getSourceRange() const {
994 SourceLocation StartLoc;
995 if (isImplicitAccess()) {
996 if (hasQualifier())
997 StartLoc = getQualifierLoc().getBeginLoc();
998 else
999 StartLoc = MemberLoc;
1000 } else {
1001 // FIXME: We don't want this to happen. Rather, we should be able to
1002 // detect all kinds of implicit accesses more cleanly.
1003 StartLoc = getBase()->getLocStart();
1004 if (StartLoc.isInvalid())
1005 StartLoc = MemberLoc;
1006 }
1007
1008 SourceLocation EndLoc =
1009 HasExplicitTemplateArgumentList? getRAngleLoc()
1010 : getMemberNameInfo().getEndLoc();
1011
1012 return SourceRange(StartLoc, EndLoc);
1013}
1014
John McCall1d9b3b22011-09-09 05:25:32 +00001015void CastExpr::CheckCastConsistency() const {
1016 switch (getCastKind()) {
1017 case CK_DerivedToBase:
1018 case CK_UncheckedDerivedToBase:
1019 case CK_DerivedToBaseMemberPointer:
1020 case CK_BaseToDerived:
1021 case CK_BaseToDerivedMemberPointer:
1022 assert(!path_empty() && "Cast kind should have a base path!");
1023 break;
1024
1025 case CK_CPointerToObjCPointerCast:
1026 assert(getType()->isObjCObjectPointerType());
1027 assert(getSubExpr()->getType()->isPointerType());
1028 goto CheckNoBasePath;
1029
1030 case CK_BlockPointerToObjCPointerCast:
1031 assert(getType()->isObjCObjectPointerType());
1032 assert(getSubExpr()->getType()->isBlockPointerType());
1033 goto CheckNoBasePath;
1034
1035 case CK_BitCast:
1036 // Arbitrary casts to C pointer types count as bitcasts.
1037 // Otherwise, we should only have block and ObjC pointer casts
1038 // here if they stay within the type kind.
1039 if (!getType()->isPointerType()) {
1040 assert(getType()->isObjCObjectPointerType() ==
1041 getSubExpr()->getType()->isObjCObjectPointerType());
1042 assert(getType()->isBlockPointerType() ==
1043 getSubExpr()->getType()->isBlockPointerType());
1044 }
1045 goto CheckNoBasePath;
1046
1047 case CK_AnyPointerToBlockPointerCast:
1048 assert(getType()->isBlockPointerType());
1049 assert(getSubExpr()->getType()->isAnyPointerType() &&
1050 !getSubExpr()->getType()->isBlockPointerType());
1051 goto CheckNoBasePath;
1052
1053 // These should not have an inheritance path.
1054 case CK_Dynamic:
1055 case CK_ToUnion:
1056 case CK_ArrayToPointerDecay:
1057 case CK_FunctionToPointerDecay:
1058 case CK_NullToMemberPointer:
1059 case CK_NullToPointer:
1060 case CK_ConstructorConversion:
1061 case CK_IntegralToPointer:
1062 case CK_PointerToIntegral:
1063 case CK_ToVoid:
1064 case CK_VectorSplat:
1065 case CK_IntegralCast:
1066 case CK_IntegralToFloating:
1067 case CK_FloatingToIntegral:
1068 case CK_FloatingCast:
1069 case CK_ObjCObjectLValueCast:
1070 case CK_FloatingRealToComplex:
1071 case CK_FloatingComplexToReal:
1072 case CK_FloatingComplexCast:
1073 case CK_FloatingComplexToIntegralComplex:
1074 case CK_IntegralRealToComplex:
1075 case CK_IntegralComplexToReal:
1076 case CK_IntegralComplexCast:
1077 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001078 case CK_ARCProduceObject:
1079 case CK_ARCConsumeObject:
1080 case CK_ARCReclaimReturnedObject:
1081 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001082 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1083 goto CheckNoBasePath;
1084
1085 case CK_Dependent:
1086 case CK_LValueToRValue:
1087 case CK_GetObjCProperty:
1088 case CK_NoOp:
1089 case CK_PointerToBoolean:
1090 case CK_IntegralToBoolean:
1091 case CK_FloatingToBoolean:
1092 case CK_MemberPointerToBoolean:
1093 case CK_FloatingComplexToBoolean:
1094 case CK_IntegralComplexToBoolean:
1095 case CK_LValueBitCast: // -> bool&
1096 case CK_UserDefinedConversion: // operator bool()
1097 CheckNoBasePath:
1098 assert(path_empty() && "Cast kind should not have a base path!");
1099 break;
1100 }
1101}
1102
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001103const char *CastExpr::getCastKindName() const {
1104 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001105 case CK_Dependent:
1106 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001107 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001108 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001109 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001110 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001111 case CK_LValueToRValue:
1112 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +00001113 case CK_GetObjCProperty:
1114 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +00001115 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001116 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001117 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001118 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001119 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001120 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001121 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001122 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001123 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001124 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001125 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001126 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001127 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001128 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001129 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001130 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001131 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001132 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001133 case CK_NullToPointer:
1134 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001135 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001136 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001137 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001138 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001139 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001140 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001141 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001142 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001143 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001144 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001145 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001146 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001147 case CK_PointerToBoolean:
1148 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001149 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001150 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001151 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001152 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001153 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001154 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001155 case CK_IntegralToBoolean:
1156 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001157 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001158 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001159 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001160 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001161 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001162 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001163 case CK_FloatingToBoolean:
1164 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001165 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001166 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001167 case CK_CPointerToObjCPointerCast:
1168 return "CPointerToObjCPointerCast";
1169 case CK_BlockPointerToObjCPointerCast:
1170 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001171 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001172 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001173 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001174 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001175 case CK_FloatingRealToComplex:
1176 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001177 case CK_FloatingComplexToReal:
1178 return "FloatingComplexToReal";
1179 case CK_FloatingComplexToBoolean:
1180 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001181 case CK_FloatingComplexCast:
1182 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001183 case CK_FloatingComplexToIntegralComplex:
1184 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001185 case CK_IntegralRealToComplex:
1186 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001187 case CK_IntegralComplexToReal:
1188 return "IntegralComplexToReal";
1189 case CK_IntegralComplexToBoolean:
1190 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001191 case CK_IntegralComplexCast:
1192 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001193 case CK_IntegralComplexToFloatingComplex:
1194 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001195 case CK_ARCConsumeObject:
1196 return "ARCConsumeObject";
1197 case CK_ARCProduceObject:
1198 return "ARCProduceObject";
1199 case CK_ARCReclaimReturnedObject:
1200 return "ARCReclaimReturnedObject";
1201 case CK_ARCExtendBlockObject:
1202 return "ARCCExtendBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001203 }
Mike Stump1eb44332009-09-09 15:08:12 +00001204
John McCall2bb5d002010-11-13 09:02:35 +00001205 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001206 return 0;
1207}
1208
Douglas Gregor6eef5192009-12-14 19:27:10 +00001209Expr *CastExpr::getSubExprAsWritten() {
1210 Expr *SubExpr = 0;
1211 CastExpr *E = this;
1212 do {
1213 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001214
1215 // Skip through reference binding to temporary.
1216 if (MaterializeTemporaryExpr *Materialize
1217 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1218 SubExpr = Materialize->GetTemporaryExpr();
1219
Douglas Gregor6eef5192009-12-14 19:27:10 +00001220 // Skip any temporary bindings; they're implicit.
1221 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1222 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001223
Douglas Gregor6eef5192009-12-14 19:27:10 +00001224 // Conversions by constructor and conversion functions have a
1225 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001226 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001227 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001228 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001229 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001230
Douglas Gregor6eef5192009-12-14 19:27:10 +00001231 // If the subexpression we're left with is an implicit cast, look
1232 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001233 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1234
Douglas Gregor6eef5192009-12-14 19:27:10 +00001235 return SubExpr;
1236}
1237
John McCallf871d0c2010-08-07 06:22:56 +00001238CXXBaseSpecifier **CastExpr::path_buffer() {
1239 switch (getStmtClass()) {
1240#define ABSTRACT_STMT(x)
1241#define CASTEXPR(Type, Base) \
1242 case Stmt::Type##Class: \
1243 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1244#define STMT(Type, Base)
1245#include "clang/AST/StmtNodes.inc"
1246 default:
1247 llvm_unreachable("non-cast expressions not possible here");
1248 return 0;
1249 }
1250}
1251
1252void CastExpr::setCastPath(const CXXCastPath &Path) {
1253 assert(Path.size() == path_size());
1254 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1255}
1256
1257ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1258 CastKind Kind, Expr *Operand,
1259 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001260 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001261 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1262 void *Buffer =
1263 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1264 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001265 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001266 if (PathSize) E->setCastPath(*BasePath);
1267 return E;
1268}
1269
1270ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1271 unsigned PathSize) {
1272 void *Buffer =
1273 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1274 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1275}
1276
1277
1278CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001279 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001280 const CXXCastPath *BasePath,
1281 TypeSourceInfo *WrittenTy,
1282 SourceLocation L, SourceLocation R) {
1283 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1284 void *Buffer =
1285 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1286 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001287 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001288 if (PathSize) E->setCastPath(*BasePath);
1289 return E;
1290}
1291
1292CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1293 void *Buffer =
1294 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1295 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1296}
1297
Reid Spencer5f016e22007-07-11 17:01:13 +00001298/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1299/// corresponds to, e.g. "<<=".
1300const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1301 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001302 case BO_PtrMemD: return ".*";
1303 case BO_PtrMemI: return "->*";
1304 case BO_Mul: return "*";
1305 case BO_Div: return "/";
1306 case BO_Rem: return "%";
1307 case BO_Add: return "+";
1308 case BO_Sub: return "-";
1309 case BO_Shl: return "<<";
1310 case BO_Shr: return ">>";
1311 case BO_LT: return "<";
1312 case BO_GT: return ">";
1313 case BO_LE: return "<=";
1314 case BO_GE: return ">=";
1315 case BO_EQ: return "==";
1316 case BO_NE: return "!=";
1317 case BO_And: return "&";
1318 case BO_Xor: return "^";
1319 case BO_Or: return "|";
1320 case BO_LAnd: return "&&";
1321 case BO_LOr: return "||";
1322 case BO_Assign: return "=";
1323 case BO_MulAssign: return "*=";
1324 case BO_DivAssign: return "/=";
1325 case BO_RemAssign: return "%=";
1326 case BO_AddAssign: return "+=";
1327 case BO_SubAssign: return "-=";
1328 case BO_ShlAssign: return "<<=";
1329 case BO_ShrAssign: return ">>=";
1330 case BO_AndAssign: return "&=";
1331 case BO_XorAssign: return "^=";
1332 case BO_OrAssign: return "|=";
1333 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001334 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001335
1336 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001337}
1338
John McCall2de56d12010-08-25 11:45:40 +00001339BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001340BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1341 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001342 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001343 case OO_Plus: return BO_Add;
1344 case OO_Minus: return BO_Sub;
1345 case OO_Star: return BO_Mul;
1346 case OO_Slash: return BO_Div;
1347 case OO_Percent: return BO_Rem;
1348 case OO_Caret: return BO_Xor;
1349 case OO_Amp: return BO_And;
1350 case OO_Pipe: return BO_Or;
1351 case OO_Equal: return BO_Assign;
1352 case OO_Less: return BO_LT;
1353 case OO_Greater: return BO_GT;
1354 case OO_PlusEqual: return BO_AddAssign;
1355 case OO_MinusEqual: return BO_SubAssign;
1356 case OO_StarEqual: return BO_MulAssign;
1357 case OO_SlashEqual: return BO_DivAssign;
1358 case OO_PercentEqual: return BO_RemAssign;
1359 case OO_CaretEqual: return BO_XorAssign;
1360 case OO_AmpEqual: return BO_AndAssign;
1361 case OO_PipeEqual: return BO_OrAssign;
1362 case OO_LessLess: return BO_Shl;
1363 case OO_GreaterGreater: return BO_Shr;
1364 case OO_LessLessEqual: return BO_ShlAssign;
1365 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1366 case OO_EqualEqual: return BO_EQ;
1367 case OO_ExclaimEqual: return BO_NE;
1368 case OO_LessEqual: return BO_LE;
1369 case OO_GreaterEqual: return BO_GE;
1370 case OO_AmpAmp: return BO_LAnd;
1371 case OO_PipePipe: return BO_LOr;
1372 case OO_Comma: return BO_Comma;
1373 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001374 }
1375}
1376
1377OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1378 static const OverloadedOperatorKind OverOps[] = {
1379 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1380 OO_Star, OO_Slash, OO_Percent,
1381 OO_Plus, OO_Minus,
1382 OO_LessLess, OO_GreaterGreater,
1383 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1384 OO_EqualEqual, OO_ExclaimEqual,
1385 OO_Amp,
1386 OO_Caret,
1387 OO_Pipe,
1388 OO_AmpAmp,
1389 OO_PipePipe,
1390 OO_Equal, OO_StarEqual,
1391 OO_SlashEqual, OO_PercentEqual,
1392 OO_PlusEqual, OO_MinusEqual,
1393 OO_LessLessEqual, OO_GreaterGreaterEqual,
1394 OO_AmpEqual, OO_CaretEqual,
1395 OO_PipeEqual,
1396 OO_Comma
1397 };
1398 return OverOps[Opc];
1399}
1400
Ted Kremenek709210f2010-04-13 23:39:13 +00001401InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001402 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001403 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001404 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001405 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001406 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001407 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +00001408 HadArrayRangeDesignator(false)
Sean Huntc3021132010-05-05 15:23:54 +00001409{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001410 for (unsigned I = 0; I != numInits; ++I) {
1411 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001412 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001413 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001414 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001415 if (initExprs[I]->isInstantiationDependent())
1416 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001417 if (initExprs[I]->containsUnexpandedParameterPack())
1418 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001419 }
Sean Huntc3021132010-05-05 15:23:54 +00001420
Ted Kremenek709210f2010-04-13 23:39:13 +00001421 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001422}
Reid Spencer5f016e22007-07-11 17:01:13 +00001423
Ted Kremenek709210f2010-04-13 23:39:13 +00001424void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001425 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001426 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001427}
1428
Ted Kremenek709210f2010-04-13 23:39:13 +00001429void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001430 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001431}
1432
Ted Kremenek709210f2010-04-13 23:39:13 +00001433Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001434 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001435 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001436 InitExprs.back() = expr;
1437 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001438 }
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Douglas Gregor4c678342009-01-28 21:54:33 +00001440 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1441 InitExprs[Init] = expr;
1442 return Result;
1443}
1444
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001445void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001446 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001447 ArrayFillerOrUnionFieldInit = filler;
1448 // Fill out any "holes" in the array due to designated initializers.
1449 Expr **inits = getInits();
1450 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1451 if (inits[i] == 0)
1452 inits[i] = filler;
1453}
1454
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001455SourceRange InitListExpr::getSourceRange() const {
1456 if (SyntacticForm)
1457 return SyntacticForm->getSourceRange();
1458 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1459 if (Beg.isInvalid()) {
1460 // Find the first non-null initializer.
1461 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1462 E = InitExprs.end();
1463 I != E; ++I) {
1464 if (Stmt *S = *I) {
1465 Beg = S->getLocStart();
1466 break;
1467 }
1468 }
1469 }
1470 if (End.isInvalid()) {
1471 // Find the first non-null initializer from the end.
1472 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1473 E = InitExprs.rend();
1474 I != E; ++I) {
1475 if (Stmt *S = *I) {
1476 End = S->getSourceRange().getEnd();
1477 break;
1478 }
1479 }
1480 }
1481 return SourceRange(Beg, End);
1482}
1483
Steve Naroffbfdcae62008-09-04 15:31:07 +00001484/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001485///
1486const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001487 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001488 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001489}
1490
Mike Stump1eb44332009-09-09 15:08:12 +00001491SourceLocation BlockExpr::getCaretLocation() const {
1492 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001493}
Mike Stump1eb44332009-09-09 15:08:12 +00001494const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001495 return TheBlock->getBody();
1496}
Mike Stump1eb44332009-09-09 15:08:12 +00001497Stmt *BlockExpr::getBody() {
1498 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001499}
Steve Naroff56ee6892008-10-08 17:01:13 +00001500
1501
Reid Spencer5f016e22007-07-11 17:01:13 +00001502//===----------------------------------------------------------------------===//
1503// Generic Expression Routines
1504//===----------------------------------------------------------------------===//
1505
Chris Lattner026dc962009-02-14 07:37:35 +00001506/// isUnusedResultAWarning - Return true if this immediate expression should
1507/// be warned about if the result is unused. If so, fill in Loc and Ranges
1508/// with location to warn on and the source range[s] to report with the
1509/// warning.
1510bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001511 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001512 // Don't warn if the expr is type dependent. The type could end up
1513 // instantiating to void.
1514 if (isTypeDependent())
1515 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001516
Reid Spencer5f016e22007-07-11 17:01:13 +00001517 switch (getStmtClass()) {
1518 default:
John McCall0faede62010-03-12 07:11:26 +00001519 if (getType()->isVoidType())
1520 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001521 Loc = getExprLoc();
1522 R1 = getSourceRange();
1523 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001525 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001526 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001527 case GenericSelectionExprClass:
1528 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1529 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001530 case UnaryOperatorClass: {
1531 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Reid Spencer5f016e22007-07-11 17:01:13 +00001533 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001534 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001535 case UO_PostInc:
1536 case UO_PostDec:
1537 case UO_PreInc:
1538 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001539 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001540 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001541 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001542 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001543 return false;
1544 break;
John McCall2de56d12010-08-25 11:45:40 +00001545 case UO_Real:
1546 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001547 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001548 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1549 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001550 return false;
1551 break;
John McCall2de56d12010-08-25 11:45:40 +00001552 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001553 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001554 }
Chris Lattner026dc962009-02-14 07:37:35 +00001555 Loc = UO->getOperatorLoc();
1556 R1 = UO->getSubExpr()->getSourceRange();
1557 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001559 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001560 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001561 switch (BO->getOpcode()) {
1562 default:
1563 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001564 // Consider the RHS of comma for side effects. LHS was checked by
1565 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001566 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001567 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1568 // lvalue-ness) of an assignment written in a macro.
1569 if (IntegerLiteral *IE =
1570 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1571 if (IE->getValue() == 0)
1572 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001573 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1574 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001575 case BO_LAnd:
1576 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001577 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1578 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1579 return false;
1580 break;
John McCallbf0ee352010-02-16 04:10:53 +00001581 }
Chris Lattner026dc962009-02-14 07:37:35 +00001582 if (BO->isAssignmentOp())
1583 return false;
1584 Loc = BO->getOperatorLoc();
1585 R1 = BO->getLHS()->getSourceRange();
1586 R2 = BO->getRHS()->getSourceRange();
1587 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001588 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001589 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001590 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001591 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001592 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001593
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001594 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001595 // If only one of the LHS or RHS is a warning, the operator might
1596 // be being used for control flow. Only warn if both the LHS and
1597 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001598 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001599 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1600 return false;
1601 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001602 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001603 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001604 }
1605
Reid Spencer5f016e22007-07-11 17:01:13 +00001606 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001607 // If the base pointer or element is to a volatile pointer/field, accessing
1608 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001609 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001610 return false;
1611 Loc = cast<MemberExpr>(this)->getMemberLoc();
1612 R1 = SourceRange(Loc, Loc);
1613 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1614 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001615
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 case ArraySubscriptExprClass:
1617 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001618 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001619 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001620 return false;
1621 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1622 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1623 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1624 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001625
Chandler Carruth9b106832011-08-17 09:49:44 +00001626 case CXXOperatorCallExprClass: {
1627 // We warn about operator== and operator!= even when user-defined operator
1628 // overloads as there is no reasonable way to define these such that they
1629 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1630 // warning: these operators are commonly typo'ed, and so warning on them
1631 // provides additional value as well. If this list is updated,
1632 // DiagnoseUnusedComparison should be as well.
1633 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1634 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001635 Op->getOperator() == OO_ExclaimEqual) {
1636 Loc = Op->getOperatorLoc();
1637 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001638 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001639 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001640
1641 // Fallthrough for generic call handling.
1642 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001643 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001644 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001645 // If this is a direct call, get the callee.
1646 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001647 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001648 // If the callee has attribute pure, const, or warn_unused_result, warn
1649 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001650 //
1651 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1652 // updated to match for QoI.
1653 if (FD->getAttr<WarnUnusedResultAttr>() ||
1654 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1655 Loc = CE->getCallee()->getLocStart();
1656 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001657
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001658 if (unsigned NumArgs = CE->getNumArgs())
1659 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1660 CE->getArg(NumArgs-1)->getLocEnd());
1661 return true;
1662 }
Chris Lattner026dc962009-02-14 07:37:35 +00001663 }
1664 return false;
1665 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001666
1667 case CXXTemporaryObjectExprClass:
1668 case CXXConstructExprClass:
1669 return false;
1670
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001671 case ObjCMessageExprClass: {
1672 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001673 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1674 ME->isInstanceMessage() &&
1675 !ME->getType()->isVoidType() &&
1676 ME->getSelector().getIdentifierInfoForSlot(0) &&
1677 ME->getSelector().getIdentifierInfoForSlot(0)
1678 ->getName().startswith("init")) {
1679 Loc = getExprLoc();
1680 R1 = ME->getSourceRange();
1681 return true;
1682 }
1683
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001684 const ObjCMethodDecl *MD = ME->getMethodDecl();
1685 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1686 Loc = getExprLoc();
1687 return true;
1688 }
Chris Lattner026dc962009-02-14 07:37:35 +00001689 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001690 }
Mike Stump1eb44332009-09-09 15:08:12 +00001691
John McCall12f78a62010-12-02 01:19:52 +00001692 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001693 Loc = getExprLoc();
1694 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001695 return true;
John McCall12f78a62010-12-02 01:19:52 +00001696
John McCall4b9c2d22011-11-06 09:01:30 +00001697 case PseudoObjectExprClass: {
1698 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1699
1700 // Only complain about things that have the form of a getter.
1701 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1702 isa<BinaryOperator>(PO->getSyntacticForm()))
1703 return false;
1704
1705 Loc = getExprLoc();
1706 R1 = getSourceRange();
1707 return true;
1708 }
1709
Chris Lattner611b2ec2008-07-26 19:51:01 +00001710 case StmtExprClass: {
1711 // Statement exprs don't logically have side effects themselves, but are
1712 // sometimes used in macros in ways that give them a type that is unused.
1713 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1714 // however, if the result of the stmt expr is dead, we don't want to emit a
1715 // warning.
1716 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001717 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001718 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001719 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001720 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1721 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1722 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1723 }
Mike Stump1eb44332009-09-09 15:08:12 +00001724
John McCall0faede62010-03-12 07:11:26 +00001725 if (getType()->isVoidType())
1726 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001727 Loc = cast<StmtExpr>(this)->getLParenLoc();
1728 R1 = getSourceRange();
1729 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001730 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001731 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001732 // If this is an explicit cast to void, allow it. People do this when they
1733 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001734 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001735 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001736 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1737 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1738 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001739 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001740 if (getType()->isVoidType())
1741 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001742 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001743
Anders Carlsson58beed92009-11-17 17:11:23 +00001744 // If this is a cast to void or a constructor conversion, check the operand.
1745 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001746 if (CE->getCastKind() == CK_ToVoid ||
1747 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001748 return (cast<CastExpr>(this)->getSubExpr()
1749 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001750 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1751 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1752 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001753 }
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Eli Friedman4be1f472008-05-19 21:24:43 +00001755 case ImplicitCastExprClass:
1756 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001757 return (cast<ImplicitCastExpr>(this)
1758 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001759
Chris Lattner04421082008-04-08 04:40:51 +00001760 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001761 return (cast<CXXDefaultArgExpr>(this)
1762 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001763
1764 case CXXNewExprClass:
1765 // FIXME: In theory, there might be new expressions that don't have side
1766 // effects (e.g. a placement new with an uninitialized POD).
1767 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001768 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001769 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001770 return (cast<CXXBindTemporaryExpr>(this)
1771 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001772 case ExprWithCleanupsClass:
1773 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001774 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001775 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001776}
1777
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001778/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001779/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001780bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001781 const Expr *E = IgnoreParens();
1782 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001783 default:
1784 return false;
1785 case ObjCIvarRefExprClass:
1786 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001787 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001788 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001789 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001790 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001791 case MaterializeTemporaryExprClass:
1792 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1793 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001794 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001795 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001796 case BlockDeclRefExprClass:
Douglas Gregora2813ce2009-10-23 18:54:35 +00001797 case DeclRefExprClass: {
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001798
1799 const Decl *D;
1800 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1801 D = BDRE->getDecl();
1802 else
1803 D = cast<DeclRefExpr>(E)->getDecl();
1804
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001805 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1806 if (VD->hasGlobalStorage())
1807 return true;
1808 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001809 // dereferencing to a pointer is always a gc'able candidate,
1810 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001811 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001812 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001813 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001814 return false;
1815 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001816 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001817 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001818 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001819 }
1820 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001821 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001822 }
1823}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001824
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001825bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1826 if (isTypeDependent())
1827 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001828 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001829}
1830
John McCall864c0412011-04-26 20:42:42 +00001831QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00001832 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00001833
1834 // Bound member expressions are always one of these possibilities:
1835 // x->m x.m x->*y x.*y
1836 // (possibly parenthesized)
1837
1838 expr = expr->IgnoreParens();
1839 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1840 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1841 return mem->getMemberDecl()->getType();
1842 }
1843
1844 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1845 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1846 ->getPointeeType();
1847 assert(type->isFunctionType());
1848 return type;
1849 }
1850
1851 assert(isa<UnresolvedMemberExpr>(expr));
1852 return QualType();
1853}
1854
Sebastian Redl369e51f2010-09-10 20:55:33 +00001855static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1856 Expr::CanThrowResult CT2) {
1857 // CanThrowResult constants are ordered so that the maximum is the correct
1858 // merge result.
1859 return CT1 > CT2 ? CT1 : CT2;
1860}
1861
1862static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1863 Expr *E = const_cast<Expr*>(CE);
1864 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001865 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001866 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1867 }
1868 return R;
1869}
1870
Richard Smith7a614d82011-06-11 17:19:42 +00001871static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1872 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001873 bool NullThrows = true) {
1874 if (!D)
1875 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1876
1877 // See if we can get a function type from the decl somehow.
1878 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1879 if (!VD) // If we have no clue what we're calling, assume the worst.
1880 return Expr::CT_Can;
1881
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001882 // As an extension, we assume that __attribute__((nothrow)) functions don't
1883 // throw.
1884 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1885 return Expr::CT_Cannot;
1886
Sebastian Redl369e51f2010-09-10 20:55:33 +00001887 QualType T = VD->getType();
1888 const FunctionProtoType *FT;
1889 if ((FT = T->getAs<FunctionProtoType>())) {
1890 } else if (const PointerType *PT = T->getAs<PointerType>())
1891 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1892 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1893 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1894 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1895 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1896 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1897 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1898
1899 if (!FT)
1900 return Expr::CT_Can;
1901
Richard Smith7a614d82011-06-11 17:19:42 +00001902 if (FT->getExceptionSpecType() == EST_Delayed) {
1903 assert(isa<CXXConstructorDecl>(D) &&
1904 "only constructor exception specs can be unknown");
1905 Ctx.getDiagnostics().Report(E->getLocStart(),
1906 diag::err_exception_spec_unknown)
1907 << E->getSourceRange();
1908 return Expr::CT_Can;
1909 }
1910
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001911 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001912}
1913
1914static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1915 if (DC->isTypeDependent())
1916 return Expr::CT_Dependent;
1917
Sebastian Redl295995c2010-09-10 20:55:47 +00001918 if (!DC->getTypeAsWritten()->isReferenceType())
1919 return Expr::CT_Cannot;
1920
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001921 if (DC->getSubExpr()->isTypeDependent())
1922 return Expr::CT_Dependent;
1923
Sebastian Redl369e51f2010-09-10 20:55:33 +00001924 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1925}
1926
1927static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1928 const CXXTypeidExpr *DC) {
1929 if (DC->isTypeOperand())
1930 return Expr::CT_Cannot;
1931
1932 Expr *Op = DC->getExprOperand();
1933 if (Op->isTypeDependent())
1934 return Expr::CT_Dependent;
1935
1936 const RecordType *RT = Op->getType()->getAs<RecordType>();
1937 if (!RT)
1938 return Expr::CT_Cannot;
1939
1940 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1941 return Expr::CT_Cannot;
1942
1943 if (Op->Classify(C).isPRValue())
1944 return Expr::CT_Cannot;
1945
1946 return Expr::CT_Can;
1947}
1948
1949Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1950 // C++ [expr.unary.noexcept]p3:
1951 // [Can throw] if in a potentially-evaluated context the expression would
1952 // contain:
1953 switch (getStmtClass()) {
1954 case CXXThrowExprClass:
1955 // - a potentially evaluated throw-expression
1956 return CT_Can;
1957
1958 case CXXDynamicCastExprClass: {
1959 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1960 // where T is a reference type, that requires a run-time check
1961 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1962 if (CT == CT_Can)
1963 return CT;
1964 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1965 }
1966
1967 case CXXTypeidExprClass:
1968 // - a potentially evaluated typeid expression applied to a glvalue
1969 // expression whose type is a polymorphic class type
1970 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1971
1972 // - a potentially evaluated call to a function, member function, function
1973 // pointer, or member function pointer that does not have a non-throwing
1974 // exception-specification
1975 case CallExprClass:
1976 case CXXOperatorCallExprClass:
1977 case CXXMemberCallExprClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001978 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001979 CanThrowResult CT;
1980 if (isTypeDependent())
1981 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001982 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1983 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001984 else
Richard Smith7a614d82011-06-11 17:19:42 +00001985 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001986 if (CT == CT_Can)
1987 return CT;
1988 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1989 }
1990
Sebastian Redl295995c2010-09-10 20:55:47 +00001991 case CXXConstructExprClass:
1992 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00001993 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001994 cast<CXXConstructExpr>(this)->getConstructor());
1995 if (CT == CT_Can)
1996 return CT;
1997 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1998 }
1999
2000 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002001 CanThrowResult CT;
2002 if (isTypeDependent())
2003 CT = CT_Dependent;
2004 else
2005 CT = MergeCanThrow(
Richard Smith7a614d82011-06-11 17:19:42 +00002006 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
2007 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00002008 /*NullThrows*/false));
2009 if (CT == CT_Can)
2010 return CT;
2011 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2012 }
2013
2014 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002015 CanThrowResult CT;
2016 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
2017 if (DTy.isNull() || DTy->isDependentType()) {
2018 CT = CT_Dependent;
2019 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00002020 CT = CanCalleeThrow(C, this,
2021 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002022 if (const RecordType *RT = DTy->getAs<RecordType>()) {
2023 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00002024 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002025 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002026 if (CT == CT_Can)
2027 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002028 }
2029 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2030 }
2031
2032 case CXXBindTemporaryExprClass: {
2033 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00002034 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002035 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
2036 if (CT == CT_Can)
2037 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00002038 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2039 }
2040
2041 // ObjC message sends are like function calls, but never have exception
2042 // specs.
2043 case ObjCMessageExprClass:
2044 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002045 return CT_Can;
2046
2047 // Many other things have subexpressions, so we have to test those.
2048 // Some are simple:
2049 case ParenExprClass:
2050 case MemberExprClass:
2051 case CXXReinterpretCastExprClass:
2052 case CXXConstCastExprClass:
2053 case ConditionalOperatorClass:
2054 case CompoundLiteralExprClass:
2055 case ExtVectorElementExprClass:
2056 case InitListExprClass:
2057 case DesignatedInitExprClass:
2058 case ParenListExprClass:
2059 case VAArgExprClass:
2060 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00002061 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002062 case ObjCIvarRefExprClass:
2063 case ObjCIsaExprClass:
2064 case ShuffleVectorExprClass:
2065 return CanSubExprsThrow(C, this);
2066
2067 // Some might be dependent for other reasons.
2068 case UnaryOperatorClass:
2069 case ArraySubscriptExprClass:
2070 case ImplicitCastExprClass:
2071 case CStyleCastExprClass:
2072 case CXXStaticCastExprClass:
2073 case CXXFunctionalCastExprClass:
2074 case BinaryOperatorClass:
Douglas Gregor03e80032011-06-21 17:03:29 +00002075 case CompoundAssignOperatorClass:
2076 case MaterializeTemporaryExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002077 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2078 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2079 }
2080
2081 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2082 case StmtExprClass:
2083 return CT_Can;
2084
2085 case ChooseExprClass:
2086 if (isTypeDependent() || isValueDependent())
2087 return CT_Dependent;
2088 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2089
Peter Collingbournef111d932011-04-15 00:35:48 +00002090 case GenericSelectionExprClass:
2091 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2092 return CT_Dependent;
2093 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2094
Sebastian Redl369e51f2010-09-10 20:55:33 +00002095 // Some expressions are always dependent.
2096 case DependentScopeDeclRefExprClass:
2097 case CXXUnresolvedConstructExprClass:
2098 case CXXDependentScopeMemberExprClass:
2099 return CT_Dependent;
2100
2101 default:
2102 // All other expressions don't have subexpressions, or else they are
2103 // unevaluated.
2104 return CT_Cannot;
2105 }
2106}
2107
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002108Expr* Expr::IgnoreParens() {
2109 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002110 while (true) {
2111 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2112 E = P->getSubExpr();
2113 continue;
2114 }
2115 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2116 if (P->getOpcode() == UO_Extension) {
2117 E = P->getSubExpr();
2118 continue;
2119 }
2120 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002121 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2122 if (!P->isResultDependent()) {
2123 E = P->getResultExpr();
2124 continue;
2125 }
2126 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002127 return E;
2128 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002129}
2130
Chris Lattner56f34942008-02-13 01:02:39 +00002131/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2132/// or CastExprs or ImplicitCastExprs, returning their operand.
2133Expr *Expr::IgnoreParenCasts() {
2134 Expr *E = this;
2135 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002136 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002137 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002138 continue;
2139 }
2140 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002141 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002142 continue;
2143 }
2144 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2145 if (P->getOpcode() == UO_Extension) {
2146 E = P->getSubExpr();
2147 continue;
2148 }
2149 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002150 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2151 if (!P->isResultDependent()) {
2152 E = P->getResultExpr();
2153 continue;
2154 }
2155 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002156 if (MaterializeTemporaryExpr *Materialize
2157 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2158 E = Materialize->GetTemporaryExpr();
2159 continue;
2160 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002161 if (SubstNonTypeTemplateParmExpr *NTTP
2162 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2163 E = NTTP->getReplacement();
2164 continue;
2165 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002166 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002167 }
2168}
2169
John McCall9c5d70c2010-12-04 08:24:19 +00002170/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2171/// casts. This is intended purely as a temporary workaround for code
2172/// that hasn't yet been rewritten to do the right thing about those
2173/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002174Expr *Expr::IgnoreParenLValueCasts() {
2175 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002176 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002177 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2178 E = P->getSubExpr();
2179 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002180 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002181 if (P->getCastKind() == CK_LValueToRValue) {
2182 E = P->getSubExpr();
2183 continue;
2184 }
John McCall9c5d70c2010-12-04 08:24:19 +00002185 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2186 if (P->getOpcode() == UO_Extension) {
2187 E = P->getSubExpr();
2188 continue;
2189 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002190 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2191 if (!P->isResultDependent()) {
2192 E = P->getResultExpr();
2193 continue;
2194 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002195 } else if (MaterializeTemporaryExpr *Materialize
2196 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2197 E = Materialize->GetTemporaryExpr();
2198 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002199 } else if (SubstNonTypeTemplateParmExpr *NTTP
2200 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2201 E = NTTP->getReplacement();
2202 continue;
John McCallf6a16482010-12-04 03:47:34 +00002203 }
2204 break;
2205 }
2206 return E;
2207}
2208
John McCall2fc46bf2010-05-05 22:59:52 +00002209Expr *Expr::IgnoreParenImpCasts() {
2210 Expr *E = this;
2211 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002212 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002213 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002214 continue;
2215 }
2216 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002217 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002218 continue;
2219 }
2220 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2221 if (P->getOpcode() == UO_Extension) {
2222 E = P->getSubExpr();
2223 continue;
2224 }
2225 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002226 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2227 if (!P->isResultDependent()) {
2228 E = P->getResultExpr();
2229 continue;
2230 }
2231 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002232 if (MaterializeTemporaryExpr *Materialize
2233 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2234 E = Materialize->GetTemporaryExpr();
2235 continue;
2236 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002237 if (SubstNonTypeTemplateParmExpr *NTTP
2238 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2239 E = NTTP->getReplacement();
2240 continue;
2241 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002242 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002243 }
2244}
2245
Hans Wennborg2f072b42011-06-09 17:06:51 +00002246Expr *Expr::IgnoreConversionOperator() {
2247 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002248 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002249 return MCE->getImplicitObjectArgument();
2250 }
2251 return this;
2252}
2253
Chris Lattnerecdd8412009-03-13 17:28:01 +00002254/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2255/// value (including ptr->int casts of the same size). Strip off any
2256/// ParenExpr or CastExprs, returning their operand.
2257Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2258 Expr *E = this;
2259 while (true) {
2260 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2261 E = P->getSubExpr();
2262 continue;
2263 }
Mike Stump1eb44332009-09-09 15:08:12 +00002264
Chris Lattnerecdd8412009-03-13 17:28:01 +00002265 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2266 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002267 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002268 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002269
Chris Lattnerecdd8412009-03-13 17:28:01 +00002270 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2271 E = SE;
2272 continue;
2273 }
Mike Stump1eb44332009-09-09 15:08:12 +00002274
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002275 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002276 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002277 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002278 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002279 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2280 E = SE;
2281 continue;
2282 }
2283 }
Mike Stump1eb44332009-09-09 15:08:12 +00002284
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002285 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2286 if (P->getOpcode() == UO_Extension) {
2287 E = P->getSubExpr();
2288 continue;
2289 }
2290 }
2291
Peter Collingbournef111d932011-04-15 00:35:48 +00002292 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2293 if (!P->isResultDependent()) {
2294 E = P->getResultExpr();
2295 continue;
2296 }
2297 }
2298
Douglas Gregorc0244c52011-09-08 17:56:33 +00002299 if (SubstNonTypeTemplateParmExpr *NTTP
2300 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2301 E = NTTP->getReplacement();
2302 continue;
2303 }
2304
Chris Lattnerecdd8412009-03-13 17:28:01 +00002305 return E;
2306 }
2307}
2308
Douglas Gregor6eef5192009-12-14 19:27:10 +00002309bool Expr::isDefaultArgument() const {
2310 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002311 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2312 E = M->GetTemporaryExpr();
2313
Douglas Gregor6eef5192009-12-14 19:27:10 +00002314 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2315 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002316
Douglas Gregor6eef5192009-12-14 19:27:10 +00002317 return isa<CXXDefaultArgExpr>(E);
2318}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002319
Douglas Gregor2f599792010-04-02 18:24:57 +00002320/// \brief Skip over any no-op casts and any temporary-binding
2321/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002322static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002323 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2324 E = M->GetTemporaryExpr();
2325
Douglas Gregor2f599792010-04-02 18:24:57 +00002326 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002327 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002328 E = ICE->getSubExpr();
2329 else
2330 break;
2331 }
2332
2333 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2334 E = BE->getSubExpr();
2335
2336 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002337 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002338 E = ICE->getSubExpr();
2339 else
2340 break;
2341 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002342
2343 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002344}
2345
John McCall558d2ab2010-09-15 10:14:12 +00002346/// isTemporaryObject - Determines if this expression produces a
2347/// temporary of the given class type.
2348bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2349 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2350 return false;
2351
Anders Carlssonf8b30152010-11-28 16:40:49 +00002352 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002353
John McCall58277b52010-09-15 20:59:13 +00002354 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002355 if (!E->Classify(C).isPRValue()) {
2356 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002357 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002358 return false;
2359 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002360
John McCall19e60ad2010-09-16 06:57:56 +00002361 // Black-list a few cases which yield pr-values of class type that don't
2362 // refer to temporaries of that type:
2363
2364 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002365 if (isa<ImplicitCastExpr>(E)) {
2366 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2367 case CK_DerivedToBase:
2368 case CK_UncheckedDerivedToBase:
2369 return false;
2370 default:
2371 break;
2372 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002373 }
2374
John McCall19e60ad2010-09-16 06:57:56 +00002375 // - member expressions (all)
2376 if (isa<MemberExpr>(E))
2377 return false;
2378
John McCall56ca35d2011-02-17 10:25:35 +00002379 // - opaque values (all)
2380 if (isa<OpaqueValueExpr>(E))
2381 return false;
2382
John McCall558d2ab2010-09-15 10:14:12 +00002383 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002384}
2385
Douglas Gregor75e85042011-03-02 21:06:53 +00002386bool Expr::isImplicitCXXThis() const {
2387 const Expr *E = this;
2388
2389 // Strip away parentheses and casts we don't care about.
2390 while (true) {
2391 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2392 E = Paren->getSubExpr();
2393 continue;
2394 }
2395
2396 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2397 if (ICE->getCastKind() == CK_NoOp ||
2398 ICE->getCastKind() == CK_LValueToRValue ||
2399 ICE->getCastKind() == CK_DerivedToBase ||
2400 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2401 E = ICE->getSubExpr();
2402 continue;
2403 }
2404 }
2405
2406 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2407 if (UnOp->getOpcode() == UO_Extension) {
2408 E = UnOp->getSubExpr();
2409 continue;
2410 }
2411 }
2412
Douglas Gregor03e80032011-06-21 17:03:29 +00002413 if (const MaterializeTemporaryExpr *M
2414 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2415 E = M->GetTemporaryExpr();
2416 continue;
2417 }
2418
Douglas Gregor75e85042011-03-02 21:06:53 +00002419 break;
2420 }
2421
2422 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2423 return This->isImplicit();
2424
2425 return false;
2426}
2427
Douglas Gregor898574e2008-12-05 23:32:09 +00002428/// hasAnyTypeDependentArguments - Determines if any of the expressions
2429/// in Exprs is type-dependent.
2430bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2431 for (unsigned I = 0; I < NumExprs; ++I)
2432 if (Exprs[I]->isTypeDependent())
2433 return true;
2434
2435 return false;
2436}
2437
2438/// hasAnyValueDependentArguments - Determines if any of the expressions
2439/// in Exprs is value-dependent.
2440bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2441 for (unsigned I = 0; I < NumExprs; ++I)
2442 if (Exprs[I]->isValueDependent())
2443 return true;
2444
2445 return false;
2446}
2447
John McCall4204f072010-08-02 21:13:48 +00002448bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002449 // This function is attempting whether an expression is an initializer
2450 // which can be evaluated at compile-time. isEvaluatable handles most
2451 // of the cases, but it can't deal with some initializer-specific
2452 // expressions, and it can't deal with aggregates; we deal with those here,
2453 // and fall back to isEvaluatable for the other cases.
2454
John McCall4204f072010-08-02 21:13:48 +00002455 // If we ever capture reference-binding directly in the AST, we can
2456 // kill the second parameter.
2457
2458 if (IsForRef) {
2459 EvalResult Result;
2460 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2461 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002462
Anders Carlssone8a32b82008-11-24 05:23:59 +00002463 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002464 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002465 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002466 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002467 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002468 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002469 case CXXTemporaryObjectExprClass:
2470 case CXXConstructExprClass: {
2471 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002472
2473 // Only if it's
2474 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002475 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002476 if (!CE->getNumArgs()) return true;
2477
2478 // 2) an elidable trivial copy construction of an operand which is
2479 // itself a constant initializer. Note that we consider the
2480 // operand on its own, *not* as a reference binding.
2481 return CE->isElidable() &&
2482 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002483 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002484 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002485 // This handles gcc's extension that allows global initializers like
2486 // "struct x {int x;} x = (struct x) {};".
2487 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002488 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002489 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002490 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002491 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002492 // FIXME: This doesn't deal with fields with reference types correctly.
2493 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2494 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002495 const InitListExpr *Exp = cast<InitListExpr>(this);
2496 unsigned numInits = Exp->getNumInits();
2497 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002498 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002499 return false;
2500 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002501 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002502 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002503 case ImplicitValueInitExprClass:
2504 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002505 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002506 return cast<ParenExpr>(this)->getSubExpr()
2507 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002508 case GenericSelectionExprClass:
2509 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2510 return false;
2511 return cast<GenericSelectionExpr>(this)->getResultExpr()
2512 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002513 case ChooseExprClass:
2514 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2515 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002516 case UnaryOperatorClass: {
2517 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002518 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002519 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002520 break;
2521 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002522 case BinaryOperatorClass: {
2523 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2524 // but this handles the common case.
2525 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002526 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002527 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2528 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2529 return true;
2530 break;
2531 }
John McCall4204f072010-08-02 21:13:48 +00002532 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002533 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002534 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002535 case CStyleCastExprClass:
2536 // Handle casts with a destination that's a struct or union; this
2537 // deals with both the gcc no-op struct cast extension and the
2538 // cast-to-union extension.
2539 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002540 return cast<CastExpr>(this)->getSubExpr()
2541 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002542
Chris Lattner430656e2009-10-13 22:12:09 +00002543 // Integer->integer casts can be handled here, which is important for
2544 // things like (int)(&&x-&&y). Scary but true.
2545 if (getType()->isIntegerType() &&
2546 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002547 return cast<CastExpr>(this)->getSubExpr()
2548 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002549
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002550 break;
Douglas Gregor03e80032011-06-21 17:03:29 +00002551
2552 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002553 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002554 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002555 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002556 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002557}
2558
Chandler Carruth82214a82011-02-18 23:54:50 +00002559/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2560/// pointer constant or not, as well as the specific kind of constant detected.
2561/// Null pointer constants can be integer constant expressions with the
2562/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2563/// (a GNU extension).
2564Expr::NullPointerConstantKind
2565Expr::isNullPointerConstant(ASTContext &Ctx,
2566 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002567 if (isValueDependent()) {
2568 switch (NPC) {
2569 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002570 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002571 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002572 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2573 return NPCK_ZeroInteger;
2574 else
2575 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002576
Douglas Gregorce940492009-09-25 04:25:58 +00002577 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002578 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002579 }
2580 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002581
Sebastian Redl07779722008-10-31 14:43:28 +00002582 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002583 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002584 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002585 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002586 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002587 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002588 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002589 Pointee->isVoidType() && // to void*
2590 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002591 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002592 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002593 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002594 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2595 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002596 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002597 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2598 // Accept ((void*)0) as a null pointer constant, as many other
2599 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002600 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002601 } else if (const GenericSelectionExpr *GE =
2602 dyn_cast<GenericSelectionExpr>(this)) {
2603 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002604 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002605 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002606 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002607 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002608 } else if (isa<GNUNullExpr>(this)) {
2609 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002610 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002611 } else if (const MaterializeTemporaryExpr *M
2612 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2613 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002614 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2615 if (const Expr *Source = OVE->getSourceExpr())
2616 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002617 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002618
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002619 // C++0x nullptr_t is always a null pointer constant.
2620 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002621 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002622
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002623 if (const RecordType *UT = getType()->getAsUnionType())
2624 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2625 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2626 const Expr *InitExpr = CLE->getInitializer();
2627 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2628 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2629 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002630 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002631 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002632 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002633 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002634
Reid Spencer5f016e22007-07-11 17:01:13 +00002635 // If we have an integer constant expression, we need to *evaluate* it and
2636 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002637 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002638 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2639
2640 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002641}
Steve Naroff31a45842007-07-28 23:10:27 +00002642
John McCallf6a16482010-12-04 03:47:34 +00002643/// \brief If this expression is an l-value for an Objective C
2644/// property, find the underlying property reference expression.
2645const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2646 const Expr *E = this;
2647 while (true) {
2648 assert((E->getValueKind() == VK_LValue &&
2649 E->getObjectKind() == OK_ObjCProperty) &&
2650 "expression is not a property reference");
2651 E = E->IgnoreParenCasts();
2652 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2653 if (BO->getOpcode() == BO_Comma) {
2654 E = BO->getRHS();
2655 continue;
2656 }
2657 }
2658
2659 break;
2660 }
2661
2662 return cast<ObjCPropertyRefExpr>(E);
2663}
2664
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002665FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002666 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002667
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002668 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002669 if (ICE->getCastKind() == CK_LValueToRValue ||
2670 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002671 E = ICE->getSubExpr()->IgnoreParens();
2672 else
2673 break;
2674 }
2675
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002676 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002677 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002678 if (Field->isBitField())
2679 return Field;
2680
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002681 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2682 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2683 if (Field->isBitField())
2684 return Field;
2685
Eli Friedman42068e92011-07-13 02:05:57 +00002686 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002687 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2688 return BinOp->getLHS()->getBitField();
2689
Eli Friedman42068e92011-07-13 02:05:57 +00002690 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2691 return BinOp->getRHS()->getBitField();
2692 }
2693
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002694 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002695}
2696
Anders Carlsson09380262010-01-31 17:18:49 +00002697bool Expr::refersToVectorElement() const {
2698 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002699
Anders Carlsson09380262010-01-31 17:18:49 +00002700 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002701 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002702 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002703 E = ICE->getSubExpr()->IgnoreParens();
2704 else
2705 break;
2706 }
Sean Huntc3021132010-05-05 15:23:54 +00002707
Anders Carlsson09380262010-01-31 17:18:49 +00002708 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2709 return ASE->getBase()->getType()->isVectorType();
2710
2711 if (isa<ExtVectorElementExpr>(E))
2712 return true;
2713
2714 return false;
2715}
2716
Chris Lattner2140e902009-02-16 22:14:05 +00002717/// isArrow - Return true if the base expression is a pointer to vector,
2718/// return false if the base expression is a vector.
2719bool ExtVectorElementExpr::isArrow() const {
2720 return getBase()->getType()->isPointerType();
2721}
2722
Nate Begeman213541a2008-04-18 23:10:10 +00002723unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002724 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002725 return VT->getNumElements();
2726 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002727}
2728
Nate Begeman8a997642008-05-09 06:41:27 +00002729/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002730bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002731 // FIXME: Refactor this code to an accessor on the AST node which returns the
2732 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002733 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002734
2735 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002736 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002737 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002738
Nate Begeman190d6a22009-01-18 02:01:21 +00002739 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002740 if (Comp[0] == 's' || Comp[0] == 'S')
2741 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002742
Daniel Dunbar15027422009-10-17 23:53:04 +00002743 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002744 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002745 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002746
Steve Narofffec0b492007-07-30 03:29:09 +00002747 return false;
2748}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002749
Nate Begeman8a997642008-05-09 06:41:27 +00002750/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002751void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002752 SmallVectorImpl<unsigned> &Elts) const {
2753 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002754 if (Comp[0] == 's' || Comp[0] == 'S')
2755 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002756
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002757 bool isHi = Comp == "hi";
2758 bool isLo = Comp == "lo";
2759 bool isEven = Comp == "even";
2760 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002761
Nate Begeman8a997642008-05-09 06:41:27 +00002762 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2763 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002764
Nate Begeman8a997642008-05-09 06:41:27 +00002765 if (isHi)
2766 Index = e + i;
2767 else if (isLo)
2768 Index = i;
2769 else if (isEven)
2770 Index = 2 * i;
2771 else if (isOdd)
2772 Index = 2 * i + 1;
2773 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002774 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002775
Nate Begeman3b8d1162008-05-13 21:03:02 +00002776 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002777 }
Nate Begeman8a997642008-05-09 06:41:27 +00002778}
2779
Douglas Gregor04badcf2010-04-21 00:45:42 +00002780ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002781 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002782 SourceLocation LBracLoc,
2783 SourceLocation SuperLoc,
2784 bool IsInstanceSuper,
2785 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002786 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002787 ArrayRef<SourceLocation> SelLocs,
2788 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002789 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002790 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002791 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002792 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002793 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002794 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002795 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002796 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2797 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002798 Kind(IsInstanceSuper? SuperInstance : SuperClass),
2799 HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002800 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002801{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002802 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002803 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002804}
2805
Douglas Gregor04badcf2010-04-21 00:45:42 +00002806ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002807 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002808 SourceLocation LBracLoc,
2809 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002810 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002811 ArrayRef<SourceLocation> SelLocs,
2812 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002813 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002814 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002815 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002816 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002817 T->isDependentType(), T->isInstantiationDependentType(),
2818 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002819 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2820 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002821 Kind(Class),
2822 HasMethod(Method != 0), IsDelegateInitCall(false),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002823 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002824{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002825 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002826 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002827}
2828
Douglas Gregor04badcf2010-04-21 00:45:42 +00002829ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002830 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002831 SourceLocation LBracLoc,
2832 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002833 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002834 ArrayRef<SourceLocation> SelLocs,
2835 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002836 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002837 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002838 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002839 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002840 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002841 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002842 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002843 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2844 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002845 Kind(Instance),
2846 HasMethod(Method != 0), IsDelegateInitCall(false),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002847 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002848{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002849 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002850 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002851}
2852
2853void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2854 ArrayRef<SourceLocation> SelLocs,
2855 SelectorLocationsKind SelLocsK) {
2856 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00002857 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002858 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002859 if (Args[I]->isTypeDependent())
2860 ExprBits.TypeDependent = true;
2861 if (Args[I]->isValueDependent())
2862 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002863 if (Args[I]->isInstantiationDependent())
2864 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002865 if (Args[I]->containsUnexpandedParameterPack())
2866 ExprBits.ContainsUnexpandedParameterPack = true;
2867
2868 MyArgs[I] = Args[I];
2869 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002870
2871 SelLocsKind = SelLocsK;
2872 if (SelLocsK == SelLoc_NonStandard)
2873 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
Chris Lattner0389e6b2009-04-26 00:44:05 +00002874}
2875
Douglas Gregor04badcf2010-04-21 00:45:42 +00002876ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002877 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002878 SourceLocation LBracLoc,
2879 SourceLocation SuperLoc,
2880 bool IsInstanceSuper,
2881 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002882 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002883 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002884 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002885 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002886 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002887 SelectorLocationsKind SelLocsK;
2888 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00002889 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002890 SuperType, Sel, SelLocs, SelLocsK,
2891 Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002892}
2893
2894ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002895 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002896 SourceLocation LBracLoc,
2897 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002898 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002899 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002900 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002901 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002902 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002903 SelectorLocationsKind SelLocsK;
2904 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002905 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002906 SelLocs, SelLocsK, Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002907}
2908
2909ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002910 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002911 SourceLocation LBracLoc,
2912 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002913 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002914 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002915 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002916 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002917 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002918 SelectorLocationsKind SelLocsK;
2919 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002920 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002921 SelLocs, SelLocsK, Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002922}
2923
Sean Huntc3021132010-05-05 15:23:54 +00002924ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002925 unsigned NumArgs,
2926 unsigned NumStoredSelLocs) {
2927 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002928 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2929}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002930
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002931ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2932 ArrayRef<Expr *> Args,
2933 SourceLocation RBraceLoc,
2934 ArrayRef<SourceLocation> SelLocs,
2935 Selector Sel,
2936 SelectorLocationsKind &SelLocsK) {
2937 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
2938 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
2939 : 0;
2940 return alloc(C, Args.size(), NumStoredSelLocs);
2941}
2942
2943ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2944 unsigned NumArgs,
2945 unsigned NumStoredSelLocs) {
2946 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2947 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
2948 return (ObjCMessageExpr *)C.Allocate(Size,
2949 llvm::AlignOf<ObjCMessageExpr>::Alignment);
2950}
2951
2952void ObjCMessageExpr::getSelectorLocs(
2953 SmallVectorImpl<SourceLocation> &SelLocs) const {
2954 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
2955 SelLocs.push_back(getSelectorLoc(i));
2956}
2957
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002958SourceRange ObjCMessageExpr::getReceiverRange() const {
2959 switch (getReceiverKind()) {
2960 case Instance:
2961 return getInstanceReceiver()->getSourceRange();
2962
2963 case Class:
2964 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2965
2966 case SuperInstance:
2967 case SuperClass:
2968 return getSuperLoc();
2969 }
2970
2971 return SourceLocation();
2972}
2973
Douglas Gregor04badcf2010-04-21 00:45:42 +00002974Selector ObjCMessageExpr::getSelector() const {
2975 if (HasMethod)
2976 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2977 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002978 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002979}
2980
2981ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2982 switch (getReceiverKind()) {
2983 case Instance:
2984 if (const ObjCObjectPointerType *Ptr
2985 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2986 return Ptr->getInterfaceDecl();
2987 break;
2988
2989 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002990 if (const ObjCObjectType *Ty
2991 = getClassReceiver()->getAs<ObjCObjectType>())
2992 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002993 break;
2994
2995 case SuperInstance:
2996 if (const ObjCObjectPointerType *Ptr
2997 = getSuperType()->getAs<ObjCObjectPointerType>())
2998 return Ptr->getInterfaceDecl();
2999 break;
3000
3001 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00003002 if (const ObjCObjectType *Iface
3003 = getSuperType()->getAs<ObjCObjectType>())
3004 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003005 break;
3006 }
3007
3008 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003009}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003010
Chris Lattner5f9e2722011-07-23 10:55:15 +00003011StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003012 switch (getBridgeKind()) {
3013 case OBC_Bridge:
3014 return "__bridge";
3015 case OBC_BridgeTransfer:
3016 return "__bridge_transfer";
3017 case OBC_BridgeRetained:
3018 return "__bridge_retained";
3019 }
3020
3021 return "__bridge";
3022}
3023
Jay Foad4ba2a172011-01-12 09:06:06 +00003024bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003025 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003026}
3027
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003028ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3029 QualType Type, SourceLocation BLoc,
3030 SourceLocation RP)
3031 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3032 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003033 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003034 Type->containsUnexpandedParameterPack()),
3035 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3036{
3037 SubExprs = new (C) Stmt*[nexpr];
3038 for (unsigned i = 0; i < nexpr; i++) {
3039 if (args[i]->isTypeDependent())
3040 ExprBits.TypeDependent = true;
3041 if (args[i]->isValueDependent())
3042 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003043 if (args[i]->isInstantiationDependent())
3044 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003045 if (args[i]->containsUnexpandedParameterPack())
3046 ExprBits.ContainsUnexpandedParameterPack = true;
3047
3048 SubExprs[i] = args[i];
3049 }
3050}
3051
Nate Begeman888376a2009-08-12 02:28:50 +00003052void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3053 unsigned NumExprs) {
3054 if (SubExprs) C.Deallocate(SubExprs);
3055
3056 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003057 this->NumExprs = NumExprs;
3058 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003059}
Nate Begeman888376a2009-08-12 02:28:50 +00003060
Peter Collingbournef111d932011-04-15 00:35:48 +00003061GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3062 SourceLocation GenericLoc, Expr *ControllingExpr,
3063 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3064 unsigned NumAssocs, SourceLocation DefaultLoc,
3065 SourceLocation RParenLoc,
3066 bool ContainsUnexpandedParameterPack,
3067 unsigned ResultIndex)
3068 : Expr(GenericSelectionExprClass,
3069 AssocExprs[ResultIndex]->getType(),
3070 AssocExprs[ResultIndex]->getValueKind(),
3071 AssocExprs[ResultIndex]->getObjectKind(),
3072 AssocExprs[ResultIndex]->isTypeDependent(),
3073 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003074 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003075 ContainsUnexpandedParameterPack),
3076 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3077 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3078 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3079 RParenLoc(RParenLoc) {
3080 SubExprs[CONTROLLING] = ControllingExpr;
3081 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3082 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3083}
3084
3085GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3086 SourceLocation GenericLoc, Expr *ControllingExpr,
3087 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3088 unsigned NumAssocs, SourceLocation DefaultLoc,
3089 SourceLocation RParenLoc,
3090 bool ContainsUnexpandedParameterPack)
3091 : Expr(GenericSelectionExprClass,
3092 Context.DependentTy,
3093 VK_RValue,
3094 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003095 /*isTypeDependent=*/true,
3096 /*isValueDependent=*/true,
3097 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003098 ContainsUnexpandedParameterPack),
3099 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3100 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3101 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3102 RParenLoc(RParenLoc) {
3103 SubExprs[CONTROLLING] = ControllingExpr;
3104 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3105 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3106}
3107
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003108//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003109// DesignatedInitExpr
3110//===----------------------------------------------------------------------===//
3111
Chandler Carruthb1138242011-06-16 06:47:06 +00003112IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003113 assert(Kind == FieldDesignator && "Only valid on a field designator");
3114 if (Field.NameOrField & 0x01)
3115 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3116 else
3117 return getField()->getIdentifier();
3118}
3119
Sean Huntc3021132010-05-05 15:23:54 +00003120DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003121 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003122 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003123 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003124 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003125 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003126 unsigned NumIndexExprs,
3127 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003128 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003129 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003130 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003131 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003132 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003133 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3134 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003135 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003136
3137 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003138 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003139 *Child++ = Init;
3140
3141 // Copy the designators and their subexpressions, computing
3142 // value-dependence along the way.
3143 unsigned IndexIdx = 0;
3144 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003145 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003146
3147 if (this->Designators[I].isArrayDesignator()) {
3148 // Compute type- and value-dependence.
3149 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003150 if (Index->isTypeDependent() || Index->isValueDependent())
3151 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003152 if (Index->isInstantiationDependent())
3153 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003154 // Propagate unexpanded parameter packs.
3155 if (Index->containsUnexpandedParameterPack())
3156 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003157
3158 // Copy the index expressions into permanent storage.
3159 *Child++ = IndexExprs[IndexIdx++];
3160 } else if (this->Designators[I].isArrayRangeDesignator()) {
3161 // Compute type- and value-dependence.
3162 Expr *Start = IndexExprs[IndexIdx];
3163 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003164 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003165 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003166 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003167 ExprBits.InstantiationDependent = true;
3168 } else if (Start->isInstantiationDependent() ||
3169 End->isInstantiationDependent()) {
3170 ExprBits.InstantiationDependent = true;
3171 }
3172
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003173 // Propagate unexpanded parameter packs.
3174 if (Start->containsUnexpandedParameterPack() ||
3175 End->containsUnexpandedParameterPack())
3176 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003177
3178 // Copy the start/end expressions into permanent storage.
3179 *Child++ = IndexExprs[IndexIdx++];
3180 *Child++ = IndexExprs[IndexIdx++];
3181 }
3182 }
3183
3184 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003185}
3186
Douglas Gregor05c13a32009-01-22 00:58:24 +00003187DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003188DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003189 unsigned NumDesignators,
3190 Expr **IndexExprs, unsigned NumIndexExprs,
3191 SourceLocation ColonOrEqualLoc,
3192 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003193 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003194 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003195 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003196 ColonOrEqualLoc, UsesColonSyntax,
3197 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003198}
3199
Mike Stump1eb44332009-09-09 15:08:12 +00003200DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003201 unsigned NumIndexExprs) {
3202 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3203 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3204 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3205}
3206
Douglas Gregor319d57f2010-01-06 23:17:19 +00003207void DesignatedInitExpr::setDesignators(ASTContext &C,
3208 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003209 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003210 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003211 NumDesignators = NumDesigs;
3212 for (unsigned I = 0; I != NumDesigs; ++I)
3213 Designators[I] = Desigs[I];
3214}
3215
Abramo Bagnara24f46742011-03-16 15:08:46 +00003216SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3217 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3218 if (size() == 1)
3219 return DIE->getDesignator(0)->getSourceRange();
3220 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3221 DIE->getDesignator(size()-1)->getEndLocation());
3222}
3223
Douglas Gregor05c13a32009-01-22 00:58:24 +00003224SourceRange DesignatedInitExpr::getSourceRange() const {
3225 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003226 Designator &First =
3227 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003228 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003229 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003230 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3231 else
3232 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3233 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003234 StartLoc =
3235 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003236 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3237}
3238
Douglas Gregor05c13a32009-01-22 00:58:24 +00003239Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3240 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3241 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3242 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003243 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3244 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3245}
3246
3247Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003248 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003249 "Requires array range designator");
3250 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3251 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003252 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3253 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3254}
3255
3256Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003257 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003258 "Requires array range designator");
3259 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3260 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003261 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3262 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3263}
3264
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003265/// \brief Replaces the designator at index @p Idx with the series
3266/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003267void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003268 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003269 const Designator *Last) {
3270 unsigned NumNewDesignators = Last - First;
3271 if (NumNewDesignators == 0) {
3272 std::copy_backward(Designators + Idx + 1,
3273 Designators + NumDesignators,
3274 Designators + Idx);
3275 --NumNewDesignators;
3276 return;
3277 } else if (NumNewDesignators == 1) {
3278 Designators[Idx] = *First;
3279 return;
3280 }
3281
Mike Stump1eb44332009-09-09 15:08:12 +00003282 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003283 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003284 std::copy(Designators, Designators + Idx, NewDesignators);
3285 std::copy(First, Last, NewDesignators + Idx);
3286 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3287 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003288 Designators = NewDesignators;
3289 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3290}
3291
Mike Stump1eb44332009-09-09 15:08:12 +00003292ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003293 Expr **exprs, unsigned nexprs,
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003294 SourceLocation rparenloc, QualType T)
3295 : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003296 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003297 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003298 assert(!T.isNull() && "ParenListExpr must have a valid type");
Nate Begeman2ef13e52009-08-10 23:49:36 +00003299 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003300 for (unsigned i = 0; i != nexprs; ++i) {
3301 if (exprs[i]->isTypeDependent())
3302 ExprBits.TypeDependent = true;
3303 if (exprs[i]->isValueDependent())
3304 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003305 if (exprs[i]->isInstantiationDependent())
3306 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003307 if (exprs[i]->containsUnexpandedParameterPack())
3308 ExprBits.ContainsUnexpandedParameterPack = true;
3309
Nate Begeman2ef13e52009-08-10 23:49:36 +00003310 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003311 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003312}
3313
John McCalle996ffd2011-02-16 08:02:54 +00003314const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3315 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3316 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003317 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3318 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003319 e = cast<CXXConstructExpr>(e)->getArg(0);
3320 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3321 e = ice->getSubExpr();
3322 return cast<OpaqueValueExpr>(e);
3323}
3324
John McCall4b9c2d22011-11-06 09:01:30 +00003325PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3326 unsigned numSemanticExprs) {
3327 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3328 (1 + numSemanticExprs) * sizeof(Expr*),
3329 llvm::alignOf<PseudoObjectExpr>());
3330 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3331}
3332
3333PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3334 : Expr(PseudoObjectExprClass, shell) {
3335 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3336}
3337
3338PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3339 ArrayRef<Expr*> semantics,
3340 unsigned resultIndex) {
3341 assert(syntax && "no syntactic expression!");
3342 assert(semantics.size() && "no semantic expressions!");
3343
3344 QualType type;
3345 ExprValueKind VK;
3346 if (resultIndex == NoResult) {
3347 type = C.VoidTy;
3348 VK = VK_RValue;
3349 } else {
3350 assert(resultIndex < semantics.size());
3351 type = semantics[resultIndex]->getType();
3352 VK = semantics[resultIndex]->getValueKind();
3353 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3354 }
3355
3356 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3357 (1 + semantics.size()) * sizeof(Expr*),
3358 llvm::alignOf<PseudoObjectExpr>());
3359 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3360 resultIndex);
3361}
3362
3363PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3364 Expr *syntax, ArrayRef<Expr*> semantics,
3365 unsigned resultIndex)
3366 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3367 /*filled in at end of ctor*/ false, false, false, false) {
3368 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3369 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3370
3371 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3372 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3373 getSubExprsBuffer()[i] = E;
3374
3375 if (E->isTypeDependent())
3376 ExprBits.TypeDependent = true;
3377 if (E->isValueDependent())
3378 ExprBits.ValueDependent = true;
3379 if (E->isInstantiationDependent())
3380 ExprBits.InstantiationDependent = true;
3381 if (E->containsUnexpandedParameterPack())
3382 ExprBits.ContainsUnexpandedParameterPack = true;
3383
3384 if (isa<OpaqueValueExpr>(E))
3385 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3386 "opaque-value semantic expressions for pseudo-object "
3387 "operations must have sources");
3388 }
3389}
3390
Douglas Gregor05c13a32009-01-22 00:58:24 +00003391//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003392// ExprIterator.
3393//===----------------------------------------------------------------------===//
3394
3395Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3396Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3397Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3398const Expr* ConstExprIterator::operator[](size_t idx) const {
3399 return cast<Expr>(I[idx]);
3400}
3401const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3402const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3403
3404//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003405// Child Iterators for iterating over subexpressions/substatements
3406//===----------------------------------------------------------------------===//
3407
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003408// UnaryExprOrTypeTraitExpr
3409Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003410 // If this is of a type and the type is a VLA type (and not a typedef), the
3411 // size expression of the VLA needs to be treated as an executable expression.
3412 // Why isn't this weirdness documented better in StmtIterator?
3413 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003414 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003415 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003416 return child_range(child_iterator(T), child_iterator());
3417 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003418 }
John McCall63c00d72011-02-09 08:16:59 +00003419 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003420}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003421
Steve Naroff563477d2007-09-18 23:55:05 +00003422// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003423Stmt::child_range ObjCMessageExpr::children() {
3424 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003425 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003426 begin = reinterpret_cast<Stmt **>(this + 1);
3427 else
3428 begin = reinterpret_cast<Stmt **>(getArgs());
3429 return child_range(begin,
3430 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003431}
3432
Steve Naroff4eb206b2008-09-03 18:15:37 +00003433// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003434BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003435 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003436 bool constAdded)
Douglas Gregor561f8122011-07-01 01:22:09 +00003437 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003438 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003439 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003440{
Douglas Gregord967e312011-01-19 21:52:31 +00003441 bool TypeDependent = false;
3442 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +00003443 bool InstantiationDependent = false;
3444 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3445 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +00003446 ExprBits.TypeDependent = TypeDependent;
3447 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +00003448 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003449}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003450
3451
3452AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3453 QualType t, AtomicOp op, SourceLocation RP)
3454 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3455 false, false, false, false),
3456 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3457{
3458 for (unsigned i = 0; i < nexpr; i++) {
3459 if (args[i]->isTypeDependent())
3460 ExprBits.TypeDependent = true;
3461 if (args[i]->isValueDependent())
3462 ExprBits.ValueDependent = true;
3463 if (args[i]->isInstantiationDependent())
3464 ExprBits.InstantiationDependent = true;
3465 if (args[i]->containsUnexpandedParameterPack())
3466 ExprBits.ContainsUnexpandedParameterPack = true;
3467
3468 SubExprs[i] = args[i];
3469 }
3470}