blob: 8e44c073682bf44bbb4b83cf9ae1ab3f9a658aba [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,
514 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;
530 SL->TokLocs[0] = Loc[0];
531 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000532
Chris Lattner630970d2009-02-18 05:49:11 +0000533 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000534 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
535 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000536}
537
Douglas Gregor958dfc92009-04-15 16:35:07 +0000538StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
539 void *Mem = C.Allocate(sizeof(StringLiteral)+
540 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000541 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000542 StringLiteral *SL = new (Mem) StringLiteral(QualType());
543 SL->StrData = 0;
544 SL->ByteLength = 0;
545 SL->NumConcatenated = NumStrs;
546 return SL;
547}
548
Daniel Dunbar36217882009-09-22 03:27:33 +0000549void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbar36217882009-09-22 03:27:33 +0000550 char *AStrData = new (C, 1) char[Str.size()];
551 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000552 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000553 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000554}
555
Chris Lattnere925d612010-11-17 07:37:15 +0000556/// getLocationOfByte - Return a source location that points to the specified
557/// byte of this string literal.
558///
559/// Strings are amazingly complex. They can be formed from multiple tokens and
560/// can have escape sequences in them in addition to the usual trigraph and
561/// escaped newline business. This routine handles this complexity.
562///
563SourceLocation StringLiteral::
564getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
565 const LangOptions &Features, const TargetInfo &Target) const {
566 assert(!isWide() && "This doesn't work for wide strings yet");
567
568 // Loop over all of the tokens in this string until we find the one that
569 // contains the byte we're looking for.
570 unsigned TokNo = 0;
571 while (1) {
572 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
573 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
574
575 // Get the spelling of the string so that we can get the data that makes up
576 // the string literal, not the identifier for the macro it is potentially
577 // expanded through.
578 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
579
580 // Re-lex the token to get its length and original spelling.
581 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
582 bool Invalid = false;
583 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
584 if (Invalid)
585 return StrTokSpellingLoc;
586
587 const char *StrData = Buffer.data()+LocInfo.second;
588
589 // Create a langops struct and enable trigraphs. This is sufficient for
590 // relexing tokens.
591 LangOptions LangOpts;
592 LangOpts.Trigraphs = true;
593
594 // Create a lexer starting at the beginning of this token.
595 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
596 Buffer.end());
597 Token TheTok;
598 TheLexer.LexFromRawLexer(TheTok);
599
600 // Use the StringLiteralParser to compute the length of the string in bytes.
601 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
602 unsigned TokNumBytes = SLP.GetStringLength();
603
604 // If the byte is in this token, return the location of the byte.
605 if (ByteNo < TokNumBytes ||
606 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
607 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
608
609 // Now that we know the offset of the token in the spelling, use the
610 // preprocessor to get the offset in the original source.
611 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
612 }
613
614 // Move to the next string token.
615 ++TokNo;
616 ByteNo -= TokNumBytes;
617 }
618}
619
620
621
Chris Lattner1b926492006-08-23 06:42:10 +0000622/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
623/// corresponds to, e.g. "sizeof" or "[pre]++".
624const char *UnaryOperator::getOpcodeStr(Opcode Op) {
625 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000626 default: assert(0 && "Unknown unary operator");
John McCalle3027922010-08-25 11:45:40 +0000627 case UO_PostInc: return "++";
628 case UO_PostDec: return "--";
629 case UO_PreInc: return "++";
630 case UO_PreDec: return "--";
631 case UO_AddrOf: return "&";
632 case UO_Deref: return "*";
633 case UO_Plus: return "+";
634 case UO_Minus: return "-";
635 case UO_Not: return "~";
636 case UO_LNot: return "!";
637 case UO_Real: return "__real";
638 case UO_Imag: return "__imag";
639 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000640 }
641}
642
John McCalle3027922010-08-25 11:45:40 +0000643UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000644UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
645 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000646 default: assert(false && "No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000647 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
648 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
649 case OO_Amp: return UO_AddrOf;
650 case OO_Star: return UO_Deref;
651 case OO_Plus: return UO_Plus;
652 case OO_Minus: return UO_Minus;
653 case OO_Tilde: return UO_Not;
654 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000655 }
656}
657
658OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
659 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000660 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
661 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
662 case UO_AddrOf: return OO_Amp;
663 case UO_Deref: return OO_Star;
664 case UO_Plus: return OO_Plus;
665 case UO_Minus: return OO_Minus;
666 case UO_Not: return OO_Tilde;
667 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000668 default: return OO_None;
669 }
670}
671
672
Chris Lattner0eedafe2006-08-24 04:56:27 +0000673//===----------------------------------------------------------------------===//
674// Postfix Operators.
675//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000676
Peter Collingbourne3a347252011-02-08 21:18:02 +0000677CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
678 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCall7decc9e2010-11-18 06:31:45 +0000679 SourceLocation rparenloc)
680 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000681 fn->isTypeDependent(),
682 fn->isValueDependent(),
683 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000684 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000685
Peter Collingbourne3a347252011-02-08 21:18:02 +0000686 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +0000687 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000688 for (unsigned i = 0; i != numargs; ++i) {
689 if (args[i]->isTypeDependent())
690 ExprBits.TypeDependent = true;
691 if (args[i]->isValueDependent())
692 ExprBits.ValueDependent = true;
693 if (args[i]->containsUnexpandedParameterPack())
694 ExprBits.ContainsUnexpandedParameterPack = true;
695
Peter Collingbourne3a347252011-02-08 21:18:02 +0000696 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000697 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000698
Peter Collingbourne3a347252011-02-08 21:18:02 +0000699 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +0000700 RParenLoc = rparenloc;
701}
Nate Begeman1e36a852008-01-17 17:46:27 +0000702
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000703CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCall7decc9e2010-11-18 06:31:45 +0000704 QualType t, ExprValueKind VK, SourceLocation rparenloc)
705 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000706 fn->isTypeDependent(),
707 fn->isValueDependent(),
708 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000709 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000710
Peter Collingbourne3a347252011-02-08 21:18:02 +0000711 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000712 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000713 for (unsigned i = 0; i != numargs; ++i) {
714 if (args[i]->isTypeDependent())
715 ExprBits.TypeDependent = true;
716 if (args[i]->isValueDependent())
717 ExprBits.ValueDependent = true;
718 if (args[i]->containsUnexpandedParameterPack())
719 ExprBits.ContainsUnexpandedParameterPack = true;
720
Peter Collingbourne3a347252011-02-08 21:18:02 +0000721 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000722 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000723
Peter Collingbourne3a347252011-02-08 21:18:02 +0000724 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000725 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000726}
727
Mike Stump11289f42009-09-09 15:08:12 +0000728CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
729 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000730 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +0000731 SubExprs = new (C) Stmt*[PREARGS_START];
732 CallExprBits.NumPreArgs = 0;
733}
734
735CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
736 EmptyShell Empty)
737 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
738 // FIXME: Why do we allocate this?
739 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
740 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +0000741}
742
Nuno Lopes518e3702009-12-20 23:11:08 +0000743Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000744 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl2b1832e2010-09-10 20:55:30 +0000745 // If we're calling a dereference, look at the pointer instead.
746 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
747 if (BO->isPtrMemOp())
748 CEE = BO->getRHS()->IgnoreParenCasts();
749 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
750 if (UO->getOpcode() == UO_Deref)
751 CEE = UO->getSubExpr()->IgnoreParenCasts();
752 }
Chris Lattner52301912009-07-17 15:46:27 +0000753 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000754 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000755 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
756 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000757
758 return 0;
759}
760
Nuno Lopes518e3702009-12-20 23:11:08 +0000761FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000762 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000763}
764
Chris Lattnere4407ed2007-12-28 05:25:02 +0000765/// setNumArgs - This changes the number of arguments present in this call.
766/// Any orphaned expressions are deleted by this, and any new operands are set
767/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000768void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000769 // No change, just return.
770 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000771
Chris Lattnere4407ed2007-12-28 05:25:02 +0000772 // If shrinking # arguments, just delete the extras and forgot them.
773 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000774 this->NumArgs = NumArgs;
775 return;
776 }
777
778 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000779 unsigned NumPreArgs = getNumPreArgs();
780 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000781 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000782 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000783 NewSubExprs[i] = SubExprs[i];
784 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000785 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
786 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000787 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000788
Douglas Gregorba6e5572009-04-17 21:46:47 +0000789 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000790 SubExprs = NewSubExprs;
791 this->NumArgs = NumArgs;
792}
793
Chris Lattner01ff98a2008-10-06 05:00:53 +0000794/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
795/// not, return 0.
Jay Foad39c79802011-01-12 09:06:06 +0000796unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000797 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000798 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000799 // ImplicitCastExpr.
800 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
801 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000802 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000803
Steve Narofff6e3b3292008-01-31 01:07:12 +0000804 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
805 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000806 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000807
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000808 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
809 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000810 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000811
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000812 if (!FDecl->getIdentifier())
813 return 0;
814
Douglas Gregor15fc9562009-09-12 00:22:50 +0000815 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000816}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000817
Anders Carlsson00a27592009-05-26 04:57:27 +0000818QualType CallExpr::getCallReturnType() const {
819 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000820 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000821 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000822 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000823 CalleeType = BPT->getPointeeType();
Douglas Gregor603d81b2010-07-13 08:18:22 +0000824 else if (const MemberPointerType *MPT
825 = CalleeType->getAs<MemberPointerType>())
826 CalleeType = MPT->getPointeeType();
827
John McCall9dd450b2009-09-21 23:43:11 +0000828 const FunctionType *FnType = CalleeType->getAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000829 return FnType->getResultType();
830}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000831
John McCall701417a2011-02-21 06:23:05 +0000832SourceRange CallExpr::getSourceRange() const {
833 if (isa<CXXOperatorCallExpr>(this))
834 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
835
836 SourceLocation begin = getCallee()->getLocStart();
837 if (begin.isInvalid() && getNumArgs() > 0)
838 begin = getArg(0)->getLocStart();
839 SourceLocation end = getRParenLoc();
840 if (end.isInvalid() && getNumArgs() > 0)
841 end = getArg(getNumArgs() - 1)->getLocEnd();
842 return SourceRange(begin, end);
843}
844
Alexis Hunta8136cc2010-05-05 15:23:54 +0000845OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000846 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000847 TypeSourceInfo *tsi,
848 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000849 Expr** exprsPtr, unsigned numExprs,
850 SourceLocation RParenLoc) {
851 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000852 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000853 sizeof(Expr*) * numExprs);
854
855 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
856 exprsPtr, numExprs, RParenLoc);
857}
858
859OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
860 unsigned numComps, unsigned numExprs) {
861 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
862 sizeof(OffsetOfNode) * numComps +
863 sizeof(Expr*) * numExprs);
864 return new (Mem) OffsetOfExpr(numComps, numExprs);
865}
866
Alexis Hunta8136cc2010-05-05 15:23:54 +0000867OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000868 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000869 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000870 Expr** exprsPtr, unsigned numExprs,
871 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +0000872 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
873 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000874 /*ValueDependent=*/tsi->getType()->isDependentType(),
875 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000876 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
877 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000878{
879 for(unsigned i = 0; i < numComps; ++i) {
880 setComponent(i, compsPtr[i]);
881 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000882
Douglas Gregor882211c2010-04-28 22:16:22 +0000883 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000884 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
885 ExprBits.ValueDependent = true;
886 if (exprsPtr[i]->containsUnexpandedParameterPack())
887 ExprBits.ContainsUnexpandedParameterPack = true;
888
Douglas Gregor882211c2010-04-28 22:16:22 +0000889 setIndexExpr(i, exprsPtr[i]);
890 }
891}
892
893IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
894 assert(getKind() == Field || getKind() == Identifier);
895 if (getKind() == Field)
896 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000897
Douglas Gregor882211c2010-04-28 22:16:22 +0000898 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
899}
900
Mike Stump11289f42009-09-09 15:08:12 +0000901MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +0000902 NestedNameSpecifierLoc QualifierLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000903 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000904 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000905 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +0000906 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +0000907 QualType ty,
908 ExprValueKind vk,
909 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000910 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000911
Douglas Gregorea972d32011-02-28 21:54:11 +0000912 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +0000913 founddecl.getDecl() != memberdecl ||
914 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000915 if (hasQualOrFound)
916 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000917
John McCall6b51f282009-11-23 01:53:49 +0000918 if (targs)
919 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000920
Chris Lattner5c0b4052010-10-30 05:14:06 +0000921 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +0000922 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
923 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +0000924
925 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000926 // FIXME: Wrong. We should be looking at the member declaration we found.
927 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +0000928 E->setValueDependent(true);
929 E->setTypeDependent(true);
930 }
931 E->HasQualifierOrFoundDecl = true;
932
933 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +0000934 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +0000935 NQ->FoundDecl = founddecl;
936 }
937
938 if (targs) {
939 E->HasExplicitTemplateArgumentList = true;
John McCallb3774b52010-08-19 23:49:38 +0000940 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall16df1e52010-03-30 21:47:33 +0000941 }
942
943 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000944}
945
Anders Carlsson496335e2009-09-03 00:59:21 +0000946const char *CastExpr::getCastKindName() const {
947 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +0000948 case CK_Dependent:
949 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +0000950 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +0000951 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +0000952 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +0000953 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +0000954 case CK_LValueToRValue:
955 return "LValueToRValue";
John McCall34376a62010-12-04 03:47:34 +0000956 case CK_GetObjCProperty:
957 return "GetObjCProperty";
John McCalle3027922010-08-25 11:45:40 +0000958 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +0000959 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +0000960 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +0000961 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +0000962 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +0000963 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000964 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +0000965 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000966 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +0000967 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +0000968 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000969 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +0000970 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000971 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000972 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000973 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000974 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000975 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +0000976 case CK_NullToPointer:
977 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +0000978 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000979 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000980 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000981 return "DerivedToBaseMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000982 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000983 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +0000984 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000985 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +0000986 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000987 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +0000988 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000989 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +0000990 case CK_PointerToBoolean:
991 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +0000992 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +0000993 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +0000994 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +0000995 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +0000996 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +0000997 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +0000998 case CK_IntegralToBoolean:
999 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001000 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001001 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001002 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001003 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001004 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001005 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001006 case CK_FloatingToBoolean:
1007 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001008 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001009 return "MemberPointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001010 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahaniane19122f2009-12-08 23:46:15 +00001011 return "AnyPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001012 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001013 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001014 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001015 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001016 case CK_FloatingRealToComplex:
1017 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001018 case CK_FloatingComplexToReal:
1019 return "FloatingComplexToReal";
1020 case CK_FloatingComplexToBoolean:
1021 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001022 case CK_FloatingComplexCast:
1023 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001024 case CK_FloatingComplexToIntegralComplex:
1025 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001026 case CK_IntegralRealToComplex:
1027 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001028 case CK_IntegralComplexToReal:
1029 return "IntegralComplexToReal";
1030 case CK_IntegralComplexToBoolean:
1031 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001032 case CK_IntegralComplexCast:
1033 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001034 case CK_IntegralComplexToFloatingComplex:
1035 return "IntegralComplexToFloatingComplex";
Anders Carlsson496335e2009-09-03 00:59:21 +00001036 }
Mike Stump11289f42009-09-09 15:08:12 +00001037
John McCallc5e62b42010-11-13 09:02:35 +00001038 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001039 return 0;
1040}
1041
Douglas Gregord196a582009-12-14 19:27:10 +00001042Expr *CastExpr::getSubExprAsWritten() {
1043 Expr *SubExpr = 0;
1044 CastExpr *E = this;
1045 do {
1046 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001047
Douglas Gregord196a582009-12-14 19:27:10 +00001048 // Skip any temporary bindings; they're implicit.
1049 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1050 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001051
Douglas Gregord196a582009-12-14 19:27:10 +00001052 // Conversions by constructor and conversion functions have a
1053 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001054 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001055 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001056 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001057 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001058
Douglas Gregord196a582009-12-14 19:27:10 +00001059 // If the subexpression we're left with is an implicit cast, look
1060 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001061 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1062
Douglas Gregord196a582009-12-14 19:27:10 +00001063 return SubExpr;
1064}
1065
John McCallcf142162010-08-07 06:22:56 +00001066CXXBaseSpecifier **CastExpr::path_buffer() {
1067 switch (getStmtClass()) {
1068#define ABSTRACT_STMT(x)
1069#define CASTEXPR(Type, Base) \
1070 case Stmt::Type##Class: \
1071 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1072#define STMT(Type, Base)
1073#include "clang/AST/StmtNodes.inc"
1074 default:
1075 llvm_unreachable("non-cast expressions not possible here");
1076 return 0;
1077 }
1078}
1079
1080void CastExpr::setCastPath(const CXXCastPath &Path) {
1081 assert(Path.size() == path_size());
1082 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1083}
1084
1085ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1086 CastKind Kind, Expr *Operand,
1087 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001088 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001089 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1090 void *Buffer =
1091 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1092 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001093 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001094 if (PathSize) E->setCastPath(*BasePath);
1095 return E;
1096}
1097
1098ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1099 unsigned PathSize) {
1100 void *Buffer =
1101 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1102 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1103}
1104
1105
1106CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001107 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001108 const CXXCastPath *BasePath,
1109 TypeSourceInfo *WrittenTy,
1110 SourceLocation L, SourceLocation R) {
1111 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1112 void *Buffer =
1113 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1114 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001115 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001116 if (PathSize) E->setCastPath(*BasePath);
1117 return E;
1118}
1119
1120CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1121 void *Buffer =
1122 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1123 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1124}
1125
Chris Lattner1b926492006-08-23 06:42:10 +00001126/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1127/// corresponds to, e.g. "<<=".
1128const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1129 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001130 case BO_PtrMemD: return ".*";
1131 case BO_PtrMemI: return "->*";
1132 case BO_Mul: return "*";
1133 case BO_Div: return "/";
1134 case BO_Rem: return "%";
1135 case BO_Add: return "+";
1136 case BO_Sub: return "-";
1137 case BO_Shl: return "<<";
1138 case BO_Shr: return ">>";
1139 case BO_LT: return "<";
1140 case BO_GT: return ">";
1141 case BO_LE: return "<=";
1142 case BO_GE: return ">=";
1143 case BO_EQ: return "==";
1144 case BO_NE: return "!=";
1145 case BO_And: return "&";
1146 case BO_Xor: return "^";
1147 case BO_Or: return "|";
1148 case BO_LAnd: return "&&";
1149 case BO_LOr: return "||";
1150 case BO_Assign: return "=";
1151 case BO_MulAssign: return "*=";
1152 case BO_DivAssign: return "/=";
1153 case BO_RemAssign: return "%=";
1154 case BO_AddAssign: return "+=";
1155 case BO_SubAssign: return "-=";
1156 case BO_ShlAssign: return "<<=";
1157 case BO_ShrAssign: return ">>=";
1158 case BO_AndAssign: return "&=";
1159 case BO_XorAssign: return "^=";
1160 case BO_OrAssign: return "|=";
1161 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001162 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001163
1164 return "";
Chris Lattner1b926492006-08-23 06:42:10 +00001165}
Steve Naroff47500512007-04-19 23:00:49 +00001166
John McCalle3027922010-08-25 11:45:40 +00001167BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001168BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1169 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +00001170 default: assert(false && "Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001171 case OO_Plus: return BO_Add;
1172 case OO_Minus: return BO_Sub;
1173 case OO_Star: return BO_Mul;
1174 case OO_Slash: return BO_Div;
1175 case OO_Percent: return BO_Rem;
1176 case OO_Caret: return BO_Xor;
1177 case OO_Amp: return BO_And;
1178 case OO_Pipe: return BO_Or;
1179 case OO_Equal: return BO_Assign;
1180 case OO_Less: return BO_LT;
1181 case OO_Greater: return BO_GT;
1182 case OO_PlusEqual: return BO_AddAssign;
1183 case OO_MinusEqual: return BO_SubAssign;
1184 case OO_StarEqual: return BO_MulAssign;
1185 case OO_SlashEqual: return BO_DivAssign;
1186 case OO_PercentEqual: return BO_RemAssign;
1187 case OO_CaretEqual: return BO_XorAssign;
1188 case OO_AmpEqual: return BO_AndAssign;
1189 case OO_PipeEqual: return BO_OrAssign;
1190 case OO_LessLess: return BO_Shl;
1191 case OO_GreaterGreater: return BO_Shr;
1192 case OO_LessLessEqual: return BO_ShlAssign;
1193 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1194 case OO_EqualEqual: return BO_EQ;
1195 case OO_ExclaimEqual: return BO_NE;
1196 case OO_LessEqual: return BO_LE;
1197 case OO_GreaterEqual: return BO_GE;
1198 case OO_AmpAmp: return BO_LAnd;
1199 case OO_PipePipe: return BO_LOr;
1200 case OO_Comma: return BO_Comma;
1201 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001202 }
1203}
1204
1205OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1206 static const OverloadedOperatorKind OverOps[] = {
1207 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1208 OO_Star, OO_Slash, OO_Percent,
1209 OO_Plus, OO_Minus,
1210 OO_LessLess, OO_GreaterGreater,
1211 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1212 OO_EqualEqual, OO_ExclaimEqual,
1213 OO_Amp,
1214 OO_Caret,
1215 OO_Pipe,
1216 OO_AmpAmp,
1217 OO_PipePipe,
1218 OO_Equal, OO_StarEqual,
1219 OO_SlashEqual, OO_PercentEqual,
1220 OO_PlusEqual, OO_MinusEqual,
1221 OO_LessLessEqual, OO_GreaterGreaterEqual,
1222 OO_AmpEqual, OO_CaretEqual,
1223 OO_PipeEqual,
1224 OO_Comma
1225 };
1226 return OverOps[Opc];
1227}
1228
Ted Kremenekac034612010-04-13 23:39:13 +00001229InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +00001230 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001231 SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001232 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1233 false),
Ted Kremenekac034612010-04-13 23:39:13 +00001234 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +00001235 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001236 UnionFieldInit(0), HadArrayRangeDesignator(false)
1237{
Ted Kremenek013041e2010-02-19 01:50:18 +00001238 for (unsigned I = 0; I != numInits; ++I) {
1239 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001240 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001241 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001242 ExprBits.ValueDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001243 if (initExprs[I]->containsUnexpandedParameterPack())
1244 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001245 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001246
Ted Kremenekac034612010-04-13 23:39:13 +00001247 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001248}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001249
Ted Kremenekac034612010-04-13 23:39:13 +00001250void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001251 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001252 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001253}
1254
Ted Kremenekac034612010-04-13 23:39:13 +00001255void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001256 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001257}
1258
Ted Kremenekac034612010-04-13 23:39:13 +00001259Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001260 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001261 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001262 InitExprs.back() = expr;
1263 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001264 }
Mike Stump11289f42009-09-09 15:08:12 +00001265
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001266 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1267 InitExprs[Init] = expr;
1268 return Result;
1269}
1270
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001271SourceRange InitListExpr::getSourceRange() const {
1272 if (SyntacticForm)
1273 return SyntacticForm->getSourceRange();
1274 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1275 if (Beg.isInvalid()) {
1276 // Find the first non-null initializer.
1277 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1278 E = InitExprs.end();
1279 I != E; ++I) {
1280 if (Stmt *S = *I) {
1281 Beg = S->getLocStart();
1282 break;
1283 }
1284 }
1285 }
1286 if (End.isInvalid()) {
1287 // Find the first non-null initializer from the end.
1288 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1289 E = InitExprs.rend();
1290 I != E; ++I) {
1291 if (Stmt *S = *I) {
1292 End = S->getSourceRange().getEnd();
1293 break;
1294 }
1295 }
1296 }
1297 return SourceRange(Beg, End);
1298}
1299
Steve Naroff991e99d2008-09-04 15:31:07 +00001300/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001301///
1302const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001303 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +00001304 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001305}
1306
Mike Stump11289f42009-09-09 15:08:12 +00001307SourceLocation BlockExpr::getCaretLocation() const {
1308 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001309}
Mike Stump11289f42009-09-09 15:08:12 +00001310const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001311 return TheBlock->getBody();
1312}
Mike Stump11289f42009-09-09 15:08:12 +00001313Stmt *BlockExpr::getBody() {
1314 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001315}
Steve Naroff415d3d52008-10-08 17:01:13 +00001316
1317
Chris Lattner1ec5f562007-06-27 05:38:08 +00001318//===----------------------------------------------------------------------===//
1319// Generic Expression Routines
1320//===----------------------------------------------------------------------===//
1321
Chris Lattner237f2752009-02-14 07:37:35 +00001322/// isUnusedResultAWarning - Return true if this immediate expression should
1323/// be warned about if the result is unused. If so, fill in Loc and Ranges
1324/// with location to warn on and the source range[s] to report with the
1325/// warning.
1326bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +00001327 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001328 // Don't warn if the expr is type dependent. The type could end up
1329 // instantiating to void.
1330 if (isTypeDependent())
1331 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001332
Chris Lattner1ec5f562007-06-27 05:38:08 +00001333 switch (getStmtClass()) {
1334 default:
John McCallc493a732010-03-12 07:11:26 +00001335 if (getType()->isVoidType())
1336 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001337 Loc = getExprLoc();
1338 R1 = getSourceRange();
1339 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001340 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001341 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +00001342 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001343 case UnaryOperatorClass: {
1344 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001345
Chris Lattner1ec5f562007-06-27 05:38:08 +00001346 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001347 default: break;
John McCalle3027922010-08-25 11:45:40 +00001348 case UO_PostInc:
1349 case UO_PostDec:
1350 case UO_PreInc:
1351 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001352 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001353 case UO_Deref:
Chris Lattnera44d1162007-06-27 05:58:59 +00001354 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001355 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001356 return false;
1357 break;
John McCalle3027922010-08-25 11:45:40 +00001358 case UO_Real:
1359 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001360 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001361 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1362 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001363 return false;
1364 break;
John McCalle3027922010-08-25 11:45:40 +00001365 case UO_Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001366 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001367 }
Chris Lattner237f2752009-02-14 07:37:35 +00001368 Loc = UO->getOperatorLoc();
1369 R1 = UO->getSubExpr()->getSourceRange();
1370 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001371 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001372 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001373 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001374 switch (BO->getOpcode()) {
1375 default:
1376 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001377 // Consider the RHS of comma for side effects. LHS was checked by
1378 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001379 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001380 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1381 // lvalue-ness) of an assignment written in a macro.
1382 if (IntegerLiteral *IE =
1383 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1384 if (IE->getValue() == 0)
1385 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001386 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1387 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001388 case BO_LAnd:
1389 case BO_LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001390 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1391 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1392 return false;
1393 break;
John McCall1e3715a2010-02-16 04:10:53 +00001394 }
Chris Lattner237f2752009-02-14 07:37:35 +00001395 if (BO->isAssignmentOp())
1396 return false;
1397 Loc = BO->getOperatorLoc();
1398 R1 = BO->getLHS()->getSourceRange();
1399 R2 = BO->getRHS()->getSourceRange();
1400 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001401 }
Chris Lattner86928112007-08-25 02:00:02 +00001402 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001403 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001404 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001405
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001406 case ConditionalOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001407 // The condition must be evaluated, but if either the LHS or RHS is a
1408 // warning, warn about them.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001409 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001410 if (Exp->getLHS() &&
Mike Stump53f9ded2009-11-03 23:25:48 +00001411 Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
Chris Lattner237f2752009-02-14 07:37:35 +00001412 return true;
Mike Stump53f9ded2009-11-03 23:25:48 +00001413 return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001414 }
1415
Chris Lattnera44d1162007-06-27 05:58:59 +00001416 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001417 // If the base pointer or element is to a volatile pointer/field, accessing
1418 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001419 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001420 return false;
1421 Loc = cast<MemberExpr>(this)->getMemberLoc();
1422 R1 = SourceRange(Loc, Loc);
1423 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1424 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001425
Chris Lattner1ec5f562007-06-27 05:38:08 +00001426 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001427 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001428 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001429 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001430 return false;
1431 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1432 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1433 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1434 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001435
Chris Lattner1ec5f562007-06-27 05:38:08 +00001436 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001437 case CXXOperatorCallExprClass:
1438 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001439 // If this is a direct call, get the callee.
1440 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001441 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001442 // If the callee has attribute pure, const, or warn_unused_result, warn
1443 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001444 //
1445 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1446 // updated to match for QoI.
1447 if (FD->getAttr<WarnUnusedResultAttr>() ||
1448 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1449 Loc = CE->getCallee()->getLocStart();
1450 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001451
Chris Lattner1a6babf2009-10-13 04:53:48 +00001452 if (unsigned NumArgs = CE->getNumArgs())
1453 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1454 CE->getArg(NumArgs-1)->getLocEnd());
1455 return true;
1456 }
Chris Lattner237f2752009-02-14 07:37:35 +00001457 }
1458 return false;
1459 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001460
1461 case CXXTemporaryObjectExprClass:
1462 case CXXConstructExprClass:
1463 return false;
1464
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001465 case ObjCMessageExprClass: {
1466 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
1467 const ObjCMethodDecl *MD = ME->getMethodDecl();
1468 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1469 Loc = getExprLoc();
1470 return true;
1471 }
Chris Lattner237f2752009-02-14 07:37:35 +00001472 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001473 }
Mike Stump11289f42009-09-09 15:08:12 +00001474
John McCallb7bd14f2010-12-02 01:19:52 +00001475 case ObjCPropertyRefExprClass:
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001476 Loc = getExprLoc();
1477 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001478 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00001479
Chris Lattner944d3062008-07-26 19:51:01 +00001480 case StmtExprClass: {
1481 // Statement exprs don't logically have side effects themselves, but are
1482 // sometimes used in macros in ways that give them a type that is unused.
1483 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1484 // however, if the result of the stmt expr is dead, we don't want to emit a
1485 // warning.
1486 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001487 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00001488 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001489 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001490 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1491 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1492 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1493 }
Mike Stump11289f42009-09-09 15:08:12 +00001494
John McCallc493a732010-03-12 07:11:26 +00001495 if (getType()->isVoidType())
1496 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001497 Loc = cast<StmtExpr>(this)->getLParenLoc();
1498 R1 = getSourceRange();
1499 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001500 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001501 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001502 // If this is an explicit cast to void, allow it. People do this when they
1503 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001504 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001505 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001506 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1507 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1508 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001509 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001510 if (getType()->isVoidType())
1511 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001512 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001513
Anders Carlsson6aa50392009-11-17 17:11:23 +00001514 // If this is a cast to void or a constructor conversion, check the operand.
1515 // Otherwise, the result of the cast is unused.
John McCalle3027922010-08-25 11:45:40 +00001516 if (CE->getCastKind() == CK_ToVoid ||
1517 CE->getCastKind() == CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001518 return (cast<CastExpr>(this)->getSubExpr()
1519 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001520 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1521 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1522 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001523 }
Mike Stump11289f42009-09-09 15:08:12 +00001524
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001525 case ImplicitCastExprClass:
1526 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001527 return (cast<ImplicitCastExpr>(this)
1528 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001529
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001530 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001531 return (cast<CXXDefaultArgExpr>(this)
1532 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001533
1534 case CXXNewExprClass:
1535 // FIXME: In theory, there might be new expressions that don't have side
1536 // effects (e.g. a placement new with an uninitialized POD).
1537 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001538 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001539 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001540 return (cast<CXXBindTemporaryExpr>(this)
1541 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00001542 case ExprWithCleanupsClass:
1543 return (cast<ExprWithCleanups>(this)
Mike Stump53f9ded2009-11-03 23:25:48 +00001544 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001545 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001546}
1547
Fariborz Jahanian07735332009-02-22 18:40:18 +00001548/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001549/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001550bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001551 switch (getStmtClass()) {
1552 default:
1553 return false;
1554 case ObjCIvarRefExprClass:
1555 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001556 case Expr::UnaryOperatorClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001557 return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001558 case ParenExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001559 return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001560 case ImplicitCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001561 return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001562 case CStyleCastExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001563 return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001564 case DeclRefExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001565 const Decl *D = cast<DeclRefExpr>(this)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001566 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1567 if (VD->hasGlobalStorage())
1568 return true;
1569 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001570 // dereferencing to a pointer is always a gc'able candidate,
1571 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001572 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001573 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001574 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001575 return false;
1576 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001577 case MemberExprClass: {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001578 const MemberExpr *M = cast<MemberExpr>(this);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001579 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001580 }
1581 case ArraySubscriptExprClass:
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001582 return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001583 }
1584}
Sebastian Redlce354af2010-09-10 20:55:33 +00001585
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001586bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1587 if (isTypeDependent())
1588 return false;
John McCall086a4642010-11-24 05:12:34 +00001589 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001590}
1591
Sebastian Redlce354af2010-09-10 20:55:33 +00001592static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1593 Expr::CanThrowResult CT2) {
1594 // CanThrowResult constants are ordered so that the maximum is the correct
1595 // merge result.
1596 return CT1 > CT2 ? CT1 : CT2;
1597}
1598
1599static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1600 Expr *E = const_cast<Expr*>(CE);
1601 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall8322c3a2011-02-13 04:07:26 +00001602 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redlce354af2010-09-10 20:55:33 +00001603 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1604 }
1605 return R;
1606}
1607
1608static Expr::CanThrowResult CanCalleeThrow(const Decl *D,
1609 bool NullThrows = true) {
1610 if (!D)
1611 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1612
1613 // See if we can get a function type from the decl somehow.
1614 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1615 if (!VD) // If we have no clue what we're calling, assume the worst.
1616 return Expr::CT_Can;
1617
Sebastian Redlb8a76c42010-09-10 22:34:40 +00001618 // As an extension, we assume that __attribute__((nothrow)) functions don't
1619 // throw.
1620 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1621 return Expr::CT_Cannot;
1622
Sebastian Redlce354af2010-09-10 20:55:33 +00001623 QualType T = VD->getType();
1624 const FunctionProtoType *FT;
1625 if ((FT = T->getAs<FunctionProtoType>())) {
1626 } else if (const PointerType *PT = T->getAs<PointerType>())
1627 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1628 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1629 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1630 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1631 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1632 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1633 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1634
1635 if (!FT)
1636 return Expr::CT_Can;
1637
1638 return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can;
1639}
1640
1641static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1642 if (DC->isTypeDependent())
1643 return Expr::CT_Dependent;
1644
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001645 if (!DC->getTypeAsWritten()->isReferenceType())
1646 return Expr::CT_Cannot;
1647
Sebastian Redlce354af2010-09-10 20:55:33 +00001648 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1649}
1650
1651static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1652 const CXXTypeidExpr *DC) {
1653 if (DC->isTypeOperand())
1654 return Expr::CT_Cannot;
1655
1656 Expr *Op = DC->getExprOperand();
1657 if (Op->isTypeDependent())
1658 return Expr::CT_Dependent;
1659
1660 const RecordType *RT = Op->getType()->getAs<RecordType>();
1661 if (!RT)
1662 return Expr::CT_Cannot;
1663
1664 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1665 return Expr::CT_Cannot;
1666
1667 if (Op->Classify(C).isPRValue())
1668 return Expr::CT_Cannot;
1669
1670 return Expr::CT_Can;
1671}
1672
1673Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1674 // C++ [expr.unary.noexcept]p3:
1675 // [Can throw] if in a potentially-evaluated context the expression would
1676 // contain:
1677 switch (getStmtClass()) {
1678 case CXXThrowExprClass:
1679 // - a potentially evaluated throw-expression
1680 return CT_Can;
1681
1682 case CXXDynamicCastExprClass: {
1683 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1684 // where T is a reference type, that requires a run-time check
1685 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1686 if (CT == CT_Can)
1687 return CT;
1688 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1689 }
1690
1691 case CXXTypeidExprClass:
1692 // - a potentially evaluated typeid expression applied to a glvalue
1693 // expression whose type is a polymorphic class type
1694 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1695
1696 // - a potentially evaluated call to a function, member function, function
1697 // pointer, or member function pointer that does not have a non-throwing
1698 // exception-specification
1699 case CallExprClass:
1700 case CXXOperatorCallExprClass:
1701 case CXXMemberCallExprClass: {
1702 CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl());
1703 if (CT == CT_Can)
1704 return CT;
1705 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1706 }
1707
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001708 case CXXConstructExprClass:
1709 case CXXTemporaryObjectExprClass: {
Sebastian Redlce354af2010-09-10 20:55:33 +00001710 CanThrowResult CT = CanCalleeThrow(
1711 cast<CXXConstructExpr>(this)->getConstructor());
1712 if (CT == CT_Can)
1713 return CT;
1714 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1715 }
1716
1717 case CXXNewExprClass: {
1718 CanThrowResult CT = MergeCanThrow(
1719 CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()),
1720 CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(),
1721 /*NullThrows*/false));
1722 if (CT == CT_Can)
1723 return CT;
1724 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1725 }
1726
1727 case CXXDeleteExprClass: {
Sebastian Redlce354af2010-09-10 20:55:33 +00001728 CanThrowResult CT = CanCalleeThrow(
1729 cast<CXXDeleteExpr>(this)->getOperatorDelete());
1730 if (CT == CT_Can)
1731 return CT;
Sebastian Redla8bac372010-09-10 23:27:10 +00001732 const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument();
1733 // Unwrap exactly one implicit cast, which converts all pointers to void*.
1734 if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg))
1735 Arg = Cast->getSubExpr();
1736 if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) {
1737 if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) {
1738 CanThrowResult CT2 = CanCalleeThrow(
1739 cast<CXXRecordDecl>(RT->getDecl())->getDestructor());
1740 if (CT2 == CT_Can)
1741 return CT2;
1742 CT = MergeCanThrow(CT, CT2);
1743 }
1744 }
1745 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1746 }
1747
1748 case CXXBindTemporaryExprClass: {
1749 // The bound temporary has to be destroyed again, which might throw.
1750 CanThrowResult CT = CanCalleeThrow(
1751 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1752 if (CT == CT_Can)
1753 return CT;
Sebastian Redlce354af2010-09-10 20:55:33 +00001754 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1755 }
1756
1757 // ObjC message sends are like function calls, but never have exception
1758 // specs.
1759 case ObjCMessageExprClass:
1760 case ObjCPropertyRefExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001761 return CT_Can;
1762
1763 // Many other things have subexpressions, so we have to test those.
1764 // Some are simple:
1765 case ParenExprClass:
1766 case MemberExprClass:
1767 case CXXReinterpretCastExprClass:
1768 case CXXConstCastExprClass:
1769 case ConditionalOperatorClass:
1770 case CompoundLiteralExprClass:
1771 case ExtVectorElementExprClass:
1772 case InitListExprClass:
1773 case DesignatedInitExprClass:
1774 case ParenListExprClass:
1775 case VAArgExprClass:
1776 case CXXDefaultArgExprClass:
John McCall5d413782010-12-06 08:20:24 +00001777 case ExprWithCleanupsClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001778 case ObjCIvarRefExprClass:
1779 case ObjCIsaExprClass:
1780 case ShuffleVectorExprClass:
1781 return CanSubExprsThrow(C, this);
1782
1783 // Some might be dependent for other reasons.
1784 case UnaryOperatorClass:
1785 case ArraySubscriptExprClass:
1786 case ImplicitCastExprClass:
1787 case CStyleCastExprClass:
1788 case CXXStaticCastExprClass:
1789 case CXXFunctionalCastExprClass:
1790 case BinaryOperatorClass:
1791 case CompoundAssignOperatorClass: {
1792 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1793 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1794 }
1795
1796 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1797 case StmtExprClass:
1798 return CT_Can;
1799
1800 case ChooseExprClass:
1801 if (isTypeDependent() || isValueDependent())
1802 return CT_Dependent;
1803 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1804
1805 // Some expressions are always dependent.
1806 case DependentScopeDeclRefExprClass:
1807 case CXXUnresolvedConstructExprClass:
1808 case CXXDependentScopeMemberExprClass:
1809 return CT_Dependent;
1810
1811 default:
1812 // All other expressions don't have subexpressions, or else they are
1813 // unevaluated.
1814 return CT_Cannot;
1815 }
1816}
1817
Ted Kremenekfff70962008-01-17 16:57:34 +00001818Expr* Expr::IgnoreParens() {
1819 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00001820 while (true) {
1821 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1822 E = P->getSubExpr();
1823 continue;
1824 }
1825 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1826 if (P->getOpcode() == UO_Extension) {
1827 E = P->getSubExpr();
1828 continue;
1829 }
1830 }
1831 return E;
1832 }
Ted Kremenekfff70962008-01-17 16:57:34 +00001833}
1834
Chris Lattnerf2660962008-02-13 01:02:39 +00001835/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1836/// or CastExprs or ImplicitCastExprs, returning their operand.
1837Expr *Expr::IgnoreParenCasts() {
1838 Expr *E = this;
1839 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001840 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001841 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001842 continue;
1843 }
1844 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001845 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001846 continue;
1847 }
1848 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1849 if (P->getOpcode() == UO_Extension) {
1850 E = P->getSubExpr();
1851 continue;
1852 }
1853 }
1854 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00001855 }
1856}
1857
John McCall5a4ce8b2010-12-04 08:24:19 +00001858/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1859/// casts. This is intended purely as a temporary workaround for code
1860/// that hasn't yet been rewritten to do the right thing about those
1861/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00001862Expr *Expr::IgnoreParenLValueCasts() {
1863 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00001864 while (true) {
John McCall34376a62010-12-04 03:47:34 +00001865 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1866 E = P->getSubExpr();
1867 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00001868 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00001869 if (P->getCastKind() == CK_LValueToRValue) {
1870 E = P->getSubExpr();
1871 continue;
1872 }
John McCall5a4ce8b2010-12-04 08:24:19 +00001873 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1874 if (P->getOpcode() == UO_Extension) {
1875 E = P->getSubExpr();
1876 continue;
1877 }
John McCall34376a62010-12-04 03:47:34 +00001878 }
1879 break;
1880 }
1881 return E;
1882}
1883
John McCalleebc8322010-05-05 22:59:52 +00001884Expr *Expr::IgnoreParenImpCasts() {
1885 Expr *E = this;
1886 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001887 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001888 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001889 continue;
1890 }
1891 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001892 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001893 continue;
1894 }
1895 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1896 if (P->getOpcode() == UO_Extension) {
1897 E = P->getSubExpr();
1898 continue;
1899 }
1900 }
1901 return E;
John McCalleebc8322010-05-05 22:59:52 +00001902 }
1903}
1904
Chris Lattneref26c772009-03-13 17:28:01 +00001905/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
1906/// value (including ptr->int casts of the same size). Strip off any
1907/// ParenExpr or CastExprs, returning their operand.
1908Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
1909 Expr *E = this;
1910 while (true) {
1911 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1912 E = P->getSubExpr();
1913 continue;
1914 }
Mike Stump11289f42009-09-09 15:08:12 +00001915
Chris Lattneref26c772009-03-13 17:28:01 +00001916 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
1917 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00001918 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00001919 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00001920
Chris Lattneref26c772009-03-13 17:28:01 +00001921 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
1922 E = SE;
1923 continue;
1924 }
Mike Stump11289f42009-09-09 15:08:12 +00001925
Abramo Bagnara932e3932010-10-15 07:51:18 +00001926 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00001927 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00001928 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00001929 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00001930 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
1931 E = SE;
1932 continue;
1933 }
1934 }
Mike Stump11289f42009-09-09 15:08:12 +00001935
Abramo Bagnara932e3932010-10-15 07:51:18 +00001936 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1937 if (P->getOpcode() == UO_Extension) {
1938 E = P->getSubExpr();
1939 continue;
1940 }
1941 }
1942
Chris Lattneref26c772009-03-13 17:28:01 +00001943 return E;
1944 }
1945}
1946
Douglas Gregord196a582009-12-14 19:27:10 +00001947bool Expr::isDefaultArgument() const {
1948 const Expr *E = this;
1949 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
1950 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001951
Douglas Gregord196a582009-12-14 19:27:10 +00001952 return isa<CXXDefaultArgExpr>(E);
1953}
Chris Lattneref26c772009-03-13 17:28:01 +00001954
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001955/// \brief Skip over any no-op casts and any temporary-binding
1956/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00001957static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001958 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00001959 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001960 E = ICE->getSubExpr();
1961 else
1962 break;
1963 }
1964
1965 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
1966 E = BE->getSubExpr();
1967
1968 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00001969 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001970 E = ICE->getSubExpr();
1971 else
1972 break;
1973 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00001974
1975 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001976}
1977
John McCall7a626f62010-09-15 10:14:12 +00001978/// isTemporaryObject - Determines if this expression produces a
1979/// temporary of the given class type.
1980bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
1981 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
1982 return false;
1983
Anders Carlsson66bbf502010-11-28 16:40:49 +00001984 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001985
John McCall02dc8c72010-09-15 20:59:13 +00001986 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00001987 if (!E->Classify(C).isPRValue()) {
1988 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00001989 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00001990 return false;
1991 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00001992
John McCallf4ee1dd2010-09-16 06:57:56 +00001993 // Black-list a few cases which yield pr-values of class type that don't
1994 // refer to temporaries of that type:
1995
1996 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00001997 if (isa<ImplicitCastExpr>(E)) {
1998 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
1999 case CK_DerivedToBase:
2000 case CK_UncheckedDerivedToBase:
2001 return false;
2002 default:
2003 break;
2004 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002005 }
2006
John McCallf4ee1dd2010-09-16 06:57:56 +00002007 // - member expressions (all)
2008 if (isa<MemberExpr>(E))
2009 return false;
2010
John McCallc07a0c72011-02-17 10:25:35 +00002011 // - opaque values (all)
2012 if (isa<OpaqueValueExpr>(E))
2013 return false;
2014
John McCall7a626f62010-09-15 10:14:12 +00002015 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002016}
2017
Douglas Gregor4619e432008-12-05 23:32:09 +00002018/// hasAnyTypeDependentArguments - Determines if any of the expressions
2019/// in Exprs is type-dependent.
2020bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2021 for (unsigned I = 0; I < NumExprs; ++I)
2022 if (Exprs[I]->isTypeDependent())
2023 return true;
2024
2025 return false;
2026}
2027
2028/// hasAnyValueDependentArguments - Determines if any of the expressions
2029/// in Exprs is value-dependent.
2030bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2031 for (unsigned I = 0; I < NumExprs; ++I)
2032 if (Exprs[I]->isValueDependent())
2033 return true;
2034
2035 return false;
2036}
2037
John McCall8b0f4ff2010-08-02 21:13:48 +00002038bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002039 // This function is attempting whether an expression is an initializer
2040 // which can be evaluated at compile-time. isEvaluatable handles most
2041 // of the cases, but it can't deal with some initializer-specific
2042 // expressions, and it can't deal with aggregates; we deal with those here,
2043 // and fall back to isEvaluatable for the other cases.
2044
John McCall8b0f4ff2010-08-02 21:13:48 +00002045 // If we ever capture reference-binding directly in the AST, we can
2046 // kill the second parameter.
2047
2048 if (IsForRef) {
2049 EvalResult Result;
2050 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2051 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002052
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002053 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002054 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002055 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002056 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002057 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002058 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002059 case CXXTemporaryObjectExprClass:
2060 case CXXConstructExprClass: {
2061 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002062
2063 // Only if it's
2064 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00002065 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002066 if (!CE->getNumArgs()) return true;
2067
2068 // 2) an elidable trivial copy construction of an operand which is
2069 // itself a constant initializer. Note that we consider the
2070 // operand on its own, *not* as a reference binding.
2071 return CE->isElidable() &&
2072 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00002073 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002074 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002075 // This handles gcc's extension that allows global initializers like
2076 // "struct x {int x;} x = (struct x) {};".
2077 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002078 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002079 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002080 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002081 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002082 // FIXME: This doesn't deal with fields with reference types correctly.
2083 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2084 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002085 const InitListExpr *Exp = cast<InitListExpr>(this);
2086 unsigned numInits = Exp->getNumInits();
2087 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002088 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002089 return false;
2090 }
Eli Friedman384da272009-01-25 03:12:18 +00002091 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002092 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002093 case ImplicitValueInitExprClass:
2094 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002095 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002096 return cast<ParenExpr>(this)->getSubExpr()
2097 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002098 case ChooseExprClass:
2099 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2100 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002101 case UnaryOperatorClass: {
2102 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002103 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002104 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002105 break;
2106 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00002107 case BinaryOperatorClass: {
2108 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2109 // but this handles the common case.
2110 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002111 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3eb172a2009-10-13 07:14:16 +00002112 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2113 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2114 return true;
2115 break;
2116 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002117 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002118 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002119 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00002120 case CStyleCastExprClass:
2121 // Handle casts with a destination that's a struct or union; this
2122 // deals with both the gcc no-op struct cast extension and the
2123 // cast-to-union extension.
2124 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002125 return cast<CastExpr>(this)->getSubExpr()
2126 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002127
Chris Lattnera2f9bd52009-10-13 22:12:09 +00002128 // Integer->integer casts can be handled here, which is important for
2129 // things like (int)(&&x-&&y). Scary but true.
2130 if (getType()->isIntegerType() &&
2131 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002132 return cast<CastExpr>(this)->getSubExpr()
2133 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002134
Eli Friedman384da272009-01-25 03:12:18 +00002135 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002136 }
Eli Friedman384da272009-01-25 03:12:18 +00002137 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002138}
2139
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002140/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2141/// pointer constant or not, as well as the specific kind of constant detected.
2142/// Null pointer constants can be integer constant expressions with the
2143/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2144/// (a GNU extension).
2145Expr::NullPointerConstantKind
2146Expr::isNullPointerConstant(ASTContext &Ctx,
2147 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00002148 if (isValueDependent()) {
2149 switch (NPC) {
2150 case NPC_NeverValueDependent:
2151 assert(false && "Unexpected value dependent expression!");
2152 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002153
Douglas Gregor56751b52009-09-25 04:25:58 +00002154 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002155 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2156 return NPCK_ZeroInteger;
2157 else
2158 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00002159
Douglas Gregor56751b52009-09-25 04:25:58 +00002160 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002161 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00002162 }
2163 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002164
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002165 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002166 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00002167 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002168 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002169 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002170 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002171 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002172 Pointee->isVoidType() && // to void*
2173 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002174 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002175 }
Steve Naroffada7d422007-05-20 17:54:12 +00002176 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002177 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2178 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002179 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002180 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2181 // Accept ((void*)0) as a null pointer constant, as many other
2182 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002183 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002184 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002185 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002186 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002187 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002188 } else if (isa<GNUNullExpr>(this)) {
2189 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002190 return NPCK_GNUNull;
Steve Naroff09035312008-01-14 02:53:34 +00002191 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002192
Sebastian Redl576fd422009-05-10 18:38:11 +00002193 // C++0x nullptr_t is always a null pointer constant.
2194 if (getType()->isNullPtrType())
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002195 return NPCK_CXX0X_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00002196
Fariborz Jahanian3567c422010-09-27 22:42:37 +00002197 if (const RecordType *UT = getType()->getAsUnionType())
2198 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2199 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2200 const Expr *InitExpr = CLE->getInitializer();
2201 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2202 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2203 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002204 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002205 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002206 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002207 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00002208
Chris Lattner1abbd412007-06-08 17:58:43 +00002209 // If we have an integer constant expression, we need to *evaluate* it and
2210 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002211 llvm::APSInt Result;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002212 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2213
2214 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002215}
Steve Narofff7a5da12007-07-28 23:10:27 +00002216
John McCall34376a62010-12-04 03:47:34 +00002217/// \brief If this expression is an l-value for an Objective C
2218/// property, find the underlying property reference expression.
2219const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2220 const Expr *E = this;
2221 while (true) {
2222 assert((E->getValueKind() == VK_LValue &&
2223 E->getObjectKind() == OK_ObjCProperty) &&
2224 "expression is not a property reference");
2225 E = E->IgnoreParenCasts();
2226 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2227 if (BO->getOpcode() == BO_Comma) {
2228 E = BO->getRHS();
2229 continue;
2230 }
2231 }
2232
2233 break;
2234 }
2235
2236 return cast<ObjCPropertyRefExpr>(E);
2237}
2238
Douglas Gregor71235ec2009-05-02 02:18:30 +00002239FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002240 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002241
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002242 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002243 if (ICE->getCastKind() == CK_LValueToRValue ||
2244 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002245 E = ICE->getSubExpr()->IgnoreParens();
2246 else
2247 break;
2248 }
2249
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002250 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002251 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002252 if (Field->isBitField())
2253 return Field;
2254
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00002255 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2256 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2257 if (Field->isBitField())
2258 return Field;
2259
Douglas Gregor71235ec2009-05-02 02:18:30 +00002260 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2261 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2262 return BinOp->getLHS()->getBitField();
2263
2264 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002265}
2266
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002267bool Expr::refersToVectorElement() const {
2268 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002269
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002270 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002271 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002272 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002273 E = ICE->getSubExpr()->IgnoreParens();
2274 else
2275 break;
2276 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002277
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002278 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2279 return ASE->getBase()->getType()->isVectorType();
2280
2281 if (isa<ExtVectorElementExpr>(E))
2282 return true;
2283
2284 return false;
2285}
2286
Chris Lattnerb8211f62009-02-16 22:14:05 +00002287/// isArrow - Return true if the base expression is a pointer to vector,
2288/// return false if the base expression is a vector.
2289bool ExtVectorElementExpr::isArrow() const {
2290 return getBase()->getType()->isPointerType();
2291}
2292
Nate Begemance4d7fc2008-04-18 23:10:10 +00002293unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002294 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002295 return VT->getNumElements();
2296 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002297}
2298
Nate Begemanf322eab2008-05-09 06:41:27 +00002299/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002300bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002301 // FIXME: Refactor this code to an accessor on the AST node which returns the
2302 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002303 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002304
2305 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002306 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002307 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002308
Nate Begeman7e5185b2009-01-18 02:01:21 +00002309 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002310 if (Comp[0] == 's' || Comp[0] == 'S')
2311 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002312
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002313 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2314 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002315 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002316
Steve Naroff0d595ca2007-07-30 03:29:09 +00002317 return false;
2318}
Chris Lattner885b4952007-08-02 23:36:59 +00002319
Nate Begemanf322eab2008-05-09 06:41:27 +00002320/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002321void ExtVectorElementExpr::getEncodedElementAccess(
2322 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002323 llvm::StringRef Comp = Accessor->getName();
2324 if (Comp[0] == 's' || Comp[0] == 'S')
2325 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002326
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002327 bool isHi = Comp == "hi";
2328 bool isLo = Comp == "lo";
2329 bool isEven = Comp == "even";
2330 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002331
Nate Begemanf322eab2008-05-09 06:41:27 +00002332 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2333 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002334
Nate Begemanf322eab2008-05-09 06:41:27 +00002335 if (isHi)
2336 Index = e + i;
2337 else if (isLo)
2338 Index = i;
2339 else if (isEven)
2340 Index = 2 * i;
2341 else if (isOdd)
2342 Index = 2 * i + 1;
2343 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002344 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002345
Nate Begemand3862152008-05-13 21:03:02 +00002346 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002347 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002348}
2349
Douglas Gregor9a129192010-04-21 00:45:42 +00002350ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002351 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002352 SourceLocation LBracLoc,
2353 SourceLocation SuperLoc,
2354 bool IsInstanceSuper,
2355 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002356 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002357 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002358 ObjCMethodDecl *Method,
2359 Expr **Args, unsigned NumArgs,
2360 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002361 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002362 /*TypeDependent=*/false, /*ValueDependent=*/false,
2363 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002364 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
2365 HasMethod(Method != 0), SuperLoc(SuperLoc),
2366 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2367 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002368 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002369{
Douglas Gregor9a129192010-04-21 00:45:42 +00002370 setReceiverPointer(SuperType.getAsOpaquePtr());
2371 if (NumArgs)
2372 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002373}
2374
Douglas Gregor9a129192010-04-21 00:45:42 +00002375ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002376 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002377 SourceLocation LBracLoc,
2378 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002379 Selector Sel,
2380 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002381 ObjCMethodDecl *Method,
2382 Expr **Args, unsigned NumArgs,
2383 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002384 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002385 T->isDependentType(), T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002386 NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0),
2387 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2388 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002389 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002390{
2391 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002392 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002393 for (unsigned I = 0; I != NumArgs; ++I) {
2394 if (Args[I]->isTypeDependent())
2395 ExprBits.TypeDependent = true;
2396 if (Args[I]->isValueDependent())
2397 ExprBits.ValueDependent = true;
2398 if (Args[I]->containsUnexpandedParameterPack())
2399 ExprBits.ContainsUnexpandedParameterPack = true;
2400
2401 MyArgs[I] = Args[I];
2402 }
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002403}
2404
Douglas Gregor9a129192010-04-21 00:45:42 +00002405ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002406 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002407 SourceLocation LBracLoc,
2408 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002409 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002410 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002411 ObjCMethodDecl *Method,
2412 Expr **Args, unsigned NumArgs,
2413 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002414 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002415 Receiver->isTypeDependent(),
2416 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002417 NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0),
2418 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2419 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002420 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002421{
2422 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002423 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002424 for (unsigned I = 0; I != NumArgs; ++I) {
2425 if (Args[I]->isTypeDependent())
2426 ExprBits.TypeDependent = true;
2427 if (Args[I]->isValueDependent())
2428 ExprBits.ValueDependent = true;
2429 if (Args[I]->containsUnexpandedParameterPack())
2430 ExprBits.ContainsUnexpandedParameterPack = true;
2431
2432 MyArgs[I] = Args[I];
2433 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00002434}
2435
Douglas Gregor9a129192010-04-21 00:45:42 +00002436ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002437 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002438 SourceLocation LBracLoc,
2439 SourceLocation SuperLoc,
2440 bool IsInstanceSuper,
2441 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002442 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002443 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002444 ObjCMethodDecl *Method,
2445 Expr **Args, unsigned NumArgs,
2446 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002447 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002448 NumArgs * sizeof(Expr *);
2449 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCall7decc9e2010-11-18 06:31:45 +00002450 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002451 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002452 RBracLoc);
2453}
2454
2455ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002456 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002457 SourceLocation LBracLoc,
2458 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002459 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002460 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002461 ObjCMethodDecl *Method,
2462 Expr **Args, unsigned NumArgs,
2463 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002464 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002465 NumArgs * sizeof(Expr *);
2466 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002467 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2468 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002469}
2470
2471ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002472 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002473 SourceLocation LBracLoc,
2474 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002475 Selector Sel,
2476 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002477 ObjCMethodDecl *Method,
2478 Expr **Args, unsigned NumArgs,
2479 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002480 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002481 NumArgs * sizeof(Expr *);
2482 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002483 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2484 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002485}
2486
Alexis Hunta8136cc2010-05-05 15:23:54 +00002487ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002488 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002489 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002490 NumArgs * sizeof(Expr *);
2491 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2492 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2493}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00002494
2495SourceRange ObjCMessageExpr::getReceiverRange() const {
2496 switch (getReceiverKind()) {
2497 case Instance:
2498 return getInstanceReceiver()->getSourceRange();
2499
2500 case Class:
2501 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2502
2503 case SuperInstance:
2504 case SuperClass:
2505 return getSuperLoc();
2506 }
2507
2508 return SourceLocation();
2509}
2510
Douglas Gregor9a129192010-04-21 00:45:42 +00002511Selector ObjCMessageExpr::getSelector() const {
2512 if (HasMethod)
2513 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2514 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002515 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002516}
2517
2518ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2519 switch (getReceiverKind()) {
2520 case Instance:
2521 if (const ObjCObjectPointerType *Ptr
2522 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2523 return Ptr->getInterfaceDecl();
2524 break;
2525
2526 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00002527 if (const ObjCObjectType *Ty
2528 = getClassReceiver()->getAs<ObjCObjectType>())
2529 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002530 break;
2531
2532 case SuperInstance:
2533 if (const ObjCObjectPointerType *Ptr
2534 = getSuperType()->getAs<ObjCObjectPointerType>())
2535 return Ptr->getInterfaceDecl();
2536 break;
2537
2538 case SuperClass:
Argyrios Kyrtzidis1b9747f2011-01-25 00:03:48 +00002539 if (const ObjCObjectType *Iface
2540 = getSuperType()->getAs<ObjCObjectType>())
2541 return Iface->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002542 break;
2543 }
2544
2545 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002546}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002547
Jay Foad39c79802011-01-12 09:06:06 +00002548bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002549 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002550}
2551
Douglas Gregora6e053e2010-12-15 01:34:56 +00002552ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2553 QualType Type, SourceLocation BLoc,
2554 SourceLocation RP)
2555 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2556 Type->isDependentType(), Type->isDependentType(),
2557 Type->containsUnexpandedParameterPack()),
2558 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2559{
2560 SubExprs = new (C) Stmt*[nexpr];
2561 for (unsigned i = 0; i < nexpr; i++) {
2562 if (args[i]->isTypeDependent())
2563 ExprBits.TypeDependent = true;
2564 if (args[i]->isValueDependent())
2565 ExprBits.ValueDependent = true;
2566 if (args[i]->containsUnexpandedParameterPack())
2567 ExprBits.ContainsUnexpandedParameterPack = true;
2568
2569 SubExprs[i] = args[i];
2570 }
2571}
2572
Nate Begeman48745922009-08-12 02:28:50 +00002573void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2574 unsigned NumExprs) {
2575 if (SubExprs) C.Deallocate(SubExprs);
2576
2577 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002578 this->NumExprs = NumExprs;
2579 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002580}
Nate Begeman48745922009-08-12 02:28:50 +00002581
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002582//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002583// DesignatedInitExpr
2584//===----------------------------------------------------------------------===//
2585
2586IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() {
2587 assert(Kind == FieldDesignator && "Only valid on a field designator");
2588 if (Field.NameOrField & 0x01)
2589 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2590 else
2591 return getField()->getIdentifier();
2592}
2593
Alexis Hunta8136cc2010-05-05 15:23:54 +00002594DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002595 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002596 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002597 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002598 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002599 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002600 unsigned NumIndexExprs,
2601 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002602 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00002603 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002604 Init->isTypeDependent(), Init->isValueDependent(),
2605 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00002606 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2607 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002608 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002609
2610 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00002611 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002612 *Child++ = Init;
2613
2614 // Copy the designators and their subexpressions, computing
2615 // value-dependence along the way.
2616 unsigned IndexIdx = 0;
2617 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002618 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002619
2620 if (this->Designators[I].isArrayDesignator()) {
2621 // Compute type- and value-dependence.
2622 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002623 if (Index->isTypeDependent() || Index->isValueDependent())
2624 ExprBits.ValueDependent = true;
2625
2626 // Propagate unexpanded parameter packs.
2627 if (Index->containsUnexpandedParameterPack())
2628 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002629
2630 // Copy the index expressions into permanent storage.
2631 *Child++ = IndexExprs[IndexIdx++];
2632 } else if (this->Designators[I].isArrayRangeDesignator()) {
2633 // Compute type- and value-dependence.
2634 Expr *Start = IndexExprs[IndexIdx];
2635 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002636 if (Start->isTypeDependent() || Start->isValueDependent() ||
2637 End->isTypeDependent() || End->isValueDependent())
2638 ExprBits.ValueDependent = true;
2639
2640 // Propagate unexpanded parameter packs.
2641 if (Start->containsUnexpandedParameterPack() ||
2642 End->containsUnexpandedParameterPack())
2643 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002644
2645 // Copy the start/end expressions into permanent storage.
2646 *Child++ = IndexExprs[IndexIdx++];
2647 *Child++ = IndexExprs[IndexIdx++];
2648 }
2649 }
2650
2651 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002652}
2653
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002654DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002655DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002656 unsigned NumDesignators,
2657 Expr **IndexExprs, unsigned NumIndexExprs,
2658 SourceLocation ColonOrEqualLoc,
2659 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002660 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002661 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002662 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002663 ColonOrEqualLoc, UsesColonSyntax,
2664 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002665}
2666
Mike Stump11289f42009-09-09 15:08:12 +00002667DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002668 unsigned NumIndexExprs) {
2669 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2670 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2671 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2672}
2673
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002674void DesignatedInitExpr::setDesignators(ASTContext &C,
2675 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002676 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002677 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002678 NumDesignators = NumDesigs;
2679 for (unsigned I = 0; I != NumDesigs; ++I)
2680 Designators[I] = Desigs[I];
2681}
2682
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002683SourceRange DesignatedInitExpr::getSourceRange() const {
2684 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002685 Designator &First =
2686 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002687 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002688 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002689 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2690 else
2691 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2692 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002693 StartLoc =
2694 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002695 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2696}
2697
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002698Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2699 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2700 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2701 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002702 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2703 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2704}
2705
2706Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002707 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002708 "Requires array range designator");
2709 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2710 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002711 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2712 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2713}
2714
2715Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002716 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002717 "Requires array range designator");
2718 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2719 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002720 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2721 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2722}
2723
Douglas Gregord5846a12009-04-15 06:41:24 +00002724/// \brief Replaces the designator at index @p Idx with the series
2725/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002726void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002727 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002728 const Designator *Last) {
2729 unsigned NumNewDesignators = Last - First;
2730 if (NumNewDesignators == 0) {
2731 std::copy_backward(Designators + Idx + 1,
2732 Designators + NumDesignators,
2733 Designators + Idx);
2734 --NumNewDesignators;
2735 return;
2736 } else if (NumNewDesignators == 1) {
2737 Designators[Idx] = *First;
2738 return;
2739 }
2740
Mike Stump11289f42009-09-09 15:08:12 +00002741 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002742 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002743 std::copy(Designators, Designators + Idx, NewDesignators);
2744 std::copy(First, Last, NewDesignators + Idx);
2745 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2746 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00002747 Designators = NewDesignators;
2748 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2749}
2750
Mike Stump11289f42009-09-09 15:08:12 +00002751ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002752 Expr **exprs, unsigned nexprs,
2753 SourceLocation rparenloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00002754 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2755 false, false, false),
2756 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002757
Nate Begeman5ec4b312009-08-10 23:49:36 +00002758 Exprs = new (C) Stmt*[nexprs];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002759 for (unsigned i = 0; i != nexprs; ++i) {
2760 if (exprs[i]->isTypeDependent())
2761 ExprBits.TypeDependent = true;
2762 if (exprs[i]->isValueDependent())
2763 ExprBits.ValueDependent = true;
2764 if (exprs[i]->containsUnexpandedParameterPack())
2765 ExprBits.ContainsUnexpandedParameterPack = true;
2766
Nate Begeman5ec4b312009-08-10 23:49:36 +00002767 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002768 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00002769}
2770
John McCall1bf58462011-02-16 08:02:54 +00002771const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
2772 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
2773 e = ewc->getSubExpr();
2774 e = cast<CXXConstructExpr>(e)->getArg(0);
2775 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
2776 e = ice->getSubExpr();
2777 return cast<OpaqueValueExpr>(e);
2778}
2779
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002780//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00002781// ExprIterator.
2782//===----------------------------------------------------------------------===//
2783
2784Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
2785Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
2786Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
2787const Expr* ConstExprIterator::operator[](size_t idx) const {
2788 return cast<Expr>(I[idx]);
2789}
2790const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
2791const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
2792
2793//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002794// Child Iterators for iterating over subexpressions/substatements
2795//===----------------------------------------------------------------------===//
2796
Sebastian Redl6f282892008-11-11 17:56:53 +00002797// SizeOfAlignOfExpr
John McCallbd066782011-02-09 08:16:59 +00002798Stmt::child_range SizeOfAlignOfExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00002799 // If this is of a type and the type is a VLA type (and not a typedef), the
2800 // size expression of the VLA needs to be treated as an executable expression.
2801 // Why isn't this weirdness documented better in StmtIterator?
2802 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00002803 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00002804 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00002805 return child_range(child_iterator(T), child_iterator());
2806 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00002807 }
John McCallbd066782011-02-09 08:16:59 +00002808 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00002809}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00002810
Steve Naroffd54978b2007-09-18 23:55:05 +00002811// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00002812Stmt::child_range ObjCMessageExpr::children() {
2813 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00002814 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00002815 begin = reinterpret_cast<Stmt **>(this + 1);
2816 else
2817 begin = reinterpret_cast<Stmt **>(getArgs());
2818 return child_range(begin,
2819 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00002820}
2821
Steve Naroffc540d662008-09-03 18:15:37 +00002822// Blocks
John McCall351762c2011-02-07 10:33:21 +00002823BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregor476e3022011-01-19 21:32:01 +00002824 SourceLocation l, bool ByRef,
John McCall351762c2011-02-07 10:33:21 +00002825 bool constAdded)
Douglas Gregorf144f4f2011-01-19 21:52:31 +00002826 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregor476e3022011-01-19 21:32:01 +00002827 d->isParameterPack()),
John McCall351762c2011-02-07 10:33:21 +00002828 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregor476e3022011-01-19 21:32:01 +00002829{
Douglas Gregorf144f4f2011-01-19 21:52:31 +00002830 bool TypeDependent = false;
2831 bool ValueDependent = false;
2832 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
2833 ExprBits.TypeDependent = TypeDependent;
2834 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor476e3022011-01-19 21:32:01 +00002835}
2836