blob: 3002e305ac59c839ec3b56fb8a9ba8291ab89330 [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(
Douglas Gregor678d76c2011-07-01 01:22:09 +0000145 const TemplateArgumentListInfo &Info,
146 bool &Dependent,
147 bool &InstantiationDependent,
148 bool &ContainsUnexpandedParameterPack) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000149 LAngleLoc = Info.getLAngleLoc();
150 RAngleLoc = Info.getRAngleLoc();
151 NumTemplateArgs = Info.size();
152
153 TemplateArgumentLoc *ArgBuffer = getTemplateArgs();
154 for (unsigned i = 0; i != NumTemplateArgs; ++i) {
155 Dependent = Dependent || Info[i].getArgument().isDependent();
Douglas Gregor678d76c2011-07-01 01:22:09 +0000156 InstantiationDependent = InstantiationDependent ||
157 Info[i].getArgument().isInstantiationDependent();
Douglas Gregora6e053e2010-12-15 01:34:56 +0000158 ContainsUnexpandedParameterPack
159 = ContainsUnexpandedParameterPack ||
160 Info[i].getArgument().containsUnexpandedParameterPack();
161
162 new (&ArgBuffer[i]) TemplateArgumentLoc(Info[i]);
163 }
164}
165
John McCall6b51f282009-11-23 01:53:49 +0000166void ExplicitTemplateArgumentList::copyInto(
167 TemplateArgumentListInfo &Info) const {
168 Info.setLAngleLoc(LAngleLoc);
169 Info.setRAngleLoc(RAngleLoc);
170 for (unsigned I = 0; I != NumTemplateArgs; ++I)
171 Info.addArgument(getTemplateArgs()[I]);
172}
173
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000174std::size_t ExplicitTemplateArgumentList::sizeFor(unsigned NumTemplateArgs) {
175 return sizeof(ExplicitTemplateArgumentList) +
176 sizeof(TemplateArgumentLoc) * NumTemplateArgs;
177}
178
John McCall6b51f282009-11-23 01:53:49 +0000179std::size_t ExplicitTemplateArgumentList::sizeFor(
180 const TemplateArgumentListInfo &Info) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +0000181 return sizeFor(Info.size());
John McCall6b51f282009-11-23 01:53:49 +0000182}
183
Douglas Gregor678d76c2011-07-01 01:22:09 +0000184/// \brief Compute the type-, value-, and instantiation-dependence of a
185/// declaration reference
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000186/// based on the declaration being referenced.
187static void computeDeclRefDependence(NamedDecl *D, QualType T,
188 bool &TypeDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000189 bool &ValueDependent,
190 bool &InstantiationDependent) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000191 TypeDependent = false;
192 ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000193 InstantiationDependent = false;
Douglas Gregored6c7442009-11-23 11:41:28 +0000194
195 // (TD) C++ [temp.dep.expr]p3:
196 // An id-expression is type-dependent if it contains:
197 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000198 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000199 //
200 // (VD) C++ [temp.dep.constexpr]p2:
201 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000202
Douglas Gregored6c7442009-11-23 11:41:28 +0000203 // (TD) - an identifier that was declared with dependent type
204 // (VD) - a name declared with a dependent type,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000205 if (T->isDependentType()) {
206 TypeDependent = true;
207 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000208 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000209 return;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000210 } else if (T->isInstantiationDependentType()) {
211 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000212 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000213
Douglas Gregored6c7442009-11-23 11:41:28 +0000214 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000215 if (D->getDeclName().getNameKind()
Douglas Gregor678d76c2011-07-01 01:22:09 +0000216 == DeclarationName::CXXConversionFunctionName) {
217 QualType T = D->getDeclName().getCXXNameType();
218 if (T->isDependentType()) {
219 TypeDependent = true;
220 ValueDependent = true;
221 InstantiationDependent = true;
222 return;
223 }
224
225 if (T->isInstantiationDependentType())
226 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000227 }
Douglas Gregor678d76c2011-07-01 01:22:09 +0000228
Douglas Gregored6c7442009-11-23 11:41:28 +0000229 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000230 if (isa<NonTypeTemplateParmDecl>(D)) {
231 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000232 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000233 return;
234 }
235
Douglas Gregored6c7442009-11-23 11:41:28 +0000236 // (VD) - a constant with integral or enumeration type and is
237 // initialized with an expression that is value-dependent.
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000238 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000239 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000240 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000241 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000242 if (Init->isValueDependent()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000243 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000244 InstantiationDependent = true;
245 }
Douglas Gregor0e4de762010-05-11 08:41:30 +0000246 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000247
Douglas Gregor0e4de762010-05-11 08:41:30 +0000248 // (VD) - FIXME: Missing from the standard:
249 // - a member function or a static data member of the current
250 // instantiation
251 else if (Var->isStaticDataMember() &&
Douglas Gregor678d76c2011-07-01 01:22:09 +0000252 Var->getDeclContext()->isDependentContext()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000253 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000254 InstantiationDependent = true;
255 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000256
257 return;
258 }
259
Douglas Gregor0e4de762010-05-11 08:41:30 +0000260 // (VD) - FIXME: Missing from the standard:
261 // - a member function or a static data member of the current
262 // instantiation
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000263 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
264 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000265 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000266 return;
267 }
268}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000269
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000270void DeclRefExpr::computeDependence() {
271 bool TypeDependent = false;
272 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000273 bool InstantiationDependent = false;
274 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
275 InstantiationDependent);
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000276
277 // (TD) C++ [temp.dep.expr]p3:
278 // An id-expression is type-dependent if it contains:
279 //
280 // and
281 //
282 // (VD) C++ [temp.dep.constexpr]p2:
283 // An identifier is value-dependent if it is:
284 if (!TypeDependent && !ValueDependent &&
285 hasExplicitTemplateArgs() &&
286 TemplateSpecializationType::anyDependentTemplateArguments(
287 getTemplateArgs(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000288 getNumTemplateArgs(),
289 InstantiationDependent)) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000290 TypeDependent = true;
291 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000292 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000293 }
294
295 ExprBits.TypeDependent = TypeDependent;
296 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000297 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000298
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000299 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000300 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000301 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000302}
303
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000304DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000305 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000306 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000307 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000308 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000309 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000310 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
311 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000312 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000313 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000314 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
315 if (FoundD)
316 getInternalFoundDecl() = FoundD;
Chandler Carruth0e439962011-05-01 21:29:53 +0000317 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000318 if (TemplateArgs) {
319 bool Dependent = false;
320 bool InstantiationDependent = false;
321 bool ContainsUnexpandedParameterPack = false;
322 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
323 InstantiationDependent,
324 ContainsUnexpandedParameterPack);
325 if (InstantiationDependent)
326 setInstantiationDependent(true);
327 }
328
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000329 computeDependence();
330}
331
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000332DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000333 NestedNameSpecifierLoc QualifierLoc,
John McCallce546572009-12-08 09:08:17 +0000334 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000335 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000336 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000337 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000338 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000339 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000340 return Create(Context, QualifierLoc, D,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000341 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000342 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000343}
344
345DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000346 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000347 ValueDecl *D,
348 const DeclarationNameInfo &NameInfo,
349 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000350 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000351 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000352 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000353 // Filter out cases where the found Decl is the same as the value refenenced.
354 if (D == FoundD)
355 FoundD = 0;
356
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000357 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregorea972d32011-02-28 21:54:11 +0000358 if (QualifierLoc != 0)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000359 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000360 if (FoundD)
361 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000362 if (TemplateArgs)
363 Size += ExplicitTemplateArgumentList::sizeFor(*TemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000364
Chris Lattner5c0b4052010-10-30 05:14:06 +0000365 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000366 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
367 T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000368}
369
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000370DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000371 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000372 bool HasFoundDecl,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000373 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000374 unsigned NumTemplateArgs) {
375 std::size_t Size = sizeof(DeclRefExpr);
376 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000377 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000378 if (HasFoundDecl)
379 Size += sizeof(NamedDecl *);
Douglas Gregor87866ce2011-02-04 12:01:24 +0000380 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000381 Size += ExplicitTemplateArgumentList::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000382
Chris Lattner5c0b4052010-10-30 05:14:06 +0000383 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000384 return new (Mem) DeclRefExpr(EmptyShell());
385}
386
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000387SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000388 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000389 if (hasQualifier())
Douglas Gregorea972d32011-02-28 21:54:11 +0000390 R.setBegin(getQualifierLoc().getBeginLoc());
John McCallb3774b52010-08-19 23:49:38 +0000391 if (hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000392 R.setEnd(getRAngleLoc());
393 return R;
394}
395
Anders Carlsson2fb08242009-09-08 18:24:21 +0000396// FIXME: Maybe this should use DeclPrinter with a special "print predefined
397// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000398std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
399 ASTContext &Context = CurrentDecl->getASTContext();
400
Anders Carlsson2fb08242009-09-08 18:24:21 +0000401 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000402 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000403 return FD->getNameAsString();
404
405 llvm::SmallString<256> Name;
406 llvm::raw_svector_ostream Out(Name);
407
408 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000409 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000410 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000411 if (MD->isStatic())
412 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000413 }
414
415 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000416
417 std::string Proto = FD->getQualifiedNameAsString(Policy);
418
John McCall9dd450b2009-09-21 23:43:11 +0000419 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000420 const FunctionProtoType *FT = 0;
421 if (FD->hasWrittenPrototype())
422 FT = dyn_cast<FunctionProtoType>(AFT);
423
424 Proto += "(";
425 if (FT) {
426 llvm::raw_string_ostream POut(Proto);
427 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
428 if (i) POut << ", ";
429 std::string Param;
430 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
431 POut << Param;
432 }
433
434 if (FT->isVariadic()) {
435 if (FD->getNumParams()) POut << ", ";
436 POut << "...";
437 }
438 }
439 Proto += ")";
440
Sam Weinig4e83bd22009-12-27 01:38:20 +0000441 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
442 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
443 if (ThisQuals.hasConst())
444 Proto += " const";
445 if (ThisQuals.hasVolatile())
446 Proto += " volatile";
447 }
448
Sam Weinigd060ed42009-12-06 23:55:13 +0000449 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
450 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000451
452 Out << Proto;
453
454 Out.flush();
455 return Name.str().str();
456 }
457 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
458 llvm::SmallString<256> Name;
459 llvm::raw_svector_ostream Out(Name);
460 Out << (MD->isInstanceMethod() ? '-' : '+');
461 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000462
463 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
464 // a null check to avoid a crash.
465 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000466 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000467
Anders Carlsson2fb08242009-09-08 18:24:21 +0000468 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000469 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
470 Out << '(' << CID << ')';
471
Anders Carlsson2fb08242009-09-08 18:24:21 +0000472 Out << ' ';
473 Out << MD->getSelector().getAsString();
474 Out << ']';
475
476 Out.flush();
477 return Name.str().str();
478 }
479 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
480 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
481 return "top level";
482 }
483 return "";
484}
485
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000486void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
487 if (hasAllocation())
488 C.Deallocate(pVal);
489
490 BitWidth = Val.getBitWidth();
491 unsigned NumWords = Val.getNumWords();
492 const uint64_t* Words = Val.getRawData();
493 if (NumWords > 1) {
494 pVal = new (C) uint64_t[NumWords];
495 std::copy(Words, Words + NumWords, pVal);
496 } else if (NumWords == 1)
497 VAL = Words[0];
498 else
499 VAL = 0;
500}
501
502IntegerLiteral *
503IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
504 QualType type, SourceLocation l) {
505 return new (C) IntegerLiteral(C, V, type, l);
506}
507
508IntegerLiteral *
509IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
510 return new (C) IntegerLiteral(Empty);
511}
512
513FloatingLiteral *
514FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
515 bool isexact, QualType Type, SourceLocation L) {
516 return new (C) FloatingLiteral(C, V, isexact, Type, L);
517}
518
519FloatingLiteral *
520FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
521 return new (C) FloatingLiteral(Empty);
522}
523
Chris Lattnera0173132008-06-07 22:13:43 +0000524/// getValueAsApproximateDouble - This returns the value as an inaccurate
525/// double. Note that this may cause loss of precision, but is useful for
526/// debugging dumps, etc.
527double FloatingLiteral::getValueAsApproximateDouble() const {
528 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000529 bool ignored;
530 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
531 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000532 return V.convertToDouble();
533}
534
Jay Foad9a6b0982011-06-21 15:13:30 +0000535StringLiteral *StringLiteral::Create(ASTContext &C, llvm::StringRef Str,
536 bool Wide,
Anders Carlsson75245402011-04-14 00:40:03 +0000537 bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000538 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000539 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000540 // Allocate enough space for the StringLiteral plus an array of locations for
541 // any concatenated string tokens.
542 void *Mem = C.Allocate(sizeof(StringLiteral)+
543 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000544 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000545 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000546
Steve Naroffdf7855b2007-02-21 23:46:25 +0000547 // OPTIMIZE: could allocate this appended to the StringLiteral.
Jay Foad9a6b0982011-06-21 15:13:30 +0000548 char *AStrData = new (C, 1) char[Str.size()];
549 memcpy(AStrData, Str.data(), Str.size());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000550 SL->StrData = AStrData;
Jay Foad9a6b0982011-06-21 15:13:30 +0000551 SL->ByteLength = Str.size();
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000552 SL->IsWide = Wide;
Anders Carlsson75245402011-04-14 00:40:03 +0000553 SL->IsPascal = Pascal;
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000554 SL->TokLocs[0] = Loc[0];
555 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000556
Chris Lattner630970d2009-02-18 05:49:11 +0000557 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000558 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
559 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000560}
561
Douglas Gregor958dfc92009-04-15 16:35:07 +0000562StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
563 void *Mem = C.Allocate(sizeof(StringLiteral)+
564 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000565 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000566 StringLiteral *SL = new (Mem) StringLiteral(QualType());
567 SL->StrData = 0;
568 SL->ByteLength = 0;
569 SL->NumConcatenated = NumStrs;
570 return SL;
571}
572
Daniel Dunbar36217882009-09-22 03:27:33 +0000573void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) {
Daniel Dunbar36217882009-09-22 03:27:33 +0000574 char *AStrData = new (C, 1) char[Str.size()];
575 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000576 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000577 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000578}
579
Chris Lattnere925d612010-11-17 07:37:15 +0000580/// getLocationOfByte - Return a source location that points to the specified
581/// byte of this string literal.
582///
583/// Strings are amazingly complex. They can be formed from multiple tokens and
584/// can have escape sequences in them in addition to the usual trigraph and
585/// escaped newline business. This routine handles this complexity.
586///
587SourceLocation StringLiteral::
588getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
589 const LangOptions &Features, const TargetInfo &Target) const {
590 assert(!isWide() && "This doesn't work for wide strings yet");
591
592 // Loop over all of the tokens in this string until we find the one that
593 // contains the byte we're looking for.
594 unsigned TokNo = 0;
595 while (1) {
596 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
597 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
598
599 // Get the spelling of the string so that we can get the data that makes up
600 // the string literal, not the identifier for the macro it is potentially
601 // expanded through.
602 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
603
604 // Re-lex the token to get its length and original spelling.
605 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
606 bool Invalid = false;
607 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
608 if (Invalid)
609 return StrTokSpellingLoc;
610
611 const char *StrData = Buffer.data()+LocInfo.second;
612
613 // Create a langops struct and enable trigraphs. This is sufficient for
614 // relexing tokens.
615 LangOptions LangOpts;
616 LangOpts.Trigraphs = true;
617
618 // Create a lexer starting at the beginning of this token.
619 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
620 Buffer.end());
621 Token TheTok;
622 TheLexer.LexFromRawLexer(TheTok);
623
624 // Use the StringLiteralParser to compute the length of the string in bytes.
625 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
626 unsigned TokNumBytes = SLP.GetStringLength();
627
628 // If the byte is in this token, return the location of the byte.
629 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +0000630 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +0000631 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
632
633 // Now that we know the offset of the token in the spelling, use the
634 // preprocessor to get the offset in the original source.
635 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
636 }
637
638 // Move to the next string token.
639 ++TokNo;
640 ByteNo -= TokNumBytes;
641 }
642}
643
644
645
Chris Lattner1b926492006-08-23 06:42:10 +0000646/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
647/// corresponds to, e.g. "sizeof" or "[pre]++".
648const char *UnaryOperator::getOpcodeStr(Opcode Op) {
649 switch (Op) {
Chris Lattnerc52b1182006-10-25 05:45:55 +0000650 default: assert(0 && "Unknown unary operator");
John McCalle3027922010-08-25 11:45:40 +0000651 case UO_PostInc: return "++";
652 case UO_PostDec: return "--";
653 case UO_PreInc: return "++";
654 case UO_PreDec: return "--";
655 case UO_AddrOf: return "&";
656 case UO_Deref: return "*";
657 case UO_Plus: return "+";
658 case UO_Minus: return "-";
659 case UO_Not: return "~";
660 case UO_LNot: return "!";
661 case UO_Real: return "__real";
662 case UO_Imag: return "__imag";
663 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000664 }
665}
666
John McCalle3027922010-08-25 11:45:40 +0000667UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000668UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
669 switch (OO) {
Douglas Gregor084d8552009-03-13 23:49:33 +0000670 default: assert(false && "No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000671 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
672 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
673 case OO_Amp: return UO_AddrOf;
674 case OO_Star: return UO_Deref;
675 case OO_Plus: return UO_Plus;
676 case OO_Minus: return UO_Minus;
677 case OO_Tilde: return UO_Not;
678 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000679 }
680}
681
682OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
683 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000684 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
685 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
686 case UO_AddrOf: return OO_Amp;
687 case UO_Deref: return OO_Star;
688 case UO_Plus: return OO_Plus;
689 case UO_Minus: return OO_Minus;
690 case UO_Not: return OO_Tilde;
691 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000692 default: return OO_None;
693 }
694}
695
696
Chris Lattner0eedafe2006-08-24 04:56:27 +0000697//===----------------------------------------------------------------------===//
698// Postfix Operators.
699//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000700
Peter Collingbourne3a347252011-02-08 21:18:02 +0000701CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
702 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCall7decc9e2010-11-18 06:31:45 +0000703 SourceLocation rparenloc)
704 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000705 fn->isTypeDependent(),
706 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000707 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000708 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000709 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000710
Peter Collingbourne3a347252011-02-08 21:18:02 +0000711 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +0000712 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000713 for (unsigned i = 0; i != numargs; ++i) {
714 if (args[i]->isTypeDependent())
715 ExprBits.TypeDependent = true;
716 if (args[i]->isValueDependent())
717 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000718 if (args[i]->isInstantiationDependent())
719 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000720 if (args[i]->containsUnexpandedParameterPack())
721 ExprBits.ContainsUnexpandedParameterPack = true;
722
Peter Collingbourne3a347252011-02-08 21:18:02 +0000723 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000724 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000725
Peter Collingbourne3a347252011-02-08 21:18:02 +0000726 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +0000727 RParenLoc = rparenloc;
728}
Nate Begeman1e36a852008-01-17 17:46:27 +0000729
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000730CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCall7decc9e2010-11-18 06:31:45 +0000731 QualType t, ExprValueKind VK, SourceLocation rparenloc)
732 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000733 fn->isTypeDependent(),
734 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000735 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000736 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000737 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000738
Peter Collingbourne3a347252011-02-08 21:18:02 +0000739 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000740 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000741 for (unsigned i = 0; i != numargs; ++i) {
742 if (args[i]->isTypeDependent())
743 ExprBits.TypeDependent = true;
744 if (args[i]->isValueDependent())
745 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000746 if (args[i]->isInstantiationDependent())
747 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000748 if (args[i]->containsUnexpandedParameterPack())
749 ExprBits.ContainsUnexpandedParameterPack = true;
750
Peter Collingbourne3a347252011-02-08 21:18:02 +0000751 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000752 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000753
Peter Collingbourne3a347252011-02-08 21:18:02 +0000754 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000755 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000756}
757
Mike Stump11289f42009-09-09 15:08:12 +0000758CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
759 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000760 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +0000761 SubExprs = new (C) Stmt*[PREARGS_START];
762 CallExprBits.NumPreArgs = 0;
763}
764
765CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
766 EmptyShell Empty)
767 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
768 // FIXME: Why do we allocate this?
769 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
770 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +0000771}
772
Nuno Lopes518e3702009-12-20 23:11:08 +0000773Decl *CallExpr::getCalleeDecl() {
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000774 Expr *CEE = getCallee()->IgnoreParenCasts();
Sebastian Redl2b1832e2010-09-10 20:55:30 +0000775 // If we're calling a dereference, look at the pointer instead.
776 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
777 if (BO->isPtrMemOp())
778 CEE = BO->getRHS()->IgnoreParenCasts();
779 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
780 if (UO->getOpcode() == UO_Deref)
781 CEE = UO->getSubExpr()->IgnoreParenCasts();
782 }
Chris Lattner52301912009-07-17 15:46:27 +0000783 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000784 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000785 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
786 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000787
788 return 0;
789}
790
Nuno Lopes518e3702009-12-20 23:11:08 +0000791FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000792 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000793}
794
Chris Lattnere4407ed2007-12-28 05:25:02 +0000795/// setNumArgs - This changes the number of arguments present in this call.
796/// Any orphaned expressions are deleted by this, and any new operands are set
797/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000798void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000799 // No change, just return.
800 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000801
Chris Lattnere4407ed2007-12-28 05:25:02 +0000802 // If shrinking # arguments, just delete the extras and forgot them.
803 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000804 this->NumArgs = NumArgs;
805 return;
806 }
807
808 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000809 unsigned NumPreArgs = getNumPreArgs();
810 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000811 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000812 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000813 NewSubExprs[i] = SubExprs[i];
814 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000815 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
816 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000817 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000818
Douglas Gregorba6e5572009-04-17 21:46:47 +0000819 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000820 SubExprs = NewSubExprs;
821 this->NumArgs = NumArgs;
822}
823
Chris Lattner01ff98a2008-10-06 05:00:53 +0000824/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
825/// not, return 0.
Jay Foad39c79802011-01-12 09:06:06 +0000826unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000827 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000828 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000829 // ImplicitCastExpr.
830 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
831 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000832 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000833
Steve Narofff6e3b3292008-01-31 01:07:12 +0000834 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
835 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000836 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000837
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000838 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
839 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000840 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000841
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000842 if (!FDecl->getIdentifier())
843 return 0;
844
Douglas Gregor15fc9562009-09-12 00:22:50 +0000845 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000846}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000847
Anders Carlsson00a27592009-05-26 04:57:27 +0000848QualType CallExpr::getCallReturnType() const {
849 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000850 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000851 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000852 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000853 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +0000854 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
855 // This should never be overloaded and so should never return null.
856 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +0000857
John McCall0009fcc2011-04-26 20:42:42 +0000858 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000859 return FnType->getResultType();
860}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000861
John McCall701417a2011-02-21 06:23:05 +0000862SourceRange CallExpr::getSourceRange() const {
863 if (isa<CXXOperatorCallExpr>(this))
864 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
865
866 SourceLocation begin = getCallee()->getLocStart();
867 if (begin.isInvalid() && getNumArgs() > 0)
868 begin = getArg(0)->getLocStart();
869 SourceLocation end = getRParenLoc();
870 if (end.isInvalid() && getNumArgs() > 0)
871 end = getArg(getNumArgs() - 1)->getLocEnd();
872 return SourceRange(begin, end);
873}
874
Alexis Hunta8136cc2010-05-05 15:23:54 +0000875OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000876 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000877 TypeSourceInfo *tsi,
878 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000879 Expr** exprsPtr, unsigned numExprs,
880 SourceLocation RParenLoc) {
881 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000882 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000883 sizeof(Expr*) * numExprs);
884
885 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
886 exprsPtr, numExprs, RParenLoc);
887}
888
889OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
890 unsigned numComps, unsigned numExprs) {
891 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
892 sizeof(OffsetOfNode) * numComps +
893 sizeof(Expr*) * numExprs);
894 return new (Mem) OffsetOfExpr(numComps, numExprs);
895}
896
Alexis Hunta8136cc2010-05-05 15:23:54 +0000897OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000898 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000899 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000900 Expr** exprsPtr, unsigned numExprs,
901 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +0000902 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
903 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000904 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000905 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000906 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000907 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
908 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000909{
910 for(unsigned i = 0; i < numComps; ++i) {
911 setComponent(i, compsPtr[i]);
912 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000913
Douglas Gregor882211c2010-04-28 22:16:22 +0000914 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000915 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
916 ExprBits.ValueDependent = true;
917 if (exprsPtr[i]->containsUnexpandedParameterPack())
918 ExprBits.ContainsUnexpandedParameterPack = true;
919
Douglas Gregor882211c2010-04-28 22:16:22 +0000920 setIndexExpr(i, exprsPtr[i]);
921 }
922}
923
924IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
925 assert(getKind() == Field || getKind() == Identifier);
926 if (getKind() == Field)
927 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000928
Douglas Gregor882211c2010-04-28 22:16:22 +0000929 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
930}
931
Mike Stump11289f42009-09-09 15:08:12 +0000932MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +0000933 NestedNameSpecifierLoc QualifierLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000934 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000935 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000936 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +0000937 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +0000938 QualType ty,
939 ExprValueKind vk,
940 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000941 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000942
Douglas Gregorea972d32011-02-28 21:54:11 +0000943 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +0000944 founddecl.getDecl() != memberdecl ||
945 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000946 if (hasQualOrFound)
947 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000948
John McCall6b51f282009-11-23 01:53:49 +0000949 if (targs)
950 Size += ExplicitTemplateArgumentList::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000951
Chris Lattner5c0b4052010-10-30 05:14:06 +0000952 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +0000953 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
954 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +0000955
956 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000957 // FIXME: Wrong. We should be looking at the member declaration we found.
958 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +0000959 E->setValueDependent(true);
960 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000961 E->setInstantiationDependent(true);
962 }
963 else if (QualifierLoc &&
964 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
965 E->setInstantiationDependent(true);
966
John McCall16df1e52010-03-30 21:47:33 +0000967 E->HasQualifierOrFoundDecl = true;
968
969 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +0000970 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +0000971 NQ->FoundDecl = founddecl;
972 }
973
974 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000975 bool Dependent = false;
976 bool InstantiationDependent = false;
977 bool ContainsUnexpandedParameterPack = false;
John McCall16df1e52010-03-30 21:47:33 +0000978 E->HasExplicitTemplateArgumentList = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000979 E->getExplicitTemplateArgs().initializeFrom(*targs, Dependent,
980 InstantiationDependent,
981 ContainsUnexpandedParameterPack);
982 if (InstantiationDependent)
983 E->setInstantiationDependent(true);
John McCall16df1e52010-03-30 21:47:33 +0000984 }
985
986 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000987}
988
Douglas Gregor25b7e052011-03-02 21:06:53 +0000989SourceRange MemberExpr::getSourceRange() const {
990 SourceLocation StartLoc;
991 if (isImplicitAccess()) {
992 if (hasQualifier())
993 StartLoc = getQualifierLoc().getBeginLoc();
994 else
995 StartLoc = MemberLoc;
996 } else {
997 // FIXME: We don't want this to happen. Rather, we should be able to
998 // detect all kinds of implicit accesses more cleanly.
999 StartLoc = getBase()->getLocStart();
1000 if (StartLoc.isInvalid())
1001 StartLoc = MemberLoc;
1002 }
1003
1004 SourceLocation EndLoc =
1005 HasExplicitTemplateArgumentList? getRAngleLoc()
1006 : getMemberNameInfo().getEndLoc();
1007
1008 return SourceRange(StartLoc, EndLoc);
1009}
1010
Anders Carlsson496335e2009-09-03 00:59:21 +00001011const char *CastExpr::getCastKindName() const {
1012 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001013 case CK_Dependent:
1014 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001015 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001016 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001017 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001018 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001019 case CK_LValueToRValue:
1020 return "LValueToRValue";
John McCall34376a62010-12-04 03:47:34 +00001021 case CK_GetObjCProperty:
1022 return "GetObjCProperty";
John McCalle3027922010-08-25 11:45:40 +00001023 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001024 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001025 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001026 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001027 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001028 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001029 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001030 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001031 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001032 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001033 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001034 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001035 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001036 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001037 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001038 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001039 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001040 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001041 case CK_NullToPointer:
1042 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001043 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001044 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001045 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001046 return "DerivedToBaseMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001047 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001048 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001049 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001050 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001051 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001052 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001053 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001054 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001055 case CK_PointerToBoolean:
1056 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001057 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001058 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001059 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001060 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001061 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001062 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001063 case CK_IntegralToBoolean:
1064 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001065 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001066 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001067 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001068 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001069 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001070 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001071 case CK_FloatingToBoolean:
1072 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001073 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001074 return "MemberPointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001075 case CK_AnyPointerToObjCPointerCast:
Fariborz Jahaniane19122f2009-12-08 23:46:15 +00001076 return "AnyPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001077 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001078 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001079 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001080 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001081 case CK_FloatingRealToComplex:
1082 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001083 case CK_FloatingComplexToReal:
1084 return "FloatingComplexToReal";
1085 case CK_FloatingComplexToBoolean:
1086 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001087 case CK_FloatingComplexCast:
1088 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001089 case CK_FloatingComplexToIntegralComplex:
1090 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001091 case CK_IntegralRealToComplex:
1092 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001093 case CK_IntegralComplexToReal:
1094 return "IntegralComplexToReal";
1095 case CK_IntegralComplexToBoolean:
1096 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001097 case CK_IntegralComplexCast:
1098 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001099 case CK_IntegralComplexToFloatingComplex:
1100 return "IntegralComplexToFloatingComplex";
John McCall31168b02011-06-15 23:02:42 +00001101 case CK_ObjCConsumeObject:
1102 return "ObjCConsumeObject";
1103 case CK_ObjCProduceObject:
1104 return "ObjCProduceObject";
Anders Carlsson496335e2009-09-03 00:59:21 +00001105 }
Mike Stump11289f42009-09-09 15:08:12 +00001106
John McCallc5e62b42010-11-13 09:02:35 +00001107 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001108 return 0;
1109}
1110
Douglas Gregord196a582009-12-14 19:27:10 +00001111Expr *CastExpr::getSubExprAsWritten() {
1112 Expr *SubExpr = 0;
1113 CastExpr *E = this;
1114 do {
1115 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001116
1117 // Skip through reference binding to temporary.
1118 if (MaterializeTemporaryExpr *Materialize
1119 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1120 SubExpr = Materialize->GetTemporaryExpr();
1121
Douglas Gregord196a582009-12-14 19:27:10 +00001122 // Skip any temporary bindings; they're implicit.
1123 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1124 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001125
Douglas Gregord196a582009-12-14 19:27:10 +00001126 // Conversions by constructor and conversion functions have a
1127 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001128 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001129 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001130 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001131 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001132
Douglas Gregord196a582009-12-14 19:27:10 +00001133 // If the subexpression we're left with is an implicit cast, look
1134 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001135 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1136
Douglas Gregord196a582009-12-14 19:27:10 +00001137 return SubExpr;
1138}
1139
John McCallcf142162010-08-07 06:22:56 +00001140CXXBaseSpecifier **CastExpr::path_buffer() {
1141 switch (getStmtClass()) {
1142#define ABSTRACT_STMT(x)
1143#define CASTEXPR(Type, Base) \
1144 case Stmt::Type##Class: \
1145 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1146#define STMT(Type, Base)
1147#include "clang/AST/StmtNodes.inc"
1148 default:
1149 llvm_unreachable("non-cast expressions not possible here");
1150 return 0;
1151 }
1152}
1153
1154void CastExpr::setCastPath(const CXXCastPath &Path) {
1155 assert(Path.size() == path_size());
1156 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1157}
1158
1159ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1160 CastKind Kind, Expr *Operand,
1161 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001162 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001163 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1164 void *Buffer =
1165 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1166 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001167 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001168 if (PathSize) E->setCastPath(*BasePath);
1169 return E;
1170}
1171
1172ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1173 unsigned PathSize) {
1174 void *Buffer =
1175 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1176 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1177}
1178
1179
1180CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001181 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001182 const CXXCastPath *BasePath,
1183 TypeSourceInfo *WrittenTy,
1184 SourceLocation L, SourceLocation R) {
1185 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1186 void *Buffer =
1187 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1188 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001189 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001190 if (PathSize) E->setCastPath(*BasePath);
1191 return E;
1192}
1193
1194CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1195 void *Buffer =
1196 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1197 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1198}
1199
Chris Lattner1b926492006-08-23 06:42:10 +00001200/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1201/// corresponds to, e.g. "<<=".
1202const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1203 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001204 case BO_PtrMemD: return ".*";
1205 case BO_PtrMemI: return "->*";
1206 case BO_Mul: return "*";
1207 case BO_Div: return "/";
1208 case BO_Rem: return "%";
1209 case BO_Add: return "+";
1210 case BO_Sub: return "-";
1211 case BO_Shl: return "<<";
1212 case BO_Shr: return ">>";
1213 case BO_LT: return "<";
1214 case BO_GT: return ">";
1215 case BO_LE: return "<=";
1216 case BO_GE: return ">=";
1217 case BO_EQ: return "==";
1218 case BO_NE: return "!=";
1219 case BO_And: return "&";
1220 case BO_Xor: return "^";
1221 case BO_Or: return "|";
1222 case BO_LAnd: return "&&";
1223 case BO_LOr: return "||";
1224 case BO_Assign: return "=";
1225 case BO_MulAssign: return "*=";
1226 case BO_DivAssign: return "/=";
1227 case BO_RemAssign: return "%=";
1228 case BO_AddAssign: return "+=";
1229 case BO_SubAssign: return "-=";
1230 case BO_ShlAssign: return "<<=";
1231 case BO_ShrAssign: return ">>=";
1232 case BO_AndAssign: return "&=";
1233 case BO_XorAssign: return "^=";
1234 case BO_OrAssign: return "|=";
1235 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001236 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001237
1238 return "";
Chris Lattner1b926492006-08-23 06:42:10 +00001239}
Steve Naroff47500512007-04-19 23:00:49 +00001240
John McCalle3027922010-08-25 11:45:40 +00001241BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001242BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1243 switch (OO) {
Chris Lattner17556b22009-03-22 00:10:22 +00001244 default: assert(false && "Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001245 case OO_Plus: return BO_Add;
1246 case OO_Minus: return BO_Sub;
1247 case OO_Star: return BO_Mul;
1248 case OO_Slash: return BO_Div;
1249 case OO_Percent: return BO_Rem;
1250 case OO_Caret: return BO_Xor;
1251 case OO_Amp: return BO_And;
1252 case OO_Pipe: return BO_Or;
1253 case OO_Equal: return BO_Assign;
1254 case OO_Less: return BO_LT;
1255 case OO_Greater: return BO_GT;
1256 case OO_PlusEqual: return BO_AddAssign;
1257 case OO_MinusEqual: return BO_SubAssign;
1258 case OO_StarEqual: return BO_MulAssign;
1259 case OO_SlashEqual: return BO_DivAssign;
1260 case OO_PercentEqual: return BO_RemAssign;
1261 case OO_CaretEqual: return BO_XorAssign;
1262 case OO_AmpEqual: return BO_AndAssign;
1263 case OO_PipeEqual: return BO_OrAssign;
1264 case OO_LessLess: return BO_Shl;
1265 case OO_GreaterGreater: return BO_Shr;
1266 case OO_LessLessEqual: return BO_ShlAssign;
1267 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1268 case OO_EqualEqual: return BO_EQ;
1269 case OO_ExclaimEqual: return BO_NE;
1270 case OO_LessEqual: return BO_LE;
1271 case OO_GreaterEqual: return BO_GE;
1272 case OO_AmpAmp: return BO_LAnd;
1273 case OO_PipePipe: return BO_LOr;
1274 case OO_Comma: return BO_Comma;
1275 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001276 }
1277}
1278
1279OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1280 static const OverloadedOperatorKind OverOps[] = {
1281 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1282 OO_Star, OO_Slash, OO_Percent,
1283 OO_Plus, OO_Minus,
1284 OO_LessLess, OO_GreaterGreater,
1285 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1286 OO_EqualEqual, OO_ExclaimEqual,
1287 OO_Amp,
1288 OO_Caret,
1289 OO_Pipe,
1290 OO_AmpAmp,
1291 OO_PipePipe,
1292 OO_Equal, OO_StarEqual,
1293 OO_SlashEqual, OO_PercentEqual,
1294 OO_PlusEqual, OO_MinusEqual,
1295 OO_LessLessEqual, OO_GreaterGreaterEqual,
1296 OO_AmpEqual, OO_CaretEqual,
1297 OO_PipeEqual,
1298 OO_Comma
1299 };
1300 return OverOps[Opc];
1301}
1302
Ted Kremenekac034612010-04-13 23:39:13 +00001303InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +00001304 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001305 SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001306 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001307 false, false),
Ted Kremenekac034612010-04-13 23:39:13 +00001308 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +00001309 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidisb2ed28e2011-04-21 00:27:41 +00001310 HadArrayRangeDesignator(false)
Alexis Hunta8136cc2010-05-05 15:23:54 +00001311{
Ted Kremenek013041e2010-02-19 01:50:18 +00001312 for (unsigned I = 0; I != numInits; ++I) {
1313 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001314 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001315 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001316 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001317 if (initExprs[I]->isInstantiationDependent())
1318 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001319 if (initExprs[I]->containsUnexpandedParameterPack())
1320 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001321 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001322
Ted Kremenekac034612010-04-13 23:39:13 +00001323 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001324}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001325
Ted Kremenekac034612010-04-13 23:39:13 +00001326void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001327 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001328 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001329}
1330
Ted Kremenekac034612010-04-13 23:39:13 +00001331void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001332 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001333}
1334
Ted Kremenekac034612010-04-13 23:39:13 +00001335Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001336 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001337 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001338 InitExprs.back() = expr;
1339 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001340 }
Mike Stump11289f42009-09-09 15:08:12 +00001341
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001342 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1343 InitExprs[Init] = expr;
1344 return Result;
1345}
1346
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001347void InitListExpr::setArrayFiller(Expr *filler) {
1348 ArrayFillerOrUnionFieldInit = filler;
1349 // Fill out any "holes" in the array due to designated initializers.
1350 Expr **inits = getInits();
1351 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1352 if (inits[i] == 0)
1353 inits[i] = filler;
1354}
1355
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001356SourceRange InitListExpr::getSourceRange() const {
1357 if (SyntacticForm)
1358 return SyntacticForm->getSourceRange();
1359 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1360 if (Beg.isInvalid()) {
1361 // Find the first non-null initializer.
1362 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1363 E = InitExprs.end();
1364 I != E; ++I) {
1365 if (Stmt *S = *I) {
1366 Beg = S->getLocStart();
1367 break;
1368 }
1369 }
1370 }
1371 if (End.isInvalid()) {
1372 // Find the first non-null initializer from the end.
1373 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1374 E = InitExprs.rend();
1375 I != E; ++I) {
1376 if (Stmt *S = *I) {
1377 End = S->getSourceRange().getEnd();
1378 break;
1379 }
1380 }
1381 }
1382 return SourceRange(Beg, End);
1383}
1384
Steve Naroff991e99d2008-09-04 15:31:07 +00001385/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001386///
1387const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001388 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +00001389 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001390}
1391
Mike Stump11289f42009-09-09 15:08:12 +00001392SourceLocation BlockExpr::getCaretLocation() const {
1393 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001394}
Mike Stump11289f42009-09-09 15:08:12 +00001395const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001396 return TheBlock->getBody();
1397}
Mike Stump11289f42009-09-09 15:08:12 +00001398Stmt *BlockExpr::getBody() {
1399 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001400}
Steve Naroff415d3d52008-10-08 17:01:13 +00001401
1402
Chris Lattner1ec5f562007-06-27 05:38:08 +00001403//===----------------------------------------------------------------------===//
1404// Generic Expression Routines
1405//===----------------------------------------------------------------------===//
1406
Chris Lattner237f2752009-02-14 07:37:35 +00001407/// isUnusedResultAWarning - Return true if this immediate expression should
1408/// be warned about if the result is unused. If so, fill in Loc and Ranges
1409/// with location to warn on and the source range[s] to report with the
1410/// warning.
1411bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +00001412 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001413 // Don't warn if the expr is type dependent. The type could end up
1414 // instantiating to void.
1415 if (isTypeDependent())
1416 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001417
Chris Lattner1ec5f562007-06-27 05:38:08 +00001418 switch (getStmtClass()) {
1419 default:
John McCallc493a732010-03-12 07:11:26 +00001420 if (getType()->isVoidType())
1421 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001422 Loc = getExprLoc();
1423 R1 = getSourceRange();
1424 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001425 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001426 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +00001427 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001428 case GenericSelectionExprClass:
1429 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1430 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001431 case UnaryOperatorClass: {
1432 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001433
Chris Lattner1ec5f562007-06-27 05:38:08 +00001434 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001435 default: break;
John McCalle3027922010-08-25 11:45:40 +00001436 case UO_PostInc:
1437 case UO_PostDec:
1438 case UO_PreInc:
1439 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001440 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001441 case UO_Deref:
Chris Lattnera44d1162007-06-27 05:58:59 +00001442 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001443 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001444 return false;
1445 break;
John McCalle3027922010-08-25 11:45:40 +00001446 case UO_Real:
1447 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001448 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001449 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1450 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001451 return false;
1452 break;
John McCalle3027922010-08-25 11:45:40 +00001453 case UO_Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001454 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001455 }
Chris Lattner237f2752009-02-14 07:37:35 +00001456 Loc = UO->getOperatorLoc();
1457 R1 = UO->getSubExpr()->getSourceRange();
1458 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001459 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001460 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001461 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001462 switch (BO->getOpcode()) {
1463 default:
1464 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001465 // Consider the RHS of comma for side effects. LHS was checked by
1466 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001467 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001468 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1469 // lvalue-ness) of an assignment written in a macro.
1470 if (IntegerLiteral *IE =
1471 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1472 if (IE->getValue() == 0)
1473 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001474 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1475 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001476 case BO_LAnd:
1477 case BO_LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001478 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1479 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1480 return false;
1481 break;
John McCall1e3715a2010-02-16 04:10:53 +00001482 }
Chris Lattner237f2752009-02-14 07:37:35 +00001483 if (BO->isAssignmentOp())
1484 return false;
1485 Loc = BO->getOperatorLoc();
1486 R1 = BO->getLHS()->getSourceRange();
1487 R2 = BO->getRHS()->getSourceRange();
1488 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001489 }
Chris Lattner86928112007-08-25 02:00:02 +00001490 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001491 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001492 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001493
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001494 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00001495 // If only one of the LHS or RHS is a warning, the operator might
1496 // be being used for control flow. Only warn if both the LHS and
1497 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001498 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremeneke96dad92011-03-01 20:34:48 +00001499 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1500 return false;
1501 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00001502 return true;
Ted Kremeneke96dad92011-03-01 20:34:48 +00001503 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001504 }
1505
Chris Lattnera44d1162007-06-27 05:58:59 +00001506 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001507 // If the base pointer or element is to a volatile pointer/field, accessing
1508 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001509 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001510 return false;
1511 Loc = cast<MemberExpr>(this)->getMemberLoc();
1512 R1 = SourceRange(Loc, Loc);
1513 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1514 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001515
Chris Lattner1ec5f562007-06-27 05:38:08 +00001516 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001517 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001518 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001519 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001520 return false;
1521 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1522 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1523 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1524 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001525
Chris Lattner1ec5f562007-06-27 05:38:08 +00001526 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001527 case CXXOperatorCallExprClass:
1528 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001529 // If this is a direct call, get the callee.
1530 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001531 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001532 // If the callee has attribute pure, const, or warn_unused_result, warn
1533 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001534 //
1535 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1536 // updated to match for QoI.
1537 if (FD->getAttr<WarnUnusedResultAttr>() ||
1538 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1539 Loc = CE->getCallee()->getLocStart();
1540 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001541
Chris Lattner1a6babf2009-10-13 04:53:48 +00001542 if (unsigned NumArgs = CE->getNumArgs())
1543 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1544 CE->getArg(NumArgs-1)->getLocEnd());
1545 return true;
1546 }
Chris Lattner237f2752009-02-14 07:37:35 +00001547 }
1548 return false;
1549 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001550
1551 case CXXTemporaryObjectExprClass:
1552 case CXXConstructExprClass:
1553 return false;
1554
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001555 case ObjCMessageExprClass: {
1556 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCall31168b02011-06-15 23:02:42 +00001557 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1558 ME->isInstanceMessage() &&
1559 !ME->getType()->isVoidType() &&
1560 ME->getSelector().getIdentifierInfoForSlot(0) &&
1561 ME->getSelector().getIdentifierInfoForSlot(0)
1562 ->getName().startswith("init")) {
1563 Loc = getExprLoc();
1564 R1 = ME->getSourceRange();
1565 return true;
1566 }
1567
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001568 const ObjCMethodDecl *MD = ME->getMethodDecl();
1569 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1570 Loc = getExprLoc();
1571 return true;
1572 }
Chris Lattner237f2752009-02-14 07:37:35 +00001573 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001574 }
Mike Stump11289f42009-09-09 15:08:12 +00001575
John McCallb7bd14f2010-12-02 01:19:52 +00001576 case ObjCPropertyRefExprClass:
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001577 Loc = getExprLoc();
1578 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001579 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00001580
Chris Lattner944d3062008-07-26 19:51:01 +00001581 case StmtExprClass: {
1582 // Statement exprs don't logically have side effects themselves, but are
1583 // sometimes used in macros in ways that give them a type that is unused.
1584 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1585 // however, if the result of the stmt expr is dead, we don't want to emit a
1586 // warning.
1587 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001588 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00001589 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001590 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001591 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1592 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1593 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1594 }
Mike Stump11289f42009-09-09 15:08:12 +00001595
John McCallc493a732010-03-12 07:11:26 +00001596 if (getType()->isVoidType())
1597 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001598 Loc = cast<StmtExpr>(this)->getLParenLoc();
1599 R1 = getSourceRange();
1600 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001601 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001602 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001603 // If this is an explicit cast to void, allow it. People do this when they
1604 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001605 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001606 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001607 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1608 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1609 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001610 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001611 if (getType()->isVoidType())
1612 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001613 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001614
Anders Carlsson6aa50392009-11-17 17:11:23 +00001615 // If this is a cast to void or a constructor conversion, check the operand.
1616 // Otherwise, the result of the cast is unused.
John McCalle3027922010-08-25 11:45:40 +00001617 if (CE->getCastKind() == CK_ToVoid ||
1618 CE->getCastKind() == CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001619 return (cast<CastExpr>(this)->getSubExpr()
1620 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001621 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1622 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1623 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001624 }
Mike Stump11289f42009-09-09 15:08:12 +00001625
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001626 case ImplicitCastExprClass:
1627 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001628 return (cast<ImplicitCastExpr>(this)
1629 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001630
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001631 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001632 return (cast<CXXDefaultArgExpr>(this)
1633 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001634
1635 case CXXNewExprClass:
1636 // FIXME: In theory, there might be new expressions that don't have side
1637 // effects (e.g. a placement new with an uninitialized POD).
1638 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001639 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001640 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001641 return (cast<CXXBindTemporaryExpr>(this)
1642 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00001643 case ExprWithCleanupsClass:
1644 return (cast<ExprWithCleanups>(this)
Mike Stump53f9ded2009-11-03 23:25:48 +00001645 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001646 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001647}
1648
Fariborz Jahanian07735332009-02-22 18:40:18 +00001649/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001650/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001651bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00001652 const Expr *E = IgnoreParens();
1653 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001654 default:
1655 return false;
1656 case ObjCIvarRefExprClass:
1657 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001658 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001659 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001660 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001661 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00001662 case MaterializeTemporaryExprClass:
1663 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1664 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001665 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001666 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001667 case DeclRefExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00001668 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001669 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1670 if (VD->hasGlobalStorage())
1671 return true;
1672 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001673 // dereferencing to a pointer is always a gc'able candidate,
1674 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001675 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001676 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001677 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001678 return false;
1679 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001680 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00001681 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001682 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001683 }
1684 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001685 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001686 }
1687}
Sebastian Redlce354af2010-09-10 20:55:33 +00001688
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001689bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1690 if (isTypeDependent())
1691 return false;
John McCall086a4642010-11-24 05:12:34 +00001692 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001693}
1694
John McCall0009fcc2011-04-26 20:42:42 +00001695QualType Expr::findBoundMemberType(const Expr *expr) {
1696 assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1697
1698 // Bound member expressions are always one of these possibilities:
1699 // x->m x.m x->*y x.*y
1700 // (possibly parenthesized)
1701
1702 expr = expr->IgnoreParens();
1703 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1704 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1705 return mem->getMemberDecl()->getType();
1706 }
1707
1708 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1709 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1710 ->getPointeeType();
1711 assert(type->isFunctionType());
1712 return type;
1713 }
1714
1715 assert(isa<UnresolvedMemberExpr>(expr));
1716 return QualType();
1717}
1718
Sebastian Redlce354af2010-09-10 20:55:33 +00001719static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1720 Expr::CanThrowResult CT2) {
1721 // CanThrowResult constants are ordered so that the maximum is the correct
1722 // merge result.
1723 return CT1 > CT2 ? CT1 : CT2;
1724}
1725
1726static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1727 Expr *E = const_cast<Expr*>(CE);
1728 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall8322c3a2011-02-13 04:07:26 +00001729 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redlce354af2010-09-10 20:55:33 +00001730 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1731 }
1732 return R;
1733}
1734
Richard Smith938f40b2011-06-11 17:19:42 +00001735static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1736 const Decl *D,
Sebastian Redlce354af2010-09-10 20:55:33 +00001737 bool NullThrows = true) {
1738 if (!D)
1739 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1740
1741 // See if we can get a function type from the decl somehow.
1742 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1743 if (!VD) // If we have no clue what we're calling, assume the worst.
1744 return Expr::CT_Can;
1745
Sebastian Redlb8a76c42010-09-10 22:34:40 +00001746 // As an extension, we assume that __attribute__((nothrow)) functions don't
1747 // throw.
1748 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1749 return Expr::CT_Cannot;
1750
Sebastian Redlce354af2010-09-10 20:55:33 +00001751 QualType T = VD->getType();
1752 const FunctionProtoType *FT;
1753 if ((FT = T->getAs<FunctionProtoType>())) {
1754 } else if (const PointerType *PT = T->getAs<PointerType>())
1755 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1756 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1757 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1758 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1759 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1760 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1761 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1762
1763 if (!FT)
1764 return Expr::CT_Can;
1765
Richard Smith938f40b2011-06-11 17:19:42 +00001766 if (FT->getExceptionSpecType() == EST_Delayed) {
1767 assert(isa<CXXConstructorDecl>(D) &&
1768 "only constructor exception specs can be unknown");
1769 Ctx.getDiagnostics().Report(E->getLocStart(),
1770 diag::err_exception_spec_unknown)
1771 << E->getSourceRange();
1772 return Expr::CT_Can;
1773 }
1774
Sebastian Redl31ad7542011-03-13 17:09:40 +00001775 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redlce354af2010-09-10 20:55:33 +00001776}
1777
1778static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1779 if (DC->isTypeDependent())
1780 return Expr::CT_Dependent;
1781
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001782 if (!DC->getTypeAsWritten()->isReferenceType())
1783 return Expr::CT_Cannot;
1784
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001785 if (DC->getSubExpr()->isTypeDependent())
1786 return Expr::CT_Dependent;
1787
Sebastian Redlce354af2010-09-10 20:55:33 +00001788 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1789}
1790
1791static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1792 const CXXTypeidExpr *DC) {
1793 if (DC->isTypeOperand())
1794 return Expr::CT_Cannot;
1795
1796 Expr *Op = DC->getExprOperand();
1797 if (Op->isTypeDependent())
1798 return Expr::CT_Dependent;
1799
1800 const RecordType *RT = Op->getType()->getAs<RecordType>();
1801 if (!RT)
1802 return Expr::CT_Cannot;
1803
1804 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1805 return Expr::CT_Cannot;
1806
1807 if (Op->Classify(C).isPRValue())
1808 return Expr::CT_Cannot;
1809
1810 return Expr::CT_Can;
1811}
1812
1813Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1814 // C++ [expr.unary.noexcept]p3:
1815 // [Can throw] if in a potentially-evaluated context the expression would
1816 // contain:
1817 switch (getStmtClass()) {
1818 case CXXThrowExprClass:
1819 // - a potentially evaluated throw-expression
1820 return CT_Can;
1821
1822 case CXXDynamicCastExprClass: {
1823 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1824 // where T is a reference type, that requires a run-time check
1825 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1826 if (CT == CT_Can)
1827 return CT;
1828 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1829 }
1830
1831 case CXXTypeidExprClass:
1832 // - a potentially evaluated typeid expression applied to a glvalue
1833 // expression whose type is a polymorphic class type
1834 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1835
1836 // - a potentially evaluated call to a function, member function, function
1837 // pointer, or member function pointer that does not have a non-throwing
1838 // exception-specification
1839 case CallExprClass:
1840 case CXXOperatorCallExprClass:
1841 case CXXMemberCallExprClass: {
Eli Friedman622e4fc2011-05-12 02:11:32 +00001842 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001843 CanThrowResult CT;
1844 if (isTypeDependent())
1845 CT = CT_Dependent;
Eli Friedman622e4fc2011-05-12 02:11:32 +00001846 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1847 CT = CT_Cannot;
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001848 else
Richard Smith938f40b2011-06-11 17:19:42 +00001849 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redlce354af2010-09-10 20:55:33 +00001850 if (CT == CT_Can)
1851 return CT;
1852 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1853 }
1854
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001855 case CXXConstructExprClass:
1856 case CXXTemporaryObjectExprClass: {
Richard Smith938f40b2011-06-11 17:19:42 +00001857 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redlce354af2010-09-10 20:55:33 +00001858 cast<CXXConstructExpr>(this)->getConstructor());
1859 if (CT == CT_Can)
1860 return CT;
1861 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1862 }
1863
1864 case CXXNewExprClass: {
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001865 CanThrowResult CT;
1866 if (isTypeDependent())
1867 CT = CT_Dependent;
1868 else
1869 CT = MergeCanThrow(
Richard Smith938f40b2011-06-11 17:19:42 +00001870 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
1871 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redlce354af2010-09-10 20:55:33 +00001872 /*NullThrows*/false));
1873 if (CT == CT_Can)
1874 return CT;
1875 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1876 }
1877
1878 case CXXDeleteExprClass: {
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001879 CanThrowResult CT;
1880 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
1881 if (DTy.isNull() || DTy->isDependentType()) {
1882 CT = CT_Dependent;
1883 } else {
Richard Smith938f40b2011-06-11 17:19:42 +00001884 CT = CanCalleeThrow(C, this,
1885 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001886 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1887 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith938f40b2011-06-11 17:19:42 +00001888 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redla8bac372010-09-10 23:27:10 +00001889 }
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001890 if (CT == CT_Can)
1891 return CT;
Sebastian Redla8bac372010-09-10 23:27:10 +00001892 }
1893 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1894 }
1895
1896 case CXXBindTemporaryExprClass: {
1897 // The bound temporary has to be destroyed again, which might throw.
Richard Smith938f40b2011-06-11 17:19:42 +00001898 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redla8bac372010-09-10 23:27:10 +00001899 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1900 if (CT == CT_Can)
1901 return CT;
Sebastian Redlce354af2010-09-10 20:55:33 +00001902 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1903 }
1904
1905 // ObjC message sends are like function calls, but never have exception
1906 // specs.
1907 case ObjCMessageExprClass:
1908 case ObjCPropertyRefExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001909 return CT_Can;
1910
1911 // Many other things have subexpressions, so we have to test those.
1912 // Some are simple:
1913 case ParenExprClass:
1914 case MemberExprClass:
1915 case CXXReinterpretCastExprClass:
1916 case CXXConstCastExprClass:
1917 case ConditionalOperatorClass:
1918 case CompoundLiteralExprClass:
1919 case ExtVectorElementExprClass:
1920 case InitListExprClass:
1921 case DesignatedInitExprClass:
1922 case ParenListExprClass:
1923 case VAArgExprClass:
1924 case CXXDefaultArgExprClass:
John McCall5d413782010-12-06 08:20:24 +00001925 case ExprWithCleanupsClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001926 case ObjCIvarRefExprClass:
1927 case ObjCIsaExprClass:
1928 case ShuffleVectorExprClass:
1929 return CanSubExprsThrow(C, this);
1930
1931 // Some might be dependent for other reasons.
1932 case UnaryOperatorClass:
1933 case ArraySubscriptExprClass:
1934 case ImplicitCastExprClass:
1935 case CStyleCastExprClass:
1936 case CXXStaticCastExprClass:
1937 case CXXFunctionalCastExprClass:
1938 case BinaryOperatorClass:
Douglas Gregorfe314812011-06-21 17:03:29 +00001939 case CompoundAssignOperatorClass:
1940 case MaterializeTemporaryExprClass: {
Sebastian Redlce354af2010-09-10 20:55:33 +00001941 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
1942 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1943 }
1944
1945 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
1946 case StmtExprClass:
1947 return CT_Can;
1948
1949 case ChooseExprClass:
1950 if (isTypeDependent() || isValueDependent())
1951 return CT_Dependent;
1952 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
1953
Peter Collingbourne91147592011-04-15 00:35:48 +00001954 case GenericSelectionExprClass:
1955 if (cast<GenericSelectionExpr>(this)->isResultDependent())
1956 return CT_Dependent;
1957 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
1958
Sebastian Redlce354af2010-09-10 20:55:33 +00001959 // Some expressions are always dependent.
1960 case DependentScopeDeclRefExprClass:
1961 case CXXUnresolvedConstructExprClass:
1962 case CXXDependentScopeMemberExprClass:
1963 return CT_Dependent;
1964
1965 default:
1966 // All other expressions don't have subexpressions, or else they are
1967 // unevaluated.
1968 return CT_Cannot;
1969 }
1970}
1971
Ted Kremenekfff70962008-01-17 16:57:34 +00001972Expr* Expr::IgnoreParens() {
1973 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00001974 while (true) {
1975 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
1976 E = P->getSubExpr();
1977 continue;
1978 }
1979 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
1980 if (P->getOpcode() == UO_Extension) {
1981 E = P->getSubExpr();
1982 continue;
1983 }
1984 }
Peter Collingbourne91147592011-04-15 00:35:48 +00001985 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
1986 if (!P->isResultDependent()) {
1987 E = P->getResultExpr();
1988 continue;
1989 }
1990 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00001991 return E;
1992 }
Ted Kremenekfff70962008-01-17 16:57:34 +00001993}
1994
Chris Lattnerf2660962008-02-13 01:02:39 +00001995/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
1996/// or CastExprs or ImplicitCastExprs, returning their operand.
1997Expr *Expr::IgnoreParenCasts() {
1998 Expr *E = this;
1999 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00002000 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002001 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002002 continue;
2003 }
2004 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002005 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002006 continue;
2007 }
2008 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2009 if (P->getOpcode() == UO_Extension) {
2010 E = P->getSubExpr();
2011 continue;
2012 }
2013 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002014 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2015 if (!P->isResultDependent()) {
2016 E = P->getResultExpr();
2017 continue;
2018 }
2019 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002020 if (MaterializeTemporaryExpr *Materialize
2021 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2022 E = Materialize->GetTemporaryExpr();
2023 continue;
2024 }
2025
Abramo Bagnara932e3932010-10-15 07:51:18 +00002026 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002027 }
2028}
2029
John McCall5a4ce8b2010-12-04 08:24:19 +00002030/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2031/// casts. This is intended purely as a temporary workaround for code
2032/// that hasn't yet been rewritten to do the right thing about those
2033/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002034Expr *Expr::IgnoreParenLValueCasts() {
2035 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002036 while (true) {
John McCall34376a62010-12-04 03:47:34 +00002037 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2038 E = P->getSubExpr();
2039 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00002040 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002041 if (P->getCastKind() == CK_LValueToRValue) {
2042 E = P->getSubExpr();
2043 continue;
2044 }
John McCall5a4ce8b2010-12-04 08:24:19 +00002045 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2046 if (P->getOpcode() == UO_Extension) {
2047 E = P->getSubExpr();
2048 continue;
2049 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002050 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2051 if (!P->isResultDependent()) {
2052 E = P->getResultExpr();
2053 continue;
2054 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002055 } else if (MaterializeTemporaryExpr *Materialize
2056 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2057 E = Materialize->GetTemporaryExpr();
2058 continue;
John McCall34376a62010-12-04 03:47:34 +00002059 }
2060 break;
2061 }
2062 return E;
2063}
2064
John McCalleebc8322010-05-05 22:59:52 +00002065Expr *Expr::IgnoreParenImpCasts() {
2066 Expr *E = this;
2067 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00002068 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002069 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002070 continue;
2071 }
2072 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002073 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002074 continue;
2075 }
2076 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2077 if (P->getOpcode() == UO_Extension) {
2078 E = P->getSubExpr();
2079 continue;
2080 }
2081 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002082 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2083 if (!P->isResultDependent()) {
2084 E = P->getResultExpr();
2085 continue;
2086 }
2087 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002088 if (MaterializeTemporaryExpr *Materialize
2089 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2090 E = Materialize->GetTemporaryExpr();
2091 continue;
2092 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002093 return E;
John McCalleebc8322010-05-05 22:59:52 +00002094 }
2095}
2096
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002097Expr *Expr::IgnoreConversionOperator() {
2098 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002099 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002100 return MCE->getImplicitObjectArgument();
2101 }
2102 return this;
2103}
2104
Chris Lattneref26c772009-03-13 17:28:01 +00002105/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2106/// value (including ptr->int casts of the same size). Strip off any
2107/// ParenExpr or CastExprs, returning their operand.
2108Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2109 Expr *E = this;
2110 while (true) {
2111 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2112 E = P->getSubExpr();
2113 continue;
2114 }
Mike Stump11289f42009-09-09 15:08:12 +00002115
Chris Lattneref26c772009-03-13 17:28:01 +00002116 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2117 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002118 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002119 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002120
Chris Lattneref26c772009-03-13 17:28:01 +00002121 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2122 E = SE;
2123 continue;
2124 }
Mike Stump11289f42009-09-09 15:08:12 +00002125
Abramo Bagnara932e3932010-10-15 07:51:18 +00002126 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002127 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002128 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002129 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002130 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2131 E = SE;
2132 continue;
2133 }
2134 }
Mike Stump11289f42009-09-09 15:08:12 +00002135
Abramo Bagnara932e3932010-10-15 07:51:18 +00002136 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2137 if (P->getOpcode() == UO_Extension) {
2138 E = P->getSubExpr();
2139 continue;
2140 }
2141 }
2142
Peter Collingbourne91147592011-04-15 00:35:48 +00002143 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2144 if (!P->isResultDependent()) {
2145 E = P->getResultExpr();
2146 continue;
2147 }
2148 }
2149
Chris Lattneref26c772009-03-13 17:28:01 +00002150 return E;
2151 }
2152}
2153
Douglas Gregord196a582009-12-14 19:27:10 +00002154bool Expr::isDefaultArgument() const {
2155 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002156 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2157 E = M->GetTemporaryExpr();
2158
Douglas Gregord196a582009-12-14 19:27:10 +00002159 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2160 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002161
Douglas Gregord196a582009-12-14 19:27:10 +00002162 return isa<CXXDefaultArgExpr>(E);
2163}
Chris Lattneref26c772009-03-13 17:28:01 +00002164
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002165/// \brief Skip over any no-op casts and any temporary-binding
2166/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002167static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002168 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2169 E = M->GetTemporaryExpr();
2170
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002171 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002172 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002173 E = ICE->getSubExpr();
2174 else
2175 break;
2176 }
2177
2178 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2179 E = BE->getSubExpr();
2180
2181 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002182 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002183 E = ICE->getSubExpr();
2184 else
2185 break;
2186 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002187
2188 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002189}
2190
John McCall7a626f62010-09-15 10:14:12 +00002191/// isTemporaryObject - Determines if this expression produces a
2192/// temporary of the given class type.
2193bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2194 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2195 return false;
2196
Anders Carlsson66bbf502010-11-28 16:40:49 +00002197 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002198
John McCall02dc8c72010-09-15 20:59:13 +00002199 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002200 if (!E->Classify(C).isPRValue()) {
2201 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002202 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002203 return false;
2204 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002205
John McCallf4ee1dd2010-09-16 06:57:56 +00002206 // Black-list a few cases which yield pr-values of class type that don't
2207 // refer to temporaries of that type:
2208
2209 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002210 if (isa<ImplicitCastExpr>(E)) {
2211 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2212 case CK_DerivedToBase:
2213 case CK_UncheckedDerivedToBase:
2214 return false;
2215 default:
2216 break;
2217 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002218 }
2219
John McCallf4ee1dd2010-09-16 06:57:56 +00002220 // - member expressions (all)
2221 if (isa<MemberExpr>(E))
2222 return false;
2223
John McCallc07a0c72011-02-17 10:25:35 +00002224 // - opaque values (all)
2225 if (isa<OpaqueValueExpr>(E))
2226 return false;
2227
John McCall7a626f62010-09-15 10:14:12 +00002228 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002229}
2230
Douglas Gregor25b7e052011-03-02 21:06:53 +00002231bool Expr::isImplicitCXXThis() const {
2232 const Expr *E = this;
2233
2234 // Strip away parentheses and casts we don't care about.
2235 while (true) {
2236 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2237 E = Paren->getSubExpr();
2238 continue;
2239 }
2240
2241 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2242 if (ICE->getCastKind() == CK_NoOp ||
2243 ICE->getCastKind() == CK_LValueToRValue ||
2244 ICE->getCastKind() == CK_DerivedToBase ||
2245 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2246 E = ICE->getSubExpr();
2247 continue;
2248 }
2249 }
2250
2251 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2252 if (UnOp->getOpcode() == UO_Extension) {
2253 E = UnOp->getSubExpr();
2254 continue;
2255 }
2256 }
2257
Douglas Gregorfe314812011-06-21 17:03:29 +00002258 if (const MaterializeTemporaryExpr *M
2259 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2260 E = M->GetTemporaryExpr();
2261 continue;
2262 }
2263
Douglas Gregor25b7e052011-03-02 21:06:53 +00002264 break;
2265 }
2266
2267 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2268 return This->isImplicit();
2269
2270 return false;
2271}
2272
Douglas Gregor4619e432008-12-05 23:32:09 +00002273/// hasAnyTypeDependentArguments - Determines if any of the expressions
2274/// in Exprs is type-dependent.
2275bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2276 for (unsigned I = 0; I < NumExprs; ++I)
2277 if (Exprs[I]->isTypeDependent())
2278 return true;
2279
2280 return false;
2281}
2282
2283/// hasAnyValueDependentArguments - Determines if any of the expressions
2284/// in Exprs is value-dependent.
2285bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2286 for (unsigned I = 0; I < NumExprs; ++I)
2287 if (Exprs[I]->isValueDependent())
2288 return true;
2289
2290 return false;
2291}
2292
John McCall8b0f4ff2010-08-02 21:13:48 +00002293bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002294 // This function is attempting whether an expression is an initializer
2295 // which can be evaluated at compile-time. isEvaluatable handles most
2296 // of the cases, but it can't deal with some initializer-specific
2297 // expressions, and it can't deal with aggregates; we deal with those here,
2298 // and fall back to isEvaluatable for the other cases.
2299
John McCall8b0f4ff2010-08-02 21:13:48 +00002300 // If we ever capture reference-binding directly in the AST, we can
2301 // kill the second parameter.
2302
2303 if (IsForRef) {
2304 EvalResult Result;
2305 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2306 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002307
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002308 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002309 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002310 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002311 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002312 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002313 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002314 case CXXTemporaryObjectExprClass:
2315 case CXXConstructExprClass: {
2316 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002317
2318 // Only if it's
2319 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00002320 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002321 if (!CE->getNumArgs()) return true;
2322
2323 // 2) an elidable trivial copy construction of an operand which is
2324 // itself a constant initializer. Note that we consider the
2325 // operand on its own, *not* as a reference binding.
2326 return CE->isElidable() &&
2327 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00002328 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002329 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002330 // This handles gcc's extension that allows global initializers like
2331 // "struct x {int x;} x = (struct x) {};".
2332 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002333 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002334 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002335 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002336 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002337 // FIXME: This doesn't deal with fields with reference types correctly.
2338 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2339 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002340 const InitListExpr *Exp = cast<InitListExpr>(this);
2341 unsigned numInits = Exp->getNumInits();
2342 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002343 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002344 return false;
2345 }
Eli Friedman384da272009-01-25 03:12:18 +00002346 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002347 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002348 case ImplicitValueInitExprClass:
2349 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002350 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002351 return cast<ParenExpr>(this)->getSubExpr()
2352 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002353 case GenericSelectionExprClass:
2354 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2355 return false;
2356 return cast<GenericSelectionExpr>(this)->getResultExpr()
2357 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002358 case ChooseExprClass:
2359 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2360 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002361 case UnaryOperatorClass: {
2362 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002363 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002364 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002365 break;
2366 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00002367 case BinaryOperatorClass: {
2368 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2369 // but this handles the common case.
2370 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002371 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3eb172a2009-10-13 07:14:16 +00002372 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2373 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2374 return true;
2375 break;
2376 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002377 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002378 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002379 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00002380 case CStyleCastExprClass:
2381 // Handle casts with a destination that's a struct or union; this
2382 // deals with both the gcc no-op struct cast extension and the
2383 // cast-to-union extension.
2384 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002385 return cast<CastExpr>(this)->getSubExpr()
2386 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002387
Chris Lattnera2f9bd52009-10-13 22:12:09 +00002388 // Integer->integer casts can be handled here, which is important for
2389 // things like (int)(&&x-&&y). Scary but true.
2390 if (getType()->isIntegerType() &&
2391 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002392 return cast<CastExpr>(this)->getSubExpr()
2393 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002394
Eli Friedman384da272009-01-25 03:12:18 +00002395 break;
Douglas Gregorfe314812011-06-21 17:03:29 +00002396
2397 case MaterializeTemporaryExprClass:
2398 return llvm::cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
2399 ->isConstantInitializer(Ctx, false);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002400 }
Eli Friedman384da272009-01-25 03:12:18 +00002401 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002402}
2403
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002404/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2405/// pointer constant or not, as well as the specific kind of constant detected.
2406/// Null pointer constants can be integer constant expressions with the
2407/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2408/// (a GNU extension).
2409Expr::NullPointerConstantKind
2410Expr::isNullPointerConstant(ASTContext &Ctx,
2411 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00002412 if (isValueDependent()) {
2413 switch (NPC) {
2414 case NPC_NeverValueDependent:
2415 assert(false && "Unexpected value dependent expression!");
2416 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002417
Douglas Gregor56751b52009-09-25 04:25:58 +00002418 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002419 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2420 return NPCK_ZeroInteger;
2421 else
2422 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00002423
Douglas Gregor56751b52009-09-25 04:25:58 +00002424 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002425 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00002426 }
2427 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002428
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002429 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002430 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00002431 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002432 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002433 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002434 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002435 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002436 Pointee->isVoidType() && // to void*
2437 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002438 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002439 }
Steve Naroffada7d422007-05-20 17:54:12 +00002440 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002441 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2442 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002443 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002444 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2445 // Accept ((void*)0) as a null pointer constant, as many other
2446 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002447 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00002448 } else if (const GenericSelectionExpr *GE =
2449 dyn_cast<GenericSelectionExpr>(this)) {
2450 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002451 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002452 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002453 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002454 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002455 } else if (isa<GNUNullExpr>(this)) {
2456 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002457 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00002458 } else if (const MaterializeTemporaryExpr *M
2459 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2460 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00002461 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002462
Sebastian Redl576fd422009-05-10 18:38:11 +00002463 // C++0x nullptr_t is always a null pointer constant.
2464 if (getType()->isNullPtrType())
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002465 return NPCK_CXX0X_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00002466
Fariborz Jahanian3567c422010-09-27 22:42:37 +00002467 if (const RecordType *UT = getType()->getAsUnionType())
2468 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2469 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2470 const Expr *InitExpr = CLE->getInitializer();
2471 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2472 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2473 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002474 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002475 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002476 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002477 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00002478
Chris Lattner1abbd412007-06-08 17:58:43 +00002479 // If we have an integer constant expression, we need to *evaluate* it and
2480 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002481 llvm::APSInt Result;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002482 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2483
2484 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002485}
Steve Narofff7a5da12007-07-28 23:10:27 +00002486
John McCall34376a62010-12-04 03:47:34 +00002487/// \brief If this expression is an l-value for an Objective C
2488/// property, find the underlying property reference expression.
2489const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2490 const Expr *E = this;
2491 while (true) {
2492 assert((E->getValueKind() == VK_LValue &&
2493 E->getObjectKind() == OK_ObjCProperty) &&
2494 "expression is not a property reference");
2495 E = E->IgnoreParenCasts();
2496 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2497 if (BO->getOpcode() == BO_Comma) {
2498 E = BO->getRHS();
2499 continue;
2500 }
2501 }
2502
2503 break;
2504 }
2505
2506 return cast<ObjCPropertyRefExpr>(E);
2507}
2508
Douglas Gregor71235ec2009-05-02 02:18:30 +00002509FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002510 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002511
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002512 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002513 if (ICE->getCastKind() == CK_LValueToRValue ||
2514 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002515 E = ICE->getSubExpr()->IgnoreParens();
2516 else
2517 break;
2518 }
2519
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002520 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002521 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002522 if (Field->isBitField())
2523 return Field;
2524
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00002525 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2526 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2527 if (Field->isBitField())
2528 return Field;
2529
Douglas Gregor71235ec2009-05-02 02:18:30 +00002530 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E))
2531 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2532 return BinOp->getLHS()->getBitField();
2533
2534 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002535}
2536
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002537bool Expr::refersToVectorElement() const {
2538 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002539
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002540 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002541 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002542 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002543 E = ICE->getSubExpr()->IgnoreParens();
2544 else
2545 break;
2546 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002547
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002548 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2549 return ASE->getBase()->getType()->isVectorType();
2550
2551 if (isa<ExtVectorElementExpr>(E))
2552 return true;
2553
2554 return false;
2555}
2556
Chris Lattnerb8211f62009-02-16 22:14:05 +00002557/// isArrow - Return true if the base expression is a pointer to vector,
2558/// return false if the base expression is a vector.
2559bool ExtVectorElementExpr::isArrow() const {
2560 return getBase()->getType()->isPointerType();
2561}
2562
Nate Begemance4d7fc2008-04-18 23:10:10 +00002563unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002564 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002565 return VT->getNumElements();
2566 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002567}
2568
Nate Begemanf322eab2008-05-09 06:41:27 +00002569/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002570bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002571 // FIXME: Refactor this code to an accessor on the AST node which returns the
2572 // "type" of component access, and share with code below and in Sema.
Daniel Dunbar07d07852009-10-18 21:17:35 +00002573 llvm::StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002574
2575 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002576 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002577 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002578
Nate Begeman7e5185b2009-01-18 02:01:21 +00002579 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002580 if (Comp[0] == 's' || Comp[0] == 'S')
2581 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002582
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002583 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
2584 if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002585 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002586
Steve Naroff0d595ca2007-07-30 03:29:09 +00002587 return false;
2588}
Chris Lattner885b4952007-08-02 23:36:59 +00002589
Nate Begemanf322eab2008-05-09 06:41:27 +00002590/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002591void ExtVectorElementExpr::getEncodedElementAccess(
2592 llvm::SmallVectorImpl<unsigned> &Elts) const {
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002593 llvm::StringRef Comp = Accessor->getName();
2594 if (Comp[0] == 's' || Comp[0] == 'S')
2595 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002596
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002597 bool isHi = Comp == "hi";
2598 bool isLo = Comp == "lo";
2599 bool isEven = Comp == "even";
2600 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002601
Nate Begemanf322eab2008-05-09 06:41:27 +00002602 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2603 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002604
Nate Begemanf322eab2008-05-09 06:41:27 +00002605 if (isHi)
2606 Index = e + i;
2607 else if (isLo)
2608 Index = i;
2609 else if (isEven)
2610 Index = 2 * i;
2611 else if (isOdd)
2612 Index = 2 * i + 1;
2613 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002614 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002615
Nate Begemand3862152008-05-13 21:03:02 +00002616 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002617 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002618}
2619
Douglas Gregor9a129192010-04-21 00:45:42 +00002620ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002621 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002622 SourceLocation LBracLoc,
2623 SourceLocation SuperLoc,
2624 bool IsInstanceSuper,
2625 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002626 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002627 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002628 ObjCMethodDecl *Method,
2629 Expr **Args, unsigned NumArgs,
2630 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002631 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002632 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00002633 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002634 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002635 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
John McCall31168b02011-06-15 23:02:42 +00002636 HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
Douglas Gregor9a129192010-04-21 00:45:42 +00002637 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2638 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002639 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002640{
Douglas Gregor9a129192010-04-21 00:45:42 +00002641 setReceiverPointer(SuperType.getAsOpaquePtr());
2642 if (NumArgs)
2643 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002644}
2645
Douglas Gregor9a129192010-04-21 00:45:42 +00002646ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002647 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002648 SourceLocation LBracLoc,
2649 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002650 Selector Sel,
2651 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002652 ObjCMethodDecl *Method,
2653 Expr **Args, unsigned NumArgs,
2654 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002655 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002656 T->isDependentType(), T->isInstantiationDependentType(),
2657 T->containsUnexpandedParameterPack()),
John McCall31168b02011-06-15 23:02:42 +00002658 NumArgs(NumArgs), Kind(Class),
2659 HasMethod(Method != 0), IsDelegateInitCall(false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002660 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2661 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002662 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002663{
2664 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002665 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002666 for (unsigned I = 0; I != NumArgs; ++I) {
2667 if (Args[I]->isTypeDependent())
2668 ExprBits.TypeDependent = true;
2669 if (Args[I]->isValueDependent())
2670 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00002671 if (Args[I]->isInstantiationDependent())
2672 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00002673 if (Args[I]->containsUnexpandedParameterPack())
2674 ExprBits.ContainsUnexpandedParameterPack = true;
2675
2676 MyArgs[I] = Args[I];
2677 }
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002678}
2679
Douglas Gregor9a129192010-04-21 00:45:42 +00002680ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002681 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002682 SourceLocation LBracLoc,
2683 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002684 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002685 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002686 ObjCMethodDecl *Method,
2687 Expr **Args, unsigned NumArgs,
2688 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002689 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002690 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002691 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002692 Receiver->containsUnexpandedParameterPack()),
John McCall31168b02011-06-15 23:02:42 +00002693 NumArgs(NumArgs), Kind(Instance),
2694 HasMethod(Method != 0), IsDelegateInitCall(false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002695 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2696 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002697 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002698{
2699 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002700 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002701 for (unsigned I = 0; I != NumArgs; ++I) {
2702 if (Args[I]->isTypeDependent())
2703 ExprBits.TypeDependent = true;
2704 if (Args[I]->isValueDependent())
2705 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00002706 if (Args[I]->isInstantiationDependent())
2707 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00002708 if (Args[I]->containsUnexpandedParameterPack())
2709 ExprBits.ContainsUnexpandedParameterPack = true;
2710
2711 MyArgs[I] = Args[I];
2712 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00002713}
2714
Douglas Gregor9a129192010-04-21 00:45:42 +00002715ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002716 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002717 SourceLocation LBracLoc,
2718 SourceLocation SuperLoc,
2719 bool IsInstanceSuper,
2720 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002721 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002722 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002723 ObjCMethodDecl *Method,
2724 Expr **Args, unsigned NumArgs,
2725 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002726 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002727 NumArgs * sizeof(Expr *);
2728 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCall7decc9e2010-11-18 06:31:45 +00002729 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002730 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002731 RBracLoc);
2732}
2733
2734ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002735 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002736 SourceLocation LBracLoc,
2737 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002738 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002739 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002740 ObjCMethodDecl *Method,
2741 Expr **Args, unsigned NumArgs,
2742 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002743 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002744 NumArgs * sizeof(Expr *);
2745 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002746 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2747 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002748}
2749
2750ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002751 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002752 SourceLocation LBracLoc,
2753 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002754 Selector Sel,
2755 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002756 ObjCMethodDecl *Method,
2757 Expr **Args, unsigned NumArgs,
2758 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002759 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002760 NumArgs * sizeof(Expr *);
2761 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002762 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2763 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002764}
2765
Alexis Hunta8136cc2010-05-05 15:23:54 +00002766ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002767 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002768 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002769 NumArgs * sizeof(Expr *);
2770 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2771 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2772}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00002773
2774SourceRange ObjCMessageExpr::getReceiverRange() const {
2775 switch (getReceiverKind()) {
2776 case Instance:
2777 return getInstanceReceiver()->getSourceRange();
2778
2779 case Class:
2780 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2781
2782 case SuperInstance:
2783 case SuperClass:
2784 return getSuperLoc();
2785 }
2786
2787 return SourceLocation();
2788}
2789
Douglas Gregor9a129192010-04-21 00:45:42 +00002790Selector ObjCMessageExpr::getSelector() const {
2791 if (HasMethod)
2792 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2793 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002794 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002795}
2796
2797ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2798 switch (getReceiverKind()) {
2799 case Instance:
2800 if (const ObjCObjectPointerType *Ptr
2801 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2802 return Ptr->getInterfaceDecl();
2803 break;
2804
2805 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00002806 if (const ObjCObjectType *Ty
2807 = getClassReceiver()->getAs<ObjCObjectType>())
2808 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002809 break;
2810
2811 case SuperInstance:
2812 if (const ObjCObjectPointerType *Ptr
2813 = getSuperType()->getAs<ObjCObjectPointerType>())
2814 return Ptr->getInterfaceDecl();
2815 break;
2816
2817 case SuperClass:
Argyrios Kyrtzidis1b9747f2011-01-25 00:03:48 +00002818 if (const ObjCObjectType *Iface
2819 = getSuperType()->getAs<ObjCObjectType>())
2820 return Iface->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002821 break;
2822 }
2823
2824 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002825}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002826
John McCall31168b02011-06-15 23:02:42 +00002827llvm::StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
2828 switch (getBridgeKind()) {
2829 case OBC_Bridge:
2830 return "__bridge";
2831 case OBC_BridgeTransfer:
2832 return "__bridge_transfer";
2833 case OBC_BridgeRetained:
2834 return "__bridge_retained";
2835 }
2836
2837 return "__bridge";
2838}
2839
Jay Foad39c79802011-01-12 09:06:06 +00002840bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002841 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002842}
2843
Douglas Gregora6e053e2010-12-15 01:34:56 +00002844ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2845 QualType Type, SourceLocation BLoc,
2846 SourceLocation RP)
2847 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2848 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002849 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002850 Type->containsUnexpandedParameterPack()),
2851 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2852{
2853 SubExprs = new (C) Stmt*[nexpr];
2854 for (unsigned i = 0; i < nexpr; i++) {
2855 if (args[i]->isTypeDependent())
2856 ExprBits.TypeDependent = true;
2857 if (args[i]->isValueDependent())
2858 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00002859 if (args[i]->isInstantiationDependent())
2860 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00002861 if (args[i]->containsUnexpandedParameterPack())
2862 ExprBits.ContainsUnexpandedParameterPack = true;
2863
2864 SubExprs[i] = args[i];
2865 }
2866}
2867
Nate Begeman48745922009-08-12 02:28:50 +00002868void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2869 unsigned NumExprs) {
2870 if (SubExprs) C.Deallocate(SubExprs);
2871
2872 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002873 this->NumExprs = NumExprs;
2874 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002875}
Nate Begeman48745922009-08-12 02:28:50 +00002876
Peter Collingbourne91147592011-04-15 00:35:48 +00002877GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2878 SourceLocation GenericLoc, Expr *ControllingExpr,
2879 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2880 unsigned NumAssocs, SourceLocation DefaultLoc,
2881 SourceLocation RParenLoc,
2882 bool ContainsUnexpandedParameterPack,
2883 unsigned ResultIndex)
2884 : Expr(GenericSelectionExprClass,
2885 AssocExprs[ResultIndex]->getType(),
2886 AssocExprs[ResultIndex]->getValueKind(),
2887 AssocExprs[ResultIndex]->getObjectKind(),
2888 AssocExprs[ResultIndex]->isTypeDependent(),
2889 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002890 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00002891 ContainsUnexpandedParameterPack),
2892 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2893 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2894 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2895 RParenLoc(RParenLoc) {
2896 SubExprs[CONTROLLING] = ControllingExpr;
2897 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2898 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2899}
2900
2901GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2902 SourceLocation GenericLoc, Expr *ControllingExpr,
2903 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2904 unsigned NumAssocs, SourceLocation DefaultLoc,
2905 SourceLocation RParenLoc,
2906 bool ContainsUnexpandedParameterPack)
2907 : Expr(GenericSelectionExprClass,
2908 Context.DependentTy,
2909 VK_RValue,
2910 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00002911 /*isTypeDependent=*/true,
2912 /*isValueDependent=*/true,
2913 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00002914 ContainsUnexpandedParameterPack),
2915 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2916 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2917 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2918 RParenLoc(RParenLoc) {
2919 SubExprs[CONTROLLING] = ControllingExpr;
2920 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2921 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2922}
2923
Ted Kremenek85e92ec2007-08-24 18:13:47 +00002924//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002925// DesignatedInitExpr
2926//===----------------------------------------------------------------------===//
2927
Chandler Carruth631abd92011-06-16 06:47:06 +00002928IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00002929 assert(Kind == FieldDesignator && "Only valid on a field designator");
2930 if (Field.NameOrField & 0x01)
2931 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
2932 else
2933 return getField()->getIdentifier();
2934}
2935
Alexis Hunta8136cc2010-05-05 15:23:54 +00002936DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002937 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00002938 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00002939 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00002940 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00002941 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002942 unsigned NumIndexExprs,
2943 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00002944 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00002945 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002946 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002947 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002948 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00002949 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
2950 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00002951 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002952
2953 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00002954 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002955 *Child++ = Init;
2956
2957 // Copy the designators and their subexpressions, computing
2958 // value-dependence along the way.
2959 unsigned IndexIdx = 0;
2960 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00002961 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002962
2963 if (this->Designators[I].isArrayDesignator()) {
2964 // Compute type- and value-dependence.
2965 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002966 if (Index->isTypeDependent() || Index->isValueDependent())
2967 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00002968 if (Index->isInstantiationDependent())
2969 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00002970 // Propagate unexpanded parameter packs.
2971 if (Index->containsUnexpandedParameterPack())
2972 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002973
2974 // Copy the index expressions into permanent storage.
2975 *Child++ = IndexExprs[IndexIdx++];
2976 } else if (this->Designators[I].isArrayRangeDesignator()) {
2977 // Compute type- and value-dependence.
2978 Expr *Start = IndexExprs[IndexIdx];
2979 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00002980 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00002981 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00002982 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00002983 ExprBits.InstantiationDependent = true;
2984 } else if (Start->isInstantiationDependent() ||
2985 End->isInstantiationDependent()) {
2986 ExprBits.InstantiationDependent = true;
2987 }
2988
Douglas Gregora6e053e2010-12-15 01:34:56 +00002989 // Propagate unexpanded parameter packs.
2990 if (Start->containsUnexpandedParameterPack() ||
2991 End->containsUnexpandedParameterPack())
2992 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00002993
2994 // Copy the start/end expressions into permanent storage.
2995 *Child++ = IndexExprs[IndexIdx++];
2996 *Child++ = IndexExprs[IndexIdx++];
2997 }
2998 }
2999
3000 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003001}
3002
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003003DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00003004DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003005 unsigned NumDesignators,
3006 Expr **IndexExprs, unsigned NumIndexExprs,
3007 SourceLocation ColonOrEqualLoc,
3008 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003009 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003010 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003011 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003012 ColonOrEqualLoc, UsesColonSyntax,
3013 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003014}
3015
Mike Stump11289f42009-09-09 15:08:12 +00003016DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003017 unsigned NumIndexExprs) {
3018 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3019 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3020 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3021}
3022
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003023void DesignatedInitExpr::setDesignators(ASTContext &C,
3024 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003025 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003026 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003027 NumDesignators = NumDesigs;
3028 for (unsigned I = 0; I != NumDesigs; ++I)
3029 Designators[I] = Desigs[I];
3030}
3031
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003032SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3033 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3034 if (size() == 1)
3035 return DIE->getDesignator(0)->getSourceRange();
3036 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3037 DIE->getDesignator(size()-1)->getEndLocation());
3038}
3039
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003040SourceRange DesignatedInitExpr::getSourceRange() const {
3041 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003042 Designator &First =
3043 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003044 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003045 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003046 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3047 else
3048 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3049 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003050 StartLoc =
3051 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003052 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3053}
3054
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003055Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3056 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3057 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3058 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003059 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3060 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3061}
3062
3063Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00003064 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003065 "Requires array range designator");
3066 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3067 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003068 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3069 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3070}
3071
3072Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00003073 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003074 "Requires array range designator");
3075 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3076 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003077 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3078 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3079}
3080
Douglas Gregord5846a12009-04-15 06:41:24 +00003081/// \brief Replaces the designator at index @p Idx with the series
3082/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003083void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003084 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003085 const Designator *Last) {
3086 unsigned NumNewDesignators = Last - First;
3087 if (NumNewDesignators == 0) {
3088 std::copy_backward(Designators + Idx + 1,
3089 Designators + NumDesignators,
3090 Designators + Idx);
3091 --NumNewDesignators;
3092 return;
3093 } else if (NumNewDesignators == 1) {
3094 Designators[Idx] = *First;
3095 return;
3096 }
3097
Mike Stump11289f42009-09-09 15:08:12 +00003098 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003099 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003100 std::copy(Designators, Designators + Idx, NewDesignators);
3101 std::copy(First, Last, NewDesignators + Idx);
3102 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3103 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003104 Designators = NewDesignators;
3105 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3106}
3107
Mike Stump11289f42009-09-09 15:08:12 +00003108ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00003109 Expr **exprs, unsigned nexprs,
Manuel Klimekf2b4b692011-06-22 20:02:16 +00003110 SourceLocation rparenloc, QualType T)
3111 : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003112 false, false, false, false),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003113 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Manuel Klimekf2b4b692011-06-22 20:02:16 +00003114 assert(!T.isNull() && "ParenListExpr must have a valid type");
Nate Begeman5ec4b312009-08-10 23:49:36 +00003115 Exprs = new (C) Stmt*[nexprs];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003116 for (unsigned i = 0; i != nexprs; ++i) {
3117 if (exprs[i]->isTypeDependent())
3118 ExprBits.TypeDependent = true;
3119 if (exprs[i]->isValueDependent())
3120 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003121 if (exprs[i]->isInstantiationDependent())
3122 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003123 if (exprs[i]->containsUnexpandedParameterPack())
3124 ExprBits.ContainsUnexpandedParameterPack = true;
3125
Nate Begeman5ec4b312009-08-10 23:49:36 +00003126 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003127 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003128}
3129
John McCall1bf58462011-02-16 08:02:54 +00003130const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3131 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3132 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003133 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3134 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003135 e = cast<CXXConstructExpr>(e)->getArg(0);
3136 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3137 e = ice->getSubExpr();
3138 return cast<OpaqueValueExpr>(e);
3139}
3140
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003141//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003142// ExprIterator.
3143//===----------------------------------------------------------------------===//
3144
3145Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3146Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3147Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3148const Expr* ConstExprIterator::operator[](size_t idx) const {
3149 return cast<Expr>(I[idx]);
3150}
3151const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3152const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3153
3154//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003155// Child Iterators for iterating over subexpressions/substatements
3156//===----------------------------------------------------------------------===//
3157
Peter Collingbournee190dee2011-03-11 19:24:49 +00003158// UnaryExprOrTypeTraitExpr
3159Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003160 // If this is of a type and the type is a VLA type (and not a typedef), the
3161 // size expression of the VLA needs to be treated as an executable expression.
3162 // Why isn't this weirdness documented better in StmtIterator?
3163 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003164 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003165 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003166 return child_range(child_iterator(T), child_iterator());
3167 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003168 }
John McCallbd066782011-02-09 08:16:59 +00003169 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003170}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003171
Steve Naroffd54978b2007-09-18 23:55:05 +00003172// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00003173Stmt::child_range ObjCMessageExpr::children() {
3174 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003175 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003176 begin = reinterpret_cast<Stmt **>(this + 1);
3177 else
3178 begin = reinterpret_cast<Stmt **>(getArgs());
3179 return child_range(begin,
3180 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003181}
3182
Steve Naroffc540d662008-09-03 18:15:37 +00003183// Blocks
John McCall351762c2011-02-07 10:33:21 +00003184BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregor476e3022011-01-19 21:32:01 +00003185 SourceLocation l, bool ByRef,
John McCall351762c2011-02-07 10:33:21 +00003186 bool constAdded)
Douglas Gregor678d76c2011-07-01 01:22:09 +00003187 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregor476e3022011-01-19 21:32:01 +00003188 d->isParameterPack()),
John McCall351762c2011-02-07 10:33:21 +00003189 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregor476e3022011-01-19 21:32:01 +00003190{
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003191 bool TypeDependent = false;
3192 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003193 bool InstantiationDependent = false;
3194 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3195 InstantiationDependent);
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003196 ExprBits.TypeDependent = TypeDependent;
3197 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003198 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregor476e3022011-01-19 21:32:01 +00003199}