blob: bba35ef532ff466e40b31e1de5c52cc33a792c7c [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/Lexer.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000026#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000028#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000030#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000031using namespace clang;
32
Chris Lattner2b334bb2010-04-16 23:34:13 +000033/// isKnownToHaveBooleanValue - Return true if this is an integer expression
34/// that is known to return 0 or 1. This happens for _Bool/bool expressions
35/// but also int expressions which are produced by things like comparisons in
36/// C.
37bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000038 const Expr *E = IgnoreParens();
39
Chris Lattner2b334bb2010-04-16 23:34:13 +000040 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000041 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000042 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000043 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000044
Peter Collingbournef111d932011-04-15 00:35:48 +000045 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000046 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000047 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000048 return UO->getSubExpr()->isKnownToHaveBooleanValue();
49 default:
50 return false;
51 }
52 }
Sean Huntc3021132010-05-05 15:23:54 +000053
John McCall6907fbe2010-06-12 01:56:02 +000054 // Only look through implicit casts. If the user writes
55 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000056 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000057 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000058
Peter Collingbournef111d932011-04-15 00:35:48 +000059 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000060 switch (BO->getOpcode()) {
61 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000062 case BO_LT: // Relational operators.
63 case BO_GT:
64 case BO_LE:
65 case BO_GE:
66 case BO_EQ: // Equality operators.
67 case BO_NE:
68 case BO_LAnd: // AND operator.
69 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000070 return true;
Sean Huntc3021132010-05-05 15:23:54 +000071
John McCall2de56d12010-08-25 11:45:40 +000072 case BO_And: // Bitwise AND operator.
73 case BO_Xor: // Bitwise XOR operator.
74 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000075 // Handle things like (x==2)|(y==12).
76 return BO->getLHS()->isKnownToHaveBooleanValue() &&
77 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000078
John McCall2de56d12010-08-25 11:45:40 +000079 case BO_Comma:
80 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000081 return BO->getRHS()->isKnownToHaveBooleanValue();
82 }
83 }
Sean Huntc3021132010-05-05 15:23:54 +000084
Peter Collingbournef111d932011-04-15 00:35:48 +000085 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000086 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
87 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000088
Chris Lattner2b334bb2010-04-16 23:34:13 +000089 return false;
90}
91
John McCall63c00d72011-02-09 08:16:59 +000092// Amusing macro metaprogramming hack: check whether a class provides
93// a more specific implementation of getExprLoc().
94namespace {
95 /// This implementation is used when a class provides a custom
96 /// implementation of getExprLoc.
97 template <class E, class T>
98 SourceLocation getExprLocImpl(const Expr *expr,
99 SourceLocation (T::*v)() const) {
100 return static_cast<const E*>(expr)->getExprLoc();
101 }
102
103 /// This implementation is used when a class doesn't provide
104 /// a custom implementation of getExprLoc. Overload resolution
105 /// should pick it over the implementation above because it's
106 /// more specialized according to function template partial ordering.
107 template <class E>
108 SourceLocation getExprLocImpl(const Expr *expr,
109 SourceLocation (Expr::*v)() const) {
110 return static_cast<const E*>(expr)->getSourceRange().getBegin();
111 }
112}
113
114SourceLocation Expr::getExprLoc() const {
115 switch (getStmtClass()) {
116 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
117#define ABSTRACT_STMT(type)
118#define STMT(type, base) \
119 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
120#define EXPR(type, base) \
121 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
122#include "clang/AST/StmtNodes.inc"
123 }
124 llvm_unreachable("unknown statement kind");
125 return SourceLocation();
126}
127
Reid Spencer5f016e22007-07-11 17:01:13 +0000128//===----------------------------------------------------------------------===//
129// Primary Expressions.
130//===----------------------------------------------------------------------===//
131
John McCalld5532b62009-11-23 01:53:49 +0000132void ExplicitTemplateArgumentList::initializeFrom(
133 const TemplateArgumentListInfo &Info) {
134 LAngleLoc = Info.getLAngleLoc();
135 RAngleLoc = Info.getRAngleLoc();
136 NumTemplateArgs = Info.size();
137
138 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
139 for (unsigned i = 0; i != NumTemplateArgs; ++i)
140 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
141}
142
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000143void ExplicitTemplateArgumentList::initializeFrom(
144 const TemplateArgumentListInfo &Info,
145 bool &Dependent,
146 bool &ContainsUnexpandedParameterPack) {
147 LAngleLoc = Info.getLAngleLoc();
148 RAngleLoc = Info.getRAngleLoc();
149 NumTemplateArgs = Info.size();
150
151 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
152 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
153 Dependent = Dependent || Info[i].getArgument().isDependent();
154 ContainsUnexpandedParameterPack
155 = ContainsUnexpandedParameterPack ||
156 Info[i].getArgument().containsUnexpandedParameterPack();
157
158 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
159 }
160}
161
John McCalld5532b62009-11-23 01:53:49 +0000162void ExplicitTemplateArgumentList::copyInto(
163 TemplateArgumentListInfo &Info) const {
164 Info.setLAngleLoc(LAngleLoc);
165 Info.setRAngleLoc(RAngleLoc);
166 for (unsigned I = 0; I != NumTemplateArgs; ++I)
167 Info.addArgument(getTemplateArgs()[I]);
168}
169
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000170std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
171 return sizeof(ExplicitTemplateArgumentList) +
172 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
173}
174
John McCalld5532b62009-11-23 01:53:49 +0000175std::size_t ExplicitTemplateArgumentList::sizeFor(
176 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000177 return sizeFor(Info.size());
John McCalld5532b62009-11-23 01:53:49 +0000178}
179
Douglas Gregord967e312011-01-19 21:52:31 +0000180/// \brief Compute the type- and value-dependence of a declaration reference
181/// based on the declaration being referenced.
182static void computeDeclRefDependence(NamedDecl *D, QualType T,
183 bool &TypeDependent,
184 bool &ValueDependent) {
185 TypeDependent = false;
186 ValueDependent = false;
Sean Huntc3021132010-05-05 15:23:54 +0000187
Douglas Gregor0da76df2009-11-23 11:41:28 +0000188
189 // (TD) C++ [temp.dep.expr]p3:
190 // An id-expression is type-dependent if it contains:
191 //
Sean Huntc3021132010-05-05 15:23:54 +0000192 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000193 //
194 // (VD) C++ [temp.dep.constexpr]p2:
195 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000196
Douglas Gregor0da76df2009-11-23 11:41:28 +0000197 // (TD) - an identifier that was declared with dependent type
198 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000199 if (T->isDependentType()) {
200 TypeDependent = true;
201 ValueDependent = true;
202 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000203 }
Douglas Gregord967e312011-01-19 21:52:31 +0000204
Douglas Gregor0da76df2009-11-23 11:41:28 +0000205 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000206 if (D->getDeclName().getNameKind()
207 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor0da76df2009-11-23 11:41:28 +0000208 D->getDeclName().getCXXNameType()->isDependentType()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000209 TypeDependent = true;
210 ValueDependent = true;
211 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000212 }
213 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000214 if (isa<NonTypeTemplateParmDecl>(D)) {
215 ValueDependent = true;
216 return;
217 }
218
Douglas Gregor0da76df2009-11-23 11:41:28 +0000219 // (VD) - a constant with integral or enumeration type and is
220 // initialized with an expression that is value-dependent.
Douglas Gregord967e312011-01-19 21:52:31 +0000221 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000222 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000223 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000224 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000225 if (Init->isValueDependent())
Douglas Gregord967e312011-01-19 21:52:31 +0000226 ValueDependent = true;
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000227 }
Douglas Gregord967e312011-01-19 21:52:31 +0000228
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000229 // (VD) - FIXME: Missing from the standard:
230 // - a member function or a static data member of the current
231 // instantiation
232 else if (Var->isStaticDataMember() &&
Douglas Gregor7ed5bd32010-05-11 08:44:04 +0000233 Var->getDeclContext()->isDependentContext())
Douglas Gregord967e312011-01-19 21:52:31 +0000234 ValueDependent = true;
235
236 return;
237 }
238
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000239 // (VD) - FIXME: Missing from the standard:
240 // - a member function or a static data member of the current
241 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000242 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
243 ValueDependent = true;
244 return;
245 }
246}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000247
Douglas Gregord967e312011-01-19 21:52:31 +0000248void DeclRefExpr::computeDependence() {
249 bool TypeDependent = false;
250 bool ValueDependent = false;
251 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent);
252
253 // (TD) C++ [temp.dep.expr]p3:
254 // An id-expression is type-dependent if it contains:
255 //
256 // and
257 //
258 // (VD) C++ [temp.dep.constexpr]p2:
259 // An identifier is value-dependent if it is:
260 if (!TypeDependent && !ValueDependent &&
261 hasExplicitTemplateArgs() &&
262 TemplateSpecializationType::anyDependentTemplateArguments(
263 getTemplateArgs(),
264 getNumTemplateArgs())) {
265 TypeDependent = true;
266 ValueDependent = true;
267 }
268
269 ExprBits.TypeDependent = TypeDependent;
270 ExprBits.ValueDependent = ValueDependent;
271
Douglas Gregor10738d32010-12-23 23:51:58 +0000272 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000273 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000274 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000275}
276
Chandler Carruth3aa81402011-05-01 23:48:14 +0000277DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000278 ValueDecl *D, SourceLocation NameLoc,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000279 NamedDecl *FoundD,
John McCalld5532b62009-11-23 01:53:49 +0000280 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000281 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000282 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000283 D(D), Loc(NameLoc) {
284 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000285 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000286 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000287 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
288 if (FoundD)
289 getInternalFoundDecl() = FoundD;
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000290 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000291 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000292 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000293
294 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000295}
296
Douglas Gregor40d96a62011-02-28 21:54:11 +0000297DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000298 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000299 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000300 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000301 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000302 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000303 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
304 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000305 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000306 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000307 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
308 if (FoundD)
309 getInternalFoundDecl() = FoundD;
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000310 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Abramo Bagnara25777432010-08-11 22:01:17 +0000311 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000312 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000313
314 computeDependence();
315}
316
Douglas Gregora2813ce2009-10-23 18:54:35 +0000317DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000318 NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000319 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000320 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000321 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000322 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000323 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000324 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000325 return Create(Context, QualifierLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000326 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000327 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000328}
329
330DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000331 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000332 ValueDecl *D,
333 const DeclarationNameInfo &NameInfo,
334 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000335 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000336 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000337 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000338 // Filter out cases where the found Decl is the same as the value refenenced.
339 if (D == FoundD)
340 FoundD = 0;
341
Douglas Gregora2813ce2009-10-23 18:54:35 +0000342 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000343 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000344 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000345 if (FoundD)
346 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000347 if (TemplateArgs)
348 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000349
Chris Lattner32488542010-10-30 05:14:06 +0000350 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth3aa81402011-05-01 23:48:14 +0000351 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
352 T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000353}
354
Chandler Carruth3aa81402011-05-01 23:48:14 +0000355DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000356 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000357 bool HasFoundDecl,
Douglas Gregordef03542011-02-04 12:01:24 +0000358 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000359 unsigned NumTemplateArgs) {
360 std::size_t Size = sizeof(DeclRefExpr);
361 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000362 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000363 if (HasFoundDecl)
364 Size += sizeof(NamedDecl *);
Douglas Gregordef03542011-02-04 12:01:24 +0000365 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000366 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000367
Chris Lattner32488542010-10-30 05:14:06 +0000368 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000369 return new (Mem) DeclRefExpr(EmptyShell());
370}
371
Douglas Gregora2813ce2009-10-23 18:54:35 +0000372SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000373 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000374 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000375 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000376 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000377 R.setEnd(getRAngleLoc());
378 return R;
379}
380
Anders Carlsson3a082d82009-09-08 18:24:21 +0000381// FIXME: Maybe this should use DeclPrinter with a special "print predefined
382// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000383std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
384 ASTContext &Context = CurrentDecl->getASTContext();
385
Anders Carlsson3a082d82009-09-08 18:24:21 +0000386 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000387 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000388 return FD->getNameAsString();
389
390 llvm::SmallString<256> Name;
391 llvm::raw_svector_ostream Out(Name);
392
393 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000394 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000395 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000396 if (MD->isStatic())
397 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000398 }
399
400 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000401
402 std::string Proto = FD->getQualifiedNameAsString(Policy);
403
John McCall183700f2009-09-21 23:43:11 +0000404 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000405 const FunctionProtoType *FT = 0;
406 if (FD->hasWrittenPrototype())
407 FT = dyn_cast<FunctionProtoType>(AFT);
408
409 Proto += "(";
410 if (FT) {
411 llvm::raw_string_ostream POut(Proto);
412 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
413 if (i) POut << ", ";
414 std::string Param;
415 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
416 POut << Param;
417 }
418
419 if (FT->isVariadic()) {
420 if (FD->getNumParams()) POut << ", ";
421 POut << "...";
422 }
423 }
424 Proto += ")";
425
Sam Weinig4eadcc52009-12-27 01:38:20 +0000426 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
427 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
428 if (ThisQuals.hasConst())
429 Proto += " const";
430 if (ThisQuals.hasVolatile())
431 Proto += " volatile";
432 }
433
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000434 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
435 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000436
437 Out << Proto;
438
439 Out.flush();
440 return Name.str().str();
441 }
442 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
443 llvm::SmallString<256> Name;
444 llvm::raw_svector_ostream Out(Name);
445 Out << (MD->isInstanceMethod() ? '-' : '+');
446 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000447
448 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
449 // a null check to avoid a crash.
450 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000451 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000452
Anders Carlsson3a082d82009-09-08 18:24:21 +0000453 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000454 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
455 Out << '(' << CID << ')';
456
Anders Carlsson3a082d82009-09-08 18:24:21 +0000457 Out << ' ';
458 Out << MD->getSelector().getAsString();
459 Out << ']';
460
461 Out.flush();
462 return Name.str().str();
463 }
464 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
465 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
466 return "top level";
467 }
468 return "";
469}
470
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000471void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
472 if (hasAllocation())
473 C.Deallocate(pVal);
474
475 BitWidth = Val.getBitWidth();
476 unsigned NumWords = Val.getNumWords();
477 const uint64_t* Words = Val.getRawData();
478 if (NumWords > 1) {
479 pVal = new (C) uint64_t[NumWords];
480 std::copy(Words, Words + NumWords, pVal);
481 } else if (NumWords == 1)
482 VAL = Words[0];
483 else
484 VAL = 0;
485}
486
487IntegerLiteral *
488IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
489 QualType type, SourceLocation l) {
490 return new (C) IntegerLiteral(C, V, type, l);
491}
492
493IntegerLiteral *
494IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
495 return new (C) IntegerLiteral(Empty);
496}
497
498FloatingLiteral *
499FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
500 bool isexact, QualType Type, SourceLocation L) {
501 return new (C) FloatingLiteral(C, V, isexact, Type, L);
502}
503
504FloatingLiteral *
505FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
506 return new (C) FloatingLiteral(Empty);
507}
508
Chris Lattnerda8249e2008-06-07 22:13:43 +0000509/// getValueAsApproximateDouble - This returns the value as an inaccurate
510/// double. Note that this may cause loss of precision, but is useful for
511/// debugging dumps, etc.
512double FloatingLiteral::getValueAsApproximateDouble() const {
513 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000514 bool ignored;
515 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
516 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000517 return V.convertToDouble();
518}
519
Chris Lattner2085fd62009-02-18 06:40:38 +0000520StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
521 unsigned ByteLength, bool Wide,
Anders Carlsson3e2193c2011-04-14 00:40:03 +0000522 bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000523 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000524 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000525 // Allocate enough space for the StringLiteral plus an array of locations for
526 // any concatenated string tokens.
527 void *Mem = C.Allocate(sizeof(StringLiteral)+
528 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000529 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000530 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000531
Reid Spencer5f016e22007-07-11 17:01:13 +0000532 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000533 char *AStrData = new (C, 1) char[ByteLength];
534 memcpy(AStrData, StrData, ByteLength);
535 SL->StrData = AStrData;
536 SL->ByteLength = ByteLength;
537 SL->IsWide = Wide;
Anders Carlsson3e2193c2011-04-14 00:40:03 +0000538 SL->IsPascal = Pascal;
Chris Lattner2085fd62009-02-18 06:40:38 +0000539 SL->TokLocs[0] = Loc[0];
540 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000541
Chris Lattner726e1682009-02-18 05:49:11 +0000542 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000543 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
544 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000545}
546
Douglas Gregor673ecd62009-04-15 16:35:07 +0000547StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
548 void *Mem = C.Allocate(sizeof(StringLiteral)+
549 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000550 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000551 StringLiteral *SL = new (Mem) StringLiteral(QualType());
552 SL->StrData = 0;
553 SL->ByteLength = 0;
554 SL->NumConcatenated = NumStrs;
555 return SL;
556}
557
Daniel Dunbarb6480232009-09-22 03:27:33 +0000558void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbarb6480232009-09-22 03:27:33 +0000559 char *AStrData = new (C, 1) char[Str.size()];
560 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000561 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000562 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000563}
564
Chris Lattner08f92e32010-11-17 07:37:15 +0000565/// getLocationOfByte - Return a source location that points to the specified
566/// byte of this string literal.
567///
568/// Strings are amazingly complex. They can be formed from multiple tokens and
569/// can have escape sequences in them in addition to the usual trigraph and
570/// escaped newline business. This routine handles this complexity.
571///
572SourceLocation StringLiteral::
573getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
574 const LangOptions &Features, const TargetInfo &Target) const {
575 assert(!isWide() && "This doesn't work for wide strings yet");
576
577 // Loop over all of the tokens in this string until we find the one that
578 // contains the byte we're looking for.
579 unsigned TokNo = 0;
580 while (1) {
581 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
582 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
583
584 // Get the spelling of the string so that we can get the data that makes up
585 // the string literal, not the identifier for the macro it is potentially
586 // expanded through.
587 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
588
589 // Re-lex the token to get its length and original spelling.
590 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
591 bool Invalid = false;
592 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
593 if (Invalid)
594 return StrTokSpellingLoc;
595
596 const char *StrData = Buffer.data()+LocInfo.second;
597
598 // Create a langops struct and enable trigraphs. This is sufficient for
599 // relexing tokens.
600 LangOptions LangOpts;
601 LangOpts.Trigraphs = true;
602
603 // Create a lexer starting at the beginning of this token.
604 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
605 Buffer.end());
606 Token TheTok;
607 TheLexer.LexFromRawLexer(TheTok);
608
609 // Use the StringLiteralParser to compute the length of the string in bytes.
610 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
611 unsigned TokNumBytes = SLP.GetStringLength();
612
613 // If the byte is in this token, return the location of the byte.
614 if (ByteNo < TokNumBytes ||
615 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
616 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
617
618 // Now that we know the offset of the token in the spelling, use the
619 // preprocessor to get the offset in the original source.
620 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
621 }
622
623 // Move to the next string token.
624 ++TokNo;
625 ByteNo -= TokNumBytes;
626 }
627}
628
629
630
Reid Spencer5f016e22007-07-11 17:01:13 +0000631/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
632/// corresponds to, e.g. "sizeof" or "[pre]++".
633const char *UnaryOperator::getOpcodeStr(Opcode Op) {
634 switch (Op) {
635 default: assert(0 && "Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000636 case UO_PostInc: return "++";
637 case UO_PostDec: return "--";
638 case UO_PreInc: return "++";
639 case UO_PreDec: return "--";
640 case UO_AddrOf: return "&";
641 case UO_Deref: return "*";
642 case UO_Plus: return "+";
643 case UO_Minus: return "-";
644 case UO_Not: return "~";
645 case UO_LNot: return "!";
646 case UO_Real: return "__real";
647 case UO_Imag: return "__imag";
648 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000649 }
650}
651
John McCall2de56d12010-08-25 11:45:40 +0000652UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000653UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
654 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000655 default: assert(false && "No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000656 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
657 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
658 case OO_Amp: return UO_AddrOf;
659 case OO_Star: return UO_Deref;
660 case OO_Plus: return UO_Plus;
661 case OO_Minus: return UO_Minus;
662 case OO_Tilde: return UO_Not;
663 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000664 }
665}
666
667OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
668 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000669 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
670 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
671 case UO_AddrOf: return OO_Amp;
672 case UO_Deref: return OO_Star;
673 case UO_Plus: return OO_Plus;
674 case UO_Minus: return OO_Minus;
675 case UO_Not: return OO_Tilde;
676 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000677 default: return OO_None;
678 }
679}
680
681
Reid Spencer5f016e22007-07-11 17:01:13 +0000682//===----------------------------------------------------------------------===//
683// Postfix Operators.
684//===----------------------------------------------------------------------===//
685
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000686CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
687 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000688 SourceLocation rparenloc)
689 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000690 fn->isTypeDependent(),
691 fn->isValueDependent(),
692 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000693 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000695 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000696 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000697 for (unsigned i = 0; i != numargs; ++i) {
698 if (args[i]->isTypeDependent())
699 ExprBits.TypeDependent = true;
700 if (args[i]->isValueDependent())
701 ExprBits.ValueDependent = true;
702 if (args[i]->containsUnexpandedParameterPack())
703 ExprBits.ContainsUnexpandedParameterPack = true;
704
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000705 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000706 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000707
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000708 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000709 RParenLoc = rparenloc;
710}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000711
Ted Kremenek668bf912009-02-09 20:51:47 +0000712CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000713 QualType t, ExprValueKind VK, SourceLocation rparenloc)
714 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000715 fn->isTypeDependent(),
716 fn->isValueDependent(),
717 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000718 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000719
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000720 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000721 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000722 for (unsigned i = 0; i != numargs; ++i) {
723 if (args[i]->isTypeDependent())
724 ExprBits.TypeDependent = true;
725 if (args[i]->isValueDependent())
726 ExprBits.ValueDependent = true;
727 if (args[i]->containsUnexpandedParameterPack())
728 ExprBits.ContainsUnexpandedParameterPack = true;
729
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000730 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000731 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000732
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000733 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000734 RParenLoc = rparenloc;
735}
736
Mike Stump1eb44332009-09-09 15:08:12 +0000737CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
738 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000739 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000740 SubExprs = new (C) Stmt*[PREARGS_START];
741 CallExprBits.NumPreArgs = 0;
742}
743
744CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
745 EmptyShell Empty)
746 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
747 // FIXME: Why do we allocate this?
748 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
749 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000750}
751
Nuno Lopesd20254f2009-12-20 23:11:08 +0000752Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000753 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl20012152010-09-10 20:55:30 +0000754 // If we're calling a dereference, look at the pointer instead.
755 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
756 if (BO->isPtrMemOp())
757 CEE = BO->getRHS()->IgnoreParenCasts();
758 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
759 if (UO->getOpcode() == UO_Deref)
760 CEE = UO->getSubExpr()->IgnoreParenCasts();
761 }
Chris Lattner6346f962009-07-17 15:46:27 +0000762 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000763 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000764 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
765 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000766
767 return 0;
768}
769
Nuno Lopesd20254f2009-12-20 23:11:08 +0000770FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000771 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000772}
773
Chris Lattnerd18b3292007-12-28 05:25:02 +0000774/// setNumArgs - This changes the number of arguments present in this call.
775/// Any orphaned expressions are deleted by this, and any new operands are set
776/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000777void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000778 // No change, just return.
779 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000780
Chris Lattnerd18b3292007-12-28 05:25:02 +0000781 // If shrinking # arguments, just delete the extras and forgot them.
782 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000783 this->NumArgs = NumArgs;
784 return;
785 }
786
787 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000788 unsigned NumPreArgs = getNumPreArgs();
789 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000790 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000791 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000792 NewSubExprs[i] = SubExprs[i];
793 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000794 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
795 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000796 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Douglas Gregor88c9a462009-04-17 21:46:47 +0000798 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000799 SubExprs = NewSubExprs;
800 this->NumArgs = NumArgs;
801}
802
Chris Lattnercb888962008-10-06 05:00:53 +0000803/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
804/// not, return 0.
Jay Foad4ba2a172011-01-12 09:06:06 +0000805unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000806 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000807 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000808 // ImplicitCastExpr.
809 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
810 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000811 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000813 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
814 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000815 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Anders Carlssonbcba2012008-01-31 02:13:57 +0000817 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
818 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000819 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000821 if (!FDecl->getIdentifier())
822 return 0;
823
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000824 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000825}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000826
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000827QualType CallExpr::getCallReturnType() const {
828 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000829 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000830 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000831 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000832 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000833 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
834 // This should never be overloaded and so should never return null.
835 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000836
John McCall864c0412011-04-26 20:42:42 +0000837 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000838 return FnType->getResultType();
839}
Chris Lattnercb888962008-10-06 05:00:53 +0000840
John McCall2882eca2011-02-21 06:23:05 +0000841SourceRange CallExpr::getSourceRange() const {
842 if (isa<CXXOperatorCallExpr>(this))
843 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
844
845 SourceLocation begin = getCallee()->getLocStart();
846 if (begin.isInvalid() && getNumArgs() > 0)
847 begin = getArg(0)->getLocStart();
848 SourceLocation end = getRParenLoc();
849 if (end.isInvalid() && getNumArgs() > 0)
850 end = getArg(getNumArgs() - 1)->getLocEnd();
851 return SourceRange(begin, end);
852}
853
Sean Huntc3021132010-05-05 15:23:54 +0000854OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000855 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000856 TypeSourceInfo *tsi,
857 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000858 Expr** exprsPtr, unsigned numExprs,
859 SourceLocation RParenLoc) {
860 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000861 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000862 sizeof(Expr*) * numExprs);
863
864 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
865 exprsPtr, numExprs, RParenLoc);
866}
867
868OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
869 unsigned numComps, unsigned numExprs) {
870 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
871 sizeof(OffsetOfNode) * numComps +
872 sizeof(Expr*) * numExprs);
873 return new (Mem) OffsetOfExpr(numComps, numExprs);
874}
875
Sean Huntc3021132010-05-05 15:23:54 +0000876OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000877 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000878 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000879 Expr** exprsPtr, unsigned numExprs,
880 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000881 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
882 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000883 /*ValueDependent=*/tsi->getType()->isDependentType(),
884 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000885 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
886 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000887{
888 for(unsigned i = 0; i < numComps; ++i) {
889 setComponent(i, compsPtr[i]);
890 }
Sean Huntc3021132010-05-05 15:23:54 +0000891
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000892 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000893 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
894 ExprBits.ValueDependent = true;
895 if (exprsPtr[i]->containsUnexpandedParameterPack())
896 ExprBits.ContainsUnexpandedParameterPack = true;
897
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000898 setIndexExpr(i, exprsPtr[i]);
899 }
900}
901
902IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
903 assert(getKind() == Field || getKind() == Identifier);
904 if (getKind() == Field)
905 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000906
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000907 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
908}
909
Mike Stump1eb44332009-09-09 15:08:12 +0000910MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000911 NestedNameSpecifierLoc QualifierLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000912 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000913 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000914 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000915 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000916 QualType ty,
917 ExprValueKind vk,
918 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000919 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000920
Douglas Gregor40d96a62011-02-28 21:54:11 +0000921 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000922 founddecl.getDecl() != memberdecl ||
923 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000924 if (hasQualOrFound)
925 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000926
John McCalld5532b62009-11-23 01:53:49 +0000927 if (targs)
928 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Chris Lattner32488542010-10-30 05:14:06 +0000930 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000931 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
932 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000933
934 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000935 // FIXME: Wrong. We should be looking at the member declaration we found.
936 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000937 E->setValueDependent(true);
938 E->setTypeDependent(true);
939 }
940 E->HasQualifierOrFoundDecl = true;
941
942 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000943 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000944 NQ->FoundDecl = founddecl;
945 }
946
947 if (targs) {
948 E->HasExplicitTemplateArgumentList = true;
John McCall096832c2010-08-19 23:49:38 +0000949 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall6bb80172010-03-30 21:47:33 +0000950 }
951
952 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000953}
954
Douglas Gregor75e85042011-03-02 21:06:53 +0000955SourceRange MemberExpr::getSourceRange() const {
956 SourceLocation StartLoc;
957 if (isImplicitAccess()) {
958 if (hasQualifier())
959 StartLoc = getQualifierLoc().getBeginLoc();
960 else
961 StartLoc = MemberLoc;
962 } else {
963 // FIXME: We don't want this to happen. Rather, we should be able to
964 // detect all kinds of implicit accesses more cleanly.
965 StartLoc = getBase()->getLocStart();
966 if (StartLoc.isInvalid())
967 StartLoc = MemberLoc;
968 }
969
970 SourceLocation EndLoc =
971 HasExplicitTemplateArgumentList? getRAngleLoc()
972 : getMemberNameInfo().getEndLoc();
973
974 return SourceRange(StartLoc, EndLoc);
975}
976
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000977const char *CastExpr::getCastKindName() const {
978 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000979 case CK_Dependent:
980 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +0000981 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000982 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +0000983 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +0000984 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +0000985 case CK_LValueToRValue:
986 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +0000987 case CK_GetObjCProperty:
988 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +0000989 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000990 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +0000991 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +0000992 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +0000993 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000994 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000995 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +0000996 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000997 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000998 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +0000999 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001000 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001001 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001002 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001003 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001004 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001005 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001006 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001007 case CK_NullToPointer:
1008 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001009 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001010 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001011 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001012 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001013 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001014 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001015 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001016 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001017 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001018 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001019 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001020 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001021 case CK_PointerToBoolean:
1022 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001023 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001024 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001025 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001026 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001027 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001028 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001029 case CK_IntegralToBoolean:
1030 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001031 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001032 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001033 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001034 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001035 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001036 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001037 case CK_FloatingToBoolean:
1038 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001039 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001040 return "MemberPointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001041 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +00001042 return "AnyPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001043 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001044 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001045 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001046 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001047 case CK_FloatingRealToComplex:
1048 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001049 case CK_FloatingComplexToReal:
1050 return "FloatingComplexToReal";
1051 case CK_FloatingComplexToBoolean:
1052 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001053 case CK_FloatingComplexCast:
1054 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001055 case CK_FloatingComplexToIntegralComplex:
1056 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001057 case CK_IntegralRealToComplex:
1058 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001059 case CK_IntegralComplexToReal:
1060 return "IntegralComplexToReal";
1061 case CK_IntegralComplexToBoolean:
1062 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001063 case CK_IntegralComplexCast:
1064 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001065 case CK_IntegralComplexToFloatingComplex:
1066 return "IntegralComplexToFloatingComplex";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001067 }
Mike Stump1eb44332009-09-09 15:08:12 +00001068
John McCall2bb5d002010-11-13 09:02:35 +00001069 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001070 return 0;
1071}
1072
Douglas Gregor6eef5192009-12-14 19:27:10 +00001073Expr *CastExpr::getSubExprAsWritten() {
1074 Expr *SubExpr = 0;
1075 CastExpr *E = this;
1076 do {
1077 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001078
Douglas Gregor6eef5192009-12-14 19:27:10 +00001079 // Skip any temporary bindings; they're implicit.
1080 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1081 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001082
Douglas Gregor6eef5192009-12-14 19:27:10 +00001083 // Conversions by constructor and conversion functions have a
1084 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001085 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001086 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001087 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001088 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001089
Douglas Gregor6eef5192009-12-14 19:27:10 +00001090 // If the subexpression we're left with is an implicit cast, look
1091 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001092 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1093
Douglas Gregor6eef5192009-12-14 19:27:10 +00001094 return SubExpr;
1095}
1096
John McCallf871d0c2010-08-07 06:22:56 +00001097CXXBaseSpecifier **CastExpr::path_buffer() {
1098 switch (getStmtClass()) {
1099#define ABSTRACT_STMT(x)
1100#define CASTEXPR(Type, Base) \
1101 case Stmt::Type##Class: \
1102 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1103#define STMT(Type, Base)
1104#include "clang/AST/StmtNodes.inc"
1105 default:
1106 llvm_unreachable("non-cast expressions not possible here");
1107 return 0;
1108 }
1109}
1110
1111void CastExpr::setCastPath(const CXXCastPath &Path) {
1112 assert(Path.size() == path_size());
1113 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1114}
1115
1116ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1117 CastKind Kind, Expr *Operand,
1118 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001119 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001120 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1121 void *Buffer =
1122 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1123 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001124 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001125 if (PathSize) E->setCastPath(*BasePath);
1126 return E;
1127}
1128
1129ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1130 unsigned PathSize) {
1131 void *Buffer =
1132 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1133 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1134}
1135
1136
1137CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001138 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001139 const CXXCastPath *BasePath,
1140 TypeSourceInfo *WrittenTy,
1141 SourceLocation L, SourceLocation R) {
1142 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1143 void *Buffer =
1144 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1145 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001146 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001147 if (PathSize) E->setCastPath(*BasePath);
1148 return E;
1149}
1150
1151CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1152 void *Buffer =
1153 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1154 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1155}
1156
Reid Spencer5f016e22007-07-11 17:01:13 +00001157/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1158/// corresponds to, e.g. "<<=".
1159const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1160 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001161 case BO_PtrMemD: return ".*";
1162 case BO_PtrMemI: return "->*";
1163 case BO_Mul: return "*";
1164 case BO_Div: return "/";
1165 case BO_Rem: return "%";
1166 case BO_Add: return "+";
1167 case BO_Sub: return "-";
1168 case BO_Shl: return "<<";
1169 case BO_Shr: return ">>";
1170 case BO_LT: return "<";
1171 case BO_GT: return ">";
1172 case BO_LE: return "<=";
1173 case BO_GE: return ">=";
1174 case BO_EQ: return "==";
1175 case BO_NE: return "!=";
1176 case BO_And: return "&";
1177 case BO_Xor: return "^";
1178 case BO_Or: return "|";
1179 case BO_LAnd: return "&&";
1180 case BO_LOr: return "||";
1181 case BO_Assign: return "=";
1182 case BO_MulAssign: return "*=";
1183 case BO_DivAssign: return "/=";
1184 case BO_RemAssign: return "%=";
1185 case BO_AddAssign: return "+=";
1186 case BO_SubAssign: return "-=";
1187 case BO_ShlAssign: return "<<=";
1188 case BO_ShrAssign: return ">>=";
1189 case BO_AndAssign: return "&=";
1190 case BO_XorAssign: return "^=";
1191 case BO_OrAssign: return "|=";
1192 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001194
1195 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001196}
1197
John McCall2de56d12010-08-25 11:45:40 +00001198BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001199BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1200 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001201 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001202 case OO_Plus: return BO_Add;
1203 case OO_Minus: return BO_Sub;
1204 case OO_Star: return BO_Mul;
1205 case OO_Slash: return BO_Div;
1206 case OO_Percent: return BO_Rem;
1207 case OO_Caret: return BO_Xor;
1208 case OO_Amp: return BO_And;
1209 case OO_Pipe: return BO_Or;
1210 case OO_Equal: return BO_Assign;
1211 case OO_Less: return BO_LT;
1212 case OO_Greater: return BO_GT;
1213 case OO_PlusEqual: return BO_AddAssign;
1214 case OO_MinusEqual: return BO_SubAssign;
1215 case OO_StarEqual: return BO_MulAssign;
1216 case OO_SlashEqual: return BO_DivAssign;
1217 case OO_PercentEqual: return BO_RemAssign;
1218 case OO_CaretEqual: return BO_XorAssign;
1219 case OO_AmpEqual: return BO_AndAssign;
1220 case OO_PipeEqual: return BO_OrAssign;
1221 case OO_LessLess: return BO_Shl;
1222 case OO_GreaterGreater: return BO_Shr;
1223 case OO_LessLessEqual: return BO_ShlAssign;
1224 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1225 case OO_EqualEqual: return BO_EQ;
1226 case OO_ExclaimEqual: return BO_NE;
1227 case OO_LessEqual: return BO_LE;
1228 case OO_GreaterEqual: return BO_GE;
1229 case OO_AmpAmp: return BO_LAnd;
1230 case OO_PipePipe: return BO_LOr;
1231 case OO_Comma: return BO_Comma;
1232 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001233 }
1234}
1235
1236OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1237 static const OverloadedOperatorKind OverOps[] = {
1238 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1239 OO_Star, OO_Slash, OO_Percent,
1240 OO_Plus, OO_Minus,
1241 OO_LessLess, OO_GreaterGreater,
1242 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1243 OO_EqualEqual, OO_ExclaimEqual,
1244 OO_Amp,
1245 OO_Caret,
1246 OO_Pipe,
1247 OO_AmpAmp,
1248 OO_PipePipe,
1249 OO_Equal, OO_StarEqual,
1250 OO_SlashEqual, OO_PercentEqual,
1251 OO_PlusEqual, OO_MinusEqual,
1252 OO_LessLessEqual, OO_GreaterGreaterEqual,
1253 OO_AmpEqual, OO_CaretEqual,
1254 OO_PipeEqual,
1255 OO_Comma
1256 };
1257 return OverOps[Opc];
1258}
1259
Ted Kremenek709210f2010-04-13 23:39:13 +00001260InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001261 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001262 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001263 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1264 false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001265 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001266 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +00001267 HadArrayRangeDesignator(false)
Sean Huntc3021132010-05-05 15:23:54 +00001268{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001269 for (unsigned I = 0; I != numInits; ++I) {
1270 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001271 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001272 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001273 ExprBits.ValueDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001274 if (initExprs[I]->containsUnexpandedParameterPack())
1275 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001276 }
Sean Huntc3021132010-05-05 15:23:54 +00001277
Ted Kremenek709210f2010-04-13 23:39:13 +00001278 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001279}
Reid Spencer5f016e22007-07-11 17:01:13 +00001280
Ted Kremenek709210f2010-04-13 23:39:13 +00001281void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001282 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001283 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001284}
1285
Ted Kremenek709210f2010-04-13 23:39:13 +00001286void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001287 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001288}
1289
Ted Kremenek709210f2010-04-13 23:39:13 +00001290Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001291 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001292 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001293 InitExprs.back() = expr;
1294 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001295 }
Mike Stump1eb44332009-09-09 15:08:12 +00001296
Douglas Gregor4c678342009-01-28 21:54:33 +00001297 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1298 InitExprs[Init] = expr;
1299 return Result;
1300}
1301
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001302void InitListExpr::setArrayFiller(Expr *filler) {
1303 ArrayFillerOrUnionFieldInit = filler;
1304 // Fill out any "holes" in the array due to designated initializers.
1305 Expr **inits = getInits();
1306 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1307 if (inits[i] == 0)
1308 inits[i] = filler;
1309}
1310
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001311SourceRange InitListExpr::getSourceRange() const {
1312 if (SyntacticForm)
1313 return SyntacticForm->getSourceRange();
1314 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1315 if (Beg.isInvalid()) {
1316 // Find the first non-null initializer.
1317 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1318 E = InitExprs.end();
1319 I != E; ++I) {
1320 if (Stmt *S = *I) {
1321 Beg = S->getLocStart();
1322 break;
1323 }
1324 }
1325 }
1326 if (End.isInvalid()) {
1327 // Find the first non-null initializer from the end.
1328 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1329 E = InitExprs.rend();
1330 I != E; ++I) {
1331 if (Stmt *S = *I) {
1332 End = S->getSourceRange().getEnd();
1333 break;
1334 }
1335 }
1336 }
1337 return SourceRange(Beg, End);
1338}
1339
Steve Naroffbfdcae62008-09-04 15:31:07 +00001340/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001341///
1342const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001343 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001344 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001345}
1346
Mike Stump1eb44332009-09-09 15:08:12 +00001347SourceLocation BlockExpr::getCaretLocation() const {
1348 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001349}
Mike Stump1eb44332009-09-09 15:08:12 +00001350const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001351 return TheBlock->getBody();
1352}
Mike Stump1eb44332009-09-09 15:08:12 +00001353Stmt *BlockExpr::getBody() {
1354 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001355}
Steve Naroff56ee6892008-10-08 17:01:13 +00001356
1357
Reid Spencer5f016e22007-07-11 17:01:13 +00001358//===----------------------------------------------------------------------===//
1359// Generic Expression Routines
1360//===----------------------------------------------------------------------===//
1361
Chris Lattner026dc962009-02-14 07:37:35 +00001362/// isUnusedResultAWarning - Return true if this immediate expression should
1363/// be warned about if the result is unused. If so, fill in Loc and Ranges
1364/// with location to warn on and the source range[s] to report with the
1365/// warning.
1366bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001367 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001368 // Don't warn if the expr is type dependent. The type could end up
1369 // instantiating to void.
1370 if (isTypeDependent())
1371 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Reid Spencer5f016e22007-07-11 17:01:13 +00001373 switch (getStmtClass()) {
1374 default:
John McCall0faede62010-03-12 07:11:26 +00001375 if (getType()->isVoidType())
1376 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001377 Loc = getExprLoc();
1378 R1 = getSourceRange();
1379 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001380 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001381 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001382 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001383 case GenericSelectionExprClass:
1384 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1385 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 case UnaryOperatorClass: {
1387 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001390 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001391 case UO_PostInc:
1392 case UO_PostDec:
1393 case UO_PreInc:
1394 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001395 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001396 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001397 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001398 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001399 return false;
1400 break;
John McCall2de56d12010-08-25 11:45:40 +00001401 case UO_Real:
1402 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001403 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001404 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1405 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001406 return false;
1407 break;
John McCall2de56d12010-08-25 11:45:40 +00001408 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001409 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001410 }
Chris Lattner026dc962009-02-14 07:37:35 +00001411 Loc = UO->getOperatorLoc();
1412 R1 = UO->getSubExpr()->getSourceRange();
1413 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001415 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001416 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001417 switch (BO->getOpcode()) {
1418 default:
1419 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001420 // Consider the RHS of comma for side effects. LHS was checked by
1421 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001422 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001423 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1424 // lvalue-ness) of an assignment written in a macro.
1425 if (IntegerLiteral *IE =
1426 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1427 if (IE->getValue() == 0)
1428 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001429 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1430 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001431 case BO_LAnd:
1432 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001433 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1434 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1435 return false;
1436 break;
John McCallbf0ee352010-02-16 04:10:53 +00001437 }
Chris Lattner026dc962009-02-14 07:37:35 +00001438 if (BO->isAssignmentOp())
1439 return false;
1440 Loc = BO->getOperatorLoc();
1441 R1 = BO->getLHS()->getSourceRange();
1442 R2 = BO->getRHS()->getSourceRange();
1443 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001444 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001445 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001446 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001447 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001448
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001449 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001450 // If only one of the LHS or RHS is a warning, the operator might
1451 // be being used for control flow. Only warn if both the LHS and
1452 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001453 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001454 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1455 return false;
1456 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001457 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001458 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001459 }
1460
Reid Spencer5f016e22007-07-11 17:01:13 +00001461 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001462 // If the base pointer or element is to a volatile pointer/field, accessing
1463 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001464 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001465 return false;
1466 Loc = cast<MemberExpr>(this)->getMemberLoc();
1467 R1 = SourceRange(Loc, Loc);
1468 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1469 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Reid Spencer5f016e22007-07-11 17:01:13 +00001471 case ArraySubscriptExprClass:
1472 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001473 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001474 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001475 return false;
1476 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1477 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1478 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1479 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001480
Reid Spencer5f016e22007-07-11 17:01:13 +00001481 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001482 case CXXOperatorCallExprClass:
1483 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001484 // If this is a direct call, get the callee.
1485 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001486 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001487 // If the callee has attribute pure, const, or warn_unused_result, warn
1488 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001489 //
1490 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1491 // updated to match for QoI.
1492 if (FD->getAttr<WarnUnusedResultAttr>() ||
1493 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1494 Loc = CE->getCallee()->getLocStart();
1495 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001497 if (unsigned NumArgs = CE->getNumArgs())
1498 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1499 CE->getArg(NumArgs-1)->getLocEnd());
1500 return true;
1501 }
Chris Lattner026dc962009-02-14 07:37:35 +00001502 }
1503 return false;
1504 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001505
1506 case CXXTemporaryObjectExprClass:
1507 case CXXConstructExprClass:
1508 return false;
1509
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001510 case ObjCMessageExprClass: {
1511 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1512 const ObjCMethodDecl *MD = ME->getMethodDecl();
1513 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1514 Loc = getExprLoc();
1515 return true;
1516 }
Chris Lattner026dc962009-02-14 07:37:35 +00001517 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001518 }
Mike Stump1eb44332009-09-09 15:08:12 +00001519
John McCall12f78a62010-12-02 01:19:52 +00001520 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001521 Loc = getExprLoc();
1522 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001523 return true;
John McCall12f78a62010-12-02 01:19:52 +00001524
Chris Lattner611b2ec2008-07-26 19:51:01 +00001525 case StmtExprClass: {
1526 // Statement exprs don't logically have side effects themselves, but are
1527 // sometimes used in macros in ways that give them a type that is unused.
1528 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1529 // however, if the result of the stmt expr is dead, we don't want to emit a
1530 // warning.
1531 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001532 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001533 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001534 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001535 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1536 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1537 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1538 }
Mike Stump1eb44332009-09-09 15:08:12 +00001539
John McCall0faede62010-03-12 07:11:26 +00001540 if (getType()->isVoidType())
1541 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001542 Loc = cast<StmtExpr>(this)->getLParenLoc();
1543 R1 = getSourceRange();
1544 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001545 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001546 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001547 // If this is an explicit cast to void, allow it. People do this when they
1548 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001549 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001550 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001551 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1552 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1553 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001554 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001555 if (getType()->isVoidType())
1556 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001557 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001558
Anders Carlsson58beed92009-11-17 17:11:23 +00001559 // If this is a cast to void or a constructor conversion, check the operand.
1560 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001561 if (CE->getCastKind() == CK_ToVoid ||
1562 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001563 return (cast<CastExpr>(this)->getSubExpr()
1564 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001565 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1566 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1567 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001568 }
Mike Stump1eb44332009-09-09 15:08:12 +00001569
Eli Friedman4be1f472008-05-19 21:24:43 +00001570 case ImplicitCastExprClass:
1571 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001572 return (cast<ImplicitCastExpr>(this)
1573 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001574
Chris Lattner04421082008-04-08 04:40:51 +00001575 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001576 return (cast<CXXDefaultArgExpr>(this)
1577 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001578
1579 case CXXNewExprClass:
1580 // FIXME: In theory, there might be new expressions that don't have side
1581 // effects (e.g. a placement new with an uninitialized POD).
1582 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001583 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001584 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001585 return (cast<CXXBindTemporaryExpr>(this)
1586 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001587 case ExprWithCleanupsClass:
1588 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001589 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001590 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001591}
1592
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001593/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001594/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001595bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001596 const Expr *E = IgnoreParens();
1597 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001598 default:
1599 return false;
1600 case ObjCIvarRefExprClass:
1601 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001602 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001603 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001604 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001605 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001606 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001607 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001608 case DeclRefExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001609 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001610 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1611 if (VD->hasGlobalStorage())
1612 return true;
1613 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001614 // dereferencing to a pointer is always a gc'able candidate,
1615 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001616 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001617 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001618 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001619 return false;
1620 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001621 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001622 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001623 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001624 }
1625 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001626 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001627 }
1628}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001629
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001630bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1631 if (isTypeDependent())
1632 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001633 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001634}
1635
John McCall864c0412011-04-26 20:42:42 +00001636QualType Expr::findBoundMemberType(const Expr *expr) {
1637 assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1638
1639 // Bound member expressions are always one of these possibilities:
1640 // x->m x.m x->*y x.*y
1641 // (possibly parenthesized)
1642
1643 expr = expr->IgnoreParens();
1644 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1645 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1646 return mem->getMemberDecl()->getType();
1647 }
1648
1649 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1650 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1651 ->getPointeeType();
1652 assert(type->isFunctionType());
1653 return type;
1654 }
1655
1656 assert(isa<UnresolvedMemberExpr>(expr));
1657 return QualType();
1658}
1659
Sebastian Redl369e51f2010-09-10 20:55:33 +00001660static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1661 Expr::CanThrowResult CT2) {
1662 // CanThrowResult constants are ordered so that the maximum is the correct
1663 // merge result.
1664 return CT1 > CT2 ? CT1 : CT2;
1665}
1666
1667static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1668 Expr *E = const_cast<Expr*>(CE);
1669 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001670 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001671 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1672 }
1673 return R;
1674}
1675
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001676static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001677 bool NullThrows = true) {
1678 if (!D)
1679 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1680
1681 // See if we can get a function type from the decl somehow.
1682 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1683 if (!VD) // If we have no clue what we're calling, assume the worst.
1684 return Expr::CT_Can;
1685
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001686 // As an extension, we assume that __attribute__((nothrow)) functions don't
1687 // throw.
1688 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1689 return Expr::CT_Cannot;
1690
Sebastian Redl369e51f2010-09-10 20:55:33 +00001691 QualType T = VD->getType();
1692 const FunctionProtoType *FT;
1693 if ((FT = T->getAs<FunctionProtoType>())) {
1694 } else if (const PointerType *PT = T->getAs<PointerType>())
1695 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1696 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1697 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1698 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1699 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1700 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1701 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1702
1703 if (!FT)
1704 return Expr::CT_Can;
1705
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001706 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001707}
1708
1709static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1710 if (DC->isTypeDependent())
1711 return Expr::CT_Dependent;
1712
Sebastian Redl295995c2010-09-10 20:55:47 +00001713 if (!DC->getTypeAsWritten()->isReferenceType())
1714 return Expr::CT_Cannot;
1715
Sebastian Redl369e51f2010-09-10 20:55:33 +00001716 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1717}
1718
1719static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1720 const CXXTypeidExpr *DC) {
1721 if (DC->isTypeOperand())
1722 return Expr::CT_Cannot;
1723
1724 Expr *Op = DC->getExprOperand();
1725 if (Op->isTypeDependent())
1726 return Expr::CT_Dependent;
1727
1728 const RecordType *RT = Op->getType()->getAs<RecordType>();
1729 if (!RT)
1730 return Expr::CT_Cannot;
1731
1732 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1733 return Expr::CT_Cannot;
1734
1735 if (Op->Classify(C).isPRValue())
1736 return Expr::CT_Cannot;
1737
1738 return Expr::CT_Can;
1739}
1740
1741Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1742 // C++ [expr.unary.noexcept]p3:
1743 // [Can throw] if in a potentially-evaluated context the expression would
1744 // contain:
1745 switch (getStmtClass()) {
1746 case CXXThrowExprClass:
1747 // - a potentially evaluated throw-expression
1748 return CT_Can;
1749
1750 case CXXDynamicCastExprClass: {
1751 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1752 // where T is a reference type, that requires a run-time check
1753 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1754 if (CT == CT_Can)
1755 return CT;
1756 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1757 }
1758
1759 case CXXTypeidExprClass:
1760 // - a potentially evaluated typeid expression applied to a glvalue
1761 // expression whose type is a polymorphic class type
1762 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1763
1764 // - a potentially evaluated call to a function, member function, function
1765 // pointer, or member function pointer that does not have a non-throwing
1766 // exception-specification
1767 case CallExprClass:
1768 case CXXOperatorCallExprClass:
1769 case CXXMemberCallExprClass: {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001770 CanThrowResult CT = CanCalleeThrow(C,cast<CallExpr>(this)->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001771 if (CT == CT_Can)
1772 return CT;
1773 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1774 }
1775
Sebastian Redl295995c2010-09-10 20:55:47 +00001776 case CXXConstructExprClass:
1777 case CXXTemporaryObjectExprClass: {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001778 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001779 cast<CXXConstructExpr>(this)->getConstructor());
1780 if (CT == CT_Can)
1781 return CT;
1782 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1783 }
1784
1785 case CXXNewExprClass: {
1786 CanThrowResult CT = MergeCanThrow(
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001787 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getOperatorNew()),
1788 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00001789 /*NullThrows*/false));
1790 if (CT == CT_Can)
1791 return CT;
1792 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1793 }
1794
1795 case CXXDeleteExprClass: {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001796 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001797 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1798 if (CT == CT_Can)
1799 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001800 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1801 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1802 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1803 Arg = Cast->getSubExpr();
1804 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1805 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001806 CanThrowResult CT2 = CanCalleeThrow(C,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001807 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1808 if (CT2 == CT_Can)
1809 return CT2;
1810 CT = MergeCanThrow(CT, CT2);
1811 }
1812 }
1813 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1814 }
1815
1816 case CXXBindTemporaryExprClass: {
1817 // The bound temporary has to be destroyed again, which might throw.
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001818 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001819 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1820 if (CT == CT_Can)
1821 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001822 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1823 }
1824
1825 // ObjC message sends are like function calls, but never have exception
1826 // specs.
1827 case ObjCMessageExprClass:
1828 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001829 return CT_Can;
1830
1831 // Many other things have subexpressions, so we have to test those.
1832 // Some are simple:
1833 case ParenExprClass:
1834 case MemberExprClass:
1835 case CXXReinterpretCastExprClass:
1836 case CXXConstCastExprClass:
1837 case ConditionalOperatorClass:
1838 case CompoundLiteralExprClass:
1839 case ExtVectorElementExprClass:
1840 case InitListExprClass:
1841 case DesignatedInitExprClass:
1842 case ParenListExprClass:
1843 case VAArgExprClass:
1844 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00001845 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001846 case ObjCIvarRefExprClass:
1847 case ObjCIsaExprClass:
1848 case ShuffleVectorExprClass:
1849 return CanSubExprsThrow(C, this);
1850
1851 // Some might be dependent for other reasons.
1852 case UnaryOperatorClass:
1853 case ArraySubscriptExprClass:
1854 case ImplicitCastExprClass:
1855 case CStyleCastExprClass:
1856 case CXXStaticCastExprClass:
1857 case CXXFunctionalCastExprClass:
1858 case BinaryOperatorClass:
1859 case CompoundAssignOperatorClass: {
1860 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1861 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1862 }
1863
1864 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1865 case StmtExprClass:
1866 return CT_Can;
1867
1868 case ChooseExprClass:
1869 if (isTypeDependent() || isValueDependent())
1870 return CT_Dependent;
1871 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1872
Peter Collingbournef111d932011-04-15 00:35:48 +00001873 case GenericSelectionExprClass:
1874 if (cast<GenericSelectionExpr>(this)->isResultDependent())
1875 return CT_Dependent;
1876 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
1877
Sebastian Redl369e51f2010-09-10 20:55:33 +00001878 // Some expressions are always dependent.
1879 case DependentScopeDeclRefExprClass:
1880 case CXXUnresolvedConstructExprClass:
1881 case CXXDependentScopeMemberExprClass:
1882 return CT_Dependent;
1883
1884 default:
1885 // All other expressions don't have subexpressions, or else they are
1886 // unevaluated.
1887 return CT_Cannot;
1888 }
1889}
1890
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001891Expr* Expr::IgnoreParens() {
1892 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001893 while (true) {
1894 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1895 E = P->getSubExpr();
1896 continue;
1897 }
1898 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1899 if (P->getOpcode() == UO_Extension) {
1900 E = P->getSubExpr();
1901 continue;
1902 }
1903 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001904 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1905 if (!P->isResultDependent()) {
1906 E = P->getResultExpr();
1907 continue;
1908 }
1909 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001910 return E;
1911 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001912}
1913
Chris Lattner56f34942008-02-13 01:02:39 +00001914/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1915/// or CastExprs or ImplicitCastExprs, returning their operand.
1916Expr *Expr::IgnoreParenCasts() {
1917 Expr *E = this;
1918 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001919 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001920 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001921 continue;
1922 }
1923 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001924 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001925 continue;
1926 }
1927 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1928 if (P->getOpcode() == UO_Extension) {
1929 E = P->getSubExpr();
1930 continue;
1931 }
1932 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001933 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1934 if (!P->isResultDependent()) {
1935 E = P->getResultExpr();
1936 continue;
1937 }
1938 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001939 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001940 }
1941}
1942
John McCall9c5d70c2010-12-04 08:24:19 +00001943/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1944/// casts. This is intended purely as a temporary workaround for code
1945/// that hasn't yet been rewritten to do the right thing about those
1946/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00001947Expr *Expr::IgnoreParenLValueCasts() {
1948 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00001949 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00001950 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1951 E = P->getSubExpr();
1952 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00001953 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00001954 if (P->getCastKind() == CK_LValueToRValue) {
1955 E = P->getSubExpr();
1956 continue;
1957 }
John McCall9c5d70c2010-12-04 08:24:19 +00001958 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1959 if (P->getOpcode() == UO_Extension) {
1960 E = P->getSubExpr();
1961 continue;
1962 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001963 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1964 if (!P->isResultDependent()) {
1965 E = P->getResultExpr();
1966 continue;
1967 }
John McCallf6a16482010-12-04 03:47:34 +00001968 }
1969 break;
1970 }
1971 return E;
1972}
1973
John McCall2fc46bf2010-05-05 22:59:52 +00001974Expr *Expr::IgnoreParenImpCasts() {
1975 Expr *E = this;
1976 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001977 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001978 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001979 continue;
1980 }
1981 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001982 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001983 continue;
1984 }
1985 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1986 if (P->getOpcode() == UO_Extension) {
1987 E = P->getSubExpr();
1988 continue;
1989 }
1990 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001991 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1992 if (!P->isResultDependent()) {
1993 E = P->getResultExpr();
1994 continue;
1995 }
1996 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001997 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00001998 }
1999}
2000
Chris Lattnerecdd8412009-03-13 17:28:01 +00002001/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2002/// value (including ptr->int casts of the same size). Strip off any
2003/// ParenExpr or CastExprs, returning their operand.
2004Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2005 Expr *E = this;
2006 while (true) {
2007 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2008 E = P->getSubExpr();
2009 continue;
2010 }
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Chris Lattnerecdd8412009-03-13 17:28:01 +00002012 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2013 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002014 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002015 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002016
Chris Lattnerecdd8412009-03-13 17:28:01 +00002017 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2018 E = SE;
2019 continue;
2020 }
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002022 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002023 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002024 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002025 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002026 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2027 E = SE;
2028 continue;
2029 }
2030 }
Mike Stump1eb44332009-09-09 15:08:12 +00002031
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002032 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2033 if (P->getOpcode() == UO_Extension) {
2034 E = P->getSubExpr();
2035 continue;
2036 }
2037 }
2038
Peter Collingbournef111d932011-04-15 00:35:48 +00002039 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2040 if (!P->isResultDependent()) {
2041 E = P->getResultExpr();
2042 continue;
2043 }
2044 }
2045
Chris Lattnerecdd8412009-03-13 17:28:01 +00002046 return E;
2047 }
2048}
2049
Douglas Gregor6eef5192009-12-14 19:27:10 +00002050bool Expr::isDefaultArgument() const {
2051 const Expr *E = this;
2052 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2053 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002054
Douglas Gregor6eef5192009-12-14 19:27:10 +00002055 return isa<CXXDefaultArgExpr>(E);
2056}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002057
Douglas Gregor2f599792010-04-02 18:24:57 +00002058/// \brief Skip over any no-op casts and any temporary-binding
2059/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002060static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor2f599792010-04-02 18:24:57 +00002061 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002062 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002063 E = ICE->getSubExpr();
2064 else
2065 break;
2066 }
2067
2068 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2069 E = BE->getSubExpr();
2070
2071 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002072 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002073 E = ICE->getSubExpr();
2074 else
2075 break;
2076 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002077
2078 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002079}
2080
John McCall558d2ab2010-09-15 10:14:12 +00002081/// isTemporaryObject - Determines if this expression produces a
2082/// temporary of the given class type.
2083bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2084 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2085 return false;
2086
Anders Carlssonf8b30152010-11-28 16:40:49 +00002087 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002088
John McCall58277b52010-09-15 20:59:13 +00002089 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002090 if (!E->Classify(C).isPRValue()) {
2091 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002092 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002093 return false;
2094 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002095
John McCall19e60ad2010-09-16 06:57:56 +00002096 // Black-list a few cases which yield pr-values of class type that don't
2097 // refer to temporaries of that type:
2098
2099 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002100 if (isa<ImplicitCastExpr>(E)) {
2101 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2102 case CK_DerivedToBase:
2103 case CK_UncheckedDerivedToBase:
2104 return false;
2105 default:
2106 break;
2107 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002108 }
2109
John McCall19e60ad2010-09-16 06:57:56 +00002110 // - member expressions (all)
2111 if (isa<MemberExpr>(E))
2112 return false;
2113
John McCall56ca35d2011-02-17 10:25:35 +00002114 // - opaque values (all)
2115 if (isa<OpaqueValueExpr>(E))
2116 return false;
2117
John McCall558d2ab2010-09-15 10:14:12 +00002118 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002119}
2120
Douglas Gregor75e85042011-03-02 21:06:53 +00002121bool Expr::isImplicitCXXThis() const {
2122 const Expr *E = this;
2123
2124 // Strip away parentheses and casts we don't care about.
2125 while (true) {
2126 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2127 E = Paren->getSubExpr();
2128 continue;
2129 }
2130
2131 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2132 if (ICE->getCastKind() == CK_NoOp ||
2133 ICE->getCastKind() == CK_LValueToRValue ||
2134 ICE->getCastKind() == CK_DerivedToBase ||
2135 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2136 E = ICE->getSubExpr();
2137 continue;
2138 }
2139 }
2140
2141 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2142 if (UnOp->getOpcode() == UO_Extension) {
2143 E = UnOp->getSubExpr();
2144 continue;
2145 }
2146 }
2147
2148 break;
2149 }
2150
2151 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2152 return This->isImplicit();
2153
2154 return false;
2155}
2156
Douglas Gregor898574e2008-12-05 23:32:09 +00002157/// hasAnyTypeDependentArguments - Determines if any of the expressions
2158/// in Exprs is type-dependent.
2159bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2160 for (unsigned I = 0; I < NumExprs; ++I)
2161 if (Exprs[I]->isTypeDependent())
2162 return true;
2163
2164 return false;
2165}
2166
2167/// hasAnyValueDependentArguments - Determines if any of the expressions
2168/// in Exprs is value-dependent.
2169bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2170 for (unsigned I = 0; I < NumExprs; ++I)
2171 if (Exprs[I]->isValueDependent())
2172 return true;
2173
2174 return false;
2175}
2176
John McCall4204f072010-08-02 21:13:48 +00002177bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002178 // This function is attempting whether an expression is an initializer
2179 // which can be evaluated at compile-time. isEvaluatable handles most
2180 // of the cases, but it can't deal with some initializer-specific
2181 // expressions, and it can't deal with aggregates; we deal with those here,
2182 // and fall back to isEvaluatable for the other cases.
2183
John McCall4204f072010-08-02 21:13:48 +00002184 // If we ever capture reference-binding directly in the AST, we can
2185 // kill the second parameter.
2186
2187 if (IsForRef) {
2188 EvalResult Result;
2189 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2190 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002191
Anders Carlssone8a32b82008-11-24 05:23:59 +00002192 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002193 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002194 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002195 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002196 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002197 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002198 case CXXTemporaryObjectExprClass:
2199 case CXXConstructExprClass: {
2200 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002201
2202 // Only if it's
2203 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002204 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002205 if (!CE->getNumArgs()) return true;
2206
2207 // 2) an elidable trivial copy construction of an operand which is
2208 // itself a constant initializer. Note that we consider the
2209 // operand on its own, *not* as a reference binding.
2210 return CE->isElidable() &&
2211 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002212 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002213 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002214 // This handles gcc's extension that allows global initializers like
2215 // "struct x {int x;} x = (struct x) {};".
2216 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002217 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002218 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002219 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002220 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002221 // FIXME: This doesn't deal with fields with reference types correctly.
2222 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2223 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002224 const InitListExpr *Exp = cast<InitListExpr>(this);
2225 unsigned numInits = Exp->getNumInits();
2226 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002227 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002228 return false;
2229 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002230 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002231 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002232 case ImplicitValueInitExprClass:
2233 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002234 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002235 return cast<ParenExpr>(this)->getSubExpr()
2236 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002237 case GenericSelectionExprClass:
2238 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2239 return false;
2240 return cast<GenericSelectionExpr>(this)->getResultExpr()
2241 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002242 case ChooseExprClass:
2243 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2244 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002245 case UnaryOperatorClass: {
2246 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002247 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002248 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002249 break;
2250 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002251 case BinaryOperatorClass: {
2252 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2253 // but this handles the common case.
2254 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002255 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002256 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2257 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2258 return true;
2259 break;
2260 }
John McCall4204f072010-08-02 21:13:48 +00002261 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002262 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002263 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002264 case CStyleCastExprClass:
2265 // Handle casts with a destination that's a struct or union; this
2266 // deals with both the gcc no-op struct cast extension and the
2267 // cast-to-union extension.
2268 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002269 return cast<CastExpr>(this)->getSubExpr()
2270 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002271
Chris Lattner430656e2009-10-13 22:12:09 +00002272 // Integer->integer casts can be handled here, which is important for
2273 // things like (int)(&&x-&&y). Scary but true.
2274 if (getType()->isIntegerType() &&
2275 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002276 return cast<CastExpr>(this)->getSubExpr()
2277 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002278
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002279 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002280 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002281 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002282}
2283
Chandler Carruth82214a82011-02-18 23:54:50 +00002284/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2285/// pointer constant or not, as well as the specific kind of constant detected.
2286/// Null pointer constants can be integer constant expressions with the
2287/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2288/// (a GNU extension).
2289Expr::NullPointerConstantKind
2290Expr::isNullPointerConstant(ASTContext &Ctx,
2291 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002292 if (isValueDependent()) {
2293 switch (NPC) {
2294 case NPC_NeverValueDependent:
2295 assert(false && "Unexpected value dependent expression!");
2296 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002297
Douglas Gregorce940492009-09-25 04:25:58 +00002298 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002299 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2300 return NPCK_ZeroInteger;
2301 else
2302 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002303
Douglas Gregorce940492009-09-25 04:25:58 +00002304 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002305 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002306 }
2307 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002308
Sebastian Redl07779722008-10-31 14:43:28 +00002309 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002310 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002311 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002312 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002313 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002314 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002315 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002316 Pointee->isVoidType() && // to void*
2317 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002318 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002319 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002320 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002321 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2322 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002323 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002324 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2325 // Accept ((void*)0) as a null pointer constant, as many other
2326 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002327 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002328 } else if (const GenericSelectionExpr *GE =
2329 dyn_cast<GenericSelectionExpr>(this)) {
2330 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002331 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002332 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002333 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002334 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002335 } else if (isa<GNUNullExpr>(this)) {
2336 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002337 return NPCK_GNUNull;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002338 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002339
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002340 // C++0x nullptr_t is always a null pointer constant.
2341 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002342 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002343
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002344 if (const RecordType *UT = getType()->getAsUnionType())
2345 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2346 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2347 const Expr *InitExpr = CLE->getInitializer();
2348 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2349 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2350 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002351 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002352 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002353 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002354 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002355
Reid Spencer5f016e22007-07-11 17:01:13 +00002356 // If we have an integer constant expression, we need to *evaluate* it and
2357 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002358 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002359 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2360
2361 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002362}
Steve Naroff31a45842007-07-28 23:10:27 +00002363
John McCallf6a16482010-12-04 03:47:34 +00002364/// \brief If this expression is an l-value for an Objective C
2365/// property, find the underlying property reference expression.
2366const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2367 const Expr *E = this;
2368 while (true) {
2369 assert((E->getValueKind() == VK_LValue &&
2370 E->getObjectKind() == OK_ObjCProperty) &&
2371 "expression is not a property reference");
2372 E = E->IgnoreParenCasts();
2373 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2374 if (BO->getOpcode() == BO_Comma) {
2375 E = BO->getRHS();
2376 continue;
2377 }
2378 }
2379
2380 break;
2381 }
2382
2383 return cast<ObjCPropertyRefExpr>(E);
2384}
2385
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002386FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002387 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002388
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002389 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002390 if (ICE->getCastKind() == CK_LValueToRValue ||
2391 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002392 E = ICE->getSubExpr()->IgnoreParens();
2393 else
2394 break;
2395 }
2396
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002397 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002398 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002399 if (Field->isBitField())
2400 return Field;
2401
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002402 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2403 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2404 if (Field->isBitField())
2405 return Field;
2406
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002407 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2408 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2409 return BinOp->getLHS()->getBitField();
2410
2411 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002412}
2413
Anders Carlsson09380262010-01-31 17:18:49 +00002414bool Expr::refersToVectorElement() const {
2415 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002416
Anders Carlsson09380262010-01-31 17:18:49 +00002417 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002418 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002419 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002420 E = ICE->getSubExpr()->IgnoreParens();
2421 else
2422 break;
2423 }
Sean Huntc3021132010-05-05 15:23:54 +00002424
Anders Carlsson09380262010-01-31 17:18:49 +00002425 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2426 return ASE->getBase()->getType()->isVectorType();
2427
2428 if (isa<ExtVectorElementExpr>(E))
2429 return true;
2430
2431 return false;
2432}
2433
Chris Lattner2140e902009-02-16 22:14:05 +00002434/// isArrow - Return true if the base expression is a pointer to vector,
2435/// return false if the base expression is a vector.
2436bool ExtVectorElementExpr::isArrow() const {
2437 return getBase()->getType()->isPointerType();
2438}
2439
Nate Begeman213541a2008-04-18 23:10:10 +00002440unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002441 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002442 return VT->getNumElements();
2443 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002444}
2445
Nate Begeman8a997642008-05-09 06:41:27 +00002446/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002447bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002448 // FIXME: Refactor this code to an accessor on the AST node which returns the
2449 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002450 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002451
2452 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002453 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002454 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002455
Nate Begeman190d6a22009-01-18 02:01:21 +00002456 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002457 if (Comp[0] == 's' || Comp[0] == 'S')
2458 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002459
Daniel Dunbar15027422009-10-17 23:53:04 +00002460 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2461 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002462 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002463
Steve Narofffec0b492007-07-30 03:29:09 +00002464 return false;
2465}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002466
Nate Begeman8a997642008-05-09 06:41:27 +00002467/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002468void ExtVectorElementExpr::getEncodedElementAccess(
2469 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002470 llvm::StringRef Comp = Accessor->getName();
2471 if (Comp[0] == 's' || Comp[0] == 'S')
2472 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002473
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002474 bool isHi = Comp == "hi";
2475 bool isLo = Comp == "lo";
2476 bool isEven = Comp == "even";
2477 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002478
Nate Begeman8a997642008-05-09 06:41:27 +00002479 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2480 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002481
Nate Begeman8a997642008-05-09 06:41:27 +00002482 if (isHi)
2483 Index = e + i;
2484 else if (isLo)
2485 Index = i;
2486 else if (isEven)
2487 Index = 2 * i;
2488 else if (isOdd)
2489 Index = 2 * i + 1;
2490 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002491 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002492
Nate Begeman3b8d1162008-05-13 21:03:02 +00002493 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002494 }
Nate Begeman8a997642008-05-09 06:41:27 +00002495}
2496
Douglas Gregor04badcf2010-04-21 00:45:42 +00002497ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002498 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002499 SourceLocation LBracLoc,
2500 SourceLocation SuperLoc,
2501 bool IsInstanceSuper,
2502 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002503 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002504 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002505 ObjCMethodDecl *Method,
2506 Expr **Args, unsigned NumArgs,
2507 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002508 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002509 /*TypeDependent=*/false, /*ValueDependent=*/false,
2510 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002511 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2512 HasMethod(Method != 0), SuperLoc(SuperLoc),
2513 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2514 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002515 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002516{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002517 setReceiverPointer(SuperType.getAsOpaquePtr());
2518 if (NumArgs)
2519 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002520}
2521
Douglas Gregor04badcf2010-04-21 00:45:42 +00002522ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002523 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002524 SourceLocation LBracLoc,
2525 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002526 Selector Sel,
2527 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002528 ObjCMethodDecl *Method,
2529 Expr **Args, unsigned NumArgs,
2530 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002531 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002532 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002533 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2534 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2535 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002536 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002537{
2538 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002539 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002540 for (unsigned I = 0; I != NumArgs; ++I) {
2541 if (Args[I]->isTypeDependent())
2542 ExprBits.TypeDependent = true;
2543 if (Args[I]->isValueDependent())
2544 ExprBits.ValueDependent = true;
2545 if (Args[I]->containsUnexpandedParameterPack())
2546 ExprBits.ContainsUnexpandedParameterPack = true;
2547
2548 MyArgs[I] = Args[I];
2549 }
Ted Kremenek4df728e2008-06-24 15:50:53 +00002550}
2551
Douglas Gregor04badcf2010-04-21 00:45:42 +00002552ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002553 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002554 SourceLocation LBracLoc,
2555 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002556 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002557 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002558 ObjCMethodDecl *Method,
2559 Expr **Args, unsigned NumArgs,
2560 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002561 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002562 Receiver->isTypeDependent(),
2563 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002564 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2565 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2566 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002567 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002568{
2569 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002570 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002571 for (unsigned I = 0; I != NumArgs; ++I) {
2572 if (Args[I]->isTypeDependent())
2573 ExprBits.TypeDependent = true;
2574 if (Args[I]->isValueDependent())
2575 ExprBits.ValueDependent = true;
2576 if (Args[I]->containsUnexpandedParameterPack())
2577 ExprBits.ContainsUnexpandedParameterPack = true;
2578
2579 MyArgs[I] = Args[I];
2580 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002581}
2582
Douglas Gregor04badcf2010-04-21 00:45:42 +00002583ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002584 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002585 SourceLocation LBracLoc,
2586 SourceLocation SuperLoc,
2587 bool IsInstanceSuper,
2588 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002589 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002590 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002591 ObjCMethodDecl *Method,
2592 Expr **Args, unsigned NumArgs,
2593 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002594 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002595 NumArgs * sizeof(Expr *);
2596 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002597 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002598 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002599 RBracLoc);
2600}
2601
2602ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002603 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002604 SourceLocation LBracLoc,
2605 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002606 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002607 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002608 ObjCMethodDecl *Method,
2609 Expr **Args, unsigned NumArgs,
2610 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002611 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002612 NumArgs * sizeof(Expr *);
2613 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002614 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2615 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002616}
2617
2618ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002619 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002620 SourceLocation LBracLoc,
2621 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002622 Selector Sel,
2623 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002624 ObjCMethodDecl *Method,
2625 Expr **Args, unsigned NumArgs,
2626 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002627 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002628 NumArgs * sizeof(Expr *);
2629 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002630 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2631 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002632}
2633
Sean Huntc3021132010-05-05 15:23:54 +00002634ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002635 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002636 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002637 NumArgs * sizeof(Expr *);
2638 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2639 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2640}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002641
2642SourceRange ObjCMessageExpr::getReceiverRange() const {
2643 switch (getReceiverKind()) {
2644 case Instance:
2645 return getInstanceReceiver()->getSourceRange();
2646
2647 case Class:
2648 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2649
2650 case SuperInstance:
2651 case SuperClass:
2652 return getSuperLoc();
2653 }
2654
2655 return SourceLocation();
2656}
2657
Douglas Gregor04badcf2010-04-21 00:45:42 +00002658Selector ObjCMessageExpr::getSelector() const {
2659 if (HasMethod)
2660 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2661 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002662 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002663}
2664
2665ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2666 switch (getReceiverKind()) {
2667 case Instance:
2668 if (const ObjCObjectPointerType *Ptr
2669 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2670 return Ptr->getInterfaceDecl();
2671 break;
2672
2673 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002674 if (const ObjCObjectType *Ty
2675 = getClassReceiver()->getAs<ObjCObjectType>())
2676 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002677 break;
2678
2679 case SuperInstance:
2680 if (const ObjCObjectPointerType *Ptr
2681 = getSuperType()->getAs<ObjCObjectPointerType>())
2682 return Ptr->getInterfaceDecl();
2683 break;
2684
2685 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002686 if (const ObjCObjectType *Iface
2687 = getSuperType()->getAs<ObjCObjectType>())
2688 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002689 break;
2690 }
2691
2692 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002693}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002694
Jay Foad4ba2a172011-01-12 09:06:06 +00002695bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002696 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002697}
2698
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002699ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2700 QualType Type, SourceLocation BLoc,
2701 SourceLocation RP)
2702 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2703 Type->isDependentType(), Type->isDependentType(),
2704 Type->containsUnexpandedParameterPack()),
2705 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2706{
2707 SubExprs = new (C) Stmt*[nexpr];
2708 for (unsigned i = 0; i < nexpr; i++) {
2709 if (args[i]->isTypeDependent())
2710 ExprBits.TypeDependent = true;
2711 if (args[i]->isValueDependent())
2712 ExprBits.ValueDependent = true;
2713 if (args[i]->containsUnexpandedParameterPack())
2714 ExprBits.ContainsUnexpandedParameterPack = true;
2715
2716 SubExprs[i] = args[i];
2717 }
2718}
2719
Nate Begeman888376a2009-08-12 02:28:50 +00002720void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2721 unsigned NumExprs) {
2722 if (SubExprs) C.Deallocate(SubExprs);
2723
2724 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002725 this->NumExprs = NumExprs;
2726 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002727}
Nate Begeman888376a2009-08-12 02:28:50 +00002728
Peter Collingbournef111d932011-04-15 00:35:48 +00002729GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2730 SourceLocation GenericLoc, Expr *ControllingExpr,
2731 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2732 unsigned NumAssocs, SourceLocation DefaultLoc,
2733 SourceLocation RParenLoc,
2734 bool ContainsUnexpandedParameterPack,
2735 unsigned ResultIndex)
2736 : Expr(GenericSelectionExprClass,
2737 AssocExprs[ResultIndex]->getType(),
2738 AssocExprs[ResultIndex]->getValueKind(),
2739 AssocExprs[ResultIndex]->getObjectKind(),
2740 AssocExprs[ResultIndex]->isTypeDependent(),
2741 AssocExprs[ResultIndex]->isValueDependent(),
2742 ContainsUnexpandedParameterPack),
2743 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2744 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2745 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2746 RParenLoc(RParenLoc) {
2747 SubExprs[CONTROLLING] = ControllingExpr;
2748 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2749 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2750}
2751
2752GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2753 SourceLocation GenericLoc, Expr *ControllingExpr,
2754 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2755 unsigned NumAssocs, SourceLocation DefaultLoc,
2756 SourceLocation RParenLoc,
2757 bool ContainsUnexpandedParameterPack)
2758 : Expr(GenericSelectionExprClass,
2759 Context.DependentTy,
2760 VK_RValue,
2761 OK_Ordinary,
2762 /*isTypeDependent=*/ true,
2763 /*isValueDependent=*/ true,
2764 ContainsUnexpandedParameterPack),
2765 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2766 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2767 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2768 RParenLoc(RParenLoc) {
2769 SubExprs[CONTROLLING] = ControllingExpr;
2770 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2771 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2772}
2773
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002774//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002775// DesignatedInitExpr
2776//===----------------------------------------------------------------------===//
2777
2778IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2779 assert(Kind == FieldDesignator && "Only valid on a field designator");
2780 if (Field.NameOrField & 0x01)
2781 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2782 else
2783 return getField()->getIdentifier();
2784}
2785
Sean Huntc3021132010-05-05 15:23:54 +00002786DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002787 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002788 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002789 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002790 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002791 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002792 unsigned NumIndexExprs,
2793 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002794 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002795 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002796 Init->isTypeDependent(), Init->isValueDependent(),
2797 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00002798 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2799 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002800 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002801
2802 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00002803 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00002804 *Child++ = Init;
2805
2806 // Copy the designators and their subexpressions, computing
2807 // value-dependence along the way.
2808 unsigned IndexIdx = 0;
2809 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002810 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002811
2812 if (this->Designators[I].isArrayDesignator()) {
2813 // Compute type- and value-dependence.
2814 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002815 if (Index->isTypeDependent() || Index->isValueDependent())
2816 ExprBits.ValueDependent = true;
2817
2818 // Propagate unexpanded parameter packs.
2819 if (Index->containsUnexpandedParameterPack())
2820 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002821
2822 // Copy the index expressions into permanent storage.
2823 *Child++ = IndexExprs[IndexIdx++];
2824 } else if (this->Designators[I].isArrayRangeDesignator()) {
2825 // Compute type- and value-dependence.
2826 Expr *Start = IndexExprs[IndexIdx];
2827 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002828 if (Start->isTypeDependent() || Start->isValueDependent() ||
2829 End->isTypeDependent() || End->isValueDependent())
2830 ExprBits.ValueDependent = true;
2831
2832 // Propagate unexpanded parameter packs.
2833 if (Start->containsUnexpandedParameterPack() ||
2834 End->containsUnexpandedParameterPack())
2835 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002836
2837 // Copy the start/end expressions into permanent storage.
2838 *Child++ = IndexExprs[IndexIdx++];
2839 *Child++ = IndexExprs[IndexIdx++];
2840 }
2841 }
2842
2843 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002844}
2845
Douglas Gregor05c13a32009-01-22 00:58:24 +00002846DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002847DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002848 unsigned NumDesignators,
2849 Expr **IndexExprs, unsigned NumIndexExprs,
2850 SourceLocation ColonOrEqualLoc,
2851 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002852 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002853 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002854 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002855 ColonOrEqualLoc, UsesColonSyntax,
2856 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002857}
2858
Mike Stump1eb44332009-09-09 15:08:12 +00002859DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002860 unsigned NumIndexExprs) {
2861 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2862 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2863 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2864}
2865
Douglas Gregor319d57f2010-01-06 23:17:19 +00002866void DesignatedInitExpr::setDesignators(ASTContext &C,
2867 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002868 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002869 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002870 NumDesignators = NumDesigs;
2871 for (unsigned I = 0; I != NumDesigs; ++I)
2872 Designators[I] = Desigs[I];
2873}
2874
Abramo Bagnara24f46742011-03-16 15:08:46 +00002875SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
2876 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
2877 if (size() == 1)
2878 return DIE->getDesignator(0)->getSourceRange();
2879 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
2880 DIE->getDesignator(size()-1)->getEndLocation());
2881}
2882
Douglas Gregor05c13a32009-01-22 00:58:24 +00002883SourceRange DesignatedInitExpr::getSourceRange() const {
2884 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002885 Designator &First =
2886 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002887 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002888 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002889 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2890 else
2891 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2892 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002893 StartLoc =
2894 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002895 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2896}
2897
Douglas Gregor05c13a32009-01-22 00:58:24 +00002898Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2899 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2900 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2901 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002902 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2903 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2904}
2905
2906Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002907 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002908 "Requires array range designator");
2909 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2910 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002911 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2912 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2913}
2914
2915Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002916 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002917 "Requires array range designator");
2918 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2919 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002920 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2921 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2922}
2923
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002924/// \brief Replaces the designator at index @p Idx with the series
2925/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002926void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002927 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002928 const Designator *Last) {
2929 unsigned NumNewDesignators = Last - First;
2930 if (NumNewDesignators == 0) {
2931 std::copy_backward(Designators + Idx + 1,
2932 Designators + NumDesignators,
2933 Designators + Idx);
2934 --NumNewDesignators;
2935 return;
2936 } else if (NumNewDesignators == 1) {
2937 Designators[Idx] = *First;
2938 return;
2939 }
2940
Mike Stump1eb44332009-09-09 15:08:12 +00002941 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002942 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002943 std::copy(Designators, Designators + Idx, NewDesignators);
2944 std::copy(First, Last, NewDesignators + Idx);
2945 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2946 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002947 Designators = NewDesignators;
2948 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2949}
2950
Mike Stump1eb44332009-09-09 15:08:12 +00002951ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002952 Expr **exprs, unsigned nexprs,
2953 SourceLocation rparenloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002954 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2955 false, false, false),
2956 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002957
Nate Begeman2ef13e52009-08-10 23:49:36 +00002958 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002959 for (unsigned i = 0; i != nexprs; ++i) {
2960 if (exprs[i]->isTypeDependent())
2961 ExprBits.TypeDependent = true;
2962 if (exprs[i]->isValueDependent())
2963 ExprBits.ValueDependent = true;
2964 if (exprs[i]->containsUnexpandedParameterPack())
2965 ExprBits.ContainsUnexpandedParameterPack = true;
2966
Nate Begeman2ef13e52009-08-10 23:49:36 +00002967 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002968 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00002969}
2970
John McCalle996ffd2011-02-16 08:02:54 +00002971const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
2972 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
2973 e = ewc->getSubExpr();
2974 e = cast<CXXConstructExpr>(e)->getArg(0);
2975 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
2976 e = ice->getSubExpr();
2977 return cast<OpaqueValueExpr>(e);
2978}
2979
Douglas Gregor05c13a32009-01-22 00:58:24 +00002980//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002981// ExprIterator.
2982//===----------------------------------------------------------------------===//
2983
2984Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2985Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2986Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2987const Expr* ConstExprIterator::operator[](size_t idx) const {
2988 return cast<Expr>(I[idx]);
2989}
2990const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2991const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2992
2993//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002994// Child Iterators for iterating over subexpressions/substatements
2995//===----------------------------------------------------------------------===//
2996
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002997// UnaryExprOrTypeTraitExpr
2998Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00002999 // If this is of a type and the type is a VLA type (and not a typedef), the
3000 // size expression of the VLA needs to be treated as an executable expression.
3001 // Why isn't this weirdness documented better in StmtIterator?
3002 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003003 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003004 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003005 return child_range(child_iterator(T), child_iterator());
3006 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003007 }
John McCall63c00d72011-02-09 08:16:59 +00003008 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003009}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003010
Steve Naroff563477d2007-09-18 23:55:05 +00003011// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003012Stmt::child_range ObjCMessageExpr::children() {
3013 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003014 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003015 begin = reinterpret_cast<Stmt **>(this + 1);
3016 else
3017 begin = reinterpret_cast<Stmt **>(getArgs());
3018 return child_range(begin,
3019 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003020}
3021
Steve Naroff4eb206b2008-09-03 18:15:37 +00003022// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003023BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003024 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003025 bool constAdded)
Douglas Gregord967e312011-01-19 21:52:31 +00003026 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003027 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003028 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003029{
Douglas Gregord967e312011-01-19 21:52:31 +00003030 bool TypeDependent = false;
3031 bool ValueDependent = false;
3032 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
3033 ExprBits.TypeDependent = TypeDependent;
3034 ExprBits.ValueDependent = ValueDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003035}
3036