blob: a4365f1c44ad5f64509a2c93139137372661479b [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor96ee7892009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattnere925d612010-11-17 07:37:15 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/Lexer.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000025#include "clang/Basic/Builtins.h"
Chris Lattnere925d612010-11-17 07:37:15 +000026#include "clang/Basic/SourceManager.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000027#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000028#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000029#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000030#include <algorithm>
Chris Lattner1b926492006-08-23 06:42:10 +000031using namespace clang;
32
Chris Lattner4ebae652010-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 Collingbourne91147592011-04-15 00:35:48 +000038 const Expr *E = IgnoreParens();
39
Chris Lattner4ebae652010-04-16 23:34:13 +000040 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbourne91147592011-04-15 00:35:48 +000041 if (E->getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000042 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbourne91147592011-04-15 00:35:48 +000043 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +000044
Peter Collingbourne91147592011-04-15 00:35:48 +000045 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +000046 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +000047 case UO_Plus:
Chris Lattner4ebae652010-04-16 23:34:13 +000048 return UO->getSubExpr()->isKnownToHaveBooleanValue();
49 default:
50 return false;
51 }
52 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000053
John McCall45d30c32010-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 Collingbourne91147592011-04-15 00:35:48 +000056 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +000057 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000058
Peter Collingbourne91147592011-04-15 00:35:48 +000059 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +000060 switch (BO->getOpcode()) {
61 default: return false;
John McCalle3027922010-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 Lattner4ebae652010-04-16 23:34:13 +000070 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000071
John McCalle3027922010-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 Lattner4ebae652010-04-16 23:34:13 +000075 // Handle things like (x==2)|(y==12).
76 return BO->getLHS()->isKnownToHaveBooleanValue() &&
77 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000078
John McCalle3027922010-08-25 11:45:40 +000079 case BO_Comma:
80 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +000081 return BO->getRHS()->isKnownToHaveBooleanValue();
82 }
83 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000084
Peter Collingbourne91147592011-04-15 00:35:48 +000085 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +000086 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
87 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000088
Chris Lattner4ebae652010-04-16 23:34:13 +000089 return false;
90}
91
John McCallbd066782011-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
Chris Lattner0eedafe2006-08-24 04:56:27 +0000128//===----------------------------------------------------------------------===//
129// Primary Expressions.
130//===----------------------------------------------------------------------===//
131
John McCall6b51f282009-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 Gregora6e053e2010-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 McCall6b51f282009-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 Kyrtzidisbfcacee2010-06-24 08:57:31 +0000170std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
171 return sizeof(ExplicitTemplateArgumentList) +
172 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
173}
174
John McCall6b51f282009-11-23 01:53:49 +0000175std::size_t ExplicitTemplateArgumentList::sizeFor(
176 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000177 return sizeFor(Info.size());
John McCall6b51f282009-11-23 01:53:49 +0000178}
179
Douglas Gregorf144f4f2011-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;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000187
Douglas Gregored6c7442009-11-23 11:41:28 +0000188
189 // (TD) C++ [temp.dep.expr]p3:
190 // An id-expression is type-dependent if it contains:
191 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000192 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000193 //
194 // (VD) C++ [temp.dep.constexpr]p2:
195 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000196
Douglas Gregored6c7442009-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 Gregorf144f4f2011-01-19 21:52:31 +0000199 if (T->isDependentType()) {
200 TypeDependent = true;
201 ValueDependent = true;
202 return;
Douglas Gregored6c7442009-11-23 11:41:28 +0000203 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000204
Douglas Gregored6c7442009-11-23 11:41:28 +0000205 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000206 if (D->getDeclName().getNameKind()
207 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregored6c7442009-11-23 11:41:28 +0000208 D->getDeclName().getCXXNameType()->isDependentType()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000209 TypeDependent = true;
210 ValueDependent = true;
211 return;
Douglas Gregored6c7442009-11-23 11:41:28 +0000212 }
213 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000214 if (isa<NonTypeTemplateParmDecl>(D)) {
215 ValueDependent = true;
216 return;
217 }
218
Douglas Gregored6c7442009-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 Gregorf144f4f2011-01-19 21:52:31 +0000221 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000222 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000223 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000224 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000225 if (Init->isValueDependent())
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000226 ValueDependent = true;
Douglas Gregor0e4de762010-05-11 08:41:30 +0000227 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000228
Douglas Gregor0e4de762010-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 Gregorbe49fc52010-05-11 08:44:04 +0000233 Var->getDeclContext()->isDependentContext())
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000234 ValueDependent = true;
235
236 return;
237 }
238
Douglas Gregor0e4de762010-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 Gregorf144f4f2011-01-19 21:52:31 +0000242 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
243 ValueDependent = true;
244 return;
245 }
246}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000247
Douglas Gregorf144f4f2011-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 Gregorda3cc0d2010-12-23 23:51:58 +0000272 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000273 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000274 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000275}
276
Douglas Gregorea972d32011-02-28 21:54:11 +0000277DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
John McCallce546572009-12-08 09:08:17 +0000278 ValueDecl *D, SourceLocation NameLoc,
John McCall6b51f282009-11-23 01:53:49 +0000279 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000280 QualType T, ExprValueKind VK)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000281 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000282 D(D), Loc(NameLoc) {
283 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000284 if (QualifierLoc)
285 getNameQualifier().QualifierLoc = QualifierLoc;
Chandler Carruth0e439962011-05-01 21:29:53 +0000286
287 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
288 if (TemplateArgs) {
John McCallb3774b52010-08-19 23:49:38 +0000289 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Chandler Carruth0e439962011-05-01 21:29:53 +0000290 }
Douglas Gregored6c7442009-11-23 11:41:28 +0000291
292 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000293}
294
Douglas Gregorea972d32011-02-28 21:54:11 +0000295DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000296 ValueDecl *D, const DeclarationNameInfo &NameInfo,
297 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000298 QualType T, ExprValueKind VK)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000299 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000300 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
301 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000302 if (QualifierLoc)
303 getNameQualifier().QualifierLoc = QualifierLoc;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000304
Chandler Carruth0e439962011-05-01 21:29:53 +0000305 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000306 if (TemplateArgs)
John McCallb3774b52010-08-19 23:49:38 +0000307 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000308
309 computeDependence();
310}
311
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000312DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000313 NestedNameSpecifierLoc QualifierLoc,
John McCallce546572009-12-08 09:08:17 +0000314 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000315 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000316 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000317 ExprValueKind VK,
Douglas Gregored6c7442009-11-23 11:41:28 +0000318 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000319 return Create(Context, QualifierLoc, D,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000320 DeclarationNameInfo(D->getDeclName(), NameLoc),
John McCall7decc9e2010-11-18 06:31:45 +0000321 T, VK, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000322}
323
324DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000325 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000326 ValueDecl *D,
327 const DeclarationNameInfo &NameInfo,
328 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000329 ExprValueKind VK,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000330 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000331 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregorea972d32011-02-28 21:54:11 +0000332 if (QualifierLoc != 0)
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000333 Size += sizeof(NameQualifier);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000334
John McCall6b51f282009-11-23 01:53:49 +0000335 if (TemplateArgs)
336 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000337
Chris Lattner5c0b4052010-10-30 05:14:06 +0000338 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Douglas Gregorea972d32011-02-28 21:54:11 +0000339 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000340}
341
Douglas Gregor87866ce2011-02-04 12:01:24 +0000342DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
343 bool HasQualifier,
344 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000345 unsigned NumTemplateArgs) {
346 std::size_t Size = sizeof(DeclRefExpr);
347 if (HasQualifier)
348 Size += sizeof(NameQualifier);
349
Douglas Gregor87866ce2011-02-04 12:01:24 +0000350 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000351 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
352
Chris Lattner5c0b4052010-10-30 05:14:06 +0000353 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000354 return new (Mem) DeclRefExpr(EmptyShell());
355}
356
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000357SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000358 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000359 if (hasQualifier())
Douglas Gregorea972d32011-02-28 21:54:11 +0000360 R.setBegin(getQualifierLoc().getBeginLoc());
John McCallb3774b52010-08-19 23:49:38 +0000361 if (hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000362 R.setEnd(getRAngleLoc());
363 return R;
364}
365
Anders Carlsson2fb08242009-09-08 18:24:21 +0000366// FIXME: Maybe this should use DeclPrinter with a special "print predefined
367// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000368std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
369 ASTContext &Context = CurrentDecl->getASTContext();
370
Anders Carlsson2fb08242009-09-08 18:24:21 +0000371 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000372 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000373 return FD->getNameAsString();
374
375 llvm::SmallString<256> Name;
376 llvm::raw_svector_ostream Out(Name);
377
378 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000379 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000380 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000381 if (MD->isStatic())
382 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000383 }
384
385 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000386
387 std::string Proto = FD->getQualifiedNameAsString(Policy);
388
John McCall9dd450b2009-09-21 23:43:11 +0000389 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000390 const FunctionProtoType *FT = 0;
391 if (FD->hasWrittenPrototype())
392 FT = dyn_cast<FunctionProtoType>(AFT);
393
394 Proto += "(";
395 if (FT) {
396 llvm::raw_string_ostream POut(Proto);
397 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
398 if (i) POut << ", ";
399 std::string Param;
400 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
401 POut << Param;
402 }
403
404 if (FT->isVariadic()) {
405 if (FD->getNumParams()) POut << ", ";
406 POut << "...";
407 }
408 }
409 Proto += ")";
410
Sam Weinig4e83bd22009-12-27 01:38:20 +0000411 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
412 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
413 if (ThisQuals.hasConst())
414 Proto += " const";
415 if (ThisQuals.hasVolatile())
416 Proto += " volatile";
417 }
418
Sam Weinigd060ed42009-12-06 23:55:13 +0000419 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
420 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000421
422 Out << Proto;
423
424 Out.flush();
425 return Name.str().str();
426 }
427 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
428 llvm::SmallString<256> Name;
429 llvm::raw_svector_ostream Out(Name);
430 Out << (MD->isInstanceMethod() ? '-' : '+');
431 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000432
433 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
434 // a null check to avoid a crash.
435 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000436 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000437
Anders Carlsson2fb08242009-09-08 18:24:21 +0000438 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000439 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
440 Out << '(' << CID << ')';
441
Anders Carlsson2fb08242009-09-08 18:24:21 +0000442 Out << ' ';
443 Out << MD->getSelector().getAsString();
444 Out << ']';
445
446 Out.flush();
447 return Name.str().str();
448 }
449 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
450 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
451 return "top level";
452 }
453 return "";
454}
455
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000456void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
457 if (hasAllocation())
458 C.Deallocate(pVal);
459
460 BitWidth = Val.getBitWidth();
461 unsigned NumWords = Val.getNumWords();
462 const uint64_t* Words = Val.getRawData();
463 if (NumWords > 1) {
464 pVal = new (C) uint64_t[NumWords];
465 std::copy(Words, Words + NumWords, pVal);
466 } else if (NumWords == 1)
467 VAL = Words[0];
468 else
469 VAL = 0;
470}
471
472IntegerLiteral *
473IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
474 QualType type, SourceLocation l) {
475 return new (C) IntegerLiteral(C, V, type, l);
476}
477
478IntegerLiteral *
479IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
480 return new (C) IntegerLiteral(Empty);
481}
482
483FloatingLiteral *
484FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
485 bool isexact, QualType Type, SourceLocation L) {
486 return new (C) FloatingLiteral(C, V, isexact, Type, L);
487}
488
489FloatingLiteral *
490FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
491 return new (C) FloatingLiteral(Empty);
492}
493
Chris Lattnera0173132008-06-07 22:13:43 +0000494/// getValueAsApproximateDouble - This returns the value as an inaccurate
495/// double. Note that this may cause loss of precision, but is useful for
496/// debugging dumps, etc.
497double FloatingLiteral::getValueAsApproximateDouble() const {
498 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000499 bool ignored;
500 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
501 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000502 return V.convertToDouble();
503}
504
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000505StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
506 unsigned ByteLength, bool Wide,
Anders Carlsson75245402011-04-14 00:40:03 +0000507 bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000508 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000509 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000510 // Allocate enough space for the StringLiteral plus an array of locations for
511 // any concatenated string tokens.
512 void *Mem = C.Allocate(sizeof(StringLiteral)+
513 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000514 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000515 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000516
Steve Naroffdf7855b2007-02-21 23:46:25 +0000517 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000518 char *AStrData = new (C, 1) char[ByteLength];
519 memcpy(AStrData, StrData, ByteLength);
520 SL->StrData = AStrData;
521 SL->ByteLength = ByteLength;
522 SL->IsWide = Wide;
Anders Carlsson75245402011-04-14 00:40:03 +0000523 SL->IsPascal = Pascal;
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000524 SL->TokLocs[0] = Loc[0];
525 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000526
Chris Lattner630970d2009-02-18 05:49:11 +0000527 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000528 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
529 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000530}
531
Douglas Gregor958dfc92009-04-15 16:35:07 +0000532StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
533 void *Mem = C.Allocate(sizeof(StringLiteral)+
534 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000535 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000536 StringLiteral *SL = new (Mem) StringLiteral(QualType());
537 SL->StrData = 0;
538 SL->ByteLength = 0;
539 SL->NumConcatenated = NumStrs;
540 return SL;
541}
542
Daniel Dunbar36217882009-09-22 03:27:33 +0000543void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbar36217882009-09-22 03:27:33 +0000544 char *AStrData = new (C, 1) char[Str.size()];
545 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000546 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000547 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000548}
549
Chris Lattnere925d612010-11-17 07:37:15 +0000550/// getLocationOfByte - Return a source location that points to the specified
551/// byte of this string literal.
552///
553/// Strings are amazingly complex. They can be formed from multiple tokens and
554/// can have escape sequences in them in addition to the usual trigraph and
555/// escaped newline business. This routine handles this complexity.
556///
557SourceLocation StringLiteral::
558getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
559 const LangOptions &Features, const TargetInfo &Target) const {
560 assert(!isWide() && "This doesn't work for wide strings yet");
561
562 // Loop over all of the tokens in this string until we find the one that
563 // contains the byte we're looking for.
564 unsigned TokNo = 0;
565 while (1) {
566 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
567 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
568
569 // Get the spelling of the string so that we can get the data that makes up
570 // the string literal, not the identifier for the macro it is potentially
571 // expanded through.
572 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
573
574 // Re-lex the token to get its length and original spelling.
575 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
576 bool Invalid = false;
577 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
578 if (Invalid)
579 return StrTokSpellingLoc;
580
581 const char *StrData = Buffer.data()+LocInfo.second;
582
583 // Create a langops struct and enable trigraphs. This is sufficient for
584 // relexing tokens.
585 LangOptions LangOpts;
586 LangOpts.Trigraphs = true;
587
588 // Create a lexer starting at the beginning of this token.
589 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
590 Buffer.end());
591 Token TheTok;
592 TheLexer.LexFromRawLexer(TheTok);
593
594 // Use the StringLiteralParser to compute the length of the string in bytes.
595 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
596 unsigned TokNumBytes = SLP.GetStringLength();
597
598 // If the byte is in this token, return the location of the byte.
599 if (ByteNo < TokNumBytes ||
600 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
601 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
602
603 // Now that we know the offset of the token in the spelling, use the
604 // preprocessor to get the offset in the original source.
605 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
606 }
607
608 // Move to the next string token.
609 ++TokNo;
610 ByteNo -= TokNumBytes;
611 }
612}
613
614
615
Chris Lattner1b926492006-08-23 06:42:10 +0000616/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
617/// corresponds to, e.g. "sizeof" or "[pre]++".
618const char *UnaryOperator::getOpcodeStr(Opcode Op) {
619 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000620 default: assert(0 && "Unknown unary operator");
John McCalle3027922010-08-25 11:45:40 +0000621 case UO_PostInc: return "++";
622 case UO_PostDec: return "--";
623 case UO_PreInc: return "++";
624 case UO_PreDec: return "--";
625 case UO_AddrOf: return "&";
626 case UO_Deref: return "*";
627 case UO_Plus: return "+";
628 case UO_Minus: return "-";
629 case UO_Not: return "~";
630 case UO_LNot: return "!";
631 case UO_Real: return "__real";
632 case UO_Imag: return "__imag";
633 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000634 }
635}
636
John McCalle3027922010-08-25 11:45:40 +0000637UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000638UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
639 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000640 default: assert(false && "No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000641 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
642 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
643 case OO_Amp: return UO_AddrOf;
644 case OO_Star: return UO_Deref;
645 case OO_Plus: return UO_Plus;
646 case OO_Minus: return UO_Minus;
647 case OO_Tilde: return UO_Not;
648 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000649 }
650}
651
652OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
653 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000654 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
655 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
656 case UO_AddrOf: return OO_Amp;
657 case UO_Deref: return OO_Star;
658 case UO_Plus: return OO_Plus;
659 case UO_Minus: return OO_Minus;
660 case UO_Not: return OO_Tilde;
661 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000662 default: return OO_None;
663 }
664}
665
666
Chris Lattner0eedafe2006-08-24 04:56:27 +0000667//===----------------------------------------------------------------------===//
668// Postfix Operators.
669//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000670
Peter Collingbourne3a347252011-02-08 21:18:02 +0000671CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
672 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCall7decc9e2010-11-18 06:31:45 +0000673 SourceLocation rparenloc)
674 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000675 fn->isTypeDependent(),
676 fn->isValueDependent(),
677 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000678 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000679
Peter Collingbourne3a347252011-02-08 21:18:02 +0000680 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +0000681 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000682 for (unsigned i = 0; i != numargs; ++i) {
683 if (args[i]->isTypeDependent())
684 ExprBits.TypeDependent = true;
685 if (args[i]->isValueDependent())
686 ExprBits.ValueDependent = true;
687 if (args[i]->containsUnexpandedParameterPack())
688 ExprBits.ContainsUnexpandedParameterPack = true;
689
Peter Collingbourne3a347252011-02-08 21:18:02 +0000690 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000691 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000692
Peter Collingbourne3a347252011-02-08 21:18:02 +0000693 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +0000694 RParenLoc = rparenloc;
695}
Nate Begeman1e36a852008-01-17 17:46:27 +0000696
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000697CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCall7decc9e2010-11-18 06:31:45 +0000698 QualType t, ExprValueKind VK, SourceLocation rparenloc)
699 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000700 fn->isTypeDependent(),
701 fn->isValueDependent(),
702 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000703 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000704
Peter Collingbourne3a347252011-02-08 21:18:02 +0000705 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000706 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000707 for (unsigned i = 0; i != numargs; ++i) {
708 if (args[i]->isTypeDependent())
709 ExprBits.TypeDependent = true;
710 if (args[i]->isValueDependent())
711 ExprBits.ValueDependent = true;
712 if (args[i]->containsUnexpandedParameterPack())
713 ExprBits.ContainsUnexpandedParameterPack = true;
714
Peter Collingbourne3a347252011-02-08 21:18:02 +0000715 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000716 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000717
Peter Collingbourne3a347252011-02-08 21:18:02 +0000718 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000719 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000720}
721
Mike Stump11289f42009-09-09 15:08:12 +0000722CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
723 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000724 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +0000725 SubExprs = new (C) Stmt*[PREARGS_START];
726 CallExprBits.NumPreArgs = 0;
727}
728
729CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
730 EmptyShell Empty)
731 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
732 // FIXME: Why do we allocate this?
733 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
734 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +0000735}
736
Nuno Lopes518e3702009-12-20 23:11:08 +0000737Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000738 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl2b1832e2010-09-10 20:55:30 +0000739 // If we're calling a dereference, look at the pointer instead.
740 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
741 if (BO->isPtrMemOp())
742 CEE = BO->getRHS()->IgnoreParenCasts();
743 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
744 if (UO->getOpcode() == UO_Deref)
745 CEE = UO->getSubExpr()->IgnoreParenCasts();
746 }
Chris Lattner52301912009-07-17 15:46:27 +0000747 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000748 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000749 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
750 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000751
752 return 0;
753}
754
Nuno Lopes518e3702009-12-20 23:11:08 +0000755FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000756 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000757}
758
Chris Lattnere4407ed2007-12-28 05:25:02 +0000759/// setNumArgs - This changes the number of arguments present in this call.
760/// Any orphaned expressions are deleted by this, and any new operands are set
761/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000762void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000763 // No change, just return.
764 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000765
Chris Lattnere4407ed2007-12-28 05:25:02 +0000766 // If shrinking # arguments, just delete the extras and forgot them.
767 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000768 this->NumArgs = NumArgs;
769 return;
770 }
771
772 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000773 unsigned NumPreArgs = getNumPreArgs();
774 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000775 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000776 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000777 NewSubExprs[i] = SubExprs[i];
778 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000779 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
780 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000781 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000782
Douglas Gregorba6e5572009-04-17 21:46:47 +0000783 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000784 SubExprs = NewSubExprs;
785 this->NumArgs = NumArgs;
786}
787
Chris Lattner01ff98a2008-10-06 05:00:53 +0000788/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
789/// not, return 0.
Jay Foad39c79802011-01-12 09:06:06 +0000790unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000791 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000792 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000793 // ImplicitCastExpr.
794 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
795 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000796 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000797
Steve Narofff6e3b3292008-01-31 01:07:12 +0000798 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
799 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000800 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000801
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000802 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
803 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000804 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000805
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000806 if (!FDecl->getIdentifier())
807 return 0;
808
Douglas Gregor15fc9562009-09-12 00:22:50 +0000809 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000810}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000811
Anders Carlsson00a27592009-05-26 04:57:27 +0000812QualType CallExpr::getCallReturnType() const {
813 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000814 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000815 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000816 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000817 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +0000818 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
819 // This should never be overloaded and so should never return null.
820 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +0000821
John McCall0009fcc2011-04-26 20:42:42 +0000822 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000823 return FnType->getResultType();
824}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000825
John McCall701417a2011-02-21 06:23:05 +0000826SourceRange CallExpr::getSourceRange() const {
827 if (isa<CXXOperatorCallExpr>(this))
828 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
829
830 SourceLocation begin = getCallee()->getLocStart();
831 if (begin.isInvalid() && getNumArgs() > 0)
832 begin = getArg(0)->getLocStart();
833 SourceLocation end = getRParenLoc();
834 if (end.isInvalid() && getNumArgs() > 0)
835 end = getArg(getNumArgs() - 1)->getLocEnd();
836 return SourceRange(begin, end);
837}
838
Alexis Hunta8136cc2010-05-05 15:23:54 +0000839OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000840 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000841 TypeSourceInfo *tsi,
842 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000843 Expr** exprsPtr, unsigned numExprs,
844 SourceLocation RParenLoc) {
845 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000846 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000847 sizeof(Expr*) * numExprs);
848
849 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
850 exprsPtr, numExprs, RParenLoc);
851}
852
853OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
854 unsigned numComps, unsigned numExprs) {
855 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
856 sizeof(OffsetOfNode) * numComps +
857 sizeof(Expr*) * numExprs);
858 return new (Mem) OffsetOfExpr(numComps, numExprs);
859}
860
Alexis Hunta8136cc2010-05-05 15:23:54 +0000861OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000862 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000863 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000864 Expr** exprsPtr, unsigned numExprs,
865 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +0000866 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
867 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000868 /*ValueDependent=*/tsi->getType()->isDependentType(),
869 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000870 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
871 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000872{
873 for(unsigned i = 0; i < numComps; ++i) {
874 setComponent(i, compsPtr[i]);
875 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000876
Douglas Gregor882211c2010-04-28 22:16:22 +0000877 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000878 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
879 ExprBits.ValueDependent = true;
880 if (exprsPtr[i]->containsUnexpandedParameterPack())
881 ExprBits.ContainsUnexpandedParameterPack = true;
882
Douglas Gregor882211c2010-04-28 22:16:22 +0000883 setIndexExpr(i, exprsPtr[i]);
884 }
885}
886
887IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
888 assert(getKind() == Field || getKind() == Identifier);
889 if (getKind() == Field)
890 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000891
Douglas Gregor882211c2010-04-28 22:16:22 +0000892 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
893}
894
Mike Stump11289f42009-09-09 15:08:12 +0000895MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +0000896 NestedNameSpecifierLoc QualifierLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000897 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000898 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000899 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +0000900 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +0000901 QualType ty,
902 ExprValueKind vk,
903 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000904 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000905
Douglas Gregorea972d32011-02-28 21:54:11 +0000906 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +0000907 founddecl.getDecl() != memberdecl ||
908 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000909 if (hasQualOrFound)
910 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000911
John McCall6b51f282009-11-23 01:53:49 +0000912 if (targs)
913 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000914
Chris Lattner5c0b4052010-10-30 05:14:06 +0000915 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +0000916 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
917 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +0000918
919 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000920 // FIXME: Wrong. We should be looking at the member declaration we found.
921 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +0000922 E->setValueDependent(true);
923 E->setTypeDependent(true);
924 }
925 E->HasQualifierOrFoundDecl = true;
926
927 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +0000928 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +0000929 NQ->FoundDecl = founddecl;
930 }
931
932 if (targs) {
933 E->HasExplicitTemplateArgumentList = true;
John McCallb3774b52010-08-19 23:49:38 +0000934 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall16df1e52010-03-30 21:47:33 +0000935 }
936
937 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000938}
939
Douglas Gregor25b7e052011-03-02 21:06:53 +0000940SourceRange MemberExpr::getSourceRange() const {
941 SourceLocation StartLoc;
942 if (isImplicitAccess()) {
943 if (hasQualifier())
944 StartLoc = getQualifierLoc().getBeginLoc();
945 else
946 StartLoc = MemberLoc;
947 } else {
948 // FIXME: We don't want this to happen. Rather, we should be able to
949 // detect all kinds of implicit accesses more cleanly.
950 StartLoc = getBase()->getLocStart();
951 if (StartLoc.isInvalid())
952 StartLoc = MemberLoc;
953 }
954
955 SourceLocation EndLoc =
956 HasExplicitTemplateArgumentList? getRAngleLoc()
957 : getMemberNameInfo().getEndLoc();
958
959 return SourceRange(StartLoc, EndLoc);
960}
961
Anders Carlsson496335e2009-09-03 00:59:21 +0000962const char *CastExpr::getCastKindName() const {
963 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +0000964 case CK_Dependent:
965 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +0000966 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +0000967 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +0000968 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +0000969 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +0000970 case CK_LValueToRValue:
971 return "LValueToRValue";
John McCall34376a62010-12-04 03:47:34 +0000972 case CK_GetObjCProperty:
973 return "GetObjCProperty";
John McCalle3027922010-08-25 11:45:40 +0000974 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +0000975 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +0000976 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +0000977 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +0000978 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +0000979 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000980 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +0000981 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000982 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +0000983 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +0000984 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000985 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +0000986 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000987 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000988 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000989 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000990 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000991 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +0000992 case CK_NullToPointer:
993 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +0000994 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000995 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000996 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000997 return "DerivedToBaseMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000998 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000999 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001000 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001001 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001002 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001003 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001004 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001005 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001006 case CK_PointerToBoolean:
1007 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001008 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001009 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001010 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001011 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001012 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001013 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001014 case CK_IntegralToBoolean:
1015 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001016 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001017 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001018 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001019 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001020 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001021 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001022 case CK_FloatingToBoolean:
1023 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001024 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001025 return "MemberPointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001026 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahaniane19122f2009-12-08 23:46:15 +00001027 return "AnyPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001028 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001029 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001030 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001031 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001032 case CK_FloatingRealToComplex:
1033 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001034 case CK_FloatingComplexToReal:
1035 return "FloatingComplexToReal";
1036 case CK_FloatingComplexToBoolean:
1037 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001038 case CK_FloatingComplexCast:
1039 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001040 case CK_FloatingComplexToIntegralComplex:
1041 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001042 case CK_IntegralRealToComplex:
1043 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001044 case CK_IntegralComplexToReal:
1045 return "IntegralComplexToReal";
1046 case CK_IntegralComplexToBoolean:
1047 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001048 case CK_IntegralComplexCast:
1049 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001050 case CK_IntegralComplexToFloatingComplex:
1051 return "IntegralComplexToFloatingComplex";
Anders Carlsson496335e2009-09-03 00:59:21 +00001052 }
Mike Stump11289f42009-09-09 15:08:12 +00001053
John McCallc5e62b42010-11-13 09:02:35 +00001054 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001055 return 0;
1056}
1057
Douglas Gregord196a582009-12-14 19:27:10 +00001058Expr *CastExpr::getSubExprAsWritten() {
1059 Expr *SubExpr = 0;
1060 CastExpr *E = this;
1061 do {
1062 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001063
Douglas Gregord196a582009-12-14 19:27:10 +00001064 // Skip any temporary bindings; they're implicit.
1065 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1066 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001067
Douglas Gregord196a582009-12-14 19:27:10 +00001068 // Conversions by constructor and conversion functions have a
1069 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001070 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001071 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001072 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001073 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001074
Douglas Gregord196a582009-12-14 19:27:10 +00001075 // If the subexpression we're left with is an implicit cast, look
1076 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001077 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1078
Douglas Gregord196a582009-12-14 19:27:10 +00001079 return SubExpr;
1080}
1081
John McCallcf142162010-08-07 06:22:56 +00001082CXXBaseSpecifier **CastExpr::path_buffer() {
1083 switch (getStmtClass()) {
1084#define ABSTRACT_STMT(x)
1085#define CASTEXPR(Type, Base) \
1086 case Stmt::Type##Class: \
1087 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1088#define STMT(Type, Base)
1089#include "clang/AST/StmtNodes.inc"
1090 default:
1091 llvm_unreachable("non-cast expressions not possible here");
1092 return 0;
1093 }
1094}
1095
1096void CastExpr::setCastPath(const CXXCastPath &Path) {
1097 assert(Path.size() == path_size());
1098 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1099}
1100
1101ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1102 CastKind Kind, Expr *Operand,
1103 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001104 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001105 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1106 void *Buffer =
1107 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1108 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001109 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001110 if (PathSize) E->setCastPath(*BasePath);
1111 return E;
1112}
1113
1114ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1115 unsigned PathSize) {
1116 void *Buffer =
1117 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1118 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1119}
1120
1121
1122CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001123 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001124 const CXXCastPath *BasePath,
1125 TypeSourceInfo *WrittenTy,
1126 SourceLocation L, SourceLocation R) {
1127 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1128 void *Buffer =
1129 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1130 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001131 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001132 if (PathSize) E->setCastPath(*BasePath);
1133 return E;
1134}
1135
1136CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1137 void *Buffer =
1138 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1139 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1140}
1141
Chris Lattner1b926492006-08-23 06:42:10 +00001142/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1143/// corresponds to, e.g. "<<=".
1144const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1145 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001146 case BO_PtrMemD: return ".*";
1147 case BO_PtrMemI: return "->*";
1148 case BO_Mul: return "*";
1149 case BO_Div: return "/";
1150 case BO_Rem: return "%";
1151 case BO_Add: return "+";
1152 case BO_Sub: return "-";
1153 case BO_Shl: return "<<";
1154 case BO_Shr: return ">>";
1155 case BO_LT: return "<";
1156 case BO_GT: return ">";
1157 case BO_LE: return "<=";
1158 case BO_GE: return ">=";
1159 case BO_EQ: return "==";
1160 case BO_NE: return "!=";
1161 case BO_And: return "&";
1162 case BO_Xor: return "^";
1163 case BO_Or: return "|";
1164 case BO_LAnd: return "&&";
1165 case BO_LOr: return "||";
1166 case BO_Assign: return "=";
1167 case BO_MulAssign: return "*=";
1168 case BO_DivAssign: return "/=";
1169 case BO_RemAssign: return "%=";
1170 case BO_AddAssign: return "+=";
1171 case BO_SubAssign: return "-=";
1172 case BO_ShlAssign: return "<<=";
1173 case BO_ShrAssign: return ">>=";
1174 case BO_AndAssign: return "&=";
1175 case BO_XorAssign: return "^=";
1176 case BO_OrAssign: return "|=";
1177 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001178 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001179
1180 return "";
Chris Lattner1b926492006-08-23 06:42:10 +00001181}
Steve Naroff47500512007-04-19 23:00:49 +00001182
John McCalle3027922010-08-25 11:45:40 +00001183BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001184BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1185 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +00001186 default: assert(false && "Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001187 case OO_Plus: return BO_Add;
1188 case OO_Minus: return BO_Sub;
1189 case OO_Star: return BO_Mul;
1190 case OO_Slash: return BO_Div;
1191 case OO_Percent: return BO_Rem;
1192 case OO_Caret: return BO_Xor;
1193 case OO_Amp: return BO_And;
1194 case OO_Pipe: return BO_Or;
1195 case OO_Equal: return BO_Assign;
1196 case OO_Less: return BO_LT;
1197 case OO_Greater: return BO_GT;
1198 case OO_PlusEqual: return BO_AddAssign;
1199 case OO_MinusEqual: return BO_SubAssign;
1200 case OO_StarEqual: return BO_MulAssign;
1201 case OO_SlashEqual: return BO_DivAssign;
1202 case OO_PercentEqual: return BO_RemAssign;
1203 case OO_CaretEqual: return BO_XorAssign;
1204 case OO_AmpEqual: return BO_AndAssign;
1205 case OO_PipeEqual: return BO_OrAssign;
1206 case OO_LessLess: return BO_Shl;
1207 case OO_GreaterGreater: return BO_Shr;
1208 case OO_LessLessEqual: return BO_ShlAssign;
1209 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1210 case OO_EqualEqual: return BO_EQ;
1211 case OO_ExclaimEqual: return BO_NE;
1212 case OO_LessEqual: return BO_LE;
1213 case OO_GreaterEqual: return BO_GE;
1214 case OO_AmpAmp: return BO_LAnd;
1215 case OO_PipePipe: return BO_LOr;
1216 case OO_Comma: return BO_Comma;
1217 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001218 }
1219}
1220
1221OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1222 static const OverloadedOperatorKind OverOps[] = {
1223 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1224 OO_Star, OO_Slash, OO_Percent,
1225 OO_Plus, OO_Minus,
1226 OO_LessLess, OO_GreaterGreater,
1227 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1228 OO_EqualEqual, OO_ExclaimEqual,
1229 OO_Amp,
1230 OO_Caret,
1231 OO_Pipe,
1232 OO_AmpAmp,
1233 OO_PipePipe,
1234 OO_Equal, OO_StarEqual,
1235 OO_SlashEqual, OO_PercentEqual,
1236 OO_PlusEqual, OO_MinusEqual,
1237 OO_LessLessEqual, OO_GreaterGreaterEqual,
1238 OO_AmpEqual, OO_CaretEqual,
1239 OO_PipeEqual,
1240 OO_Comma
1241 };
1242 return OverOps[Opc];
1243}
1244
Ted Kremenekac034612010-04-13 23:39:13 +00001245InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +00001246 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001247 SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001248 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1249 false),
Ted Kremenekac034612010-04-13 23:39:13 +00001250 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +00001251 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidisb2ed28e2011-04-21 00:27:41 +00001252 HadArrayRangeDesignator(false)
Alexis Hunta8136cc2010-05-05 15:23:54 +00001253{
Ted Kremenek013041e2010-02-19 01:50:18 +00001254 for (unsigned I = 0; I != numInits; ++I) {
1255 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001256 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001257 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001258 ExprBits.ValueDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001259 if (initExprs[I]->containsUnexpandedParameterPack())
1260 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001261 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001262
Ted Kremenekac034612010-04-13 23:39:13 +00001263 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001264}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001265
Ted Kremenekac034612010-04-13 23:39:13 +00001266void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001267 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001268 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001269}
1270
Ted Kremenekac034612010-04-13 23:39:13 +00001271void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001272 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001273}
1274
Ted Kremenekac034612010-04-13 23:39:13 +00001275Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001276 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001277 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001278 InitExprs.back() = expr;
1279 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001280 }
Mike Stump11289f42009-09-09 15:08:12 +00001281
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001282 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1283 InitExprs[Init] = expr;
1284 return Result;
1285}
1286
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001287void InitListExpr::setArrayFiller(Expr *filler) {
1288 ArrayFillerOrUnionFieldInit = filler;
1289 // Fill out any "holes" in the array due to designated initializers.
1290 Expr **inits = getInits();
1291 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1292 if (inits[i] == 0)
1293 inits[i] = filler;
1294}
1295
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001296SourceRange InitListExpr::getSourceRange() const {
1297 if (SyntacticForm)
1298 return SyntacticForm->getSourceRange();
1299 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1300 if (Beg.isInvalid()) {
1301 // Find the first non-null initializer.
1302 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1303 E = InitExprs.end();
1304 I != E; ++I) {
1305 if (Stmt *S = *I) {
1306 Beg = S->getLocStart();
1307 break;
1308 }
1309 }
1310 }
1311 if (End.isInvalid()) {
1312 // Find the first non-null initializer from the end.
1313 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1314 E = InitExprs.rend();
1315 I != E; ++I) {
1316 if (Stmt *S = *I) {
1317 End = S->getSourceRange().getEnd();
1318 break;
1319 }
1320 }
1321 }
1322 return SourceRange(Beg, End);
1323}
1324
Steve Naroff991e99d2008-09-04 15:31:07 +00001325/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001326///
1327const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001328 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +00001329 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001330}
1331
Mike Stump11289f42009-09-09 15:08:12 +00001332SourceLocation BlockExpr::getCaretLocation() const {
1333 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001334}
Mike Stump11289f42009-09-09 15:08:12 +00001335const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001336 return TheBlock->getBody();
1337}
Mike Stump11289f42009-09-09 15:08:12 +00001338Stmt *BlockExpr::getBody() {
1339 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001340}
Steve Naroff415d3d52008-10-08 17:01:13 +00001341
1342
Chris Lattner1ec5f562007-06-27 05:38:08 +00001343//===----------------------------------------------------------------------===//
1344// Generic Expression Routines
1345//===----------------------------------------------------------------------===//
1346
Chris Lattner237f2752009-02-14 07:37:35 +00001347/// isUnusedResultAWarning - Return true if this immediate expression should
1348/// be warned about if the result is unused. If so, fill in Loc and Ranges
1349/// with location to warn on and the source range[s] to report with the
1350/// warning.
1351bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +00001352 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001353 // Don't warn if the expr is type dependent. The type could end up
1354 // instantiating to void.
1355 if (isTypeDependent())
1356 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001357
Chris Lattner1ec5f562007-06-27 05:38:08 +00001358 switch (getStmtClass()) {
1359 default:
John McCallc493a732010-03-12 07:11:26 +00001360 if (getType()->isVoidType())
1361 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001362 Loc = getExprLoc();
1363 R1 = getSourceRange();
1364 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001365 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001366 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +00001367 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001368 case GenericSelectionExprClass:
1369 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1370 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001371 case UnaryOperatorClass: {
1372 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001373
Chris Lattner1ec5f562007-06-27 05:38:08 +00001374 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001375 default: break;
John McCalle3027922010-08-25 11:45:40 +00001376 case UO_PostInc:
1377 case UO_PostDec:
1378 case UO_PreInc:
1379 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001380 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001381 case UO_Deref:
Chris Lattnera44d1162007-06-27 05:58:59 +00001382 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001383 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001384 return false;
1385 break;
John McCalle3027922010-08-25 11:45:40 +00001386 case UO_Real:
1387 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001388 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001389 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1390 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001391 return false;
1392 break;
John McCalle3027922010-08-25 11:45:40 +00001393 case UO_Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001394 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001395 }
Chris Lattner237f2752009-02-14 07:37:35 +00001396 Loc = UO->getOperatorLoc();
1397 R1 = UO->getSubExpr()->getSourceRange();
1398 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001399 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001400 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001401 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001402 switch (BO->getOpcode()) {
1403 default:
1404 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001405 // Consider the RHS of comma for side effects. LHS was checked by
1406 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001407 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001408 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1409 // lvalue-ness) of an assignment written in a macro.
1410 if (IntegerLiteral *IE =
1411 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1412 if (IE->getValue() == 0)
1413 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001414 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1415 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001416 case BO_LAnd:
1417 case BO_LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001418 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1419 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1420 return false;
1421 break;
John McCall1e3715a2010-02-16 04:10:53 +00001422 }
Chris Lattner237f2752009-02-14 07:37:35 +00001423 if (BO->isAssignmentOp())
1424 return false;
1425 Loc = BO->getOperatorLoc();
1426 R1 = BO->getLHS()->getSourceRange();
1427 R2 = BO->getRHS()->getSourceRange();
1428 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001429 }
Chris Lattner86928112007-08-25 02:00:02 +00001430 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001431 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001432 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001433
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001434 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00001435 // If only one of the LHS or RHS is a warning, the operator might
1436 // be being used for control flow. Only warn if both the LHS and
1437 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001438 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremeneke96dad92011-03-01 20:34:48 +00001439 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1440 return false;
1441 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00001442 return true;
Ted Kremeneke96dad92011-03-01 20:34:48 +00001443 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001444 }
1445
Chris Lattnera44d1162007-06-27 05:58:59 +00001446 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001447 // If the base pointer or element is to a volatile pointer/field, accessing
1448 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001449 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001450 return false;
1451 Loc = cast<MemberExpr>(this)->getMemberLoc();
1452 R1 = SourceRange(Loc, Loc);
1453 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1454 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001455
Chris Lattner1ec5f562007-06-27 05:38:08 +00001456 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001457 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001458 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001459 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001460 return false;
1461 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1462 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1463 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1464 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001465
Chris Lattner1ec5f562007-06-27 05:38:08 +00001466 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001467 case CXXOperatorCallExprClass:
1468 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001469 // If this is a direct call, get the callee.
1470 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001471 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001472 // If the callee has attribute pure, const, or warn_unused_result, warn
1473 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001474 //
1475 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1476 // updated to match for QoI.
1477 if (FD->getAttr<WarnUnusedResultAttr>() ||
1478 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1479 Loc = CE->getCallee()->getLocStart();
1480 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001481
Chris Lattner1a6babf2009-10-13 04:53:48 +00001482 if (unsigned NumArgs = CE->getNumArgs())
1483 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1484 CE->getArg(NumArgs-1)->getLocEnd());
1485 return true;
1486 }
Chris Lattner237f2752009-02-14 07:37:35 +00001487 }
1488 return false;
1489 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001490
1491 case CXXTemporaryObjectExprClass:
1492 case CXXConstructExprClass:
1493 return false;
1494
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001495 case ObjCMessageExprClass: {
1496 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1497 const ObjCMethodDecl *MD = ME->getMethodDecl();
1498 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1499 Loc = getExprLoc();
1500 return true;
1501 }
Chris Lattner237f2752009-02-14 07:37:35 +00001502 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001503 }
Mike Stump11289f42009-09-09 15:08:12 +00001504
John McCallb7bd14f2010-12-02 01:19:52 +00001505 case ObjCPropertyRefExprClass:
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001506 Loc = getExprLoc();
1507 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001508 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00001509
Chris Lattner944d3062008-07-26 19:51:01 +00001510 case StmtExprClass: {
1511 // Statement exprs don't logically have side effects themselves, but are
1512 // sometimes used in macros in ways that give them a type that is unused.
1513 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1514 // however, if the result of the stmt expr is dead, we don't want to emit a
1515 // warning.
1516 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001517 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00001518 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001519 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001520 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1521 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1522 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1523 }
Mike Stump11289f42009-09-09 15:08:12 +00001524
John McCallc493a732010-03-12 07:11:26 +00001525 if (getType()->isVoidType())
1526 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001527 Loc = cast<StmtExpr>(this)->getLParenLoc();
1528 R1 = getSourceRange();
1529 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001530 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001531 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001532 // If this is an explicit cast to void, allow it. People do this when they
1533 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001534 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001535 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001536 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1537 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1538 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001539 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001540 if (getType()->isVoidType())
1541 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001542 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001543
Anders Carlsson6aa50392009-11-17 17:11:23 +00001544 // If this is a cast to void or a constructor conversion, check the operand.
1545 // Otherwise, the result of the cast is unused.
John McCalle3027922010-08-25 11:45:40 +00001546 if (CE->getCastKind() == CK_ToVoid ||
1547 CE->getCastKind() == CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001548 return (cast<CastExpr>(this)->getSubExpr()
1549 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001550 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1551 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1552 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001553 }
Mike Stump11289f42009-09-09 15:08:12 +00001554
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001555 case ImplicitCastExprClass:
1556 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001557 return (cast<ImplicitCastExpr>(this)
1558 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001559
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001560 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001561 return (cast<CXXDefaultArgExpr>(this)
1562 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001563
1564 case CXXNewExprClass:
1565 // FIXME: In theory, there might be new expressions that don't have side
1566 // effects (e.g. a placement new with an uninitialized POD).
1567 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001568 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001569 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001570 return (cast<CXXBindTemporaryExpr>(this)
1571 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00001572 case ExprWithCleanupsClass:
1573 return (cast<ExprWithCleanups>(this)
Mike Stump53f9ded2009-11-03 23:25:48 +00001574 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001575 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001576}
1577
Fariborz Jahanian07735332009-02-22 18:40:18 +00001578/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001579/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001580bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00001581 const Expr *E = IgnoreParens();
1582 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001583 default:
1584 return false;
1585 case ObjCIvarRefExprClass:
1586 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001587 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001588 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001589 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001590 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001591 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001592 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001593 case DeclRefExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00001594 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001595 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1596 if (VD->hasGlobalStorage())
1597 return true;
1598 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001599 // dereferencing to a pointer is always a gc'able candidate,
1600 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001601 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001602 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001603 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001604 return false;
1605 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001606 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00001607 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001608 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001609 }
1610 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001611 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001612 }
1613}
Sebastian Redlce354af2010-09-10 20:55:33 +00001614
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001615bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1616 if (isTypeDependent())
1617 return false;
John McCall086a4642010-11-24 05:12:34 +00001618 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001619}
1620
John McCall0009fcc2011-04-26 20:42:42 +00001621QualType Expr::findBoundMemberType(const Expr *expr) {
1622 assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1623
1624 // Bound member expressions are always one of these possibilities:
1625 // x->m x.m x->*y x.*y
1626 // (possibly parenthesized)
1627
1628 expr = expr->IgnoreParens();
1629 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1630 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1631 return mem->getMemberDecl()->getType();
1632 }
1633
1634 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1635 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1636 ->getPointeeType();
1637 assert(type->isFunctionType());
1638 return type;
1639 }
1640
1641 assert(isa<UnresolvedMemberExpr>(expr));
1642 return QualType();
1643}
1644
Sebastian Redlce354af2010-09-10 20:55:33 +00001645static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1646 Expr::CanThrowResult CT2) {
1647 // CanThrowResult constants are ordered so that the maximum is the correct
1648 // merge result.
1649 return CT1 > CT2 ? CT1 : CT2;
1650}
1651
1652static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1653 Expr *E = const_cast<Expr*>(CE);
1654 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall8322c3a2011-02-13 04:07:26 +00001655 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redlce354af2010-09-10 20:55:33 +00001656 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1657 }
1658 return R;
1659}
1660
Sebastian Redl31ad7542011-03-13 17:09:40 +00001661static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Decl *D,
Sebastian Redlce354af2010-09-10 20:55:33 +00001662 bool NullThrows = true) {
1663 if (!D)
1664 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1665
1666 // See if we can get a function type from the decl somehow.
1667 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1668 if (!VD) // If we have no clue what we're calling, assume the worst.
1669 return Expr::CT_Can;
1670
Sebastian Redlb8a76c42010-09-10 22:34:40 +00001671 // As an extension, we assume that __attribute__((nothrow)) functions don't
1672 // throw.
1673 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1674 return Expr::CT_Cannot;
1675
Sebastian Redlce354af2010-09-10 20:55:33 +00001676 QualType T = VD->getType();
1677 const FunctionProtoType *FT;
1678 if ((FT = T->getAs<FunctionProtoType>())) {
1679 } else if (const PointerType *PT = T->getAs<PointerType>())
1680 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1681 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1682 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1683 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1684 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1685 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1686 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1687
1688 if (!FT)
1689 return Expr::CT_Can;
1690
Sebastian Redl31ad7542011-03-13 17:09:40 +00001691 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redlce354af2010-09-10 20:55:33 +00001692}
1693
1694static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1695 if (DC->isTypeDependent())
1696 return Expr::CT_Dependent;
1697
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001698 if (!DC->getTypeAsWritten()->isReferenceType())
1699 return Expr::CT_Cannot;
1700
Sebastian Redlce354af2010-09-10 20:55:33 +00001701 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1702}
1703
1704static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1705 const CXXTypeidExpr *DC) {
1706 if (DC->isTypeOperand())
1707 return Expr::CT_Cannot;
1708
1709 Expr *Op = DC->getExprOperand();
1710 if (Op->isTypeDependent())
1711 return Expr::CT_Dependent;
1712
1713 const RecordType *RT = Op->getType()->getAs<RecordType>();
1714 if (!RT)
1715 return Expr::CT_Cannot;
1716
1717 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1718 return Expr::CT_Cannot;
1719
1720 if (Op->Classify(C).isPRValue())
1721 return Expr::CT_Cannot;
1722
1723 return Expr::CT_Can;
1724}
1725
1726Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1727 // C++ [expr.unary.noexcept]p3:
1728 // [Can throw] if in a potentially-evaluated context the expression would
1729 // contain:
1730 switch (getStmtClass()) {
1731 case CXXThrowExprClass:
1732 // - a potentially evaluated throw-expression
1733 return CT_Can;
1734
1735 case CXXDynamicCastExprClass: {
1736 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1737 // where T is a reference type, that requires a run-time check
1738 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1739 if (CT == CT_Can)
1740 return CT;
1741 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1742 }
1743
1744 case CXXTypeidExprClass:
1745 // - a potentially evaluated typeid expression applied to a glvalue
1746 // expression whose type is a polymorphic class type
1747 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1748
1749 // - a potentially evaluated call to a function, member function, function
1750 // pointer, or member function pointer that does not have a non-throwing
1751 // exception-specification
1752 case CallExprClass:
1753 case CXXOperatorCallExprClass:
1754 case CXXMemberCallExprClass: {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001755 CanThrowResult CT = CanCalleeThrow(C,cast<CallExpr>(this)->getCalleeDecl());
Sebastian Redlce354af2010-09-10 20:55:33 +00001756 if (CT == CT_Can)
1757 return CT;
1758 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1759 }
1760
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001761 case CXXConstructExprClass:
1762 case CXXTemporaryObjectExprClass: {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001763 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redlce354af2010-09-10 20:55:33 +00001764 cast<CXXConstructExpr>(this)->getConstructor());
1765 if (CT == CT_Can)
1766 return CT;
1767 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1768 }
1769
1770 case CXXNewExprClass: {
1771 CanThrowResult CT = MergeCanThrow(
Sebastian Redl31ad7542011-03-13 17:09:40 +00001772 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getOperatorNew()),
1773 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redlce354af2010-09-10 20:55:33 +00001774 /*NullThrows*/false));
1775 if (CT == CT_Can)
1776 return CT;
1777 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1778 }
1779
1780 case CXXDeleteExprClass: {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001781 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redlce354af2010-09-10 20:55:33 +00001782 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1783 if (CT == CT_Can)
1784 return CT;
Sebastian Redla8bac372010-09-10 23:27:10 +00001785 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1786 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1787 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1788 Arg = Cast->getSubExpr();
1789 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1790 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001791 CanThrowResult CT2 = CanCalleeThrow(C,
Sebastian Redla8bac372010-09-10 23:27:10 +00001792 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1793 if (CT2 == CT_Can)
1794 return CT2;
1795 CT = MergeCanThrow(CT, CT2);
1796 }
1797 }
1798 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1799 }
1800
1801 case CXXBindTemporaryExprClass: {
1802 // The bound temporary has to be destroyed again, which might throw.
Sebastian Redl31ad7542011-03-13 17:09:40 +00001803 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redla8bac372010-09-10 23:27:10 +00001804 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1805 if (CT == CT_Can)
1806 return CT;
Sebastian Redlce354af2010-09-10 20:55:33 +00001807 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1808 }
1809
1810 // ObjC message sends are like function calls, but never have exception
1811 // specs.
1812 case ObjCMessageExprClass:
1813 case ObjCPropertyRefExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001814 return CT_Can;
1815
1816 // Many other things have subexpressions, so we have to test those.
1817 // Some are simple:
1818 case ParenExprClass:
1819 case MemberExprClass:
1820 case CXXReinterpretCastExprClass:
1821 case CXXConstCastExprClass:
1822 case ConditionalOperatorClass:
1823 case CompoundLiteralExprClass:
1824 case ExtVectorElementExprClass:
1825 case InitListExprClass:
1826 case DesignatedInitExprClass:
1827 case ParenListExprClass:
1828 case VAArgExprClass:
1829 case CXXDefaultArgExprClass:
John McCall5d413782010-12-06 08:20:24 +00001830 case ExprWithCleanupsClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001831 case ObjCIvarRefExprClass:
1832 case ObjCIsaExprClass:
1833 case ShuffleVectorExprClass:
1834 return CanSubExprsThrow(C, this);
1835
1836 // Some might be dependent for other reasons.
1837 case UnaryOperatorClass:
1838 case ArraySubscriptExprClass:
1839 case ImplicitCastExprClass:
1840 case CStyleCastExprClass:
1841 case CXXStaticCastExprClass:
1842 case CXXFunctionalCastExprClass:
1843 case BinaryOperatorClass:
1844 case CompoundAssignOperatorClass: {
1845 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1846 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1847 }
1848
1849 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1850 case StmtExprClass:
1851 return CT_Can;
1852
1853 case ChooseExprClass:
1854 if (isTypeDependent() || isValueDependent())
1855 return CT_Dependent;
1856 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1857
Peter Collingbourne91147592011-04-15 00:35:48 +00001858 case GenericSelectionExprClass:
1859 if (cast<GenericSelectionExpr>(this)->isResultDependent())
1860 return CT_Dependent;
1861 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
1862
Sebastian Redlce354af2010-09-10 20:55:33 +00001863 // Some expressions are always dependent.
1864 case DependentScopeDeclRefExprClass:
1865 case CXXUnresolvedConstructExprClass:
1866 case CXXDependentScopeMemberExprClass:
1867 return CT_Dependent;
1868
1869 default:
1870 // All other expressions don't have subexpressions, or else they are
1871 // unevaluated.
1872 return CT_Cannot;
1873 }
1874}
1875
Ted Kremenekfff70962008-01-17 16:57:34 +00001876Expr* Expr::IgnoreParens() {
1877 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00001878 while (true) {
1879 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1880 E = P->getSubExpr();
1881 continue;
1882 }
1883 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1884 if (P->getOpcode() == UO_Extension) {
1885 E = P->getSubExpr();
1886 continue;
1887 }
1888 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001889 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1890 if (!P->isResultDependent()) {
1891 E = P->getResultExpr();
1892 continue;
1893 }
1894 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00001895 return E;
1896 }
Ted Kremenekfff70962008-01-17 16:57:34 +00001897}
1898
Chris Lattnerf2660962008-02-13 01:02:39 +00001899/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1900/// or CastExprs or ImplicitCastExprs, returning their operand.
1901Expr *Expr::IgnoreParenCasts() {
1902 Expr *E = this;
1903 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001904 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001905 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001906 continue;
1907 }
1908 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001909 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001910 continue;
1911 }
1912 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1913 if (P->getOpcode() == UO_Extension) {
1914 E = P->getSubExpr();
1915 continue;
1916 }
1917 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001918 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1919 if (!P->isResultDependent()) {
1920 E = P->getResultExpr();
1921 continue;
1922 }
1923 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00001924 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00001925 }
1926}
1927
John McCall5a4ce8b2010-12-04 08:24:19 +00001928/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1929/// casts. This is intended purely as a temporary workaround for code
1930/// that hasn't yet been rewritten to do the right thing about those
1931/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00001932Expr *Expr::IgnoreParenLValueCasts() {
1933 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00001934 while (true) {
John McCall34376a62010-12-04 03:47:34 +00001935 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1936 E = P->getSubExpr();
1937 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00001938 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00001939 if (P->getCastKind() == CK_LValueToRValue) {
1940 E = P->getSubExpr();
1941 continue;
1942 }
John McCall5a4ce8b2010-12-04 08:24:19 +00001943 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1944 if (P->getOpcode() == UO_Extension) {
1945 E = P->getSubExpr();
1946 continue;
1947 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001948 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1949 if (!P->isResultDependent()) {
1950 E = P->getResultExpr();
1951 continue;
1952 }
John McCall34376a62010-12-04 03:47:34 +00001953 }
1954 break;
1955 }
1956 return E;
1957}
1958
John McCalleebc8322010-05-05 22:59:52 +00001959Expr *Expr::IgnoreParenImpCasts() {
1960 Expr *E = this;
1961 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001962 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001963 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001964 continue;
1965 }
1966 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001967 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001968 continue;
1969 }
1970 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1971 if (P->getOpcode() == UO_Extension) {
1972 E = P->getSubExpr();
1973 continue;
1974 }
1975 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001976 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1977 if (!P->isResultDependent()) {
1978 E = P->getResultExpr();
1979 continue;
1980 }
1981 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00001982 return E;
John McCalleebc8322010-05-05 22:59:52 +00001983 }
1984}
1985
Chris Lattneref26c772009-03-13 17:28:01 +00001986/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1987/// value (including ptr->int casts of the same size). Strip off any
1988/// ParenExpr or CastExprs, returning their operand.
1989Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1990 Expr *E = this;
1991 while (true) {
1992 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1993 E = P->getSubExpr();
1994 continue;
1995 }
Mike Stump11289f42009-09-09 15:08:12 +00001996
Chris Lattneref26c772009-03-13 17:28:01 +00001997 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1998 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00001999 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002000 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002001
Chris Lattneref26c772009-03-13 17:28:01 +00002002 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2003 E = SE;
2004 continue;
2005 }
Mike Stump11289f42009-09-09 15:08:12 +00002006
Abramo Bagnara932e3932010-10-15 07:51:18 +00002007 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002008 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002009 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002010 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002011 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2012 E = SE;
2013 continue;
2014 }
2015 }
Mike Stump11289f42009-09-09 15:08:12 +00002016
Abramo Bagnara932e3932010-10-15 07:51:18 +00002017 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2018 if (P->getOpcode() == UO_Extension) {
2019 E = P->getSubExpr();
2020 continue;
2021 }
2022 }
2023
Peter Collingbourne91147592011-04-15 00:35:48 +00002024 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2025 if (!P->isResultDependent()) {
2026 E = P->getResultExpr();
2027 continue;
2028 }
2029 }
2030
Chris Lattneref26c772009-03-13 17:28:01 +00002031 return E;
2032 }
2033}
2034
Douglas Gregord196a582009-12-14 19:27:10 +00002035bool Expr::isDefaultArgument() const {
2036 const Expr *E = this;
2037 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2038 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002039
Douglas Gregord196a582009-12-14 19:27:10 +00002040 return isa<CXXDefaultArgExpr>(E);
2041}
Chris Lattneref26c772009-03-13 17:28:01 +00002042
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002043/// \brief Skip over any no-op casts and any temporary-binding
2044/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002045static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002046 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002047 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002048 E = ICE->getSubExpr();
2049 else
2050 break;
2051 }
2052
2053 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2054 E = BE->getSubExpr();
2055
2056 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002057 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002058 E = ICE->getSubExpr();
2059 else
2060 break;
2061 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002062
2063 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002064}
2065
John McCall7a626f62010-09-15 10:14:12 +00002066/// isTemporaryObject - Determines if this expression produces a
2067/// temporary of the given class type.
2068bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2069 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2070 return false;
2071
Anders Carlsson66bbf502010-11-28 16:40:49 +00002072 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002073
John McCall02dc8c72010-09-15 20:59:13 +00002074 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002075 if (!E->Classify(C).isPRValue()) {
2076 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002077 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002078 return false;
2079 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002080
John McCallf4ee1dd2010-09-16 06:57:56 +00002081 // Black-list a few cases which yield pr-values of class type that don't
2082 // refer to temporaries of that type:
2083
2084 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002085 if (isa<ImplicitCastExpr>(E)) {
2086 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2087 case CK_DerivedToBase:
2088 case CK_UncheckedDerivedToBase:
2089 return false;
2090 default:
2091 break;
2092 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002093 }
2094
John McCallf4ee1dd2010-09-16 06:57:56 +00002095 // - member expressions (all)
2096 if (isa<MemberExpr>(E))
2097 return false;
2098
John McCallc07a0c72011-02-17 10:25:35 +00002099 // - opaque values (all)
2100 if (isa<OpaqueValueExpr>(E))
2101 return false;
2102
John McCall7a626f62010-09-15 10:14:12 +00002103 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002104}
2105
Douglas Gregor25b7e052011-03-02 21:06:53 +00002106bool Expr::isImplicitCXXThis() const {
2107 const Expr *E = this;
2108
2109 // Strip away parentheses and casts we don't care about.
2110 while (true) {
2111 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2112 E = Paren->getSubExpr();
2113 continue;
2114 }
2115
2116 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2117 if (ICE->getCastKind() == CK_NoOp ||
2118 ICE->getCastKind() == CK_LValueToRValue ||
2119 ICE->getCastKind() == CK_DerivedToBase ||
2120 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2121 E = ICE->getSubExpr();
2122 continue;
2123 }
2124 }
2125
2126 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2127 if (UnOp->getOpcode() == UO_Extension) {
2128 E = UnOp->getSubExpr();
2129 continue;
2130 }
2131 }
2132
2133 break;
2134 }
2135
2136 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2137 return This->isImplicit();
2138
2139 return false;
2140}
2141
Douglas Gregor4619e432008-12-05 23:32:09 +00002142/// hasAnyTypeDependentArguments - Determines if any of the expressions
2143/// in Exprs is type-dependent.
2144bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2145 for (unsigned I = 0; I < NumExprs; ++I)
2146 if (Exprs[I]->isTypeDependent())
2147 return true;
2148
2149 return false;
2150}
2151
2152/// hasAnyValueDependentArguments - Determines if any of the expressions
2153/// in Exprs is value-dependent.
2154bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2155 for (unsigned I = 0; I < NumExprs; ++I)
2156 if (Exprs[I]->isValueDependent())
2157 return true;
2158
2159 return false;
2160}
2161
John McCall8b0f4ff2010-08-02 21:13:48 +00002162bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002163 // This function is attempting whether an expression is an initializer
2164 // which can be evaluated at compile-time. isEvaluatable handles most
2165 // of the cases, but it can't deal with some initializer-specific
2166 // expressions, and it can't deal with aggregates; we deal with those here,
2167 // and fall back to isEvaluatable for the other cases.
2168
John McCall8b0f4ff2010-08-02 21:13:48 +00002169 // If we ever capture reference-binding directly in the AST, we can
2170 // kill the second parameter.
2171
2172 if (IsForRef) {
2173 EvalResult Result;
2174 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2175 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002176
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002177 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002178 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002179 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002180 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002181 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002182 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002183 case CXXTemporaryObjectExprClass:
2184 case CXXConstructExprClass: {
2185 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002186
2187 // Only if it's
2188 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00002189 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002190 if (!CE->getNumArgs()) return true;
2191
2192 // 2) an elidable trivial copy construction of an operand which is
2193 // itself a constant initializer. Note that we consider the
2194 // operand on its own, *not* as a reference binding.
2195 return CE->isElidable() &&
2196 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00002197 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002198 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002199 // This handles gcc's extension that allows global initializers like
2200 // "struct x {int x;} x = (struct x) {};".
2201 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002202 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002203 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002204 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002205 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002206 // FIXME: This doesn't deal with fields with reference types correctly.
2207 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2208 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002209 const InitListExpr *Exp = cast<InitListExpr>(this);
2210 unsigned numInits = Exp->getNumInits();
2211 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002212 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002213 return false;
2214 }
Eli Friedman384da272009-01-25 03:12:18 +00002215 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002216 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002217 case ImplicitValueInitExprClass:
2218 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002219 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002220 return cast<ParenExpr>(this)->getSubExpr()
2221 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002222 case GenericSelectionExprClass:
2223 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2224 return false;
2225 return cast<GenericSelectionExpr>(this)->getResultExpr()
2226 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002227 case ChooseExprClass:
2228 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2229 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002230 case UnaryOperatorClass: {
2231 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002232 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002233 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002234 break;
2235 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00002236 case BinaryOperatorClass: {
2237 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2238 // but this handles the common case.
2239 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002240 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3eb172a2009-10-13 07:14:16 +00002241 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2242 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2243 return true;
2244 break;
2245 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002246 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002247 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002248 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00002249 case CStyleCastExprClass:
2250 // Handle casts with a destination that's a struct or union; this
2251 // deals with both the gcc no-op struct cast extension and the
2252 // cast-to-union extension.
2253 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002254 return cast<CastExpr>(this)->getSubExpr()
2255 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002256
Chris Lattnera2f9bd52009-10-13 22:12:09 +00002257 // Integer->integer casts can be handled here, which is important for
2258 // things like (int)(&&x-&&y). Scary but true.
2259 if (getType()->isIntegerType() &&
2260 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002261 return cast<CastExpr>(this)->getSubExpr()
2262 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002263
Eli Friedman384da272009-01-25 03:12:18 +00002264 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002265 }
Eli Friedman384da272009-01-25 03:12:18 +00002266 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002267}
2268
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002269/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2270/// pointer constant or not, as well as the specific kind of constant detected.
2271/// Null pointer constants can be integer constant expressions with the
2272/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2273/// (a GNU extension).
2274Expr::NullPointerConstantKind
2275Expr::isNullPointerConstant(ASTContext &Ctx,
2276 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00002277 if (isValueDependent()) {
2278 switch (NPC) {
2279 case NPC_NeverValueDependent:
2280 assert(false && "Unexpected value dependent expression!");
2281 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002282
Douglas Gregor56751b52009-09-25 04:25:58 +00002283 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002284 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2285 return NPCK_ZeroInteger;
2286 else
2287 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00002288
Douglas Gregor56751b52009-09-25 04:25:58 +00002289 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002290 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00002291 }
2292 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002293
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002294 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002295 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00002296 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002297 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002298 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002299 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002300 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002301 Pointee->isVoidType() && // to void*
2302 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002303 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002304 }
Steve Naroffada7d422007-05-20 17:54:12 +00002305 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002306 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2307 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002308 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002309 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2310 // Accept ((void*)0) as a null pointer constant, as many other
2311 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002312 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00002313 } else if (const GenericSelectionExpr *GE =
2314 dyn_cast<GenericSelectionExpr>(this)) {
2315 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002316 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002317 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002318 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002319 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002320 } else if (isa<GNUNullExpr>(this)) {
2321 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002322 return NPCK_GNUNull;
Steve Naroff09035312008-01-14 02:53:34 +00002323 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002324
Sebastian Redl576fd422009-05-10 18:38:11 +00002325 // C++0x nullptr_t is always a null pointer constant.
2326 if (getType()->isNullPtrType())
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002327 return NPCK_CXX0X_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00002328
Fariborz Jahanian3567c422010-09-27 22:42:37 +00002329 if (const RecordType *UT = getType()->getAsUnionType())
2330 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2331 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2332 const Expr *InitExpr = CLE->getInitializer();
2333 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2334 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2335 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002336 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002337 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002338 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002339 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00002340
Chris Lattner1abbd412007-06-08 17:58:43 +00002341 // If we have an integer constant expression, we need to *evaluate* it and
2342 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002343 llvm::APSInt Result;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002344 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2345
2346 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002347}
Steve Narofff7a5da12007-07-28 23:10:27 +00002348
John McCall34376a62010-12-04 03:47:34 +00002349/// \brief If this expression is an l-value for an Objective C
2350/// property, find the underlying property reference expression.
2351const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2352 const Expr *E = this;
2353 while (true) {
2354 assert((E->getValueKind() == VK_LValue &&
2355 E->getObjectKind() == OK_ObjCProperty) &&
2356 "expression is not a property reference");
2357 E = E->IgnoreParenCasts();
2358 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2359 if (BO->getOpcode() == BO_Comma) {
2360 E = BO->getRHS();
2361 continue;
2362 }
2363 }
2364
2365 break;
2366 }
2367
2368 return cast<ObjCPropertyRefExpr>(E);
2369}
2370
Douglas Gregor71235ec2009-05-02 02:18:30 +00002371FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002372 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002373
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002374 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002375 if (ICE->getCastKind() == CK_LValueToRValue ||
2376 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002377 E = ICE->getSubExpr()->IgnoreParens();
2378 else
2379 break;
2380 }
2381
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002382 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002383 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002384 if (Field->isBitField())
2385 return Field;
2386
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00002387 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2388 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2389 if (Field->isBitField())
2390 return Field;
2391
Douglas Gregor71235ec2009-05-02 02:18:30 +00002392 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2393 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2394 return BinOp->getLHS()->getBitField();
2395
2396 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002397}
2398
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002399bool Expr::refersToVectorElement() const {
2400 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002401
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002402 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002403 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002404 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002405 E = ICE->getSubExpr()->IgnoreParens();
2406 else
2407 break;
2408 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002409
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002410 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2411 return ASE->getBase()->getType()->isVectorType();
2412
2413 if (isa<ExtVectorElementExpr>(E))
2414 return true;
2415
2416 return false;
2417}
2418
Chris Lattnerb8211f62009-02-16 22:14:05 +00002419/// isArrow - Return true if the base expression is a pointer to vector,
2420/// return false if the base expression is a vector.
2421bool ExtVectorElementExpr::isArrow() const {
2422 return getBase()->getType()->isPointerType();
2423}
2424
Nate Begemance4d7fc2008-04-18 23:10:10 +00002425unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002426 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002427 return VT->getNumElements();
2428 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002429}
2430
Nate Begemanf322eab2008-05-09 06:41:27 +00002431/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002432bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002433 // FIXME: Refactor this code to an accessor on the AST node which returns the
2434 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002435 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002436
2437 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002438 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002439 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002440
Nate Begeman7e5185b2009-01-18 02:01:21 +00002441 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002442 if (Comp[0] == 's' || Comp[0] == 'S')
2443 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002444
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002445 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2446 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002447 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002448
Steve Naroff0d595ca2007-07-30 03:29:09 +00002449 return false;
2450}
Chris Lattner885b4952007-08-02 23:36:59 +00002451
Nate Begemanf322eab2008-05-09 06:41:27 +00002452/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002453void ExtVectorElementExpr::getEncodedElementAccess(
2454 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002455 llvm::StringRef Comp = Accessor->getName();
2456 if (Comp[0] == 's' || Comp[0] == 'S')
2457 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002458
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002459 bool isHi = Comp == "hi";
2460 bool isLo = Comp == "lo";
2461 bool isEven = Comp == "even";
2462 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002463
Nate Begemanf322eab2008-05-09 06:41:27 +00002464 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2465 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002466
Nate Begemanf322eab2008-05-09 06:41:27 +00002467 if (isHi)
2468 Index = e + i;
2469 else if (isLo)
2470 Index = i;
2471 else if (isEven)
2472 Index = 2 * i;
2473 else if (isOdd)
2474 Index = 2 * i + 1;
2475 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002476 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002477
Nate Begemand3862152008-05-13 21:03:02 +00002478 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002479 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002480}
2481
Douglas Gregor9a129192010-04-21 00:45:42 +00002482ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002483 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002484 SourceLocation LBracLoc,
2485 SourceLocation SuperLoc,
2486 bool IsInstanceSuper,
2487 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002488 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002489 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002490 ObjCMethodDecl *Method,
2491 Expr **Args, unsigned NumArgs,
2492 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002493 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002494 /*TypeDependent=*/false, /*ValueDependent=*/false,
2495 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002496 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2497 HasMethod(Method != 0), SuperLoc(SuperLoc),
2498 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2499 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002500 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002501{
Douglas Gregor9a129192010-04-21 00:45:42 +00002502 setReceiverPointer(SuperType.getAsOpaquePtr());
2503 if (NumArgs)
2504 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002505}
2506
Douglas Gregor9a129192010-04-21 00:45:42 +00002507ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002508 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002509 SourceLocation LBracLoc,
2510 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002511 Selector Sel,
2512 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002513 ObjCMethodDecl *Method,
2514 Expr **Args, unsigned NumArgs,
2515 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002516 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002517 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002518 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2519 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2520 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002521 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002522{
2523 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002524 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002525 for (unsigned I = 0; I != NumArgs; ++I) {
2526 if (Args[I]->isTypeDependent())
2527 ExprBits.TypeDependent = true;
2528 if (Args[I]->isValueDependent())
2529 ExprBits.ValueDependent = true;
2530 if (Args[I]->containsUnexpandedParameterPack())
2531 ExprBits.ContainsUnexpandedParameterPack = true;
2532
2533 MyArgs[I] = Args[I];
2534 }
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002535}
2536
Douglas Gregor9a129192010-04-21 00:45:42 +00002537ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002538 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002539 SourceLocation LBracLoc,
2540 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002541 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002542 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002543 ObjCMethodDecl *Method,
2544 Expr **Args, unsigned NumArgs,
2545 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002546 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002547 Receiver->isTypeDependent(),
2548 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002549 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2550 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2551 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002552 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002553{
2554 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002555 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002556 for (unsigned I = 0; I != NumArgs; ++I) {
2557 if (Args[I]->isTypeDependent())
2558 ExprBits.TypeDependent = true;
2559 if (Args[I]->isValueDependent())
2560 ExprBits.ValueDependent = true;
2561 if (Args[I]->containsUnexpandedParameterPack())
2562 ExprBits.ContainsUnexpandedParameterPack = true;
2563
2564 MyArgs[I] = Args[I];
2565 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00002566}
2567
Douglas Gregor9a129192010-04-21 00:45:42 +00002568ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002569 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002570 SourceLocation LBracLoc,
2571 SourceLocation SuperLoc,
2572 bool IsInstanceSuper,
2573 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002574 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002575 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002576 ObjCMethodDecl *Method,
2577 Expr **Args, unsigned NumArgs,
2578 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002579 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002580 NumArgs * sizeof(Expr *);
2581 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCall7decc9e2010-11-18 06:31:45 +00002582 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002583 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002584 RBracLoc);
2585}
2586
2587ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002588 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002589 SourceLocation LBracLoc,
2590 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002591 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002592 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002593 ObjCMethodDecl *Method,
2594 Expr **Args, unsigned NumArgs,
2595 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002596 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002597 NumArgs * sizeof(Expr *);
2598 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002599 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2600 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002601}
2602
2603ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002604 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002605 SourceLocation LBracLoc,
2606 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002607 Selector Sel,
2608 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002609 ObjCMethodDecl *Method,
2610 Expr **Args, unsigned NumArgs,
2611 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002612 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002613 NumArgs * sizeof(Expr *);
2614 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002615 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2616 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002617}
2618
Alexis Hunta8136cc2010-05-05 15:23:54 +00002619ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002620 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002621 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002622 NumArgs * sizeof(Expr *);
2623 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2624 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2625}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00002626
2627SourceRange ObjCMessageExpr::getReceiverRange() const {
2628 switch (getReceiverKind()) {
2629 case Instance:
2630 return getInstanceReceiver()->getSourceRange();
2631
2632 case Class:
2633 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2634
2635 case SuperInstance:
2636 case SuperClass:
2637 return getSuperLoc();
2638 }
2639
2640 return SourceLocation();
2641}
2642
Douglas Gregor9a129192010-04-21 00:45:42 +00002643Selector ObjCMessageExpr::getSelector() const {
2644 if (HasMethod)
2645 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2646 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002647 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002648}
2649
2650ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2651 switch (getReceiverKind()) {
2652 case Instance:
2653 if (const ObjCObjectPointerType *Ptr
2654 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2655 return Ptr->getInterfaceDecl();
2656 break;
2657
2658 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00002659 if (const ObjCObjectType *Ty
2660 = getClassReceiver()->getAs<ObjCObjectType>())
2661 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002662 break;
2663
2664 case SuperInstance:
2665 if (const ObjCObjectPointerType *Ptr
2666 = getSuperType()->getAs<ObjCObjectPointerType>())
2667 return Ptr->getInterfaceDecl();
2668 break;
2669
2670 case SuperClass:
Argyrios Kyrtzidis1b9747f2011-01-25 00:03:48 +00002671 if (const ObjCObjectType *Iface
2672 = getSuperType()->getAs<ObjCObjectType>())
2673 return Iface->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002674 break;
2675 }
2676
2677 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002678}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002679
Jay Foad39c79802011-01-12 09:06:06 +00002680bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002681 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002682}
2683
Douglas Gregora6e053e2010-12-15 01:34:56 +00002684ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2685 QualType Type, SourceLocation BLoc,
2686 SourceLocation RP)
2687 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2688 Type->isDependentType(), Type->isDependentType(),
2689 Type->containsUnexpandedParameterPack()),
2690 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2691{
2692 SubExprs = new (C) Stmt*[nexpr];
2693 for (unsigned i = 0; i < nexpr; i++) {
2694 if (args[i]->isTypeDependent())
2695 ExprBits.TypeDependent = true;
2696 if (args[i]->isValueDependent())
2697 ExprBits.ValueDependent = true;
2698 if (args[i]->containsUnexpandedParameterPack())
2699 ExprBits.ContainsUnexpandedParameterPack = true;
2700
2701 SubExprs[i] = args[i];
2702 }
2703}
2704
Nate Begeman48745922009-08-12 02:28:50 +00002705void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2706 unsigned NumExprs) {
2707 if (SubExprs) C.Deallocate(SubExprs);
2708
2709 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002710 this->NumExprs = NumExprs;
2711 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002712}
Nate Begeman48745922009-08-12 02:28:50 +00002713
Peter Collingbourne91147592011-04-15 00:35:48 +00002714GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2715 SourceLocation GenericLoc, Expr *ControllingExpr,
2716 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2717 unsigned NumAssocs, SourceLocation DefaultLoc,
2718 SourceLocation RParenLoc,
2719 bool ContainsUnexpandedParameterPack,
2720 unsigned ResultIndex)
2721 : Expr(GenericSelectionExprClass,
2722 AssocExprs[ResultIndex]->getType(),
2723 AssocExprs[ResultIndex]->getValueKind(),
2724 AssocExprs[ResultIndex]->getObjectKind(),
2725 AssocExprs[ResultIndex]->isTypeDependent(),
2726 AssocExprs[ResultIndex]->isValueDependent(),
2727 ContainsUnexpandedParameterPack),
2728 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2729 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2730 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2731 RParenLoc(RParenLoc) {
2732 SubExprs[CONTROLLING] = ControllingExpr;
2733 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2734 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2735}
2736
2737GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2738 SourceLocation GenericLoc, Expr *ControllingExpr,
2739 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2740 unsigned NumAssocs, SourceLocation DefaultLoc,
2741 SourceLocation RParenLoc,
2742 bool ContainsUnexpandedParameterPack)
2743 : Expr(GenericSelectionExprClass,
2744 Context.DependentTy,
2745 VK_RValue,
2746 OK_Ordinary,
2747 /*isTypeDependent=*/ true,
2748 /*isValueDependent=*/ true,
2749 ContainsUnexpandedParameterPack),
2750 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2751 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2752 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2753 RParenLoc(RParenLoc) {
2754 SubExprs[CONTROLLING] = ControllingExpr;
2755 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2756 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2757}
2758
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002759//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002760// DesignatedInitExpr
2761//===----------------------------------------------------------------------===//
2762
2763IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2764 assert(Kind == FieldDesignator && "Only valid on a field designator");
2765 if (Field.NameOrField & 0x01)
2766 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2767 else
2768 return getField()->getIdentifier();
2769}
2770
Alexis Hunta8136cc2010-05-05 15:23:54 +00002771DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002772 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002773 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002774 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002775 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002776 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002777 unsigned NumIndexExprs,
2778 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002779 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00002780 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002781 Init->isTypeDependent(), Init->isValueDependent(),
2782 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00002783 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2784 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002785 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002786
2787 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00002788 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002789 *Child++ = Init;
2790
2791 // Copy the designators and their subexpressions, computing
2792 // value-dependence along the way.
2793 unsigned IndexIdx = 0;
2794 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002795 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002796
2797 if (this->Designators[I].isArrayDesignator()) {
2798 // Compute type- and value-dependence.
2799 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002800 if (Index->isTypeDependent() || Index->isValueDependent())
2801 ExprBits.ValueDependent = true;
2802
2803 // Propagate unexpanded parameter packs.
2804 if (Index->containsUnexpandedParameterPack())
2805 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002806
2807 // Copy the index expressions into permanent storage.
2808 *Child++ = IndexExprs[IndexIdx++];
2809 } else if (this->Designators[I].isArrayRangeDesignator()) {
2810 // Compute type- and value-dependence.
2811 Expr *Start = IndexExprs[IndexIdx];
2812 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002813 if (Start->isTypeDependent() || Start->isValueDependent() ||
2814 End->isTypeDependent() || End->isValueDependent())
2815 ExprBits.ValueDependent = true;
2816
2817 // Propagate unexpanded parameter packs.
2818 if (Start->containsUnexpandedParameterPack() ||
2819 End->containsUnexpandedParameterPack())
2820 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002821
2822 // Copy the start/end expressions into permanent storage.
2823 *Child++ = IndexExprs[IndexIdx++];
2824 *Child++ = IndexExprs[IndexIdx++];
2825 }
2826 }
2827
2828 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002829}
2830
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002831DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002832DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002833 unsigned NumDesignators,
2834 Expr **IndexExprs, unsigned NumIndexExprs,
2835 SourceLocation ColonOrEqualLoc,
2836 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002837 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002838 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002839 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002840 ColonOrEqualLoc, UsesColonSyntax,
2841 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002842}
2843
Mike Stump11289f42009-09-09 15:08:12 +00002844DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002845 unsigned NumIndexExprs) {
2846 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2847 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2848 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2849}
2850
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002851void DesignatedInitExpr::setDesignators(ASTContext &C,
2852 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002853 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002854 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002855 NumDesignators = NumDesigs;
2856 for (unsigned I = 0; I != NumDesigs; ++I)
2857 Designators[I] = Desigs[I];
2858}
2859
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00002860SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
2861 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
2862 if (size() == 1)
2863 return DIE->getDesignator(0)->getSourceRange();
2864 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
2865 DIE->getDesignator(size()-1)->getEndLocation());
2866}
2867
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002868SourceRange DesignatedInitExpr::getSourceRange() const {
2869 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002870 Designator &First =
2871 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002872 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002873 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002874 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2875 else
2876 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2877 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002878 StartLoc =
2879 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002880 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2881}
2882
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002883Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2884 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2885 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2886 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002887 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2888 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2889}
2890
2891Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002892 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002893 "Requires array range designator");
2894 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2895 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002896 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2897 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2898}
2899
2900Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002901 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002902 "Requires array range designator");
2903 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2904 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002905 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2906 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2907}
2908
Douglas Gregord5846a12009-04-15 06:41:24 +00002909/// \brief Replaces the designator at index @p Idx with the series
2910/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002911void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002912 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002913 const Designator *Last) {
2914 unsigned NumNewDesignators = Last - First;
2915 if (NumNewDesignators == 0) {
2916 std::copy_backward(Designators + Idx + 1,
2917 Designators + NumDesignators,
2918 Designators + Idx);
2919 --NumNewDesignators;
2920 return;
2921 } else if (NumNewDesignators == 1) {
2922 Designators[Idx] = *First;
2923 return;
2924 }
2925
Mike Stump11289f42009-09-09 15:08:12 +00002926 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002927 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002928 std::copy(Designators, Designators + Idx, NewDesignators);
2929 std::copy(First, Last, NewDesignators + Idx);
2930 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2931 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00002932 Designators = NewDesignators;
2933 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2934}
2935
Mike Stump11289f42009-09-09 15:08:12 +00002936ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002937 Expr **exprs, unsigned nexprs,
2938 SourceLocation rparenloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00002939 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2940 false, false, false),
2941 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002942
Nate Begeman5ec4b312009-08-10 23:49:36 +00002943 Exprs = new (C) Stmt*[nexprs];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002944 for (unsigned i = 0; i != nexprs; ++i) {
2945 if (exprs[i]->isTypeDependent())
2946 ExprBits.TypeDependent = true;
2947 if (exprs[i]->isValueDependent())
2948 ExprBits.ValueDependent = true;
2949 if (exprs[i]->containsUnexpandedParameterPack())
2950 ExprBits.ContainsUnexpandedParameterPack = true;
2951
Nate Begeman5ec4b312009-08-10 23:49:36 +00002952 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002953 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00002954}
2955
John McCall1bf58462011-02-16 08:02:54 +00002956const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
2957 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
2958 e = ewc->getSubExpr();
2959 e = cast<CXXConstructExpr>(e)->getArg(0);
2960 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
2961 e = ice->getSubExpr();
2962 return cast<OpaqueValueExpr>(e);
2963}
2964
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002965//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002966// ExprIterator.
2967//===----------------------------------------------------------------------===//
2968
2969Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2970Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2971Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2972const Expr* ConstExprIterator::operator[](size_t idx) const {
2973 return cast<Expr>(I[idx]);
2974}
2975const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2976const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2977
2978//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002979// Child Iterators for iterating over subexpressions/substatements
2980//===----------------------------------------------------------------------===//
2981
Peter Collingbournee190dee2011-03-11 19:24:49 +00002982// UnaryExprOrTypeTraitExpr
2983Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002984 // If this is of a type and the type is a VLA type (and not a typedef), the
2985 // size expression of the VLA needs to be treated as an executable expression.
2986 // Why isn't this weirdness documented better in StmtIterator?
2987 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002988 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00002989 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00002990 return child_range(child_iterator(T), child_iterator());
2991 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00002992 }
John McCallbd066782011-02-09 08:16:59 +00002993 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002994}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002995
Steve Naroffd54978b2007-09-18 23:55:05 +00002996// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00002997Stmt::child_range ObjCMessageExpr::children() {
2998 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00002999 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003000 begin = reinterpret_cast<Stmt **>(this + 1);
3001 else
3002 begin = reinterpret_cast<Stmt **>(getArgs());
3003 return child_range(begin,
3004 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003005}
3006
Steve Naroffc540d662008-09-03 18:15:37 +00003007// Blocks
John McCall351762c2011-02-07 10:33:21 +00003008BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregor476e3022011-01-19 21:32:01 +00003009 SourceLocation l, bool ByRef,
John McCall351762c2011-02-07 10:33:21 +00003010 bool constAdded)
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003011 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregor476e3022011-01-19 21:32:01 +00003012 d->isParameterPack()),
John McCall351762c2011-02-07 10:33:21 +00003013 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregor476e3022011-01-19 21:32:01 +00003014{
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003015 bool TypeDependent = false;
3016 bool ValueDependent = false;
3017 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
3018 ExprBits.TypeDependent = TypeDependent;
3019 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor476e3022011-01-19 21:32:01 +00003020}
3021