blob: 18cc5b37bf7badfdf9d8a4330fd394a468027487 [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}
363
Anders Carlsson3a082d82009-09-08 18:24:21 +0000364// FIXME: Maybe this should use DeclPrinter with a special "print predefined
365// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000366std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
367 ASTContext &Context = CurrentDecl->getASTContext();
368
Anders Carlsson3a082d82009-09-08 18:24:21 +0000369 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000370 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000371 return FD->getNameAsString();
372
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000373 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000374 llvm::raw_svector_ostream Out(Name);
375
376 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000377 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000378 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000379 if (MD->isStatic())
380 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000381 }
382
383 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000384
385 std::string Proto = FD->getQualifiedNameAsString(Policy);
386
John McCall183700f2009-09-21 23:43:11 +0000387 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000388 const FunctionProtoType *FT = 0;
389 if (FD->hasWrittenPrototype())
390 FT = dyn_cast<FunctionProtoType>(AFT);
391
392 Proto += "(";
393 if (FT) {
394 llvm::raw_string_ostream POut(Proto);
395 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
396 if (i) POut << ", ";
397 std::string Param;
398 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
399 POut << Param;
400 }
401
402 if (FT->isVariadic()) {
403 if (FD->getNumParams()) POut << ", ";
404 POut << "...";
405 }
406 }
407 Proto += ")";
408
Sam Weinig4eadcc52009-12-27 01:38:20 +0000409 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
410 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
411 if (ThisQuals.hasConst())
412 Proto += " const";
413 if (ThisQuals.hasVolatile())
414 Proto += " volatile";
415 }
416
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000417 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
418 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000419
420 Out << Proto;
421
422 Out.flush();
423 return Name.str().str();
424 }
425 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000426 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000427 llvm::raw_svector_ostream Out(Name);
428 Out << (MD->isInstanceMethod() ? '-' : '+');
429 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000430
431 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
432 // a null check to avoid a crash.
433 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000434 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000435
Anders Carlsson3a082d82009-09-08 18:24:21 +0000436 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000437 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000438 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000439
Anders Carlsson3a082d82009-09-08 18:24:21 +0000440 Out << ' ';
441 Out << MD->getSelector().getAsString();
442 Out << ']';
443
444 Out.flush();
445 return Name.str().str();
446 }
447 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
448 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
449 return "top level";
450 }
451 return "";
452}
453
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000454void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
455 if (hasAllocation())
456 C.Deallocate(pVal);
457
458 BitWidth = Val.getBitWidth();
459 unsigned NumWords = Val.getNumWords();
460 const uint64_t* Words = Val.getRawData();
461 if (NumWords > 1) {
462 pVal = new (C) uint64_t[NumWords];
463 std::copy(Words, Words + NumWords, pVal);
464 } else if (NumWords == 1)
465 VAL = Words[0];
466 else
467 VAL = 0;
468}
469
470IntegerLiteral *
471IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
472 QualType type, SourceLocation l) {
473 return new (C) IntegerLiteral(C, V, type, l);
474}
475
476IntegerLiteral *
477IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
478 return new (C) IntegerLiteral(Empty);
479}
480
481FloatingLiteral *
482FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
483 bool isexact, QualType Type, SourceLocation L) {
484 return new (C) FloatingLiteral(C, V, isexact, Type, L);
485}
486
487FloatingLiteral *
488FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000489 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000490}
491
Chris Lattnerda8249e2008-06-07 22:13:43 +0000492/// getValueAsApproximateDouble - This returns the value as an inaccurate
493/// double. Note that this may cause loss of precision, but is useful for
494/// debugging dumps, etc.
495double FloatingLiteral::getValueAsApproximateDouble() const {
496 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000497 bool ignored;
498 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
499 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000500 return V.convertToDouble();
501}
502
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000503int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000504 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000505 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000506 case Ascii:
507 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000508 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000509 break;
510 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000511 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000512 break;
513 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000514 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000515 break;
516 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000517 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000518 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000519 }
520 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
521 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000522 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000523 && "character byte widths supported are 1, 2, and 4 only");
524 return CharByteWidth;
525}
526
Chris Lattner5f9e2722011-07-23 10:55:15 +0000527StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000528 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000529 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000530 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000531 // Allocate enough space for the StringLiteral plus an array of locations for
532 // any concatenated string tokens.
533 void *Mem = C.Allocate(sizeof(StringLiteral)+
534 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000535 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000536 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000537
Reid Spencer5f016e22007-07-11 17:01:13 +0000538 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000539 SL->setString(C,Str,Kind,Pascal);
540
Chris Lattner2085fd62009-02-18 06:40:38 +0000541 SL->TokLocs[0] = Loc[0];
542 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000543
Chris Lattner726e1682009-02-18 05:49:11 +0000544 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000545 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
546 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000547}
548
Douglas Gregor673ecd62009-04-15 16:35:07 +0000549StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
550 void *Mem = C.Allocate(sizeof(StringLiteral)+
551 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000552 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000553 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000554 SL->CharByteWidth = 0;
555 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000556 SL->NumConcatenated = NumStrs;
557 return SL;
558}
559
Eli Friedman64f45a22011-11-01 02:23:42 +0000560void StringLiteral::setString(ASTContext &C, StringRef Str,
561 StringKind Kind, bool IsPascal) {
562 //FIXME: we assume that the string data comes from a target that uses the same
563 // code unit size and endianess for the type of string.
564 this->Kind = Kind;
565 this->IsPascal = IsPascal;
566
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000567 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000568 assert((Str.size()%CharByteWidth == 0)
569 && "size of data must be multiple of CharByteWidth");
570 Length = Str.size()/CharByteWidth;
571
572 switch(CharByteWidth) {
573 case 1: {
574 char *AStrData = new (C) char[Length];
575 std::memcpy(AStrData,Str.data(),Str.size());
576 StrData.asChar = AStrData;
577 break;
578 }
579 case 2: {
580 uint16_t *AStrData = new (C) uint16_t[Length];
581 std::memcpy(AStrData,Str.data(),Str.size());
582 StrData.asUInt16 = AStrData;
583 break;
584 }
585 case 4: {
586 uint32_t *AStrData = new (C) uint32_t[Length];
587 std::memcpy(AStrData,Str.data(),Str.size());
588 StrData.asUInt32 = AStrData;
589 break;
590 }
591 default:
592 assert(false && "unsupported CharByteWidth");
593 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000594}
595
Chris Lattner08f92e32010-11-17 07:37:15 +0000596/// getLocationOfByte - Return a source location that points to the specified
597/// byte of this string literal.
598///
599/// Strings are amazingly complex. They can be formed from multiple tokens and
600/// can have escape sequences in them in addition to the usual trigraph and
601/// escaped newline business. This routine handles this complexity.
602///
603SourceLocation StringLiteral::
604getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
605 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000606 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
607
Chris Lattner08f92e32010-11-17 07:37:15 +0000608 // Loop over all of the tokens in this string until we find the one that
609 // contains the byte we're looking for.
610 unsigned TokNo = 0;
611 while (1) {
612 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
613 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
614
615 // Get the spelling of the string so that we can get the data that makes up
616 // the string literal, not the identifier for the macro it is potentially
617 // expanded through.
618 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
619
620 // Re-lex the token to get its length and original spelling.
621 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
622 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000623 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000624 if (Invalid)
625 return StrTokSpellingLoc;
626
627 const char *StrData = Buffer.data()+LocInfo.second;
628
629 // Create a langops struct and enable trigraphs. This is sufficient for
630 // relexing tokens.
631 LangOptions LangOpts;
632 LangOpts.Trigraphs = true;
633
634 // Create a lexer starting at the beginning of this token.
635 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
636 Buffer.end());
637 Token TheTok;
638 TheLexer.LexFromRawLexer(TheTok);
639
640 // Use the StringLiteralParser to compute the length of the string in bytes.
641 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
642 unsigned TokNumBytes = SLP.GetStringLength();
643
644 // If the byte is in this token, return the location of the byte.
645 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000646 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000647 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
648
649 // Now that we know the offset of the token in the spelling, use the
650 // preprocessor to get the offset in the original source.
651 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
652 }
653
654 // Move to the next string token.
655 ++TokNo;
656 ByteNo -= TokNumBytes;
657 }
658}
659
660
661
Reid Spencer5f016e22007-07-11 17:01:13 +0000662/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
663/// corresponds to, e.g. "sizeof" or "[pre]++".
664const char *UnaryOperator::getOpcodeStr(Opcode Op) {
665 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000666 case UO_PostInc: return "++";
667 case UO_PostDec: return "--";
668 case UO_PreInc: return "++";
669 case UO_PreDec: return "--";
670 case UO_AddrOf: return "&";
671 case UO_Deref: return "*";
672 case UO_Plus: return "+";
673 case UO_Minus: return "-";
674 case UO_Not: return "~";
675 case UO_LNot: return "!";
676 case UO_Real: return "__real";
677 case UO_Imag: return "__imag";
678 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000680 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000681}
682
John McCall2de56d12010-08-25 11:45:40 +0000683UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000684UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
685 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000686 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000687 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
688 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
689 case OO_Amp: return UO_AddrOf;
690 case OO_Star: return UO_Deref;
691 case OO_Plus: return UO_Plus;
692 case OO_Minus: return UO_Minus;
693 case OO_Tilde: return UO_Not;
694 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000695 }
696}
697
698OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
699 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000700 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
701 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
702 case UO_AddrOf: return OO_Amp;
703 case UO_Deref: return OO_Star;
704 case UO_Plus: return OO_Plus;
705 case UO_Minus: return OO_Minus;
706 case UO_Not: return OO_Tilde;
707 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000708 default: return OO_None;
709 }
710}
711
712
Reid Spencer5f016e22007-07-11 17:01:13 +0000713//===----------------------------------------------------------------------===//
714// Postfix Operators.
715//===----------------------------------------------------------------------===//
716
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000717CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
718 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000719 SourceLocation rparenloc)
720 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000721 fn->isTypeDependent(),
722 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000723 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000724 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000725 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000727 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000728 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000729 for (unsigned i = 0; i != numargs; ++i) {
730 if (args[i]->isTypeDependent())
731 ExprBits.TypeDependent = true;
732 if (args[i]->isValueDependent())
733 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000734 if (args[i]->isInstantiationDependent())
735 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000736 if (args[i]->containsUnexpandedParameterPack())
737 ExprBits.ContainsUnexpandedParameterPack = true;
738
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000739 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000740 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000741
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000742 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000743 RParenLoc = rparenloc;
744}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000745
Ted Kremenek668bf912009-02-09 20:51:47 +0000746CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000747 QualType t, ExprValueKind VK, SourceLocation rparenloc)
748 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000749 fn->isTypeDependent(),
750 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000751 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000752 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000753 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000754
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000755 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000756 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000757 for (unsigned i = 0; i != numargs; ++i) {
758 if (args[i]->isTypeDependent())
759 ExprBits.TypeDependent = true;
760 if (args[i]->isValueDependent())
761 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000762 if (args[i]->isInstantiationDependent())
763 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000764 if (args[i]->containsUnexpandedParameterPack())
765 ExprBits.ContainsUnexpandedParameterPack = true;
766
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000767 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000768 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000769
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000770 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 RParenLoc = rparenloc;
772}
773
Mike Stump1eb44332009-09-09 15:08:12 +0000774CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
775 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000776 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000777 SubExprs = new (C) Stmt*[PREARGS_START];
778 CallExprBits.NumPreArgs = 0;
779}
780
781CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
782 EmptyShell Empty)
783 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
784 // FIXME: Why do we allocate this?
785 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
786 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000787}
788
Nuno Lopesd20254f2009-12-20 23:11:08 +0000789Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000790 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000791
792 while (SubstNonTypeTemplateParmExpr *NTTP
793 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
794 CEE = NTTP->getReplacement()->IgnoreParenCasts();
795 }
796
Sebastian Redl20012152010-09-10 20:55:30 +0000797 // If we're calling a dereference, look at the pointer instead.
798 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
799 if (BO->isPtrMemOp())
800 CEE = BO->getRHS()->IgnoreParenCasts();
801 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
802 if (UO->getOpcode() == UO_Deref)
803 CEE = UO->getSubExpr()->IgnoreParenCasts();
804 }
Chris Lattner6346f962009-07-17 15:46:27 +0000805 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000806 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000807 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
808 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000809
810 return 0;
811}
812
Nuno Lopesd20254f2009-12-20 23:11:08 +0000813FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000814 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000815}
816
Chris Lattnerd18b3292007-12-28 05:25:02 +0000817/// setNumArgs - This changes the number of arguments present in this call.
818/// Any orphaned expressions are deleted by this, and any new operands are set
819/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000820void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000821 // No change, just return.
822 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Chris Lattnerd18b3292007-12-28 05:25:02 +0000824 // If shrinking # arguments, just delete the extras and forgot them.
825 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000826 this->NumArgs = NumArgs;
827 return;
828 }
829
830 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000831 unsigned NumPreArgs = getNumPreArgs();
832 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000833 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000834 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000835 NewSubExprs[i] = SubExprs[i];
836 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000837 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
838 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000839 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000840
Douglas Gregor88c9a462009-04-17 21:46:47 +0000841 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000842 SubExprs = NewSubExprs;
843 this->NumArgs = NumArgs;
844}
845
Chris Lattnercb888962008-10-06 05:00:53 +0000846/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
847/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +0000848unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000849 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000850 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000851 // ImplicitCastExpr.
852 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
853 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000854 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000855
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000856 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
857 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000858 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Anders Carlssonbcba2012008-01-31 02:13:57 +0000860 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
861 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000862 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000863
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000864 if (!FDecl->getIdentifier())
865 return 0;
866
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000867 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000868}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000869
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000870QualType CallExpr::getCallReturnType() const {
871 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000872 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000873 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000874 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000875 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000876 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
877 // This should never be overloaded and so should never return null.
878 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000879
John McCall864c0412011-04-26 20:42:42 +0000880 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000881 return FnType->getResultType();
882}
Chris Lattnercb888962008-10-06 05:00:53 +0000883
John McCall2882eca2011-02-21 06:23:05 +0000884SourceRange CallExpr::getSourceRange() const {
885 if (isa<CXXOperatorCallExpr>(this))
886 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
887
888 SourceLocation begin = getCallee()->getLocStart();
889 if (begin.isInvalid() && getNumArgs() > 0)
890 begin = getArg(0)->getLocStart();
891 SourceLocation end = getRParenLoc();
892 if (end.isInvalid() && getNumArgs() > 0)
893 end = getArg(getNumArgs() - 1)->getLocEnd();
894 return SourceRange(begin, end);
895}
896
Sean Huntc3021132010-05-05 15:23:54 +0000897OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000898 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000899 TypeSourceInfo *tsi,
900 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000901 Expr** exprsPtr, unsigned numExprs,
902 SourceLocation RParenLoc) {
903 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000904 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000905 sizeof(Expr*) * numExprs);
906
907 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
908 exprsPtr, numExprs, RParenLoc);
909}
910
911OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
912 unsigned numComps, unsigned numExprs) {
913 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
914 sizeof(OffsetOfNode) * numComps +
915 sizeof(Expr*) * numExprs);
916 return new (Mem) OffsetOfExpr(numComps, numExprs);
917}
918
Sean Huntc3021132010-05-05 15:23:54 +0000919OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000920 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000921 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000922 Expr** exprsPtr, unsigned numExprs,
923 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000924 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
925 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000926 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000927 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000928 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000929 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
930 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000931{
932 for(unsigned i = 0; i < numComps; ++i) {
933 setComponent(i, compsPtr[i]);
934 }
Sean Huntc3021132010-05-05 15:23:54 +0000935
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000936 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000937 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
938 ExprBits.ValueDependent = true;
939 if (exprsPtr[i]->containsUnexpandedParameterPack())
940 ExprBits.ContainsUnexpandedParameterPack = true;
941
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000942 setIndexExpr(i, exprsPtr[i]);
943 }
944}
945
946IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
947 assert(getKind() == Field || getKind() == Identifier);
948 if (getKind() == Field)
949 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000950
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000951 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
952}
953
Mike Stump1eb44332009-09-09 15:08:12 +0000954MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000955 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000956 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000957 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000958 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000959 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000960 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000961 QualType ty,
962 ExprValueKind vk,
963 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000964 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000965
Douglas Gregor40d96a62011-02-28 21:54:11 +0000966 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000967 founddecl.getDecl() != memberdecl ||
968 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000969 if (hasQualOrFound)
970 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000971
John McCalld5532b62009-11-23 01:53:49 +0000972 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000973 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
974 else if (TemplateKWLoc.isValid())
975 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000976
Chris Lattner32488542010-10-30 05:14:06 +0000977 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000978 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
979 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000980
981 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000982 // FIXME: Wrong. We should be looking at the member declaration we found.
983 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000984 E->setValueDependent(true);
985 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +0000986 E->setInstantiationDependent(true);
987 }
988 else if (QualifierLoc &&
989 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
990 E->setInstantiationDependent(true);
991
John McCall6bb80172010-03-30 21:47:33 +0000992 E->HasQualifierOrFoundDecl = true;
993
994 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000995 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000996 NQ->FoundDecl = founddecl;
997 }
998
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000999 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1000
John McCall6bb80172010-03-30 21:47:33 +00001001 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001002 bool Dependent = false;
1003 bool InstantiationDependent = false;
1004 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001005 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1006 Dependent,
1007 InstantiationDependent,
1008 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001009 if (InstantiationDependent)
1010 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001011 } else if (TemplateKWLoc.isValid()) {
1012 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001013 }
1014
1015 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001016}
1017
Douglas Gregor75e85042011-03-02 21:06:53 +00001018SourceRange MemberExpr::getSourceRange() const {
1019 SourceLocation StartLoc;
1020 if (isImplicitAccess()) {
1021 if (hasQualifier())
1022 StartLoc = getQualifierLoc().getBeginLoc();
1023 else
1024 StartLoc = MemberLoc;
1025 } else {
1026 // FIXME: We don't want this to happen. Rather, we should be able to
1027 // detect all kinds of implicit accesses more cleanly.
1028 StartLoc = getBase()->getLocStart();
1029 if (StartLoc.isInvalid())
1030 StartLoc = MemberLoc;
1031 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001032
1033 SourceLocation EndLoc = hasExplicitTemplateArgs()
1034 ? getRAngleLoc() : getMemberNameInfo().getEndLoc();
1035
Douglas Gregor75e85042011-03-02 21:06:53 +00001036 return SourceRange(StartLoc, EndLoc);
1037}
1038
John McCall1d9b3b22011-09-09 05:25:32 +00001039void CastExpr::CheckCastConsistency() const {
1040 switch (getCastKind()) {
1041 case CK_DerivedToBase:
1042 case CK_UncheckedDerivedToBase:
1043 case CK_DerivedToBaseMemberPointer:
1044 case CK_BaseToDerived:
1045 case CK_BaseToDerivedMemberPointer:
1046 assert(!path_empty() && "Cast kind should have a base path!");
1047 break;
1048
1049 case CK_CPointerToObjCPointerCast:
1050 assert(getType()->isObjCObjectPointerType());
1051 assert(getSubExpr()->getType()->isPointerType());
1052 goto CheckNoBasePath;
1053
1054 case CK_BlockPointerToObjCPointerCast:
1055 assert(getType()->isObjCObjectPointerType());
1056 assert(getSubExpr()->getType()->isBlockPointerType());
1057 goto CheckNoBasePath;
1058
John McCall4d4e5c12012-02-15 01:22:51 +00001059 case CK_ReinterpretMemberPointer:
1060 assert(getType()->isMemberPointerType());
1061 assert(getSubExpr()->getType()->isMemberPointerType());
1062 goto CheckNoBasePath;
1063
John McCall1d9b3b22011-09-09 05:25:32 +00001064 case CK_BitCast:
1065 // Arbitrary casts to C pointer types count as bitcasts.
1066 // Otherwise, we should only have block and ObjC pointer casts
1067 // here if they stay within the type kind.
1068 if (!getType()->isPointerType()) {
1069 assert(getType()->isObjCObjectPointerType() ==
1070 getSubExpr()->getType()->isObjCObjectPointerType());
1071 assert(getType()->isBlockPointerType() ==
1072 getSubExpr()->getType()->isBlockPointerType());
1073 }
1074 goto CheckNoBasePath;
1075
1076 case CK_AnyPointerToBlockPointerCast:
1077 assert(getType()->isBlockPointerType());
1078 assert(getSubExpr()->getType()->isAnyPointerType() &&
1079 !getSubExpr()->getType()->isBlockPointerType());
1080 goto CheckNoBasePath;
1081
Douglas Gregorac1303e2012-02-22 05:02:47 +00001082 case CK_CopyAndAutoreleaseBlockObject:
1083 assert(getType()->isBlockPointerType());
1084 assert(getSubExpr()->getType()->isBlockPointerType());
1085 goto CheckNoBasePath;
1086
John McCall1d9b3b22011-09-09 05:25:32 +00001087 // These should not have an inheritance path.
1088 case CK_Dynamic:
1089 case CK_ToUnion:
1090 case CK_ArrayToPointerDecay:
1091 case CK_FunctionToPointerDecay:
1092 case CK_NullToMemberPointer:
1093 case CK_NullToPointer:
1094 case CK_ConstructorConversion:
1095 case CK_IntegralToPointer:
1096 case CK_PointerToIntegral:
1097 case CK_ToVoid:
1098 case CK_VectorSplat:
1099 case CK_IntegralCast:
1100 case CK_IntegralToFloating:
1101 case CK_FloatingToIntegral:
1102 case CK_FloatingCast:
1103 case CK_ObjCObjectLValueCast:
1104 case CK_FloatingRealToComplex:
1105 case CK_FloatingComplexToReal:
1106 case CK_FloatingComplexCast:
1107 case CK_FloatingComplexToIntegralComplex:
1108 case CK_IntegralRealToComplex:
1109 case CK_IntegralComplexToReal:
1110 case CK_IntegralComplexCast:
1111 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001112 case CK_ARCProduceObject:
1113 case CK_ARCConsumeObject:
1114 case CK_ARCReclaimReturnedObject:
1115 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001116 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1117 goto CheckNoBasePath;
1118
1119 case CK_Dependent:
1120 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001121 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001122 case CK_AtomicToNonAtomic:
1123 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001124 case CK_PointerToBoolean:
1125 case CK_IntegralToBoolean:
1126 case CK_FloatingToBoolean:
1127 case CK_MemberPointerToBoolean:
1128 case CK_FloatingComplexToBoolean:
1129 case CK_IntegralComplexToBoolean:
1130 case CK_LValueBitCast: // -> bool&
1131 case CK_UserDefinedConversion: // operator bool()
1132 CheckNoBasePath:
1133 assert(path_empty() && "Cast kind should not have a base path!");
1134 break;
1135 }
1136}
1137
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001138const char *CastExpr::getCastKindName() const {
1139 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001140 case CK_Dependent:
1141 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001142 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001143 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001144 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001145 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001146 case CK_LValueToRValue:
1147 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001148 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001149 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001150 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001151 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001152 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001153 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001154 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001155 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001156 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001157 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001158 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001159 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001160 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001161 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001162 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001163 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001164 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001165 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001166 case CK_NullToPointer:
1167 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001168 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001169 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001170 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001171 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001172 case CK_ReinterpretMemberPointer:
1173 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001174 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001175 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001176 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001177 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001178 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001179 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001180 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001181 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001182 case CK_PointerToBoolean:
1183 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001184 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001185 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001186 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001187 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001188 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001189 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001190 case CK_IntegralToBoolean:
1191 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001192 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001193 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001194 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001195 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001196 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001197 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001198 case CK_FloatingToBoolean:
1199 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001200 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001201 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001202 case CK_CPointerToObjCPointerCast:
1203 return "CPointerToObjCPointerCast";
1204 case CK_BlockPointerToObjCPointerCast:
1205 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001206 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001207 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001208 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001209 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001210 case CK_FloatingRealToComplex:
1211 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001212 case CK_FloatingComplexToReal:
1213 return "FloatingComplexToReal";
1214 case CK_FloatingComplexToBoolean:
1215 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001216 case CK_FloatingComplexCast:
1217 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001218 case CK_FloatingComplexToIntegralComplex:
1219 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001220 case CK_IntegralRealToComplex:
1221 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001222 case CK_IntegralComplexToReal:
1223 return "IntegralComplexToReal";
1224 case CK_IntegralComplexToBoolean:
1225 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001226 case CK_IntegralComplexCast:
1227 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001228 case CK_IntegralComplexToFloatingComplex:
1229 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001230 case CK_ARCConsumeObject:
1231 return "ARCConsumeObject";
1232 case CK_ARCProduceObject:
1233 return "ARCProduceObject";
1234 case CK_ARCReclaimReturnedObject:
1235 return "ARCReclaimReturnedObject";
1236 case CK_ARCExtendBlockObject:
1237 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001238 case CK_AtomicToNonAtomic:
1239 return "AtomicToNonAtomic";
1240 case CK_NonAtomicToAtomic:
1241 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001242 case CK_CopyAndAutoreleaseBlockObject:
1243 return "CopyAndAutoreleaseBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001244 }
Mike Stump1eb44332009-09-09 15:08:12 +00001245
John McCall2bb5d002010-11-13 09:02:35 +00001246 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001247}
1248
Douglas Gregor6eef5192009-12-14 19:27:10 +00001249Expr *CastExpr::getSubExprAsWritten() {
1250 Expr *SubExpr = 0;
1251 CastExpr *E = this;
1252 do {
1253 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001254
1255 // Skip through reference binding to temporary.
1256 if (MaterializeTemporaryExpr *Materialize
1257 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1258 SubExpr = Materialize->GetTemporaryExpr();
1259
Douglas Gregor6eef5192009-12-14 19:27:10 +00001260 // Skip any temporary bindings; they're implicit.
1261 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1262 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001263
Douglas Gregor6eef5192009-12-14 19:27:10 +00001264 // Conversions by constructor and conversion functions have a
1265 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001266 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001267 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001268 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001269 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001270
Douglas Gregor6eef5192009-12-14 19:27:10 +00001271 // If the subexpression we're left with is an implicit cast, look
1272 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001273 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1274
Douglas Gregor6eef5192009-12-14 19:27:10 +00001275 return SubExpr;
1276}
1277
John McCallf871d0c2010-08-07 06:22:56 +00001278CXXBaseSpecifier **CastExpr::path_buffer() {
1279 switch (getStmtClass()) {
1280#define ABSTRACT_STMT(x)
1281#define CASTEXPR(Type, Base) \
1282 case Stmt::Type##Class: \
1283 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1284#define STMT(Type, Base)
1285#include "clang/AST/StmtNodes.inc"
1286 default:
1287 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001288 }
1289}
1290
1291void CastExpr::setCastPath(const CXXCastPath &Path) {
1292 assert(Path.size() == path_size());
1293 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1294}
1295
1296ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1297 CastKind Kind, Expr *Operand,
1298 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001299 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001300 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1301 void *Buffer =
1302 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1303 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001304 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001305 if (PathSize) E->setCastPath(*BasePath);
1306 return E;
1307}
1308
1309ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1310 unsigned PathSize) {
1311 void *Buffer =
1312 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1313 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1314}
1315
1316
1317CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001318 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001319 const CXXCastPath *BasePath,
1320 TypeSourceInfo *WrittenTy,
1321 SourceLocation L, SourceLocation R) {
1322 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1323 void *Buffer =
1324 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1325 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001326 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001327 if (PathSize) E->setCastPath(*BasePath);
1328 return E;
1329}
1330
1331CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1332 void *Buffer =
1333 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1334 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1335}
1336
Reid Spencer5f016e22007-07-11 17:01:13 +00001337/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1338/// corresponds to, e.g. "<<=".
1339const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1340 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001341 case BO_PtrMemD: return ".*";
1342 case BO_PtrMemI: return "->*";
1343 case BO_Mul: return "*";
1344 case BO_Div: return "/";
1345 case BO_Rem: return "%";
1346 case BO_Add: return "+";
1347 case BO_Sub: return "-";
1348 case BO_Shl: return "<<";
1349 case BO_Shr: return ">>";
1350 case BO_LT: return "<";
1351 case BO_GT: return ">";
1352 case BO_LE: return "<=";
1353 case BO_GE: return ">=";
1354 case BO_EQ: return "==";
1355 case BO_NE: return "!=";
1356 case BO_And: return "&";
1357 case BO_Xor: return "^";
1358 case BO_Or: return "|";
1359 case BO_LAnd: return "&&";
1360 case BO_LOr: return "||";
1361 case BO_Assign: return "=";
1362 case BO_MulAssign: return "*=";
1363 case BO_DivAssign: return "/=";
1364 case BO_RemAssign: return "%=";
1365 case BO_AddAssign: return "+=";
1366 case BO_SubAssign: return "-=";
1367 case BO_ShlAssign: return "<<=";
1368 case BO_ShrAssign: return ">>=";
1369 case BO_AndAssign: return "&=";
1370 case BO_XorAssign: return "^=";
1371 case BO_OrAssign: return "|=";
1372 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001373 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001374
David Blaikie30263482012-01-20 21:50:17 +00001375 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001376}
1377
John McCall2de56d12010-08-25 11:45:40 +00001378BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001379BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1380 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001381 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001382 case OO_Plus: return BO_Add;
1383 case OO_Minus: return BO_Sub;
1384 case OO_Star: return BO_Mul;
1385 case OO_Slash: return BO_Div;
1386 case OO_Percent: return BO_Rem;
1387 case OO_Caret: return BO_Xor;
1388 case OO_Amp: return BO_And;
1389 case OO_Pipe: return BO_Or;
1390 case OO_Equal: return BO_Assign;
1391 case OO_Less: return BO_LT;
1392 case OO_Greater: return BO_GT;
1393 case OO_PlusEqual: return BO_AddAssign;
1394 case OO_MinusEqual: return BO_SubAssign;
1395 case OO_StarEqual: return BO_MulAssign;
1396 case OO_SlashEqual: return BO_DivAssign;
1397 case OO_PercentEqual: return BO_RemAssign;
1398 case OO_CaretEqual: return BO_XorAssign;
1399 case OO_AmpEqual: return BO_AndAssign;
1400 case OO_PipeEqual: return BO_OrAssign;
1401 case OO_LessLess: return BO_Shl;
1402 case OO_GreaterGreater: return BO_Shr;
1403 case OO_LessLessEqual: return BO_ShlAssign;
1404 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1405 case OO_EqualEqual: return BO_EQ;
1406 case OO_ExclaimEqual: return BO_NE;
1407 case OO_LessEqual: return BO_LE;
1408 case OO_GreaterEqual: return BO_GE;
1409 case OO_AmpAmp: return BO_LAnd;
1410 case OO_PipePipe: return BO_LOr;
1411 case OO_Comma: return BO_Comma;
1412 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001413 }
1414}
1415
1416OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1417 static const OverloadedOperatorKind OverOps[] = {
1418 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1419 OO_Star, OO_Slash, OO_Percent,
1420 OO_Plus, OO_Minus,
1421 OO_LessLess, OO_GreaterGreater,
1422 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1423 OO_EqualEqual, OO_ExclaimEqual,
1424 OO_Amp,
1425 OO_Caret,
1426 OO_Pipe,
1427 OO_AmpAmp,
1428 OO_PipePipe,
1429 OO_Equal, OO_StarEqual,
1430 OO_SlashEqual, OO_PercentEqual,
1431 OO_PlusEqual, OO_MinusEqual,
1432 OO_LessLessEqual, OO_GreaterGreaterEqual,
1433 OO_AmpEqual, OO_CaretEqual,
1434 OO_PipeEqual,
1435 OO_Comma
1436 };
1437 return OverOps[Opc];
1438}
1439
Ted Kremenek709210f2010-04-13 23:39:13 +00001440InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001441 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001442 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001443 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001444 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001445 InitExprs(C, numInits),
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001446 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0)
1447{
1448 sawArrayRangeDesignator(false);
1449 setInitializesStdInitializerList(false);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001450 for (unsigned I = 0; I != numInits; ++I) {
1451 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001452 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001453 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001454 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001455 if (initExprs[I]->isInstantiationDependent())
1456 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001457 if (initExprs[I]->containsUnexpandedParameterPack())
1458 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001459 }
Sean Huntc3021132010-05-05 15:23:54 +00001460
Ted Kremenek709210f2010-04-13 23:39:13 +00001461 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001462}
Reid Spencer5f016e22007-07-11 17:01:13 +00001463
Ted Kremenek709210f2010-04-13 23:39:13 +00001464void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001465 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001466 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001467}
1468
Ted Kremenek709210f2010-04-13 23:39:13 +00001469void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001470 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001471}
1472
Ted Kremenek709210f2010-04-13 23:39:13 +00001473Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001474 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001475 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001476 InitExprs.back() = expr;
1477 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001478 }
Mike Stump1eb44332009-09-09 15:08:12 +00001479
Douglas Gregor4c678342009-01-28 21:54:33 +00001480 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1481 InitExprs[Init] = expr;
1482 return Result;
1483}
1484
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001485void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001486 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001487 ArrayFillerOrUnionFieldInit = filler;
1488 // Fill out any "holes" in the array due to designated initializers.
1489 Expr **inits = getInits();
1490 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1491 if (inits[i] == 0)
1492 inits[i] = filler;
1493}
1494
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001495SourceRange InitListExpr::getSourceRange() const {
1496 if (SyntacticForm)
1497 return SyntacticForm->getSourceRange();
1498 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1499 if (Beg.isInvalid()) {
1500 // Find the first non-null initializer.
1501 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1502 E = InitExprs.end();
1503 I != E; ++I) {
1504 if (Stmt *S = *I) {
1505 Beg = S->getLocStart();
1506 break;
1507 }
1508 }
1509 }
1510 if (End.isInvalid()) {
1511 // Find the first non-null initializer from the end.
1512 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1513 E = InitExprs.rend();
1514 I != E; ++I) {
1515 if (Stmt *S = *I) {
1516 End = S->getSourceRange().getEnd();
1517 break;
1518 }
1519 }
1520 }
1521 return SourceRange(Beg, End);
1522}
1523
Steve Naroffbfdcae62008-09-04 15:31:07 +00001524/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001525///
John McCalla345edb2012-02-17 03:32:35 +00001526const FunctionProtoType *BlockExpr::getFunctionType() const {
1527 // The block pointer is never sugared, but the function type might be.
1528 return cast<BlockPointerType>(getType())
1529 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001530}
1531
Mike Stump1eb44332009-09-09 15:08:12 +00001532SourceLocation BlockExpr::getCaretLocation() const {
1533 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001534}
Mike Stump1eb44332009-09-09 15:08:12 +00001535const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001536 return TheBlock->getBody();
1537}
Mike Stump1eb44332009-09-09 15:08:12 +00001538Stmt *BlockExpr::getBody() {
1539 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001540}
Steve Naroff56ee6892008-10-08 17:01:13 +00001541
1542
Reid Spencer5f016e22007-07-11 17:01:13 +00001543//===----------------------------------------------------------------------===//
1544// Generic Expression Routines
1545//===----------------------------------------------------------------------===//
1546
Chris Lattner026dc962009-02-14 07:37:35 +00001547/// isUnusedResultAWarning - Return true if this immediate expression should
1548/// be warned about if the result is unused. If so, fill in Loc and Ranges
1549/// with location to warn on and the source range[s] to report with the
1550/// warning.
1551bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001552 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001553 // Don't warn if the expr is type dependent. The type could end up
1554 // instantiating to void.
1555 if (isTypeDependent())
1556 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 switch (getStmtClass()) {
1559 default:
John McCall0faede62010-03-12 07:11:26 +00001560 if (getType()->isVoidType())
1561 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001562 Loc = getExprLoc();
1563 R1 = getSourceRange();
1564 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001566 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001567 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001568 case GenericSelectionExprClass:
1569 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1570 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001571 case UnaryOperatorClass: {
1572 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001573
Reid Spencer5f016e22007-07-11 17:01:13 +00001574 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001575 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001576 case UO_PostInc:
1577 case UO_PostDec:
1578 case UO_PreInc:
1579 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001580 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001581 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001582 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001583 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001584 return false;
1585 break;
John McCall2de56d12010-08-25 11:45:40 +00001586 case UO_Real:
1587 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001588 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001589 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1590 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001591 return false;
1592 break;
John McCall2de56d12010-08-25 11:45:40 +00001593 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001594 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001595 }
Chris Lattner026dc962009-02-14 07:37:35 +00001596 Loc = UO->getOperatorLoc();
1597 R1 = UO->getSubExpr()->getSourceRange();
1598 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001600 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001601 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001602 switch (BO->getOpcode()) {
1603 default:
1604 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001605 // Consider the RHS of comma for side effects. LHS was checked by
1606 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001607 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001608 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1609 // lvalue-ness) of an assignment written in a macro.
1610 if (IntegerLiteral *IE =
1611 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1612 if (IE->getValue() == 0)
1613 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001614 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1615 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001616 case BO_LAnd:
1617 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001618 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1619 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1620 return false;
1621 break;
John McCallbf0ee352010-02-16 04:10:53 +00001622 }
Chris Lattner026dc962009-02-14 07:37:35 +00001623 if (BO->isAssignmentOp())
1624 return false;
1625 Loc = BO->getOperatorLoc();
1626 R1 = BO->getLHS()->getSourceRange();
1627 R2 = BO->getRHS()->getSourceRange();
1628 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001629 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001630 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001631 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001632 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001633 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001634
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001635 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001636 // If only one of the LHS or RHS is a warning, the operator might
1637 // be being used for control flow. Only warn if both the LHS and
1638 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001639 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001640 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1641 return false;
1642 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001643 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001644 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001645 }
1646
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001648 // If the base pointer or element is to a volatile pointer/field, accessing
1649 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001650 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001651 return false;
1652 Loc = cast<MemberExpr>(this)->getMemberLoc();
1653 R1 = SourceRange(Loc, Loc);
1654 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1655 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 case ArraySubscriptExprClass:
1658 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001659 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001660 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001661 return false;
1662 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1663 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1664 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1665 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001666
Chandler Carruth9b106832011-08-17 09:49:44 +00001667 case CXXOperatorCallExprClass: {
1668 // We warn about operator== and operator!= even when user-defined operator
1669 // overloads as there is no reasonable way to define these such that they
1670 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1671 // warning: these operators are commonly typo'ed, and so warning on them
1672 // provides additional value as well. If this list is updated,
1673 // DiagnoseUnusedComparison should be as well.
1674 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1675 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001676 Op->getOperator() == OO_ExclaimEqual) {
1677 Loc = Op->getOperatorLoc();
1678 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001679 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001680 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001681
1682 // Fallthrough for generic call handling.
1683 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001684 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00001685 case CXXMemberCallExprClass:
1686 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001687 // If this is a direct call, get the callee.
1688 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001689 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001690 // If the callee has attribute pure, const, or warn_unused_result, warn
1691 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001692 //
1693 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1694 // updated to match for QoI.
1695 if (FD->getAttr<WarnUnusedResultAttr>() ||
1696 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1697 Loc = CE->getCallee()->getLocStart();
1698 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001700 if (unsigned NumArgs = CE->getNumArgs())
1701 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1702 CE->getArg(NumArgs-1)->getLocEnd());
1703 return true;
1704 }
Chris Lattner026dc962009-02-14 07:37:35 +00001705 }
1706 return false;
1707 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001708
1709 case CXXTemporaryObjectExprClass:
1710 case CXXConstructExprClass:
1711 return false;
1712
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001713 case ObjCMessageExprClass: {
1714 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001715 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1716 ME->isInstanceMessage() &&
1717 !ME->getType()->isVoidType() &&
1718 ME->getSelector().getIdentifierInfoForSlot(0) &&
1719 ME->getSelector().getIdentifierInfoForSlot(0)
1720 ->getName().startswith("init")) {
1721 Loc = getExprLoc();
1722 R1 = ME->getSourceRange();
1723 return true;
1724 }
1725
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001726 const ObjCMethodDecl *MD = ME->getMethodDecl();
1727 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1728 Loc = getExprLoc();
1729 return true;
1730 }
Chris Lattner026dc962009-02-14 07:37:35 +00001731 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001732 }
Mike Stump1eb44332009-09-09 15:08:12 +00001733
John McCall12f78a62010-12-02 01:19:52 +00001734 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001735 Loc = getExprLoc();
1736 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001737 return true;
John McCall12f78a62010-12-02 01:19:52 +00001738
John McCall4b9c2d22011-11-06 09:01:30 +00001739 case PseudoObjectExprClass: {
1740 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1741
1742 // Only complain about things that have the form of a getter.
1743 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1744 isa<BinaryOperator>(PO->getSyntacticForm()))
1745 return false;
1746
1747 Loc = getExprLoc();
1748 R1 = getSourceRange();
1749 return true;
1750 }
1751
Chris Lattner611b2ec2008-07-26 19:51:01 +00001752 case StmtExprClass: {
1753 // Statement exprs don't logically have side effects themselves, but are
1754 // sometimes used in macros in ways that give them a type that is unused.
1755 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1756 // however, if the result of the stmt expr is dead, we don't want to emit a
1757 // warning.
1758 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001759 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001760 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001761 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001762 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1763 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1764 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1765 }
Mike Stump1eb44332009-09-09 15:08:12 +00001766
John McCall0faede62010-03-12 07:11:26 +00001767 if (getType()->isVoidType())
1768 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001769 Loc = cast<StmtExpr>(this)->getLParenLoc();
1770 R1 = getSourceRange();
1771 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001772 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001773 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001774 // If this is an explicit cast to void, allow it. People do this when they
1775 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001776 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001777 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001778 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1779 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1780 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001781 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001782 if (getType()->isVoidType())
1783 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001784 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001785
Anders Carlsson58beed92009-11-17 17:11:23 +00001786 // If this is a cast to void or a constructor conversion, check the operand.
1787 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001788 if (CE->getCastKind() == CK_ToVoid ||
1789 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001790 return (cast<CastExpr>(this)->getSubExpr()
1791 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001792 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1793 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1794 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001795 }
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Eli Friedman4be1f472008-05-19 21:24:43 +00001797 case ImplicitCastExprClass:
1798 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001799 return (cast<ImplicitCastExpr>(this)
1800 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001801
Chris Lattner04421082008-04-08 04:40:51 +00001802 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001803 return (cast<CXXDefaultArgExpr>(this)
1804 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001805
1806 case CXXNewExprClass:
1807 // FIXME: In theory, there might be new expressions that don't have side
1808 // effects (e.g. a placement new with an uninitialized POD).
1809 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001810 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001811 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001812 return (cast<CXXBindTemporaryExpr>(this)
1813 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001814 case ExprWithCleanupsClass:
1815 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001816 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001817 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001818}
1819
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001820/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001821/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001822bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001823 const Expr *E = IgnoreParens();
1824 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001825 default:
1826 return false;
1827 case ObjCIvarRefExprClass:
1828 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001829 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001830 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001831 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001832 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001833 case MaterializeTemporaryExprClass:
1834 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1835 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001836 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001837 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001838 case BlockDeclRefExprClass:
Douglas Gregora2813ce2009-10-23 18:54:35 +00001839 case DeclRefExprClass: {
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001840
1841 const Decl *D;
1842 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1843 D = BDRE->getDecl();
1844 else
1845 D = cast<DeclRefExpr>(E)->getDecl();
1846
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001847 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1848 if (VD->hasGlobalStorage())
1849 return true;
1850 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001851 // dereferencing to a pointer is always a gc'able candidate,
1852 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001853 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001854 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001855 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001856 return false;
1857 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001858 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001859 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001860 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001861 }
1862 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001863 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001864 }
1865}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001866
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001867bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1868 if (isTypeDependent())
1869 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001870 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001871}
1872
John McCall864c0412011-04-26 20:42:42 +00001873QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00001874 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00001875
1876 // Bound member expressions are always one of these possibilities:
1877 // x->m x.m x->*y x.*y
1878 // (possibly parenthesized)
1879
1880 expr = expr->IgnoreParens();
1881 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1882 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1883 return mem->getMemberDecl()->getType();
1884 }
1885
1886 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1887 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1888 ->getPointeeType();
1889 assert(type->isFunctionType());
1890 return type;
1891 }
1892
1893 assert(isa<UnresolvedMemberExpr>(expr));
1894 return QualType();
1895}
1896
Sebastian Redl369e51f2010-09-10 20:55:33 +00001897static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1898 Expr::CanThrowResult CT2) {
1899 // CanThrowResult constants are ordered so that the maximum is the correct
1900 // merge result.
1901 return CT1 > CT2 ? CT1 : CT2;
1902}
1903
1904static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1905 Expr *E = const_cast<Expr*>(CE);
1906 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001907 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001908 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1909 }
1910 return R;
1911}
1912
Richard Smith7a614d82011-06-11 17:19:42 +00001913static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1914 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001915 bool NullThrows = true) {
1916 if (!D)
1917 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1918
1919 // See if we can get a function type from the decl somehow.
1920 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1921 if (!VD) // If we have no clue what we're calling, assume the worst.
1922 return Expr::CT_Can;
1923
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001924 // As an extension, we assume that __attribute__((nothrow)) functions don't
1925 // throw.
1926 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1927 return Expr::CT_Cannot;
1928
Sebastian Redl369e51f2010-09-10 20:55:33 +00001929 QualType T = VD->getType();
1930 const FunctionProtoType *FT;
1931 if ((FT = T->getAs<FunctionProtoType>())) {
1932 } else if (const PointerType *PT = T->getAs<PointerType>())
1933 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1934 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1935 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1936 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1937 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1938 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1939 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1940
1941 if (!FT)
1942 return Expr::CT_Can;
1943
Richard Smith7a614d82011-06-11 17:19:42 +00001944 if (FT->getExceptionSpecType() == EST_Delayed) {
1945 assert(isa<CXXConstructorDecl>(D) &&
1946 "only constructor exception specs can be unknown");
1947 Ctx.getDiagnostics().Report(E->getLocStart(),
1948 diag::err_exception_spec_unknown)
1949 << E->getSourceRange();
1950 return Expr::CT_Can;
1951 }
1952
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001953 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001954}
1955
1956static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1957 if (DC->isTypeDependent())
1958 return Expr::CT_Dependent;
1959
Sebastian Redl295995c2010-09-10 20:55:47 +00001960 if (!DC->getTypeAsWritten()->isReferenceType())
1961 return Expr::CT_Cannot;
1962
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001963 if (DC->getSubExpr()->isTypeDependent())
1964 return Expr::CT_Dependent;
1965
Sebastian Redl369e51f2010-09-10 20:55:33 +00001966 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1967}
1968
1969static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1970 const CXXTypeidExpr *DC) {
1971 if (DC->isTypeOperand())
1972 return Expr::CT_Cannot;
1973
1974 Expr *Op = DC->getExprOperand();
1975 if (Op->isTypeDependent())
1976 return Expr::CT_Dependent;
1977
1978 const RecordType *RT = Op->getType()->getAs<RecordType>();
1979 if (!RT)
1980 return Expr::CT_Cannot;
1981
1982 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1983 return Expr::CT_Cannot;
1984
1985 if (Op->Classify(C).isPRValue())
1986 return Expr::CT_Cannot;
1987
1988 return Expr::CT_Can;
1989}
1990
1991Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1992 // C++ [expr.unary.noexcept]p3:
1993 // [Can throw] if in a potentially-evaluated context the expression would
1994 // contain:
1995 switch (getStmtClass()) {
1996 case CXXThrowExprClass:
1997 // - a potentially evaluated throw-expression
1998 return CT_Can;
1999
2000 case CXXDynamicCastExprClass: {
2001 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
2002 // where T is a reference type, that requires a run-time check
2003 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
2004 if (CT == CT_Can)
2005 return CT;
2006 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2007 }
2008
2009 case CXXTypeidExprClass:
2010 // - a potentially evaluated typeid expression applied to a glvalue
2011 // expression whose type is a polymorphic class type
2012 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
2013
2014 // - a potentially evaluated call to a function, member function, function
2015 // pointer, or member function pointer that does not have a non-throwing
2016 // exception-specification
2017 case CallExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002018 case CXXMemberCallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00002019 case CXXOperatorCallExprClass:
2020 case UserDefinedLiteralClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00002021 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002022 CanThrowResult CT;
2023 if (isTypeDependent())
2024 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00002025 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
2026 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002027 else
Richard Smith7a614d82011-06-11 17:19:42 +00002028 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00002029 if (CT == CT_Can)
2030 return CT;
2031 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2032 }
2033
Sebastian Redl295995c2010-09-10 20:55:47 +00002034 case CXXConstructExprClass:
2035 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00002036 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00002037 cast<CXXConstructExpr>(this)->getConstructor());
2038 if (CT == CT_Can)
2039 return CT;
2040 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2041 }
2042
Douglas Gregor01d08012012-02-07 10:09:13 +00002043 case LambdaExprClass: {
2044 const LambdaExpr *Lambda = cast<LambdaExpr>(this);
2045 CanThrowResult CT = Expr::CT_Cannot;
2046 for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(),
2047 CapEnd = Lambda->capture_init_end();
2048 Cap != CapEnd; ++Cap)
2049 CT = MergeCanThrow(CT, (*Cap)->CanThrow(C));
2050 return CT;
2051 }
2052
Sebastian Redl369e51f2010-09-10 20:55:33 +00002053 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002054 CanThrowResult CT;
2055 if (isTypeDependent())
2056 CT = CT_Dependent;
2057 else
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002058 CT = CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew());
Sebastian Redl369e51f2010-09-10 20:55:33 +00002059 if (CT == CT_Can)
2060 return CT;
2061 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2062 }
2063
2064 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002065 CanThrowResult CT;
2066 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
2067 if (DTy.isNull() || DTy->isDependentType()) {
2068 CT = CT_Dependent;
2069 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00002070 CT = CanCalleeThrow(C, this,
2071 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002072 if (const RecordType *RT = DTy->getAs<RecordType>()) {
2073 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00002074 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002075 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002076 if (CT == CT_Can)
2077 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002078 }
2079 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2080 }
2081
2082 case CXXBindTemporaryExprClass: {
2083 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00002084 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002085 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
2086 if (CT == CT_Can)
2087 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00002088 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2089 }
2090
2091 // ObjC message sends are like function calls, but never have exception
2092 // specs.
2093 case ObjCMessageExprClass:
2094 case ObjCPropertyRefExprClass:
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002095 case ObjCSubscriptRefExprClass:
2096 return CT_Can;
2097
2098 // All the ObjC literals that are implemented as calls are
2099 // potentially throwing unless we decide to close off that
2100 // possibility.
2101 case ObjCArrayLiteralClass:
2102 case ObjCBoolLiteralExprClass:
2103 case ObjCDictionaryLiteralClass:
2104 case ObjCNumericLiteralClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002105 return CT_Can;
2106
2107 // Many other things have subexpressions, so we have to test those.
2108 // Some are simple:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002109 case ConditionalOperatorClass:
2110 case CompoundLiteralExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002111 case CXXConstCastExprClass:
2112 case CXXDefaultArgExprClass:
2113 case CXXReinterpretCastExprClass:
2114 case DesignatedInitExprClass:
2115 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002116 case ExtVectorElementExprClass:
2117 case InitListExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002118 case MemberExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002119 case ObjCIsaExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002120 case ObjCIvarRefExprClass:
2121 case ParenExprClass:
2122 case ParenListExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002123 case ShuffleVectorExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002124 case VAArgExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002125 return CanSubExprsThrow(C, this);
2126
2127 // Some might be dependent for other reasons.
Sebastian Redl369e51f2010-09-10 20:55:33 +00002128 case ArraySubscriptExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002129 case BinaryOperatorClass:
2130 case CompoundAssignOperatorClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002131 case CStyleCastExprClass:
2132 case CXXStaticCastExprClass:
2133 case CXXFunctionalCastExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002134 case ImplicitCastExprClass:
2135 case MaterializeTemporaryExprClass:
2136 case UnaryOperatorClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002137 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2138 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2139 }
2140
2141 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2142 case StmtExprClass:
2143 return CT_Can;
2144
2145 case ChooseExprClass:
2146 if (isTypeDependent() || isValueDependent())
2147 return CT_Dependent;
2148 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2149
Peter Collingbournef111d932011-04-15 00:35:48 +00002150 case GenericSelectionExprClass:
2151 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2152 return CT_Dependent;
2153 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2154
Sebastian Redl369e51f2010-09-10 20:55:33 +00002155 // Some expressions are always dependent.
Sebastian Redl369e51f2010-09-10 20:55:33 +00002156 case CXXDependentScopeMemberExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002157 case CXXUnresolvedConstructExprClass:
2158 case DependentScopeDeclRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002159 return CT_Dependent;
2160
Eli Friedmanc9674be2012-01-31 01:21:45 +00002161 case AtomicExprClass:
2162 case AsTypeExprClass:
2163 case BinaryConditionalOperatorClass:
2164 case BlockExprClass:
2165 case BlockDeclRefExprClass:
2166 case CUDAKernelCallExprClass:
2167 case DeclRefExprClass:
2168 case ObjCBridgedCastExprClass:
2169 case ObjCIndirectCopyRestoreExprClass:
2170 case ObjCProtocolExprClass:
2171 case ObjCSelectorExprClass:
2172 case OffsetOfExprClass:
2173 case PackExpansionExprClass:
2174 case PseudoObjectExprClass:
2175 case SubstNonTypeTemplateParmExprClass:
2176 case SubstNonTypeTemplateParmPackExprClass:
2177 case UnaryExprOrTypeTraitExprClass:
2178 case UnresolvedLookupExprClass:
2179 case UnresolvedMemberExprClass:
2180 // FIXME: Can any of the above throw? If so, when?
Sebastian Redl369e51f2010-09-10 20:55:33 +00002181 return CT_Cannot;
Eli Friedmanc9674be2012-01-31 01:21:45 +00002182
2183 case AddrLabelExprClass:
2184 case ArrayTypeTraitExprClass:
2185 case BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002186 case TypeTraitExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002187 case CXXBoolLiteralExprClass:
2188 case CXXNoexceptExprClass:
2189 case CXXNullPtrLiteralExprClass:
2190 case CXXPseudoDestructorExprClass:
2191 case CXXScalarValueInitExprClass:
2192 case CXXThisExprClass:
2193 case CXXUuidofExprClass:
2194 case CharacterLiteralClass:
2195 case ExpressionTraitExprClass:
2196 case FloatingLiteralClass:
2197 case GNUNullExprClass:
2198 case ImaginaryLiteralClass:
2199 case ImplicitValueInitExprClass:
2200 case IntegerLiteralClass:
2201 case ObjCEncodeExprClass:
2202 case ObjCStringLiteralClass:
2203 case OpaqueValueExprClass:
2204 case PredefinedExprClass:
2205 case SizeOfPackExprClass:
2206 case StringLiteralClass:
2207 case UnaryTypeTraitExprClass:
2208 // These expressions can never throw.
2209 return CT_Cannot;
2210
2211#define STMT(CLASS, PARENT) case CLASS##Class:
2212#define STMT_RANGE(Base, First, Last)
2213#define LAST_STMT_RANGE(BASE, FIRST, LAST)
2214#define EXPR(CLASS, PARENT)
2215#define ABSTRACT_STMT(STMT)
2216#include "clang/AST/StmtNodes.inc"
2217 case NoStmtClass:
2218 llvm_unreachable("Invalid class for expression");
Sebastian Redl369e51f2010-09-10 20:55:33 +00002219 }
Matt Beaumont-Gay56e68b72012-01-31 18:59:25 +00002220 llvm_unreachable("Bogus StmtClass");
Sebastian Redl369e51f2010-09-10 20:55:33 +00002221}
2222
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002223Expr* Expr::IgnoreParens() {
2224 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002225 while (true) {
2226 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2227 E = P->getSubExpr();
2228 continue;
2229 }
2230 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2231 if (P->getOpcode() == UO_Extension) {
2232 E = P->getSubExpr();
2233 continue;
2234 }
2235 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002236 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2237 if (!P->isResultDependent()) {
2238 E = P->getResultExpr();
2239 continue;
2240 }
2241 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002242 return E;
2243 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002244}
2245
Chris Lattner56f34942008-02-13 01:02:39 +00002246/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2247/// or CastExprs or ImplicitCastExprs, returning their operand.
2248Expr *Expr::IgnoreParenCasts() {
2249 Expr *E = this;
2250 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002251 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002252 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002253 continue;
2254 }
2255 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002256 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002257 continue;
2258 }
2259 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2260 if (P->getOpcode() == UO_Extension) {
2261 E = P->getSubExpr();
2262 continue;
2263 }
2264 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002265 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2266 if (!P->isResultDependent()) {
2267 E = P->getResultExpr();
2268 continue;
2269 }
2270 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002271 if (MaterializeTemporaryExpr *Materialize
2272 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2273 E = Materialize->GetTemporaryExpr();
2274 continue;
2275 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002276 if (SubstNonTypeTemplateParmExpr *NTTP
2277 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2278 E = NTTP->getReplacement();
2279 continue;
2280 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002281 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002282 }
2283}
2284
John McCall9c5d70c2010-12-04 08:24:19 +00002285/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2286/// casts. This is intended purely as a temporary workaround for code
2287/// that hasn't yet been rewritten to do the right thing about those
2288/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002289Expr *Expr::IgnoreParenLValueCasts() {
2290 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002291 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002292 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2293 E = P->getSubExpr();
2294 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002295 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002296 if (P->getCastKind() == CK_LValueToRValue) {
2297 E = P->getSubExpr();
2298 continue;
2299 }
John McCall9c5d70c2010-12-04 08:24:19 +00002300 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2301 if (P->getOpcode() == UO_Extension) {
2302 E = P->getSubExpr();
2303 continue;
2304 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002305 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2306 if (!P->isResultDependent()) {
2307 E = P->getResultExpr();
2308 continue;
2309 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002310 } else if (MaterializeTemporaryExpr *Materialize
2311 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2312 E = Materialize->GetTemporaryExpr();
2313 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002314 } else if (SubstNonTypeTemplateParmExpr *NTTP
2315 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2316 E = NTTP->getReplacement();
2317 continue;
John McCallf6a16482010-12-04 03:47:34 +00002318 }
2319 break;
2320 }
2321 return E;
2322}
2323
John McCall2fc46bf2010-05-05 22:59:52 +00002324Expr *Expr::IgnoreParenImpCasts() {
2325 Expr *E = this;
2326 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002327 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002328 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002329 continue;
2330 }
2331 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002332 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002333 continue;
2334 }
2335 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2336 if (P->getOpcode() == UO_Extension) {
2337 E = P->getSubExpr();
2338 continue;
2339 }
2340 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002341 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2342 if (!P->isResultDependent()) {
2343 E = P->getResultExpr();
2344 continue;
2345 }
2346 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002347 if (MaterializeTemporaryExpr *Materialize
2348 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2349 E = Materialize->GetTemporaryExpr();
2350 continue;
2351 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002352 if (SubstNonTypeTemplateParmExpr *NTTP
2353 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2354 E = NTTP->getReplacement();
2355 continue;
2356 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002357 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002358 }
2359}
2360
Hans Wennborg2f072b42011-06-09 17:06:51 +00002361Expr *Expr::IgnoreConversionOperator() {
2362 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002363 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002364 return MCE->getImplicitObjectArgument();
2365 }
2366 return this;
2367}
2368
Chris Lattnerecdd8412009-03-13 17:28:01 +00002369/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2370/// value (including ptr->int casts of the same size). Strip off any
2371/// ParenExpr or CastExprs, returning their operand.
2372Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2373 Expr *E = this;
2374 while (true) {
2375 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2376 E = P->getSubExpr();
2377 continue;
2378 }
Mike Stump1eb44332009-09-09 15:08:12 +00002379
Chris Lattnerecdd8412009-03-13 17:28:01 +00002380 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2381 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002382 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002383 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002384
Chris Lattnerecdd8412009-03-13 17:28:01 +00002385 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2386 E = SE;
2387 continue;
2388 }
Mike Stump1eb44332009-09-09 15:08:12 +00002389
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002390 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002391 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002392 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002393 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002394 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2395 E = SE;
2396 continue;
2397 }
2398 }
Mike Stump1eb44332009-09-09 15:08:12 +00002399
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002400 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2401 if (P->getOpcode() == UO_Extension) {
2402 E = P->getSubExpr();
2403 continue;
2404 }
2405 }
2406
Peter Collingbournef111d932011-04-15 00:35:48 +00002407 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2408 if (!P->isResultDependent()) {
2409 E = P->getResultExpr();
2410 continue;
2411 }
2412 }
2413
Douglas Gregorc0244c52011-09-08 17:56:33 +00002414 if (SubstNonTypeTemplateParmExpr *NTTP
2415 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2416 E = NTTP->getReplacement();
2417 continue;
2418 }
2419
Chris Lattnerecdd8412009-03-13 17:28:01 +00002420 return E;
2421 }
2422}
2423
Douglas Gregor6eef5192009-12-14 19:27:10 +00002424bool Expr::isDefaultArgument() const {
2425 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002426 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2427 E = M->GetTemporaryExpr();
2428
Douglas Gregor6eef5192009-12-14 19:27:10 +00002429 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2430 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002431
Douglas Gregor6eef5192009-12-14 19:27:10 +00002432 return isa<CXXDefaultArgExpr>(E);
2433}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002434
Douglas Gregor2f599792010-04-02 18:24:57 +00002435/// \brief Skip over any no-op casts and any temporary-binding
2436/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002437static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002438 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2439 E = M->GetTemporaryExpr();
2440
Douglas Gregor2f599792010-04-02 18:24:57 +00002441 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002442 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002443 E = ICE->getSubExpr();
2444 else
2445 break;
2446 }
2447
2448 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2449 E = BE->getSubExpr();
2450
2451 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002452 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002453 E = ICE->getSubExpr();
2454 else
2455 break;
2456 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002457
2458 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002459}
2460
John McCall558d2ab2010-09-15 10:14:12 +00002461/// isTemporaryObject - Determines if this expression produces a
2462/// temporary of the given class type.
2463bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2464 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2465 return false;
2466
Anders Carlssonf8b30152010-11-28 16:40:49 +00002467 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002468
John McCall58277b52010-09-15 20:59:13 +00002469 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002470 if (!E->Classify(C).isPRValue()) {
2471 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002472 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002473 return false;
2474 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002475
John McCall19e60ad2010-09-16 06:57:56 +00002476 // Black-list a few cases which yield pr-values of class type that don't
2477 // refer to temporaries of that type:
2478
2479 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002480 if (isa<ImplicitCastExpr>(E)) {
2481 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2482 case CK_DerivedToBase:
2483 case CK_UncheckedDerivedToBase:
2484 return false;
2485 default:
2486 break;
2487 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002488 }
2489
John McCall19e60ad2010-09-16 06:57:56 +00002490 // - member expressions (all)
2491 if (isa<MemberExpr>(E))
2492 return false;
2493
John McCall56ca35d2011-02-17 10:25:35 +00002494 // - opaque values (all)
2495 if (isa<OpaqueValueExpr>(E))
2496 return false;
2497
John McCall558d2ab2010-09-15 10:14:12 +00002498 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002499}
2500
Douglas Gregor75e85042011-03-02 21:06:53 +00002501bool Expr::isImplicitCXXThis() const {
2502 const Expr *E = this;
2503
2504 // Strip away parentheses and casts we don't care about.
2505 while (true) {
2506 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2507 E = Paren->getSubExpr();
2508 continue;
2509 }
2510
2511 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2512 if (ICE->getCastKind() == CK_NoOp ||
2513 ICE->getCastKind() == CK_LValueToRValue ||
2514 ICE->getCastKind() == CK_DerivedToBase ||
2515 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2516 E = ICE->getSubExpr();
2517 continue;
2518 }
2519 }
2520
2521 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2522 if (UnOp->getOpcode() == UO_Extension) {
2523 E = UnOp->getSubExpr();
2524 continue;
2525 }
2526 }
2527
Douglas Gregor03e80032011-06-21 17:03:29 +00002528 if (const MaterializeTemporaryExpr *M
2529 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2530 E = M->GetTemporaryExpr();
2531 continue;
2532 }
2533
Douglas Gregor75e85042011-03-02 21:06:53 +00002534 break;
2535 }
2536
2537 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2538 return This->isImplicit();
2539
2540 return false;
2541}
2542
Douglas Gregor898574e2008-12-05 23:32:09 +00002543/// hasAnyTypeDependentArguments - Determines if any of the expressions
2544/// in Exprs is type-dependent.
Ahmed Charles13a140c2012-02-25 11:00:22 +00002545bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2546 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002547 if (Exprs[I]->isTypeDependent())
2548 return true;
2549
2550 return false;
2551}
2552
John McCall4204f072010-08-02 21:13:48 +00002553bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002554 // This function is attempting whether an expression is an initializer
2555 // which can be evaluated at compile-time. isEvaluatable handles most
2556 // of the cases, but it can't deal with some initializer-specific
2557 // expressions, and it can't deal with aggregates; we deal with those here,
2558 // and fall back to isEvaluatable for the other cases.
2559
John McCall4204f072010-08-02 21:13:48 +00002560 // If we ever capture reference-binding directly in the AST, we can
2561 // kill the second parameter.
2562
2563 if (IsForRef) {
2564 EvalResult Result;
2565 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2566 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002567
Anders Carlssone8a32b82008-11-24 05:23:59 +00002568 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002569 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002570 case IntegerLiteralClass:
2571 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002572 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002573 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002574 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002575 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002576 case CXXTemporaryObjectExprClass:
2577 case CXXConstructExprClass: {
2578 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002579
2580 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002581 if (CE->getConstructor()->isTrivial()) {
2582 // 1) an application of the trivial default constructor or
2583 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002584
Richard Smith180f4792011-11-10 06:34:14 +00002585 // 2) an elidable trivial copy construction of an operand which is
2586 // itself a constant initializer. Note that we consider the
2587 // operand on its own, *not* as a reference binding.
2588 if (CE->isElidable() &&
2589 CE->getArg(0)->isConstantInitializer(Ctx, false))
2590 return true;
2591 }
2592
2593 // 3) a foldable constexpr constructor.
2594 break;
John McCallb4b9b152010-08-01 21:51:45 +00002595 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002596 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002597 // This handles gcc's extension that allows global initializers like
2598 // "struct x {int x;} x = (struct x) {};".
2599 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002600 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002601 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002602 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002603 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002604 // FIXME: This doesn't deal with fields with reference types correctly.
2605 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2606 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002607 const InitListExpr *Exp = cast<InitListExpr>(this);
2608 unsigned numInits = Exp->getNumInits();
2609 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002610 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002611 return false;
2612 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002613 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002614 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002615 case ImplicitValueInitExprClass:
2616 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002617 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002618 return cast<ParenExpr>(this)->getSubExpr()
2619 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002620 case GenericSelectionExprClass:
2621 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2622 return false;
2623 return cast<GenericSelectionExpr>(this)->getResultExpr()
2624 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002625 case ChooseExprClass:
2626 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2627 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002628 case UnaryOperatorClass: {
2629 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002630 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002631 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002632 break;
2633 }
John McCall4204f072010-08-02 21:13:48 +00002634 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002635 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002636 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002637 case CStyleCastExprClass: {
2638 const CastExpr *CE = cast<CastExpr>(this);
2639
David Chisnall7a7ee302012-01-16 17:27:18 +00002640 // If we're promoting an integer to an _Atomic type then this is constant
2641 // if the integer is constant. We also need to check the converse in case
2642 // someone does something like:
2643 //
2644 // int a = (_Atomic(int))42;
2645 //
2646 // I doubt anyone would write code like this directly, but it's quite
2647 // possible as the result of macro expansions.
2648 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2649 CE->getCastKind() == CK_AtomicToNonAtomic)
2650 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2651
Richard Smithd62ca372011-12-06 22:44:34 +00002652 // Handle bitcasts of vector constants.
2653 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2654 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2655
Eli Friedman6bd97192011-12-21 00:43:02 +00002656 // Handle misc casts we want to ignore.
2657 // FIXME: Is it really safe to ignore all these?
2658 if (CE->getCastKind() == CK_NoOp ||
2659 CE->getCastKind() == CK_LValueToRValue ||
2660 CE->getCastKind() == CK_ToUnion ||
2661 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002662 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2663
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002664 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002665 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002666 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002667 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002668 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002669 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002670 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002671}
2672
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002673namespace {
2674 /// \brief Look for a call to a non-trivial function within an expression.
2675 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2676 {
2677 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2678
2679 bool NonTrivial;
2680
2681 public:
2682 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002683 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002684
2685 bool hasNonTrivialCall() const { return NonTrivial; }
2686
2687 void VisitCallExpr(CallExpr *E) {
2688 if (CXXMethodDecl *Method
2689 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2690 if (Method->isTrivial()) {
2691 // Recurse to children of the call.
2692 Inherited::VisitStmt(E);
2693 return;
2694 }
2695 }
2696
2697 NonTrivial = true;
2698 }
2699
2700 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2701 if (E->getConstructor()->isTrivial()) {
2702 // Recurse to children of the call.
2703 Inherited::VisitStmt(E);
2704 return;
2705 }
2706
2707 NonTrivial = true;
2708 }
2709
2710 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2711 if (E->getTemporary()->getDestructor()->isTrivial()) {
2712 Inherited::VisitStmt(E);
2713 return;
2714 }
2715
2716 NonTrivial = true;
2717 }
2718 };
2719}
2720
2721bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2722 NonTrivialCallFinder Finder(Ctx);
2723 Finder.Visit(this);
2724 return Finder.hasNonTrivialCall();
2725}
2726
Chandler Carruth82214a82011-02-18 23:54:50 +00002727/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2728/// pointer constant or not, as well as the specific kind of constant detected.
2729/// Null pointer constants can be integer constant expressions with the
2730/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2731/// (a GNU extension).
2732Expr::NullPointerConstantKind
2733Expr::isNullPointerConstant(ASTContext &Ctx,
2734 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002735 if (isValueDependent()) {
2736 switch (NPC) {
2737 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002738 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002739 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002740 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2741 return NPCK_ZeroInteger;
2742 else
2743 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002744
Douglas Gregorce940492009-09-25 04:25:58 +00002745 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002746 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002747 }
2748 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002749
Sebastian Redl07779722008-10-31 14:43:28 +00002750 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002751 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002752 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002753 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002754 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002755 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002756 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002757 Pointee->isVoidType() && // to void*
2758 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002759 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002760 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002761 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002762 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2763 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002764 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002765 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2766 // Accept ((void*)0) as a null pointer constant, as many other
2767 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002768 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002769 } else if (const GenericSelectionExpr *GE =
2770 dyn_cast<GenericSelectionExpr>(this)) {
2771 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002772 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002773 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002774 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002775 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002776 } else if (isa<GNUNullExpr>(this)) {
2777 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002778 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002779 } else if (const MaterializeTemporaryExpr *M
2780 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2781 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002782 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2783 if (const Expr *Source = OVE->getSourceExpr())
2784 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002785 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002786
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002787 // C++0x nullptr_t is always a null pointer constant.
2788 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002789 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002790
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002791 if (const RecordType *UT = getType()->getAsUnionType())
2792 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2793 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2794 const Expr *InitExpr = CLE->getInitializer();
2795 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2796 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2797 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002798 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002799 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002800 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002801 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Reid Spencer5f016e22007-07-11 17:01:13 +00002803 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith70488e22012-02-14 21:38:30 +00002804 // test for the value 0. Don't use the C++11 constant expression semantics
2805 // for this, for now; once the dust settles on core issue 903, we might only
2806 // allow a literal 0 here in C++11 mode.
2807 if (Ctx.getLangOptions().CPlusPlus0x) {
2808 if (!isCXX98IntegralConstantExpr(Ctx))
2809 return NPCK_NotNull;
2810 } else {
2811 if (!isIntegerConstantExpr(Ctx))
2812 return NPCK_NotNull;
2813 }
Chandler Carruth82214a82011-02-18 23:54:50 +00002814
Richard Smith70488e22012-02-14 21:38:30 +00002815 return (EvaluateKnownConstInt(Ctx) == 0) ? NPCK_ZeroInteger : NPCK_NotNull;
Reid Spencer5f016e22007-07-11 17:01:13 +00002816}
Steve Naroff31a45842007-07-28 23:10:27 +00002817
John McCallf6a16482010-12-04 03:47:34 +00002818/// \brief If this expression is an l-value for an Objective C
2819/// property, find the underlying property reference expression.
2820const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2821 const Expr *E = this;
2822 while (true) {
2823 assert((E->getValueKind() == VK_LValue &&
2824 E->getObjectKind() == OK_ObjCProperty) &&
2825 "expression is not a property reference");
2826 E = E->IgnoreParenCasts();
2827 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2828 if (BO->getOpcode() == BO_Comma) {
2829 E = BO->getRHS();
2830 continue;
2831 }
2832 }
2833
2834 break;
2835 }
2836
2837 return cast<ObjCPropertyRefExpr>(E);
2838}
2839
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002840FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002841 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002842
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002843 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002844 if (ICE->getCastKind() == CK_LValueToRValue ||
2845 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002846 E = ICE->getSubExpr()->IgnoreParens();
2847 else
2848 break;
2849 }
2850
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002851 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002852 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002853 if (Field->isBitField())
2854 return Field;
2855
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002856 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2857 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2858 if (Field->isBitField())
2859 return Field;
2860
Eli Friedman42068e92011-07-13 02:05:57 +00002861 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002862 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2863 return BinOp->getLHS()->getBitField();
2864
Eli Friedman42068e92011-07-13 02:05:57 +00002865 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2866 return BinOp->getRHS()->getBitField();
2867 }
2868
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002869 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002870}
2871
Anders Carlsson09380262010-01-31 17:18:49 +00002872bool Expr::refersToVectorElement() const {
2873 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002874
Anders Carlsson09380262010-01-31 17:18:49 +00002875 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002876 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002877 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002878 E = ICE->getSubExpr()->IgnoreParens();
2879 else
2880 break;
2881 }
Sean Huntc3021132010-05-05 15:23:54 +00002882
Anders Carlsson09380262010-01-31 17:18:49 +00002883 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2884 return ASE->getBase()->getType()->isVectorType();
2885
2886 if (isa<ExtVectorElementExpr>(E))
2887 return true;
2888
2889 return false;
2890}
2891
Chris Lattner2140e902009-02-16 22:14:05 +00002892/// isArrow - Return true if the base expression is a pointer to vector,
2893/// return false if the base expression is a vector.
2894bool ExtVectorElementExpr::isArrow() const {
2895 return getBase()->getType()->isPointerType();
2896}
2897
Nate Begeman213541a2008-04-18 23:10:10 +00002898unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002899 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002900 return VT->getNumElements();
2901 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002902}
2903
Nate Begeman8a997642008-05-09 06:41:27 +00002904/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002905bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002906 // FIXME: Refactor this code to an accessor on the AST node which returns the
2907 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002908 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002909
2910 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002911 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002912 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002913
Nate Begeman190d6a22009-01-18 02:01:21 +00002914 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002915 if (Comp[0] == 's' || Comp[0] == 'S')
2916 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002917
Daniel Dunbar15027422009-10-17 23:53:04 +00002918 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002919 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002920 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002921
Steve Narofffec0b492007-07-30 03:29:09 +00002922 return false;
2923}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002924
Nate Begeman8a997642008-05-09 06:41:27 +00002925/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002926void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002927 SmallVectorImpl<unsigned> &Elts) const {
2928 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002929 if (Comp[0] == 's' || Comp[0] == 'S')
2930 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002931
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002932 bool isHi = Comp == "hi";
2933 bool isLo = Comp == "lo";
2934 bool isEven = Comp == "even";
2935 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Nate Begeman8a997642008-05-09 06:41:27 +00002937 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2938 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002939
Nate Begeman8a997642008-05-09 06:41:27 +00002940 if (isHi)
2941 Index = e + i;
2942 else if (isLo)
2943 Index = i;
2944 else if (isEven)
2945 Index = 2 * i;
2946 else if (isOdd)
2947 Index = 2 * i + 1;
2948 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002949 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002950
Nate Begeman3b8d1162008-05-13 21:03:02 +00002951 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002952 }
Nate Begeman8a997642008-05-09 06:41:27 +00002953}
2954
Douglas Gregor04badcf2010-04-21 00:45:42 +00002955ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002956 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002957 SourceLocation LBracLoc,
2958 SourceLocation SuperLoc,
2959 bool IsInstanceSuper,
2960 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002961 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002962 ArrayRef<SourceLocation> SelLocs,
2963 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002964 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002965 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002966 SourceLocation RBracLoc,
2967 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002968 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002969 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002970 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002971 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002972 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2973 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002974 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002975 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
2976 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002977{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002978 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002979 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002980}
2981
Douglas Gregor04badcf2010-04-21 00:45:42 +00002982ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002983 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002984 SourceLocation LBracLoc,
2985 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002986 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002987 ArrayRef<SourceLocation> SelLocs,
2988 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002989 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002990 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002991 SourceLocation RBracLoc,
2992 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002993 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002994 T->isDependentType(), T->isInstantiationDependentType(),
2995 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002996 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2997 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002998 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002999 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003000 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003001{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003002 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003003 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003004}
3005
Douglas Gregor04badcf2010-04-21 00:45:42 +00003006ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003007 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003008 SourceLocation LBracLoc,
3009 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003010 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003011 ArrayRef<SourceLocation> SelLocs,
3012 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003013 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003014 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003015 SourceLocation RBracLoc,
3016 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003017 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003018 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003019 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003020 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003021 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3022 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003023 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003024 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003025 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003026{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003027 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003028 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003029}
3030
3031void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3032 ArrayRef<SourceLocation> SelLocs,
3033 SelectorLocationsKind SelLocsK) {
3034 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003035 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003036 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003037 if (Args[I]->isTypeDependent())
3038 ExprBits.TypeDependent = true;
3039 if (Args[I]->isValueDependent())
3040 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003041 if (Args[I]->isInstantiationDependent())
3042 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003043 if (Args[I]->containsUnexpandedParameterPack())
3044 ExprBits.ContainsUnexpandedParameterPack = true;
3045
3046 MyArgs[I] = Args[I];
3047 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003048
Benjamin Kramer19562c92012-02-20 00:20:48 +00003049 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003050 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003051 if (SelLocsK == SelLoc_NonStandard)
3052 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3053 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003054}
3055
Douglas Gregor04badcf2010-04-21 00:45:42 +00003056ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003057 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003058 SourceLocation LBracLoc,
3059 SourceLocation SuperLoc,
3060 bool IsInstanceSuper,
3061 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003062 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003063 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003064 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003065 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003066 SourceLocation RBracLoc,
3067 bool isImplicit) {
3068 assert((!SelLocs.empty() || isImplicit) &&
3069 "No selector locs for non-implicit message");
3070 ObjCMessageExpr *Mem;
3071 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3072 if (isImplicit)
3073 Mem = alloc(Context, Args.size(), 0);
3074 else
3075 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003076 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003077 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003078 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003079}
3080
3081ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003082 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003083 SourceLocation LBracLoc,
3084 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003085 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003086 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003087 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003088 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003089 SourceLocation RBracLoc,
3090 bool isImplicit) {
3091 assert((!SelLocs.empty() || isImplicit) &&
3092 "No selector locs for non-implicit message");
3093 ObjCMessageExpr *Mem;
3094 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3095 if (isImplicit)
3096 Mem = alloc(Context, Args.size(), 0);
3097 else
3098 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003099 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003100 SelLocs, SelLocsK, Method, Args, RBracLoc,
3101 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003102}
3103
3104ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003105 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003106 SourceLocation LBracLoc,
3107 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003108 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003109 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003110 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003111 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003112 SourceLocation RBracLoc,
3113 bool isImplicit) {
3114 assert((!SelLocs.empty() || isImplicit) &&
3115 "No selector locs for non-implicit message");
3116 ObjCMessageExpr *Mem;
3117 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3118 if (isImplicit)
3119 Mem = alloc(Context, Args.size(), 0);
3120 else
3121 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003122 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003123 SelLocs, SelLocsK, Method, Args, RBracLoc,
3124 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003125}
3126
Sean Huntc3021132010-05-05 15:23:54 +00003127ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003128 unsigned NumArgs,
3129 unsigned NumStoredSelLocs) {
3130 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003131 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3132}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003133
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003134ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3135 ArrayRef<Expr *> Args,
3136 SourceLocation RBraceLoc,
3137 ArrayRef<SourceLocation> SelLocs,
3138 Selector Sel,
3139 SelectorLocationsKind &SelLocsK) {
3140 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3141 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3142 : 0;
3143 return alloc(C, Args.size(), NumStoredSelLocs);
3144}
3145
3146ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3147 unsigned NumArgs,
3148 unsigned NumStoredSelLocs) {
3149 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3150 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3151 return (ObjCMessageExpr *)C.Allocate(Size,
3152 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3153}
3154
3155void ObjCMessageExpr::getSelectorLocs(
3156 SmallVectorImpl<SourceLocation> &SelLocs) const {
3157 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3158 SelLocs.push_back(getSelectorLoc(i));
3159}
3160
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003161SourceRange ObjCMessageExpr::getReceiverRange() const {
3162 switch (getReceiverKind()) {
3163 case Instance:
3164 return getInstanceReceiver()->getSourceRange();
3165
3166 case Class:
3167 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3168
3169 case SuperInstance:
3170 case SuperClass:
3171 return getSuperLoc();
3172 }
3173
David Blaikie30263482012-01-20 21:50:17 +00003174 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003175}
3176
Douglas Gregor04badcf2010-04-21 00:45:42 +00003177Selector ObjCMessageExpr::getSelector() const {
3178 if (HasMethod)
3179 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3180 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003181 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003182}
3183
3184ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3185 switch (getReceiverKind()) {
3186 case Instance:
3187 if (const ObjCObjectPointerType *Ptr
3188 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
3189 return Ptr->getInterfaceDecl();
3190 break;
3191
3192 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00003193 if (const ObjCObjectType *Ty
3194 = getClassReceiver()->getAs<ObjCObjectType>())
3195 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003196 break;
3197
3198 case SuperInstance:
3199 if (const ObjCObjectPointerType *Ptr
3200 = getSuperType()->getAs<ObjCObjectPointerType>())
3201 return Ptr->getInterfaceDecl();
3202 break;
3203
3204 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00003205 if (const ObjCObjectType *Iface
3206 = getSuperType()->getAs<ObjCObjectType>())
3207 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003208 break;
3209 }
3210
3211 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003212}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003213
Chris Lattner5f9e2722011-07-23 10:55:15 +00003214StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003215 switch (getBridgeKind()) {
3216 case OBC_Bridge:
3217 return "__bridge";
3218 case OBC_BridgeTransfer:
3219 return "__bridge_transfer";
3220 case OBC_BridgeRetained:
3221 return "__bridge_retained";
3222 }
David Blaikie30263482012-01-20 21:50:17 +00003223
3224 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003225}
3226
Jay Foad4ba2a172011-01-12 09:06:06 +00003227bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003228 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003229}
3230
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003231ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3232 QualType Type, SourceLocation BLoc,
3233 SourceLocation RP)
3234 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3235 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003236 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003237 Type->containsUnexpandedParameterPack()),
3238 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3239{
3240 SubExprs = new (C) Stmt*[nexpr];
3241 for (unsigned i = 0; i < nexpr; i++) {
3242 if (args[i]->isTypeDependent())
3243 ExprBits.TypeDependent = true;
3244 if (args[i]->isValueDependent())
3245 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003246 if (args[i]->isInstantiationDependent())
3247 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003248 if (args[i]->containsUnexpandedParameterPack())
3249 ExprBits.ContainsUnexpandedParameterPack = true;
3250
3251 SubExprs[i] = args[i];
3252 }
3253}
3254
Nate Begeman888376a2009-08-12 02:28:50 +00003255void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3256 unsigned NumExprs) {
3257 if (SubExprs) C.Deallocate(SubExprs);
3258
3259 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003260 this->NumExprs = NumExprs;
3261 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003262}
Nate Begeman888376a2009-08-12 02:28:50 +00003263
Peter Collingbournef111d932011-04-15 00:35:48 +00003264GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3265 SourceLocation GenericLoc, Expr *ControllingExpr,
3266 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3267 unsigned NumAssocs, SourceLocation DefaultLoc,
3268 SourceLocation RParenLoc,
3269 bool ContainsUnexpandedParameterPack,
3270 unsigned ResultIndex)
3271 : Expr(GenericSelectionExprClass,
3272 AssocExprs[ResultIndex]->getType(),
3273 AssocExprs[ResultIndex]->getValueKind(),
3274 AssocExprs[ResultIndex]->getObjectKind(),
3275 AssocExprs[ResultIndex]->isTypeDependent(),
3276 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003277 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003278 ContainsUnexpandedParameterPack),
3279 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3280 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3281 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3282 RParenLoc(RParenLoc) {
3283 SubExprs[CONTROLLING] = ControllingExpr;
3284 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3285 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3286}
3287
3288GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3289 SourceLocation GenericLoc, Expr *ControllingExpr,
3290 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3291 unsigned NumAssocs, SourceLocation DefaultLoc,
3292 SourceLocation RParenLoc,
3293 bool ContainsUnexpandedParameterPack)
3294 : Expr(GenericSelectionExprClass,
3295 Context.DependentTy,
3296 VK_RValue,
3297 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003298 /*isTypeDependent=*/true,
3299 /*isValueDependent=*/true,
3300 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003301 ContainsUnexpandedParameterPack),
3302 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3303 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3304 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3305 RParenLoc(RParenLoc) {
3306 SubExprs[CONTROLLING] = ControllingExpr;
3307 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3308 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3309}
3310
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003311//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003312// DesignatedInitExpr
3313//===----------------------------------------------------------------------===//
3314
Chandler Carruthb1138242011-06-16 06:47:06 +00003315IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003316 assert(Kind == FieldDesignator && "Only valid on a field designator");
3317 if (Field.NameOrField & 0x01)
3318 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3319 else
3320 return getField()->getIdentifier();
3321}
3322
Sean Huntc3021132010-05-05 15:23:54 +00003323DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003324 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003325 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003326 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003327 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003328 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003329 unsigned NumIndexExprs,
3330 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003331 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003332 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003333 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003334 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003335 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003336 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3337 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003338 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003339
3340 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003341 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003342 *Child++ = Init;
3343
3344 // Copy the designators and their subexpressions, computing
3345 // value-dependence along the way.
3346 unsigned IndexIdx = 0;
3347 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003348 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003349
3350 if (this->Designators[I].isArrayDesignator()) {
3351 // Compute type- and value-dependence.
3352 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003353 if (Index->isTypeDependent() || Index->isValueDependent())
3354 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003355 if (Index->isInstantiationDependent())
3356 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003357 // Propagate unexpanded parameter packs.
3358 if (Index->containsUnexpandedParameterPack())
3359 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003360
3361 // Copy the index expressions into permanent storage.
3362 *Child++ = IndexExprs[IndexIdx++];
3363 } else if (this->Designators[I].isArrayRangeDesignator()) {
3364 // Compute type- and value-dependence.
3365 Expr *Start = IndexExprs[IndexIdx];
3366 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003367 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003368 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003369 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003370 ExprBits.InstantiationDependent = true;
3371 } else if (Start->isInstantiationDependent() ||
3372 End->isInstantiationDependent()) {
3373 ExprBits.InstantiationDependent = true;
3374 }
3375
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003376 // Propagate unexpanded parameter packs.
3377 if (Start->containsUnexpandedParameterPack() ||
3378 End->containsUnexpandedParameterPack())
3379 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003380
3381 // Copy the start/end expressions into permanent storage.
3382 *Child++ = IndexExprs[IndexIdx++];
3383 *Child++ = IndexExprs[IndexIdx++];
3384 }
3385 }
3386
3387 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003388}
3389
Douglas Gregor05c13a32009-01-22 00:58:24 +00003390DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003391DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003392 unsigned NumDesignators,
3393 Expr **IndexExprs, unsigned NumIndexExprs,
3394 SourceLocation ColonOrEqualLoc,
3395 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003396 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003397 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003398 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003399 ColonOrEqualLoc, UsesColonSyntax,
3400 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003401}
3402
Mike Stump1eb44332009-09-09 15:08:12 +00003403DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003404 unsigned NumIndexExprs) {
3405 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3406 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3407 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3408}
3409
Douglas Gregor319d57f2010-01-06 23:17:19 +00003410void DesignatedInitExpr::setDesignators(ASTContext &C,
3411 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003412 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003413 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003414 NumDesignators = NumDesigs;
3415 for (unsigned I = 0; I != NumDesigs; ++I)
3416 Designators[I] = Desigs[I];
3417}
3418
Abramo Bagnara24f46742011-03-16 15:08:46 +00003419SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3420 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3421 if (size() == 1)
3422 return DIE->getDesignator(0)->getSourceRange();
3423 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3424 DIE->getDesignator(size()-1)->getEndLocation());
3425}
3426
Douglas Gregor05c13a32009-01-22 00:58:24 +00003427SourceRange DesignatedInitExpr::getSourceRange() const {
3428 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003429 Designator &First =
3430 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003431 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003432 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003433 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3434 else
3435 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3436 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003437 StartLoc =
3438 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003439 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3440}
3441
Douglas Gregor05c13a32009-01-22 00:58:24 +00003442Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3443 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3444 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3445 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003446 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3447 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3448}
3449
3450Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003451 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003452 "Requires array range designator");
3453 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3454 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003455 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3456 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3457}
3458
3459Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003460 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003461 "Requires array range designator");
3462 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3463 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003464 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3465 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3466}
3467
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003468/// \brief Replaces the designator at index @p Idx with the series
3469/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003470void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003471 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003472 const Designator *Last) {
3473 unsigned NumNewDesignators = Last - First;
3474 if (NumNewDesignators == 0) {
3475 std::copy_backward(Designators + Idx + 1,
3476 Designators + NumDesignators,
3477 Designators + Idx);
3478 --NumNewDesignators;
3479 return;
3480 } else if (NumNewDesignators == 1) {
3481 Designators[Idx] = *First;
3482 return;
3483 }
3484
Mike Stump1eb44332009-09-09 15:08:12 +00003485 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003486 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003487 std::copy(Designators, Designators + Idx, NewDesignators);
3488 std::copy(First, Last, NewDesignators + Idx);
3489 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3490 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003491 Designators = NewDesignators;
3492 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3493}
3494
Mike Stump1eb44332009-09-09 15:08:12 +00003495ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003496 Expr **exprs, unsigned nexprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003497 SourceLocation rparenloc)
3498 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003499 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003500 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00003501 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003502 for (unsigned i = 0; i != nexprs; ++i) {
3503 if (exprs[i]->isTypeDependent())
3504 ExprBits.TypeDependent = true;
3505 if (exprs[i]->isValueDependent())
3506 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003507 if (exprs[i]->isInstantiationDependent())
3508 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003509 if (exprs[i]->containsUnexpandedParameterPack())
3510 ExprBits.ContainsUnexpandedParameterPack = true;
3511
Nate Begeman2ef13e52009-08-10 23:49:36 +00003512 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003513 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003514}
3515
John McCalle996ffd2011-02-16 08:02:54 +00003516const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3517 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3518 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003519 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3520 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003521 e = cast<CXXConstructExpr>(e)->getArg(0);
3522 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3523 e = ice->getSubExpr();
3524 return cast<OpaqueValueExpr>(e);
3525}
3526
John McCall4b9c2d22011-11-06 09:01:30 +00003527PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3528 unsigned numSemanticExprs) {
3529 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3530 (1 + numSemanticExprs) * sizeof(Expr*),
3531 llvm::alignOf<PseudoObjectExpr>());
3532 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3533}
3534
3535PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3536 : Expr(PseudoObjectExprClass, shell) {
3537 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3538}
3539
3540PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3541 ArrayRef<Expr*> semantics,
3542 unsigned resultIndex) {
3543 assert(syntax && "no syntactic expression!");
3544 assert(semantics.size() && "no semantic expressions!");
3545
3546 QualType type;
3547 ExprValueKind VK;
3548 if (resultIndex == NoResult) {
3549 type = C.VoidTy;
3550 VK = VK_RValue;
3551 } else {
3552 assert(resultIndex < semantics.size());
3553 type = semantics[resultIndex]->getType();
3554 VK = semantics[resultIndex]->getValueKind();
3555 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3556 }
3557
3558 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3559 (1 + semantics.size()) * sizeof(Expr*),
3560 llvm::alignOf<PseudoObjectExpr>());
3561 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3562 resultIndex);
3563}
3564
3565PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3566 Expr *syntax, ArrayRef<Expr*> semantics,
3567 unsigned resultIndex)
3568 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3569 /*filled in at end of ctor*/ false, false, false, false) {
3570 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3571 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3572
3573 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3574 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3575 getSubExprsBuffer()[i] = E;
3576
3577 if (E->isTypeDependent())
3578 ExprBits.TypeDependent = true;
3579 if (E->isValueDependent())
3580 ExprBits.ValueDependent = true;
3581 if (E->isInstantiationDependent())
3582 ExprBits.InstantiationDependent = true;
3583 if (E->containsUnexpandedParameterPack())
3584 ExprBits.ContainsUnexpandedParameterPack = true;
3585
3586 if (isa<OpaqueValueExpr>(E))
3587 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3588 "opaque-value semantic expressions for pseudo-object "
3589 "operations must have sources");
3590 }
3591}
3592
Douglas Gregor05c13a32009-01-22 00:58:24 +00003593//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003594// ExprIterator.
3595//===----------------------------------------------------------------------===//
3596
3597Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3598Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3599Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3600const Expr* ConstExprIterator::operator[](size_t idx) const {
3601 return cast<Expr>(I[idx]);
3602}
3603const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3604const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3605
3606//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003607// Child Iterators for iterating over subexpressions/substatements
3608//===----------------------------------------------------------------------===//
3609
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003610// UnaryExprOrTypeTraitExpr
3611Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003612 // If this is of a type and the type is a VLA type (and not a typedef), the
3613 // size expression of the VLA needs to be treated as an executable expression.
3614 // Why isn't this weirdness documented better in StmtIterator?
3615 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003616 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003617 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003618 return child_range(child_iterator(T), child_iterator());
3619 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003620 }
John McCall63c00d72011-02-09 08:16:59 +00003621 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003622}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003623
Steve Naroff563477d2007-09-18 23:55:05 +00003624// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003625Stmt::child_range ObjCMessageExpr::children() {
3626 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003627 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003628 begin = reinterpret_cast<Stmt **>(this + 1);
3629 else
3630 begin = reinterpret_cast<Stmt **>(getArgs());
3631 return child_range(begin,
3632 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003633}
3634
Steve Naroff4eb206b2008-09-03 18:15:37 +00003635// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003636BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003637 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003638 bool constAdded)
Douglas Gregor561f8122011-07-01 01:22:09 +00003639 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003640 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003641 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003642{
Douglas Gregord967e312011-01-19 21:52:31 +00003643 bool TypeDependent = false;
3644 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +00003645 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +00003646 computeDeclRefDependence(D->getASTContext(), D, getType(), TypeDependent,
3647 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +00003648 ExprBits.TypeDependent = TypeDependent;
3649 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +00003650 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003651}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003652
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003653ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3654 QualType T, ObjCMethodDecl *Method,
3655 SourceRange SR)
3656 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3657 false, false, false, false),
3658 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3659{
3660 Expr **SaveElements = getElements();
3661 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3662 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3663 ExprBits.ValueDependent = true;
3664 if (Elements[I]->isInstantiationDependent())
3665 ExprBits.InstantiationDependent = true;
3666 if (Elements[I]->containsUnexpandedParameterPack())
3667 ExprBits.ContainsUnexpandedParameterPack = true;
3668
3669 SaveElements[I] = Elements[I];
3670 }
3671}
3672
3673ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3674 llvm::ArrayRef<Expr *> Elements,
3675 QualType T, ObjCMethodDecl * Method,
3676 SourceRange SR) {
3677 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3678 + Elements.size() * sizeof(Expr *));
3679 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3680}
3681
3682ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3683 unsigned NumElements) {
3684
3685 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3686 + NumElements * sizeof(Expr *));
3687 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3688}
3689
3690ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3691 ArrayRef<ObjCDictionaryElement> VK,
3692 bool HasPackExpansions,
3693 QualType T, ObjCMethodDecl *method,
3694 SourceRange SR)
3695 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3696 false, false),
3697 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3698 DictWithObjectsMethod(method)
3699{
3700 KeyValuePair *KeyValues = getKeyValues();
3701 ExpansionData *Expansions = getExpansionData();
3702 for (unsigned I = 0; I < NumElements; I++) {
3703 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3704 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3705 ExprBits.ValueDependent = true;
3706 if (VK[I].Key->isInstantiationDependent() ||
3707 VK[I].Value->isInstantiationDependent())
3708 ExprBits.InstantiationDependent = true;
3709 if (VK[I].EllipsisLoc.isInvalid() &&
3710 (VK[I].Key->containsUnexpandedParameterPack() ||
3711 VK[I].Value->containsUnexpandedParameterPack()))
3712 ExprBits.ContainsUnexpandedParameterPack = true;
3713
3714 KeyValues[I].Key = VK[I].Key;
3715 KeyValues[I].Value = VK[I].Value;
3716 if (Expansions) {
3717 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3718 if (VK[I].NumExpansions)
3719 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3720 else
3721 Expansions[I].NumExpansionsPlusOne = 0;
3722 }
3723 }
3724}
3725
3726ObjCDictionaryLiteral *
3727ObjCDictionaryLiteral::Create(ASTContext &C,
3728 ArrayRef<ObjCDictionaryElement> VK,
3729 bool HasPackExpansions,
3730 QualType T, ObjCMethodDecl *method,
3731 SourceRange SR) {
3732 unsigned ExpansionsSize = 0;
3733 if (HasPackExpansions)
3734 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3735
3736 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3737 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3738 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3739}
3740
3741ObjCDictionaryLiteral *
3742ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3743 bool HasPackExpansions) {
3744 unsigned ExpansionsSize = 0;
3745 if (HasPackExpansions)
3746 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3747 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3748 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3749 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3750 HasPackExpansions);
3751}
3752
3753ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
3754 Expr *base,
3755 Expr *key, QualType T,
3756 ObjCMethodDecl *getMethod,
3757 ObjCMethodDecl *setMethod,
3758 SourceLocation RB) {
3759 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
3760 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
3761 OK_ObjCSubscript,
3762 getMethod, setMethod, RB);
3763}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003764
3765AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3766 QualType t, AtomicOp op, SourceLocation RP)
3767 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3768 false, false, false, false),
3769 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3770{
3771 for (unsigned i = 0; i < nexpr; i++) {
3772 if (args[i]->isTypeDependent())
3773 ExprBits.TypeDependent = true;
3774 if (args[i]->isValueDependent())
3775 ExprBits.ValueDependent = true;
3776 if (args[i]->isInstantiationDependent())
3777 ExprBits.InstantiationDependent = true;
3778 if (args[i]->containsUnexpandedParameterPack())
3779 ExprBits.ContainsUnexpandedParameterPack = true;
3780
3781 SubExprs[i] = args[i];
3782 }
3783}