blob: 259c39c7d8eba5fe68d8e8cf457c8f1c4fd4cf44 [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
Douglas Gregorea972d32011-02-28 21:54:11 +0000279DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
John McCallce546572009-12-08 09:08:17 +0000280 ValueDecl *D, SourceLocation NameLoc,
John McCall6b51f282009-11-23 01:53:49 +0000281 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000282 QualType T, ExprValueKind VK)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000283 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000284 DecoratedD(D,
Douglas Gregorea972d32011-02-28 21:54:11 +0000285 (QualifierLoc? HasQualifierFlag : 0) |
John McCall6b51f282009-11-23 01:53:49 +0000286 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000287 Loc(NameLoc) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000288 if (QualifierLoc) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000289 NameQualifier *NQ = getNameQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +0000290 NQ->QualifierLoc = QualifierLoc;
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000291 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000292
John McCall6b51f282009-11-23 01:53:49 +0000293 if (TemplateArgs)
John McCallb3774b52010-08-19 23:49:38 +0000294 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Douglas Gregored6c7442009-11-23 11:41:28 +0000295
296 computeDependence();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000297}
298
Douglas Gregorea972d32011-02-28 21:54:11 +0000299DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000300 ValueDecl *D, const DeclarationNameInfo &NameInfo,
301 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000302 QualType T, ExprValueKind VK)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000303 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000304 DecoratedD(D,
Douglas Gregorea972d32011-02-28 21:54:11 +0000305 (QualifierLoc? HasQualifierFlag : 0) |
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000306 (TemplateArgs ? HasExplicitTemplateArgumentListFlag : 0)),
307 Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000308 if (QualifierLoc) {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000309 NameQualifier *NQ = getNameQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +0000310 NQ->QualifierLoc = QualifierLoc;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000311 }
312
313 if (TemplateArgs)
John McCallb3774b52010-08-19 23:49:38 +0000314 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000315
316 computeDependence();
317}
318
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000319DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000320 NestedNameSpecifierLoc QualifierLoc,
John McCallce546572009-12-08 09:08:17 +0000321 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000322 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000323 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000324 ExprValueKind VK,
Douglas Gregored6c7442009-11-23 11:41:28 +0000325 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000326 return Create(Context, QualifierLoc, D,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000327 DeclarationNameInfo(D->getDeclName(), NameLoc),
John McCall7decc9e2010-11-18 06:31:45 +0000328 T, VK, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000329}
330
331DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000332 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000333 ValueDecl *D,
334 const DeclarationNameInfo &NameInfo,
335 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000336 ExprValueKind VK,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000337 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000338 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregorea972d32011-02-28 21:54:11 +0000339 if (QualifierLoc != 0)
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000340 Size += sizeof(NameQualifier);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000341
John McCall6b51f282009-11-23 01:53:49 +0000342 if (TemplateArgs)
343 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Alexis Hunta8136cc2010-05-05 15:23:54 +0000344
Chris Lattner5c0b4052010-10-30 05:14:06 +0000345 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Douglas Gregorea972d32011-02-28 21:54:11 +0000346 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000347}
348
Douglas Gregor87866ce2011-02-04 12:01:24 +0000349DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
350 bool HasQualifier,
351 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000352 unsigned NumTemplateArgs) {
353 std::size_t Size = sizeof(DeclRefExpr);
354 if (HasQualifier)
355 Size += sizeof(NameQualifier);
356
Douglas Gregor87866ce2011-02-04 12:01:24 +0000357 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000358 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
359
Chris Lattner5c0b4052010-10-30 05:14:06 +0000360 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000361 return new (Mem) DeclRefExpr(EmptyShell());
362}
363
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000364SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000365 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000366 if (hasQualifier())
Douglas Gregorea972d32011-02-28 21:54:11 +0000367 R.setBegin(getQualifierLoc().getBeginLoc());
John McCallb3774b52010-08-19 23:49:38 +0000368 if (hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000369 R.setEnd(getRAngleLoc());
370 return R;
371}
372
Anders Carlsson2fb08242009-09-08 18:24:21 +0000373// FIXME: Maybe this should use DeclPrinter with a special "print predefined
374// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000375std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
376 ASTContext &Context = CurrentDecl->getASTContext();
377
Anders Carlsson2fb08242009-09-08 18:24:21 +0000378 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000379 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000380 return FD->getNameAsString();
381
382 llvm::SmallString<256> Name;
383 llvm::raw_svector_ostream Out(Name);
384
385 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000386 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000387 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000388 if (MD->isStatic())
389 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000390 }
391
392 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000393
394 std::string Proto = FD->getQualifiedNameAsString(Policy);
395
John McCall9dd450b2009-09-21 23:43:11 +0000396 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000397 const FunctionProtoType *FT = 0;
398 if (FD->hasWrittenPrototype())
399 FT = dyn_cast<FunctionProtoType>(AFT);
400
401 Proto += "(";
402 if (FT) {
403 llvm::raw_string_ostream POut(Proto);
404 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
405 if (i) POut << ", ";
406 std::string Param;
407 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
408 POut << Param;
409 }
410
411 if (FT->isVariadic()) {
412 if (FD->getNumParams()) POut << ", ";
413 POut << "...";
414 }
415 }
416 Proto += ")";
417
Sam Weinig4e83bd22009-12-27 01:38:20 +0000418 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
419 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
420 if (ThisQuals.hasConst())
421 Proto += " const";
422 if (ThisQuals.hasVolatile())
423 Proto += " volatile";
424 }
425
Sam Weinigd060ed42009-12-06 23:55:13 +0000426 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
427 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000428
429 Out << Proto;
430
431 Out.flush();
432 return Name.str().str();
433 }
434 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
435 llvm::SmallString<256> Name;
436 llvm::raw_svector_ostream Out(Name);
437 Out << (MD->isInstanceMethod() ? '-' : '+');
438 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000439
440 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
441 // a null check to avoid a crash.
442 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000443 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000444
Anders Carlsson2fb08242009-09-08 18:24:21 +0000445 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000446 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
447 Out << '(' << CID << ')';
448
Anders Carlsson2fb08242009-09-08 18:24:21 +0000449 Out << ' ';
450 Out << MD->getSelector().getAsString();
451 Out << ']';
452
453 Out.flush();
454 return Name.str().str();
455 }
456 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
457 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
458 return "top level";
459 }
460 return "";
461}
462
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000463void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
464 if (hasAllocation())
465 C.Deallocate(pVal);
466
467 BitWidth = Val.getBitWidth();
468 unsigned NumWords = Val.getNumWords();
469 const uint64_t* Words = Val.getRawData();
470 if (NumWords > 1) {
471 pVal = new (C) uint64_t[NumWords];
472 std::copy(Words, Words + NumWords, pVal);
473 } else if (NumWords == 1)
474 VAL = Words[0];
475 else
476 VAL = 0;
477}
478
479IntegerLiteral *
480IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
481 QualType type, SourceLocation l) {
482 return new (C) IntegerLiteral(C, V, type, l);
483}
484
485IntegerLiteral *
486IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
487 return new (C) IntegerLiteral(Empty);
488}
489
490FloatingLiteral *
491FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
492 bool isexact, QualType Type, SourceLocation L) {
493 return new (C) FloatingLiteral(C, V, isexact, Type, L);
494}
495
496FloatingLiteral *
497FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
498 return new (C) FloatingLiteral(Empty);
499}
500
Chris Lattnera0173132008-06-07 22:13:43 +0000501/// getValueAsApproximateDouble - This returns the value as an inaccurate
502/// double. Note that this may cause loss of precision, but is useful for
503/// debugging dumps, etc.
504double FloatingLiteral::getValueAsApproximateDouble() const {
505 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000506 bool ignored;
507 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
508 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000509 return V.convertToDouble();
510}
511
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000512StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
513 unsigned ByteLength, bool Wide,
Anders Carlsson75245402011-04-14 00:40:03 +0000514 bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000515 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000516 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000517 // Allocate enough space for the StringLiteral plus an array of locations for
518 // any concatenated string tokens.
519 void *Mem = C.Allocate(sizeof(StringLiteral)+
520 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000521 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000522 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000523
Steve Naroffdf7855b2007-02-21 23:46:25 +0000524 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000525 char *AStrData = new (C, 1) char[ByteLength];
526 memcpy(AStrData, StrData, ByteLength);
527 SL->StrData = AStrData;
528 SL->ByteLength = ByteLength;
529 SL->IsWide = Wide;
Anders Carlsson75245402011-04-14 00:40:03 +0000530 SL->IsPascal = Pascal;
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000531 SL->TokLocs[0] = Loc[0];
532 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000533
Chris Lattner630970d2009-02-18 05:49:11 +0000534 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000535 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
536 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000537}
538
Douglas Gregor958dfc92009-04-15 16:35:07 +0000539StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
540 void *Mem = C.Allocate(sizeof(StringLiteral)+
541 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000542 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000543 StringLiteral *SL = new (Mem) StringLiteral(QualType());
544 SL->StrData = 0;
545 SL->ByteLength = 0;
546 SL->NumConcatenated = NumStrs;
547 return SL;
548}
549
Daniel Dunbar36217882009-09-22 03:27:33 +0000550void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbar36217882009-09-22 03:27:33 +0000551 char *AStrData = new (C, 1) char[Str.size()];
552 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000553 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000554 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000555}
556
Chris Lattnere925d612010-11-17 07:37:15 +0000557/// getLocationOfByte - Return a source location that points to the specified
558/// byte of this string literal.
559///
560/// Strings are amazingly complex. They can be formed from multiple tokens and
561/// can have escape sequences in them in addition to the usual trigraph and
562/// escaped newline business. This routine handles this complexity.
563///
564SourceLocation StringLiteral::
565getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
566 const LangOptions &Features, const TargetInfo &Target) const {
567 assert(!isWide() && "This doesn't work for wide strings yet");
568
569 // Loop over all of the tokens in this string until we find the one that
570 // contains the byte we're looking for.
571 unsigned TokNo = 0;
572 while (1) {
573 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
574 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
575
576 // Get the spelling of the string so that we can get the data that makes up
577 // the string literal, not the identifier for the macro it is potentially
578 // expanded through.
579 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
580
581 // Re-lex the token to get its length and original spelling.
582 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
583 bool Invalid = false;
584 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
585 if (Invalid)
586 return StrTokSpellingLoc;
587
588 const char *StrData = Buffer.data()+LocInfo.second;
589
590 // Create a langops struct and enable trigraphs. This is sufficient for
591 // relexing tokens.
592 LangOptions LangOpts;
593 LangOpts.Trigraphs = true;
594
595 // Create a lexer starting at the beginning of this token.
596 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
597 Buffer.end());
598 Token TheTok;
599 TheLexer.LexFromRawLexer(TheTok);
600
601 // Use the StringLiteralParser to compute the length of the string in bytes.
602 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
603 unsigned TokNumBytes = SLP.GetStringLength();
604
605 // If the byte is in this token, return the location of the byte.
606 if (ByteNo < TokNumBytes ||
607 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
608 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
609
610 // Now that we know the offset of the token in the spelling, use the
611 // preprocessor to get the offset in the original source.
612 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
613 }
614
615 // Move to the next string token.
616 ++TokNo;
617 ByteNo -= TokNumBytes;
618 }
619}
620
621
622
Chris Lattner1b926492006-08-23 06:42:10 +0000623/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
624/// corresponds to, e.g. "sizeof" or "[pre]++".
625const char *UnaryOperator::getOpcodeStr(Opcode Op) {
626 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000627 default: assert(0 && "Unknown unary operator");
John McCalle3027922010-08-25 11:45:40 +0000628 case UO_PostInc: return "++";
629 case UO_PostDec: return "--";
630 case UO_PreInc: return "++";
631 case UO_PreDec: return "--";
632 case UO_AddrOf: return "&";
633 case UO_Deref: return "*";
634 case UO_Plus: return "+";
635 case UO_Minus: return "-";
636 case UO_Not: return "~";
637 case UO_LNot: return "!";
638 case UO_Real: return "__real";
639 case UO_Imag: return "__imag";
640 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000641 }
642}
643
John McCalle3027922010-08-25 11:45:40 +0000644UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000645UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
646 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000647 default: assert(false && "No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000648 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
649 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
650 case OO_Amp: return UO_AddrOf;
651 case OO_Star: return UO_Deref;
652 case OO_Plus: return UO_Plus;
653 case OO_Minus: return UO_Minus;
654 case OO_Tilde: return UO_Not;
655 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000656 }
657}
658
659OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
660 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000661 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
662 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
663 case UO_AddrOf: return OO_Amp;
664 case UO_Deref: return OO_Star;
665 case UO_Plus: return OO_Plus;
666 case UO_Minus: return OO_Minus;
667 case UO_Not: return OO_Tilde;
668 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000669 default: return OO_None;
670 }
671}
672
673
Chris Lattner0eedafe2006-08-24 04:56:27 +0000674//===----------------------------------------------------------------------===//
675// Postfix Operators.
676//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000677
Peter Collingbourne3a347252011-02-08 21:18:02 +0000678CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
679 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCall7decc9e2010-11-18 06:31:45 +0000680 SourceLocation rparenloc)
681 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000682 fn->isTypeDependent(),
683 fn->isValueDependent(),
684 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000685 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000686
Peter Collingbourne3a347252011-02-08 21:18:02 +0000687 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +0000688 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000689 for (unsigned i = 0; i != numargs; ++i) {
690 if (args[i]->isTypeDependent())
691 ExprBits.TypeDependent = true;
692 if (args[i]->isValueDependent())
693 ExprBits.ValueDependent = true;
694 if (args[i]->containsUnexpandedParameterPack())
695 ExprBits.ContainsUnexpandedParameterPack = true;
696
Peter Collingbourne3a347252011-02-08 21:18:02 +0000697 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000698 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000699
Peter Collingbourne3a347252011-02-08 21:18:02 +0000700 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +0000701 RParenLoc = rparenloc;
702}
Nate Begeman1e36a852008-01-17 17:46:27 +0000703
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000704CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCall7decc9e2010-11-18 06:31:45 +0000705 QualType t, ExprValueKind VK, SourceLocation rparenloc)
706 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000707 fn->isTypeDependent(),
708 fn->isValueDependent(),
709 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000710 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000711
Peter Collingbourne3a347252011-02-08 21:18:02 +0000712 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000713 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000714 for (unsigned i = 0; i != numargs; ++i) {
715 if (args[i]->isTypeDependent())
716 ExprBits.TypeDependent = true;
717 if (args[i]->isValueDependent())
718 ExprBits.ValueDependent = true;
719 if (args[i]->containsUnexpandedParameterPack())
720 ExprBits.ContainsUnexpandedParameterPack = true;
721
Peter Collingbourne3a347252011-02-08 21:18:02 +0000722 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000723 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000724
Peter Collingbourne3a347252011-02-08 21:18:02 +0000725 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000726 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000727}
728
Mike Stump11289f42009-09-09 15:08:12 +0000729CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
730 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000731 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +0000732 SubExprs = new (C) Stmt*[PREARGS_START];
733 CallExprBits.NumPreArgs = 0;
734}
735
736CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
737 EmptyShell Empty)
738 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
739 // FIXME: Why do we allocate this?
740 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
741 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +0000742}
743
Nuno Lopes518e3702009-12-20 23:11:08 +0000744Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000745 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl2b1832e2010-09-10 20:55:30 +0000746 // If we're calling a dereference, look at the pointer instead.
747 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
748 if (BO->isPtrMemOp())
749 CEE = BO->getRHS()->IgnoreParenCasts();
750 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
751 if (UO->getOpcode() == UO_Deref)
752 CEE = UO->getSubExpr()->IgnoreParenCasts();
753 }
Chris Lattner52301912009-07-17 15:46:27 +0000754 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000755 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000756 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
757 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000758
759 return 0;
760}
761
Nuno Lopes518e3702009-12-20 23:11:08 +0000762FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000763 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000764}
765
Chris Lattnere4407ed2007-12-28 05:25:02 +0000766/// setNumArgs - This changes the number of arguments present in this call.
767/// Any orphaned expressions are deleted by this, and any new operands are set
768/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000769void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000770 // No change, just return.
771 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000772
Chris Lattnere4407ed2007-12-28 05:25:02 +0000773 // If shrinking # arguments, just delete the extras and forgot them.
774 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000775 this->NumArgs = NumArgs;
776 return;
777 }
778
779 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000780 unsigned NumPreArgs = getNumPreArgs();
781 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000782 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000783 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000784 NewSubExprs[i] = SubExprs[i];
785 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000786 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
787 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000788 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000789
Douglas Gregorba6e5572009-04-17 21:46:47 +0000790 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000791 SubExprs = NewSubExprs;
792 this->NumArgs = NumArgs;
793}
794
Chris Lattner01ff98a2008-10-06 05:00:53 +0000795/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
796/// not, return 0.
Jay Foad39c79802011-01-12 09:06:06 +0000797unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000798 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000799 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000800 // ImplicitCastExpr.
801 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
802 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000803 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000804
Steve Narofff6e3b3292008-01-31 01:07:12 +0000805 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
806 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000807 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000808
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000809 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
810 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000811 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000812
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000813 if (!FDecl->getIdentifier())
814 return 0;
815
Douglas Gregor15fc9562009-09-12 00:22:50 +0000816 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000817}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000818
Anders Carlsson00a27592009-05-26 04:57:27 +0000819QualType CallExpr::getCallReturnType() const {
820 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000821 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000822 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000823 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000824 CalleeType = BPT->getPointeeType();
Douglas Gregor603d81b2010-07-13 08:18:22 +0000825 else if (const MemberPointerType *MPT
826 = CalleeType->getAs<MemberPointerType>())
827 CalleeType = MPT->getPointeeType();
828
John McCall9dd450b2009-09-21 23:43:11 +0000829 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000830 return FnType->getResultType();
831}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000832
John McCall701417a2011-02-21 06:23:05 +0000833SourceRange CallExpr::getSourceRange() const {
834 if (isa<CXXOperatorCallExpr>(this))
835 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
836
837 SourceLocation begin = getCallee()->getLocStart();
838 if (begin.isInvalid() && getNumArgs() > 0)
839 begin = getArg(0)->getLocStart();
840 SourceLocation end = getRParenLoc();
841 if (end.isInvalid() && getNumArgs() > 0)
842 end = getArg(getNumArgs() - 1)->getLocEnd();
843 return SourceRange(begin, end);
844}
845
Alexis Hunta8136cc2010-05-05 15:23:54 +0000846OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000847 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000848 TypeSourceInfo *tsi,
849 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000850 Expr** exprsPtr, unsigned numExprs,
851 SourceLocation RParenLoc) {
852 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000853 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000854 sizeof(Expr*) * numExprs);
855
856 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
857 exprsPtr, numExprs, RParenLoc);
858}
859
860OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
861 unsigned numComps, unsigned numExprs) {
862 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
863 sizeof(OffsetOfNode) * numComps +
864 sizeof(Expr*) * numExprs);
865 return new (Mem) OffsetOfExpr(numComps, numExprs);
866}
867
Alexis Hunta8136cc2010-05-05 15:23:54 +0000868OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000869 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000870 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000871 Expr** exprsPtr, unsigned numExprs,
872 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +0000873 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
874 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000875 /*ValueDependent=*/tsi->getType()->isDependentType(),
876 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000877 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
878 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000879{
880 for(unsigned i = 0; i < numComps; ++i) {
881 setComponent(i, compsPtr[i]);
882 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000883
Douglas Gregor882211c2010-04-28 22:16:22 +0000884 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000885 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
886 ExprBits.ValueDependent = true;
887 if (exprsPtr[i]->containsUnexpandedParameterPack())
888 ExprBits.ContainsUnexpandedParameterPack = true;
889
Douglas Gregor882211c2010-04-28 22:16:22 +0000890 setIndexExpr(i, exprsPtr[i]);
891 }
892}
893
894IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
895 assert(getKind() == Field || getKind() == Identifier);
896 if (getKind() == Field)
897 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000898
Douglas Gregor882211c2010-04-28 22:16:22 +0000899 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
900}
901
Mike Stump11289f42009-09-09 15:08:12 +0000902MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +0000903 NestedNameSpecifierLoc QualifierLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000904 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000905 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000906 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +0000907 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +0000908 QualType ty,
909 ExprValueKind vk,
910 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000911 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000912
Douglas Gregorea972d32011-02-28 21:54:11 +0000913 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +0000914 founddecl.getDecl() != memberdecl ||
915 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000916 if (hasQualOrFound)
917 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000918
John McCall6b51f282009-11-23 01:53:49 +0000919 if (targs)
920 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000921
Chris Lattner5c0b4052010-10-30 05:14:06 +0000922 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +0000923 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
924 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +0000925
926 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000927 // FIXME: Wrong. We should be looking at the member declaration we found.
928 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +0000929 E->setValueDependent(true);
930 E->setTypeDependent(true);
931 }
932 E->HasQualifierOrFoundDecl = true;
933
934 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +0000935 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +0000936 NQ->FoundDecl = founddecl;
937 }
938
939 if (targs) {
940 E->HasExplicitTemplateArgumentList = true;
John McCallb3774b52010-08-19 23:49:38 +0000941 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall16df1e52010-03-30 21:47:33 +0000942 }
943
944 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000945}
946
Douglas Gregor25b7e052011-03-02 21:06:53 +0000947SourceRange MemberExpr::getSourceRange() const {
948 SourceLocation StartLoc;
949 if (isImplicitAccess()) {
950 if (hasQualifier())
951 StartLoc = getQualifierLoc().getBeginLoc();
952 else
953 StartLoc = MemberLoc;
954 } else {
955 // FIXME: We don't want this to happen. Rather, we should be able to
956 // detect all kinds of implicit accesses more cleanly.
957 StartLoc = getBase()->getLocStart();
958 if (StartLoc.isInvalid())
959 StartLoc = MemberLoc;
960 }
961
962 SourceLocation EndLoc =
963 HasExplicitTemplateArgumentList? getRAngleLoc()
964 : getMemberNameInfo().getEndLoc();
965
966 return SourceRange(StartLoc, EndLoc);
967}
968
Anders Carlsson496335e2009-09-03 00:59:21 +0000969const char *CastExpr::getCastKindName() const {
970 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +0000971 case CK_Dependent:
972 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +0000973 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +0000974 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +0000975 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +0000976 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +0000977 case CK_LValueToRValue:
978 return "LValueToRValue";
John McCall34376a62010-12-04 03:47:34 +0000979 case CK_GetObjCProperty:
980 return "GetObjCProperty";
John McCalle3027922010-08-25 11:45:40 +0000981 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +0000982 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +0000983 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +0000984 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +0000985 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +0000986 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000987 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +0000988 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000989 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +0000990 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +0000991 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000992 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +0000993 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000994 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000995 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000996 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000997 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000998 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +0000999 case CK_NullToPointer:
1000 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001001 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001002 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001003 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001004 return "DerivedToBaseMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001005 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001006 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001007 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001008 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001009 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001010 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001011 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001012 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001013 case CK_PointerToBoolean:
1014 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001015 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001016 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001017 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001018 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001019 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001020 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001021 case CK_IntegralToBoolean:
1022 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001023 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001024 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001025 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001026 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001027 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001028 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001029 case CK_FloatingToBoolean:
1030 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001031 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001032 return "MemberPointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001033 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahaniane19122f2009-12-08 23:46:15 +00001034 return "AnyPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001035 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001036 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001037 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001038 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001039 case CK_FloatingRealToComplex:
1040 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001041 case CK_FloatingComplexToReal:
1042 return "FloatingComplexToReal";
1043 case CK_FloatingComplexToBoolean:
1044 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001045 case CK_FloatingComplexCast:
1046 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001047 case CK_FloatingComplexToIntegralComplex:
1048 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001049 case CK_IntegralRealToComplex:
1050 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001051 case CK_IntegralComplexToReal:
1052 return "IntegralComplexToReal";
1053 case CK_IntegralComplexToBoolean:
1054 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001055 case CK_IntegralComplexCast:
1056 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001057 case CK_IntegralComplexToFloatingComplex:
1058 return "IntegralComplexToFloatingComplex";
Anders Carlsson496335e2009-09-03 00:59:21 +00001059 }
Mike Stump11289f42009-09-09 15:08:12 +00001060
John McCallc5e62b42010-11-13 09:02:35 +00001061 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001062 return 0;
1063}
1064
Douglas Gregord196a582009-12-14 19:27:10 +00001065Expr *CastExpr::getSubExprAsWritten() {
1066 Expr *SubExpr = 0;
1067 CastExpr *E = this;
1068 do {
1069 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001070
Douglas Gregord196a582009-12-14 19:27:10 +00001071 // Skip any temporary bindings; they're implicit.
1072 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1073 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001074
Douglas Gregord196a582009-12-14 19:27:10 +00001075 // Conversions by constructor and conversion functions have a
1076 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001077 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001078 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001079 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001080 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001081
Douglas Gregord196a582009-12-14 19:27:10 +00001082 // If the subexpression we're left with is an implicit cast, look
1083 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001084 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1085
Douglas Gregord196a582009-12-14 19:27:10 +00001086 return SubExpr;
1087}
1088
John McCallcf142162010-08-07 06:22:56 +00001089CXXBaseSpecifier **CastExpr::path_buffer() {
1090 switch (getStmtClass()) {
1091#define ABSTRACT_STMT(x)
1092#define CASTEXPR(Type, Base) \
1093 case Stmt::Type##Class: \
1094 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1095#define STMT(Type, Base)
1096#include "clang/AST/StmtNodes.inc"
1097 default:
1098 llvm_unreachable("non-cast expressions not possible here");
1099 return 0;
1100 }
1101}
1102
1103void CastExpr::setCastPath(const CXXCastPath &Path) {
1104 assert(Path.size() == path_size());
1105 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1106}
1107
1108ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1109 CastKind Kind, Expr *Operand,
1110 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001111 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001112 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1113 void *Buffer =
1114 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1115 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001116 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001117 if (PathSize) E->setCastPath(*BasePath);
1118 return E;
1119}
1120
1121ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1122 unsigned PathSize) {
1123 void *Buffer =
1124 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1125 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1126}
1127
1128
1129CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001130 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001131 const CXXCastPath *BasePath,
1132 TypeSourceInfo *WrittenTy,
1133 SourceLocation L, SourceLocation R) {
1134 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1135 void *Buffer =
1136 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1137 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001138 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001139 if (PathSize) E->setCastPath(*BasePath);
1140 return E;
1141}
1142
1143CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1144 void *Buffer =
1145 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1146 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1147}
1148
Chris Lattner1b926492006-08-23 06:42:10 +00001149/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1150/// corresponds to, e.g. "<<=".
1151const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1152 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001153 case BO_PtrMemD: return ".*";
1154 case BO_PtrMemI: return "->*";
1155 case BO_Mul: return "*";
1156 case BO_Div: return "/";
1157 case BO_Rem: return "%";
1158 case BO_Add: return "+";
1159 case BO_Sub: return "-";
1160 case BO_Shl: return "<<";
1161 case BO_Shr: return ">>";
1162 case BO_LT: return "<";
1163 case BO_GT: return ">";
1164 case BO_LE: return "<=";
1165 case BO_GE: return ">=";
1166 case BO_EQ: return "==";
1167 case BO_NE: return "!=";
1168 case BO_And: return "&";
1169 case BO_Xor: return "^";
1170 case BO_Or: return "|";
1171 case BO_LAnd: return "&&";
1172 case BO_LOr: return "||";
1173 case BO_Assign: return "=";
1174 case BO_MulAssign: return "*=";
1175 case BO_DivAssign: return "/=";
1176 case BO_RemAssign: return "%=";
1177 case BO_AddAssign: return "+=";
1178 case BO_SubAssign: return "-=";
1179 case BO_ShlAssign: return "<<=";
1180 case BO_ShrAssign: return ">>=";
1181 case BO_AndAssign: return "&=";
1182 case BO_XorAssign: return "^=";
1183 case BO_OrAssign: return "|=";
1184 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001185 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001186
1187 return "";
Chris Lattner1b926492006-08-23 06:42:10 +00001188}
Steve Naroff47500512007-04-19 23:00:49 +00001189
John McCalle3027922010-08-25 11:45:40 +00001190BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001191BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1192 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +00001193 default: assert(false && "Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001194 case OO_Plus: return BO_Add;
1195 case OO_Minus: return BO_Sub;
1196 case OO_Star: return BO_Mul;
1197 case OO_Slash: return BO_Div;
1198 case OO_Percent: return BO_Rem;
1199 case OO_Caret: return BO_Xor;
1200 case OO_Amp: return BO_And;
1201 case OO_Pipe: return BO_Or;
1202 case OO_Equal: return BO_Assign;
1203 case OO_Less: return BO_LT;
1204 case OO_Greater: return BO_GT;
1205 case OO_PlusEqual: return BO_AddAssign;
1206 case OO_MinusEqual: return BO_SubAssign;
1207 case OO_StarEqual: return BO_MulAssign;
1208 case OO_SlashEqual: return BO_DivAssign;
1209 case OO_PercentEqual: return BO_RemAssign;
1210 case OO_CaretEqual: return BO_XorAssign;
1211 case OO_AmpEqual: return BO_AndAssign;
1212 case OO_PipeEqual: return BO_OrAssign;
1213 case OO_LessLess: return BO_Shl;
1214 case OO_GreaterGreater: return BO_Shr;
1215 case OO_LessLessEqual: return BO_ShlAssign;
1216 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1217 case OO_EqualEqual: return BO_EQ;
1218 case OO_ExclaimEqual: return BO_NE;
1219 case OO_LessEqual: return BO_LE;
1220 case OO_GreaterEqual: return BO_GE;
1221 case OO_AmpAmp: return BO_LAnd;
1222 case OO_PipePipe: return BO_LOr;
1223 case OO_Comma: return BO_Comma;
1224 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001225 }
1226}
1227
1228OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1229 static const OverloadedOperatorKind OverOps[] = {
1230 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1231 OO_Star, OO_Slash, OO_Percent,
1232 OO_Plus, OO_Minus,
1233 OO_LessLess, OO_GreaterGreater,
1234 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1235 OO_EqualEqual, OO_ExclaimEqual,
1236 OO_Amp,
1237 OO_Caret,
1238 OO_Pipe,
1239 OO_AmpAmp,
1240 OO_PipePipe,
1241 OO_Equal, OO_StarEqual,
1242 OO_SlashEqual, OO_PercentEqual,
1243 OO_PlusEqual, OO_MinusEqual,
1244 OO_LessLessEqual, OO_GreaterGreaterEqual,
1245 OO_AmpEqual, OO_CaretEqual,
1246 OO_PipeEqual,
1247 OO_Comma
1248 };
1249 return OverOps[Opc];
1250}
1251
Ted Kremenekac034612010-04-13 23:39:13 +00001252InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +00001253 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001254 SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001255 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1256 false),
Ted Kremenekac034612010-04-13 23:39:13 +00001257 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +00001258 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001259 UnionFieldInit(0), HadArrayRangeDesignator(false)
1260{
Ted Kremenek013041e2010-02-19 01:50:18 +00001261 for (unsigned I = 0; I != numInits; ++I) {
1262 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001263 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001264 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001265 ExprBits.ValueDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001266 if (initExprs[I]->containsUnexpandedParameterPack())
1267 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001268 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001269
Ted Kremenekac034612010-04-13 23:39:13 +00001270 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001271}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001272
Ted Kremenekac034612010-04-13 23:39:13 +00001273void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001274 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001275 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001276}
1277
Ted Kremenekac034612010-04-13 23:39:13 +00001278void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001279 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001280}
1281
Ted Kremenekac034612010-04-13 23:39:13 +00001282Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001283 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001284 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001285 InitExprs.back() = expr;
1286 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001287 }
Mike Stump11289f42009-09-09 15:08:12 +00001288
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001289 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1290 InitExprs[Init] = expr;
1291 return Result;
1292}
1293
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001294SourceRange InitListExpr::getSourceRange() const {
1295 if (SyntacticForm)
1296 return SyntacticForm->getSourceRange();
1297 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1298 if (Beg.isInvalid()) {
1299 // Find the first non-null initializer.
1300 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1301 E = InitExprs.end();
1302 I != E; ++I) {
1303 if (Stmt *S = *I) {
1304 Beg = S->getLocStart();
1305 break;
1306 }
1307 }
1308 }
1309 if (End.isInvalid()) {
1310 // Find the first non-null initializer from the end.
1311 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1312 E = InitExprs.rend();
1313 I != E; ++I) {
1314 if (Stmt *S = *I) {
1315 End = S->getSourceRange().getEnd();
1316 break;
1317 }
1318 }
1319 }
1320 return SourceRange(Beg, End);
1321}
1322
Steve Naroff991e99d2008-09-04 15:31:07 +00001323/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001324///
1325const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001326 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +00001327 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001328}
1329
Mike Stump11289f42009-09-09 15:08:12 +00001330SourceLocation BlockExpr::getCaretLocation() const {
1331 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001332}
Mike Stump11289f42009-09-09 15:08:12 +00001333const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001334 return TheBlock->getBody();
1335}
Mike Stump11289f42009-09-09 15:08:12 +00001336Stmt *BlockExpr::getBody() {
1337 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001338}
Steve Naroff415d3d52008-10-08 17:01:13 +00001339
1340
Chris Lattner1ec5f562007-06-27 05:38:08 +00001341//===----------------------------------------------------------------------===//
1342// Generic Expression Routines
1343//===----------------------------------------------------------------------===//
1344
Chris Lattner237f2752009-02-14 07:37:35 +00001345/// isUnusedResultAWarning - Return true if this immediate expression should
1346/// be warned about if the result is unused. If so, fill in Loc and Ranges
1347/// with location to warn on and the source range[s] to report with the
1348/// warning.
1349bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +00001350 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001351 // Don't warn if the expr is type dependent. The type could end up
1352 // instantiating to void.
1353 if (isTypeDependent())
1354 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001355
Chris Lattner1ec5f562007-06-27 05:38:08 +00001356 switch (getStmtClass()) {
1357 default:
John McCallc493a732010-03-12 07:11:26 +00001358 if (getType()->isVoidType())
1359 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001360 Loc = getExprLoc();
1361 R1 = getSourceRange();
1362 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001363 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001364 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +00001365 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001366 case UnaryOperatorClass: {
1367 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001368
Chris Lattner1ec5f562007-06-27 05:38:08 +00001369 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001370 default: break;
John McCalle3027922010-08-25 11:45:40 +00001371 case UO_PostInc:
1372 case UO_PostDec:
1373 case UO_PreInc:
1374 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001375 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001376 case UO_Deref:
Chris Lattnera44d1162007-06-27 05:58:59 +00001377 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001378 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001379 return false;
1380 break;
John McCalle3027922010-08-25 11:45:40 +00001381 case UO_Real:
1382 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001383 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001384 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1385 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001386 return false;
1387 break;
John McCalle3027922010-08-25 11:45:40 +00001388 case UO_Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001389 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001390 }
Chris Lattner237f2752009-02-14 07:37:35 +00001391 Loc = UO->getOperatorLoc();
1392 R1 = UO->getSubExpr()->getSourceRange();
1393 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001394 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001395 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001396 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001397 switch (BO->getOpcode()) {
1398 default:
1399 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001400 // Consider the RHS of comma for side effects. LHS was checked by
1401 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001402 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001403 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1404 // lvalue-ness) of an assignment written in a macro.
1405 if (IntegerLiteral *IE =
1406 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1407 if (IE->getValue() == 0)
1408 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001409 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1410 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001411 case BO_LAnd:
1412 case BO_LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001413 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1414 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1415 return false;
1416 break;
John McCall1e3715a2010-02-16 04:10:53 +00001417 }
Chris Lattner237f2752009-02-14 07:37:35 +00001418 if (BO->isAssignmentOp())
1419 return false;
1420 Loc = BO->getOperatorLoc();
1421 R1 = BO->getLHS()->getSourceRange();
1422 R2 = BO->getRHS()->getSourceRange();
1423 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001424 }
Chris Lattner86928112007-08-25 02:00:02 +00001425 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001426 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001427 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001428
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001429 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00001430 // If only one of the LHS or RHS is a warning, the operator might
1431 // be being used for control flow. Only warn if both the LHS and
1432 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001433 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremeneke96dad92011-03-01 20:34:48 +00001434 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1435 return false;
1436 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00001437 return true;
Ted Kremeneke96dad92011-03-01 20:34:48 +00001438 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001439 }
1440
Chris Lattnera44d1162007-06-27 05:58:59 +00001441 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001442 // If the base pointer or element is to a volatile pointer/field, accessing
1443 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001444 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001445 return false;
1446 Loc = cast<MemberExpr>(this)->getMemberLoc();
1447 R1 = SourceRange(Loc, Loc);
1448 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1449 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001450
Chris Lattner1ec5f562007-06-27 05:38:08 +00001451 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001452 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001453 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001454 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001455 return false;
1456 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1457 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1458 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1459 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001460
Chris Lattner1ec5f562007-06-27 05:38:08 +00001461 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001462 case CXXOperatorCallExprClass:
1463 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001464 // If this is a direct call, get the callee.
1465 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001466 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001467 // If the callee has attribute pure, const, or warn_unused_result, warn
1468 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001469 //
1470 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1471 // updated to match for QoI.
1472 if (FD->getAttr<WarnUnusedResultAttr>() ||
1473 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1474 Loc = CE->getCallee()->getLocStart();
1475 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001476
Chris Lattner1a6babf2009-10-13 04:53:48 +00001477 if (unsigned NumArgs = CE->getNumArgs())
1478 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1479 CE->getArg(NumArgs-1)->getLocEnd());
1480 return true;
1481 }
Chris Lattner237f2752009-02-14 07:37:35 +00001482 }
1483 return false;
1484 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001485
1486 case CXXTemporaryObjectExprClass:
1487 case CXXConstructExprClass:
1488 return false;
1489
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001490 case ObjCMessageExprClass: {
1491 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1492 const ObjCMethodDecl *MD = ME->getMethodDecl();
1493 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1494 Loc = getExprLoc();
1495 return true;
1496 }
Chris Lattner237f2752009-02-14 07:37:35 +00001497 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001498 }
Mike Stump11289f42009-09-09 15:08:12 +00001499
John McCallb7bd14f2010-12-02 01:19:52 +00001500 case ObjCPropertyRefExprClass:
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001501 Loc = getExprLoc();
1502 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001503 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00001504
Chris Lattner944d3062008-07-26 19:51:01 +00001505 case StmtExprClass: {
1506 // Statement exprs don't logically have side effects themselves, but are
1507 // sometimes used in macros in ways that give them a type that is unused.
1508 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1509 // however, if the result of the stmt expr is dead, we don't want to emit a
1510 // warning.
1511 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001512 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00001513 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001514 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001515 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1516 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1517 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1518 }
Mike Stump11289f42009-09-09 15:08:12 +00001519
John McCallc493a732010-03-12 07:11:26 +00001520 if (getType()->isVoidType())
1521 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001522 Loc = cast<StmtExpr>(this)->getLParenLoc();
1523 R1 = getSourceRange();
1524 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001525 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001526 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001527 // If this is an explicit cast to void, allow it. People do this when they
1528 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001529 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001530 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001531 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1532 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1533 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001534 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001535 if (getType()->isVoidType())
1536 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001537 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001538
Anders Carlsson6aa50392009-11-17 17:11:23 +00001539 // If this is a cast to void or a constructor conversion, check the operand.
1540 // Otherwise, the result of the cast is unused.
John McCalle3027922010-08-25 11:45:40 +00001541 if (CE->getCastKind() == CK_ToVoid ||
1542 CE->getCastKind() == CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001543 return (cast<CastExpr>(this)->getSubExpr()
1544 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001545 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1546 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1547 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001548 }
Mike Stump11289f42009-09-09 15:08:12 +00001549
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001550 case ImplicitCastExprClass:
1551 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001552 return (cast<ImplicitCastExpr>(this)
1553 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001554
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001555 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001556 return (cast<CXXDefaultArgExpr>(this)
1557 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001558
1559 case CXXNewExprClass:
1560 // FIXME: In theory, there might be new expressions that don't have side
1561 // effects (e.g. a placement new with an uninitialized POD).
1562 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001563 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001564 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001565 return (cast<CXXBindTemporaryExpr>(this)
1566 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00001567 case ExprWithCleanupsClass:
1568 return (cast<ExprWithCleanups>(this)
Mike Stump53f9ded2009-11-03 23:25:48 +00001569 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001570 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001571}
1572
Fariborz Jahanian07735332009-02-22 18:40:18 +00001573/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001574/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001575bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001576 switch (getStmtClass()) {
1577 default:
1578 return false;
1579 case ObjCIvarRefExprClass:
1580 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001581 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001582 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001583 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001584 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001585 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001586 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001587 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001588 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001589 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001590 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001591 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1592 if (VD->hasGlobalStorage())
1593 return true;
1594 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001595 // dereferencing to a pointer is always a gc'able candidate,
1596 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001597 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001598 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001599 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001600 return false;
1601 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001602 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001603 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001604 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001605 }
1606 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001607 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001608 }
1609}
Sebastian Redlce354af2010-09-10 20:55:33 +00001610
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001611bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1612 if (isTypeDependent())
1613 return false;
John McCall086a4642010-11-24 05:12:34 +00001614 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001615}
1616
Sebastian Redlce354af2010-09-10 20:55:33 +00001617static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1618 Expr::CanThrowResult CT2) {
1619 // CanThrowResult constants are ordered so that the maximum is the correct
1620 // merge result.
1621 return CT1 > CT2 ? CT1 : CT2;
1622}
1623
1624static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1625 Expr *E = const_cast<Expr*>(CE);
1626 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall8322c3a2011-02-13 04:07:26 +00001627 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redlce354af2010-09-10 20:55:33 +00001628 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1629 }
1630 return R;
1631}
1632
Sebastian Redl31ad7542011-03-13 17:09:40 +00001633static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Decl *D,
Sebastian Redlce354af2010-09-10 20:55:33 +00001634 bool NullThrows = true) {
1635 if (!D)
1636 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1637
1638 // See if we can get a function type from the decl somehow.
1639 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1640 if (!VD) // If we have no clue what we're calling, assume the worst.
1641 return Expr::CT_Can;
1642
Sebastian Redlb8a76c42010-09-10 22:34:40 +00001643 // As an extension, we assume that __attribute__((nothrow)) functions don't
1644 // throw.
1645 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1646 return Expr::CT_Cannot;
1647
Sebastian Redlce354af2010-09-10 20:55:33 +00001648 QualType T = VD->getType();
1649 const FunctionProtoType *FT;
1650 if ((FT = T->getAs<FunctionProtoType>())) {
1651 } else if (const PointerType *PT = T->getAs<PointerType>())
1652 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1653 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1654 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1655 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1656 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1657 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1658 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1659
1660 if (!FT)
1661 return Expr::CT_Can;
1662
Sebastian Redl31ad7542011-03-13 17:09:40 +00001663 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redlce354af2010-09-10 20:55:33 +00001664}
1665
1666static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1667 if (DC->isTypeDependent())
1668 return Expr::CT_Dependent;
1669
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001670 if (!DC->getTypeAsWritten()->isReferenceType())
1671 return Expr::CT_Cannot;
1672
Sebastian Redlce354af2010-09-10 20:55:33 +00001673 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1674}
1675
1676static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1677 const CXXTypeidExpr *DC) {
1678 if (DC->isTypeOperand())
1679 return Expr::CT_Cannot;
1680
1681 Expr *Op = DC->getExprOperand();
1682 if (Op->isTypeDependent())
1683 return Expr::CT_Dependent;
1684
1685 const RecordType *RT = Op->getType()->getAs<RecordType>();
1686 if (!RT)
1687 return Expr::CT_Cannot;
1688
1689 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1690 return Expr::CT_Cannot;
1691
1692 if (Op->Classify(C).isPRValue())
1693 return Expr::CT_Cannot;
1694
1695 return Expr::CT_Can;
1696}
1697
1698Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1699 // C++ [expr.unary.noexcept]p3:
1700 // [Can throw] if in a potentially-evaluated context the expression would
1701 // contain:
1702 switch (getStmtClass()) {
1703 case CXXThrowExprClass:
1704 // - a potentially evaluated throw-expression
1705 return CT_Can;
1706
1707 case CXXDynamicCastExprClass: {
1708 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1709 // where T is a reference type, that requires a run-time check
1710 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1711 if (CT == CT_Can)
1712 return CT;
1713 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1714 }
1715
1716 case CXXTypeidExprClass:
1717 // - a potentially evaluated typeid expression applied to a glvalue
1718 // expression whose type is a polymorphic class type
1719 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1720
1721 // - a potentially evaluated call to a function, member function, function
1722 // pointer, or member function pointer that does not have a non-throwing
1723 // exception-specification
1724 case CallExprClass:
1725 case CXXOperatorCallExprClass:
1726 case CXXMemberCallExprClass: {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001727 CanThrowResult CT = CanCalleeThrow(C,cast<CallExpr>(this)->getCalleeDecl());
Sebastian Redlce354af2010-09-10 20:55:33 +00001728 if (CT == CT_Can)
1729 return CT;
1730 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1731 }
1732
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001733 case CXXConstructExprClass:
1734 case CXXTemporaryObjectExprClass: {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001735 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redlce354af2010-09-10 20:55:33 +00001736 cast<CXXConstructExpr>(this)->getConstructor());
1737 if (CT == CT_Can)
1738 return CT;
1739 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1740 }
1741
1742 case CXXNewExprClass: {
1743 CanThrowResult CT = MergeCanThrow(
Sebastian Redl31ad7542011-03-13 17:09:40 +00001744 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getOperatorNew()),
1745 CanCalleeThrow(C, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redlce354af2010-09-10 20:55:33 +00001746 /*NullThrows*/false));
1747 if (CT == CT_Can)
1748 return CT;
1749 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1750 }
1751
1752 case CXXDeleteExprClass: {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001753 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redlce354af2010-09-10 20:55:33 +00001754 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1755 if (CT == CT_Can)
1756 return CT;
Sebastian Redla8bac372010-09-10 23:27:10 +00001757 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1758 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1759 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1760 Arg = Cast->getSubExpr();
1761 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1762 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
Sebastian Redl31ad7542011-03-13 17:09:40 +00001763 CanThrowResult CT2 = CanCalleeThrow(C,
Sebastian Redla8bac372010-09-10 23:27:10 +00001764 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1765 if (CT2 == CT_Can)
1766 return CT2;
1767 CT = MergeCanThrow(CT, CT2);
1768 }
1769 }
1770 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1771 }
1772
1773 case CXXBindTemporaryExprClass: {
1774 // The bound temporary has to be destroyed again, which might throw.
Sebastian Redl31ad7542011-03-13 17:09:40 +00001775 CanThrowResult CT = CanCalleeThrow(C,
Sebastian Redla8bac372010-09-10 23:27:10 +00001776 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1777 if (CT == CT_Can)
1778 return CT;
Sebastian Redlce354af2010-09-10 20:55:33 +00001779 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1780 }
1781
1782 // ObjC message sends are like function calls, but never have exception
1783 // specs.
1784 case ObjCMessageExprClass:
1785 case ObjCPropertyRefExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001786 return CT_Can;
1787
1788 // Many other things have subexpressions, so we have to test those.
1789 // Some are simple:
1790 case ParenExprClass:
1791 case MemberExprClass:
1792 case CXXReinterpretCastExprClass:
1793 case CXXConstCastExprClass:
1794 case ConditionalOperatorClass:
1795 case CompoundLiteralExprClass:
1796 case ExtVectorElementExprClass:
1797 case InitListExprClass:
1798 case DesignatedInitExprClass:
1799 case ParenListExprClass:
1800 case VAArgExprClass:
1801 case CXXDefaultArgExprClass:
John McCall5d413782010-12-06 08:20:24 +00001802 case ExprWithCleanupsClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001803 case ObjCIvarRefExprClass:
1804 case ObjCIsaExprClass:
1805 case ShuffleVectorExprClass:
1806 return CanSubExprsThrow(C, this);
1807
1808 // Some might be dependent for other reasons.
1809 case UnaryOperatorClass:
1810 case ArraySubscriptExprClass:
1811 case ImplicitCastExprClass:
1812 case CStyleCastExprClass:
1813 case CXXStaticCastExprClass:
1814 case CXXFunctionalCastExprClass:
1815 case BinaryOperatorClass:
1816 case CompoundAssignOperatorClass: {
1817 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1818 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1819 }
1820
1821 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1822 case StmtExprClass:
1823 return CT_Can;
1824
1825 case ChooseExprClass:
1826 if (isTypeDependent() || isValueDependent())
1827 return CT_Dependent;
1828 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1829
1830 // Some expressions are always dependent.
1831 case DependentScopeDeclRefExprClass:
1832 case CXXUnresolvedConstructExprClass:
1833 case CXXDependentScopeMemberExprClass:
1834 return CT_Dependent;
1835
1836 default:
1837 // All other expressions don't have subexpressions, or else they are
1838 // unevaluated.
1839 return CT_Cannot;
1840 }
1841}
1842
Ted Kremenekfff70962008-01-17 16:57:34 +00001843Expr* Expr::IgnoreParens() {
1844 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00001845 while (true) {
1846 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1847 E = P->getSubExpr();
1848 continue;
1849 }
1850 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1851 if (P->getOpcode() == UO_Extension) {
1852 E = P->getSubExpr();
1853 continue;
1854 }
1855 }
1856 return E;
1857 }
Ted Kremenekfff70962008-01-17 16:57:34 +00001858}
1859
Chris Lattnerf2660962008-02-13 01:02:39 +00001860/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1861/// or CastExprs or ImplicitCastExprs, returning their operand.
1862Expr *Expr::IgnoreParenCasts() {
1863 Expr *E = this;
1864 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001865 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001866 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001867 continue;
1868 }
1869 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001870 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001871 continue;
1872 }
1873 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1874 if (P->getOpcode() == UO_Extension) {
1875 E = P->getSubExpr();
1876 continue;
1877 }
1878 }
1879 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00001880 }
1881}
1882
John McCall5a4ce8b2010-12-04 08:24:19 +00001883/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1884/// casts. This is intended purely as a temporary workaround for code
1885/// that hasn't yet been rewritten to do the right thing about those
1886/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00001887Expr *Expr::IgnoreParenLValueCasts() {
1888 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00001889 while (true) {
John McCall34376a62010-12-04 03:47:34 +00001890 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1891 E = P->getSubExpr();
1892 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00001893 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00001894 if (P->getCastKind() == CK_LValueToRValue) {
1895 E = P->getSubExpr();
1896 continue;
1897 }
John McCall5a4ce8b2010-12-04 08:24:19 +00001898 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1899 if (P->getOpcode() == UO_Extension) {
1900 E = P->getSubExpr();
1901 continue;
1902 }
John McCall34376a62010-12-04 03:47:34 +00001903 }
1904 break;
1905 }
1906 return E;
1907}
1908
John McCalleebc8322010-05-05 22:59:52 +00001909Expr *Expr::IgnoreParenImpCasts() {
1910 Expr *E = this;
1911 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001912 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001913 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001914 continue;
1915 }
1916 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001917 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001918 continue;
1919 }
1920 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1921 if (P->getOpcode() == UO_Extension) {
1922 E = P->getSubExpr();
1923 continue;
1924 }
1925 }
1926 return E;
John McCalleebc8322010-05-05 22:59:52 +00001927 }
1928}
1929
Chris Lattneref26c772009-03-13 17:28:01 +00001930/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1931/// value (including ptr->int casts of the same size). Strip off any
1932/// ParenExpr or CastExprs, returning their operand.
1933Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1934 Expr *E = this;
1935 while (true) {
1936 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1937 E = P->getSubExpr();
1938 continue;
1939 }
Mike Stump11289f42009-09-09 15:08:12 +00001940
Chris Lattneref26c772009-03-13 17:28:01 +00001941 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1942 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00001943 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00001944 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001945
Chris Lattneref26c772009-03-13 17:28:01 +00001946 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1947 E = SE;
1948 continue;
1949 }
Mike Stump11289f42009-09-09 15:08:12 +00001950
Abramo Bagnara932e3932010-10-15 07:51:18 +00001951 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00001952 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00001953 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00001954 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00001955 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1956 E = SE;
1957 continue;
1958 }
1959 }
Mike Stump11289f42009-09-09 15:08:12 +00001960
Abramo Bagnara932e3932010-10-15 07:51:18 +00001961 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1962 if (P->getOpcode() == UO_Extension) {
1963 E = P->getSubExpr();
1964 continue;
1965 }
1966 }
1967
Chris Lattneref26c772009-03-13 17:28:01 +00001968 return E;
1969 }
1970}
1971
Douglas Gregord196a582009-12-14 19:27:10 +00001972bool Expr::isDefaultArgument() const {
1973 const Expr *E = this;
1974 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1975 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001976
Douglas Gregord196a582009-12-14 19:27:10 +00001977 return isa<CXXDefaultArgExpr>(E);
1978}
Chris Lattneref26c772009-03-13 17:28:01 +00001979
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001980/// \brief Skip over any no-op casts and any temporary-binding
1981/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00001982static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001983 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00001984 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001985 E = ICE->getSubExpr();
1986 else
1987 break;
1988 }
1989
1990 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1991 E = BE->getSubExpr();
1992
1993 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00001994 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001995 E = ICE->getSubExpr();
1996 else
1997 break;
1998 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00001999
2000 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002001}
2002
John McCall7a626f62010-09-15 10:14:12 +00002003/// isTemporaryObject - Determines if this expression produces a
2004/// temporary of the given class type.
2005bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2006 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2007 return false;
2008
Anders Carlsson66bbf502010-11-28 16:40:49 +00002009 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002010
John McCall02dc8c72010-09-15 20:59:13 +00002011 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002012 if (!E->Classify(C).isPRValue()) {
2013 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002014 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002015 return false;
2016 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002017
John McCallf4ee1dd2010-09-16 06:57:56 +00002018 // Black-list a few cases which yield pr-values of class type that don't
2019 // refer to temporaries of that type:
2020
2021 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002022 if (isa<ImplicitCastExpr>(E)) {
2023 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2024 case CK_DerivedToBase:
2025 case CK_UncheckedDerivedToBase:
2026 return false;
2027 default:
2028 break;
2029 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002030 }
2031
John McCallf4ee1dd2010-09-16 06:57:56 +00002032 // - member expressions (all)
2033 if (isa<MemberExpr>(E))
2034 return false;
2035
John McCallc07a0c72011-02-17 10:25:35 +00002036 // - opaque values (all)
2037 if (isa<OpaqueValueExpr>(E))
2038 return false;
2039
John McCall7a626f62010-09-15 10:14:12 +00002040 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002041}
2042
Douglas Gregor25b7e052011-03-02 21:06:53 +00002043bool Expr::isImplicitCXXThis() const {
2044 const Expr *E = this;
2045
2046 // Strip away parentheses and casts we don't care about.
2047 while (true) {
2048 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2049 E = Paren->getSubExpr();
2050 continue;
2051 }
2052
2053 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2054 if (ICE->getCastKind() == CK_NoOp ||
2055 ICE->getCastKind() == CK_LValueToRValue ||
2056 ICE->getCastKind() == CK_DerivedToBase ||
2057 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2058 E = ICE->getSubExpr();
2059 continue;
2060 }
2061 }
2062
2063 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2064 if (UnOp->getOpcode() == UO_Extension) {
2065 E = UnOp->getSubExpr();
2066 continue;
2067 }
2068 }
2069
2070 break;
2071 }
2072
2073 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2074 return This->isImplicit();
2075
2076 return false;
2077}
2078
Douglas Gregor4619e432008-12-05 23:32:09 +00002079/// hasAnyTypeDependentArguments - Determines if any of the expressions
2080/// in Exprs is type-dependent.
2081bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2082 for (unsigned I = 0; I < NumExprs; ++I)
2083 if (Exprs[I]->isTypeDependent())
2084 return true;
2085
2086 return false;
2087}
2088
2089/// hasAnyValueDependentArguments - Determines if any of the expressions
2090/// in Exprs is value-dependent.
2091bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2092 for (unsigned I = 0; I < NumExprs; ++I)
2093 if (Exprs[I]->isValueDependent())
2094 return true;
2095
2096 return false;
2097}
2098
John McCall8b0f4ff2010-08-02 21:13:48 +00002099bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002100 // This function is attempting whether an expression is an initializer
2101 // which can be evaluated at compile-time. isEvaluatable handles most
2102 // of the cases, but it can't deal with some initializer-specific
2103 // expressions, and it can't deal with aggregates; we deal with those here,
2104 // and fall back to isEvaluatable for the other cases.
2105
John McCall8b0f4ff2010-08-02 21:13:48 +00002106 // If we ever capture reference-binding directly in the AST, we can
2107 // kill the second parameter.
2108
2109 if (IsForRef) {
2110 EvalResult Result;
2111 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2112 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002113
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002114 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002115 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002116 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002117 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002118 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002119 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002120 case CXXTemporaryObjectExprClass:
2121 case CXXConstructExprClass: {
2122 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002123
2124 // Only if it's
2125 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00002126 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002127 if (!CE->getNumArgs()) return true;
2128
2129 // 2) an elidable trivial copy construction of an operand which is
2130 // itself a constant initializer. Note that we consider the
2131 // operand on its own, *not* as a reference binding.
2132 return CE->isElidable() &&
2133 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00002134 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002135 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002136 // This handles gcc's extension that allows global initializers like
2137 // "struct x {int x;} x = (struct x) {};".
2138 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002139 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002140 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002141 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002142 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002143 // FIXME: This doesn't deal with fields with reference types correctly.
2144 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2145 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002146 const InitListExpr *Exp = cast<InitListExpr>(this);
2147 unsigned numInits = Exp->getNumInits();
2148 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002149 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002150 return false;
2151 }
Eli Friedman384da272009-01-25 03:12:18 +00002152 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002153 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002154 case ImplicitValueInitExprClass:
2155 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002156 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002157 return cast<ParenExpr>(this)->getSubExpr()
2158 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002159 case ChooseExprClass:
2160 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2161 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002162 case UnaryOperatorClass: {
2163 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002164 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002165 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002166 break;
2167 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00002168 case BinaryOperatorClass: {
2169 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2170 // but this handles the common case.
2171 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002172 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3eb172a2009-10-13 07:14:16 +00002173 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2174 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2175 return true;
2176 break;
2177 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002178 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002179 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002180 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00002181 case CStyleCastExprClass:
2182 // Handle casts with a destination that's a struct or union; this
2183 // deals with both the gcc no-op struct cast extension and the
2184 // cast-to-union extension.
2185 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002186 return cast<CastExpr>(this)->getSubExpr()
2187 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002188
Chris Lattnera2f9bd52009-10-13 22:12:09 +00002189 // Integer->integer casts can be handled here, which is important for
2190 // things like (int)(&&x-&&y). Scary but true.
2191 if (getType()->isIntegerType() &&
2192 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002193 return cast<CastExpr>(this)->getSubExpr()
2194 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002195
Eli Friedman384da272009-01-25 03:12:18 +00002196 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002197 }
Eli Friedman384da272009-01-25 03:12:18 +00002198 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002199}
2200
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002201/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2202/// pointer constant or not, as well as the specific kind of constant detected.
2203/// Null pointer constants can be integer constant expressions with the
2204/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2205/// (a GNU extension).
2206Expr::NullPointerConstantKind
2207Expr::isNullPointerConstant(ASTContext &Ctx,
2208 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00002209 if (isValueDependent()) {
2210 switch (NPC) {
2211 case NPC_NeverValueDependent:
2212 assert(false && "Unexpected value dependent expression!");
2213 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002214
Douglas Gregor56751b52009-09-25 04:25:58 +00002215 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002216 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2217 return NPCK_ZeroInteger;
2218 else
2219 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00002220
Douglas Gregor56751b52009-09-25 04:25:58 +00002221 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002222 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00002223 }
2224 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002225
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002226 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002227 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00002228 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002229 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002230 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002231 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002232 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002233 Pointee->isVoidType() && // to void*
2234 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002235 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002236 }
Steve Naroffada7d422007-05-20 17:54:12 +00002237 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002238 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2239 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002240 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002241 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2242 // Accept ((void*)0) as a null pointer constant, as many other
2243 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002244 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002245 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002246 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002247 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002248 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002249 } else if (isa<GNUNullExpr>(this)) {
2250 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002251 return NPCK_GNUNull;
Steve Naroff09035312008-01-14 02:53:34 +00002252 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002253
Sebastian Redl576fd422009-05-10 18:38:11 +00002254 // C++0x nullptr_t is always a null pointer constant.
2255 if (getType()->isNullPtrType())
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002256 return NPCK_CXX0X_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00002257
Fariborz Jahanian3567c422010-09-27 22:42:37 +00002258 if (const RecordType *UT = getType()->getAsUnionType())
2259 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2260 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2261 const Expr *InitExpr = CLE->getInitializer();
2262 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2263 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2264 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002265 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002266 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002267 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002268 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00002269
Chris Lattner1abbd412007-06-08 17:58:43 +00002270 // If we have an integer constant expression, we need to *evaluate* it and
2271 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002272 llvm::APSInt Result;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002273 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2274
2275 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002276}
Steve Narofff7a5da12007-07-28 23:10:27 +00002277
John McCall34376a62010-12-04 03:47:34 +00002278/// \brief If this expression is an l-value for an Objective C
2279/// property, find the underlying property reference expression.
2280const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2281 const Expr *E = this;
2282 while (true) {
2283 assert((E->getValueKind() == VK_LValue &&
2284 E->getObjectKind() == OK_ObjCProperty) &&
2285 "expression is not a property reference");
2286 E = E->IgnoreParenCasts();
2287 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2288 if (BO->getOpcode() == BO_Comma) {
2289 E = BO->getRHS();
2290 continue;
2291 }
2292 }
2293
2294 break;
2295 }
2296
2297 return cast<ObjCPropertyRefExpr>(E);
2298}
2299
Douglas Gregor71235ec2009-05-02 02:18:30 +00002300FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002301 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002302
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002303 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002304 if (ICE->getCastKind() == CK_LValueToRValue ||
2305 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002306 E = ICE->getSubExpr()->IgnoreParens();
2307 else
2308 break;
2309 }
2310
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002311 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002312 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002313 if (Field->isBitField())
2314 return Field;
2315
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00002316 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2317 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2318 if (Field->isBitField())
2319 return Field;
2320
Douglas Gregor71235ec2009-05-02 02:18:30 +00002321 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2322 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2323 return BinOp->getLHS()->getBitField();
2324
2325 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002326}
2327
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002328bool Expr::refersToVectorElement() const {
2329 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002330
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002331 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002332 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002333 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002334 E = ICE->getSubExpr()->IgnoreParens();
2335 else
2336 break;
2337 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002338
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002339 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2340 return ASE->getBase()->getType()->isVectorType();
2341
2342 if (isa<ExtVectorElementExpr>(E))
2343 return true;
2344
2345 return false;
2346}
2347
Chris Lattnerb8211f62009-02-16 22:14:05 +00002348/// isArrow - Return true if the base expression is a pointer to vector,
2349/// return false if the base expression is a vector.
2350bool ExtVectorElementExpr::isArrow() const {
2351 return getBase()->getType()->isPointerType();
2352}
2353
Nate Begemance4d7fc2008-04-18 23:10:10 +00002354unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002355 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002356 return VT->getNumElements();
2357 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002358}
2359
Nate Begemanf322eab2008-05-09 06:41:27 +00002360/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002361bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002362 // FIXME: Refactor this code to an accessor on the AST node which returns the
2363 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002364 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002365
2366 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002367 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002368 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002369
Nate Begeman7e5185b2009-01-18 02:01:21 +00002370 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002371 if (Comp[0] == 's' || Comp[0] == 'S')
2372 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002373
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002374 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2375 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002376 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002377
Steve Naroff0d595ca2007-07-30 03:29:09 +00002378 return false;
2379}
Chris Lattner885b4952007-08-02 23:36:59 +00002380
Nate Begemanf322eab2008-05-09 06:41:27 +00002381/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002382void ExtVectorElementExpr::getEncodedElementAccess(
2383 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002384 llvm::StringRef Comp = Accessor->getName();
2385 if (Comp[0] == 's' || Comp[0] == 'S')
2386 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002387
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002388 bool isHi = Comp == "hi";
2389 bool isLo = Comp == "lo";
2390 bool isEven = Comp == "even";
2391 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002392
Nate Begemanf322eab2008-05-09 06:41:27 +00002393 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2394 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002395
Nate Begemanf322eab2008-05-09 06:41:27 +00002396 if (isHi)
2397 Index = e + i;
2398 else if (isLo)
2399 Index = i;
2400 else if (isEven)
2401 Index = 2 * i;
2402 else if (isOdd)
2403 Index = 2 * i + 1;
2404 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002405 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002406
Nate Begemand3862152008-05-13 21:03:02 +00002407 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002408 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002409}
2410
Douglas Gregor9a129192010-04-21 00:45:42 +00002411ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002412 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002413 SourceLocation LBracLoc,
2414 SourceLocation SuperLoc,
2415 bool IsInstanceSuper,
2416 QualType SuperType,
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,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002423 /*TypeDependent=*/false, /*ValueDependent=*/false,
2424 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002425 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2426 HasMethod(Method != 0), SuperLoc(SuperLoc),
2427 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2428 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002429 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002430{
Douglas Gregor9a129192010-04-21 00:45:42 +00002431 setReceiverPointer(SuperType.getAsOpaquePtr());
2432 if (NumArgs)
2433 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002434}
2435
Douglas Gregor9a129192010-04-21 00:45:42 +00002436ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002437 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002438 SourceLocation LBracLoc,
2439 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002440 Selector Sel,
2441 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002442 ObjCMethodDecl *Method,
2443 Expr **Args, unsigned NumArgs,
2444 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002445 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002446 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002447 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2448 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2449 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002450 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002451{
2452 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002453 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002454 for (unsigned I = 0; I != NumArgs; ++I) {
2455 if (Args[I]->isTypeDependent())
2456 ExprBits.TypeDependent = true;
2457 if (Args[I]->isValueDependent())
2458 ExprBits.ValueDependent = true;
2459 if (Args[I]->containsUnexpandedParameterPack())
2460 ExprBits.ContainsUnexpandedParameterPack = true;
2461
2462 MyArgs[I] = Args[I];
2463 }
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002464}
2465
Douglas Gregor9a129192010-04-21 00:45:42 +00002466ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002467 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002468 SourceLocation LBracLoc,
2469 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002470 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002471 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002472 ObjCMethodDecl *Method,
2473 Expr **Args, unsigned NumArgs,
2474 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002475 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002476 Receiver->isTypeDependent(),
2477 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002478 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2479 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2480 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002481 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002482{
2483 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002484 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002485 for (unsigned I = 0; I != NumArgs; ++I) {
2486 if (Args[I]->isTypeDependent())
2487 ExprBits.TypeDependent = true;
2488 if (Args[I]->isValueDependent())
2489 ExprBits.ValueDependent = true;
2490 if (Args[I]->containsUnexpandedParameterPack())
2491 ExprBits.ContainsUnexpandedParameterPack = true;
2492
2493 MyArgs[I] = Args[I];
2494 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00002495}
2496
Douglas Gregor9a129192010-04-21 00:45:42 +00002497ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002498 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002499 SourceLocation LBracLoc,
2500 SourceLocation SuperLoc,
2501 bool IsInstanceSuper,
2502 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002503 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002504 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002505 ObjCMethodDecl *Method,
2506 Expr **Args, unsigned NumArgs,
2507 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002508 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002509 NumArgs * sizeof(Expr *);
2510 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCall7decc9e2010-11-18 06:31:45 +00002511 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002512 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002513 RBracLoc);
2514}
2515
2516ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002517 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002518 SourceLocation LBracLoc,
2519 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002520 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002521 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002522 ObjCMethodDecl *Method,
2523 Expr **Args, unsigned NumArgs,
2524 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002525 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002526 NumArgs * sizeof(Expr *);
2527 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002528 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2529 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002530}
2531
2532ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002533 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002534 SourceLocation LBracLoc,
2535 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002536 Selector Sel,
2537 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002538 ObjCMethodDecl *Method,
2539 Expr **Args, unsigned NumArgs,
2540 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002541 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002542 NumArgs * sizeof(Expr *);
2543 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002544 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2545 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002546}
2547
Alexis Hunta8136cc2010-05-05 15:23:54 +00002548ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002549 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002550 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002551 NumArgs * sizeof(Expr *);
2552 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2553 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2554}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00002555
2556SourceRange ObjCMessageExpr::getReceiverRange() const {
2557 switch (getReceiverKind()) {
2558 case Instance:
2559 return getInstanceReceiver()->getSourceRange();
2560
2561 case Class:
2562 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2563
2564 case SuperInstance:
2565 case SuperClass:
2566 return getSuperLoc();
2567 }
2568
2569 return SourceLocation();
2570}
2571
Douglas Gregor9a129192010-04-21 00:45:42 +00002572Selector ObjCMessageExpr::getSelector() const {
2573 if (HasMethod)
2574 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2575 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002576 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002577}
2578
2579ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2580 switch (getReceiverKind()) {
2581 case Instance:
2582 if (const ObjCObjectPointerType *Ptr
2583 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2584 return Ptr->getInterfaceDecl();
2585 break;
2586
2587 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00002588 if (const ObjCObjectType *Ty
2589 = getClassReceiver()->getAs<ObjCObjectType>())
2590 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002591 break;
2592
2593 case SuperInstance:
2594 if (const ObjCObjectPointerType *Ptr
2595 = getSuperType()->getAs<ObjCObjectPointerType>())
2596 return Ptr->getInterfaceDecl();
2597 break;
2598
2599 case SuperClass:
Argyrios Kyrtzidis1b9747f2011-01-25 00:03:48 +00002600 if (const ObjCObjectType *Iface
2601 = getSuperType()->getAs<ObjCObjectType>())
2602 return Iface->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002603 break;
2604 }
2605
2606 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002607}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002608
Jay Foad39c79802011-01-12 09:06:06 +00002609bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002610 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002611}
2612
Douglas Gregora6e053e2010-12-15 01:34:56 +00002613ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2614 QualType Type, SourceLocation BLoc,
2615 SourceLocation RP)
2616 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2617 Type->isDependentType(), Type->isDependentType(),
2618 Type->containsUnexpandedParameterPack()),
2619 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2620{
2621 SubExprs = new (C) Stmt*[nexpr];
2622 for (unsigned i = 0; i < nexpr; i++) {
2623 if (args[i]->isTypeDependent())
2624 ExprBits.TypeDependent = true;
2625 if (args[i]->isValueDependent())
2626 ExprBits.ValueDependent = true;
2627 if (args[i]->containsUnexpandedParameterPack())
2628 ExprBits.ContainsUnexpandedParameterPack = true;
2629
2630 SubExprs[i] = args[i];
2631 }
2632}
2633
Nate Begeman48745922009-08-12 02:28:50 +00002634void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2635 unsigned NumExprs) {
2636 if (SubExprs) C.Deallocate(SubExprs);
2637
2638 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002639 this->NumExprs = NumExprs;
2640 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002641}
Nate Begeman48745922009-08-12 02:28:50 +00002642
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002643//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002644// DesignatedInitExpr
2645//===----------------------------------------------------------------------===//
2646
2647IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2648 assert(Kind == FieldDesignator && "Only valid on a field designator");
2649 if (Field.NameOrField & 0x01)
2650 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2651 else
2652 return getField()->getIdentifier();
2653}
2654
Alexis Hunta8136cc2010-05-05 15:23:54 +00002655DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002656 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002657 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002658 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002659 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002660 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002661 unsigned NumIndexExprs,
2662 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002663 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00002664 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002665 Init->isTypeDependent(), Init->isValueDependent(),
2666 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00002667 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2668 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002669 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002670
2671 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00002672 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002673 *Child++ = Init;
2674
2675 // Copy the designators and their subexpressions, computing
2676 // value-dependence along the way.
2677 unsigned IndexIdx = 0;
2678 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002679 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002680
2681 if (this->Designators[I].isArrayDesignator()) {
2682 // Compute type- and value-dependence.
2683 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002684 if (Index->isTypeDependent() || Index->isValueDependent())
2685 ExprBits.ValueDependent = true;
2686
2687 // Propagate unexpanded parameter packs.
2688 if (Index->containsUnexpandedParameterPack())
2689 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002690
2691 // Copy the index expressions into permanent storage.
2692 *Child++ = IndexExprs[IndexIdx++];
2693 } else if (this->Designators[I].isArrayRangeDesignator()) {
2694 // Compute type- and value-dependence.
2695 Expr *Start = IndexExprs[IndexIdx];
2696 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002697 if (Start->isTypeDependent() || Start->isValueDependent() ||
2698 End->isTypeDependent() || End->isValueDependent())
2699 ExprBits.ValueDependent = true;
2700
2701 // Propagate unexpanded parameter packs.
2702 if (Start->containsUnexpandedParameterPack() ||
2703 End->containsUnexpandedParameterPack())
2704 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002705
2706 // Copy the start/end expressions into permanent storage.
2707 *Child++ = IndexExprs[IndexIdx++];
2708 *Child++ = IndexExprs[IndexIdx++];
2709 }
2710 }
2711
2712 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002713}
2714
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002715DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002716DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002717 unsigned NumDesignators,
2718 Expr **IndexExprs, unsigned NumIndexExprs,
2719 SourceLocation ColonOrEqualLoc,
2720 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002721 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002722 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002723 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002724 ColonOrEqualLoc, UsesColonSyntax,
2725 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002726}
2727
Mike Stump11289f42009-09-09 15:08:12 +00002728DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002729 unsigned NumIndexExprs) {
2730 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2731 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2732 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2733}
2734
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002735void DesignatedInitExpr::setDesignators(ASTContext &C,
2736 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002737 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002738 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002739 NumDesignators = NumDesigs;
2740 for (unsigned I = 0; I != NumDesigs; ++I)
2741 Designators[I] = Desigs[I];
2742}
2743
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00002744SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
2745 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
2746 if (size() == 1)
2747 return DIE->getDesignator(0)->getSourceRange();
2748 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
2749 DIE->getDesignator(size()-1)->getEndLocation());
2750}
2751
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002752SourceRange DesignatedInitExpr::getSourceRange() const {
2753 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002754 Designator &First =
2755 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002756 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002757 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002758 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2759 else
2760 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2761 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002762 StartLoc =
2763 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002764 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2765}
2766
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002767Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2768 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2769 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2770 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002771 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2772 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2773}
2774
2775Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002776 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002777 "Requires array range designator");
2778 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2779 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002780 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2781 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2782}
2783
2784Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002785 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002786 "Requires array range designator");
2787 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2788 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002789 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2790 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2791}
2792
Douglas Gregord5846a12009-04-15 06:41:24 +00002793/// \brief Replaces the designator at index @p Idx with the series
2794/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002795void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002796 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002797 const Designator *Last) {
2798 unsigned NumNewDesignators = Last - First;
2799 if (NumNewDesignators == 0) {
2800 std::copy_backward(Designators + Idx + 1,
2801 Designators + NumDesignators,
2802 Designators + Idx);
2803 --NumNewDesignators;
2804 return;
2805 } else if (NumNewDesignators == 1) {
2806 Designators[Idx] = *First;
2807 return;
2808 }
2809
Mike Stump11289f42009-09-09 15:08:12 +00002810 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002811 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002812 std::copy(Designators, Designators + Idx, NewDesignators);
2813 std::copy(First, Last, NewDesignators + Idx);
2814 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2815 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00002816 Designators = NewDesignators;
2817 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2818}
2819
Mike Stump11289f42009-09-09 15:08:12 +00002820ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002821 Expr **exprs, unsigned nexprs,
2822 SourceLocation rparenloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00002823 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2824 false, false, false),
2825 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002826
Nate Begeman5ec4b312009-08-10 23:49:36 +00002827 Exprs = new (C) Stmt*[nexprs];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002828 for (unsigned i = 0; i != nexprs; ++i) {
2829 if (exprs[i]->isTypeDependent())
2830 ExprBits.TypeDependent = true;
2831 if (exprs[i]->isValueDependent())
2832 ExprBits.ValueDependent = true;
2833 if (exprs[i]->containsUnexpandedParameterPack())
2834 ExprBits.ContainsUnexpandedParameterPack = true;
2835
Nate Begeman5ec4b312009-08-10 23:49:36 +00002836 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002837 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00002838}
2839
John McCall1bf58462011-02-16 08:02:54 +00002840const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
2841 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
2842 e = ewc->getSubExpr();
2843 e = cast<CXXConstructExpr>(e)->getArg(0);
2844 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
2845 e = ice->getSubExpr();
2846 return cast<OpaqueValueExpr>(e);
2847}
2848
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002849//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002850// ExprIterator.
2851//===----------------------------------------------------------------------===//
2852
2853Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2854Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2855Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2856const Expr* ConstExprIterator::operator[](size_t idx) const {
2857 return cast<Expr>(I[idx]);
2858}
2859const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2860const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2861
2862//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002863// Child Iterators for iterating over subexpressions/substatements
2864//===----------------------------------------------------------------------===//
2865
Peter Collingbournee190dee2011-03-11 19:24:49 +00002866// UnaryExprOrTypeTraitExpr
2867Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002868 // If this is of a type and the type is a VLA type (and not a typedef), the
2869 // size expression of the VLA needs to be treated as an executable expression.
2870 // Why isn't this weirdness documented better in StmtIterator?
2871 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002872 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00002873 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00002874 return child_range(child_iterator(T), child_iterator());
2875 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00002876 }
John McCallbd066782011-02-09 08:16:59 +00002877 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002878}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002879
Steve Naroffd54978b2007-09-18 23:55:05 +00002880// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00002881Stmt::child_range ObjCMessageExpr::children() {
2882 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00002883 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00002884 begin = reinterpret_cast<Stmt **>(this + 1);
2885 else
2886 begin = reinterpret_cast<Stmt **>(getArgs());
2887 return child_range(begin,
2888 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00002889}
2890
Steve Naroffc540d662008-09-03 18:15:37 +00002891// Blocks
John McCall351762c2011-02-07 10:33:21 +00002892BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregor476e3022011-01-19 21:32:01 +00002893 SourceLocation l, bool ByRef,
John McCall351762c2011-02-07 10:33:21 +00002894 bool constAdded)
Douglas Gregorf144f4f2011-01-19 21:52:31 +00002895 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregor476e3022011-01-19 21:32:01 +00002896 d->isParameterPack()),
John McCall351762c2011-02-07 10:33:21 +00002897 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregor476e3022011-01-19 21:32:01 +00002898{
Douglas Gregorf144f4f2011-01-19 21:52:31 +00002899 bool TypeDependent = false;
2900 bool ValueDependent = false;
2901 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
2902 ExprBits.TypeDependent = TypeDependent;
2903 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor476e3022011-01-19 21:32:01 +00002904}
2905