blob: 1c1061b5a2296500d555a2cca76c8364b27e2adf [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 {
38 // If this value has _Bool type, it is obvious 0/1.
39 if (getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000040 // If this is a non-scalar-integer type, we don't care enough to try.
Douglas Gregorb90df602010-06-16 00:17:44 +000041 if (!getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +000042
Chris Lattner4ebae652010-04-16 23:34:13 +000043 if (const ParenExpr *PE = dyn_cast<ParenExpr>(this))
44 return PE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000045
Chris Lattner4ebae652010-04-16 23:34:13 +000046 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(this)) {
47 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +000048 case UO_Plus:
49 case UO_Extension:
Chris Lattner4ebae652010-04-16 23:34:13 +000050 return UO->getSubExpr()->isKnownToHaveBooleanValue();
51 default:
52 return false;
53 }
54 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000055
John McCall45d30c32010-06-12 01:56:02 +000056 // Only look through implicit casts. If the user writes
57 // '(int) (a && b)' treat it as an arbitrary int.
58 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(this))
Chris Lattner4ebae652010-04-16 23:34:13 +000059 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000060
Chris Lattner4ebae652010-04-16 23:34:13 +000061 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(this)) {
62 switch (BO->getOpcode()) {
63 default: return false;
John McCalle3027922010-08-25 11:45:40 +000064 case BO_LT: // Relational operators.
65 case BO_GT:
66 case BO_LE:
67 case BO_GE:
68 case BO_EQ: // Equality operators.
69 case BO_NE:
70 case BO_LAnd: // AND operator.
71 case BO_LOr: // Logical OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +000072 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000073
John McCalle3027922010-08-25 11:45:40 +000074 case BO_And: // Bitwise AND operator.
75 case BO_Xor: // Bitwise XOR operator.
76 case BO_Or: // Bitwise OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +000077 // Handle things like (x==2)|(y==12).
78 return BO->getLHS()->isKnownToHaveBooleanValue() &&
79 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000080
John McCalle3027922010-08-25 11:45:40 +000081 case BO_Comma:
82 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +000083 return BO->getRHS()->isKnownToHaveBooleanValue();
84 }
85 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000086
Chris Lattner4ebae652010-04-16 23:34:13 +000087 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(this))
88 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
89 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000090
Chris Lattner4ebae652010-04-16 23:34:13 +000091 return false;
92}
93
John McCallbd066782011-02-09 08:16:59 +000094// Amusing macro metaprogramming hack: check whether a class provides
95// a more specific implementation of getExprLoc().
96namespace {
97 /// This implementation is used when a class provides a custom
98 /// implementation of getExprLoc.
99 template <class E, class T>
100 SourceLocation getExprLocImpl(const Expr *expr,
101 SourceLocation (T::*v)() const) {
102 return static_cast<const E*>(expr)->getExprLoc();
103 }
104
105 /// This implementation is used when a class doesn't provide
106 /// a custom implementation of getExprLoc. Overload resolution
107 /// should pick it over the implementation above because it's
108 /// more specialized according to function template partial ordering.
109 template <class E>
110 SourceLocation getExprLocImpl(const Expr *expr,
111 SourceLocation (Expr::*v)() const) {
112 return static_cast<const E*>(expr)->getSourceRange().getBegin();
113 }
114}
115
116SourceLocation Expr::getExprLoc() const {
117 switch (getStmtClass()) {
118 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
119#define ABSTRACT_STMT(type)
120#define STMT(type, base) \
121 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
122#define EXPR(type, base) \
123 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
124#include "clang/AST/StmtNodes.inc"
125 }
126 llvm_unreachable("unknown statement kind");
127 return SourceLocation();
128}
129
Chris Lattner0eedafe2006-08-24 04:56:27 +0000130//===----------------------------------------------------------------------===//
131// Primary Expressions.
132//===----------------------------------------------------------------------===//
133
John McCall6b51f282009-11-23 01:53:49 +0000134void ExplicitTemplateArgumentList::initializeFrom(
135 const TemplateArgumentListInfo &Info) {
136 LAngleLoc = Info.getLAngleLoc();
137 RAngleLoc = Info.getRAngleLoc();
138 NumTemplateArgs = Info.size();
139
140 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
141 for (unsigned i = 0; i != NumTemplateArgs; ++i)
142 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
143}
144
Douglas Gregora6e053e2010-12-15 01:34:56 +0000145void ExplicitTemplateArgumentList::initializeFrom(
146 const TemplateArgumentListInfo &Info,
147 bool &Dependent,
148 bool &ContainsUnexpandedParameterPack) {
149 LAngleLoc = Info.getLAngleLoc();
150 RAngleLoc = Info.getRAngleLoc();
151 NumTemplateArgs = Info.size();
152
153 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
154 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
155 Dependent = Dependent || Info[i].getArgument().isDependent();
156 ContainsUnexpandedParameterPack
157 = ContainsUnexpandedParameterPack ||
158 Info[i].getArgument().containsUnexpandedParameterPack();
159
160 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
161 }
162}
163
John McCall6b51f282009-11-23 01:53:49 +0000164void ExplicitTemplateArgumentList::copyInto(
165 TemplateArgumentListInfo &Info) const {
166 Info.setLAngleLoc(LAngleLoc);
167 Info.setRAngleLoc(RAngleLoc);
168 for (unsigned I = 0; I != NumTemplateArgs; ++I)
169 Info.addArgument(getTemplateArgs()[I]);
170}
171
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000172std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
173 return sizeof(ExplicitTemplateArgumentList) +
174 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
175}
176
John McCall6b51f282009-11-23 01:53:49 +0000177std::size_t ExplicitTemplateArgumentList::sizeFor(
178 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000179 return sizeFor(Info.size());
John McCall6b51f282009-11-23 01:53:49 +0000180}
181
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000182/// \brief Compute the type- and value-dependence of a declaration reference
183/// based on the declaration being referenced.
184static void computeDeclRefDependence(NamedDecl *D, QualType T,
185 bool &TypeDependent,
186 bool &ValueDependent) {
187 TypeDependent = false;
188 ValueDependent = false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000189
Douglas Gregored6c7442009-11-23 11:41:28 +0000190
191 // (TD) C++ [temp.dep.expr]p3:
192 // An id-expression is type-dependent if it contains:
193 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000194 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000195 //
196 // (VD) C++ [temp.dep.constexpr]p2:
197 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000198
Douglas Gregored6c7442009-11-23 11:41:28 +0000199 // (TD) - an identifier that was declared with dependent type
200 // (VD) - a name declared with a dependent type,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000201 if (T->isDependentType()) {
202 TypeDependent = true;
203 ValueDependent = true;
204 return;
Douglas Gregored6c7442009-11-23 11:41:28 +0000205 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000206
Douglas Gregored6c7442009-11-23 11:41:28 +0000207 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000208 if (D->getDeclName().getNameKind()
209 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregored6c7442009-11-23 11:41:28 +0000210 D->getDeclName().getCXXNameType()->isDependentType()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000211 TypeDependent = true;
212 ValueDependent = true;
213 return;
Douglas Gregored6c7442009-11-23 11:41:28 +0000214 }
215 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000216 if (isa<NonTypeTemplateParmDecl>(D)) {
217 ValueDependent = true;
218 return;
219 }
220
Douglas Gregored6c7442009-11-23 11:41:28 +0000221 // (VD) - a constant with integral or enumeration type and is
222 // initialized with an expression that is value-dependent.
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000223 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000224 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000225 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000226 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000227 if (Init->isValueDependent())
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000228 ValueDependent = true;
Douglas Gregor0e4de762010-05-11 08:41:30 +0000229 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000230
Douglas Gregor0e4de762010-05-11 08:41:30 +0000231 // (VD) - FIXME: Missing from the standard:
232 // - a member function or a static data member of the current
233 // instantiation
234 else if (Var->isStaticDataMember() &&
Douglas Gregorbe49fc52010-05-11 08:44:04 +0000235 Var->getDeclContext()->isDependentContext())
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000236 ValueDependent = true;
237
238 return;
239 }
240
Douglas Gregor0e4de762010-05-11 08:41:30 +0000241 // (VD) - FIXME: Missing from the standard:
242 // - a member function or a static data member of the current
243 // instantiation
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000244 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
245 ValueDependent = true;
246 return;
247 }
248}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000249
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000250void DeclRefExpr::computeDependence() {
251 bool TypeDependent = false;
252 bool ValueDependent = false;
253 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent);
254
255 // (TD) C++ [temp.dep.expr]p3:
256 // An id-expression is type-dependent if it contains:
257 //
258 // and
259 //
260 // (VD) C++ [temp.dep.constexpr]p2:
261 // An identifier is value-dependent if it is:
262 if (!TypeDependent && !ValueDependent &&
263 hasExplicitTemplateArgs() &&
264 TemplateSpecializationType::anyDependentTemplateArguments(
265 getTemplateArgs(),
266 getNumTemplateArgs())) {
267 TypeDependent = true;
268 ValueDependent = true;
269 }
270
271 ExprBits.TypeDependent = TypeDependent;
272 ExprBits.ValueDependent = ValueDependent;
273
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000274 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000275 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000276 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000277}
278
Alexis Hunta8136cc2010-05-05 15:23:54 +0000279DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000280 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000281 ValueDecl *D, SourceLocation NameLoc,
John McCall6b51f282009-11-23 01:53:49 +0000282 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000283 QualType T, ExprValueKind VK)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000284 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000285 DecoratedD(D,
286 (Qualifier? HasQualifierFlag : 0) |
John McCall6b51f282009-11-23 01:53:49 +0000287 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000288 Loc(NameLoc) {
289 if (Qualifier) {
290 NameQualifier *NQ = getNameQualifier();
291 NQ->NNS = Qualifier;
292 NQ->Range = QualifierRange;
293 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000294
John McCall6b51f282009-11-23 01:53:49 +0000295 if (TemplateArgs)
John McCallb3774b52010-08-19 23:49:38 +0000296 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000297
298 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000299}
300
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000301DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier,
302 SourceRange QualifierRange,
303 ValueDecl *D, const DeclarationNameInfo &NameInfo,
304 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000305 QualType T, ExprValueKind VK)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000306 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000307 DecoratedD(D,
308 (Qualifier? HasQualifierFlag : 0) |
309 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
310 Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
311 if (Qualifier) {
312 NameQualifier *NQ = getNameQualifier();
313 NQ->NNS = Qualifier;
314 NQ->Range = QualifierRange;
315 }
316
317 if (TemplateArgs)
John McCallb3774b52010-08-19 23:49:38 +0000318 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000319
320 computeDependence();
321}
322
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000323DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
324 NestedNameSpecifier *Qualifier,
325 SourceRange QualifierRange,
John McCallce546572009-12-08 09:08:17 +0000326 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000327 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000328 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000329 ExprValueKind VK,
Douglas Gregored6c7442009-11-23 11:41:28 +0000330 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000331 return Create(Context, Qualifier, QualifierRange, D,
332 DeclarationNameInfo(D->getDeclName(), NameLoc),
John McCall7decc9e2010-11-18 06:31:45 +0000333 T, VK, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000334}
335
336DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
337 NestedNameSpecifier *Qualifier,
338 SourceRange QualifierRange,
339 ValueDecl *D,
340 const DeclarationNameInfo &NameInfo,
341 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000342 ExprValueKind VK,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000343 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000344 std::size_t Size = sizeof(DeclRefExpr);
345 if (Qualifier != 0)
346 Size += sizeof(NameQualifier);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000347
John McCall6b51f282009-11-23 01:53:49 +0000348 if (TemplateArgs)
349 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000350
Chris Lattner5c0b4052010-10-30 05:14:06 +0000351 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000352 return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameInfo,
John McCall7decc9e2010-11-18 06:31:45 +0000353 TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000354}
355
Douglas Gregor87866ce2011-02-04 12:01:24 +0000356DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
357 bool HasQualifier,
358 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000359 unsigned NumTemplateArgs) {
360 std::size_t Size = sizeof(DeclRefExpr);
361 if (HasQualifier)
362 Size += sizeof(NameQualifier);
363
Douglas Gregor87866ce2011-02-04 12:01:24 +0000364 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000365 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
366
Chris Lattner5c0b4052010-10-30 05:14:06 +0000367 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000368 return new (Mem) DeclRefExpr(EmptyShell());
369}
370
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000371SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000372 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000373 if (hasQualifier())
374 R.setBegin(getQualifierRange().getBegin());
John McCallb3774b52010-08-19 23:49:38 +0000375 if (hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000376 R.setEnd(getRAngleLoc());
377 return R;
378}
379
Anders Carlsson2fb08242009-09-08 18:24:21 +0000380// FIXME: Maybe this should use DeclPrinter with a special "print predefined
381// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000382std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
383 ASTContext &Context = CurrentDecl->getASTContext();
384
Anders Carlsson2fb08242009-09-08 18:24:21 +0000385 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000386 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000387 return FD->getNameAsString();
388
389 llvm::SmallString<256> Name;
390 llvm::raw_svector_ostream Out(Name);
391
392 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000393 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000394 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000395 if (MD->isStatic())
396 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000397 }
398
399 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000400
401 std::string Proto = FD->getQualifiedNameAsString(Policy);
402
John McCall9dd450b2009-09-21 23:43:11 +0000403 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000404 const FunctionProtoType *FT = 0;
405 if (FD->hasWrittenPrototype())
406 FT = dyn_cast<FunctionProtoType>(AFT);
407
408 Proto += "(";
409 if (FT) {
410 llvm::raw_string_ostream POut(Proto);
411 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
412 if (i) POut << ", ";
413 std::string Param;
414 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
415 POut << Param;
416 }
417
418 if (FT->isVariadic()) {
419 if (FD->getNumParams()) POut << ", ";
420 POut << "...";
421 }
422 }
423 Proto += ")";
424
Sam Weinig4e83bd22009-12-27 01:38:20 +0000425 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
426 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
427 if (ThisQuals.hasConst())
428 Proto += " const";
429 if (ThisQuals.hasVolatile())
430 Proto += " volatile";
431 }
432
Sam Weinigd060ed42009-12-06 23:55:13 +0000433 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
434 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000435
436 Out << Proto;
437
438 Out.flush();
439 return Name.str().str();
440 }
441 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
442 llvm::SmallString<256> Name;
443 llvm::raw_svector_ostream Out(Name);
444 Out << (MD->isInstanceMethod() ? '-' : '+');
445 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000446
447 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
448 // a null check to avoid a crash.
449 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000450 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000451
Anders Carlsson2fb08242009-09-08 18:24:21 +0000452 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000453 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
454 Out << '(' << CID << ')';
455
Anders Carlsson2fb08242009-09-08 18:24:21 +0000456 Out << ' ';
457 Out << MD->getSelector().getAsString();
458 Out << ']';
459
460 Out.flush();
461 return Name.str().str();
462 }
463 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
464 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
465 return "top level";
466 }
467 return "";
468}
469
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000470void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
471 if (hasAllocation())
472 C.Deallocate(pVal);
473
474 BitWidth = Val.getBitWidth();
475 unsigned NumWords = Val.getNumWords();
476 const uint64_t* Words = Val.getRawData();
477 if (NumWords > 1) {
478 pVal = new (C) uint64_t[NumWords];
479 std::copy(Words, Words + NumWords, pVal);
480 } else if (NumWords == 1)
481 VAL = Words[0];
482 else
483 VAL = 0;
484}
485
486IntegerLiteral *
487IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
488 QualType type, SourceLocation l) {
489 return new (C) IntegerLiteral(C, V, type, l);
490}
491
492IntegerLiteral *
493IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
494 return new (C) IntegerLiteral(Empty);
495}
496
497FloatingLiteral *
498FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
499 bool isexact, QualType Type, SourceLocation L) {
500 return new (C) FloatingLiteral(C, V, isexact, Type, L);
501}
502
503FloatingLiteral *
504FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
505 return new (C) FloatingLiteral(Empty);
506}
507
Chris Lattnera0173132008-06-07 22:13:43 +0000508/// getValueAsApproximateDouble - This returns the value as an inaccurate
509/// double. Note that this may cause loss of precision, but is useful for
510/// debugging dumps, etc.
511double FloatingLiteral::getValueAsApproximateDouble() const {
512 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000513 bool ignored;
514 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
515 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000516 return V.convertToDouble();
517}
518
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000519StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
520 unsigned ByteLength, bool Wide,
521 QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000522 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000523 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000524 // Allocate enough space for the StringLiteral plus an array of locations for
525 // any concatenated string tokens.
526 void *Mem = C.Allocate(sizeof(StringLiteral)+
527 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000528 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000529 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000530
Steve Naroffdf7855b2007-02-21 23:46:25 +0000531 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000532 char *AStrData = new (C, 1) char[ByteLength];
533 memcpy(AStrData, StrData, ByteLength);
534 SL->StrData = AStrData;
535 SL->ByteLength = ByteLength;
536 SL->IsWide = Wide;
537 SL->TokLocs[0] = Loc[0];
538 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000539
Chris Lattner630970d2009-02-18 05:49:11 +0000540 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000541 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
542 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000543}
544
Douglas Gregor958dfc92009-04-15 16:35:07 +0000545StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
546 void *Mem = C.Allocate(sizeof(StringLiteral)+
547 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000548 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000549 StringLiteral *SL = new (Mem) StringLiteral(QualType());
550 SL->StrData = 0;
551 SL->ByteLength = 0;
552 SL->NumConcatenated = NumStrs;
553 return SL;
554}
555
Daniel Dunbar36217882009-09-22 03:27:33 +0000556void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbar36217882009-09-22 03:27:33 +0000557 char *AStrData = new (C, 1) char[Str.size()];
558 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000559 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000560 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000561}
562
Chris Lattnere925d612010-11-17 07:37:15 +0000563/// getLocationOfByte - Return a source location that points to the specified
564/// byte of this string literal.
565///
566/// Strings are amazingly complex. They can be formed from multiple tokens and
567/// can have escape sequences in them in addition to the usual trigraph and
568/// escaped newline business. This routine handles this complexity.
569///
570SourceLocation StringLiteral::
571getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
572 const LangOptions &Features, const TargetInfo &Target) const {
573 assert(!isWide() && "This doesn't work for wide strings yet");
574
575 // Loop over all of the tokens in this string until we find the one that
576 // contains the byte we're looking for.
577 unsigned TokNo = 0;
578 while (1) {
579 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
580 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
581
582 // Get the spelling of the string so that we can get the data that makes up
583 // the string literal, not the identifier for the macro it is potentially
584 // expanded through.
585 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
586
587 // Re-lex the token to get its length and original spelling.
588 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
589 bool Invalid = false;
590 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
591 if (Invalid)
592 return StrTokSpellingLoc;
593
594 const char *StrData = Buffer.data()+LocInfo.second;
595
596 // Create a langops struct and enable trigraphs. This is sufficient for
597 // relexing tokens.
598 LangOptions LangOpts;
599 LangOpts.Trigraphs = true;
600
601 // Create a lexer starting at the beginning of this token.
602 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
603 Buffer.end());
604 Token TheTok;
605 TheLexer.LexFromRawLexer(TheTok);
606
607 // Use the StringLiteralParser to compute the length of the string in bytes.
608 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
609 unsigned TokNumBytes = SLP.GetStringLength();
610
611 // If the byte is in this token, return the location of the byte.
612 if (ByteNo < TokNumBytes ||
613 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
614 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
615
616 // Now that we know the offset of the token in the spelling, use the
617 // preprocessor to get the offset in the original source.
618 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
619 }
620
621 // Move to the next string token.
622 ++TokNo;
623 ByteNo -= TokNumBytes;
624 }
625}
626
627
628
Chris Lattner1b926492006-08-23 06:42:10 +0000629/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
630/// corresponds to, e.g. "sizeof" or "[pre]++".
631const char *UnaryOperator::getOpcodeStr(Opcode Op) {
632 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000633 default: assert(0 && "Unknown unary operator");
John McCalle3027922010-08-25 11:45:40 +0000634 case UO_PostInc: return "++";
635 case UO_PostDec: return "--";
636 case UO_PreInc: return "++";
637 case UO_PreDec: return "--";
638 case UO_AddrOf: return "&";
639 case UO_Deref: return "*";
640 case UO_Plus: return "+";
641 case UO_Minus: return "-";
642 case UO_Not: return "~";
643 case UO_LNot: return "!";
644 case UO_Real: return "__real";
645 case UO_Imag: return "__imag";
646 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000647 }
648}
649
John McCalle3027922010-08-25 11:45:40 +0000650UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000651UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
652 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000653 default: assert(false && "No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000654 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
655 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
656 case OO_Amp: return UO_AddrOf;
657 case OO_Star: return UO_Deref;
658 case OO_Plus: return UO_Plus;
659 case OO_Minus: return UO_Minus;
660 case OO_Tilde: return UO_Not;
661 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000662 }
663}
664
665OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
666 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000667 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
668 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
669 case UO_AddrOf: return OO_Amp;
670 case UO_Deref: return OO_Star;
671 case UO_Plus: return OO_Plus;
672 case UO_Minus: return OO_Minus;
673 case UO_Not: return OO_Tilde;
674 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000675 default: return OO_None;
676 }
677}
678
679
Chris Lattner0eedafe2006-08-24 04:56:27 +0000680//===----------------------------------------------------------------------===//
681// Postfix Operators.
682//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000683
Peter Collingbourne3a347252011-02-08 21:18:02 +0000684CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
685 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCall7decc9e2010-11-18 06:31:45 +0000686 SourceLocation rparenloc)
687 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000688 fn->isTypeDependent(),
689 fn->isValueDependent(),
690 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000691 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000692
Peter Collingbourne3a347252011-02-08 21:18:02 +0000693 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +0000694 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000695 for (unsigned i = 0; i != numargs; ++i) {
696 if (args[i]->isTypeDependent())
697 ExprBits.TypeDependent = true;
698 if (args[i]->isValueDependent())
699 ExprBits.ValueDependent = true;
700 if (args[i]->containsUnexpandedParameterPack())
701 ExprBits.ContainsUnexpandedParameterPack = true;
702
Peter Collingbourne3a347252011-02-08 21:18:02 +0000703 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000704 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000705
Peter Collingbourne3a347252011-02-08 21:18:02 +0000706 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +0000707 RParenLoc = rparenloc;
708}
Nate Begeman1e36a852008-01-17 17:46:27 +0000709
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000710CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCall7decc9e2010-11-18 06:31:45 +0000711 QualType t, ExprValueKind VK, SourceLocation rparenloc)
712 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000713 fn->isTypeDependent(),
714 fn->isValueDependent(),
715 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000716 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000717
Peter Collingbourne3a347252011-02-08 21:18:02 +0000718 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000719 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000720 for (unsigned i = 0; i != numargs; ++i) {
721 if (args[i]->isTypeDependent())
722 ExprBits.TypeDependent = true;
723 if (args[i]->isValueDependent())
724 ExprBits.ValueDependent = true;
725 if (args[i]->containsUnexpandedParameterPack())
726 ExprBits.ContainsUnexpandedParameterPack = true;
727
Peter Collingbourne3a347252011-02-08 21:18:02 +0000728 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000729 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000730
Peter Collingbourne3a347252011-02-08 21:18:02 +0000731 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000732 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000733}
734
Mike Stump11289f42009-09-09 15:08:12 +0000735CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
736 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000737 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +0000738 SubExprs = new (C) Stmt*[PREARGS_START];
739 CallExprBits.NumPreArgs = 0;
740}
741
742CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
743 EmptyShell Empty)
744 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
745 // FIXME: Why do we allocate this?
746 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
747 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +0000748}
749
Nuno Lopes518e3702009-12-20 23:11:08 +0000750Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000751 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl2b1832e2010-09-10 20:55:30 +0000752 // If we're calling a dereference, look at the pointer instead.
753 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
754 if (BO->isPtrMemOp())
755 CEE = BO->getRHS()->IgnoreParenCasts();
756 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
757 if (UO->getOpcode() == UO_Deref)
758 CEE = UO->getSubExpr()->IgnoreParenCasts();
759 }
Chris Lattner52301912009-07-17 15:46:27 +0000760 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000761 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000762 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
763 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000764
765 return 0;
766}
767
Nuno Lopes518e3702009-12-20 23:11:08 +0000768FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000769 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000770}
771
Chris Lattnere4407ed2007-12-28 05:25:02 +0000772/// setNumArgs - This changes the number of arguments present in this call.
773/// Any orphaned expressions are deleted by this, and any new operands are set
774/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000775void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000776 // No change, just return.
777 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000778
Chris Lattnere4407ed2007-12-28 05:25:02 +0000779 // If shrinking # arguments, just delete the extras and forgot them.
780 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000781 this->NumArgs = NumArgs;
782 return;
783 }
784
785 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000786 unsigned NumPreArgs = getNumPreArgs();
787 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000788 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000789 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000790 NewSubExprs[i] = SubExprs[i];
791 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000792 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
793 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000794 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000795
Douglas Gregorba6e5572009-04-17 21:46:47 +0000796 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000797 SubExprs = NewSubExprs;
798 this->NumArgs = NumArgs;
799}
800
Chris Lattner01ff98a2008-10-06 05:00:53 +0000801/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
802/// not, return 0.
Jay Foad39c79802011-01-12 09:06:06 +0000803unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000804 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000805 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000806 // ImplicitCastExpr.
807 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
808 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000809 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000810
Steve Narofff6e3b3292008-01-31 01:07:12 +0000811 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
812 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000813 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000814
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000815 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
816 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000817 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000818
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000819 if (!FDecl->getIdentifier())
820 return 0;
821
Douglas Gregor15fc9562009-09-12 00:22:50 +0000822 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000823}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000824
Anders Carlsson00a27592009-05-26 04:57:27 +0000825QualType CallExpr::getCallReturnType() const {
826 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000827 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000828 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000829 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000830 CalleeType = BPT->getPointeeType();
Douglas Gregor603d81b2010-07-13 08:18:22 +0000831 else if (const MemberPointerType *MPT
832 = CalleeType->getAs<MemberPointerType>())
833 CalleeType = MPT->getPointeeType();
834
John McCall9dd450b2009-09-21 23:43:11 +0000835 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000836 return FnType->getResultType();
837}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000838
John McCall701417a2011-02-21 06:23:05 +0000839SourceRange CallExpr::getSourceRange() const {
840 if (isa<CXXOperatorCallExpr>(this))
841 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
842
843 SourceLocation begin = getCallee()->getLocStart();
844 if (begin.isInvalid() && getNumArgs() > 0)
845 begin = getArg(0)->getLocStart();
846 SourceLocation end = getRParenLoc();
847 if (end.isInvalid() && getNumArgs() > 0)
848 end = getArg(getNumArgs() - 1)->getLocEnd();
849 return SourceRange(begin, end);
850}
851
Alexis Hunta8136cc2010-05-05 15:23:54 +0000852OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000853 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000854 TypeSourceInfo *tsi,
855 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000856 Expr** exprsPtr, unsigned numExprs,
857 SourceLocation RParenLoc) {
858 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000859 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000860 sizeof(Expr*) * numExprs);
861
862 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
863 exprsPtr, numExprs, RParenLoc);
864}
865
866OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
867 unsigned numComps, unsigned numExprs) {
868 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
869 sizeof(OffsetOfNode) * numComps +
870 sizeof(Expr*) * numExprs);
871 return new (Mem) OffsetOfExpr(numComps, numExprs);
872}
873
Alexis Hunta8136cc2010-05-05 15:23:54 +0000874OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000875 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000876 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000877 Expr** exprsPtr, unsigned numExprs,
878 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +0000879 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
880 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000881 /*ValueDependent=*/tsi->getType()->isDependentType(),
882 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000883 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
884 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000885{
886 for(unsigned i = 0; i < numComps; ++i) {
887 setComponent(i, compsPtr[i]);
888 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000889
Douglas Gregor882211c2010-04-28 22:16:22 +0000890 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000891 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
892 ExprBits.ValueDependent = true;
893 if (exprsPtr[i]->containsUnexpandedParameterPack())
894 ExprBits.ContainsUnexpandedParameterPack = true;
895
Douglas Gregor882211c2010-04-28 22:16:22 +0000896 setIndexExpr(i, exprsPtr[i]);
897 }
898}
899
900IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
901 assert(getKind() == Field || getKind() == Identifier);
902 if (getKind() == Field)
903 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000904
Douglas Gregor882211c2010-04-28 22:16:22 +0000905 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
906}
907
Mike Stump11289f42009-09-09 15:08:12 +0000908MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
909 NestedNameSpecifier *qual,
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000910 SourceRange qualrange,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000911 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000912 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000913 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +0000914 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +0000915 QualType ty,
916 ExprValueKind vk,
917 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000918 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000919
John McCalla8ae2222010-04-06 21:38:20 +0000920 bool hasQualOrFound = (qual != 0 ||
921 founddecl.getDecl() != memberdecl ||
922 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000923 if (hasQualOrFound)
924 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000925
John McCall6b51f282009-11-23 01:53:49 +0000926 if (targs)
927 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000928
Chris Lattner5c0b4052010-10-30 05:14:06 +0000929 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +0000930 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
931 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +0000932
933 if (hasQualOrFound) {
934 if (qual && qual->isDependent()) {
935 E->setValueDependent(true);
936 E->setTypeDependent(true);
937 }
938 E->HasQualifierOrFoundDecl = true;
939
940 MemberNameQualifier *NQ = E->getMemberQualifier();
941 NQ->NNS = qual;
942 NQ->Range = qualrange;
943 NQ->FoundDecl = founddecl;
944 }
945
946 if (targs) {
947 E->HasExplicitTemplateArgumentList = true;
John McCallb3774b52010-08-19 23:49:38 +0000948 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall16df1e52010-03-30 21:47:33 +0000949 }
950
951 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000952}
953
Anders Carlsson496335e2009-09-03 00:59:21 +0000954const char *CastExpr::getCastKindName() const {
955 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +0000956 case CK_Dependent:
957 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +0000958 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +0000959 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +0000960 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +0000961 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +0000962 case CK_LValueToRValue:
963 return "LValueToRValue";
John McCall34376a62010-12-04 03:47:34 +0000964 case CK_GetObjCProperty:
965 return "GetObjCProperty";
John McCalle3027922010-08-25 11:45:40 +0000966 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +0000967 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +0000968 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +0000969 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +0000970 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +0000971 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000972 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +0000973 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000974 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +0000975 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +0000976 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000977 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +0000978 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000979 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000980 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000981 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000982 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000983 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +0000984 case CK_NullToPointer:
985 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +0000986 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000987 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000988 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000989 return "DerivedToBaseMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000990 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000991 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +0000992 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000993 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +0000994 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000995 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +0000996 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000997 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +0000998 case CK_PointerToBoolean:
999 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001000 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001001 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001002 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001003 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001004 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001005 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001006 case CK_IntegralToBoolean:
1007 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001008 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001009 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001010 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001011 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001012 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001013 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001014 case CK_FloatingToBoolean:
1015 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001016 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001017 return "MemberPointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001018 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahaniane19122f2009-12-08 23:46:15 +00001019 return "AnyPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001020 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001021 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001022 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001023 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001024 case CK_FloatingRealToComplex:
1025 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001026 case CK_FloatingComplexToReal:
1027 return "FloatingComplexToReal";
1028 case CK_FloatingComplexToBoolean:
1029 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001030 case CK_FloatingComplexCast:
1031 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001032 case CK_FloatingComplexToIntegralComplex:
1033 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001034 case CK_IntegralRealToComplex:
1035 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001036 case CK_IntegralComplexToReal:
1037 return "IntegralComplexToReal";
1038 case CK_IntegralComplexToBoolean:
1039 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001040 case CK_IntegralComplexCast:
1041 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001042 case CK_IntegralComplexToFloatingComplex:
1043 return "IntegralComplexToFloatingComplex";
Anders Carlsson496335e2009-09-03 00:59:21 +00001044 }
Mike Stump11289f42009-09-09 15:08:12 +00001045
John McCallc5e62b42010-11-13 09:02:35 +00001046 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001047 return 0;
1048}
1049
Douglas Gregord196a582009-12-14 19:27:10 +00001050Expr *CastExpr::getSubExprAsWritten() {
1051 Expr *SubExpr = 0;
1052 CastExpr *E = this;
1053 do {
1054 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001055
Douglas Gregord196a582009-12-14 19:27:10 +00001056 // Skip any temporary bindings; they're implicit.
1057 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1058 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001059
Douglas Gregord196a582009-12-14 19:27:10 +00001060 // Conversions by constructor and conversion functions have a
1061 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001062 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001063 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001064 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001065 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001066
Douglas Gregord196a582009-12-14 19:27:10 +00001067 // If the subexpression we're left with is an implicit cast, look
1068 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001069 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1070
Douglas Gregord196a582009-12-14 19:27:10 +00001071 return SubExpr;
1072}
1073
John McCallcf142162010-08-07 06:22:56 +00001074CXXBaseSpecifier **CastExpr::path_buffer() {
1075 switch (getStmtClass()) {
1076#define ABSTRACT_STMT(x)
1077#define CASTEXPR(Type, Base) \
1078 case Stmt::Type##Class: \
1079 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1080#define STMT(Type, Base)
1081#include "clang/AST/StmtNodes.inc"
1082 default:
1083 llvm_unreachable("non-cast expressions not possible here");
1084 return 0;
1085 }
1086}
1087
1088void CastExpr::setCastPath(const CXXCastPath &Path) {
1089 assert(Path.size() == path_size());
1090 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1091}
1092
1093ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1094 CastKind Kind, Expr *Operand,
1095 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001096 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001097 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1098 void *Buffer =
1099 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1100 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001101 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001102 if (PathSize) E->setCastPath(*BasePath);
1103 return E;
1104}
1105
1106ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1107 unsigned PathSize) {
1108 void *Buffer =
1109 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1110 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1111}
1112
1113
1114CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001115 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001116 const CXXCastPath *BasePath,
1117 TypeSourceInfo *WrittenTy,
1118 SourceLocation L, SourceLocation R) {
1119 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1120 void *Buffer =
1121 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1122 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001123 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001124 if (PathSize) E->setCastPath(*BasePath);
1125 return E;
1126}
1127
1128CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1129 void *Buffer =
1130 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1131 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1132}
1133
Chris Lattner1b926492006-08-23 06:42:10 +00001134/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1135/// corresponds to, e.g. "<<=".
1136const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1137 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001138 case BO_PtrMemD: return ".*";
1139 case BO_PtrMemI: return "->*";
1140 case BO_Mul: return "*";
1141 case BO_Div: return "/";
1142 case BO_Rem: return "%";
1143 case BO_Add: return "+";
1144 case BO_Sub: return "-";
1145 case BO_Shl: return "<<";
1146 case BO_Shr: return ">>";
1147 case BO_LT: return "<";
1148 case BO_GT: return ">";
1149 case BO_LE: return "<=";
1150 case BO_GE: return ">=";
1151 case BO_EQ: return "==";
1152 case BO_NE: return "!=";
1153 case BO_And: return "&";
1154 case BO_Xor: return "^";
1155 case BO_Or: return "|";
1156 case BO_LAnd: return "&&";
1157 case BO_LOr: return "||";
1158 case BO_Assign: return "=";
1159 case BO_MulAssign: return "*=";
1160 case BO_DivAssign: return "/=";
1161 case BO_RemAssign: return "%=";
1162 case BO_AddAssign: return "+=";
1163 case BO_SubAssign: return "-=";
1164 case BO_ShlAssign: return "<<=";
1165 case BO_ShrAssign: return ">>=";
1166 case BO_AndAssign: return "&=";
1167 case BO_XorAssign: return "^=";
1168 case BO_OrAssign: return "|=";
1169 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001170 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001171
1172 return "";
Chris Lattner1b926492006-08-23 06:42:10 +00001173}
Steve Naroff47500512007-04-19 23:00:49 +00001174
John McCalle3027922010-08-25 11:45:40 +00001175BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001176BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1177 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +00001178 default: assert(false && "Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001179 case OO_Plus: return BO_Add;
1180 case OO_Minus: return BO_Sub;
1181 case OO_Star: return BO_Mul;
1182 case OO_Slash: return BO_Div;
1183 case OO_Percent: return BO_Rem;
1184 case OO_Caret: return BO_Xor;
1185 case OO_Amp: return BO_And;
1186 case OO_Pipe: return BO_Or;
1187 case OO_Equal: return BO_Assign;
1188 case OO_Less: return BO_LT;
1189 case OO_Greater: return BO_GT;
1190 case OO_PlusEqual: return BO_AddAssign;
1191 case OO_MinusEqual: return BO_SubAssign;
1192 case OO_StarEqual: return BO_MulAssign;
1193 case OO_SlashEqual: return BO_DivAssign;
1194 case OO_PercentEqual: return BO_RemAssign;
1195 case OO_CaretEqual: return BO_XorAssign;
1196 case OO_AmpEqual: return BO_AndAssign;
1197 case OO_PipeEqual: return BO_OrAssign;
1198 case OO_LessLess: return BO_Shl;
1199 case OO_GreaterGreater: return BO_Shr;
1200 case OO_LessLessEqual: return BO_ShlAssign;
1201 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1202 case OO_EqualEqual: return BO_EQ;
1203 case OO_ExclaimEqual: return BO_NE;
1204 case OO_LessEqual: return BO_LE;
1205 case OO_GreaterEqual: return BO_GE;
1206 case OO_AmpAmp: return BO_LAnd;
1207 case OO_PipePipe: return BO_LOr;
1208 case OO_Comma: return BO_Comma;
1209 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001210 }
1211}
1212
1213OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1214 static const OverloadedOperatorKind OverOps[] = {
1215 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1216 OO_Star, OO_Slash, OO_Percent,
1217 OO_Plus, OO_Minus,
1218 OO_LessLess, OO_GreaterGreater,
1219 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1220 OO_EqualEqual, OO_ExclaimEqual,
1221 OO_Amp,
1222 OO_Caret,
1223 OO_Pipe,
1224 OO_AmpAmp,
1225 OO_PipePipe,
1226 OO_Equal, OO_StarEqual,
1227 OO_SlashEqual, OO_PercentEqual,
1228 OO_PlusEqual, OO_MinusEqual,
1229 OO_LessLessEqual, OO_GreaterGreaterEqual,
1230 OO_AmpEqual, OO_CaretEqual,
1231 OO_PipeEqual,
1232 OO_Comma
1233 };
1234 return OverOps[Opc];
1235}
1236
Ted Kremenekac034612010-04-13 23:39:13 +00001237InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +00001238 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001239 SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001240 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1241 false),
Ted Kremenekac034612010-04-13 23:39:13 +00001242 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +00001243 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001244 UnionFieldInit(0), HadArrayRangeDesignator(false)
1245{
Ted Kremenek013041e2010-02-19 01:50:18 +00001246 for (unsigned I = 0; I != numInits; ++I) {
1247 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001248 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001249 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001250 ExprBits.ValueDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001251 if (initExprs[I]->containsUnexpandedParameterPack())
1252 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001253 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001254
Ted Kremenekac034612010-04-13 23:39:13 +00001255 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001256}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001257
Ted Kremenekac034612010-04-13 23:39:13 +00001258void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001259 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001260 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001261}
1262
Ted Kremenekac034612010-04-13 23:39:13 +00001263void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001264 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001265}
1266
Ted Kremenekac034612010-04-13 23:39:13 +00001267Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001268 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001269 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001270 InitExprs.back() = expr;
1271 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001272 }
Mike Stump11289f42009-09-09 15:08:12 +00001273
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001274 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1275 InitExprs[Init] = expr;
1276 return Result;
1277}
1278
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001279SourceRange InitListExpr::getSourceRange() const {
1280 if (SyntacticForm)
1281 return SyntacticForm->getSourceRange();
1282 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1283 if (Beg.isInvalid()) {
1284 // Find the first non-null initializer.
1285 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1286 E = InitExprs.end();
1287 I != E; ++I) {
1288 if (Stmt *S = *I) {
1289 Beg = S->getLocStart();
1290 break;
1291 }
1292 }
1293 }
1294 if (End.isInvalid()) {
1295 // Find the first non-null initializer from the end.
1296 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1297 E = InitExprs.rend();
1298 I != E; ++I) {
1299 if (Stmt *S = *I) {
1300 End = S->getSourceRange().getEnd();
1301 break;
1302 }
1303 }
1304 }
1305 return SourceRange(Beg, End);
1306}
1307
Steve Naroff991e99d2008-09-04 15:31:07 +00001308/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001309///
1310const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001311 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +00001312 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001313}
1314
Mike Stump11289f42009-09-09 15:08:12 +00001315SourceLocation BlockExpr::getCaretLocation() const {
1316 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001317}
Mike Stump11289f42009-09-09 15:08:12 +00001318const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001319 return TheBlock->getBody();
1320}
Mike Stump11289f42009-09-09 15:08:12 +00001321Stmt *BlockExpr::getBody() {
1322 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001323}
Steve Naroff415d3d52008-10-08 17:01:13 +00001324
1325
Chris Lattner1ec5f562007-06-27 05:38:08 +00001326//===----------------------------------------------------------------------===//
1327// Generic Expression Routines
1328//===----------------------------------------------------------------------===//
1329
Chris Lattner237f2752009-02-14 07:37:35 +00001330/// isUnusedResultAWarning - Return true if this immediate expression should
1331/// be warned about if the result is unused. If so, fill in Loc and Ranges
1332/// with location to warn on and the source range[s] to report with the
1333/// warning.
1334bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +00001335 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001336 // Don't warn if the expr is type dependent. The type could end up
1337 // instantiating to void.
1338 if (isTypeDependent())
1339 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001340
Chris Lattner1ec5f562007-06-27 05:38:08 +00001341 switch (getStmtClass()) {
1342 default:
John McCallc493a732010-03-12 07:11:26 +00001343 if (getType()->isVoidType())
1344 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001345 Loc = getExprLoc();
1346 R1 = getSourceRange();
1347 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001348 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001349 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +00001350 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001351 case UnaryOperatorClass: {
1352 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001353
Chris Lattner1ec5f562007-06-27 05:38:08 +00001354 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001355 default: break;
John McCalle3027922010-08-25 11:45:40 +00001356 case UO_PostInc:
1357 case UO_PostDec:
1358 case UO_PreInc:
1359 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001360 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001361 case UO_Deref:
Chris Lattnera44d1162007-06-27 05:58:59 +00001362 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001363 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001364 return false;
1365 break;
John McCalle3027922010-08-25 11:45:40 +00001366 case UO_Real:
1367 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001368 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001369 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1370 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001371 return false;
1372 break;
John McCalle3027922010-08-25 11:45:40 +00001373 case UO_Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001374 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001375 }
Chris Lattner237f2752009-02-14 07:37:35 +00001376 Loc = UO->getOperatorLoc();
1377 R1 = UO->getSubExpr()->getSourceRange();
1378 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001379 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001380 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001381 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001382 switch (BO->getOpcode()) {
1383 default:
1384 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001385 // Consider the RHS of comma for side effects. LHS was checked by
1386 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001387 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001388 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1389 // lvalue-ness) of an assignment written in a macro.
1390 if (IntegerLiteral *IE =
1391 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1392 if (IE->getValue() == 0)
1393 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001394 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1395 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001396 case BO_LAnd:
1397 case BO_LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001398 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1399 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1400 return false;
1401 break;
John McCall1e3715a2010-02-16 04:10:53 +00001402 }
Chris Lattner237f2752009-02-14 07:37:35 +00001403 if (BO->isAssignmentOp())
1404 return false;
1405 Loc = BO->getOperatorLoc();
1406 R1 = BO->getLHS()->getSourceRange();
1407 R2 = BO->getRHS()->getSourceRange();
1408 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001409 }
Chris Lattner86928112007-08-25 02:00:02 +00001410 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001411 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001412 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001413
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001414 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001415 // The condition must be evaluated, but if either the LHS or RHS is a
1416 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001417 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001418 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +00001419 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +00001420 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +00001421 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001422 }
1423
Chris Lattnera44d1162007-06-27 05:58:59 +00001424 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001425 // If the base pointer or element is to a volatile pointer/field, accessing
1426 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001427 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001428 return false;
1429 Loc = cast<MemberExpr>(this)->getMemberLoc();
1430 R1 = SourceRange(Loc, Loc);
1431 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1432 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001433
Chris Lattner1ec5f562007-06-27 05:38:08 +00001434 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001435 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001436 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001437 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001438 return false;
1439 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1440 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1441 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1442 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001443
Chris Lattner1ec5f562007-06-27 05:38:08 +00001444 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001445 case CXXOperatorCallExprClass:
1446 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001447 // If this is a direct call, get the callee.
1448 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001449 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001450 // If the callee has attribute pure, const, or warn_unused_result, warn
1451 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001452 //
1453 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1454 // updated to match for QoI.
1455 if (FD->getAttr<WarnUnusedResultAttr>() ||
1456 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1457 Loc = CE->getCallee()->getLocStart();
1458 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001459
Chris Lattner1a6babf2009-10-13 04:53:48 +00001460 if (unsigned NumArgs = CE->getNumArgs())
1461 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1462 CE->getArg(NumArgs-1)->getLocEnd());
1463 return true;
1464 }
Chris Lattner237f2752009-02-14 07:37:35 +00001465 }
1466 return false;
1467 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001468
1469 case CXXTemporaryObjectExprClass:
1470 case CXXConstructExprClass:
1471 return false;
1472
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001473 case ObjCMessageExprClass: {
1474 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1475 const ObjCMethodDecl *MD = ME->getMethodDecl();
1476 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1477 Loc = getExprLoc();
1478 return true;
1479 }
Chris Lattner237f2752009-02-14 07:37:35 +00001480 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001481 }
Mike Stump11289f42009-09-09 15:08:12 +00001482
John McCallb7bd14f2010-12-02 01:19:52 +00001483 case ObjCPropertyRefExprClass:
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001484 Loc = getExprLoc();
1485 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001486 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00001487
Chris Lattner944d3062008-07-26 19:51:01 +00001488 case StmtExprClass: {
1489 // Statement exprs don't logically have side effects themselves, but are
1490 // sometimes used in macros in ways that give them a type that is unused.
1491 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1492 // however, if the result of the stmt expr is dead, we don't want to emit a
1493 // warning.
1494 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001495 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00001496 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001497 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001498 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1499 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1500 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1501 }
Mike Stump11289f42009-09-09 15:08:12 +00001502
John McCallc493a732010-03-12 07:11:26 +00001503 if (getType()->isVoidType())
1504 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001505 Loc = cast<StmtExpr>(this)->getLParenLoc();
1506 R1 = getSourceRange();
1507 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001508 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001509 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001510 // If this is an explicit cast to void, allow it. People do this when they
1511 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001512 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001513 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001514 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1515 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1516 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001517 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001518 if (getType()->isVoidType())
1519 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001520 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001521
Anders Carlsson6aa50392009-11-17 17:11:23 +00001522 // If this is a cast to void or a constructor conversion, check the operand.
1523 // Otherwise, the result of the cast is unused.
John McCalle3027922010-08-25 11:45:40 +00001524 if (CE->getCastKind() == CK_ToVoid ||
1525 CE->getCastKind() == CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001526 return (cast<CastExpr>(this)->getSubExpr()
1527 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001528 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1529 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1530 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001531 }
Mike Stump11289f42009-09-09 15:08:12 +00001532
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001533 case ImplicitCastExprClass:
1534 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001535 return (cast<ImplicitCastExpr>(this)
1536 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001537
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001538 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001539 return (cast<CXXDefaultArgExpr>(this)
1540 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001541
1542 case CXXNewExprClass:
1543 // FIXME: In theory, there might be new expressions that don't have side
1544 // effects (e.g. a placement new with an uninitialized POD).
1545 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001546 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001547 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001548 return (cast<CXXBindTemporaryExpr>(this)
1549 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00001550 case ExprWithCleanupsClass:
1551 return (cast<ExprWithCleanups>(this)
Mike Stump53f9ded2009-11-03 23:25:48 +00001552 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001553 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001554}
1555
Fariborz Jahanian07735332009-02-22 18:40:18 +00001556/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001557/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001558bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001559 switch (getStmtClass()) {
1560 default:
1561 return false;
1562 case ObjCIvarRefExprClass:
1563 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001564 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001565 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001566 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001567 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001568 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001569 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001570 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001571 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001572 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001573 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001574 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1575 if (VD->hasGlobalStorage())
1576 return true;
1577 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001578 // dereferencing to a pointer is always a gc'able candidate,
1579 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001580 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001581 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001582 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001583 return false;
1584 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001585 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001586 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001587 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001588 }
1589 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001590 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001591 }
1592}
Sebastian Redlce354af2010-09-10 20:55:33 +00001593
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001594bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1595 if (isTypeDependent())
1596 return false;
John McCall086a4642010-11-24 05:12:34 +00001597 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001598}
1599
Sebastian Redlce354af2010-09-10 20:55:33 +00001600static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1601 Expr::CanThrowResult CT2) {
1602 // CanThrowResult constants are ordered so that the maximum is the correct
1603 // merge result.
1604 return CT1 > CT2 ? CT1 : CT2;
1605}
1606
1607static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1608 Expr *E = const_cast<Expr*>(CE);
1609 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall8322c3a2011-02-13 04:07:26 +00001610 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redlce354af2010-09-10 20:55:33 +00001611 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1612 }
1613 return R;
1614}
1615
1616static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1617 bool NullThrows = true) {
1618 if (!D)
1619 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1620
1621 // See if we can get a function type from the decl somehow.
1622 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1623 if (!VD) // If we have no clue what we're calling, assume the worst.
1624 return Expr::CT_Can;
1625
Sebastian Redlb8a76c42010-09-10 22:34:40 +00001626 // As an extension, we assume that __attribute__((nothrow)) functions don't
1627 // throw.
1628 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1629 return Expr::CT_Cannot;
1630
Sebastian Redlce354af2010-09-10 20:55:33 +00001631 QualType T = VD->getType();
1632 const FunctionProtoType *FT;
1633 if ((FT = T->getAs<FunctionProtoType>())) {
1634 } else if (const PointerType *PT = T->getAs<PointerType>())
1635 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1636 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1637 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1638 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1639 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1640 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1641 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1642
1643 if (!FT)
1644 return Expr::CT_Can;
1645
1646 return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1647}
1648
1649static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1650 if (DC->isTypeDependent())
1651 return Expr::CT_Dependent;
1652
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001653 if (!DC->getTypeAsWritten()->isReferenceType())
1654 return Expr::CT_Cannot;
1655
Sebastian Redlce354af2010-09-10 20:55:33 +00001656 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1657}
1658
1659static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1660 const CXXTypeidExpr *DC) {
1661 if (DC->isTypeOperand())
1662 return Expr::CT_Cannot;
1663
1664 Expr *Op = DC->getExprOperand();
1665 if (Op->isTypeDependent())
1666 return Expr::CT_Dependent;
1667
1668 const RecordType *RT = Op->getType()->getAs<RecordType>();
1669 if (!RT)
1670 return Expr::CT_Cannot;
1671
1672 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1673 return Expr::CT_Cannot;
1674
1675 if (Op->Classify(C).isPRValue())
1676 return Expr::CT_Cannot;
1677
1678 return Expr::CT_Can;
1679}
1680
1681Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1682 // C++ [expr.unary.noexcept]p3:
1683 // [Can throw] if in a potentially-evaluated context the expression would
1684 // contain:
1685 switch (getStmtClass()) {
1686 case CXXThrowExprClass:
1687 // - a potentially evaluated throw-expression
1688 return CT_Can;
1689
1690 case CXXDynamicCastExprClass: {
1691 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1692 // where T is a reference type, that requires a run-time check
1693 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1694 if (CT == CT_Can)
1695 return CT;
1696 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1697 }
1698
1699 case CXXTypeidExprClass:
1700 // - a potentially evaluated typeid expression applied to a glvalue
1701 // expression whose type is a polymorphic class type
1702 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1703
1704 // - a potentially evaluated call to a function, member function, function
1705 // pointer, or member function pointer that does not have a non-throwing
1706 // exception-specification
1707 case CallExprClass:
1708 case CXXOperatorCallExprClass:
1709 case CXXMemberCallExprClass: {
1710 CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1711 if (CT == CT_Can)
1712 return CT;
1713 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1714 }
1715
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001716 case CXXConstructExprClass:
1717 case CXXTemporaryObjectExprClass: {
Sebastian Redlce354af2010-09-10 20:55:33 +00001718 CanThrowResult CT = CanCalleeThrow(
1719 cast<CXXConstructExpr>(this)->getConstructor());
1720 if (CT == CT_Can)
1721 return CT;
1722 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1723 }
1724
1725 case CXXNewExprClass: {
1726 CanThrowResult CT = MergeCanThrow(
1727 CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1728 CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1729 /*NullThrows*/false));
1730 if (CT == CT_Can)
1731 return CT;
1732 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1733 }
1734
1735 case CXXDeleteExprClass: {
Sebastian Redlce354af2010-09-10 20:55:33 +00001736 CanThrowResult CT = CanCalleeThrow(
1737 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1738 if (CT == CT_Can)
1739 return CT;
Sebastian Redla8bac372010-09-10 23:27:10 +00001740 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1741 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1742 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1743 Arg = Cast->getSubExpr();
1744 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1745 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
1746 CanThrowResult CT2 = CanCalleeThrow(
1747 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1748 if (CT2 == CT_Can)
1749 return CT2;
1750 CT = MergeCanThrow(CT, CT2);
1751 }
1752 }
1753 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1754 }
1755
1756 case CXXBindTemporaryExprClass: {
1757 // The bound temporary has to be destroyed again, which might throw.
1758 CanThrowResult CT = CanCalleeThrow(
1759 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1760 if (CT == CT_Can)
1761 return CT;
Sebastian Redlce354af2010-09-10 20:55:33 +00001762 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1763 }
1764
1765 // ObjC message sends are like function calls, but never have exception
1766 // specs.
1767 case ObjCMessageExprClass:
1768 case ObjCPropertyRefExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001769 return CT_Can;
1770
1771 // Many other things have subexpressions, so we have to test those.
1772 // Some are simple:
1773 case ParenExprClass:
1774 case MemberExprClass:
1775 case CXXReinterpretCastExprClass:
1776 case CXXConstCastExprClass:
1777 case ConditionalOperatorClass:
1778 case CompoundLiteralExprClass:
1779 case ExtVectorElementExprClass:
1780 case InitListExprClass:
1781 case DesignatedInitExprClass:
1782 case ParenListExprClass:
1783 case VAArgExprClass:
1784 case CXXDefaultArgExprClass:
John McCall5d413782010-12-06 08:20:24 +00001785 case ExprWithCleanupsClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001786 case ObjCIvarRefExprClass:
1787 case ObjCIsaExprClass:
1788 case ShuffleVectorExprClass:
1789 return CanSubExprsThrow(C, this);
1790
1791 // Some might be dependent for other reasons.
1792 case UnaryOperatorClass:
1793 case ArraySubscriptExprClass:
1794 case ImplicitCastExprClass:
1795 case CStyleCastExprClass:
1796 case CXXStaticCastExprClass:
1797 case CXXFunctionalCastExprClass:
1798 case BinaryOperatorClass:
1799 case CompoundAssignOperatorClass: {
1800 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1801 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1802 }
1803
1804 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1805 case StmtExprClass:
1806 return CT_Can;
1807
1808 case ChooseExprClass:
1809 if (isTypeDependent() || isValueDependent())
1810 return CT_Dependent;
1811 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1812
1813 // Some expressions are always dependent.
1814 case DependentScopeDeclRefExprClass:
1815 case CXXUnresolvedConstructExprClass:
1816 case CXXDependentScopeMemberExprClass:
1817 return CT_Dependent;
1818
1819 default:
1820 // All other expressions don't have subexpressions, or else they are
1821 // unevaluated.
1822 return CT_Cannot;
1823 }
1824}
1825
Ted Kremenekfff70962008-01-17 16:57:34 +00001826Expr* Expr::IgnoreParens() {
1827 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00001828 while (true) {
1829 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1830 E = P->getSubExpr();
1831 continue;
1832 }
1833 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1834 if (P->getOpcode() == UO_Extension) {
1835 E = P->getSubExpr();
1836 continue;
1837 }
1838 }
1839 return E;
1840 }
Ted Kremenekfff70962008-01-17 16:57:34 +00001841}
1842
Chris Lattnerf2660962008-02-13 01:02:39 +00001843/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1844/// or CastExprs or ImplicitCastExprs, returning their operand.
1845Expr *Expr::IgnoreParenCasts() {
1846 Expr *E = this;
1847 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001848 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001849 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001850 continue;
1851 }
1852 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001853 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001854 continue;
1855 }
1856 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1857 if (P->getOpcode() == UO_Extension) {
1858 E = P->getSubExpr();
1859 continue;
1860 }
1861 }
1862 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00001863 }
1864}
1865
John McCall5a4ce8b2010-12-04 08:24:19 +00001866/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1867/// casts. This is intended purely as a temporary workaround for code
1868/// that hasn't yet been rewritten to do the right thing about those
1869/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00001870Expr *Expr::IgnoreParenLValueCasts() {
1871 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00001872 while (true) {
John McCall34376a62010-12-04 03:47:34 +00001873 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1874 E = P->getSubExpr();
1875 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00001876 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00001877 if (P->getCastKind() == CK_LValueToRValue) {
1878 E = P->getSubExpr();
1879 continue;
1880 }
John McCall5a4ce8b2010-12-04 08:24:19 +00001881 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1882 if (P->getOpcode() == UO_Extension) {
1883 E = P->getSubExpr();
1884 continue;
1885 }
John McCall34376a62010-12-04 03:47:34 +00001886 }
1887 break;
1888 }
1889 return E;
1890}
1891
John McCalleebc8322010-05-05 22:59:52 +00001892Expr *Expr::IgnoreParenImpCasts() {
1893 Expr *E = this;
1894 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001895 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001896 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001897 continue;
1898 }
1899 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001900 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001901 continue;
1902 }
1903 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1904 if (P->getOpcode() == UO_Extension) {
1905 E = P->getSubExpr();
1906 continue;
1907 }
1908 }
1909 return E;
John McCalleebc8322010-05-05 22:59:52 +00001910 }
1911}
1912
Chris Lattneref26c772009-03-13 17:28:01 +00001913/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1914/// value (including ptr->int casts of the same size). Strip off any
1915/// ParenExpr or CastExprs, returning their operand.
1916Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1917 Expr *E = this;
1918 while (true) {
1919 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1920 E = P->getSubExpr();
1921 continue;
1922 }
Mike Stump11289f42009-09-09 15:08:12 +00001923
Chris Lattneref26c772009-03-13 17:28:01 +00001924 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1925 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00001926 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00001927 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001928
Chris Lattneref26c772009-03-13 17:28:01 +00001929 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1930 E = SE;
1931 continue;
1932 }
Mike Stump11289f42009-09-09 15:08:12 +00001933
Abramo Bagnara932e3932010-10-15 07:51:18 +00001934 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00001935 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00001936 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00001937 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00001938 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1939 E = SE;
1940 continue;
1941 }
1942 }
Mike Stump11289f42009-09-09 15:08:12 +00001943
Abramo Bagnara932e3932010-10-15 07:51:18 +00001944 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1945 if (P->getOpcode() == UO_Extension) {
1946 E = P->getSubExpr();
1947 continue;
1948 }
1949 }
1950
Chris Lattneref26c772009-03-13 17:28:01 +00001951 return E;
1952 }
1953}
1954
Douglas Gregord196a582009-12-14 19:27:10 +00001955bool Expr::isDefaultArgument() const {
1956 const Expr *E = this;
1957 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1958 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001959
Douglas Gregord196a582009-12-14 19:27:10 +00001960 return isa<CXXDefaultArgExpr>(E);
1961}
Chris Lattneref26c772009-03-13 17:28:01 +00001962
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001963/// \brief Skip over any no-op casts and any temporary-binding
1964/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00001965static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001966 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00001967 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001968 E = ICE->getSubExpr();
1969 else
1970 break;
1971 }
1972
1973 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1974 E = BE->getSubExpr();
1975
1976 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00001977 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001978 E = ICE->getSubExpr();
1979 else
1980 break;
1981 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00001982
1983 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001984}
1985
John McCall7a626f62010-09-15 10:14:12 +00001986/// isTemporaryObject - Determines if this expression produces a
1987/// temporary of the given class type.
1988bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1989 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1990 return false;
1991
Anders Carlsson66bbf502010-11-28 16:40:49 +00001992 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001993
John McCall02dc8c72010-09-15 20:59:13 +00001994 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00001995 if (!E->Classify(C).isPRValue()) {
1996 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00001997 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00001998 return false;
1999 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002000
John McCallf4ee1dd2010-09-16 06:57:56 +00002001 // Black-list a few cases which yield pr-values of class type that don't
2002 // refer to temporaries of that type:
2003
2004 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002005 if (isa<ImplicitCastExpr>(E)) {
2006 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2007 case CK_DerivedToBase:
2008 case CK_UncheckedDerivedToBase:
2009 return false;
2010 default:
2011 break;
2012 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002013 }
2014
John McCallf4ee1dd2010-09-16 06:57:56 +00002015 // - member expressions (all)
2016 if (isa<MemberExpr>(E))
2017 return false;
2018
John McCallc07a0c72011-02-17 10:25:35 +00002019 // - opaque values (all)
2020 if (isa<OpaqueValueExpr>(E))
2021 return false;
2022
John McCall7a626f62010-09-15 10:14:12 +00002023 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002024}
2025
Douglas Gregor4619e432008-12-05 23:32:09 +00002026/// hasAnyTypeDependentArguments - Determines if any of the expressions
2027/// in Exprs is type-dependent.
2028bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2029 for (unsigned I = 0; I < NumExprs; ++I)
2030 if (Exprs[I]->isTypeDependent())
2031 return true;
2032
2033 return false;
2034}
2035
2036/// hasAnyValueDependentArguments - Determines if any of the expressions
2037/// in Exprs is value-dependent.
2038bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2039 for (unsigned I = 0; I < NumExprs; ++I)
2040 if (Exprs[I]->isValueDependent())
2041 return true;
2042
2043 return false;
2044}
2045
John McCall8b0f4ff2010-08-02 21:13:48 +00002046bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002047 // This function is attempting whether an expression is an initializer
2048 // which can be evaluated at compile-time. isEvaluatable handles most
2049 // of the cases, but it can't deal with some initializer-specific
2050 // expressions, and it can't deal with aggregates; we deal with those here,
2051 // and fall back to isEvaluatable for the other cases.
2052
John McCall8b0f4ff2010-08-02 21:13:48 +00002053 // If we ever capture reference-binding directly in the AST, we can
2054 // kill the second parameter.
2055
2056 if (IsForRef) {
2057 EvalResult Result;
2058 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2059 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002060
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002061 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002062 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002063 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002064 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002065 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002066 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002067 case CXXTemporaryObjectExprClass:
2068 case CXXConstructExprClass: {
2069 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002070
2071 // Only if it's
2072 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00002073 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002074 if (!CE->getNumArgs()) return true;
2075
2076 // 2) an elidable trivial copy construction of an operand which is
2077 // itself a constant initializer. Note that we consider the
2078 // operand on its own, *not* as a reference binding.
2079 return CE->isElidable() &&
2080 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00002081 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002082 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002083 // This handles gcc's extension that allows global initializers like
2084 // "struct x {int x;} x = (struct x) {};".
2085 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002086 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002087 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002088 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002089 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002090 // FIXME: This doesn't deal with fields with reference types correctly.
2091 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2092 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002093 const InitListExpr *Exp = cast<InitListExpr>(this);
2094 unsigned numInits = Exp->getNumInits();
2095 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002096 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002097 return false;
2098 }
Eli Friedman384da272009-01-25 03:12:18 +00002099 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002100 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002101 case ImplicitValueInitExprClass:
2102 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002103 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002104 return cast<ParenExpr>(this)->getSubExpr()
2105 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002106 case ChooseExprClass:
2107 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2108 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002109 case UnaryOperatorClass: {
2110 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002111 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002112 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002113 break;
2114 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00002115 case BinaryOperatorClass: {
2116 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2117 // but this handles the common case.
2118 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002119 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3eb172a2009-10-13 07:14:16 +00002120 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2121 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2122 return true;
2123 break;
2124 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002125 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002126 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002127 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00002128 case CStyleCastExprClass:
2129 // Handle casts with a destination that's a struct or union; this
2130 // deals with both the gcc no-op struct cast extension and the
2131 // cast-to-union extension.
2132 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002133 return cast<CastExpr>(this)->getSubExpr()
2134 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002135
Chris Lattnera2f9bd52009-10-13 22:12:09 +00002136 // Integer->integer casts can be handled here, which is important for
2137 // things like (int)(&&x-&&y). Scary but true.
2138 if (getType()->isIntegerType() &&
2139 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002140 return cast<CastExpr>(this)->getSubExpr()
2141 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002142
Eli Friedman384da272009-01-25 03:12:18 +00002143 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002144 }
Eli Friedman384da272009-01-25 03:12:18 +00002145 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002146}
2147
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002148/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2149/// pointer constant or not, as well as the specific kind of constant detected.
2150/// Null pointer constants can be integer constant expressions with the
2151/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2152/// (a GNU extension).
2153Expr::NullPointerConstantKind
2154Expr::isNullPointerConstant(ASTContext &Ctx,
2155 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00002156 if (isValueDependent()) {
2157 switch (NPC) {
2158 case NPC_NeverValueDependent:
2159 assert(false && "Unexpected value dependent expression!");
2160 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002161
Douglas Gregor56751b52009-09-25 04:25:58 +00002162 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002163 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2164 return NPCK_ZeroInteger;
2165 else
2166 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00002167
Douglas Gregor56751b52009-09-25 04:25:58 +00002168 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002169 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00002170 }
2171 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002172
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002173 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002174 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00002175 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002176 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002177 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002178 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002179 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002180 Pointee->isVoidType() && // to void*
2181 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002182 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002183 }
Steve Naroffada7d422007-05-20 17:54:12 +00002184 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002185 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2186 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002187 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002188 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2189 // Accept ((void*)0) as a null pointer constant, as many other
2190 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002191 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002192 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002193 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002194 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002195 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002196 } else if (isa<GNUNullExpr>(this)) {
2197 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002198 return NPCK_GNUNull;
Steve Naroff09035312008-01-14 02:53:34 +00002199 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002200
Sebastian Redl576fd422009-05-10 18:38:11 +00002201 // C++0x nullptr_t is always a null pointer constant.
2202 if (getType()->isNullPtrType())
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002203 return NPCK_CXX0X_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00002204
Fariborz Jahanian3567c422010-09-27 22:42:37 +00002205 if (const RecordType *UT = getType()->getAsUnionType())
2206 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2207 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2208 const Expr *InitExpr = CLE->getInitializer();
2209 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2210 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2211 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002212 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002213 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002214 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002215 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00002216
Chris Lattner1abbd412007-06-08 17:58:43 +00002217 // If we have an integer constant expression, we need to *evaluate* it and
2218 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002219 llvm::APSInt Result;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002220 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2221
2222 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002223}
Steve Narofff7a5da12007-07-28 23:10:27 +00002224
John McCall34376a62010-12-04 03:47:34 +00002225/// \brief If this expression is an l-value for an Objective C
2226/// property, find the underlying property reference expression.
2227const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2228 const Expr *E = this;
2229 while (true) {
2230 assert((E->getValueKind() == VK_LValue &&
2231 E->getObjectKind() == OK_ObjCProperty) &&
2232 "expression is not a property reference");
2233 E = E->IgnoreParenCasts();
2234 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2235 if (BO->getOpcode() == BO_Comma) {
2236 E = BO->getRHS();
2237 continue;
2238 }
2239 }
2240
2241 break;
2242 }
2243
2244 return cast<ObjCPropertyRefExpr>(E);
2245}
2246
Douglas Gregor71235ec2009-05-02 02:18:30 +00002247FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002248 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002249
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002250 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002251 if (ICE->getCastKind() == CK_LValueToRValue ||
2252 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002253 E = ICE->getSubExpr()->IgnoreParens();
2254 else
2255 break;
2256 }
2257
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002258 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002259 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002260 if (Field->isBitField())
2261 return Field;
2262
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00002263 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2264 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2265 if (Field->isBitField())
2266 return Field;
2267
Douglas Gregor71235ec2009-05-02 02:18:30 +00002268 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2269 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2270 return BinOp->getLHS()->getBitField();
2271
2272 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002273}
2274
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002275bool Expr::refersToVectorElement() const {
2276 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002277
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002278 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002279 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002280 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002281 E = ICE->getSubExpr()->IgnoreParens();
2282 else
2283 break;
2284 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002285
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002286 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2287 return ASE->getBase()->getType()->isVectorType();
2288
2289 if (isa<ExtVectorElementExpr>(E))
2290 return true;
2291
2292 return false;
2293}
2294
Chris Lattnerb8211f62009-02-16 22:14:05 +00002295/// isArrow - Return true if the base expression is a pointer to vector,
2296/// return false if the base expression is a vector.
2297bool ExtVectorElementExpr::isArrow() const {
2298 return getBase()->getType()->isPointerType();
2299}
2300
Nate Begemance4d7fc2008-04-18 23:10:10 +00002301unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002302 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002303 return VT->getNumElements();
2304 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002305}
2306
Nate Begemanf322eab2008-05-09 06:41:27 +00002307/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002308bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002309 // FIXME: Refactor this code to an accessor on the AST node which returns the
2310 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002311 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002312
2313 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002314 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002315 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002316
Nate Begeman7e5185b2009-01-18 02:01:21 +00002317 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002318 if (Comp[0] == 's' || Comp[0] == 'S')
2319 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002320
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002321 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2322 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002323 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002324
Steve Naroff0d595ca2007-07-30 03:29:09 +00002325 return false;
2326}
Chris Lattner885b4952007-08-02 23:36:59 +00002327
Nate Begemanf322eab2008-05-09 06:41:27 +00002328/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002329void ExtVectorElementExpr::getEncodedElementAccess(
2330 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002331 llvm::StringRef Comp = Accessor->getName();
2332 if (Comp[0] == 's' || Comp[0] == 'S')
2333 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002334
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002335 bool isHi = Comp == "hi";
2336 bool isLo = Comp == "lo";
2337 bool isEven = Comp == "even";
2338 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002339
Nate Begemanf322eab2008-05-09 06:41:27 +00002340 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2341 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002342
Nate Begemanf322eab2008-05-09 06:41:27 +00002343 if (isHi)
2344 Index = e + i;
2345 else if (isLo)
2346 Index = i;
2347 else if (isEven)
2348 Index = 2 * i;
2349 else if (isOdd)
2350 Index = 2 * i + 1;
2351 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002352 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002353
Nate Begemand3862152008-05-13 21:03:02 +00002354 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002355 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002356}
2357
Douglas Gregor9a129192010-04-21 00:45:42 +00002358ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002359 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002360 SourceLocation LBracLoc,
2361 SourceLocation SuperLoc,
2362 bool IsInstanceSuper,
2363 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002364 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002365 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002366 ObjCMethodDecl *Method,
2367 Expr **Args, unsigned NumArgs,
2368 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002369 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002370 /*TypeDependent=*/false, /*ValueDependent=*/false,
2371 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002372 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2373 HasMethod(Method != 0), SuperLoc(SuperLoc),
2374 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2375 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002376 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002377{
Douglas Gregor9a129192010-04-21 00:45:42 +00002378 setReceiverPointer(SuperType.getAsOpaquePtr());
2379 if (NumArgs)
2380 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002381}
2382
Douglas Gregor9a129192010-04-21 00:45:42 +00002383ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002384 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002385 SourceLocation LBracLoc,
2386 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002387 Selector Sel,
2388 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002389 ObjCMethodDecl *Method,
2390 Expr **Args, unsigned NumArgs,
2391 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002392 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002393 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002394 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2395 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2396 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002397 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002398{
2399 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002400 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002401 for (unsigned I = 0; I != NumArgs; ++I) {
2402 if (Args[I]->isTypeDependent())
2403 ExprBits.TypeDependent = true;
2404 if (Args[I]->isValueDependent())
2405 ExprBits.ValueDependent = true;
2406 if (Args[I]->containsUnexpandedParameterPack())
2407 ExprBits.ContainsUnexpandedParameterPack = true;
2408
2409 MyArgs[I] = Args[I];
2410 }
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002411}
2412
Douglas Gregor9a129192010-04-21 00:45:42 +00002413ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002414 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002415 SourceLocation LBracLoc,
2416 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002417 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002418 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002419 ObjCMethodDecl *Method,
2420 Expr **Args, unsigned NumArgs,
2421 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002422 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002423 Receiver->isTypeDependent(),
2424 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002425 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2426 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2427 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002428 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002429{
2430 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002431 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002432 for (unsigned I = 0; I != NumArgs; ++I) {
2433 if (Args[I]->isTypeDependent())
2434 ExprBits.TypeDependent = true;
2435 if (Args[I]->isValueDependent())
2436 ExprBits.ValueDependent = true;
2437 if (Args[I]->containsUnexpandedParameterPack())
2438 ExprBits.ContainsUnexpandedParameterPack = true;
2439
2440 MyArgs[I] = Args[I];
2441 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00002442}
2443
Douglas Gregor9a129192010-04-21 00:45:42 +00002444ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002445 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002446 SourceLocation LBracLoc,
2447 SourceLocation SuperLoc,
2448 bool IsInstanceSuper,
2449 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002450 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002451 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002452 ObjCMethodDecl *Method,
2453 Expr **Args, unsigned NumArgs,
2454 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002455 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002456 NumArgs * sizeof(Expr *);
2457 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCall7decc9e2010-11-18 06:31:45 +00002458 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002459 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002460 RBracLoc);
2461}
2462
2463ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002464 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002465 SourceLocation LBracLoc,
2466 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002467 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002468 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002469 ObjCMethodDecl *Method,
2470 Expr **Args, unsigned NumArgs,
2471 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002472 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002473 NumArgs * sizeof(Expr *);
2474 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002475 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2476 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002477}
2478
2479ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002480 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002481 SourceLocation LBracLoc,
2482 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002483 Selector Sel,
2484 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002485 ObjCMethodDecl *Method,
2486 Expr **Args, unsigned NumArgs,
2487 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002488 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002489 NumArgs * sizeof(Expr *);
2490 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002491 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2492 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002493}
2494
Alexis Hunta8136cc2010-05-05 15:23:54 +00002495ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002496 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002497 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002498 NumArgs * sizeof(Expr *);
2499 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2500 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2501}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00002502
2503SourceRange ObjCMessageExpr::getReceiverRange() const {
2504 switch (getReceiverKind()) {
2505 case Instance:
2506 return getInstanceReceiver()->getSourceRange();
2507
2508 case Class:
2509 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2510
2511 case SuperInstance:
2512 case SuperClass:
2513 return getSuperLoc();
2514 }
2515
2516 return SourceLocation();
2517}
2518
Douglas Gregor9a129192010-04-21 00:45:42 +00002519Selector ObjCMessageExpr::getSelector() const {
2520 if (HasMethod)
2521 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2522 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002523 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002524}
2525
2526ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2527 switch (getReceiverKind()) {
2528 case Instance:
2529 if (const ObjCObjectPointerType *Ptr
2530 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2531 return Ptr->getInterfaceDecl();
2532 break;
2533
2534 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00002535 if (const ObjCObjectType *Ty
2536 = getClassReceiver()->getAs<ObjCObjectType>())
2537 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002538 break;
2539
2540 case SuperInstance:
2541 if (const ObjCObjectPointerType *Ptr
2542 = getSuperType()->getAs<ObjCObjectPointerType>())
2543 return Ptr->getInterfaceDecl();
2544 break;
2545
2546 case SuperClass:
Argyrios Kyrtzidis1b9747f2011-01-25 00:03:48 +00002547 if (const ObjCObjectType *Iface
2548 = getSuperType()->getAs<ObjCObjectType>())
2549 return Iface->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002550 break;
2551 }
2552
2553 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002554}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002555
Jay Foad39c79802011-01-12 09:06:06 +00002556bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002557 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002558}
2559
Douglas Gregora6e053e2010-12-15 01:34:56 +00002560ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2561 QualType Type, SourceLocation BLoc,
2562 SourceLocation RP)
2563 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2564 Type->isDependentType(), Type->isDependentType(),
2565 Type->containsUnexpandedParameterPack()),
2566 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2567{
2568 SubExprs = new (C) Stmt*[nexpr];
2569 for (unsigned i = 0; i < nexpr; i++) {
2570 if (args[i]->isTypeDependent())
2571 ExprBits.TypeDependent = true;
2572 if (args[i]->isValueDependent())
2573 ExprBits.ValueDependent = true;
2574 if (args[i]->containsUnexpandedParameterPack())
2575 ExprBits.ContainsUnexpandedParameterPack = true;
2576
2577 SubExprs[i] = args[i];
2578 }
2579}
2580
Nate Begeman48745922009-08-12 02:28:50 +00002581void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2582 unsigned NumExprs) {
2583 if (SubExprs) C.Deallocate(SubExprs);
2584
2585 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002586 this->NumExprs = NumExprs;
2587 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002588}
Nate Begeman48745922009-08-12 02:28:50 +00002589
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002590//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002591// DesignatedInitExpr
2592//===----------------------------------------------------------------------===//
2593
2594IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2595 assert(Kind == FieldDesignator && "Only valid on a field designator");
2596 if (Field.NameOrField & 0x01)
2597 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2598 else
2599 return getField()->getIdentifier();
2600}
2601
Alexis Hunta8136cc2010-05-05 15:23:54 +00002602DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002603 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002604 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002605 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002606 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002607 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002608 unsigned NumIndexExprs,
2609 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002610 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00002611 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002612 Init->isTypeDependent(), Init->isValueDependent(),
2613 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00002614 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2615 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002616 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002617
2618 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00002619 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002620 *Child++ = Init;
2621
2622 // Copy the designators and their subexpressions, computing
2623 // value-dependence along the way.
2624 unsigned IndexIdx = 0;
2625 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002626 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002627
2628 if (this->Designators[I].isArrayDesignator()) {
2629 // Compute type- and value-dependence.
2630 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002631 if (Index->isTypeDependent() || Index->isValueDependent())
2632 ExprBits.ValueDependent = true;
2633
2634 // Propagate unexpanded parameter packs.
2635 if (Index->containsUnexpandedParameterPack())
2636 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002637
2638 // Copy the index expressions into permanent storage.
2639 *Child++ = IndexExprs[IndexIdx++];
2640 } else if (this->Designators[I].isArrayRangeDesignator()) {
2641 // Compute type- and value-dependence.
2642 Expr *Start = IndexExprs[IndexIdx];
2643 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002644 if (Start->isTypeDependent() || Start->isValueDependent() ||
2645 End->isTypeDependent() || End->isValueDependent())
2646 ExprBits.ValueDependent = true;
2647
2648 // Propagate unexpanded parameter packs.
2649 if (Start->containsUnexpandedParameterPack() ||
2650 End->containsUnexpandedParameterPack())
2651 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002652
2653 // Copy the start/end expressions into permanent storage.
2654 *Child++ = IndexExprs[IndexIdx++];
2655 *Child++ = IndexExprs[IndexIdx++];
2656 }
2657 }
2658
2659 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002660}
2661
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002662DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002663DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002664 unsigned NumDesignators,
2665 Expr **IndexExprs, unsigned NumIndexExprs,
2666 SourceLocation ColonOrEqualLoc,
2667 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002668 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002669 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002670 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002671 ColonOrEqualLoc, UsesColonSyntax,
2672 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002673}
2674
Mike Stump11289f42009-09-09 15:08:12 +00002675DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002676 unsigned NumIndexExprs) {
2677 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2678 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2679 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2680}
2681
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002682void DesignatedInitExpr::setDesignators(ASTContext &C,
2683 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002684 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002685 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002686 NumDesignators = NumDesigs;
2687 for (unsigned I = 0; I != NumDesigs; ++I)
2688 Designators[I] = Desigs[I];
2689}
2690
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002691SourceRange DesignatedInitExpr::getSourceRange() const {
2692 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002693 Designator &First =
2694 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002695 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002696 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002697 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2698 else
2699 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2700 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002701 StartLoc =
2702 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002703 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2704}
2705
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002706Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2707 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2708 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2709 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002710 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2711 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2712}
2713
2714Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002715 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002716 "Requires array range designator");
2717 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2718 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002719 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2720 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2721}
2722
2723Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002724 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002725 "Requires array range designator");
2726 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2727 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002728 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2729 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2730}
2731
Douglas Gregord5846a12009-04-15 06:41:24 +00002732/// \brief Replaces the designator at index @p Idx with the series
2733/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002734void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002735 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002736 const Designator *Last) {
2737 unsigned NumNewDesignators = Last - First;
2738 if (NumNewDesignators == 0) {
2739 std::copy_backward(Designators + Idx + 1,
2740 Designators + NumDesignators,
2741 Designators + Idx);
2742 --NumNewDesignators;
2743 return;
2744 } else if (NumNewDesignators == 1) {
2745 Designators[Idx] = *First;
2746 return;
2747 }
2748
Mike Stump11289f42009-09-09 15:08:12 +00002749 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002750 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002751 std::copy(Designators, Designators + Idx, NewDesignators);
2752 std::copy(First, Last, NewDesignators + Idx);
2753 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2754 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00002755 Designators = NewDesignators;
2756 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2757}
2758
Mike Stump11289f42009-09-09 15:08:12 +00002759ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002760 Expr **exprs, unsigned nexprs,
2761 SourceLocation rparenloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00002762 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2763 false, false, false),
2764 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002765
Nate Begeman5ec4b312009-08-10 23:49:36 +00002766 Exprs = new (C) Stmt*[nexprs];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002767 for (unsigned i = 0; i != nexprs; ++i) {
2768 if (exprs[i]->isTypeDependent())
2769 ExprBits.TypeDependent = true;
2770 if (exprs[i]->isValueDependent())
2771 ExprBits.ValueDependent = true;
2772 if (exprs[i]->containsUnexpandedParameterPack())
2773 ExprBits.ContainsUnexpandedParameterPack = true;
2774
Nate Begeman5ec4b312009-08-10 23:49:36 +00002775 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002776 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00002777}
2778
John McCall1bf58462011-02-16 08:02:54 +00002779const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
2780 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
2781 e = ewc->getSubExpr();
2782 e = cast<CXXConstructExpr>(e)->getArg(0);
2783 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
2784 e = ice->getSubExpr();
2785 return cast<OpaqueValueExpr>(e);
2786}
2787
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002788//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002789// ExprIterator.
2790//===----------------------------------------------------------------------===//
2791
2792Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2793Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2794Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2795const Expr* ConstExprIterator::operator[](size_t idx) const {
2796 return cast<Expr>(I[idx]);
2797}
2798const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2799const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2800
2801//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002802// Child Iterators for iterating over subexpressions/substatements
2803//===----------------------------------------------------------------------===//
2804
Sebastian Redl6f282892008-11-11 17:56:53 +00002805// SizeOfAlignOfExpr
John McCallbd066782011-02-09 08:16:59 +00002806Stmt::child_range SizeOfAlignOfExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002807 // If this is of a type and the type is a VLA type (and not a typedef), the
2808 // size expression of the VLA needs to be treated as an executable expression.
2809 // Why isn't this weirdness documented better in StmtIterator?
2810 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002811 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00002812 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00002813 return child_range(child_iterator(T), child_iterator());
2814 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00002815 }
John McCallbd066782011-02-09 08:16:59 +00002816 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002817}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002818
Steve Naroffd54978b2007-09-18 23:55:05 +00002819// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00002820Stmt::child_range ObjCMessageExpr::children() {
2821 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00002822 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00002823 begin = reinterpret_cast<Stmt **>(this + 1);
2824 else
2825 begin = reinterpret_cast<Stmt **>(getArgs());
2826 return child_range(begin,
2827 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00002828}
2829
Steve Naroffc540d662008-09-03 18:15:37 +00002830// Blocks
John McCall351762c2011-02-07 10:33:21 +00002831BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregor476e3022011-01-19 21:32:01 +00002832 SourceLocation l, bool ByRef,
John McCall351762c2011-02-07 10:33:21 +00002833 bool constAdded)
Douglas Gregorf144f4f2011-01-19 21:52:31 +00002834 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregor476e3022011-01-19 21:32:01 +00002835 d->isParameterPack()),
John McCall351762c2011-02-07 10:33:21 +00002836 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregor476e3022011-01-19 21:32:01 +00002837{
Douglas Gregorf144f4f2011-01-19 21:52:31 +00002838 bool TypeDependent = false;
2839 bool ValueDependent = false;
2840 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
2841 ExprBits.TypeDependent = TypeDependent;
2842 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor476e3022011-01-19 21:32:01 +00002843}
2844