blob: 9556b1acb94f9b812f83b8c7fa6ca592efc5c0c5 [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"
Douglas Gregor25d0a0f2012-02-23 07:33:15 +000021#include "clang/AST/EvaluatedExprVisitor.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000024#include "clang/Lex/LiteralSupport.h"
25#include "clang/Lex/Lexer.h"
Richard Smith7a614d82011-06-11 17:19:42 +000026#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000027#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000029#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000030#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000031#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000032#include <algorithm>
Eli Friedman64f45a22011-11-01 02:23:42 +000033#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000034using namespace clang;
35
Chris Lattner2b334bb2010-04-16 23:34:13 +000036/// isKnownToHaveBooleanValue - Return true if this is an integer expression
37/// that is known to return 0 or 1. This happens for _Bool/bool expressions
38/// but also int expressions which are produced by things like comparisons in
39/// C.
40bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000041 const Expr *E = IgnoreParens();
42
Chris Lattner2b334bb2010-04-16 23:34:13 +000043 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000044 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000045 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000046 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000047
Peter Collingbournef111d932011-04-15 00:35:48 +000048 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000049 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000050 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000051 return UO->getSubExpr()->isKnownToHaveBooleanValue();
52 default:
53 return false;
54 }
55 }
Sean Huntc3021132010-05-05 15:23:54 +000056
John McCall6907fbe2010-06-12 01:56:02 +000057 // Only look through implicit casts. If the user writes
58 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000059 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000060 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000061
Peter Collingbournef111d932011-04-15 00:35:48 +000062 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000063 switch (BO->getOpcode()) {
64 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000065 case BO_LT: // Relational operators.
66 case BO_GT:
67 case BO_LE:
68 case BO_GE:
69 case BO_EQ: // Equality operators.
70 case BO_NE:
71 case BO_LAnd: // AND operator.
72 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000073 return true;
Sean Huntc3021132010-05-05 15:23:54 +000074
John McCall2de56d12010-08-25 11:45:40 +000075 case BO_And: // Bitwise AND operator.
76 case BO_Xor: // Bitwise XOR operator.
77 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000078 // Handle things like (x==2)|(y==12).
79 return BO->getLHS()->isKnownToHaveBooleanValue() &&
80 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000081
John McCall2de56d12010-08-25 11:45:40 +000082 case BO_Comma:
83 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000084 return BO->getRHS()->isKnownToHaveBooleanValue();
85 }
86 }
Sean Huntc3021132010-05-05 15:23:54 +000087
Peter Collingbournef111d932011-04-15 00:35:48 +000088 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000089 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
90 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000091
Chris Lattner2b334bb2010-04-16 23:34:13 +000092 return false;
93}
94
John McCall63c00d72011-02-09 08:16:59 +000095// Amusing macro metaprogramming hack: check whether a class provides
96// a more specific implementation of getExprLoc().
Daniel Dunbar90e25a82012-03-09 15:39:19 +000097//
98// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCall63c00d72011-02-09 08:16:59 +000099namespace {
100 /// This implementation is used when a class provides a custom
101 /// implementation of getExprLoc.
102 template <class E, class T>
103 SourceLocation getExprLocImpl(const Expr *expr,
104 SourceLocation (T::*v)() const) {
105 return static_cast<const E*>(expr)->getExprLoc();
106 }
107
108 /// This implementation is used when a class doesn't provide
109 /// a custom implementation of getExprLoc. Overload resolution
110 /// should pick it over the implementation above because it's
111 /// more specialized according to function template partial ordering.
112 template <class E>
113 SourceLocation getExprLocImpl(const Expr *expr,
114 SourceLocation (Expr::*v)() const) {
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000115 return static_cast<const E*>(expr)->getLocStart();
John McCall63c00d72011-02-09 08:16:59 +0000116 }
117}
118
119SourceLocation Expr::getExprLoc() const {
120 switch (getStmtClass()) {
121 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
122#define ABSTRACT_STMT(type)
123#define STMT(type, base) \
124 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
125#define EXPR(type, base) \
126 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
127#include "clang/AST/StmtNodes.inc"
128 }
129 llvm_unreachable("unknown statement kind");
John McCall63c00d72011-02-09 08:16:59 +0000130}
131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132//===----------------------------------------------------------------------===//
133// Primary Expressions.
134//===----------------------------------------------------------------------===//
135
Douglas Gregor561f8122011-07-01 01:22:09 +0000136/// \brief Compute the type-, value-, and instantiation-dependence of a
137/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000138/// based on the declaration being referenced.
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000139static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T,
Douglas Gregord967e312011-01-19 21:52:31 +0000140 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000141 bool &ValueDependent,
142 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000143 TypeDependent = false;
144 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000145 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000146
147 // (TD) C++ [temp.dep.expr]p3:
148 // An id-expression is type-dependent if it contains:
149 //
Sean Huntc3021132010-05-05 15:23:54 +0000150 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000151 //
152 // (VD) C++ [temp.dep.constexpr]p2:
153 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000154
Douglas Gregor0da76df2009-11-23 11:41:28 +0000155 // (TD) - an identifier that was declared with dependent type
156 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000157 if (T->isDependentType()) {
158 TypeDependent = true;
159 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000160 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000161 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000162 } else if (T->isInstantiationDependentType()) {
163 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000164 }
Douglas Gregord967e312011-01-19 21:52:31 +0000165
Douglas Gregor0da76df2009-11-23 11:41:28 +0000166 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000167 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000168 == DeclarationName::CXXConversionFunctionName) {
169 QualType T = D->getDeclName().getCXXNameType();
170 if (T->isDependentType()) {
171 TypeDependent = true;
172 ValueDependent = true;
173 InstantiationDependent = true;
174 return;
175 }
176
177 if (T->isInstantiationDependentType())
178 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000179 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000180
Douglas Gregor0da76df2009-11-23 11:41:28 +0000181 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000182 if (isa<NonTypeTemplateParmDecl>(D)) {
183 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000184 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000185 return;
186 }
187
Douglas Gregor0da76df2009-11-23 11:41:28 +0000188 // (VD) - a constant with integral or enumeration type and is
189 // initialized with an expression that is value-dependent.
Richard Smithdb1822c2011-11-08 01:31:09 +0000190 // (VD) - a constant with literal type and is initialized with an
191 // expression that is value-dependent [C++11].
192 // (VD) - FIXME: Missing from the standard:
193 // - an entity with reference type and is initialized with an
194 // expression that is value-dependent [C++11]
Douglas Gregord967e312011-01-19 21:52:31 +0000195 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000196 if ((Ctx.getLangOpts().CPlusPlus0x ?
Richard Smithdb1822c2011-11-08 01:31:09 +0000197 Var->getType()->isLiteralType() :
198 Var->getType()->isIntegralOrEnumerationType()) &&
199 (Var->getType().getCVRQualifiers() == Qualifiers::Const ||
200 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000201 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000202 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000203 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000204 InstantiationDependent = true;
205 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000206 }
207
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000208 // (VD) - FIXME: Missing from the standard:
209 // - a member function or a static data member of the current
210 // instantiation
Richard Smithdb1822c2011-11-08 01:31:09 +0000211 if (Var->isStaticDataMember() &&
212 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000213 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000214 InstantiationDependent = true;
215 }
Douglas Gregord967e312011-01-19 21:52:31 +0000216
217 return;
218 }
219
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000220 // (VD) - FIXME: Missing from the standard:
221 // - a member function or a static data member of the current
222 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000223 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
224 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000225 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000226 }
Douglas Gregord967e312011-01-19 21:52:31 +0000227}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000228
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000229void DeclRefExpr::computeDependence(ASTContext &Ctx) {
Douglas Gregord967e312011-01-19 21:52:31 +0000230 bool TypeDependent = false;
231 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000232 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000233 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
234 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000235
236 // (TD) C++ [temp.dep.expr]p3:
237 // An id-expression is type-dependent if it contains:
238 //
239 // and
240 //
241 // (VD) C++ [temp.dep.constexpr]p2:
242 // An identifier is value-dependent if it is:
243 if (!TypeDependent && !ValueDependent &&
244 hasExplicitTemplateArgs() &&
245 TemplateSpecializationType::anyDependentTemplateArguments(
246 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000247 getNumTemplateArgs(),
248 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000249 TypeDependent = true;
250 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000251 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000252 }
253
254 ExprBits.TypeDependent = TypeDependent;
255 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000256 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000257
Douglas Gregor10738d32010-12-23 23:51:58 +0000258 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000259 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000260 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000261}
262
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000263DeclRefExpr::DeclRefExpr(ASTContext &Ctx,
264 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000265 SourceLocation TemplateKWLoc,
John McCallf4b88a42012-03-10 09:33:50 +0000266 ValueDecl *D, bool RefersToEnclosingLocal,
267 const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000268 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000269 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000270 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000271 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000272 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
273 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000274 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000275 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000276 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
277 if (FoundD)
278 getInternalFoundDecl() = FoundD;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000279 DeclRefExprBits.HasTemplateKWAndArgsInfo
280 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCallf4b88a42012-03-10 09:33:50 +0000281 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor561f8122011-07-01 01:22:09 +0000282 if (TemplateArgs) {
283 bool Dependent = false;
284 bool InstantiationDependent = false;
285 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000286 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
287 Dependent,
288 InstantiationDependent,
289 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +0000290 if (InstantiationDependent)
291 setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000292 } else if (TemplateKWLoc.isValid()) {
293 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor561f8122011-07-01 01:22:09 +0000294 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000295 DeclRefExprBits.HadMultipleCandidates = 0;
296
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000297 computeDependence(Ctx);
Abramo Bagnara25777432010-08-11 22:01:17 +0000298}
299
Douglas Gregora2813ce2009-10-23 18:54:35 +0000300DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000301 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000302 SourceLocation TemplateKWLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000303 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000304 bool RefersToEnclosingLocal,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000305 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000306 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000307 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000308 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000309 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000310 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000311 RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000312 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000313 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000314}
315
316DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000317 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000318 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000319 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000320 bool RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000321 const DeclarationNameInfo &NameInfo,
322 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000323 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000324 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000325 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000326 // Filter out cases where the found Decl is the same as the value refenenced.
327 if (D == FoundD)
328 FoundD = 0;
329
Douglas Gregora2813ce2009-10-23 18:54:35 +0000330 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000331 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000332 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000333 if (FoundD)
334 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000335 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000336 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
337 else if (TemplateKWLoc.isValid())
338 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000339
Chris Lattner32488542010-10-30 05:14:06 +0000340 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000341 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000342 RefersToEnclosingLocal,
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000343 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000344}
345
Chandler Carruth3aa81402011-05-01 23:48:14 +0000346DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000347 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000348 bool HasFoundDecl,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000349 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000350 unsigned NumTemplateArgs) {
351 std::size_t Size = sizeof(DeclRefExpr);
352 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000353 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000354 if (HasFoundDecl)
355 Size += sizeof(NamedDecl *);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000356 if (HasTemplateKWAndArgsInfo)
357 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000358
Chris Lattner32488542010-10-30 05:14:06 +0000359 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000360 return new (Mem) DeclRefExpr(EmptyShell());
361}
362
Douglas Gregora2813ce2009-10-23 18:54:35 +0000363SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000364 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000365 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000366 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000367 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000368 R.setEnd(getRAngleLoc());
369 return R;
370}
Daniel Dunbar396ec672012-03-09 15:39:15 +0000371SourceLocation DeclRefExpr::getLocStart() const {
372 if (hasQualifier())
373 return getQualifierLoc().getBeginLoc();
374 return getNameInfo().getLocStart();
375}
376SourceLocation DeclRefExpr::getLocEnd() const {
377 if (hasExplicitTemplateArgs())
378 return getRAngleLoc();
379 return getNameInfo().getLocEnd();
380}
Douglas Gregora2813ce2009-10-23 18:54:35 +0000381
Anders Carlsson3a082d82009-09-08 18:24:21 +0000382// FIXME: Maybe this should use DeclPrinter with a special "print predefined
383// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000384std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
385 ASTContext &Context = CurrentDecl->getASTContext();
386
Anders Carlsson3a082d82009-09-08 18:24:21 +0000387 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000388 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000389 return FD->getNameAsString();
390
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000391 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000392 llvm::raw_svector_ostream Out(Name);
393
394 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000395 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000396 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000397 if (MD->isStatic())
398 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000399 }
400
David Blaikie4e4d0842012-03-11 07:00:24 +0000401 PrintingPolicy Policy(Context.getLangOpts());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000402 std::string Proto = FD->getQualifiedNameAsString(Policy);
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000403 llvm::raw_string_ostream POut(Proto);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000404
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000405 const FunctionDecl *Decl = FD;
406 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
407 Decl = Pattern;
408 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000409 const FunctionProtoType *FT = 0;
410 if (FD->hasWrittenPrototype())
411 FT = dyn_cast<FunctionProtoType>(AFT);
412
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000413 POut << "(";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000414 if (FT) {
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000415 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000416 if (i) POut << ", ";
417 std::string Param;
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000418 Decl->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000419 POut << Param;
420 }
421
422 if (FT->isVariadic()) {
423 if (FD->getNumParams()) POut << ", ";
424 POut << "...";
425 }
426 }
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000427 POut << ")";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000428
Sam Weinig4eadcc52009-12-27 01:38:20 +0000429 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
430 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
431 if (ThisQuals.hasConst())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000432 POut << " const";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000433 if (ThisQuals.hasVolatile())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000434 POut << " volatile";
435 RefQualifierKind Ref = MD->getRefQualifier();
436 if (Ref == RQ_LValue)
437 POut << " &";
438 else if (Ref == RQ_RValue)
439 POut << " &&";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000440 }
441
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000442 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
443 SpecsTy Specs;
444 const DeclContext *Ctx = FD->getDeclContext();
445 while (Ctx && isa<NamedDecl>(Ctx)) {
446 const ClassTemplateSpecializationDecl *Spec
447 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
448 if (Spec && !Spec->isExplicitSpecialization())
449 Specs.push_back(Spec);
450 Ctx = Ctx->getParent();
451 }
452
453 std::string TemplateParams;
454 llvm::raw_string_ostream TOut(TemplateParams);
455 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
456 I != E; ++I) {
457 const TemplateParameterList *Params
458 = (*I)->getSpecializedTemplate()->getTemplateParameters();
459 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
460 assert(Params->size() == Args.size());
461 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
462 StringRef Param = Params->getParam(i)->getName();
463 if (Param.empty()) continue;
464 TOut << Param << " = ";
465 Args.get(i).print(Policy, TOut);
466 TOut << ", ";
467 }
468 }
469
470 FunctionTemplateSpecializationInfo *FSI
471 = FD->getTemplateSpecializationInfo();
472 if (FSI && !FSI->isExplicitSpecialization()) {
473 const TemplateParameterList* Params
474 = FSI->getTemplate()->getTemplateParameters();
475 const TemplateArgumentList* Args = FSI->TemplateArguments;
476 assert(Params->size() == Args->size());
477 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
478 StringRef Param = Params->getParam(i)->getName();
479 if (Param.empty()) continue;
480 TOut << Param << " = ";
481 Args->get(i).print(Policy, TOut);
482 TOut << ", ";
483 }
484 }
485
486 TOut.flush();
487 if (!TemplateParams.empty()) {
488 // remove the trailing comma and space
489 TemplateParams.resize(TemplateParams.size() - 2);
490 POut << " [" << TemplateParams << "]";
491 }
492
493 POut.flush();
494
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000495 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
496 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000497
498 Out << Proto;
499
500 Out.flush();
501 return Name.str().str();
502 }
503 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000504 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000505 llvm::raw_svector_ostream Out(Name);
506 Out << (MD->isInstanceMethod() ? '-' : '+');
507 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000508
509 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
510 // a null check to avoid a crash.
511 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000512 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000513
Anders Carlsson3a082d82009-09-08 18:24:21 +0000514 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000515 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000516 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000517
Anders Carlsson3a082d82009-09-08 18:24:21 +0000518 Out << ' ';
519 Out << MD->getSelector().getAsString();
520 Out << ']';
521
522 Out.flush();
523 return Name.str().str();
524 }
525 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
526 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
527 return "top level";
528 }
529 return "";
530}
531
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000532void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
533 if (hasAllocation())
534 C.Deallocate(pVal);
535
536 BitWidth = Val.getBitWidth();
537 unsigned NumWords = Val.getNumWords();
538 const uint64_t* Words = Val.getRawData();
539 if (NumWords > 1) {
540 pVal = new (C) uint64_t[NumWords];
541 std::copy(Words, Words + NumWords, pVal);
542 } else if (NumWords == 1)
543 VAL = Words[0];
544 else
545 VAL = 0;
546}
547
548IntegerLiteral *
549IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
550 QualType type, SourceLocation l) {
551 return new (C) IntegerLiteral(C, V, type, l);
552}
553
554IntegerLiteral *
555IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
556 return new (C) IntegerLiteral(Empty);
557}
558
559FloatingLiteral *
560FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
561 bool isexact, QualType Type, SourceLocation L) {
562 return new (C) FloatingLiteral(C, V, isexact, Type, L);
563}
564
565FloatingLiteral *
566FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000567 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000568}
569
Chris Lattnerda8249e2008-06-07 22:13:43 +0000570/// getValueAsApproximateDouble - This returns the value as an inaccurate
571/// double. Note that this may cause loss of precision, but is useful for
572/// debugging dumps, etc.
573double FloatingLiteral::getValueAsApproximateDouble() const {
574 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000575 bool ignored;
576 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
577 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000578 return V.convertToDouble();
579}
580
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000581int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000582 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000583 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000584 case Ascii:
585 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000586 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000587 break;
588 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000589 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000590 break;
591 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000592 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000593 break;
594 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000595 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000596 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000597 }
598 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
599 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000600 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000601 && "character byte widths supported are 1, 2, and 4 only");
602 return CharByteWidth;
603}
604
Chris Lattner5f9e2722011-07-23 10:55:15 +0000605StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000606 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000607 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000608 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000609 // Allocate enough space for the StringLiteral plus an array of locations for
610 // any concatenated string tokens.
611 void *Mem = C.Allocate(sizeof(StringLiteral)+
612 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000613 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000614 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000617 SL->setString(C,Str,Kind,Pascal);
618
Chris Lattner2085fd62009-02-18 06:40:38 +0000619 SL->TokLocs[0] = Loc[0];
620 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000621
Chris Lattner726e1682009-02-18 05:49:11 +0000622 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000623 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
624 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000625}
626
Douglas Gregor673ecd62009-04-15 16:35:07 +0000627StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
628 void *Mem = C.Allocate(sizeof(StringLiteral)+
629 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000630 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000631 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000632 SL->CharByteWidth = 0;
633 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000634 SL->NumConcatenated = NumStrs;
635 return SL;
636}
637
Eli Friedman64f45a22011-11-01 02:23:42 +0000638void StringLiteral::setString(ASTContext &C, StringRef Str,
639 StringKind Kind, bool IsPascal) {
640 //FIXME: we assume that the string data comes from a target that uses the same
641 // code unit size and endianess for the type of string.
642 this->Kind = Kind;
643 this->IsPascal = IsPascal;
644
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000645 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000646 assert((Str.size()%CharByteWidth == 0)
647 && "size of data must be multiple of CharByteWidth");
648 Length = Str.size()/CharByteWidth;
649
650 switch(CharByteWidth) {
651 case 1: {
652 char *AStrData = new (C) char[Length];
653 std::memcpy(AStrData,Str.data(),Str.size());
654 StrData.asChar = AStrData;
655 break;
656 }
657 case 2: {
658 uint16_t *AStrData = new (C) uint16_t[Length];
659 std::memcpy(AStrData,Str.data(),Str.size());
660 StrData.asUInt16 = AStrData;
661 break;
662 }
663 case 4: {
664 uint32_t *AStrData = new (C) uint32_t[Length];
665 std::memcpy(AStrData,Str.data(),Str.size());
666 StrData.asUInt32 = AStrData;
667 break;
668 }
669 default:
670 assert(false && "unsupported CharByteWidth");
671 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000672}
673
Chris Lattner08f92e32010-11-17 07:37:15 +0000674/// getLocationOfByte - Return a source location that points to the specified
675/// byte of this string literal.
676///
677/// Strings are amazingly complex. They can be formed from multiple tokens and
678/// can have escape sequences in them in addition to the usual trigraph and
679/// escaped newline business. This routine handles this complexity.
680///
681SourceLocation StringLiteral::
682getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
683 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000684 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
685
Chris Lattner08f92e32010-11-17 07:37:15 +0000686 // Loop over all of the tokens in this string until we find the one that
687 // contains the byte we're looking for.
688 unsigned TokNo = 0;
689 while (1) {
690 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
691 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
692
693 // Get the spelling of the string so that we can get the data that makes up
694 // the string literal, not the identifier for the macro it is potentially
695 // expanded through.
696 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
697
698 // Re-lex the token to get its length and original spelling.
699 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
700 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000701 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000702 if (Invalid)
703 return StrTokSpellingLoc;
704
705 const char *StrData = Buffer.data()+LocInfo.second;
706
707 // Create a langops struct and enable trigraphs. This is sufficient for
708 // relexing tokens.
709 LangOptions LangOpts;
710 LangOpts.Trigraphs = true;
711
712 // Create a lexer starting at the beginning of this token.
713 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
714 Buffer.end());
715 Token TheTok;
716 TheLexer.LexFromRawLexer(TheTok);
717
718 // Use the StringLiteralParser to compute the length of the string in bytes.
719 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
720 unsigned TokNumBytes = SLP.GetStringLength();
721
722 // If the byte is in this token, return the location of the byte.
723 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000724 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000725 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
726
727 // Now that we know the offset of the token in the spelling, use the
728 // preprocessor to get the offset in the original source.
729 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
730 }
731
732 // Move to the next string token.
733 ++TokNo;
734 ByteNo -= TokNumBytes;
735 }
736}
737
738
739
Reid Spencer5f016e22007-07-11 17:01:13 +0000740/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
741/// corresponds to, e.g. "sizeof" or "[pre]++".
742const char *UnaryOperator::getOpcodeStr(Opcode Op) {
743 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000744 case UO_PostInc: return "++";
745 case UO_PostDec: return "--";
746 case UO_PreInc: return "++";
747 case UO_PreDec: return "--";
748 case UO_AddrOf: return "&";
749 case UO_Deref: return "*";
750 case UO_Plus: return "+";
751 case UO_Minus: return "-";
752 case UO_Not: return "~";
753 case UO_LNot: return "!";
754 case UO_Real: return "__real";
755 case UO_Imag: return "__imag";
756 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000757 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000758 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000759}
760
John McCall2de56d12010-08-25 11:45:40 +0000761UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000762UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
763 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000764 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000765 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
766 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
767 case OO_Amp: return UO_AddrOf;
768 case OO_Star: return UO_Deref;
769 case OO_Plus: return UO_Plus;
770 case OO_Minus: return UO_Minus;
771 case OO_Tilde: return UO_Not;
772 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000773 }
774}
775
776OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
777 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000778 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
779 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
780 case UO_AddrOf: return OO_Amp;
781 case UO_Deref: return OO_Star;
782 case UO_Plus: return OO_Plus;
783 case UO_Minus: return OO_Minus;
784 case UO_Not: return OO_Tilde;
785 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000786 default: return OO_None;
787 }
788}
789
790
Reid Spencer5f016e22007-07-11 17:01:13 +0000791//===----------------------------------------------------------------------===//
792// Postfix Operators.
793//===----------------------------------------------------------------------===//
794
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000795CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
796 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000797 SourceLocation rparenloc)
798 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000799 fn->isTypeDependent(),
800 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000801 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000802 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000803 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000804
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000805 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000806 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000807 for (unsigned i = 0; i != numargs; ++i) {
808 if (args[i]->isTypeDependent())
809 ExprBits.TypeDependent = true;
810 if (args[i]->isValueDependent())
811 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000812 if (args[i]->isInstantiationDependent())
813 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000814 if (args[i]->containsUnexpandedParameterPack())
815 ExprBits.ContainsUnexpandedParameterPack = true;
816
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000817 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000818 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000819
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000820 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000821 RParenLoc = rparenloc;
822}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000823
Ted Kremenek668bf912009-02-09 20:51:47 +0000824CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000825 QualType t, ExprValueKind VK, SourceLocation rparenloc)
826 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000827 fn->isTypeDependent(),
828 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000829 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000830 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000831 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000832
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000833 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000834 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000835 for (unsigned i = 0; i != numargs; ++i) {
836 if (args[i]->isTypeDependent())
837 ExprBits.TypeDependent = true;
838 if (args[i]->isValueDependent())
839 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000840 if (args[i]->isInstantiationDependent())
841 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000842 if (args[i]->containsUnexpandedParameterPack())
843 ExprBits.ContainsUnexpandedParameterPack = true;
844
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000845 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000846 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000847
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000848 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 RParenLoc = rparenloc;
850}
851
Mike Stump1eb44332009-09-09 15:08:12 +0000852CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
853 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000854 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000855 SubExprs = new (C) Stmt*[PREARGS_START];
856 CallExprBits.NumPreArgs = 0;
857}
858
859CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
860 EmptyShell Empty)
861 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
862 // FIXME: Why do we allocate this?
863 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
864 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000865}
866
Nuno Lopesd20254f2009-12-20 23:11:08 +0000867Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000868 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000869
870 while (SubstNonTypeTemplateParmExpr *NTTP
871 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
872 CEE = NTTP->getReplacement()->IgnoreParenCasts();
873 }
874
Sebastian Redl20012152010-09-10 20:55:30 +0000875 // If we're calling a dereference, look at the pointer instead.
876 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
877 if (BO->isPtrMemOp())
878 CEE = BO->getRHS()->IgnoreParenCasts();
879 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
880 if (UO->getOpcode() == UO_Deref)
881 CEE = UO->getSubExpr()->IgnoreParenCasts();
882 }
Chris Lattner6346f962009-07-17 15:46:27 +0000883 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000884 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000885 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
886 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000887
888 return 0;
889}
890
Nuno Lopesd20254f2009-12-20 23:11:08 +0000891FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000892 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000893}
894
Chris Lattnerd18b3292007-12-28 05:25:02 +0000895/// setNumArgs - This changes the number of arguments present in this call.
896/// Any orphaned expressions are deleted by this, and any new operands are set
897/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000898void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000899 // No change, just return.
900 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Chris Lattnerd18b3292007-12-28 05:25:02 +0000902 // If shrinking # arguments, just delete the extras and forgot them.
903 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000904 this->NumArgs = NumArgs;
905 return;
906 }
907
908 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000909 unsigned NumPreArgs = getNumPreArgs();
910 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000911 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000912 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000913 NewSubExprs[i] = SubExprs[i];
914 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000915 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
916 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000917 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000918
Douglas Gregor88c9a462009-04-17 21:46:47 +0000919 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000920 SubExprs = NewSubExprs;
921 this->NumArgs = NumArgs;
922}
923
Chris Lattnercb888962008-10-06 05:00:53 +0000924/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
925/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +0000926unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000927 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000928 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000929 // ImplicitCastExpr.
930 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
931 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000932 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000934 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
935 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000936 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Anders Carlssonbcba2012008-01-31 02:13:57 +0000938 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
939 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000940 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000942 if (!FDecl->getIdentifier())
943 return 0;
944
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000945 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000946}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000947
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000948QualType CallExpr::getCallReturnType() const {
949 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000950 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000951 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000952 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000953 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000954 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
955 // This should never be overloaded and so should never return null.
956 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000957
John McCall864c0412011-04-26 20:42:42 +0000958 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000959 return FnType->getResultType();
960}
Chris Lattnercb888962008-10-06 05:00:53 +0000961
John McCall2882eca2011-02-21 06:23:05 +0000962SourceRange CallExpr::getSourceRange() const {
963 if (isa<CXXOperatorCallExpr>(this))
964 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
965
966 SourceLocation begin = getCallee()->getLocStart();
967 if (begin.isInvalid() && getNumArgs() > 0)
968 begin = getArg(0)->getLocStart();
969 SourceLocation end = getRParenLoc();
970 if (end.isInvalid() && getNumArgs() > 0)
971 end = getArg(getNumArgs() - 1)->getLocEnd();
972 return SourceRange(begin, end);
973}
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +0000974SourceLocation CallExpr::getLocStart() const {
975 if (isa<CXXOperatorCallExpr>(this))
976 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getBegin();
977
978 SourceLocation begin = getCallee()->getLocStart();
979 if (begin.isInvalid() && getNumArgs() > 0)
980 begin = getArg(0)->getLocStart();
981 return begin;
982}
983SourceLocation CallExpr::getLocEnd() const {
984 if (isa<CXXOperatorCallExpr>(this))
985 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getEnd();
986
987 SourceLocation end = getRParenLoc();
988 if (end.isInvalid() && getNumArgs() > 0)
989 end = getArg(getNumArgs() - 1)->getLocEnd();
990 return end;
991}
John McCall2882eca2011-02-21 06:23:05 +0000992
Sean Huntc3021132010-05-05 15:23:54 +0000993OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000994 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000995 TypeSourceInfo *tsi,
996 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000997 Expr** exprsPtr, unsigned numExprs,
998 SourceLocation RParenLoc) {
999 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +00001000 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001001 sizeof(Expr*) * numExprs);
1002
1003 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
1004 exprsPtr, numExprs, RParenLoc);
1005}
1006
1007OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
1008 unsigned numComps, unsigned numExprs) {
1009 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1010 sizeof(OffsetOfNode) * numComps +
1011 sizeof(Expr*) * numExprs);
1012 return new (Mem) OffsetOfExpr(numComps, numExprs);
1013}
1014
Sean Huntc3021132010-05-05 15:23:54 +00001015OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001016 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +00001017 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001018 Expr** exprsPtr, unsigned numExprs,
1019 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001020 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1021 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001022 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001023 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001024 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001025 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
1026 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001027{
1028 for(unsigned i = 0; i < numComps; ++i) {
1029 setComponent(i, compsPtr[i]);
1030 }
Sean Huntc3021132010-05-05 15:23:54 +00001031
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001032 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001033 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
1034 ExprBits.ValueDependent = true;
1035 if (exprsPtr[i]->containsUnexpandedParameterPack())
1036 ExprBits.ContainsUnexpandedParameterPack = true;
1037
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001038 setIndexExpr(i, exprsPtr[i]);
1039 }
1040}
1041
1042IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1043 assert(getKind() == Field || getKind() == Identifier);
1044 if (getKind() == Field)
1045 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001046
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001047 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1048}
1049
Mike Stump1eb44332009-09-09 15:08:12 +00001050MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001051 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001052 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001053 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001054 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001055 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001056 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001057 QualType ty,
1058 ExprValueKind vk,
1059 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001060 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001061
Douglas Gregor40d96a62011-02-28 21:54:11 +00001062 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001063 founddecl.getDecl() != memberdecl ||
1064 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001065 if (hasQualOrFound)
1066 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001067
John McCalld5532b62009-11-23 01:53:49 +00001068 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001069 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1070 else if (TemplateKWLoc.isValid())
1071 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Chris Lattner32488542010-10-30 05:14:06 +00001073 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001074 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1075 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001076
1077 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001078 // FIXME: Wrong. We should be looking at the member declaration we found.
1079 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001080 E->setValueDependent(true);
1081 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001082 E->setInstantiationDependent(true);
1083 }
1084 else if (QualifierLoc &&
1085 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1086 E->setInstantiationDependent(true);
1087
John McCall6bb80172010-03-30 21:47:33 +00001088 E->HasQualifierOrFoundDecl = true;
1089
1090 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001091 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001092 NQ->FoundDecl = founddecl;
1093 }
1094
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001095 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1096
John McCall6bb80172010-03-30 21:47:33 +00001097 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001098 bool Dependent = false;
1099 bool InstantiationDependent = false;
1100 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001101 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1102 Dependent,
1103 InstantiationDependent,
1104 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001105 if (InstantiationDependent)
1106 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001107 } else if (TemplateKWLoc.isValid()) {
1108 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001109 }
1110
1111 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001112}
1113
Douglas Gregor75e85042011-03-02 21:06:53 +00001114SourceRange MemberExpr::getSourceRange() const {
Daniel Dunbar396ec672012-03-09 15:39:15 +00001115 return SourceRange(getLocStart(), getLocEnd());
1116}
1117SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001118 if (isImplicitAccess()) {
1119 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001120 return getQualifierLoc().getBeginLoc();
1121 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001122 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001123
Daniel Dunbar396ec672012-03-09 15:39:15 +00001124 // FIXME: We don't want this to happen. Rather, we should be able to
1125 // detect all kinds of implicit accesses more cleanly.
1126 SourceLocation BaseStartLoc = getBase()->getLocStart();
1127 if (BaseStartLoc.isValid())
1128 return BaseStartLoc;
1129 return MemberLoc;
1130}
1131SourceLocation MemberExpr::getLocEnd() const {
1132 if (hasExplicitTemplateArgs())
1133 return getRAngleLoc();
1134 return getMemberNameInfo().getEndLoc();
Douglas Gregor75e85042011-03-02 21:06:53 +00001135}
1136
John McCall1d9b3b22011-09-09 05:25:32 +00001137void CastExpr::CheckCastConsistency() const {
1138 switch (getCastKind()) {
1139 case CK_DerivedToBase:
1140 case CK_UncheckedDerivedToBase:
1141 case CK_DerivedToBaseMemberPointer:
1142 case CK_BaseToDerived:
1143 case CK_BaseToDerivedMemberPointer:
1144 assert(!path_empty() && "Cast kind should have a base path!");
1145 break;
1146
1147 case CK_CPointerToObjCPointerCast:
1148 assert(getType()->isObjCObjectPointerType());
1149 assert(getSubExpr()->getType()->isPointerType());
1150 goto CheckNoBasePath;
1151
1152 case CK_BlockPointerToObjCPointerCast:
1153 assert(getType()->isObjCObjectPointerType());
1154 assert(getSubExpr()->getType()->isBlockPointerType());
1155 goto CheckNoBasePath;
1156
John McCall4d4e5c12012-02-15 01:22:51 +00001157 case CK_ReinterpretMemberPointer:
1158 assert(getType()->isMemberPointerType());
1159 assert(getSubExpr()->getType()->isMemberPointerType());
1160 goto CheckNoBasePath;
1161
John McCall1d9b3b22011-09-09 05:25:32 +00001162 case CK_BitCast:
1163 // Arbitrary casts to C pointer types count as bitcasts.
1164 // Otherwise, we should only have block and ObjC pointer casts
1165 // here if they stay within the type kind.
1166 if (!getType()->isPointerType()) {
1167 assert(getType()->isObjCObjectPointerType() ==
1168 getSubExpr()->getType()->isObjCObjectPointerType());
1169 assert(getType()->isBlockPointerType() ==
1170 getSubExpr()->getType()->isBlockPointerType());
1171 }
1172 goto CheckNoBasePath;
1173
1174 case CK_AnyPointerToBlockPointerCast:
1175 assert(getType()->isBlockPointerType());
1176 assert(getSubExpr()->getType()->isAnyPointerType() &&
1177 !getSubExpr()->getType()->isBlockPointerType());
1178 goto CheckNoBasePath;
1179
Douglas Gregorac1303e2012-02-22 05:02:47 +00001180 case CK_CopyAndAutoreleaseBlockObject:
1181 assert(getType()->isBlockPointerType());
1182 assert(getSubExpr()->getType()->isBlockPointerType());
1183 goto CheckNoBasePath;
1184
John McCall1d9b3b22011-09-09 05:25:32 +00001185 // These should not have an inheritance path.
1186 case CK_Dynamic:
1187 case CK_ToUnion:
1188 case CK_ArrayToPointerDecay:
1189 case CK_FunctionToPointerDecay:
1190 case CK_NullToMemberPointer:
1191 case CK_NullToPointer:
1192 case CK_ConstructorConversion:
1193 case CK_IntegralToPointer:
1194 case CK_PointerToIntegral:
1195 case CK_ToVoid:
1196 case CK_VectorSplat:
1197 case CK_IntegralCast:
1198 case CK_IntegralToFloating:
1199 case CK_FloatingToIntegral:
1200 case CK_FloatingCast:
1201 case CK_ObjCObjectLValueCast:
1202 case CK_FloatingRealToComplex:
1203 case CK_FloatingComplexToReal:
1204 case CK_FloatingComplexCast:
1205 case CK_FloatingComplexToIntegralComplex:
1206 case CK_IntegralRealToComplex:
1207 case CK_IntegralComplexToReal:
1208 case CK_IntegralComplexCast:
1209 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001210 case CK_ARCProduceObject:
1211 case CK_ARCConsumeObject:
1212 case CK_ARCReclaimReturnedObject:
1213 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001214 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1215 goto CheckNoBasePath;
1216
1217 case CK_Dependent:
1218 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001219 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001220 case CK_AtomicToNonAtomic:
1221 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001222 case CK_PointerToBoolean:
1223 case CK_IntegralToBoolean:
1224 case CK_FloatingToBoolean:
1225 case CK_MemberPointerToBoolean:
1226 case CK_FloatingComplexToBoolean:
1227 case CK_IntegralComplexToBoolean:
1228 case CK_LValueBitCast: // -> bool&
1229 case CK_UserDefinedConversion: // operator bool()
1230 CheckNoBasePath:
1231 assert(path_empty() && "Cast kind should not have a base path!");
1232 break;
1233 }
1234}
1235
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001236const char *CastExpr::getCastKindName() const {
1237 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001238 case CK_Dependent:
1239 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001240 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001241 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001242 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001243 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001244 case CK_LValueToRValue:
1245 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001246 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001247 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001248 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001249 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001250 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001251 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001252 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001253 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001254 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001255 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001256 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001257 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001258 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001259 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001260 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001261 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001262 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001263 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001264 case CK_NullToPointer:
1265 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001266 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001267 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001268 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001269 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001270 case CK_ReinterpretMemberPointer:
1271 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001272 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001273 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001274 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001275 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001276 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001277 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001278 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001279 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001280 case CK_PointerToBoolean:
1281 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001282 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001283 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001284 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001285 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001286 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001287 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001288 case CK_IntegralToBoolean:
1289 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001290 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001291 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001292 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001293 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001294 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001295 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001296 case CK_FloatingToBoolean:
1297 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001298 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001299 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001300 case CK_CPointerToObjCPointerCast:
1301 return "CPointerToObjCPointerCast";
1302 case CK_BlockPointerToObjCPointerCast:
1303 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001304 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001305 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001306 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001307 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001308 case CK_FloatingRealToComplex:
1309 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001310 case CK_FloatingComplexToReal:
1311 return "FloatingComplexToReal";
1312 case CK_FloatingComplexToBoolean:
1313 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001314 case CK_FloatingComplexCast:
1315 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001316 case CK_FloatingComplexToIntegralComplex:
1317 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001318 case CK_IntegralRealToComplex:
1319 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001320 case CK_IntegralComplexToReal:
1321 return "IntegralComplexToReal";
1322 case CK_IntegralComplexToBoolean:
1323 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001324 case CK_IntegralComplexCast:
1325 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001326 case CK_IntegralComplexToFloatingComplex:
1327 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001328 case CK_ARCConsumeObject:
1329 return "ARCConsumeObject";
1330 case CK_ARCProduceObject:
1331 return "ARCProduceObject";
1332 case CK_ARCReclaimReturnedObject:
1333 return "ARCReclaimReturnedObject";
1334 case CK_ARCExtendBlockObject:
1335 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001336 case CK_AtomicToNonAtomic:
1337 return "AtomicToNonAtomic";
1338 case CK_NonAtomicToAtomic:
1339 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001340 case CK_CopyAndAutoreleaseBlockObject:
1341 return "CopyAndAutoreleaseBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001342 }
Mike Stump1eb44332009-09-09 15:08:12 +00001343
John McCall2bb5d002010-11-13 09:02:35 +00001344 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001345}
1346
Douglas Gregor6eef5192009-12-14 19:27:10 +00001347Expr *CastExpr::getSubExprAsWritten() {
1348 Expr *SubExpr = 0;
1349 CastExpr *E = this;
1350 do {
1351 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001352
1353 // Skip through reference binding to temporary.
1354 if (MaterializeTemporaryExpr *Materialize
1355 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1356 SubExpr = Materialize->GetTemporaryExpr();
1357
Douglas Gregor6eef5192009-12-14 19:27:10 +00001358 // Skip any temporary bindings; they're implicit.
1359 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1360 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001361
Douglas Gregor6eef5192009-12-14 19:27:10 +00001362 // Conversions by constructor and conversion functions have a
1363 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001364 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001365 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001366 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001367 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001368
Douglas Gregor6eef5192009-12-14 19:27:10 +00001369 // If the subexpression we're left with is an implicit cast, look
1370 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001371 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1372
Douglas Gregor6eef5192009-12-14 19:27:10 +00001373 return SubExpr;
1374}
1375
John McCallf871d0c2010-08-07 06:22:56 +00001376CXXBaseSpecifier **CastExpr::path_buffer() {
1377 switch (getStmtClass()) {
1378#define ABSTRACT_STMT(x)
1379#define CASTEXPR(Type, Base) \
1380 case Stmt::Type##Class: \
1381 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1382#define STMT(Type, Base)
1383#include "clang/AST/StmtNodes.inc"
1384 default:
1385 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001386 }
1387}
1388
1389void CastExpr::setCastPath(const CXXCastPath &Path) {
1390 assert(Path.size() == path_size());
1391 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1392}
1393
1394ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1395 CastKind Kind, Expr *Operand,
1396 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001397 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001398 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1399 void *Buffer =
1400 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1401 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001402 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001403 if (PathSize) E->setCastPath(*BasePath);
1404 return E;
1405}
1406
1407ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1408 unsigned PathSize) {
1409 void *Buffer =
1410 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1411 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1412}
1413
1414
1415CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001416 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001417 const CXXCastPath *BasePath,
1418 TypeSourceInfo *WrittenTy,
1419 SourceLocation L, SourceLocation R) {
1420 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1421 void *Buffer =
1422 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1423 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001424 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001425 if (PathSize) E->setCastPath(*BasePath);
1426 return E;
1427}
1428
1429CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1430 void *Buffer =
1431 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1432 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1433}
1434
Reid Spencer5f016e22007-07-11 17:01:13 +00001435/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1436/// corresponds to, e.g. "<<=".
1437const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1438 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001439 case BO_PtrMemD: return ".*";
1440 case BO_PtrMemI: return "->*";
1441 case BO_Mul: return "*";
1442 case BO_Div: return "/";
1443 case BO_Rem: return "%";
1444 case BO_Add: return "+";
1445 case BO_Sub: return "-";
1446 case BO_Shl: return "<<";
1447 case BO_Shr: return ">>";
1448 case BO_LT: return "<";
1449 case BO_GT: return ">";
1450 case BO_LE: return "<=";
1451 case BO_GE: return ">=";
1452 case BO_EQ: return "==";
1453 case BO_NE: return "!=";
1454 case BO_And: return "&";
1455 case BO_Xor: return "^";
1456 case BO_Or: return "|";
1457 case BO_LAnd: return "&&";
1458 case BO_LOr: return "||";
1459 case BO_Assign: return "=";
1460 case BO_MulAssign: return "*=";
1461 case BO_DivAssign: return "/=";
1462 case BO_RemAssign: return "%=";
1463 case BO_AddAssign: return "+=";
1464 case BO_SubAssign: return "-=";
1465 case BO_ShlAssign: return "<<=";
1466 case BO_ShrAssign: return ">>=";
1467 case BO_AndAssign: return "&=";
1468 case BO_XorAssign: return "^=";
1469 case BO_OrAssign: return "|=";
1470 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001471 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001472
David Blaikie30263482012-01-20 21:50:17 +00001473 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001474}
1475
John McCall2de56d12010-08-25 11:45:40 +00001476BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001477BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1478 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001479 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001480 case OO_Plus: return BO_Add;
1481 case OO_Minus: return BO_Sub;
1482 case OO_Star: return BO_Mul;
1483 case OO_Slash: return BO_Div;
1484 case OO_Percent: return BO_Rem;
1485 case OO_Caret: return BO_Xor;
1486 case OO_Amp: return BO_And;
1487 case OO_Pipe: return BO_Or;
1488 case OO_Equal: return BO_Assign;
1489 case OO_Less: return BO_LT;
1490 case OO_Greater: return BO_GT;
1491 case OO_PlusEqual: return BO_AddAssign;
1492 case OO_MinusEqual: return BO_SubAssign;
1493 case OO_StarEqual: return BO_MulAssign;
1494 case OO_SlashEqual: return BO_DivAssign;
1495 case OO_PercentEqual: return BO_RemAssign;
1496 case OO_CaretEqual: return BO_XorAssign;
1497 case OO_AmpEqual: return BO_AndAssign;
1498 case OO_PipeEqual: return BO_OrAssign;
1499 case OO_LessLess: return BO_Shl;
1500 case OO_GreaterGreater: return BO_Shr;
1501 case OO_LessLessEqual: return BO_ShlAssign;
1502 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1503 case OO_EqualEqual: return BO_EQ;
1504 case OO_ExclaimEqual: return BO_NE;
1505 case OO_LessEqual: return BO_LE;
1506 case OO_GreaterEqual: return BO_GE;
1507 case OO_AmpAmp: return BO_LAnd;
1508 case OO_PipePipe: return BO_LOr;
1509 case OO_Comma: return BO_Comma;
1510 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001511 }
1512}
1513
1514OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1515 static const OverloadedOperatorKind OverOps[] = {
1516 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1517 OO_Star, OO_Slash, OO_Percent,
1518 OO_Plus, OO_Minus,
1519 OO_LessLess, OO_GreaterGreater,
1520 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1521 OO_EqualEqual, OO_ExclaimEqual,
1522 OO_Amp,
1523 OO_Caret,
1524 OO_Pipe,
1525 OO_AmpAmp,
1526 OO_PipePipe,
1527 OO_Equal, OO_StarEqual,
1528 OO_SlashEqual, OO_PercentEqual,
1529 OO_PlusEqual, OO_MinusEqual,
1530 OO_LessLessEqual, OO_GreaterGreaterEqual,
1531 OO_AmpEqual, OO_CaretEqual,
1532 OO_PipeEqual,
1533 OO_Comma
1534 };
1535 return OverOps[Opc];
1536}
1537
Ted Kremenek709210f2010-04-13 23:39:13 +00001538InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001539 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001540 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001541 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001542 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001543 InitExprs(C, numInits),
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001544 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0)
1545{
1546 sawArrayRangeDesignator(false);
1547 setInitializesStdInitializerList(false);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001548 for (unsigned I = 0; I != numInits; ++I) {
1549 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001550 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001551 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001552 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001553 if (initExprs[I]->isInstantiationDependent())
1554 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001555 if (initExprs[I]->containsUnexpandedParameterPack())
1556 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001557 }
Sean Huntc3021132010-05-05 15:23:54 +00001558
Ted Kremenek709210f2010-04-13 23:39:13 +00001559 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001560}
Reid Spencer5f016e22007-07-11 17:01:13 +00001561
Ted Kremenek709210f2010-04-13 23:39:13 +00001562void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001563 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001564 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001565}
1566
Ted Kremenek709210f2010-04-13 23:39:13 +00001567void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001568 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001569}
1570
Ted Kremenek709210f2010-04-13 23:39:13 +00001571Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001572 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001573 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001574 InitExprs.back() = expr;
1575 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001576 }
Mike Stump1eb44332009-09-09 15:08:12 +00001577
Douglas Gregor4c678342009-01-28 21:54:33 +00001578 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1579 InitExprs[Init] = expr;
1580 return Result;
1581}
1582
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001583void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001584 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001585 ArrayFillerOrUnionFieldInit = filler;
1586 // Fill out any "holes" in the array due to designated initializers.
1587 Expr **inits = getInits();
1588 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1589 if (inits[i] == 0)
1590 inits[i] = filler;
1591}
1592
Richard Smithfe587202012-04-15 02:50:59 +00001593bool InitListExpr::isStringLiteralInit() const {
1594 if (getNumInits() != 1)
1595 return false;
1596 const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(getType());
1597 if (!CAT || !CAT->getElementType()->isIntegerType())
1598 return false;
1599 const Expr *Init = getInit(0)->IgnoreParenImpCasts();
1600 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1601}
1602
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001603SourceRange InitListExpr::getSourceRange() const {
1604 if (SyntacticForm)
1605 return SyntacticForm->getSourceRange();
1606 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1607 if (Beg.isInvalid()) {
1608 // Find the first non-null initializer.
1609 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1610 E = InitExprs.end();
1611 I != E; ++I) {
1612 if (Stmt *S = *I) {
1613 Beg = S->getLocStart();
1614 break;
1615 }
1616 }
1617 }
1618 if (End.isInvalid()) {
1619 // Find the first non-null initializer from the end.
1620 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1621 E = InitExprs.rend();
1622 I != E; ++I) {
1623 if (Stmt *S = *I) {
1624 End = S->getSourceRange().getEnd();
1625 break;
1626 }
1627 }
1628 }
1629 return SourceRange(Beg, End);
1630}
1631
Steve Naroffbfdcae62008-09-04 15:31:07 +00001632/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001633///
John McCalla345edb2012-02-17 03:32:35 +00001634const FunctionProtoType *BlockExpr::getFunctionType() const {
1635 // The block pointer is never sugared, but the function type might be.
1636 return cast<BlockPointerType>(getType())
1637 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001638}
1639
Mike Stump1eb44332009-09-09 15:08:12 +00001640SourceLocation BlockExpr::getCaretLocation() const {
1641 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001642}
Mike Stump1eb44332009-09-09 15:08:12 +00001643const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001644 return TheBlock->getBody();
1645}
Mike Stump1eb44332009-09-09 15:08:12 +00001646Stmt *BlockExpr::getBody() {
1647 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001648}
Steve Naroff56ee6892008-10-08 17:01:13 +00001649
1650
Reid Spencer5f016e22007-07-11 17:01:13 +00001651//===----------------------------------------------------------------------===//
1652// Generic Expression Routines
1653//===----------------------------------------------------------------------===//
1654
Chris Lattner026dc962009-02-14 07:37:35 +00001655/// isUnusedResultAWarning - Return true if this immediate expression should
1656/// be warned about if the result is unused. If so, fill in Loc and Ranges
1657/// with location to warn on and the source range[s] to report with the
1658/// warning.
1659bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001660 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001661 // Don't warn if the expr is type dependent. The type could end up
1662 // instantiating to void.
1663 if (isTypeDependent())
1664 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001665
Reid Spencer5f016e22007-07-11 17:01:13 +00001666 switch (getStmtClass()) {
1667 default:
John McCall0faede62010-03-12 07:11:26 +00001668 if (getType()->isVoidType())
1669 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001670 Loc = getExprLoc();
1671 R1 = getSourceRange();
1672 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001674 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001675 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001676 case GenericSelectionExprClass:
1677 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1678 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001679 case UnaryOperatorClass: {
1680 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Reid Spencer5f016e22007-07-11 17:01:13 +00001682 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001683 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001684 case UO_PostInc:
1685 case UO_PostDec:
1686 case UO_PreInc:
1687 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001688 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001689 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001690 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001691 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001692 return false;
1693 break;
John McCall2de56d12010-08-25 11:45:40 +00001694 case UO_Real:
1695 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001696 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001697 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1698 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001699 return false;
1700 break;
John McCall2de56d12010-08-25 11:45:40 +00001701 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001702 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001703 }
Chris Lattner026dc962009-02-14 07:37:35 +00001704 Loc = UO->getOperatorLoc();
1705 R1 = UO->getSubExpr()->getSourceRange();
1706 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001707 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001708 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001709 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001710 switch (BO->getOpcode()) {
1711 default:
1712 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001713 // Consider the RHS of comma for side effects. LHS was checked by
1714 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001715 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001716 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1717 // lvalue-ness) of an assignment written in a macro.
1718 if (IntegerLiteral *IE =
1719 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1720 if (IE->getValue() == 0)
1721 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001722 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1723 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001724 case BO_LAnd:
1725 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001726 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1727 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1728 return false;
1729 break;
John McCallbf0ee352010-02-16 04:10:53 +00001730 }
Chris Lattner026dc962009-02-14 07:37:35 +00001731 if (BO->isAssignmentOp())
1732 return false;
1733 Loc = BO->getOperatorLoc();
1734 R1 = BO->getLHS()->getSourceRange();
1735 R2 = BO->getRHS()->getSourceRange();
1736 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001737 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001738 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001739 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001740 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001741 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001742
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001743 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001744 // If only one of the LHS or RHS is a warning, the operator might
1745 // be being used for control flow. Only warn if both the LHS and
1746 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001747 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001748 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1749 return false;
1750 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001751 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001752 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001753 }
1754
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001756 // If the base pointer or element is to a volatile pointer/field, accessing
1757 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001758 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001759 return false;
1760 Loc = cast<MemberExpr>(this)->getMemberLoc();
1761 R1 = SourceRange(Loc, Loc);
1762 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1763 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001764
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 case ArraySubscriptExprClass:
1766 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001767 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001768 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001769 return false;
1770 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1771 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1772 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1773 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001774
Chandler Carruth9b106832011-08-17 09:49:44 +00001775 case CXXOperatorCallExprClass: {
1776 // We warn about operator== and operator!= even when user-defined operator
1777 // overloads as there is no reasonable way to define these such that they
1778 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1779 // warning: these operators are commonly typo'ed, and so warning on them
1780 // provides additional value as well. If this list is updated,
1781 // DiagnoseUnusedComparison should be as well.
1782 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1783 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001784 Op->getOperator() == OO_ExclaimEqual) {
1785 Loc = Op->getOperatorLoc();
1786 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001787 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001788 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001789
1790 // Fallthrough for generic call handling.
1791 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001792 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00001793 case CXXMemberCallExprClass:
1794 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001795 // If this is a direct call, get the callee.
1796 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001797 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001798 // If the callee has attribute pure, const, or warn_unused_result, warn
1799 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001800 //
1801 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1802 // updated to match for QoI.
1803 if (FD->getAttr<WarnUnusedResultAttr>() ||
1804 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1805 Loc = CE->getCallee()->getLocStart();
1806 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001807
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001808 if (unsigned NumArgs = CE->getNumArgs())
1809 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1810 CE->getArg(NumArgs-1)->getLocEnd());
1811 return true;
1812 }
Chris Lattner026dc962009-02-14 07:37:35 +00001813 }
1814 return false;
1815 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001816
1817 case CXXTemporaryObjectExprClass:
1818 case CXXConstructExprClass:
1819 return false;
1820
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001821 case ObjCMessageExprClass: {
1822 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00001823 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001824 ME->isInstanceMessage() &&
1825 !ME->getType()->isVoidType() &&
1826 ME->getSelector().getIdentifierInfoForSlot(0) &&
1827 ME->getSelector().getIdentifierInfoForSlot(0)
1828 ->getName().startswith("init")) {
1829 Loc = getExprLoc();
1830 R1 = ME->getSourceRange();
1831 return true;
1832 }
1833
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001834 const ObjCMethodDecl *MD = ME->getMethodDecl();
1835 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1836 Loc = getExprLoc();
1837 return true;
1838 }
Chris Lattner026dc962009-02-14 07:37:35 +00001839 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001840 }
Mike Stump1eb44332009-09-09 15:08:12 +00001841
John McCall12f78a62010-12-02 01:19:52 +00001842 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001843 Loc = getExprLoc();
1844 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001845 return true;
John McCall12f78a62010-12-02 01:19:52 +00001846
John McCall4b9c2d22011-11-06 09:01:30 +00001847 case PseudoObjectExprClass: {
1848 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1849
1850 // Only complain about things that have the form of a getter.
1851 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1852 isa<BinaryOperator>(PO->getSyntacticForm()))
1853 return false;
1854
1855 Loc = getExprLoc();
1856 R1 = getSourceRange();
1857 return true;
1858 }
1859
Chris Lattner611b2ec2008-07-26 19:51:01 +00001860 case StmtExprClass: {
1861 // Statement exprs don't logically have side effects themselves, but are
1862 // sometimes used in macros in ways that give them a type that is unused.
1863 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1864 // however, if the result of the stmt expr is dead, we don't want to emit a
1865 // warning.
1866 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001867 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001868 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001869 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001870 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1871 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1872 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1873 }
Mike Stump1eb44332009-09-09 15:08:12 +00001874
John McCall0faede62010-03-12 07:11:26 +00001875 if (getType()->isVoidType())
1876 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001877 Loc = cast<StmtExpr>(this)->getLParenLoc();
1878 R1 = getSourceRange();
1879 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001880 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001881 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001882 // If this is an explicit cast to void, allow it. People do this when they
1883 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001884 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001885 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001886 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1887 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1888 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001889 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001890 if (getType()->isVoidType())
1891 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001892 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001893
Anders Carlsson58beed92009-11-17 17:11:23 +00001894 // If this is a cast to void or a constructor conversion, check the operand.
1895 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001896 if (CE->getCastKind() == CK_ToVoid ||
1897 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001898 return (cast<CastExpr>(this)->getSubExpr()
1899 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001900 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1901 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1902 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001903 }
Mike Stump1eb44332009-09-09 15:08:12 +00001904
Eli Friedman4be1f472008-05-19 21:24:43 +00001905 case ImplicitCastExprClass:
1906 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001907 return (cast<ImplicitCastExpr>(this)
1908 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001909
Chris Lattner04421082008-04-08 04:40:51 +00001910 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001911 return (cast<CXXDefaultArgExpr>(this)
1912 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001913
1914 case CXXNewExprClass:
1915 // FIXME: In theory, there might be new expressions that don't have side
1916 // effects (e.g. a placement new with an uninitialized POD).
1917 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001918 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001919 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001920 return (cast<CXXBindTemporaryExpr>(this)
1921 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001922 case ExprWithCleanupsClass:
1923 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001924 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001925 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001926}
1927
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001928/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001929/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001930bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001931 const Expr *E = IgnoreParens();
1932 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001933 default:
1934 return false;
1935 case ObjCIvarRefExprClass:
1936 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001937 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001938 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001939 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001940 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001941 case MaterializeTemporaryExprClass:
1942 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1943 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001944 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001945 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001946 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00001947 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001948
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001949 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1950 if (VD->hasGlobalStorage())
1951 return true;
1952 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001953 // dereferencing to a pointer is always a gc'able candidate,
1954 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001955 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001956 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001957 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001958 return false;
1959 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001960 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001961 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001962 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001963 }
1964 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001965 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001966 }
1967}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001968
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001969bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1970 if (isTypeDependent())
1971 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001972 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001973}
1974
John McCall864c0412011-04-26 20:42:42 +00001975QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00001976 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00001977
1978 // Bound member expressions are always one of these possibilities:
1979 // x->m x.m x->*y x.*y
1980 // (possibly parenthesized)
1981
1982 expr = expr->IgnoreParens();
1983 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1984 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1985 return mem->getMemberDecl()->getType();
1986 }
1987
1988 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1989 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1990 ->getPointeeType();
1991 assert(type->isFunctionType());
1992 return type;
1993 }
1994
1995 assert(isa<UnresolvedMemberExpr>(expr));
1996 return QualType();
1997}
1998
Sebastian Redl369e51f2010-09-10 20:55:33 +00001999static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
2000 Expr::CanThrowResult CT2) {
2001 // CanThrowResult constants are ordered so that the maximum is the correct
2002 // merge result.
2003 return CT1 > CT2 ? CT1 : CT2;
2004}
2005
2006static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
2007 Expr *E = const_cast<Expr*>(CE);
2008 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00002009 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002010 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
2011 }
2012 return R;
2013}
2014
Richard Smith7a614d82011-06-11 17:19:42 +00002015static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
2016 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00002017 bool NullThrows = true) {
2018 if (!D)
2019 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
2020
2021 // See if we can get a function type from the decl somehow.
2022 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
2023 if (!VD) // If we have no clue what we're calling, assume the worst.
2024 return Expr::CT_Can;
2025
Sebastian Redl5221d8f2010-09-10 22:34:40 +00002026 // As an extension, we assume that __attribute__((nothrow)) functions don't
2027 // throw.
2028 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
2029 return Expr::CT_Cannot;
2030
Sebastian Redl369e51f2010-09-10 20:55:33 +00002031 QualType T = VD->getType();
2032 const FunctionProtoType *FT;
2033 if ((FT = T->getAs<FunctionProtoType>())) {
2034 } else if (const PointerType *PT = T->getAs<PointerType>())
2035 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
2036 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
2037 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
2038 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
2039 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
2040 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
2041 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
2042
2043 if (!FT)
2044 return Expr::CT_Can;
2045
Richard Smith7a614d82011-06-11 17:19:42 +00002046 if (FT->getExceptionSpecType() == EST_Delayed) {
2047 assert(isa<CXXConstructorDecl>(D) &&
2048 "only constructor exception specs can be unknown");
2049 Ctx.getDiagnostics().Report(E->getLocStart(),
2050 diag::err_exception_spec_unknown)
2051 << E->getSourceRange();
2052 return Expr::CT_Can;
2053 }
2054
Sebastian Redl8026f6d2011-03-13 17:09:40 +00002055 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00002056}
2057
2058static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
2059 if (DC->isTypeDependent())
2060 return Expr::CT_Dependent;
2061
Sebastian Redl295995c2010-09-10 20:55:47 +00002062 if (!DC->getTypeAsWritten()->isReferenceType())
2063 return Expr::CT_Cannot;
2064
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002065 if (DC->getSubExpr()->isTypeDependent())
2066 return Expr::CT_Dependent;
2067
Sebastian Redl369e51f2010-09-10 20:55:33 +00002068 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
2069}
2070
2071static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
2072 const CXXTypeidExpr *DC) {
2073 if (DC->isTypeOperand())
2074 return Expr::CT_Cannot;
2075
2076 Expr *Op = DC->getExprOperand();
2077 if (Op->isTypeDependent())
2078 return Expr::CT_Dependent;
2079
2080 const RecordType *RT = Op->getType()->getAs<RecordType>();
2081 if (!RT)
2082 return Expr::CT_Cannot;
2083
2084 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
2085 return Expr::CT_Cannot;
2086
2087 if (Op->Classify(C).isPRValue())
2088 return Expr::CT_Cannot;
2089
2090 return Expr::CT_Can;
2091}
2092
2093Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
2094 // C++ [expr.unary.noexcept]p3:
2095 // [Can throw] if in a potentially-evaluated context the expression would
2096 // contain:
2097 switch (getStmtClass()) {
2098 case CXXThrowExprClass:
2099 // - a potentially evaluated throw-expression
2100 return CT_Can;
2101
2102 case CXXDynamicCastExprClass: {
2103 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
2104 // where T is a reference type, that requires a run-time check
2105 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
2106 if (CT == CT_Can)
2107 return CT;
2108 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2109 }
2110
2111 case CXXTypeidExprClass:
2112 // - a potentially evaluated typeid expression applied to a glvalue
2113 // expression whose type is a polymorphic class type
2114 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
2115
2116 // - a potentially evaluated call to a function, member function, function
2117 // pointer, or member function pointer that does not have a non-throwing
2118 // exception-specification
2119 case CallExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002120 case CXXMemberCallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00002121 case CXXOperatorCallExprClass:
2122 case UserDefinedLiteralClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00002123 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002124 CanThrowResult CT;
2125 if (isTypeDependent())
2126 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00002127 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
2128 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002129 else
Richard Smith7a614d82011-06-11 17:19:42 +00002130 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00002131 if (CT == CT_Can)
2132 return CT;
2133 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2134 }
2135
Sebastian Redl295995c2010-09-10 20:55:47 +00002136 case CXXConstructExprClass:
2137 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00002138 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00002139 cast<CXXConstructExpr>(this)->getConstructor());
2140 if (CT == CT_Can)
2141 return CT;
2142 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2143 }
2144
Douglas Gregor01d08012012-02-07 10:09:13 +00002145 case LambdaExprClass: {
2146 const LambdaExpr *Lambda = cast<LambdaExpr>(this);
2147 CanThrowResult CT = Expr::CT_Cannot;
2148 for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(),
2149 CapEnd = Lambda->capture_init_end();
2150 Cap != CapEnd; ++Cap)
2151 CT = MergeCanThrow(CT, (*Cap)->CanThrow(C));
2152 return CT;
2153 }
2154
Sebastian Redl369e51f2010-09-10 20:55:33 +00002155 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002156 CanThrowResult CT;
2157 if (isTypeDependent())
2158 CT = CT_Dependent;
2159 else
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002160 CT = CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew());
Sebastian Redl369e51f2010-09-10 20:55:33 +00002161 if (CT == CT_Can)
2162 return CT;
2163 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2164 }
2165
2166 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002167 CanThrowResult CT;
2168 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
2169 if (DTy.isNull() || DTy->isDependentType()) {
2170 CT = CT_Dependent;
2171 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00002172 CT = CanCalleeThrow(C, this,
2173 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002174 if (const RecordType *RT = DTy->getAs<RecordType>()) {
2175 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00002176 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002177 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002178 if (CT == CT_Can)
2179 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002180 }
2181 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2182 }
2183
2184 case CXXBindTemporaryExprClass: {
2185 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00002186 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002187 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
2188 if (CT == CT_Can)
2189 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00002190 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2191 }
2192
2193 // ObjC message sends are like function calls, but never have exception
2194 // specs.
2195 case ObjCMessageExprClass:
2196 case ObjCPropertyRefExprClass:
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002197 case ObjCSubscriptRefExprClass:
2198 return CT_Can;
2199
2200 // All the ObjC literals that are implemented as calls are
2201 // potentially throwing unless we decide to close off that
2202 // possibility.
2203 case ObjCArrayLiteralClass:
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002204 case ObjCDictionaryLiteralClass:
2205 case ObjCNumericLiteralClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002206 return CT_Can;
2207
2208 // Many other things have subexpressions, so we have to test those.
2209 // Some are simple:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002210 case ConditionalOperatorClass:
2211 case CompoundLiteralExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002212 case CXXConstCastExprClass:
2213 case CXXDefaultArgExprClass:
2214 case CXXReinterpretCastExprClass:
2215 case DesignatedInitExprClass:
2216 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002217 case ExtVectorElementExprClass:
2218 case InitListExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002219 case MemberExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002220 case ObjCIsaExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002221 case ObjCIvarRefExprClass:
2222 case ParenExprClass:
2223 case ParenListExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002224 case ShuffleVectorExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002225 case VAArgExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002226 return CanSubExprsThrow(C, this);
2227
2228 // Some might be dependent for other reasons.
Sebastian Redl369e51f2010-09-10 20:55:33 +00002229 case ArraySubscriptExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002230 case BinaryOperatorClass:
2231 case CompoundAssignOperatorClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002232 case CStyleCastExprClass:
2233 case CXXStaticCastExprClass:
2234 case CXXFunctionalCastExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002235 case ImplicitCastExprClass:
2236 case MaterializeTemporaryExprClass:
2237 case UnaryOperatorClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002238 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2239 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2240 }
2241
2242 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2243 case StmtExprClass:
2244 return CT_Can;
2245
2246 case ChooseExprClass:
2247 if (isTypeDependent() || isValueDependent())
2248 return CT_Dependent;
2249 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2250
Peter Collingbournef111d932011-04-15 00:35:48 +00002251 case GenericSelectionExprClass:
2252 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2253 return CT_Dependent;
2254 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2255
Sebastian Redl369e51f2010-09-10 20:55:33 +00002256 // Some expressions are always dependent.
Sebastian Redl369e51f2010-09-10 20:55:33 +00002257 case CXXDependentScopeMemberExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002258 case CXXUnresolvedConstructExprClass:
2259 case DependentScopeDeclRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002260 return CT_Dependent;
2261
Eli Friedmanc9674be2012-01-31 01:21:45 +00002262 case AtomicExprClass:
2263 case AsTypeExprClass:
2264 case BinaryConditionalOperatorClass:
2265 case BlockExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002266 case CUDAKernelCallExprClass:
2267 case DeclRefExprClass:
2268 case ObjCBridgedCastExprClass:
2269 case ObjCIndirectCopyRestoreExprClass:
2270 case ObjCProtocolExprClass:
2271 case ObjCSelectorExprClass:
2272 case OffsetOfExprClass:
2273 case PackExpansionExprClass:
2274 case PseudoObjectExprClass:
2275 case SubstNonTypeTemplateParmExprClass:
2276 case SubstNonTypeTemplateParmPackExprClass:
2277 case UnaryExprOrTypeTraitExprClass:
2278 case UnresolvedLookupExprClass:
2279 case UnresolvedMemberExprClass:
2280 // FIXME: Can any of the above throw? If so, when?
Sebastian Redl369e51f2010-09-10 20:55:33 +00002281 return CT_Cannot;
Eli Friedmanc9674be2012-01-31 01:21:45 +00002282
2283 case AddrLabelExprClass:
2284 case ArrayTypeTraitExprClass:
2285 case BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002286 case TypeTraitExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002287 case CXXBoolLiteralExprClass:
2288 case CXXNoexceptExprClass:
2289 case CXXNullPtrLiteralExprClass:
2290 case CXXPseudoDestructorExprClass:
2291 case CXXScalarValueInitExprClass:
2292 case CXXThisExprClass:
2293 case CXXUuidofExprClass:
2294 case CharacterLiteralClass:
2295 case ExpressionTraitExprClass:
2296 case FloatingLiteralClass:
2297 case GNUNullExprClass:
2298 case ImaginaryLiteralClass:
2299 case ImplicitValueInitExprClass:
2300 case IntegerLiteralClass:
2301 case ObjCEncodeExprClass:
2302 case ObjCStringLiteralClass:
Jordy Rosed8b5ca12012-03-12 17:53:02 +00002303 case ObjCBoolLiteralExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002304 case OpaqueValueExprClass:
2305 case PredefinedExprClass:
2306 case SizeOfPackExprClass:
2307 case StringLiteralClass:
2308 case UnaryTypeTraitExprClass:
2309 // These expressions can never throw.
2310 return CT_Cannot;
2311
2312#define STMT(CLASS, PARENT) case CLASS##Class:
2313#define STMT_RANGE(Base, First, Last)
2314#define LAST_STMT_RANGE(BASE, FIRST, LAST)
2315#define EXPR(CLASS, PARENT)
2316#define ABSTRACT_STMT(STMT)
2317#include "clang/AST/StmtNodes.inc"
2318 case NoStmtClass:
2319 llvm_unreachable("Invalid class for expression");
Sebastian Redl369e51f2010-09-10 20:55:33 +00002320 }
Matt Beaumont-Gay56e68b72012-01-31 18:59:25 +00002321 llvm_unreachable("Bogus StmtClass");
Sebastian Redl369e51f2010-09-10 20:55:33 +00002322}
2323
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002324Expr* Expr::IgnoreParens() {
2325 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002326 while (true) {
2327 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2328 E = P->getSubExpr();
2329 continue;
2330 }
2331 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2332 if (P->getOpcode() == UO_Extension) {
2333 E = P->getSubExpr();
2334 continue;
2335 }
2336 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002337 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2338 if (!P->isResultDependent()) {
2339 E = P->getResultExpr();
2340 continue;
2341 }
2342 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002343 return E;
2344 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002345}
2346
Chris Lattner56f34942008-02-13 01:02:39 +00002347/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2348/// or CastExprs or ImplicitCastExprs, returning their operand.
2349Expr *Expr::IgnoreParenCasts() {
2350 Expr *E = this;
2351 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002352 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002353 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002354 continue;
2355 }
2356 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002357 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002358 continue;
2359 }
2360 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2361 if (P->getOpcode() == UO_Extension) {
2362 E = P->getSubExpr();
2363 continue;
2364 }
2365 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002366 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2367 if (!P->isResultDependent()) {
2368 E = P->getResultExpr();
2369 continue;
2370 }
2371 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002372 if (MaterializeTemporaryExpr *Materialize
2373 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2374 E = Materialize->GetTemporaryExpr();
2375 continue;
2376 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002377 if (SubstNonTypeTemplateParmExpr *NTTP
2378 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2379 E = NTTP->getReplacement();
2380 continue;
2381 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002382 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002383 }
2384}
2385
John McCall9c5d70c2010-12-04 08:24:19 +00002386/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2387/// casts. This is intended purely as a temporary workaround for code
2388/// that hasn't yet been rewritten to do the right thing about those
2389/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002390Expr *Expr::IgnoreParenLValueCasts() {
2391 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002392 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002393 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2394 E = P->getSubExpr();
2395 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002396 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002397 if (P->getCastKind() == CK_LValueToRValue) {
2398 E = P->getSubExpr();
2399 continue;
2400 }
John McCall9c5d70c2010-12-04 08:24:19 +00002401 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2402 if (P->getOpcode() == UO_Extension) {
2403 E = P->getSubExpr();
2404 continue;
2405 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002406 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2407 if (!P->isResultDependent()) {
2408 E = P->getResultExpr();
2409 continue;
2410 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002411 } else if (MaterializeTemporaryExpr *Materialize
2412 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2413 E = Materialize->GetTemporaryExpr();
2414 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002415 } else if (SubstNonTypeTemplateParmExpr *NTTP
2416 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2417 E = NTTP->getReplacement();
2418 continue;
John McCallf6a16482010-12-04 03:47:34 +00002419 }
2420 break;
2421 }
2422 return E;
2423}
2424
John McCall2fc46bf2010-05-05 22:59:52 +00002425Expr *Expr::IgnoreParenImpCasts() {
2426 Expr *E = this;
2427 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002428 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002429 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002430 continue;
2431 }
2432 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002433 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002434 continue;
2435 }
2436 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2437 if (P->getOpcode() == UO_Extension) {
2438 E = P->getSubExpr();
2439 continue;
2440 }
2441 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002442 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2443 if (!P->isResultDependent()) {
2444 E = P->getResultExpr();
2445 continue;
2446 }
2447 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002448 if (MaterializeTemporaryExpr *Materialize
2449 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2450 E = Materialize->GetTemporaryExpr();
2451 continue;
2452 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002453 if (SubstNonTypeTemplateParmExpr *NTTP
2454 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2455 E = NTTP->getReplacement();
2456 continue;
2457 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002458 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002459 }
2460}
2461
Hans Wennborg2f072b42011-06-09 17:06:51 +00002462Expr *Expr::IgnoreConversionOperator() {
2463 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002464 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002465 return MCE->getImplicitObjectArgument();
2466 }
2467 return this;
2468}
2469
Chris Lattnerecdd8412009-03-13 17:28:01 +00002470/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2471/// value (including ptr->int casts of the same size). Strip off any
2472/// ParenExpr or CastExprs, returning their operand.
2473Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2474 Expr *E = this;
2475 while (true) {
2476 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2477 E = P->getSubExpr();
2478 continue;
2479 }
Mike Stump1eb44332009-09-09 15:08:12 +00002480
Chris Lattnerecdd8412009-03-13 17:28:01 +00002481 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2482 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002483 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002484 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002485
Chris Lattnerecdd8412009-03-13 17:28:01 +00002486 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2487 E = SE;
2488 continue;
2489 }
Mike Stump1eb44332009-09-09 15:08:12 +00002490
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002491 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002492 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002493 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002494 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002495 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2496 E = SE;
2497 continue;
2498 }
2499 }
Mike Stump1eb44332009-09-09 15:08:12 +00002500
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002501 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2502 if (P->getOpcode() == UO_Extension) {
2503 E = P->getSubExpr();
2504 continue;
2505 }
2506 }
2507
Peter Collingbournef111d932011-04-15 00:35:48 +00002508 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2509 if (!P->isResultDependent()) {
2510 E = P->getResultExpr();
2511 continue;
2512 }
2513 }
2514
Douglas Gregorc0244c52011-09-08 17:56:33 +00002515 if (SubstNonTypeTemplateParmExpr *NTTP
2516 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2517 E = NTTP->getReplacement();
2518 continue;
2519 }
2520
Chris Lattnerecdd8412009-03-13 17:28:01 +00002521 return E;
2522 }
2523}
2524
Douglas Gregor6eef5192009-12-14 19:27:10 +00002525bool Expr::isDefaultArgument() const {
2526 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002527 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2528 E = M->GetTemporaryExpr();
2529
Douglas Gregor6eef5192009-12-14 19:27:10 +00002530 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2531 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002532
Douglas Gregor6eef5192009-12-14 19:27:10 +00002533 return isa<CXXDefaultArgExpr>(E);
2534}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002535
Douglas Gregor2f599792010-04-02 18:24:57 +00002536/// \brief Skip over any no-op casts and any temporary-binding
2537/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002538static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002539 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2540 E = M->GetTemporaryExpr();
2541
Douglas Gregor2f599792010-04-02 18:24:57 +00002542 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002543 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002544 E = ICE->getSubExpr();
2545 else
2546 break;
2547 }
2548
2549 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2550 E = BE->getSubExpr();
2551
2552 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002553 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002554 E = ICE->getSubExpr();
2555 else
2556 break;
2557 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002558
2559 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002560}
2561
John McCall558d2ab2010-09-15 10:14:12 +00002562/// isTemporaryObject - Determines if this expression produces a
2563/// temporary of the given class type.
2564bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2565 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2566 return false;
2567
Anders Carlssonf8b30152010-11-28 16:40:49 +00002568 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002569
John McCall58277b52010-09-15 20:59:13 +00002570 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002571 if (!E->Classify(C).isPRValue()) {
2572 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002573 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002574 return false;
2575 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002576
John McCall19e60ad2010-09-16 06:57:56 +00002577 // Black-list a few cases which yield pr-values of class type that don't
2578 // refer to temporaries of that type:
2579
2580 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002581 if (isa<ImplicitCastExpr>(E)) {
2582 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2583 case CK_DerivedToBase:
2584 case CK_UncheckedDerivedToBase:
2585 return false;
2586 default:
2587 break;
2588 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002589 }
2590
John McCall19e60ad2010-09-16 06:57:56 +00002591 // - member expressions (all)
2592 if (isa<MemberExpr>(E))
2593 return false;
2594
John McCall56ca35d2011-02-17 10:25:35 +00002595 // - opaque values (all)
2596 if (isa<OpaqueValueExpr>(E))
2597 return false;
2598
John McCall558d2ab2010-09-15 10:14:12 +00002599 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002600}
2601
Douglas Gregor75e85042011-03-02 21:06:53 +00002602bool Expr::isImplicitCXXThis() const {
2603 const Expr *E = this;
2604
2605 // Strip away parentheses and casts we don't care about.
2606 while (true) {
2607 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2608 E = Paren->getSubExpr();
2609 continue;
2610 }
2611
2612 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2613 if (ICE->getCastKind() == CK_NoOp ||
2614 ICE->getCastKind() == CK_LValueToRValue ||
2615 ICE->getCastKind() == CK_DerivedToBase ||
2616 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2617 E = ICE->getSubExpr();
2618 continue;
2619 }
2620 }
2621
2622 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2623 if (UnOp->getOpcode() == UO_Extension) {
2624 E = UnOp->getSubExpr();
2625 continue;
2626 }
2627 }
2628
Douglas Gregor03e80032011-06-21 17:03:29 +00002629 if (const MaterializeTemporaryExpr *M
2630 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2631 E = M->GetTemporaryExpr();
2632 continue;
2633 }
2634
Douglas Gregor75e85042011-03-02 21:06:53 +00002635 break;
2636 }
2637
2638 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2639 return This->isImplicit();
2640
2641 return false;
2642}
2643
Douglas Gregor898574e2008-12-05 23:32:09 +00002644/// hasAnyTypeDependentArguments - Determines if any of the expressions
2645/// in Exprs is type-dependent.
Ahmed Charles13a140c2012-02-25 11:00:22 +00002646bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2647 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002648 if (Exprs[I]->isTypeDependent())
2649 return true;
2650
2651 return false;
2652}
2653
John McCall4204f072010-08-02 21:13:48 +00002654bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002655 // This function is attempting whether an expression is an initializer
2656 // which can be evaluated at compile-time. isEvaluatable handles most
2657 // of the cases, but it can't deal with some initializer-specific
2658 // expressions, and it can't deal with aggregates; we deal with those here,
2659 // and fall back to isEvaluatable for the other cases.
2660
John McCall4204f072010-08-02 21:13:48 +00002661 // If we ever capture reference-binding directly in the AST, we can
2662 // kill the second parameter.
2663
2664 if (IsForRef) {
2665 EvalResult Result;
2666 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2667 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002668
Anders Carlssone8a32b82008-11-24 05:23:59 +00002669 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002670 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002671 case IntegerLiteralClass:
2672 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002673 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002674 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002675 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002676 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002677 case CXXTemporaryObjectExprClass:
2678 case CXXConstructExprClass: {
2679 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002680
2681 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002682 if (CE->getConstructor()->isTrivial()) {
2683 // 1) an application of the trivial default constructor or
2684 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002685
Richard Smith180f4792011-11-10 06:34:14 +00002686 // 2) an elidable trivial copy construction of an operand which is
2687 // itself a constant initializer. Note that we consider the
2688 // operand on its own, *not* as a reference binding.
2689 if (CE->isElidable() &&
2690 CE->getArg(0)->isConstantInitializer(Ctx, false))
2691 return true;
2692 }
2693
2694 // 3) a foldable constexpr constructor.
2695 break;
John McCallb4b9b152010-08-01 21:51:45 +00002696 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002697 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002698 // This handles gcc's extension that allows global initializers like
2699 // "struct x {int x;} x = (struct x) {};".
2700 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002701 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002702 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002703 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002704 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002705 // FIXME: This doesn't deal with fields with reference types correctly.
2706 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2707 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002708 const InitListExpr *Exp = cast<InitListExpr>(this);
2709 unsigned numInits = Exp->getNumInits();
2710 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002711 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002712 return false;
2713 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002714 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002715 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002716 case ImplicitValueInitExprClass:
2717 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002718 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002719 return cast<ParenExpr>(this)->getSubExpr()
2720 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002721 case GenericSelectionExprClass:
2722 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2723 return false;
2724 return cast<GenericSelectionExpr>(this)->getResultExpr()
2725 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002726 case ChooseExprClass:
2727 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2728 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002729 case UnaryOperatorClass: {
2730 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002731 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002732 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002733 break;
2734 }
John McCall4204f072010-08-02 21:13:48 +00002735 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002736 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002737 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002738 case CStyleCastExprClass: {
2739 const CastExpr *CE = cast<CastExpr>(this);
2740
David Chisnall7a7ee302012-01-16 17:27:18 +00002741 // If we're promoting an integer to an _Atomic type then this is constant
2742 // if the integer is constant. We also need to check the converse in case
2743 // someone does something like:
2744 //
2745 // int a = (_Atomic(int))42;
2746 //
2747 // I doubt anyone would write code like this directly, but it's quite
2748 // possible as the result of macro expansions.
2749 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2750 CE->getCastKind() == CK_AtomicToNonAtomic)
2751 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2752
Richard Smithd62ca372011-12-06 22:44:34 +00002753 // Handle bitcasts of vector constants.
2754 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2755 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2756
Eli Friedman6bd97192011-12-21 00:43:02 +00002757 // Handle misc casts we want to ignore.
2758 // FIXME: Is it really safe to ignore all these?
2759 if (CE->getCastKind() == CK_NoOp ||
2760 CE->getCastKind() == CK_LValueToRValue ||
2761 CE->getCastKind() == CK_ToUnion ||
2762 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002763 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2764
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002765 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002766 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002767 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002768 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002769 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002770 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002771 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002772}
2773
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002774namespace {
2775 /// \brief Look for a call to a non-trivial function within an expression.
2776 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2777 {
2778 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2779
2780 bool NonTrivial;
2781
2782 public:
2783 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002784 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002785
2786 bool hasNonTrivialCall() const { return NonTrivial; }
2787
2788 void VisitCallExpr(CallExpr *E) {
2789 if (CXXMethodDecl *Method
2790 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2791 if (Method->isTrivial()) {
2792 // Recurse to children of the call.
2793 Inherited::VisitStmt(E);
2794 return;
2795 }
2796 }
2797
2798 NonTrivial = true;
2799 }
2800
2801 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2802 if (E->getConstructor()->isTrivial()) {
2803 // Recurse to children of the call.
2804 Inherited::VisitStmt(E);
2805 return;
2806 }
2807
2808 NonTrivial = true;
2809 }
2810
2811 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2812 if (E->getTemporary()->getDestructor()->isTrivial()) {
2813 Inherited::VisitStmt(E);
2814 return;
2815 }
2816
2817 NonTrivial = true;
2818 }
2819 };
2820}
2821
2822bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2823 NonTrivialCallFinder Finder(Ctx);
2824 Finder.Visit(this);
2825 return Finder.hasNonTrivialCall();
2826}
2827
Chandler Carruth82214a82011-02-18 23:54:50 +00002828/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2829/// pointer constant or not, as well as the specific kind of constant detected.
2830/// Null pointer constants can be integer constant expressions with the
2831/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2832/// (a GNU extension).
2833Expr::NullPointerConstantKind
2834Expr::isNullPointerConstant(ASTContext &Ctx,
2835 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002836 if (isValueDependent()) {
2837 switch (NPC) {
2838 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002839 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002840 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002841 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2842 return NPCK_ZeroInteger;
2843 else
2844 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002845
Douglas Gregorce940492009-09-25 04:25:58 +00002846 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002847 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002848 }
2849 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002850
Sebastian Redl07779722008-10-31 14:43:28 +00002851 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002852 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002853 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002854 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002855 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002856 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002857 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002858 Pointee->isVoidType() && // to void*
2859 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002860 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002861 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002862 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002863 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2864 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002865 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002866 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2867 // Accept ((void*)0) as a null pointer constant, as many other
2868 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002869 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002870 } else if (const GenericSelectionExpr *GE =
2871 dyn_cast<GenericSelectionExpr>(this)) {
2872 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002873 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002874 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002875 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002876 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002877 } else if (isa<GNUNullExpr>(this)) {
2878 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002879 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002880 } else if (const MaterializeTemporaryExpr *M
2881 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2882 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002883 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2884 if (const Expr *Source = OVE->getSourceExpr())
2885 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002886 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002887
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002888 // C++0x nullptr_t is always a null pointer constant.
2889 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002890 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002891
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002892 if (const RecordType *UT = getType()->getAsUnionType())
2893 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2894 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2895 const Expr *InitExpr = CLE->getInitializer();
2896 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2897 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2898 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002899 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002900 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002901 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002902 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002903
Reid Spencer5f016e22007-07-11 17:01:13 +00002904 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith70488e22012-02-14 21:38:30 +00002905 // test for the value 0. Don't use the C++11 constant expression semantics
2906 // for this, for now; once the dust settles on core issue 903, we might only
2907 // allow a literal 0 here in C++11 mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002908 if (Ctx.getLangOpts().CPlusPlus0x) {
Richard Smith70488e22012-02-14 21:38:30 +00002909 if (!isCXX98IntegralConstantExpr(Ctx))
2910 return NPCK_NotNull;
2911 } else {
2912 if (!isIntegerConstantExpr(Ctx))
2913 return NPCK_NotNull;
2914 }
Chandler Carruth82214a82011-02-18 23:54:50 +00002915
Richard Smith70488e22012-02-14 21:38:30 +00002916 return (EvaluateKnownConstInt(Ctx) == 0) ? NPCK_ZeroInteger : NPCK_NotNull;
Reid Spencer5f016e22007-07-11 17:01:13 +00002917}
Steve Naroff31a45842007-07-28 23:10:27 +00002918
John McCallf6a16482010-12-04 03:47:34 +00002919/// \brief If this expression is an l-value for an Objective C
2920/// property, find the underlying property reference expression.
2921const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2922 const Expr *E = this;
2923 while (true) {
2924 assert((E->getValueKind() == VK_LValue &&
2925 E->getObjectKind() == OK_ObjCProperty) &&
2926 "expression is not a property reference");
2927 E = E->IgnoreParenCasts();
2928 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2929 if (BO->getOpcode() == BO_Comma) {
2930 E = BO->getRHS();
2931 continue;
2932 }
2933 }
2934
2935 break;
2936 }
2937
2938 return cast<ObjCPropertyRefExpr>(E);
2939}
2940
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002941FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002942 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002943
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002944 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002945 if (ICE->getCastKind() == CK_LValueToRValue ||
2946 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002947 E = ICE->getSubExpr()->IgnoreParens();
2948 else
2949 break;
2950 }
2951
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002952 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002953 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002954 if (Field->isBitField())
2955 return Field;
2956
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002957 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2958 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2959 if (Field->isBitField())
2960 return Field;
2961
Eli Friedman42068e92011-07-13 02:05:57 +00002962 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002963 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2964 return BinOp->getLHS()->getBitField();
2965
Eli Friedman42068e92011-07-13 02:05:57 +00002966 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2967 return BinOp->getRHS()->getBitField();
2968 }
2969
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002970 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002971}
2972
Anders Carlsson09380262010-01-31 17:18:49 +00002973bool Expr::refersToVectorElement() const {
2974 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002975
Anders Carlsson09380262010-01-31 17:18:49 +00002976 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002977 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002978 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002979 E = ICE->getSubExpr()->IgnoreParens();
2980 else
2981 break;
2982 }
Sean Huntc3021132010-05-05 15:23:54 +00002983
Anders Carlsson09380262010-01-31 17:18:49 +00002984 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2985 return ASE->getBase()->getType()->isVectorType();
2986
2987 if (isa<ExtVectorElementExpr>(E))
2988 return true;
2989
2990 return false;
2991}
2992
Chris Lattner2140e902009-02-16 22:14:05 +00002993/// isArrow - Return true if the base expression is a pointer to vector,
2994/// return false if the base expression is a vector.
2995bool ExtVectorElementExpr::isArrow() const {
2996 return getBase()->getType()->isPointerType();
2997}
2998
Nate Begeman213541a2008-04-18 23:10:10 +00002999unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00003000 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00003001 return VT->getNumElements();
3002 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00003003}
3004
Nate Begeman8a997642008-05-09 06:41:27 +00003005/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00003006bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00003007 // FIXME: Refactor this code to an accessor on the AST node which returns the
3008 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00003009 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00003010
3011 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00003012 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00003013 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003014
Nate Begeman190d6a22009-01-18 02:01:21 +00003015 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00003016 if (Comp[0] == 's' || Comp[0] == 'S')
3017 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003018
Daniel Dunbar15027422009-10-17 23:53:04 +00003019 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00003020 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00003021 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00003022
Steve Narofffec0b492007-07-30 03:29:09 +00003023 return false;
3024}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003025
Nate Begeman8a997642008-05-09 06:41:27 +00003026/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00003027void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00003028 SmallVectorImpl<unsigned> &Elts) const {
3029 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003030 if (Comp[0] == 's' || Comp[0] == 'S')
3031 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00003032
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003033 bool isHi = Comp == "hi";
3034 bool isLo = Comp == "lo";
3035 bool isEven = Comp == "even";
3036 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00003037
Nate Begeman8a997642008-05-09 06:41:27 +00003038 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
3039 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00003040
Nate Begeman8a997642008-05-09 06:41:27 +00003041 if (isHi)
3042 Index = e + i;
3043 else if (isLo)
3044 Index = i;
3045 else if (isEven)
3046 Index = 2 * i;
3047 else if (isOdd)
3048 Index = 2 * i + 1;
3049 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00003050 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003051
Nate Begeman3b8d1162008-05-13 21:03:02 +00003052 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00003053 }
Nate Begeman8a997642008-05-09 06:41:27 +00003054}
3055
Douglas Gregor04badcf2010-04-21 00:45:42 +00003056ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003057 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003058 SourceLocation LBracLoc,
3059 SourceLocation SuperLoc,
3060 bool IsInstanceSuper,
3061 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003062 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003063 ArrayRef<SourceLocation> SelLocs,
3064 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003065 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003066 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003067 SourceLocation RBracLoc,
3068 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003069 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003070 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00003071 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003072 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003073 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3074 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003075 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003076 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
3077 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00003078{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003079 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003080 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00003081}
3082
Douglas Gregor04badcf2010-04-21 00:45:42 +00003083ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003084 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003085 SourceLocation LBracLoc,
3086 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003087 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003088 ArrayRef<SourceLocation> SelLocs,
3089 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003090 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003091 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003092 SourceLocation RBracLoc,
3093 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003094 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003095 T->isDependentType(), T->isInstantiationDependentType(),
3096 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003097 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3098 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003099 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003100 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003101 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003102{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003103 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003104 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003105}
3106
Douglas Gregor04badcf2010-04-21 00:45:42 +00003107ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003108 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003109 SourceLocation LBracLoc,
3110 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003111 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003112 ArrayRef<SourceLocation> SelLocs,
3113 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003114 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003115 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003116 SourceLocation RBracLoc,
3117 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003118 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003119 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003120 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003121 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003122 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3123 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003124 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003125 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003126 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003127{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003128 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003129 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003130}
3131
3132void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3133 ArrayRef<SourceLocation> SelLocs,
3134 SelectorLocationsKind SelLocsK) {
3135 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003136 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003137 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003138 if (Args[I]->isTypeDependent())
3139 ExprBits.TypeDependent = true;
3140 if (Args[I]->isValueDependent())
3141 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003142 if (Args[I]->isInstantiationDependent())
3143 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003144 if (Args[I]->containsUnexpandedParameterPack())
3145 ExprBits.ContainsUnexpandedParameterPack = true;
3146
3147 MyArgs[I] = Args[I];
3148 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003149
Benjamin Kramer19562c92012-02-20 00:20:48 +00003150 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003151 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003152 if (SelLocsK == SelLoc_NonStandard)
3153 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3154 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003155}
3156
Douglas Gregor04badcf2010-04-21 00:45:42 +00003157ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003158 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003159 SourceLocation LBracLoc,
3160 SourceLocation SuperLoc,
3161 bool IsInstanceSuper,
3162 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003163 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003164 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003165 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003166 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003167 SourceLocation RBracLoc,
3168 bool isImplicit) {
3169 assert((!SelLocs.empty() || isImplicit) &&
3170 "No selector locs for non-implicit message");
3171 ObjCMessageExpr *Mem;
3172 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3173 if (isImplicit)
3174 Mem = alloc(Context, Args.size(), 0);
3175 else
3176 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003177 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003178 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003179 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003180}
3181
3182ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003183 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003184 SourceLocation LBracLoc,
3185 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003186 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003187 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003188 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003189 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003190 SourceLocation RBracLoc,
3191 bool isImplicit) {
3192 assert((!SelLocs.empty() || isImplicit) &&
3193 "No selector locs for non-implicit message");
3194 ObjCMessageExpr *Mem;
3195 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3196 if (isImplicit)
3197 Mem = alloc(Context, Args.size(), 0);
3198 else
3199 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003200 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003201 SelLocs, SelLocsK, Method, Args, RBracLoc,
3202 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003203}
3204
3205ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003206 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003207 SourceLocation LBracLoc,
3208 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003209 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003210 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003211 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003212 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003213 SourceLocation RBracLoc,
3214 bool isImplicit) {
3215 assert((!SelLocs.empty() || isImplicit) &&
3216 "No selector locs for non-implicit message");
3217 ObjCMessageExpr *Mem;
3218 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3219 if (isImplicit)
3220 Mem = alloc(Context, Args.size(), 0);
3221 else
3222 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003223 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003224 SelLocs, SelLocsK, Method, Args, RBracLoc,
3225 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003226}
3227
Sean Huntc3021132010-05-05 15:23:54 +00003228ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003229 unsigned NumArgs,
3230 unsigned NumStoredSelLocs) {
3231 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003232 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3233}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003234
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003235ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3236 ArrayRef<Expr *> Args,
3237 SourceLocation RBraceLoc,
3238 ArrayRef<SourceLocation> SelLocs,
3239 Selector Sel,
3240 SelectorLocationsKind &SelLocsK) {
3241 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3242 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3243 : 0;
3244 return alloc(C, Args.size(), NumStoredSelLocs);
3245}
3246
3247ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3248 unsigned NumArgs,
3249 unsigned NumStoredSelLocs) {
3250 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3251 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3252 return (ObjCMessageExpr *)C.Allocate(Size,
3253 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3254}
3255
3256void ObjCMessageExpr::getSelectorLocs(
3257 SmallVectorImpl<SourceLocation> &SelLocs) const {
3258 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3259 SelLocs.push_back(getSelectorLoc(i));
3260}
3261
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003262SourceRange ObjCMessageExpr::getReceiverRange() const {
3263 switch (getReceiverKind()) {
3264 case Instance:
3265 return getInstanceReceiver()->getSourceRange();
3266
3267 case Class:
3268 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3269
3270 case SuperInstance:
3271 case SuperClass:
3272 return getSuperLoc();
3273 }
3274
David Blaikie30263482012-01-20 21:50:17 +00003275 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003276}
3277
Douglas Gregor04badcf2010-04-21 00:45:42 +00003278Selector ObjCMessageExpr::getSelector() const {
3279 if (HasMethod)
3280 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3281 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003282 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003283}
3284
3285ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3286 switch (getReceiverKind()) {
3287 case Instance:
3288 if (const ObjCObjectPointerType *Ptr
3289 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
3290 return Ptr->getInterfaceDecl();
3291 break;
3292
3293 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00003294 if (const ObjCObjectType *Ty
3295 = getClassReceiver()->getAs<ObjCObjectType>())
3296 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003297 break;
3298
3299 case SuperInstance:
3300 if (const ObjCObjectPointerType *Ptr
3301 = getSuperType()->getAs<ObjCObjectPointerType>())
3302 return Ptr->getInterfaceDecl();
3303 break;
3304
3305 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00003306 if (const ObjCObjectType *Iface
3307 = getSuperType()->getAs<ObjCObjectType>())
3308 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003309 break;
3310 }
3311
3312 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003313}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003314
Chris Lattner5f9e2722011-07-23 10:55:15 +00003315StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003316 switch (getBridgeKind()) {
3317 case OBC_Bridge:
3318 return "__bridge";
3319 case OBC_BridgeTransfer:
3320 return "__bridge_transfer";
3321 case OBC_BridgeRetained:
3322 return "__bridge_retained";
3323 }
David Blaikie30263482012-01-20 21:50:17 +00003324
3325 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003326}
3327
Jay Foad4ba2a172011-01-12 09:06:06 +00003328bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003329 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003330}
3331
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003332ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3333 QualType Type, SourceLocation BLoc,
3334 SourceLocation RP)
3335 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3336 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003337 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003338 Type->containsUnexpandedParameterPack()),
3339 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3340{
3341 SubExprs = new (C) Stmt*[nexpr];
3342 for (unsigned i = 0; i < nexpr; i++) {
3343 if (args[i]->isTypeDependent())
3344 ExprBits.TypeDependent = true;
3345 if (args[i]->isValueDependent())
3346 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003347 if (args[i]->isInstantiationDependent())
3348 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003349 if (args[i]->containsUnexpandedParameterPack())
3350 ExprBits.ContainsUnexpandedParameterPack = true;
3351
3352 SubExprs[i] = args[i];
3353 }
3354}
3355
Nate Begeman888376a2009-08-12 02:28:50 +00003356void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3357 unsigned NumExprs) {
3358 if (SubExprs) C.Deallocate(SubExprs);
3359
3360 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003361 this->NumExprs = NumExprs;
3362 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003363}
Nate Begeman888376a2009-08-12 02:28:50 +00003364
Peter Collingbournef111d932011-04-15 00:35:48 +00003365GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3366 SourceLocation GenericLoc, Expr *ControllingExpr,
3367 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3368 unsigned NumAssocs, SourceLocation DefaultLoc,
3369 SourceLocation RParenLoc,
3370 bool ContainsUnexpandedParameterPack,
3371 unsigned ResultIndex)
3372 : Expr(GenericSelectionExprClass,
3373 AssocExprs[ResultIndex]->getType(),
3374 AssocExprs[ResultIndex]->getValueKind(),
3375 AssocExprs[ResultIndex]->getObjectKind(),
3376 AssocExprs[ResultIndex]->isTypeDependent(),
3377 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003378 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003379 ContainsUnexpandedParameterPack),
3380 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3381 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3382 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3383 RParenLoc(RParenLoc) {
3384 SubExprs[CONTROLLING] = ControllingExpr;
3385 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3386 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3387}
3388
3389GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3390 SourceLocation GenericLoc, Expr *ControllingExpr,
3391 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3392 unsigned NumAssocs, SourceLocation DefaultLoc,
3393 SourceLocation RParenLoc,
3394 bool ContainsUnexpandedParameterPack)
3395 : Expr(GenericSelectionExprClass,
3396 Context.DependentTy,
3397 VK_RValue,
3398 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003399 /*isTypeDependent=*/true,
3400 /*isValueDependent=*/true,
3401 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003402 ContainsUnexpandedParameterPack),
3403 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3404 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3405 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3406 RParenLoc(RParenLoc) {
3407 SubExprs[CONTROLLING] = ControllingExpr;
3408 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3409 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3410}
3411
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003412//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003413// DesignatedInitExpr
3414//===----------------------------------------------------------------------===//
3415
Chandler Carruthb1138242011-06-16 06:47:06 +00003416IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003417 assert(Kind == FieldDesignator && "Only valid on a field designator");
3418 if (Field.NameOrField & 0x01)
3419 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3420 else
3421 return getField()->getIdentifier();
3422}
3423
Sean Huntc3021132010-05-05 15:23:54 +00003424DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003425 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003426 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003427 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003428 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003429 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003430 unsigned NumIndexExprs,
3431 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003432 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003433 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003434 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003435 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003436 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003437 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3438 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003439 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003440
3441 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003442 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003443 *Child++ = Init;
3444
3445 // Copy the designators and their subexpressions, computing
3446 // value-dependence along the way.
3447 unsigned IndexIdx = 0;
3448 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003449 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003450
3451 if (this->Designators[I].isArrayDesignator()) {
3452 // Compute type- and value-dependence.
3453 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003454 if (Index->isTypeDependent() || Index->isValueDependent())
3455 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003456 if (Index->isInstantiationDependent())
3457 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003458 // Propagate unexpanded parameter packs.
3459 if (Index->containsUnexpandedParameterPack())
3460 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003461
3462 // Copy the index expressions into permanent storage.
3463 *Child++ = IndexExprs[IndexIdx++];
3464 } else if (this->Designators[I].isArrayRangeDesignator()) {
3465 // Compute type- and value-dependence.
3466 Expr *Start = IndexExprs[IndexIdx];
3467 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003468 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003469 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003470 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003471 ExprBits.InstantiationDependent = true;
3472 } else if (Start->isInstantiationDependent() ||
3473 End->isInstantiationDependent()) {
3474 ExprBits.InstantiationDependent = true;
3475 }
3476
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003477 // Propagate unexpanded parameter packs.
3478 if (Start->containsUnexpandedParameterPack() ||
3479 End->containsUnexpandedParameterPack())
3480 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003481
3482 // Copy the start/end expressions into permanent storage.
3483 *Child++ = IndexExprs[IndexIdx++];
3484 *Child++ = IndexExprs[IndexIdx++];
3485 }
3486 }
3487
3488 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003489}
3490
Douglas Gregor05c13a32009-01-22 00:58:24 +00003491DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003492DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003493 unsigned NumDesignators,
3494 Expr **IndexExprs, unsigned NumIndexExprs,
3495 SourceLocation ColonOrEqualLoc,
3496 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003497 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003498 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003499 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003500 ColonOrEqualLoc, UsesColonSyntax,
3501 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003502}
3503
Mike Stump1eb44332009-09-09 15:08:12 +00003504DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003505 unsigned NumIndexExprs) {
3506 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3507 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3508 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3509}
3510
Douglas Gregor319d57f2010-01-06 23:17:19 +00003511void DesignatedInitExpr::setDesignators(ASTContext &C,
3512 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003513 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003514 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003515 NumDesignators = NumDesigs;
3516 for (unsigned I = 0; I != NumDesigs; ++I)
3517 Designators[I] = Desigs[I];
3518}
3519
Abramo Bagnara24f46742011-03-16 15:08:46 +00003520SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3521 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3522 if (size() == 1)
3523 return DIE->getDesignator(0)->getSourceRange();
3524 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3525 DIE->getDesignator(size()-1)->getEndLocation());
3526}
3527
Douglas Gregor05c13a32009-01-22 00:58:24 +00003528SourceRange DesignatedInitExpr::getSourceRange() const {
3529 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003530 Designator &First =
3531 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003532 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003533 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003534 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3535 else
3536 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3537 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003538 StartLoc =
3539 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003540 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3541}
3542
Douglas Gregor05c13a32009-01-22 00:58:24 +00003543Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3544 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3545 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3546 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003547 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3548 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3549}
3550
3551Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003552 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003553 "Requires array range designator");
3554 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3555 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003556 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3557 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3558}
3559
3560Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003561 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003562 "Requires array range designator");
3563 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3564 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003565 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3566 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3567}
3568
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003569/// \brief Replaces the designator at index @p Idx with the series
3570/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003571void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003572 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003573 const Designator *Last) {
3574 unsigned NumNewDesignators = Last - First;
3575 if (NumNewDesignators == 0) {
3576 std::copy_backward(Designators + Idx + 1,
3577 Designators + NumDesignators,
3578 Designators + Idx);
3579 --NumNewDesignators;
3580 return;
3581 } else if (NumNewDesignators == 1) {
3582 Designators[Idx] = *First;
3583 return;
3584 }
3585
Mike Stump1eb44332009-09-09 15:08:12 +00003586 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003587 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003588 std::copy(Designators, Designators + Idx, NewDesignators);
3589 std::copy(First, Last, NewDesignators + Idx);
3590 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3591 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003592 Designators = NewDesignators;
3593 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3594}
3595
Mike Stump1eb44332009-09-09 15:08:12 +00003596ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003597 Expr **exprs, unsigned nexprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003598 SourceLocation rparenloc)
3599 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003600 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003601 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00003602 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003603 for (unsigned i = 0; i != nexprs; ++i) {
3604 if (exprs[i]->isTypeDependent())
3605 ExprBits.TypeDependent = true;
3606 if (exprs[i]->isValueDependent())
3607 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003608 if (exprs[i]->isInstantiationDependent())
3609 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003610 if (exprs[i]->containsUnexpandedParameterPack())
3611 ExprBits.ContainsUnexpandedParameterPack = true;
3612
Nate Begeman2ef13e52009-08-10 23:49:36 +00003613 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003614 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003615}
3616
John McCalle996ffd2011-02-16 08:02:54 +00003617const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3618 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3619 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003620 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3621 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003622 e = cast<CXXConstructExpr>(e)->getArg(0);
3623 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3624 e = ice->getSubExpr();
3625 return cast<OpaqueValueExpr>(e);
3626}
3627
John McCall4b9c2d22011-11-06 09:01:30 +00003628PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3629 unsigned numSemanticExprs) {
3630 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3631 (1 + numSemanticExprs) * sizeof(Expr*),
3632 llvm::alignOf<PseudoObjectExpr>());
3633 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3634}
3635
3636PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3637 : Expr(PseudoObjectExprClass, shell) {
3638 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3639}
3640
3641PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3642 ArrayRef<Expr*> semantics,
3643 unsigned resultIndex) {
3644 assert(syntax && "no syntactic expression!");
3645 assert(semantics.size() && "no semantic expressions!");
3646
3647 QualType type;
3648 ExprValueKind VK;
3649 if (resultIndex == NoResult) {
3650 type = C.VoidTy;
3651 VK = VK_RValue;
3652 } else {
3653 assert(resultIndex < semantics.size());
3654 type = semantics[resultIndex]->getType();
3655 VK = semantics[resultIndex]->getValueKind();
3656 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3657 }
3658
3659 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3660 (1 + semantics.size()) * sizeof(Expr*),
3661 llvm::alignOf<PseudoObjectExpr>());
3662 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3663 resultIndex);
3664}
3665
3666PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3667 Expr *syntax, ArrayRef<Expr*> semantics,
3668 unsigned resultIndex)
3669 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3670 /*filled in at end of ctor*/ false, false, false, false) {
3671 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3672 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3673
3674 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3675 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3676 getSubExprsBuffer()[i] = E;
3677
3678 if (E->isTypeDependent())
3679 ExprBits.TypeDependent = true;
3680 if (E->isValueDependent())
3681 ExprBits.ValueDependent = true;
3682 if (E->isInstantiationDependent())
3683 ExprBits.InstantiationDependent = true;
3684 if (E->containsUnexpandedParameterPack())
3685 ExprBits.ContainsUnexpandedParameterPack = true;
3686
3687 if (isa<OpaqueValueExpr>(E))
3688 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3689 "opaque-value semantic expressions for pseudo-object "
3690 "operations must have sources");
3691 }
3692}
3693
Douglas Gregor05c13a32009-01-22 00:58:24 +00003694//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003695// ExprIterator.
3696//===----------------------------------------------------------------------===//
3697
3698Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3699Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3700Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3701const Expr* ConstExprIterator::operator[](size_t idx) const {
3702 return cast<Expr>(I[idx]);
3703}
3704const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3705const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3706
3707//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003708// Child Iterators for iterating over subexpressions/substatements
3709//===----------------------------------------------------------------------===//
3710
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003711// UnaryExprOrTypeTraitExpr
3712Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003713 // If this is of a type and the type is a VLA type (and not a typedef), the
3714 // size expression of the VLA needs to be treated as an executable expression.
3715 // Why isn't this weirdness documented better in StmtIterator?
3716 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003717 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003718 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003719 return child_range(child_iterator(T), child_iterator());
3720 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003721 }
John McCall63c00d72011-02-09 08:16:59 +00003722 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003723}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003724
Steve Naroff563477d2007-09-18 23:55:05 +00003725// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003726Stmt::child_range ObjCMessageExpr::children() {
3727 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003728 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003729 begin = reinterpret_cast<Stmt **>(this + 1);
3730 else
3731 begin = reinterpret_cast<Stmt **>(getArgs());
3732 return child_range(begin,
3733 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003734}
3735
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003736ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3737 QualType T, ObjCMethodDecl *Method,
3738 SourceRange SR)
3739 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3740 false, false, false, false),
3741 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3742{
3743 Expr **SaveElements = getElements();
3744 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3745 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3746 ExprBits.ValueDependent = true;
3747 if (Elements[I]->isInstantiationDependent())
3748 ExprBits.InstantiationDependent = true;
3749 if (Elements[I]->containsUnexpandedParameterPack())
3750 ExprBits.ContainsUnexpandedParameterPack = true;
3751
3752 SaveElements[I] = Elements[I];
3753 }
3754}
3755
3756ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3757 llvm::ArrayRef<Expr *> Elements,
3758 QualType T, ObjCMethodDecl * Method,
3759 SourceRange SR) {
3760 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3761 + Elements.size() * sizeof(Expr *));
3762 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3763}
3764
3765ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3766 unsigned NumElements) {
3767
3768 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3769 + NumElements * sizeof(Expr *));
3770 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3771}
3772
3773ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3774 ArrayRef<ObjCDictionaryElement> VK,
3775 bool HasPackExpansions,
3776 QualType T, ObjCMethodDecl *method,
3777 SourceRange SR)
3778 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3779 false, false),
3780 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3781 DictWithObjectsMethod(method)
3782{
3783 KeyValuePair *KeyValues = getKeyValues();
3784 ExpansionData *Expansions = getExpansionData();
3785 for (unsigned I = 0; I < NumElements; I++) {
3786 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3787 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3788 ExprBits.ValueDependent = true;
3789 if (VK[I].Key->isInstantiationDependent() ||
3790 VK[I].Value->isInstantiationDependent())
3791 ExprBits.InstantiationDependent = true;
3792 if (VK[I].EllipsisLoc.isInvalid() &&
3793 (VK[I].Key->containsUnexpandedParameterPack() ||
3794 VK[I].Value->containsUnexpandedParameterPack()))
3795 ExprBits.ContainsUnexpandedParameterPack = true;
3796
3797 KeyValues[I].Key = VK[I].Key;
3798 KeyValues[I].Value = VK[I].Value;
3799 if (Expansions) {
3800 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3801 if (VK[I].NumExpansions)
3802 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3803 else
3804 Expansions[I].NumExpansionsPlusOne = 0;
3805 }
3806 }
3807}
3808
3809ObjCDictionaryLiteral *
3810ObjCDictionaryLiteral::Create(ASTContext &C,
3811 ArrayRef<ObjCDictionaryElement> VK,
3812 bool HasPackExpansions,
3813 QualType T, ObjCMethodDecl *method,
3814 SourceRange SR) {
3815 unsigned ExpansionsSize = 0;
3816 if (HasPackExpansions)
3817 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3818
3819 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3820 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3821 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3822}
3823
3824ObjCDictionaryLiteral *
3825ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3826 bool HasPackExpansions) {
3827 unsigned ExpansionsSize = 0;
3828 if (HasPackExpansions)
3829 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3830 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3831 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3832 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3833 HasPackExpansions);
3834}
3835
3836ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
3837 Expr *base,
3838 Expr *key, QualType T,
3839 ObjCMethodDecl *getMethod,
3840 ObjCMethodDecl *setMethod,
3841 SourceLocation RB) {
3842 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
3843 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
3844 OK_ObjCSubscript,
3845 getMethod, setMethod, RB);
3846}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003847
3848AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3849 QualType t, AtomicOp op, SourceLocation RP)
3850 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3851 false, false, false, false),
3852 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3853{
Richard Smithe1b2abc2012-04-10 22:49:28 +00003854 assert(nexpr == getNumSubExprs(op) && "wrong number of subexpressions");
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003855 for (unsigned i = 0; i < nexpr; i++) {
3856 if (args[i]->isTypeDependent())
3857 ExprBits.TypeDependent = true;
3858 if (args[i]->isValueDependent())
3859 ExprBits.ValueDependent = true;
3860 if (args[i]->isInstantiationDependent())
3861 ExprBits.InstantiationDependent = true;
3862 if (args[i]->containsUnexpandedParameterPack())
3863 ExprBits.ContainsUnexpandedParameterPack = true;
3864
3865 SubExprs[i] = args[i];
3866 }
3867}
Richard Smithe1b2abc2012-04-10 22:49:28 +00003868
3869unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
3870 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00003871 case AO__c11_atomic_init:
3872 case AO__c11_atomic_load:
3873 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003874 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00003875
3876 case AO__c11_atomic_store:
3877 case AO__c11_atomic_exchange:
3878 case AO__atomic_load:
3879 case AO__atomic_store:
3880 case AO__atomic_store_n:
3881 case AO__atomic_exchange_n:
3882 case AO__c11_atomic_fetch_add:
3883 case AO__c11_atomic_fetch_sub:
3884 case AO__c11_atomic_fetch_and:
3885 case AO__c11_atomic_fetch_or:
3886 case AO__c11_atomic_fetch_xor:
3887 case AO__atomic_fetch_add:
3888 case AO__atomic_fetch_sub:
3889 case AO__atomic_fetch_and:
3890 case AO__atomic_fetch_or:
3891 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00003892 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00003893 case AO__atomic_add_fetch:
3894 case AO__atomic_sub_fetch:
3895 case AO__atomic_and_fetch:
3896 case AO__atomic_or_fetch:
3897 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00003898 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003899 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00003900
3901 case AO__atomic_exchange:
3902 return 4;
3903
3904 case AO__c11_atomic_compare_exchange_strong:
3905 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003906 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00003907
3908 case AO__atomic_compare_exchange:
3909 case AO__atomic_compare_exchange_n:
3910 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00003911 }
3912 llvm_unreachable("unknown atomic op");
3913}