blob: e35091a101736ae2ff0bb89eba417c4a2a8acbd3 [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.
137static void computeDeclRefDependence(NamedDecl *D, QualType T,
138 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000139 bool &ValueDependent,
140 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000141 TypeDependent = false;
142 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000143 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000144
145 // (TD) C++ [temp.dep.expr]p3:
146 // An id-expression is type-dependent if it contains:
147 //
Sean Huntc3021132010-05-05 15:23:54 +0000148 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000149 //
150 // (VD) C++ [temp.dep.constexpr]p2:
151 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000152
Douglas Gregor0da76df2009-11-23 11:41:28 +0000153 // (TD) - an identifier that was declared with dependent type
154 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000155 if (T->isDependentType()) {
156 TypeDependent = true;
157 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000158 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000159 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000160 } else if (T->isInstantiationDependentType()) {
161 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000162 }
Douglas Gregord967e312011-01-19 21:52:31 +0000163
Douglas Gregor0da76df2009-11-23 11:41:28 +0000164 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000165 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000166 == DeclarationName::CXXConversionFunctionName) {
167 QualType T = D->getDeclName().getCXXNameType();
168 if (T->isDependentType()) {
169 TypeDependent = true;
170 ValueDependent = true;
171 InstantiationDependent = true;
172 return;
173 }
174
175 if (T->isInstantiationDependentType())
176 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000177 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000178
Douglas Gregor0da76df2009-11-23 11:41:28 +0000179 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000180 if (isa<NonTypeTemplateParmDecl>(D)) {
181 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000182 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000183 return;
184 }
185
Douglas Gregor0da76df2009-11-23 11:41:28 +0000186 // (VD) - a constant with integral or enumeration type and is
187 // initialized with an expression that is value-dependent.
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)) {
Richard Smithdb1822c2011-11-08 01:31:09 +0000194 if ((D->getASTContext().getLangOptions().CPlusPlus0x ?
195 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
Douglas Gregord967e312011-01-19 21:52:31 +0000227void DeclRefExpr::computeDependence() {
228 bool TypeDependent = false;
229 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000230 bool InstantiationDependent = false;
231 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
232 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
Chandler Carruth3aa81402011-05-01 23:48:14 +0000261DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000262 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000263 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000264 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000265 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000266 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000267 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000268 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
269 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000270 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000271 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000272 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
273 if (FoundD)
274 getInternalFoundDecl() = FoundD;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000275 DeclRefExprBits.HasTemplateKWAndArgsInfo
276 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
Douglas Gregor561f8122011-07-01 01:22:09 +0000277 if (TemplateArgs) {
278 bool Dependent = false;
279 bool InstantiationDependent = false;
280 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000281 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
282 Dependent,
283 InstantiationDependent,
284 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +0000285 if (InstantiationDependent)
286 setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000287 } else if (TemplateKWLoc.isValid()) {
288 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor561f8122011-07-01 01:22:09 +0000289 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000290 DeclRefExprBits.HadMultipleCandidates = 0;
291
Abramo Bagnara25777432010-08-11 22:01:17 +0000292 computeDependence();
293}
294
Douglas Gregora2813ce2009-10-23 18:54:35 +0000295DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000296 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000297 SourceLocation TemplateKWLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000298 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000299 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000300 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000301 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000302 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000303 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000304 return Create(Context, QualifierLoc, TemplateKWLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000305 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000306 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000307}
308
309DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000310 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000311 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000312 ValueDecl *D,
313 const DeclarationNameInfo &NameInfo,
314 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000315 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000316 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000317 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000318 // Filter out cases where the found Decl is the same as the value refenenced.
319 if (D == FoundD)
320 FoundD = 0;
321
Douglas Gregora2813ce2009-10-23 18:54:35 +0000322 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000323 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000324 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000325 if (FoundD)
326 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000327 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000328 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
329 else if (TemplateKWLoc.isValid())
330 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000331
Chris Lattner32488542010-10-30 05:14:06 +0000332 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000333 return new (Mem) DeclRefExpr(QualifierLoc, TemplateKWLoc, D, NameInfo,
334 FoundD, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000335}
336
Chandler Carruth3aa81402011-05-01 23:48:14 +0000337DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000338 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000339 bool HasFoundDecl,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000340 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000341 unsigned NumTemplateArgs) {
342 std::size_t Size = sizeof(DeclRefExpr);
343 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000344 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000345 if (HasFoundDecl)
346 Size += sizeof(NamedDecl *);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000347 if (HasTemplateKWAndArgsInfo)
348 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000349
Chris Lattner32488542010-10-30 05:14:06 +0000350 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000351 return new (Mem) DeclRefExpr(EmptyShell());
352}
353
Douglas Gregora2813ce2009-10-23 18:54:35 +0000354SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000355 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000356 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000357 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000358 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000359 R.setEnd(getRAngleLoc());
360 return R;
361}
362
Anders Carlsson3a082d82009-09-08 18:24:21 +0000363// FIXME: Maybe this should use DeclPrinter with a special "print predefined
364// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000365std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
366 ASTContext &Context = CurrentDecl->getASTContext();
367
Anders Carlsson3a082d82009-09-08 18:24:21 +0000368 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000369 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000370 return FD->getNameAsString();
371
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000372 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000373 llvm::raw_svector_ostream Out(Name);
374
375 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000376 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000377 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000378 if (MD->isStatic())
379 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000380 }
381
382 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000383
384 std::string Proto = FD->getQualifiedNameAsString(Policy);
385
John McCall183700f2009-09-21 23:43:11 +0000386 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000387 const FunctionProtoType *FT = 0;
388 if (FD->hasWrittenPrototype())
389 FT = dyn_cast<FunctionProtoType>(AFT);
390
391 Proto += "(";
392 if (FT) {
393 llvm::raw_string_ostream POut(Proto);
394 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
395 if (i) POut << ", ";
396 std::string Param;
397 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
398 POut << Param;
399 }
400
401 if (FT->isVariadic()) {
402 if (FD->getNumParams()) POut << ", ";
403 POut << "...";
404 }
405 }
406 Proto += ")";
407
Sam Weinig4eadcc52009-12-27 01:38:20 +0000408 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
409 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
410 if (ThisQuals.hasConst())
411 Proto += " const";
412 if (ThisQuals.hasVolatile())
413 Proto += " volatile";
414 }
415
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000416 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
417 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000418
419 Out << Proto;
420
421 Out.flush();
422 return Name.str().str();
423 }
424 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000425 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000426 llvm::raw_svector_ostream Out(Name);
427 Out << (MD->isInstanceMethod() ? '-' : '+');
428 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000429
430 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
431 // a null check to avoid a crash.
432 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000433 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000434
Anders Carlsson3a082d82009-09-08 18:24:21 +0000435 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000436 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000437 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000438
Anders Carlsson3a082d82009-09-08 18:24:21 +0000439 Out << ' ';
440 Out << MD->getSelector().getAsString();
441 Out << ']';
442
443 Out.flush();
444 return Name.str().str();
445 }
446 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
447 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
448 return "top level";
449 }
450 return "";
451}
452
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000453void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
454 if (hasAllocation())
455 C.Deallocate(pVal);
456
457 BitWidth = Val.getBitWidth();
458 unsigned NumWords = Val.getNumWords();
459 const uint64_t* Words = Val.getRawData();
460 if (NumWords > 1) {
461 pVal = new (C) uint64_t[NumWords];
462 std::copy(Words, Words + NumWords, pVal);
463 } else if (NumWords == 1)
464 VAL = Words[0];
465 else
466 VAL = 0;
467}
468
469IntegerLiteral *
470IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
471 QualType type, SourceLocation l) {
472 return new (C) IntegerLiteral(C, V, type, l);
473}
474
475IntegerLiteral *
476IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
477 return new (C) IntegerLiteral(Empty);
478}
479
480FloatingLiteral *
481FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
482 bool isexact, QualType Type, SourceLocation L) {
483 return new (C) FloatingLiteral(C, V, isexact, Type, L);
484}
485
486FloatingLiteral *
487FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000488 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000489}
490
Chris Lattnerda8249e2008-06-07 22:13:43 +0000491/// getValueAsApproximateDouble - This returns the value as an inaccurate
492/// double. Note that this may cause loss of precision, but is useful for
493/// debugging dumps, etc.
494double FloatingLiteral::getValueAsApproximateDouble() const {
495 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000496 bool ignored;
497 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
498 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000499 return V.convertToDouble();
500}
501
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000502int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000503 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000504 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000505 case Ascii:
506 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000507 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000508 break;
509 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000510 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000511 break;
512 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000513 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000514 break;
515 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000516 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000517 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000518 }
519 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
520 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000521 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000522 && "character byte widths supported are 1, 2, and 4 only");
523 return CharByteWidth;
524}
525
Chris Lattner5f9e2722011-07-23 10:55:15 +0000526StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000527 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000528 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000529 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000530 // Allocate enough space for the StringLiteral plus an array of locations for
531 // any concatenated string tokens.
532 void *Mem = C.Allocate(sizeof(StringLiteral)+
533 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000534 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000535 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Reid Spencer5f016e22007-07-11 17:01:13 +0000537 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000538 SL->setString(C,Str,Kind,Pascal);
539
Chris Lattner2085fd62009-02-18 06:40:38 +0000540 SL->TokLocs[0] = Loc[0];
541 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000542
Chris Lattner726e1682009-02-18 05:49:11 +0000543 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000544 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
545 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000546}
547
Douglas Gregor673ecd62009-04-15 16:35:07 +0000548StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
549 void *Mem = C.Allocate(sizeof(StringLiteral)+
550 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000551 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000552 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000553 SL->CharByteWidth = 0;
554 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000555 SL->NumConcatenated = NumStrs;
556 return SL;
557}
558
Eli Friedman64f45a22011-11-01 02:23:42 +0000559void StringLiteral::setString(ASTContext &C, StringRef Str,
560 StringKind Kind, bool IsPascal) {
561 //FIXME: we assume that the string data comes from a target that uses the same
562 // code unit size and endianess for the type of string.
563 this->Kind = Kind;
564 this->IsPascal = IsPascal;
565
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000566 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000567 assert((Str.size()%CharByteWidth == 0)
568 && "size of data must be multiple of CharByteWidth");
569 Length = Str.size()/CharByteWidth;
570
571 switch(CharByteWidth) {
572 case 1: {
573 char *AStrData = new (C) char[Length];
574 std::memcpy(AStrData,Str.data(),Str.size());
575 StrData.asChar = AStrData;
576 break;
577 }
578 case 2: {
579 uint16_t *AStrData = new (C) uint16_t[Length];
580 std::memcpy(AStrData,Str.data(),Str.size());
581 StrData.asUInt16 = AStrData;
582 break;
583 }
584 case 4: {
585 uint32_t *AStrData = new (C) uint32_t[Length];
586 std::memcpy(AStrData,Str.data(),Str.size());
587 StrData.asUInt32 = AStrData;
588 break;
589 }
590 default:
591 assert(false && "unsupported CharByteWidth");
592 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000593}
594
Chris Lattner08f92e32010-11-17 07:37:15 +0000595/// getLocationOfByte - Return a source location that points to the specified
596/// byte of this string literal.
597///
598/// Strings are amazingly complex. They can be formed from multiple tokens and
599/// can have escape sequences in them in addition to the usual trigraph and
600/// escaped newline business. This routine handles this complexity.
601///
602SourceLocation StringLiteral::
603getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
604 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000605 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
606
Chris Lattner08f92e32010-11-17 07:37:15 +0000607 // Loop over all of the tokens in this string until we find the one that
608 // contains the byte we're looking for.
609 unsigned TokNo = 0;
610 while (1) {
611 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
612 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
613
614 // Get the spelling of the string so that we can get the data that makes up
615 // the string literal, not the identifier for the macro it is potentially
616 // expanded through.
617 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
618
619 // Re-lex the token to get its length and original spelling.
620 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
621 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000622 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000623 if (Invalid)
624 return StrTokSpellingLoc;
625
626 const char *StrData = Buffer.data()+LocInfo.second;
627
628 // Create a langops struct and enable trigraphs. This is sufficient for
629 // relexing tokens.
630 LangOptions LangOpts;
631 LangOpts.Trigraphs = true;
632
633 // Create a lexer starting at the beginning of this token.
634 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
635 Buffer.end());
636 Token TheTok;
637 TheLexer.LexFromRawLexer(TheTok);
638
639 // Use the StringLiteralParser to compute the length of the string in bytes.
640 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
641 unsigned TokNumBytes = SLP.GetStringLength();
642
643 // If the byte is in this token, return the location of the byte.
644 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000645 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000646 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
647
648 // Now that we know the offset of the token in the spelling, use the
649 // preprocessor to get the offset in the original source.
650 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
651 }
652
653 // Move to the next string token.
654 ++TokNo;
655 ByteNo -= TokNumBytes;
656 }
657}
658
659
660
Reid Spencer5f016e22007-07-11 17:01:13 +0000661/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
662/// corresponds to, e.g. "sizeof" or "[pre]++".
663const char *UnaryOperator::getOpcodeStr(Opcode Op) {
664 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000665 case UO_PostInc: return "++";
666 case UO_PostDec: return "--";
667 case UO_PreInc: return "++";
668 case UO_PreDec: return "--";
669 case UO_AddrOf: return "&";
670 case UO_Deref: return "*";
671 case UO_Plus: return "+";
672 case UO_Minus: return "-";
673 case UO_Not: return "~";
674 case UO_LNot: return "!";
675 case UO_Real: return "__real";
676 case UO_Imag: return "__imag";
677 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000679 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000680}
681
John McCall2de56d12010-08-25 11:45:40 +0000682UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000683UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
684 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000685 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000686 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
687 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
688 case OO_Amp: return UO_AddrOf;
689 case OO_Star: return UO_Deref;
690 case OO_Plus: return UO_Plus;
691 case OO_Minus: return UO_Minus;
692 case OO_Tilde: return UO_Not;
693 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000694 }
695}
696
697OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
698 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000699 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
700 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
701 case UO_AddrOf: return OO_Amp;
702 case UO_Deref: return OO_Star;
703 case UO_Plus: return OO_Plus;
704 case UO_Minus: return OO_Minus;
705 case UO_Not: return OO_Tilde;
706 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000707 default: return OO_None;
708 }
709}
710
711
Reid Spencer5f016e22007-07-11 17:01:13 +0000712//===----------------------------------------------------------------------===//
713// Postfix Operators.
714//===----------------------------------------------------------------------===//
715
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000716CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
717 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000718 SourceLocation rparenloc)
719 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000720 fn->isTypeDependent(),
721 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000722 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000723 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000724 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000726 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000727 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000728 for (unsigned i = 0; i != numargs; ++i) {
729 if (args[i]->isTypeDependent())
730 ExprBits.TypeDependent = true;
731 if (args[i]->isValueDependent())
732 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000733 if (args[i]->isInstantiationDependent())
734 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000735 if (args[i]->containsUnexpandedParameterPack())
736 ExprBits.ContainsUnexpandedParameterPack = true;
737
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000738 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000739 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000740
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000741 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000742 RParenLoc = rparenloc;
743}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000744
Ted Kremenek668bf912009-02-09 20:51:47 +0000745CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000746 QualType t, ExprValueKind VK, SourceLocation rparenloc)
747 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000748 fn->isTypeDependent(),
749 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000750 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000751 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000752 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000753
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000754 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000755 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000756 for (unsigned i = 0; i != numargs; ++i) {
757 if (args[i]->isTypeDependent())
758 ExprBits.TypeDependent = true;
759 if (args[i]->isValueDependent())
760 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000761 if (args[i]->isInstantiationDependent())
762 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000763 if (args[i]->containsUnexpandedParameterPack())
764 ExprBits.ContainsUnexpandedParameterPack = true;
765
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000766 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000767 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000768
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000769 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 RParenLoc = rparenloc;
771}
772
Mike Stump1eb44332009-09-09 15:08:12 +0000773CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
774 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000775 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000776 SubExprs = new (C) Stmt*[PREARGS_START];
777 CallExprBits.NumPreArgs = 0;
778}
779
780CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
781 EmptyShell Empty)
782 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
783 // FIXME: Why do we allocate this?
784 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
785 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000786}
787
Nuno Lopesd20254f2009-12-20 23:11:08 +0000788Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000789 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000790
791 while (SubstNonTypeTemplateParmExpr *NTTP
792 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
793 CEE = NTTP->getReplacement()->IgnoreParenCasts();
794 }
795
Sebastian Redl20012152010-09-10 20:55:30 +0000796 // If we're calling a dereference, look at the pointer instead.
797 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
798 if (BO->isPtrMemOp())
799 CEE = BO->getRHS()->IgnoreParenCasts();
800 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
801 if (UO->getOpcode() == UO_Deref)
802 CEE = UO->getSubExpr()->IgnoreParenCasts();
803 }
Chris Lattner6346f962009-07-17 15:46:27 +0000804 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000805 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000806 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
807 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000808
809 return 0;
810}
811
Nuno Lopesd20254f2009-12-20 23:11:08 +0000812FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000813 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000814}
815
Chris Lattnerd18b3292007-12-28 05:25:02 +0000816/// setNumArgs - This changes the number of arguments present in this call.
817/// Any orphaned expressions are deleted by this, and any new operands are set
818/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000819void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000820 // No change, just return.
821 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Chris Lattnerd18b3292007-12-28 05:25:02 +0000823 // If shrinking # arguments, just delete the extras and forgot them.
824 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000825 this->NumArgs = NumArgs;
826 return;
827 }
828
829 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000830 unsigned NumPreArgs = getNumPreArgs();
831 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000832 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000833 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000834 NewSubExprs[i] = SubExprs[i];
835 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000836 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
837 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000838 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Douglas Gregor88c9a462009-04-17 21:46:47 +0000840 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000841 SubExprs = NewSubExprs;
842 this->NumArgs = NumArgs;
843}
844
Chris Lattnercb888962008-10-06 05:00:53 +0000845/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
846/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +0000847unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000848 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000849 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000850 // ImplicitCastExpr.
851 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
852 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000853 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000854
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000855 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
856 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000857 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Anders Carlssonbcba2012008-01-31 02:13:57 +0000859 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
860 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000861 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000863 if (!FDecl->getIdentifier())
864 return 0;
865
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000866 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000867}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000868
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000869QualType CallExpr::getCallReturnType() const {
870 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000871 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000872 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000873 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000874 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000875 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
876 // This should never be overloaded and so should never return null.
877 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000878
John McCall864c0412011-04-26 20:42:42 +0000879 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000880 return FnType->getResultType();
881}
Chris Lattnercb888962008-10-06 05:00:53 +0000882
John McCall2882eca2011-02-21 06:23:05 +0000883SourceRange CallExpr::getSourceRange() const {
884 if (isa<CXXOperatorCallExpr>(this))
885 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
886
887 SourceLocation begin = getCallee()->getLocStart();
888 if (begin.isInvalid() && getNumArgs() > 0)
889 begin = getArg(0)->getLocStart();
890 SourceLocation end = getRParenLoc();
891 if (end.isInvalid() && getNumArgs() > 0)
892 end = getArg(getNumArgs() - 1)->getLocEnd();
893 return SourceRange(begin, end);
894}
895
Sean Huntc3021132010-05-05 15:23:54 +0000896OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000897 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000898 TypeSourceInfo *tsi,
899 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000900 Expr** exprsPtr, unsigned numExprs,
901 SourceLocation RParenLoc) {
902 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000903 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000904 sizeof(Expr*) * numExprs);
905
906 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
907 exprsPtr, numExprs, RParenLoc);
908}
909
910OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
911 unsigned numComps, unsigned numExprs) {
912 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
913 sizeof(OffsetOfNode) * numComps +
914 sizeof(Expr*) * numExprs);
915 return new (Mem) OffsetOfExpr(numComps, numExprs);
916}
917
Sean Huntc3021132010-05-05 15:23:54 +0000918OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000919 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000920 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000921 Expr** exprsPtr, unsigned numExprs,
922 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000923 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
924 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000925 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000926 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000927 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000928 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
929 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000930{
931 for(unsigned i = 0; i < numComps; ++i) {
932 setComponent(i, compsPtr[i]);
933 }
Sean Huntc3021132010-05-05 15:23:54 +0000934
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000935 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000936 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
937 ExprBits.ValueDependent = true;
938 if (exprsPtr[i]->containsUnexpandedParameterPack())
939 ExprBits.ContainsUnexpandedParameterPack = true;
940
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000941 setIndexExpr(i, exprsPtr[i]);
942 }
943}
944
945IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
946 assert(getKind() == Field || getKind() == Identifier);
947 if (getKind() == Field)
948 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000949
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000950 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
951}
952
Mike Stump1eb44332009-09-09 15:08:12 +0000953MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000954 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000955 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000956 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000957 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000958 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000959 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000960 QualType ty,
961 ExprValueKind vk,
962 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000963 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000964
Douglas Gregor40d96a62011-02-28 21:54:11 +0000965 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000966 founddecl.getDecl() != memberdecl ||
967 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000968 if (hasQualOrFound)
969 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000970
John McCalld5532b62009-11-23 01:53:49 +0000971 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000972 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
973 else if (TemplateKWLoc.isValid())
974 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Chris Lattner32488542010-10-30 05:14:06 +0000976 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000977 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
978 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000979
980 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000981 // FIXME: Wrong. We should be looking at the member declaration we found.
982 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000983 E->setValueDependent(true);
984 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +0000985 E->setInstantiationDependent(true);
986 }
987 else if (QualifierLoc &&
988 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
989 E->setInstantiationDependent(true);
990
John McCall6bb80172010-03-30 21:47:33 +0000991 E->HasQualifierOrFoundDecl = true;
992
993 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000994 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000995 NQ->FoundDecl = founddecl;
996 }
997
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000998 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
999
John McCall6bb80172010-03-30 21:47:33 +00001000 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001001 bool Dependent = false;
1002 bool InstantiationDependent = false;
1003 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001004 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1005 Dependent,
1006 InstantiationDependent,
1007 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001008 if (InstantiationDependent)
1009 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001010 } else if (TemplateKWLoc.isValid()) {
1011 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001012 }
1013
1014 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001015}
1016
Douglas Gregor75e85042011-03-02 21:06:53 +00001017SourceRange MemberExpr::getSourceRange() const {
1018 SourceLocation StartLoc;
1019 if (isImplicitAccess()) {
1020 if (hasQualifier())
1021 StartLoc = getQualifierLoc().getBeginLoc();
1022 else
1023 StartLoc = MemberLoc;
1024 } else {
1025 // FIXME: We don't want this to happen. Rather, we should be able to
1026 // detect all kinds of implicit accesses more cleanly.
1027 StartLoc = getBase()->getLocStart();
1028 if (StartLoc.isInvalid())
1029 StartLoc = MemberLoc;
1030 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001031
1032 SourceLocation EndLoc = hasExplicitTemplateArgs()
1033 ? getRAngleLoc() : getMemberNameInfo().getEndLoc();
1034
Douglas Gregor75e85042011-03-02 21:06:53 +00001035 return SourceRange(StartLoc, EndLoc);
1036}
1037
John McCall1d9b3b22011-09-09 05:25:32 +00001038void CastExpr::CheckCastConsistency() const {
1039 switch (getCastKind()) {
1040 case CK_DerivedToBase:
1041 case CK_UncheckedDerivedToBase:
1042 case CK_DerivedToBaseMemberPointer:
1043 case CK_BaseToDerived:
1044 case CK_BaseToDerivedMemberPointer:
1045 assert(!path_empty() && "Cast kind should have a base path!");
1046 break;
1047
1048 case CK_CPointerToObjCPointerCast:
1049 assert(getType()->isObjCObjectPointerType());
1050 assert(getSubExpr()->getType()->isPointerType());
1051 goto CheckNoBasePath;
1052
1053 case CK_BlockPointerToObjCPointerCast:
1054 assert(getType()->isObjCObjectPointerType());
1055 assert(getSubExpr()->getType()->isBlockPointerType());
1056 goto CheckNoBasePath;
1057
John McCall4d4e5c12012-02-15 01:22:51 +00001058 case CK_ReinterpretMemberPointer:
1059 assert(getType()->isMemberPointerType());
1060 assert(getSubExpr()->getType()->isMemberPointerType());
1061 goto CheckNoBasePath;
1062
John McCall1d9b3b22011-09-09 05:25:32 +00001063 case CK_BitCast:
1064 // Arbitrary casts to C pointer types count as bitcasts.
1065 // Otherwise, we should only have block and ObjC pointer casts
1066 // here if they stay within the type kind.
1067 if (!getType()->isPointerType()) {
1068 assert(getType()->isObjCObjectPointerType() ==
1069 getSubExpr()->getType()->isObjCObjectPointerType());
1070 assert(getType()->isBlockPointerType() ==
1071 getSubExpr()->getType()->isBlockPointerType());
1072 }
1073 goto CheckNoBasePath;
1074
1075 case CK_AnyPointerToBlockPointerCast:
1076 assert(getType()->isBlockPointerType());
1077 assert(getSubExpr()->getType()->isAnyPointerType() &&
1078 !getSubExpr()->getType()->isBlockPointerType());
1079 goto CheckNoBasePath;
1080
Douglas Gregorac1303e2012-02-22 05:02:47 +00001081 case CK_CopyAndAutoreleaseBlockObject:
1082 assert(getType()->isBlockPointerType());
1083 assert(getSubExpr()->getType()->isBlockPointerType());
1084 goto CheckNoBasePath;
1085
John McCall1d9b3b22011-09-09 05:25:32 +00001086 // These should not have an inheritance path.
1087 case CK_Dynamic:
1088 case CK_ToUnion:
1089 case CK_ArrayToPointerDecay:
1090 case CK_FunctionToPointerDecay:
1091 case CK_NullToMemberPointer:
1092 case CK_NullToPointer:
1093 case CK_ConstructorConversion:
1094 case CK_IntegralToPointer:
1095 case CK_PointerToIntegral:
1096 case CK_ToVoid:
1097 case CK_VectorSplat:
1098 case CK_IntegralCast:
1099 case CK_IntegralToFloating:
1100 case CK_FloatingToIntegral:
1101 case CK_FloatingCast:
1102 case CK_ObjCObjectLValueCast:
1103 case CK_FloatingRealToComplex:
1104 case CK_FloatingComplexToReal:
1105 case CK_FloatingComplexCast:
1106 case CK_FloatingComplexToIntegralComplex:
1107 case CK_IntegralRealToComplex:
1108 case CK_IntegralComplexToReal:
1109 case CK_IntegralComplexCast:
1110 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001111 case CK_ARCProduceObject:
1112 case CK_ARCConsumeObject:
1113 case CK_ARCReclaimReturnedObject:
1114 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001115 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1116 goto CheckNoBasePath;
1117
1118 case CK_Dependent:
1119 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001120 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001121 case CK_AtomicToNonAtomic:
1122 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001123 case CK_PointerToBoolean:
1124 case CK_IntegralToBoolean:
1125 case CK_FloatingToBoolean:
1126 case CK_MemberPointerToBoolean:
1127 case CK_FloatingComplexToBoolean:
1128 case CK_IntegralComplexToBoolean:
1129 case CK_LValueBitCast: // -> bool&
1130 case CK_UserDefinedConversion: // operator bool()
1131 CheckNoBasePath:
1132 assert(path_empty() && "Cast kind should not have a base path!");
1133 break;
1134 }
1135}
1136
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001137const char *CastExpr::getCastKindName() const {
1138 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001139 case CK_Dependent:
1140 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001141 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001142 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001143 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001144 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001145 case CK_LValueToRValue:
1146 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001147 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001148 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001149 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001150 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001151 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001152 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001153 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001154 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001155 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001156 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001157 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001158 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001159 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001160 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001161 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001162 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001163 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001164 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001165 case CK_NullToPointer:
1166 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001167 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001168 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001169 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001170 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001171 case CK_ReinterpretMemberPointer:
1172 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001173 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001174 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001175 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001176 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001177 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001178 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001179 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001180 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001181 case CK_PointerToBoolean:
1182 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001183 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001184 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001185 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001186 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001187 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001188 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001189 case CK_IntegralToBoolean:
1190 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001191 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001192 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001193 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001194 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001195 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001196 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001197 case CK_FloatingToBoolean:
1198 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001199 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001200 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001201 case CK_CPointerToObjCPointerCast:
1202 return "CPointerToObjCPointerCast";
1203 case CK_BlockPointerToObjCPointerCast:
1204 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001205 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001206 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001207 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001208 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001209 case CK_FloatingRealToComplex:
1210 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001211 case CK_FloatingComplexToReal:
1212 return "FloatingComplexToReal";
1213 case CK_FloatingComplexToBoolean:
1214 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001215 case CK_FloatingComplexCast:
1216 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001217 case CK_FloatingComplexToIntegralComplex:
1218 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001219 case CK_IntegralRealToComplex:
1220 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001221 case CK_IntegralComplexToReal:
1222 return "IntegralComplexToReal";
1223 case CK_IntegralComplexToBoolean:
1224 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001225 case CK_IntegralComplexCast:
1226 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001227 case CK_IntegralComplexToFloatingComplex:
1228 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001229 case CK_ARCConsumeObject:
1230 return "ARCConsumeObject";
1231 case CK_ARCProduceObject:
1232 return "ARCProduceObject";
1233 case CK_ARCReclaimReturnedObject:
1234 return "ARCReclaimReturnedObject";
1235 case CK_ARCExtendBlockObject:
1236 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001237 case CK_AtomicToNonAtomic:
1238 return "AtomicToNonAtomic";
1239 case CK_NonAtomicToAtomic:
1240 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001241 case CK_CopyAndAutoreleaseBlockObject:
1242 return "CopyAndAutoreleaseBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001243 }
Mike Stump1eb44332009-09-09 15:08:12 +00001244
John McCall2bb5d002010-11-13 09:02:35 +00001245 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001246}
1247
Douglas Gregor6eef5192009-12-14 19:27:10 +00001248Expr *CastExpr::getSubExprAsWritten() {
1249 Expr *SubExpr = 0;
1250 CastExpr *E = this;
1251 do {
1252 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001253
1254 // Skip through reference binding to temporary.
1255 if (MaterializeTemporaryExpr *Materialize
1256 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1257 SubExpr = Materialize->GetTemporaryExpr();
1258
Douglas Gregor6eef5192009-12-14 19:27:10 +00001259 // Skip any temporary bindings; they're implicit.
1260 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1261 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001262
Douglas Gregor6eef5192009-12-14 19:27:10 +00001263 // Conversions by constructor and conversion functions have a
1264 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001265 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001266 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001267 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001268 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001269
Douglas Gregor6eef5192009-12-14 19:27:10 +00001270 // If the subexpression we're left with is an implicit cast, look
1271 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001272 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1273
Douglas Gregor6eef5192009-12-14 19:27:10 +00001274 return SubExpr;
1275}
1276
John McCallf871d0c2010-08-07 06:22:56 +00001277CXXBaseSpecifier **CastExpr::path_buffer() {
1278 switch (getStmtClass()) {
1279#define ABSTRACT_STMT(x)
1280#define CASTEXPR(Type, Base) \
1281 case Stmt::Type##Class: \
1282 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1283#define STMT(Type, Base)
1284#include "clang/AST/StmtNodes.inc"
1285 default:
1286 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001287 }
1288}
1289
1290void CastExpr::setCastPath(const CXXCastPath &Path) {
1291 assert(Path.size() == path_size());
1292 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1293}
1294
1295ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1296 CastKind Kind, Expr *Operand,
1297 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001298 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001299 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1300 void *Buffer =
1301 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1302 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001303 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001304 if (PathSize) E->setCastPath(*BasePath);
1305 return E;
1306}
1307
1308ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1309 unsigned PathSize) {
1310 void *Buffer =
1311 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1312 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1313}
1314
1315
1316CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001317 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001318 const CXXCastPath *BasePath,
1319 TypeSourceInfo *WrittenTy,
1320 SourceLocation L, SourceLocation R) {
1321 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1322 void *Buffer =
1323 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1324 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001325 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001326 if (PathSize) E->setCastPath(*BasePath);
1327 return E;
1328}
1329
1330CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1331 void *Buffer =
1332 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1333 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1334}
1335
Reid Spencer5f016e22007-07-11 17:01:13 +00001336/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1337/// corresponds to, e.g. "<<=".
1338const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1339 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001340 case BO_PtrMemD: return ".*";
1341 case BO_PtrMemI: return "->*";
1342 case BO_Mul: return "*";
1343 case BO_Div: return "/";
1344 case BO_Rem: return "%";
1345 case BO_Add: return "+";
1346 case BO_Sub: return "-";
1347 case BO_Shl: return "<<";
1348 case BO_Shr: return ">>";
1349 case BO_LT: return "<";
1350 case BO_GT: return ">";
1351 case BO_LE: return "<=";
1352 case BO_GE: return ">=";
1353 case BO_EQ: return "==";
1354 case BO_NE: return "!=";
1355 case BO_And: return "&";
1356 case BO_Xor: return "^";
1357 case BO_Or: return "|";
1358 case BO_LAnd: return "&&";
1359 case BO_LOr: return "||";
1360 case BO_Assign: return "=";
1361 case BO_MulAssign: return "*=";
1362 case BO_DivAssign: return "/=";
1363 case BO_RemAssign: return "%=";
1364 case BO_AddAssign: return "+=";
1365 case BO_SubAssign: return "-=";
1366 case BO_ShlAssign: return "<<=";
1367 case BO_ShrAssign: return ">>=";
1368 case BO_AndAssign: return "&=";
1369 case BO_XorAssign: return "^=";
1370 case BO_OrAssign: return "|=";
1371 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001372 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001373
David Blaikie30263482012-01-20 21:50:17 +00001374 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001375}
1376
John McCall2de56d12010-08-25 11:45:40 +00001377BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001378BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1379 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001380 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001381 case OO_Plus: return BO_Add;
1382 case OO_Minus: return BO_Sub;
1383 case OO_Star: return BO_Mul;
1384 case OO_Slash: return BO_Div;
1385 case OO_Percent: return BO_Rem;
1386 case OO_Caret: return BO_Xor;
1387 case OO_Amp: return BO_And;
1388 case OO_Pipe: return BO_Or;
1389 case OO_Equal: return BO_Assign;
1390 case OO_Less: return BO_LT;
1391 case OO_Greater: return BO_GT;
1392 case OO_PlusEqual: return BO_AddAssign;
1393 case OO_MinusEqual: return BO_SubAssign;
1394 case OO_StarEqual: return BO_MulAssign;
1395 case OO_SlashEqual: return BO_DivAssign;
1396 case OO_PercentEqual: return BO_RemAssign;
1397 case OO_CaretEqual: return BO_XorAssign;
1398 case OO_AmpEqual: return BO_AndAssign;
1399 case OO_PipeEqual: return BO_OrAssign;
1400 case OO_LessLess: return BO_Shl;
1401 case OO_GreaterGreater: return BO_Shr;
1402 case OO_LessLessEqual: return BO_ShlAssign;
1403 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1404 case OO_EqualEqual: return BO_EQ;
1405 case OO_ExclaimEqual: return BO_NE;
1406 case OO_LessEqual: return BO_LE;
1407 case OO_GreaterEqual: return BO_GE;
1408 case OO_AmpAmp: return BO_LAnd;
1409 case OO_PipePipe: return BO_LOr;
1410 case OO_Comma: return BO_Comma;
1411 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001412 }
1413}
1414
1415OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1416 static const OverloadedOperatorKind OverOps[] = {
1417 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1418 OO_Star, OO_Slash, OO_Percent,
1419 OO_Plus, OO_Minus,
1420 OO_LessLess, OO_GreaterGreater,
1421 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1422 OO_EqualEqual, OO_ExclaimEqual,
1423 OO_Amp,
1424 OO_Caret,
1425 OO_Pipe,
1426 OO_AmpAmp,
1427 OO_PipePipe,
1428 OO_Equal, OO_StarEqual,
1429 OO_SlashEqual, OO_PercentEqual,
1430 OO_PlusEqual, OO_MinusEqual,
1431 OO_LessLessEqual, OO_GreaterGreaterEqual,
1432 OO_AmpEqual, OO_CaretEqual,
1433 OO_PipeEqual,
1434 OO_Comma
1435 };
1436 return OverOps[Opc];
1437}
1438
Ted Kremenek709210f2010-04-13 23:39:13 +00001439InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001440 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001441 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001442 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001443 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001444 InitExprs(C, numInits),
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001445 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0)
1446{
1447 sawArrayRangeDesignator(false);
1448 setInitializesStdInitializerList(false);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001449 for (unsigned I = 0; I != numInits; ++I) {
1450 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001451 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001452 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001453 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001454 if (initExprs[I]->isInstantiationDependent())
1455 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001456 if (initExprs[I]->containsUnexpandedParameterPack())
1457 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001458 }
Sean Huntc3021132010-05-05 15:23:54 +00001459
Ted Kremenek709210f2010-04-13 23:39:13 +00001460 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001461}
Reid Spencer5f016e22007-07-11 17:01:13 +00001462
Ted Kremenek709210f2010-04-13 23:39:13 +00001463void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001464 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001465 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001466}
1467
Ted Kremenek709210f2010-04-13 23:39:13 +00001468void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001469 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001470}
1471
Ted Kremenek709210f2010-04-13 23:39:13 +00001472Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001473 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001474 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001475 InitExprs.back() = expr;
1476 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001477 }
Mike Stump1eb44332009-09-09 15:08:12 +00001478
Douglas Gregor4c678342009-01-28 21:54:33 +00001479 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1480 InitExprs[Init] = expr;
1481 return Result;
1482}
1483
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001484void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001485 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001486 ArrayFillerOrUnionFieldInit = filler;
1487 // Fill out any "holes" in the array due to designated initializers.
1488 Expr **inits = getInits();
1489 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1490 if (inits[i] == 0)
1491 inits[i] = filler;
1492}
1493
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001494SourceRange InitListExpr::getSourceRange() const {
1495 if (SyntacticForm)
1496 return SyntacticForm->getSourceRange();
1497 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1498 if (Beg.isInvalid()) {
1499 // Find the first non-null initializer.
1500 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1501 E = InitExprs.end();
1502 I != E; ++I) {
1503 if (Stmt *S = *I) {
1504 Beg = S->getLocStart();
1505 break;
1506 }
1507 }
1508 }
1509 if (End.isInvalid()) {
1510 // Find the first non-null initializer from the end.
1511 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1512 E = InitExprs.rend();
1513 I != E; ++I) {
1514 if (Stmt *S = *I) {
1515 End = S->getSourceRange().getEnd();
1516 break;
1517 }
1518 }
1519 }
1520 return SourceRange(Beg, End);
1521}
1522
Steve Naroffbfdcae62008-09-04 15:31:07 +00001523/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001524///
John McCalla345edb2012-02-17 03:32:35 +00001525const FunctionProtoType *BlockExpr::getFunctionType() const {
1526 // The block pointer is never sugared, but the function type might be.
1527 return cast<BlockPointerType>(getType())
1528 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001529}
1530
Mike Stump1eb44332009-09-09 15:08:12 +00001531SourceLocation BlockExpr::getCaretLocation() const {
1532 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001533}
Mike Stump1eb44332009-09-09 15:08:12 +00001534const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001535 return TheBlock->getBody();
1536}
Mike Stump1eb44332009-09-09 15:08:12 +00001537Stmt *BlockExpr::getBody() {
1538 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001539}
Steve Naroff56ee6892008-10-08 17:01:13 +00001540
1541
Reid Spencer5f016e22007-07-11 17:01:13 +00001542//===----------------------------------------------------------------------===//
1543// Generic Expression Routines
1544//===----------------------------------------------------------------------===//
1545
Chris Lattner026dc962009-02-14 07:37:35 +00001546/// isUnusedResultAWarning - Return true if this immediate expression should
1547/// be warned about if the result is unused. If so, fill in Loc and Ranges
1548/// with location to warn on and the source range[s] to report with the
1549/// warning.
1550bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001551 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001552 // Don't warn if the expr is type dependent. The type could end up
1553 // instantiating to void.
1554 if (isTypeDependent())
1555 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Reid Spencer5f016e22007-07-11 17:01:13 +00001557 switch (getStmtClass()) {
1558 default:
John McCall0faede62010-03-12 07:11:26 +00001559 if (getType()->isVoidType())
1560 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001561 Loc = getExprLoc();
1562 R1 = getSourceRange();
1563 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001564 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001565 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001566 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001567 case GenericSelectionExprClass:
1568 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1569 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001570 case UnaryOperatorClass: {
1571 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001574 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001575 case UO_PostInc:
1576 case UO_PostDec:
1577 case UO_PreInc:
1578 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001579 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001580 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001581 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001582 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001583 return false;
1584 break;
John McCall2de56d12010-08-25 11:45:40 +00001585 case UO_Real:
1586 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001587 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001588 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1589 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001590 return false;
1591 break;
John McCall2de56d12010-08-25 11:45:40 +00001592 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001593 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001594 }
Chris Lattner026dc962009-02-14 07:37:35 +00001595 Loc = UO->getOperatorLoc();
1596 R1 = UO->getSubExpr()->getSourceRange();
1597 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001598 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001599 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001600 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001601 switch (BO->getOpcode()) {
1602 default:
1603 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001604 // Consider the RHS of comma for side effects. LHS was checked by
1605 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001606 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001607 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1608 // lvalue-ness) of an assignment written in a macro.
1609 if (IntegerLiteral *IE =
1610 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1611 if (IE->getValue() == 0)
1612 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001613 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1614 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001615 case BO_LAnd:
1616 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001617 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1618 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1619 return false;
1620 break;
John McCallbf0ee352010-02-16 04:10:53 +00001621 }
Chris Lattner026dc962009-02-14 07:37:35 +00001622 if (BO->isAssignmentOp())
1623 return false;
1624 Loc = BO->getOperatorLoc();
1625 R1 = BO->getLHS()->getSourceRange();
1626 R2 = BO->getRHS()->getSourceRange();
1627 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001628 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001629 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001630 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001631 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001632 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001633
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001634 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001635 // If only one of the LHS or RHS is a warning, the operator might
1636 // be being used for control flow. Only warn if both the LHS and
1637 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001638 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001639 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1640 return false;
1641 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001642 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001643 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001644 }
1645
Reid Spencer5f016e22007-07-11 17:01:13 +00001646 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001647 // If the base pointer or element is to a volatile pointer/field, accessing
1648 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001649 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001650 return false;
1651 Loc = cast<MemberExpr>(this)->getMemberLoc();
1652 R1 = SourceRange(Loc, Loc);
1653 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1654 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Reid Spencer5f016e22007-07-11 17:01:13 +00001656 case ArraySubscriptExprClass:
1657 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001658 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001659 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001660 return false;
1661 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1662 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1663 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1664 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001665
Chandler Carruth9b106832011-08-17 09:49:44 +00001666 case CXXOperatorCallExprClass: {
1667 // We warn about operator== and operator!= even when user-defined operator
1668 // overloads as there is no reasonable way to define these such that they
1669 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1670 // warning: these operators are commonly typo'ed, and so warning on them
1671 // provides additional value as well. If this list is updated,
1672 // DiagnoseUnusedComparison should be as well.
1673 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1674 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001675 Op->getOperator() == OO_ExclaimEqual) {
1676 Loc = Op->getOperatorLoc();
1677 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001678 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001679 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001680
1681 // Fallthrough for generic call handling.
1682 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001684 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001685 // If this is a direct call, get the callee.
1686 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001687 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001688 // If the callee has attribute pure, const, or warn_unused_result, warn
1689 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001690 //
1691 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1692 // updated to match for QoI.
1693 if (FD->getAttr<WarnUnusedResultAttr>() ||
1694 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1695 Loc = CE->getCallee()->getLocStart();
1696 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001698 if (unsigned NumArgs = CE->getNumArgs())
1699 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1700 CE->getArg(NumArgs-1)->getLocEnd());
1701 return true;
1702 }
Chris Lattner026dc962009-02-14 07:37:35 +00001703 }
1704 return false;
1705 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001706
1707 case CXXTemporaryObjectExprClass:
1708 case CXXConstructExprClass:
1709 return false;
1710
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001711 case ObjCMessageExprClass: {
1712 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001713 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1714 ME->isInstanceMessage() &&
1715 !ME->getType()->isVoidType() &&
1716 ME->getSelector().getIdentifierInfoForSlot(0) &&
1717 ME->getSelector().getIdentifierInfoForSlot(0)
1718 ->getName().startswith("init")) {
1719 Loc = getExprLoc();
1720 R1 = ME->getSourceRange();
1721 return true;
1722 }
1723
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001724 const ObjCMethodDecl *MD = ME->getMethodDecl();
1725 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1726 Loc = getExprLoc();
1727 return true;
1728 }
Chris Lattner026dc962009-02-14 07:37:35 +00001729 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001730 }
Mike Stump1eb44332009-09-09 15:08:12 +00001731
John McCall12f78a62010-12-02 01:19:52 +00001732 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001733 Loc = getExprLoc();
1734 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001735 return true;
John McCall12f78a62010-12-02 01:19:52 +00001736
John McCall4b9c2d22011-11-06 09:01:30 +00001737 case PseudoObjectExprClass: {
1738 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1739
1740 // Only complain about things that have the form of a getter.
1741 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1742 isa<BinaryOperator>(PO->getSyntacticForm()))
1743 return false;
1744
1745 Loc = getExprLoc();
1746 R1 = getSourceRange();
1747 return true;
1748 }
1749
Chris Lattner611b2ec2008-07-26 19:51:01 +00001750 case StmtExprClass: {
1751 // Statement exprs don't logically have side effects themselves, but are
1752 // sometimes used in macros in ways that give them a type that is unused.
1753 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1754 // however, if the result of the stmt expr is dead, we don't want to emit a
1755 // warning.
1756 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001757 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001758 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001759 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001760 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1761 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1762 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1763 }
Mike Stump1eb44332009-09-09 15:08:12 +00001764
John McCall0faede62010-03-12 07:11:26 +00001765 if (getType()->isVoidType())
1766 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001767 Loc = cast<StmtExpr>(this)->getLParenLoc();
1768 R1 = getSourceRange();
1769 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001770 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001771 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001772 // If this is an explicit cast to void, allow it. People do this when they
1773 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001774 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001775 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001776 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1777 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1778 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001779 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001780 if (getType()->isVoidType())
1781 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001782 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001783
Anders Carlsson58beed92009-11-17 17:11:23 +00001784 // If this is a cast to void or a constructor conversion, check the operand.
1785 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001786 if (CE->getCastKind() == CK_ToVoid ||
1787 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001788 return (cast<CastExpr>(this)->getSubExpr()
1789 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001790 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1791 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1792 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001793 }
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Eli Friedman4be1f472008-05-19 21:24:43 +00001795 case ImplicitCastExprClass:
1796 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001797 return (cast<ImplicitCastExpr>(this)
1798 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001799
Chris Lattner04421082008-04-08 04:40:51 +00001800 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001801 return (cast<CXXDefaultArgExpr>(this)
1802 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001803
1804 case CXXNewExprClass:
1805 // FIXME: In theory, there might be new expressions that don't have side
1806 // effects (e.g. a placement new with an uninitialized POD).
1807 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001808 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001809 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001810 return (cast<CXXBindTemporaryExpr>(this)
1811 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001812 case ExprWithCleanupsClass:
1813 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001814 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001815 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001816}
1817
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001818/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001819/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001820bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001821 const Expr *E = IgnoreParens();
1822 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001823 default:
1824 return false;
1825 case ObjCIvarRefExprClass:
1826 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001827 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001828 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001829 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001830 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001831 case MaterializeTemporaryExprClass:
1832 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1833 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001834 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001835 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001836 case BlockDeclRefExprClass:
Douglas Gregora2813ce2009-10-23 18:54:35 +00001837 case DeclRefExprClass: {
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001838
1839 const Decl *D;
1840 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1841 D = BDRE->getDecl();
1842 else
1843 D = cast<DeclRefExpr>(E)->getDecl();
1844
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001845 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1846 if (VD->hasGlobalStorage())
1847 return true;
1848 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001849 // dereferencing to a pointer is always a gc'able candidate,
1850 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001851 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001852 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001853 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001854 return false;
1855 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001856 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001857 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001858 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001859 }
1860 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001861 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001862 }
1863}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001864
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001865bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1866 if (isTypeDependent())
1867 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001868 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001869}
1870
John McCall864c0412011-04-26 20:42:42 +00001871QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00001872 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00001873
1874 // Bound member expressions are always one of these possibilities:
1875 // x->m x.m x->*y x.*y
1876 // (possibly parenthesized)
1877
1878 expr = expr->IgnoreParens();
1879 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1880 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1881 return mem->getMemberDecl()->getType();
1882 }
1883
1884 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1885 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1886 ->getPointeeType();
1887 assert(type->isFunctionType());
1888 return type;
1889 }
1890
1891 assert(isa<UnresolvedMemberExpr>(expr));
1892 return QualType();
1893}
1894
Sebastian Redl369e51f2010-09-10 20:55:33 +00001895static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1896 Expr::CanThrowResult CT2) {
1897 // CanThrowResult constants are ordered so that the maximum is the correct
1898 // merge result.
1899 return CT1 > CT2 ? CT1 : CT2;
1900}
1901
1902static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1903 Expr *E = const_cast<Expr*>(CE);
1904 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001905 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001906 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1907 }
1908 return R;
1909}
1910
Richard Smith7a614d82011-06-11 17:19:42 +00001911static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1912 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001913 bool NullThrows = true) {
1914 if (!D)
1915 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1916
1917 // See if we can get a function type from the decl somehow.
1918 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1919 if (!VD) // If we have no clue what we're calling, assume the worst.
1920 return Expr::CT_Can;
1921
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001922 // As an extension, we assume that __attribute__((nothrow)) functions don't
1923 // throw.
1924 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1925 return Expr::CT_Cannot;
1926
Sebastian Redl369e51f2010-09-10 20:55:33 +00001927 QualType T = VD->getType();
1928 const FunctionProtoType *FT;
1929 if ((FT = T->getAs<FunctionProtoType>())) {
1930 } else if (const PointerType *PT = T->getAs<PointerType>())
1931 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1932 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1933 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1934 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1935 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1936 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1937 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1938
1939 if (!FT)
1940 return Expr::CT_Can;
1941
Richard Smith7a614d82011-06-11 17:19:42 +00001942 if (FT->getExceptionSpecType() == EST_Delayed) {
1943 assert(isa<CXXConstructorDecl>(D) &&
1944 "only constructor exception specs can be unknown");
1945 Ctx.getDiagnostics().Report(E->getLocStart(),
1946 diag::err_exception_spec_unknown)
1947 << E->getSourceRange();
1948 return Expr::CT_Can;
1949 }
1950
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001951 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001952}
1953
1954static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1955 if (DC->isTypeDependent())
1956 return Expr::CT_Dependent;
1957
Sebastian Redl295995c2010-09-10 20:55:47 +00001958 if (!DC->getTypeAsWritten()->isReferenceType())
1959 return Expr::CT_Cannot;
1960
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001961 if (DC->getSubExpr()->isTypeDependent())
1962 return Expr::CT_Dependent;
1963
Sebastian Redl369e51f2010-09-10 20:55:33 +00001964 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1965}
1966
1967static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1968 const CXXTypeidExpr *DC) {
1969 if (DC->isTypeOperand())
1970 return Expr::CT_Cannot;
1971
1972 Expr *Op = DC->getExprOperand();
1973 if (Op->isTypeDependent())
1974 return Expr::CT_Dependent;
1975
1976 const RecordType *RT = Op->getType()->getAs<RecordType>();
1977 if (!RT)
1978 return Expr::CT_Cannot;
1979
1980 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1981 return Expr::CT_Cannot;
1982
1983 if (Op->Classify(C).isPRValue())
1984 return Expr::CT_Cannot;
1985
1986 return Expr::CT_Can;
1987}
1988
1989Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1990 // C++ [expr.unary.noexcept]p3:
1991 // [Can throw] if in a potentially-evaluated context the expression would
1992 // contain:
1993 switch (getStmtClass()) {
1994 case CXXThrowExprClass:
1995 // - a potentially evaluated throw-expression
1996 return CT_Can;
1997
1998 case CXXDynamicCastExprClass: {
1999 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
2000 // where T is a reference type, that requires a run-time check
2001 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
2002 if (CT == CT_Can)
2003 return CT;
2004 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2005 }
2006
2007 case CXXTypeidExprClass:
2008 // - a potentially evaluated typeid expression applied to a glvalue
2009 // expression whose type is a polymorphic class type
2010 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
2011
2012 // - a potentially evaluated call to a function, member function, function
2013 // pointer, or member function pointer that does not have a non-throwing
2014 // exception-specification
2015 case CallExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002016 case CXXMemberCallExprClass:
2017 case CXXOperatorCallExprClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00002018 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002019 CanThrowResult CT;
2020 if (isTypeDependent())
2021 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00002022 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
2023 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002024 else
Richard Smith7a614d82011-06-11 17:19:42 +00002025 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00002026 if (CT == CT_Can)
2027 return CT;
2028 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2029 }
2030
Sebastian Redl295995c2010-09-10 20:55:47 +00002031 case CXXConstructExprClass:
2032 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00002033 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00002034 cast<CXXConstructExpr>(this)->getConstructor());
2035 if (CT == CT_Can)
2036 return CT;
2037 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2038 }
2039
Douglas Gregor01d08012012-02-07 10:09:13 +00002040 case LambdaExprClass: {
2041 const LambdaExpr *Lambda = cast<LambdaExpr>(this);
2042 CanThrowResult CT = Expr::CT_Cannot;
2043 for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(),
2044 CapEnd = Lambda->capture_init_end();
2045 Cap != CapEnd; ++Cap)
2046 CT = MergeCanThrow(CT, (*Cap)->CanThrow(C));
2047 return CT;
2048 }
2049
Sebastian Redl369e51f2010-09-10 20:55:33 +00002050 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002051 CanThrowResult CT;
2052 if (isTypeDependent())
2053 CT = CT_Dependent;
2054 else
Sebastian Redl2aed8b82012-02-16 12:22:20 +00002055 CT = CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew());
Sebastian Redl369e51f2010-09-10 20:55:33 +00002056 if (CT == CT_Can)
2057 return CT;
2058 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2059 }
2060
2061 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002062 CanThrowResult CT;
2063 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
2064 if (DTy.isNull() || DTy->isDependentType()) {
2065 CT = CT_Dependent;
2066 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00002067 CT = CanCalleeThrow(C, this,
2068 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002069 if (const RecordType *RT = DTy->getAs<RecordType>()) {
2070 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00002071 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002072 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002073 if (CT == CT_Can)
2074 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002075 }
2076 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2077 }
2078
2079 case CXXBindTemporaryExprClass: {
2080 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00002081 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002082 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
2083 if (CT == CT_Can)
2084 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00002085 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2086 }
2087
2088 // ObjC message sends are like function calls, but never have exception
2089 // specs.
2090 case ObjCMessageExprClass:
2091 case ObjCPropertyRefExprClass:
Ted Kremenekebcb57a2012-03-06 20:05:56 +00002092 case ObjCSubscriptRefExprClass:
2093 return CT_Can;
2094
2095 // All the ObjC literals that are implemented as calls are
2096 // potentially throwing unless we decide to close off that
2097 // possibility.
2098 case ObjCArrayLiteralClass:
2099 case ObjCBoolLiteralExprClass:
2100 case ObjCDictionaryLiteralClass:
2101 case ObjCNumericLiteralClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002102 return CT_Can;
2103
2104 // Many other things have subexpressions, so we have to test those.
2105 // Some are simple:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002106 case ConditionalOperatorClass:
2107 case CompoundLiteralExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002108 case CXXConstCastExprClass:
2109 case CXXDefaultArgExprClass:
2110 case CXXReinterpretCastExprClass:
2111 case DesignatedInitExprClass:
2112 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002113 case ExtVectorElementExprClass:
2114 case InitListExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002115 case MemberExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002116 case ObjCIsaExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002117 case ObjCIvarRefExprClass:
2118 case ParenExprClass:
2119 case ParenListExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002120 case ShuffleVectorExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002121 case VAArgExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002122 return CanSubExprsThrow(C, this);
2123
2124 // Some might be dependent for other reasons.
Sebastian Redl369e51f2010-09-10 20:55:33 +00002125 case ArraySubscriptExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002126 case BinaryOperatorClass:
2127 case CompoundAssignOperatorClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002128 case CStyleCastExprClass:
2129 case CXXStaticCastExprClass:
2130 case CXXFunctionalCastExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002131 case ImplicitCastExprClass:
2132 case MaterializeTemporaryExprClass:
2133 case UnaryOperatorClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002134 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2135 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2136 }
2137
2138 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2139 case StmtExprClass:
2140 return CT_Can;
2141
2142 case ChooseExprClass:
2143 if (isTypeDependent() || isValueDependent())
2144 return CT_Dependent;
2145 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2146
Peter Collingbournef111d932011-04-15 00:35:48 +00002147 case GenericSelectionExprClass:
2148 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2149 return CT_Dependent;
2150 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2151
Sebastian Redl369e51f2010-09-10 20:55:33 +00002152 // Some expressions are always dependent.
Sebastian Redl369e51f2010-09-10 20:55:33 +00002153 case CXXDependentScopeMemberExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002154 case CXXUnresolvedConstructExprClass:
2155 case DependentScopeDeclRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002156 return CT_Dependent;
2157
Eli Friedmanc9674be2012-01-31 01:21:45 +00002158 case AtomicExprClass:
2159 case AsTypeExprClass:
2160 case BinaryConditionalOperatorClass:
2161 case BlockExprClass:
2162 case BlockDeclRefExprClass:
2163 case CUDAKernelCallExprClass:
2164 case DeclRefExprClass:
2165 case ObjCBridgedCastExprClass:
2166 case ObjCIndirectCopyRestoreExprClass:
2167 case ObjCProtocolExprClass:
2168 case ObjCSelectorExprClass:
2169 case OffsetOfExprClass:
2170 case PackExpansionExprClass:
2171 case PseudoObjectExprClass:
2172 case SubstNonTypeTemplateParmExprClass:
2173 case SubstNonTypeTemplateParmPackExprClass:
2174 case UnaryExprOrTypeTraitExprClass:
2175 case UnresolvedLookupExprClass:
2176 case UnresolvedMemberExprClass:
2177 // FIXME: Can any of the above throw? If so, when?
Sebastian Redl369e51f2010-09-10 20:55:33 +00002178 return CT_Cannot;
Eli Friedmanc9674be2012-01-31 01:21:45 +00002179
2180 case AddrLabelExprClass:
2181 case ArrayTypeTraitExprClass:
2182 case BinaryTypeTraitExprClass:
Douglas Gregor4ca8ac22012-02-24 07:38:34 +00002183 case TypeTraitExprClass:
Eli Friedmanc9674be2012-01-31 01:21:45 +00002184 case CXXBoolLiteralExprClass:
2185 case CXXNoexceptExprClass:
2186 case CXXNullPtrLiteralExprClass:
2187 case CXXPseudoDestructorExprClass:
2188 case CXXScalarValueInitExprClass:
2189 case CXXThisExprClass:
2190 case CXXUuidofExprClass:
2191 case CharacterLiteralClass:
2192 case ExpressionTraitExprClass:
2193 case FloatingLiteralClass:
2194 case GNUNullExprClass:
2195 case ImaginaryLiteralClass:
2196 case ImplicitValueInitExprClass:
2197 case IntegerLiteralClass:
2198 case ObjCEncodeExprClass:
2199 case ObjCStringLiteralClass:
2200 case OpaqueValueExprClass:
2201 case PredefinedExprClass:
2202 case SizeOfPackExprClass:
2203 case StringLiteralClass:
2204 case UnaryTypeTraitExprClass:
2205 // These expressions can never throw.
2206 return CT_Cannot;
2207
2208#define STMT(CLASS, PARENT) case CLASS##Class:
2209#define STMT_RANGE(Base, First, Last)
2210#define LAST_STMT_RANGE(BASE, FIRST, LAST)
2211#define EXPR(CLASS, PARENT)
2212#define ABSTRACT_STMT(STMT)
2213#include "clang/AST/StmtNodes.inc"
2214 case NoStmtClass:
2215 llvm_unreachable("Invalid class for expression");
Sebastian Redl369e51f2010-09-10 20:55:33 +00002216 }
Matt Beaumont-Gay56e68b72012-01-31 18:59:25 +00002217 llvm_unreachable("Bogus StmtClass");
Sebastian Redl369e51f2010-09-10 20:55:33 +00002218}
2219
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002220Expr* Expr::IgnoreParens() {
2221 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002222 while (true) {
2223 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2224 E = P->getSubExpr();
2225 continue;
2226 }
2227 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2228 if (P->getOpcode() == UO_Extension) {
2229 E = P->getSubExpr();
2230 continue;
2231 }
2232 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002233 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2234 if (!P->isResultDependent()) {
2235 E = P->getResultExpr();
2236 continue;
2237 }
2238 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002239 return E;
2240 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002241}
2242
Chris Lattner56f34942008-02-13 01:02:39 +00002243/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2244/// or CastExprs or ImplicitCastExprs, returning their operand.
2245Expr *Expr::IgnoreParenCasts() {
2246 Expr *E = this;
2247 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002248 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002249 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002250 continue;
2251 }
2252 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002253 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002254 continue;
2255 }
2256 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2257 if (P->getOpcode() == UO_Extension) {
2258 E = P->getSubExpr();
2259 continue;
2260 }
2261 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002262 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2263 if (!P->isResultDependent()) {
2264 E = P->getResultExpr();
2265 continue;
2266 }
2267 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002268 if (MaterializeTemporaryExpr *Materialize
2269 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2270 E = Materialize->GetTemporaryExpr();
2271 continue;
2272 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002273 if (SubstNonTypeTemplateParmExpr *NTTP
2274 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2275 E = NTTP->getReplacement();
2276 continue;
2277 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002278 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002279 }
2280}
2281
John McCall9c5d70c2010-12-04 08:24:19 +00002282/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2283/// casts. This is intended purely as a temporary workaround for code
2284/// that hasn't yet been rewritten to do the right thing about those
2285/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002286Expr *Expr::IgnoreParenLValueCasts() {
2287 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002288 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002289 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2290 E = P->getSubExpr();
2291 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002292 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002293 if (P->getCastKind() == CK_LValueToRValue) {
2294 E = P->getSubExpr();
2295 continue;
2296 }
John McCall9c5d70c2010-12-04 08:24:19 +00002297 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2298 if (P->getOpcode() == UO_Extension) {
2299 E = P->getSubExpr();
2300 continue;
2301 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002302 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2303 if (!P->isResultDependent()) {
2304 E = P->getResultExpr();
2305 continue;
2306 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002307 } else if (MaterializeTemporaryExpr *Materialize
2308 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2309 E = Materialize->GetTemporaryExpr();
2310 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002311 } else if (SubstNonTypeTemplateParmExpr *NTTP
2312 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2313 E = NTTP->getReplacement();
2314 continue;
John McCallf6a16482010-12-04 03:47:34 +00002315 }
2316 break;
2317 }
2318 return E;
2319}
2320
John McCall2fc46bf2010-05-05 22:59:52 +00002321Expr *Expr::IgnoreParenImpCasts() {
2322 Expr *E = this;
2323 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002324 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002325 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002326 continue;
2327 }
2328 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002329 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002330 continue;
2331 }
2332 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2333 if (P->getOpcode() == UO_Extension) {
2334 E = P->getSubExpr();
2335 continue;
2336 }
2337 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002338 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2339 if (!P->isResultDependent()) {
2340 E = P->getResultExpr();
2341 continue;
2342 }
2343 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002344 if (MaterializeTemporaryExpr *Materialize
2345 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2346 E = Materialize->GetTemporaryExpr();
2347 continue;
2348 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002349 if (SubstNonTypeTemplateParmExpr *NTTP
2350 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2351 E = NTTP->getReplacement();
2352 continue;
2353 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002354 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002355 }
2356}
2357
Hans Wennborg2f072b42011-06-09 17:06:51 +00002358Expr *Expr::IgnoreConversionOperator() {
2359 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002360 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002361 return MCE->getImplicitObjectArgument();
2362 }
2363 return this;
2364}
2365
Chris Lattnerecdd8412009-03-13 17:28:01 +00002366/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2367/// value (including ptr->int casts of the same size). Strip off any
2368/// ParenExpr or CastExprs, returning their operand.
2369Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2370 Expr *E = this;
2371 while (true) {
2372 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2373 E = P->getSubExpr();
2374 continue;
2375 }
Mike Stump1eb44332009-09-09 15:08:12 +00002376
Chris Lattnerecdd8412009-03-13 17:28:01 +00002377 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2378 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002379 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002380 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002381
Chris Lattnerecdd8412009-03-13 17:28:01 +00002382 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2383 E = SE;
2384 continue;
2385 }
Mike Stump1eb44332009-09-09 15:08:12 +00002386
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002387 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002388 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002389 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002390 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002391 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2392 E = SE;
2393 continue;
2394 }
2395 }
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002397 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2398 if (P->getOpcode() == UO_Extension) {
2399 E = P->getSubExpr();
2400 continue;
2401 }
2402 }
2403
Peter Collingbournef111d932011-04-15 00:35:48 +00002404 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2405 if (!P->isResultDependent()) {
2406 E = P->getResultExpr();
2407 continue;
2408 }
2409 }
2410
Douglas Gregorc0244c52011-09-08 17:56:33 +00002411 if (SubstNonTypeTemplateParmExpr *NTTP
2412 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2413 E = NTTP->getReplacement();
2414 continue;
2415 }
2416
Chris Lattnerecdd8412009-03-13 17:28:01 +00002417 return E;
2418 }
2419}
2420
Douglas Gregor6eef5192009-12-14 19:27:10 +00002421bool Expr::isDefaultArgument() const {
2422 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002423 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2424 E = M->GetTemporaryExpr();
2425
Douglas Gregor6eef5192009-12-14 19:27:10 +00002426 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2427 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002428
Douglas Gregor6eef5192009-12-14 19:27:10 +00002429 return isa<CXXDefaultArgExpr>(E);
2430}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002431
Douglas Gregor2f599792010-04-02 18:24:57 +00002432/// \brief Skip over any no-op casts and any temporary-binding
2433/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002434static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002435 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2436 E = M->GetTemporaryExpr();
2437
Douglas Gregor2f599792010-04-02 18:24:57 +00002438 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002439 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002440 E = ICE->getSubExpr();
2441 else
2442 break;
2443 }
2444
2445 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2446 E = BE->getSubExpr();
2447
2448 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002449 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002450 E = ICE->getSubExpr();
2451 else
2452 break;
2453 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002454
2455 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002456}
2457
John McCall558d2ab2010-09-15 10:14:12 +00002458/// isTemporaryObject - Determines if this expression produces a
2459/// temporary of the given class type.
2460bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2461 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2462 return false;
2463
Anders Carlssonf8b30152010-11-28 16:40:49 +00002464 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002465
John McCall58277b52010-09-15 20:59:13 +00002466 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002467 if (!E->Classify(C).isPRValue()) {
2468 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002469 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002470 return false;
2471 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002472
John McCall19e60ad2010-09-16 06:57:56 +00002473 // Black-list a few cases which yield pr-values of class type that don't
2474 // refer to temporaries of that type:
2475
2476 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002477 if (isa<ImplicitCastExpr>(E)) {
2478 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2479 case CK_DerivedToBase:
2480 case CK_UncheckedDerivedToBase:
2481 return false;
2482 default:
2483 break;
2484 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002485 }
2486
John McCall19e60ad2010-09-16 06:57:56 +00002487 // - member expressions (all)
2488 if (isa<MemberExpr>(E))
2489 return false;
2490
John McCall56ca35d2011-02-17 10:25:35 +00002491 // - opaque values (all)
2492 if (isa<OpaqueValueExpr>(E))
2493 return false;
2494
John McCall558d2ab2010-09-15 10:14:12 +00002495 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002496}
2497
Douglas Gregor75e85042011-03-02 21:06:53 +00002498bool Expr::isImplicitCXXThis() const {
2499 const Expr *E = this;
2500
2501 // Strip away parentheses and casts we don't care about.
2502 while (true) {
2503 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2504 E = Paren->getSubExpr();
2505 continue;
2506 }
2507
2508 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2509 if (ICE->getCastKind() == CK_NoOp ||
2510 ICE->getCastKind() == CK_LValueToRValue ||
2511 ICE->getCastKind() == CK_DerivedToBase ||
2512 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2513 E = ICE->getSubExpr();
2514 continue;
2515 }
2516 }
2517
2518 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2519 if (UnOp->getOpcode() == UO_Extension) {
2520 E = UnOp->getSubExpr();
2521 continue;
2522 }
2523 }
2524
Douglas Gregor03e80032011-06-21 17:03:29 +00002525 if (const MaterializeTemporaryExpr *M
2526 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2527 E = M->GetTemporaryExpr();
2528 continue;
2529 }
2530
Douglas Gregor75e85042011-03-02 21:06:53 +00002531 break;
2532 }
2533
2534 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2535 return This->isImplicit();
2536
2537 return false;
2538}
2539
Douglas Gregor898574e2008-12-05 23:32:09 +00002540/// hasAnyTypeDependentArguments - Determines if any of the expressions
2541/// in Exprs is type-dependent.
Ahmed Charles13a140c2012-02-25 11:00:22 +00002542bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2543 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002544 if (Exprs[I]->isTypeDependent())
2545 return true;
2546
2547 return false;
2548}
2549
John McCall4204f072010-08-02 21:13:48 +00002550bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002551 // This function is attempting whether an expression is an initializer
2552 // which can be evaluated at compile-time. isEvaluatable handles most
2553 // of the cases, but it can't deal with some initializer-specific
2554 // expressions, and it can't deal with aggregates; we deal with those here,
2555 // and fall back to isEvaluatable for the other cases.
2556
John McCall4204f072010-08-02 21:13:48 +00002557 // If we ever capture reference-binding directly in the AST, we can
2558 // kill the second parameter.
2559
2560 if (IsForRef) {
2561 EvalResult Result;
2562 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2563 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002564
Anders Carlssone8a32b82008-11-24 05:23:59 +00002565 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002566 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002567 case IntegerLiteralClass:
2568 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002569 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002570 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002571 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002572 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002573 case CXXTemporaryObjectExprClass:
2574 case CXXConstructExprClass: {
2575 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002576
2577 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002578 if (CE->getConstructor()->isTrivial()) {
2579 // 1) an application of the trivial default constructor or
2580 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002581
Richard Smith180f4792011-11-10 06:34:14 +00002582 // 2) an elidable trivial copy construction of an operand which is
2583 // itself a constant initializer. Note that we consider the
2584 // operand on its own, *not* as a reference binding.
2585 if (CE->isElidable() &&
2586 CE->getArg(0)->isConstantInitializer(Ctx, false))
2587 return true;
2588 }
2589
2590 // 3) a foldable constexpr constructor.
2591 break;
John McCallb4b9b152010-08-01 21:51:45 +00002592 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002593 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002594 // This handles gcc's extension that allows global initializers like
2595 // "struct x {int x;} x = (struct x) {};".
2596 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002597 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002598 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002599 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002600 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002601 // FIXME: This doesn't deal with fields with reference types correctly.
2602 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2603 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002604 const InitListExpr *Exp = cast<InitListExpr>(this);
2605 unsigned numInits = Exp->getNumInits();
2606 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002607 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002608 return false;
2609 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002610 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002611 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002612 case ImplicitValueInitExprClass:
2613 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002614 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002615 return cast<ParenExpr>(this)->getSubExpr()
2616 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002617 case GenericSelectionExprClass:
2618 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2619 return false;
2620 return cast<GenericSelectionExpr>(this)->getResultExpr()
2621 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002622 case ChooseExprClass:
2623 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2624 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002625 case UnaryOperatorClass: {
2626 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002627 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002628 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002629 break;
2630 }
John McCall4204f072010-08-02 21:13:48 +00002631 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002632 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002633 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002634 case CStyleCastExprClass: {
2635 const CastExpr *CE = cast<CastExpr>(this);
2636
David Chisnall7a7ee302012-01-16 17:27:18 +00002637 // If we're promoting an integer to an _Atomic type then this is constant
2638 // if the integer is constant. We also need to check the converse in case
2639 // someone does something like:
2640 //
2641 // int a = (_Atomic(int))42;
2642 //
2643 // I doubt anyone would write code like this directly, but it's quite
2644 // possible as the result of macro expansions.
2645 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2646 CE->getCastKind() == CK_AtomicToNonAtomic)
2647 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2648
Richard Smithd62ca372011-12-06 22:44:34 +00002649 // Handle bitcasts of vector constants.
2650 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2651 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2652
Eli Friedman6bd97192011-12-21 00:43:02 +00002653 // Handle misc casts we want to ignore.
2654 // FIXME: Is it really safe to ignore all these?
2655 if (CE->getCastKind() == CK_NoOp ||
2656 CE->getCastKind() == CK_LValueToRValue ||
2657 CE->getCastKind() == CK_ToUnion ||
2658 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002659 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2660
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002661 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002662 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002663 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002664 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002665 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002666 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002667 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002668}
2669
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002670namespace {
2671 /// \brief Look for a call to a non-trivial function within an expression.
2672 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2673 {
2674 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2675
2676 bool NonTrivial;
2677
2678 public:
2679 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002680 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002681
2682 bool hasNonTrivialCall() const { return NonTrivial; }
2683
2684 void VisitCallExpr(CallExpr *E) {
2685 if (CXXMethodDecl *Method
2686 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2687 if (Method->isTrivial()) {
2688 // Recurse to children of the call.
2689 Inherited::VisitStmt(E);
2690 return;
2691 }
2692 }
2693
2694 NonTrivial = true;
2695 }
2696
2697 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2698 if (E->getConstructor()->isTrivial()) {
2699 // Recurse to children of the call.
2700 Inherited::VisitStmt(E);
2701 return;
2702 }
2703
2704 NonTrivial = true;
2705 }
2706
2707 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2708 if (E->getTemporary()->getDestructor()->isTrivial()) {
2709 Inherited::VisitStmt(E);
2710 return;
2711 }
2712
2713 NonTrivial = true;
2714 }
2715 };
2716}
2717
2718bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2719 NonTrivialCallFinder Finder(Ctx);
2720 Finder.Visit(this);
2721 return Finder.hasNonTrivialCall();
2722}
2723
Chandler Carruth82214a82011-02-18 23:54:50 +00002724/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2725/// pointer constant or not, as well as the specific kind of constant detected.
2726/// Null pointer constants can be integer constant expressions with the
2727/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2728/// (a GNU extension).
2729Expr::NullPointerConstantKind
2730Expr::isNullPointerConstant(ASTContext &Ctx,
2731 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002732 if (isValueDependent()) {
2733 switch (NPC) {
2734 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002735 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002736 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002737 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2738 return NPCK_ZeroInteger;
2739 else
2740 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002741
Douglas Gregorce940492009-09-25 04:25:58 +00002742 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002743 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002744 }
2745 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002746
Sebastian Redl07779722008-10-31 14:43:28 +00002747 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002748 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002749 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002750 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002751 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002752 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002753 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002754 Pointee->isVoidType() && // to void*
2755 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002756 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002757 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002758 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002759 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2760 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002761 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002762 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2763 // Accept ((void*)0) as a null pointer constant, as many other
2764 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002765 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002766 } else if (const GenericSelectionExpr *GE =
2767 dyn_cast<GenericSelectionExpr>(this)) {
2768 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002769 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002770 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002771 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002772 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002773 } else if (isa<GNUNullExpr>(this)) {
2774 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002775 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002776 } else if (const MaterializeTemporaryExpr *M
2777 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2778 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002779 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2780 if (const Expr *Source = OVE->getSourceExpr())
2781 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002782 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002783
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002784 // C++0x nullptr_t is always a null pointer constant.
2785 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002786 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002787
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002788 if (const RecordType *UT = getType()->getAsUnionType())
2789 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2790 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2791 const Expr *InitExpr = CLE->getInitializer();
2792 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2793 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2794 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002795 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002796 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002797 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002798 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002799
Reid Spencer5f016e22007-07-11 17:01:13 +00002800 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith70488e22012-02-14 21:38:30 +00002801 // test for the value 0. Don't use the C++11 constant expression semantics
2802 // for this, for now; once the dust settles on core issue 903, we might only
2803 // allow a literal 0 here in C++11 mode.
2804 if (Ctx.getLangOptions().CPlusPlus0x) {
2805 if (!isCXX98IntegralConstantExpr(Ctx))
2806 return NPCK_NotNull;
2807 } else {
2808 if (!isIntegerConstantExpr(Ctx))
2809 return NPCK_NotNull;
2810 }
Chandler Carruth82214a82011-02-18 23:54:50 +00002811
Richard Smith70488e22012-02-14 21:38:30 +00002812 return (EvaluateKnownConstInt(Ctx) == 0) ? NPCK_ZeroInteger : NPCK_NotNull;
Reid Spencer5f016e22007-07-11 17:01:13 +00002813}
Steve Naroff31a45842007-07-28 23:10:27 +00002814
John McCallf6a16482010-12-04 03:47:34 +00002815/// \brief If this expression is an l-value for an Objective C
2816/// property, find the underlying property reference expression.
2817const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2818 const Expr *E = this;
2819 while (true) {
2820 assert((E->getValueKind() == VK_LValue &&
2821 E->getObjectKind() == OK_ObjCProperty) &&
2822 "expression is not a property reference");
2823 E = E->IgnoreParenCasts();
2824 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2825 if (BO->getOpcode() == BO_Comma) {
2826 E = BO->getRHS();
2827 continue;
2828 }
2829 }
2830
2831 break;
2832 }
2833
2834 return cast<ObjCPropertyRefExpr>(E);
2835}
2836
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002837FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002838 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002839
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002840 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002841 if (ICE->getCastKind() == CK_LValueToRValue ||
2842 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002843 E = ICE->getSubExpr()->IgnoreParens();
2844 else
2845 break;
2846 }
2847
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002848 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002849 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002850 if (Field->isBitField())
2851 return Field;
2852
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002853 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2854 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2855 if (Field->isBitField())
2856 return Field;
2857
Eli Friedman42068e92011-07-13 02:05:57 +00002858 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002859 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2860 return BinOp->getLHS()->getBitField();
2861
Eli Friedman42068e92011-07-13 02:05:57 +00002862 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2863 return BinOp->getRHS()->getBitField();
2864 }
2865
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002866 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002867}
2868
Anders Carlsson09380262010-01-31 17:18:49 +00002869bool Expr::refersToVectorElement() const {
2870 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002871
Anders Carlsson09380262010-01-31 17:18:49 +00002872 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002873 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002874 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002875 E = ICE->getSubExpr()->IgnoreParens();
2876 else
2877 break;
2878 }
Sean Huntc3021132010-05-05 15:23:54 +00002879
Anders Carlsson09380262010-01-31 17:18:49 +00002880 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2881 return ASE->getBase()->getType()->isVectorType();
2882
2883 if (isa<ExtVectorElementExpr>(E))
2884 return true;
2885
2886 return false;
2887}
2888
Chris Lattner2140e902009-02-16 22:14:05 +00002889/// isArrow - Return true if the base expression is a pointer to vector,
2890/// return false if the base expression is a vector.
2891bool ExtVectorElementExpr::isArrow() const {
2892 return getBase()->getType()->isPointerType();
2893}
2894
Nate Begeman213541a2008-04-18 23:10:10 +00002895unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002896 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002897 return VT->getNumElements();
2898 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002899}
2900
Nate Begeman8a997642008-05-09 06:41:27 +00002901/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002902bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002903 // FIXME: Refactor this code to an accessor on the AST node which returns the
2904 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002905 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002906
2907 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002908 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002909 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002910
Nate Begeman190d6a22009-01-18 02:01:21 +00002911 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002912 if (Comp[0] == 's' || Comp[0] == 'S')
2913 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002914
Daniel Dunbar15027422009-10-17 23:53:04 +00002915 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002916 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002917 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002918
Steve Narofffec0b492007-07-30 03:29:09 +00002919 return false;
2920}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002921
Nate Begeman8a997642008-05-09 06:41:27 +00002922/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002923void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002924 SmallVectorImpl<unsigned> &Elts) const {
2925 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002926 if (Comp[0] == 's' || Comp[0] == 'S')
2927 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002928
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002929 bool isHi = Comp == "hi";
2930 bool isLo = Comp == "lo";
2931 bool isEven = Comp == "even";
2932 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002933
Nate Begeman8a997642008-05-09 06:41:27 +00002934 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2935 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002936
Nate Begeman8a997642008-05-09 06:41:27 +00002937 if (isHi)
2938 Index = e + i;
2939 else if (isLo)
2940 Index = i;
2941 else if (isEven)
2942 Index = 2 * i;
2943 else if (isOdd)
2944 Index = 2 * i + 1;
2945 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002946 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002947
Nate Begeman3b8d1162008-05-13 21:03:02 +00002948 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002949 }
Nate Begeman8a997642008-05-09 06:41:27 +00002950}
2951
Douglas Gregor04badcf2010-04-21 00:45:42 +00002952ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002953 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002954 SourceLocation LBracLoc,
2955 SourceLocation SuperLoc,
2956 bool IsInstanceSuper,
2957 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002958 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002959 ArrayRef<SourceLocation> SelLocs,
2960 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002961 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002962 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002963 SourceLocation RBracLoc,
2964 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002965 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002966 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002967 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002968 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002969 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2970 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002971 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002972 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
2973 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002974{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002975 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002976 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002977}
2978
Douglas Gregor04badcf2010-04-21 00:45:42 +00002979ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002980 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002981 SourceLocation LBracLoc,
2982 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002983 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002984 ArrayRef<SourceLocation> SelLocs,
2985 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002986 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002987 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002988 SourceLocation RBracLoc,
2989 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002990 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002991 T->isDependentType(), T->isInstantiationDependentType(),
2992 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002993 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2994 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002995 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002996 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002997 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002998{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002999 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003000 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00003001}
3002
Douglas Gregor04badcf2010-04-21 00:45:42 +00003003ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003004 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003005 SourceLocation LBracLoc,
3006 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003007 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003008 ArrayRef<SourceLocation> SelLocs,
3009 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003010 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003011 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003012 SourceLocation RBracLoc,
3013 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00003014 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003015 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003016 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003017 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00003018 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
3019 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00003020 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003021 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003022 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00003023{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003024 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003025 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003026}
3027
3028void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
3029 ArrayRef<SourceLocation> SelLocs,
3030 SelectorLocationsKind SelLocsK) {
3031 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00003032 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003033 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003034 if (Args[I]->isTypeDependent())
3035 ExprBits.TypeDependent = true;
3036 if (Args[I]->isValueDependent())
3037 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003038 if (Args[I]->isInstantiationDependent())
3039 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003040 if (Args[I]->containsUnexpandedParameterPack())
3041 ExprBits.ContainsUnexpandedParameterPack = true;
3042
3043 MyArgs[I] = Args[I];
3044 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003045
Benjamin Kramer19562c92012-02-20 00:20:48 +00003046 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003047 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003048 if (SelLocsK == SelLoc_NonStandard)
3049 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3050 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003051}
3052
Douglas Gregor04badcf2010-04-21 00:45:42 +00003053ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003054 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003055 SourceLocation LBracLoc,
3056 SourceLocation SuperLoc,
3057 bool IsInstanceSuper,
3058 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003059 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003060 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003061 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003062 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003063 SourceLocation RBracLoc,
3064 bool isImplicit) {
3065 assert((!SelLocs.empty() || isImplicit) &&
3066 "No selector locs for non-implicit message");
3067 ObjCMessageExpr *Mem;
3068 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3069 if (isImplicit)
3070 Mem = alloc(Context, Args.size(), 0);
3071 else
3072 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003073 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003074 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003075 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003076}
3077
3078ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003079 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003080 SourceLocation LBracLoc,
3081 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003082 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003083 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003084 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003085 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003086 SourceLocation RBracLoc,
3087 bool isImplicit) {
3088 assert((!SelLocs.empty() || isImplicit) &&
3089 "No selector locs for non-implicit message");
3090 ObjCMessageExpr *Mem;
3091 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3092 if (isImplicit)
3093 Mem = alloc(Context, Args.size(), 0);
3094 else
3095 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003096 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003097 SelLocs, SelLocsK, Method, Args, RBracLoc,
3098 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003099}
3100
3101ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003102 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003103 SourceLocation LBracLoc,
3104 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003105 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003106 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003107 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003108 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003109 SourceLocation RBracLoc,
3110 bool isImplicit) {
3111 assert((!SelLocs.empty() || isImplicit) &&
3112 "No selector locs for non-implicit message");
3113 ObjCMessageExpr *Mem;
3114 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3115 if (isImplicit)
3116 Mem = alloc(Context, Args.size(), 0);
3117 else
3118 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003119 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003120 SelLocs, SelLocsK, Method, Args, RBracLoc,
3121 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003122}
3123
Sean Huntc3021132010-05-05 15:23:54 +00003124ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003125 unsigned NumArgs,
3126 unsigned NumStoredSelLocs) {
3127 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003128 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3129}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003130
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003131ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3132 ArrayRef<Expr *> Args,
3133 SourceLocation RBraceLoc,
3134 ArrayRef<SourceLocation> SelLocs,
3135 Selector Sel,
3136 SelectorLocationsKind &SelLocsK) {
3137 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3138 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3139 : 0;
3140 return alloc(C, Args.size(), NumStoredSelLocs);
3141}
3142
3143ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3144 unsigned NumArgs,
3145 unsigned NumStoredSelLocs) {
3146 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3147 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3148 return (ObjCMessageExpr *)C.Allocate(Size,
3149 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3150}
3151
3152void ObjCMessageExpr::getSelectorLocs(
3153 SmallVectorImpl<SourceLocation> &SelLocs) const {
3154 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3155 SelLocs.push_back(getSelectorLoc(i));
3156}
3157
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003158SourceRange ObjCMessageExpr::getReceiverRange() const {
3159 switch (getReceiverKind()) {
3160 case Instance:
3161 return getInstanceReceiver()->getSourceRange();
3162
3163 case Class:
3164 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3165
3166 case SuperInstance:
3167 case SuperClass:
3168 return getSuperLoc();
3169 }
3170
David Blaikie30263482012-01-20 21:50:17 +00003171 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003172}
3173
Douglas Gregor04badcf2010-04-21 00:45:42 +00003174Selector ObjCMessageExpr::getSelector() const {
3175 if (HasMethod)
3176 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3177 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003178 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003179}
3180
3181ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3182 switch (getReceiverKind()) {
3183 case Instance:
3184 if (const ObjCObjectPointerType *Ptr
3185 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
3186 return Ptr->getInterfaceDecl();
3187 break;
3188
3189 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00003190 if (const ObjCObjectType *Ty
3191 = getClassReceiver()->getAs<ObjCObjectType>())
3192 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003193 break;
3194
3195 case SuperInstance:
3196 if (const ObjCObjectPointerType *Ptr
3197 = getSuperType()->getAs<ObjCObjectPointerType>())
3198 return Ptr->getInterfaceDecl();
3199 break;
3200
3201 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00003202 if (const ObjCObjectType *Iface
3203 = getSuperType()->getAs<ObjCObjectType>())
3204 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003205 break;
3206 }
3207
3208 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003209}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003210
Chris Lattner5f9e2722011-07-23 10:55:15 +00003211StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003212 switch (getBridgeKind()) {
3213 case OBC_Bridge:
3214 return "__bridge";
3215 case OBC_BridgeTransfer:
3216 return "__bridge_transfer";
3217 case OBC_BridgeRetained:
3218 return "__bridge_retained";
3219 }
David Blaikie30263482012-01-20 21:50:17 +00003220
3221 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003222}
3223
Jay Foad4ba2a172011-01-12 09:06:06 +00003224bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003225 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003226}
3227
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003228ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3229 QualType Type, SourceLocation BLoc,
3230 SourceLocation RP)
3231 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3232 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003233 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003234 Type->containsUnexpandedParameterPack()),
3235 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3236{
3237 SubExprs = new (C) Stmt*[nexpr];
3238 for (unsigned i = 0; i < nexpr; i++) {
3239 if (args[i]->isTypeDependent())
3240 ExprBits.TypeDependent = true;
3241 if (args[i]->isValueDependent())
3242 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003243 if (args[i]->isInstantiationDependent())
3244 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003245 if (args[i]->containsUnexpandedParameterPack())
3246 ExprBits.ContainsUnexpandedParameterPack = true;
3247
3248 SubExprs[i] = args[i];
3249 }
3250}
3251
Nate Begeman888376a2009-08-12 02:28:50 +00003252void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3253 unsigned NumExprs) {
3254 if (SubExprs) C.Deallocate(SubExprs);
3255
3256 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003257 this->NumExprs = NumExprs;
3258 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003259}
Nate Begeman888376a2009-08-12 02:28:50 +00003260
Peter Collingbournef111d932011-04-15 00:35:48 +00003261GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3262 SourceLocation GenericLoc, Expr *ControllingExpr,
3263 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3264 unsigned NumAssocs, SourceLocation DefaultLoc,
3265 SourceLocation RParenLoc,
3266 bool ContainsUnexpandedParameterPack,
3267 unsigned ResultIndex)
3268 : Expr(GenericSelectionExprClass,
3269 AssocExprs[ResultIndex]->getType(),
3270 AssocExprs[ResultIndex]->getValueKind(),
3271 AssocExprs[ResultIndex]->getObjectKind(),
3272 AssocExprs[ResultIndex]->isTypeDependent(),
3273 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003274 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003275 ContainsUnexpandedParameterPack),
3276 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3277 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3278 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3279 RParenLoc(RParenLoc) {
3280 SubExprs[CONTROLLING] = ControllingExpr;
3281 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3282 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3283}
3284
3285GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3286 SourceLocation GenericLoc, Expr *ControllingExpr,
3287 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3288 unsigned NumAssocs, SourceLocation DefaultLoc,
3289 SourceLocation RParenLoc,
3290 bool ContainsUnexpandedParameterPack)
3291 : Expr(GenericSelectionExprClass,
3292 Context.DependentTy,
3293 VK_RValue,
3294 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003295 /*isTypeDependent=*/true,
3296 /*isValueDependent=*/true,
3297 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003298 ContainsUnexpandedParameterPack),
3299 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3300 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3301 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3302 RParenLoc(RParenLoc) {
3303 SubExprs[CONTROLLING] = ControllingExpr;
3304 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3305 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3306}
3307
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003308//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003309// DesignatedInitExpr
3310//===----------------------------------------------------------------------===//
3311
Chandler Carruthb1138242011-06-16 06:47:06 +00003312IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003313 assert(Kind == FieldDesignator && "Only valid on a field designator");
3314 if (Field.NameOrField & 0x01)
3315 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3316 else
3317 return getField()->getIdentifier();
3318}
3319
Sean Huntc3021132010-05-05 15:23:54 +00003320DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003321 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003322 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003323 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003324 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003325 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003326 unsigned NumIndexExprs,
3327 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003328 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003329 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003330 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003331 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003332 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003333 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3334 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003335 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003336
3337 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003338 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003339 *Child++ = Init;
3340
3341 // Copy the designators and their subexpressions, computing
3342 // value-dependence along the way.
3343 unsigned IndexIdx = 0;
3344 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003345 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003346
3347 if (this->Designators[I].isArrayDesignator()) {
3348 // Compute type- and value-dependence.
3349 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003350 if (Index->isTypeDependent() || Index->isValueDependent())
3351 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003352 if (Index->isInstantiationDependent())
3353 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003354 // Propagate unexpanded parameter packs.
3355 if (Index->containsUnexpandedParameterPack())
3356 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003357
3358 // Copy the index expressions into permanent storage.
3359 *Child++ = IndexExprs[IndexIdx++];
3360 } else if (this->Designators[I].isArrayRangeDesignator()) {
3361 // Compute type- and value-dependence.
3362 Expr *Start = IndexExprs[IndexIdx];
3363 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003364 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003365 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003366 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003367 ExprBits.InstantiationDependent = true;
3368 } else if (Start->isInstantiationDependent() ||
3369 End->isInstantiationDependent()) {
3370 ExprBits.InstantiationDependent = true;
3371 }
3372
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003373 // Propagate unexpanded parameter packs.
3374 if (Start->containsUnexpandedParameterPack() ||
3375 End->containsUnexpandedParameterPack())
3376 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003377
3378 // Copy the start/end expressions into permanent storage.
3379 *Child++ = IndexExprs[IndexIdx++];
3380 *Child++ = IndexExprs[IndexIdx++];
3381 }
3382 }
3383
3384 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003385}
3386
Douglas Gregor05c13a32009-01-22 00:58:24 +00003387DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003388DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003389 unsigned NumDesignators,
3390 Expr **IndexExprs, unsigned NumIndexExprs,
3391 SourceLocation ColonOrEqualLoc,
3392 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003393 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003394 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003395 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003396 ColonOrEqualLoc, UsesColonSyntax,
3397 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003398}
3399
Mike Stump1eb44332009-09-09 15:08:12 +00003400DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003401 unsigned NumIndexExprs) {
3402 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3403 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3404 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3405}
3406
Douglas Gregor319d57f2010-01-06 23:17:19 +00003407void DesignatedInitExpr::setDesignators(ASTContext &C,
3408 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003409 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003410 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003411 NumDesignators = NumDesigs;
3412 for (unsigned I = 0; I != NumDesigs; ++I)
3413 Designators[I] = Desigs[I];
3414}
3415
Abramo Bagnara24f46742011-03-16 15:08:46 +00003416SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3417 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3418 if (size() == 1)
3419 return DIE->getDesignator(0)->getSourceRange();
3420 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3421 DIE->getDesignator(size()-1)->getEndLocation());
3422}
3423
Douglas Gregor05c13a32009-01-22 00:58:24 +00003424SourceRange DesignatedInitExpr::getSourceRange() const {
3425 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003426 Designator &First =
3427 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003428 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003429 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003430 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3431 else
3432 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3433 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003434 StartLoc =
3435 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003436 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3437}
3438
Douglas Gregor05c13a32009-01-22 00:58:24 +00003439Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3440 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3441 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3442 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003443 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3444 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3445}
3446
3447Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003448 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003449 "Requires array range designator");
3450 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3451 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003452 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3453 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3454}
3455
3456Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003457 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003458 "Requires array range designator");
3459 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3460 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003461 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3462 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3463}
3464
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003465/// \brief Replaces the designator at index @p Idx with the series
3466/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003467void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003468 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003469 const Designator *Last) {
3470 unsigned NumNewDesignators = Last - First;
3471 if (NumNewDesignators == 0) {
3472 std::copy_backward(Designators + Idx + 1,
3473 Designators + NumDesignators,
3474 Designators + Idx);
3475 --NumNewDesignators;
3476 return;
3477 } else if (NumNewDesignators == 1) {
3478 Designators[Idx] = *First;
3479 return;
3480 }
3481
Mike Stump1eb44332009-09-09 15:08:12 +00003482 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003483 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003484 std::copy(Designators, Designators + Idx, NewDesignators);
3485 std::copy(First, Last, NewDesignators + Idx);
3486 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3487 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003488 Designators = NewDesignators;
3489 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3490}
3491
Mike Stump1eb44332009-09-09 15:08:12 +00003492ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003493 Expr **exprs, unsigned nexprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003494 SourceLocation rparenloc)
3495 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003496 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003497 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00003498 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003499 for (unsigned i = 0; i != nexprs; ++i) {
3500 if (exprs[i]->isTypeDependent())
3501 ExprBits.TypeDependent = true;
3502 if (exprs[i]->isValueDependent())
3503 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003504 if (exprs[i]->isInstantiationDependent())
3505 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003506 if (exprs[i]->containsUnexpandedParameterPack())
3507 ExprBits.ContainsUnexpandedParameterPack = true;
3508
Nate Begeman2ef13e52009-08-10 23:49:36 +00003509 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003510 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003511}
3512
John McCalle996ffd2011-02-16 08:02:54 +00003513const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3514 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3515 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003516 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3517 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003518 e = cast<CXXConstructExpr>(e)->getArg(0);
3519 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3520 e = ice->getSubExpr();
3521 return cast<OpaqueValueExpr>(e);
3522}
3523
John McCall4b9c2d22011-11-06 09:01:30 +00003524PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3525 unsigned numSemanticExprs) {
3526 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3527 (1 + numSemanticExprs) * sizeof(Expr*),
3528 llvm::alignOf<PseudoObjectExpr>());
3529 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3530}
3531
3532PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3533 : Expr(PseudoObjectExprClass, shell) {
3534 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3535}
3536
3537PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3538 ArrayRef<Expr*> semantics,
3539 unsigned resultIndex) {
3540 assert(syntax && "no syntactic expression!");
3541 assert(semantics.size() && "no semantic expressions!");
3542
3543 QualType type;
3544 ExprValueKind VK;
3545 if (resultIndex == NoResult) {
3546 type = C.VoidTy;
3547 VK = VK_RValue;
3548 } else {
3549 assert(resultIndex < semantics.size());
3550 type = semantics[resultIndex]->getType();
3551 VK = semantics[resultIndex]->getValueKind();
3552 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3553 }
3554
3555 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3556 (1 + semantics.size()) * sizeof(Expr*),
3557 llvm::alignOf<PseudoObjectExpr>());
3558 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3559 resultIndex);
3560}
3561
3562PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3563 Expr *syntax, ArrayRef<Expr*> semantics,
3564 unsigned resultIndex)
3565 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3566 /*filled in at end of ctor*/ false, false, false, false) {
3567 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3568 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3569
3570 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3571 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3572 getSubExprsBuffer()[i] = E;
3573
3574 if (E->isTypeDependent())
3575 ExprBits.TypeDependent = true;
3576 if (E->isValueDependent())
3577 ExprBits.ValueDependent = true;
3578 if (E->isInstantiationDependent())
3579 ExprBits.InstantiationDependent = true;
3580 if (E->containsUnexpandedParameterPack())
3581 ExprBits.ContainsUnexpandedParameterPack = true;
3582
3583 if (isa<OpaqueValueExpr>(E))
3584 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3585 "opaque-value semantic expressions for pseudo-object "
3586 "operations must have sources");
3587 }
3588}
3589
Douglas Gregor05c13a32009-01-22 00:58:24 +00003590//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003591// ExprIterator.
3592//===----------------------------------------------------------------------===//
3593
3594Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3595Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3596Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3597const Expr* ConstExprIterator::operator[](size_t idx) const {
3598 return cast<Expr>(I[idx]);
3599}
3600const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3601const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3602
3603//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003604// Child Iterators for iterating over subexpressions/substatements
3605//===----------------------------------------------------------------------===//
3606
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003607// UnaryExprOrTypeTraitExpr
3608Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003609 // If this is of a type and the type is a VLA type (and not a typedef), the
3610 // size expression of the VLA needs to be treated as an executable expression.
3611 // Why isn't this weirdness documented better in StmtIterator?
3612 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003613 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003614 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003615 return child_range(child_iterator(T), child_iterator());
3616 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003617 }
John McCall63c00d72011-02-09 08:16:59 +00003618 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003619}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003620
Steve Naroff563477d2007-09-18 23:55:05 +00003621// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003622Stmt::child_range ObjCMessageExpr::children() {
3623 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003624 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003625 begin = reinterpret_cast<Stmt **>(this + 1);
3626 else
3627 begin = reinterpret_cast<Stmt **>(getArgs());
3628 return child_range(begin,
3629 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003630}
3631
Steve Naroff4eb206b2008-09-03 18:15:37 +00003632// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003633BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003634 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003635 bool constAdded)
Douglas Gregor561f8122011-07-01 01:22:09 +00003636 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003637 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003638 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003639{
Douglas Gregord967e312011-01-19 21:52:31 +00003640 bool TypeDependent = false;
3641 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +00003642 bool InstantiationDependent = false;
3643 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3644 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +00003645 ExprBits.TypeDependent = TypeDependent;
3646 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +00003647 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003648}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003649
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003650ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3651 QualType T, ObjCMethodDecl *Method,
3652 SourceRange SR)
3653 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3654 false, false, false, false),
3655 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3656{
3657 Expr **SaveElements = getElements();
3658 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3659 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3660 ExprBits.ValueDependent = true;
3661 if (Elements[I]->isInstantiationDependent())
3662 ExprBits.InstantiationDependent = true;
3663 if (Elements[I]->containsUnexpandedParameterPack())
3664 ExprBits.ContainsUnexpandedParameterPack = true;
3665
3666 SaveElements[I] = Elements[I];
3667 }
3668}
3669
3670ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3671 llvm::ArrayRef<Expr *> Elements,
3672 QualType T, ObjCMethodDecl * Method,
3673 SourceRange SR) {
3674 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3675 + Elements.size() * sizeof(Expr *));
3676 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3677}
3678
3679ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3680 unsigned NumElements) {
3681
3682 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3683 + NumElements * sizeof(Expr *));
3684 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3685}
3686
3687ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3688 ArrayRef<ObjCDictionaryElement> VK,
3689 bool HasPackExpansions,
3690 QualType T, ObjCMethodDecl *method,
3691 SourceRange SR)
3692 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3693 false, false),
3694 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3695 DictWithObjectsMethod(method)
3696{
3697 KeyValuePair *KeyValues = getKeyValues();
3698 ExpansionData *Expansions = getExpansionData();
3699 for (unsigned I = 0; I < NumElements; I++) {
3700 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3701 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3702 ExprBits.ValueDependent = true;
3703 if (VK[I].Key->isInstantiationDependent() ||
3704 VK[I].Value->isInstantiationDependent())
3705 ExprBits.InstantiationDependent = true;
3706 if (VK[I].EllipsisLoc.isInvalid() &&
3707 (VK[I].Key->containsUnexpandedParameterPack() ||
3708 VK[I].Value->containsUnexpandedParameterPack()))
3709 ExprBits.ContainsUnexpandedParameterPack = true;
3710
3711 KeyValues[I].Key = VK[I].Key;
3712 KeyValues[I].Value = VK[I].Value;
3713 if (Expansions) {
3714 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3715 if (VK[I].NumExpansions)
3716 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3717 else
3718 Expansions[I].NumExpansionsPlusOne = 0;
3719 }
3720 }
3721}
3722
3723ObjCDictionaryLiteral *
3724ObjCDictionaryLiteral::Create(ASTContext &C,
3725 ArrayRef<ObjCDictionaryElement> VK,
3726 bool HasPackExpansions,
3727 QualType T, ObjCMethodDecl *method,
3728 SourceRange SR) {
3729 unsigned ExpansionsSize = 0;
3730 if (HasPackExpansions)
3731 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3732
3733 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3734 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3735 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3736}
3737
3738ObjCDictionaryLiteral *
3739ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3740 bool HasPackExpansions) {
3741 unsigned ExpansionsSize = 0;
3742 if (HasPackExpansions)
3743 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3744 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3745 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3746 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3747 HasPackExpansions);
3748}
3749
3750ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
3751 Expr *base,
3752 Expr *key, QualType T,
3753 ObjCMethodDecl *getMethod,
3754 ObjCMethodDecl *setMethod,
3755 SourceLocation RB) {
3756 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
3757 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
3758 OK_ObjCSubscript,
3759 getMethod, setMethod, RB);
3760}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003761
3762AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3763 QualType t, AtomicOp op, SourceLocation RP)
3764 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3765 false, false, false, false),
3766 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3767{
3768 for (unsigned i = 0; i < nexpr; i++) {
3769 if (args[i]->isTypeDependent())
3770 ExprBits.TypeDependent = true;
3771 if (args[i]->isValueDependent())
3772 ExprBits.ValueDependent = true;
3773 if (args[i]->isInstantiationDependent())
3774 ExprBits.InstantiationDependent = true;
3775 if (args[i]->containsUnexpandedParameterPack())
3776 ExprBits.ContainsUnexpandedParameterPack = true;
3777
3778 SubExprs[i] = args[i];
3779 }
3780}