blob: cc2bdf5cd0af0a7c66dd55887623a4c004f10d8e [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"
Richard Smith938f40b2011-06-11 17:19:42 +000025#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Chris Lattnere925d612010-11-17 07:37:15 +000027#include "clang/Basic/SourceManager.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000029#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000031#include <algorithm>
Chris Lattner1b926492006-08-23 06:42:10 +000032using namespace clang;
33
Chris Lattner4ebae652010-04-16 23:34:13 +000034/// isKnownToHaveBooleanValue - Return true if this is an integer expression
35/// that is known to return 0 or 1. This happens for _Bool/bool expressions
36/// but also int expressions which are produced by things like comparisons in
37/// C.
38bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbourne91147592011-04-15 00:35:48 +000039 const Expr *E = IgnoreParens();
40
Chris Lattner4ebae652010-04-16 23:34:13 +000041 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbourne91147592011-04-15 00:35:48 +000042 if (E->getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000043 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbourne91147592011-04-15 00:35:48 +000044 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +000045
Peter Collingbourne91147592011-04-15 00:35:48 +000046 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +000047 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +000048 case UO_Plus:
Chris Lattner4ebae652010-04-16 23:34:13 +000049 return UO->getSubExpr()->isKnownToHaveBooleanValue();
50 default:
51 return false;
52 }
53 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000054
John McCall45d30c32010-06-12 01:56:02 +000055 // Only look through implicit casts. If the user writes
56 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbourne91147592011-04-15 00:35:48 +000057 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +000058 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000059
Peter Collingbourne91147592011-04-15 00:35:48 +000060 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +000061 switch (BO->getOpcode()) {
62 default: return false;
John McCalle3027922010-08-25 11:45:40 +000063 case BO_LT: // Relational operators.
64 case BO_GT:
65 case BO_LE:
66 case BO_GE:
67 case BO_EQ: // Equality operators.
68 case BO_NE:
69 case BO_LAnd: // AND operator.
70 case BO_LOr: // Logical OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +000071 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000072
John McCalle3027922010-08-25 11:45:40 +000073 case BO_And: // Bitwise AND operator.
74 case BO_Xor: // Bitwise XOR operator.
75 case BO_Or: // Bitwise OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +000076 // Handle things like (x==2)|(y==12).
77 return BO->getLHS()->isKnownToHaveBooleanValue() &&
78 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000079
John McCalle3027922010-08-25 11:45:40 +000080 case BO_Comma:
81 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +000082 return BO->getRHS()->isKnownToHaveBooleanValue();
83 }
84 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000085
Peter Collingbourne91147592011-04-15 00:35:48 +000086 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +000087 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
88 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000089
Chris Lattner4ebae652010-04-16 23:34:13 +000090 return false;
91}
92
John McCallbd066782011-02-09 08:16:59 +000093// Amusing macro metaprogramming hack: check whether a class provides
94// a more specific implementation of getExprLoc().
95namespace {
96 /// This implementation is used when a class provides a custom
97 /// implementation of getExprLoc.
98 template <class E, class T>
99 SourceLocation getExprLocImpl(const Expr *expr,
100 SourceLocation (T::*v)() const) {
101 return static_cast<const E*>(expr)->getExprLoc();
102 }
103
104 /// This implementation is used when a class doesn't provide
105 /// a custom implementation of getExprLoc. Overload resolution
106 /// should pick it over the implementation above because it's
107 /// more specialized according to function template partial ordering.
108 template <class E>
109 SourceLocation getExprLocImpl(const Expr *expr,
110 SourceLocation (Expr::*v)() const) {
111 return static_cast<const E*>(expr)->getSourceRange().getBegin();
112 }
113}
114
115SourceLocation Expr::getExprLoc() const {
116 switch (getStmtClass()) {
117 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
118#define ABSTRACT_STMT(type)
119#define STMT(type, base) \
120 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
121#define EXPR(type, base) \
122 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
123#include "clang/AST/StmtNodes.inc"
124 }
125 llvm_unreachable("unknown statement kind");
126 return SourceLocation();
127}
128
Chris Lattner0eedafe2006-08-24 04:56:27 +0000129//===----------------------------------------------------------------------===//
130// Primary Expressions.
131//===----------------------------------------------------------------------===//
132
John McCall6b51f282009-11-23 01:53:49 +0000133void ExplicitTemplateArgumentList::initializeFrom(
134 const TemplateArgumentListInfo &Info) {
135 LAngleLoc = Info.getLAngleLoc();
136 RAngleLoc = Info.getRAngleLoc();
137 NumTemplateArgs = Info.size();
138
139 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
140 for (unsigned i = 0; i != NumTemplateArgs; ++i)
141 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
142}
143
Douglas Gregora6e053e2010-12-15 01:34:56 +0000144void ExplicitTemplateArgumentList::initializeFrom(
145 const TemplateArgumentListInfo &Info,
146 bool &Dependent,
147 bool &ContainsUnexpandedParameterPack) {
148 LAngleLoc = Info.getLAngleLoc();
149 RAngleLoc = Info.getRAngleLoc();
150 NumTemplateArgs = Info.size();
151
152 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
153 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
154 Dependent = Dependent || Info[i].getArgument().isDependent();
155 ContainsUnexpandedParameterPack
156 = ContainsUnexpandedParameterPack ||
157 Info[i].getArgument().containsUnexpandedParameterPack();
158
159 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
160 }
161}
162
John McCall6b51f282009-11-23 01:53:49 +0000163void ExplicitTemplateArgumentList::copyInto(
164 TemplateArgumentListInfo &Info) const {
165 Info.setLAngleLoc(LAngleLoc);
166 Info.setRAngleLoc(RAngleLoc);
167 for (unsigned I = 0; I != NumTemplateArgs; ++I)
168 Info.addArgument(getTemplateArgs()[I]);
169}
170
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000171std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
172 return sizeof(ExplicitTemplateArgumentList) +
173 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
174}
175
John McCall6b51f282009-11-23 01:53:49 +0000176std::size_t ExplicitTemplateArgumentList::sizeFor(
177 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000178 return sizeFor(Info.size());
John McCall6b51f282009-11-23 01:53:49 +0000179}
180
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000181/// \brief Compute the type- and value-dependence of a declaration reference
182/// based on the declaration being referenced.
183static void computeDeclRefDependence(NamedDecl *D, QualType T,
184 bool &TypeDependent,
185 bool &ValueDependent) {
186 TypeDependent = false;
187 ValueDependent = false;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000188
Douglas Gregored6c7442009-11-23 11:41:28 +0000189
190 // (TD) C++ [temp.dep.expr]p3:
191 // An id-expression is type-dependent if it contains:
192 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000193 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000194 //
195 // (VD) C++ [temp.dep.constexpr]p2:
196 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000197
Douglas Gregored6c7442009-11-23 11:41:28 +0000198 // (TD) - an identifier that was declared with dependent type
199 // (VD) - a name declared with a dependent type,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000200 if (T->isDependentType()) {
201 TypeDependent = true;
202 ValueDependent = true;
203 return;
Douglas Gregored6c7442009-11-23 11:41:28 +0000204 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000205
Douglas Gregored6c7442009-11-23 11:41:28 +0000206 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000207 if (D->getDeclName().getNameKind()
208 == DeclarationName::CXXConversionFunctionName &&
Douglas Gregored6c7442009-11-23 11:41:28 +0000209 D->getDeclName().getCXXNameType()->isDependentType()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000210 TypeDependent = true;
211 ValueDependent = true;
212 return;
Douglas Gregored6c7442009-11-23 11:41:28 +0000213 }
214 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000215 if (isa<NonTypeTemplateParmDecl>(D)) {
216 ValueDependent = true;
217 return;
218 }
219
Douglas Gregored6c7442009-11-23 11:41:28 +0000220 // (VD) - a constant with integral or enumeration type and is
221 // initialized with an expression that is value-dependent.
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000222 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000223 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000224 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000225 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000226 if (Init->isValueDependent())
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000227 ValueDependent = true;
Douglas Gregor0e4de762010-05-11 08:41:30 +0000228 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000229
Douglas Gregor0e4de762010-05-11 08:41:30 +0000230 // (VD) - FIXME: Missing from the standard:
231 // - a member function or a static data member of the current
232 // instantiation
233 else if (Var->isStaticDataMember() &&
Douglas Gregorbe49fc52010-05-11 08:44:04 +0000234 Var->getDeclContext()->isDependentContext())
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000235 ValueDependent = true;
236
237 return;
238 }
239
Douglas Gregor0e4de762010-05-11 08:41:30 +0000240 // (VD) - FIXME: Missing from the standard:
241 // - a member function or a static data member of the current
242 // instantiation
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000243 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
244 ValueDependent = true;
245 return;
246 }
247}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000248
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000249void DeclRefExpr::computeDependence() {
250 bool TypeDependent = false;
251 bool ValueDependent = false;
252 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent);
253
254 // (TD) C++ [temp.dep.expr]p3:
255 // An id-expression is type-dependent if it contains:
256 //
257 // and
258 //
259 // (VD) C++ [temp.dep.constexpr]p2:
260 // An identifier is value-dependent if it is:
261 if (!TypeDependent && !ValueDependent &&
262 hasExplicitTemplateArgs() &&
263 TemplateSpecializationType::anyDependentTemplateArguments(
264 getTemplateArgs(),
265 getNumTemplateArgs())) {
266 TypeDependent = true;
267 ValueDependent = true;
268 }
269
270 ExprBits.TypeDependent = TypeDependent;
271 ExprBits.ValueDependent = ValueDependent;
272
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000273 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000274 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000275 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000276}
277
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000278DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000279 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000280 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +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),
Chandler Carruth0e439962011-05-01 21:29:53 +0000284 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
285 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000286 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000287 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000288 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
289 if (FoundD)
290 getInternalFoundDecl() = FoundD;
Chandler Carruth0e439962011-05-01 21:29:53 +0000291 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000292 if (TemplateArgs)
John McCallb3774b52010-08-19 23:49:38 +0000293 getExplicitTemplateArgs().initializeFrom(*TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000294
295 computeDependence();
296}
297
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000298DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000299 NestedNameSpecifierLoc QualifierLoc,
John McCallce546572009-12-08 09:08:17 +0000300 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000301 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000302 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000303 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000304 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000305 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000306 return Create(Context, QualifierLoc, D,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000307 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000308 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000309}
310
311DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000312 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000313 ValueDecl *D,
314 const DeclarationNameInfo &NameInfo,
315 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000316 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000317 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000318 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000319 // Filter out cases where the found Decl is the same as the value refenenced.
320 if (D == FoundD)
321 FoundD = 0;
322
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000323 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregorea972d32011-02-28 21:54:11 +0000324 if (QualifierLoc != 0)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000325 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000326 if (FoundD)
327 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000328 if (TemplateArgs)
329 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000330
Chris Lattner5c0b4052010-10-30 05:14:06 +0000331 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000332 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
333 T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000334}
335
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000336DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000337 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000338 bool HasFoundDecl,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000339 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000340 unsigned NumTemplateArgs) {
341 std::size_t Size = sizeof(DeclRefExpr);
342 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000343 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000344 if (HasFoundDecl)
345 Size += sizeof(NamedDecl *);
Douglas Gregor87866ce2011-02-04 12:01:24 +0000346 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000347 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000348
Chris Lattner5c0b4052010-10-30 05:14:06 +0000349 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000350 return new (Mem) DeclRefExpr(EmptyShell());
351}
352
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000353SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000354 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000355 if (hasQualifier())
Douglas Gregorea972d32011-02-28 21:54:11 +0000356 R.setBegin(getQualifierLoc().getBeginLoc());
John McCallb3774b52010-08-19 23:49:38 +0000357 if (hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000358 R.setEnd(getRAngleLoc());
359 return R;
360}
361
Anders Carlsson2fb08242009-09-08 18:24:21 +0000362// FIXME: Maybe this should use DeclPrinter with a special "print predefined
363// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000364std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
365 ASTContext &Context = CurrentDecl->getASTContext();
366
Anders Carlsson2fb08242009-09-08 18:24:21 +0000367 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000368 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000369 return FD->getNameAsString();
370
371 llvm::SmallString<256> Name;
372 llvm::raw_svector_ostream Out(Name);
373
374 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000375 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000376 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000377 if (MD->isStatic())
378 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000379 }
380
381 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000382
383 std::string Proto = FD->getQualifiedNameAsString(Policy);
384
John McCall9dd450b2009-09-21 23:43:11 +0000385 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000386 const FunctionProtoType *FT = 0;
387 if (FD->hasWrittenPrototype())
388 FT = dyn_cast<FunctionProtoType>(AFT);
389
390 Proto += "(";
391 if (FT) {
392 llvm::raw_string_ostream POut(Proto);
393 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
394 if (i) POut << ", ";
395 std::string Param;
396 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
397 POut << Param;
398 }
399
400 if (FT->isVariadic()) {
401 if (FD->getNumParams()) POut << ", ";
402 POut << "...";
403 }
404 }
405 Proto += ")";
406
Sam Weinig4e83bd22009-12-27 01:38:20 +0000407 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
408 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
409 if (ThisQuals.hasConst())
410 Proto += " const";
411 if (ThisQuals.hasVolatile())
412 Proto += " volatile";
413 }
414
Sam Weinigd060ed42009-12-06 23:55:13 +0000415 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
416 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000417
418 Out << Proto;
419
420 Out.flush();
421 return Name.str().str();
422 }
423 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
424 llvm::SmallString<256> Name;
425 llvm::raw_svector_ostream Out(Name);
426 Out << (MD->isInstanceMethod() ? '-' : '+');
427 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000428
429 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
430 // a null check to avoid a crash.
431 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000432 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000433
Anders Carlsson2fb08242009-09-08 18:24:21 +0000434 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000435 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
436 Out << '(' << CID << ')';
437
Anders Carlsson2fb08242009-09-08 18:24:21 +0000438 Out << ' ';
439 Out << MD->getSelector().getAsString();
440 Out << ']';
441
442 Out.flush();
443 return Name.str().str();
444 }
445 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
446 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
447 return "top level";
448 }
449 return "";
450}
451
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000452void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
453 if (hasAllocation())
454 C.Deallocate(pVal);
455
456 BitWidth = Val.getBitWidth();
457 unsigned NumWords = Val.getNumWords();
458 const uint64_t* Words = Val.getRawData();
459 if (NumWords > 1) {
460 pVal = new (C) uint64_t[NumWords];
461 std::copy(Words, Words + NumWords, pVal);
462 } else if (NumWords == 1)
463 VAL = Words[0];
464 else
465 VAL = 0;
466}
467
468IntegerLiteral *
469IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
470 QualType type, SourceLocation l) {
471 return new (C) IntegerLiteral(C, V, type, l);
472}
473
474IntegerLiteral *
475IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
476 return new (C) IntegerLiteral(Empty);
477}
478
479FloatingLiteral *
480FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
481 bool isexact, QualType Type, SourceLocation L) {
482 return new (C) FloatingLiteral(C, V, isexact, Type, L);
483}
484
485FloatingLiteral *
486FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
487 return new (C) FloatingLiteral(Empty);
488}
489
Chris Lattnera0173132008-06-07 22:13:43 +0000490/// getValueAsApproximateDouble - This returns the value as an inaccurate
491/// double. Note that this may cause loss of precision, but is useful for
492/// debugging dumps, etc.
493double FloatingLiteral::getValueAsApproximateDouble() const {
494 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000495 bool ignored;
496 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
497 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000498 return V.convertToDouble();
499}
500
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000501StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData,
502 unsigned ByteLength, bool Wide,
Anders Carlsson75245402011-04-14 00:40:03 +0000503 bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000504 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000505 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000506 // Allocate enough space for the StringLiteral plus an array of locations for
507 // any concatenated string tokens.
508 void *Mem = C.Allocate(sizeof(StringLiteral)+
509 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000510 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000511 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000512
Steve Naroffdf7855b2007-02-21 23:46:25 +0000513 // OPTIMIZE: could allocate this appended to the StringLiteral.
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000514 char *AStrData = new (C, 1) char[ByteLength];
515 memcpy(AStrData, StrData, ByteLength);
516 SL->StrData = AStrData;
517 SL->ByteLength = ByteLength;
518 SL->IsWide = Wide;
Anders Carlsson75245402011-04-14 00:40:03 +0000519 SL->IsPascal = Pascal;
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000520 SL->TokLocs[0] = Loc[0];
521 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000522
Chris Lattner630970d2009-02-18 05:49:11 +0000523 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000524 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
525 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000526}
527
Douglas Gregor958dfc92009-04-15 16:35:07 +0000528StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
529 void *Mem = C.Allocate(sizeof(StringLiteral)+
530 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000531 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000532 StringLiteral *SL = new (Mem) StringLiteral(QualType());
533 SL->StrData = 0;
534 SL->ByteLength = 0;
535 SL->NumConcatenated = NumStrs;
536 return SL;
537}
538
Daniel Dunbar36217882009-09-22 03:27:33 +0000539void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbar36217882009-09-22 03:27:33 +0000540 char *AStrData = new (C, 1) char[Str.size()];
541 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000542 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000543 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000544}
545
Chris Lattnere925d612010-11-17 07:37:15 +0000546/// getLocationOfByte - Return a source location that points to the specified
547/// byte of this string literal.
548///
549/// Strings are amazingly complex. They can be formed from multiple tokens and
550/// can have escape sequences in them in addition to the usual trigraph and
551/// escaped newline business. This routine handles this complexity.
552///
553SourceLocation StringLiteral::
554getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
555 const LangOptions &Features, const TargetInfo &Target) const {
556 assert(!isWide() && "This doesn't work for wide strings yet");
557
558 // Loop over all of the tokens in this string until we find the one that
559 // contains the byte we're looking for.
560 unsigned TokNo = 0;
561 while (1) {
562 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
563 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
564
565 // Get the spelling of the string so that we can get the data that makes up
566 // the string literal, not the identifier for the macro it is potentially
567 // expanded through.
568 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
569
570 // Re-lex the token to get its length and original spelling.
571 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
572 bool Invalid = false;
573 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
574 if (Invalid)
575 return StrTokSpellingLoc;
576
577 const char *StrData = Buffer.data()+LocInfo.second;
578
579 // Create a langops struct and enable trigraphs. This is sufficient for
580 // relexing tokens.
581 LangOptions LangOpts;
582 LangOpts.Trigraphs = true;
583
584 // Create a lexer starting at the beginning of this token.
585 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
586 Buffer.end());
587 Token TheTok;
588 TheLexer.LexFromRawLexer(TheTok);
589
590 // Use the StringLiteralParser to compute the length of the string in bytes.
591 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
592 unsigned TokNumBytes = SLP.GetStringLength();
593
594 // If the byte is in this token, return the location of the byte.
595 if (ByteNo < TokNumBytes ||
596 (ByteNo == TokNumBytes && TokNo == getNumConcatenated())) {
597 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
598
599 // Now that we know the offset of the token in the spelling, use the
600 // preprocessor to get the offset in the original source.
601 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
602 }
603
604 // Move to the next string token.
605 ++TokNo;
606 ByteNo -= TokNumBytes;
607 }
608}
609
610
611
Chris Lattner1b926492006-08-23 06:42:10 +0000612/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
613/// corresponds to, e.g. "sizeof" or "[pre]++".
614const char *UnaryOperator::getOpcodeStr(Opcode Op) {
615 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000616 default: assert(0 && "Unknown unary operator");
John McCalle3027922010-08-25 11:45:40 +0000617 case UO_PostInc: return "++";
618 case UO_PostDec: return "--";
619 case UO_PreInc: return "++";
620 case UO_PreDec: return "--";
621 case UO_AddrOf: return "&";
622 case UO_Deref: return "*";
623 case UO_Plus: return "+";
624 case UO_Minus: return "-";
625 case UO_Not: return "~";
626 case UO_LNot: return "!";
627 case UO_Real: return "__real";
628 case UO_Imag: return "__imag";
629 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000630 }
631}
632
John McCalle3027922010-08-25 11:45:40 +0000633UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000634UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
635 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000636 default: assert(false && "No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000637 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
638 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
639 case OO_Amp: return UO_AddrOf;
640 case OO_Star: return UO_Deref;
641 case OO_Plus: return UO_Plus;
642 case OO_Minus: return UO_Minus;
643 case OO_Tilde: return UO_Not;
644 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000645 }
646}
647
648OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
649 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000650 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
651 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
652 case UO_AddrOf: return OO_Amp;
653 case UO_Deref: return OO_Star;
654 case UO_Plus: return OO_Plus;
655 case UO_Minus: return OO_Minus;
656 case UO_Not: return OO_Tilde;
657 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000658 default: return OO_None;
659 }
660}
661
662
Chris Lattner0eedafe2006-08-24 04:56:27 +0000663//===----------------------------------------------------------------------===//
664// Postfix Operators.
665//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000666
Peter Collingbourne3a347252011-02-08 21:18:02 +0000667CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
668 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCall7decc9e2010-11-18 06:31:45 +0000669 SourceLocation rparenloc)
670 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000671 fn->isTypeDependent(),
672 fn->isValueDependent(),
673 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000674 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000675
Peter Collingbourne3a347252011-02-08 21:18:02 +0000676 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +0000677 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000678 for (unsigned i = 0; i != numargs; ++i) {
679 if (args[i]->isTypeDependent())
680 ExprBits.TypeDependent = true;
681 if (args[i]->isValueDependent())
682 ExprBits.ValueDependent = true;
683 if (args[i]->containsUnexpandedParameterPack())
684 ExprBits.ContainsUnexpandedParameterPack = true;
685
Peter Collingbourne3a347252011-02-08 21:18:02 +0000686 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000687 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000688
Peter Collingbourne3a347252011-02-08 21:18:02 +0000689 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +0000690 RParenLoc = rparenloc;
691}
Nate Begeman1e36a852008-01-17 17:46:27 +0000692
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000693CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCall7decc9e2010-11-18 06:31:45 +0000694 QualType t, ExprValueKind VK, SourceLocation rparenloc)
695 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000696 fn->isTypeDependent(),
697 fn->isValueDependent(),
698 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000699 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000700
Peter Collingbourne3a347252011-02-08 21:18:02 +0000701 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000702 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000703 for (unsigned i = 0; i != numargs; ++i) {
704 if (args[i]->isTypeDependent())
705 ExprBits.TypeDependent = true;
706 if (args[i]->isValueDependent())
707 ExprBits.ValueDependent = true;
708 if (args[i]->containsUnexpandedParameterPack())
709 ExprBits.ContainsUnexpandedParameterPack = true;
710
Peter Collingbourne3a347252011-02-08 21:18:02 +0000711 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000712 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000713
Peter Collingbourne3a347252011-02-08 21:18:02 +0000714 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000715 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000716}
717
Mike Stump11289f42009-09-09 15:08:12 +0000718CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
719 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000720 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +0000721 SubExprs = new (C) Stmt*[PREARGS_START];
722 CallExprBits.NumPreArgs = 0;
723}
724
725CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
726 EmptyShell Empty)
727 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
728 // FIXME: Why do we allocate this?
729 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
730 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +0000731}
732
Nuno Lopes518e3702009-12-20 23:11:08 +0000733Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000734 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl2b1832e2010-09-10 20:55:30 +0000735 // If we're calling a dereference, look at the pointer instead.
736 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
737 if (BO->isPtrMemOp())
738 CEE = BO->getRHS()->IgnoreParenCasts();
739 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
740 if (UO->getOpcode() == UO_Deref)
741 CEE = UO->getSubExpr()->IgnoreParenCasts();
742 }
Chris Lattner52301912009-07-17 15:46:27 +0000743 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000744 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000745 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
746 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000747
748 return 0;
749}
750
Nuno Lopes518e3702009-12-20 23:11:08 +0000751FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000752 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000753}
754
Chris Lattnere4407ed2007-12-28 05:25:02 +0000755/// setNumArgs - This changes the number of arguments present in this call.
756/// Any orphaned expressions are deleted by this, and any new operands are set
757/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000758void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000759 // No change, just return.
760 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000761
Chris Lattnere4407ed2007-12-28 05:25:02 +0000762 // If shrinking # arguments, just delete the extras and forgot them.
763 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000764 this->NumArgs = NumArgs;
765 return;
766 }
767
768 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000769 unsigned NumPreArgs = getNumPreArgs();
770 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000771 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000772 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000773 NewSubExprs[i] = SubExprs[i];
774 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000775 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
776 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000777 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000778
Douglas Gregorba6e5572009-04-17 21:46:47 +0000779 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000780 SubExprs = NewSubExprs;
781 this->NumArgs = NumArgs;
782}
783
Chris Lattner01ff98a2008-10-06 05:00:53 +0000784/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
785/// not, return 0.
Jay Foad39c79802011-01-12 09:06:06 +0000786unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000787 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000788 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000789 // ImplicitCastExpr.
790 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
791 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000792 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000793
Steve Narofff6e3b3292008-01-31 01:07:12 +0000794 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
795 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000796 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000797
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000798 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
799 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000800 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000801
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000802 if (!FDecl->getIdentifier())
803 return 0;
804
Douglas Gregor15fc9562009-09-12 00:22:50 +0000805 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000806}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000807
Anders Carlsson00a27592009-05-26 04:57:27 +0000808QualType CallExpr::getCallReturnType() const {
809 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000810 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000811 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000812 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000813 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +0000814 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
815 // This should never be overloaded and so should never return null.
816 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +0000817
John McCall0009fcc2011-04-26 20:42:42 +0000818 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000819 return FnType->getResultType();
820}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000821
John McCall701417a2011-02-21 06:23:05 +0000822SourceRange CallExpr::getSourceRange() const {
823 if (isa<CXXOperatorCallExpr>(this))
824 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
825
826 SourceLocation begin = getCallee()->getLocStart();
827 if (begin.isInvalid() && getNumArgs() > 0)
828 begin = getArg(0)->getLocStart();
829 SourceLocation end = getRParenLoc();
830 if (end.isInvalid() && getNumArgs() > 0)
831 end = getArg(getNumArgs() - 1)->getLocEnd();
832 return SourceRange(begin, end);
833}
834
Alexis Hunta8136cc2010-05-05 15:23:54 +0000835OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000836 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000837 TypeSourceInfo *tsi,
838 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000839 Expr** exprsPtr, unsigned numExprs,
840 SourceLocation RParenLoc) {
841 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000842 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000843 sizeof(Expr*) * numExprs);
844
845 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
846 exprsPtr, numExprs, RParenLoc);
847}
848
849OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
850 unsigned numComps, unsigned numExprs) {
851 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
852 sizeof(OffsetOfNode) * numComps +
853 sizeof(Expr*) * numExprs);
854 return new (Mem) OffsetOfExpr(numComps, numExprs);
855}
856
Alexis Hunta8136cc2010-05-05 15:23:54 +0000857OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000858 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000859 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000860 Expr** exprsPtr, unsigned numExprs,
861 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +0000862 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
863 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000864 /*ValueDependent=*/tsi->getType()->isDependentType(),
865 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000866 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
867 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000868{
869 for(unsigned i = 0; i < numComps; ++i) {
870 setComponent(i, compsPtr[i]);
871 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000872
Douglas Gregor882211c2010-04-28 22:16:22 +0000873 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000874 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
875 ExprBits.ValueDependent = true;
876 if (exprsPtr[i]->containsUnexpandedParameterPack())
877 ExprBits.ContainsUnexpandedParameterPack = true;
878
Douglas Gregor882211c2010-04-28 22:16:22 +0000879 setIndexExpr(i, exprsPtr[i]);
880 }
881}
882
883IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
884 assert(getKind() == Field || getKind() == Identifier);
885 if (getKind() == Field)
886 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000887
Douglas Gregor882211c2010-04-28 22:16:22 +0000888 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
889}
890
Mike Stump11289f42009-09-09 15:08:12 +0000891MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +0000892 NestedNameSpecifierLoc QualifierLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000893 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000894 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000895 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +0000896 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +0000897 QualType ty,
898 ExprValueKind vk,
899 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000900 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000901
Douglas Gregorea972d32011-02-28 21:54:11 +0000902 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +0000903 founddecl.getDecl() != memberdecl ||
904 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000905 if (hasQualOrFound)
906 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000907
John McCall6b51f282009-11-23 01:53:49 +0000908 if (targs)
909 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000910
Chris Lattner5c0b4052010-10-30 05:14:06 +0000911 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +0000912 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
913 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +0000914
915 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000916 // FIXME: Wrong. We should be looking at the member declaration we found.
917 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +0000918 E->setValueDependent(true);
919 E->setTypeDependent(true);
920 }
921 E->HasQualifierOrFoundDecl = true;
922
923 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +0000924 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +0000925 NQ->FoundDecl = founddecl;
926 }
927
928 if (targs) {
929 E->HasExplicitTemplateArgumentList = true;
John McCallb3774b52010-08-19 23:49:38 +0000930 E->getExplicitTemplateArgs().initializeFrom(*targs);
John McCall16df1e52010-03-30 21:47:33 +0000931 }
932
933 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000934}
935
Douglas Gregor25b7e052011-03-02 21:06:53 +0000936SourceRange MemberExpr::getSourceRange() const {
937 SourceLocation StartLoc;
938 if (isImplicitAccess()) {
939 if (hasQualifier())
940 StartLoc = getQualifierLoc().getBeginLoc();
941 else
942 StartLoc = MemberLoc;
943 } else {
944 // FIXME: We don't want this to happen. Rather, we should be able to
945 // detect all kinds of implicit accesses more cleanly.
946 StartLoc = getBase()->getLocStart();
947 if (StartLoc.isInvalid())
948 StartLoc = MemberLoc;
949 }
950
951 SourceLocation EndLoc =
952 HasExplicitTemplateArgumentList? getRAngleLoc()
953 : getMemberNameInfo().getEndLoc();
954
955 return SourceRange(StartLoc, EndLoc);
956}
957
Anders Carlsson496335e2009-09-03 00:59:21 +0000958const char *CastExpr::getCastKindName() const {
959 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +0000960 case CK_Dependent:
961 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +0000962 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +0000963 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +0000964 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +0000965 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +0000966 case CK_LValueToRValue:
967 return "LValueToRValue";
John McCall34376a62010-12-04 03:47:34 +0000968 case CK_GetObjCProperty:
969 return "GetObjCProperty";
John McCalle3027922010-08-25 11:45:40 +0000970 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +0000971 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +0000972 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +0000973 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +0000974 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +0000975 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000976 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +0000977 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +0000978 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +0000979 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +0000980 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000981 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +0000982 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000983 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000984 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +0000985 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +0000986 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000987 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +0000988 case CK_NullToPointer:
989 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +0000990 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +0000991 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000992 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +0000993 return "DerivedToBaseMemberPointer";
John McCalle3027922010-08-25 11:45:40 +0000994 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000995 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +0000996 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +0000997 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +0000998 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +0000999 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001000 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001001 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001002 case CK_PointerToBoolean:
1003 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001004 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001005 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001006 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001007 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001008 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001009 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001010 case CK_IntegralToBoolean:
1011 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001012 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001013 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001014 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001015 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001016 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001017 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001018 case CK_FloatingToBoolean:
1019 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001020 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001021 return "MemberPointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001022 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahaniane19122f2009-12-08 23:46:15 +00001023 return "AnyPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001024 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001025 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001026 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001027 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001028 case CK_FloatingRealToComplex:
1029 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001030 case CK_FloatingComplexToReal:
1031 return "FloatingComplexToReal";
1032 case CK_FloatingComplexToBoolean:
1033 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001034 case CK_FloatingComplexCast:
1035 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001036 case CK_FloatingComplexToIntegralComplex:
1037 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001038 case CK_IntegralRealToComplex:
1039 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001040 case CK_IntegralComplexToReal:
1041 return "IntegralComplexToReal";
1042 case CK_IntegralComplexToBoolean:
1043 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001044 case CK_IntegralComplexCast:
1045 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001046 case CK_IntegralComplexToFloatingComplex:
1047 return "IntegralComplexToFloatingComplex";
John McCall31168b02011-06-15 23:02:42 +00001048 case CK_ObjCConsumeObject:
1049 return "ObjCConsumeObject";
1050 case CK_ObjCProduceObject:
1051 return "ObjCProduceObject";
Anders Carlsson496335e2009-09-03 00:59:21 +00001052 }
Mike Stump11289f42009-09-09 15:08:12 +00001053
John McCallc5e62b42010-11-13 09:02:35 +00001054 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001055 return 0;
1056}
1057
Douglas Gregord196a582009-12-14 19:27:10 +00001058Expr *CastExpr::getSubExprAsWritten() {
1059 Expr *SubExpr = 0;
1060 CastExpr *E = this;
1061 do {
1062 SubExpr = E->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001063
Douglas Gregord196a582009-12-14 19:27:10 +00001064 // Skip any temporary bindings; they're implicit.
1065 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1066 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001067
Douglas Gregord196a582009-12-14 19:27:10 +00001068 // Conversions by constructor and conversion functions have a
1069 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001070 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001071 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001072 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001073 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001074
Douglas Gregord196a582009-12-14 19:27:10 +00001075 // If the subexpression we're left with is an implicit cast, look
1076 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001077 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1078
Douglas Gregord196a582009-12-14 19:27:10 +00001079 return SubExpr;
1080}
1081
John McCallcf142162010-08-07 06:22:56 +00001082CXXBaseSpecifier **CastExpr::path_buffer() {
1083 switch (getStmtClass()) {
1084#define ABSTRACT_STMT(x)
1085#define CASTEXPR(Type, Base) \
1086 case Stmt::Type##Class: \
1087 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1088#define STMT(Type, Base)
1089#include "clang/AST/StmtNodes.inc"
1090 default:
1091 llvm_unreachable("non-cast expressions not possible here");
1092 return 0;
1093 }
1094}
1095
1096void CastExpr::setCastPath(const CXXCastPath &Path) {
1097 assert(Path.size() == path_size());
1098 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1099}
1100
1101ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1102 CastKind Kind, Expr *Operand,
1103 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001104 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001105 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1106 void *Buffer =
1107 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1108 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001109 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001110 if (PathSize) E->setCastPath(*BasePath);
1111 return E;
1112}
1113
1114ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1115 unsigned PathSize) {
1116 void *Buffer =
1117 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1118 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1119}
1120
1121
1122CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001123 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001124 const CXXCastPath *BasePath,
1125 TypeSourceInfo *WrittenTy,
1126 SourceLocation L, SourceLocation R) {
1127 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1128 void *Buffer =
1129 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1130 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001131 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001132 if (PathSize) E->setCastPath(*BasePath);
1133 return E;
1134}
1135
1136CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1137 void *Buffer =
1138 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1139 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1140}
1141
Chris Lattner1b926492006-08-23 06:42:10 +00001142/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1143/// corresponds to, e.g. "<<=".
1144const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1145 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001146 case BO_PtrMemD: return ".*";
1147 case BO_PtrMemI: return "->*";
1148 case BO_Mul: return "*";
1149 case BO_Div: return "/";
1150 case BO_Rem: return "%";
1151 case BO_Add: return "+";
1152 case BO_Sub: return "-";
1153 case BO_Shl: return "<<";
1154 case BO_Shr: return ">>";
1155 case BO_LT: return "<";
1156 case BO_GT: return ">";
1157 case BO_LE: return "<=";
1158 case BO_GE: return ">=";
1159 case BO_EQ: return "==";
1160 case BO_NE: return "!=";
1161 case BO_And: return "&";
1162 case BO_Xor: return "^";
1163 case BO_Or: return "|";
1164 case BO_LAnd: return "&&";
1165 case BO_LOr: return "||";
1166 case BO_Assign: return "=";
1167 case BO_MulAssign: return "*=";
1168 case BO_DivAssign: return "/=";
1169 case BO_RemAssign: return "%=";
1170 case BO_AddAssign: return "+=";
1171 case BO_SubAssign: return "-=";
1172 case BO_ShlAssign: return "<<=";
1173 case BO_ShrAssign: return ">>=";
1174 case BO_AndAssign: return "&=";
1175 case BO_XorAssign: return "^=";
1176 case BO_OrAssign: return "|=";
1177 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001178 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001179
1180 return "";
Chris Lattner1b926492006-08-23 06:42:10 +00001181}
Steve Naroff47500512007-04-19 23:00:49 +00001182
John McCalle3027922010-08-25 11:45:40 +00001183BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001184BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1185 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +00001186 default: assert(false && "Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001187 case OO_Plus: return BO_Add;
1188 case OO_Minus: return BO_Sub;
1189 case OO_Star: return BO_Mul;
1190 case OO_Slash: return BO_Div;
1191 case OO_Percent: return BO_Rem;
1192 case OO_Caret: return BO_Xor;
1193 case OO_Amp: return BO_And;
1194 case OO_Pipe: return BO_Or;
1195 case OO_Equal: return BO_Assign;
1196 case OO_Less: return BO_LT;
1197 case OO_Greater: return BO_GT;
1198 case OO_PlusEqual: return BO_AddAssign;
1199 case OO_MinusEqual: return BO_SubAssign;
1200 case OO_StarEqual: return BO_MulAssign;
1201 case OO_SlashEqual: return BO_DivAssign;
1202 case OO_PercentEqual: return BO_RemAssign;
1203 case OO_CaretEqual: return BO_XorAssign;
1204 case OO_AmpEqual: return BO_AndAssign;
1205 case OO_PipeEqual: return BO_OrAssign;
1206 case OO_LessLess: return BO_Shl;
1207 case OO_GreaterGreater: return BO_Shr;
1208 case OO_LessLessEqual: return BO_ShlAssign;
1209 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1210 case OO_EqualEqual: return BO_EQ;
1211 case OO_ExclaimEqual: return BO_NE;
1212 case OO_LessEqual: return BO_LE;
1213 case OO_GreaterEqual: return BO_GE;
1214 case OO_AmpAmp: return BO_LAnd;
1215 case OO_PipePipe: return BO_LOr;
1216 case OO_Comma: return BO_Comma;
1217 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001218 }
1219}
1220
1221OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1222 static const OverloadedOperatorKind OverOps[] = {
1223 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1224 OO_Star, OO_Slash, OO_Percent,
1225 OO_Plus, OO_Minus,
1226 OO_LessLess, OO_GreaterGreater,
1227 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1228 OO_EqualEqual, OO_ExclaimEqual,
1229 OO_Amp,
1230 OO_Caret,
1231 OO_Pipe,
1232 OO_AmpAmp,
1233 OO_PipePipe,
1234 OO_Equal, OO_StarEqual,
1235 OO_SlashEqual, OO_PercentEqual,
1236 OO_PlusEqual, OO_MinusEqual,
1237 OO_LessLessEqual, OO_GreaterGreaterEqual,
1238 OO_AmpEqual, OO_CaretEqual,
1239 OO_PipeEqual,
1240 OO_Comma
1241 };
1242 return OverOps[Opc];
1243}
1244
Ted Kremenekac034612010-04-13 23:39:13 +00001245InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +00001246 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001247 SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001248 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
1249 false),
Ted Kremenekac034612010-04-13 23:39:13 +00001250 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +00001251 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidisb2ed28e2011-04-21 00:27:41 +00001252 HadArrayRangeDesignator(false)
Alexis Hunta8136cc2010-05-05 15:23:54 +00001253{
Ted Kremenek013041e2010-02-19 01:50:18 +00001254 for (unsigned I = 0; I != numInits; ++I) {
1255 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001256 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001257 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001258 ExprBits.ValueDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001259 if (initExprs[I]->containsUnexpandedParameterPack())
1260 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001261 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001262
Ted Kremenekac034612010-04-13 23:39:13 +00001263 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001264}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001265
Ted Kremenekac034612010-04-13 23:39:13 +00001266void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001267 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001268 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001269}
1270
Ted Kremenekac034612010-04-13 23:39:13 +00001271void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001272 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001273}
1274
Ted Kremenekac034612010-04-13 23:39:13 +00001275Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001276 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001277 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001278 InitExprs.back() = expr;
1279 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001280 }
Mike Stump11289f42009-09-09 15:08:12 +00001281
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001282 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1283 InitExprs[Init] = expr;
1284 return Result;
1285}
1286
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001287void InitListExpr::setArrayFiller(Expr *filler) {
1288 ArrayFillerOrUnionFieldInit = filler;
1289 // Fill out any "holes" in the array due to designated initializers.
1290 Expr **inits = getInits();
1291 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1292 if (inits[i] == 0)
1293 inits[i] = filler;
1294}
1295
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001296SourceRange InitListExpr::getSourceRange() const {
1297 if (SyntacticForm)
1298 return SyntacticForm->getSourceRange();
1299 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1300 if (Beg.isInvalid()) {
1301 // Find the first non-null initializer.
1302 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1303 E = InitExprs.end();
1304 I != E; ++I) {
1305 if (Stmt *S = *I) {
1306 Beg = S->getLocStart();
1307 break;
1308 }
1309 }
1310 }
1311 if (End.isInvalid()) {
1312 // Find the first non-null initializer from the end.
1313 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1314 E = InitExprs.rend();
1315 I != E; ++I) {
1316 if (Stmt *S = *I) {
1317 End = S->getSourceRange().getEnd();
1318 break;
1319 }
1320 }
1321 }
1322 return SourceRange(Beg, End);
1323}
1324
Steve Naroff991e99d2008-09-04 15:31:07 +00001325/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001326///
1327const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001328 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +00001329 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001330}
1331
Mike Stump11289f42009-09-09 15:08:12 +00001332SourceLocation BlockExpr::getCaretLocation() const {
1333 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001334}
Mike Stump11289f42009-09-09 15:08:12 +00001335const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001336 return TheBlock->getBody();
1337}
Mike Stump11289f42009-09-09 15:08:12 +00001338Stmt *BlockExpr::getBody() {
1339 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001340}
Steve Naroff415d3d52008-10-08 17:01:13 +00001341
1342
Chris Lattner1ec5f562007-06-27 05:38:08 +00001343//===----------------------------------------------------------------------===//
1344// Generic Expression Routines
1345//===----------------------------------------------------------------------===//
1346
Chris Lattner237f2752009-02-14 07:37:35 +00001347/// isUnusedResultAWarning - Return true if this immediate expression should
1348/// be warned about if the result is unused. If so, fill in Loc and Ranges
1349/// with location to warn on and the source range[s] to report with the
1350/// warning.
1351bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +00001352 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001353 // Don't warn if the expr is type dependent. The type could end up
1354 // instantiating to void.
1355 if (isTypeDependent())
1356 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001357
Chris Lattner1ec5f562007-06-27 05:38:08 +00001358 switch (getStmtClass()) {
1359 default:
John McCallc493a732010-03-12 07:11:26 +00001360 if (getType()->isVoidType())
1361 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001362 Loc = getExprLoc();
1363 R1 = getSourceRange();
1364 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001365 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001366 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +00001367 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001368 case GenericSelectionExprClass:
1369 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1370 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001371 case UnaryOperatorClass: {
1372 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001373
Chris Lattner1ec5f562007-06-27 05:38:08 +00001374 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001375 default: break;
John McCalle3027922010-08-25 11:45:40 +00001376 case UO_PostInc:
1377 case UO_PostDec:
1378 case UO_PreInc:
1379 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001380 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001381 case UO_Deref:
Chris Lattnera44d1162007-06-27 05:58:59 +00001382 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001383 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001384 return false;
1385 break;
John McCalle3027922010-08-25 11:45:40 +00001386 case UO_Real:
1387 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001388 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001389 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1390 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001391 return false;
1392 break;
John McCalle3027922010-08-25 11:45:40 +00001393 case UO_Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001394 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001395 }
Chris Lattner237f2752009-02-14 07:37:35 +00001396 Loc = UO->getOperatorLoc();
1397 R1 = UO->getSubExpr()->getSourceRange();
1398 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001399 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001400 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001401 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001402 switch (BO->getOpcode()) {
1403 default:
1404 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001405 // Consider the RHS of comma for side effects. LHS was checked by
1406 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001407 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001408 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1409 // lvalue-ness) of an assignment written in a macro.
1410 if (IntegerLiteral *IE =
1411 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1412 if (IE->getValue() == 0)
1413 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001414 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1415 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001416 case BO_LAnd:
1417 case BO_LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001418 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1419 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1420 return false;
1421 break;
John McCall1e3715a2010-02-16 04:10:53 +00001422 }
Chris Lattner237f2752009-02-14 07:37:35 +00001423 if (BO->isAssignmentOp())
1424 return false;
1425 Loc = BO->getOperatorLoc();
1426 R1 = BO->getLHS()->getSourceRange();
1427 R2 = BO->getRHS()->getSourceRange();
1428 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001429 }
Chris Lattner86928112007-08-25 02:00:02 +00001430 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001431 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001432 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001433
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001434 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00001435 // If only one of the LHS or RHS is a warning, the operator might
1436 // be being used for control flow. Only warn if both the LHS and
1437 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001438 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremeneke96dad92011-03-01 20:34:48 +00001439 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1440 return false;
1441 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00001442 return true;
Ted Kremeneke96dad92011-03-01 20:34:48 +00001443 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001444 }
1445
Chris Lattnera44d1162007-06-27 05:58:59 +00001446 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001447 // If the base pointer or element is to a volatile pointer/field, accessing
1448 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001449 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001450 return false;
1451 Loc = cast<MemberExpr>(this)->getMemberLoc();
1452 R1 = SourceRange(Loc, Loc);
1453 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1454 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001455
Chris Lattner1ec5f562007-06-27 05:38:08 +00001456 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001457 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001458 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001459 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001460 return false;
1461 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1462 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1463 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1464 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001465
Chris Lattner1ec5f562007-06-27 05:38:08 +00001466 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001467 case CXXOperatorCallExprClass:
1468 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001469 // If this is a direct call, get the callee.
1470 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001471 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001472 // If the callee has attribute pure, const, or warn_unused_result, warn
1473 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001474 //
1475 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1476 // updated to match for QoI.
1477 if (FD->getAttr<WarnUnusedResultAttr>() ||
1478 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1479 Loc = CE->getCallee()->getLocStart();
1480 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001481
Chris Lattner1a6babf2009-10-13 04:53:48 +00001482 if (unsigned NumArgs = CE->getNumArgs())
1483 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1484 CE->getArg(NumArgs-1)->getLocEnd());
1485 return true;
1486 }
Chris Lattner237f2752009-02-14 07:37:35 +00001487 }
1488 return false;
1489 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001490
1491 case CXXTemporaryObjectExprClass:
1492 case CXXConstructExprClass:
1493 return false;
1494
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001495 case ObjCMessageExprClass: {
1496 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCall31168b02011-06-15 23:02:42 +00001497 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1498 ME->isInstanceMessage() &&
1499 !ME->getType()->isVoidType() &&
1500 ME->getSelector().getIdentifierInfoForSlot(0) &&
1501 ME->getSelector().getIdentifierInfoForSlot(0)
1502 ->getName().startswith("init")) {
1503 Loc = getExprLoc();
1504 R1 = ME->getSourceRange();
1505 return true;
1506 }
1507
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001508 const ObjCMethodDecl *MD = ME->getMethodDecl();
1509 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1510 Loc = getExprLoc();
1511 return true;
1512 }
Chris Lattner237f2752009-02-14 07:37:35 +00001513 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001514 }
Mike Stump11289f42009-09-09 15:08:12 +00001515
John McCallb7bd14f2010-12-02 01:19:52 +00001516 case ObjCPropertyRefExprClass:
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001517 Loc = getExprLoc();
1518 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001519 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00001520
Chris Lattner944d3062008-07-26 19:51:01 +00001521 case StmtExprClass: {
1522 // Statement exprs don't logically have side effects themselves, but are
1523 // sometimes used in macros in ways that give them a type that is unused.
1524 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1525 // however, if the result of the stmt expr is dead, we don't want to emit a
1526 // warning.
1527 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001528 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00001529 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001530 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001531 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1532 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1533 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1534 }
Mike Stump11289f42009-09-09 15:08:12 +00001535
John McCallc493a732010-03-12 07:11:26 +00001536 if (getType()->isVoidType())
1537 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001538 Loc = cast<StmtExpr>(this)->getLParenLoc();
1539 R1 = getSourceRange();
1540 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001541 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001542 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001543 // If this is an explicit cast to void, allow it. People do this when they
1544 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001545 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001546 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001547 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1548 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1549 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001550 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001551 if (getType()->isVoidType())
1552 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001553 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001554
Anders Carlsson6aa50392009-11-17 17:11:23 +00001555 // If this is a cast to void or a constructor conversion, check the operand.
1556 // Otherwise, the result of the cast is unused.
John McCalle3027922010-08-25 11:45:40 +00001557 if (CE->getCastKind() == CK_ToVoid ||
1558 CE->getCastKind() == CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001559 return (cast<CastExpr>(this)->getSubExpr()
1560 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001561 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1562 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1563 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001564 }
Mike Stump11289f42009-09-09 15:08:12 +00001565
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001566 case ImplicitCastExprClass:
1567 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001568 return (cast<ImplicitCastExpr>(this)
1569 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001570
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001571 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001572 return (cast<CXXDefaultArgExpr>(this)
1573 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001574
1575 case CXXNewExprClass:
1576 // FIXME: In theory, there might be new expressions that don't have side
1577 // effects (e.g. a placement new with an uninitialized POD).
1578 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001579 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001580 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001581 return (cast<CXXBindTemporaryExpr>(this)
1582 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00001583 case ExprWithCleanupsClass:
1584 return (cast<ExprWithCleanups>(this)
Mike Stump53f9ded2009-11-03 23:25:48 +00001585 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001586 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001587}
1588
Fariborz Jahanian07735332009-02-22 18:40:18 +00001589/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001590/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001591bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00001592 const Expr *E = IgnoreParens();
1593 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001594 default:
1595 return false;
1596 case ObjCIvarRefExprClass:
1597 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001598 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001599 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001600 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001601 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001602 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001603 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001604 case DeclRefExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00001605 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001606 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1607 if (VD->hasGlobalStorage())
1608 return true;
1609 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001610 // dereferencing to a pointer is always a gc'able candidate,
1611 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001612 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001613 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001614 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001615 return false;
1616 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001617 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00001618 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001619 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001620 }
1621 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001622 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001623 }
1624}
Sebastian Redlce354af2010-09-10 20:55:33 +00001625
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001626bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1627 if (isTypeDependent())
1628 return false;
John McCall086a4642010-11-24 05:12:34 +00001629 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001630}
1631
John McCall0009fcc2011-04-26 20:42:42 +00001632QualType Expr::findBoundMemberType(const Expr *expr) {
1633 assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1634
1635 // Bound member expressions are always one of these possibilities:
1636 // x->m x.m x->*y x.*y
1637 // (possibly parenthesized)
1638
1639 expr = expr->IgnoreParens();
1640 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1641 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1642 return mem->getMemberDecl()->getType();
1643 }
1644
1645 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1646 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1647 ->getPointeeType();
1648 assert(type->isFunctionType());
1649 return type;
1650 }
1651
1652 assert(isa<UnresolvedMemberExpr>(expr));
1653 return QualType();
1654}
1655
Sebastian Redlce354af2010-09-10 20:55:33 +00001656static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1657 Expr::CanThrowResult CT2) {
1658 // CanThrowResult constants are ordered so that the maximum is the correct
1659 // merge result.
1660 return CT1 > CT2 ? CT1 : CT2;
1661}
1662
1663static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1664 Expr *E = const_cast<Expr*>(CE);
1665 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall8322c3a2011-02-13 04:07:26 +00001666 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redlce354af2010-09-10 20:55:33 +00001667 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1668 }
1669 return R;
1670}
1671
Richard Smith938f40b2011-06-11 17:19:42 +00001672static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1673 const Decl *D,
Sebastian Redlce354af2010-09-10 20:55:33 +00001674 bool NullThrows = true) {
1675 if (!D)
1676 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1677
1678 // See if we can get a function type from the decl somehow.
1679 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1680 if (!VD) // If we have no clue what we're calling, assume the worst.
1681 return Expr::CT_Can;
1682
Sebastian Redlb8a76c42010-09-10 22:34:40 +00001683 // As an extension, we assume that __attribute__((nothrow)) functions don't
1684 // throw.
1685 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1686 return Expr::CT_Cannot;
1687
Sebastian Redlce354af2010-09-10 20:55:33 +00001688 QualType T = VD->getType();
1689 const FunctionProtoType *FT;
1690 if ((FT = T->getAs<FunctionProtoType>())) {
1691 } else if (const PointerType *PT = T->getAs<PointerType>())
1692 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1693 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1694 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1695 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1696 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1697 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1698 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1699
1700 if (!FT)
1701 return Expr::CT_Can;
1702
Richard Smith938f40b2011-06-11 17:19:42 +00001703 if (FT->getExceptionSpecType() == EST_Delayed) {
1704 assert(isa<CXXConstructorDecl>(D) &&
1705 "only constructor exception specs can be unknown");
1706 Ctx.getDiagnostics().Report(E->getLocStart(),
1707 diag::err_exception_spec_unknown)
1708 << E->getSourceRange();
1709 return Expr::CT_Can;
1710 }
1711
Sebastian Redl31ad7542011-03-13 17:09:40 +00001712 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redlce354af2010-09-10 20:55:33 +00001713}
1714
1715static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1716 if (DC->isTypeDependent())
1717 return Expr::CT_Dependent;
1718
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001719 if (!DC->getTypeAsWritten()->isReferenceType())
1720 return Expr::CT_Cannot;
1721
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001722 if (DC->getSubExpr()->isTypeDependent())
1723 return Expr::CT_Dependent;
1724
Sebastian Redlce354af2010-09-10 20:55:33 +00001725 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1726}
1727
1728static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1729 const CXXTypeidExpr *DC) {
1730 if (DC->isTypeOperand())
1731 return Expr::CT_Cannot;
1732
1733 Expr *Op = DC->getExprOperand();
1734 if (Op->isTypeDependent())
1735 return Expr::CT_Dependent;
1736
1737 const RecordType *RT = Op->getType()->getAs<RecordType>();
1738 if (!RT)
1739 return Expr::CT_Cannot;
1740
1741 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1742 return Expr::CT_Cannot;
1743
1744 if (Op->Classify(C).isPRValue())
1745 return Expr::CT_Cannot;
1746
1747 return Expr::CT_Can;
1748}
1749
1750Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1751 // C++ [expr.unary.noexcept]p3:
1752 // [Can throw] if in a potentially-evaluated context the expression would
1753 // contain:
1754 switch (getStmtClass()) {
1755 case CXXThrowExprClass:
1756 // - a potentially evaluated throw-expression
1757 return CT_Can;
1758
1759 case CXXDynamicCastExprClass: {
1760 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1761 // where T is a reference type, that requires a run-time check
1762 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1763 if (CT == CT_Can)
1764 return CT;
1765 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1766 }
1767
1768 case CXXTypeidExprClass:
1769 // - a potentially evaluated typeid expression applied to a glvalue
1770 // expression whose type is a polymorphic class type
1771 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1772
1773 // - a potentially evaluated call to a function, member function, function
1774 // pointer, or member function pointer that does not have a non-throwing
1775 // exception-specification
1776 case CallExprClass:
1777 case CXXOperatorCallExprClass:
1778 case CXXMemberCallExprClass: {
Eli Friedman622e4fc2011-05-12 02:11:32 +00001779 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001780 CanThrowResult CT;
1781 if (isTypeDependent())
1782 CT = CT_Dependent;
Eli Friedman622e4fc2011-05-12 02:11:32 +00001783 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1784 CT = CT_Cannot;
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001785 else
Richard Smith938f40b2011-06-11 17:19:42 +00001786 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redlce354af2010-09-10 20:55:33 +00001787 if (CT == CT_Can)
1788 return CT;
1789 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1790 }
1791
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001792 case CXXConstructExprClass:
1793 case CXXTemporaryObjectExprClass: {
Richard Smith938f40b2011-06-11 17:19:42 +00001794 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redlce354af2010-09-10 20:55:33 +00001795 cast<CXXConstructExpr>(this)->getConstructor());
1796 if (CT == CT_Can)
1797 return CT;
1798 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1799 }
1800
1801 case CXXNewExprClass: {
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001802 CanThrowResult CT;
1803 if (isTypeDependent())
1804 CT = CT_Dependent;
1805 else
1806 CT = MergeCanThrow(
Richard Smith938f40b2011-06-11 17:19:42 +00001807 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
1808 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redlce354af2010-09-10 20:55:33 +00001809 /*NullThrows*/false));
1810 if (CT == CT_Can)
1811 return CT;
1812 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1813 }
1814
1815 case CXXDeleteExprClass: {
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001816 CanThrowResult CT;
1817 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
1818 if (DTy.isNull() || DTy->isDependentType()) {
1819 CT = CT_Dependent;
1820 } else {
Richard Smith938f40b2011-06-11 17:19:42 +00001821 CT = CanCalleeThrow(C, this,
1822 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001823 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1824 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith938f40b2011-06-11 17:19:42 +00001825 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redla8bac372010-09-10 23:27:10 +00001826 }
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001827 if (CT == CT_Can)
1828 return CT;
Sebastian Redla8bac372010-09-10 23:27:10 +00001829 }
1830 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1831 }
1832
1833 case CXXBindTemporaryExprClass: {
1834 // The bound temporary has to be destroyed again, which might throw.
Richard Smith938f40b2011-06-11 17:19:42 +00001835 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redla8bac372010-09-10 23:27:10 +00001836 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1837 if (CT == CT_Can)
1838 return CT;
Sebastian Redlce354af2010-09-10 20:55:33 +00001839 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1840 }
1841
1842 // ObjC message sends are like function calls, but never have exception
1843 // specs.
1844 case ObjCMessageExprClass:
1845 case ObjCPropertyRefExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001846 return CT_Can;
1847
1848 // Many other things have subexpressions, so we have to test those.
1849 // Some are simple:
1850 case ParenExprClass:
1851 case MemberExprClass:
1852 case CXXReinterpretCastExprClass:
1853 case CXXConstCastExprClass:
1854 case ConditionalOperatorClass:
1855 case CompoundLiteralExprClass:
1856 case ExtVectorElementExprClass:
1857 case InitListExprClass:
1858 case DesignatedInitExprClass:
1859 case ParenListExprClass:
1860 case VAArgExprClass:
1861 case CXXDefaultArgExprClass:
John McCall5d413782010-12-06 08:20:24 +00001862 case ExprWithCleanupsClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001863 case ObjCIvarRefExprClass:
1864 case ObjCIsaExprClass:
1865 case ShuffleVectorExprClass:
1866 return CanSubExprsThrow(C, this);
1867
1868 // Some might be dependent for other reasons.
1869 case UnaryOperatorClass:
1870 case ArraySubscriptExprClass:
1871 case ImplicitCastExprClass:
1872 case CStyleCastExprClass:
1873 case CXXStaticCastExprClass:
1874 case CXXFunctionalCastExprClass:
1875 case BinaryOperatorClass:
1876 case CompoundAssignOperatorClass: {
1877 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1878 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1879 }
1880
1881 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1882 case StmtExprClass:
1883 return CT_Can;
1884
1885 case ChooseExprClass:
1886 if (isTypeDependent() || isValueDependent())
1887 return CT_Dependent;
1888 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1889
Peter Collingbourne91147592011-04-15 00:35:48 +00001890 case GenericSelectionExprClass:
1891 if (cast<GenericSelectionExpr>(this)->isResultDependent())
1892 return CT_Dependent;
1893 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
1894
Sebastian Redlce354af2010-09-10 20:55:33 +00001895 // Some expressions are always dependent.
1896 case DependentScopeDeclRefExprClass:
1897 case CXXUnresolvedConstructExprClass:
1898 case CXXDependentScopeMemberExprClass:
1899 return CT_Dependent;
1900
1901 default:
1902 // All other expressions don't have subexpressions, or else they are
1903 // unevaluated.
1904 return CT_Cannot;
1905 }
1906}
1907
Ted Kremenekfff70962008-01-17 16:57:34 +00001908Expr* Expr::IgnoreParens() {
1909 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00001910 while (true) {
1911 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1912 E = P->getSubExpr();
1913 continue;
1914 }
1915 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1916 if (P->getOpcode() == UO_Extension) {
1917 E = P->getSubExpr();
1918 continue;
1919 }
1920 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001921 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1922 if (!P->isResultDependent()) {
1923 E = P->getResultExpr();
1924 continue;
1925 }
1926 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00001927 return E;
1928 }
Ted Kremenekfff70962008-01-17 16:57:34 +00001929}
1930
Chris Lattnerf2660962008-02-13 01:02:39 +00001931/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1932/// or CastExprs or ImplicitCastExprs, returning their operand.
1933Expr *Expr::IgnoreParenCasts() {
1934 Expr *E = this;
1935 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001936 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001937 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001938 continue;
1939 }
1940 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00001941 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001942 continue;
1943 }
1944 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1945 if (P->getOpcode() == UO_Extension) {
1946 E = P->getSubExpr();
1947 continue;
1948 }
1949 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001950 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1951 if (!P->isResultDependent()) {
1952 E = P->getResultExpr();
1953 continue;
1954 }
1955 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00001956 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00001957 }
1958}
1959
John McCall5a4ce8b2010-12-04 08:24:19 +00001960/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
1961/// casts. This is intended purely as a temporary workaround for code
1962/// that hasn't yet been rewritten to do the right thing about those
1963/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00001964Expr *Expr::IgnoreParenLValueCasts() {
1965 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00001966 while (true) {
John McCall34376a62010-12-04 03:47:34 +00001967 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
1968 E = P->getSubExpr();
1969 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00001970 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00001971 if (P->getCastKind() == CK_LValueToRValue) {
1972 E = P->getSubExpr();
1973 continue;
1974 }
John McCall5a4ce8b2010-12-04 08:24:19 +00001975 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1976 if (P->getOpcode() == UO_Extension) {
1977 E = P->getSubExpr();
1978 continue;
1979 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001980 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1981 if (!P->isResultDependent()) {
1982 E = P->getResultExpr();
1983 continue;
1984 }
John McCall34376a62010-12-04 03:47:34 +00001985 }
1986 break;
1987 }
1988 return E;
1989}
1990
John McCalleebc8322010-05-05 22:59:52 +00001991Expr *Expr::IgnoreParenImpCasts() {
1992 Expr *E = this;
1993 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00001994 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001995 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00001996 continue;
1997 }
1998 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00001999 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002000 continue;
2001 }
2002 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2003 if (P->getOpcode() == UO_Extension) {
2004 E = P->getSubExpr();
2005 continue;
2006 }
2007 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002008 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2009 if (!P->isResultDependent()) {
2010 E = P->getResultExpr();
2011 continue;
2012 }
2013 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002014 return E;
John McCalleebc8322010-05-05 22:59:52 +00002015 }
2016}
2017
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002018Expr *Expr::IgnoreConversionOperator() {
2019 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
2020 if (isa<CXXConversionDecl>(MCE->getMethodDecl()))
2021 return MCE->getImplicitObjectArgument();
2022 }
2023 return this;
2024}
2025
Chris Lattneref26c772009-03-13 17:28:01 +00002026/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2027/// value (including ptr->int casts of the same size). Strip off any
2028/// ParenExpr or CastExprs, returning their operand.
2029Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2030 Expr *E = this;
2031 while (true) {
2032 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2033 E = P->getSubExpr();
2034 continue;
2035 }
Mike Stump11289f42009-09-09 15:08:12 +00002036
Chris Lattneref26c772009-03-13 17:28:01 +00002037 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2038 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002039 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002040 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002041
Chris Lattneref26c772009-03-13 17:28:01 +00002042 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2043 E = SE;
2044 continue;
2045 }
Mike Stump11289f42009-09-09 15:08:12 +00002046
Abramo Bagnara932e3932010-10-15 07:51:18 +00002047 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002048 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002049 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002050 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002051 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2052 E = SE;
2053 continue;
2054 }
2055 }
Mike Stump11289f42009-09-09 15:08:12 +00002056
Abramo Bagnara932e3932010-10-15 07:51:18 +00002057 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2058 if (P->getOpcode() == UO_Extension) {
2059 E = P->getSubExpr();
2060 continue;
2061 }
2062 }
2063
Peter Collingbourne91147592011-04-15 00:35:48 +00002064 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2065 if (!P->isResultDependent()) {
2066 E = P->getResultExpr();
2067 continue;
2068 }
2069 }
2070
Chris Lattneref26c772009-03-13 17:28:01 +00002071 return E;
2072 }
2073}
2074
Douglas Gregord196a582009-12-14 19:27:10 +00002075bool Expr::isDefaultArgument() const {
2076 const Expr *E = this;
2077 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2078 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002079
Douglas Gregord196a582009-12-14 19:27:10 +00002080 return isa<CXXDefaultArgExpr>(E);
2081}
Chris Lattneref26c772009-03-13 17:28:01 +00002082
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002083/// \brief Skip over any no-op casts and any temporary-binding
2084/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002085static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002086 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002087 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002088 E = ICE->getSubExpr();
2089 else
2090 break;
2091 }
2092
2093 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2094 E = BE->getSubExpr();
2095
2096 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002097 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002098 E = ICE->getSubExpr();
2099 else
2100 break;
2101 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002102
2103 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002104}
2105
John McCall7a626f62010-09-15 10:14:12 +00002106/// isTemporaryObject - Determines if this expression produces a
2107/// temporary of the given class type.
2108bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2109 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2110 return false;
2111
Anders Carlsson66bbf502010-11-28 16:40:49 +00002112 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002113
John McCall02dc8c72010-09-15 20:59:13 +00002114 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002115 if (!E->Classify(C).isPRValue()) {
2116 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002117 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002118 return false;
2119 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002120
John McCallf4ee1dd2010-09-16 06:57:56 +00002121 // Black-list a few cases which yield pr-values of class type that don't
2122 // refer to temporaries of that type:
2123
2124 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002125 if (isa<ImplicitCastExpr>(E)) {
2126 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2127 case CK_DerivedToBase:
2128 case CK_UncheckedDerivedToBase:
2129 return false;
2130 default:
2131 break;
2132 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002133 }
2134
John McCallf4ee1dd2010-09-16 06:57:56 +00002135 // - member expressions (all)
2136 if (isa<MemberExpr>(E))
2137 return false;
2138
John McCallc07a0c72011-02-17 10:25:35 +00002139 // - opaque values (all)
2140 if (isa<OpaqueValueExpr>(E))
2141 return false;
2142
John McCall7a626f62010-09-15 10:14:12 +00002143 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002144}
2145
Douglas Gregor25b7e052011-03-02 21:06:53 +00002146bool Expr::isImplicitCXXThis() const {
2147 const Expr *E = this;
2148
2149 // Strip away parentheses and casts we don't care about.
2150 while (true) {
2151 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2152 E = Paren->getSubExpr();
2153 continue;
2154 }
2155
2156 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2157 if (ICE->getCastKind() == CK_NoOp ||
2158 ICE->getCastKind() == CK_LValueToRValue ||
2159 ICE->getCastKind() == CK_DerivedToBase ||
2160 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2161 E = ICE->getSubExpr();
2162 continue;
2163 }
2164 }
2165
2166 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2167 if (UnOp->getOpcode() == UO_Extension) {
2168 E = UnOp->getSubExpr();
2169 continue;
2170 }
2171 }
2172
2173 break;
2174 }
2175
2176 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2177 return This->isImplicit();
2178
2179 return false;
2180}
2181
Douglas Gregor4619e432008-12-05 23:32:09 +00002182/// hasAnyTypeDependentArguments - Determines if any of the expressions
2183/// in Exprs is type-dependent.
2184bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2185 for (unsigned I = 0; I < NumExprs; ++I)
2186 if (Exprs[I]->isTypeDependent())
2187 return true;
2188
2189 return false;
2190}
2191
2192/// hasAnyValueDependentArguments - Determines if any of the expressions
2193/// in Exprs is value-dependent.
2194bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2195 for (unsigned I = 0; I < NumExprs; ++I)
2196 if (Exprs[I]->isValueDependent())
2197 return true;
2198
2199 return false;
2200}
2201
John McCall8b0f4ff2010-08-02 21:13:48 +00002202bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002203 // This function is attempting whether an expression is an initializer
2204 // which can be evaluated at compile-time. isEvaluatable handles most
2205 // of the cases, but it can't deal with some initializer-specific
2206 // expressions, and it can't deal with aggregates; we deal with those here,
2207 // and fall back to isEvaluatable for the other cases.
2208
John McCall8b0f4ff2010-08-02 21:13:48 +00002209 // If we ever capture reference-binding directly in the AST, we can
2210 // kill the second parameter.
2211
2212 if (IsForRef) {
2213 EvalResult Result;
2214 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2215 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002216
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002217 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002218 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002219 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002220 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002221 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002222 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002223 case CXXTemporaryObjectExprClass:
2224 case CXXConstructExprClass: {
2225 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002226
2227 // Only if it's
2228 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00002229 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002230 if (!CE->getNumArgs()) return true;
2231
2232 // 2) an elidable trivial copy construction of an operand which is
2233 // itself a constant initializer. Note that we consider the
2234 // operand on its own, *not* as a reference binding.
2235 return CE->isElidable() &&
2236 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00002237 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002238 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002239 // This handles gcc's extension that allows global initializers like
2240 // "struct x {int x;} x = (struct x) {};".
2241 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002242 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002243 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002244 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002245 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002246 // FIXME: This doesn't deal with fields with reference types correctly.
2247 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2248 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002249 const InitListExpr *Exp = cast<InitListExpr>(this);
2250 unsigned numInits = Exp->getNumInits();
2251 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002252 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002253 return false;
2254 }
Eli Friedman384da272009-01-25 03:12:18 +00002255 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002256 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002257 case ImplicitValueInitExprClass:
2258 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002259 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002260 return cast<ParenExpr>(this)->getSubExpr()
2261 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002262 case GenericSelectionExprClass:
2263 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2264 return false;
2265 return cast<GenericSelectionExpr>(this)->getResultExpr()
2266 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002267 case ChooseExprClass:
2268 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2269 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002270 case UnaryOperatorClass: {
2271 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002272 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002273 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002274 break;
2275 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00002276 case BinaryOperatorClass: {
2277 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2278 // but this handles the common case.
2279 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002280 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3eb172a2009-10-13 07:14:16 +00002281 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2282 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2283 return true;
2284 break;
2285 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002286 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002287 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002288 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00002289 case CStyleCastExprClass:
2290 // Handle casts with a destination that's a struct or union; this
2291 // deals with both the gcc no-op struct cast extension and the
2292 // cast-to-union extension.
2293 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002294 return cast<CastExpr>(this)->getSubExpr()
2295 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002296
Chris Lattnera2f9bd52009-10-13 22:12:09 +00002297 // Integer->integer casts can be handled here, which is important for
2298 // things like (int)(&&x-&&y). Scary but true.
2299 if (getType()->isIntegerType() &&
2300 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002301 return cast<CastExpr>(this)->getSubExpr()
2302 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002303
Eli Friedman384da272009-01-25 03:12:18 +00002304 break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002305 }
Eli Friedman384da272009-01-25 03:12:18 +00002306 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002307}
2308
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002309/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2310/// pointer constant or not, as well as the specific kind of constant detected.
2311/// Null pointer constants can be integer constant expressions with the
2312/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2313/// (a GNU extension).
2314Expr::NullPointerConstantKind
2315Expr::isNullPointerConstant(ASTContext &Ctx,
2316 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00002317 if (isValueDependent()) {
2318 switch (NPC) {
2319 case NPC_NeverValueDependent:
2320 assert(false && "Unexpected value dependent expression!");
2321 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002322
Douglas Gregor56751b52009-09-25 04:25:58 +00002323 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002324 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2325 return NPCK_ZeroInteger;
2326 else
2327 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00002328
Douglas Gregor56751b52009-09-25 04:25:58 +00002329 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002330 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00002331 }
2332 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002333
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002334 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002335 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00002336 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002337 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002338 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002339 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002340 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002341 Pointee->isVoidType() && // to void*
2342 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002343 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002344 }
Steve Naroffada7d422007-05-20 17:54:12 +00002345 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002346 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2347 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002348 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002349 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2350 // Accept ((void*)0) as a null pointer constant, as many other
2351 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002352 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00002353 } else if (const GenericSelectionExpr *GE =
2354 dyn_cast<GenericSelectionExpr>(this)) {
2355 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002356 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002357 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002358 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002359 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002360 } else if (isa<GNUNullExpr>(this)) {
2361 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002362 return NPCK_GNUNull;
Steve Naroff09035312008-01-14 02:53:34 +00002363 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002364
Sebastian Redl576fd422009-05-10 18:38:11 +00002365 // C++0x nullptr_t is always a null pointer constant.
2366 if (getType()->isNullPtrType())
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002367 return NPCK_CXX0X_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00002368
Fariborz Jahanian3567c422010-09-27 22:42:37 +00002369 if (const RecordType *UT = getType()->getAsUnionType())
2370 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2371 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2372 const Expr *InitExpr = CLE->getInitializer();
2373 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2374 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2375 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002376 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002377 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002378 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002379 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00002380
Chris Lattner1abbd412007-06-08 17:58:43 +00002381 // If we have an integer constant expression, we need to *evaluate* it and
2382 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002383 llvm::APSInt Result;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002384 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2385
2386 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002387}
Steve Narofff7a5da12007-07-28 23:10:27 +00002388
John McCall34376a62010-12-04 03:47:34 +00002389/// \brief If this expression is an l-value for an Objective C
2390/// property, find the underlying property reference expression.
2391const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2392 const Expr *E = this;
2393 while (true) {
2394 assert((E->getValueKind() == VK_LValue &&
2395 E->getObjectKind() == OK_ObjCProperty) &&
2396 "expression is not a property reference");
2397 E = E->IgnoreParenCasts();
2398 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2399 if (BO->getOpcode() == BO_Comma) {
2400 E = BO->getRHS();
2401 continue;
2402 }
2403 }
2404
2405 break;
2406 }
2407
2408 return cast<ObjCPropertyRefExpr>(E);
2409}
2410
Douglas Gregor71235ec2009-05-02 02:18:30 +00002411FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002412 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002413
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002414 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002415 if (ICE->getCastKind() == CK_LValueToRValue ||
2416 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002417 E = ICE->getSubExpr()->IgnoreParens();
2418 else
2419 break;
2420 }
2421
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002422 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002423 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002424 if (Field->isBitField())
2425 return Field;
2426
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00002427 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2428 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2429 if (Field->isBitField())
2430 return Field;
2431
Douglas Gregor71235ec2009-05-02 02:18:30 +00002432 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2433 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2434 return BinOp->getLHS()->getBitField();
2435
2436 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002437}
2438
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002439bool Expr::refersToVectorElement() const {
2440 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002441
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002442 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002443 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002444 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002445 E = ICE->getSubExpr()->IgnoreParens();
2446 else
2447 break;
2448 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002449
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002450 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2451 return ASE->getBase()->getType()->isVectorType();
2452
2453 if (isa<ExtVectorElementExpr>(E))
2454 return true;
2455
2456 return false;
2457}
2458
Chris Lattnerb8211f62009-02-16 22:14:05 +00002459/// isArrow - Return true if the base expression is a pointer to vector,
2460/// return false if the base expression is a vector.
2461bool ExtVectorElementExpr::isArrow() const {
2462 return getBase()->getType()->isPointerType();
2463}
2464
Nate Begemance4d7fc2008-04-18 23:10:10 +00002465unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002466 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002467 return VT->getNumElements();
2468 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002469}
2470
Nate Begemanf322eab2008-05-09 06:41:27 +00002471/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002472bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002473 // FIXME: Refactor this code to an accessor on the AST node which returns the
2474 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002475 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002476
2477 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002478 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002479 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002480
Nate Begeman7e5185b2009-01-18 02:01:21 +00002481 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002482 if (Comp[0] == 's' || Comp[0] == 'S')
2483 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002484
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002485 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2486 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002487 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002488
Steve Naroff0d595ca2007-07-30 03:29:09 +00002489 return false;
2490}
Chris Lattner885b4952007-08-02 23:36:59 +00002491
Nate Begemanf322eab2008-05-09 06:41:27 +00002492/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002493void ExtVectorElementExpr::getEncodedElementAccess(
2494 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002495 llvm::StringRef Comp = Accessor->getName();
2496 if (Comp[0] == 's' || Comp[0] == 'S')
2497 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002498
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002499 bool isHi = Comp == "hi";
2500 bool isLo = Comp == "lo";
2501 bool isEven = Comp == "even";
2502 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002503
Nate Begemanf322eab2008-05-09 06:41:27 +00002504 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2505 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002506
Nate Begemanf322eab2008-05-09 06:41:27 +00002507 if (isHi)
2508 Index = e + i;
2509 else if (isLo)
2510 Index = i;
2511 else if (isEven)
2512 Index = 2 * i;
2513 else if (isOdd)
2514 Index = 2 * i + 1;
2515 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002516 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002517
Nate Begemand3862152008-05-13 21:03:02 +00002518 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002519 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002520}
2521
Douglas Gregor9a129192010-04-21 00:45:42 +00002522ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002523 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002524 SourceLocation LBracLoc,
2525 SourceLocation SuperLoc,
2526 bool IsInstanceSuper,
2527 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002528 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002529 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002530 ObjCMethodDecl *Method,
2531 Expr **Args, unsigned NumArgs,
2532 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002533 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002534 /*TypeDependent=*/false, /*ValueDependent=*/false,
2535 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002536 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
John McCall31168b02011-06-15 23:02:42 +00002537 HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
Douglas Gregor9a129192010-04-21 00:45:42 +00002538 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2539 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002540 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002541{
Douglas Gregor9a129192010-04-21 00:45:42 +00002542 setReceiverPointer(SuperType.getAsOpaquePtr());
2543 if (NumArgs)
2544 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002545}
2546
Douglas Gregor9a129192010-04-21 00:45:42 +00002547ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002548 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002549 SourceLocation LBracLoc,
2550 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002551 Selector Sel,
2552 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002553 ObjCMethodDecl *Method,
2554 Expr **Args, unsigned NumArgs,
2555 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002556 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002557 T->isDependentType(), T->containsUnexpandedParameterPack()),
John McCall31168b02011-06-15 23:02:42 +00002558 NumArgs(NumArgs), Kind(Class),
2559 HasMethod(Method != 0), IsDelegateInitCall(false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002560 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2561 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002562 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002563{
2564 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002565 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002566 for (unsigned I = 0; I != NumArgs; ++I) {
2567 if (Args[I]->isTypeDependent())
2568 ExprBits.TypeDependent = true;
2569 if (Args[I]->isValueDependent())
2570 ExprBits.ValueDependent = true;
2571 if (Args[I]->containsUnexpandedParameterPack())
2572 ExprBits.ContainsUnexpandedParameterPack = true;
2573
2574 MyArgs[I] = Args[I];
2575 }
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002576}
2577
Douglas Gregor9a129192010-04-21 00:45:42 +00002578ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002579 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002580 SourceLocation LBracLoc,
2581 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002582 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002583 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002584 ObjCMethodDecl *Method,
2585 Expr **Args, unsigned NumArgs,
2586 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002587 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002588 Receiver->isTypeDependent(),
2589 Receiver->containsUnexpandedParameterPack()),
John McCall31168b02011-06-15 23:02:42 +00002590 NumArgs(NumArgs), Kind(Instance),
2591 HasMethod(Method != 0), IsDelegateInitCall(false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002592 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2593 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002594 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002595{
2596 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002597 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002598 for (unsigned I = 0; I != NumArgs; ++I) {
2599 if (Args[I]->isTypeDependent())
2600 ExprBits.TypeDependent = true;
2601 if (Args[I]->isValueDependent())
2602 ExprBits.ValueDependent = true;
2603 if (Args[I]->containsUnexpandedParameterPack())
2604 ExprBits.ContainsUnexpandedParameterPack = true;
2605
2606 MyArgs[I] = Args[I];
2607 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00002608}
2609
Douglas Gregor9a129192010-04-21 00:45:42 +00002610ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002611 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002612 SourceLocation LBracLoc,
2613 SourceLocation SuperLoc,
2614 bool IsInstanceSuper,
2615 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002616 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002617 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002618 ObjCMethodDecl *Method,
2619 Expr **Args, unsigned NumArgs,
2620 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002621 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002622 NumArgs * sizeof(Expr *);
2623 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCall7decc9e2010-11-18 06:31:45 +00002624 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002625 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002626 RBracLoc);
2627}
2628
2629ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002630 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002631 SourceLocation LBracLoc,
2632 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002633 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002634 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002635 ObjCMethodDecl *Method,
2636 Expr **Args, unsigned NumArgs,
2637 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002638 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002639 NumArgs * sizeof(Expr *);
2640 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002641 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2642 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002643}
2644
2645ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002646 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002647 SourceLocation LBracLoc,
2648 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002649 Selector Sel,
2650 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002651 ObjCMethodDecl *Method,
2652 Expr **Args, unsigned NumArgs,
2653 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002654 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002655 NumArgs * sizeof(Expr *);
2656 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002657 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2658 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002659}
2660
Alexis Hunta8136cc2010-05-05 15:23:54 +00002661ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002662 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002663 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002664 NumArgs * sizeof(Expr *);
2665 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2666 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2667}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00002668
2669SourceRange ObjCMessageExpr::getReceiverRange() const {
2670 switch (getReceiverKind()) {
2671 case Instance:
2672 return getInstanceReceiver()->getSourceRange();
2673
2674 case Class:
2675 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2676
2677 case SuperInstance:
2678 case SuperClass:
2679 return getSuperLoc();
2680 }
2681
2682 return SourceLocation();
2683}
2684
Douglas Gregor9a129192010-04-21 00:45:42 +00002685Selector ObjCMessageExpr::getSelector() const {
2686 if (HasMethod)
2687 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2688 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002689 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002690}
2691
2692ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2693 switch (getReceiverKind()) {
2694 case Instance:
2695 if (const ObjCObjectPointerType *Ptr
2696 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2697 return Ptr->getInterfaceDecl();
2698 break;
2699
2700 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00002701 if (const ObjCObjectType *Ty
2702 = getClassReceiver()->getAs<ObjCObjectType>())
2703 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002704 break;
2705
2706 case SuperInstance:
2707 if (const ObjCObjectPointerType *Ptr
2708 = getSuperType()->getAs<ObjCObjectPointerType>())
2709 return Ptr->getInterfaceDecl();
2710 break;
2711
2712 case SuperClass:
Argyrios Kyrtzidis1b9747f2011-01-25 00:03:48 +00002713 if (const ObjCObjectType *Iface
2714 = getSuperType()->getAs<ObjCObjectType>())
2715 return Iface->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002716 break;
2717 }
2718
2719 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002720}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002721
John McCall31168b02011-06-15 23:02:42 +00002722llvm::StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
2723 switch (getBridgeKind()) {
2724 case OBC_Bridge:
2725 return "__bridge";
2726 case OBC_BridgeTransfer:
2727 return "__bridge_transfer";
2728 case OBC_BridgeRetained:
2729 return "__bridge_retained";
2730 }
2731
2732 return "__bridge";
2733}
2734
Jay Foad39c79802011-01-12 09:06:06 +00002735bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002736 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002737}
2738
Douglas Gregora6e053e2010-12-15 01:34:56 +00002739ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2740 QualType Type, SourceLocation BLoc,
2741 SourceLocation RP)
2742 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2743 Type->isDependentType(), Type->isDependentType(),
2744 Type->containsUnexpandedParameterPack()),
2745 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2746{
2747 SubExprs = new (C) Stmt*[nexpr];
2748 for (unsigned i = 0; i < nexpr; i++) {
2749 if (args[i]->isTypeDependent())
2750 ExprBits.TypeDependent = true;
2751 if (args[i]->isValueDependent())
2752 ExprBits.ValueDependent = true;
2753 if (args[i]->containsUnexpandedParameterPack())
2754 ExprBits.ContainsUnexpandedParameterPack = true;
2755
2756 SubExprs[i] = args[i];
2757 }
2758}
2759
Nate Begeman48745922009-08-12 02:28:50 +00002760void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2761 unsigned NumExprs) {
2762 if (SubExprs) C.Deallocate(SubExprs);
2763
2764 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002765 this->NumExprs = NumExprs;
2766 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002767}
Nate Begeman48745922009-08-12 02:28:50 +00002768
Peter Collingbourne91147592011-04-15 00:35:48 +00002769GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2770 SourceLocation GenericLoc, Expr *ControllingExpr,
2771 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2772 unsigned NumAssocs, SourceLocation DefaultLoc,
2773 SourceLocation RParenLoc,
2774 bool ContainsUnexpandedParameterPack,
2775 unsigned ResultIndex)
2776 : Expr(GenericSelectionExprClass,
2777 AssocExprs[ResultIndex]->getType(),
2778 AssocExprs[ResultIndex]->getValueKind(),
2779 AssocExprs[ResultIndex]->getObjectKind(),
2780 AssocExprs[ResultIndex]->isTypeDependent(),
2781 AssocExprs[ResultIndex]->isValueDependent(),
2782 ContainsUnexpandedParameterPack),
2783 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2784 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2785 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2786 RParenLoc(RParenLoc) {
2787 SubExprs[CONTROLLING] = ControllingExpr;
2788 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2789 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2790}
2791
2792GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2793 SourceLocation GenericLoc, Expr *ControllingExpr,
2794 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2795 unsigned NumAssocs, SourceLocation DefaultLoc,
2796 SourceLocation RParenLoc,
2797 bool ContainsUnexpandedParameterPack)
2798 : Expr(GenericSelectionExprClass,
2799 Context.DependentTy,
2800 VK_RValue,
2801 OK_Ordinary,
2802 /*isTypeDependent=*/ true,
2803 /*isValueDependent=*/ true,
2804 ContainsUnexpandedParameterPack),
2805 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2806 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2807 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2808 RParenLoc(RParenLoc) {
2809 SubExprs[CONTROLLING] = ControllingExpr;
2810 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2811 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2812}
2813
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002814//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002815// DesignatedInitExpr
2816//===----------------------------------------------------------------------===//
2817
Chandler Carruth631abd92011-06-16 06:47:06 +00002818IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002819 assert(Kind == FieldDesignator && "Only valid on a field designator");
2820 if (Field.NameOrField & 0x01)
2821 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2822 else
2823 return getField()->getIdentifier();
2824}
2825
Alexis Hunta8136cc2010-05-05 15:23:54 +00002826DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002827 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002828 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002829 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002830 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002831 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002832 unsigned NumIndexExprs,
2833 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002834 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00002835 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002836 Init->isTypeDependent(), Init->isValueDependent(),
2837 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00002838 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2839 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002840 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002841
2842 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00002843 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002844 *Child++ = Init;
2845
2846 // Copy the designators and their subexpressions, computing
2847 // value-dependence along the way.
2848 unsigned IndexIdx = 0;
2849 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002850 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002851
2852 if (this->Designators[I].isArrayDesignator()) {
2853 // Compute type- and value-dependence.
2854 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002855 if (Index->isTypeDependent() || Index->isValueDependent())
2856 ExprBits.ValueDependent = true;
2857
2858 // Propagate unexpanded parameter packs.
2859 if (Index->containsUnexpandedParameterPack())
2860 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002861
2862 // Copy the index expressions into permanent storage.
2863 *Child++ = IndexExprs[IndexIdx++];
2864 } else if (this->Designators[I].isArrayRangeDesignator()) {
2865 // Compute type- and value-dependence.
2866 Expr *Start = IndexExprs[IndexIdx];
2867 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002868 if (Start->isTypeDependent() || Start->isValueDependent() ||
2869 End->isTypeDependent() || End->isValueDependent())
2870 ExprBits.ValueDependent = true;
2871
2872 // Propagate unexpanded parameter packs.
2873 if (Start->containsUnexpandedParameterPack() ||
2874 End->containsUnexpandedParameterPack())
2875 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002876
2877 // Copy the start/end expressions into permanent storage.
2878 *Child++ = IndexExprs[IndexIdx++];
2879 *Child++ = IndexExprs[IndexIdx++];
2880 }
2881 }
2882
2883 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00002884}
2885
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002886DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00002887DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002888 unsigned NumDesignators,
2889 Expr **IndexExprs, unsigned NumIndexExprs,
2890 SourceLocation ColonOrEqualLoc,
2891 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002892 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00002893 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002894 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002895 ColonOrEqualLoc, UsesColonSyntax,
2896 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002897}
2898
Mike Stump11289f42009-09-09 15:08:12 +00002899DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00002900 unsigned NumIndexExprs) {
2901 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
2902 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
2903 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
2904}
2905
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002906void DesignatedInitExpr::setDesignators(ASTContext &C,
2907 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00002908 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002909 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00002910 NumDesignators = NumDesigs;
2911 for (unsigned I = 0; I != NumDesigs; ++I)
2912 Designators[I] = Desigs[I];
2913}
2914
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00002915SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
2916 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
2917 if (size() == 1)
2918 return DIE->getDesignator(0)->getSourceRange();
2919 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
2920 DIE->getDesignator(size()-1)->getEndLocation());
2921}
2922
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002923SourceRange DesignatedInitExpr::getSourceRange() const {
2924 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00002925 Designator &First =
2926 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002927 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00002928 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002929 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
2930 else
2931 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
2932 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00002933 StartLoc =
2934 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002935 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
2936}
2937
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002938Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
2939 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
2940 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2941 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002942 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2943 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2944}
2945
2946Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002947 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002948 "Requires array range designator");
2949 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2950 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002951 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2952 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
2953}
2954
2955Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00002956 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002957 "Requires array range designator");
2958 char* Ptr = static_cast<char*>(static_cast<void *>(this));
2959 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002960 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
2961 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
2962}
2963
Douglas Gregord5846a12009-04-15 06:41:24 +00002964/// \brief Replaces the designator at index @p Idx with the series
2965/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002966void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00002967 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00002968 const Designator *Last) {
2969 unsigned NumNewDesignators = Last - First;
2970 if (NumNewDesignators == 0) {
2971 std::copy_backward(Designators + Idx + 1,
2972 Designators + NumDesignators,
2973 Designators + Idx);
2974 --NumNewDesignators;
2975 return;
2976 } else if (NumNewDesignators == 1) {
2977 Designators[Idx] = *First;
2978 return;
2979 }
2980
Mike Stump11289f42009-09-09 15:08:12 +00002981 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002982 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00002983 std::copy(Designators, Designators + Idx, NewDesignators);
2984 std::copy(First, Last, NewDesignators + Idx);
2985 std::copy(Designators + Idx + 1, Designators + NumDesignators,
2986 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00002987 Designators = NewDesignators;
2988 NumDesignators = NumDesignators - 1 + NumNewDesignators;
2989}
2990
Mike Stump11289f42009-09-09 15:08:12 +00002991ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00002992 Expr **exprs, unsigned nexprs,
2993 SourceLocation rparenloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00002994 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
2995 false, false, false),
2996 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Mike Stump11289f42009-09-09 15:08:12 +00002997
Nate Begeman5ec4b312009-08-10 23:49:36 +00002998 Exprs = new (C) Stmt*[nexprs];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002999 for (unsigned i = 0; i != nexprs; ++i) {
3000 if (exprs[i]->isTypeDependent())
3001 ExprBits.TypeDependent = true;
3002 if (exprs[i]->isValueDependent())
3003 ExprBits.ValueDependent = true;
3004 if (exprs[i]->containsUnexpandedParameterPack())
3005 ExprBits.ContainsUnexpandedParameterPack = true;
3006
Nate Begeman5ec4b312009-08-10 23:49:36 +00003007 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003008 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003009}
3010
John McCall1bf58462011-02-16 08:02:54 +00003011const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3012 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3013 e = ewc->getSubExpr();
3014 e = cast<CXXConstructExpr>(e)->getArg(0);
3015 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3016 e = ice->getSubExpr();
3017 return cast<OpaqueValueExpr>(e);
3018}
3019
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003020//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003021// ExprIterator.
3022//===----------------------------------------------------------------------===//
3023
3024Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3025Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3026Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3027const Expr* ConstExprIterator::operator[](size_t idx) const {
3028 return cast<Expr>(I[idx]);
3029}
3030const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3031const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3032
3033//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003034// Child Iterators for iterating over subexpressions/substatements
3035//===----------------------------------------------------------------------===//
3036
Peter Collingbournee190dee2011-03-11 19:24:49 +00003037// UnaryExprOrTypeTraitExpr
3038Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003039 // If this is of a type and the type is a VLA type (and not a typedef), the
3040 // size expression of the VLA needs to be treated as an executable expression.
3041 // Why isn't this weirdness documented better in StmtIterator?
3042 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003043 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003044 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003045 return child_range(child_iterator(T), child_iterator());
3046 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003047 }
John McCallbd066782011-02-09 08:16:59 +00003048 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003049}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003050
Steve Naroffd54978b2007-09-18 23:55:05 +00003051// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00003052Stmt::child_range ObjCMessageExpr::children() {
3053 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003054 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003055 begin = reinterpret_cast<Stmt **>(this + 1);
3056 else
3057 begin = reinterpret_cast<Stmt **>(getArgs());
3058 return child_range(begin,
3059 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003060}
3061
Steve Naroffc540d662008-09-03 18:15:37 +00003062// Blocks
John McCall351762c2011-02-07 10:33:21 +00003063BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregor476e3022011-01-19 21:32:01 +00003064 SourceLocation l, bool ByRef,
John McCall351762c2011-02-07 10:33:21 +00003065 bool constAdded)
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003066 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false,
Douglas Gregor476e3022011-01-19 21:32:01 +00003067 d->isParameterPack()),
John McCall351762c2011-02-07 10:33:21 +00003068 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregor476e3022011-01-19 21:32:01 +00003069{
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003070 bool TypeDependent = false;
3071 bool ValueDependent = false;
3072 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent);
3073 ExprBits.TypeDependent = TypeDependent;
3074 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor476e3022011-01-19 21:32:01 +00003075}