blob: 6499f327b07e70263374ff56bfc473bad5ccac6e [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
Sebastian Redlce354af2010-09-10 20:55:33 +00001696 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1697}
1698
1699static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1700 const CXXTypeidExpr *DC) {
1701 if (DC->isTypeOperand())
1702 return Expr::CT_Cannot;
1703
1704 Expr *Op = DC->getExprOperand();
1705 if (Op->isTypeDependent())
1706 return Expr::CT_Dependent;
1707
1708 const RecordType *RT = Op->getType()->getAs<RecordType>();
1709 if (!RT)
1710 return Expr::CT_Cannot;
1711
1712 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1713 return Expr::CT_Cannot;
1714
1715 if (Op->Classify(C).isPRValue())
1716 return Expr::CT_Cannot;
1717
1718 return Expr::CT_Can;
1719}
1720
1721Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1722 // C++ [expr.unary.noexcept]p3:
1723 // [Can throw] if in a potentially-evaluated context the expression would
1724 // contain:
1725 switch (getStmtClass()) {
1726 case CXXThrowExprClass:
1727 // - a potentially evaluated throw-expression
1728 return CT_Can;
1729
1730 case CXXDynamicCastExprClass: {
1731 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1732 // where T is a reference type, that requires a run-time check
1733 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1734 if (CT == CT_Can)
1735 return CT;
1736 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1737 }
1738
1739 case CXXTypeidExprClass:
1740 // - a potentially evaluated typeid expression applied to a glvalue
1741 // expression whose type is a polymorphic class type
1742 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1743
1744 // - a potentially evaluated call to a function, member function, function
1745 // pointer, or member function pointer that does not have a non-throwing
1746 // exception-specification
1747 case CallExprClass:
1748 case CXXOperatorCallExprClass:
1749 case CXXMemberCallExprClass: {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001750 CanThrowResult CT = CanCalleeThrow(C,cast<CallExpr>(this)->getCalleeDecl());
Sebastian Redlce354af2010-09-10 20:55:33 +00001751 if (CT == CT_Can)
1752 return CT;
1753 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1754 }
1755
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001756 case CXXConstructExprClass:
1757 case CXXTemporaryObjectExprClass: {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001758 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redlce354af2010-09-10 20:55:33 +00001759 cast<CXXConstructExpr>(this)->getConstructor());
1760 if (CT == CT_Can)
1761 return CT;
1762 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1763 }
1764
1765 case CXXNewExprClass: {
1766 CanThrowResult CT = MergeCanThrow(
Sebastian Redl31ad7542011-03-13 17:09:40 +00001767 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getOperatorNew()),
1768 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redlce354af2010-09-10 20:55:33 +00001769 /*NullThrows*/false));
1770 if (CT == CT_Can)
1771 return CT;
1772 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1773 }
1774
1775 case CXXDeleteExprClass: {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001776 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redlce354af2010-09-10 20:55:33 +00001777 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1778 if (CT == CT_Can)
1779 return CT;
Sebastian Redla8bac372010-09-10 23:27:10 +00001780 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1781 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1782 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1783 Arg = Cast->getSubExpr();
1784 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1785 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001786 CanThrowResult CT2 = CanCalleeThrow(C,
Sebastian Redla8bac372010-09-10 23:27:10 +00001787 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1788 if (CT2 == CT_Can)
1789 return CT2;
1790 CT = MergeCanThrow(CT, CT2);
1791 }
1792 }
1793 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1794 }
1795
1796 case CXXBindTemporaryExprClass: {
1797 // The bound temporary has to be destroyed again, which might throw.
Sebastian Redl31ad7542011-03-13 17:09:40 +00001798 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redla8bac372010-09-10 23:27:10 +00001799 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1800 if (CT == CT_Can)
1801 return CT;
Sebastian Redlce354af2010-09-10 20:55:33 +00001802 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1803 }
1804
1805 // ObjC message sends are like function calls, but never have exception
1806 // specs.
1807 case ObjCMessageExprClass:
1808 case ObjCPropertyRefExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001809 return CT_Can;
1810
1811 // Many other things have subexpressions, so we have to test those.
1812 // Some are simple:
1813 case ParenExprClass:
1814 case MemberExprClass:
1815 case CXXReinterpretCastExprClass:
1816 case CXXConstCastExprClass:
1817 case ConditionalOperatorClass:
1818 case CompoundLiteralExprClass:
1819 case ExtVectorElementExprClass:
1820 case InitListExprClass:
1821 case DesignatedInitExprClass:
1822 case ParenListExprClass:
1823 case VAArgExprClass:
1824 case CXXDefaultArgExprClass:
John McCall5d413782010-12-06 08:20:24 +00001825 case ExprWithCleanupsClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001826 case ObjCIvarRefExprClass:
1827 case ObjCIsaExprClass:
1828 case ShuffleVectorExprClass:
1829 return CanSubExprsThrow(C, this);
1830
1831 // Some might be dependent for other reasons.
1832 case UnaryOperatorClass:
1833 case ArraySubscriptExprClass:
1834 case ImplicitCastExprClass:
1835 case CStyleCastExprClass:
1836 case CXXStaticCastExprClass:
1837 case CXXFunctionalCastExprClass:
1838 case BinaryOperatorClass:
1839 case CompoundAssignOperatorClass: {
1840 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1841 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1842 }
1843
1844 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1845 case StmtExprClass:
1846 return CT_Can;
1847
1848 case ChooseExprClass:
1849 if (isTypeDependent() || isValueDependent())
1850 return CT_Dependent;
1851 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1852
Peter Collingbourne91147592011-04-15 00:35:48 +00001853 case GenericSelectionExprClass:
1854 if (cast<GenericSelectionExpr>(this)->isResultDependent())
1855 return CT_Dependent;
1856 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
1857
Sebastian Redlce354af2010-09-10 20:55:33 +00001858 // Some expressions are always dependent.
1859 case DependentScopeDeclRefExprClass:
1860 case CXXUnresolvedConstructExprClass:
1861 case CXXDependentScopeMemberExprClass:
1862 return CT_Dependent;
1863
1864 default:
1865 // All other expressions don't have subexpressions, or else they are
1866 // unevaluated.
1867 return CT_Cannot;
1868 }
1869}
1870
Ted Kremenekfff70962008-01-17 16:57:34 +00001871Expr* Expr::IgnoreParens() {
1872 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00001873 while (true) {
1874 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1875 E = P->getSubExpr();
1876 continue;
1877 }
1878 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1879 if (P->getOpcode() == UO_Extension) {
1880 E = P->getSubExpr();
1881 continue;
1882 }
1883 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001884 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1885 if (!P->isResultDependent()) {
1886 E = P->getResultExpr();
1887 continue;
1888 }
1889 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00001890 return E;
1891 }
Ted Kremenekfff70962008-01-17 16:57:34 +00001892}
1893
Chris Lattnerf2660962008-02-13 01:02:39 +00001894/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1895/// or CastExprs or ImplicitCastExprs, returning their operand.
1896Expr *Expr::IgnoreParenCasts() {
1897 Expr *E = this;
1898 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001899 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001900 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001901 continue;
1902 }
1903 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001904 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001905 continue;
1906 }
1907 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1908 if (P->getOpcode() == UO_Extension) {
1909 E = P->getSubExpr();
1910 continue;
1911 }
1912 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001913 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1914 if (!P->isResultDependent()) {
1915 E = P->getResultExpr();
1916 continue;
1917 }
1918 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00001919 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00001920 }
1921}
1922
John McCall5a4ce8b2010-12-04 08:24:19 +00001923/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1924/// casts. This is intended purely as a temporary workaround for code
1925/// that hasn't yet been rewritten to do the right thing about those
1926/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00001927Expr *Expr::IgnoreParenLValueCasts() {
1928 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00001929 while (true) {
John McCall34376a62010-12-04 03:47:34 +00001930 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1931 E = P->getSubExpr();
1932 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00001933 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00001934 if (P->getCastKind() == CK_LValueToRValue) {
1935 E = P->getSubExpr();
1936 continue;
1937 }
John McCall5a4ce8b2010-12-04 08:24:19 +00001938 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1939 if (P->getOpcode() == UO_Extension) {
1940 E = P->getSubExpr();
1941 continue;
1942 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001943 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1944 if (!P->isResultDependent()) {
1945 E = P->getResultExpr();
1946 continue;
1947 }
John McCall34376a62010-12-04 03:47:34 +00001948 }
1949 break;
1950 }
1951 return E;
1952}
1953
John McCalleebc8322010-05-05 22:59:52 +00001954Expr *Expr::IgnoreParenImpCasts() {
1955 Expr *E = this;
1956 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001957 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001958 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001959 continue;
1960 }
1961 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001962 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001963 continue;
1964 }
1965 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1966 if (P->getOpcode() == UO_Extension) {
1967 E = P->getSubExpr();
1968 continue;
1969 }
1970 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001971 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1972 if (!P->isResultDependent()) {
1973 E = P->getResultExpr();
1974 continue;
1975 }
1976 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00001977 return E;
John McCalleebc8322010-05-05 22:59:52 +00001978 }
1979}
1980
Chris Lattneref26c772009-03-13 17:28:01 +00001981/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1982/// value (including ptr->int casts of the same size). Strip off any
1983/// ParenExpr or CastExprs, returning their operand.
1984Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1985 Expr *E = this;
1986 while (true) {
1987 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1988 E = P->getSubExpr();
1989 continue;
1990 }
Mike Stump11289f42009-09-09 15:08:12 +00001991
Chris Lattneref26c772009-03-13 17:28:01 +00001992 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1993 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00001994 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00001995 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001996
Chris Lattneref26c772009-03-13 17:28:01 +00001997 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1998 E = SE;
1999 continue;
2000 }
Mike Stump11289f42009-09-09 15:08:12 +00002001
Abramo Bagnara932e3932010-10-15 07:51:18 +00002002 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002003 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002004 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002005 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002006 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2007 E = SE;
2008 continue;
2009 }
2010 }
Mike Stump11289f42009-09-09 15:08:12 +00002011
Abramo Bagnara932e3932010-10-15 07:51:18 +00002012 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2013 if (P->getOpcode() == UO_Extension) {
2014 E = P->getSubExpr();
2015 continue;
2016 }
2017 }
2018
Peter Collingbourne91147592011-04-15 00:35:48 +00002019 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2020 if (!P->isResultDependent()) {
2021 E = P->getResultExpr();
2022 continue;
2023 }
2024 }
2025
Chris Lattneref26c772009-03-13 17:28:01 +00002026 return E;
2027 }
2028}
2029
Douglas Gregord196a582009-12-14 19:27:10 +00002030bool Expr::isDefaultArgument() const {
2031 const Expr *E = this;
2032 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2033 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002034
Douglas Gregord196a582009-12-14 19:27:10 +00002035 return isa<CXXDefaultArgExpr>(E);
2036}
Chris Lattneref26c772009-03-13 17:28:01 +00002037
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002038/// \brief Skip over any no-op casts and any temporary-binding
2039/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002040static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002041 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002042 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002043 E = ICE->getSubExpr();
2044 else
2045 break;
2046 }
2047
2048 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2049 E = BE->getSubExpr();
2050
2051 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002052 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002053 E = ICE->getSubExpr();
2054 else
2055 break;
2056 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002057
2058 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002059}
2060
John McCall7a626f62010-09-15 10:14:12 +00002061/// isTemporaryObject - Determines if this expression produces a
2062/// temporary of the given class type.
2063bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2064 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2065 return false;
2066
Anders Carlsson66bbf502010-11-28 16:40:49 +00002067 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002068
John McCall02dc8c72010-09-15 20:59:13 +00002069 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002070 if (!E->Classify(C).isPRValue()) {
2071 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002072 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002073 return false;
2074 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002075
John McCallf4ee1dd2010-09-16 06:57:56 +00002076 // Black-list a few cases which yield pr-values of class type that don't
2077 // refer to temporaries of that type:
2078
2079 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002080 if (isa<ImplicitCastExpr>(E)) {
2081 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2082 case CK_DerivedToBase:
2083 case CK_UncheckedDerivedToBase:
2084 return false;
2085 default:
2086 break;
2087 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002088 }
2089
John McCallf4ee1dd2010-09-16 06:57:56 +00002090 // - member expressions (all)
2091 if (isa<MemberExpr>(E))
2092 return false;
2093
John McCallc07a0c72011-02-17 10:25:35 +00002094 // - opaque values (all)
2095 if (isa<OpaqueValueExpr>(E))
2096 return false;
2097
John McCall7a626f62010-09-15 10:14:12 +00002098 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002099}
2100
Douglas Gregor25b7e052011-03-02 21:06:53 +00002101bool Expr::isImplicitCXXThis() const {
2102 const Expr *E = this;
2103
2104 // Strip away parentheses and casts we don't care about.
2105 while (true) {
2106 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2107 E = Paren->getSubExpr();
2108 continue;
2109 }
2110
2111 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2112 if (ICE->getCastKind() == CK_NoOp ||
2113 ICE->getCastKind() == CK_LValueToRValue ||
2114 ICE->getCastKind() == CK_DerivedToBase ||
2115 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2116 E = ICE->getSubExpr();
2117 continue;
2118 }
2119 }
2120
2121 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2122 if (UnOp->getOpcode() == UO_Extension) {
2123 E = UnOp->getSubExpr();
2124 continue;
2125 }
2126 }
2127
2128 break;
2129 }
2130
2131 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2132 return This->isImplicit();
2133
2134 return false;
2135}
2136
Douglas Gregor4619e432008-12-05 23:32:09 +00002137/// hasAnyTypeDependentArguments - Determines if any of the expressions
2138/// in Exprs is type-dependent.
2139bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2140 for (unsigned I = 0; I < NumExprs; ++I)
2141 if (Exprs[I]->isTypeDependent())
2142 return true;
2143
2144 return false;
2145}
2146
2147/// hasAnyValueDependentArguments - Determines if any of the expressions
2148/// in Exprs is value-dependent.
2149bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2150 for (unsigned I = 0; I < NumExprs; ++I)
2151 if (Exprs[I]->isValueDependent())
2152 return true;
2153
2154 return false;
2155}
2156
John McCall8b0f4ff2010-08-02 21:13:48 +00002157bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002158 // This function is attempting whether an expression is an initializer
2159 // which can be evaluated at compile-time. isEvaluatable handles most
2160 // of the cases, but it can't deal with some initializer-specific
2161 // expressions, and it can't deal with aggregates; we deal with those here,
2162 // and fall back to isEvaluatable for the other cases.
2163
John McCall8b0f4ff2010-08-02 21:13:48 +00002164 // If we ever capture reference-binding directly in the AST, we can
2165 // kill the second parameter.
2166
2167 if (IsForRef) {
2168 EvalResult Result;
2169 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2170 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002171
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002172 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002173 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002174 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002175 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002176 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002177 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002178 case CXXTemporaryObjectExprClass:
2179 case CXXConstructExprClass: {
2180 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002181
2182 // Only if it's
2183 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00002184 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002185 if (!CE->getNumArgs()) return true;
2186
2187 // 2) an elidable trivial copy construction of an operand which is
2188 // itself a constant initializer. Note that we consider the
2189 // operand on its own, *not* as a reference binding.
2190 return CE->isElidable() &&
2191 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00002192 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002193 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002194 // This handles gcc's extension that allows global initializers like
2195 // "struct x {int x;} x = (struct x) {};".
2196 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002197 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002198 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002199 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002200 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002201 // FIXME: This doesn't deal with fields with reference types correctly.
2202 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2203 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002204 const InitListExpr *Exp = cast<InitListExpr>(this);
2205 unsigned numInits = Exp->getNumInits();
2206 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002207 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002208 return false;
2209 }
Eli Friedman384da272009-01-25 03:12:18 +00002210 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002211 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002212 case ImplicitValueInitExprClass:
2213 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002214 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002215 return cast<ParenExpr>(this)->getSubExpr()
2216 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002217 case GenericSelectionExprClass:
2218 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2219 return false;
2220 return cast<GenericSelectionExpr>(this)->getResultExpr()
2221 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002222 case ChooseExprClass:
2223 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2224 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002225 case UnaryOperatorClass: {
2226 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002227 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002228 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002229 break;
2230 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00002231 case BinaryOperatorClass: {
2232 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2233 // but this handles the common case.
2234 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002235 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3eb172a2009-10-13 07:14:16 +00002236 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2237 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2238 return true;
2239 break;
2240 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002241 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002242 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002243 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00002244 case CStyleCastExprClass:
2245 // Handle casts with a destination that's a struct or union; this
2246 // deals with both the gcc no-op struct cast extension and the
2247 // cast-to-union extension.
2248 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002249 return cast<CastExpr>(this)->getSubExpr()
2250 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002251
Chris Lattnera2f9bd52009-10-13 22:12:09 +00002252 // Integer->integer casts can be handled here, which is important for
2253 // things like (int)(&&x-&&y). Scary but true.
2254 if (getType()->isIntegerType() &&
2255 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
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
Eli Friedman384da272009-01-25 03:12:18 +00002259 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002260 }
Eli Friedman384da272009-01-25 03:12:18 +00002261 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002262}
2263
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002264/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2265/// pointer constant or not, as well as the specific kind of constant detected.
2266/// Null pointer constants can be integer constant expressions with the
2267/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2268/// (a GNU extension).
2269Expr::NullPointerConstantKind
2270Expr::isNullPointerConstant(ASTContext &Ctx,
2271 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00002272 if (isValueDependent()) {
2273 switch (NPC) {
2274 case NPC_NeverValueDependent:
2275 assert(false && "Unexpected value dependent expression!");
2276 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002277
Douglas Gregor56751b52009-09-25 04:25:58 +00002278 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002279 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2280 return NPCK_ZeroInteger;
2281 else
2282 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00002283
Douglas Gregor56751b52009-09-25 04:25:58 +00002284 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002285 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00002286 }
2287 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002288
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002289 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002290 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00002291 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002292 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002293 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002294 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002295 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002296 Pointee->isVoidType() && // to void*
2297 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002298 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002299 }
Steve Naroffada7d422007-05-20 17:54:12 +00002300 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002301 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2302 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002303 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002304 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2305 // Accept ((void*)0) as a null pointer constant, as many other
2306 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002307 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00002308 } else if (const GenericSelectionExpr *GE =
2309 dyn_cast<GenericSelectionExpr>(this)) {
2310 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002311 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002312 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002313 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002314 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002315 } else if (isa<GNUNullExpr>(this)) {
2316 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002317 return NPCK_GNUNull;
Steve Naroff09035312008-01-14 02:53:34 +00002318 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002319
Sebastian Redl576fd422009-05-10 18:38:11 +00002320 // C++0x nullptr_t is always a null pointer constant.
2321 if (getType()->isNullPtrType())
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002322 return NPCK_CXX0X_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00002323
Fariborz Jahanian3567c422010-09-27 22:42:37 +00002324 if (const RecordType *UT = getType()->getAsUnionType())
2325 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2326 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2327 const Expr *InitExpr = CLE->getInitializer();
2328 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2329 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2330 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002331 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002332 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002333 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002334 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00002335
Chris Lattner1abbd412007-06-08 17:58:43 +00002336 // If we have an integer constant expression, we need to *evaluate* it and
2337 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002338 llvm::APSInt Result;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002339 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2340
2341 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002342}
Steve Narofff7a5da12007-07-28 23:10:27 +00002343
John McCall34376a62010-12-04 03:47:34 +00002344/// \brief If this expression is an l-value for an Objective C
2345/// property, find the underlying property reference expression.
2346const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2347 const Expr *E = this;
2348 while (true) {
2349 assert((E->getValueKind() == VK_LValue &&
2350 E->getObjectKind() == OK_ObjCProperty) &&
2351 "expression is not a property reference");
2352 E = E->IgnoreParenCasts();
2353 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2354 if (BO->getOpcode() == BO_Comma) {
2355 E = BO->getRHS();
2356 continue;
2357 }
2358 }
2359
2360 break;
2361 }
2362
2363 return cast<ObjCPropertyRefExpr>(E);
2364}
2365
Douglas Gregor71235ec2009-05-02 02:18:30 +00002366FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002367 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002368
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002369 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002370 if (ICE->getCastKind() == CK_LValueToRValue ||
2371 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002372 E = ICE->getSubExpr()->IgnoreParens();
2373 else
2374 break;
2375 }
2376
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002377 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002378 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002379 if (Field->isBitField())
2380 return Field;
2381
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00002382 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2383 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2384 if (Field->isBitField())
2385 return Field;
2386
Douglas Gregor71235ec2009-05-02 02:18:30 +00002387 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2388 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2389 return BinOp->getLHS()->getBitField();
2390
2391 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002392}
2393
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002394bool Expr::refersToVectorElement() const {
2395 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002396
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002397 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002398 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002399 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002400 E = ICE->getSubExpr()->IgnoreParens();
2401 else
2402 break;
2403 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002404
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002405 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2406 return ASE->getBase()->getType()->isVectorType();
2407
2408 if (isa<ExtVectorElementExpr>(E))
2409 return true;
2410
2411 return false;
2412}
2413
Chris Lattnerb8211f62009-02-16 22:14:05 +00002414/// isArrow - Return true if the base expression is a pointer to vector,
2415/// return false if the base expression is a vector.
2416bool ExtVectorElementExpr::isArrow() const {
2417 return getBase()->getType()->isPointerType();
2418}
2419
Nate Begemance4d7fc2008-04-18 23:10:10 +00002420unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002421 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002422 return VT->getNumElements();
2423 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002424}
2425
Nate Begemanf322eab2008-05-09 06:41:27 +00002426/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002427bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002428 // FIXME: Refactor this code to an accessor on the AST node which returns the
2429 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002430 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002431
2432 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002433 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002434 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002435
Nate Begeman7e5185b2009-01-18 02:01:21 +00002436 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002437 if (Comp[0] == 's' || Comp[0] == 'S')
2438 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002439
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002440 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2441 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002442 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002443
Steve Naroff0d595ca2007-07-30 03:29:09 +00002444 return false;
2445}
Chris Lattner885b4952007-08-02 23:36:59 +00002446
Nate Begemanf322eab2008-05-09 06:41:27 +00002447/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002448void ExtVectorElementExpr::getEncodedElementAccess(
2449 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002450 llvm::StringRef Comp = Accessor->getName();
2451 if (Comp[0] == 's' || Comp[0] == 'S')
2452 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002453
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002454 bool isHi = Comp == "hi";
2455 bool isLo = Comp == "lo";
2456 bool isEven = Comp == "even";
2457 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002458
Nate Begemanf322eab2008-05-09 06:41:27 +00002459 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2460 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002461
Nate Begemanf322eab2008-05-09 06:41:27 +00002462 if (isHi)
2463 Index = e + i;
2464 else if (isLo)
2465 Index = i;
2466 else if (isEven)
2467 Index = 2 * i;
2468 else if (isOdd)
2469 Index = 2 * i + 1;
2470 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002471 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002472
Nate Begemand3862152008-05-13 21:03:02 +00002473 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002474 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002475}
2476
Douglas Gregor9a129192010-04-21 00:45:42 +00002477ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002478 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002479 SourceLocation LBracLoc,
2480 SourceLocation SuperLoc,
2481 bool IsInstanceSuper,
2482 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002483 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002484 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002485 ObjCMethodDecl *Method,
2486 Expr **Args, unsigned NumArgs,
2487 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002488 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002489 /*TypeDependent=*/false, /*ValueDependent=*/false,
2490 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002491 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2492 HasMethod(Method != 0), SuperLoc(SuperLoc),
2493 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2494 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002495 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002496{
Douglas Gregor9a129192010-04-21 00:45:42 +00002497 setReceiverPointer(SuperType.getAsOpaquePtr());
2498 if (NumArgs)
2499 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002500}
2501
Douglas Gregor9a129192010-04-21 00:45:42 +00002502ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002503 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002504 SourceLocation LBracLoc,
2505 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002506 Selector Sel,
2507 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002508 ObjCMethodDecl *Method,
2509 Expr **Args, unsigned NumArgs,
2510 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002511 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002512 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002513 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2514 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2515 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002516 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002517{
2518 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002519 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002520 for (unsigned I = 0; I != NumArgs; ++I) {
2521 if (Args[I]->isTypeDependent())
2522 ExprBits.TypeDependent = true;
2523 if (Args[I]->isValueDependent())
2524 ExprBits.ValueDependent = true;
2525 if (Args[I]->containsUnexpandedParameterPack())
2526 ExprBits.ContainsUnexpandedParameterPack = true;
2527
2528 MyArgs[I] = Args[I];
2529 }
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002530}
2531
Douglas Gregor9a129192010-04-21 00:45:42 +00002532ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002533 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002534 SourceLocation LBracLoc,
2535 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002536 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002537 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002538 ObjCMethodDecl *Method,
2539 Expr **Args, unsigned NumArgs,
2540 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002541 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002542 Receiver->isTypeDependent(),
2543 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002544 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2545 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2546 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002547 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002548{
2549 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002550 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002551 for (unsigned I = 0; I != NumArgs; ++I) {
2552 if (Args[I]->isTypeDependent())
2553 ExprBits.TypeDependent = true;
2554 if (Args[I]->isValueDependent())
2555 ExprBits.ValueDependent = true;
2556 if (Args[I]->containsUnexpandedParameterPack())
2557 ExprBits.ContainsUnexpandedParameterPack = true;
2558
2559 MyArgs[I] = Args[I];
2560 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00002561}
2562
Douglas Gregor9a129192010-04-21 00:45:42 +00002563ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002564 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002565 SourceLocation LBracLoc,
2566 SourceLocation SuperLoc,
2567 bool IsInstanceSuper,
2568 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002569 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002570 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002571 ObjCMethodDecl *Method,
2572 Expr **Args, unsigned NumArgs,
2573 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002574 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002575 NumArgs * sizeof(Expr *);
2576 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCall7decc9e2010-11-18 06:31:45 +00002577 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002578 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002579 RBracLoc);
2580}
2581
2582ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002583 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002584 SourceLocation LBracLoc,
2585 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002586 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002587 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002588 ObjCMethodDecl *Method,
2589 Expr **Args, unsigned NumArgs,
2590 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002591 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002592 NumArgs * sizeof(Expr *);
2593 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002594 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2595 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002596}
2597
2598ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002599 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002600 SourceLocation LBracLoc,
2601 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002602 Selector Sel,
2603 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002604 ObjCMethodDecl *Method,
2605 Expr **Args, unsigned NumArgs,
2606 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002607 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002608 NumArgs * sizeof(Expr *);
2609 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002610 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2611 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002612}
2613
Alexis Hunta8136cc2010-05-05 15:23:54 +00002614ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002615 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002616 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002617 NumArgs * sizeof(Expr *);
2618 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2619 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2620}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00002621
2622SourceRange ObjCMessageExpr::getReceiverRange() const {
2623 switch (getReceiverKind()) {
2624 case Instance:
2625 return getInstanceReceiver()->getSourceRange();
2626
2627 case Class:
2628 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2629
2630 case SuperInstance:
2631 case SuperClass:
2632 return getSuperLoc();
2633 }
2634
2635 return SourceLocation();
2636}
2637
Douglas Gregor9a129192010-04-21 00:45:42 +00002638Selector ObjCMessageExpr::getSelector() const {
2639 if (HasMethod)
2640 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2641 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002642 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002643}
2644
2645ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2646 switch (getReceiverKind()) {
2647 case Instance:
2648 if (const ObjCObjectPointerType *Ptr
2649 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2650 return Ptr->getInterfaceDecl();
2651 break;
2652
2653 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00002654 if (const ObjCObjectType *Ty
2655 = getClassReceiver()->getAs<ObjCObjectType>())
2656 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002657 break;
2658
2659 case SuperInstance:
2660 if (const ObjCObjectPointerType *Ptr
2661 = getSuperType()->getAs<ObjCObjectPointerType>())
2662 return Ptr->getInterfaceDecl();
2663 break;
2664
2665 case SuperClass:
Argyrios Kyrtzidis1b9747f2011-01-25 00:03:48 +00002666 if (const ObjCObjectType *Iface
2667 = getSuperType()->getAs<ObjCObjectType>())
2668 return Iface->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002669 break;
2670 }
2671
2672 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002673}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002674
Jay Foad39c79802011-01-12 09:06:06 +00002675bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002676 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002677}
2678
Douglas Gregora6e053e2010-12-15 01:34:56 +00002679ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2680 QualType Type, SourceLocation BLoc,
2681 SourceLocation RP)
2682 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2683 Type->isDependentType(), Type->isDependentType(),
2684 Type->containsUnexpandedParameterPack()),
2685 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2686{
2687 SubExprs = new (C) Stmt*[nexpr];
2688 for (unsigned i = 0; i < nexpr; i++) {
2689 if (args[i]->isTypeDependent())
2690 ExprBits.TypeDependent = true;
2691 if (args[i]->isValueDependent())
2692 ExprBits.ValueDependent = true;
2693 if (args[i]->containsUnexpandedParameterPack())
2694 ExprBits.ContainsUnexpandedParameterPack = true;
2695
2696 SubExprs[i] = args[i];
2697 }
2698}
2699
Nate Begeman48745922009-08-12 02:28:50 +00002700void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2701 unsigned NumExprs) {
2702 if (SubExprs) C.Deallocate(SubExprs);
2703
2704 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002705 this->NumExprs = NumExprs;
2706 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002707}
Nate Begeman48745922009-08-12 02:28:50 +00002708
Peter Collingbourne91147592011-04-15 00:35:48 +00002709GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2710 SourceLocation GenericLoc, Expr *ControllingExpr,
2711 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2712 unsigned NumAssocs, SourceLocation DefaultLoc,
2713 SourceLocation RParenLoc,
2714 bool ContainsUnexpandedParameterPack,
2715 unsigned ResultIndex)
2716 : Expr(GenericSelectionExprClass,
2717 AssocExprs[ResultIndex]->getType(),
2718 AssocExprs[ResultIndex]->getValueKind(),
2719 AssocExprs[ResultIndex]->getObjectKind(),
2720 AssocExprs[ResultIndex]->isTypeDependent(),
2721 AssocExprs[ResultIndex]->isValueDependent(),
2722 ContainsUnexpandedParameterPack),
2723 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2724 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2725 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2726 RParenLoc(RParenLoc) {
2727 SubExprs[CONTROLLING] = ControllingExpr;
2728 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2729 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2730}
2731
2732GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2733 SourceLocation GenericLoc, Expr *ControllingExpr,
2734 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2735 unsigned NumAssocs, SourceLocation DefaultLoc,
2736 SourceLocation RParenLoc,
2737 bool ContainsUnexpandedParameterPack)
2738 : Expr(GenericSelectionExprClass,
2739 Context.DependentTy,
2740 VK_RValue,
2741 OK_Ordinary,
2742 /*isTypeDependent=*/ true,
2743 /*isValueDependent=*/ true,
2744 ContainsUnexpandedParameterPack),
2745 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2746 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2747 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2748 RParenLoc(RParenLoc) {
2749 SubExprs[CONTROLLING] = ControllingExpr;
2750 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2751 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2752}
2753
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002754//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002755// DesignatedInitExpr
2756//===----------------------------------------------------------------------===//
2757
2758IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2759 assert(Kind == FieldDesignator && "Only valid on a field designator");
2760 if (Field.NameOrField & 0x01)
2761 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2762 else
2763 return getField()->getIdentifier();
2764}
2765
Alexis Hunta8136cc2010-05-05 15:23:54 +00002766DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002767 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002768 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002769 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002770 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002771 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002772 unsigned NumIndexExprs,
2773 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002774 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00002775 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002776 Init->isTypeDependent(), Init->isValueDependent(),
2777 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00002778 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2779 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002780 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002781
2782 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00002783 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002784 *Child++ = Init;
2785
2786 // Copy the designators and their subexpressions, computing
2787 // value-dependence along the way.
2788 unsigned IndexIdx = 0;
2789 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002790 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002791
2792 if (this->Designators[I].isArrayDesignator()) {
2793 // Compute type- and value-dependence.
2794 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002795 if (Index->isTypeDependent() || Index->isValueDependent())
2796 ExprBits.ValueDependent = true;
2797
2798 // Propagate unexpanded parameter packs.
2799 if (Index->containsUnexpandedParameterPack())
2800 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002801
2802 // Copy the index expressions into permanent storage.
2803 *Child++ = IndexExprs[IndexIdx++];
2804 } else if (this->Designators[I].isArrayRangeDesignator()) {
2805 // Compute type- and value-dependence.
2806 Expr *Start = IndexExprs[IndexIdx];
2807 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002808 if (Start->isTypeDependent() || Start->isValueDependent() ||
2809 End->isTypeDependent() || End->isValueDependent())
2810 ExprBits.ValueDependent = true;
2811
2812 // Propagate unexpanded parameter packs.
2813 if (Start->containsUnexpandedParameterPack() ||
2814 End->containsUnexpandedParameterPack())
2815 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002816
2817 // Copy the start/end expressions into permanent storage.
2818 *Child++ = IndexExprs[IndexIdx++];
2819 *Child++ = IndexExprs[IndexIdx++];
2820 }
2821 }
2822
2823 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002824}
2825
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002826DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002827DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002828 unsigned NumDesignators,
2829 Expr **IndexExprs, unsigned NumIndexExprs,
2830 SourceLocation ColonOrEqualLoc,
2831 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002832 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002833 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002834 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002835 ColonOrEqualLoc, UsesColonSyntax,
2836 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002837}
2838
Mike Stump11289f42009-09-09 15:08:12 +00002839DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002840 unsigned NumIndexExprs) {
2841 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2842 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2843 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2844}
2845
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002846void DesignatedInitExpr::setDesignators(ASTContext &C,
2847 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002848 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002849 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002850 NumDesignators = NumDesigs;
2851 for (unsigned I = 0; I != NumDesigs; ++I)
2852 Designators[I] = Desigs[I];
2853}
2854
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00002855SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
2856 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
2857 if (size() == 1)
2858 return DIE->getDesignator(0)->getSourceRange();
2859 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
2860 DIE->getDesignator(size()-1)->getEndLocation());
2861}
2862
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002863SourceRange DesignatedInitExpr::getSourceRange() const {
2864 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002865 Designator &First =
2866 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002867 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002868 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002869 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2870 else
2871 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2872 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002873 StartLoc =
2874 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002875 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2876}
2877
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002878Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2879 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2880 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2881 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002882 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2883 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2884}
2885
2886Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002887 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002888 "Requires array range designator");
2889 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2890 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002891 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2892 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2893}
2894
2895Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002896 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002897 "Requires array range designator");
2898 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2899 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002900 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2901 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2902}
2903
Douglas Gregord5846a12009-04-15 06:41:24 +00002904/// \brief Replaces the designator at index @p Idx with the series
2905/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002906void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002907 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002908 const Designator *Last) {
2909 unsigned NumNewDesignators = Last - First;
2910 if (NumNewDesignators == 0) {
2911 std::copy_backward(Designators + Idx + 1,
2912 Designators + NumDesignators,
2913 Designators + Idx);
2914 --NumNewDesignators;
2915 return;
2916 } else if (NumNewDesignators == 1) {
2917 Designators[Idx] = *First;
2918 return;
2919 }
2920
Mike Stump11289f42009-09-09 15:08:12 +00002921 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002922 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002923 std::copy(Designators, Designators + Idx, NewDesignators);
2924 std::copy(First, Last, NewDesignators + Idx);
2925 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2926 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00002927 Designators = NewDesignators;
2928 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2929}
2930
Mike Stump11289f42009-09-09 15:08:12 +00002931ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002932 Expr **exprs, unsigned nexprs,
2933 SourceLocation rparenloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00002934 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2935 false, false, false),
2936 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002937
Nate Begeman5ec4b312009-08-10 23:49:36 +00002938 Exprs = new (C) Stmt*[nexprs];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002939 for (unsigned i = 0; i != nexprs; ++i) {
2940 if (exprs[i]->isTypeDependent())
2941 ExprBits.TypeDependent = true;
2942 if (exprs[i]->isValueDependent())
2943 ExprBits.ValueDependent = true;
2944 if (exprs[i]->containsUnexpandedParameterPack())
2945 ExprBits.ContainsUnexpandedParameterPack = true;
2946
Nate Begeman5ec4b312009-08-10 23:49:36 +00002947 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002948 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00002949}
2950
John McCall1bf58462011-02-16 08:02:54 +00002951const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
2952 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
2953 e = ewc->getSubExpr();
2954 e = cast<CXXConstructExpr>(e)->getArg(0);
2955 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
2956 e = ice->getSubExpr();
2957 return cast<OpaqueValueExpr>(e);
2958}
2959
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002960//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002961// ExprIterator.
2962//===----------------------------------------------------------------------===//
2963
2964Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2965Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2966Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2967const Expr* ConstExprIterator::operator[](size_t idx) const {
2968 return cast<Expr>(I[idx]);
2969}
2970const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2971const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2972
2973//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002974// Child Iterators for iterating over subexpressions/substatements
2975//===----------------------------------------------------------------------===//
2976
Peter Collingbournee190dee2011-03-11 19:24:49 +00002977// UnaryExprOrTypeTraitExpr
2978Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002979 // If this is of a type and the type is a VLA type (and not a typedef), the
2980 // size expression of the VLA needs to be treated as an executable expression.
2981 // Why isn't this weirdness documented better in StmtIterator?
2982 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002983 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00002984 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00002985 return child_range(child_iterator(T), child_iterator());
2986 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00002987 }
John McCallbd066782011-02-09 08:16:59 +00002988 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002989}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002990
Steve Naroffd54978b2007-09-18 23:55:05 +00002991// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00002992Stmt::child_range ObjCMessageExpr::children() {
2993 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00002994 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00002995 begin = reinterpret_cast<Stmt **>(this + 1);
2996 else
2997 begin = reinterpret_cast<Stmt **>(getArgs());
2998 return child_range(begin,
2999 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003000}
3001
Steve Naroffc540d662008-09-03 18:15:37 +00003002// Blocks
John McCall351762c2011-02-07 10:33:21 +00003003BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregor476e3022011-01-19 21:32:01 +00003004 SourceLocation l, bool ByRef,
John McCall351762c2011-02-07 10:33:21 +00003005 bool constAdded)
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003006 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregor476e3022011-01-19 21:32:01 +00003007 d->isParameterPack()),
John McCall351762c2011-02-07 10:33:21 +00003008 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregor476e3022011-01-19 21:32:01 +00003009{
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003010 bool TypeDependent = false;
3011 bool ValueDependent = false;
3012 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
3013 ExprBits.TypeDependent = TypeDependent;
3014 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor476e3022011-01-19 21:32:01 +00003015}
3016