blob: 848e07974445e927cad1568fa7c1ef0a21c07066 [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:
John McCall1d9b3b22011-09-09 05:25:32 +00001087 case CK_NoOp:
1088 case CK_PointerToBoolean:
1089 case CK_IntegralToBoolean:
1090 case CK_FloatingToBoolean:
1091 case CK_MemberPointerToBoolean:
1092 case CK_FloatingComplexToBoolean:
1093 case CK_IntegralComplexToBoolean:
1094 case CK_LValueBitCast: // -> bool&
1095 case CK_UserDefinedConversion: // operator bool()
1096 CheckNoBasePath:
1097 assert(path_empty() && "Cast kind should not have a base path!");
1098 break;
1099 }
1100}
1101
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001102const char *CastExpr::getCastKindName() const {
1103 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001104 case CK_Dependent:
1105 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001106 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001107 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001108 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001109 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001110 case CK_LValueToRValue:
1111 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001112 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001113 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001114 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001115 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001116 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001117 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001118 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001119 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001120 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001121 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001122 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001123 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001124 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001125 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001126 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001127 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001128 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001129 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001130 case CK_NullToPointer:
1131 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001132 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001133 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001134 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001135 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001136 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001137 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001138 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001139 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001140 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001141 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001142 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001143 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001144 case CK_PointerToBoolean:
1145 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001146 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001147 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001148 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001149 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001150 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001151 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001152 case CK_IntegralToBoolean:
1153 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001154 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001155 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001156 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001157 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001158 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001159 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001160 case CK_FloatingToBoolean:
1161 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001162 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001163 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001164 case CK_CPointerToObjCPointerCast:
1165 return "CPointerToObjCPointerCast";
1166 case CK_BlockPointerToObjCPointerCast:
1167 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001168 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001169 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001170 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001171 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001172 case CK_FloatingRealToComplex:
1173 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001174 case CK_FloatingComplexToReal:
1175 return "FloatingComplexToReal";
1176 case CK_FloatingComplexToBoolean:
1177 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001178 case CK_FloatingComplexCast:
1179 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001180 case CK_FloatingComplexToIntegralComplex:
1181 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001182 case CK_IntegralRealToComplex:
1183 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001184 case CK_IntegralComplexToReal:
1185 return "IntegralComplexToReal";
1186 case CK_IntegralComplexToBoolean:
1187 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001188 case CK_IntegralComplexCast:
1189 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001190 case CK_IntegralComplexToFloatingComplex:
1191 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001192 case CK_ARCConsumeObject:
1193 return "ARCConsumeObject";
1194 case CK_ARCProduceObject:
1195 return "ARCProduceObject";
1196 case CK_ARCReclaimReturnedObject:
1197 return "ARCReclaimReturnedObject";
1198 case CK_ARCExtendBlockObject:
1199 return "ARCCExtendBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001200 }
Mike Stump1eb44332009-09-09 15:08:12 +00001201
John McCall2bb5d002010-11-13 09:02:35 +00001202 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001203 return 0;
1204}
1205
Douglas Gregor6eef5192009-12-14 19:27:10 +00001206Expr *CastExpr::getSubExprAsWritten() {
1207 Expr *SubExpr = 0;
1208 CastExpr *E = this;
1209 do {
1210 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001211
1212 // Skip through reference binding to temporary.
1213 if (MaterializeTemporaryExpr *Materialize
1214 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1215 SubExpr = Materialize->GetTemporaryExpr();
1216
Douglas Gregor6eef5192009-12-14 19:27:10 +00001217 // Skip any temporary bindings; they're implicit.
1218 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1219 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001220
Douglas Gregor6eef5192009-12-14 19:27:10 +00001221 // Conversions by constructor and conversion functions have a
1222 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001223 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001224 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001225 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001226 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001227
Douglas Gregor6eef5192009-12-14 19:27:10 +00001228 // If the subexpression we're left with is an implicit cast, look
1229 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001230 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1231
Douglas Gregor6eef5192009-12-14 19:27:10 +00001232 return SubExpr;
1233}
1234
John McCallf871d0c2010-08-07 06:22:56 +00001235CXXBaseSpecifier **CastExpr::path_buffer() {
1236 switch (getStmtClass()) {
1237#define ABSTRACT_STMT(x)
1238#define CASTEXPR(Type, Base) \
1239 case Stmt::Type##Class: \
1240 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1241#define STMT(Type, Base)
1242#include "clang/AST/StmtNodes.inc"
1243 default:
1244 llvm_unreachable("non-cast expressions not possible here");
1245 return 0;
1246 }
1247}
1248
1249void CastExpr::setCastPath(const CXXCastPath &Path) {
1250 assert(Path.size() == path_size());
1251 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1252}
1253
1254ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1255 CastKind Kind, Expr *Operand,
1256 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001257 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001258 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1259 void *Buffer =
1260 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1261 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001262 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001263 if (PathSize) E->setCastPath(*BasePath);
1264 return E;
1265}
1266
1267ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1268 unsigned PathSize) {
1269 void *Buffer =
1270 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1271 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1272}
1273
1274
1275CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001276 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001277 const CXXCastPath *BasePath,
1278 TypeSourceInfo *WrittenTy,
1279 SourceLocation L, SourceLocation R) {
1280 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1281 void *Buffer =
1282 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1283 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001284 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001285 if (PathSize) E->setCastPath(*BasePath);
1286 return E;
1287}
1288
1289CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1290 void *Buffer =
1291 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1292 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1293}
1294
Reid Spencer5f016e22007-07-11 17:01:13 +00001295/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1296/// corresponds to, e.g. "<<=".
1297const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1298 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001299 case BO_PtrMemD: return ".*";
1300 case BO_PtrMemI: return "->*";
1301 case BO_Mul: return "*";
1302 case BO_Div: return "/";
1303 case BO_Rem: return "%";
1304 case BO_Add: return "+";
1305 case BO_Sub: return "-";
1306 case BO_Shl: return "<<";
1307 case BO_Shr: return ">>";
1308 case BO_LT: return "<";
1309 case BO_GT: return ">";
1310 case BO_LE: return "<=";
1311 case BO_GE: return ">=";
1312 case BO_EQ: return "==";
1313 case BO_NE: return "!=";
1314 case BO_And: return "&";
1315 case BO_Xor: return "^";
1316 case BO_Or: return "|";
1317 case BO_LAnd: return "&&";
1318 case BO_LOr: return "||";
1319 case BO_Assign: return "=";
1320 case BO_MulAssign: return "*=";
1321 case BO_DivAssign: return "/=";
1322 case BO_RemAssign: return "%=";
1323 case BO_AddAssign: return "+=";
1324 case BO_SubAssign: return "-=";
1325 case BO_ShlAssign: return "<<=";
1326 case BO_ShrAssign: return ">>=";
1327 case BO_AndAssign: return "&=";
1328 case BO_XorAssign: return "^=";
1329 case BO_OrAssign: return "|=";
1330 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001331 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001332
1333 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001334}
1335
John McCall2de56d12010-08-25 11:45:40 +00001336BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001337BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1338 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001339 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001340 case OO_Plus: return BO_Add;
1341 case OO_Minus: return BO_Sub;
1342 case OO_Star: return BO_Mul;
1343 case OO_Slash: return BO_Div;
1344 case OO_Percent: return BO_Rem;
1345 case OO_Caret: return BO_Xor;
1346 case OO_Amp: return BO_And;
1347 case OO_Pipe: return BO_Or;
1348 case OO_Equal: return BO_Assign;
1349 case OO_Less: return BO_LT;
1350 case OO_Greater: return BO_GT;
1351 case OO_PlusEqual: return BO_AddAssign;
1352 case OO_MinusEqual: return BO_SubAssign;
1353 case OO_StarEqual: return BO_MulAssign;
1354 case OO_SlashEqual: return BO_DivAssign;
1355 case OO_PercentEqual: return BO_RemAssign;
1356 case OO_CaretEqual: return BO_XorAssign;
1357 case OO_AmpEqual: return BO_AndAssign;
1358 case OO_PipeEqual: return BO_OrAssign;
1359 case OO_LessLess: return BO_Shl;
1360 case OO_GreaterGreater: return BO_Shr;
1361 case OO_LessLessEqual: return BO_ShlAssign;
1362 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1363 case OO_EqualEqual: return BO_EQ;
1364 case OO_ExclaimEqual: return BO_NE;
1365 case OO_LessEqual: return BO_LE;
1366 case OO_GreaterEqual: return BO_GE;
1367 case OO_AmpAmp: return BO_LAnd;
1368 case OO_PipePipe: return BO_LOr;
1369 case OO_Comma: return BO_Comma;
1370 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001371 }
1372}
1373
1374OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1375 static const OverloadedOperatorKind OverOps[] = {
1376 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1377 OO_Star, OO_Slash, OO_Percent,
1378 OO_Plus, OO_Minus,
1379 OO_LessLess, OO_GreaterGreater,
1380 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1381 OO_EqualEqual, OO_ExclaimEqual,
1382 OO_Amp,
1383 OO_Caret,
1384 OO_Pipe,
1385 OO_AmpAmp,
1386 OO_PipePipe,
1387 OO_Equal, OO_StarEqual,
1388 OO_SlashEqual, OO_PercentEqual,
1389 OO_PlusEqual, OO_MinusEqual,
1390 OO_LessLessEqual, OO_GreaterGreaterEqual,
1391 OO_AmpEqual, OO_CaretEqual,
1392 OO_PipeEqual,
1393 OO_Comma
1394 };
1395 return OverOps[Opc];
1396}
1397
Ted Kremenek709210f2010-04-13 23:39:13 +00001398InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001399 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001400 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001401 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001402 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001403 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001404 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +00001405 HadArrayRangeDesignator(false)
Sean Huntc3021132010-05-05 15:23:54 +00001406{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001407 for (unsigned I = 0; I != numInits; ++I) {
1408 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001409 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001410 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001411 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001412 if (initExprs[I]->isInstantiationDependent())
1413 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001414 if (initExprs[I]->containsUnexpandedParameterPack())
1415 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001416 }
Sean Huntc3021132010-05-05 15:23:54 +00001417
Ted Kremenek709210f2010-04-13 23:39:13 +00001418 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001419}
Reid Spencer5f016e22007-07-11 17:01:13 +00001420
Ted Kremenek709210f2010-04-13 23:39:13 +00001421void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001422 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001423 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001424}
1425
Ted Kremenek709210f2010-04-13 23:39:13 +00001426void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001427 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001428}
1429
Ted Kremenek709210f2010-04-13 23:39:13 +00001430Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001431 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001432 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001433 InitExprs.back() = expr;
1434 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001435 }
Mike Stump1eb44332009-09-09 15:08:12 +00001436
Douglas Gregor4c678342009-01-28 21:54:33 +00001437 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1438 InitExprs[Init] = expr;
1439 return Result;
1440}
1441
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001442void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001443 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001444 ArrayFillerOrUnionFieldInit = filler;
1445 // Fill out any "holes" in the array due to designated initializers.
1446 Expr **inits = getInits();
1447 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1448 if (inits[i] == 0)
1449 inits[i] = filler;
1450}
1451
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001452SourceRange InitListExpr::getSourceRange() const {
1453 if (SyntacticForm)
1454 return SyntacticForm->getSourceRange();
1455 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1456 if (Beg.isInvalid()) {
1457 // Find the first non-null initializer.
1458 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1459 E = InitExprs.end();
1460 I != E; ++I) {
1461 if (Stmt *S = *I) {
1462 Beg = S->getLocStart();
1463 break;
1464 }
1465 }
1466 }
1467 if (End.isInvalid()) {
1468 // Find the first non-null initializer from the end.
1469 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1470 E = InitExprs.rend();
1471 I != E; ++I) {
1472 if (Stmt *S = *I) {
1473 End = S->getSourceRange().getEnd();
1474 break;
1475 }
1476 }
1477 }
1478 return SourceRange(Beg, End);
1479}
1480
Steve Naroffbfdcae62008-09-04 15:31:07 +00001481/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001482///
1483const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001484 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001485 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001486}
1487
Mike Stump1eb44332009-09-09 15:08:12 +00001488SourceLocation BlockExpr::getCaretLocation() const {
1489 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001490}
Mike Stump1eb44332009-09-09 15:08:12 +00001491const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001492 return TheBlock->getBody();
1493}
Mike Stump1eb44332009-09-09 15:08:12 +00001494Stmt *BlockExpr::getBody() {
1495 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001496}
Steve Naroff56ee6892008-10-08 17:01:13 +00001497
1498
Reid Spencer5f016e22007-07-11 17:01:13 +00001499//===----------------------------------------------------------------------===//
1500// Generic Expression Routines
1501//===----------------------------------------------------------------------===//
1502
Chris Lattner026dc962009-02-14 07:37:35 +00001503/// isUnusedResultAWarning - Return true if this immediate expression should
1504/// be warned about if the result is unused. If so, fill in Loc and Ranges
1505/// with location to warn on and the source range[s] to report with the
1506/// warning.
1507bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001508 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001509 // Don't warn if the expr is type dependent. The type could end up
1510 // instantiating to void.
1511 if (isTypeDependent())
1512 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Reid Spencer5f016e22007-07-11 17:01:13 +00001514 switch (getStmtClass()) {
1515 default:
John McCall0faede62010-03-12 07:11:26 +00001516 if (getType()->isVoidType())
1517 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001518 Loc = getExprLoc();
1519 R1 = getSourceRange();
1520 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001522 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001523 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001524 case GenericSelectionExprClass:
1525 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1526 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001527 case UnaryOperatorClass: {
1528 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001529
Reid Spencer5f016e22007-07-11 17:01:13 +00001530 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001531 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001532 case UO_PostInc:
1533 case UO_PostDec:
1534 case UO_PreInc:
1535 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001536 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001537 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001538 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001539 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001540 return false;
1541 break;
John McCall2de56d12010-08-25 11:45:40 +00001542 case UO_Real:
1543 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001544 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001545 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1546 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001547 return false;
1548 break;
John McCall2de56d12010-08-25 11:45:40 +00001549 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001550 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 }
Chris Lattner026dc962009-02-14 07:37:35 +00001552 Loc = UO->getOperatorLoc();
1553 R1 = UO->getSubExpr()->getSourceRange();
1554 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001555 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001556 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001557 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001558 switch (BO->getOpcode()) {
1559 default:
1560 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001561 // Consider the RHS of comma for side effects. LHS was checked by
1562 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001563 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001564 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1565 // lvalue-ness) of an assignment written in a macro.
1566 if (IntegerLiteral *IE =
1567 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1568 if (IE->getValue() == 0)
1569 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001570 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1571 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001572 case BO_LAnd:
1573 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001574 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1575 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1576 return false;
1577 break;
John McCallbf0ee352010-02-16 04:10:53 +00001578 }
Chris Lattner026dc962009-02-14 07:37:35 +00001579 if (BO->isAssignmentOp())
1580 return false;
1581 Loc = BO->getOperatorLoc();
1582 R1 = BO->getLHS()->getSourceRange();
1583 R2 = BO->getRHS()->getSourceRange();
1584 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001585 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001586 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001587 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001588 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001589 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001590
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001591 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001592 // If only one of the LHS or RHS is a warning, the operator might
1593 // be being used for control flow. Only warn if both the LHS and
1594 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001595 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001596 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1597 return false;
1598 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001599 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001600 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001601 }
1602
Reid Spencer5f016e22007-07-11 17:01:13 +00001603 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001604 // If the base pointer or element is to a volatile pointer/field, accessing
1605 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001606 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001607 return false;
1608 Loc = cast<MemberExpr>(this)->getMemberLoc();
1609 R1 = SourceRange(Loc, Loc);
1610 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1611 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Reid Spencer5f016e22007-07-11 17:01:13 +00001613 case ArraySubscriptExprClass:
1614 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001615 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001616 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001617 return false;
1618 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1619 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1620 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1621 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001622
Chandler Carruth9b106832011-08-17 09:49:44 +00001623 case CXXOperatorCallExprClass: {
1624 // We warn about operator== and operator!= even when user-defined operator
1625 // overloads as there is no reasonable way to define these such that they
1626 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1627 // warning: these operators are commonly typo'ed, and so warning on them
1628 // provides additional value as well. If this list is updated,
1629 // DiagnoseUnusedComparison should be as well.
1630 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1631 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001632 Op->getOperator() == OO_ExclaimEqual) {
1633 Loc = Op->getOperatorLoc();
1634 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001635 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001636 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001637
1638 // Fallthrough for generic call handling.
1639 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001640 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001641 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001642 // If this is a direct call, get the callee.
1643 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001644 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001645 // If the callee has attribute pure, const, or warn_unused_result, warn
1646 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001647 //
1648 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1649 // updated to match for QoI.
1650 if (FD->getAttr<WarnUnusedResultAttr>() ||
1651 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1652 Loc = CE->getCallee()->getLocStart();
1653 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001655 if (unsigned NumArgs = CE->getNumArgs())
1656 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1657 CE->getArg(NumArgs-1)->getLocEnd());
1658 return true;
1659 }
Chris Lattner026dc962009-02-14 07:37:35 +00001660 }
1661 return false;
1662 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001663
1664 case CXXTemporaryObjectExprClass:
1665 case CXXConstructExprClass:
1666 return false;
1667
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001668 case ObjCMessageExprClass: {
1669 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001670 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1671 ME->isInstanceMessage() &&
1672 !ME->getType()->isVoidType() &&
1673 ME->getSelector().getIdentifierInfoForSlot(0) &&
1674 ME->getSelector().getIdentifierInfoForSlot(0)
1675 ->getName().startswith("init")) {
1676 Loc = getExprLoc();
1677 R1 = ME->getSourceRange();
1678 return true;
1679 }
1680
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001681 const ObjCMethodDecl *MD = ME->getMethodDecl();
1682 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1683 Loc = getExprLoc();
1684 return true;
1685 }
Chris Lattner026dc962009-02-14 07:37:35 +00001686 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001687 }
Mike Stump1eb44332009-09-09 15:08:12 +00001688
John McCall12f78a62010-12-02 01:19:52 +00001689 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001690 Loc = getExprLoc();
1691 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001692 return true;
John McCall12f78a62010-12-02 01:19:52 +00001693
John McCall4b9c2d22011-11-06 09:01:30 +00001694 case PseudoObjectExprClass: {
1695 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1696
1697 // Only complain about things that have the form of a getter.
1698 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1699 isa<BinaryOperator>(PO->getSyntacticForm()))
1700 return false;
1701
1702 Loc = getExprLoc();
1703 R1 = getSourceRange();
1704 return true;
1705 }
1706
Chris Lattner611b2ec2008-07-26 19:51:01 +00001707 case StmtExprClass: {
1708 // Statement exprs don't logically have side effects themselves, but are
1709 // sometimes used in macros in ways that give them a type that is unused.
1710 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1711 // however, if the result of the stmt expr is dead, we don't want to emit a
1712 // warning.
1713 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001714 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001715 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001716 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001717 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1718 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1719 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1720 }
Mike Stump1eb44332009-09-09 15:08:12 +00001721
John McCall0faede62010-03-12 07:11:26 +00001722 if (getType()->isVoidType())
1723 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001724 Loc = cast<StmtExpr>(this)->getLParenLoc();
1725 R1 = getSourceRange();
1726 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001727 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001728 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001729 // If this is an explicit cast to void, allow it. People do this when they
1730 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001731 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001732 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001733 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1734 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1735 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001736 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001737 if (getType()->isVoidType())
1738 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001739 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001740
Anders Carlsson58beed92009-11-17 17:11:23 +00001741 // If this is a cast to void or a constructor conversion, check the operand.
1742 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001743 if (CE->getCastKind() == CK_ToVoid ||
1744 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001745 return (cast<CastExpr>(this)->getSubExpr()
1746 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001747 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1748 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1749 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001750 }
Mike Stump1eb44332009-09-09 15:08:12 +00001751
Eli Friedman4be1f472008-05-19 21:24:43 +00001752 case ImplicitCastExprClass:
1753 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001754 return (cast<ImplicitCastExpr>(this)
1755 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001756
Chris Lattner04421082008-04-08 04:40:51 +00001757 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001758 return (cast<CXXDefaultArgExpr>(this)
1759 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001760
1761 case CXXNewExprClass:
1762 // FIXME: In theory, there might be new expressions that don't have side
1763 // effects (e.g. a placement new with an uninitialized POD).
1764 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001765 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001766 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001767 return (cast<CXXBindTemporaryExpr>(this)
1768 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001769 case ExprWithCleanupsClass:
1770 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001771 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001772 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001773}
1774
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001775/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001776/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001777bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001778 const Expr *E = IgnoreParens();
1779 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001780 default:
1781 return false;
1782 case ObjCIvarRefExprClass:
1783 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001784 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001785 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001786 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001787 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001788 case MaterializeTemporaryExprClass:
1789 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1790 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001791 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001792 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001793 case BlockDeclRefExprClass:
Douglas Gregora2813ce2009-10-23 18:54:35 +00001794 case DeclRefExprClass: {
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001795
1796 const Decl *D;
1797 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1798 D = BDRE->getDecl();
1799 else
1800 D = cast<DeclRefExpr>(E)->getDecl();
1801
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001802 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1803 if (VD->hasGlobalStorage())
1804 return true;
1805 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001806 // dereferencing to a pointer is always a gc'able candidate,
1807 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001808 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001809 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001810 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001811 return false;
1812 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001813 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001814 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001815 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001816 }
1817 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001818 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001819 }
1820}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001821
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001822bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1823 if (isTypeDependent())
1824 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001825 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001826}
1827
John McCall864c0412011-04-26 20:42:42 +00001828QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00001829 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00001830
1831 // Bound member expressions are always one of these possibilities:
1832 // x->m x.m x->*y x.*y
1833 // (possibly parenthesized)
1834
1835 expr = expr->IgnoreParens();
1836 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1837 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1838 return mem->getMemberDecl()->getType();
1839 }
1840
1841 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1842 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1843 ->getPointeeType();
1844 assert(type->isFunctionType());
1845 return type;
1846 }
1847
1848 assert(isa<UnresolvedMemberExpr>(expr));
1849 return QualType();
1850}
1851
Sebastian Redl369e51f2010-09-10 20:55:33 +00001852static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1853 Expr::CanThrowResult CT2) {
1854 // CanThrowResult constants are ordered so that the maximum is the correct
1855 // merge result.
1856 return CT1 > CT2 ? CT1 : CT2;
1857}
1858
1859static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1860 Expr *E = const_cast<Expr*>(CE);
1861 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001862 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001863 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1864 }
1865 return R;
1866}
1867
Richard Smith7a614d82011-06-11 17:19:42 +00001868static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1869 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001870 bool NullThrows = true) {
1871 if (!D)
1872 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1873
1874 // See if we can get a function type from the decl somehow.
1875 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1876 if (!VD) // If we have no clue what we're calling, assume the worst.
1877 return Expr::CT_Can;
1878
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001879 // As an extension, we assume that __attribute__((nothrow)) functions don't
1880 // throw.
1881 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1882 return Expr::CT_Cannot;
1883
Sebastian Redl369e51f2010-09-10 20:55:33 +00001884 QualType T = VD->getType();
1885 const FunctionProtoType *FT;
1886 if ((FT = T->getAs<FunctionProtoType>())) {
1887 } else if (const PointerType *PT = T->getAs<PointerType>())
1888 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1889 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1890 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1891 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1892 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1893 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1894 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1895
1896 if (!FT)
1897 return Expr::CT_Can;
1898
Richard Smith7a614d82011-06-11 17:19:42 +00001899 if (FT->getExceptionSpecType() == EST_Delayed) {
1900 assert(isa<CXXConstructorDecl>(D) &&
1901 "only constructor exception specs can be unknown");
1902 Ctx.getDiagnostics().Report(E->getLocStart(),
1903 diag::err_exception_spec_unknown)
1904 << E->getSourceRange();
1905 return Expr::CT_Can;
1906 }
1907
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001908 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001909}
1910
1911static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1912 if (DC->isTypeDependent())
1913 return Expr::CT_Dependent;
1914
Sebastian Redl295995c2010-09-10 20:55:47 +00001915 if (!DC->getTypeAsWritten()->isReferenceType())
1916 return Expr::CT_Cannot;
1917
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001918 if (DC->getSubExpr()->isTypeDependent())
1919 return Expr::CT_Dependent;
1920
Sebastian Redl369e51f2010-09-10 20:55:33 +00001921 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1922}
1923
1924static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1925 const CXXTypeidExpr *DC) {
1926 if (DC->isTypeOperand())
1927 return Expr::CT_Cannot;
1928
1929 Expr *Op = DC->getExprOperand();
1930 if (Op->isTypeDependent())
1931 return Expr::CT_Dependent;
1932
1933 const RecordType *RT = Op->getType()->getAs<RecordType>();
1934 if (!RT)
1935 return Expr::CT_Cannot;
1936
1937 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1938 return Expr::CT_Cannot;
1939
1940 if (Op->Classify(C).isPRValue())
1941 return Expr::CT_Cannot;
1942
1943 return Expr::CT_Can;
1944}
1945
1946Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1947 // C++ [expr.unary.noexcept]p3:
1948 // [Can throw] if in a potentially-evaluated context the expression would
1949 // contain:
1950 switch (getStmtClass()) {
1951 case CXXThrowExprClass:
1952 // - a potentially evaluated throw-expression
1953 return CT_Can;
1954
1955 case CXXDynamicCastExprClass: {
1956 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1957 // where T is a reference type, that requires a run-time check
1958 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1959 if (CT == CT_Can)
1960 return CT;
1961 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1962 }
1963
1964 case CXXTypeidExprClass:
1965 // - a potentially evaluated typeid expression applied to a glvalue
1966 // expression whose type is a polymorphic class type
1967 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1968
1969 // - a potentially evaluated call to a function, member function, function
1970 // pointer, or member function pointer that does not have a non-throwing
1971 // exception-specification
1972 case CallExprClass:
1973 case CXXOperatorCallExprClass:
1974 case CXXMemberCallExprClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001975 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001976 CanThrowResult CT;
1977 if (isTypeDependent())
1978 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001979 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1980 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001981 else
Richard Smith7a614d82011-06-11 17:19:42 +00001982 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001983 if (CT == CT_Can)
1984 return CT;
1985 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1986 }
1987
Sebastian Redl295995c2010-09-10 20:55:47 +00001988 case CXXConstructExprClass:
1989 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00001990 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001991 cast<CXXConstructExpr>(this)->getConstructor());
1992 if (CT == CT_Can)
1993 return CT;
1994 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1995 }
1996
1997 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001998 CanThrowResult CT;
1999 if (isTypeDependent())
2000 CT = CT_Dependent;
2001 else
2002 CT = MergeCanThrow(
Richard Smith7a614d82011-06-11 17:19:42 +00002003 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
2004 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00002005 /*NullThrows*/false));
2006 if (CT == CT_Can)
2007 return CT;
2008 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2009 }
2010
2011 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002012 CanThrowResult CT;
2013 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
2014 if (DTy.isNull() || DTy->isDependentType()) {
2015 CT = CT_Dependent;
2016 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00002017 CT = CanCalleeThrow(C, this,
2018 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002019 if (const RecordType *RT = DTy->getAs<RecordType>()) {
2020 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00002021 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002022 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002023 if (CT == CT_Can)
2024 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002025 }
2026 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2027 }
2028
2029 case CXXBindTemporaryExprClass: {
2030 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00002031 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002032 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
2033 if (CT == CT_Can)
2034 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00002035 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2036 }
2037
2038 // ObjC message sends are like function calls, but never have exception
2039 // specs.
2040 case ObjCMessageExprClass:
2041 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002042 return CT_Can;
2043
2044 // Many other things have subexpressions, so we have to test those.
2045 // Some are simple:
2046 case ParenExprClass:
2047 case MemberExprClass:
2048 case CXXReinterpretCastExprClass:
2049 case CXXConstCastExprClass:
2050 case ConditionalOperatorClass:
2051 case CompoundLiteralExprClass:
2052 case ExtVectorElementExprClass:
2053 case InitListExprClass:
2054 case DesignatedInitExprClass:
2055 case ParenListExprClass:
2056 case VAArgExprClass:
2057 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00002058 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002059 case ObjCIvarRefExprClass:
2060 case ObjCIsaExprClass:
2061 case ShuffleVectorExprClass:
2062 return CanSubExprsThrow(C, this);
2063
2064 // Some might be dependent for other reasons.
2065 case UnaryOperatorClass:
2066 case ArraySubscriptExprClass:
2067 case ImplicitCastExprClass:
2068 case CStyleCastExprClass:
2069 case CXXStaticCastExprClass:
2070 case CXXFunctionalCastExprClass:
2071 case BinaryOperatorClass:
Douglas Gregor03e80032011-06-21 17:03:29 +00002072 case CompoundAssignOperatorClass:
2073 case MaterializeTemporaryExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002074 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2075 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2076 }
2077
2078 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2079 case StmtExprClass:
2080 return CT_Can;
2081
2082 case ChooseExprClass:
2083 if (isTypeDependent() || isValueDependent())
2084 return CT_Dependent;
2085 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2086
Peter Collingbournef111d932011-04-15 00:35:48 +00002087 case GenericSelectionExprClass:
2088 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2089 return CT_Dependent;
2090 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2091
Sebastian Redl369e51f2010-09-10 20:55:33 +00002092 // Some expressions are always dependent.
2093 case DependentScopeDeclRefExprClass:
2094 case CXXUnresolvedConstructExprClass:
2095 case CXXDependentScopeMemberExprClass:
2096 return CT_Dependent;
2097
2098 default:
2099 // All other expressions don't have subexpressions, or else they are
2100 // unevaluated.
2101 return CT_Cannot;
2102 }
2103}
2104
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002105Expr* Expr::IgnoreParens() {
2106 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002107 while (true) {
2108 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2109 E = P->getSubExpr();
2110 continue;
2111 }
2112 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2113 if (P->getOpcode() == UO_Extension) {
2114 E = P->getSubExpr();
2115 continue;
2116 }
2117 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002118 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2119 if (!P->isResultDependent()) {
2120 E = P->getResultExpr();
2121 continue;
2122 }
2123 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002124 return E;
2125 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002126}
2127
Chris Lattner56f34942008-02-13 01:02:39 +00002128/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2129/// or CastExprs or ImplicitCastExprs, returning their operand.
2130Expr *Expr::IgnoreParenCasts() {
2131 Expr *E = this;
2132 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002133 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002134 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002135 continue;
2136 }
2137 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002138 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002139 continue;
2140 }
2141 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2142 if (P->getOpcode() == UO_Extension) {
2143 E = P->getSubExpr();
2144 continue;
2145 }
2146 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002147 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2148 if (!P->isResultDependent()) {
2149 E = P->getResultExpr();
2150 continue;
2151 }
2152 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002153 if (MaterializeTemporaryExpr *Materialize
2154 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2155 E = Materialize->GetTemporaryExpr();
2156 continue;
2157 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002158 if (SubstNonTypeTemplateParmExpr *NTTP
2159 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2160 E = NTTP->getReplacement();
2161 continue;
2162 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002163 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002164 }
2165}
2166
John McCall9c5d70c2010-12-04 08:24:19 +00002167/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2168/// casts. This is intended purely as a temporary workaround for code
2169/// that hasn't yet been rewritten to do the right thing about those
2170/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002171Expr *Expr::IgnoreParenLValueCasts() {
2172 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002173 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002174 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2175 E = P->getSubExpr();
2176 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002177 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002178 if (P->getCastKind() == CK_LValueToRValue) {
2179 E = P->getSubExpr();
2180 continue;
2181 }
John McCall9c5d70c2010-12-04 08:24:19 +00002182 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2183 if (P->getOpcode() == UO_Extension) {
2184 E = P->getSubExpr();
2185 continue;
2186 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002187 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2188 if (!P->isResultDependent()) {
2189 E = P->getResultExpr();
2190 continue;
2191 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002192 } else if (MaterializeTemporaryExpr *Materialize
2193 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2194 E = Materialize->GetTemporaryExpr();
2195 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002196 } else if (SubstNonTypeTemplateParmExpr *NTTP
2197 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2198 E = NTTP->getReplacement();
2199 continue;
John McCallf6a16482010-12-04 03:47:34 +00002200 }
2201 break;
2202 }
2203 return E;
2204}
2205
John McCall2fc46bf2010-05-05 22:59:52 +00002206Expr *Expr::IgnoreParenImpCasts() {
2207 Expr *E = this;
2208 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002209 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002210 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002211 continue;
2212 }
2213 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002214 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002215 continue;
2216 }
2217 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2218 if (P->getOpcode() == UO_Extension) {
2219 E = P->getSubExpr();
2220 continue;
2221 }
2222 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002223 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2224 if (!P->isResultDependent()) {
2225 E = P->getResultExpr();
2226 continue;
2227 }
2228 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002229 if (MaterializeTemporaryExpr *Materialize
2230 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2231 E = Materialize->GetTemporaryExpr();
2232 continue;
2233 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002234 if (SubstNonTypeTemplateParmExpr *NTTP
2235 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2236 E = NTTP->getReplacement();
2237 continue;
2238 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002239 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002240 }
2241}
2242
Hans Wennborg2f072b42011-06-09 17:06:51 +00002243Expr *Expr::IgnoreConversionOperator() {
2244 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002245 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002246 return MCE->getImplicitObjectArgument();
2247 }
2248 return this;
2249}
2250
Chris Lattnerecdd8412009-03-13 17:28:01 +00002251/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2252/// value (including ptr->int casts of the same size). Strip off any
2253/// ParenExpr or CastExprs, returning their operand.
2254Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2255 Expr *E = this;
2256 while (true) {
2257 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2258 E = P->getSubExpr();
2259 continue;
2260 }
Mike Stump1eb44332009-09-09 15:08:12 +00002261
Chris Lattnerecdd8412009-03-13 17:28:01 +00002262 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2263 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002264 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002265 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002266
Chris Lattnerecdd8412009-03-13 17:28:01 +00002267 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2268 E = SE;
2269 continue;
2270 }
Mike Stump1eb44332009-09-09 15:08:12 +00002271
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002272 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002273 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002274 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002275 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002276 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2277 E = SE;
2278 continue;
2279 }
2280 }
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002282 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2283 if (P->getOpcode() == UO_Extension) {
2284 E = P->getSubExpr();
2285 continue;
2286 }
2287 }
2288
Peter Collingbournef111d932011-04-15 00:35:48 +00002289 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2290 if (!P->isResultDependent()) {
2291 E = P->getResultExpr();
2292 continue;
2293 }
2294 }
2295
Douglas Gregorc0244c52011-09-08 17:56:33 +00002296 if (SubstNonTypeTemplateParmExpr *NTTP
2297 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2298 E = NTTP->getReplacement();
2299 continue;
2300 }
2301
Chris Lattnerecdd8412009-03-13 17:28:01 +00002302 return E;
2303 }
2304}
2305
Douglas Gregor6eef5192009-12-14 19:27:10 +00002306bool Expr::isDefaultArgument() const {
2307 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002308 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2309 E = M->GetTemporaryExpr();
2310
Douglas Gregor6eef5192009-12-14 19:27:10 +00002311 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2312 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002313
Douglas Gregor6eef5192009-12-14 19:27:10 +00002314 return isa<CXXDefaultArgExpr>(E);
2315}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002316
Douglas Gregor2f599792010-04-02 18:24:57 +00002317/// \brief Skip over any no-op casts and any temporary-binding
2318/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002319static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002320 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2321 E = M->GetTemporaryExpr();
2322
Douglas Gregor2f599792010-04-02 18:24:57 +00002323 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002324 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002325 E = ICE->getSubExpr();
2326 else
2327 break;
2328 }
2329
2330 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2331 E = BE->getSubExpr();
2332
2333 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002334 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002335 E = ICE->getSubExpr();
2336 else
2337 break;
2338 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002339
2340 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002341}
2342
John McCall558d2ab2010-09-15 10:14:12 +00002343/// isTemporaryObject - Determines if this expression produces a
2344/// temporary of the given class type.
2345bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2346 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2347 return false;
2348
Anders Carlssonf8b30152010-11-28 16:40:49 +00002349 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002350
John McCall58277b52010-09-15 20:59:13 +00002351 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002352 if (!E->Classify(C).isPRValue()) {
2353 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002354 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002355 return false;
2356 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002357
John McCall19e60ad2010-09-16 06:57:56 +00002358 // Black-list a few cases which yield pr-values of class type that don't
2359 // refer to temporaries of that type:
2360
2361 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002362 if (isa<ImplicitCastExpr>(E)) {
2363 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2364 case CK_DerivedToBase:
2365 case CK_UncheckedDerivedToBase:
2366 return false;
2367 default:
2368 break;
2369 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002370 }
2371
John McCall19e60ad2010-09-16 06:57:56 +00002372 // - member expressions (all)
2373 if (isa<MemberExpr>(E))
2374 return false;
2375
John McCall56ca35d2011-02-17 10:25:35 +00002376 // - opaque values (all)
2377 if (isa<OpaqueValueExpr>(E))
2378 return false;
2379
John McCall558d2ab2010-09-15 10:14:12 +00002380 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002381}
2382
Douglas Gregor75e85042011-03-02 21:06:53 +00002383bool Expr::isImplicitCXXThis() const {
2384 const Expr *E = this;
2385
2386 // Strip away parentheses and casts we don't care about.
2387 while (true) {
2388 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2389 E = Paren->getSubExpr();
2390 continue;
2391 }
2392
2393 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2394 if (ICE->getCastKind() == CK_NoOp ||
2395 ICE->getCastKind() == CK_LValueToRValue ||
2396 ICE->getCastKind() == CK_DerivedToBase ||
2397 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2398 E = ICE->getSubExpr();
2399 continue;
2400 }
2401 }
2402
2403 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2404 if (UnOp->getOpcode() == UO_Extension) {
2405 E = UnOp->getSubExpr();
2406 continue;
2407 }
2408 }
2409
Douglas Gregor03e80032011-06-21 17:03:29 +00002410 if (const MaterializeTemporaryExpr *M
2411 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2412 E = M->GetTemporaryExpr();
2413 continue;
2414 }
2415
Douglas Gregor75e85042011-03-02 21:06:53 +00002416 break;
2417 }
2418
2419 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2420 return This->isImplicit();
2421
2422 return false;
2423}
2424
Douglas Gregor898574e2008-12-05 23:32:09 +00002425/// hasAnyTypeDependentArguments - Determines if any of the expressions
2426/// in Exprs is type-dependent.
2427bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2428 for (unsigned I = 0; I < NumExprs; ++I)
2429 if (Exprs[I]->isTypeDependent())
2430 return true;
2431
2432 return false;
2433}
2434
2435/// hasAnyValueDependentArguments - Determines if any of the expressions
2436/// in Exprs is value-dependent.
2437bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2438 for (unsigned I = 0; I < NumExprs; ++I)
2439 if (Exprs[I]->isValueDependent())
2440 return true;
2441
2442 return false;
2443}
2444
John McCall4204f072010-08-02 21:13:48 +00002445bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002446 // This function is attempting whether an expression is an initializer
2447 // which can be evaluated at compile-time. isEvaluatable handles most
2448 // of the cases, but it can't deal with some initializer-specific
2449 // expressions, and it can't deal with aggregates; we deal with those here,
2450 // and fall back to isEvaluatable for the other cases.
2451
John McCall4204f072010-08-02 21:13:48 +00002452 // If we ever capture reference-binding directly in the AST, we can
2453 // kill the second parameter.
2454
2455 if (IsForRef) {
2456 EvalResult Result;
2457 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2458 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002459
Anders Carlssone8a32b82008-11-24 05:23:59 +00002460 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002461 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002462 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002463 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002464 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002465 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002466 case CXXTemporaryObjectExprClass:
2467 case CXXConstructExprClass: {
2468 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002469
2470 // Only if it's
2471 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002472 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002473 if (!CE->getNumArgs()) return true;
2474
2475 // 2) an elidable trivial copy construction of an operand which is
2476 // itself a constant initializer. Note that we consider the
2477 // operand on its own, *not* as a reference binding.
2478 return CE->isElidable() &&
2479 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002480 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002481 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002482 // This handles gcc's extension that allows global initializers like
2483 // "struct x {int x;} x = (struct x) {};".
2484 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002485 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002486 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002487 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002488 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002489 // FIXME: This doesn't deal with fields with reference types correctly.
2490 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2491 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002492 const InitListExpr *Exp = cast<InitListExpr>(this);
2493 unsigned numInits = Exp->getNumInits();
2494 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002495 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002496 return false;
2497 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002498 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002499 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002500 case ImplicitValueInitExprClass:
2501 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002502 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002503 return cast<ParenExpr>(this)->getSubExpr()
2504 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002505 case GenericSelectionExprClass:
2506 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2507 return false;
2508 return cast<GenericSelectionExpr>(this)->getResultExpr()
2509 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002510 case ChooseExprClass:
2511 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2512 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002513 case UnaryOperatorClass: {
2514 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002515 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002516 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002517 break;
2518 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002519 case BinaryOperatorClass: {
2520 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2521 // but this handles the common case.
2522 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002523 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002524 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2525 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2526 return true;
2527 break;
2528 }
John McCall4204f072010-08-02 21:13:48 +00002529 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002530 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002531 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002532 case CStyleCastExprClass:
2533 // Handle casts with a destination that's a struct or union; this
2534 // deals with both the gcc no-op struct cast extension and the
2535 // cast-to-union extension.
2536 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002537 return cast<CastExpr>(this)->getSubExpr()
2538 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002539
Chris Lattner430656e2009-10-13 22:12:09 +00002540 // Integer->integer casts can be handled here, which is important for
2541 // things like (int)(&&x-&&y). Scary but true.
2542 if (getType()->isIntegerType() &&
2543 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002544 return cast<CastExpr>(this)->getSubExpr()
2545 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002546
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002547 break;
Douglas Gregor03e80032011-06-21 17:03:29 +00002548
2549 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002550 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002551 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002552 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002553 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002554}
2555
Chandler Carruth82214a82011-02-18 23:54:50 +00002556/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2557/// pointer constant or not, as well as the specific kind of constant detected.
2558/// Null pointer constants can be integer constant expressions with the
2559/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2560/// (a GNU extension).
2561Expr::NullPointerConstantKind
2562Expr::isNullPointerConstant(ASTContext &Ctx,
2563 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002564 if (isValueDependent()) {
2565 switch (NPC) {
2566 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002567 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002568 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002569 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2570 return NPCK_ZeroInteger;
2571 else
2572 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002573
Douglas Gregorce940492009-09-25 04:25:58 +00002574 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002575 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002576 }
2577 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002578
Sebastian Redl07779722008-10-31 14:43:28 +00002579 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002580 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002581 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002582 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002583 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002584 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002585 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002586 Pointee->isVoidType() && // to void*
2587 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002588 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002589 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002590 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002591 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2592 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002593 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002594 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2595 // Accept ((void*)0) as a null pointer constant, as many other
2596 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002597 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002598 } else if (const GenericSelectionExpr *GE =
2599 dyn_cast<GenericSelectionExpr>(this)) {
2600 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002601 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002602 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002603 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002604 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002605 } else if (isa<GNUNullExpr>(this)) {
2606 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002607 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002608 } else if (const MaterializeTemporaryExpr *M
2609 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2610 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002611 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2612 if (const Expr *Source = OVE->getSourceExpr())
2613 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002614 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002615
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002616 // C++0x nullptr_t is always a null pointer constant.
2617 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002618 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002619
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002620 if (const RecordType *UT = getType()->getAsUnionType())
2621 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2622 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2623 const Expr *InitExpr = CLE->getInitializer();
2624 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2625 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2626 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002627 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002628 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002629 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002630 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002631
Reid Spencer5f016e22007-07-11 17:01:13 +00002632 // If we have an integer constant expression, we need to *evaluate* it and
2633 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002634 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002635 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2636
2637 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002638}
Steve Naroff31a45842007-07-28 23:10:27 +00002639
John McCallf6a16482010-12-04 03:47:34 +00002640/// \brief If this expression is an l-value for an Objective C
2641/// property, find the underlying property reference expression.
2642const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2643 const Expr *E = this;
2644 while (true) {
2645 assert((E->getValueKind() == VK_LValue &&
2646 E->getObjectKind() == OK_ObjCProperty) &&
2647 "expression is not a property reference");
2648 E = E->IgnoreParenCasts();
2649 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2650 if (BO->getOpcode() == BO_Comma) {
2651 E = BO->getRHS();
2652 continue;
2653 }
2654 }
2655
2656 break;
2657 }
2658
2659 return cast<ObjCPropertyRefExpr>(E);
2660}
2661
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002662FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002663 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002664
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002665 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002666 if (ICE->getCastKind() == CK_LValueToRValue ||
2667 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002668 E = ICE->getSubExpr()->IgnoreParens();
2669 else
2670 break;
2671 }
2672
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002673 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002674 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002675 if (Field->isBitField())
2676 return Field;
2677
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002678 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2679 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2680 if (Field->isBitField())
2681 return Field;
2682
Eli Friedman42068e92011-07-13 02:05:57 +00002683 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002684 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2685 return BinOp->getLHS()->getBitField();
2686
Eli Friedman42068e92011-07-13 02:05:57 +00002687 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2688 return BinOp->getRHS()->getBitField();
2689 }
2690
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002691 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002692}
2693
Anders Carlsson09380262010-01-31 17:18:49 +00002694bool Expr::refersToVectorElement() const {
2695 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002696
Anders Carlsson09380262010-01-31 17:18:49 +00002697 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002698 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002699 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002700 E = ICE->getSubExpr()->IgnoreParens();
2701 else
2702 break;
2703 }
Sean Huntc3021132010-05-05 15:23:54 +00002704
Anders Carlsson09380262010-01-31 17:18:49 +00002705 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2706 return ASE->getBase()->getType()->isVectorType();
2707
2708 if (isa<ExtVectorElementExpr>(E))
2709 return true;
2710
2711 return false;
2712}
2713
Chris Lattner2140e902009-02-16 22:14:05 +00002714/// isArrow - Return true if the base expression is a pointer to vector,
2715/// return false if the base expression is a vector.
2716bool ExtVectorElementExpr::isArrow() const {
2717 return getBase()->getType()->isPointerType();
2718}
2719
Nate Begeman213541a2008-04-18 23:10:10 +00002720unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002721 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002722 return VT->getNumElements();
2723 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002724}
2725
Nate Begeman8a997642008-05-09 06:41:27 +00002726/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002727bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002728 // FIXME: Refactor this code to an accessor on the AST node which returns the
2729 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002730 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002731
2732 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002733 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002734 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002735
Nate Begeman190d6a22009-01-18 02:01:21 +00002736 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002737 if (Comp[0] == 's' || Comp[0] == 'S')
2738 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002739
Daniel Dunbar15027422009-10-17 23:53:04 +00002740 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002741 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002742 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002743
Steve Narofffec0b492007-07-30 03:29:09 +00002744 return false;
2745}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002746
Nate Begeman8a997642008-05-09 06:41:27 +00002747/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002748void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002749 SmallVectorImpl<unsigned> &Elts) const {
2750 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002751 if (Comp[0] == 's' || Comp[0] == 'S')
2752 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002753
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002754 bool isHi = Comp == "hi";
2755 bool isLo = Comp == "lo";
2756 bool isEven = Comp == "even";
2757 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002758
Nate Begeman8a997642008-05-09 06:41:27 +00002759 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2760 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002761
Nate Begeman8a997642008-05-09 06:41:27 +00002762 if (isHi)
2763 Index = e + i;
2764 else if (isLo)
2765 Index = i;
2766 else if (isEven)
2767 Index = 2 * i;
2768 else if (isOdd)
2769 Index = 2 * i + 1;
2770 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002771 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002772
Nate Begeman3b8d1162008-05-13 21:03:02 +00002773 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002774 }
Nate Begeman8a997642008-05-09 06:41:27 +00002775}
2776
Douglas Gregor04badcf2010-04-21 00:45:42 +00002777ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002778 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002779 SourceLocation LBracLoc,
2780 SourceLocation SuperLoc,
2781 bool IsInstanceSuper,
2782 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002783 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002784 ArrayRef<SourceLocation> SelLocs,
2785 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002786 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002787 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002788 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002789 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002790 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002791 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002792 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002793 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2794 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002795 Kind(IsInstanceSuper? SuperInstance : SuperClass),
2796 HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002797 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002798{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002799 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002800 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002801}
2802
Douglas Gregor04badcf2010-04-21 00:45:42 +00002803ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002804 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002805 SourceLocation LBracLoc,
2806 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002807 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002808 ArrayRef<SourceLocation> SelLocs,
2809 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002810 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002811 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002812 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002813 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002814 T->isDependentType(), T->isInstantiationDependentType(),
2815 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002816 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2817 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002818 Kind(Class),
2819 HasMethod(Method != 0), IsDelegateInitCall(false),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002820 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002821{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002822 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002823 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002824}
2825
Douglas Gregor04badcf2010-04-21 00:45:42 +00002826ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002827 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002828 SourceLocation LBracLoc,
2829 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002830 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002831 ArrayRef<SourceLocation> SelLocs,
2832 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002833 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002834 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002835 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002836 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002837 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002838 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002839 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002840 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2841 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002842 Kind(Instance),
2843 HasMethod(Method != 0), IsDelegateInitCall(false),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002844 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002845{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002846 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002847 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002848}
2849
2850void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2851 ArrayRef<SourceLocation> SelLocs,
2852 SelectorLocationsKind SelLocsK) {
2853 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00002854 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002855 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002856 if (Args[I]->isTypeDependent())
2857 ExprBits.TypeDependent = true;
2858 if (Args[I]->isValueDependent())
2859 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002860 if (Args[I]->isInstantiationDependent())
2861 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002862 if (Args[I]->containsUnexpandedParameterPack())
2863 ExprBits.ContainsUnexpandedParameterPack = true;
2864
2865 MyArgs[I] = Args[I];
2866 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002867
2868 SelLocsKind = SelLocsK;
2869 if (SelLocsK == SelLoc_NonStandard)
2870 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
Chris Lattner0389e6b2009-04-26 00:44:05 +00002871}
2872
Douglas Gregor04badcf2010-04-21 00:45:42 +00002873ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002874 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002875 SourceLocation LBracLoc,
2876 SourceLocation SuperLoc,
2877 bool IsInstanceSuper,
2878 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002879 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002880 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002881 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002882 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002883 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002884 SelectorLocationsKind SelLocsK;
2885 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00002886 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002887 SuperType, Sel, SelLocs, SelLocsK,
2888 Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002889}
2890
2891ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002892 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002893 SourceLocation LBracLoc,
2894 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002895 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002896 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002897 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002898 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002899 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002900 SelectorLocationsKind SelLocsK;
2901 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002902 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002903 SelLocs, SelLocsK, Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002904}
2905
2906ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002907 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002908 SourceLocation LBracLoc,
2909 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002910 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002911 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002912 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002913 ArrayRef<Expr *> Args,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002914 SourceLocation RBracLoc) {
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002915 SelectorLocationsKind SelLocsK;
2916 ObjCMessageExpr *Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002917 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002918 SelLocs, SelLocsK, Method, Args, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002919}
2920
Sean Huntc3021132010-05-05 15:23:54 +00002921ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002922 unsigned NumArgs,
2923 unsigned NumStoredSelLocs) {
2924 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002925 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2926}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002927
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002928ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2929 ArrayRef<Expr *> Args,
2930 SourceLocation RBraceLoc,
2931 ArrayRef<SourceLocation> SelLocs,
2932 Selector Sel,
2933 SelectorLocationsKind &SelLocsK) {
2934 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
2935 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
2936 : 0;
2937 return alloc(C, Args.size(), NumStoredSelLocs);
2938}
2939
2940ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2941 unsigned NumArgs,
2942 unsigned NumStoredSelLocs) {
2943 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2944 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
2945 return (ObjCMessageExpr *)C.Allocate(Size,
2946 llvm::AlignOf<ObjCMessageExpr>::Alignment);
2947}
2948
2949void ObjCMessageExpr::getSelectorLocs(
2950 SmallVectorImpl<SourceLocation> &SelLocs) const {
2951 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
2952 SelLocs.push_back(getSelectorLoc(i));
2953}
2954
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002955SourceRange ObjCMessageExpr::getReceiverRange() const {
2956 switch (getReceiverKind()) {
2957 case Instance:
2958 return getInstanceReceiver()->getSourceRange();
2959
2960 case Class:
2961 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2962
2963 case SuperInstance:
2964 case SuperClass:
2965 return getSuperLoc();
2966 }
2967
2968 return SourceLocation();
2969}
2970
Douglas Gregor04badcf2010-04-21 00:45:42 +00002971Selector ObjCMessageExpr::getSelector() const {
2972 if (HasMethod)
2973 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2974 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002975 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002976}
2977
2978ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2979 switch (getReceiverKind()) {
2980 case Instance:
2981 if (const ObjCObjectPointerType *Ptr
2982 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2983 return Ptr->getInterfaceDecl();
2984 break;
2985
2986 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002987 if (const ObjCObjectType *Ty
2988 = getClassReceiver()->getAs<ObjCObjectType>())
2989 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002990 break;
2991
2992 case SuperInstance:
2993 if (const ObjCObjectPointerType *Ptr
2994 = getSuperType()->getAs<ObjCObjectPointerType>())
2995 return Ptr->getInterfaceDecl();
2996 break;
2997
2998 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002999 if (const ObjCObjectType *Iface
3000 = getSuperType()->getAs<ObjCObjectType>())
3001 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003002 break;
3003 }
3004
3005 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003006}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003007
Chris Lattner5f9e2722011-07-23 10:55:15 +00003008StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003009 switch (getBridgeKind()) {
3010 case OBC_Bridge:
3011 return "__bridge";
3012 case OBC_BridgeTransfer:
3013 return "__bridge_transfer";
3014 case OBC_BridgeRetained:
3015 return "__bridge_retained";
3016 }
3017
3018 return "__bridge";
3019}
3020
Jay Foad4ba2a172011-01-12 09:06:06 +00003021bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003022 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003023}
3024
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003025ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3026 QualType Type, SourceLocation BLoc,
3027 SourceLocation RP)
3028 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3029 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003030 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003031 Type->containsUnexpandedParameterPack()),
3032 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3033{
3034 SubExprs = new (C) Stmt*[nexpr];
3035 for (unsigned i = 0; i < nexpr; i++) {
3036 if (args[i]->isTypeDependent())
3037 ExprBits.TypeDependent = true;
3038 if (args[i]->isValueDependent())
3039 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003040 if (args[i]->isInstantiationDependent())
3041 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003042 if (args[i]->containsUnexpandedParameterPack())
3043 ExprBits.ContainsUnexpandedParameterPack = true;
3044
3045 SubExprs[i] = args[i];
3046 }
3047}
3048
Nate Begeman888376a2009-08-12 02:28:50 +00003049void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3050 unsigned NumExprs) {
3051 if (SubExprs) C.Deallocate(SubExprs);
3052
3053 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003054 this->NumExprs = NumExprs;
3055 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003056}
Nate Begeman888376a2009-08-12 02:28:50 +00003057
Peter Collingbournef111d932011-04-15 00:35:48 +00003058GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3059 SourceLocation GenericLoc, Expr *ControllingExpr,
3060 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3061 unsigned NumAssocs, SourceLocation DefaultLoc,
3062 SourceLocation RParenLoc,
3063 bool ContainsUnexpandedParameterPack,
3064 unsigned ResultIndex)
3065 : Expr(GenericSelectionExprClass,
3066 AssocExprs[ResultIndex]->getType(),
3067 AssocExprs[ResultIndex]->getValueKind(),
3068 AssocExprs[ResultIndex]->getObjectKind(),
3069 AssocExprs[ResultIndex]->isTypeDependent(),
3070 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003071 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003072 ContainsUnexpandedParameterPack),
3073 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3074 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3075 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3076 RParenLoc(RParenLoc) {
3077 SubExprs[CONTROLLING] = ControllingExpr;
3078 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3079 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3080}
3081
3082GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3083 SourceLocation GenericLoc, Expr *ControllingExpr,
3084 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3085 unsigned NumAssocs, SourceLocation DefaultLoc,
3086 SourceLocation RParenLoc,
3087 bool ContainsUnexpandedParameterPack)
3088 : Expr(GenericSelectionExprClass,
3089 Context.DependentTy,
3090 VK_RValue,
3091 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003092 /*isTypeDependent=*/true,
3093 /*isValueDependent=*/true,
3094 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003095 ContainsUnexpandedParameterPack),
3096 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3097 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3098 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3099 RParenLoc(RParenLoc) {
3100 SubExprs[CONTROLLING] = ControllingExpr;
3101 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3102 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3103}
3104
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003105//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003106// DesignatedInitExpr
3107//===----------------------------------------------------------------------===//
3108
Chandler Carruthb1138242011-06-16 06:47:06 +00003109IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003110 assert(Kind == FieldDesignator && "Only valid on a field designator");
3111 if (Field.NameOrField & 0x01)
3112 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3113 else
3114 return getField()->getIdentifier();
3115}
3116
Sean Huntc3021132010-05-05 15:23:54 +00003117DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003118 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003119 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003120 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003121 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003122 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003123 unsigned NumIndexExprs,
3124 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003125 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003126 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003127 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003128 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003129 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003130 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3131 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003132 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003133
3134 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003135 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003136 *Child++ = Init;
3137
3138 // Copy the designators and their subexpressions, computing
3139 // value-dependence along the way.
3140 unsigned IndexIdx = 0;
3141 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003142 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003143
3144 if (this->Designators[I].isArrayDesignator()) {
3145 // Compute type- and value-dependence.
3146 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003147 if (Index->isTypeDependent() || Index->isValueDependent())
3148 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003149 if (Index->isInstantiationDependent())
3150 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003151 // Propagate unexpanded parameter packs.
3152 if (Index->containsUnexpandedParameterPack())
3153 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003154
3155 // Copy the index expressions into permanent storage.
3156 *Child++ = IndexExprs[IndexIdx++];
3157 } else if (this->Designators[I].isArrayRangeDesignator()) {
3158 // Compute type- and value-dependence.
3159 Expr *Start = IndexExprs[IndexIdx];
3160 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003161 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003162 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003163 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003164 ExprBits.InstantiationDependent = true;
3165 } else if (Start->isInstantiationDependent() ||
3166 End->isInstantiationDependent()) {
3167 ExprBits.InstantiationDependent = true;
3168 }
3169
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003170 // Propagate unexpanded parameter packs.
3171 if (Start->containsUnexpandedParameterPack() ||
3172 End->containsUnexpandedParameterPack())
3173 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003174
3175 // Copy the start/end expressions into permanent storage.
3176 *Child++ = IndexExprs[IndexIdx++];
3177 *Child++ = IndexExprs[IndexIdx++];
3178 }
3179 }
3180
3181 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003182}
3183
Douglas Gregor05c13a32009-01-22 00:58:24 +00003184DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003185DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003186 unsigned NumDesignators,
3187 Expr **IndexExprs, unsigned NumIndexExprs,
3188 SourceLocation ColonOrEqualLoc,
3189 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003190 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003191 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003192 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003193 ColonOrEqualLoc, UsesColonSyntax,
3194 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003195}
3196
Mike Stump1eb44332009-09-09 15:08:12 +00003197DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003198 unsigned NumIndexExprs) {
3199 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3200 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3201 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3202}
3203
Douglas Gregor319d57f2010-01-06 23:17:19 +00003204void DesignatedInitExpr::setDesignators(ASTContext &C,
3205 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003206 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003207 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003208 NumDesignators = NumDesigs;
3209 for (unsigned I = 0; I != NumDesigs; ++I)
3210 Designators[I] = Desigs[I];
3211}
3212
Abramo Bagnara24f46742011-03-16 15:08:46 +00003213SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3214 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3215 if (size() == 1)
3216 return DIE->getDesignator(0)->getSourceRange();
3217 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3218 DIE->getDesignator(size()-1)->getEndLocation());
3219}
3220
Douglas Gregor05c13a32009-01-22 00:58:24 +00003221SourceRange DesignatedInitExpr::getSourceRange() const {
3222 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003223 Designator &First =
3224 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003225 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003226 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003227 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3228 else
3229 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3230 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003231 StartLoc =
3232 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003233 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3234}
3235
Douglas Gregor05c13a32009-01-22 00:58:24 +00003236Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3237 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3238 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3239 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003240 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3241 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3242}
3243
3244Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003245 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003246 "Requires array range designator");
3247 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3248 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003249 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3250 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3251}
3252
3253Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003254 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003255 "Requires array range designator");
3256 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3257 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003258 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3259 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3260}
3261
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003262/// \brief Replaces the designator at index @p Idx with the series
3263/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003264void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003265 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003266 const Designator *Last) {
3267 unsigned NumNewDesignators = Last - First;
3268 if (NumNewDesignators == 0) {
3269 std::copy_backward(Designators + Idx + 1,
3270 Designators + NumDesignators,
3271 Designators + Idx);
3272 --NumNewDesignators;
3273 return;
3274 } else if (NumNewDesignators == 1) {
3275 Designators[Idx] = *First;
3276 return;
3277 }
3278
Mike Stump1eb44332009-09-09 15:08:12 +00003279 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003280 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003281 std::copy(Designators, Designators + Idx, NewDesignators);
3282 std::copy(First, Last, NewDesignators + Idx);
3283 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3284 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003285 Designators = NewDesignators;
3286 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3287}
3288
Mike Stump1eb44332009-09-09 15:08:12 +00003289ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003290 Expr **exprs, unsigned nexprs,
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003291 SourceLocation rparenloc, QualType T)
3292 : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003293 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003294 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003295 assert(!T.isNull() && "ParenListExpr must have a valid type");
Nate Begeman2ef13e52009-08-10 23:49:36 +00003296 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003297 for (unsigned i = 0; i != nexprs; ++i) {
3298 if (exprs[i]->isTypeDependent())
3299 ExprBits.TypeDependent = true;
3300 if (exprs[i]->isValueDependent())
3301 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003302 if (exprs[i]->isInstantiationDependent())
3303 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003304 if (exprs[i]->containsUnexpandedParameterPack())
3305 ExprBits.ContainsUnexpandedParameterPack = true;
3306
Nate Begeman2ef13e52009-08-10 23:49:36 +00003307 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003308 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003309}
3310
John McCalle996ffd2011-02-16 08:02:54 +00003311const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3312 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3313 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003314 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3315 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003316 e = cast<CXXConstructExpr>(e)->getArg(0);
3317 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3318 e = ice->getSubExpr();
3319 return cast<OpaqueValueExpr>(e);
3320}
3321
John McCall4b9c2d22011-11-06 09:01:30 +00003322PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3323 unsigned numSemanticExprs) {
3324 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3325 (1 + numSemanticExprs) * sizeof(Expr*),
3326 llvm::alignOf<PseudoObjectExpr>());
3327 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3328}
3329
3330PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3331 : Expr(PseudoObjectExprClass, shell) {
3332 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3333}
3334
3335PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3336 ArrayRef<Expr*> semantics,
3337 unsigned resultIndex) {
3338 assert(syntax && "no syntactic expression!");
3339 assert(semantics.size() && "no semantic expressions!");
3340
3341 QualType type;
3342 ExprValueKind VK;
3343 if (resultIndex == NoResult) {
3344 type = C.VoidTy;
3345 VK = VK_RValue;
3346 } else {
3347 assert(resultIndex < semantics.size());
3348 type = semantics[resultIndex]->getType();
3349 VK = semantics[resultIndex]->getValueKind();
3350 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3351 }
3352
3353 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3354 (1 + semantics.size()) * sizeof(Expr*),
3355 llvm::alignOf<PseudoObjectExpr>());
3356 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3357 resultIndex);
3358}
3359
3360PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3361 Expr *syntax, ArrayRef<Expr*> semantics,
3362 unsigned resultIndex)
3363 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3364 /*filled in at end of ctor*/ false, false, false, false) {
3365 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3366 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3367
3368 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3369 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3370 getSubExprsBuffer()[i] = E;
3371
3372 if (E->isTypeDependent())
3373 ExprBits.TypeDependent = true;
3374 if (E->isValueDependent())
3375 ExprBits.ValueDependent = true;
3376 if (E->isInstantiationDependent())
3377 ExprBits.InstantiationDependent = true;
3378 if (E->containsUnexpandedParameterPack())
3379 ExprBits.ContainsUnexpandedParameterPack = true;
3380
3381 if (isa<OpaqueValueExpr>(E))
3382 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3383 "opaque-value semantic expressions for pseudo-object "
3384 "operations must have sources");
3385 }
3386}
3387
Douglas Gregor05c13a32009-01-22 00:58:24 +00003388//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003389// ExprIterator.
3390//===----------------------------------------------------------------------===//
3391
3392Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3393Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3394Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3395const Expr* ConstExprIterator::operator[](size_t idx) const {
3396 return cast<Expr>(I[idx]);
3397}
3398const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3399const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3400
3401//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003402// Child Iterators for iterating over subexpressions/substatements
3403//===----------------------------------------------------------------------===//
3404
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003405// UnaryExprOrTypeTraitExpr
3406Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003407 // If this is of a type and the type is a VLA type (and not a typedef), the
3408 // size expression of the VLA needs to be treated as an executable expression.
3409 // Why isn't this weirdness documented better in StmtIterator?
3410 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003411 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003412 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003413 return child_range(child_iterator(T), child_iterator());
3414 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003415 }
John McCall63c00d72011-02-09 08:16:59 +00003416 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003417}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003418
Steve Naroff563477d2007-09-18 23:55:05 +00003419// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003420Stmt::child_range ObjCMessageExpr::children() {
3421 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003422 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003423 begin = reinterpret_cast<Stmt **>(this + 1);
3424 else
3425 begin = reinterpret_cast<Stmt **>(getArgs());
3426 return child_range(begin,
3427 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003428}
3429
Steve Naroff4eb206b2008-09-03 18:15:37 +00003430// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003431BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003432 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003433 bool constAdded)
Douglas Gregor561f8122011-07-01 01:22:09 +00003434 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003435 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003436 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003437{
Douglas Gregord967e312011-01-19 21:52:31 +00003438 bool TypeDependent = false;
3439 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +00003440 bool InstantiationDependent = false;
3441 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3442 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +00003443 ExprBits.TypeDependent = TypeDependent;
3444 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +00003445 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003446}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003447
3448
3449AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3450 QualType t, AtomicOp op, SourceLocation RP)
3451 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3452 false, false, false, false),
3453 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3454{
3455 for (unsigned i = 0; i < nexpr; i++) {
3456 if (args[i]->isTypeDependent())
3457 ExprBits.TypeDependent = true;
3458 if (args[i]->isValueDependent())
3459 ExprBits.ValueDependent = true;
3460 if (args[i]->isInstantiationDependent())
3461 ExprBits.InstantiationDependent = true;
3462 if (args[i]->containsUnexpandedParameterPack())
3463 ExprBits.ContainsUnexpandedParameterPack = true;
3464
3465 SubExprs[i] = args[i];
3466 }
3467}