blob: 7f15d86a19f4fc2243a45d56cef1d60ebe134e38 [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
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000277DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000278 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000279 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000280 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000281 QualType T, ExprValueKind VK)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000282 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000283 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
284 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000285 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000286 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000287 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
288 if (FoundD)
289 getInternalFoundDecl() = FoundD;
Chandler Carruth0e439962011-05-01 21:29:53 +0000290 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000291 if (TemplateArgs)
John McCallb3774b52010-08-19 23:49:38 +0000292 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000293
294 computeDependence();
295}
296
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000297DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000298 NestedNameSpecifierLoc QualifierLoc,
John McCallce546572009-12-08 09:08:17 +0000299 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000300 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000301 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000302 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000303 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000304 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000305 return Create(Context, QualifierLoc, D,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000306 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000307 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000308}
309
310DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000311 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000312 ValueDecl *D,
313 const DeclarationNameInfo &NameInfo,
314 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000315 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000316 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000317 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000318 // Filter out cases where the found Decl is the same as the value refenenced.
319 if (D == FoundD)
320 FoundD = 0;
321
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000322 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregorea972d32011-02-28 21:54:11 +0000323 if (QualifierLoc != 0)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000324 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000325 if (FoundD)
326 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000327 if (TemplateArgs)
328 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000329
Chris Lattner5c0b4052010-10-30 05:14:06 +0000330 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000331 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
332 T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000333}
334
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000335DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000336 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000337 bool HasFoundDecl,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000338 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000339 unsigned NumTemplateArgs) {
340 std::size_t Size = sizeof(DeclRefExpr);
341 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000342 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000343 if (HasFoundDecl)
344 Size += sizeof(NamedDecl *);
Douglas Gregor87866ce2011-02-04 12:01:24 +0000345 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000346 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000347
Chris Lattner5c0b4052010-10-30 05:14:06 +0000348 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000349 return new (Mem) DeclRefExpr(EmptyShell());
350}
351
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000352SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000353 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000354 if (hasQualifier())
Douglas Gregorea972d32011-02-28 21:54:11 +0000355 R.setBegin(getQualifierLoc().getBeginLoc());
John McCallb3774b52010-08-19 23:49:38 +0000356 if (hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000357 R.setEnd(getRAngleLoc());
358 return R;
359}
360
Anders Carlsson2fb08242009-09-08 18:24:21 +0000361// FIXME: Maybe this should use DeclPrinter with a special "print predefined
362// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000363std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
364 ASTContext &Context = CurrentDecl->getASTContext();
365
Anders Carlsson2fb08242009-09-08 18:24:21 +0000366 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000367 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000368 return FD->getNameAsString();
369
370 llvm::SmallString<256> Name;
371 llvm::raw_svector_ostream Out(Name);
372
373 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000374 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000375 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000376 if (MD->isStatic())
377 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000378 }
379
380 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000381
382 std::string Proto = FD->getQualifiedNameAsString(Policy);
383
John McCall9dd450b2009-09-21 23:43:11 +0000384 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000385 const FunctionProtoType *FT = 0;
386 if (FD->hasWrittenPrototype())
387 FT = dyn_cast<FunctionProtoType>(AFT);
388
389 Proto += "(";
390 if (FT) {
391 llvm::raw_string_ostream POut(Proto);
392 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
393 if (i) POut << ", ";
394 std::string Param;
395 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
396 POut << Param;
397 }
398
399 if (FT->isVariadic()) {
400 if (FD->getNumParams()) POut << ", ";
401 POut << "...";
402 }
403 }
404 Proto += ")";
405
Sam Weinig4e83bd22009-12-27 01:38:20 +0000406 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
407 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
408 if (ThisQuals.hasConst())
409 Proto += " const";
410 if (ThisQuals.hasVolatile())
411 Proto += " volatile";
412 }
413
Sam Weinigd060ed42009-12-06 23:55:13 +0000414 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
415 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000416
417 Out << Proto;
418
419 Out.flush();
420 return Name.str().str();
421 }
422 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
423 llvm::SmallString<256> Name;
424 llvm::raw_svector_ostream Out(Name);
425 Out << (MD->isInstanceMethod() ? '-' : '+');
426 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000427
428 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
429 // a null check to avoid a crash.
430 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000431 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000432
Anders Carlsson2fb08242009-09-08 18:24:21 +0000433 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000434 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
435 Out << '(' << CID << ')';
436
Anders Carlsson2fb08242009-09-08 18:24:21 +0000437 Out << ' ';
438 Out << MD->getSelector().getAsString();
439 Out << ']';
440
441 Out.flush();
442 return Name.str().str();
443 }
444 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
445 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
446 return "top level";
447 }
448 return "";
449}
450
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000451void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
452 if (hasAllocation())
453 C.Deallocate(pVal);
454
455 BitWidth = Val.getBitWidth();
456 unsigned NumWords = Val.getNumWords();
457 const uint64_t* Words = Val.getRawData();
458 if (NumWords > 1) {
459 pVal = new (C) uint64_t[NumWords];
460 std::copy(Words, Words + NumWords, pVal);
461 } else if (NumWords == 1)
462 VAL = Words[0];
463 else
464 VAL = 0;
465}
466
467IntegerLiteral *
468IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
469 QualType type, SourceLocation l) {
470 return new (C) IntegerLiteral(C, V, type, l);
471}
472
473IntegerLiteral *
474IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
475 return new (C) IntegerLiteral(Empty);
476}
477
478FloatingLiteral *
479FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
480 bool isexact, QualType Type, SourceLocation L) {
481 return new (C) FloatingLiteral(C, V, isexact, Type, L);
482}
483
484FloatingLiteral *
485FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
486 return new (C) FloatingLiteral(Empty);
487}
488
Chris Lattnera0173132008-06-07 22:13:43 +0000489/// getValueAsApproximateDouble - This returns the value as an inaccurate
490/// double. Note that this may cause loss of precision, but is useful for
491/// debugging dumps, etc.
492double FloatingLiteral::getValueAsApproximateDouble() const {
493 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000494 bool ignored;
495 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
496 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000497 return V.convertToDouble();
498}
499
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000500StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
501 unsigned ByteLength, bool Wide,
Anders Carlsson75245402011-04-14 00:40:03 +0000502 bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000503 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000504 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000505 // Allocate enough space for the StringLiteral plus an array of locations for
506 // any concatenated string tokens.
507 void *Mem = C.Allocate(sizeof(StringLiteral)+
508 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000509 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000510 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000511
Steve Naroffdf7855b2007-02-21 23:46:25 +0000512 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000513 char *AStrData = new (C, 1) char[ByteLength];
514 memcpy(AStrData, StrData, ByteLength);
515 SL->StrData = AStrData;
516 SL->ByteLength = ByteLength;
517 SL->IsWide = Wide;
Anders Carlsson75245402011-04-14 00:40:03 +0000518 SL->IsPascal = Pascal;
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000519 SL->TokLocs[0] = Loc[0];
520 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000521
Chris Lattner630970d2009-02-18 05:49:11 +0000522 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000523 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
524 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000525}
526
Douglas Gregor958dfc92009-04-15 16:35:07 +0000527StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
528 void *Mem = C.Allocate(sizeof(StringLiteral)+
529 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000530 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000531 StringLiteral *SL = new (Mem) StringLiteral(QualType());
532 SL->StrData = 0;
533 SL->ByteLength = 0;
534 SL->NumConcatenated = NumStrs;
535 return SL;
536}
537
Daniel Dunbar36217882009-09-22 03:27:33 +0000538void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbar36217882009-09-22 03:27:33 +0000539 char *AStrData = new (C, 1) char[Str.size()];
540 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000541 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000542 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000543}
544
Chris Lattnere925d612010-11-17 07:37:15 +0000545/// getLocationOfByte - Return a source location that points to the specified
546/// byte of this string literal.
547///
548/// Strings are amazingly complex. They can be formed from multiple tokens and
549/// can have escape sequences in them in addition to the usual trigraph and
550/// escaped newline business. This routine handles this complexity.
551///
552SourceLocation StringLiteral::
553getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
554 const LangOptions &Features, const TargetInfo &Target) const {
555 assert(!isWide() && "This doesn't work for wide strings yet");
556
557 // Loop over all of the tokens in this string until we find the one that
558 // contains the byte we're looking for.
559 unsigned TokNo = 0;
560 while (1) {
561 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
562 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
563
564 // Get the spelling of the string so that we can get the data that makes up
565 // the string literal, not the identifier for the macro it is potentially
566 // expanded through.
567 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
568
569 // Re-lex the token to get its length and original spelling.
570 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
571 bool Invalid = false;
572 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
573 if (Invalid)
574 return StrTokSpellingLoc;
575
576 const char *StrData = Buffer.data()+LocInfo.second;
577
578 // Create a langops struct and enable trigraphs. This is sufficient for
579 // relexing tokens.
580 LangOptions LangOpts;
581 LangOpts.Trigraphs = true;
582
583 // Create a lexer starting at the beginning of this token.
584 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
585 Buffer.end());
586 Token TheTok;
587 TheLexer.LexFromRawLexer(TheTok);
588
589 // Use the StringLiteralParser to compute the length of the string in bytes.
590 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
591 unsigned TokNumBytes = SLP.GetStringLength();
592
593 // If the byte is in this token, return the location of the byte.
594 if (ByteNo < TokNumBytes ||
595 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
596 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
597
598 // Now that we know the offset of the token in the spelling, use the
599 // preprocessor to get the offset in the original source.
600 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
601 }
602
603 // Move to the next string token.
604 ++TokNo;
605 ByteNo -= TokNumBytes;
606 }
607}
608
609
610
Chris Lattner1b926492006-08-23 06:42:10 +0000611/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
612/// corresponds to, e.g. "sizeof" or "[pre]++".
613const char *UnaryOperator::getOpcodeStr(Opcode Op) {
614 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000615 default: assert(0 && "Unknown unary operator");
John McCalle3027922010-08-25 11:45:40 +0000616 case UO_PostInc: return "++";
617 case UO_PostDec: return "--";
618 case UO_PreInc: return "++";
619 case UO_PreDec: return "--";
620 case UO_AddrOf: return "&";
621 case UO_Deref: return "*";
622 case UO_Plus: return "+";
623 case UO_Minus: return "-";
624 case UO_Not: return "~";
625 case UO_LNot: return "!";
626 case UO_Real: return "__real";
627 case UO_Imag: return "__imag";
628 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000629 }
630}
631
John McCalle3027922010-08-25 11:45:40 +0000632UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000633UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
634 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000635 default: assert(false && "No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000636 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
637 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
638 case OO_Amp: return UO_AddrOf;
639 case OO_Star: return UO_Deref;
640 case OO_Plus: return UO_Plus;
641 case OO_Minus: return UO_Minus;
642 case OO_Tilde: return UO_Not;
643 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000644 }
645}
646
647OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
648 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000649 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
650 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
651 case UO_AddrOf: return OO_Amp;
652 case UO_Deref: return OO_Star;
653 case UO_Plus: return OO_Plus;
654 case UO_Minus: return OO_Minus;
655 case UO_Not: return OO_Tilde;
656 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000657 default: return OO_None;
658 }
659}
660
661
Chris Lattner0eedafe2006-08-24 04:56:27 +0000662//===----------------------------------------------------------------------===//
663// Postfix Operators.
664//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000665
Peter Collingbourne3a347252011-02-08 21:18:02 +0000666CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
667 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCall7decc9e2010-11-18 06:31:45 +0000668 SourceLocation rparenloc)
669 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000670 fn->isTypeDependent(),
671 fn->isValueDependent(),
672 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000673 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000674
Peter Collingbourne3a347252011-02-08 21:18:02 +0000675 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +0000676 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000677 for (unsigned i = 0; i != numargs; ++i) {
678 if (args[i]->isTypeDependent())
679 ExprBits.TypeDependent = true;
680 if (args[i]->isValueDependent())
681 ExprBits.ValueDependent = true;
682 if (args[i]->containsUnexpandedParameterPack())
683 ExprBits.ContainsUnexpandedParameterPack = true;
684
Peter Collingbourne3a347252011-02-08 21:18:02 +0000685 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000686 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000687
Peter Collingbourne3a347252011-02-08 21:18:02 +0000688 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +0000689 RParenLoc = rparenloc;
690}
Nate Begeman1e36a852008-01-17 17:46:27 +0000691
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000692CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCall7decc9e2010-11-18 06:31:45 +0000693 QualType t, ExprValueKind VK, SourceLocation rparenloc)
694 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000695 fn->isTypeDependent(),
696 fn->isValueDependent(),
697 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000698 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000699
Peter Collingbourne3a347252011-02-08 21:18:02 +0000700 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000701 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000702 for (unsigned i = 0; i != numargs; ++i) {
703 if (args[i]->isTypeDependent())
704 ExprBits.TypeDependent = true;
705 if (args[i]->isValueDependent())
706 ExprBits.ValueDependent = true;
707 if (args[i]->containsUnexpandedParameterPack())
708 ExprBits.ContainsUnexpandedParameterPack = true;
709
Peter Collingbourne3a347252011-02-08 21:18:02 +0000710 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000711 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000712
Peter Collingbourne3a347252011-02-08 21:18:02 +0000713 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000714 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000715}
716
Mike Stump11289f42009-09-09 15:08:12 +0000717CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
718 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000719 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +0000720 SubExprs = new (C) Stmt*[PREARGS_START];
721 CallExprBits.NumPreArgs = 0;
722}
723
724CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
725 EmptyShell Empty)
726 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
727 // FIXME: Why do we allocate this?
728 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
729 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +0000730}
731
Nuno Lopes518e3702009-12-20 23:11:08 +0000732Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000733 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl2b1832e2010-09-10 20:55:30 +0000734 // If we're calling a dereference, look at the pointer instead.
735 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
736 if (BO->isPtrMemOp())
737 CEE = BO->getRHS()->IgnoreParenCasts();
738 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
739 if (UO->getOpcode() == UO_Deref)
740 CEE = UO->getSubExpr()->IgnoreParenCasts();
741 }
Chris Lattner52301912009-07-17 15:46:27 +0000742 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000743 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000744 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
745 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000746
747 return 0;
748}
749
Nuno Lopes518e3702009-12-20 23:11:08 +0000750FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000751 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000752}
753
Chris Lattnere4407ed2007-12-28 05:25:02 +0000754/// setNumArgs - This changes the number of arguments present in this call.
755/// Any orphaned expressions are deleted by this, and any new operands are set
756/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000757void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000758 // No change, just return.
759 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000760
Chris Lattnere4407ed2007-12-28 05:25:02 +0000761 // If shrinking # arguments, just delete the extras and forgot them.
762 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000763 this->NumArgs = NumArgs;
764 return;
765 }
766
767 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000768 unsigned NumPreArgs = getNumPreArgs();
769 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000770 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000771 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000772 NewSubExprs[i] = SubExprs[i];
773 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000774 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
775 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000776 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000777
Douglas Gregorba6e5572009-04-17 21:46:47 +0000778 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000779 SubExprs = NewSubExprs;
780 this->NumArgs = NumArgs;
781}
782
Chris Lattner01ff98a2008-10-06 05:00:53 +0000783/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
784/// not, return 0.
Jay Foad39c79802011-01-12 09:06:06 +0000785unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000786 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000787 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000788 // ImplicitCastExpr.
789 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
790 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000791 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000792
Steve Narofff6e3b3292008-01-31 01:07:12 +0000793 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
794 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000795 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000796
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000797 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
798 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000799 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000800
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000801 if (!FDecl->getIdentifier())
802 return 0;
803
Douglas Gregor15fc9562009-09-12 00:22:50 +0000804 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000805}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000806
Anders Carlsson00a27592009-05-26 04:57:27 +0000807QualType CallExpr::getCallReturnType() const {
808 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000809 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000810 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000811 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000812 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +0000813 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
814 // This should never be overloaded and so should never return null.
815 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +0000816
John McCall0009fcc2011-04-26 20:42:42 +0000817 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000818 return FnType->getResultType();
819}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000820
John McCall701417a2011-02-21 06:23:05 +0000821SourceRange CallExpr::getSourceRange() const {
822 if (isa<CXXOperatorCallExpr>(this))
823 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
824
825 SourceLocation begin = getCallee()->getLocStart();
826 if (begin.isInvalid() && getNumArgs() > 0)
827 begin = getArg(0)->getLocStart();
828 SourceLocation end = getRParenLoc();
829 if (end.isInvalid() && getNumArgs() > 0)
830 end = getArg(getNumArgs() - 1)->getLocEnd();
831 return SourceRange(begin, end);
832}
833
Alexis Hunta8136cc2010-05-05 15:23:54 +0000834OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000835 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000836 TypeSourceInfo *tsi,
837 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000838 Expr** exprsPtr, unsigned numExprs,
839 SourceLocation RParenLoc) {
840 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000841 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000842 sizeof(Expr*) * numExprs);
843
844 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
845 exprsPtr, numExprs, RParenLoc);
846}
847
848OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
849 unsigned numComps, unsigned numExprs) {
850 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
851 sizeof(OffsetOfNode) * numComps +
852 sizeof(Expr*) * numExprs);
853 return new (Mem) OffsetOfExpr(numComps, numExprs);
854}
855
Alexis Hunta8136cc2010-05-05 15:23:54 +0000856OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000857 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000858 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000859 Expr** exprsPtr, unsigned numExprs,
860 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +0000861 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
862 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000863 /*ValueDependent=*/tsi->getType()->isDependentType(),
864 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000865 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
866 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000867{
868 for(unsigned i = 0; i < numComps; ++i) {
869 setComponent(i, compsPtr[i]);
870 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000871
Douglas Gregor882211c2010-04-28 22:16:22 +0000872 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000873 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
874 ExprBits.ValueDependent = true;
875 if (exprsPtr[i]->containsUnexpandedParameterPack())
876 ExprBits.ContainsUnexpandedParameterPack = true;
877
Douglas Gregor882211c2010-04-28 22:16:22 +0000878 setIndexExpr(i, exprsPtr[i]);
879 }
880}
881
882IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
883 assert(getKind() == Field || getKind() == Identifier);
884 if (getKind() == Field)
885 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000886
Douglas Gregor882211c2010-04-28 22:16:22 +0000887 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
888}
889
Mike Stump11289f42009-09-09 15:08:12 +0000890MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +0000891 NestedNameSpecifierLoc QualifierLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000892 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000893 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000894 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +0000895 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +0000896 QualType ty,
897 ExprValueKind vk,
898 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000899 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000900
Douglas Gregorea972d32011-02-28 21:54:11 +0000901 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +0000902 founddecl.getDecl() != memberdecl ||
903 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000904 if (hasQualOrFound)
905 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000906
John McCall6b51f282009-11-23 01:53:49 +0000907 if (targs)
908 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000909
Chris Lattner5c0b4052010-10-30 05:14:06 +0000910 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +0000911 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
912 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +0000913
914 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000915 // FIXME: Wrong. We should be looking at the member declaration we found.
916 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +0000917 E->setValueDependent(true);
918 E->setTypeDependent(true);
919 }
920 E->HasQualifierOrFoundDecl = true;
921
922 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +0000923 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +0000924 NQ->FoundDecl = founddecl;
925 }
926
927 if (targs) {
928 E->HasExplicitTemplateArgumentList = true;
John McCallb3774b52010-08-19 23:49:38 +0000929 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall16df1e52010-03-30 21:47:33 +0000930 }
931
932 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000933}
934
Douglas Gregor25b7e052011-03-02 21:06:53 +0000935SourceRange MemberExpr::getSourceRange() const {
936 SourceLocation StartLoc;
937 if (isImplicitAccess()) {
938 if (hasQualifier())
939 StartLoc = getQualifierLoc().getBeginLoc();
940 else
941 StartLoc = MemberLoc;
942 } else {
943 // FIXME: We don't want this to happen. Rather, we should be able to
944 // detect all kinds of implicit accesses more cleanly.
945 StartLoc = getBase()->getLocStart();
946 if (StartLoc.isInvalid())
947 StartLoc = MemberLoc;
948 }
949
950 SourceLocation EndLoc =
951 HasExplicitTemplateArgumentList? getRAngleLoc()
952 : getMemberNameInfo().getEndLoc();
953
954 return SourceRange(StartLoc, EndLoc);
955}
956
Anders Carlsson496335e2009-09-03 00:59:21 +0000957const char *CastExpr::getCastKindName() const {
958 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +0000959 case CK_Dependent:
960 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +0000961 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +0000962 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +0000963 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +0000964 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +0000965 case CK_LValueToRValue:
966 return "LValueToRValue";
John McCall34376a62010-12-04 03:47:34 +0000967 case CK_GetObjCProperty:
968 return "GetObjCProperty";
John McCalle3027922010-08-25 11:45:40 +0000969 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +0000970 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +0000971 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +0000972 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +0000973 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +0000974 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000975 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +0000976 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000977 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +0000978 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +0000979 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000980 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +0000981 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000982 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000983 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000984 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000985 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000986 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +0000987 case CK_NullToPointer:
988 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +0000989 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000990 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000991 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000992 return "DerivedToBaseMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000993 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000994 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +0000995 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000996 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +0000997 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000998 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +0000999 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001000 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001001 case CK_PointerToBoolean:
1002 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001003 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001004 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001005 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001006 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001007 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001008 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001009 case CK_IntegralToBoolean:
1010 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001011 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001012 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001013 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001014 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001015 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001016 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001017 case CK_FloatingToBoolean:
1018 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001019 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001020 return "MemberPointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001021 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahaniane19122f2009-12-08 23:46:15 +00001022 return "AnyPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001023 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001024 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001025 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001026 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001027 case CK_FloatingRealToComplex:
1028 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001029 case CK_FloatingComplexToReal:
1030 return "FloatingComplexToReal";
1031 case CK_FloatingComplexToBoolean:
1032 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001033 case CK_FloatingComplexCast:
1034 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001035 case CK_FloatingComplexToIntegralComplex:
1036 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001037 case CK_IntegralRealToComplex:
1038 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001039 case CK_IntegralComplexToReal:
1040 return "IntegralComplexToReal";
1041 case CK_IntegralComplexToBoolean:
1042 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001043 case CK_IntegralComplexCast:
1044 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001045 case CK_IntegralComplexToFloatingComplex:
1046 return "IntegralComplexToFloatingComplex";
Anders Carlsson496335e2009-09-03 00:59:21 +00001047 }
Mike Stump11289f42009-09-09 15:08:12 +00001048
John McCallc5e62b42010-11-13 09:02:35 +00001049 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001050 return 0;
1051}
1052
Douglas Gregord196a582009-12-14 19:27:10 +00001053Expr *CastExpr::getSubExprAsWritten() {
1054 Expr *SubExpr = 0;
1055 CastExpr *E = this;
1056 do {
1057 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001058
Douglas Gregord196a582009-12-14 19:27:10 +00001059 // Skip any temporary bindings; they're implicit.
1060 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1061 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001062
Douglas Gregord196a582009-12-14 19:27:10 +00001063 // Conversions by constructor and conversion functions have a
1064 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001065 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001066 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001067 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001068 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001069
Douglas Gregord196a582009-12-14 19:27:10 +00001070 // If the subexpression we're left with is an implicit cast, look
1071 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001072 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1073
Douglas Gregord196a582009-12-14 19:27:10 +00001074 return SubExpr;
1075}
1076
John McCallcf142162010-08-07 06:22:56 +00001077CXXBaseSpecifier **CastExpr::path_buffer() {
1078 switch (getStmtClass()) {
1079#define ABSTRACT_STMT(x)
1080#define CASTEXPR(Type, Base) \
1081 case Stmt::Type##Class: \
1082 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1083#define STMT(Type, Base)
1084#include "clang/AST/StmtNodes.inc"
1085 default:
1086 llvm_unreachable("non-cast expressions not possible here");
1087 return 0;
1088 }
1089}
1090
1091void CastExpr::setCastPath(const CXXCastPath &Path) {
1092 assert(Path.size() == path_size());
1093 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1094}
1095
1096ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1097 CastKind Kind, Expr *Operand,
1098 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001099 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001100 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1101 void *Buffer =
1102 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1103 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001104 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001105 if (PathSize) E->setCastPath(*BasePath);
1106 return E;
1107}
1108
1109ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1110 unsigned PathSize) {
1111 void *Buffer =
1112 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1113 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1114}
1115
1116
1117CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001118 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001119 const CXXCastPath *BasePath,
1120 TypeSourceInfo *WrittenTy,
1121 SourceLocation L, SourceLocation R) {
1122 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1123 void *Buffer =
1124 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1125 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001126 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001127 if (PathSize) E->setCastPath(*BasePath);
1128 return E;
1129}
1130
1131CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1132 void *Buffer =
1133 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1134 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1135}
1136
Chris Lattner1b926492006-08-23 06:42:10 +00001137/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1138/// corresponds to, e.g. "<<=".
1139const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1140 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001141 case BO_PtrMemD: return ".*";
1142 case BO_PtrMemI: return "->*";
1143 case BO_Mul: return "*";
1144 case BO_Div: return "/";
1145 case BO_Rem: return "%";
1146 case BO_Add: return "+";
1147 case BO_Sub: return "-";
1148 case BO_Shl: return "<<";
1149 case BO_Shr: return ">>";
1150 case BO_LT: return "<";
1151 case BO_GT: return ">";
1152 case BO_LE: return "<=";
1153 case BO_GE: return ">=";
1154 case BO_EQ: return "==";
1155 case BO_NE: return "!=";
1156 case BO_And: return "&";
1157 case BO_Xor: return "^";
1158 case BO_Or: return "|";
1159 case BO_LAnd: return "&&";
1160 case BO_LOr: return "||";
1161 case BO_Assign: return "=";
1162 case BO_MulAssign: return "*=";
1163 case BO_DivAssign: return "/=";
1164 case BO_RemAssign: return "%=";
1165 case BO_AddAssign: return "+=";
1166 case BO_SubAssign: return "-=";
1167 case BO_ShlAssign: return "<<=";
1168 case BO_ShrAssign: return ">>=";
1169 case BO_AndAssign: return "&=";
1170 case BO_XorAssign: return "^=";
1171 case BO_OrAssign: return "|=";
1172 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001173 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001174
1175 return "";
Chris Lattner1b926492006-08-23 06:42:10 +00001176}
Steve Naroff47500512007-04-19 23:00:49 +00001177
John McCalle3027922010-08-25 11:45:40 +00001178BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001179BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1180 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +00001181 default: assert(false && "Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001182 case OO_Plus: return BO_Add;
1183 case OO_Minus: return BO_Sub;
1184 case OO_Star: return BO_Mul;
1185 case OO_Slash: return BO_Div;
1186 case OO_Percent: return BO_Rem;
1187 case OO_Caret: return BO_Xor;
1188 case OO_Amp: return BO_And;
1189 case OO_Pipe: return BO_Or;
1190 case OO_Equal: return BO_Assign;
1191 case OO_Less: return BO_LT;
1192 case OO_Greater: return BO_GT;
1193 case OO_PlusEqual: return BO_AddAssign;
1194 case OO_MinusEqual: return BO_SubAssign;
1195 case OO_StarEqual: return BO_MulAssign;
1196 case OO_SlashEqual: return BO_DivAssign;
1197 case OO_PercentEqual: return BO_RemAssign;
1198 case OO_CaretEqual: return BO_XorAssign;
1199 case OO_AmpEqual: return BO_AndAssign;
1200 case OO_PipeEqual: return BO_OrAssign;
1201 case OO_LessLess: return BO_Shl;
1202 case OO_GreaterGreater: return BO_Shr;
1203 case OO_LessLessEqual: return BO_ShlAssign;
1204 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1205 case OO_EqualEqual: return BO_EQ;
1206 case OO_ExclaimEqual: return BO_NE;
1207 case OO_LessEqual: return BO_LE;
1208 case OO_GreaterEqual: return BO_GE;
1209 case OO_AmpAmp: return BO_LAnd;
1210 case OO_PipePipe: return BO_LOr;
1211 case OO_Comma: return BO_Comma;
1212 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001213 }
1214}
1215
1216OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1217 static const OverloadedOperatorKind OverOps[] = {
1218 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1219 OO_Star, OO_Slash, OO_Percent,
1220 OO_Plus, OO_Minus,
1221 OO_LessLess, OO_GreaterGreater,
1222 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1223 OO_EqualEqual, OO_ExclaimEqual,
1224 OO_Amp,
1225 OO_Caret,
1226 OO_Pipe,
1227 OO_AmpAmp,
1228 OO_PipePipe,
1229 OO_Equal, OO_StarEqual,
1230 OO_SlashEqual, OO_PercentEqual,
1231 OO_PlusEqual, OO_MinusEqual,
1232 OO_LessLessEqual, OO_GreaterGreaterEqual,
1233 OO_AmpEqual, OO_CaretEqual,
1234 OO_PipeEqual,
1235 OO_Comma
1236 };
1237 return OverOps[Opc];
1238}
1239
Ted Kremenekac034612010-04-13 23:39:13 +00001240InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +00001241 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001242 SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001243 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1244 false),
Ted Kremenekac034612010-04-13 23:39:13 +00001245 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +00001246 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidisb2ed28e2011-04-21 00:27:41 +00001247 HadArrayRangeDesignator(false)
Alexis Hunta8136cc2010-05-05 15:23:54 +00001248{
Ted Kremenek013041e2010-02-19 01:50:18 +00001249 for (unsigned I = 0; I != numInits; ++I) {
1250 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001251 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001252 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001253 ExprBits.ValueDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001254 if (initExprs[I]->containsUnexpandedParameterPack())
1255 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001256 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001257
Ted Kremenekac034612010-04-13 23:39:13 +00001258 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001259}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001260
Ted Kremenekac034612010-04-13 23:39:13 +00001261void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001262 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001263 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001264}
1265
Ted Kremenekac034612010-04-13 23:39:13 +00001266void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001267 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001268}
1269
Ted Kremenekac034612010-04-13 23:39:13 +00001270Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001271 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001272 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001273 InitExprs.back() = expr;
1274 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001275 }
Mike Stump11289f42009-09-09 15:08:12 +00001276
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001277 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1278 InitExprs[Init] = expr;
1279 return Result;
1280}
1281
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001282void InitListExpr::setArrayFiller(Expr *filler) {
1283 ArrayFillerOrUnionFieldInit = filler;
1284 // Fill out any "holes" in the array due to designated initializers.
1285 Expr **inits = getInits();
1286 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1287 if (inits[i] == 0)
1288 inits[i] = filler;
1289}
1290
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001291SourceRange InitListExpr::getSourceRange() const {
1292 if (SyntacticForm)
1293 return SyntacticForm->getSourceRange();
1294 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1295 if (Beg.isInvalid()) {
1296 // Find the first non-null initializer.
1297 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1298 E = InitExprs.end();
1299 I != E; ++I) {
1300 if (Stmt *S = *I) {
1301 Beg = S->getLocStart();
1302 break;
1303 }
1304 }
1305 }
1306 if (End.isInvalid()) {
1307 // Find the first non-null initializer from the end.
1308 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1309 E = InitExprs.rend();
1310 I != E; ++I) {
1311 if (Stmt *S = *I) {
1312 End = S->getSourceRange().getEnd();
1313 break;
1314 }
1315 }
1316 }
1317 return SourceRange(Beg, End);
1318}
1319
Steve Naroff991e99d2008-09-04 15:31:07 +00001320/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001321///
1322const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001323 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +00001324 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001325}
1326
Mike Stump11289f42009-09-09 15:08:12 +00001327SourceLocation BlockExpr::getCaretLocation() const {
1328 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001329}
Mike Stump11289f42009-09-09 15:08:12 +00001330const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001331 return TheBlock->getBody();
1332}
Mike Stump11289f42009-09-09 15:08:12 +00001333Stmt *BlockExpr::getBody() {
1334 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001335}
Steve Naroff415d3d52008-10-08 17:01:13 +00001336
1337
Chris Lattner1ec5f562007-06-27 05:38:08 +00001338//===----------------------------------------------------------------------===//
1339// Generic Expression Routines
1340//===----------------------------------------------------------------------===//
1341
Chris Lattner237f2752009-02-14 07:37:35 +00001342/// isUnusedResultAWarning - Return true if this immediate expression should
1343/// be warned about if the result is unused. If so, fill in Loc and Ranges
1344/// with location to warn on and the source range[s] to report with the
1345/// warning.
1346bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +00001347 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001348 // Don't warn if the expr is type dependent. The type could end up
1349 // instantiating to void.
1350 if (isTypeDependent())
1351 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001352
Chris Lattner1ec5f562007-06-27 05:38:08 +00001353 switch (getStmtClass()) {
1354 default:
John McCallc493a732010-03-12 07:11:26 +00001355 if (getType()->isVoidType())
1356 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001357 Loc = getExprLoc();
1358 R1 = getSourceRange();
1359 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001360 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001361 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +00001362 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001363 case GenericSelectionExprClass:
1364 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1365 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001366 case UnaryOperatorClass: {
1367 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001368
Chris Lattner1ec5f562007-06-27 05:38:08 +00001369 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001370 default: break;
John McCalle3027922010-08-25 11:45:40 +00001371 case UO_PostInc:
1372 case UO_PostDec:
1373 case UO_PreInc:
1374 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001375 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001376 case UO_Deref:
Chris Lattnera44d1162007-06-27 05:58:59 +00001377 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001378 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001379 return false;
1380 break;
John McCalle3027922010-08-25 11:45:40 +00001381 case UO_Real:
1382 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001383 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001384 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1385 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001386 return false;
1387 break;
John McCalle3027922010-08-25 11:45:40 +00001388 case UO_Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001389 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001390 }
Chris Lattner237f2752009-02-14 07:37:35 +00001391 Loc = UO->getOperatorLoc();
1392 R1 = UO->getSubExpr()->getSourceRange();
1393 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001394 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001395 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001396 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001397 switch (BO->getOpcode()) {
1398 default:
1399 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001400 // Consider the RHS of comma for side effects. LHS was checked by
1401 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001402 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001403 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1404 // lvalue-ness) of an assignment written in a macro.
1405 if (IntegerLiteral *IE =
1406 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1407 if (IE->getValue() == 0)
1408 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001409 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1410 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001411 case BO_LAnd:
1412 case BO_LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001413 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1414 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1415 return false;
1416 break;
John McCall1e3715a2010-02-16 04:10:53 +00001417 }
Chris Lattner237f2752009-02-14 07:37:35 +00001418 if (BO->isAssignmentOp())
1419 return false;
1420 Loc = BO->getOperatorLoc();
1421 R1 = BO->getLHS()->getSourceRange();
1422 R2 = BO->getRHS()->getSourceRange();
1423 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001424 }
Chris Lattner86928112007-08-25 02:00:02 +00001425 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001426 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001427 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001428
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001429 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00001430 // If only one of the LHS or RHS is a warning, the operator might
1431 // be being used for control flow. Only warn if both the LHS and
1432 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001433 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremeneke96dad92011-03-01 20:34:48 +00001434 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1435 return false;
1436 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00001437 return true;
Ted Kremeneke96dad92011-03-01 20:34:48 +00001438 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001439 }
1440
Chris Lattnera44d1162007-06-27 05:58:59 +00001441 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001442 // If the base pointer or element is to a volatile pointer/field, accessing
1443 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001444 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001445 return false;
1446 Loc = cast<MemberExpr>(this)->getMemberLoc();
1447 R1 = SourceRange(Loc, Loc);
1448 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1449 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001450
Chris Lattner1ec5f562007-06-27 05:38:08 +00001451 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001452 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001453 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001454 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001455 return false;
1456 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1457 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1458 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1459 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001460
Chris Lattner1ec5f562007-06-27 05:38:08 +00001461 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001462 case CXXOperatorCallExprClass:
1463 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001464 // If this is a direct call, get the callee.
1465 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001466 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001467 // If the callee has attribute pure, const, or warn_unused_result, warn
1468 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001469 //
1470 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1471 // updated to match for QoI.
1472 if (FD->getAttr<WarnUnusedResultAttr>() ||
1473 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1474 Loc = CE->getCallee()->getLocStart();
1475 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001476
Chris Lattner1a6babf2009-10-13 04:53:48 +00001477 if (unsigned NumArgs = CE->getNumArgs())
1478 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1479 CE->getArg(NumArgs-1)->getLocEnd());
1480 return true;
1481 }
Chris Lattner237f2752009-02-14 07:37:35 +00001482 }
1483 return false;
1484 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001485
1486 case CXXTemporaryObjectExprClass:
1487 case CXXConstructExprClass:
1488 return false;
1489
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001490 case ObjCMessageExprClass: {
1491 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1492 const ObjCMethodDecl *MD = ME->getMethodDecl();
1493 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1494 Loc = getExprLoc();
1495 return true;
1496 }
Chris Lattner237f2752009-02-14 07:37:35 +00001497 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001498 }
Mike Stump11289f42009-09-09 15:08:12 +00001499
John McCallb7bd14f2010-12-02 01:19:52 +00001500 case ObjCPropertyRefExprClass:
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001501 Loc = getExprLoc();
1502 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001503 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00001504
Chris Lattner944d3062008-07-26 19:51:01 +00001505 case StmtExprClass: {
1506 // Statement exprs don't logically have side effects themselves, but are
1507 // sometimes used in macros in ways that give them a type that is unused.
1508 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1509 // however, if the result of the stmt expr is dead, we don't want to emit a
1510 // warning.
1511 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001512 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00001513 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001514 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001515 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1516 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1517 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1518 }
Mike Stump11289f42009-09-09 15:08:12 +00001519
John McCallc493a732010-03-12 07:11:26 +00001520 if (getType()->isVoidType())
1521 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001522 Loc = cast<StmtExpr>(this)->getLParenLoc();
1523 R1 = getSourceRange();
1524 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001525 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001526 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001527 // If this is an explicit cast to void, allow it. People do this when they
1528 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001529 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001530 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001531 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1532 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1533 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001534 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001535 if (getType()->isVoidType())
1536 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001537 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001538
Anders Carlsson6aa50392009-11-17 17:11:23 +00001539 // If this is a cast to void or a constructor conversion, check the operand.
1540 // Otherwise, the result of the cast is unused.
John McCalle3027922010-08-25 11:45:40 +00001541 if (CE->getCastKind() == CK_ToVoid ||
1542 CE->getCastKind() == CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001543 return (cast<CastExpr>(this)->getSubExpr()
1544 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001545 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1546 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1547 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001548 }
Mike Stump11289f42009-09-09 15:08:12 +00001549
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001550 case ImplicitCastExprClass:
1551 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001552 return (cast<ImplicitCastExpr>(this)
1553 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001554
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001555 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001556 return (cast<CXXDefaultArgExpr>(this)
1557 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001558
1559 case CXXNewExprClass:
1560 // FIXME: In theory, there might be new expressions that don't have side
1561 // effects (e.g. a placement new with an uninitialized POD).
1562 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001563 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001564 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001565 return (cast<CXXBindTemporaryExpr>(this)
1566 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00001567 case ExprWithCleanupsClass:
1568 return (cast<ExprWithCleanups>(this)
Mike Stump53f9ded2009-11-03 23:25:48 +00001569 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001570 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001571}
1572
Fariborz Jahanian07735332009-02-22 18:40:18 +00001573/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001574/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001575bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00001576 const Expr *E = IgnoreParens();
1577 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001578 default:
1579 return false;
1580 case ObjCIvarRefExprClass:
1581 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001582 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001583 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001584 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001585 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001586 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001587 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001588 case DeclRefExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00001589 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001590 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1591 if (VD->hasGlobalStorage())
1592 return true;
1593 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001594 // dereferencing to a pointer is always a gc'able candidate,
1595 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001596 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001597 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001598 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001599 return false;
1600 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001601 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00001602 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001603 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001604 }
1605 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001606 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001607 }
1608}
Sebastian Redlce354af2010-09-10 20:55:33 +00001609
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001610bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1611 if (isTypeDependent())
1612 return false;
John McCall086a4642010-11-24 05:12:34 +00001613 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001614}
1615
John McCall0009fcc2011-04-26 20:42:42 +00001616QualType Expr::findBoundMemberType(const Expr *expr) {
1617 assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1618
1619 // Bound member expressions are always one of these possibilities:
1620 // x->m x.m x->*y x.*y
1621 // (possibly parenthesized)
1622
1623 expr = expr->IgnoreParens();
1624 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1625 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1626 return mem->getMemberDecl()->getType();
1627 }
1628
1629 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1630 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1631 ->getPointeeType();
1632 assert(type->isFunctionType());
1633 return type;
1634 }
1635
1636 assert(isa<UnresolvedMemberExpr>(expr));
1637 return QualType();
1638}
1639
Sebastian Redlce354af2010-09-10 20:55:33 +00001640static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1641 Expr::CanThrowResult CT2) {
1642 // CanThrowResult constants are ordered so that the maximum is the correct
1643 // merge result.
1644 return CT1 > CT2 ? CT1 : CT2;
1645}
1646
1647static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1648 Expr *E = const_cast<Expr*>(CE);
1649 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall8322c3a2011-02-13 04:07:26 +00001650 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redlce354af2010-09-10 20:55:33 +00001651 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1652 }
1653 return R;
1654}
1655
Sebastian Redl31ad7542011-03-13 17:09:40 +00001656static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Decl *D,
Sebastian Redlce354af2010-09-10 20:55:33 +00001657 bool NullThrows = true) {
1658 if (!D)
1659 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1660
1661 // See if we can get a function type from the decl somehow.
1662 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1663 if (!VD) // If we have no clue what we're calling, assume the worst.
1664 return Expr::CT_Can;
1665
Sebastian Redlb8a76c42010-09-10 22:34:40 +00001666 // As an extension, we assume that __attribute__((nothrow)) functions don't
1667 // throw.
1668 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1669 return Expr::CT_Cannot;
1670
Sebastian Redlce354af2010-09-10 20:55:33 +00001671 QualType T = VD->getType();
1672 const FunctionProtoType *FT;
1673 if ((FT = T->getAs<FunctionProtoType>())) {
1674 } else if (const PointerType *PT = T->getAs<PointerType>())
1675 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1676 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1677 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1678 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1679 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1680 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1681 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1682
1683 if (!FT)
1684 return Expr::CT_Can;
1685
Sebastian Redl31ad7542011-03-13 17:09:40 +00001686 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redlce354af2010-09-10 20:55:33 +00001687}
1688
1689static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1690 if (DC->isTypeDependent())
1691 return Expr::CT_Dependent;
1692
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001693 if (!DC->getTypeAsWritten()->isReferenceType())
1694 return Expr::CT_Cannot;
1695
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001696 if (DC->getSubExpr()->isTypeDependent())
1697 return Expr::CT_Dependent;
1698
Sebastian Redlce354af2010-09-10 20:55:33 +00001699 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1700}
1701
1702static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1703 const CXXTypeidExpr *DC) {
1704 if (DC->isTypeOperand())
1705 return Expr::CT_Cannot;
1706
1707 Expr *Op = DC->getExprOperand();
1708 if (Op->isTypeDependent())
1709 return Expr::CT_Dependent;
1710
1711 const RecordType *RT = Op->getType()->getAs<RecordType>();
1712 if (!RT)
1713 return Expr::CT_Cannot;
1714
1715 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1716 return Expr::CT_Cannot;
1717
1718 if (Op->Classify(C).isPRValue())
1719 return Expr::CT_Cannot;
1720
1721 return Expr::CT_Can;
1722}
1723
1724Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1725 // C++ [expr.unary.noexcept]p3:
1726 // [Can throw] if in a potentially-evaluated context the expression would
1727 // contain:
1728 switch (getStmtClass()) {
1729 case CXXThrowExprClass:
1730 // - a potentially evaluated throw-expression
1731 return CT_Can;
1732
1733 case CXXDynamicCastExprClass: {
1734 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1735 // where T is a reference type, that requires a run-time check
1736 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1737 if (CT == CT_Can)
1738 return CT;
1739 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1740 }
1741
1742 case CXXTypeidExprClass:
1743 // - a potentially evaluated typeid expression applied to a glvalue
1744 // expression whose type is a polymorphic class type
1745 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1746
1747 // - a potentially evaluated call to a function, member function, function
1748 // pointer, or member function pointer that does not have a non-throwing
1749 // exception-specification
1750 case CallExprClass:
1751 case CXXOperatorCallExprClass:
1752 case CXXMemberCallExprClass: {
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001753 CanThrowResult CT;
1754 if (isTypeDependent())
1755 CT = CT_Dependent;
1756 else
1757 CT = CanCalleeThrow(C, cast<CallExpr>(this)->getCalleeDecl());
Sebastian Redlce354af2010-09-10 20:55:33 +00001758 if (CT == CT_Can)
1759 return CT;
1760 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1761 }
1762
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001763 case CXXConstructExprClass:
1764 case CXXTemporaryObjectExprClass: {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001765 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redlce354af2010-09-10 20:55:33 +00001766 cast<CXXConstructExpr>(this)->getConstructor());
1767 if (CT == CT_Can)
1768 return CT;
1769 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1770 }
1771
1772 case CXXNewExprClass: {
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001773 CanThrowResult CT;
1774 if (isTypeDependent())
1775 CT = CT_Dependent;
1776 else
1777 CT = MergeCanThrow(
Sebastian Redl31ad7542011-03-13 17:09:40 +00001778 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getOperatorNew()),
1779 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redlce354af2010-09-10 20:55:33 +00001780 /*NullThrows*/false));
1781 if (CT == CT_Can)
1782 return CT;
1783 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1784 }
1785
1786 case CXXDeleteExprClass: {
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001787 CanThrowResult CT;
1788 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
1789 if (DTy.isNull() || DTy->isDependentType()) {
1790 CT = CT_Dependent;
1791 } else {
1792 CT = CanCalleeThrow(C, cast<CXXDeleteExpr>(this)->getOperatorDelete());
1793 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1794 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
1795 CT = MergeCanThrow(CT, CanCalleeThrow(C, RD->getDestructor()));
Sebastian Redla8bac372010-09-10 23:27:10 +00001796 }
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001797 if (CT == CT_Can)
1798 return CT;
Sebastian Redla8bac372010-09-10 23:27:10 +00001799 }
1800 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1801 }
1802
1803 case CXXBindTemporaryExprClass: {
1804 // The bound temporary has to be destroyed again, which might throw.
Sebastian Redl31ad7542011-03-13 17:09:40 +00001805 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redla8bac372010-09-10 23:27:10 +00001806 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1807 if (CT == CT_Can)
1808 return CT;
Sebastian Redlce354af2010-09-10 20:55:33 +00001809 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1810 }
1811
1812 // ObjC message sends are like function calls, but never have exception
1813 // specs.
1814 case ObjCMessageExprClass:
1815 case ObjCPropertyRefExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001816 return CT_Can;
1817
1818 // Many other things have subexpressions, so we have to test those.
1819 // Some are simple:
1820 case ParenExprClass:
1821 case MemberExprClass:
1822 case CXXReinterpretCastExprClass:
1823 case CXXConstCastExprClass:
1824 case ConditionalOperatorClass:
1825 case CompoundLiteralExprClass:
1826 case ExtVectorElementExprClass:
1827 case InitListExprClass:
1828 case DesignatedInitExprClass:
1829 case ParenListExprClass:
1830 case VAArgExprClass:
1831 case CXXDefaultArgExprClass:
John McCall5d413782010-12-06 08:20:24 +00001832 case ExprWithCleanupsClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001833 case ObjCIvarRefExprClass:
1834 case ObjCIsaExprClass:
1835 case ShuffleVectorExprClass:
1836 return CanSubExprsThrow(C, this);
1837
1838 // Some might be dependent for other reasons.
1839 case UnaryOperatorClass:
1840 case ArraySubscriptExprClass:
1841 case ImplicitCastExprClass:
1842 case CStyleCastExprClass:
1843 case CXXStaticCastExprClass:
1844 case CXXFunctionalCastExprClass:
1845 case BinaryOperatorClass:
1846 case CompoundAssignOperatorClass: {
1847 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1848 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1849 }
1850
1851 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1852 case StmtExprClass:
1853 return CT_Can;
1854
1855 case ChooseExprClass:
1856 if (isTypeDependent() || isValueDependent())
1857 return CT_Dependent;
1858 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1859
Peter Collingbourne91147592011-04-15 00:35:48 +00001860 case GenericSelectionExprClass:
1861 if (cast<GenericSelectionExpr>(this)->isResultDependent())
1862 return CT_Dependent;
1863 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
1864
Sebastian Redlce354af2010-09-10 20:55:33 +00001865 // Some expressions are always dependent.
1866 case DependentScopeDeclRefExprClass:
1867 case CXXUnresolvedConstructExprClass:
1868 case CXXDependentScopeMemberExprClass:
1869 return CT_Dependent;
1870
1871 default:
1872 // All other expressions don't have subexpressions, or else they are
1873 // unevaluated.
1874 return CT_Cannot;
1875 }
1876}
1877
Ted Kremenekfff70962008-01-17 16:57:34 +00001878Expr* Expr::IgnoreParens() {
1879 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00001880 while (true) {
1881 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1882 E = P->getSubExpr();
1883 continue;
1884 }
1885 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1886 if (P->getOpcode() == UO_Extension) {
1887 E = P->getSubExpr();
1888 continue;
1889 }
1890 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001891 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1892 if (!P->isResultDependent()) {
1893 E = P->getResultExpr();
1894 continue;
1895 }
1896 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00001897 return E;
1898 }
Ted Kremenekfff70962008-01-17 16:57:34 +00001899}
1900
Chris Lattnerf2660962008-02-13 01:02:39 +00001901/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1902/// or CastExprs or ImplicitCastExprs, returning their operand.
1903Expr *Expr::IgnoreParenCasts() {
1904 Expr *E = this;
1905 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001906 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001907 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001908 continue;
1909 }
1910 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001911 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001912 continue;
1913 }
1914 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1915 if (P->getOpcode() == UO_Extension) {
1916 E = P->getSubExpr();
1917 continue;
1918 }
1919 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001920 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1921 if (!P->isResultDependent()) {
1922 E = P->getResultExpr();
1923 continue;
1924 }
1925 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00001926 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00001927 }
1928}
1929
John McCall5a4ce8b2010-12-04 08:24:19 +00001930/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1931/// casts. This is intended purely as a temporary workaround for code
1932/// that hasn't yet been rewritten to do the right thing about those
1933/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00001934Expr *Expr::IgnoreParenLValueCasts() {
1935 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00001936 while (true) {
John McCall34376a62010-12-04 03:47:34 +00001937 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1938 E = P->getSubExpr();
1939 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00001940 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00001941 if (P->getCastKind() == CK_LValueToRValue) {
1942 E = P->getSubExpr();
1943 continue;
1944 }
John McCall5a4ce8b2010-12-04 08:24:19 +00001945 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1946 if (P->getOpcode() == UO_Extension) {
1947 E = P->getSubExpr();
1948 continue;
1949 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001950 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1951 if (!P->isResultDependent()) {
1952 E = P->getResultExpr();
1953 continue;
1954 }
John McCall34376a62010-12-04 03:47:34 +00001955 }
1956 break;
1957 }
1958 return E;
1959}
1960
John McCalleebc8322010-05-05 22:59:52 +00001961Expr *Expr::IgnoreParenImpCasts() {
1962 Expr *E = this;
1963 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001964 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001965 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001966 continue;
1967 }
1968 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001969 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001970 continue;
1971 }
1972 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1973 if (P->getOpcode() == UO_Extension) {
1974 E = P->getSubExpr();
1975 continue;
1976 }
1977 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001978 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1979 if (!P->isResultDependent()) {
1980 E = P->getResultExpr();
1981 continue;
1982 }
1983 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00001984 return E;
John McCalleebc8322010-05-05 22:59:52 +00001985 }
1986}
1987
Chris Lattneref26c772009-03-13 17:28:01 +00001988/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1989/// value (including ptr->int casts of the same size). Strip off any
1990/// ParenExpr or CastExprs, returning their operand.
1991Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1992 Expr *E = this;
1993 while (true) {
1994 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1995 E = P->getSubExpr();
1996 continue;
1997 }
Mike Stump11289f42009-09-09 15:08:12 +00001998
Chris Lattneref26c772009-03-13 17:28:01 +00001999 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2000 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002001 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002002 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002003
Chris Lattneref26c772009-03-13 17:28:01 +00002004 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2005 E = SE;
2006 continue;
2007 }
Mike Stump11289f42009-09-09 15:08:12 +00002008
Abramo Bagnara932e3932010-10-15 07:51:18 +00002009 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002010 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002011 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002012 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002013 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2014 E = SE;
2015 continue;
2016 }
2017 }
Mike Stump11289f42009-09-09 15:08:12 +00002018
Abramo Bagnara932e3932010-10-15 07:51:18 +00002019 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2020 if (P->getOpcode() == UO_Extension) {
2021 E = P->getSubExpr();
2022 continue;
2023 }
2024 }
2025
Peter Collingbourne91147592011-04-15 00:35:48 +00002026 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2027 if (!P->isResultDependent()) {
2028 E = P->getResultExpr();
2029 continue;
2030 }
2031 }
2032
Chris Lattneref26c772009-03-13 17:28:01 +00002033 return E;
2034 }
2035}
2036
Douglas Gregord196a582009-12-14 19:27:10 +00002037bool Expr::isDefaultArgument() const {
2038 const Expr *E = this;
2039 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2040 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002041
Douglas Gregord196a582009-12-14 19:27:10 +00002042 return isa<CXXDefaultArgExpr>(E);
2043}
Chris Lattneref26c772009-03-13 17:28:01 +00002044
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002045/// \brief Skip over any no-op casts and any temporary-binding
2046/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002047static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002048 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002049 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002050 E = ICE->getSubExpr();
2051 else
2052 break;
2053 }
2054
2055 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2056 E = BE->getSubExpr();
2057
2058 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002059 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002060 E = ICE->getSubExpr();
2061 else
2062 break;
2063 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002064
2065 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002066}
2067
John McCall7a626f62010-09-15 10:14:12 +00002068/// isTemporaryObject - Determines if this expression produces a
2069/// temporary of the given class type.
2070bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2071 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2072 return false;
2073
Anders Carlsson66bbf502010-11-28 16:40:49 +00002074 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002075
John McCall02dc8c72010-09-15 20:59:13 +00002076 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002077 if (!E->Classify(C).isPRValue()) {
2078 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002079 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002080 return false;
2081 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002082
John McCallf4ee1dd2010-09-16 06:57:56 +00002083 // Black-list a few cases which yield pr-values of class type that don't
2084 // refer to temporaries of that type:
2085
2086 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002087 if (isa<ImplicitCastExpr>(E)) {
2088 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2089 case CK_DerivedToBase:
2090 case CK_UncheckedDerivedToBase:
2091 return false;
2092 default:
2093 break;
2094 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002095 }
2096
John McCallf4ee1dd2010-09-16 06:57:56 +00002097 // - member expressions (all)
2098 if (isa<MemberExpr>(E))
2099 return false;
2100
John McCallc07a0c72011-02-17 10:25:35 +00002101 // - opaque values (all)
2102 if (isa<OpaqueValueExpr>(E))
2103 return false;
2104
John McCall7a626f62010-09-15 10:14:12 +00002105 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002106}
2107
Douglas Gregor25b7e052011-03-02 21:06:53 +00002108bool Expr::isImplicitCXXThis() const {
2109 const Expr *E = this;
2110
2111 // Strip away parentheses and casts we don't care about.
2112 while (true) {
2113 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2114 E = Paren->getSubExpr();
2115 continue;
2116 }
2117
2118 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2119 if (ICE->getCastKind() == CK_NoOp ||
2120 ICE->getCastKind() == CK_LValueToRValue ||
2121 ICE->getCastKind() == CK_DerivedToBase ||
2122 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2123 E = ICE->getSubExpr();
2124 continue;
2125 }
2126 }
2127
2128 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2129 if (UnOp->getOpcode() == UO_Extension) {
2130 E = UnOp->getSubExpr();
2131 continue;
2132 }
2133 }
2134
2135 break;
2136 }
2137
2138 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2139 return This->isImplicit();
2140
2141 return false;
2142}
2143
Douglas Gregor4619e432008-12-05 23:32:09 +00002144/// hasAnyTypeDependentArguments - Determines if any of the expressions
2145/// in Exprs is type-dependent.
2146bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2147 for (unsigned I = 0; I < NumExprs; ++I)
2148 if (Exprs[I]->isTypeDependent())
2149 return true;
2150
2151 return false;
2152}
2153
2154/// hasAnyValueDependentArguments - Determines if any of the expressions
2155/// in Exprs is value-dependent.
2156bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2157 for (unsigned I = 0; I < NumExprs; ++I)
2158 if (Exprs[I]->isValueDependent())
2159 return true;
2160
2161 return false;
2162}
2163
John McCall8b0f4ff2010-08-02 21:13:48 +00002164bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002165 // This function is attempting whether an expression is an initializer
2166 // which can be evaluated at compile-time. isEvaluatable handles most
2167 // of the cases, but it can't deal with some initializer-specific
2168 // expressions, and it can't deal with aggregates; we deal with those here,
2169 // and fall back to isEvaluatable for the other cases.
2170
John McCall8b0f4ff2010-08-02 21:13:48 +00002171 // If we ever capture reference-binding directly in the AST, we can
2172 // kill the second parameter.
2173
2174 if (IsForRef) {
2175 EvalResult Result;
2176 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2177 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002178
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002179 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002180 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002181 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002182 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002183 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002184 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002185 case CXXTemporaryObjectExprClass:
2186 case CXXConstructExprClass: {
2187 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002188
2189 // Only if it's
2190 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00002191 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002192 if (!CE->getNumArgs()) return true;
2193
2194 // 2) an elidable trivial copy construction of an operand which is
2195 // itself a constant initializer. Note that we consider the
2196 // operand on its own, *not* as a reference binding.
2197 return CE->isElidable() &&
2198 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00002199 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002200 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002201 // This handles gcc's extension that allows global initializers like
2202 // "struct x {int x;} x = (struct x) {};".
2203 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002204 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002205 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002206 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002207 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002208 // FIXME: This doesn't deal with fields with reference types correctly.
2209 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2210 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002211 const InitListExpr *Exp = cast<InitListExpr>(this);
2212 unsigned numInits = Exp->getNumInits();
2213 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002214 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002215 return false;
2216 }
Eli Friedman384da272009-01-25 03:12:18 +00002217 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002218 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002219 case ImplicitValueInitExprClass:
2220 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002221 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002222 return cast<ParenExpr>(this)->getSubExpr()
2223 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002224 case GenericSelectionExprClass:
2225 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2226 return false;
2227 return cast<GenericSelectionExpr>(this)->getResultExpr()
2228 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002229 case ChooseExprClass:
2230 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2231 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002232 case UnaryOperatorClass: {
2233 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002234 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002235 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002236 break;
2237 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00002238 case BinaryOperatorClass: {
2239 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2240 // but this handles the common case.
2241 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002242 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3eb172a2009-10-13 07:14:16 +00002243 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2244 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2245 return true;
2246 break;
2247 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002248 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002249 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002250 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00002251 case CStyleCastExprClass:
2252 // Handle casts with a destination that's a struct or union; this
2253 // deals with both the gcc no-op struct cast extension and the
2254 // cast-to-union extension.
2255 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002256 return cast<CastExpr>(this)->getSubExpr()
2257 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002258
Chris Lattnera2f9bd52009-10-13 22:12:09 +00002259 // Integer->integer casts can be handled here, which is important for
2260 // things like (int)(&&x-&&y). Scary but true.
2261 if (getType()->isIntegerType() &&
2262 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002263 return cast<CastExpr>(this)->getSubExpr()
2264 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002265
Eli Friedman384da272009-01-25 03:12:18 +00002266 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002267 }
Eli Friedman384da272009-01-25 03:12:18 +00002268 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002269}
2270
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002271/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2272/// pointer constant or not, as well as the specific kind of constant detected.
2273/// Null pointer constants can be integer constant expressions with the
2274/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2275/// (a GNU extension).
2276Expr::NullPointerConstantKind
2277Expr::isNullPointerConstant(ASTContext &Ctx,
2278 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00002279 if (isValueDependent()) {
2280 switch (NPC) {
2281 case NPC_NeverValueDependent:
2282 assert(false && "Unexpected value dependent expression!");
2283 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002284
Douglas Gregor56751b52009-09-25 04:25:58 +00002285 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002286 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2287 return NPCK_ZeroInteger;
2288 else
2289 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00002290
Douglas Gregor56751b52009-09-25 04:25:58 +00002291 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002292 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00002293 }
2294 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002295
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002296 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002297 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00002298 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002299 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002300 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002301 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002302 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002303 Pointee->isVoidType() && // to void*
2304 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002305 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002306 }
Steve Naroffada7d422007-05-20 17:54:12 +00002307 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002308 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2309 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002310 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002311 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2312 // Accept ((void*)0) as a null pointer constant, as many other
2313 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002314 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00002315 } else if (const GenericSelectionExpr *GE =
2316 dyn_cast<GenericSelectionExpr>(this)) {
2317 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002318 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002319 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002320 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002321 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002322 } else if (isa<GNUNullExpr>(this)) {
2323 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002324 return NPCK_GNUNull;
Steve Naroff09035312008-01-14 02:53:34 +00002325 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002326
Sebastian Redl576fd422009-05-10 18:38:11 +00002327 // C++0x nullptr_t is always a null pointer constant.
2328 if (getType()->isNullPtrType())
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002329 return NPCK_CXX0X_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00002330
Fariborz Jahanian3567c422010-09-27 22:42:37 +00002331 if (const RecordType *UT = getType()->getAsUnionType())
2332 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2333 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2334 const Expr *InitExpr = CLE->getInitializer();
2335 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2336 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2337 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002338 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002339 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002340 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002341 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00002342
Chris Lattner1abbd412007-06-08 17:58:43 +00002343 // If we have an integer constant expression, we need to *evaluate* it and
2344 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002345 llvm::APSInt Result;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002346 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2347
2348 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002349}
Steve Narofff7a5da12007-07-28 23:10:27 +00002350
John McCall34376a62010-12-04 03:47:34 +00002351/// \brief If this expression is an l-value for an Objective C
2352/// property, find the underlying property reference expression.
2353const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2354 const Expr *E = this;
2355 while (true) {
2356 assert((E->getValueKind() == VK_LValue &&
2357 E->getObjectKind() == OK_ObjCProperty) &&
2358 "expression is not a property reference");
2359 E = E->IgnoreParenCasts();
2360 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2361 if (BO->getOpcode() == BO_Comma) {
2362 E = BO->getRHS();
2363 continue;
2364 }
2365 }
2366
2367 break;
2368 }
2369
2370 return cast<ObjCPropertyRefExpr>(E);
2371}
2372
Douglas Gregor71235ec2009-05-02 02:18:30 +00002373FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002374 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002375
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002376 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002377 if (ICE->getCastKind() == CK_LValueToRValue ||
2378 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002379 E = ICE->getSubExpr()->IgnoreParens();
2380 else
2381 break;
2382 }
2383
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002384 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002385 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002386 if (Field->isBitField())
2387 return Field;
2388
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00002389 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2390 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2391 if (Field->isBitField())
2392 return Field;
2393
Douglas Gregor71235ec2009-05-02 02:18:30 +00002394 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2395 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2396 return BinOp->getLHS()->getBitField();
2397
2398 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002399}
2400
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002401bool Expr::refersToVectorElement() const {
2402 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002403
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002404 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002405 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002406 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002407 E = ICE->getSubExpr()->IgnoreParens();
2408 else
2409 break;
2410 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002411
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002412 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2413 return ASE->getBase()->getType()->isVectorType();
2414
2415 if (isa<ExtVectorElementExpr>(E))
2416 return true;
2417
2418 return false;
2419}
2420
Chris Lattnerb8211f62009-02-16 22:14:05 +00002421/// isArrow - Return true if the base expression is a pointer to vector,
2422/// return false if the base expression is a vector.
2423bool ExtVectorElementExpr::isArrow() const {
2424 return getBase()->getType()->isPointerType();
2425}
2426
Nate Begemance4d7fc2008-04-18 23:10:10 +00002427unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002428 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002429 return VT->getNumElements();
2430 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002431}
2432
Nate Begemanf322eab2008-05-09 06:41:27 +00002433/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002434bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002435 // FIXME: Refactor this code to an accessor on the AST node which returns the
2436 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002437 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002438
2439 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002440 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002441 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002442
Nate Begeman7e5185b2009-01-18 02:01:21 +00002443 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002444 if (Comp[0] == 's' || Comp[0] == 'S')
2445 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002446
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002447 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2448 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002449 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002450
Steve Naroff0d595ca2007-07-30 03:29:09 +00002451 return false;
2452}
Chris Lattner885b4952007-08-02 23:36:59 +00002453
Nate Begemanf322eab2008-05-09 06:41:27 +00002454/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002455void ExtVectorElementExpr::getEncodedElementAccess(
2456 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002457 llvm::StringRef Comp = Accessor->getName();
2458 if (Comp[0] == 's' || Comp[0] == 'S')
2459 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002460
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002461 bool isHi = Comp == "hi";
2462 bool isLo = Comp == "lo";
2463 bool isEven = Comp == "even";
2464 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002465
Nate Begemanf322eab2008-05-09 06:41:27 +00002466 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2467 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002468
Nate Begemanf322eab2008-05-09 06:41:27 +00002469 if (isHi)
2470 Index = e + i;
2471 else if (isLo)
2472 Index = i;
2473 else if (isEven)
2474 Index = 2 * i;
2475 else if (isOdd)
2476 Index = 2 * i + 1;
2477 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002478 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002479
Nate Begemand3862152008-05-13 21:03:02 +00002480 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002481 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002482}
2483
Douglas Gregor9a129192010-04-21 00:45:42 +00002484ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002485 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002486 SourceLocation LBracLoc,
2487 SourceLocation SuperLoc,
2488 bool IsInstanceSuper,
2489 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002490 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002491 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002492 ObjCMethodDecl *Method,
2493 Expr **Args, unsigned NumArgs,
2494 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002495 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002496 /*TypeDependent=*/false, /*ValueDependent=*/false,
2497 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002498 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2499 HasMethod(Method != 0), SuperLoc(SuperLoc),
2500 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2501 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002502 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002503{
Douglas Gregor9a129192010-04-21 00:45:42 +00002504 setReceiverPointer(SuperType.getAsOpaquePtr());
2505 if (NumArgs)
2506 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002507}
2508
Douglas Gregor9a129192010-04-21 00:45:42 +00002509ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002510 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002511 SourceLocation LBracLoc,
2512 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002513 Selector Sel,
2514 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002515 ObjCMethodDecl *Method,
2516 Expr **Args, unsigned NumArgs,
2517 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002518 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002519 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002520 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2521 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2522 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002523 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002524{
2525 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002526 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002527 for (unsigned I = 0; I != NumArgs; ++I) {
2528 if (Args[I]->isTypeDependent())
2529 ExprBits.TypeDependent = true;
2530 if (Args[I]->isValueDependent())
2531 ExprBits.ValueDependent = true;
2532 if (Args[I]->containsUnexpandedParameterPack())
2533 ExprBits.ContainsUnexpandedParameterPack = true;
2534
2535 MyArgs[I] = Args[I];
2536 }
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002537}
2538
Douglas Gregor9a129192010-04-21 00:45:42 +00002539ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002540 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002541 SourceLocation LBracLoc,
2542 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002543 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002544 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002545 ObjCMethodDecl *Method,
2546 Expr **Args, unsigned NumArgs,
2547 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002548 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002549 Receiver->isTypeDependent(),
2550 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002551 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2552 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2553 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002554 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002555{
2556 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002557 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002558 for (unsigned I = 0; I != NumArgs; ++I) {
2559 if (Args[I]->isTypeDependent())
2560 ExprBits.TypeDependent = true;
2561 if (Args[I]->isValueDependent())
2562 ExprBits.ValueDependent = true;
2563 if (Args[I]->containsUnexpandedParameterPack())
2564 ExprBits.ContainsUnexpandedParameterPack = true;
2565
2566 MyArgs[I] = Args[I];
2567 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00002568}
2569
Douglas Gregor9a129192010-04-21 00:45:42 +00002570ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002571 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002572 SourceLocation LBracLoc,
2573 SourceLocation SuperLoc,
2574 bool IsInstanceSuper,
2575 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002576 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002577 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002578 ObjCMethodDecl *Method,
2579 Expr **Args, unsigned NumArgs,
2580 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002581 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002582 NumArgs * sizeof(Expr *);
2583 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCall7decc9e2010-11-18 06:31:45 +00002584 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002585 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002586 RBracLoc);
2587}
2588
2589ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002590 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002591 SourceLocation LBracLoc,
2592 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002593 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002594 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002595 ObjCMethodDecl *Method,
2596 Expr **Args, unsigned NumArgs,
2597 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002598 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002599 NumArgs * sizeof(Expr *);
2600 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002601 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2602 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002603}
2604
2605ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002606 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002607 SourceLocation LBracLoc,
2608 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002609 Selector Sel,
2610 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002611 ObjCMethodDecl *Method,
2612 Expr **Args, unsigned NumArgs,
2613 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002614 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002615 NumArgs * sizeof(Expr *);
2616 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002617 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2618 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002619}
2620
Alexis Hunta8136cc2010-05-05 15:23:54 +00002621ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002622 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002623 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002624 NumArgs * sizeof(Expr *);
2625 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2626 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2627}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00002628
2629SourceRange ObjCMessageExpr::getReceiverRange() const {
2630 switch (getReceiverKind()) {
2631 case Instance:
2632 return getInstanceReceiver()->getSourceRange();
2633
2634 case Class:
2635 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2636
2637 case SuperInstance:
2638 case SuperClass:
2639 return getSuperLoc();
2640 }
2641
2642 return SourceLocation();
2643}
2644
Douglas Gregor9a129192010-04-21 00:45:42 +00002645Selector ObjCMessageExpr::getSelector() const {
2646 if (HasMethod)
2647 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2648 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002649 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002650}
2651
2652ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2653 switch (getReceiverKind()) {
2654 case Instance:
2655 if (const ObjCObjectPointerType *Ptr
2656 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2657 return Ptr->getInterfaceDecl();
2658 break;
2659
2660 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00002661 if (const ObjCObjectType *Ty
2662 = getClassReceiver()->getAs<ObjCObjectType>())
2663 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002664 break;
2665
2666 case SuperInstance:
2667 if (const ObjCObjectPointerType *Ptr
2668 = getSuperType()->getAs<ObjCObjectPointerType>())
2669 return Ptr->getInterfaceDecl();
2670 break;
2671
2672 case SuperClass:
Argyrios Kyrtzidis1b9747f2011-01-25 00:03:48 +00002673 if (const ObjCObjectType *Iface
2674 = getSuperType()->getAs<ObjCObjectType>())
2675 return Iface->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002676 break;
2677 }
2678
2679 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002680}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002681
Jay Foad39c79802011-01-12 09:06:06 +00002682bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002683 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002684}
2685
Douglas Gregora6e053e2010-12-15 01:34:56 +00002686ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2687 QualType Type, SourceLocation BLoc,
2688 SourceLocation RP)
2689 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2690 Type->isDependentType(), Type->isDependentType(),
2691 Type->containsUnexpandedParameterPack()),
2692 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2693{
2694 SubExprs = new (C) Stmt*[nexpr];
2695 for (unsigned i = 0; i < nexpr; i++) {
2696 if (args[i]->isTypeDependent())
2697 ExprBits.TypeDependent = true;
2698 if (args[i]->isValueDependent())
2699 ExprBits.ValueDependent = true;
2700 if (args[i]->containsUnexpandedParameterPack())
2701 ExprBits.ContainsUnexpandedParameterPack = true;
2702
2703 SubExprs[i] = args[i];
2704 }
2705}
2706
Nate Begeman48745922009-08-12 02:28:50 +00002707void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2708 unsigned NumExprs) {
2709 if (SubExprs) C.Deallocate(SubExprs);
2710
2711 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002712 this->NumExprs = NumExprs;
2713 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002714}
Nate Begeman48745922009-08-12 02:28:50 +00002715
Peter Collingbourne91147592011-04-15 00:35:48 +00002716GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2717 SourceLocation GenericLoc, Expr *ControllingExpr,
2718 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2719 unsigned NumAssocs, SourceLocation DefaultLoc,
2720 SourceLocation RParenLoc,
2721 bool ContainsUnexpandedParameterPack,
2722 unsigned ResultIndex)
2723 : Expr(GenericSelectionExprClass,
2724 AssocExprs[ResultIndex]->getType(),
2725 AssocExprs[ResultIndex]->getValueKind(),
2726 AssocExprs[ResultIndex]->getObjectKind(),
2727 AssocExprs[ResultIndex]->isTypeDependent(),
2728 AssocExprs[ResultIndex]->isValueDependent(),
2729 ContainsUnexpandedParameterPack),
2730 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2731 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2732 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2733 RParenLoc(RParenLoc) {
2734 SubExprs[CONTROLLING] = ControllingExpr;
2735 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2736 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2737}
2738
2739GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2740 SourceLocation GenericLoc, Expr *ControllingExpr,
2741 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2742 unsigned NumAssocs, SourceLocation DefaultLoc,
2743 SourceLocation RParenLoc,
2744 bool ContainsUnexpandedParameterPack)
2745 : Expr(GenericSelectionExprClass,
2746 Context.DependentTy,
2747 VK_RValue,
2748 OK_Ordinary,
2749 /*isTypeDependent=*/ true,
2750 /*isValueDependent=*/ true,
2751 ContainsUnexpandedParameterPack),
2752 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2753 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2754 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2755 RParenLoc(RParenLoc) {
2756 SubExprs[CONTROLLING] = ControllingExpr;
2757 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2758 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2759}
2760
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002761//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002762// DesignatedInitExpr
2763//===----------------------------------------------------------------------===//
2764
2765IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2766 assert(Kind == FieldDesignator && "Only valid on a field designator");
2767 if (Field.NameOrField & 0x01)
2768 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2769 else
2770 return getField()->getIdentifier();
2771}
2772
Alexis Hunta8136cc2010-05-05 15:23:54 +00002773DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002774 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002775 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002776 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002777 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002778 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002779 unsigned NumIndexExprs,
2780 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002781 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00002782 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002783 Init->isTypeDependent(), Init->isValueDependent(),
2784 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00002785 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2786 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002787 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002788
2789 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00002790 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002791 *Child++ = Init;
2792
2793 // Copy the designators and their subexpressions, computing
2794 // value-dependence along the way.
2795 unsigned IndexIdx = 0;
2796 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002797 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002798
2799 if (this->Designators[I].isArrayDesignator()) {
2800 // Compute type- and value-dependence.
2801 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002802 if (Index->isTypeDependent() || Index->isValueDependent())
2803 ExprBits.ValueDependent = true;
2804
2805 // Propagate unexpanded parameter packs.
2806 if (Index->containsUnexpandedParameterPack())
2807 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002808
2809 // Copy the index expressions into permanent storage.
2810 *Child++ = IndexExprs[IndexIdx++];
2811 } else if (this->Designators[I].isArrayRangeDesignator()) {
2812 // Compute type- and value-dependence.
2813 Expr *Start = IndexExprs[IndexIdx];
2814 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002815 if (Start->isTypeDependent() || Start->isValueDependent() ||
2816 End->isTypeDependent() || End->isValueDependent())
2817 ExprBits.ValueDependent = true;
2818
2819 // Propagate unexpanded parameter packs.
2820 if (Start->containsUnexpandedParameterPack() ||
2821 End->containsUnexpandedParameterPack())
2822 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002823
2824 // Copy the start/end expressions into permanent storage.
2825 *Child++ = IndexExprs[IndexIdx++];
2826 *Child++ = IndexExprs[IndexIdx++];
2827 }
2828 }
2829
2830 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002831}
2832
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002833DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002834DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002835 unsigned NumDesignators,
2836 Expr **IndexExprs, unsigned NumIndexExprs,
2837 SourceLocation ColonOrEqualLoc,
2838 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002839 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002840 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002841 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002842 ColonOrEqualLoc, UsesColonSyntax,
2843 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002844}
2845
Mike Stump11289f42009-09-09 15:08:12 +00002846DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002847 unsigned NumIndexExprs) {
2848 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2849 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2850 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2851}
2852
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002853void DesignatedInitExpr::setDesignators(ASTContext &C,
2854 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002855 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002856 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002857 NumDesignators = NumDesigs;
2858 for (unsigned I = 0; I != NumDesigs; ++I)
2859 Designators[I] = Desigs[I];
2860}
2861
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00002862SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
2863 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
2864 if (size() == 1)
2865 return DIE->getDesignator(0)->getSourceRange();
2866 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
2867 DIE->getDesignator(size()-1)->getEndLocation());
2868}
2869
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002870SourceRange DesignatedInitExpr::getSourceRange() const {
2871 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002872 Designator &First =
2873 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002874 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002875 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002876 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2877 else
2878 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2879 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002880 StartLoc =
2881 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002882 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2883}
2884
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002885Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2886 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2887 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2888 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002889 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2890 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2891}
2892
2893Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002894 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002895 "Requires array range designator");
2896 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2897 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002898 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2899 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2900}
2901
2902Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002903 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002904 "Requires array range designator");
2905 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2906 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002907 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2908 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2909}
2910
Douglas Gregord5846a12009-04-15 06:41:24 +00002911/// \brief Replaces the designator at index @p Idx with the series
2912/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002913void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002914 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002915 const Designator *Last) {
2916 unsigned NumNewDesignators = Last - First;
2917 if (NumNewDesignators == 0) {
2918 std::copy_backward(Designators + Idx + 1,
2919 Designators + NumDesignators,
2920 Designators + Idx);
2921 --NumNewDesignators;
2922 return;
2923 } else if (NumNewDesignators == 1) {
2924 Designators[Idx] = *First;
2925 return;
2926 }
2927
Mike Stump11289f42009-09-09 15:08:12 +00002928 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002929 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002930 std::copy(Designators, Designators + Idx, NewDesignators);
2931 std::copy(First, Last, NewDesignators + Idx);
2932 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2933 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00002934 Designators = NewDesignators;
2935 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2936}
2937
Mike Stump11289f42009-09-09 15:08:12 +00002938ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002939 Expr **exprs, unsigned nexprs,
2940 SourceLocation rparenloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00002941 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2942 false, false, false),
2943 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002944
Nate Begeman5ec4b312009-08-10 23:49:36 +00002945 Exprs = new (C) Stmt*[nexprs];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002946 for (unsigned i = 0; i != nexprs; ++i) {
2947 if (exprs[i]->isTypeDependent())
2948 ExprBits.TypeDependent = true;
2949 if (exprs[i]->isValueDependent())
2950 ExprBits.ValueDependent = true;
2951 if (exprs[i]->containsUnexpandedParameterPack())
2952 ExprBits.ContainsUnexpandedParameterPack = true;
2953
Nate Begeman5ec4b312009-08-10 23:49:36 +00002954 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002955 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00002956}
2957
John McCall1bf58462011-02-16 08:02:54 +00002958const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
2959 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
2960 e = ewc->getSubExpr();
2961 e = cast<CXXConstructExpr>(e)->getArg(0);
2962 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
2963 e = ice->getSubExpr();
2964 return cast<OpaqueValueExpr>(e);
2965}
2966
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002967//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002968// ExprIterator.
2969//===----------------------------------------------------------------------===//
2970
2971Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2972Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2973Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2974const Expr* ConstExprIterator::operator[](size_t idx) const {
2975 return cast<Expr>(I[idx]);
2976}
2977const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2978const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2979
2980//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002981// Child Iterators for iterating over subexpressions/substatements
2982//===----------------------------------------------------------------------===//
2983
Peter Collingbournee190dee2011-03-11 19:24:49 +00002984// UnaryExprOrTypeTraitExpr
2985Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002986 // If this is of a type and the type is a VLA type (and not a typedef), the
2987 // size expression of the VLA needs to be treated as an executable expression.
2988 // Why isn't this weirdness documented better in StmtIterator?
2989 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002990 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00002991 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00002992 return child_range(child_iterator(T), child_iterator());
2993 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00002994 }
John McCallbd066782011-02-09 08:16:59 +00002995 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002996}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002997
Steve Naroffd54978b2007-09-18 23:55:05 +00002998// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00002999Stmt::child_range ObjCMessageExpr::children() {
3000 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003001 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003002 begin = reinterpret_cast<Stmt **>(this + 1);
3003 else
3004 begin = reinterpret_cast<Stmt **>(getArgs());
3005 return child_range(begin,
3006 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003007}
3008
Steve Naroffc540d662008-09-03 18:15:37 +00003009// Blocks
John McCall351762c2011-02-07 10:33:21 +00003010BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregor476e3022011-01-19 21:32:01 +00003011 SourceLocation l, bool ByRef,
John McCall351762c2011-02-07 10:33:21 +00003012 bool constAdded)
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003013 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregor476e3022011-01-19 21:32:01 +00003014 d->isParameterPack()),
John McCall351762c2011-02-07 10:33:21 +00003015 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregor476e3022011-01-19 21:32:01 +00003016{
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003017 bool TypeDependent = false;
3018 bool ValueDependent = false;
3019 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
3020 ExprBits.TypeDependent = TypeDependent;
3021 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor476e3022011-01-19 21:32:01 +00003022}
3023