blob: 0266cd63bbc953909a720c1cd41ce541900760e6 [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().
97namespace {
98 /// This implementation is used when a class provides a custom
99 /// implementation of getExprLoc.
100 template <class E, class T>
101 SourceLocation getExprLocImpl(const Expr *expr,
102 SourceLocation (T::*v)() const) {
103 return static_cast<const E*>(expr)->getExprLoc();
104 }
105
106 /// This implementation is used when a class doesn't provide
107 /// a custom implementation of getExprLoc. Overload resolution
108 /// should pick it over the implementation above because it's
109 /// more specialized according to function template partial ordering.
110 template <class E>
111 SourceLocation getExprLocImpl(const Expr *expr,
112 SourceLocation (Expr::*v)() const) {
113 return static_cast<const E*>(expr)->getSourceRange().getBegin();
114 }
115}
116
117SourceLocation Expr::getExprLoc() const {
118 switch (getStmtClass()) {
119 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
120#define ABSTRACT_STMT(type)
121#define STMT(type, base) \
122 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
123#define EXPR(type, base) \
124 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
125#include "clang/AST/StmtNodes.inc"
126 }
127 llvm_unreachable("unknown statement kind");
John McCall63c00d72011-02-09 08:16:59 +0000128}
129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130//===----------------------------------------------------------------------===//
131// Primary Expressions.
132//===----------------------------------------------------------------------===//
133
Douglas Gregor561f8122011-07-01 01:22:09 +0000134/// \brief Compute the type-, value-, and instantiation-dependence of a
135/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000136/// based on the declaration being referenced.
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000137static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T,
Douglas Gregord967e312011-01-19 21:52:31 +0000138 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000139 bool &ValueDependent,
140 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000141 TypeDependent = false;
142 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000143 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000144
145 // (TD) C++ [temp.dep.expr]p3:
146 // An id-expression is type-dependent if it contains:
147 //
Sean Huntc3021132010-05-05 15:23:54 +0000148 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000149 //
150 // (VD) C++ [temp.dep.constexpr]p2:
151 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000152
Douglas Gregor0da76df2009-11-23 11:41:28 +0000153 // (TD) - an identifier that was declared with dependent type
154 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000155 if (T->isDependentType()) {
156 TypeDependent = true;
157 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000158 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000159 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000160 } else if (T->isInstantiationDependentType()) {
161 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000162 }
Douglas Gregord967e312011-01-19 21:52:31 +0000163
Douglas Gregor0da76df2009-11-23 11:41:28 +0000164 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000165 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000166 == DeclarationName::CXXConversionFunctionName) {
167 QualType T = D->getDeclName().getCXXNameType();
168 if (T->isDependentType()) {
169 TypeDependent = true;
170 ValueDependent = true;
171 InstantiationDependent = true;
172 return;
173 }
174
175 if (T->isInstantiationDependentType())
176 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000177 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000178
Douglas Gregor0da76df2009-11-23 11:41:28 +0000179 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000180 if (isa<NonTypeTemplateParmDecl>(D)) {
181 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000182 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000183 return;
184 }
185
Douglas Gregor0da76df2009-11-23 11:41:28 +0000186 // (VD) - a constant with integral or enumeration type and is
187 // initialized with an expression that is value-dependent.
Richard Smithdb1822c2011-11-08 01:31:09 +0000188 // (VD) - a constant with literal type and is initialized with an
189 // expression that is value-dependent [C++11].
190 // (VD) - FIXME: Missing from the standard:
191 // - an entity with reference type and is initialized with an
192 // expression that is value-dependent [C++11]
Douglas Gregord967e312011-01-19 21:52:31 +0000193 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000194 if ((Ctx.getLangOptions().CPlusPlus0x ?
Richard Smithdb1822c2011-11-08 01:31:09 +0000195 Var->getType()->isLiteralType() :
196 Var->getType()->isIntegralOrEnumerationType()) &&
197 (Var->getType().getCVRQualifiers() == Qualifiers::Const ||
198 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000199 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000200 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000201 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000202 InstantiationDependent = true;
203 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000204 }
205
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000206 // (VD) - FIXME: Missing from the standard:
207 // - a member function or a static data member of the current
208 // instantiation
Richard Smithdb1822c2011-11-08 01:31:09 +0000209 if (Var->isStaticDataMember() &&
210 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000211 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000212 InstantiationDependent = true;
213 }
Douglas Gregord967e312011-01-19 21:52:31 +0000214
215 return;
216 }
217
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000218 // (VD) - FIXME: Missing from the standard:
219 // - a member function or a static data member of the current
220 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000221 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
222 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000223 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000224 }
Douglas Gregord967e312011-01-19 21:52:31 +0000225}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000226
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000227void DeclRefExpr::computeDependence(ASTContext &Ctx) {
Douglas Gregord967e312011-01-19 21:52:31 +0000228 bool TypeDependent = false;
229 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000230 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000231 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
232 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000233
234 // (TD) C++ [temp.dep.expr]p3:
235 // An id-expression is type-dependent if it contains:
236 //
237 // and
238 //
239 // (VD) C++ [temp.dep.constexpr]p2:
240 // An identifier is value-dependent if it is:
241 if (!TypeDependent && !ValueDependent &&
242 hasExplicitTemplateArgs() &&
243 TemplateSpecializationType::anyDependentTemplateArguments(
244 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000245 getNumTemplateArgs(),
246 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000247 TypeDependent = true;
248 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000249 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000250 }
251
252 ExprBits.TypeDependent = TypeDependent;
253 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000254 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000255
Douglas Gregor10738d32010-12-23 23:51:58 +0000256 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000257 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000258 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000259}
260
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000261DeclRefExpr::DeclRefExpr(ASTContext &Ctx,
262 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000263 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000264 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000265 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000266 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000267 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000268 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000269 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
270 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000271 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000272 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000273 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
274 if (FoundD)
275 getInternalFoundDecl() = FoundD;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000276 DeclRefExprBits.HasTemplateKWAndArgsInfo
277 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
Douglas Gregor561f8122011-07-01 01:22:09 +0000278 if (TemplateArgs) {
279 bool Dependent = false;
280 bool InstantiationDependent = false;
281 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000282 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
283 Dependent,
284 InstantiationDependent,
285 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +0000286 if (InstantiationDependent)
287 setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000288 } else if (TemplateKWLoc.isValid()) {
289 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor561f8122011-07-01 01:22:09 +0000290 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000291 DeclRefExprBits.HadMultipleCandidates = 0;
292
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000293 computeDependence(Ctx);
Abramo Bagnara25777432010-08-11 22:01:17 +0000294}
295
Douglas Gregora2813ce2009-10-23 18:54:35 +0000296DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000297 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000298 SourceLocation TemplateKWLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000299 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000300 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000301 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000302 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000303 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000304 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000305 return Create(Context, QualifierLoc, TemplateKWLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000306 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000307 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000308}
309
310DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000311 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000312 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000313 ValueDecl *D,
314 const DeclarationNameInfo &NameInfo,
315 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000316 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000317 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000318 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000319 // Filter out cases where the found Decl is the same as the value refenenced.
320 if (D == FoundD)
321 FoundD = 0;
322
Douglas Gregora2813ce2009-10-23 18:54:35 +0000323 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000324 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000325 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000326 if (FoundD)
327 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000328 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000329 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
330 else if (TemplateKWLoc.isValid())
331 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000332
Chris Lattner32488542010-10-30 05:14:06 +0000333 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000334 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
335 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000336}
337
Chandler Carruth3aa81402011-05-01 23:48:14 +0000338DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000339 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000340 bool HasFoundDecl,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000341 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000342 unsigned NumTemplateArgs) {
343 std::size_t Size = sizeof(DeclRefExpr);
344 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000345 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000346 if (HasFoundDecl)
347 Size += sizeof(NamedDecl *);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000348 if (HasTemplateKWAndArgsInfo)
349 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000350
Chris Lattner32488542010-10-30 05:14:06 +0000351 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000352 return new (Mem) DeclRefExpr(EmptyShell());
353}
354
Douglas Gregora2813ce2009-10-23 18:54:35 +0000355SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000356 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000357 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000358 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000359 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000360 R.setEnd(getRAngleLoc());
361 return R;
362}
Daniel Dunbar396ec672012-03-09 15:39:15 +0000363SourceLocation DeclRefExpr::getLocStart() const {
364 if (hasQualifier())
365 return getQualifierLoc().getBeginLoc();
366 return getNameInfo().getLocStart();
367}
368SourceLocation DeclRefExpr::getLocEnd() const {
369 if (hasExplicitTemplateArgs())
370 return getRAngleLoc();
371 return getNameInfo().getLocEnd();
372}
Douglas Gregora2813ce2009-10-23 18:54:35 +0000373
Anders Carlsson3a082d82009-09-08 18:24:21 +0000374// FIXME: Maybe this should use DeclPrinter with a special "print predefined
375// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000376std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
377 ASTContext &Context = CurrentDecl->getASTContext();
378
Anders Carlsson3a082d82009-09-08 18:24:21 +0000379 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000380 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000381 return FD->getNameAsString();
382
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000383 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000384 llvm::raw_svector_ostream Out(Name);
385
386 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000387 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000388 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000389 if (MD->isStatic())
390 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000391 }
392
393 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000394
395 std::string Proto = FD->getQualifiedNameAsString(Policy);
396
John McCall183700f2009-09-21 23:43:11 +0000397 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000398 const FunctionProtoType *FT = 0;
399 if (FD->hasWrittenPrototype())
400 FT = dyn_cast<FunctionProtoType>(AFT);
401
402 Proto += "(";
403 if (FT) {
404 llvm::raw_string_ostream POut(Proto);
405 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
406 if (i) POut << ", ";
407 std::string Param;
408 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
409 POut << Param;
410 }
411
412 if (FT->isVariadic()) {
413 if (FD->getNumParams()) POut << ", ";
414 POut << "...";
415 }
416 }
417 Proto += ")";
418
Sam Weinig4eadcc52009-12-27 01:38:20 +0000419 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
420 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
421 if (ThisQuals.hasConst())
422 Proto += " const";
423 if (ThisQuals.hasVolatile())
424 Proto += " volatile";
425 }
426
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000427 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
428 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000429
430 Out << Proto;
431
432 Out.flush();
433 return Name.str().str();
434 }
435 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000436 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000437 llvm::raw_svector_ostream Out(Name);
438 Out << (MD->isInstanceMethod() ? '-' : '+');
439 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000440
441 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
442 // a null check to avoid a crash.
443 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000444 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000445
Anders Carlsson3a082d82009-09-08 18:24:21 +0000446 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000447 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000448 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000449
Anders Carlsson3a082d82009-09-08 18:24:21 +0000450 Out << ' ';
451 Out << MD->getSelector().getAsString();
452 Out << ']';
453
454 Out.flush();
455 return Name.str().str();
456 }
457 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
458 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
459 return "top level";
460 }
461 return "";
462}
463
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000464void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
465 if (hasAllocation())
466 C.Deallocate(pVal);
467
468 BitWidth = Val.getBitWidth();
469 unsigned NumWords = Val.getNumWords();
470 const uint64_t* Words = Val.getRawData();
471 if (NumWords > 1) {
472 pVal = new (C) uint64_t[NumWords];
473 std::copy(Words, Words + NumWords, pVal);
474 } else if (NumWords == 1)
475 VAL = Words[0];
476 else
477 VAL = 0;
478}
479
480IntegerLiteral *
481IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
482 QualType type, SourceLocation l) {
483 return new (C) IntegerLiteral(C, V, type, l);
484}
485
486IntegerLiteral *
487IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
488 return new (C) IntegerLiteral(Empty);
489}
490
491FloatingLiteral *
492FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
493 bool isexact, QualType Type, SourceLocation L) {
494 return new (C) FloatingLiteral(C, V, isexact, Type, L);
495}
496
497FloatingLiteral *
498FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000499 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000500}
501
Chris Lattnerda8249e2008-06-07 22:13:43 +0000502/// getValueAsApproximateDouble - This returns the value as an inaccurate
503/// double. Note that this may cause loss of precision, but is useful for
504/// debugging dumps, etc.
505double FloatingLiteral::getValueAsApproximateDouble() const {
506 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000507 bool ignored;
508 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
509 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000510 return V.convertToDouble();
511}
512
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000513int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000514 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000515 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000516 case Ascii:
517 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000518 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000519 break;
520 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000521 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000522 break;
523 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000524 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000525 break;
526 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000527 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000528 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000529 }
530 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
531 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000532 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000533 && "character byte widths supported are 1, 2, and 4 only");
534 return CharByteWidth;
535}
536
Chris Lattner5f9e2722011-07-23 10:55:15 +0000537StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000538 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000539 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000540 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000541 // Allocate enough space for the StringLiteral plus an array of locations for
542 // any concatenated string tokens.
543 void *Mem = C.Allocate(sizeof(StringLiteral)+
544 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000545 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000546 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000549 SL->setString(C,Str,Kind,Pascal);
550
Chris Lattner2085fd62009-02-18 06:40:38 +0000551 SL->TokLocs[0] = Loc[0];
552 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000553
Chris Lattner726e1682009-02-18 05:49:11 +0000554 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000555 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
556 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000557}
558
Douglas Gregor673ecd62009-04-15 16:35:07 +0000559StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
560 void *Mem = C.Allocate(sizeof(StringLiteral)+
561 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000562 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000563 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000564 SL->CharByteWidth = 0;
565 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000566 SL->NumConcatenated = NumStrs;
567 return SL;
568}
569
Eli Friedman64f45a22011-11-01 02:23:42 +0000570void StringLiteral::setString(ASTContext &C, StringRef Str,
571 StringKind Kind, bool IsPascal) {
572 //FIXME: we assume that the string data comes from a target that uses the same
573 // code unit size and endianess for the type of string.
574 this->Kind = Kind;
575 this->IsPascal = IsPascal;
576
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000577 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000578 assert((Str.size()%CharByteWidth == 0)
579 && "size of data must be multiple of CharByteWidth");
580 Length = Str.size()/CharByteWidth;
581
582 switch(CharByteWidth) {
583 case 1: {
584 char *AStrData = new (C) char[Length];
585 std::memcpy(AStrData,Str.data(),Str.size());
586 StrData.asChar = AStrData;
587 break;
588 }
589 case 2: {
590 uint16_t *AStrData = new (C) uint16_t[Length];
591 std::memcpy(AStrData,Str.data(),Str.size());
592 StrData.asUInt16 = AStrData;
593 break;
594 }
595 case 4: {
596 uint32_t *AStrData = new (C) uint32_t[Length];
597 std::memcpy(AStrData,Str.data(),Str.size());
598 StrData.asUInt32 = AStrData;
599 break;
600 }
601 default:
602 assert(false && "unsupported CharByteWidth");
603 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000604}
605
Chris Lattner08f92e32010-11-17 07:37:15 +0000606/// getLocationOfByte - Return a source location that points to the specified
607/// byte of this string literal.
608///
609/// Strings are amazingly complex. They can be formed from multiple tokens and
610/// can have escape sequences in them in addition to the usual trigraph and
611/// escaped newline business. This routine handles this complexity.
612///
613SourceLocation StringLiteral::
614getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
615 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000616 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
617
Chris Lattner08f92e32010-11-17 07:37:15 +0000618 // Loop over all of the tokens in this string until we find the one that
619 // contains the byte we're looking for.
620 unsigned TokNo = 0;
621 while (1) {
622 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
623 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
624
625 // Get the spelling of the string so that we can get the data that makes up
626 // the string literal, not the identifier for the macro it is potentially
627 // expanded through.
628 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
629
630 // Re-lex the token to get its length and original spelling.
631 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
632 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000633 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000634 if (Invalid)
635 return StrTokSpellingLoc;
636
637 const char *StrData = Buffer.data()+LocInfo.second;
638
639 // Create a langops struct and enable trigraphs. This is sufficient for
640 // relexing tokens.
641 LangOptions LangOpts;
642 LangOpts.Trigraphs = true;
643
644 // Create a lexer starting at the beginning of this token.
645 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
646 Buffer.end());
647 Token TheTok;
648 TheLexer.LexFromRawLexer(TheTok);
649
650 // Use the StringLiteralParser to compute the length of the string in bytes.
651 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
652 unsigned TokNumBytes = SLP.GetStringLength();
653
654 // If the byte is in this token, return the location of the byte.
655 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000656 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000657 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
658
659 // Now that we know the offset of the token in the spelling, use the
660 // preprocessor to get the offset in the original source.
661 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
662 }
663
664 // Move to the next string token.
665 ++TokNo;
666 ByteNo -= TokNumBytes;
667 }
668}
669
670
671
Reid Spencer5f016e22007-07-11 17:01:13 +0000672/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
673/// corresponds to, e.g. "sizeof" or "[pre]++".
674const char *UnaryOperator::getOpcodeStr(Opcode Op) {
675 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000676 case UO_PostInc: return "++";
677 case UO_PostDec: return "--";
678 case UO_PreInc: return "++";
679 case UO_PreDec: return "--";
680 case UO_AddrOf: return "&";
681 case UO_Deref: return "*";
682 case UO_Plus: return "+";
683 case UO_Minus: return "-";
684 case UO_Not: return "~";
685 case UO_LNot: return "!";
686 case UO_Real: return "__real";
687 case UO_Imag: return "__imag";
688 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000690 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000691}
692
John McCall2de56d12010-08-25 11:45:40 +0000693UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000694UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
695 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000696 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000697 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
698 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
699 case OO_Amp: return UO_AddrOf;
700 case OO_Star: return UO_Deref;
701 case OO_Plus: return UO_Plus;
702 case OO_Minus: return UO_Minus;
703 case OO_Tilde: return UO_Not;
704 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000705 }
706}
707
708OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
709 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000710 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
711 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
712 case UO_AddrOf: return OO_Amp;
713 case UO_Deref: return OO_Star;
714 case UO_Plus: return OO_Plus;
715 case UO_Minus: return OO_Minus;
716 case UO_Not: return OO_Tilde;
717 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000718 default: return OO_None;
719 }
720}
721
722
Reid Spencer5f016e22007-07-11 17:01:13 +0000723//===----------------------------------------------------------------------===//
724// Postfix Operators.
725//===----------------------------------------------------------------------===//
726
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000727CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
728 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000729 SourceLocation rparenloc)
730 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000731 fn->isTypeDependent(),
732 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000733 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000734 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000735 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000736
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000737 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000738 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000739 for (unsigned i = 0; i != numargs; ++i) {
740 if (args[i]->isTypeDependent())
741 ExprBits.TypeDependent = true;
742 if (args[i]->isValueDependent())
743 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000744 if (args[i]->isInstantiationDependent())
745 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000746 if (args[i]->containsUnexpandedParameterPack())
747 ExprBits.ContainsUnexpandedParameterPack = true;
748
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000749 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000750 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000751
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000752 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000753 RParenLoc = rparenloc;
754}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000755
Ted Kremenek668bf912009-02-09 20:51:47 +0000756CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000757 QualType t, ExprValueKind VK, SourceLocation rparenloc)
758 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000759 fn->isTypeDependent(),
760 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000761 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000762 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000763 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000764
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000765 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000766 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000767 for (unsigned i = 0; i != numargs; ++i) {
768 if (args[i]->isTypeDependent())
769 ExprBits.TypeDependent = true;
770 if (args[i]->isValueDependent())
771 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000772 if (args[i]->isInstantiationDependent())
773 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000774 if (args[i]->containsUnexpandedParameterPack())
775 ExprBits.ContainsUnexpandedParameterPack = true;
776
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000777 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000778 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000779
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000780 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000781 RParenLoc = rparenloc;
782}
783
Mike Stump1eb44332009-09-09 15:08:12 +0000784CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
785 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000786 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000787 SubExprs = new (C) Stmt*[PREARGS_START];
788 CallExprBits.NumPreArgs = 0;
789}
790
791CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
792 EmptyShell Empty)
793 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
794 // FIXME: Why do we allocate this?
795 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
796 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000797}
798
Nuno Lopesd20254f2009-12-20 23:11:08 +0000799Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000800 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000801
802 while (SubstNonTypeTemplateParmExpr *NTTP
803 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
804 CEE = NTTP->getReplacement()->IgnoreParenCasts();
805 }
806
Sebastian Redl20012152010-09-10 20:55:30 +0000807 // If we're calling a dereference, look at the pointer instead.
808 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
809 if (BO->isPtrMemOp())
810 CEE = BO->getRHS()->IgnoreParenCasts();
811 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
812 if (UO->getOpcode() == UO_Deref)
813 CEE = UO->getSubExpr()->IgnoreParenCasts();
814 }
Chris Lattner6346f962009-07-17 15:46:27 +0000815 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000816 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000817 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
818 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000819
820 return 0;
821}
822
Nuno Lopesd20254f2009-12-20 23:11:08 +0000823FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000824 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000825}
826
Chris Lattnerd18b3292007-12-28 05:25:02 +0000827/// setNumArgs - This changes the number of arguments present in this call.
828/// Any orphaned expressions are deleted by this, and any new operands are set
829/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000830void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000831 // No change, just return.
832 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Chris Lattnerd18b3292007-12-28 05:25:02 +0000834 // If shrinking # arguments, just delete the extras and forgot them.
835 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000836 this->NumArgs = NumArgs;
837 return;
838 }
839
840 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000841 unsigned NumPreArgs = getNumPreArgs();
842 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000843 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000844 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000845 NewSubExprs[i] = SubExprs[i];
846 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000847 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
848 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000849 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Douglas Gregor88c9a462009-04-17 21:46:47 +0000851 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000852 SubExprs = NewSubExprs;
853 this->NumArgs = NumArgs;
854}
855
Chris Lattnercb888962008-10-06 05:00:53 +0000856/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
857/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +0000858unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000859 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000860 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000861 // ImplicitCastExpr.
862 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
863 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000864 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000865
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000866 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
867 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000868 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Anders Carlssonbcba2012008-01-31 02:13:57 +0000870 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
871 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000872 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000874 if (!FDecl->getIdentifier())
875 return 0;
876
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000877 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000878}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000879
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000880QualType CallExpr::getCallReturnType() const {
881 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000882 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000883 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000884 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000885 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000886 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
887 // This should never be overloaded and so should never return null.
888 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000889
John McCall864c0412011-04-26 20:42:42 +0000890 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000891 return FnType->getResultType();
892}
Chris Lattnercb888962008-10-06 05:00:53 +0000893
John McCall2882eca2011-02-21 06:23:05 +0000894SourceRange CallExpr::getSourceRange() const {
895 if (isa<CXXOperatorCallExpr>(this))
896 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
897
898 SourceLocation begin = getCallee()->getLocStart();
899 if (begin.isInvalid() && getNumArgs() > 0)
900 begin = getArg(0)->getLocStart();
901 SourceLocation end = getRParenLoc();
902 if (end.isInvalid() && getNumArgs() > 0)
903 end = getArg(getNumArgs() - 1)->getLocEnd();
904 return SourceRange(begin, end);
905}
906
Sean Huntc3021132010-05-05 15:23:54 +0000907OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000908 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000909 TypeSourceInfo *tsi,
910 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000911 Expr** exprsPtr, unsigned numExprs,
912 SourceLocation RParenLoc) {
913 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000914 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000915 sizeof(Expr*) * numExprs);
916
917 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
918 exprsPtr, numExprs, RParenLoc);
919}
920
921OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
922 unsigned numComps, unsigned numExprs) {
923 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
924 sizeof(OffsetOfNode) * numComps +
925 sizeof(Expr*) * numExprs);
926 return new (Mem) OffsetOfExpr(numComps, numExprs);
927}
928
Sean Huntc3021132010-05-05 15:23:54 +0000929OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000930 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000931 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000932 Expr** exprsPtr, unsigned numExprs,
933 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000934 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
935 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000936 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000937 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000938 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000939 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
940 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000941{
942 for(unsigned i = 0; i < numComps; ++i) {
943 setComponent(i, compsPtr[i]);
944 }
Sean Huntc3021132010-05-05 15:23:54 +0000945
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000946 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000947 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
948 ExprBits.ValueDependent = true;
949 if (exprsPtr[i]->containsUnexpandedParameterPack())
950 ExprBits.ContainsUnexpandedParameterPack = true;
951
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000952 setIndexExpr(i, exprsPtr[i]);
953 }
954}
955
956IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
957 assert(getKind() == Field || getKind() == Identifier);
958 if (getKind() == Field)
959 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000960
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000961 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
962}
963
Mike Stump1eb44332009-09-09 15:08:12 +0000964MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000965 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000966 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000967 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000968 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000969 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000970 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000971 QualType ty,
972 ExprValueKind vk,
973 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000974 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000975
Douglas Gregor40d96a62011-02-28 21:54:11 +0000976 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000977 founddecl.getDecl() != memberdecl ||
978 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000979 if (hasQualOrFound)
980 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000981
John McCalld5532b62009-11-23 01:53:49 +0000982 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000983 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
984 else if (TemplateKWLoc.isValid())
985 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Chris Lattner32488542010-10-30 05:14:06 +0000987 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000988 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
989 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000990
991 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000992 // FIXME: Wrong. We should be looking at the member declaration we found.
993 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000994 E->setValueDependent(true);
995 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +0000996 E->setInstantiationDependent(true);
997 }
998 else if (QualifierLoc &&
999 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1000 E->setInstantiationDependent(true);
1001
John McCall6bb80172010-03-30 21:47:33 +00001002 E->HasQualifierOrFoundDecl = true;
1003
1004 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001005 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001006 NQ->FoundDecl = founddecl;
1007 }
1008
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001009 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1010
John McCall6bb80172010-03-30 21:47:33 +00001011 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001012 bool Dependent = false;
1013 bool InstantiationDependent = false;
1014 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001015 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1016 Dependent,
1017 InstantiationDependent,
1018 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001019 if (InstantiationDependent)
1020 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001021 } else if (TemplateKWLoc.isValid()) {
1022 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001023 }
1024
1025 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001026}
1027
Douglas Gregor75e85042011-03-02 21:06:53 +00001028SourceRange MemberExpr::getSourceRange() const {
Daniel Dunbar396ec672012-03-09 15:39:15 +00001029 return SourceRange(getLocStart(), getLocEnd());
1030}
1031SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001032 if (isImplicitAccess()) {
1033 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001034 return getQualifierLoc().getBeginLoc();
1035 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001036 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001037
Daniel Dunbar396ec672012-03-09 15:39:15 +00001038 // FIXME: We don't want this to happen. Rather, we should be able to
1039 // detect all kinds of implicit accesses more cleanly.
1040 SourceLocation BaseStartLoc = getBase()->getLocStart();
1041 if (BaseStartLoc.isValid())
1042 return BaseStartLoc;
1043 return MemberLoc;
1044}
1045SourceLocation MemberExpr::getLocEnd() const {
1046 if (hasExplicitTemplateArgs())
1047 return getRAngleLoc();
1048 return getMemberNameInfo().getEndLoc();
Douglas Gregor75e85042011-03-02 21:06:53 +00001049}
1050
John McCall1d9b3b22011-09-09 05:25:32 +00001051void CastExpr::CheckCastConsistency() const {
1052 switch (getCastKind()) {
1053 case CK_DerivedToBase:
1054 case CK_UncheckedDerivedToBase:
1055 case CK_DerivedToBaseMemberPointer:
1056 case CK_BaseToDerived:
1057 case CK_BaseToDerivedMemberPointer:
1058 assert(!path_empty() && "Cast kind should have a base path!");
1059 break;
1060
1061 case CK_CPointerToObjCPointerCast:
1062 assert(getType()->isObjCObjectPointerType());
1063 assert(getSubExpr()->getType()->isPointerType());
1064 goto CheckNoBasePath;
1065
1066 case CK_BlockPointerToObjCPointerCast:
1067 assert(getType()->isObjCObjectPointerType());
1068 assert(getSubExpr()->getType()->isBlockPointerType());
1069 goto CheckNoBasePath;
1070
John McCall4d4e5c12012-02-15 01:22:51 +00001071 case CK_ReinterpretMemberPointer:
1072 assert(getType()->isMemberPointerType());
1073 assert(getSubExpr()->getType()->isMemberPointerType());
1074 goto CheckNoBasePath;
1075
John McCall1d9b3b22011-09-09 05:25:32 +00001076 case CK_BitCast:
1077 // Arbitrary casts to C pointer types count as bitcasts.
1078 // Otherwise, we should only have block and ObjC pointer casts
1079 // here if they stay within the type kind.
1080 if (!getType()->isPointerType()) {
1081 assert(getType()->isObjCObjectPointerType() ==
1082 getSubExpr()->getType()->isObjCObjectPointerType());
1083 assert(getType()->isBlockPointerType() ==
1084 getSubExpr()->getType()->isBlockPointerType());
1085 }
1086 goto CheckNoBasePath;
1087
1088 case CK_AnyPointerToBlockPointerCast:
1089 assert(getType()->isBlockPointerType());
1090 assert(getSubExpr()->getType()->isAnyPointerType() &&
1091 !getSubExpr()->getType()->isBlockPointerType());
1092 goto CheckNoBasePath;
1093
Douglas Gregorac1303e2012-02-22 05:02:47 +00001094 case CK_CopyAndAutoreleaseBlockObject:
1095 assert(getType()->isBlockPointerType());
1096 assert(getSubExpr()->getType()->isBlockPointerType());
1097 goto CheckNoBasePath;
1098
John McCall1d9b3b22011-09-09 05:25:32 +00001099 // These should not have an inheritance path.
1100 case CK_Dynamic:
1101 case CK_ToUnion:
1102 case CK_ArrayToPointerDecay:
1103 case CK_FunctionToPointerDecay:
1104 case CK_NullToMemberPointer:
1105 case CK_NullToPointer:
1106 case CK_ConstructorConversion:
1107 case CK_IntegralToPointer:
1108 case CK_PointerToIntegral:
1109 case CK_ToVoid:
1110 case CK_VectorSplat:
1111 case CK_IntegralCast:
1112 case CK_IntegralToFloating:
1113 case CK_FloatingToIntegral:
1114 case CK_FloatingCast:
1115 case CK_ObjCObjectLValueCast:
1116 case CK_FloatingRealToComplex:
1117 case CK_FloatingComplexToReal:
1118 case CK_FloatingComplexCast:
1119 case CK_FloatingComplexToIntegralComplex:
1120 case CK_IntegralRealToComplex:
1121 case CK_IntegralComplexToReal:
1122 case CK_IntegralComplexCast:
1123 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001124 case CK_ARCProduceObject:
1125 case CK_ARCConsumeObject:
1126 case CK_ARCReclaimReturnedObject:
1127 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001128 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1129 goto CheckNoBasePath;
1130
1131 case CK_Dependent:
1132 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001133 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001134 case CK_AtomicToNonAtomic:
1135 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001136 case CK_PointerToBoolean:
1137 case CK_IntegralToBoolean:
1138 case CK_FloatingToBoolean:
1139 case CK_MemberPointerToBoolean:
1140 case CK_FloatingComplexToBoolean:
1141 case CK_IntegralComplexToBoolean:
1142 case CK_LValueBitCast: // -> bool&
1143 case CK_UserDefinedConversion: // operator bool()
1144 CheckNoBasePath:
1145 assert(path_empty() && "Cast kind should not have a base path!");
1146 break;
1147 }
1148}
1149
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001150const char *CastExpr::getCastKindName() const {
1151 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001152 case CK_Dependent:
1153 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001154 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001155 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001156 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001157 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001158 case CK_LValueToRValue:
1159 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001160 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001161 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001162 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001163 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001164 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001165 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001166 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001167 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001168 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001169 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001170 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001171 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001172 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001173 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001174 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001175 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001176 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001177 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001178 case CK_NullToPointer:
1179 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001180 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001181 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001182 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001183 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001184 case CK_ReinterpretMemberPointer:
1185 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001186 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001187 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001188 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001189 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001190 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001191 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001192 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001193 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001194 case CK_PointerToBoolean:
1195 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001196 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001197 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001198 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001199 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001200 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001201 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001202 case CK_IntegralToBoolean:
1203 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001204 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001205 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001206 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001207 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001208 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001209 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001210 case CK_FloatingToBoolean:
1211 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001212 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001213 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001214 case CK_CPointerToObjCPointerCast:
1215 return "CPointerToObjCPointerCast";
1216 case CK_BlockPointerToObjCPointerCast:
1217 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001218 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001219 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001220 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001221 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001222 case CK_FloatingRealToComplex:
1223 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001224 case CK_FloatingComplexToReal:
1225 return "FloatingComplexToReal";
1226 case CK_FloatingComplexToBoolean:
1227 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001228 case CK_FloatingComplexCast:
1229 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001230 case CK_FloatingComplexToIntegralComplex:
1231 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001232 case CK_IntegralRealToComplex:
1233 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001234 case CK_IntegralComplexToReal:
1235 return "IntegralComplexToReal";
1236 case CK_IntegralComplexToBoolean:
1237 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001238 case CK_IntegralComplexCast:
1239 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001240 case CK_IntegralComplexToFloatingComplex:
1241 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001242 case CK_ARCConsumeObject:
1243 return "ARCConsumeObject";
1244 case CK_ARCProduceObject:
1245 return "ARCProduceObject";
1246 case CK_ARCReclaimReturnedObject:
1247 return "ARCReclaimReturnedObject";
1248 case CK_ARCExtendBlockObject:
1249 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001250 case CK_AtomicToNonAtomic:
1251 return "AtomicToNonAtomic";
1252 case CK_NonAtomicToAtomic:
1253 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001254 case CK_CopyAndAutoreleaseBlockObject:
1255 return "CopyAndAutoreleaseBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001256 }
Mike Stump1eb44332009-09-09 15:08:12 +00001257
John McCall2bb5d002010-11-13 09:02:35 +00001258 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001259}
1260
Douglas Gregor6eef5192009-12-14 19:27:10 +00001261Expr *CastExpr::getSubExprAsWritten() {
1262 Expr *SubExpr = 0;
1263 CastExpr *E = this;
1264 do {
1265 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001266
1267 // Skip through reference binding to temporary.
1268 if (MaterializeTemporaryExpr *Materialize
1269 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1270 SubExpr = Materialize->GetTemporaryExpr();
1271
Douglas Gregor6eef5192009-12-14 19:27:10 +00001272 // Skip any temporary bindings; they're implicit.
1273 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1274 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001275
Douglas Gregor6eef5192009-12-14 19:27:10 +00001276 // Conversions by constructor and conversion functions have a
1277 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001278 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001279 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001280 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001281 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001282
Douglas Gregor6eef5192009-12-14 19:27:10 +00001283 // If the subexpression we're left with is an implicit cast, look
1284 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001285 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1286
Douglas Gregor6eef5192009-12-14 19:27:10 +00001287 return SubExpr;
1288}
1289
John McCallf871d0c2010-08-07 06:22:56 +00001290CXXBaseSpecifier **CastExpr::path_buffer() {
1291 switch (getStmtClass()) {
1292#define ABSTRACT_STMT(x)
1293#define CASTEXPR(Type, Base) \
1294 case Stmt::Type##Class: \
1295 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1296#define STMT(Type, Base)
1297#include "clang/AST/StmtNodes.inc"
1298 default:
1299 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001300 }
1301}
1302
1303void CastExpr::setCastPath(const CXXCastPath &Path) {
1304 assert(Path.size() == path_size());
1305 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1306}
1307
1308ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1309 CastKind Kind, Expr *Operand,
1310 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001311 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001312 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1313 void *Buffer =
1314 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1315 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001316 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001317 if (PathSize) E->setCastPath(*BasePath);
1318 return E;
1319}
1320
1321ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1322 unsigned PathSize) {
1323 void *Buffer =
1324 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1325 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1326}
1327
1328
1329CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001330 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001331 const CXXCastPath *BasePath,
1332 TypeSourceInfo *WrittenTy,
1333 SourceLocation L, SourceLocation R) {
1334 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1335 void *Buffer =
1336 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1337 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001338 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001339 if (PathSize) E->setCastPath(*BasePath);
1340 return E;
1341}
1342
1343CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1344 void *Buffer =
1345 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1346 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1347}
1348
Reid Spencer5f016e22007-07-11 17:01:13 +00001349/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1350/// corresponds to, e.g. "<<=".
1351const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1352 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001353 case BO_PtrMemD: return ".*";
1354 case BO_PtrMemI: return "->*";
1355 case BO_Mul: return "*";
1356 case BO_Div: return "/";
1357 case BO_Rem: return "%";
1358 case BO_Add: return "+";
1359 case BO_Sub: return "-";
1360 case BO_Shl: return "<<";
1361 case BO_Shr: return ">>";
1362 case BO_LT: return "<";
1363 case BO_GT: return ">";
1364 case BO_LE: return "<=";
1365 case BO_GE: return ">=";
1366 case BO_EQ: return "==";
1367 case BO_NE: return "!=";
1368 case BO_And: return "&";
1369 case BO_Xor: return "^";
1370 case BO_Or: return "|";
1371 case BO_LAnd: return "&&";
1372 case BO_LOr: return "||";
1373 case BO_Assign: return "=";
1374 case BO_MulAssign: return "*=";
1375 case BO_DivAssign: return "/=";
1376 case BO_RemAssign: return "%=";
1377 case BO_AddAssign: return "+=";
1378 case BO_SubAssign: return "-=";
1379 case BO_ShlAssign: return "<<=";
1380 case BO_ShrAssign: return ">>=";
1381 case BO_AndAssign: return "&=";
1382 case BO_XorAssign: return "^=";
1383 case BO_OrAssign: return "|=";
1384 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001386
David Blaikie30263482012-01-20 21:50:17 +00001387 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001388}
1389
John McCall2de56d12010-08-25 11:45:40 +00001390BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001391BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1392 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001393 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001394 case OO_Plus: return BO_Add;
1395 case OO_Minus: return BO_Sub;
1396 case OO_Star: return BO_Mul;
1397 case OO_Slash: return BO_Div;
1398 case OO_Percent: return BO_Rem;
1399 case OO_Caret: return BO_Xor;
1400 case OO_Amp: return BO_And;
1401 case OO_Pipe: return BO_Or;
1402 case OO_Equal: return BO_Assign;
1403 case OO_Less: return BO_LT;
1404 case OO_Greater: return BO_GT;
1405 case OO_PlusEqual: return BO_AddAssign;
1406 case OO_MinusEqual: return BO_SubAssign;
1407 case OO_StarEqual: return BO_MulAssign;
1408 case OO_SlashEqual: return BO_DivAssign;
1409 case OO_PercentEqual: return BO_RemAssign;
1410 case OO_CaretEqual: return BO_XorAssign;
1411 case OO_AmpEqual: return BO_AndAssign;
1412 case OO_PipeEqual: return BO_OrAssign;
1413 case OO_LessLess: return BO_Shl;
1414 case OO_GreaterGreater: return BO_Shr;
1415 case OO_LessLessEqual: return BO_ShlAssign;
1416 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1417 case OO_EqualEqual: return BO_EQ;
1418 case OO_ExclaimEqual: return BO_NE;
1419 case OO_LessEqual: return BO_LE;
1420 case OO_GreaterEqual: return BO_GE;
1421 case OO_AmpAmp: return BO_LAnd;
1422 case OO_PipePipe: return BO_LOr;
1423 case OO_Comma: return BO_Comma;
1424 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001425 }
1426}
1427
1428OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1429 static const OverloadedOperatorKind OverOps[] = {
1430 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1431 OO_Star, OO_Slash, OO_Percent,
1432 OO_Plus, OO_Minus,
1433 OO_LessLess, OO_GreaterGreater,
1434 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1435 OO_EqualEqual, OO_ExclaimEqual,
1436 OO_Amp,
1437 OO_Caret,
1438 OO_Pipe,
1439 OO_AmpAmp,
1440 OO_PipePipe,
1441 OO_Equal, OO_StarEqual,
1442 OO_SlashEqual, OO_PercentEqual,
1443 OO_PlusEqual, OO_MinusEqual,
1444 OO_LessLessEqual, OO_GreaterGreaterEqual,
1445 OO_AmpEqual, OO_CaretEqual,
1446 OO_PipeEqual,
1447 OO_Comma
1448 };
1449 return OverOps[Opc];
1450}
1451
Ted Kremenek709210f2010-04-13 23:39:13 +00001452InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001453 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001454 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001455 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001456 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001457 InitExprs(C, numInits),
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001458 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0)
1459{
1460 sawArrayRangeDesignator(false);
1461 setInitializesStdInitializerList(false);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001462 for (unsigned I = 0; I != numInits; ++I) {
1463 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001464 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001465 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001466 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001467 if (initExprs[I]->isInstantiationDependent())
1468 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001469 if (initExprs[I]->containsUnexpandedParameterPack())
1470 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001471 }
Sean Huntc3021132010-05-05 15:23:54 +00001472
Ted Kremenek709210f2010-04-13 23:39:13 +00001473 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001474}
Reid Spencer5f016e22007-07-11 17:01:13 +00001475
Ted Kremenek709210f2010-04-13 23:39:13 +00001476void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001477 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001478 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001479}
1480
Ted Kremenek709210f2010-04-13 23:39:13 +00001481void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001482 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001483}
1484
Ted Kremenek709210f2010-04-13 23:39:13 +00001485Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001486 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001487 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001488 InitExprs.back() = expr;
1489 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001490 }
Mike Stump1eb44332009-09-09 15:08:12 +00001491
Douglas Gregor4c678342009-01-28 21:54:33 +00001492 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1493 InitExprs[Init] = expr;
1494 return Result;
1495}
1496
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001497void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001498 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001499 ArrayFillerOrUnionFieldInit = filler;
1500 // Fill out any "holes" in the array due to designated initializers.
1501 Expr **inits = getInits();
1502 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1503 if (inits[i] == 0)
1504 inits[i] = filler;
1505}
1506
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001507SourceRange InitListExpr::getSourceRange() const {
1508 if (SyntacticForm)
1509 return SyntacticForm->getSourceRange();
1510 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1511 if (Beg.isInvalid()) {
1512 // Find the first non-null initializer.
1513 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1514 E = InitExprs.end();
1515 I != E; ++I) {
1516 if (Stmt *S = *I) {
1517 Beg = S->getLocStart();
1518 break;
1519 }
1520 }
1521 }
1522 if (End.isInvalid()) {
1523 // Find the first non-null initializer from the end.
1524 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1525 E = InitExprs.rend();
1526 I != E; ++I) {
1527 if (Stmt *S = *I) {
1528 End = S->getSourceRange().getEnd();
1529 break;
1530 }
1531 }
1532 }
1533 return SourceRange(Beg, End);
1534}
1535
Steve Naroffbfdcae62008-09-04 15:31:07 +00001536/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001537///
John McCalla345edb2012-02-17 03:32:35 +00001538const FunctionProtoType *BlockExpr::getFunctionType() const {
1539 // The block pointer is never sugared, but the function type might be.
1540 return cast<BlockPointerType>(getType())
1541 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001542}
1543
Mike Stump1eb44332009-09-09 15:08:12 +00001544SourceLocation BlockExpr::getCaretLocation() const {
1545 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001546}
Mike Stump1eb44332009-09-09 15:08:12 +00001547const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001548 return TheBlock->getBody();
1549}
Mike Stump1eb44332009-09-09 15:08:12 +00001550Stmt *BlockExpr::getBody() {
1551 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001552}
Steve Naroff56ee6892008-10-08 17:01:13 +00001553
1554
Reid Spencer5f016e22007-07-11 17:01:13 +00001555//===----------------------------------------------------------------------===//
1556// Generic Expression Routines
1557//===----------------------------------------------------------------------===//
1558
Chris Lattner026dc962009-02-14 07:37:35 +00001559/// isUnusedResultAWarning - Return true if this immediate expression should
1560/// be warned about if the result is unused. If so, fill in Loc and Ranges
1561/// with location to warn on and the source range[s] to report with the
1562/// warning.
1563bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001564 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001565 // Don't warn if the expr is type dependent. The type could end up
1566 // instantiating to void.
1567 if (isTypeDependent())
1568 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Reid Spencer5f016e22007-07-11 17:01:13 +00001570 switch (getStmtClass()) {
1571 default:
John McCall0faede62010-03-12 07:11:26 +00001572 if (getType()->isVoidType())
1573 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001574 Loc = getExprLoc();
1575 R1 = getSourceRange();
1576 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001577 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001578 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001579 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001580 case GenericSelectionExprClass:
1581 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1582 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001583 case UnaryOperatorClass: {
1584 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Reid Spencer5f016e22007-07-11 17:01:13 +00001586 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001587 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001588 case UO_PostInc:
1589 case UO_PostDec:
1590 case UO_PreInc:
1591 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001592 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001593 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001594 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001595 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001596 return false;
1597 break;
John McCall2de56d12010-08-25 11:45:40 +00001598 case UO_Real:
1599 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001600 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001601 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1602 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001603 return false;
1604 break;
John McCall2de56d12010-08-25 11:45:40 +00001605 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001606 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001607 }
Chris Lattner026dc962009-02-14 07:37:35 +00001608 Loc = UO->getOperatorLoc();
1609 R1 = UO->getSubExpr()->getSourceRange();
1610 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001611 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001612 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001613 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001614 switch (BO->getOpcode()) {
1615 default:
1616 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001617 // Consider the RHS of comma for side effects. LHS was checked by
1618 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001619 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001620 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1621 // lvalue-ness) of an assignment written in a macro.
1622 if (IntegerLiteral *IE =
1623 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1624 if (IE->getValue() == 0)
1625 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001626 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1627 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001628 case BO_LAnd:
1629 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001630 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1631 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1632 return false;
1633 break;
John McCallbf0ee352010-02-16 04:10:53 +00001634 }
Chris Lattner026dc962009-02-14 07:37:35 +00001635 if (BO->isAssignmentOp())
1636 return false;
1637 Loc = BO->getOperatorLoc();
1638 R1 = BO->getLHS()->getSourceRange();
1639 R2 = BO->getRHS()->getSourceRange();
1640 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001641 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001642 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001643 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001644 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001645 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001646
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001647 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001648 // If only one of the LHS or RHS is a warning, the operator might
1649 // be being used for control flow. Only warn if both the LHS and
1650 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001651 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001652 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1653 return false;
1654 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001655 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001656 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001657 }
1658
Reid Spencer5f016e22007-07-11 17:01:13 +00001659 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001660 // If the base pointer or element is to a volatile pointer/field, accessing
1661 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001662 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001663 return false;
1664 Loc = cast<MemberExpr>(this)->getMemberLoc();
1665 R1 = SourceRange(Loc, Loc);
1666 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1667 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001668
Reid Spencer5f016e22007-07-11 17:01:13 +00001669 case ArraySubscriptExprClass:
1670 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001671 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001672 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001673 return false;
1674 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1675 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1676 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1677 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001678
Chandler Carruth9b106832011-08-17 09:49:44 +00001679 case CXXOperatorCallExprClass: {
1680 // We warn about operator== and operator!= even when user-defined operator
1681 // overloads as there is no reasonable way to define these such that they
1682 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1683 // warning: these operators are commonly typo'ed, and so warning on them
1684 // provides additional value as well. If this list is updated,
1685 // DiagnoseUnusedComparison should be as well.
1686 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1687 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001688 Op->getOperator() == OO_ExclaimEqual) {
1689 Loc = Op->getOperatorLoc();
1690 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001691 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001692 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001693
1694 // Fallthrough for generic call handling.
1695 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001696 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00001697 case CXXMemberCallExprClass:
1698 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001699 // If this is a direct call, get the callee.
1700 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001701 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001702 // If the callee has attribute pure, const, or warn_unused_result, warn
1703 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001704 //
1705 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1706 // updated to match for QoI.
1707 if (FD->getAttr<WarnUnusedResultAttr>() ||
1708 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1709 Loc = CE->getCallee()->getLocStart();
1710 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001711
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001712 if (unsigned NumArgs = CE->getNumArgs())
1713 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1714 CE->getArg(NumArgs-1)->getLocEnd());
1715 return true;
1716 }
Chris Lattner026dc962009-02-14 07:37:35 +00001717 }
1718 return false;
1719 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001720
1721 case CXXTemporaryObjectExprClass:
1722 case CXXConstructExprClass:
1723 return false;
1724
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001725 case ObjCMessageExprClass: {
1726 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001727 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1728 ME->isInstanceMessage() &&
1729 !ME->getType()->isVoidType() &&
1730 ME->getSelector().getIdentifierInfoForSlot(0) &&
1731 ME->getSelector().getIdentifierInfoForSlot(0)
1732 ->getName().startswith("init")) {
1733 Loc = getExprLoc();
1734 R1 = ME->getSourceRange();
1735 return true;
1736 }
1737
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001738 const ObjCMethodDecl *MD = ME->getMethodDecl();
1739 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1740 Loc = getExprLoc();
1741 return true;
1742 }
Chris Lattner026dc962009-02-14 07:37:35 +00001743 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001744 }
Mike Stump1eb44332009-09-09 15:08:12 +00001745
John McCall12f78a62010-12-02 01:19:52 +00001746 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001747 Loc = getExprLoc();
1748 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001749 return true;
John McCall12f78a62010-12-02 01:19:52 +00001750
John McCall4b9c2d22011-11-06 09:01:30 +00001751 case PseudoObjectExprClass: {
1752 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1753
1754 // Only complain about things that have the form of a getter.
1755 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1756 isa<BinaryOperator>(PO->getSyntacticForm()))
1757 return false;
1758
1759 Loc = getExprLoc();
1760 R1 = getSourceRange();
1761 return true;
1762 }
1763
Chris Lattner611b2ec2008-07-26 19:51:01 +00001764 case StmtExprClass: {
1765 // Statement exprs don't logically have side effects themselves, but are
1766 // sometimes used in macros in ways that give them a type that is unused.
1767 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1768 // however, if the result of the stmt expr is dead, we don't want to emit a
1769 // warning.
1770 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001771 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001772 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001773 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001774 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1775 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1776 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1777 }
Mike Stump1eb44332009-09-09 15:08:12 +00001778
John McCall0faede62010-03-12 07:11:26 +00001779 if (getType()->isVoidType())
1780 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001781 Loc = cast<StmtExpr>(this)->getLParenLoc();
1782 R1 = getSourceRange();
1783 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001784 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001785 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001786 // If this is an explicit cast to void, allow it. People do this when they
1787 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001788 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001789 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001790 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1791 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1792 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001793 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001794 if (getType()->isVoidType())
1795 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001796 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001797
Anders Carlsson58beed92009-11-17 17:11:23 +00001798 // If this is a cast to void or a constructor conversion, check the operand.
1799 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001800 if (CE->getCastKind() == CK_ToVoid ||
1801 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001802 return (cast<CastExpr>(this)->getSubExpr()
1803 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001804 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1805 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1806 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001807 }
Mike Stump1eb44332009-09-09 15:08:12 +00001808
Eli Friedman4be1f472008-05-19 21:24:43 +00001809 case ImplicitCastExprClass:
1810 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001811 return (cast<ImplicitCastExpr>(this)
1812 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001813
Chris Lattner04421082008-04-08 04:40:51 +00001814 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001815 return (cast<CXXDefaultArgExpr>(this)
1816 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001817
1818 case CXXNewExprClass:
1819 // FIXME: In theory, there might be new expressions that don't have side
1820 // effects (e.g. a placement new with an uninitialized POD).
1821 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001822 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001823 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001824 return (cast<CXXBindTemporaryExpr>(this)
1825 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001826 case ExprWithCleanupsClass:
1827 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001828 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001829 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001830}
1831
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001832/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001833/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001834bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001835 const Expr *E = IgnoreParens();
1836 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001837 default:
1838 return false;
1839 case ObjCIvarRefExprClass:
1840 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001841 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001842 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001843 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001844 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001845 case MaterializeTemporaryExprClass:
1846 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1847 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001848 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001849 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001850 case BlockDeclRefExprClass:
Douglas Gregora2813ce2009-10-23 18:54:35 +00001851 case DeclRefExprClass: {
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001852
1853 const Decl *D;
1854 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1855 D = BDRE->getDecl();
1856 else
1857 D = cast<DeclRefExpr>(E)->getDecl();
1858
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001859 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1860 if (VD->hasGlobalStorage())
1861 return true;
1862 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001863 // dereferencing to a pointer is always a gc'able candidate,
1864 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001865 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001866 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001867 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001868 return false;
1869 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001870 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001871 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001872 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001873 }
1874 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001875 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001876 }
1877}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001878
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001879bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1880 if (isTypeDependent())
1881 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001882 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001883}
1884
John McCall864c0412011-04-26 20:42:42 +00001885QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00001886 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00001887
1888 // Bound member expressions are always one of these possibilities:
1889 // x->m x.m x->*y x.*y
1890 // (possibly parenthesized)
1891
1892 expr = expr->IgnoreParens();
1893 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1894 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1895 return mem->getMemberDecl()->getType();
1896 }
1897
1898 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1899 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1900 ->getPointeeType();
1901 assert(type->isFunctionType());
1902 return type;
1903 }
1904
1905 assert(isa<UnresolvedMemberExpr>(expr));
1906 return QualType();
1907}
1908
Sebastian Redl369e51f2010-09-10 20:55:33 +00001909static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1910 Expr::CanThrowResult CT2) {
1911 // CanThrowResult constants are ordered so that the maximum is the correct
1912 // merge result.
1913 return CT1 > CT2 ? CT1 : CT2;
1914}
1915
1916static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1917 Expr *E = const_cast<Expr*>(CE);
1918 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001919 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001920 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1921 }
1922 return R;
1923}
1924
Richard Smith7a614d82011-06-11 17:19:42 +00001925static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1926 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001927 bool NullThrows = true) {
1928 if (!D)
1929 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1930
1931 // See if we can get a function type from the decl somehow.
1932 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1933 if (!VD) // If we have no clue what we're calling, assume the worst.
1934 return Expr::CT_Can;
1935
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001936 // As an extension, we assume that __attribute__((nothrow)) functions don't
1937 // throw.
1938 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1939 return Expr::CT_Cannot;
1940
Sebastian Redl369e51f2010-09-10 20:55:33 +00001941 QualType T = VD->getType();
1942 const FunctionProtoType *FT;
1943 if ((FT = T->getAs<FunctionProtoType>())) {
1944 } else if (const PointerType *PT = T->getAs<PointerType>())
1945 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1946 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1947 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1948 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1949 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1950 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1951 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1952
1953 if (!FT)
1954 return Expr::CT_Can;
1955
Richard Smith7a614d82011-06-11 17:19:42 +00001956 if (FT->getExceptionSpecType() == EST_Delayed) {
1957 assert(isa<CXXConstructorDecl>(D) &&
1958 "only constructor exception specs can be unknown");
1959 Ctx.getDiagnostics().Report(E->getLocStart(),
1960 diag::err_exception_spec_unknown)
1961 << E->getSourceRange();
1962 return Expr::CT_Can;
1963 }
1964
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001965 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001966}
1967
1968static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1969 if (DC->isTypeDependent())
1970 return Expr::CT_Dependent;
1971
Sebastian Redl295995c2010-09-10 20:55:47 +00001972 if (!DC->getTypeAsWritten()->isReferenceType())
1973 return Expr::CT_Cannot;
1974
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001975 if (DC->getSubExpr()->isTypeDependent())
1976 return Expr::CT_Dependent;
1977
Sebastian Redl369e51f2010-09-10 20:55:33 +00001978 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1979}
1980
1981static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1982 const CXXTypeidExpr *DC) {
1983 if (DC->isTypeOperand())
1984 return Expr::CT_Cannot;
1985
1986 Expr *Op = DC->getExprOperand();
1987 if (Op->isTypeDependent())
1988 return Expr::CT_Dependent;
1989
1990 const RecordType *RT = Op->getType()->getAs<RecordType>();
1991 if (!RT)
1992 return Expr::CT_Cannot;
1993
1994 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1995 return Expr::CT_Cannot;
1996
1997 if (Op->Classify(C).isPRValue())
1998 return Expr::CT_Cannot;
1999
2000 return Expr::CT_Can;
2001}
2002
2003Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
2004 // C++ [expr.unary.noexcept]p3:
2005 // [Can throw] if in a potentially-evaluated context the expression would
2006 // contain:
2007 switch (getStmtClass()) {
2008 case CXXThrowExprClass:
2009 // - a potentially evaluated throw-expression
2010 return CT_Can;
2011
2012 case CXXDynamicCastExprClass: {
2013 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
2014 // where T is a reference type, that requires a run-time check
2015 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
2016 if (CT == CT_Can)
2017 return CT;
2018 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2019 }
2020
2021 case CXXTypeidExprClass:
2022 // - a potentially evaluated typeid expression applied to a glvalue
2023 // expression whose type is a polymorphic class type
2024 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
2025
2026 // - a potentially evaluated call to a function, member function, function
2027 // pointer, or member function pointer that does not have a non-throwing
2028 // exception-specification
2029 case CallExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002030 case CXXMemberCallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00002031 case CXXOperatorCallExprClass:
2032 case UserDefinedLiteralClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00002033 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002034 CanThrowResult CT;
2035 if (isTypeDependent())
2036 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00002037 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
2038 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002039 else
Richard Smith7a614d82011-06-11 17:19:42 +00002040 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00002041 if (CT == CT_Can)
2042 return CT;
2043 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2044 }
2045
Sebastian Redl295995c2010-09-10 20:55:47 +00002046 case CXXConstructExprClass:
2047 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00002048 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00002049 cast<CXXConstructExpr>(this)->getConstructor());
2050 if (CT == CT_Can)
2051 return CT;
2052 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2053 }
2054
Douglas Gregor01d08012012-02-07 10:09:13 +00002055 case LambdaExprClass: {
2056 const LambdaExpr *Lambda = cast<LambdaExpr>(this);
2057 CanThrowResult CT = Expr::CT_Cannot;
2058 for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(),
2059 CapEnd = Lambda->capture_init_end();
2060 Cap != CapEnd; ++Cap)
2061 CT = MergeCanThrow(CT, (*Cap)->CanThrow(C));
2062 return CT;
2063 }
2064
Sebastian Redl369e51f2010-09-10 20:55:33 +00002065 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002066 CanThrowResult CT;
2067 if (isTypeDependent())
2068 CT = CT_Dependent;
2069 else
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002070 CT = CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew());
Sebastian Redl369e51f2010-09-10 20:55:33 +00002071 if (CT == CT_Can)
2072 return CT;
2073 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2074 }
2075
2076 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002077 CanThrowResult CT;
2078 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
2079 if (DTy.isNull() || DTy->isDependentType()) {
2080 CT = CT_Dependent;
2081 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00002082 CT = CanCalleeThrow(C, this,
2083 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002084 if (const RecordType *RT = DTy->getAs<RecordType>()) {
2085 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00002086 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002087 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002088 if (CT == CT_Can)
2089 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002090 }
2091 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2092 }
2093
2094 case CXXBindTemporaryExprClass: {
2095 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00002096 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002097 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
2098 if (CT == CT_Can)
2099 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00002100 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2101 }
2102
2103 // ObjC message sends are like function calls, but never have exception
2104 // specs.
2105 case ObjCMessageExprClass:
2106 case ObjCPropertyRefExprClass:
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002107 case ObjCSubscriptRefExprClass:
2108 return CT_Can;
2109
2110 // All the ObjC literals that are implemented as calls are
2111 // potentially throwing unless we decide to close off that
2112 // possibility.
2113 case ObjCArrayLiteralClass:
2114 case ObjCBoolLiteralExprClass:
2115 case ObjCDictionaryLiteralClass:
2116 case ObjCNumericLiteralClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002117 return CT_Can;
2118
2119 // Many other things have subexpressions, so we have to test those.
2120 // Some are simple:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002121 case ConditionalOperatorClass:
2122 case CompoundLiteralExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002123 case CXXConstCastExprClass:
2124 case CXXDefaultArgExprClass:
2125 case CXXReinterpretCastExprClass:
2126 case DesignatedInitExprClass:
2127 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002128 case ExtVectorElementExprClass:
2129 case InitListExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002130 case MemberExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002131 case ObjCIsaExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002132 case ObjCIvarRefExprClass:
2133 case ParenExprClass:
2134 case ParenListExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002135 case ShuffleVectorExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002136 case VAArgExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002137 return CanSubExprsThrow(C, this);
2138
2139 // Some might be dependent for other reasons.
Sebastian Redl369e51f2010-09-10 20:55:33 +00002140 case ArraySubscriptExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002141 case BinaryOperatorClass:
2142 case CompoundAssignOperatorClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002143 case CStyleCastExprClass:
2144 case CXXStaticCastExprClass:
2145 case CXXFunctionalCastExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002146 case ImplicitCastExprClass:
2147 case MaterializeTemporaryExprClass:
2148 case UnaryOperatorClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002149 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2150 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2151 }
2152
2153 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2154 case StmtExprClass:
2155 return CT_Can;
2156
2157 case ChooseExprClass:
2158 if (isTypeDependent() || isValueDependent())
2159 return CT_Dependent;
2160 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2161
Peter Collingbournef111d932011-04-15 00:35:48 +00002162 case GenericSelectionExprClass:
2163 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2164 return CT_Dependent;
2165 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2166
Sebastian Redl369e51f2010-09-10 20:55:33 +00002167 // Some expressions are always dependent.
Sebastian Redl369e51f2010-09-10 20:55:33 +00002168 case CXXDependentScopeMemberExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002169 case CXXUnresolvedConstructExprClass:
2170 case DependentScopeDeclRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002171 return CT_Dependent;
2172
Eli Friedmanc9674be2012-01-31 01:21:45 +00002173 case AtomicExprClass:
2174 case AsTypeExprClass:
2175 case BinaryConditionalOperatorClass:
2176 case BlockExprClass:
2177 case BlockDeclRefExprClass:
2178 case CUDAKernelCallExprClass:
2179 case DeclRefExprClass:
2180 case ObjCBridgedCastExprClass:
2181 case ObjCIndirectCopyRestoreExprClass:
2182 case ObjCProtocolExprClass:
2183 case ObjCSelectorExprClass:
2184 case OffsetOfExprClass:
2185 case PackExpansionExprClass:
2186 case PseudoObjectExprClass:
2187 case SubstNonTypeTemplateParmExprClass:
2188 case SubstNonTypeTemplateParmPackExprClass:
2189 case UnaryExprOrTypeTraitExprClass:
2190 case UnresolvedLookupExprClass:
2191 case UnresolvedMemberExprClass:
2192 // FIXME: Can any of the above throw? If so, when?
Sebastian Redl369e51f2010-09-10 20:55:33 +00002193 return CT_Cannot;
Eli Friedmanc9674be2012-01-31 01:21:45 +00002194
2195 case AddrLabelExprClass:
2196 case ArrayTypeTraitExprClass:
2197 case BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002198 case TypeTraitExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002199 case CXXBoolLiteralExprClass:
2200 case CXXNoexceptExprClass:
2201 case CXXNullPtrLiteralExprClass:
2202 case CXXPseudoDestructorExprClass:
2203 case CXXScalarValueInitExprClass:
2204 case CXXThisExprClass:
2205 case CXXUuidofExprClass:
2206 case CharacterLiteralClass:
2207 case ExpressionTraitExprClass:
2208 case FloatingLiteralClass:
2209 case GNUNullExprClass:
2210 case ImaginaryLiteralClass:
2211 case ImplicitValueInitExprClass:
2212 case IntegerLiteralClass:
2213 case ObjCEncodeExprClass:
2214 case ObjCStringLiteralClass:
2215 case OpaqueValueExprClass:
2216 case PredefinedExprClass:
2217 case SizeOfPackExprClass:
2218 case StringLiteralClass:
2219 case UnaryTypeTraitExprClass:
2220 // These expressions can never throw.
2221 return CT_Cannot;
2222
2223#define STMT(CLASS, PARENT) case CLASS##Class:
2224#define STMT_RANGE(Base, First, Last)
2225#define LAST_STMT_RANGE(BASE, FIRST, LAST)
2226#define EXPR(CLASS, PARENT)
2227#define ABSTRACT_STMT(STMT)
2228#include "clang/AST/StmtNodes.inc"
2229 case NoStmtClass:
2230 llvm_unreachable("Invalid class for expression");
Sebastian Redl369e51f2010-09-10 20:55:33 +00002231 }
Matt Beaumont-Gay56e68b72012-01-31 18:59:25 +00002232 llvm_unreachable("Bogus StmtClass");
Sebastian Redl369e51f2010-09-10 20:55:33 +00002233}
2234
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002235Expr* Expr::IgnoreParens() {
2236 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002237 while (true) {
2238 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2239 E = P->getSubExpr();
2240 continue;
2241 }
2242 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2243 if (P->getOpcode() == UO_Extension) {
2244 E = P->getSubExpr();
2245 continue;
2246 }
2247 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002248 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2249 if (!P->isResultDependent()) {
2250 E = P->getResultExpr();
2251 continue;
2252 }
2253 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002254 return E;
2255 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002256}
2257
Chris Lattner56f34942008-02-13 01:02:39 +00002258/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2259/// or CastExprs or ImplicitCastExprs, returning their operand.
2260Expr *Expr::IgnoreParenCasts() {
2261 Expr *E = this;
2262 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002263 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002264 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002265 continue;
2266 }
2267 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002268 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002269 continue;
2270 }
2271 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2272 if (P->getOpcode() == UO_Extension) {
2273 E = P->getSubExpr();
2274 continue;
2275 }
2276 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002277 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2278 if (!P->isResultDependent()) {
2279 E = P->getResultExpr();
2280 continue;
2281 }
2282 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002283 if (MaterializeTemporaryExpr *Materialize
2284 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2285 E = Materialize->GetTemporaryExpr();
2286 continue;
2287 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002288 if (SubstNonTypeTemplateParmExpr *NTTP
2289 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2290 E = NTTP->getReplacement();
2291 continue;
2292 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002293 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002294 }
2295}
2296
John McCall9c5d70c2010-12-04 08:24:19 +00002297/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2298/// casts. This is intended purely as a temporary workaround for code
2299/// that hasn't yet been rewritten to do the right thing about those
2300/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002301Expr *Expr::IgnoreParenLValueCasts() {
2302 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002303 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002304 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2305 E = P->getSubExpr();
2306 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002307 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002308 if (P->getCastKind() == CK_LValueToRValue) {
2309 E = P->getSubExpr();
2310 continue;
2311 }
John McCall9c5d70c2010-12-04 08:24:19 +00002312 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2313 if (P->getOpcode() == UO_Extension) {
2314 E = P->getSubExpr();
2315 continue;
2316 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002317 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2318 if (!P->isResultDependent()) {
2319 E = P->getResultExpr();
2320 continue;
2321 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002322 } else if (MaterializeTemporaryExpr *Materialize
2323 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2324 E = Materialize->GetTemporaryExpr();
2325 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002326 } else if (SubstNonTypeTemplateParmExpr *NTTP
2327 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2328 E = NTTP->getReplacement();
2329 continue;
John McCallf6a16482010-12-04 03:47:34 +00002330 }
2331 break;
2332 }
2333 return E;
2334}
2335
John McCall2fc46bf2010-05-05 22:59:52 +00002336Expr *Expr::IgnoreParenImpCasts() {
2337 Expr *E = this;
2338 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002339 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002340 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002341 continue;
2342 }
2343 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002344 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002345 continue;
2346 }
2347 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2348 if (P->getOpcode() == UO_Extension) {
2349 E = P->getSubExpr();
2350 continue;
2351 }
2352 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002353 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2354 if (!P->isResultDependent()) {
2355 E = P->getResultExpr();
2356 continue;
2357 }
2358 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002359 if (MaterializeTemporaryExpr *Materialize
2360 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2361 E = Materialize->GetTemporaryExpr();
2362 continue;
2363 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002364 if (SubstNonTypeTemplateParmExpr *NTTP
2365 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2366 E = NTTP->getReplacement();
2367 continue;
2368 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002369 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002370 }
2371}
2372
Hans Wennborg2f072b42011-06-09 17:06:51 +00002373Expr *Expr::IgnoreConversionOperator() {
2374 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002375 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002376 return MCE->getImplicitObjectArgument();
2377 }
2378 return this;
2379}
2380
Chris Lattnerecdd8412009-03-13 17:28:01 +00002381/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2382/// value (including ptr->int casts of the same size). Strip off any
2383/// ParenExpr or CastExprs, returning their operand.
2384Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2385 Expr *E = this;
2386 while (true) {
2387 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2388 E = P->getSubExpr();
2389 continue;
2390 }
Mike Stump1eb44332009-09-09 15:08:12 +00002391
Chris Lattnerecdd8412009-03-13 17:28:01 +00002392 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2393 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002394 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002395 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Chris Lattnerecdd8412009-03-13 17:28:01 +00002397 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2398 E = SE;
2399 continue;
2400 }
Mike Stump1eb44332009-09-09 15:08:12 +00002401
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002402 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002403 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002404 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002405 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002406 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2407 E = SE;
2408 continue;
2409 }
2410 }
Mike Stump1eb44332009-09-09 15:08:12 +00002411
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002412 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2413 if (P->getOpcode() == UO_Extension) {
2414 E = P->getSubExpr();
2415 continue;
2416 }
2417 }
2418
Peter Collingbournef111d932011-04-15 00:35:48 +00002419 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2420 if (!P->isResultDependent()) {
2421 E = P->getResultExpr();
2422 continue;
2423 }
2424 }
2425
Douglas Gregorc0244c52011-09-08 17:56:33 +00002426 if (SubstNonTypeTemplateParmExpr *NTTP
2427 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2428 E = NTTP->getReplacement();
2429 continue;
2430 }
2431
Chris Lattnerecdd8412009-03-13 17:28:01 +00002432 return E;
2433 }
2434}
2435
Douglas Gregor6eef5192009-12-14 19:27:10 +00002436bool Expr::isDefaultArgument() const {
2437 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002438 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2439 E = M->GetTemporaryExpr();
2440
Douglas Gregor6eef5192009-12-14 19:27:10 +00002441 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2442 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002443
Douglas Gregor6eef5192009-12-14 19:27:10 +00002444 return isa<CXXDefaultArgExpr>(E);
2445}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002446
Douglas Gregor2f599792010-04-02 18:24:57 +00002447/// \brief Skip over any no-op casts and any temporary-binding
2448/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002449static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002450 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2451 E = M->GetTemporaryExpr();
2452
Douglas Gregor2f599792010-04-02 18:24:57 +00002453 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002454 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002455 E = ICE->getSubExpr();
2456 else
2457 break;
2458 }
2459
2460 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2461 E = BE->getSubExpr();
2462
2463 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002464 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002465 E = ICE->getSubExpr();
2466 else
2467 break;
2468 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002469
2470 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002471}
2472
John McCall558d2ab2010-09-15 10:14:12 +00002473/// isTemporaryObject - Determines if this expression produces a
2474/// temporary of the given class type.
2475bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2476 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2477 return false;
2478
Anders Carlssonf8b30152010-11-28 16:40:49 +00002479 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002480
John McCall58277b52010-09-15 20:59:13 +00002481 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002482 if (!E->Classify(C).isPRValue()) {
2483 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002484 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002485 return false;
2486 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002487
John McCall19e60ad2010-09-16 06:57:56 +00002488 // Black-list a few cases which yield pr-values of class type that don't
2489 // refer to temporaries of that type:
2490
2491 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002492 if (isa<ImplicitCastExpr>(E)) {
2493 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2494 case CK_DerivedToBase:
2495 case CK_UncheckedDerivedToBase:
2496 return false;
2497 default:
2498 break;
2499 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002500 }
2501
John McCall19e60ad2010-09-16 06:57:56 +00002502 // - member expressions (all)
2503 if (isa<MemberExpr>(E))
2504 return false;
2505
John McCall56ca35d2011-02-17 10:25:35 +00002506 // - opaque values (all)
2507 if (isa<OpaqueValueExpr>(E))
2508 return false;
2509
John McCall558d2ab2010-09-15 10:14:12 +00002510 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002511}
2512
Douglas Gregor75e85042011-03-02 21:06:53 +00002513bool Expr::isImplicitCXXThis() const {
2514 const Expr *E = this;
2515
2516 // Strip away parentheses and casts we don't care about.
2517 while (true) {
2518 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2519 E = Paren->getSubExpr();
2520 continue;
2521 }
2522
2523 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2524 if (ICE->getCastKind() == CK_NoOp ||
2525 ICE->getCastKind() == CK_LValueToRValue ||
2526 ICE->getCastKind() == CK_DerivedToBase ||
2527 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2528 E = ICE->getSubExpr();
2529 continue;
2530 }
2531 }
2532
2533 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2534 if (UnOp->getOpcode() == UO_Extension) {
2535 E = UnOp->getSubExpr();
2536 continue;
2537 }
2538 }
2539
Douglas Gregor03e80032011-06-21 17:03:29 +00002540 if (const MaterializeTemporaryExpr *M
2541 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2542 E = M->GetTemporaryExpr();
2543 continue;
2544 }
2545
Douglas Gregor75e85042011-03-02 21:06:53 +00002546 break;
2547 }
2548
2549 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2550 return This->isImplicit();
2551
2552 return false;
2553}
2554
Douglas Gregor898574e2008-12-05 23:32:09 +00002555/// hasAnyTypeDependentArguments - Determines if any of the expressions
2556/// in Exprs is type-dependent.
Ahmed Charles13a140c2012-02-25 11:00:22 +00002557bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2558 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002559 if (Exprs[I]->isTypeDependent())
2560 return true;
2561
2562 return false;
2563}
2564
John McCall4204f072010-08-02 21:13:48 +00002565bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002566 // This function is attempting whether an expression is an initializer
2567 // which can be evaluated at compile-time. isEvaluatable handles most
2568 // of the cases, but it can't deal with some initializer-specific
2569 // expressions, and it can't deal with aggregates; we deal with those here,
2570 // and fall back to isEvaluatable for the other cases.
2571
John McCall4204f072010-08-02 21:13:48 +00002572 // If we ever capture reference-binding directly in the AST, we can
2573 // kill the second parameter.
2574
2575 if (IsForRef) {
2576 EvalResult Result;
2577 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2578 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002579
Anders Carlssone8a32b82008-11-24 05:23:59 +00002580 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002581 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002582 case IntegerLiteralClass:
2583 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002584 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002585 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002586 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002587 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002588 case CXXTemporaryObjectExprClass:
2589 case CXXConstructExprClass: {
2590 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002591
2592 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002593 if (CE->getConstructor()->isTrivial()) {
2594 // 1) an application of the trivial default constructor or
2595 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002596
Richard Smith180f4792011-11-10 06:34:14 +00002597 // 2) an elidable trivial copy construction of an operand which is
2598 // itself a constant initializer. Note that we consider the
2599 // operand on its own, *not* as a reference binding.
2600 if (CE->isElidable() &&
2601 CE->getArg(0)->isConstantInitializer(Ctx, false))
2602 return true;
2603 }
2604
2605 // 3) a foldable constexpr constructor.
2606 break;
John McCallb4b9b152010-08-01 21:51:45 +00002607 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002608 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002609 // This handles gcc's extension that allows global initializers like
2610 // "struct x {int x;} x = (struct x) {};".
2611 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002612 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002613 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002614 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002615 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002616 // FIXME: This doesn't deal with fields with reference types correctly.
2617 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2618 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002619 const InitListExpr *Exp = cast<InitListExpr>(this);
2620 unsigned numInits = Exp->getNumInits();
2621 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002622 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002623 return false;
2624 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002625 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002626 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002627 case ImplicitValueInitExprClass:
2628 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002629 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002630 return cast<ParenExpr>(this)->getSubExpr()
2631 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002632 case GenericSelectionExprClass:
2633 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2634 return false;
2635 return cast<GenericSelectionExpr>(this)->getResultExpr()
2636 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002637 case ChooseExprClass:
2638 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2639 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002640 case UnaryOperatorClass: {
2641 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002642 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002643 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002644 break;
2645 }
John McCall4204f072010-08-02 21:13:48 +00002646 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002647 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002648 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002649 case CStyleCastExprClass: {
2650 const CastExpr *CE = cast<CastExpr>(this);
2651
David Chisnall7a7ee302012-01-16 17:27:18 +00002652 // If we're promoting an integer to an _Atomic type then this is constant
2653 // if the integer is constant. We also need to check the converse in case
2654 // someone does something like:
2655 //
2656 // int a = (_Atomic(int))42;
2657 //
2658 // I doubt anyone would write code like this directly, but it's quite
2659 // possible as the result of macro expansions.
2660 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2661 CE->getCastKind() == CK_AtomicToNonAtomic)
2662 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2663
Richard Smithd62ca372011-12-06 22:44:34 +00002664 // Handle bitcasts of vector constants.
2665 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2666 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2667
Eli Friedman6bd97192011-12-21 00:43:02 +00002668 // Handle misc casts we want to ignore.
2669 // FIXME: Is it really safe to ignore all these?
2670 if (CE->getCastKind() == CK_NoOp ||
2671 CE->getCastKind() == CK_LValueToRValue ||
2672 CE->getCastKind() == CK_ToUnion ||
2673 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002674 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2675
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002676 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002677 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002678 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002679 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002680 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002681 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002682 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002683}
2684
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002685namespace {
2686 /// \brief Look for a call to a non-trivial function within an expression.
2687 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2688 {
2689 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2690
2691 bool NonTrivial;
2692
2693 public:
2694 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002695 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002696
2697 bool hasNonTrivialCall() const { return NonTrivial; }
2698
2699 void VisitCallExpr(CallExpr *E) {
2700 if (CXXMethodDecl *Method
2701 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2702 if (Method->isTrivial()) {
2703 // Recurse to children of the call.
2704 Inherited::VisitStmt(E);
2705 return;
2706 }
2707 }
2708
2709 NonTrivial = true;
2710 }
2711
2712 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2713 if (E->getConstructor()->isTrivial()) {
2714 // Recurse to children of the call.
2715 Inherited::VisitStmt(E);
2716 return;
2717 }
2718
2719 NonTrivial = true;
2720 }
2721
2722 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2723 if (E->getTemporary()->getDestructor()->isTrivial()) {
2724 Inherited::VisitStmt(E);
2725 return;
2726 }
2727
2728 NonTrivial = true;
2729 }
2730 };
2731}
2732
2733bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2734 NonTrivialCallFinder Finder(Ctx);
2735 Finder.Visit(this);
2736 return Finder.hasNonTrivialCall();
2737}
2738
Chandler Carruth82214a82011-02-18 23:54:50 +00002739/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2740/// pointer constant or not, as well as the specific kind of constant detected.
2741/// Null pointer constants can be integer constant expressions with the
2742/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2743/// (a GNU extension).
2744Expr::NullPointerConstantKind
2745Expr::isNullPointerConstant(ASTContext &Ctx,
2746 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002747 if (isValueDependent()) {
2748 switch (NPC) {
2749 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002750 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002751 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002752 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2753 return NPCK_ZeroInteger;
2754 else
2755 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002756
Douglas Gregorce940492009-09-25 04:25:58 +00002757 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002758 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002759 }
2760 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002761
Sebastian Redl07779722008-10-31 14:43:28 +00002762 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002763 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002764 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002765 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002766 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002767 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002768 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002769 Pointee->isVoidType() && // to void*
2770 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002771 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002772 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002773 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002774 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2775 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002776 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002777 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2778 // Accept ((void*)0) as a null pointer constant, as many other
2779 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002780 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002781 } else if (const GenericSelectionExpr *GE =
2782 dyn_cast<GenericSelectionExpr>(this)) {
2783 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002784 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002785 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002786 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002787 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002788 } else if (isa<GNUNullExpr>(this)) {
2789 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002790 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002791 } else if (const MaterializeTemporaryExpr *M
2792 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2793 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002794 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2795 if (const Expr *Source = OVE->getSourceExpr())
2796 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002797 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002798
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002799 // C++0x nullptr_t is always a null pointer constant.
2800 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002801 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002802
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002803 if (const RecordType *UT = getType()->getAsUnionType())
2804 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2805 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2806 const Expr *InitExpr = CLE->getInitializer();
2807 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2808 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2809 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002810 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002811 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002812 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002813 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002814
Reid Spencer5f016e22007-07-11 17:01:13 +00002815 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith70488e22012-02-14 21:38:30 +00002816 // test for the value 0. Don't use the C++11 constant expression semantics
2817 // for this, for now; once the dust settles on core issue 903, we might only
2818 // allow a literal 0 here in C++11 mode.
2819 if (Ctx.getLangOptions().CPlusPlus0x) {
2820 if (!isCXX98IntegralConstantExpr(Ctx))
2821 return NPCK_NotNull;
2822 } else {
2823 if (!isIntegerConstantExpr(Ctx))
2824 return NPCK_NotNull;
2825 }
Chandler Carruth82214a82011-02-18 23:54:50 +00002826
Richard Smith70488e22012-02-14 21:38:30 +00002827 return (EvaluateKnownConstInt(Ctx) == 0) ? NPCK_ZeroInteger : NPCK_NotNull;
Reid Spencer5f016e22007-07-11 17:01:13 +00002828}
Steve Naroff31a45842007-07-28 23:10:27 +00002829
John McCallf6a16482010-12-04 03:47:34 +00002830/// \brief If this expression is an l-value for an Objective C
2831/// property, find the underlying property reference expression.
2832const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2833 const Expr *E = this;
2834 while (true) {
2835 assert((E->getValueKind() == VK_LValue &&
2836 E->getObjectKind() == OK_ObjCProperty) &&
2837 "expression is not a property reference");
2838 E = E->IgnoreParenCasts();
2839 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2840 if (BO->getOpcode() == BO_Comma) {
2841 E = BO->getRHS();
2842 continue;
2843 }
2844 }
2845
2846 break;
2847 }
2848
2849 return cast<ObjCPropertyRefExpr>(E);
2850}
2851
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002852FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002853 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002854
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002855 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002856 if (ICE->getCastKind() == CK_LValueToRValue ||
2857 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002858 E = ICE->getSubExpr()->IgnoreParens();
2859 else
2860 break;
2861 }
2862
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002863 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002864 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002865 if (Field->isBitField())
2866 return Field;
2867
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002868 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2869 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2870 if (Field->isBitField())
2871 return Field;
2872
Eli Friedman42068e92011-07-13 02:05:57 +00002873 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002874 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2875 return BinOp->getLHS()->getBitField();
2876
Eli Friedman42068e92011-07-13 02:05:57 +00002877 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2878 return BinOp->getRHS()->getBitField();
2879 }
2880
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002881 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002882}
2883
Anders Carlsson09380262010-01-31 17:18:49 +00002884bool Expr::refersToVectorElement() const {
2885 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002886
Anders Carlsson09380262010-01-31 17:18:49 +00002887 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002888 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002889 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002890 E = ICE->getSubExpr()->IgnoreParens();
2891 else
2892 break;
2893 }
Sean Huntc3021132010-05-05 15:23:54 +00002894
Anders Carlsson09380262010-01-31 17:18:49 +00002895 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2896 return ASE->getBase()->getType()->isVectorType();
2897
2898 if (isa<ExtVectorElementExpr>(E))
2899 return true;
2900
2901 return false;
2902}
2903
Chris Lattner2140e902009-02-16 22:14:05 +00002904/// isArrow - Return true if the base expression is a pointer to vector,
2905/// return false if the base expression is a vector.
2906bool ExtVectorElementExpr::isArrow() const {
2907 return getBase()->getType()->isPointerType();
2908}
2909
Nate Begeman213541a2008-04-18 23:10:10 +00002910unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002911 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002912 return VT->getNumElements();
2913 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002914}
2915
Nate Begeman8a997642008-05-09 06:41:27 +00002916/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002917bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002918 // FIXME: Refactor this code to an accessor on the AST node which returns the
2919 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002920 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002921
2922 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002923 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002924 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002925
Nate Begeman190d6a22009-01-18 02:01:21 +00002926 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002927 if (Comp[0] == 's' || Comp[0] == 'S')
2928 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002929
Daniel Dunbar15027422009-10-17 23:53:04 +00002930 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002931 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002932 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002933
Steve Narofffec0b492007-07-30 03:29:09 +00002934 return false;
2935}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002936
Nate Begeman8a997642008-05-09 06:41:27 +00002937/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002938void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002939 SmallVectorImpl<unsigned> &Elts) const {
2940 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002941 if (Comp[0] == 's' || Comp[0] == 'S')
2942 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002943
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002944 bool isHi = Comp == "hi";
2945 bool isLo = Comp == "lo";
2946 bool isEven = Comp == "even";
2947 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002948
Nate Begeman8a997642008-05-09 06:41:27 +00002949 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2950 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002951
Nate Begeman8a997642008-05-09 06:41:27 +00002952 if (isHi)
2953 Index = e + i;
2954 else if (isLo)
2955 Index = i;
2956 else if (isEven)
2957 Index = 2 * i;
2958 else if (isOdd)
2959 Index = 2 * i + 1;
2960 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002961 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002962
Nate Begeman3b8d1162008-05-13 21:03:02 +00002963 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002964 }
Nate Begeman8a997642008-05-09 06:41:27 +00002965}
2966
Douglas Gregor04badcf2010-04-21 00:45:42 +00002967ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002968 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002969 SourceLocation LBracLoc,
2970 SourceLocation SuperLoc,
2971 bool IsInstanceSuper,
2972 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002973 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002974 ArrayRef<SourceLocation> SelLocs,
2975 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002976 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002977 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002978 SourceLocation RBracLoc,
2979 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002980 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002981 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002982 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002983 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002984 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2985 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002986 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002987 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
2988 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002989{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002990 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002991 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002992}
2993
Douglas Gregor04badcf2010-04-21 00:45:42 +00002994ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002995 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002996 SourceLocation LBracLoc,
2997 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002998 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002999 ArrayRef<SourceLocation> SelLocs,
3000 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003001 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003002 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003003 SourceLocation RBracLoc,
3004 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003005 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003006 T->isDependentType(), T->isInstantiationDependentType(),
3007 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003008 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3009 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003010 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003011 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003012 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003013{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003014 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003015 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003016}
3017
Douglas Gregor04badcf2010-04-21 00:45:42 +00003018ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003019 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003020 SourceLocation LBracLoc,
3021 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003022 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003023 ArrayRef<SourceLocation> SelLocs,
3024 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003025 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003026 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003027 SourceLocation RBracLoc,
3028 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003029 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003030 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003031 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003032 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003033 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3034 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003035 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003036 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003037 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003038{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003039 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003040 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003041}
3042
3043void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3044 ArrayRef<SourceLocation> SelLocs,
3045 SelectorLocationsKind SelLocsK) {
3046 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003047 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003048 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003049 if (Args[I]->isTypeDependent())
3050 ExprBits.TypeDependent = true;
3051 if (Args[I]->isValueDependent())
3052 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003053 if (Args[I]->isInstantiationDependent())
3054 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003055 if (Args[I]->containsUnexpandedParameterPack())
3056 ExprBits.ContainsUnexpandedParameterPack = true;
3057
3058 MyArgs[I] = Args[I];
3059 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003060
Benjamin Kramer19562c92012-02-20 00:20:48 +00003061 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003062 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003063 if (SelLocsK == SelLoc_NonStandard)
3064 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3065 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003066}
3067
Douglas Gregor04badcf2010-04-21 00:45:42 +00003068ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003069 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003070 SourceLocation LBracLoc,
3071 SourceLocation SuperLoc,
3072 bool IsInstanceSuper,
3073 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003074 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003075 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003076 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003077 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003078 SourceLocation RBracLoc,
3079 bool isImplicit) {
3080 assert((!SelLocs.empty() || isImplicit) &&
3081 "No selector locs for non-implicit message");
3082 ObjCMessageExpr *Mem;
3083 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3084 if (isImplicit)
3085 Mem = alloc(Context, Args.size(), 0);
3086 else
3087 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003088 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003089 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003090 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003091}
3092
3093ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003094 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003095 SourceLocation LBracLoc,
3096 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003097 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003098 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003099 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003100 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003101 SourceLocation RBracLoc,
3102 bool isImplicit) {
3103 assert((!SelLocs.empty() || isImplicit) &&
3104 "No selector locs for non-implicit message");
3105 ObjCMessageExpr *Mem;
3106 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3107 if (isImplicit)
3108 Mem = alloc(Context, Args.size(), 0);
3109 else
3110 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003111 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003112 SelLocs, SelLocsK, Method, Args, RBracLoc,
3113 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003114}
3115
3116ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003117 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003118 SourceLocation LBracLoc,
3119 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003120 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003121 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003122 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003123 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003124 SourceLocation RBracLoc,
3125 bool isImplicit) {
3126 assert((!SelLocs.empty() || isImplicit) &&
3127 "No selector locs for non-implicit message");
3128 ObjCMessageExpr *Mem;
3129 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3130 if (isImplicit)
3131 Mem = alloc(Context, Args.size(), 0);
3132 else
3133 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003134 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003135 SelLocs, SelLocsK, Method, Args, RBracLoc,
3136 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003137}
3138
Sean Huntc3021132010-05-05 15:23:54 +00003139ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003140 unsigned NumArgs,
3141 unsigned NumStoredSelLocs) {
3142 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003143 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3144}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003145
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003146ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3147 ArrayRef<Expr *> Args,
3148 SourceLocation RBraceLoc,
3149 ArrayRef<SourceLocation> SelLocs,
3150 Selector Sel,
3151 SelectorLocationsKind &SelLocsK) {
3152 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3153 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3154 : 0;
3155 return alloc(C, Args.size(), NumStoredSelLocs);
3156}
3157
3158ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3159 unsigned NumArgs,
3160 unsigned NumStoredSelLocs) {
3161 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3162 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3163 return (ObjCMessageExpr *)C.Allocate(Size,
3164 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3165}
3166
3167void ObjCMessageExpr::getSelectorLocs(
3168 SmallVectorImpl<SourceLocation> &SelLocs) const {
3169 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3170 SelLocs.push_back(getSelectorLoc(i));
3171}
3172
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003173SourceRange ObjCMessageExpr::getReceiverRange() const {
3174 switch (getReceiverKind()) {
3175 case Instance:
3176 return getInstanceReceiver()->getSourceRange();
3177
3178 case Class:
3179 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3180
3181 case SuperInstance:
3182 case SuperClass:
3183 return getSuperLoc();
3184 }
3185
David Blaikie30263482012-01-20 21:50:17 +00003186 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003187}
3188
Douglas Gregor04badcf2010-04-21 00:45:42 +00003189Selector ObjCMessageExpr::getSelector() const {
3190 if (HasMethod)
3191 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3192 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003193 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003194}
3195
3196ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3197 switch (getReceiverKind()) {
3198 case Instance:
3199 if (const ObjCObjectPointerType *Ptr
3200 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
3201 return Ptr->getInterfaceDecl();
3202 break;
3203
3204 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00003205 if (const ObjCObjectType *Ty
3206 = getClassReceiver()->getAs<ObjCObjectType>())
3207 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003208 break;
3209
3210 case SuperInstance:
3211 if (const ObjCObjectPointerType *Ptr
3212 = getSuperType()->getAs<ObjCObjectPointerType>())
3213 return Ptr->getInterfaceDecl();
3214 break;
3215
3216 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00003217 if (const ObjCObjectType *Iface
3218 = getSuperType()->getAs<ObjCObjectType>())
3219 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003220 break;
3221 }
3222
3223 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003224}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003225
Chris Lattner5f9e2722011-07-23 10:55:15 +00003226StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003227 switch (getBridgeKind()) {
3228 case OBC_Bridge:
3229 return "__bridge";
3230 case OBC_BridgeTransfer:
3231 return "__bridge_transfer";
3232 case OBC_BridgeRetained:
3233 return "__bridge_retained";
3234 }
David Blaikie30263482012-01-20 21:50:17 +00003235
3236 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003237}
3238
Jay Foad4ba2a172011-01-12 09:06:06 +00003239bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003240 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003241}
3242
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003243ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3244 QualType Type, SourceLocation BLoc,
3245 SourceLocation RP)
3246 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3247 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003248 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003249 Type->containsUnexpandedParameterPack()),
3250 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3251{
3252 SubExprs = new (C) Stmt*[nexpr];
3253 for (unsigned i = 0; i < nexpr; i++) {
3254 if (args[i]->isTypeDependent())
3255 ExprBits.TypeDependent = true;
3256 if (args[i]->isValueDependent())
3257 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003258 if (args[i]->isInstantiationDependent())
3259 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003260 if (args[i]->containsUnexpandedParameterPack())
3261 ExprBits.ContainsUnexpandedParameterPack = true;
3262
3263 SubExprs[i] = args[i];
3264 }
3265}
3266
Nate Begeman888376a2009-08-12 02:28:50 +00003267void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3268 unsigned NumExprs) {
3269 if (SubExprs) C.Deallocate(SubExprs);
3270
3271 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003272 this->NumExprs = NumExprs;
3273 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003274}
Nate Begeman888376a2009-08-12 02:28:50 +00003275
Peter Collingbournef111d932011-04-15 00:35:48 +00003276GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3277 SourceLocation GenericLoc, Expr *ControllingExpr,
3278 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3279 unsigned NumAssocs, SourceLocation DefaultLoc,
3280 SourceLocation RParenLoc,
3281 bool ContainsUnexpandedParameterPack,
3282 unsigned ResultIndex)
3283 : Expr(GenericSelectionExprClass,
3284 AssocExprs[ResultIndex]->getType(),
3285 AssocExprs[ResultIndex]->getValueKind(),
3286 AssocExprs[ResultIndex]->getObjectKind(),
3287 AssocExprs[ResultIndex]->isTypeDependent(),
3288 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003289 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003290 ContainsUnexpandedParameterPack),
3291 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3292 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3293 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3294 RParenLoc(RParenLoc) {
3295 SubExprs[CONTROLLING] = ControllingExpr;
3296 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3297 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3298}
3299
3300GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3301 SourceLocation GenericLoc, Expr *ControllingExpr,
3302 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3303 unsigned NumAssocs, SourceLocation DefaultLoc,
3304 SourceLocation RParenLoc,
3305 bool ContainsUnexpandedParameterPack)
3306 : Expr(GenericSelectionExprClass,
3307 Context.DependentTy,
3308 VK_RValue,
3309 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003310 /*isTypeDependent=*/true,
3311 /*isValueDependent=*/true,
3312 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003313 ContainsUnexpandedParameterPack),
3314 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3315 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3316 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3317 RParenLoc(RParenLoc) {
3318 SubExprs[CONTROLLING] = ControllingExpr;
3319 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3320 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3321}
3322
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003323//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003324// DesignatedInitExpr
3325//===----------------------------------------------------------------------===//
3326
Chandler Carruthb1138242011-06-16 06:47:06 +00003327IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003328 assert(Kind == FieldDesignator && "Only valid on a field designator");
3329 if (Field.NameOrField & 0x01)
3330 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3331 else
3332 return getField()->getIdentifier();
3333}
3334
Sean Huntc3021132010-05-05 15:23:54 +00003335DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003336 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003337 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003338 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003339 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003340 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003341 unsigned NumIndexExprs,
3342 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003343 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003344 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003345 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003346 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003347 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003348 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3349 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003350 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003351
3352 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003353 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003354 *Child++ = Init;
3355
3356 // Copy the designators and their subexpressions, computing
3357 // value-dependence along the way.
3358 unsigned IndexIdx = 0;
3359 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003360 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003361
3362 if (this->Designators[I].isArrayDesignator()) {
3363 // Compute type- and value-dependence.
3364 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003365 if (Index->isTypeDependent() || Index->isValueDependent())
3366 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003367 if (Index->isInstantiationDependent())
3368 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003369 // Propagate unexpanded parameter packs.
3370 if (Index->containsUnexpandedParameterPack())
3371 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003372
3373 // Copy the index expressions into permanent storage.
3374 *Child++ = IndexExprs[IndexIdx++];
3375 } else if (this->Designators[I].isArrayRangeDesignator()) {
3376 // Compute type- and value-dependence.
3377 Expr *Start = IndexExprs[IndexIdx];
3378 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003379 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003380 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003381 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003382 ExprBits.InstantiationDependent = true;
3383 } else if (Start->isInstantiationDependent() ||
3384 End->isInstantiationDependent()) {
3385 ExprBits.InstantiationDependent = true;
3386 }
3387
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003388 // Propagate unexpanded parameter packs.
3389 if (Start->containsUnexpandedParameterPack() ||
3390 End->containsUnexpandedParameterPack())
3391 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003392
3393 // Copy the start/end expressions into permanent storage.
3394 *Child++ = IndexExprs[IndexIdx++];
3395 *Child++ = IndexExprs[IndexIdx++];
3396 }
3397 }
3398
3399 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003400}
3401
Douglas Gregor05c13a32009-01-22 00:58:24 +00003402DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003403DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003404 unsigned NumDesignators,
3405 Expr **IndexExprs, unsigned NumIndexExprs,
3406 SourceLocation ColonOrEqualLoc,
3407 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003408 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003409 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003410 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003411 ColonOrEqualLoc, UsesColonSyntax,
3412 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003413}
3414
Mike Stump1eb44332009-09-09 15:08:12 +00003415DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003416 unsigned NumIndexExprs) {
3417 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3418 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3419 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3420}
3421
Douglas Gregor319d57f2010-01-06 23:17:19 +00003422void DesignatedInitExpr::setDesignators(ASTContext &C,
3423 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003424 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003425 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003426 NumDesignators = NumDesigs;
3427 for (unsigned I = 0; I != NumDesigs; ++I)
3428 Designators[I] = Desigs[I];
3429}
3430
Abramo Bagnara24f46742011-03-16 15:08:46 +00003431SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3432 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3433 if (size() == 1)
3434 return DIE->getDesignator(0)->getSourceRange();
3435 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3436 DIE->getDesignator(size()-1)->getEndLocation());
3437}
3438
Douglas Gregor05c13a32009-01-22 00:58:24 +00003439SourceRange DesignatedInitExpr::getSourceRange() const {
3440 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003441 Designator &First =
3442 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003443 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003444 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003445 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3446 else
3447 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3448 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003449 StartLoc =
3450 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003451 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3452}
3453
Douglas Gregor05c13a32009-01-22 00:58:24 +00003454Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3455 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3456 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3457 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003458 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3459 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3460}
3461
3462Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003463 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003464 "Requires array range designator");
3465 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3466 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003467 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3468 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3469}
3470
3471Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003472 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003473 "Requires array range designator");
3474 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3475 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003476 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3477 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3478}
3479
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003480/// \brief Replaces the designator at index @p Idx with the series
3481/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003482void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003483 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003484 const Designator *Last) {
3485 unsigned NumNewDesignators = Last - First;
3486 if (NumNewDesignators == 0) {
3487 std::copy_backward(Designators + Idx + 1,
3488 Designators + NumDesignators,
3489 Designators + Idx);
3490 --NumNewDesignators;
3491 return;
3492 } else if (NumNewDesignators == 1) {
3493 Designators[Idx] = *First;
3494 return;
3495 }
3496
Mike Stump1eb44332009-09-09 15:08:12 +00003497 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003498 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003499 std::copy(Designators, Designators + Idx, NewDesignators);
3500 std::copy(First, Last, NewDesignators + Idx);
3501 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3502 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003503 Designators = NewDesignators;
3504 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3505}
3506
Mike Stump1eb44332009-09-09 15:08:12 +00003507ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003508 Expr **exprs, unsigned nexprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003509 SourceLocation rparenloc)
3510 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003511 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003512 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00003513 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003514 for (unsigned i = 0; i != nexprs; ++i) {
3515 if (exprs[i]->isTypeDependent())
3516 ExprBits.TypeDependent = true;
3517 if (exprs[i]->isValueDependent())
3518 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003519 if (exprs[i]->isInstantiationDependent())
3520 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003521 if (exprs[i]->containsUnexpandedParameterPack())
3522 ExprBits.ContainsUnexpandedParameterPack = true;
3523
Nate Begeman2ef13e52009-08-10 23:49:36 +00003524 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003525 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003526}
3527
John McCalle996ffd2011-02-16 08:02:54 +00003528const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3529 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3530 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003531 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3532 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003533 e = cast<CXXConstructExpr>(e)->getArg(0);
3534 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3535 e = ice->getSubExpr();
3536 return cast<OpaqueValueExpr>(e);
3537}
3538
John McCall4b9c2d22011-11-06 09:01:30 +00003539PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3540 unsigned numSemanticExprs) {
3541 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3542 (1 + numSemanticExprs) * sizeof(Expr*),
3543 llvm::alignOf<PseudoObjectExpr>());
3544 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3545}
3546
3547PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3548 : Expr(PseudoObjectExprClass, shell) {
3549 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3550}
3551
3552PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3553 ArrayRef<Expr*> semantics,
3554 unsigned resultIndex) {
3555 assert(syntax && "no syntactic expression!");
3556 assert(semantics.size() && "no semantic expressions!");
3557
3558 QualType type;
3559 ExprValueKind VK;
3560 if (resultIndex == NoResult) {
3561 type = C.VoidTy;
3562 VK = VK_RValue;
3563 } else {
3564 assert(resultIndex < semantics.size());
3565 type = semantics[resultIndex]->getType();
3566 VK = semantics[resultIndex]->getValueKind();
3567 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3568 }
3569
3570 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3571 (1 + semantics.size()) * sizeof(Expr*),
3572 llvm::alignOf<PseudoObjectExpr>());
3573 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3574 resultIndex);
3575}
3576
3577PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3578 Expr *syntax, ArrayRef<Expr*> semantics,
3579 unsigned resultIndex)
3580 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3581 /*filled in at end of ctor*/ false, false, false, false) {
3582 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3583 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3584
3585 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3586 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3587 getSubExprsBuffer()[i] = E;
3588
3589 if (E->isTypeDependent())
3590 ExprBits.TypeDependent = true;
3591 if (E->isValueDependent())
3592 ExprBits.ValueDependent = true;
3593 if (E->isInstantiationDependent())
3594 ExprBits.InstantiationDependent = true;
3595 if (E->containsUnexpandedParameterPack())
3596 ExprBits.ContainsUnexpandedParameterPack = true;
3597
3598 if (isa<OpaqueValueExpr>(E))
3599 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3600 "opaque-value semantic expressions for pseudo-object "
3601 "operations must have sources");
3602 }
3603}
3604
Douglas Gregor05c13a32009-01-22 00:58:24 +00003605//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003606// ExprIterator.
3607//===----------------------------------------------------------------------===//
3608
3609Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3610Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3611Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3612const Expr* ConstExprIterator::operator[](size_t idx) const {
3613 return cast<Expr>(I[idx]);
3614}
3615const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3616const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3617
3618//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003619// Child Iterators for iterating over subexpressions/substatements
3620//===----------------------------------------------------------------------===//
3621
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003622// UnaryExprOrTypeTraitExpr
3623Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003624 // If this is of a type and the type is a VLA type (and not a typedef), the
3625 // size expression of the VLA needs to be treated as an executable expression.
3626 // Why isn't this weirdness documented better in StmtIterator?
3627 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003628 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003629 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003630 return child_range(child_iterator(T), child_iterator());
3631 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003632 }
John McCall63c00d72011-02-09 08:16:59 +00003633 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003634}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003635
Steve Naroff563477d2007-09-18 23:55:05 +00003636// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003637Stmt::child_range ObjCMessageExpr::children() {
3638 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003639 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003640 begin = reinterpret_cast<Stmt **>(this + 1);
3641 else
3642 begin = reinterpret_cast<Stmt **>(getArgs());
3643 return child_range(begin,
3644 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003645}
3646
Steve Naroff4eb206b2008-09-03 18:15:37 +00003647// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003648BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003649 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003650 bool constAdded)
Douglas Gregor561f8122011-07-01 01:22:09 +00003651 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003652 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003653 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003654{
Douglas Gregord967e312011-01-19 21:52:31 +00003655 bool TypeDependent = false;
3656 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +00003657 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00003658 computeDeclRefDependence(D->getASTContext(), D, getType(), TypeDependent,
3659 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +00003660 ExprBits.TypeDependent = TypeDependent;
3661 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +00003662 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003663}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003664
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003665ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3666 QualType T, ObjCMethodDecl *Method,
3667 SourceRange SR)
3668 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3669 false, false, false, false),
3670 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3671{
3672 Expr **SaveElements = getElements();
3673 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3674 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3675 ExprBits.ValueDependent = true;
3676 if (Elements[I]->isInstantiationDependent())
3677 ExprBits.InstantiationDependent = true;
3678 if (Elements[I]->containsUnexpandedParameterPack())
3679 ExprBits.ContainsUnexpandedParameterPack = true;
3680
3681 SaveElements[I] = Elements[I];
3682 }
3683}
3684
3685ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3686 llvm::ArrayRef<Expr *> Elements,
3687 QualType T, ObjCMethodDecl * Method,
3688 SourceRange SR) {
3689 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3690 + Elements.size() * sizeof(Expr *));
3691 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3692}
3693
3694ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3695 unsigned NumElements) {
3696
3697 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3698 + NumElements * sizeof(Expr *));
3699 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3700}
3701
3702ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3703 ArrayRef<ObjCDictionaryElement> VK,
3704 bool HasPackExpansions,
3705 QualType T, ObjCMethodDecl *method,
3706 SourceRange SR)
3707 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3708 false, false),
3709 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3710 DictWithObjectsMethod(method)
3711{
3712 KeyValuePair *KeyValues = getKeyValues();
3713 ExpansionData *Expansions = getExpansionData();
3714 for (unsigned I = 0; I < NumElements; I++) {
3715 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3716 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3717 ExprBits.ValueDependent = true;
3718 if (VK[I].Key->isInstantiationDependent() ||
3719 VK[I].Value->isInstantiationDependent())
3720 ExprBits.InstantiationDependent = true;
3721 if (VK[I].EllipsisLoc.isInvalid() &&
3722 (VK[I].Key->containsUnexpandedParameterPack() ||
3723 VK[I].Value->containsUnexpandedParameterPack()))
3724 ExprBits.ContainsUnexpandedParameterPack = true;
3725
3726 KeyValues[I].Key = VK[I].Key;
3727 KeyValues[I].Value = VK[I].Value;
3728 if (Expansions) {
3729 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3730 if (VK[I].NumExpansions)
3731 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3732 else
3733 Expansions[I].NumExpansionsPlusOne = 0;
3734 }
3735 }
3736}
3737
3738ObjCDictionaryLiteral *
3739ObjCDictionaryLiteral::Create(ASTContext &C,
3740 ArrayRef<ObjCDictionaryElement> VK,
3741 bool HasPackExpansions,
3742 QualType T, ObjCMethodDecl *method,
3743 SourceRange SR) {
3744 unsigned ExpansionsSize = 0;
3745 if (HasPackExpansions)
3746 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3747
3748 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3749 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3750 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3751}
3752
3753ObjCDictionaryLiteral *
3754ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3755 bool HasPackExpansions) {
3756 unsigned ExpansionsSize = 0;
3757 if (HasPackExpansions)
3758 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3759 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3760 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3761 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3762 HasPackExpansions);
3763}
3764
3765ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
3766 Expr *base,
3767 Expr *key, QualType T,
3768 ObjCMethodDecl *getMethod,
3769 ObjCMethodDecl *setMethod,
3770 SourceLocation RB) {
3771 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
3772 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
3773 OK_ObjCSubscript,
3774 getMethod, setMethod, RB);
3775}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003776
3777AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3778 QualType t, AtomicOp op, SourceLocation RP)
3779 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3780 false, false, false, false),
3781 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3782{
3783 for (unsigned i = 0; i < nexpr; i++) {
3784 if (args[i]->isTypeDependent())
3785 ExprBits.TypeDependent = true;
3786 if (args[i]->isValueDependent())
3787 ExprBits.ValueDependent = true;
3788 if (args[i]->isInstantiationDependent())
3789 ExprBits.InstantiationDependent = true;
3790 if (args[i]->containsUnexpandedParameterPack())
3791 ExprBits.ContainsUnexpandedParameterPack = true;
3792
3793 SubExprs[i] = args[i];
3794 }
3795}