blob: 6280d633aa3d564daa865e1b8107342fe9a6c4bc [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 McCall63c00d72011-02-09 08:16:59 +00001597 Expr::child_iterator I, IE;
1598 for (llvm::tie(I, IE) = E->children();
Sebastian Redl369e51f2010-09-10 20:55:33 +00001599 I != IE && R != Expr::CT_Can; ++I) {
1600 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1601 }
1602 return R;
1603}
1604
1605static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1606 bool NullThrows = true) {
1607 if (!D)
1608 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1609
1610 // See if we can get a function type from the decl somehow.
1611 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1612 if (!VD) // If we have no clue what we're calling, assume the worst.
1613 return Expr::CT_Can;
1614
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001615 // As an extension, we assume that __attribute__((nothrow)) functions don't
1616 // throw.
1617 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1618 return Expr::CT_Cannot;
1619
Sebastian Redl369e51f2010-09-10 20:55:33 +00001620 QualType T = VD->getType();
1621 const FunctionProtoType *FT;
1622 if ((FT = T->getAs<FunctionProtoType>())) {
1623 } else if (const PointerType *PT = T->getAs<PointerType>())
1624 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1625 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1626 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1627 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1628 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1629 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1630 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1631
1632 if (!FT)
1633 return Expr::CT_Can;
1634
1635 return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1636}
1637
1638static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1639 if (DC->isTypeDependent())
1640 return Expr::CT_Dependent;
1641
Sebastian Redl295995c2010-09-10 20:55:47 +00001642 if (!DC->getTypeAsWritten()->isReferenceType())
1643 return Expr::CT_Cannot;
1644
Sebastian Redl369e51f2010-09-10 20:55:33 +00001645 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1646}
1647
1648static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1649 const CXXTypeidExpr *DC) {
1650 if (DC->isTypeOperand())
1651 return Expr::CT_Cannot;
1652
1653 Expr *Op = DC->getExprOperand();
1654 if (Op->isTypeDependent())
1655 return Expr::CT_Dependent;
1656
1657 const RecordType *RT = Op->getType()->getAs<RecordType>();
1658 if (!RT)
1659 return Expr::CT_Cannot;
1660
1661 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1662 return Expr::CT_Cannot;
1663
1664 if (Op->Classify(C).isPRValue())
1665 return Expr::CT_Cannot;
1666
1667 return Expr::CT_Can;
1668}
1669
1670Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1671 // C++ [expr.unary.noexcept]p3:
1672 // [Can throw] if in a potentially-evaluated context the expression would
1673 // contain:
1674 switch (getStmtClass()) {
1675 case CXXThrowExprClass:
1676 // - a potentially evaluated throw-expression
1677 return CT_Can;
1678
1679 case CXXDynamicCastExprClass: {
1680 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1681 // where T is a reference type, that requires a run-time check
1682 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1683 if (CT == CT_Can)
1684 return CT;
1685 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1686 }
1687
1688 case CXXTypeidExprClass:
1689 // - a potentially evaluated typeid expression applied to a glvalue
1690 // expression whose type is a polymorphic class type
1691 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1692
1693 // - a potentially evaluated call to a function, member function, function
1694 // pointer, or member function pointer that does not have a non-throwing
1695 // exception-specification
1696 case CallExprClass:
1697 case CXXOperatorCallExprClass:
1698 case CXXMemberCallExprClass: {
1699 CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1700 if (CT == CT_Can)
1701 return CT;
1702 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1703 }
1704
Sebastian Redl295995c2010-09-10 20:55:47 +00001705 case CXXConstructExprClass:
1706 case CXXTemporaryObjectExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001707 CanThrowResult CT = CanCalleeThrow(
1708 cast<CXXConstructExpr>(this)->getConstructor());
1709 if (CT == CT_Can)
1710 return CT;
1711 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1712 }
1713
1714 case CXXNewExprClass: {
1715 CanThrowResult CT = MergeCanThrow(
1716 CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1717 CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1718 /*NullThrows*/false));
1719 if (CT == CT_Can)
1720 return CT;
1721 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1722 }
1723
1724 case CXXDeleteExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001725 CanThrowResult CT = CanCalleeThrow(
1726 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1727 if (CT == CT_Can)
1728 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001729 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1730 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1731 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1732 Arg = Cast->getSubExpr();
1733 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1734 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
1735 CanThrowResult CT2 = CanCalleeThrow(
1736 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1737 if (CT2 == CT_Can)
1738 return CT2;
1739 CT = MergeCanThrow(CT, CT2);
1740 }
1741 }
1742 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1743 }
1744
1745 case CXXBindTemporaryExprClass: {
1746 // The bound temporary has to be destroyed again, which might throw.
1747 CanThrowResult CT = CanCalleeThrow(
1748 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1749 if (CT == CT_Can)
1750 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001751 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1752 }
1753
1754 // ObjC message sends are like function calls, but never have exception
1755 // specs.
1756 case ObjCMessageExprClass:
1757 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001758 return CT_Can;
1759
1760 // Many other things have subexpressions, so we have to test those.
1761 // Some are simple:
1762 case ParenExprClass:
1763 case MemberExprClass:
1764 case CXXReinterpretCastExprClass:
1765 case CXXConstCastExprClass:
1766 case ConditionalOperatorClass:
1767 case CompoundLiteralExprClass:
1768 case ExtVectorElementExprClass:
1769 case InitListExprClass:
1770 case DesignatedInitExprClass:
1771 case ParenListExprClass:
1772 case VAArgExprClass:
1773 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00001774 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001775 case ObjCIvarRefExprClass:
1776 case ObjCIsaExprClass:
1777 case ShuffleVectorExprClass:
1778 return CanSubExprsThrow(C, this);
1779
1780 // Some might be dependent for other reasons.
1781 case UnaryOperatorClass:
1782 case ArraySubscriptExprClass:
1783 case ImplicitCastExprClass:
1784 case CStyleCastExprClass:
1785 case CXXStaticCastExprClass:
1786 case CXXFunctionalCastExprClass:
1787 case BinaryOperatorClass:
1788 case CompoundAssignOperatorClass: {
1789 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1790 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1791 }
1792
1793 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1794 case StmtExprClass:
1795 return CT_Can;
1796
1797 case ChooseExprClass:
1798 if (isTypeDependent() || isValueDependent())
1799 return CT_Dependent;
1800 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1801
1802 // Some expressions are always dependent.
1803 case DependentScopeDeclRefExprClass:
1804 case CXXUnresolvedConstructExprClass:
1805 case CXXDependentScopeMemberExprClass:
1806 return CT_Dependent;
1807
1808 default:
1809 // All other expressions don't have subexpressions, or else they are
1810 // unevaluated.
1811 return CT_Cannot;
1812 }
1813}
1814
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001815Expr* Expr::IgnoreParens() {
1816 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001817 while (true) {
1818 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1819 E = P->getSubExpr();
1820 continue;
1821 }
1822 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1823 if (P->getOpcode() == UO_Extension) {
1824 E = P->getSubExpr();
1825 continue;
1826 }
1827 }
1828 return E;
1829 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001830}
1831
Chris Lattner56f34942008-02-13 01:02:39 +00001832/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1833/// or CastExprs or ImplicitCastExprs, returning their operand.
1834Expr *Expr::IgnoreParenCasts() {
1835 Expr *E = this;
1836 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001837 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001838 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001839 continue;
1840 }
1841 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001842 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001843 continue;
1844 }
1845 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1846 if (P->getOpcode() == UO_Extension) {
1847 E = P->getSubExpr();
1848 continue;
1849 }
1850 }
1851 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001852 }
1853}
1854
John McCall9c5d70c2010-12-04 08:24:19 +00001855/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1856/// casts. This is intended purely as a temporary workaround for code
1857/// that hasn't yet been rewritten to do the right thing about those
1858/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00001859Expr *Expr::IgnoreParenLValueCasts() {
1860 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00001861 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00001862 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1863 E = P->getSubExpr();
1864 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00001865 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00001866 if (P->getCastKind() == CK_LValueToRValue) {
1867 E = P->getSubExpr();
1868 continue;
1869 }
John McCall9c5d70c2010-12-04 08:24:19 +00001870 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1871 if (P->getOpcode() == UO_Extension) {
1872 E = P->getSubExpr();
1873 continue;
1874 }
John McCallf6a16482010-12-04 03:47:34 +00001875 }
1876 break;
1877 }
1878 return E;
1879}
1880
John McCall2fc46bf2010-05-05 22:59:52 +00001881Expr *Expr::IgnoreParenImpCasts() {
1882 Expr *E = this;
1883 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001884 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001885 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001886 continue;
1887 }
1888 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001889 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001890 continue;
1891 }
1892 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1893 if (P->getOpcode() == UO_Extension) {
1894 E = P->getSubExpr();
1895 continue;
1896 }
1897 }
1898 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00001899 }
1900}
1901
Chris Lattnerecdd8412009-03-13 17:28:01 +00001902/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1903/// value (including ptr->int casts of the same size). Strip off any
1904/// ParenExpr or CastExprs, returning their operand.
1905Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1906 Expr *E = this;
1907 while (true) {
1908 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1909 E = P->getSubExpr();
1910 continue;
1911 }
Mike Stump1eb44332009-09-09 15:08:12 +00001912
Chris Lattnerecdd8412009-03-13 17:28:01 +00001913 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1914 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00001915 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00001916 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00001917
Chris Lattnerecdd8412009-03-13 17:28:01 +00001918 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1919 E = SE;
1920 continue;
1921 }
Mike Stump1eb44332009-09-09 15:08:12 +00001922
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001923 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001924 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001925 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00001926 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00001927 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1928 E = SE;
1929 continue;
1930 }
1931 }
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001933 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1934 if (P->getOpcode() == UO_Extension) {
1935 E = P->getSubExpr();
1936 continue;
1937 }
1938 }
1939
Chris Lattnerecdd8412009-03-13 17:28:01 +00001940 return E;
1941 }
1942}
1943
Douglas Gregor6eef5192009-12-14 19:27:10 +00001944bool Expr::isDefaultArgument() const {
1945 const Expr *E = this;
1946 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1947 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00001948
Douglas Gregor6eef5192009-12-14 19:27:10 +00001949 return isa<CXXDefaultArgExpr>(E);
1950}
Chris Lattnerecdd8412009-03-13 17:28:01 +00001951
Douglas Gregor2f599792010-04-02 18:24:57 +00001952/// \brief Skip over any no-op casts and any temporary-binding
1953/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00001954static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor2f599792010-04-02 18:24:57 +00001955 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001956 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001957 E = ICE->getSubExpr();
1958 else
1959 break;
1960 }
1961
1962 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1963 E = BE->getSubExpr();
1964
1965 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00001966 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00001967 E = ICE->getSubExpr();
1968 else
1969 break;
1970 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00001971
1972 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00001973}
1974
John McCall558d2ab2010-09-15 10:14:12 +00001975/// isTemporaryObject - Determines if this expression produces a
1976/// temporary of the given class type.
1977bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1978 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1979 return false;
1980
Anders Carlssonf8b30152010-11-28 16:40:49 +00001981 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00001982
John McCall58277b52010-09-15 20:59:13 +00001983 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001984 if (!E->Classify(C).isPRValue()) {
1985 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00001986 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00001987 return false;
1988 }
Douglas Gregor2f599792010-04-02 18:24:57 +00001989
John McCall19e60ad2010-09-16 06:57:56 +00001990 // Black-list a few cases which yield pr-values of class type that don't
1991 // refer to temporaries of that type:
1992
1993 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00001994 if (isa<ImplicitCastExpr>(E)) {
1995 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
1996 case CK_DerivedToBase:
1997 case CK_UncheckedDerivedToBase:
1998 return false;
1999 default:
2000 break;
2001 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002002 }
2003
John McCall19e60ad2010-09-16 06:57:56 +00002004 // - member expressions (all)
2005 if (isa<MemberExpr>(E))
2006 return false;
2007
John McCall558d2ab2010-09-15 10:14:12 +00002008 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002009}
2010
Douglas Gregor898574e2008-12-05 23:32:09 +00002011/// hasAnyTypeDependentArguments - Determines if any of the expressions
2012/// in Exprs is type-dependent.
2013bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2014 for (unsigned I = 0; I < NumExprs; ++I)
2015 if (Exprs[I]->isTypeDependent())
2016 return true;
2017
2018 return false;
2019}
2020
2021/// hasAnyValueDependentArguments - Determines if any of the expressions
2022/// in Exprs is value-dependent.
2023bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2024 for (unsigned I = 0; I < NumExprs; ++I)
2025 if (Exprs[I]->isValueDependent())
2026 return true;
2027
2028 return false;
2029}
2030
John McCall4204f072010-08-02 21:13:48 +00002031bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002032 // This function is attempting whether an expression is an initializer
2033 // which can be evaluated at compile-time. isEvaluatable handles most
2034 // of the cases, but it can't deal with some initializer-specific
2035 // expressions, and it can't deal with aggregates; we deal with those here,
2036 // and fall back to isEvaluatable for the other cases.
2037
John McCall4204f072010-08-02 21:13:48 +00002038 // If we ever capture reference-binding directly in the AST, we can
2039 // kill the second parameter.
2040
2041 if (IsForRef) {
2042 EvalResult Result;
2043 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2044 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002045
Anders Carlssone8a32b82008-11-24 05:23:59 +00002046 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002047 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002048 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002049 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002050 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002051 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002052 case CXXTemporaryObjectExprClass:
2053 case CXXConstructExprClass: {
2054 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002055
2056 // Only if it's
2057 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002058 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002059 if (!CE->getNumArgs()) return true;
2060
2061 // 2) an elidable trivial copy construction of an operand which is
2062 // itself a constant initializer. Note that we consider the
2063 // operand on its own, *not* as a reference binding.
2064 return CE->isElidable() &&
2065 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002066 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002067 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002068 // This handles gcc's extension that allows global initializers like
2069 // "struct x {int x;} x = (struct x) {};".
2070 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002071 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002072 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002073 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002074 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002075 // FIXME: This doesn't deal with fields with reference types correctly.
2076 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2077 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002078 const InitListExpr *Exp = cast<InitListExpr>(this);
2079 unsigned numInits = Exp->getNumInits();
2080 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002081 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002082 return false;
2083 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002084 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002085 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002086 case ImplicitValueInitExprClass:
2087 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002088 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002089 return cast<ParenExpr>(this)->getSubExpr()
2090 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002091 case ChooseExprClass:
2092 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2093 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002094 case UnaryOperatorClass: {
2095 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002096 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002097 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002098 break;
2099 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002100 case BinaryOperatorClass: {
2101 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2102 // but this handles the common case.
2103 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002104 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002105 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2106 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2107 return true;
2108 break;
2109 }
John McCall4204f072010-08-02 21:13:48 +00002110 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002111 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002112 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002113 case CStyleCastExprClass:
2114 // Handle casts with a destination that's a struct or union; this
2115 // deals with both the gcc no-op struct cast extension and the
2116 // cast-to-union extension.
2117 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002118 return cast<CastExpr>(this)->getSubExpr()
2119 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002120
Chris Lattner430656e2009-10-13 22:12:09 +00002121 // Integer->integer casts can be handled here, which is important for
2122 // things like (int)(&&x-&&y). Scary but true.
2123 if (getType()->isIntegerType() &&
2124 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002125 return cast<CastExpr>(this)->getSubExpr()
2126 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002127
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002128 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002129 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002130 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002131}
2132
Reid Spencer5f016e22007-07-11 17:01:13 +00002133/// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an
2134/// integer constant expression with the value zero, or if this is one that is
2135/// cast to void*.
Douglas Gregorce940492009-09-25 04:25:58 +00002136bool Expr::isNullPointerConstant(ASTContext &Ctx,
2137 NullPointerConstantValueDependence NPC) const {
2138 if (isValueDependent()) {
2139 switch (NPC) {
2140 case NPC_NeverValueDependent:
2141 assert(false && "Unexpected value dependent expression!");
2142 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002143
Douglas Gregorce940492009-09-25 04:25:58 +00002144 case NPC_ValueDependentIsNull:
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002145 return isTypeDependent() || getType()->isIntegralType(Ctx);
Sean Huntc3021132010-05-05 15:23:54 +00002146
Douglas Gregorce940492009-09-25 04:25:58 +00002147 case NPC_ValueDependentIsNotNull:
2148 return false;
2149 }
2150 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002151
Sebastian Redl07779722008-10-31 14:43:28 +00002152 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002153 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002154 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002155 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002156 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002157 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002158 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002159 Pointee->isVoidType() && // to void*
2160 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002161 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002162 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002163 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002164 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2165 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002166 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002167 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2168 // Accept ((void*)0) as a null pointer constant, as many other
2169 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002170 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002171 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002172 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002173 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002174 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002175 } else if (isa<GNUNullExpr>(this)) {
2176 // The GNU __null extension is always a null pointer constant.
2177 return true;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002178 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002179
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002180 // C++0x nullptr_t is always a null pointer constant.
2181 if (getType()->isNullPtrType())
2182 return true;
2183
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002184 if (const RecordType *UT = getType()->getAsUnionType())
2185 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2186 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2187 const Expr *InitExpr = CLE->getInitializer();
2188 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2189 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2190 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002191 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002192 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002193 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Steve Naroffaa58f002008-01-14 16:10:57 +00002194 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002195
Reid Spencer5f016e22007-07-11 17:01:13 +00002196 // If we have an integer constant expression, we need to *evaluate* it and
2197 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002198 llvm::APSInt Result;
2199 return isIntegerConstantExpr(Result, Ctx) && Result == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002200}
Steve Naroff31a45842007-07-28 23:10:27 +00002201
John McCallf6a16482010-12-04 03:47:34 +00002202/// \brief If this expression is an l-value for an Objective C
2203/// property, find the underlying property reference expression.
2204const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2205 const Expr *E = this;
2206 while (true) {
2207 assert((E->getValueKind() == VK_LValue &&
2208 E->getObjectKind() == OK_ObjCProperty) &&
2209 "expression is not a property reference");
2210 E = E->IgnoreParenCasts();
2211 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2212 if (BO->getOpcode() == BO_Comma) {
2213 E = BO->getRHS();
2214 continue;
2215 }
2216 }
2217
2218 break;
2219 }
2220
2221 return cast<ObjCPropertyRefExpr>(E);
2222}
2223
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002224FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002225 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002226
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002227 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002228 if (ICE->getCastKind() == CK_LValueToRValue ||
2229 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002230 E = ICE->getSubExpr()->IgnoreParens();
2231 else
2232 break;
2233 }
2234
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002235 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002236 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002237 if (Field->isBitField())
2238 return Field;
2239
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002240 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2241 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2242 if (Field->isBitField())
2243 return Field;
2244
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002245 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2246 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2247 return BinOp->getLHS()->getBitField();
2248
2249 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002250}
2251
Anders Carlsson09380262010-01-31 17:18:49 +00002252bool Expr::refersToVectorElement() const {
2253 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002254
Anders Carlsson09380262010-01-31 17:18:49 +00002255 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002256 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002257 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002258 E = ICE->getSubExpr()->IgnoreParens();
2259 else
2260 break;
2261 }
Sean Huntc3021132010-05-05 15:23:54 +00002262
Anders Carlsson09380262010-01-31 17:18:49 +00002263 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2264 return ASE->getBase()->getType()->isVectorType();
2265
2266 if (isa<ExtVectorElementExpr>(E))
2267 return true;
2268
2269 return false;
2270}
2271
Chris Lattner2140e902009-02-16 22:14:05 +00002272/// isArrow - Return true if the base expression is a pointer to vector,
2273/// return false if the base expression is a vector.
2274bool ExtVectorElementExpr::isArrow() const {
2275 return getBase()->getType()->isPointerType();
2276}
2277
Nate Begeman213541a2008-04-18 23:10:10 +00002278unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002279 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002280 return VT->getNumElements();
2281 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002282}
2283
Nate Begeman8a997642008-05-09 06:41:27 +00002284/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002285bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002286 // FIXME: Refactor this code to an accessor on the AST node which returns the
2287 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002288 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002289
2290 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002291 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002292 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002293
Nate Begeman190d6a22009-01-18 02:01:21 +00002294 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002295 if (Comp[0] == 's' || Comp[0] == 'S')
2296 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002297
Daniel Dunbar15027422009-10-17 23:53:04 +00002298 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2299 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002300 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002301
Steve Narofffec0b492007-07-30 03:29:09 +00002302 return false;
2303}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002304
Nate Begeman8a997642008-05-09 06:41:27 +00002305/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002306void ExtVectorElementExpr::getEncodedElementAccess(
2307 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002308 llvm::StringRef Comp = Accessor->getName();
2309 if (Comp[0] == 's' || Comp[0] == 'S')
2310 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002311
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002312 bool isHi = Comp == "hi";
2313 bool isLo = Comp == "lo";
2314 bool isEven = Comp == "even";
2315 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002316
Nate Begeman8a997642008-05-09 06:41:27 +00002317 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2318 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002319
Nate Begeman8a997642008-05-09 06:41:27 +00002320 if (isHi)
2321 Index = e + i;
2322 else if (isLo)
2323 Index = i;
2324 else if (isEven)
2325 Index = 2 * i;
2326 else if (isOdd)
2327 Index = 2 * i + 1;
2328 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002329 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002330
Nate Begeman3b8d1162008-05-13 21:03:02 +00002331 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002332 }
Nate Begeman8a997642008-05-09 06:41:27 +00002333}
2334
Douglas Gregor04badcf2010-04-21 00:45:42 +00002335ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002336 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002337 SourceLocation LBracLoc,
2338 SourceLocation SuperLoc,
2339 bool IsInstanceSuper,
2340 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002341 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002342 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002343 ObjCMethodDecl *Method,
2344 Expr **Args, unsigned NumArgs,
2345 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002346 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002347 /*TypeDependent=*/false, /*ValueDependent=*/false,
2348 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002349 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2350 HasMethod(Method != 0), SuperLoc(SuperLoc),
2351 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2352 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002353 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002354{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002355 setReceiverPointer(SuperType.getAsOpaquePtr());
2356 if (NumArgs)
2357 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002358}
2359
Douglas Gregor04badcf2010-04-21 00:45:42 +00002360ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002361 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002362 SourceLocation LBracLoc,
2363 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002364 Selector Sel,
2365 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002366 ObjCMethodDecl *Method,
2367 Expr **Args, unsigned NumArgs,
2368 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002369 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002370 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002371 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2372 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2373 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002374 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002375{
2376 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002377 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002378 for (unsigned I = 0; I != NumArgs; ++I) {
2379 if (Args[I]->isTypeDependent())
2380 ExprBits.TypeDependent = true;
2381 if (Args[I]->isValueDependent())
2382 ExprBits.ValueDependent = true;
2383 if (Args[I]->containsUnexpandedParameterPack())
2384 ExprBits.ContainsUnexpandedParameterPack = true;
2385
2386 MyArgs[I] = Args[I];
2387 }
Ted Kremenek4df728e2008-06-24 15:50:53 +00002388}
2389
Douglas Gregor04badcf2010-04-21 00:45:42 +00002390ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002391 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002392 SourceLocation LBracLoc,
2393 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002394 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002395 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002396 ObjCMethodDecl *Method,
2397 Expr **Args, unsigned NumArgs,
2398 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002399 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002400 Receiver->isTypeDependent(),
2401 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002402 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2403 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2404 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002405 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002406{
2407 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002408 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002409 for (unsigned I = 0; I != NumArgs; ++I) {
2410 if (Args[I]->isTypeDependent())
2411 ExprBits.TypeDependent = true;
2412 if (Args[I]->isValueDependent())
2413 ExprBits.ValueDependent = true;
2414 if (Args[I]->containsUnexpandedParameterPack())
2415 ExprBits.ContainsUnexpandedParameterPack = true;
2416
2417 MyArgs[I] = Args[I];
2418 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002419}
2420
Douglas Gregor04badcf2010-04-21 00:45:42 +00002421ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002422 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002423 SourceLocation LBracLoc,
2424 SourceLocation SuperLoc,
2425 bool IsInstanceSuper,
2426 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002427 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002428 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002429 ObjCMethodDecl *Method,
2430 Expr **Args, unsigned NumArgs,
2431 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002432 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002433 NumArgs * sizeof(Expr *);
2434 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002435 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002436 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002437 RBracLoc);
2438}
2439
2440ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002441 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002442 SourceLocation LBracLoc,
2443 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002444 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002445 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002446 ObjCMethodDecl *Method,
2447 Expr **Args, unsigned NumArgs,
2448 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002449 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002450 NumArgs * sizeof(Expr *);
2451 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002452 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2453 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002454}
2455
2456ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002457 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002458 SourceLocation LBracLoc,
2459 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002460 Selector Sel,
2461 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002462 ObjCMethodDecl *Method,
2463 Expr **Args, unsigned NumArgs,
2464 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002465 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002466 NumArgs * sizeof(Expr *);
2467 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002468 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2469 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002470}
2471
Sean Huntc3021132010-05-05 15:23:54 +00002472ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002473 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002474 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002475 NumArgs * sizeof(Expr *);
2476 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2477 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2478}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002479
2480SourceRange ObjCMessageExpr::getReceiverRange() const {
2481 switch (getReceiverKind()) {
2482 case Instance:
2483 return getInstanceReceiver()->getSourceRange();
2484
2485 case Class:
2486 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2487
2488 case SuperInstance:
2489 case SuperClass:
2490 return getSuperLoc();
2491 }
2492
2493 return SourceLocation();
2494}
2495
Douglas Gregor04badcf2010-04-21 00:45:42 +00002496Selector ObjCMessageExpr::getSelector() const {
2497 if (HasMethod)
2498 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2499 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002500 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002501}
2502
2503ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2504 switch (getReceiverKind()) {
2505 case Instance:
2506 if (const ObjCObjectPointerType *Ptr
2507 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2508 return Ptr->getInterfaceDecl();
2509 break;
2510
2511 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002512 if (const ObjCObjectType *Ty
2513 = getClassReceiver()->getAs<ObjCObjectType>())
2514 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002515 break;
2516
2517 case SuperInstance:
2518 if (const ObjCObjectPointerType *Ptr
2519 = getSuperType()->getAs<ObjCObjectPointerType>())
2520 return Ptr->getInterfaceDecl();
2521 break;
2522
2523 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002524 if (const ObjCObjectType *Iface
2525 = getSuperType()->getAs<ObjCObjectType>())
2526 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002527 break;
2528 }
2529
2530 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002531}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002532
Jay Foad4ba2a172011-01-12 09:06:06 +00002533bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002534 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002535}
2536
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002537ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2538 QualType Type, SourceLocation BLoc,
2539 SourceLocation RP)
2540 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2541 Type->isDependentType(), Type->isDependentType(),
2542 Type->containsUnexpandedParameterPack()),
2543 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2544{
2545 SubExprs = new (C) Stmt*[nexpr];
2546 for (unsigned i = 0; i < nexpr; i++) {
2547 if (args[i]->isTypeDependent())
2548 ExprBits.TypeDependent = true;
2549 if (args[i]->isValueDependent())
2550 ExprBits.ValueDependent = true;
2551 if (args[i]->containsUnexpandedParameterPack())
2552 ExprBits.ContainsUnexpandedParameterPack = true;
2553
2554 SubExprs[i] = args[i];
2555 }
2556}
2557
Nate Begeman888376a2009-08-12 02:28:50 +00002558void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2559 unsigned NumExprs) {
2560 if (SubExprs) C.Deallocate(SubExprs);
2561
2562 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002563 this->NumExprs = NumExprs;
2564 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002565}
Nate Begeman888376a2009-08-12 02:28:50 +00002566
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002567//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002568// DesignatedInitExpr
2569//===----------------------------------------------------------------------===//
2570
2571IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2572 assert(Kind == FieldDesignator && "Only valid on a field designator");
2573 if (Field.NameOrField & 0x01)
2574 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2575 else
2576 return getField()->getIdentifier();
2577}
2578
Sean Huntc3021132010-05-05 15:23:54 +00002579DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002580 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002581 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002582 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002583 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002584 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002585 unsigned NumIndexExprs,
2586 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002587 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002588 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002589 Init->isTypeDependent(), Init->isValueDependent(),
2590 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00002591 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2592 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002593 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002594
2595 // Record the initializer itself.
2596 child_iterator Child = child_begin();
2597 *Child++ = Init;
2598
2599 // Copy the designators and their subexpressions, computing
2600 // value-dependence along the way.
2601 unsigned IndexIdx = 0;
2602 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002603 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002604
2605 if (this->Designators[I].isArrayDesignator()) {
2606 // Compute type- and value-dependence.
2607 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002608 if (Index->isTypeDependent() || Index->isValueDependent())
2609 ExprBits.ValueDependent = true;
2610
2611 // Propagate unexpanded parameter packs.
2612 if (Index->containsUnexpandedParameterPack())
2613 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002614
2615 // Copy the index expressions into permanent storage.
2616 *Child++ = IndexExprs[IndexIdx++];
2617 } else if (this->Designators[I].isArrayRangeDesignator()) {
2618 // Compute type- and value-dependence.
2619 Expr *Start = IndexExprs[IndexIdx];
2620 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002621 if (Start->isTypeDependent() || Start->isValueDependent() ||
2622 End->isTypeDependent() || End->isValueDependent())
2623 ExprBits.ValueDependent = true;
2624
2625 // Propagate unexpanded parameter packs.
2626 if (Start->containsUnexpandedParameterPack() ||
2627 End->containsUnexpandedParameterPack())
2628 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002629
2630 // Copy the start/end expressions into permanent storage.
2631 *Child++ = IndexExprs[IndexIdx++];
2632 *Child++ = IndexExprs[IndexIdx++];
2633 }
2634 }
2635
2636 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002637}
2638
Douglas Gregor05c13a32009-01-22 00:58:24 +00002639DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002640DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002641 unsigned NumDesignators,
2642 Expr **IndexExprs, unsigned NumIndexExprs,
2643 SourceLocation ColonOrEqualLoc,
2644 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002645 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002646 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002647 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002648 ColonOrEqualLoc, UsesColonSyntax,
2649 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002650}
2651
Mike Stump1eb44332009-09-09 15:08:12 +00002652DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002653 unsigned NumIndexExprs) {
2654 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2655 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2656 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2657}
2658
Douglas Gregor319d57f2010-01-06 23:17:19 +00002659void DesignatedInitExpr::setDesignators(ASTContext &C,
2660 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002661 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002662 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002663 NumDesignators = NumDesigs;
2664 for (unsigned I = 0; I != NumDesigs; ++I)
2665 Designators[I] = Desigs[I];
2666}
2667
Douglas Gregor05c13a32009-01-22 00:58:24 +00002668SourceRange DesignatedInitExpr::getSourceRange() const {
2669 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002670 Designator &First =
2671 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002672 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002673 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002674 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2675 else
2676 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2677 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002678 StartLoc =
2679 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002680 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2681}
2682
Douglas Gregor05c13a32009-01-22 00:58:24 +00002683Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2684 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2685 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2686 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002687 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2688 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2689}
2690
2691Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002692 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002693 "Requires array range designator");
2694 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2695 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002696 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2697 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2698}
2699
2700Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002701 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002702 "Requires array range designator");
2703 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2704 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002705 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2706 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2707}
2708
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002709/// \brief Replaces the designator at index @p Idx with the series
2710/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002711void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002712 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002713 const Designator *Last) {
2714 unsigned NumNewDesignators = Last - First;
2715 if (NumNewDesignators == 0) {
2716 std::copy_backward(Designators + Idx + 1,
2717 Designators + NumDesignators,
2718 Designators + Idx);
2719 --NumNewDesignators;
2720 return;
2721 } else if (NumNewDesignators == 1) {
2722 Designators[Idx] = *First;
2723 return;
2724 }
2725
Mike Stump1eb44332009-09-09 15:08:12 +00002726 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002727 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002728 std::copy(Designators, Designators + Idx, NewDesignators);
2729 std::copy(First, Last, NewDesignators + Idx);
2730 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2731 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002732 Designators = NewDesignators;
2733 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2734}
2735
Mike Stump1eb44332009-09-09 15:08:12 +00002736ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002737 Expr **exprs, unsigned nexprs,
2738 SourceLocation rparenloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002739 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2740 false, false, false),
2741 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002742
Nate Begeman2ef13e52009-08-10 23:49:36 +00002743 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002744 for (unsigned i = 0; i != nexprs; ++i) {
2745 if (exprs[i]->isTypeDependent())
2746 ExprBits.TypeDependent = true;
2747 if (exprs[i]->isValueDependent())
2748 ExprBits.ValueDependent = true;
2749 if (exprs[i]->containsUnexpandedParameterPack())
2750 ExprBits.ContainsUnexpandedParameterPack = true;
2751
Nate Begeman2ef13e52009-08-10 23:49:36 +00002752 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002753 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00002754}
2755
Douglas Gregor05c13a32009-01-22 00:58:24 +00002756//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002757// ExprIterator.
2758//===----------------------------------------------------------------------===//
2759
2760Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2761Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2762Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2763const Expr* ConstExprIterator::operator[](size_t idx) const {
2764 return cast<Expr>(I[idx]);
2765}
2766const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2767const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2768
2769//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002770// Child Iterators for iterating over subexpressions/substatements
2771//===----------------------------------------------------------------------===//
2772
Sebastian Redl05189992008-11-11 17:56:53 +00002773// SizeOfAlignOfExpr
John McCall63c00d72011-02-09 08:16:59 +00002774Stmt::child_range SizeOfAlignOfExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00002775 // If this is of a type and the type is a VLA type (and not a typedef), the
2776 // size expression of the VLA needs to be treated as an executable expression.
2777 // Why isn't this weirdness documented better in StmtIterator?
2778 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00002779 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00002780 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00002781 return child_range(child_iterator(T), child_iterator());
2782 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00002783 }
John McCall63c00d72011-02-09 08:16:59 +00002784 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002785}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002786
Steve Naroff563477d2007-09-18 23:55:05 +00002787// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00002788Stmt::child_range ObjCMessageExpr::children() {
2789 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00002790 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00002791 begin = reinterpret_cast<Stmt **>(this + 1);
2792 else
2793 begin = reinterpret_cast<Stmt **>(getArgs());
2794 return child_range(begin,
2795 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00002796}
2797
Steve Naroff4eb206b2008-09-03 18:15:37 +00002798// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00002799BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00002800 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00002801 bool constAdded)
Douglas Gregord967e312011-01-19 21:52:31 +00002802 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00002803 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00002804 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00002805{
Douglas Gregord967e312011-01-19 21:52:31 +00002806 bool TypeDependent = false;
2807 bool ValueDependent = false;
2808 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
2809 ExprBits.TypeDependent = TypeDependent;
2810 ExprBits.ValueDependent = ValueDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00002811}
2812