blob: 7e46d4fe450a309d4af31b324fb14cb41d2174de [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 {
38 // If this value has _Bool type, it is obvious 0/1.
39 if (getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000040 // If this is a non-scalar-integer type, we don't care enough to try.
Douglas Gregor2ade35e2010-06-16 00:17:44 +000041 if (!getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000042
Chris Lattner2b334bb2010-04-16 23:34:13 +000043 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
44 return PE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000045
Chris Lattner2b334bb2010-04-16 23:34:13 +000046 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
47 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000048 case UO_Plus:
49 case UO_Extension:
Chris Lattner2b334bb2010-04-16 23:34:13 +000050 return UO->getSubExpr()->isKnownToHaveBooleanValue();
51 default:
52 return false;
53 }
54 }
Sean Huntc3021132010-05-05 15:23:54 +000055
John McCall6907fbe2010-06-12 01:56:02 +000056 // Only look through implicit casts. If the user writes
57 // '(int) (a && b)' treat it as an arbitrary int.
58 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(this))
Chris Lattner2b334bb2010-04-16 23:34:13 +000059 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000060
Chris Lattner2b334bb2010-04-16 23:34:13 +000061 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
62 switch (BO->getOpcode()) {
63 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000064 case BO_LT: // Relational operators.
65 case BO_GT:
66 case BO_LE:
67 case BO_GE:
68 case BO_EQ: // Equality operators.
69 case BO_NE:
70 case BO_LAnd: // AND operator.
71 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000072 return true;
Sean Huntc3021132010-05-05 15:23:54 +000073
John McCall2de56d12010-08-25 11:45:40 +000074 case BO_And: // Bitwise AND operator.
75 case BO_Xor: // Bitwise XOR operator.
76 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000077 // Handle things like (x==2)|(y==12).
78 return BO->getLHS()->isKnownToHaveBooleanValue() &&
79 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000080
John McCall2de56d12010-08-25 11:45:40 +000081 case BO_Comma:
82 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000083 return BO->getRHS()->isKnownToHaveBooleanValue();
84 }
85 }
Sean Huntc3021132010-05-05 15:23:54 +000086
Chris Lattner2b334bb2010-04-16 23:34:13 +000087 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
88 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
89 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000090
Chris Lattner2b334bb2010-04-16 23:34:13 +000091 return false;
92}
93
John McCall63c00d72011-02-09 08:16:59 +000094// Amusing macro metaprogramming hack: check whether a class provides
95// a more specific implementation of getExprLoc().
96namespace {
97 /// This implementation is used when a class provides a custom
98 /// implementation of getExprLoc.
99 template <class E, class T>
100 SourceLocation getExprLocImpl(const Expr *expr,
101 SourceLocation (T::*v)() const) {
102 return static_cast<const E*>(expr)->getExprLoc();
103 }
104
105 /// This implementation is used when a class doesn't provide
106 /// a custom implementation of getExprLoc. Overload resolution
107 /// should pick it over the implementation above because it's
108 /// more specialized according to function template partial ordering.
109 template <class E>
110 SourceLocation getExprLocImpl(const Expr *expr,
111 SourceLocation (Expr::*v)() const) {
112 return static_cast<const E*>(expr)->getSourceRange().getBegin();
113 }
114}
115
116SourceLocation Expr::getExprLoc() const {
117 switch (getStmtClass()) {
118 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
119#define ABSTRACT_STMT(type)
120#define STMT(type, base) \
121 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
122#define EXPR(type, base) \
123 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
124#include "clang/AST/StmtNodes.inc"
125 }
126 llvm_unreachable("unknown statement kind");
127 return SourceLocation();
128}
129
Reid Spencer5f016e22007-07-11 17:01:13 +0000130//===----------------------------------------------------------------------===//
131// Primary Expressions.
132//===----------------------------------------------------------------------===//
133
John McCalld5532b62009-11-23 01:53:49 +0000134void ExplicitTemplateArgumentList::initializeFrom(
135 const TemplateArgumentListInfo &Info) {
136 LAngleLoc = Info.getLAngleLoc();
137 RAngleLoc = Info.getRAngleLoc();
138 NumTemplateArgs = Info.size();
139
140 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
141 for (unsigned i = 0; i != NumTemplateArgs; ++i)
142 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
143}
144
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000145void ExplicitTemplateArgumentList::initializeFrom(
146 const TemplateArgumentListInfo &Info,
147 bool &Dependent,
148 bool &ContainsUnexpandedParameterPack) {
149 LAngleLoc = Info.getLAngleLoc();
150 RAngleLoc = Info.getRAngleLoc();
151 NumTemplateArgs = Info.size();
152
153 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
154 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
155 Dependent = Dependent || Info[i].getArgument().isDependent();
156 ContainsUnexpandedParameterPack
157 = ContainsUnexpandedParameterPack ||
158 Info[i].getArgument().containsUnexpandedParameterPack();
159
160 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
161 }
162}
163
John McCalld5532b62009-11-23 01:53:49 +0000164void ExplicitTemplateArgumentList::copyInto(
165 TemplateArgumentListInfo &Info) const {
166 Info.setLAngleLoc(LAngleLoc);
167 Info.setRAngleLoc(RAngleLoc);
168 for (unsigned I = 0; I != NumTemplateArgs; ++I)
169 Info.addArgument(getTemplateArgs()[I]);
170}
171
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000172std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
173 return sizeof(ExplicitTemplateArgumentList) +
174 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
175}
176
John McCalld5532b62009-11-23 01:53:49 +0000177std::size_t ExplicitTemplateArgumentList::sizeFor(
178 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000179 return sizeFor(Info.size());
John McCalld5532b62009-11-23 01:53:49 +0000180}
181
Douglas Gregord967e312011-01-19 21:52:31 +0000182/// \brief Compute the type- and value-dependence of a declaration reference
183/// based on the declaration being referenced.
184static void computeDeclRefDependence(NamedDecl *D, QualType T,
185 bool &TypeDependent,
186 bool &ValueDependent) {
187 TypeDependent = false;
188 ValueDependent = false;
Sean Huntc3021132010-05-05 15:23:54 +0000189
Douglas Gregor0da76df2009-11-23 11:41:28 +0000190
191 // (TD) C++ [temp.dep.expr]p3:
192 // An id-expression is type-dependent if it contains:
193 //
Sean Huntc3021132010-05-05 15:23:54 +0000194 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000195 //
196 // (VD) C++ [temp.dep.constexpr]p2:
197 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000198
Douglas Gregor0da76df2009-11-23 11:41:28 +0000199 // (TD) - an identifier that was declared with dependent type
200 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000201 if (T->isDependentType()) {
202 TypeDependent = true;
203 ValueDependent = true;
204 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000205 }
Douglas Gregord967e312011-01-19 21:52:31 +0000206
Douglas Gregor0da76df2009-11-23 11:41:28 +0000207 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000208 if (D->getDeclName().getNameKind()
209 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor0da76df2009-11-23 11:41:28 +0000210 D->getDeclName().getCXXNameType()->isDependentType()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000211 TypeDependent = true;
212 ValueDependent = true;
213 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000214 }
215 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000216 if (isa<NonTypeTemplateParmDecl>(D)) {
217 ValueDependent = true;
218 return;
219 }
220
Douglas Gregor0da76df2009-11-23 11:41:28 +0000221 // (VD) - a constant with integral or enumeration type and is
222 // initialized with an expression that is value-dependent.
Douglas Gregord967e312011-01-19 21:52:31 +0000223 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000224 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000225 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000226 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000227 if (Init->isValueDependent())
Douglas Gregord967e312011-01-19 21:52:31 +0000228 ValueDependent = true;
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000229 }
Douglas Gregord967e312011-01-19 21:52:31 +0000230
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000231 // (VD) - FIXME: Missing from the standard:
232 // - a member function or a static data member of the current
233 // instantiation
234 else if (Var->isStaticDataMember() &&
Douglas Gregor7ed5bd32010-05-11 08:44:04 +0000235 Var->getDeclContext()->isDependentContext())
Douglas Gregord967e312011-01-19 21:52:31 +0000236 ValueDependent = true;
237
238 return;
239 }
240
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000241 // (VD) - FIXME: Missing from the standard:
242 // - a member function or a static data member of the current
243 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000244 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
245 ValueDependent = true;
246 return;
247 }
248}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000249
Douglas Gregord967e312011-01-19 21:52:31 +0000250void DeclRefExpr::computeDependence() {
251 bool TypeDependent = false;
252 bool ValueDependent = false;
253 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent);
254
255 // (TD) C++ [temp.dep.expr]p3:
256 // An id-expression is type-dependent if it contains:
257 //
258 // and
259 //
260 // (VD) C++ [temp.dep.constexpr]p2:
261 // An identifier is value-dependent if it is:
262 if (!TypeDependent && !ValueDependent &&
263 hasExplicitTemplateArgs() &&
264 TemplateSpecializationType::anyDependentTemplateArguments(
265 getTemplateArgs(),
266 getNumTemplateArgs())) {
267 TypeDependent = true;
268 ValueDependent = true;
269 }
270
271 ExprBits.TypeDependent = TypeDependent;
272 ExprBits.ValueDependent = ValueDependent;
273
Douglas Gregor10738d32010-12-23 23:51:58 +0000274 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000275 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000276 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000277}
278
Sean Huntc3021132010-05-05 15:23:54 +0000279DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000280 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000281 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000282 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000283 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000284 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000285 DecoratedD(D,
286 (Qualifier? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000287 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000288 Loc(NameLoc) {
289 if (Qualifier) {
290 NameQualifier *NQ = getNameQualifier();
291 NQ->NNS = Qualifier;
292 NQ->Range = QualifierRange;
293 }
Sean Huntc3021132010-05-05 15:23:54 +0000294
John McCalld5532b62009-11-23 01:53:49 +0000295 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000296 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000297
298 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000299}
300
Abramo Bagnara25777432010-08-11 22:01:17 +0000301DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
302 SourceRange QualifierRange,
303 ValueDecl *D, const DeclarationNameInfo &NameInfo,
304 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000305 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000306 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Abramo Bagnara25777432010-08-11 22:01:17 +0000307 DecoratedD(D,
308 (Qualifier? HasQualifierFlag : 0) |
309 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
310 Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
311 if (Qualifier) {
312 NameQualifier *NQ = getNameQualifier();
313 NQ->NNS = Qualifier;
314 NQ->Range = QualifierRange;
315 }
316
317 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000318 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000319
320 computeDependence();
321}
322
Douglas Gregora2813ce2009-10-23 18:54:35 +0000323DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
324 NestedNameSpecifier *Qualifier,
325 SourceRange QualifierRange,
John McCalldbd872f2009-12-08 09:08:17 +0000326 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000327 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000328 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000329 ExprValueKind VK,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000330 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000331 return Create(Context, Qualifier, QualifierRange, D,
332 DeclarationNameInfo(D->getDeclName(), NameLoc),
John McCallf89e55a2010-11-18 06:31:45 +0000333 T, VK, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000334}
335
336DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
337 NestedNameSpecifier *Qualifier,
338 SourceRange QualifierRange,
339 ValueDecl *D,
340 const DeclarationNameInfo &NameInfo,
341 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000342 ExprValueKind VK,
Abramo Bagnara25777432010-08-11 22:01:17 +0000343 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000344 std::size_t Size = sizeof(DeclRefExpr);
345 if (Qualifier != 0)
346 Size += sizeof(NameQualifier);
Sean Huntc3021132010-05-05 15:23:54 +0000347
John McCalld5532b62009-11-23 01:53:49 +0000348 if (TemplateArgs)
349 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +0000350
Chris Lattner32488542010-10-30 05:14:06 +0000351 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Abramo Bagnara25777432010-08-11 22:01:17 +0000352 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameInfo,
John McCallf89e55a2010-11-18 06:31:45 +0000353 TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000354}
355
Douglas Gregordef03542011-02-04 12:01:24 +0000356DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
357 bool HasQualifier,
358 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000359 unsigned NumTemplateArgs) {
360 std::size_t Size = sizeof(DeclRefExpr);
361 if (HasQualifier)
362 Size += sizeof(NameQualifier);
363
Douglas Gregordef03542011-02-04 12:01:24 +0000364 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000365 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
366
Chris Lattner32488542010-10-30 05:14:06 +0000367 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000368 return new (Mem) DeclRefExpr(EmptyShell());
369}
370
Douglas Gregora2813ce2009-10-23 18:54:35 +0000371SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000372 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000373 if (hasQualifier())
374 R.setBegin(getQualifierRange().getBegin());
John McCall096832c2010-08-19 23:49:38 +0000375 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000376 R.setEnd(getRAngleLoc());
377 return R;
378}
379
Anders Carlsson3a082d82009-09-08 18:24:21 +0000380// FIXME: Maybe this should use DeclPrinter with a special "print predefined
381// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000382std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
383 ASTContext &Context = CurrentDecl->getASTContext();
384
Anders Carlsson3a082d82009-09-08 18:24:21 +0000385 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000386 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000387 return FD->getNameAsString();
388
389 llvm::SmallString<256> Name;
390 llvm::raw_svector_ostream Out(Name);
391
392 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000393 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000394 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000395 if (MD->isStatic())
396 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000397 }
398
399 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000400
401 std::string Proto = FD->getQualifiedNameAsString(Policy);
402
John McCall183700f2009-09-21 23:43:11 +0000403 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000404 const FunctionProtoType *FT = 0;
405 if (FD->hasWrittenPrototype())
406 FT = dyn_cast<FunctionProtoType>(AFT);
407
408 Proto += "(";
409 if (FT) {
410 llvm::raw_string_ostream POut(Proto);
411 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
412 if (i) POut << ", ";
413 std::string Param;
414 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
415 POut << Param;
416 }
417
418 if (FT->isVariadic()) {
419 if (FD->getNumParams()) POut << ", ";
420 POut << "...";
421 }
422 }
423 Proto += ")";
424
Sam Weinig4eadcc52009-12-27 01:38:20 +0000425 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
426 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
427 if (ThisQuals.hasConst())
428 Proto += " const";
429 if (ThisQuals.hasVolatile())
430 Proto += " volatile";
431 }
432
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000433 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
434 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000435
436 Out << Proto;
437
438 Out.flush();
439 return Name.str().str();
440 }
441 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
442 llvm::SmallString<256> Name;
443 llvm::raw_svector_ostream Out(Name);
444 Out << (MD->isInstanceMethod() ? '-' : '+');
445 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000446
447 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
448 // a null check to avoid a crash.
449 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000450 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000451
Anders Carlsson3a082d82009-09-08 18:24:21 +0000452 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000453 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
454 Out << '(' << CID << ')';
455
Anders Carlsson3a082d82009-09-08 18:24:21 +0000456 Out << ' ';
457 Out << MD->getSelector().getAsString();
458 Out << ']';
459
460 Out.flush();
461 return Name.str().str();
462 }
463 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
464 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
465 return "top level";
466 }
467 return "";
468}
469
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000470void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
471 if (hasAllocation())
472 C.Deallocate(pVal);
473
474 BitWidth = Val.getBitWidth();
475 unsigned NumWords = Val.getNumWords();
476 const uint64_t* Words = Val.getRawData();
477 if (NumWords > 1) {
478 pVal = new (C) uint64_t[NumWords];
479 std::copy(Words, Words + NumWords, pVal);
480 } else if (NumWords == 1)
481 VAL = Words[0];
482 else
483 VAL = 0;
484}
485
486IntegerLiteral *
487IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
488 QualType type, SourceLocation l) {
489 return new (C) IntegerLiteral(C, V, type, l);
490}
491
492IntegerLiteral *
493IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
494 return new (C) IntegerLiteral(Empty);
495}
496
497FloatingLiteral *
498FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
499 bool isexact, QualType Type, SourceLocation L) {
500 return new (C) FloatingLiteral(C, V, isexact, Type, L);
501}
502
503FloatingLiteral *
504FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
505 return new (C) FloatingLiteral(Empty);
506}
507
Chris Lattnerda8249e2008-06-07 22:13:43 +0000508/// getValueAsApproximateDouble - This returns the value as an inaccurate
509/// double. Note that this may cause loss of precision, but is useful for
510/// debugging dumps, etc.
511double FloatingLiteral::getValueAsApproximateDouble() const {
512 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000513 bool ignored;
514 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
515 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000516 return V.convertToDouble();
517}
518
Chris Lattner2085fd62009-02-18 06:40:38 +0000519StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
520 unsigned ByteLength, bool Wide,
521 QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000522 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000523 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000524 // Allocate enough space for the StringLiteral plus an array of locations for
525 // any concatenated string tokens.
526 void *Mem = C.Allocate(sizeof(StringLiteral)+
527 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000528 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000529 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000530
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000532 char *AStrData = new (C, 1) char[ByteLength];
533 memcpy(AStrData, StrData, ByteLength);
534 SL->StrData = AStrData;
535 SL->ByteLength = ByteLength;
536 SL->IsWide = Wide;
537 SL->TokLocs[0] = Loc[0];
538 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000539
Chris Lattner726e1682009-02-18 05:49:11 +0000540 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000541 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
542 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000543}
544
Douglas Gregor673ecd62009-04-15 16:35:07 +0000545StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
546 void *Mem = C.Allocate(sizeof(StringLiteral)+
547 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000548 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000549 StringLiteral *SL = new (Mem) StringLiteral(QualType());
550 SL->StrData = 0;
551 SL->ByteLength = 0;
552 SL->NumConcatenated = NumStrs;
553 return SL;
554}
555
Daniel Dunbarb6480232009-09-22 03:27:33 +0000556void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbarb6480232009-09-22 03:27:33 +0000557 char *AStrData = new (C, 1) char[Str.size()];
558 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000559 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000560 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000561}
562
Chris Lattner08f92e32010-11-17 07:37:15 +0000563/// getLocationOfByte - Return a source location that points to the specified
564/// byte of this string literal.
565///
566/// Strings are amazingly complex. They can be formed from multiple tokens and
567/// can have escape sequences in them in addition to the usual trigraph and
568/// escaped newline business. This routine handles this complexity.
569///
570SourceLocation StringLiteral::
571getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
572 const LangOptions &Features, const TargetInfo &Target) const {
573 assert(!isWide() && "This doesn't work for wide strings yet");
574
575 // Loop over all of the tokens in this string until we find the one that
576 // contains the byte we're looking for.
577 unsigned TokNo = 0;
578 while (1) {
579 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
580 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
581
582 // Get the spelling of the string so that we can get the data that makes up
583 // the string literal, not the identifier for the macro it is potentially
584 // expanded through.
585 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
586
587 // Re-lex the token to get its length and original spelling.
588 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
589 bool Invalid = false;
590 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
591 if (Invalid)
592 return StrTokSpellingLoc;
593
594 const char *StrData = Buffer.data()+LocInfo.second;
595
596 // Create a langops struct and enable trigraphs. This is sufficient for
597 // relexing tokens.
598 LangOptions LangOpts;
599 LangOpts.Trigraphs = true;
600
601 // Create a lexer starting at the beginning of this token.
602 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
603 Buffer.end());
604 Token TheTok;
605 TheLexer.LexFromRawLexer(TheTok);
606
607 // Use the StringLiteralParser to compute the length of the string in bytes.
608 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
609 unsigned TokNumBytes = SLP.GetStringLength();
610
611 // If the byte is in this token, return the location of the byte.
612 if (ByteNo < TokNumBytes ||
613 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
614 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
615
616 // Now that we know the offset of the token in the spelling, use the
617 // preprocessor to get the offset in the original source.
618 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
619 }
620
621 // Move to the next string token.
622 ++TokNo;
623 ByteNo -= TokNumBytes;
624 }
625}
626
627
628
Reid Spencer5f016e22007-07-11 17:01:13 +0000629/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
630/// corresponds to, e.g. "sizeof" or "[pre]++".
631const char *UnaryOperator::getOpcodeStr(Opcode Op) {
632 switch (Op) {
633 default: assert(0 && "Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000634 case UO_PostInc: return "++";
635 case UO_PostDec: return "--";
636 case UO_PreInc: return "++";
637 case UO_PreDec: return "--";
638 case UO_AddrOf: return "&";
639 case UO_Deref: return "*";
640 case UO_Plus: return "+";
641 case UO_Minus: return "-";
642 case UO_Not: return "~";
643 case UO_LNot: return "!";
644 case UO_Real: return "__real";
645 case UO_Imag: return "__imag";
646 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000647 }
648}
649
John McCall2de56d12010-08-25 11:45:40 +0000650UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000651UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
652 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000653 default: assert(false && "No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000654 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
655 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
656 case OO_Amp: return UO_AddrOf;
657 case OO_Star: return UO_Deref;
658 case OO_Plus: return UO_Plus;
659 case OO_Minus: return UO_Minus;
660 case OO_Tilde: return UO_Not;
661 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000662 }
663}
664
665OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
666 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000667 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
668 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
669 case UO_AddrOf: return OO_Amp;
670 case UO_Deref: return OO_Star;
671 case UO_Plus: return OO_Plus;
672 case UO_Minus: return OO_Minus;
673 case UO_Not: return OO_Tilde;
674 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000675 default: return OO_None;
676 }
677}
678
679
Reid Spencer5f016e22007-07-11 17:01:13 +0000680//===----------------------------------------------------------------------===//
681// Postfix Operators.
682//===----------------------------------------------------------------------===//
683
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000684CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
685 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000686 SourceLocation rparenloc)
687 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000688 fn->isTypeDependent(),
689 fn->isValueDependent(),
690 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000691 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000693 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000694 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000695 for (unsigned i = 0; i != numargs; ++i) {
696 if (args[i]->isTypeDependent())
697 ExprBits.TypeDependent = true;
698 if (args[i]->isValueDependent())
699 ExprBits.ValueDependent = true;
700 if (args[i]->containsUnexpandedParameterPack())
701 ExprBits.ContainsUnexpandedParameterPack = true;
702
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000703 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000704 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000705
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000706 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000707 RParenLoc = rparenloc;
708}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000709
Ted Kremenek668bf912009-02-09 20:51:47 +0000710CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000711 QualType t, ExprValueKind VK, SourceLocation rparenloc)
712 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000713 fn->isTypeDependent(),
714 fn->isValueDependent(),
715 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000716 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000717
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000718 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000719 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000720 for (unsigned i = 0; i != numargs; ++i) {
721 if (args[i]->isTypeDependent())
722 ExprBits.TypeDependent = true;
723 if (args[i]->isValueDependent())
724 ExprBits.ValueDependent = true;
725 if (args[i]->containsUnexpandedParameterPack())
726 ExprBits.ContainsUnexpandedParameterPack = true;
727
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000728 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000729 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000730
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000731 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000732 RParenLoc = rparenloc;
733}
734
Mike Stump1eb44332009-09-09 15:08:12 +0000735CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
736 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000737 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000738 SubExprs = new (C) Stmt*[PREARGS_START];
739 CallExprBits.NumPreArgs = 0;
740}
741
742CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
743 EmptyShell Empty)
744 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
745 // FIXME: Why do we allocate this?
746 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
747 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000748}
749
Nuno Lopesd20254f2009-12-20 23:11:08 +0000750Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000751 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl20012152010-09-10 20:55:30 +0000752 // If we're calling a dereference, look at the pointer instead.
753 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
754 if (BO->isPtrMemOp())
755 CEE = BO->getRHS()->IgnoreParenCasts();
756 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
757 if (UO->getOpcode() == UO_Deref)
758 CEE = UO->getSubExpr()->IgnoreParenCasts();
759 }
Chris Lattner6346f962009-07-17 15:46:27 +0000760 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000761 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000762 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
763 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000764
765 return 0;
766}
767
Nuno Lopesd20254f2009-12-20 23:11:08 +0000768FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000769 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000770}
771
Chris Lattnerd18b3292007-12-28 05:25:02 +0000772/// setNumArgs - This changes the number of arguments present in this call.
773/// Any orphaned expressions are deleted by this, and any new operands are set
774/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000775void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000776 // No change, just return.
777 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000778
Chris Lattnerd18b3292007-12-28 05:25:02 +0000779 // If shrinking # arguments, just delete the extras and forgot them.
780 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000781 this->NumArgs = NumArgs;
782 return;
783 }
784
785 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000786 unsigned NumPreArgs = getNumPreArgs();
787 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000788 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000789 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000790 NewSubExprs[i] = SubExprs[i];
791 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000792 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
793 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000794 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Douglas Gregor88c9a462009-04-17 21:46:47 +0000796 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000797 SubExprs = NewSubExprs;
798 this->NumArgs = NumArgs;
799}
800
Chris Lattnercb888962008-10-06 05:00:53 +0000801/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
802/// not, return 0.
Jay Foad4ba2a172011-01-12 09:06:06 +0000803unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000804 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000805 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000806 // ImplicitCastExpr.
807 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
808 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000809 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000811 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
812 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000813 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000814
Anders Carlssonbcba2012008-01-31 02:13:57 +0000815 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
816 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000817 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000818
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000819 if (!FDecl->getIdentifier())
820 return 0;
821
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000822 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000823}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000824
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000825QualType CallExpr::getCallReturnType() const {
826 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000827 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000828 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000829 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000830 CalleeType = BPT->getPointeeType();
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000831 else if (const MemberPointerType *MPT
832 = CalleeType->getAs<MemberPointerType>())
833 CalleeType = MPT->getPointeeType();
834
John McCall183700f2009-09-21 23:43:11 +0000835 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000836 return FnType->getResultType();
837}
Chris Lattnercb888962008-10-06 05:00:53 +0000838
Sean Huntc3021132010-05-05 15:23:54 +0000839OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000840 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000841 TypeSourceInfo *tsi,
842 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000843 Expr** exprsPtr, unsigned numExprs,
844 SourceLocation RParenLoc) {
845 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000846 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000847 sizeof(Expr*) * numExprs);
848
849 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
850 exprsPtr, numExprs, RParenLoc);
851}
852
853OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
854 unsigned numComps, unsigned numExprs) {
855 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
856 sizeof(OffsetOfNode) * numComps +
857 sizeof(Expr*) * numExprs);
858 return new (Mem) OffsetOfExpr(numComps, numExprs);
859}
860
Sean Huntc3021132010-05-05 15:23:54 +0000861OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000862 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000863 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000864 Expr** exprsPtr, unsigned numExprs,
865 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000866 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
867 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000868 /*ValueDependent=*/tsi->getType()->isDependentType(),
869 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000870 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
871 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000872{
873 for(unsigned i = 0; i < numComps; ++i) {
874 setComponent(i, compsPtr[i]);
875 }
Sean Huntc3021132010-05-05 15:23:54 +0000876
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000877 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000878 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
879 ExprBits.ValueDependent = true;
880 if (exprsPtr[i]->containsUnexpandedParameterPack())
881 ExprBits.ContainsUnexpandedParameterPack = true;
882
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000883 setIndexExpr(i, exprsPtr[i]);
884 }
885}
886
887IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
888 assert(getKind() == Field || getKind() == Identifier);
889 if (getKind() == Field)
890 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000891
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000892 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
893}
894
Mike Stump1eb44332009-09-09 15:08:12 +0000895MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
896 NestedNameSpecifier *qual,
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000897 SourceRange qualrange,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000898 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000899 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000900 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000901 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000902 QualType ty,
903 ExprValueKind vk,
904 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000905 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000906
John McCall161755a2010-04-06 21:38:20 +0000907 bool hasQualOrFound = (qual != 0 ||
908 founddecl.getDecl() != memberdecl ||
909 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000910 if (hasQualOrFound)
911 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000912
John McCalld5532b62009-11-23 01:53:49 +0000913 if (targs)
914 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000915
Chris Lattner32488542010-10-30 05:14:06 +0000916 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000917 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
918 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000919
920 if (hasQualOrFound) {
921 if (qual && qual->isDependent()) {
922 E->setValueDependent(true);
923 E->setTypeDependent(true);
924 }
925 E->HasQualifierOrFoundDecl = true;
926
927 MemberNameQualifier *NQ = E->getMemberQualifier();
928 NQ->NNS = qual;
929 NQ->Range = qualrange;
930 NQ->FoundDecl = founddecl;
931 }
932
933 if (targs) {
934 E->HasExplicitTemplateArgumentList = true;
John McCall096832c2010-08-19 23:49:38 +0000935 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall6bb80172010-03-30 21:47:33 +0000936 }
937
938 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000939}
940
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000941const char *CastExpr::getCastKindName() const {
942 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000943 case CK_Dependent:
944 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +0000945 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000946 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +0000947 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +0000948 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +0000949 case CK_LValueToRValue:
950 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +0000951 case CK_GetObjCProperty:
952 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +0000953 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000954 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +0000955 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +0000956 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +0000957 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000958 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000959 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +0000960 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000961 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000962 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +0000963 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000964 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +0000965 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000966 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000967 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000968 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000969 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000970 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +0000971 case CK_NullToPointer:
972 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000973 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000974 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000975 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +0000976 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +0000977 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000978 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +0000979 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000980 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +0000981 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000982 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000983 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +0000984 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +0000985 case CK_PointerToBoolean:
986 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000987 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +0000988 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +0000989 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +0000990 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +0000991 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +0000992 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +0000993 case CK_IntegralToBoolean:
994 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +0000995 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +0000996 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +0000997 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +0000998 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +0000999 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001000 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001001 case CK_FloatingToBoolean:
1002 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001003 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001004 return "MemberPointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001005 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +00001006 return "AnyPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001007 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001008 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001009 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001010 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001011 case CK_FloatingRealToComplex:
1012 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001013 case CK_FloatingComplexToReal:
1014 return "FloatingComplexToReal";
1015 case CK_FloatingComplexToBoolean:
1016 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001017 case CK_FloatingComplexCast:
1018 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001019 case CK_FloatingComplexToIntegralComplex:
1020 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001021 case CK_IntegralRealToComplex:
1022 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001023 case CK_IntegralComplexToReal:
1024 return "IntegralComplexToReal";
1025 case CK_IntegralComplexToBoolean:
1026 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001027 case CK_IntegralComplexCast:
1028 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001029 case CK_IntegralComplexToFloatingComplex:
1030 return "IntegralComplexToFloatingComplex";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001031 }
Mike Stump1eb44332009-09-09 15:08:12 +00001032
John McCall2bb5d002010-11-13 09:02:35 +00001033 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001034 return 0;
1035}
1036
Douglas Gregor6eef5192009-12-14 19:27:10 +00001037Expr *CastExpr::getSubExprAsWritten() {
1038 Expr *SubExpr = 0;
1039 CastExpr *E = this;
1040 do {
1041 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001042
Douglas Gregor6eef5192009-12-14 19:27:10 +00001043 // Skip any temporary bindings; they're implicit.
1044 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1045 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001046
Douglas Gregor6eef5192009-12-14 19:27:10 +00001047 // Conversions by constructor and conversion functions have a
1048 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001049 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001050 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001051 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001052 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001053
Douglas Gregor6eef5192009-12-14 19:27:10 +00001054 // If the subexpression we're left with is an implicit cast, look
1055 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001056 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1057
Douglas Gregor6eef5192009-12-14 19:27:10 +00001058 return SubExpr;
1059}
1060
John McCallf871d0c2010-08-07 06:22:56 +00001061CXXBaseSpecifier **CastExpr::path_buffer() {
1062 switch (getStmtClass()) {
1063#define ABSTRACT_STMT(x)
1064#define CASTEXPR(Type, Base) \
1065 case Stmt::Type##Class: \
1066 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1067#define STMT(Type, Base)
1068#include "clang/AST/StmtNodes.inc"
1069 default:
1070 llvm_unreachable("non-cast expressions not possible here");
1071 return 0;
1072 }
1073}
1074
1075void CastExpr::setCastPath(const CXXCastPath &Path) {
1076 assert(Path.size() == path_size());
1077 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1078}
1079
1080ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1081 CastKind Kind, Expr *Operand,
1082 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001083 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001084 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1085 void *Buffer =
1086 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1087 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001088 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001089 if (PathSize) E->setCastPath(*BasePath);
1090 return E;
1091}
1092
1093ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1094 unsigned PathSize) {
1095 void *Buffer =
1096 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1097 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1098}
1099
1100
1101CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001102 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001103 const CXXCastPath *BasePath,
1104 TypeSourceInfo *WrittenTy,
1105 SourceLocation L, SourceLocation R) {
1106 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1107 void *Buffer =
1108 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1109 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001110 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001111 if (PathSize) E->setCastPath(*BasePath);
1112 return E;
1113}
1114
1115CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1116 void *Buffer =
1117 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1118 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1119}
1120
Reid Spencer5f016e22007-07-11 17:01:13 +00001121/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1122/// corresponds to, e.g. "<<=".
1123const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1124 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001125 case BO_PtrMemD: return ".*";
1126 case BO_PtrMemI: return "->*";
1127 case BO_Mul: return "*";
1128 case BO_Div: return "/";
1129 case BO_Rem: return "%";
1130 case BO_Add: return "+";
1131 case BO_Sub: return "-";
1132 case BO_Shl: return "<<";
1133 case BO_Shr: return ">>";
1134 case BO_LT: return "<";
1135 case BO_GT: return ">";
1136 case BO_LE: return "<=";
1137 case BO_GE: return ">=";
1138 case BO_EQ: return "==";
1139 case BO_NE: return "!=";
1140 case BO_And: return "&";
1141 case BO_Xor: return "^";
1142 case BO_Or: return "|";
1143 case BO_LAnd: return "&&";
1144 case BO_LOr: return "||";
1145 case BO_Assign: return "=";
1146 case BO_MulAssign: return "*=";
1147 case BO_DivAssign: return "/=";
1148 case BO_RemAssign: return "%=";
1149 case BO_AddAssign: return "+=";
1150 case BO_SubAssign: return "-=";
1151 case BO_ShlAssign: return "<<=";
1152 case BO_ShrAssign: return ">>=";
1153 case BO_AndAssign: return "&=";
1154 case BO_XorAssign: return "^=";
1155 case BO_OrAssign: return "|=";
1156 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001157 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001158
1159 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001160}
1161
John McCall2de56d12010-08-25 11:45:40 +00001162BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001163BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1164 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001165 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001166 case OO_Plus: return BO_Add;
1167 case OO_Minus: return BO_Sub;
1168 case OO_Star: return BO_Mul;
1169 case OO_Slash: return BO_Div;
1170 case OO_Percent: return BO_Rem;
1171 case OO_Caret: return BO_Xor;
1172 case OO_Amp: return BO_And;
1173 case OO_Pipe: return BO_Or;
1174 case OO_Equal: return BO_Assign;
1175 case OO_Less: return BO_LT;
1176 case OO_Greater: return BO_GT;
1177 case OO_PlusEqual: return BO_AddAssign;
1178 case OO_MinusEqual: return BO_SubAssign;
1179 case OO_StarEqual: return BO_MulAssign;
1180 case OO_SlashEqual: return BO_DivAssign;
1181 case OO_PercentEqual: return BO_RemAssign;
1182 case OO_CaretEqual: return BO_XorAssign;
1183 case OO_AmpEqual: return BO_AndAssign;
1184 case OO_PipeEqual: return BO_OrAssign;
1185 case OO_LessLess: return BO_Shl;
1186 case OO_GreaterGreater: return BO_Shr;
1187 case OO_LessLessEqual: return BO_ShlAssign;
1188 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1189 case OO_EqualEqual: return BO_EQ;
1190 case OO_ExclaimEqual: return BO_NE;
1191 case OO_LessEqual: return BO_LE;
1192 case OO_GreaterEqual: return BO_GE;
1193 case OO_AmpAmp: return BO_LAnd;
1194 case OO_PipePipe: return BO_LOr;
1195 case OO_Comma: return BO_Comma;
1196 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001197 }
1198}
1199
1200OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1201 static const OverloadedOperatorKind OverOps[] = {
1202 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1203 OO_Star, OO_Slash, OO_Percent,
1204 OO_Plus, OO_Minus,
1205 OO_LessLess, OO_GreaterGreater,
1206 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1207 OO_EqualEqual, OO_ExclaimEqual,
1208 OO_Amp,
1209 OO_Caret,
1210 OO_Pipe,
1211 OO_AmpAmp,
1212 OO_PipePipe,
1213 OO_Equal, OO_StarEqual,
1214 OO_SlashEqual, OO_PercentEqual,
1215 OO_PlusEqual, OO_MinusEqual,
1216 OO_LessLessEqual, OO_GreaterGreaterEqual,
1217 OO_AmpEqual, OO_CaretEqual,
1218 OO_PipeEqual,
1219 OO_Comma
1220 };
1221 return OverOps[Opc];
1222}
1223
Ted Kremenek709210f2010-04-13 23:39:13 +00001224InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001225 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001226 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001227 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1228 false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001229 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001230 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Sean Huntc3021132010-05-05 15:23:54 +00001231 UnionFieldInit(0), HadArrayRangeDesignator(false)
1232{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001233 for (unsigned I = 0; I != numInits; ++I) {
1234 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001235 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001236 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001237 ExprBits.ValueDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001238 if (initExprs[I]->containsUnexpandedParameterPack())
1239 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001240 }
Sean Huntc3021132010-05-05 15:23:54 +00001241
Ted Kremenek709210f2010-04-13 23:39:13 +00001242 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001243}
Reid Spencer5f016e22007-07-11 17:01:13 +00001244
Ted Kremenek709210f2010-04-13 23:39:13 +00001245void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001246 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001247 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001248}
1249
Ted Kremenek709210f2010-04-13 23:39:13 +00001250void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001251 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001252}
1253
Ted Kremenek709210f2010-04-13 23:39:13 +00001254Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001255 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001256 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001257 InitExprs.back() = expr;
1258 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001259 }
Mike Stump1eb44332009-09-09 15:08:12 +00001260
Douglas Gregor4c678342009-01-28 21:54:33 +00001261 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1262 InitExprs[Init] = expr;
1263 return Result;
1264}
1265
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001266SourceRange InitListExpr::getSourceRange() const {
1267 if (SyntacticForm)
1268 return SyntacticForm->getSourceRange();
1269 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1270 if (Beg.isInvalid()) {
1271 // Find the first non-null initializer.
1272 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1273 E = InitExprs.end();
1274 I != E; ++I) {
1275 if (Stmt *S = *I) {
1276 Beg = S->getLocStart();
1277 break;
1278 }
1279 }
1280 }
1281 if (End.isInvalid()) {
1282 // Find the first non-null initializer from the end.
1283 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1284 E = InitExprs.rend();
1285 I != E; ++I) {
1286 if (Stmt *S = *I) {
1287 End = S->getSourceRange().getEnd();
1288 break;
1289 }
1290 }
1291 }
1292 return SourceRange(Beg, End);
1293}
1294
Steve Naroffbfdcae62008-09-04 15:31:07 +00001295/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001296///
1297const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001298 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001299 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001300}
1301
Mike Stump1eb44332009-09-09 15:08:12 +00001302SourceLocation BlockExpr::getCaretLocation() const {
1303 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001304}
Mike Stump1eb44332009-09-09 15:08:12 +00001305const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001306 return TheBlock->getBody();
1307}
Mike Stump1eb44332009-09-09 15:08:12 +00001308Stmt *BlockExpr::getBody() {
1309 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001310}
Steve Naroff56ee6892008-10-08 17:01:13 +00001311
1312
Reid Spencer5f016e22007-07-11 17:01:13 +00001313//===----------------------------------------------------------------------===//
1314// Generic Expression Routines
1315//===----------------------------------------------------------------------===//
1316
Chris Lattner026dc962009-02-14 07:37:35 +00001317/// isUnusedResultAWarning - Return true if this immediate expression should
1318/// be warned about if the result is unused. If so, fill in Loc and Ranges
1319/// with location to warn on and the source range[s] to report with the
1320/// warning.
1321bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001322 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001323 // Don't warn if the expr is type dependent. The type could end up
1324 // instantiating to void.
1325 if (isTypeDependent())
1326 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Reid Spencer5f016e22007-07-11 17:01:13 +00001328 switch (getStmtClass()) {
1329 default:
John McCall0faede62010-03-12 07:11:26 +00001330 if (getType()->isVoidType())
1331 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001332 Loc = getExprLoc();
1333 R1 = getSourceRange();
1334 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001336 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001337 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 case UnaryOperatorClass: {
1339 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001340
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001342 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001343 case UO_PostInc:
1344 case UO_PostDec:
1345 case UO_PreInc:
1346 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001347 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001348 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001350 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001351 return false;
1352 break;
John McCall2de56d12010-08-25 11:45:40 +00001353 case UO_Real:
1354 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001355 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001356 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1357 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001358 return false;
1359 break;
John McCall2de56d12010-08-25 11:45:40 +00001360 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001361 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001362 }
Chris Lattner026dc962009-02-14 07:37:35 +00001363 Loc = UO->getOperatorLoc();
1364 R1 = UO->getSubExpr()->getSourceRange();
1365 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001366 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001367 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001368 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001369 switch (BO->getOpcode()) {
1370 default:
1371 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001372 // Consider the RHS of comma for side effects. LHS was checked by
1373 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001374 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001375 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1376 // lvalue-ness) of an assignment written in a macro.
1377 if (IntegerLiteral *IE =
1378 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1379 if (IE->getValue() == 0)
1380 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001381 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1382 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001383 case BO_LAnd:
1384 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001385 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1386 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1387 return false;
1388 break;
John McCallbf0ee352010-02-16 04:10:53 +00001389 }
Chris Lattner026dc962009-02-14 07:37:35 +00001390 if (BO->isAssignmentOp())
1391 return false;
1392 Loc = BO->getOperatorLoc();
1393 R1 = BO->getLHS()->getSourceRange();
1394 R2 = BO->getRHS()->getSourceRange();
1395 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001396 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001397 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001398 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001399 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001400
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001401 case ConditionalOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001402 // The condition must be evaluated, but if either the LHS or RHS is a
1403 // warning, warn about them.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001404 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001405 if (Exp->getLHS() &&
Mike Stumpdf317bf2009-11-03 23:25:48 +00001406 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner026dc962009-02-14 07:37:35 +00001407 return true;
Mike Stumpdf317bf2009-11-03 23:25:48 +00001408 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001409 }
1410
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001412 // If the base pointer or element is to a volatile pointer/field, accessing
1413 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001414 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001415 return false;
1416 Loc = cast<MemberExpr>(this)->getMemberLoc();
1417 R1 = SourceRange(Loc, Loc);
1418 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1419 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001420
Reid Spencer5f016e22007-07-11 17:01:13 +00001421 case ArraySubscriptExprClass:
1422 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001423 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001424 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001425 return false;
1426 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1427 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1428 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1429 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001430
Reid Spencer5f016e22007-07-11 17:01:13 +00001431 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001432 case CXXOperatorCallExprClass:
1433 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001434 // If this is a direct call, get the callee.
1435 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001436 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001437 // If the callee has attribute pure, const, or warn_unused_result, warn
1438 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001439 //
1440 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1441 // updated to match for QoI.
1442 if (FD->getAttr<WarnUnusedResultAttr>() ||
1443 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1444 Loc = CE->getCallee()->getLocStart();
1445 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001447 if (unsigned NumArgs = CE->getNumArgs())
1448 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1449 CE->getArg(NumArgs-1)->getLocEnd());
1450 return true;
1451 }
Chris Lattner026dc962009-02-14 07:37:35 +00001452 }
1453 return false;
1454 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001455
1456 case CXXTemporaryObjectExprClass:
1457 case CXXConstructExprClass:
1458 return false;
1459
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001460 case ObjCMessageExprClass: {
1461 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1462 const ObjCMethodDecl *MD = ME->getMethodDecl();
1463 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1464 Loc = getExprLoc();
1465 return true;
1466 }
Chris Lattner026dc962009-02-14 07:37:35 +00001467 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001468 }
Mike Stump1eb44332009-09-09 15:08:12 +00001469
John McCall12f78a62010-12-02 01:19:52 +00001470 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001471 Loc = getExprLoc();
1472 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001473 return true;
John McCall12f78a62010-12-02 01:19:52 +00001474
Chris Lattner611b2ec2008-07-26 19:51:01 +00001475 case StmtExprClass: {
1476 // Statement exprs don't logically have side effects themselves, but are
1477 // sometimes used in macros in ways that give them a type that is unused.
1478 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1479 // however, if the result of the stmt expr is dead, we don't want to emit a
1480 // warning.
1481 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001482 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001483 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001484 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001485 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1486 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1487 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1488 }
Mike Stump1eb44332009-09-09 15:08:12 +00001489
John McCall0faede62010-03-12 07:11:26 +00001490 if (getType()->isVoidType())
1491 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001492 Loc = cast<StmtExpr>(this)->getLParenLoc();
1493 R1 = getSourceRange();
1494 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001495 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001496 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001497 // If this is an explicit cast to void, allow it. People do this when they
1498 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001499 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001500 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001501 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1502 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1503 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001504 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001505 if (getType()->isVoidType())
1506 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001507 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001508
Anders Carlsson58beed92009-11-17 17:11:23 +00001509 // If this is a cast to void or a constructor conversion, check the operand.
1510 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001511 if (CE->getCastKind() == CK_ToVoid ||
1512 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001513 return (cast<CastExpr>(this)->getSubExpr()
1514 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001515 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1516 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1517 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001518 }
Mike Stump1eb44332009-09-09 15:08:12 +00001519
Eli Friedman4be1f472008-05-19 21:24:43 +00001520 case ImplicitCastExprClass:
1521 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001522 return (cast<ImplicitCastExpr>(this)
1523 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001524
Chris Lattner04421082008-04-08 04:40:51 +00001525 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001526 return (cast<CXXDefaultArgExpr>(this)
1527 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001528
1529 case CXXNewExprClass:
1530 // FIXME: In theory, there might be new expressions that don't have side
1531 // effects (e.g. a placement new with an uninitialized POD).
1532 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001533 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001534 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001535 return (cast<CXXBindTemporaryExpr>(this)
1536 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001537 case ExprWithCleanupsClass:
1538 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001539 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001540 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001541}
1542
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001543/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001544/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001545bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001546 switch (getStmtClass()) {
1547 default:
1548 return false;
1549 case ObjCIvarRefExprClass:
1550 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001551 case Expr::UnaryOperatorClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001552 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001553 case ParenExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001554 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001555 case ImplicitCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001556 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001557 case CStyleCastExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001558 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001559 case DeclRefExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001560 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001561 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1562 if (VD->hasGlobalStorage())
1563 return true;
1564 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001565 // dereferencing to a pointer is always a gc'able candidate,
1566 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001567 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001568 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001569 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001570 return false;
1571 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001572 case MemberExprClass: {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001573 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001574 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001575 }
1576 case ArraySubscriptExprClass:
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001577 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001578 }
1579}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001580
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001581bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1582 if (isTypeDependent())
1583 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001584 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001585}
1586
Sebastian Redl369e51f2010-09-10 20:55:33 +00001587static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1588 Expr::CanThrowResult CT2) {
1589 // CanThrowResult constants are ordered so that the maximum is the correct
1590 // merge result.
1591 return CT1 > CT2 ? CT1 : CT2;
1592}
1593
1594static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1595 Expr *E = const_cast<Expr*>(CE);
1596 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001597 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001598 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1599 }
1600 return R;
1601}
1602
1603static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1604 bool NullThrows = true) {
1605 if (!D)
1606 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1607
1608 // See if we can get a function type from the decl somehow.
1609 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1610 if (!VD) // If we have no clue what we're calling, assume the worst.
1611 return Expr::CT_Can;
1612
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001613 // As an extension, we assume that __attribute__((nothrow)) functions don't
1614 // throw.
1615 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1616 return Expr::CT_Cannot;
1617
Sebastian Redl369e51f2010-09-10 20:55:33 +00001618 QualType T = VD->getType();
1619 const FunctionProtoType *FT;
1620 if ((FT = T->getAs<FunctionProtoType>())) {
1621 } else if (const PointerType *PT = T->getAs<PointerType>())
1622 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1623 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1624 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1625 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1626 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1627 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1628 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1629
1630 if (!FT)
1631 return Expr::CT_Can;
1632
1633 return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1634}
1635
1636static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1637 if (DC->isTypeDependent())
1638 return Expr::CT_Dependent;
1639
Sebastian Redl295995c2010-09-10 20:55:47 +00001640 if (!DC->getTypeAsWritten()->isReferenceType())
1641 return Expr::CT_Cannot;
1642
Sebastian Redl369e51f2010-09-10 20:55:33 +00001643 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1644}
1645
1646static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1647 const CXXTypeidExpr *DC) {
1648 if (DC->isTypeOperand())
1649 return Expr::CT_Cannot;
1650
1651 Expr *Op = DC->getExprOperand();
1652 if (Op->isTypeDependent())
1653 return Expr::CT_Dependent;
1654
1655 const RecordType *RT = Op->getType()->getAs<RecordType>();
1656 if (!RT)
1657 return Expr::CT_Cannot;
1658
1659 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1660 return Expr::CT_Cannot;
1661
1662 if (Op->Classify(C).isPRValue())
1663 return Expr::CT_Cannot;
1664
1665 return Expr::CT_Can;
1666}
1667
1668Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1669 // C++ [expr.unary.noexcept]p3:
1670 // [Can throw] if in a potentially-evaluated context the expression would
1671 // contain:
1672 switch (getStmtClass()) {
1673 case CXXThrowExprClass:
1674 // - a potentially evaluated throw-expression
1675 return CT_Can;
1676
1677 case CXXDynamicCastExprClass: {
1678 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1679 // where T is a reference type, that requires a run-time check
1680 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1681 if (CT == CT_Can)
1682 return CT;
1683 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1684 }
1685
1686 case CXXTypeidExprClass:
1687 // - a potentially evaluated typeid expression applied to a glvalue
1688 // expression whose type is a polymorphic class type
1689 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1690
1691 // - a potentially evaluated call to a function, member function, function
1692 // pointer, or member function pointer that does not have a non-throwing
1693 // exception-specification
1694 case CallExprClass:
1695 case CXXOperatorCallExprClass:
1696 case CXXMemberCallExprClass: {
1697 CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1698 if (CT == CT_Can)
1699 return CT;
1700 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1701 }
1702
Sebastian Redl295995c2010-09-10 20:55:47 +00001703 case CXXConstructExprClass:
1704 case CXXTemporaryObjectExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001705 CanThrowResult CT = CanCalleeThrow(
1706 cast<CXXConstructExpr>(this)->getConstructor());
1707 if (CT == CT_Can)
1708 return CT;
1709 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1710 }
1711
1712 case CXXNewExprClass: {
1713 CanThrowResult CT = MergeCanThrow(
1714 CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1715 CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1716 /*NullThrows*/false));
1717 if (CT == CT_Can)
1718 return CT;
1719 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1720 }
1721
1722 case CXXDeleteExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001723 CanThrowResult CT = CanCalleeThrow(
1724 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1725 if (CT == CT_Can)
1726 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001727 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1728 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1729 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1730 Arg = Cast->getSubExpr();
1731 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1732 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
1733 CanThrowResult CT2 = CanCalleeThrow(
1734 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1735 if (CT2 == CT_Can)
1736 return CT2;
1737 CT = MergeCanThrow(CT, CT2);
1738 }
1739 }
1740 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1741 }
1742
1743 case CXXBindTemporaryExprClass: {
1744 // The bound temporary has to be destroyed again, which might throw.
1745 CanThrowResult CT = CanCalleeThrow(
1746 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1747 if (CT == CT_Can)
1748 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001749 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1750 }
1751
1752 // ObjC message sends are like function calls, but never have exception
1753 // specs.
1754 case ObjCMessageExprClass:
1755 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001756 return CT_Can;
1757
1758 // Many other things have subexpressions, so we have to test those.
1759 // Some are simple:
1760 case ParenExprClass:
1761 case MemberExprClass:
1762 case CXXReinterpretCastExprClass:
1763 case CXXConstCastExprClass:
1764 case ConditionalOperatorClass:
1765 case CompoundLiteralExprClass:
1766 case ExtVectorElementExprClass:
1767 case InitListExprClass:
1768 case DesignatedInitExprClass:
1769 case ParenListExprClass:
1770 case VAArgExprClass:
1771 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00001772 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001773 case ObjCIvarRefExprClass:
1774 case ObjCIsaExprClass:
1775 case ShuffleVectorExprClass:
1776 return CanSubExprsThrow(C, this);
1777
1778 // Some might be dependent for other reasons.
1779 case UnaryOperatorClass:
1780 case ArraySubscriptExprClass:
1781 case ImplicitCastExprClass:
1782 case CStyleCastExprClass:
1783 case CXXStaticCastExprClass:
1784 case CXXFunctionalCastExprClass:
1785 case BinaryOperatorClass:
1786 case CompoundAssignOperatorClass: {
1787 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1788 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1789 }
1790
1791 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1792 case StmtExprClass:
1793 return CT_Can;
1794
1795 case ChooseExprClass:
1796 if (isTypeDependent() || isValueDependent())
1797 return CT_Dependent;
1798 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1799
1800 // Some expressions are always dependent.
1801 case DependentScopeDeclRefExprClass:
1802 case CXXUnresolvedConstructExprClass:
1803 case CXXDependentScopeMemberExprClass:
1804 return CT_Dependent;
1805
1806 default:
1807 // All other expressions don't have subexpressions, or else they are
1808 // unevaluated.
1809 return CT_Cannot;
1810 }
1811}
1812
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001813Expr* Expr::IgnoreParens() {
1814 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001815 while (true) {
1816 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1817 E = P->getSubExpr();
1818 continue;
1819 }
1820 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1821 if (P->getOpcode() == UO_Extension) {
1822 E = P->getSubExpr();
1823 continue;
1824 }
1825 }
1826 return E;
1827 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001828}
1829
Chris Lattner56f34942008-02-13 01:02:39 +00001830/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1831/// or CastExprs or ImplicitCastExprs, returning their operand.
1832Expr *Expr::IgnoreParenCasts() {
1833 Expr *E = this;
1834 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001835 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001836 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001837 continue;
1838 }
1839 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001840 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001841 continue;
1842 }
1843 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1844 if (P->getOpcode() == UO_Extension) {
1845 E = P->getSubExpr();
1846 continue;
1847 }
1848 }
1849 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001850 }
1851}
1852
John McCall9c5d70c2010-12-04 08:24:19 +00001853/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1854/// casts. This is intended purely as a temporary workaround for code
1855/// that hasn't yet been rewritten to do the right thing about those
1856/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00001857Expr *Expr::IgnoreParenLValueCasts() {
1858 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00001859 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00001860 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1861 E = P->getSubExpr();
1862 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00001863 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00001864 if (P->getCastKind() == CK_LValueToRValue) {
1865 E = P->getSubExpr();
1866 continue;
1867 }
John McCall9c5d70c2010-12-04 08:24:19 +00001868 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1869 if (P->getOpcode() == UO_Extension) {
1870 E = P->getSubExpr();
1871 continue;
1872 }
John McCallf6a16482010-12-04 03:47:34 +00001873 }
1874 break;
1875 }
1876 return E;
1877}
1878
John McCall2fc46bf2010-05-05 22:59:52 +00001879Expr *Expr::IgnoreParenImpCasts() {
1880 Expr *E = this;
1881 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001882 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001883 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001884 continue;
1885 }
1886 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001887 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001888 continue;
1889 }
1890 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1891 if (P->getOpcode() == UO_Extension) {
1892 E = P->getSubExpr();
1893 continue;
1894 }
1895 }
1896 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00001897 }
1898}
1899
Chris Lattnerecdd8412009-03-13 17:28:01 +00001900/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1901/// value (including ptr->int casts of the same size). Strip off any
1902/// ParenExpr or CastExprs, returning their operand.
1903Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1904 Expr *E = this;
1905 while (true) {
1906 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1907 E = P->getSubExpr();
1908 continue;
1909 }
Mike Stump1eb44332009-09-09 15:08:12 +00001910
Chris Lattnerecdd8412009-03-13 17:28:01 +00001911 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1912 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001913 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00001914 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001915
Chris Lattnerecdd8412009-03-13 17:28:01 +00001916 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1917 E = SE;
1918 continue;
1919 }
Mike Stump1eb44332009-09-09 15:08:12 +00001920
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001921 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001922 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001923 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001924 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00001925 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1926 E = SE;
1927 continue;
1928 }
1929 }
Mike Stump1eb44332009-09-09 15:08:12 +00001930
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001931 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1932 if (P->getOpcode() == UO_Extension) {
1933 E = P->getSubExpr();
1934 continue;
1935 }
1936 }
1937
Chris Lattnerecdd8412009-03-13 17:28:01 +00001938 return E;
1939 }
1940}
1941
Douglas Gregor6eef5192009-12-14 19:27:10 +00001942bool Expr::isDefaultArgument() const {
1943 const Expr *E = this;
1944 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1945 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00001946
Douglas Gregor6eef5192009-12-14 19:27:10 +00001947 return isa<CXXDefaultArgExpr>(E);
1948}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001949
Douglas Gregor2f599792010-04-02 18:24:57 +00001950/// \brief Skip over any no-op casts and any temporary-binding
1951/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00001952static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor2f599792010-04-02 18:24:57 +00001953 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001954 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001955 E = ICE->getSubExpr();
1956 else
1957 break;
1958 }
1959
1960 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1961 E = BE->getSubExpr();
1962
1963 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001964 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001965 E = ICE->getSubExpr();
1966 else
1967 break;
1968 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00001969
1970 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00001971}
1972
John McCall558d2ab2010-09-15 10:14:12 +00001973/// isTemporaryObject - Determines if this expression produces a
1974/// temporary of the given class type.
1975bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1976 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1977 return false;
1978
Anders Carlssonf8b30152010-11-28 16:40:49 +00001979 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00001980
John McCall58277b52010-09-15 20:59:13 +00001981 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001982 if (!E->Classify(C).isPRValue()) {
1983 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00001984 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001985 return false;
1986 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001987
John McCall19e60ad2010-09-16 06:57:56 +00001988 // Black-list a few cases which yield pr-values of class type that don't
1989 // refer to temporaries of that type:
1990
1991 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00001992 if (isa<ImplicitCastExpr>(E)) {
1993 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
1994 case CK_DerivedToBase:
1995 case CK_UncheckedDerivedToBase:
1996 return false;
1997 default:
1998 break;
1999 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002000 }
2001
John McCall19e60ad2010-09-16 06:57:56 +00002002 // - member expressions (all)
2003 if (isa<MemberExpr>(E))
2004 return false;
2005
John McCall56ca35d2011-02-17 10:25:35 +00002006 // - opaque values (all)
2007 if (isa<OpaqueValueExpr>(E))
2008 return false;
2009
John McCall558d2ab2010-09-15 10:14:12 +00002010 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002011}
2012
Douglas Gregor898574e2008-12-05 23:32:09 +00002013/// hasAnyTypeDependentArguments - Determines if any of the expressions
2014/// in Exprs is type-dependent.
2015bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2016 for (unsigned I = 0; I < NumExprs; ++I)
2017 if (Exprs[I]->isTypeDependent())
2018 return true;
2019
2020 return false;
2021}
2022
2023/// hasAnyValueDependentArguments - Determines if any of the expressions
2024/// in Exprs is value-dependent.
2025bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2026 for (unsigned I = 0; I < NumExprs; ++I)
2027 if (Exprs[I]->isValueDependent())
2028 return true;
2029
2030 return false;
2031}
2032
John McCall4204f072010-08-02 21:13:48 +00002033bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002034 // This function is attempting whether an expression is an initializer
2035 // which can be evaluated at compile-time. isEvaluatable handles most
2036 // of the cases, but it can't deal with some initializer-specific
2037 // expressions, and it can't deal with aggregates; we deal with those here,
2038 // and fall back to isEvaluatable for the other cases.
2039
John McCall4204f072010-08-02 21:13:48 +00002040 // If we ever capture reference-binding directly in the AST, we can
2041 // kill the second parameter.
2042
2043 if (IsForRef) {
2044 EvalResult Result;
2045 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2046 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002047
Anders Carlssone8a32b82008-11-24 05:23:59 +00002048 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002049 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002050 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002051 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002052 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002053 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002054 case CXXTemporaryObjectExprClass:
2055 case CXXConstructExprClass: {
2056 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002057
2058 // Only if it's
2059 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002060 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002061 if (!CE->getNumArgs()) return true;
2062
2063 // 2) an elidable trivial copy construction of an operand which is
2064 // itself a constant initializer. Note that we consider the
2065 // operand on its own, *not* as a reference binding.
2066 return CE->isElidable() &&
2067 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002068 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002069 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002070 // This handles gcc's extension that allows global initializers like
2071 // "struct x {int x;} x = (struct x) {};".
2072 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002073 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002074 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002075 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002076 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002077 // FIXME: This doesn't deal with fields with reference types correctly.
2078 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2079 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002080 const InitListExpr *Exp = cast<InitListExpr>(this);
2081 unsigned numInits = Exp->getNumInits();
2082 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002083 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002084 return false;
2085 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002086 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002087 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002088 case ImplicitValueInitExprClass:
2089 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002090 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002091 return cast<ParenExpr>(this)->getSubExpr()
2092 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002093 case ChooseExprClass:
2094 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2095 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002096 case UnaryOperatorClass: {
2097 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002098 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002099 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002100 break;
2101 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002102 case BinaryOperatorClass: {
2103 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2104 // but this handles the common case.
2105 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002106 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002107 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2108 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2109 return true;
2110 break;
2111 }
John McCall4204f072010-08-02 21:13:48 +00002112 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002113 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002114 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002115 case CStyleCastExprClass:
2116 // Handle casts with a destination that's a struct or union; this
2117 // deals with both the gcc no-op struct cast extension and the
2118 // cast-to-union extension.
2119 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002120 return cast<CastExpr>(this)->getSubExpr()
2121 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002122
Chris Lattner430656e2009-10-13 22:12:09 +00002123 // Integer->integer casts can be handled here, which is important for
2124 // things like (int)(&&x-&&y). Scary but true.
2125 if (getType()->isIntegerType() &&
2126 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002127 return cast<CastExpr>(this)->getSubExpr()
2128 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002129
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002130 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002131 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002132 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002133}
2134
Reid Spencer5f016e22007-07-11 17:01:13 +00002135/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
2136/// integer constant expression with the value zero, or if this is one that is
2137/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00002138bool Expr::isNullPointerConstant(ASTContext &Ctx,
2139 NullPointerConstantValueDependence NPC) const {
2140 if (isValueDependent()) {
2141 switch (NPC) {
2142 case NPC_NeverValueDependent:
2143 assert(false && "Unexpected value dependent expression!");
2144 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002145
Douglas Gregorce940492009-09-25 04:25:58 +00002146 case NPC_ValueDependentIsNull:
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002147 return isTypeDependent() || getType()->isIntegralType(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00002148
Douglas Gregorce940492009-09-25 04:25:58 +00002149 case NPC_ValueDependentIsNotNull:
2150 return false;
2151 }
2152 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002153
Sebastian Redl07779722008-10-31 14:43:28 +00002154 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002155 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002156 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002157 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002158 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002159 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002160 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002161 Pointee->isVoidType() && // to void*
2162 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002163 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002164 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002165 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002166 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2167 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002168 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002169 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2170 // Accept ((void*)0) as a null pointer constant, as many other
2171 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002172 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002173 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002174 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002175 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002176 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002177 } else if (isa<GNUNullExpr>(this)) {
2178 // The GNU __null extension is always a null pointer constant.
2179 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002180 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002181
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002182 // C++0x nullptr_t is always a null pointer constant.
2183 if (getType()->isNullPtrType())
2184 return true;
2185
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002186 if (const RecordType *UT = getType()->getAsUnionType())
2187 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2188 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2189 const Expr *InitExpr = CLE->getInitializer();
2190 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2191 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2192 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002193 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002194 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002195 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002196 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002197
Reid Spencer5f016e22007-07-11 17:01:13 +00002198 // If we have an integer constant expression, we need to *evaluate* it and
2199 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002200 llvm::APSInt Result;
2201 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002202}
Steve Naroff31a45842007-07-28 23:10:27 +00002203
John McCallf6a16482010-12-04 03:47:34 +00002204/// \brief If this expression is an l-value for an Objective C
2205/// property, find the underlying property reference expression.
2206const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2207 const Expr *E = this;
2208 while (true) {
2209 assert((E->getValueKind() == VK_LValue &&
2210 E->getObjectKind() == OK_ObjCProperty) &&
2211 "expression is not a property reference");
2212 E = E->IgnoreParenCasts();
2213 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2214 if (BO->getOpcode() == BO_Comma) {
2215 E = BO->getRHS();
2216 continue;
2217 }
2218 }
2219
2220 break;
2221 }
2222
2223 return cast<ObjCPropertyRefExpr>(E);
2224}
2225
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002226FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002227 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002228
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002229 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002230 if (ICE->getCastKind() == CK_LValueToRValue ||
2231 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002232 E = ICE->getSubExpr()->IgnoreParens();
2233 else
2234 break;
2235 }
2236
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002237 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002238 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002239 if (Field->isBitField())
2240 return Field;
2241
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002242 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2243 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2244 if (Field->isBitField())
2245 return Field;
2246
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002247 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2248 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2249 return BinOp->getLHS()->getBitField();
2250
2251 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002252}
2253
Anders Carlsson09380262010-01-31 17:18:49 +00002254bool Expr::refersToVectorElement() const {
2255 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002256
Anders Carlsson09380262010-01-31 17:18:49 +00002257 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002258 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002259 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002260 E = ICE->getSubExpr()->IgnoreParens();
2261 else
2262 break;
2263 }
Sean Huntc3021132010-05-05 15:23:54 +00002264
Anders Carlsson09380262010-01-31 17:18:49 +00002265 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2266 return ASE->getBase()->getType()->isVectorType();
2267
2268 if (isa<ExtVectorElementExpr>(E))
2269 return true;
2270
2271 return false;
2272}
2273
Chris Lattner2140e902009-02-16 22:14:05 +00002274/// isArrow - Return true if the base expression is a pointer to vector,
2275/// return false if the base expression is a vector.
2276bool ExtVectorElementExpr::isArrow() const {
2277 return getBase()->getType()->isPointerType();
2278}
2279
Nate Begeman213541a2008-04-18 23:10:10 +00002280unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002281 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002282 return VT->getNumElements();
2283 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002284}
2285
Nate Begeman8a997642008-05-09 06:41:27 +00002286/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002287bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002288 // FIXME: Refactor this code to an accessor on the AST node which returns the
2289 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002290 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002291
2292 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002293 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002294 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002295
Nate Begeman190d6a22009-01-18 02:01:21 +00002296 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002297 if (Comp[0] == 's' || Comp[0] == 'S')
2298 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002299
Daniel Dunbar15027422009-10-17 23:53:04 +00002300 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2301 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002302 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002303
Steve Narofffec0b492007-07-30 03:29:09 +00002304 return false;
2305}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002306
Nate Begeman8a997642008-05-09 06:41:27 +00002307/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002308void ExtVectorElementExpr::getEncodedElementAccess(
2309 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002310 llvm::StringRef Comp = Accessor->getName();
2311 if (Comp[0] == 's' || Comp[0] == 'S')
2312 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002313
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002314 bool isHi = Comp == "hi";
2315 bool isLo = Comp == "lo";
2316 bool isEven = Comp == "even";
2317 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002318
Nate Begeman8a997642008-05-09 06:41:27 +00002319 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2320 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002321
Nate Begeman8a997642008-05-09 06:41:27 +00002322 if (isHi)
2323 Index = e + i;
2324 else if (isLo)
2325 Index = i;
2326 else if (isEven)
2327 Index = 2 * i;
2328 else if (isOdd)
2329 Index = 2 * i + 1;
2330 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002331 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002332
Nate Begeman3b8d1162008-05-13 21:03:02 +00002333 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002334 }
Nate Begeman8a997642008-05-09 06:41:27 +00002335}
2336
Douglas Gregor04badcf2010-04-21 00:45:42 +00002337ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002338 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002339 SourceLocation LBracLoc,
2340 SourceLocation SuperLoc,
2341 bool IsInstanceSuper,
2342 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002343 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002344 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002345 ObjCMethodDecl *Method,
2346 Expr **Args, unsigned NumArgs,
2347 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002348 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002349 /*TypeDependent=*/false, /*ValueDependent=*/false,
2350 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002351 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2352 HasMethod(Method != 0), SuperLoc(SuperLoc),
2353 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2354 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002355 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002356{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002357 setReceiverPointer(SuperType.getAsOpaquePtr());
2358 if (NumArgs)
2359 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002360}
2361
Douglas Gregor04badcf2010-04-21 00:45:42 +00002362ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002363 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002364 SourceLocation LBracLoc,
2365 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002366 Selector Sel,
2367 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002368 ObjCMethodDecl *Method,
2369 Expr **Args, unsigned NumArgs,
2370 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002371 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002372 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002373 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2374 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2375 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002376 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002377{
2378 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002379 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002380 for (unsigned I = 0; I != NumArgs; ++I) {
2381 if (Args[I]->isTypeDependent())
2382 ExprBits.TypeDependent = true;
2383 if (Args[I]->isValueDependent())
2384 ExprBits.ValueDependent = true;
2385 if (Args[I]->containsUnexpandedParameterPack())
2386 ExprBits.ContainsUnexpandedParameterPack = true;
2387
2388 MyArgs[I] = Args[I];
2389 }
Ted Kremenek4df728e2008-06-24 15:50:53 +00002390}
2391
Douglas Gregor04badcf2010-04-21 00:45:42 +00002392ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002393 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002394 SourceLocation LBracLoc,
2395 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002396 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002397 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002398 ObjCMethodDecl *Method,
2399 Expr **Args, unsigned NumArgs,
2400 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002401 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002402 Receiver->isTypeDependent(),
2403 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002404 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2405 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2406 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002407 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002408{
2409 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002410 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002411 for (unsigned I = 0; I != NumArgs; ++I) {
2412 if (Args[I]->isTypeDependent())
2413 ExprBits.TypeDependent = true;
2414 if (Args[I]->isValueDependent())
2415 ExprBits.ValueDependent = true;
2416 if (Args[I]->containsUnexpandedParameterPack())
2417 ExprBits.ContainsUnexpandedParameterPack = true;
2418
2419 MyArgs[I] = Args[I];
2420 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002421}
2422
Douglas Gregor04badcf2010-04-21 00:45:42 +00002423ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002424 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002425 SourceLocation LBracLoc,
2426 SourceLocation SuperLoc,
2427 bool IsInstanceSuper,
2428 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002429 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002430 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002431 ObjCMethodDecl *Method,
2432 Expr **Args, unsigned NumArgs,
2433 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002434 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002435 NumArgs * sizeof(Expr *);
2436 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002437 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002438 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002439 RBracLoc);
2440}
2441
2442ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002443 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002444 SourceLocation LBracLoc,
2445 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002446 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002447 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002448 ObjCMethodDecl *Method,
2449 Expr **Args, unsigned NumArgs,
2450 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002451 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002452 NumArgs * sizeof(Expr *);
2453 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002454 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2455 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002456}
2457
2458ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002459 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002460 SourceLocation LBracLoc,
2461 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002462 Selector Sel,
2463 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002464 ObjCMethodDecl *Method,
2465 Expr **Args, unsigned NumArgs,
2466 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002467 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002468 NumArgs * sizeof(Expr *);
2469 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002470 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2471 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002472}
2473
Sean Huntc3021132010-05-05 15:23:54 +00002474ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002475 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002476 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002477 NumArgs * sizeof(Expr *);
2478 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2479 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2480}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002481
2482SourceRange ObjCMessageExpr::getReceiverRange() const {
2483 switch (getReceiverKind()) {
2484 case Instance:
2485 return getInstanceReceiver()->getSourceRange();
2486
2487 case Class:
2488 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2489
2490 case SuperInstance:
2491 case SuperClass:
2492 return getSuperLoc();
2493 }
2494
2495 return SourceLocation();
2496}
2497
Douglas Gregor04badcf2010-04-21 00:45:42 +00002498Selector ObjCMessageExpr::getSelector() const {
2499 if (HasMethod)
2500 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2501 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002502 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002503}
2504
2505ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2506 switch (getReceiverKind()) {
2507 case Instance:
2508 if (const ObjCObjectPointerType *Ptr
2509 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2510 return Ptr->getInterfaceDecl();
2511 break;
2512
2513 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002514 if (const ObjCObjectType *Ty
2515 = getClassReceiver()->getAs<ObjCObjectType>())
2516 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002517 break;
2518
2519 case SuperInstance:
2520 if (const ObjCObjectPointerType *Ptr
2521 = getSuperType()->getAs<ObjCObjectPointerType>())
2522 return Ptr->getInterfaceDecl();
2523 break;
2524
2525 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002526 if (const ObjCObjectType *Iface
2527 = getSuperType()->getAs<ObjCObjectType>())
2528 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002529 break;
2530 }
2531
2532 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002533}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002534
Jay Foad4ba2a172011-01-12 09:06:06 +00002535bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002536 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002537}
2538
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002539ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2540 QualType Type, SourceLocation BLoc,
2541 SourceLocation RP)
2542 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2543 Type->isDependentType(), Type->isDependentType(),
2544 Type->containsUnexpandedParameterPack()),
2545 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2546{
2547 SubExprs = new (C) Stmt*[nexpr];
2548 for (unsigned i = 0; i < nexpr; i++) {
2549 if (args[i]->isTypeDependent())
2550 ExprBits.TypeDependent = true;
2551 if (args[i]->isValueDependent())
2552 ExprBits.ValueDependent = true;
2553 if (args[i]->containsUnexpandedParameterPack())
2554 ExprBits.ContainsUnexpandedParameterPack = true;
2555
2556 SubExprs[i] = args[i];
2557 }
2558}
2559
Nate Begeman888376a2009-08-12 02:28:50 +00002560void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2561 unsigned NumExprs) {
2562 if (SubExprs) C.Deallocate(SubExprs);
2563
2564 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002565 this->NumExprs = NumExprs;
2566 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002567}
Nate Begeman888376a2009-08-12 02:28:50 +00002568
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002569//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002570// DesignatedInitExpr
2571//===----------------------------------------------------------------------===//
2572
2573IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2574 assert(Kind == FieldDesignator && "Only valid on a field designator");
2575 if (Field.NameOrField & 0x01)
2576 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2577 else
2578 return getField()->getIdentifier();
2579}
2580
Sean Huntc3021132010-05-05 15:23:54 +00002581DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002582 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002583 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002584 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002585 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002586 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002587 unsigned NumIndexExprs,
2588 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002589 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002590 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002591 Init->isTypeDependent(), Init->isValueDependent(),
2592 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00002593 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2594 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002595 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002596
2597 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00002598 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00002599 *Child++ = Init;
2600
2601 // Copy the designators and their subexpressions, computing
2602 // value-dependence along the way.
2603 unsigned IndexIdx = 0;
2604 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002605 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002606
2607 if (this->Designators[I].isArrayDesignator()) {
2608 // Compute type- and value-dependence.
2609 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002610 if (Index->isTypeDependent() || Index->isValueDependent())
2611 ExprBits.ValueDependent = true;
2612
2613 // Propagate unexpanded parameter packs.
2614 if (Index->containsUnexpandedParameterPack())
2615 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002616
2617 // Copy the index expressions into permanent storage.
2618 *Child++ = IndexExprs[IndexIdx++];
2619 } else if (this->Designators[I].isArrayRangeDesignator()) {
2620 // Compute type- and value-dependence.
2621 Expr *Start = IndexExprs[IndexIdx];
2622 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002623 if (Start->isTypeDependent() || Start->isValueDependent() ||
2624 End->isTypeDependent() || End->isValueDependent())
2625 ExprBits.ValueDependent = true;
2626
2627 // Propagate unexpanded parameter packs.
2628 if (Start->containsUnexpandedParameterPack() ||
2629 End->containsUnexpandedParameterPack())
2630 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002631
2632 // Copy the start/end expressions into permanent storage.
2633 *Child++ = IndexExprs[IndexIdx++];
2634 *Child++ = IndexExprs[IndexIdx++];
2635 }
2636 }
2637
2638 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002639}
2640
Douglas Gregor05c13a32009-01-22 00:58:24 +00002641DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002642DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002643 unsigned NumDesignators,
2644 Expr **IndexExprs, unsigned NumIndexExprs,
2645 SourceLocation ColonOrEqualLoc,
2646 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002647 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002648 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002649 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002650 ColonOrEqualLoc, UsesColonSyntax,
2651 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002652}
2653
Mike Stump1eb44332009-09-09 15:08:12 +00002654DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002655 unsigned NumIndexExprs) {
2656 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2657 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2658 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2659}
2660
Douglas Gregor319d57f2010-01-06 23:17:19 +00002661void DesignatedInitExpr::setDesignators(ASTContext &C,
2662 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002663 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002664 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002665 NumDesignators = NumDesigs;
2666 for (unsigned I = 0; I != NumDesigs; ++I)
2667 Designators[I] = Desigs[I];
2668}
2669
Douglas Gregor05c13a32009-01-22 00:58:24 +00002670SourceRange DesignatedInitExpr::getSourceRange() const {
2671 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002672 Designator &First =
2673 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002674 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002675 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002676 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2677 else
2678 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2679 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002680 StartLoc =
2681 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002682 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2683}
2684
Douglas Gregor05c13a32009-01-22 00:58:24 +00002685Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2686 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2687 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2688 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002689 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2690 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2691}
2692
2693Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002694 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002695 "Requires array range designator");
2696 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2697 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002698 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2699 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2700}
2701
2702Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002703 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002704 "Requires array range designator");
2705 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2706 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002707 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2708 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2709}
2710
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002711/// \brief Replaces the designator at index @p Idx with the series
2712/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002713void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002714 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002715 const Designator *Last) {
2716 unsigned NumNewDesignators = Last - First;
2717 if (NumNewDesignators == 0) {
2718 std::copy_backward(Designators + Idx + 1,
2719 Designators + NumDesignators,
2720 Designators + Idx);
2721 --NumNewDesignators;
2722 return;
2723 } else if (NumNewDesignators == 1) {
2724 Designators[Idx] = *First;
2725 return;
2726 }
2727
Mike Stump1eb44332009-09-09 15:08:12 +00002728 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002729 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002730 std::copy(Designators, Designators + Idx, NewDesignators);
2731 std::copy(First, Last, NewDesignators + Idx);
2732 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2733 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002734 Designators = NewDesignators;
2735 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2736}
2737
Mike Stump1eb44332009-09-09 15:08:12 +00002738ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002739 Expr **exprs, unsigned nexprs,
2740 SourceLocation rparenloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002741 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2742 false, false, false),
2743 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002744
Nate Begeman2ef13e52009-08-10 23:49:36 +00002745 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002746 for (unsigned i = 0; i != nexprs; ++i) {
2747 if (exprs[i]->isTypeDependent())
2748 ExprBits.TypeDependent = true;
2749 if (exprs[i]->isValueDependent())
2750 ExprBits.ValueDependent = true;
2751 if (exprs[i]->containsUnexpandedParameterPack())
2752 ExprBits.ContainsUnexpandedParameterPack = true;
2753
Nate Begeman2ef13e52009-08-10 23:49:36 +00002754 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002755 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00002756}
2757
John McCalle996ffd2011-02-16 08:02:54 +00002758const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
2759 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
2760 e = ewc->getSubExpr();
2761 e = cast<CXXConstructExpr>(e)->getArg(0);
2762 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
2763 e = ice->getSubExpr();
2764 return cast<OpaqueValueExpr>(e);
2765}
2766
Douglas Gregor05c13a32009-01-22 00:58:24 +00002767//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002768// ExprIterator.
2769//===----------------------------------------------------------------------===//
2770
2771Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2772Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2773Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2774const Expr* ConstExprIterator::operator[](size_t idx) const {
2775 return cast<Expr>(I[idx]);
2776}
2777const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2778const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2779
2780//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002781// Child Iterators for iterating over subexpressions/substatements
2782//===----------------------------------------------------------------------===//
2783
Sebastian Redl05189992008-11-11 17:56:53 +00002784// SizeOfAlignOfExpr
John McCall63c00d72011-02-09 08:16:59 +00002785Stmt::child_range SizeOfAlignOfExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00002786 // If this is of a type and the type is a VLA type (and not a typedef), the
2787 // size expression of the VLA needs to be treated as an executable expression.
2788 // Why isn't this weirdness documented better in StmtIterator?
2789 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00002790 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00002791 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00002792 return child_range(child_iterator(T), child_iterator());
2793 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00002794 }
John McCall63c00d72011-02-09 08:16:59 +00002795 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002796}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002797
Steve Naroff563477d2007-09-18 23:55:05 +00002798// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00002799Stmt::child_range ObjCMessageExpr::children() {
2800 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00002801 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00002802 begin = reinterpret_cast<Stmt **>(this + 1);
2803 else
2804 begin = reinterpret_cast<Stmt **>(getArgs());
2805 return child_range(begin,
2806 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00002807}
2808
Steve Naroff4eb206b2008-09-03 18:15:37 +00002809// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00002810BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00002811 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00002812 bool constAdded)
Douglas Gregord967e312011-01-19 21:52:31 +00002813 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00002814 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00002815 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00002816{
Douglas Gregord967e312011-01-19 21:52:31 +00002817 bool TypeDependent = false;
2818 bool ValueDependent = false;
2819 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
2820 ExprBits.TypeDependent = TypeDependent;
2821 ExprBits.ValueDependent = ValueDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00002822}
2823