blob: 2a5917c4594bbc812f85164233a4cbae7f8ab1a9 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/Lexer.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000026#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000028#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000030#include <algorithm>
Reid Spencer5f016e22007-07-11 17:01:13 +000031using namespace clang;
32
Chris Lattner2b334bb2010-04-16 23:34:13 +000033/// isKnownToHaveBooleanValue - Return true if this is an integer expression
34/// that is known to return 0 or 1. This happens for _Bool/bool expressions
35/// but also int expressions which are produced by things like comparisons in
36/// C.
37bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000038 const Expr *E = IgnoreParens();
39
Chris Lattner2b334bb2010-04-16 23:34:13 +000040 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000041 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000042 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000043 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000044
Peter Collingbournef111d932011-04-15 00:35:48 +000045 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000046 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000047 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000048 return UO->getSubExpr()->isKnownToHaveBooleanValue();
49 default:
50 return false;
51 }
52 }
Sean Huntc3021132010-05-05 15:23:54 +000053
John McCall6907fbe2010-06-12 01:56:02 +000054 // Only look through implicit casts. If the user writes
55 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000056 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000057 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000058
Peter Collingbournef111d932011-04-15 00:35:48 +000059 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000060 switch (BO->getOpcode()) {
61 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000062 case BO_LT: // Relational operators.
63 case BO_GT:
64 case BO_LE:
65 case BO_GE:
66 case BO_EQ: // Equality operators.
67 case BO_NE:
68 case BO_LAnd: // AND operator.
69 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000070 return true;
Sean Huntc3021132010-05-05 15:23:54 +000071
John McCall2de56d12010-08-25 11:45:40 +000072 case BO_And: // Bitwise AND operator.
73 case BO_Xor: // Bitwise XOR operator.
74 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000075 // Handle things like (x==2)|(y==12).
76 return BO->getLHS()->isKnownToHaveBooleanValue() &&
77 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000078
John McCall2de56d12010-08-25 11:45:40 +000079 case BO_Comma:
80 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000081 return BO->getRHS()->isKnownToHaveBooleanValue();
82 }
83 }
Sean Huntc3021132010-05-05 15:23:54 +000084
Peter Collingbournef111d932011-04-15 00:35:48 +000085 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000086 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
87 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000088
Chris Lattner2b334bb2010-04-16 23:34:13 +000089 return false;
90}
91
John McCall63c00d72011-02-09 08:16:59 +000092// Amusing macro metaprogramming hack: check whether a class provides
93// a more specific implementation of getExprLoc().
94namespace {
95 /// This implementation is used when a class provides a custom
96 /// implementation of getExprLoc.
97 template <class E, class T>
98 SourceLocation getExprLocImpl(const Expr *expr,
99 SourceLocation (T::*v)() const) {
100 return static_cast<const E*>(expr)->getExprLoc();
101 }
102
103 /// This implementation is used when a class doesn't provide
104 /// a custom implementation of getExprLoc. Overload resolution
105 /// should pick it over the implementation above because it's
106 /// more specialized according to function template partial ordering.
107 template <class E>
108 SourceLocation getExprLocImpl(const Expr *expr,
109 SourceLocation (Expr::*v)() const) {
110 return static_cast<const E*>(expr)->getSourceRange().getBegin();
111 }
112}
113
114SourceLocation Expr::getExprLoc() const {
115 switch (getStmtClass()) {
116 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
117#define ABSTRACT_STMT(type)
118#define STMT(type, base) \
119 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
120#define EXPR(type, base) \
121 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
122#include "clang/AST/StmtNodes.inc"
123 }
124 llvm_unreachable("unknown statement kind");
125 return SourceLocation();
126}
127
Reid Spencer5f016e22007-07-11 17:01:13 +0000128//===----------------------------------------------------------------------===//
129// Primary Expressions.
130//===----------------------------------------------------------------------===//
131
John McCalld5532b62009-11-23 01:53:49 +0000132void ExplicitTemplateArgumentList::initializeFrom(
133 const TemplateArgumentListInfo &Info) {
134 LAngleLoc = Info.getLAngleLoc();
135 RAngleLoc = Info.getRAngleLoc();
136 NumTemplateArgs = Info.size();
137
138 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
139 for (unsigned i = 0; i != NumTemplateArgs; ++i)
140 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
141}
142
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000143void ExplicitTemplateArgumentList::initializeFrom(
144 const TemplateArgumentListInfo &Info,
145 bool &Dependent,
146 bool &ContainsUnexpandedParameterPack) {
147 LAngleLoc = Info.getLAngleLoc();
148 RAngleLoc = Info.getRAngleLoc();
149 NumTemplateArgs = Info.size();
150
151 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
152 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
153 Dependent = Dependent || Info[i].getArgument().isDependent();
154 ContainsUnexpandedParameterPack
155 = ContainsUnexpandedParameterPack ||
156 Info[i].getArgument().containsUnexpandedParameterPack();
157
158 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
159 }
160}
161
John McCalld5532b62009-11-23 01:53:49 +0000162void ExplicitTemplateArgumentList::copyInto(
163 TemplateArgumentListInfo &Info) const {
164 Info.setLAngleLoc(LAngleLoc);
165 Info.setRAngleLoc(RAngleLoc);
166 for (unsigned I = 0; I != NumTemplateArgs; ++I)
167 Info.addArgument(getTemplateArgs()[I]);
168}
169
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000170std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
171 return sizeof(ExplicitTemplateArgumentList) +
172 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
173}
174
John McCalld5532b62009-11-23 01:53:49 +0000175std::size_t ExplicitTemplateArgumentList::sizeFor(
176 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidis8dfbd8b2010-06-24 08:57:31 +0000177 return sizeFor(Info.size());
John McCalld5532b62009-11-23 01:53:49 +0000178}
179
Douglas Gregord967e312011-01-19 21:52:31 +0000180/// \brief Compute the type- and value-dependence of a declaration reference
181/// based on the declaration being referenced.
182static void computeDeclRefDependence(NamedDecl *D, QualType T,
183 bool &TypeDependent,
184 bool &ValueDependent) {
185 TypeDependent = false;
186 ValueDependent = false;
Sean Huntc3021132010-05-05 15:23:54 +0000187
Douglas Gregor0da76df2009-11-23 11:41:28 +0000188
189 // (TD) C++ [temp.dep.expr]p3:
190 // An id-expression is type-dependent if it contains:
191 //
Sean Huntc3021132010-05-05 15:23:54 +0000192 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000193 //
194 // (VD) C++ [temp.dep.constexpr]p2:
195 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000196
Douglas Gregor0da76df2009-11-23 11:41:28 +0000197 // (TD) - an identifier that was declared with dependent type
198 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000199 if (T->isDependentType()) {
200 TypeDependent = true;
201 ValueDependent = true;
202 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000203 }
Douglas Gregord967e312011-01-19 21:52:31 +0000204
Douglas Gregor0da76df2009-11-23 11:41:28 +0000205 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000206 if (D->getDeclName().getNameKind()
207 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregor0da76df2009-11-23 11:41:28 +0000208 D->getDeclName().getCXXNameType()->isDependentType()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000209 TypeDependent = true;
210 ValueDependent = true;
211 return;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000212 }
213 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000214 if (isa<NonTypeTemplateParmDecl>(D)) {
215 ValueDependent = true;
216 return;
217 }
218
Douglas Gregor0da76df2009-11-23 11:41:28 +0000219 // (VD) - a constant with integral or enumeration type and is
220 // initialized with an expression that is value-dependent.
Douglas Gregord967e312011-01-19 21:52:31 +0000221 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregor2ade35e2010-06-16 00:17:44 +0000222 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor501edb62010-01-15 16:21:02 +0000223 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000224 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor501edb62010-01-15 16:21:02 +0000225 if (Init->isValueDependent())
Douglas Gregord967e312011-01-19 21:52:31 +0000226 ValueDependent = true;
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000227 }
Douglas Gregord967e312011-01-19 21:52:31 +0000228
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000229 // (VD) - FIXME: Missing from the standard:
230 // - a member function or a static data member of the current
231 // instantiation
232 else if (Var->isStaticDataMember() &&
Douglas Gregor7ed5bd32010-05-11 08:44:04 +0000233 Var->getDeclContext()->isDependentContext())
Douglas Gregord967e312011-01-19 21:52:31 +0000234 ValueDependent = true;
235
236 return;
237 }
238
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000239 // (VD) - FIXME: Missing from the standard:
240 // - a member function or a static data member of the current
241 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000242 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
243 ValueDependent = true;
244 return;
245 }
246}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000247
Douglas Gregord967e312011-01-19 21:52:31 +0000248void DeclRefExpr::computeDependence() {
249 bool TypeDependent = false;
250 bool ValueDependent = false;
251 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent);
252
253 // (TD) C++ [temp.dep.expr]p3:
254 // An id-expression is type-dependent if it contains:
255 //
256 // and
257 //
258 // (VD) C++ [temp.dep.constexpr]p2:
259 // An identifier is value-dependent if it is:
260 if (!TypeDependent && !ValueDependent &&
261 hasExplicitTemplateArgs() &&
262 TemplateSpecializationType::anyDependentTemplateArguments(
263 getTemplateArgs(),
264 getNumTemplateArgs())) {
265 TypeDependent = true;
266 ValueDependent = true;
267 }
268
269 ExprBits.TypeDependent = TypeDependent;
270 ExprBits.ValueDependent = ValueDependent;
271
Douglas Gregor10738d32010-12-23 23:51:58 +0000272 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000273 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000274 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000275}
276
Douglas Gregor40d96a62011-02-28 21:54:11 +0000277DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000278 ValueDecl *D, SourceLocation NameLoc,
John McCalld5532b62009-11-23 01:53:49 +0000279 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000280 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000281 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000282 DecoratedD(D,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000283 (QualifierLoc? HasQualifierFlag : 0) |
John McCalld5532b62009-11-23 01:53:49 +0000284 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregora2813ce2009-10-23 18:54:35 +0000285 Loc(NameLoc) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000286 if (QualifierLoc) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000287 NameQualifier *NQ = getNameQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000288 NQ->QualifierLoc = QualifierLoc;
Douglas Gregora2813ce2009-10-23 18:54:35 +0000289 }
Sean Huntc3021132010-05-05 15:23:54 +0000290
John McCalld5532b62009-11-23 01:53:49 +0000291 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000292 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregor0da76df2009-11-23 11:41:28 +0000293
294 computeDependence();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000295}
296
Douglas Gregor40d96a62011-02-28 21:54:11 +0000297DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000298 ValueDecl *D, const DeclarationNameInfo &NameInfo,
299 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000300 QualType T, ExprValueKind VK)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000301 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Abramo Bagnara25777432010-08-11 22:01:17 +0000302 DecoratedD(D,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000303 (QualifierLoc? HasQualifierFlag : 0) |
Abramo Bagnara25777432010-08-11 22:01:17 +0000304 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
305 Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000306 if (QualifierLoc) {
Abramo Bagnara25777432010-08-11 22:01:17 +0000307 NameQualifier *NQ = getNameQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000308 NQ->QualifierLoc = QualifierLoc;
Abramo Bagnara25777432010-08-11 22:01:17 +0000309 }
310
311 if (TemplateArgs)
John McCall096832c2010-08-19 23:49:38 +0000312 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000313
314 computeDependence();
315}
316
Douglas Gregora2813ce2009-10-23 18:54:35 +0000317DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000318 NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000319 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000320 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000321 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000322 ExprValueKind VK,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000323 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000324 return Create(Context, QualifierLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000325 DeclarationNameInfo(D->getDeclName(), NameLoc),
John McCallf89e55a2010-11-18 06:31:45 +0000326 T, VK, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000327}
328
329DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000330 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000331 ValueDecl *D,
332 const DeclarationNameInfo &NameInfo,
333 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000334 ExprValueKind VK,
Abramo Bagnara25777432010-08-11 22:01:17 +0000335 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregora2813ce2009-10-23 18:54:35 +0000336 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000337 if (QualifierLoc != 0)
Douglas Gregora2813ce2009-10-23 18:54:35 +0000338 Size += sizeof(NameQualifier);
Sean Huntc3021132010-05-05 15:23:54 +0000339
John McCalld5532b62009-11-23 01:53:49 +0000340 if (TemplateArgs)
341 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Sean Huntc3021132010-05-05 15:23:54 +0000342
Chris Lattner32488542010-10-30 05:14:06 +0000343 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Douglas Gregor40d96a62011-02-28 21:54:11 +0000344 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000345}
346
Douglas Gregordef03542011-02-04 12:01:24 +0000347DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
348 bool HasQualifier,
349 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000350 unsigned NumTemplateArgs) {
351 std::size_t Size = sizeof(DeclRefExpr);
352 if (HasQualifier)
353 Size += sizeof(NameQualifier);
354
Douglas Gregordef03542011-02-04 12:01:24 +0000355 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000356 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
357
Chris Lattner32488542010-10-30 05:14:06 +0000358 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000359 return new (Mem) DeclRefExpr(EmptyShell());
360}
361
Douglas Gregora2813ce2009-10-23 18:54:35 +0000362SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000363 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000364 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000365 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000366 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000367 R.setEnd(getRAngleLoc());
368 return R;
369}
370
Anders Carlsson3a082d82009-09-08 18:24:21 +0000371// FIXME: Maybe this should use DeclPrinter with a special "print predefined
372// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000373std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
374 ASTContext &Context = CurrentDecl->getASTContext();
375
Anders Carlsson3a082d82009-09-08 18:24:21 +0000376 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000377 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000378 return FD->getNameAsString();
379
380 llvm::SmallString<256> Name;
381 llvm::raw_svector_ostream Out(Name);
382
383 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000384 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000385 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000386 if (MD->isStatic())
387 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000388 }
389
390 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000391
392 std::string Proto = FD->getQualifiedNameAsString(Policy);
393
John McCall183700f2009-09-21 23:43:11 +0000394 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000395 const FunctionProtoType *FT = 0;
396 if (FD->hasWrittenPrototype())
397 FT = dyn_cast<FunctionProtoType>(AFT);
398
399 Proto += "(";
400 if (FT) {
401 llvm::raw_string_ostream POut(Proto);
402 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
403 if (i) POut << ", ";
404 std::string Param;
405 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
406 POut << Param;
407 }
408
409 if (FT->isVariadic()) {
410 if (FD->getNumParams()) POut << ", ";
411 POut << "...";
412 }
413 }
414 Proto += ")";
415
Sam Weinig4eadcc52009-12-27 01:38:20 +0000416 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
417 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
418 if (ThisQuals.hasConst())
419 Proto += " const";
420 if (ThisQuals.hasVolatile())
421 Proto += " volatile";
422 }
423
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000424 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
425 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000426
427 Out << Proto;
428
429 Out.flush();
430 return Name.str().str();
431 }
432 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
433 llvm::SmallString<256> Name;
434 llvm::raw_svector_ostream Out(Name);
435 Out << (MD->isInstanceMethod() ? '-' : '+');
436 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000437
438 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
439 // a null check to avoid a crash.
440 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramer900fc632010-04-17 09:33:03 +0000441 Out << ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000442
Anders Carlsson3a082d82009-09-08 18:24:21 +0000443 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000444 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
445 Out << '(' << CID << ')';
446
Anders Carlsson3a082d82009-09-08 18:24:21 +0000447 Out << ' ';
448 Out << MD->getSelector().getAsString();
449 Out << ']';
450
451 Out.flush();
452 return Name.str().str();
453 }
454 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
455 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
456 return "top level";
457 }
458 return "";
459}
460
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000461void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
462 if (hasAllocation())
463 C.Deallocate(pVal);
464
465 BitWidth = Val.getBitWidth();
466 unsigned NumWords = Val.getNumWords();
467 const uint64_t* Words = Val.getRawData();
468 if (NumWords > 1) {
469 pVal = new (C) uint64_t[NumWords];
470 std::copy(Words, Words + NumWords, pVal);
471 } else if (NumWords == 1)
472 VAL = Words[0];
473 else
474 VAL = 0;
475}
476
477IntegerLiteral *
478IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
479 QualType type, SourceLocation l) {
480 return new (C) IntegerLiteral(C, V, type, l);
481}
482
483IntegerLiteral *
484IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
485 return new (C) IntegerLiteral(Empty);
486}
487
488FloatingLiteral *
489FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
490 bool isexact, QualType Type, SourceLocation L) {
491 return new (C) FloatingLiteral(C, V, isexact, Type, L);
492}
493
494FloatingLiteral *
495FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
496 return new (C) FloatingLiteral(Empty);
497}
498
Chris Lattnerda8249e2008-06-07 22:13:43 +0000499/// getValueAsApproximateDouble - This returns the value as an inaccurate
500/// double. Note that this may cause loss of precision, but is useful for
501/// debugging dumps, etc.
502double FloatingLiteral::getValueAsApproximateDouble() const {
503 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000504 bool ignored;
505 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
506 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000507 return V.convertToDouble();
508}
509
Chris Lattner2085fd62009-02-18 06:40:38 +0000510StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
511 unsigned ByteLength, bool Wide,
Anders Carlsson3e2193c2011-04-14 00:40:03 +0000512 bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000513 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000514 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000515 // Allocate enough space for the StringLiteral plus an array of locations for
516 // any concatenated string tokens.
517 void *Mem = C.Allocate(sizeof(StringLiteral)+
518 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000519 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000520 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattner2085fd62009-02-18 06:40:38 +0000523 char *AStrData = new (C, 1) char[ByteLength];
524 memcpy(AStrData, StrData, ByteLength);
525 SL->StrData = AStrData;
526 SL->ByteLength = ByteLength;
527 SL->IsWide = Wide;
Anders Carlsson3e2193c2011-04-14 00:40:03 +0000528 SL->IsPascal = Pascal;
Chris Lattner2085fd62009-02-18 06:40:38 +0000529 SL->TokLocs[0] = Loc[0];
530 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000531
Chris Lattner726e1682009-02-18 05:49:11 +0000532 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000533 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
534 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000535}
536
Douglas Gregor673ecd62009-04-15 16:35:07 +0000537StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
538 void *Mem = C.Allocate(sizeof(StringLiteral)+
539 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000540 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000541 StringLiteral *SL = new (Mem) StringLiteral(QualType());
542 SL->StrData = 0;
543 SL->ByteLength = 0;
544 SL->NumConcatenated = NumStrs;
545 return SL;
546}
547
Daniel Dunbarb6480232009-09-22 03:27:33 +0000548void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbarb6480232009-09-22 03:27:33 +0000549 char *AStrData = new (C, 1) char[Str.size()];
550 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000551 StrData = AStrData;
Daniel Dunbarb6480232009-09-22 03:27:33 +0000552 ByteLength = Str.size();
Douglas Gregor673ecd62009-04-15 16:35:07 +0000553}
554
Chris Lattner08f92e32010-11-17 07:37:15 +0000555/// getLocationOfByte - Return a source location that points to the specified
556/// byte of this string literal.
557///
558/// Strings are amazingly complex. They can be formed from multiple tokens and
559/// can have escape sequences in them in addition to the usual trigraph and
560/// escaped newline business. This routine handles this complexity.
561///
562SourceLocation StringLiteral::
563getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
564 const LangOptions &Features, const TargetInfo &Target) const {
565 assert(!isWide() && "This doesn't work for wide strings yet");
566
567 // Loop over all of the tokens in this string until we find the one that
568 // contains the byte we're looking for.
569 unsigned TokNo = 0;
570 while (1) {
571 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
572 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
573
574 // Get the spelling of the string so that we can get the data that makes up
575 // the string literal, not the identifier for the macro it is potentially
576 // expanded through.
577 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
578
579 // Re-lex the token to get its length and original spelling.
580 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
581 bool Invalid = false;
582 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
583 if (Invalid)
584 return StrTokSpellingLoc;
585
586 const char *StrData = Buffer.data()+LocInfo.second;
587
588 // Create a langops struct and enable trigraphs. This is sufficient for
589 // relexing tokens.
590 LangOptions LangOpts;
591 LangOpts.Trigraphs = true;
592
593 // Create a lexer starting at the beginning of this token.
594 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
595 Buffer.end());
596 Token TheTok;
597 TheLexer.LexFromRawLexer(TheTok);
598
599 // Use the StringLiteralParser to compute the length of the string in bytes.
600 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
601 unsigned TokNumBytes = SLP.GetStringLength();
602
603 // If the byte is in this token, return the location of the byte.
604 if (ByteNo < TokNumBytes ||
605 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
606 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
607
608 // Now that we know the offset of the token in the spelling, use the
609 // preprocessor to get the offset in the original source.
610 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
611 }
612
613 // Move to the next string token.
614 ++TokNo;
615 ByteNo -= TokNumBytes;
616 }
617}
618
619
620
Reid Spencer5f016e22007-07-11 17:01:13 +0000621/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
622/// corresponds to, e.g. "sizeof" or "[pre]++".
623const char *UnaryOperator::getOpcodeStr(Opcode Op) {
624 switch (Op) {
625 default: assert(0 && "Unknown unary operator");
John McCall2de56d12010-08-25 11:45:40 +0000626 case UO_PostInc: return "++";
627 case UO_PostDec: return "--";
628 case UO_PreInc: return "++";
629 case UO_PreDec: return "--";
630 case UO_AddrOf: return "&";
631 case UO_Deref: return "*";
632 case UO_Plus: return "+";
633 case UO_Minus: return "-";
634 case UO_Not: return "~";
635 case UO_LNot: return "!";
636 case UO_Real: return "__real";
637 case UO_Imag: return "__imag";
638 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000639 }
640}
641
John McCall2de56d12010-08-25 11:45:40 +0000642UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000643UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
644 switch (OO) {
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000645 default: assert(false && "No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000646 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
647 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
648 case OO_Amp: return UO_AddrOf;
649 case OO_Star: return UO_Deref;
650 case OO_Plus: return UO_Plus;
651 case OO_Minus: return UO_Minus;
652 case OO_Tilde: return UO_Not;
653 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000654 }
655}
656
657OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
658 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000659 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
660 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
661 case UO_AddrOf: return OO_Amp;
662 case UO_Deref: return OO_Star;
663 case UO_Plus: return OO_Plus;
664 case UO_Minus: return OO_Minus;
665 case UO_Not: return OO_Tilde;
666 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000667 default: return OO_None;
668 }
669}
670
671
Reid Spencer5f016e22007-07-11 17:01:13 +0000672//===----------------------------------------------------------------------===//
673// Postfix Operators.
674//===----------------------------------------------------------------------===//
675
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000676CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
677 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000678 SourceLocation rparenloc)
679 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000680 fn->isTypeDependent(),
681 fn->isValueDependent(),
682 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000683 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000685 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000686 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000687 for (unsigned i = 0; i != numargs; ++i) {
688 if (args[i]->isTypeDependent())
689 ExprBits.TypeDependent = true;
690 if (args[i]->isValueDependent())
691 ExprBits.ValueDependent = true;
692 if (args[i]->containsUnexpandedParameterPack())
693 ExprBits.ContainsUnexpandedParameterPack = true;
694
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000695 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000696 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000697
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000698 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000699 RParenLoc = rparenloc;
700}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000701
Ted Kremenek668bf912009-02-09 20:51:47 +0000702CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000703 QualType t, ExprValueKind VK, SourceLocation rparenloc)
704 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000705 fn->isTypeDependent(),
706 fn->isValueDependent(),
707 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000708 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000709
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000710 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000711 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000712 for (unsigned i = 0; i != numargs; ++i) {
713 if (args[i]->isTypeDependent())
714 ExprBits.TypeDependent = true;
715 if (args[i]->isValueDependent())
716 ExprBits.ValueDependent = true;
717 if (args[i]->containsUnexpandedParameterPack())
718 ExprBits.ContainsUnexpandedParameterPack = true;
719
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000720 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000721 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000722
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000723 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 RParenLoc = rparenloc;
725}
726
Mike Stump1eb44332009-09-09 15:08:12 +0000727CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
728 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000729 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000730 SubExprs = new (C) Stmt*[PREARGS_START];
731 CallExprBits.NumPreArgs = 0;
732}
733
734CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
735 EmptyShell Empty)
736 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
737 // FIXME: Why do we allocate this?
738 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
739 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000740}
741
Nuno Lopesd20254f2009-12-20 23:11:08 +0000742Decl *CallExpr::getCalleeDecl() {
Zhongxing Xua0042542009-07-17 07:29:51 +0000743 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl20012152010-09-10 20:55:30 +0000744 // If we're calling a dereference, look at the pointer instead.
745 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
746 if (BO->isPtrMemOp())
747 CEE = BO->getRHS()->IgnoreParenCasts();
748 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
749 if (UO->getOpcode() == UO_Deref)
750 CEE = UO->getSubExpr()->IgnoreParenCasts();
751 }
Chris Lattner6346f962009-07-17 15:46:27 +0000752 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000753 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000754 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
755 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000756
757 return 0;
758}
759
Nuno Lopesd20254f2009-12-20 23:11:08 +0000760FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000761 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000762}
763
Chris Lattnerd18b3292007-12-28 05:25:02 +0000764/// setNumArgs - This changes the number of arguments present in this call.
765/// Any orphaned expressions are deleted by this, and any new operands are set
766/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000767void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000768 // No change, just return.
769 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000770
Chris Lattnerd18b3292007-12-28 05:25:02 +0000771 // If shrinking # arguments, just delete the extras and forgot them.
772 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000773 this->NumArgs = NumArgs;
774 return;
775 }
776
777 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000778 unsigned NumPreArgs = getNumPreArgs();
779 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000780 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000781 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000782 NewSubExprs[i] = SubExprs[i];
783 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000784 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
785 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000786 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000787
Douglas Gregor88c9a462009-04-17 21:46:47 +0000788 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000789 SubExprs = NewSubExprs;
790 this->NumArgs = NumArgs;
791}
792
Chris Lattnercb888962008-10-06 05:00:53 +0000793/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
794/// not, return 0.
Jay Foad4ba2a172011-01-12 09:06:06 +0000795unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000796 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000797 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000798 // ImplicitCastExpr.
799 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
800 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000801 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000803 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
804 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000805 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Anders Carlssonbcba2012008-01-31 02:13:57 +0000807 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
808 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000809 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000811 if (!FDecl->getIdentifier())
812 return 0;
813
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000814 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000815}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000816
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000817QualType CallExpr::getCallReturnType() const {
818 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000819 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000820 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000821 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000822 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000823 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
824 // This should never be overloaded and so should never return null.
825 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000826
John McCall864c0412011-04-26 20:42:42 +0000827 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000828 return FnType->getResultType();
829}
Chris Lattnercb888962008-10-06 05:00:53 +0000830
John McCall2882eca2011-02-21 06:23:05 +0000831SourceRange CallExpr::getSourceRange() const {
832 if (isa<CXXOperatorCallExpr>(this))
833 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
834
835 SourceLocation begin = getCallee()->getLocStart();
836 if (begin.isInvalid() && getNumArgs() > 0)
837 begin = getArg(0)->getLocStart();
838 SourceLocation end = getRParenLoc();
839 if (end.isInvalid() && getNumArgs() > 0)
840 end = getArg(getNumArgs() - 1)->getLocEnd();
841 return SourceRange(begin, end);
842}
843
Sean Huntc3021132010-05-05 15:23:54 +0000844OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000845 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000846 TypeSourceInfo *tsi,
847 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000848 Expr** exprsPtr, unsigned numExprs,
849 SourceLocation RParenLoc) {
850 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000851 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000852 sizeof(Expr*) * numExprs);
853
854 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
855 exprsPtr, numExprs, RParenLoc);
856}
857
858OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
859 unsigned numComps, unsigned numExprs) {
860 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
861 sizeof(OffsetOfNode) * numComps +
862 sizeof(Expr*) * numExprs);
863 return new (Mem) OffsetOfExpr(numComps, numExprs);
864}
865
Sean Huntc3021132010-05-05 15:23:54 +0000866OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000867 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000868 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000869 Expr** exprsPtr, unsigned numExprs,
870 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000871 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
872 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000873 /*ValueDependent=*/tsi->getType()->isDependentType(),
874 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000875 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
876 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000877{
878 for(unsigned i = 0; i < numComps; ++i) {
879 setComponent(i, compsPtr[i]);
880 }
Sean Huntc3021132010-05-05 15:23:54 +0000881
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000882 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000883 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
884 ExprBits.ValueDependent = true;
885 if (exprsPtr[i]->containsUnexpandedParameterPack())
886 ExprBits.ContainsUnexpandedParameterPack = true;
887
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000888 setIndexExpr(i, exprsPtr[i]);
889 }
890}
891
892IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
893 assert(getKind() == Field || getKind() == Identifier);
894 if (getKind() == Field)
895 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000896
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000897 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
898}
899
Mike Stump1eb44332009-09-09 15:08:12 +0000900MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000901 NestedNameSpecifierLoc QualifierLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000902 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000903 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000904 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000905 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000906 QualType ty,
907 ExprValueKind vk,
908 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000909 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000910
Douglas Gregor40d96a62011-02-28 21:54:11 +0000911 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000912 founddecl.getDecl() != memberdecl ||
913 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000914 if (hasQualOrFound)
915 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000916
John McCalld5532b62009-11-23 01:53:49 +0000917 if (targs)
918 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000919
Chris Lattner32488542010-10-30 05:14:06 +0000920 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000921 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
922 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000923
924 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000925 // FIXME: Wrong. We should be looking at the member declaration we found.
926 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000927 E->setValueDependent(true);
928 E->setTypeDependent(true);
929 }
930 E->HasQualifierOrFoundDecl = true;
931
932 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000933 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000934 NQ->FoundDecl = founddecl;
935 }
936
937 if (targs) {
938 E->HasExplicitTemplateArgumentList = true;
John McCall096832c2010-08-19 23:49:38 +0000939 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall6bb80172010-03-30 21:47:33 +0000940 }
941
942 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000943}
944
Douglas Gregor75e85042011-03-02 21:06:53 +0000945SourceRange MemberExpr::getSourceRange() const {
946 SourceLocation StartLoc;
947 if (isImplicitAccess()) {
948 if (hasQualifier())
949 StartLoc = getQualifierLoc().getBeginLoc();
950 else
951 StartLoc = MemberLoc;
952 } else {
953 // FIXME: We don't want this to happen. Rather, we should be able to
954 // detect all kinds of implicit accesses more cleanly.
955 StartLoc = getBase()->getLocStart();
956 if (StartLoc.isInvalid())
957 StartLoc = MemberLoc;
958 }
959
960 SourceLocation EndLoc =
961 HasExplicitTemplateArgumentList? getRAngleLoc()
962 : getMemberNameInfo().getEndLoc();
963
964 return SourceRange(StartLoc, EndLoc);
965}
966
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000967const char *CastExpr::getCastKindName() const {
968 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +0000969 case CK_Dependent:
970 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +0000971 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000972 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +0000973 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +0000974 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +0000975 case CK_LValueToRValue:
976 return "LValueToRValue";
John McCallf6a16482010-12-04 03:47:34 +0000977 case CK_GetObjCProperty:
978 return "GetObjCProperty";
John McCall2de56d12010-08-25 11:45:40 +0000979 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000980 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +0000981 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +0000982 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +0000983 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000984 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000985 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +0000986 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +0000987 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000988 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +0000989 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000990 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +0000991 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000992 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000993 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000994 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +0000995 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +0000996 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +0000997 case CK_NullToPointer:
998 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +0000999 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001000 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001001 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001002 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001003 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001004 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001005 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001006 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001007 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001008 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001009 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001010 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001011 case CK_PointerToBoolean:
1012 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001013 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001014 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001015 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001016 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001017 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001018 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001019 case CK_IntegralToBoolean:
1020 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001021 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001022 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001023 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001024 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001025 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001026 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001027 case CK_FloatingToBoolean:
1028 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001029 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001030 return "MemberPointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001031 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahanian4cbf9d42009-12-08 23:46:15 +00001032 return "AnyPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001033 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001034 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001035 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001036 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001037 case CK_FloatingRealToComplex:
1038 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001039 case CK_FloatingComplexToReal:
1040 return "FloatingComplexToReal";
1041 case CK_FloatingComplexToBoolean:
1042 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001043 case CK_FloatingComplexCast:
1044 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001045 case CK_FloatingComplexToIntegralComplex:
1046 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001047 case CK_IntegralRealToComplex:
1048 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001049 case CK_IntegralComplexToReal:
1050 return "IntegralComplexToReal";
1051 case CK_IntegralComplexToBoolean:
1052 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001053 case CK_IntegralComplexCast:
1054 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001055 case CK_IntegralComplexToFloatingComplex:
1056 return "IntegralComplexToFloatingComplex";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001057 }
Mike Stump1eb44332009-09-09 15:08:12 +00001058
John McCall2bb5d002010-11-13 09:02:35 +00001059 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001060 return 0;
1061}
1062
Douglas Gregor6eef5192009-12-14 19:27:10 +00001063Expr *CastExpr::getSubExprAsWritten() {
1064 Expr *SubExpr = 0;
1065 CastExpr *E = this;
1066 do {
1067 SubExpr = E->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001068
Douglas Gregor6eef5192009-12-14 19:27:10 +00001069 // Skip any temporary bindings; they're implicit.
1070 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1071 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001072
Douglas Gregor6eef5192009-12-14 19:27:10 +00001073 // Conversions by constructor and conversion functions have a
1074 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001075 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001076 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001077 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001078 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001079
Douglas Gregor6eef5192009-12-14 19:27:10 +00001080 // If the subexpression we're left with is an implicit cast, look
1081 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001082 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1083
Douglas Gregor6eef5192009-12-14 19:27:10 +00001084 return SubExpr;
1085}
1086
John McCallf871d0c2010-08-07 06:22:56 +00001087CXXBaseSpecifier **CastExpr::path_buffer() {
1088 switch (getStmtClass()) {
1089#define ABSTRACT_STMT(x)
1090#define CASTEXPR(Type, Base) \
1091 case Stmt::Type##Class: \
1092 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1093#define STMT(Type, Base)
1094#include "clang/AST/StmtNodes.inc"
1095 default:
1096 llvm_unreachable("non-cast expressions not possible here");
1097 return 0;
1098 }
1099}
1100
1101void CastExpr::setCastPath(const CXXCastPath &Path) {
1102 assert(Path.size() == path_size());
1103 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1104}
1105
1106ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1107 CastKind Kind, Expr *Operand,
1108 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001109 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001110 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1111 void *Buffer =
1112 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1113 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001114 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001115 if (PathSize) E->setCastPath(*BasePath);
1116 return E;
1117}
1118
1119ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1120 unsigned PathSize) {
1121 void *Buffer =
1122 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1123 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1124}
1125
1126
1127CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001128 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001129 const CXXCastPath *BasePath,
1130 TypeSourceInfo *WrittenTy,
1131 SourceLocation L, SourceLocation R) {
1132 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1133 void *Buffer =
1134 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1135 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001136 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001137 if (PathSize) E->setCastPath(*BasePath);
1138 return E;
1139}
1140
1141CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1142 void *Buffer =
1143 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1144 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1145}
1146
Reid Spencer5f016e22007-07-11 17:01:13 +00001147/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1148/// corresponds to, e.g. "<<=".
1149const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1150 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001151 case BO_PtrMemD: return ".*";
1152 case BO_PtrMemI: return "->*";
1153 case BO_Mul: return "*";
1154 case BO_Div: return "/";
1155 case BO_Rem: return "%";
1156 case BO_Add: return "+";
1157 case BO_Sub: return "-";
1158 case BO_Shl: return "<<";
1159 case BO_Shr: return ">>";
1160 case BO_LT: return "<";
1161 case BO_GT: return ">";
1162 case BO_LE: return "<=";
1163 case BO_GE: return ">=";
1164 case BO_EQ: return "==";
1165 case BO_NE: return "!=";
1166 case BO_And: return "&";
1167 case BO_Xor: return "^";
1168 case BO_Or: return "|";
1169 case BO_LAnd: return "&&";
1170 case BO_LOr: return "||";
1171 case BO_Assign: return "=";
1172 case BO_MulAssign: return "*=";
1173 case BO_DivAssign: return "/=";
1174 case BO_RemAssign: return "%=";
1175 case BO_AddAssign: return "+=";
1176 case BO_SubAssign: return "-=";
1177 case BO_ShlAssign: return "<<=";
1178 case BO_ShrAssign: return ">>=";
1179 case BO_AndAssign: return "&=";
1180 case BO_XorAssign: return "^=";
1181 case BO_OrAssign: return "|=";
1182 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001183 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001184
1185 return "";
Reid Spencer5f016e22007-07-11 17:01:13 +00001186}
1187
John McCall2de56d12010-08-25 11:45:40 +00001188BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001189BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1190 switch (OO) {
Chris Lattnerb7beee92009-03-22 00:10:22 +00001191 default: assert(false && "Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001192 case OO_Plus: return BO_Add;
1193 case OO_Minus: return BO_Sub;
1194 case OO_Star: return BO_Mul;
1195 case OO_Slash: return BO_Div;
1196 case OO_Percent: return BO_Rem;
1197 case OO_Caret: return BO_Xor;
1198 case OO_Amp: return BO_And;
1199 case OO_Pipe: return BO_Or;
1200 case OO_Equal: return BO_Assign;
1201 case OO_Less: return BO_LT;
1202 case OO_Greater: return BO_GT;
1203 case OO_PlusEqual: return BO_AddAssign;
1204 case OO_MinusEqual: return BO_SubAssign;
1205 case OO_StarEqual: return BO_MulAssign;
1206 case OO_SlashEqual: return BO_DivAssign;
1207 case OO_PercentEqual: return BO_RemAssign;
1208 case OO_CaretEqual: return BO_XorAssign;
1209 case OO_AmpEqual: return BO_AndAssign;
1210 case OO_PipeEqual: return BO_OrAssign;
1211 case OO_LessLess: return BO_Shl;
1212 case OO_GreaterGreater: return BO_Shr;
1213 case OO_LessLessEqual: return BO_ShlAssign;
1214 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1215 case OO_EqualEqual: return BO_EQ;
1216 case OO_ExclaimEqual: return BO_NE;
1217 case OO_LessEqual: return BO_LE;
1218 case OO_GreaterEqual: return BO_GE;
1219 case OO_AmpAmp: return BO_LAnd;
1220 case OO_PipePipe: return BO_LOr;
1221 case OO_Comma: return BO_Comma;
1222 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001223 }
1224}
1225
1226OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1227 static const OverloadedOperatorKind OverOps[] = {
1228 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1229 OO_Star, OO_Slash, OO_Percent,
1230 OO_Plus, OO_Minus,
1231 OO_LessLess, OO_GreaterGreater,
1232 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1233 OO_EqualEqual, OO_ExclaimEqual,
1234 OO_Amp,
1235 OO_Caret,
1236 OO_Pipe,
1237 OO_AmpAmp,
1238 OO_PipePipe,
1239 OO_Equal, OO_StarEqual,
1240 OO_SlashEqual, OO_PercentEqual,
1241 OO_PlusEqual, OO_MinusEqual,
1242 OO_LessLessEqual, OO_GreaterGreaterEqual,
1243 OO_AmpEqual, OO_CaretEqual,
1244 OO_PipeEqual,
1245 OO_Comma
1246 };
1247 return OverOps[Opc];
1248}
1249
Ted Kremenek709210f2010-04-13 23:39:13 +00001250InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001251 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001252 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001253 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1254 false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001255 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001256 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +00001257 HadArrayRangeDesignator(false)
Sean Huntc3021132010-05-05 15:23:54 +00001258{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001259 for (unsigned I = 0; I != numInits; ++I) {
1260 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001261 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001262 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001263 ExprBits.ValueDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001264 if (initExprs[I]->containsUnexpandedParameterPack())
1265 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001266 }
Sean Huntc3021132010-05-05 15:23:54 +00001267
Ted Kremenek709210f2010-04-13 23:39:13 +00001268 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001269}
Reid Spencer5f016e22007-07-11 17:01:13 +00001270
Ted Kremenek709210f2010-04-13 23:39:13 +00001271void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001272 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001273 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001274}
1275
Ted Kremenek709210f2010-04-13 23:39:13 +00001276void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001277 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001278}
1279
Ted Kremenek709210f2010-04-13 23:39:13 +00001280Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001281 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001282 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001283 InitExprs.back() = expr;
1284 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001285 }
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Douglas Gregor4c678342009-01-28 21:54:33 +00001287 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1288 InitExprs[Init] = expr;
1289 return Result;
1290}
1291
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001292void InitListExpr::setArrayFiller(Expr *filler) {
1293 ArrayFillerOrUnionFieldInit = filler;
1294 // Fill out any "holes" in the array due to designated initializers.
1295 Expr **inits = getInits();
1296 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1297 if (inits[i] == 0)
1298 inits[i] = filler;
1299}
1300
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001301SourceRange InitListExpr::getSourceRange() const {
1302 if (SyntacticForm)
1303 return SyntacticForm->getSourceRange();
1304 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1305 if (Beg.isInvalid()) {
1306 // Find the first non-null initializer.
1307 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1308 E = InitExprs.end();
1309 I != E; ++I) {
1310 if (Stmt *S = *I) {
1311 Beg = S->getLocStart();
1312 break;
1313 }
1314 }
1315 }
1316 if (End.isInvalid()) {
1317 // Find the first non-null initializer from the end.
1318 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1319 E = InitExprs.rend();
1320 I != E; ++I) {
1321 if (Stmt *S = *I) {
1322 End = S->getSourceRange().getEnd();
1323 break;
1324 }
1325 }
1326 }
1327 return SourceRange(Beg, End);
1328}
1329
Steve Naroffbfdcae62008-09-04 15:31:07 +00001330/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001331///
1332const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001333 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001334 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001335}
1336
Mike Stump1eb44332009-09-09 15:08:12 +00001337SourceLocation BlockExpr::getCaretLocation() const {
1338 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001339}
Mike Stump1eb44332009-09-09 15:08:12 +00001340const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001341 return TheBlock->getBody();
1342}
Mike Stump1eb44332009-09-09 15:08:12 +00001343Stmt *BlockExpr::getBody() {
1344 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001345}
Steve Naroff56ee6892008-10-08 17:01:13 +00001346
1347
Reid Spencer5f016e22007-07-11 17:01:13 +00001348//===----------------------------------------------------------------------===//
1349// Generic Expression Routines
1350//===----------------------------------------------------------------------===//
1351
Chris Lattner026dc962009-02-14 07:37:35 +00001352/// isUnusedResultAWarning - Return true if this immediate expression should
1353/// be warned about if the result is unused. If so, fill in Loc and Ranges
1354/// with location to warn on and the source range[s] to report with the
1355/// warning.
1356bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001357 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001358 // Don't warn if the expr is type dependent. The type could end up
1359 // instantiating to void.
1360 if (isTypeDependent())
1361 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 switch (getStmtClass()) {
1364 default:
John McCall0faede62010-03-12 07:11:26 +00001365 if (getType()->isVoidType())
1366 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001367 Loc = getExprLoc();
1368 R1 = getSourceRange();
1369 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001370 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001371 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001372 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001373 case GenericSelectionExprClass:
1374 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1375 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001376 case UnaryOperatorClass: {
1377 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001378
Reid Spencer5f016e22007-07-11 17:01:13 +00001379 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001380 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001381 case UO_PostInc:
1382 case UO_PostDec:
1383 case UO_PreInc:
1384 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001385 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001386 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001387 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001388 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001389 return false;
1390 break;
John McCall2de56d12010-08-25 11:45:40 +00001391 case UO_Real:
1392 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001393 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001394 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1395 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001396 return false;
1397 break;
John McCall2de56d12010-08-25 11:45:40 +00001398 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001399 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001400 }
Chris Lattner026dc962009-02-14 07:37:35 +00001401 Loc = UO->getOperatorLoc();
1402 R1 = UO->getSubExpr()->getSourceRange();
1403 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001404 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001405 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001406 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001407 switch (BO->getOpcode()) {
1408 default:
1409 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001410 // Consider the RHS of comma for side effects. LHS was checked by
1411 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001412 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001413 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1414 // lvalue-ness) of an assignment written in a macro.
1415 if (IntegerLiteral *IE =
1416 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1417 if (IE->getValue() == 0)
1418 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001419 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1420 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001421 case BO_LAnd:
1422 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001423 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1424 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1425 return false;
1426 break;
John McCallbf0ee352010-02-16 04:10:53 +00001427 }
Chris Lattner026dc962009-02-14 07:37:35 +00001428 if (BO->isAssignmentOp())
1429 return false;
1430 Loc = BO->getOperatorLoc();
1431 R1 = BO->getLHS()->getSourceRange();
1432 R2 = BO->getRHS()->getSourceRange();
1433 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001434 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001435 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001436 case VAArgExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001437 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001438
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001439 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001440 // If only one of the LHS or RHS is a warning, the operator might
1441 // be being used for control flow. Only warn if both the LHS and
1442 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001443 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001444 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1445 return false;
1446 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001447 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001448 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001449 }
1450
Reid Spencer5f016e22007-07-11 17:01:13 +00001451 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001452 // If the base pointer or element is to a volatile pointer/field, accessing
1453 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001454 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001455 return false;
1456 Loc = cast<MemberExpr>(this)->getMemberLoc();
1457 R1 = SourceRange(Loc, Loc);
1458 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1459 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001460
Reid Spencer5f016e22007-07-11 17:01:13 +00001461 case ArraySubscriptExprClass:
1462 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001463 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001464 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001465 return false;
1466 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1467 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1468 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1469 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001470
Reid Spencer5f016e22007-07-11 17:01:13 +00001471 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001472 case CXXOperatorCallExprClass:
1473 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001474 // If this is a direct call, get the callee.
1475 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001476 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001477 // If the callee has attribute pure, const, or warn_unused_result, warn
1478 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001479 //
1480 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1481 // updated to match for QoI.
1482 if (FD->getAttr<WarnUnusedResultAttr>() ||
1483 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1484 Loc = CE->getCallee()->getLocStart();
1485 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001487 if (unsigned NumArgs = CE->getNumArgs())
1488 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1489 CE->getArg(NumArgs-1)->getLocEnd());
1490 return true;
1491 }
Chris Lattner026dc962009-02-14 07:37:35 +00001492 }
1493 return false;
1494 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001495
1496 case CXXTemporaryObjectExprClass:
1497 case CXXConstructExprClass:
1498 return false;
1499
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001500 case ObjCMessageExprClass: {
1501 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1502 const ObjCMethodDecl *MD = ME->getMethodDecl();
1503 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1504 Loc = getExprLoc();
1505 return true;
1506 }
Chris Lattner026dc962009-02-14 07:37:35 +00001507 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001508 }
Mike Stump1eb44332009-09-09 15:08:12 +00001509
John McCall12f78a62010-12-02 01:19:52 +00001510 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001511 Loc = getExprLoc();
1512 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001513 return true;
John McCall12f78a62010-12-02 01:19:52 +00001514
Chris Lattner611b2ec2008-07-26 19:51:01 +00001515 case StmtExprClass: {
1516 // Statement exprs don't logically have side effects themselves, but are
1517 // sometimes used in macros in ways that give them a type that is unused.
1518 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1519 // however, if the result of the stmt expr is dead, we don't want to emit a
1520 // warning.
1521 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001522 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001523 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001524 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001525 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1526 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1527 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1528 }
Mike Stump1eb44332009-09-09 15:08:12 +00001529
John McCall0faede62010-03-12 07:11:26 +00001530 if (getType()->isVoidType())
1531 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001532 Loc = cast<StmtExpr>(this)->getLParenLoc();
1533 R1 = getSourceRange();
1534 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001535 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001536 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001537 // If this is an explicit cast to void, allow it. People do this when they
1538 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001539 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001540 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001541 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1542 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1543 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001544 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001545 if (getType()->isVoidType())
1546 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001547 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001548
Anders Carlsson58beed92009-11-17 17:11:23 +00001549 // If this is a cast to void or a constructor conversion, check the operand.
1550 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001551 if (CE->getCastKind() == CK_ToVoid ||
1552 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001553 return (cast<CastExpr>(this)->getSubExpr()
1554 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001555 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1556 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1557 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001558 }
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Eli Friedman4be1f472008-05-19 21:24:43 +00001560 case ImplicitCastExprClass:
1561 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001562 return (cast<ImplicitCastExpr>(this)
1563 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001564
Chris Lattner04421082008-04-08 04:40:51 +00001565 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001566 return (cast<CXXDefaultArgExpr>(this)
1567 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001568
1569 case CXXNewExprClass:
1570 // FIXME: In theory, there might be new expressions that don't have side
1571 // effects (e.g. a placement new with an uninitialized POD).
1572 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001573 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001574 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001575 return (cast<CXXBindTemporaryExpr>(this)
1576 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001577 case ExprWithCleanupsClass:
1578 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001579 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001580 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001581}
1582
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001583/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001584/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001585bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001586 const Expr *E = IgnoreParens();
1587 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001588 default:
1589 return false;
1590 case ObjCIvarRefExprClass:
1591 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001592 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001593 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001594 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001595 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001596 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001597 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001598 case DeclRefExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001599 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001600 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1601 if (VD->hasGlobalStorage())
1602 return true;
1603 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001604 // dereferencing to a pointer is always a gc'able candidate,
1605 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001606 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001607 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001608 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001609 return false;
1610 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001611 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001612 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001613 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001614 }
1615 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001616 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001617 }
1618}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001619
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001620bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1621 if (isTypeDependent())
1622 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001623 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001624}
1625
John McCall864c0412011-04-26 20:42:42 +00001626QualType Expr::findBoundMemberType(const Expr *expr) {
1627 assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1628
1629 // Bound member expressions are always one of these possibilities:
1630 // x->m x.m x->*y x.*y
1631 // (possibly parenthesized)
1632
1633 expr = expr->IgnoreParens();
1634 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1635 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1636 return mem->getMemberDecl()->getType();
1637 }
1638
1639 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1640 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1641 ->getPointeeType();
1642 assert(type->isFunctionType());
1643 return type;
1644 }
1645
1646 assert(isa<UnresolvedMemberExpr>(expr));
1647 return QualType();
1648}
1649
Sebastian Redl369e51f2010-09-10 20:55:33 +00001650static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1651 Expr::CanThrowResult CT2) {
1652 // CanThrowResult constants are ordered so that the maximum is the correct
1653 // merge result.
1654 return CT1 > CT2 ? CT1 : CT2;
1655}
1656
1657static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1658 Expr *E = const_cast<Expr*>(CE);
1659 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001660 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001661 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1662 }
1663 return R;
1664}
1665
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001666static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001667 bool NullThrows = true) {
1668 if (!D)
1669 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1670
1671 // See if we can get a function type from the decl somehow.
1672 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1673 if (!VD) // If we have no clue what we're calling, assume the worst.
1674 return Expr::CT_Can;
1675
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001676 // As an extension, we assume that __attribute__((nothrow)) functions don't
1677 // throw.
1678 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1679 return Expr::CT_Cannot;
1680
Sebastian Redl369e51f2010-09-10 20:55:33 +00001681 QualType T = VD->getType();
1682 const FunctionProtoType *FT;
1683 if ((FT = T->getAs<FunctionProtoType>())) {
1684 } else if (const PointerType *PT = T->getAs<PointerType>())
1685 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1686 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1687 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1688 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1689 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1690 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1691 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1692
1693 if (!FT)
1694 return Expr::CT_Can;
1695
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001696 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001697}
1698
1699static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1700 if (DC->isTypeDependent())
1701 return Expr::CT_Dependent;
1702
Sebastian Redl295995c2010-09-10 20:55:47 +00001703 if (!DC->getTypeAsWritten()->isReferenceType())
1704 return Expr::CT_Cannot;
1705
Sebastian Redl369e51f2010-09-10 20:55:33 +00001706 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1707}
1708
1709static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1710 const CXXTypeidExpr *DC) {
1711 if (DC->isTypeOperand())
1712 return Expr::CT_Cannot;
1713
1714 Expr *Op = DC->getExprOperand();
1715 if (Op->isTypeDependent())
1716 return Expr::CT_Dependent;
1717
1718 const RecordType *RT = Op->getType()->getAs<RecordType>();
1719 if (!RT)
1720 return Expr::CT_Cannot;
1721
1722 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1723 return Expr::CT_Cannot;
1724
1725 if (Op->Classify(C).isPRValue())
1726 return Expr::CT_Cannot;
1727
1728 return Expr::CT_Can;
1729}
1730
1731Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1732 // C++ [expr.unary.noexcept]p3:
1733 // [Can throw] if in a potentially-evaluated context the expression would
1734 // contain:
1735 switch (getStmtClass()) {
1736 case CXXThrowExprClass:
1737 // - a potentially evaluated throw-expression
1738 return CT_Can;
1739
1740 case CXXDynamicCastExprClass: {
1741 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1742 // where T is a reference type, that requires a run-time check
1743 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1744 if (CT == CT_Can)
1745 return CT;
1746 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1747 }
1748
1749 case CXXTypeidExprClass:
1750 // - a potentially evaluated typeid expression applied to a glvalue
1751 // expression whose type is a polymorphic class type
1752 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1753
1754 // - a potentially evaluated call to a function, member function, function
1755 // pointer, or member function pointer that does not have a non-throwing
1756 // exception-specification
1757 case CallExprClass:
1758 case CXXOperatorCallExprClass:
1759 case CXXMemberCallExprClass: {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001760 CanThrowResult CT = CanCalleeThrow(C,cast<CallExpr>(this)->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001761 if (CT == CT_Can)
1762 return CT;
1763 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1764 }
1765
Sebastian Redl295995c2010-09-10 20:55:47 +00001766 case CXXConstructExprClass:
1767 case CXXTemporaryObjectExprClass: {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001768 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001769 cast<CXXConstructExpr>(this)->getConstructor());
1770 if (CT == CT_Can)
1771 return CT;
1772 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1773 }
1774
1775 case CXXNewExprClass: {
1776 CanThrowResult CT = MergeCanThrow(
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001777 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getOperatorNew()),
1778 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00001779 /*NullThrows*/false));
1780 if (CT == CT_Can)
1781 return CT;
1782 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1783 }
1784
1785 case CXXDeleteExprClass: {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001786 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001787 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1788 if (CT == CT_Can)
1789 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001790 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1791 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1792 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1793 Arg = Cast->getSubExpr();
1794 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1795 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001796 CanThrowResult CT2 = CanCalleeThrow(C,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001797 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1798 if (CT2 == CT_Can)
1799 return CT2;
1800 CT = MergeCanThrow(CT, CT2);
1801 }
1802 }
1803 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1804 }
1805
1806 case CXXBindTemporaryExprClass: {
1807 // The bound temporary has to be destroyed again, which might throw.
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001808 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00001809 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1810 if (CT == CT_Can)
1811 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001812 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1813 }
1814
1815 // ObjC message sends are like function calls, but never have exception
1816 // specs.
1817 case ObjCMessageExprClass:
1818 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001819 return CT_Can;
1820
1821 // Many other things have subexpressions, so we have to test those.
1822 // Some are simple:
1823 case ParenExprClass:
1824 case MemberExprClass:
1825 case CXXReinterpretCastExprClass:
1826 case CXXConstCastExprClass:
1827 case ConditionalOperatorClass:
1828 case CompoundLiteralExprClass:
1829 case ExtVectorElementExprClass:
1830 case InitListExprClass:
1831 case DesignatedInitExprClass:
1832 case ParenListExprClass:
1833 case VAArgExprClass:
1834 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00001835 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00001836 case ObjCIvarRefExprClass:
1837 case ObjCIsaExprClass:
1838 case ShuffleVectorExprClass:
1839 return CanSubExprsThrow(C, this);
1840
1841 // Some might be dependent for other reasons.
1842 case UnaryOperatorClass:
1843 case ArraySubscriptExprClass:
1844 case ImplicitCastExprClass:
1845 case CStyleCastExprClass:
1846 case CXXStaticCastExprClass:
1847 case CXXFunctionalCastExprClass:
1848 case BinaryOperatorClass:
1849 case CompoundAssignOperatorClass: {
1850 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1851 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1852 }
1853
1854 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1855 case StmtExprClass:
1856 return CT_Can;
1857
1858 case ChooseExprClass:
1859 if (isTypeDependent() || isValueDependent())
1860 return CT_Dependent;
1861 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1862
Peter Collingbournef111d932011-04-15 00:35:48 +00001863 case GenericSelectionExprClass:
1864 if (cast<GenericSelectionExpr>(this)->isResultDependent())
1865 return CT_Dependent;
1866 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
1867
Sebastian Redl369e51f2010-09-10 20:55:33 +00001868 // Some expressions are always dependent.
1869 case DependentScopeDeclRefExprClass:
1870 case CXXUnresolvedConstructExprClass:
1871 case CXXDependentScopeMemberExprClass:
1872 return CT_Dependent;
1873
1874 default:
1875 // All other expressions don't have subexpressions, or else they are
1876 // unevaluated.
1877 return CT_Cannot;
1878 }
1879}
1880
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001881Expr* Expr::IgnoreParens() {
1882 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001883 while (true) {
1884 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1885 E = P->getSubExpr();
1886 continue;
1887 }
1888 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1889 if (P->getOpcode() == UO_Extension) {
1890 E = P->getSubExpr();
1891 continue;
1892 }
1893 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001894 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1895 if (!P->isResultDependent()) {
1896 E = P->getResultExpr();
1897 continue;
1898 }
1899 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001900 return E;
1901 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00001902}
1903
Chris Lattner56f34942008-02-13 01:02:39 +00001904/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1905/// or CastExprs or ImplicitCastExprs, returning their operand.
1906Expr *Expr::IgnoreParenCasts() {
1907 Expr *E = this;
1908 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001909 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001910 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001911 continue;
1912 }
1913 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00001914 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001915 continue;
1916 }
1917 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1918 if (P->getOpcode() == UO_Extension) {
1919 E = P->getSubExpr();
1920 continue;
1921 }
1922 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001923 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1924 if (!P->isResultDependent()) {
1925 E = P->getResultExpr();
1926 continue;
1927 }
1928 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001929 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00001930 }
1931}
1932
John McCall9c5d70c2010-12-04 08:24:19 +00001933/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1934/// casts. This is intended purely as a temporary workaround for code
1935/// that hasn't yet been rewritten to do the right thing about those
1936/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00001937Expr *Expr::IgnoreParenLValueCasts() {
1938 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00001939 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00001940 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1941 E = P->getSubExpr();
1942 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00001943 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00001944 if (P->getCastKind() == CK_LValueToRValue) {
1945 E = P->getSubExpr();
1946 continue;
1947 }
John McCall9c5d70c2010-12-04 08:24:19 +00001948 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1949 if (P->getOpcode() == UO_Extension) {
1950 E = P->getSubExpr();
1951 continue;
1952 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001953 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1954 if (!P->isResultDependent()) {
1955 E = P->getResultExpr();
1956 continue;
1957 }
John McCallf6a16482010-12-04 03:47:34 +00001958 }
1959 break;
1960 }
1961 return E;
1962}
1963
John McCall2fc46bf2010-05-05 22:59:52 +00001964Expr *Expr::IgnoreParenImpCasts() {
1965 Expr *E = this;
1966 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001967 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001968 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001969 continue;
1970 }
1971 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00001972 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001973 continue;
1974 }
1975 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1976 if (P->getOpcode() == UO_Extension) {
1977 E = P->getSubExpr();
1978 continue;
1979 }
1980 }
Peter Collingbournef111d932011-04-15 00:35:48 +00001981 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1982 if (!P->isResultDependent()) {
1983 E = P->getResultExpr();
1984 continue;
1985 }
1986 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00001987 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00001988 }
1989}
1990
Chris Lattnerecdd8412009-03-13 17:28:01 +00001991/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1992/// value (including ptr->int casts of the same size). Strip off any
1993/// ParenExpr or CastExprs, returning their operand.
1994Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1995 Expr *E = this;
1996 while (true) {
1997 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1998 E = P->getSubExpr();
1999 continue;
2000 }
Mike Stump1eb44332009-09-09 15:08:12 +00002001
Chris Lattnerecdd8412009-03-13 17:28:01 +00002002 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2003 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002004 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002005 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002006
Chris Lattnerecdd8412009-03-13 17:28:01 +00002007 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2008 E = SE;
2009 continue;
2010 }
Mike Stump1eb44332009-09-09 15:08:12 +00002011
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002012 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002013 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002014 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002015 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002016 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2017 E = SE;
2018 continue;
2019 }
2020 }
Mike Stump1eb44332009-09-09 15:08:12 +00002021
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002022 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2023 if (P->getOpcode() == UO_Extension) {
2024 E = P->getSubExpr();
2025 continue;
2026 }
2027 }
2028
Peter Collingbournef111d932011-04-15 00:35:48 +00002029 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2030 if (!P->isResultDependent()) {
2031 E = P->getResultExpr();
2032 continue;
2033 }
2034 }
2035
Chris Lattnerecdd8412009-03-13 17:28:01 +00002036 return E;
2037 }
2038}
2039
Douglas Gregor6eef5192009-12-14 19:27:10 +00002040bool Expr::isDefaultArgument() const {
2041 const Expr *E = this;
2042 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2043 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002044
Douglas Gregor6eef5192009-12-14 19:27:10 +00002045 return isa<CXXDefaultArgExpr>(E);
2046}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002047
Douglas Gregor2f599792010-04-02 18:24:57 +00002048/// \brief Skip over any no-op casts and any temporary-binding
2049/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002050static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor2f599792010-04-02 18:24:57 +00002051 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002052 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002053 E = ICE->getSubExpr();
2054 else
2055 break;
2056 }
2057
2058 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2059 E = BE->getSubExpr();
2060
2061 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002062 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002063 E = ICE->getSubExpr();
2064 else
2065 break;
2066 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002067
2068 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002069}
2070
John McCall558d2ab2010-09-15 10:14:12 +00002071/// isTemporaryObject - Determines if this expression produces a
2072/// temporary of the given class type.
2073bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2074 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2075 return false;
2076
Anders Carlssonf8b30152010-11-28 16:40:49 +00002077 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002078
John McCall58277b52010-09-15 20:59:13 +00002079 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002080 if (!E->Classify(C).isPRValue()) {
2081 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002082 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002083 return false;
2084 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002085
John McCall19e60ad2010-09-16 06:57:56 +00002086 // Black-list a few cases which yield pr-values of class type that don't
2087 // refer to temporaries of that type:
2088
2089 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002090 if (isa<ImplicitCastExpr>(E)) {
2091 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2092 case CK_DerivedToBase:
2093 case CK_UncheckedDerivedToBase:
2094 return false;
2095 default:
2096 break;
2097 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002098 }
2099
John McCall19e60ad2010-09-16 06:57:56 +00002100 // - member expressions (all)
2101 if (isa<MemberExpr>(E))
2102 return false;
2103
John McCall56ca35d2011-02-17 10:25:35 +00002104 // - opaque values (all)
2105 if (isa<OpaqueValueExpr>(E))
2106 return false;
2107
John McCall558d2ab2010-09-15 10:14:12 +00002108 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002109}
2110
Douglas Gregor75e85042011-03-02 21:06:53 +00002111bool Expr::isImplicitCXXThis() const {
2112 const Expr *E = this;
2113
2114 // Strip away parentheses and casts we don't care about.
2115 while (true) {
2116 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2117 E = Paren->getSubExpr();
2118 continue;
2119 }
2120
2121 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2122 if (ICE->getCastKind() == CK_NoOp ||
2123 ICE->getCastKind() == CK_LValueToRValue ||
2124 ICE->getCastKind() == CK_DerivedToBase ||
2125 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2126 E = ICE->getSubExpr();
2127 continue;
2128 }
2129 }
2130
2131 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2132 if (UnOp->getOpcode() == UO_Extension) {
2133 E = UnOp->getSubExpr();
2134 continue;
2135 }
2136 }
2137
2138 break;
2139 }
2140
2141 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2142 return This->isImplicit();
2143
2144 return false;
2145}
2146
Douglas Gregor898574e2008-12-05 23:32:09 +00002147/// hasAnyTypeDependentArguments - Determines if any of the expressions
2148/// in Exprs is type-dependent.
2149bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2150 for (unsigned I = 0; I < NumExprs; ++I)
2151 if (Exprs[I]->isTypeDependent())
2152 return true;
2153
2154 return false;
2155}
2156
2157/// hasAnyValueDependentArguments - Determines if any of the expressions
2158/// in Exprs is value-dependent.
2159bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2160 for (unsigned I = 0; I < NumExprs; ++I)
2161 if (Exprs[I]->isValueDependent())
2162 return true;
2163
2164 return false;
2165}
2166
John McCall4204f072010-08-02 21:13:48 +00002167bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002168 // This function is attempting whether an expression is an initializer
2169 // which can be evaluated at compile-time. isEvaluatable handles most
2170 // of the cases, but it can't deal with some initializer-specific
2171 // expressions, and it can't deal with aggregates; we deal with those here,
2172 // and fall back to isEvaluatable for the other cases.
2173
John McCall4204f072010-08-02 21:13:48 +00002174 // If we ever capture reference-binding directly in the AST, we can
2175 // kill the second parameter.
2176
2177 if (IsForRef) {
2178 EvalResult Result;
2179 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2180 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002181
Anders Carlssone8a32b82008-11-24 05:23:59 +00002182 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002183 default: break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002184 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002185 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002186 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002187 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002188 case CXXTemporaryObjectExprClass:
2189 case CXXConstructExprClass: {
2190 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002191
2192 // Only if it's
2193 // 1) an application of the trivial default constructor or
John McCallb4b9b152010-08-01 21:51:45 +00002194 if (!CE->getConstructor()->isTrivial()) return false;
John McCall4204f072010-08-02 21:13:48 +00002195 if (!CE->getNumArgs()) return true;
2196
2197 // 2) an elidable trivial copy construction of an operand which is
2198 // itself a constant initializer. Note that we consider the
2199 // operand on its own, *not* as a reference binding.
2200 return CE->isElidable() &&
2201 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCallb4b9b152010-08-01 21:51:45 +00002202 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002203 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002204 // This handles gcc's extension that allows global initializers like
2205 // "struct x {int x;} x = (struct x) {};".
2206 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002207 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002208 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002209 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002210 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002211 // FIXME: This doesn't deal with fields with reference types correctly.
2212 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2213 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002214 const InitListExpr *Exp = cast<InitListExpr>(this);
2215 unsigned numInits = Exp->getNumInits();
2216 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002217 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002218 return false;
2219 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002220 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002221 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002222 case ImplicitValueInitExprClass:
2223 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002224 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002225 return cast<ParenExpr>(this)->getSubExpr()
2226 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002227 case GenericSelectionExprClass:
2228 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2229 return false;
2230 return cast<GenericSelectionExpr>(this)->getResultExpr()
2231 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002232 case ChooseExprClass:
2233 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2234 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002235 case UnaryOperatorClass: {
2236 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002237 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002238 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002239 break;
2240 }
Chris Lattner3ae9f482009-10-13 07:14:16 +00002241 case BinaryOperatorClass: {
2242 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2243 // but this handles the common case.
2244 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002245 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3ae9f482009-10-13 07:14:16 +00002246 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2247 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2248 return true;
2249 break;
2250 }
John McCall4204f072010-08-02 21:13:48 +00002251 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002252 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002253 case ImplicitCastExprClass:
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002254 case CStyleCastExprClass:
2255 // Handle casts with a destination that's a struct or union; this
2256 // deals with both the gcc no-op struct cast extension and the
2257 // cast-to-union extension.
2258 if (getType()->isRecordType())
John McCall4204f072010-08-02 21:13:48 +00002259 return cast<CastExpr>(this)->getSubExpr()
2260 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002261
Chris Lattner430656e2009-10-13 22:12:09 +00002262 // Integer->integer casts can be handled here, which is important for
2263 // things like (int)(&&x-&&y). Scary but true.
2264 if (getType()->isIntegerType() &&
2265 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall4204f072010-08-02 21:13:48 +00002266 return cast<CastExpr>(this)->getSubExpr()
2267 ->isConstantInitializer(Ctx, false);
Sean Huntc3021132010-05-05 15:23:54 +00002268
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002269 break;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002270 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002271 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002272}
2273
Chandler Carruth82214a82011-02-18 23:54:50 +00002274/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2275/// pointer constant or not, as well as the specific kind of constant detected.
2276/// Null pointer constants can be integer constant expressions with the
2277/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2278/// (a GNU extension).
2279Expr::NullPointerConstantKind
2280Expr::isNullPointerConstant(ASTContext &Ctx,
2281 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002282 if (isValueDependent()) {
2283 switch (NPC) {
2284 case NPC_NeverValueDependent:
2285 assert(false && "Unexpected value dependent expression!");
2286 // If the unthinkable happens, fall through to the safest alternative.
Sean Huntc3021132010-05-05 15:23:54 +00002287
Douglas Gregorce940492009-09-25 04:25:58 +00002288 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002289 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2290 return NPCK_ZeroInteger;
2291 else
2292 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002293
Douglas Gregorce940492009-09-25 04:25:58 +00002294 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002295 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002296 }
2297 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002298
Sebastian Redl07779722008-10-31 14:43:28 +00002299 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002300 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002301 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002302 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002303 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002304 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002305 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002306 Pointee->isVoidType() && // to void*
2307 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002308 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002309 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002310 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002311 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2312 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002313 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002314 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2315 // Accept ((void*)0) as a null pointer constant, as many other
2316 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002317 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002318 } else if (const GenericSelectionExpr *GE =
2319 dyn_cast<GenericSelectionExpr>(this)) {
2320 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002321 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002322 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002323 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002324 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002325 } else if (isa<GNUNullExpr>(this)) {
2326 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002327 return NPCK_GNUNull;
Steve Naroffaaffbf72008-01-14 02:53:34 +00002328 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002329
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002330 // C++0x nullptr_t is always a null pointer constant.
2331 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002332 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002333
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002334 if (const RecordType *UT = getType()->getAsUnionType())
2335 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2336 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2337 const Expr *InitExpr = CLE->getInitializer();
2338 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2339 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2340 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002341 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002342 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002343 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002344 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002345
Reid Spencer5f016e22007-07-11 17:01:13 +00002346 // If we have an integer constant expression, we need to *evaluate* it and
2347 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002348 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002349 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2350
2351 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002352}
Steve Naroff31a45842007-07-28 23:10:27 +00002353
John McCallf6a16482010-12-04 03:47:34 +00002354/// \brief If this expression is an l-value for an Objective C
2355/// property, find the underlying property reference expression.
2356const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2357 const Expr *E = this;
2358 while (true) {
2359 assert((E->getValueKind() == VK_LValue &&
2360 E->getObjectKind() == OK_ObjCProperty) &&
2361 "expression is not a property reference");
2362 E = E->IgnoreParenCasts();
2363 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2364 if (BO->getOpcode() == BO_Comma) {
2365 E = BO->getRHS();
2366 continue;
2367 }
2368 }
2369
2370 break;
2371 }
2372
2373 return cast<ObjCPropertyRefExpr>(E);
2374}
2375
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002376FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002377 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002378
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002379 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002380 if (ICE->getCastKind() == CK_LValueToRValue ||
2381 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002382 E = ICE->getSubExpr()->IgnoreParens();
2383 else
2384 break;
2385 }
2386
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002387 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002388 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002389 if (Field->isBitField())
2390 return Field;
2391
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002392 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2393 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2394 if (Field->isBitField())
2395 return Field;
2396
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002397 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2398 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2399 return BinOp->getLHS()->getBitField();
2400
2401 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002402}
2403
Anders Carlsson09380262010-01-31 17:18:49 +00002404bool Expr::refersToVectorElement() const {
2405 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002406
Anders Carlsson09380262010-01-31 17:18:49 +00002407 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002408 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002409 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002410 E = ICE->getSubExpr()->IgnoreParens();
2411 else
2412 break;
2413 }
Sean Huntc3021132010-05-05 15:23:54 +00002414
Anders Carlsson09380262010-01-31 17:18:49 +00002415 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2416 return ASE->getBase()->getType()->isVectorType();
2417
2418 if (isa<ExtVectorElementExpr>(E))
2419 return true;
2420
2421 return false;
2422}
2423
Chris Lattner2140e902009-02-16 22:14:05 +00002424/// isArrow - Return true if the base expression is a pointer to vector,
2425/// return false if the base expression is a vector.
2426bool ExtVectorElementExpr::isArrow() const {
2427 return getBase()->getType()->isPointerType();
2428}
2429
Nate Begeman213541a2008-04-18 23:10:10 +00002430unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002431 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002432 return VT->getNumElements();
2433 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002434}
2435
Nate Begeman8a997642008-05-09 06:41:27 +00002436/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002437bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002438 // FIXME: Refactor this code to an accessor on the AST node which returns the
2439 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar01eb9b92009-10-18 21:17:35 +00002440 llvm::StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002441
2442 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002443 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002444 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002445
Nate Begeman190d6a22009-01-18 02:01:21 +00002446 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002447 if (Comp[0] == 's' || Comp[0] == 'S')
2448 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002449
Daniel Dunbar15027422009-10-17 23:53:04 +00002450 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2451 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002452 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002453
Steve Narofffec0b492007-07-30 03:29:09 +00002454 return false;
2455}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002456
Nate Begeman8a997642008-05-09 06:41:27 +00002457/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002458void ExtVectorElementExpr::getEncodedElementAccess(
2459 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002460 llvm::StringRef Comp = Accessor->getName();
2461 if (Comp[0] == 's' || Comp[0] == 'S')
2462 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002463
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002464 bool isHi = Comp == "hi";
2465 bool isLo = Comp == "lo";
2466 bool isEven = Comp == "even";
2467 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002468
Nate Begeman8a997642008-05-09 06:41:27 +00002469 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2470 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002471
Nate Begeman8a997642008-05-09 06:41:27 +00002472 if (isHi)
2473 Index = e + i;
2474 else if (isLo)
2475 Index = i;
2476 else if (isEven)
2477 Index = 2 * i;
2478 else if (isOdd)
2479 Index = 2 * i + 1;
2480 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002481 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002482
Nate Begeman3b8d1162008-05-13 21:03:02 +00002483 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002484 }
Nate Begeman8a997642008-05-09 06:41:27 +00002485}
2486
Douglas Gregor04badcf2010-04-21 00:45:42 +00002487ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002488 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002489 SourceLocation LBracLoc,
2490 SourceLocation SuperLoc,
2491 bool IsInstanceSuper,
2492 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002493 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002494 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002495 ObjCMethodDecl *Method,
2496 Expr **Args, unsigned NumArgs,
2497 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002498 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002499 /*TypeDependent=*/false, /*ValueDependent=*/false,
2500 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002501 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2502 HasMethod(Method != 0), SuperLoc(SuperLoc),
2503 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2504 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002505 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002506{
Douglas Gregor04badcf2010-04-21 00:45:42 +00002507 setReceiverPointer(SuperType.getAsOpaquePtr());
2508 if (NumArgs)
2509 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremenek4df728e2008-06-24 15:50:53 +00002510}
2511
Douglas Gregor04badcf2010-04-21 00:45:42 +00002512ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002513 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002514 SourceLocation LBracLoc,
2515 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002516 Selector Sel,
2517 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002518 ObjCMethodDecl *Method,
2519 Expr **Args, unsigned NumArgs,
2520 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002521 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002522 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002523 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2524 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2525 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002526 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002527{
2528 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002529 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002530 for (unsigned I = 0; I != NumArgs; ++I) {
2531 if (Args[I]->isTypeDependent())
2532 ExprBits.TypeDependent = true;
2533 if (Args[I]->isValueDependent())
2534 ExprBits.ValueDependent = true;
2535 if (Args[I]->containsUnexpandedParameterPack())
2536 ExprBits.ContainsUnexpandedParameterPack = true;
2537
2538 MyArgs[I] = Args[I];
2539 }
Ted Kremenek4df728e2008-06-24 15:50:53 +00002540}
2541
Douglas Gregor04badcf2010-04-21 00:45:42 +00002542ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002543 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002544 SourceLocation LBracLoc,
2545 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002546 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002547 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002548 ObjCMethodDecl *Method,
2549 Expr **Args, unsigned NumArgs,
2550 SourceLocation RBracLoc)
John McCallf89e55a2010-11-18 06:31:45 +00002551 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002552 Receiver->isTypeDependent(),
2553 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002554 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2555 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2556 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002557 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002558{
2559 setReceiverPointer(Receiver);
Douglas Gregoraa165f82011-01-03 19:04:46 +00002560 Expr **MyArgs = getArgs();
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002561 for (unsigned I = 0; I != NumArgs; ++I) {
2562 if (Args[I]->isTypeDependent())
2563 ExprBits.TypeDependent = true;
2564 if (Args[I]->isValueDependent())
2565 ExprBits.ValueDependent = true;
2566 if (Args[I]->containsUnexpandedParameterPack())
2567 ExprBits.ContainsUnexpandedParameterPack = true;
2568
2569 MyArgs[I] = Args[I];
2570 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002571}
2572
Douglas Gregor04badcf2010-04-21 00:45:42 +00002573ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002574 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002575 SourceLocation LBracLoc,
2576 SourceLocation SuperLoc,
2577 bool IsInstanceSuper,
2578 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002579 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002580 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002581 ObjCMethodDecl *Method,
2582 Expr **Args, unsigned NumArgs,
2583 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002584 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002585 NumArgs * sizeof(Expr *);
2586 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCallf89e55a2010-11-18 06:31:45 +00002587 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002588 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002589 RBracLoc);
2590}
2591
2592ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002593 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002594 SourceLocation LBracLoc,
2595 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002596 Selector Sel,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002597 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002598 ObjCMethodDecl *Method,
2599 Expr **Args, unsigned NumArgs,
2600 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002601 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002602 NumArgs * sizeof(Expr *);
2603 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002604 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2605 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002606}
2607
2608ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002609 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002610 SourceLocation LBracLoc,
2611 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002612 Selector Sel,
2613 SourceLocation SelLoc,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002614 ObjCMethodDecl *Method,
2615 Expr **Args, unsigned NumArgs,
2616 SourceLocation RBracLoc) {
Sean Huntc3021132010-05-05 15:23:54 +00002617 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002618 NumArgs * sizeof(Expr *);
2619 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002620 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2621 Method, Args, NumArgs, RBracLoc);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002622}
2623
Sean Huntc3021132010-05-05 15:23:54 +00002624ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002625 unsigned NumArgs) {
Sean Huntc3021132010-05-05 15:23:54 +00002626 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor04badcf2010-04-21 00:45:42 +00002627 NumArgs * sizeof(Expr *);
2628 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2629 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2630}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002631
2632SourceRange ObjCMessageExpr::getReceiverRange() const {
2633 switch (getReceiverKind()) {
2634 case Instance:
2635 return getInstanceReceiver()->getSourceRange();
2636
2637 case Class:
2638 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2639
2640 case SuperInstance:
2641 case SuperClass:
2642 return getSuperLoc();
2643 }
2644
2645 return SourceLocation();
2646}
2647
Douglas Gregor04badcf2010-04-21 00:45:42 +00002648Selector ObjCMessageExpr::getSelector() const {
2649 if (HasMethod)
2650 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2651 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002652 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002653}
2654
2655ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2656 switch (getReceiverKind()) {
2657 case Instance:
2658 if (const ObjCObjectPointerType *Ptr
2659 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2660 return Ptr->getInterfaceDecl();
2661 break;
2662
2663 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002664 if (const ObjCObjectType *Ty
2665 = getClassReceiver()->getAs<ObjCObjectType>())
2666 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002667 break;
2668
2669 case SuperInstance:
2670 if (const ObjCObjectPointerType *Ptr
2671 = getSuperType()->getAs<ObjCObjectPointerType>())
2672 return Ptr->getInterfaceDecl();
2673 break;
2674
2675 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002676 if (const ObjCObjectType *Iface
2677 = getSuperType()->getAs<ObjCObjectType>())
2678 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002679 break;
2680 }
2681
2682 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00002683}
Chris Lattner0389e6b2009-04-26 00:44:05 +00002684
Jay Foad4ba2a172011-01-12 09:06:06 +00002685bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman9a901bb2009-04-26 19:19:15 +00002686 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00002687}
2688
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002689ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2690 QualType Type, SourceLocation BLoc,
2691 SourceLocation RP)
2692 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2693 Type->isDependentType(), Type->isDependentType(),
2694 Type->containsUnexpandedParameterPack()),
2695 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2696{
2697 SubExprs = new (C) Stmt*[nexpr];
2698 for (unsigned i = 0; i < nexpr; i++) {
2699 if (args[i]->isTypeDependent())
2700 ExprBits.TypeDependent = true;
2701 if (args[i]->isValueDependent())
2702 ExprBits.ValueDependent = true;
2703 if (args[i]->containsUnexpandedParameterPack())
2704 ExprBits.ContainsUnexpandedParameterPack = true;
2705
2706 SubExprs[i] = args[i];
2707 }
2708}
2709
Nate Begeman888376a2009-08-12 02:28:50 +00002710void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2711 unsigned NumExprs) {
2712 if (SubExprs) C.Deallocate(SubExprs);
2713
2714 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00002715 this->NumExprs = NumExprs;
2716 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00002717}
Nate Begeman888376a2009-08-12 02:28:50 +00002718
Peter Collingbournef111d932011-04-15 00:35:48 +00002719GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2720 SourceLocation GenericLoc, Expr *ControllingExpr,
2721 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2722 unsigned NumAssocs, SourceLocation DefaultLoc,
2723 SourceLocation RParenLoc,
2724 bool ContainsUnexpandedParameterPack,
2725 unsigned ResultIndex)
2726 : Expr(GenericSelectionExprClass,
2727 AssocExprs[ResultIndex]->getType(),
2728 AssocExprs[ResultIndex]->getValueKind(),
2729 AssocExprs[ResultIndex]->getObjectKind(),
2730 AssocExprs[ResultIndex]->isTypeDependent(),
2731 AssocExprs[ResultIndex]->isValueDependent(),
2732 ContainsUnexpandedParameterPack),
2733 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2734 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2735 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2736 RParenLoc(RParenLoc) {
2737 SubExprs[CONTROLLING] = ControllingExpr;
2738 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2739 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2740}
2741
2742GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2743 SourceLocation GenericLoc, Expr *ControllingExpr,
2744 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2745 unsigned NumAssocs, SourceLocation DefaultLoc,
2746 SourceLocation RParenLoc,
2747 bool ContainsUnexpandedParameterPack)
2748 : Expr(GenericSelectionExprClass,
2749 Context.DependentTy,
2750 VK_RValue,
2751 OK_Ordinary,
2752 /*isTypeDependent=*/ true,
2753 /*isValueDependent=*/ true,
2754 ContainsUnexpandedParameterPack),
2755 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2756 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2757 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2758 RParenLoc(RParenLoc) {
2759 SubExprs[CONTROLLING] = ControllingExpr;
2760 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2761 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2762}
2763
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002764//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00002765// DesignatedInitExpr
2766//===----------------------------------------------------------------------===//
2767
2768IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2769 assert(Kind == FieldDesignator && "Only valid on a field designator");
2770 if (Field.NameOrField & 0x01)
2771 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2772 else
2773 return getField()->getIdentifier();
2774}
2775
Sean Huntc3021132010-05-05 15:23:54 +00002776DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00002777 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002778 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00002779 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002780 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00002781 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002782 unsigned NumIndexExprs,
2783 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00002784 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00002785 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002786 Init->isTypeDependent(), Init->isValueDependent(),
2787 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00002788 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2789 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002790 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002791
2792 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00002793 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00002794 *Child++ = Init;
2795
2796 // Copy the designators and their subexpressions, computing
2797 // value-dependence along the way.
2798 unsigned IndexIdx = 0;
2799 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002800 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00002801
2802 if (this->Designators[I].isArrayDesignator()) {
2803 // Compute type- and value-dependence.
2804 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002805 if (Index->isTypeDependent() || Index->isValueDependent())
2806 ExprBits.ValueDependent = true;
2807
2808 // Propagate unexpanded parameter packs.
2809 if (Index->containsUnexpandedParameterPack())
2810 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002811
2812 // Copy the index expressions into permanent storage.
2813 *Child++ = IndexExprs[IndexIdx++];
2814 } else if (this->Designators[I].isArrayRangeDesignator()) {
2815 // Compute type- and value-dependence.
2816 Expr *Start = IndexExprs[IndexIdx];
2817 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002818 if (Start->isTypeDependent() || Start->isValueDependent() ||
2819 End->isTypeDependent() || End->isValueDependent())
2820 ExprBits.ValueDependent = true;
2821
2822 // Propagate unexpanded parameter packs.
2823 if (Start->containsUnexpandedParameterPack() ||
2824 End->containsUnexpandedParameterPack())
2825 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00002826
2827 // Copy the start/end expressions into permanent storage.
2828 *Child++ = IndexExprs[IndexIdx++];
2829 *Child++ = IndexExprs[IndexIdx++];
2830 }
2831 }
2832
2833 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002834}
2835
Douglas Gregor05c13a32009-01-22 00:58:24 +00002836DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00002837DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00002838 unsigned NumDesignators,
2839 Expr **IndexExprs, unsigned NumIndexExprs,
2840 SourceLocation ColonOrEqualLoc,
2841 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00002842 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00002843 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00002844 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00002845 ColonOrEqualLoc, UsesColonSyntax,
2846 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002847}
2848
Mike Stump1eb44332009-09-09 15:08:12 +00002849DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00002850 unsigned NumIndexExprs) {
2851 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2852 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2853 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2854}
2855
Douglas Gregor319d57f2010-01-06 23:17:19 +00002856void DesignatedInitExpr::setDesignators(ASTContext &C,
2857 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00002858 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00002859 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00002860 NumDesignators = NumDesigs;
2861 for (unsigned I = 0; I != NumDesigs; ++I)
2862 Designators[I] = Desigs[I];
2863}
2864
Abramo Bagnara24f46742011-03-16 15:08:46 +00002865SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
2866 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
2867 if (size() == 1)
2868 return DIE->getDesignator(0)->getSourceRange();
2869 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
2870 DIE->getDesignator(size()-1)->getEndLocation());
2871}
2872
Douglas Gregor05c13a32009-01-22 00:58:24 +00002873SourceRange DesignatedInitExpr::getSourceRange() const {
2874 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002875 Designator &First =
2876 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00002877 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00002878 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00002879 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2880 else
2881 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2882 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00002883 StartLoc =
2884 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002885 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2886}
2887
Douglas Gregor05c13a32009-01-22 00:58:24 +00002888Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2889 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2890 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2891 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002892 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2893 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2894}
2895
2896Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002897 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002898 "Requires array range designator");
2899 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2900 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002901 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2902 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2903}
2904
2905Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00002906 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00002907 "Requires array range designator");
2908 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2909 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00002910 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2911 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2912}
2913
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002914/// \brief Replaces the designator at index @p Idx with the series
2915/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00002916void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00002917 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002918 const Designator *Last) {
2919 unsigned NumNewDesignators = Last - First;
2920 if (NumNewDesignators == 0) {
2921 std::copy_backward(Designators + Idx + 1,
2922 Designators + NumDesignators,
2923 Designators + Idx);
2924 --NumNewDesignators;
2925 return;
2926 } else if (NumNewDesignators == 1) {
2927 Designators[Idx] = *First;
2928 return;
2929 }
2930
Mike Stump1eb44332009-09-09 15:08:12 +00002931 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00002932 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002933 std::copy(Designators, Designators + Idx, NewDesignators);
2934 std::copy(First, Last, NewDesignators + Idx);
2935 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2936 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00002937 Designators = NewDesignators;
2938 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2939}
2940
Mike Stump1eb44332009-09-09 15:08:12 +00002941ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00002942 Expr **exprs, unsigned nexprs,
2943 SourceLocation rparenloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002944 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2945 false, false, false),
2946 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump1eb44332009-09-09 15:08:12 +00002947
Nate Begeman2ef13e52009-08-10 23:49:36 +00002948 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002949 for (unsigned i = 0; i != nexprs; ++i) {
2950 if (exprs[i]->isTypeDependent())
2951 ExprBits.TypeDependent = true;
2952 if (exprs[i]->isValueDependent())
2953 ExprBits.ValueDependent = true;
2954 if (exprs[i]->containsUnexpandedParameterPack())
2955 ExprBits.ContainsUnexpandedParameterPack = true;
2956
Nate Begeman2ef13e52009-08-10 23:49:36 +00002957 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002958 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00002959}
2960
John McCalle996ffd2011-02-16 08:02:54 +00002961const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
2962 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
2963 e = ewc->getSubExpr();
2964 e = cast<CXXConstructExpr>(e)->getArg(0);
2965 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
2966 e = ice->getSubExpr();
2967 return cast<OpaqueValueExpr>(e);
2968}
2969
Douglas Gregor05c13a32009-01-22 00:58:24 +00002970//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00002971// ExprIterator.
2972//===----------------------------------------------------------------------===//
2973
2974Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2975Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2976Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2977const Expr* ConstExprIterator::operator[](size_t idx) const {
2978 return cast<Expr>(I[idx]);
2979}
2980const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2981const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2982
2983//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00002984// Child Iterators for iterating over subexpressions/substatements
2985//===----------------------------------------------------------------------===//
2986
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00002987// UnaryExprOrTypeTraitExpr
2988Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00002989 // If this is of a type and the type is a VLA type (and not a typedef), the
2990 // size expression of the VLA needs to be treated as an executable expression.
2991 // Why isn't this weirdness documented better in StmtIterator?
2992 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00002993 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00002994 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00002995 return child_range(child_iterator(T), child_iterator());
2996 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00002997 }
John McCall63c00d72011-02-09 08:16:59 +00002998 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00002999}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003000
Steve Naroff563477d2007-09-18 23:55:05 +00003001// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003002Stmt::child_range ObjCMessageExpr::children() {
3003 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003004 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003005 begin = reinterpret_cast<Stmt **>(this + 1);
3006 else
3007 begin = reinterpret_cast<Stmt **>(getArgs());
3008 return child_range(begin,
3009 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003010}
3011
Steve Naroff4eb206b2008-09-03 18:15:37 +00003012// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003013BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003014 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003015 bool constAdded)
Douglas Gregord967e312011-01-19 21:52:31 +00003016 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003017 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003018 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003019{
Douglas Gregord967e312011-01-19 21:52:31 +00003020 bool TypeDependent = false;
3021 bool ValueDependent = false;
3022 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
3023 ExprBits.TypeDependent = TypeDependent;
3024 ExprBits.ValueDependent = ValueDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003025}
3026