blob: 15cf6602b71cbc82cfe57ea006587017f10f1472 [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"
Douglas Gregor1be329d2012-02-23 07:33:15 +000021#include "clang/AST/EvaluatedExprVisitor.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000023#include "clang/AST/StmtVisitor.h"
Chris Lattnere925d612010-11-17 07:37:15 +000024#include "clang/Lex/LiteralSupport.h"
25#include "clang/Lex/Lexer.h"
Richard Smith938f40b2011-06-11 17:19:42 +000026#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000027#include "clang/Basic/Builtins.h"
Chris Lattnere925d612010-11-17 07:37:15 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000029#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000030#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000031#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000032#include <algorithm>
Eli Friedmanfcec6302011-11-01 02:23:42 +000033#include <cstring>
Chris Lattner1b926492006-08-23 06:42:10 +000034using namespace clang;
35
Rafael Espindolab7f5a9c2012-06-27 18:18:05 +000036const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindola49e860b2012-06-26 17:45:31 +000037 const Expr *E = this;
38
39 while (true) {
40 E = E->IgnoreParens();
41 if (const CastExpr *CE = dyn_cast<CastExpr>(E)) {
42 if (CE->getCastKind() == CK_DerivedToBase ||
43 CE->getCastKind() == CK_UncheckedDerivedToBase ||
44 CE->getCastKind() == CK_NoOp) {
45 E = CE->getSubExpr();
46 continue;
47 }
48 }
49
50 break;
51 }
52
53 QualType DerivedType = E->getType();
Rafael Espindola49e860b2012-06-26 17:45:31 +000054 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
55 DerivedType = PTy->getPointeeType();
56
57 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola49e860b2012-06-26 17:45:31 +000058 Decl *D = Ty->getDecl();
59 return cast<CXXRecordDecl>(D);
60}
61
Chris Lattner4ebae652010-04-16 23:34:13 +000062/// isKnownToHaveBooleanValue - Return true if this is an integer expression
63/// that is known to return 0 or 1. This happens for _Bool/bool expressions
64/// but also int expressions which are produced by things like comparisons in
65/// C.
66bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbourne91147592011-04-15 00:35:48 +000067 const Expr *E = IgnoreParens();
68
Chris Lattner4ebae652010-04-16 23:34:13 +000069 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbourne91147592011-04-15 00:35:48 +000070 if (E->getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000071 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbourne91147592011-04-15 00:35:48 +000072 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +000073
Peter Collingbourne91147592011-04-15 00:35:48 +000074 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +000075 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +000076 case UO_Plus:
Chris Lattner4ebae652010-04-16 23:34:13 +000077 return UO->getSubExpr()->isKnownToHaveBooleanValue();
78 default:
79 return false;
80 }
81 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000082
John McCall45d30c32010-06-12 01:56:02 +000083 // Only look through implicit casts. If the user writes
84 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbourne91147592011-04-15 00:35:48 +000085 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +000086 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000087
Peter Collingbourne91147592011-04-15 00:35:48 +000088 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +000089 switch (BO->getOpcode()) {
90 default: return false;
John McCalle3027922010-08-25 11:45:40 +000091 case BO_LT: // Relational operators.
92 case BO_GT:
93 case BO_LE:
94 case BO_GE:
95 case BO_EQ: // Equality operators.
96 case BO_NE:
97 case BO_LAnd: // AND operator.
98 case BO_LOr: // Logical OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +000099 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +0000100
John McCalle3027922010-08-25 11:45:40 +0000101 case BO_And: // Bitwise AND operator.
102 case BO_Xor: // Bitwise XOR operator.
103 case BO_Or: // Bitwise OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +0000104 // Handle things like (x==2)|(y==12).
105 return BO->getLHS()->isKnownToHaveBooleanValue() &&
106 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000107
John McCalle3027922010-08-25 11:45:40 +0000108 case BO_Comma:
109 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +0000110 return BO->getRHS()->isKnownToHaveBooleanValue();
111 }
112 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000113
Peter Collingbourne91147592011-04-15 00:35:48 +0000114 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +0000115 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
116 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000117
Chris Lattner4ebae652010-04-16 23:34:13 +0000118 return false;
119}
120
John McCallbd066782011-02-09 08:16:59 +0000121// Amusing macro metaprogramming hack: check whether a class provides
122// a more specific implementation of getExprLoc().
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000123//
124// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCallbd066782011-02-09 08:16:59 +0000125namespace {
126 /// This implementation is used when a class provides a custom
127 /// implementation of getExprLoc.
128 template <class E, class T>
129 SourceLocation getExprLocImpl(const Expr *expr,
130 SourceLocation (T::*v)() const) {
131 return static_cast<const E*>(expr)->getExprLoc();
132 }
133
134 /// This implementation is used when a class doesn't provide
135 /// a custom implementation of getExprLoc. Overload resolution
136 /// should pick it over the implementation above because it's
137 /// more specialized according to function template partial ordering.
138 template <class E>
139 SourceLocation getExprLocImpl(const Expr *expr,
140 SourceLocation (Expr::*v)() const) {
Daniel Dunbarb0ab5e92012-03-09 15:39:19 +0000141 return static_cast<const E*>(expr)->getLocStart();
John McCallbd066782011-02-09 08:16:59 +0000142 }
143}
144
145SourceLocation Expr::getExprLoc() const {
146 switch (getStmtClass()) {
147 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
148#define ABSTRACT_STMT(type)
149#define STMT(type, base) \
150 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
151#define EXPR(type, base) \
152 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
153#include "clang/AST/StmtNodes.inc"
154 }
155 llvm_unreachable("unknown statement kind");
John McCallbd066782011-02-09 08:16:59 +0000156}
157
Chris Lattner0eedafe2006-08-24 04:56:27 +0000158//===----------------------------------------------------------------------===//
159// Primary Expressions.
160//===----------------------------------------------------------------------===//
161
Douglas Gregor678d76c2011-07-01 01:22:09 +0000162/// \brief Compute the type-, value-, and instantiation-dependence of a
163/// declaration reference
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000164/// based on the declaration being referenced.
Daniel Dunbar9d355812012-03-09 01:51:51 +0000165static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000166 bool &TypeDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000167 bool &ValueDependent,
168 bool &InstantiationDependent) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000169 TypeDependent = false;
170 ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000171 InstantiationDependent = false;
Douglas Gregored6c7442009-11-23 11:41:28 +0000172
173 // (TD) C++ [temp.dep.expr]p3:
174 // An id-expression is type-dependent if it contains:
175 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000176 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000177 //
178 // (VD) C++ [temp.dep.constexpr]p2:
179 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000180
Douglas Gregored6c7442009-11-23 11:41:28 +0000181 // (TD) - an identifier that was declared with dependent type
182 // (VD) - a name declared with a dependent type,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000183 if (T->isDependentType()) {
184 TypeDependent = true;
185 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000186 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000187 return;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000188 } else if (T->isInstantiationDependentType()) {
189 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000190 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000191
Douglas Gregored6c7442009-11-23 11:41:28 +0000192 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000193 if (D->getDeclName().getNameKind()
Douglas Gregor678d76c2011-07-01 01:22:09 +0000194 == DeclarationName::CXXConversionFunctionName) {
195 QualType T = D->getDeclName().getCXXNameType();
196 if (T->isDependentType()) {
197 TypeDependent = true;
198 ValueDependent = true;
199 InstantiationDependent = true;
200 return;
201 }
202
203 if (T->isInstantiationDependentType())
204 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000205 }
Douglas Gregor678d76c2011-07-01 01:22:09 +0000206
Douglas Gregored6c7442009-11-23 11:41:28 +0000207 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000208 if (isa<NonTypeTemplateParmDecl>(D)) {
209 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000210 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000211 return;
212 }
213
Douglas Gregored6c7442009-11-23 11:41:28 +0000214 // (VD) - a constant with integral or enumeration type and is
215 // initialized with an expression that is value-dependent.
Richard Smithec8dcd22011-11-08 01:31:09 +0000216 // (VD) - a constant with literal type and is initialized with an
217 // expression that is value-dependent [C++11].
218 // (VD) - FIXME: Missing from the standard:
219 // - an entity with reference type and is initialized with an
220 // expression that is value-dependent [C++11]
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000221 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +0000222 if ((Ctx.getLangOpts().CPlusPlus0x ?
Richard Smithec8dcd22011-11-08 01:31:09 +0000223 Var->getType()->isLiteralType() :
224 Var->getType()->isIntegralOrEnumerationType()) &&
225 (Var->getType().getCVRQualifiers() == Qualifiers::Const ||
226 Var->getType()->isReferenceType())) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000227 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000228 if (Init->isValueDependent()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000229 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000230 InstantiationDependent = true;
231 }
Richard Smithec8dcd22011-11-08 01:31:09 +0000232 }
233
Douglas Gregor0e4de762010-05-11 08:41:30 +0000234 // (VD) - FIXME: Missing from the standard:
235 // - a member function or a static data member of the current
236 // instantiation
Richard Smithec8dcd22011-11-08 01:31:09 +0000237 if (Var->isStaticDataMember() &&
238 Var->getDeclContext()->isDependentContext()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000239 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000240 InstantiationDependent = true;
241 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000242
243 return;
244 }
245
Douglas Gregor0e4de762010-05-11 08:41:30 +0000246 // (VD) - FIXME: Missing from the standard:
247 // - a member function or a static data member of the current
248 // instantiation
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000249 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
250 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000251 InstantiationDependent = true;
Richard Smithec8dcd22011-11-08 01:31:09 +0000252 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000253}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000254
Daniel Dunbar9d355812012-03-09 01:51:51 +0000255void DeclRefExpr::computeDependence(ASTContext &Ctx) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000256 bool TypeDependent = false;
257 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000258 bool InstantiationDependent = false;
Daniel Dunbar9d355812012-03-09 01:51:51 +0000259 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
260 ValueDependent, InstantiationDependent);
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000261
262 // (TD) C++ [temp.dep.expr]p3:
263 // An id-expression is type-dependent if it contains:
264 //
265 // and
266 //
267 // (VD) C++ [temp.dep.constexpr]p2:
268 // An identifier is value-dependent if it is:
269 if (!TypeDependent && !ValueDependent &&
270 hasExplicitTemplateArgs() &&
271 TemplateSpecializationType::anyDependentTemplateArguments(
272 getTemplateArgs(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000273 getNumTemplateArgs(),
274 InstantiationDependent)) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000275 TypeDependent = true;
276 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000277 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000278 }
279
280 ExprBits.TypeDependent = TypeDependent;
281 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000282 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000283
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000284 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000285 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000286 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000287}
288
Daniel Dunbar9d355812012-03-09 01:51:51 +0000289DeclRefExpr::DeclRefExpr(ASTContext &Ctx,
290 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000291 SourceLocation TemplateKWLoc,
John McCall113bee02012-03-10 09:33:50 +0000292 ValueDecl *D, bool RefersToEnclosingLocal,
293 const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000294 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000295 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000296 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000297 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000298 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
299 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000300 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000301 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000302 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
303 if (FoundD)
304 getInternalFoundDecl() = FoundD;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000305 DeclRefExprBits.HasTemplateKWAndArgsInfo
306 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCall113bee02012-03-10 09:33:50 +0000307 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000308 if (TemplateArgs) {
309 bool Dependent = false;
310 bool InstantiationDependent = false;
311 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000312 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
313 Dependent,
314 InstantiationDependent,
315 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000316 if (InstantiationDependent)
317 setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000318 } else if (TemplateKWLoc.isValid()) {
319 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000320 }
Benjamin Kramer138ef9c2011-10-10 12:54:05 +0000321 DeclRefExprBits.HadMultipleCandidates = 0;
322
Daniel Dunbar9d355812012-03-09 01:51:51 +0000323 computeDependence(Ctx);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000324}
325
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000326DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000327 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000328 SourceLocation TemplateKWLoc,
John McCallce546572009-12-08 09:08:17 +0000329 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000330 bool RefersToEnclosingLocal,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000331 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000332 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000333 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000334 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000335 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000336 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000337 RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000338 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000339 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000340}
341
342DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000343 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000344 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000345 ValueDecl *D,
John McCall113bee02012-03-10 09:33:50 +0000346 bool RefersToEnclosingLocal,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000347 const DeclarationNameInfo &NameInfo,
348 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000349 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000350 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000351 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000352 // Filter out cases where the found Decl is the same as the value refenenced.
353 if (D == FoundD)
354 FoundD = 0;
355
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000356 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregorea972d32011-02-28 21:54:11 +0000357 if (QualifierLoc != 0)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000358 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000359 if (FoundD)
360 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000361 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000362 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
363 else if (TemplateKWLoc.isValid())
364 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000365
Chris Lattner5c0b4052010-10-30 05:14:06 +0000366 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar9d355812012-03-09 01:51:51 +0000367 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCall113bee02012-03-10 09:33:50 +0000368 RefersToEnclosingLocal,
Daniel Dunbar9d355812012-03-09 01:51:51 +0000369 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000370}
371
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000372DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000373 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000374 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000375 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000376 unsigned NumTemplateArgs) {
377 std::size_t Size = sizeof(DeclRefExpr);
378 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000379 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000380 if (HasFoundDecl)
381 Size += sizeof(NamedDecl *);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000382 if (HasTemplateKWAndArgsInfo)
383 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000384
Chris Lattner5c0b4052010-10-30 05:14:06 +0000385 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000386 return new (Mem) DeclRefExpr(EmptyShell());
387}
388
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000389SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000390 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000391 if (hasQualifier())
Douglas Gregorea972d32011-02-28 21:54:11 +0000392 R.setBegin(getQualifierLoc().getBeginLoc());
John McCallb3774b52010-08-19 23:49:38 +0000393 if (hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000394 R.setEnd(getRAngleLoc());
395 return R;
396}
Daniel Dunbarb507f272012-03-09 15:39:15 +0000397SourceLocation DeclRefExpr::getLocStart() const {
398 if (hasQualifier())
399 return getQualifierLoc().getBeginLoc();
400 return getNameInfo().getLocStart();
401}
402SourceLocation DeclRefExpr::getLocEnd() const {
403 if (hasExplicitTemplateArgs())
404 return getRAngleLoc();
405 return getNameInfo().getLocEnd();
406}
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000407
Anders Carlsson2fb08242009-09-08 18:24:21 +0000408// FIXME: Maybe this should use DeclPrinter with a special "print predefined
409// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000410std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
411 ASTContext &Context = CurrentDecl->getASTContext();
412
Anders Carlsson2fb08242009-09-08 18:24:21 +0000413 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000414 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000415 return FD->getNameAsString();
416
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000417 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000418 llvm::raw_svector_ostream Out(Name);
419
420 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000421 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000422 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000423 if (MD->isStatic())
424 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000425 }
426
David Blaikiebbafb8a2012-03-11 07:00:24 +0000427 PrintingPolicy Policy(Context.getLangOpts());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000428 std::string Proto = FD->getQualifiedNameAsString(Policy);
Douglas Gregor11a434a2012-04-10 20:14:15 +0000429 llvm::raw_string_ostream POut(Proto);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000430
Douglas Gregor11a434a2012-04-10 20:14:15 +0000431 const FunctionDecl *Decl = FD;
432 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
433 Decl = Pattern;
434 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000435 const FunctionProtoType *FT = 0;
436 if (FD->hasWrittenPrototype())
437 FT = dyn_cast<FunctionProtoType>(AFT);
438
Douglas Gregor11a434a2012-04-10 20:14:15 +0000439 POut << "(";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000440 if (FT) {
Douglas Gregor11a434a2012-04-10 20:14:15 +0000441 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson2fb08242009-09-08 18:24:21 +0000442 if (i) POut << ", ";
Argyrios Kyrtzidisa18347e2012-05-05 04:20:37 +0000443 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000444 }
445
446 if (FT->isVariadic()) {
447 if (FD->getNumParams()) POut << ", ";
448 POut << "...";
449 }
450 }
Douglas Gregor11a434a2012-04-10 20:14:15 +0000451 POut << ")";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000452
Sam Weinig4e83bd22009-12-27 01:38:20 +0000453 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
454 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
455 if (ThisQuals.hasConst())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000456 POut << " const";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000457 if (ThisQuals.hasVolatile())
Douglas Gregor11a434a2012-04-10 20:14:15 +0000458 POut << " volatile";
459 RefQualifierKind Ref = MD->getRefQualifier();
460 if (Ref == RQ_LValue)
461 POut << " &";
462 else if (Ref == RQ_RValue)
463 POut << " &&";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000464 }
465
Douglas Gregor11a434a2012-04-10 20:14:15 +0000466 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
467 SpecsTy Specs;
468 const DeclContext *Ctx = FD->getDeclContext();
469 while (Ctx && isa<NamedDecl>(Ctx)) {
470 const ClassTemplateSpecializationDecl *Spec
471 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
472 if (Spec && !Spec->isExplicitSpecialization())
473 Specs.push_back(Spec);
474 Ctx = Ctx->getParent();
475 }
476
477 std::string TemplateParams;
478 llvm::raw_string_ostream TOut(TemplateParams);
479 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
480 I != E; ++I) {
481 const TemplateParameterList *Params
482 = (*I)->getSpecializedTemplate()->getTemplateParameters();
483 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
484 assert(Params->size() == Args.size());
485 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
486 StringRef Param = Params->getParam(i)->getName();
487 if (Param.empty()) continue;
488 TOut << Param << " = ";
489 Args.get(i).print(Policy, TOut);
490 TOut << ", ";
491 }
492 }
493
494 FunctionTemplateSpecializationInfo *FSI
495 = FD->getTemplateSpecializationInfo();
496 if (FSI && !FSI->isExplicitSpecialization()) {
497 const TemplateParameterList* Params
498 = FSI->getTemplate()->getTemplateParameters();
499 const TemplateArgumentList* Args = FSI->TemplateArguments;
500 assert(Params->size() == Args->size());
501 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
502 StringRef Param = Params->getParam(i)->getName();
503 if (Param.empty()) continue;
504 TOut << Param << " = ";
505 Args->get(i).print(Policy, TOut);
506 TOut << ", ";
507 }
508 }
509
510 TOut.flush();
511 if (!TemplateParams.empty()) {
512 // remove the trailing comma and space
513 TemplateParams.resize(TemplateParams.size() - 2);
514 POut << " [" << TemplateParams << "]";
515 }
516
517 POut.flush();
518
Sam Weinigd060ed42009-12-06 23:55:13 +0000519 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
520 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000521
522 Out << Proto;
523
524 Out.flush();
525 return Name.str().str();
526 }
527 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000528 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000529 llvm::raw_svector_ostream Out(Name);
530 Out << (MD->isInstanceMethod() ? '-' : '+');
531 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000532
533 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
534 // a null check to avoid a crash.
535 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000536 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000537
Anders Carlsson2fb08242009-09-08 18:24:21 +0000538 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000539 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000540 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000541
Anders Carlsson2fb08242009-09-08 18:24:21 +0000542 Out << ' ';
543 Out << MD->getSelector().getAsString();
544 Out << ']';
545
546 Out.flush();
547 return Name.str().str();
548 }
549 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
550 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
551 return "top level";
552 }
553 return "";
554}
555
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000556void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
557 if (hasAllocation())
558 C.Deallocate(pVal);
559
560 BitWidth = Val.getBitWidth();
561 unsigned NumWords = Val.getNumWords();
562 const uint64_t* Words = Val.getRawData();
563 if (NumWords > 1) {
564 pVal = new (C) uint64_t[NumWords];
565 std::copy(Words, Words + NumWords, pVal);
566 } else if (NumWords == 1)
567 VAL = Words[0];
568 else
569 VAL = 0;
570}
571
572IntegerLiteral *
573IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
574 QualType type, SourceLocation l) {
575 return new (C) IntegerLiteral(C, V, type, l);
576}
577
578IntegerLiteral *
579IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
580 return new (C) IntegerLiteral(Empty);
581}
582
583FloatingLiteral *
584FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
585 bool isexact, QualType Type, SourceLocation L) {
586 return new (C) FloatingLiteral(C, V, isexact, Type, L);
587}
588
589FloatingLiteral *
590FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000591 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000592}
593
Chris Lattnera0173132008-06-07 22:13:43 +0000594/// getValueAsApproximateDouble - This returns the value as an inaccurate
595/// double. Note that this may cause loss of precision, but is useful for
596/// debugging dumps, etc.
597double FloatingLiteral::getValueAsApproximateDouble() const {
598 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000599 bool ignored;
600 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
601 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000602 return V.convertToDouble();
603}
604
Nick Lewycky4ed84042012-02-24 09:07:53 +0000605int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedman381f4312012-02-29 20:59:56 +0000606 int CharByteWidth = 0;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000607 switch(k) {
Eli Friedmanfcec6302011-11-01 02:23:42 +0000608 case Ascii:
609 case UTF8:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000610 CharByteWidth = target.getCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000611 break;
612 case Wide:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000613 CharByteWidth = target.getWCharWidth();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000614 break;
615 case UTF16:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000616 CharByteWidth = target.getChar16Width();
Eli Friedmanfcec6302011-11-01 02:23:42 +0000617 break;
618 case UTF32:
Nick Lewycky4ed84042012-02-24 09:07:53 +0000619 CharByteWidth = target.getChar32Width();
Eli Friedman381f4312012-02-29 20:59:56 +0000620 break;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000621 }
622 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
623 CharByteWidth /= 8;
Nick Lewycky4ed84042012-02-24 09:07:53 +0000624 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedmanfcec6302011-11-01 02:23:42 +0000625 && "character byte widths supported are 1, 2, and 4 only");
626 return CharByteWidth;
627}
628
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000629StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000630 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000631 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000632 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000633 // Allocate enough space for the StringLiteral plus an array of locations for
634 // any concatenated string tokens.
635 void *Mem = C.Allocate(sizeof(StringLiteral)+
636 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000637 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000638 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000639
Steve Naroffdf7855b2007-02-21 23:46:25 +0000640 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000641 SL->setString(C,Str,Kind,Pascal);
642
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000643 SL->TokLocs[0] = Loc[0];
644 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000645
Chris Lattner630970d2009-02-18 05:49:11 +0000646 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000647 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
648 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000649}
650
Douglas Gregor958dfc92009-04-15 16:35:07 +0000651StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
652 void *Mem = C.Allocate(sizeof(StringLiteral)+
653 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000654 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000655 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000656 SL->CharByteWidth = 0;
657 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000658 SL->NumConcatenated = NumStrs;
659 return SL;
660}
661
Richard Trieudc355912012-06-13 20:25:24 +0000662void StringLiteral::outputString(raw_ostream &OS) {
663 switch (getKind()) {
664 case Ascii: break; // no prefix.
665 case Wide: OS << 'L'; break;
666 case UTF8: OS << "u8"; break;
667 case UTF16: OS << 'u'; break;
668 case UTF32: OS << 'U'; break;
669 }
670 OS << '"';
671 static const char Hex[] = "0123456789ABCDEF";
672
673 unsigned LastSlashX = getLength();
674 for (unsigned I = 0, N = getLength(); I != N; ++I) {
675 switch (uint32_t Char = getCodeUnit(I)) {
676 default:
677 // FIXME: Convert UTF-8 back to codepoints before rendering.
678
679 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
680 // Leave invalid surrogates alone; we'll use \x for those.
681 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
682 Char <= 0xdbff) {
683 uint32_t Trail = getCodeUnit(I + 1);
684 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
685 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
686 ++I;
687 }
688 }
689
690 if (Char > 0xff) {
691 // If this is a wide string, output characters over 0xff using \x
692 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
693 // codepoint: use \x escapes for invalid codepoints.
694 if (getKind() == Wide ||
695 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
696 // FIXME: Is this the best way to print wchar_t?
697 OS << "\\x";
698 int Shift = 28;
699 while ((Char >> Shift) == 0)
700 Shift -= 4;
701 for (/**/; Shift >= 0; Shift -= 4)
702 OS << Hex[(Char >> Shift) & 15];
703 LastSlashX = I;
704 break;
705 }
706
707 if (Char > 0xffff)
708 OS << "\\U00"
709 << Hex[(Char >> 20) & 15]
710 << Hex[(Char >> 16) & 15];
711 else
712 OS << "\\u";
713 OS << Hex[(Char >> 12) & 15]
714 << Hex[(Char >> 8) & 15]
715 << Hex[(Char >> 4) & 15]
716 << Hex[(Char >> 0) & 15];
717 break;
718 }
719
720 // If we used \x... for the previous character, and this character is a
721 // hexadecimal digit, prevent it being slurped as part of the \x.
722 if (LastSlashX + 1 == I) {
723 switch (Char) {
724 case '0': case '1': case '2': case '3': case '4':
725 case '5': case '6': case '7': case '8': case '9':
726 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
727 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
728 OS << "\"\"";
729 }
730 }
731
732 assert(Char <= 0xff &&
733 "Characters above 0xff should already have been handled.");
734
735 if (isprint(Char))
736 OS << (char)Char;
737 else // Output anything hard as an octal escape.
738 OS << '\\'
739 << (char)('0' + ((Char >> 6) & 7))
740 << (char)('0' + ((Char >> 3) & 7))
741 << (char)('0' + ((Char >> 0) & 7));
742 break;
743 // Handle some common non-printable cases to make dumps prettier.
744 case '\\': OS << "\\\\"; break;
745 case '"': OS << "\\\""; break;
746 case '\n': OS << "\\n"; break;
747 case '\t': OS << "\\t"; break;
748 case '\a': OS << "\\a"; break;
749 case '\b': OS << "\\b"; break;
750 }
751 }
752 OS << '"';
753}
754
Eli Friedmanfcec6302011-11-01 02:23:42 +0000755void StringLiteral::setString(ASTContext &C, StringRef Str,
756 StringKind Kind, bool IsPascal) {
757 //FIXME: we assume that the string data comes from a target that uses the same
758 // code unit size and endianess for the type of string.
759 this->Kind = Kind;
760 this->IsPascal = IsPascal;
761
Nick Lewycky4ed84042012-02-24 09:07:53 +0000762 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedmanfcec6302011-11-01 02:23:42 +0000763 assert((Str.size()%CharByteWidth == 0)
764 && "size of data must be multiple of CharByteWidth");
765 Length = Str.size()/CharByteWidth;
766
767 switch(CharByteWidth) {
768 case 1: {
769 char *AStrData = new (C) char[Length];
770 std::memcpy(AStrData,Str.data(),Str.size());
771 StrData.asChar = AStrData;
772 break;
773 }
774 case 2: {
775 uint16_t *AStrData = new (C) uint16_t[Length];
776 std::memcpy(AStrData,Str.data(),Str.size());
777 StrData.asUInt16 = AStrData;
778 break;
779 }
780 case 4: {
781 uint32_t *AStrData = new (C) uint32_t[Length];
782 std::memcpy(AStrData,Str.data(),Str.size());
783 StrData.asUInt32 = AStrData;
784 break;
785 }
786 default:
787 assert(false && "unsupported CharByteWidth");
788 }
Douglas Gregor958dfc92009-04-15 16:35:07 +0000789}
790
Chris Lattnere925d612010-11-17 07:37:15 +0000791/// getLocationOfByte - Return a source location that points to the specified
792/// byte of this string literal.
793///
794/// Strings are amazingly complex. They can be formed from multiple tokens and
795/// can have escape sequences in them in addition to the usual trigraph and
796/// escaped newline business. This routine handles this complexity.
797///
798SourceLocation StringLiteral::
799getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
800 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smith4060f772012-06-13 05:37:23 +0000801 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
802 "Only narrow string literals are currently supported");
Douglas Gregorfb65e592011-07-27 05:40:30 +0000803
Chris Lattnere925d612010-11-17 07:37:15 +0000804 // Loop over all of the tokens in this string until we find the one that
805 // contains the byte we're looking for.
806 unsigned TokNo = 0;
807 while (1) {
808 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
809 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
810
811 // Get the spelling of the string so that we can get the data that makes up
812 // the string literal, not the identifier for the macro it is potentially
813 // expanded through.
814 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
815
816 // Re-lex the token to get its length and original spelling.
817 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
818 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000819 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +0000820 if (Invalid)
821 return StrTokSpellingLoc;
822
823 const char *StrData = Buffer.data()+LocInfo.second;
824
Chris Lattnere925d612010-11-17 07:37:15 +0000825 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidis45f51182012-05-11 21:39:18 +0000826 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
827 Buffer.begin(), StrData, Buffer.end());
Chris Lattnere925d612010-11-17 07:37:15 +0000828 Token TheTok;
829 TheLexer.LexFromRawLexer(TheTok);
830
831 // Use the StringLiteralParser to compute the length of the string in bytes.
832 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
833 unsigned TokNumBytes = SLP.GetStringLength();
834
835 // If the byte is in this token, return the location of the byte.
836 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +0000837 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +0000838 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
839
840 // Now that we know the offset of the token in the spelling, use the
841 // preprocessor to get the offset in the original source.
842 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
843 }
844
845 // Move to the next string token.
846 ++TokNo;
847 ByteNo -= TokNumBytes;
848 }
849}
850
851
852
Chris Lattner1b926492006-08-23 06:42:10 +0000853/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
854/// corresponds to, e.g. "sizeof" or "[pre]++".
855const char *UnaryOperator::getOpcodeStr(Opcode Op) {
856 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +0000857 case UO_PostInc: return "++";
858 case UO_PostDec: return "--";
859 case UO_PreInc: return "++";
860 case UO_PreDec: return "--";
861 case UO_AddrOf: return "&";
862 case UO_Deref: return "*";
863 case UO_Plus: return "+";
864 case UO_Minus: return "-";
865 case UO_Not: return "~";
866 case UO_LNot: return "!";
867 case UO_Real: return "__real";
868 case UO_Imag: return "__imag";
869 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000870 }
David Blaikief47fa302012-01-17 02:30:50 +0000871 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +0000872}
873
John McCalle3027922010-08-25 11:45:40 +0000874UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000875UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
876 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +0000877 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000878 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
879 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
880 case OO_Amp: return UO_AddrOf;
881 case OO_Star: return UO_Deref;
882 case OO_Plus: return UO_Plus;
883 case OO_Minus: return UO_Minus;
884 case OO_Tilde: return UO_Not;
885 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000886 }
887}
888
889OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
890 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000891 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
892 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
893 case UO_AddrOf: return OO_Amp;
894 case UO_Deref: return OO_Star;
895 case UO_Plus: return OO_Plus;
896 case UO_Minus: return OO_Minus;
897 case UO_Not: return OO_Tilde;
898 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000899 default: return OO_None;
900 }
901}
902
903
Chris Lattner0eedafe2006-08-24 04:56:27 +0000904//===----------------------------------------------------------------------===//
905// Postfix Operators.
906//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000907
Peter Collingbourne3a347252011-02-08 21:18:02 +0000908CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
909 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCall7decc9e2010-11-18 06:31:45 +0000910 SourceLocation rparenloc)
911 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000912 fn->isTypeDependent(),
913 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000914 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000915 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000916 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000917
Peter Collingbourne3a347252011-02-08 21:18:02 +0000918 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +0000919 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000920 for (unsigned i = 0; i != numargs; ++i) {
921 if (args[i]->isTypeDependent())
922 ExprBits.TypeDependent = true;
923 if (args[i]->isValueDependent())
924 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000925 if (args[i]->isInstantiationDependent())
926 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000927 if (args[i]->containsUnexpandedParameterPack())
928 ExprBits.ContainsUnexpandedParameterPack = true;
929
Peter Collingbourne3a347252011-02-08 21:18:02 +0000930 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000931 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000932
Peter Collingbourne3a347252011-02-08 21:18:02 +0000933 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +0000934 RParenLoc = rparenloc;
935}
Nate Begeman1e36a852008-01-17 17:46:27 +0000936
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000937CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCall7decc9e2010-11-18 06:31:45 +0000938 QualType t, ExprValueKind VK, SourceLocation rparenloc)
939 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000940 fn->isTypeDependent(),
941 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000942 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000943 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000944 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000945
Peter Collingbourne3a347252011-02-08 21:18:02 +0000946 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000947 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000948 for (unsigned i = 0; i != numargs; ++i) {
949 if (args[i]->isTypeDependent())
950 ExprBits.TypeDependent = true;
951 if (args[i]->isValueDependent())
952 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000953 if (args[i]->isInstantiationDependent())
954 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000955 if (args[i]->containsUnexpandedParameterPack())
956 ExprBits.ContainsUnexpandedParameterPack = true;
957
Peter Collingbourne3a347252011-02-08 21:18:02 +0000958 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000959 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000960
Peter Collingbourne3a347252011-02-08 21:18:02 +0000961 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000962 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000963}
964
Mike Stump11289f42009-09-09 15:08:12 +0000965CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
966 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000967 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +0000968 SubExprs = new (C) Stmt*[PREARGS_START];
969 CallExprBits.NumPreArgs = 0;
970}
971
972CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
973 EmptyShell Empty)
974 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
975 // FIXME: Why do we allocate this?
976 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
977 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +0000978}
979
Nuno Lopes518e3702009-12-20 23:11:08 +0000980Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +0000981 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +0000982
983 while (SubstNonTypeTemplateParmExpr *NTTP
984 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
985 CEE = NTTP->getReplacement()->IgnoreParenCasts();
986 }
987
Sebastian Redl2b1832e2010-09-10 20:55:30 +0000988 // If we're calling a dereference, look at the pointer instead.
989 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
990 if (BO->isPtrMemOp())
991 CEE = BO->getRHS()->IgnoreParenCasts();
992 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
993 if (UO->getOpcode() == UO_Deref)
994 CEE = UO->getSubExpr()->IgnoreParenCasts();
995 }
Chris Lattner52301912009-07-17 15:46:27 +0000996 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000997 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000998 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
999 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +00001000
1001 return 0;
1002}
1003
Nuno Lopes518e3702009-12-20 23:11:08 +00001004FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +00001005 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +00001006}
1007
Chris Lattnere4407ed2007-12-28 05:25:02 +00001008/// setNumArgs - This changes the number of arguments present in this call.
1009/// Any orphaned expressions are deleted by this, and any new operands are set
1010/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +00001011void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001012 // No change, just return.
1013 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001014
Chris Lattnere4407ed2007-12-28 05:25:02 +00001015 // If shrinking # arguments, just delete the extras and forgot them.
1016 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +00001017 this->NumArgs = NumArgs;
1018 return;
1019 }
1020
1021 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001022 unsigned NumPreArgs = getNumPreArgs();
1023 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +00001024 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001025 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001026 NewSubExprs[i] = SubExprs[i];
1027 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +00001028 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1029 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +00001030 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001031
Douglas Gregorba6e5572009-04-17 21:46:47 +00001032 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +00001033 SubExprs = NewSubExprs;
1034 this->NumArgs = NumArgs;
1035}
1036
Chris Lattner01ff98a2008-10-06 05:00:53 +00001037/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1038/// not, return 0.
Richard Smithd62306a2011-11-10 06:34:14 +00001039unsigned CallExpr::isBuiltinCall() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +00001040 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +00001041 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +00001042 // ImplicitCastExpr.
1043 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1044 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +00001045 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001046
Steve Narofff6e3b3292008-01-31 01:07:12 +00001047 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1048 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001049 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001050
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001051 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1052 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +00001053 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001054
Douglas Gregor9eb16ea2008-11-21 15:30:19 +00001055 if (!FDecl->getIdentifier())
1056 return 0;
1057
Douglas Gregor15fc9562009-09-12 00:22:50 +00001058 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +00001059}
Anders Carlssonfbcf6762008-01-31 02:13:57 +00001060
Anders Carlsson00a27592009-05-26 04:57:27 +00001061QualType CallExpr::getCallReturnType() const {
1062 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001063 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001064 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001065 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +00001066 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +00001067 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1068 // This should never be overloaded and so should never return null.
1069 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +00001070
John McCall0009fcc2011-04-26 20:42:42 +00001071 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +00001072 return FnType->getResultType();
1073}
Chris Lattner01ff98a2008-10-06 05:00:53 +00001074
John McCall701417a2011-02-21 06:23:05 +00001075SourceRange CallExpr::getSourceRange() const {
1076 if (isa<CXXOperatorCallExpr>(this))
1077 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
1078
1079 SourceLocation begin = getCallee()->getLocStart();
1080 if (begin.isInvalid() && getNumArgs() > 0)
1081 begin = getArg(0)->getLocStart();
1082 SourceLocation end = getRParenLoc();
1083 if (end.isInvalid() && getNumArgs() > 0)
1084 end = getArg(getNumArgs() - 1)->getLocEnd();
1085 return SourceRange(begin, end);
1086}
Daniel Dunbarcdf295c2012-03-09 15:39:24 +00001087SourceLocation CallExpr::getLocStart() const {
1088 if (isa<CXXOperatorCallExpr>(this))
1089 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getBegin();
1090
1091 SourceLocation begin = getCallee()->getLocStart();
1092 if (begin.isInvalid() && getNumArgs() > 0)
1093 begin = getArg(0)->getLocStart();
1094 return begin;
1095}
1096SourceLocation CallExpr::getLocEnd() const {
1097 if (isa<CXXOperatorCallExpr>(this))
1098 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getEnd();
1099
1100 SourceLocation end = getRParenLoc();
1101 if (end.isInvalid() && getNumArgs() > 0)
1102 end = getArg(getNumArgs() - 1)->getLocEnd();
1103 return end;
1104}
John McCall701417a2011-02-21 06:23:05 +00001105
Alexis Hunta8136cc2010-05-05 15:23:54 +00001106OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001107 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001108 TypeSourceInfo *tsi,
1109 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +00001110 Expr** exprsPtr, unsigned numExprs,
1111 SourceLocation RParenLoc) {
1112 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +00001113 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +00001114 sizeof(Expr*) * numExprs);
1115
1116 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
1117 exprsPtr, numExprs, RParenLoc);
1118}
1119
1120OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
1121 unsigned numComps, unsigned numExprs) {
1122 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1123 sizeof(OffsetOfNode) * numComps +
1124 sizeof(Expr*) * numExprs);
1125 return new (Mem) OffsetOfExpr(numComps, numExprs);
1126}
1127
Alexis Hunta8136cc2010-05-05 15:23:54 +00001128OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +00001129 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +00001130 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +00001131 Expr** exprsPtr, unsigned numExprs,
1132 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +00001133 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1134 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001135 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001136 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001137 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +00001138 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
1139 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +00001140{
1141 for(unsigned i = 0; i < numComps; ++i) {
1142 setComponent(i, compsPtr[i]);
1143 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001144
Douglas Gregor882211c2010-04-28 22:16:22 +00001145 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001146 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
1147 ExprBits.ValueDependent = true;
1148 if (exprsPtr[i]->containsUnexpandedParameterPack())
1149 ExprBits.ContainsUnexpandedParameterPack = true;
1150
Douglas Gregor882211c2010-04-28 22:16:22 +00001151 setIndexExpr(i, exprsPtr[i]);
1152 }
1153}
1154
1155IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1156 assert(getKind() == Field || getKind() == Identifier);
1157 if (getKind() == Field)
1158 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001159
Douglas Gregor882211c2010-04-28 22:16:22 +00001160 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1161}
1162
Mike Stump11289f42009-09-09 15:08:12 +00001163MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +00001164 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001165 SourceLocation TemplateKWLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +00001166 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +00001167 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001168 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +00001169 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +00001170 QualType ty,
1171 ExprValueKind vk,
1172 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001173 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +00001174
Douglas Gregorea972d32011-02-28 21:54:11 +00001175 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +00001176 founddecl.getDecl() != memberdecl ||
1177 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +00001178 if (hasQualOrFound)
1179 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +00001180
John McCall6b51f282009-11-23 01:53:49 +00001181 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001182 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1183 else if (TemplateKWLoc.isValid())
1184 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +00001185
Chris Lattner5c0b4052010-10-30 05:14:06 +00001186 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +00001187 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1188 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +00001189
1190 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +00001191 // FIXME: Wrong. We should be looking at the member declaration we found.
1192 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +00001193 E->setValueDependent(true);
1194 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001195 E->setInstantiationDependent(true);
1196 }
1197 else if (QualifierLoc &&
1198 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1199 E->setInstantiationDependent(true);
1200
John McCall16df1e52010-03-30 21:47:33 +00001201 E->HasQualifierOrFoundDecl = true;
1202
1203 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +00001204 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +00001205 NQ->FoundDecl = founddecl;
1206 }
1207
Abramo Bagnara7945c982012-01-27 09:46:47 +00001208 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1209
John McCall16df1e52010-03-30 21:47:33 +00001210 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +00001211 bool Dependent = false;
1212 bool InstantiationDependent = false;
1213 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001214 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1215 Dependent,
1216 InstantiationDependent,
1217 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001218 if (InstantiationDependent)
1219 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001220 } else if (TemplateKWLoc.isValid()) {
1221 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001222 }
1223
1224 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001225}
1226
Douglas Gregor25b7e052011-03-02 21:06:53 +00001227SourceRange MemberExpr::getSourceRange() const {
Daniel Dunbarb507f272012-03-09 15:39:15 +00001228 return SourceRange(getLocStart(), getLocEnd());
1229}
1230SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor25b7e052011-03-02 21:06:53 +00001231 if (isImplicitAccess()) {
1232 if (hasQualifier())
Daniel Dunbarb507f272012-03-09 15:39:15 +00001233 return getQualifierLoc().getBeginLoc();
1234 return MemberLoc;
Douglas Gregor25b7e052011-03-02 21:06:53 +00001235 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001236
Daniel Dunbarb507f272012-03-09 15:39:15 +00001237 // FIXME: We don't want this to happen. Rather, we should be able to
1238 // detect all kinds of implicit accesses more cleanly.
1239 SourceLocation BaseStartLoc = getBase()->getLocStart();
1240 if (BaseStartLoc.isValid())
1241 return BaseStartLoc;
1242 return MemberLoc;
1243}
1244SourceLocation MemberExpr::getLocEnd() const {
1245 if (hasExplicitTemplateArgs())
1246 return getRAngleLoc();
1247 return getMemberNameInfo().getEndLoc();
Douglas Gregor25b7e052011-03-02 21:06:53 +00001248}
1249
John McCall9320b872011-09-09 05:25:32 +00001250void CastExpr::CheckCastConsistency() const {
1251 switch (getCastKind()) {
1252 case CK_DerivedToBase:
1253 case CK_UncheckedDerivedToBase:
1254 case CK_DerivedToBaseMemberPointer:
1255 case CK_BaseToDerived:
1256 case CK_BaseToDerivedMemberPointer:
1257 assert(!path_empty() && "Cast kind should have a base path!");
1258 break;
1259
1260 case CK_CPointerToObjCPointerCast:
1261 assert(getType()->isObjCObjectPointerType());
1262 assert(getSubExpr()->getType()->isPointerType());
1263 goto CheckNoBasePath;
1264
1265 case CK_BlockPointerToObjCPointerCast:
1266 assert(getType()->isObjCObjectPointerType());
1267 assert(getSubExpr()->getType()->isBlockPointerType());
1268 goto CheckNoBasePath;
1269
John McCallc62bb392012-02-15 01:22:51 +00001270 case CK_ReinterpretMemberPointer:
1271 assert(getType()->isMemberPointerType());
1272 assert(getSubExpr()->getType()->isMemberPointerType());
1273 goto CheckNoBasePath;
1274
John McCall9320b872011-09-09 05:25:32 +00001275 case CK_BitCast:
1276 // Arbitrary casts to C pointer types count as bitcasts.
1277 // Otherwise, we should only have block and ObjC pointer casts
1278 // here if they stay within the type kind.
1279 if (!getType()->isPointerType()) {
1280 assert(getType()->isObjCObjectPointerType() ==
1281 getSubExpr()->getType()->isObjCObjectPointerType());
1282 assert(getType()->isBlockPointerType() ==
1283 getSubExpr()->getType()->isBlockPointerType());
1284 }
1285 goto CheckNoBasePath;
1286
1287 case CK_AnyPointerToBlockPointerCast:
1288 assert(getType()->isBlockPointerType());
1289 assert(getSubExpr()->getType()->isAnyPointerType() &&
1290 !getSubExpr()->getType()->isBlockPointerType());
1291 goto CheckNoBasePath;
1292
Douglas Gregored90df32012-02-22 05:02:47 +00001293 case CK_CopyAndAutoreleaseBlockObject:
1294 assert(getType()->isBlockPointerType());
1295 assert(getSubExpr()->getType()->isBlockPointerType());
1296 goto CheckNoBasePath;
1297
John McCall9320b872011-09-09 05:25:32 +00001298 // These should not have an inheritance path.
1299 case CK_Dynamic:
1300 case CK_ToUnion:
1301 case CK_ArrayToPointerDecay:
1302 case CK_FunctionToPointerDecay:
1303 case CK_NullToMemberPointer:
1304 case CK_NullToPointer:
1305 case CK_ConstructorConversion:
1306 case CK_IntegralToPointer:
1307 case CK_PointerToIntegral:
1308 case CK_ToVoid:
1309 case CK_VectorSplat:
1310 case CK_IntegralCast:
1311 case CK_IntegralToFloating:
1312 case CK_FloatingToIntegral:
1313 case CK_FloatingCast:
1314 case CK_ObjCObjectLValueCast:
1315 case CK_FloatingRealToComplex:
1316 case CK_FloatingComplexToReal:
1317 case CK_FloatingComplexCast:
1318 case CK_FloatingComplexToIntegralComplex:
1319 case CK_IntegralRealToComplex:
1320 case CK_IntegralComplexToReal:
1321 case CK_IntegralComplexCast:
1322 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001323 case CK_ARCProduceObject:
1324 case CK_ARCConsumeObject:
1325 case CK_ARCReclaimReturnedObject:
1326 case CK_ARCExtendBlockObject:
John McCall9320b872011-09-09 05:25:32 +00001327 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1328 goto CheckNoBasePath;
1329
1330 case CK_Dependent:
1331 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001332 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001333 case CK_AtomicToNonAtomic:
1334 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001335 case CK_PointerToBoolean:
1336 case CK_IntegralToBoolean:
1337 case CK_FloatingToBoolean:
1338 case CK_MemberPointerToBoolean:
1339 case CK_FloatingComplexToBoolean:
1340 case CK_IntegralComplexToBoolean:
1341 case CK_LValueBitCast: // -> bool&
1342 case CK_UserDefinedConversion: // operator bool()
1343 CheckNoBasePath:
1344 assert(path_empty() && "Cast kind should not have a base path!");
1345 break;
1346 }
1347}
1348
Anders Carlsson496335e2009-09-03 00:59:21 +00001349const char *CastExpr::getCastKindName() const {
1350 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001351 case CK_Dependent:
1352 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001353 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001354 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001355 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001356 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001357 case CK_LValueToRValue:
1358 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001359 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001360 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001361 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001362 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001363 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001364 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001365 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001366 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001367 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001368 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001369 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001370 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001371 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001372 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001373 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001374 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001375 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001376 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001377 case CK_NullToPointer:
1378 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001379 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001380 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001381 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001382 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001383 case CK_ReinterpretMemberPointer:
1384 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001385 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001386 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001387 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001388 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001389 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001390 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001391 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001392 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001393 case CK_PointerToBoolean:
1394 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001395 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001396 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001397 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001398 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001399 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001400 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001401 case CK_IntegralToBoolean:
1402 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001403 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001404 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001405 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001406 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001407 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001408 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001409 case CK_FloatingToBoolean:
1410 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001411 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001412 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001413 case CK_CPointerToObjCPointerCast:
1414 return "CPointerToObjCPointerCast";
1415 case CK_BlockPointerToObjCPointerCast:
1416 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001417 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001418 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001419 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001420 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001421 case CK_FloatingRealToComplex:
1422 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001423 case CK_FloatingComplexToReal:
1424 return "FloatingComplexToReal";
1425 case CK_FloatingComplexToBoolean:
1426 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001427 case CK_FloatingComplexCast:
1428 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001429 case CK_FloatingComplexToIntegralComplex:
1430 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001431 case CK_IntegralRealToComplex:
1432 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001433 case CK_IntegralComplexToReal:
1434 return "IntegralComplexToReal";
1435 case CK_IntegralComplexToBoolean:
1436 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001437 case CK_IntegralComplexCast:
1438 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001439 case CK_IntegralComplexToFloatingComplex:
1440 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001441 case CK_ARCConsumeObject:
1442 return "ARCConsumeObject";
1443 case CK_ARCProduceObject:
1444 return "ARCProduceObject";
1445 case CK_ARCReclaimReturnedObject:
1446 return "ARCReclaimReturnedObject";
1447 case CK_ARCExtendBlockObject:
1448 return "ARCCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001449 case CK_AtomicToNonAtomic:
1450 return "AtomicToNonAtomic";
1451 case CK_NonAtomicToAtomic:
1452 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001453 case CK_CopyAndAutoreleaseBlockObject:
1454 return "CopyAndAutoreleaseBlockObject";
Anders Carlsson496335e2009-09-03 00:59:21 +00001455 }
Mike Stump11289f42009-09-09 15:08:12 +00001456
John McCallc5e62b42010-11-13 09:02:35 +00001457 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001458}
1459
Douglas Gregord196a582009-12-14 19:27:10 +00001460Expr *CastExpr::getSubExprAsWritten() {
1461 Expr *SubExpr = 0;
1462 CastExpr *E = this;
1463 do {
1464 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001465
1466 // Skip through reference binding to temporary.
1467 if (MaterializeTemporaryExpr *Materialize
1468 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1469 SubExpr = Materialize->GetTemporaryExpr();
1470
Douglas Gregord196a582009-12-14 19:27:10 +00001471 // Skip any temporary bindings; they're implicit.
1472 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1473 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001474
Douglas Gregord196a582009-12-14 19:27:10 +00001475 // Conversions by constructor and conversion functions have a
1476 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001477 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001478 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001479 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001480 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001481
Douglas Gregord196a582009-12-14 19:27:10 +00001482 // If the subexpression we're left with is an implicit cast, look
1483 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001484 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1485
Douglas Gregord196a582009-12-14 19:27:10 +00001486 return SubExpr;
1487}
1488
John McCallcf142162010-08-07 06:22:56 +00001489CXXBaseSpecifier **CastExpr::path_buffer() {
1490 switch (getStmtClass()) {
1491#define ABSTRACT_STMT(x)
1492#define CASTEXPR(Type, Base) \
1493 case Stmt::Type##Class: \
1494 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1495#define STMT(Type, Base)
1496#include "clang/AST/StmtNodes.inc"
1497 default:
1498 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001499 }
1500}
1501
1502void CastExpr::setCastPath(const CXXCastPath &Path) {
1503 assert(Path.size() == path_size());
1504 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1505}
1506
1507ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1508 CastKind Kind, Expr *Operand,
1509 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001510 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001511 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1512 void *Buffer =
1513 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1514 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001515 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001516 if (PathSize) E->setCastPath(*BasePath);
1517 return E;
1518}
1519
1520ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1521 unsigned PathSize) {
1522 void *Buffer =
1523 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1524 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1525}
1526
1527
1528CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001529 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001530 const CXXCastPath *BasePath,
1531 TypeSourceInfo *WrittenTy,
1532 SourceLocation L, SourceLocation R) {
1533 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1534 void *Buffer =
1535 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1536 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001537 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001538 if (PathSize) E->setCastPath(*BasePath);
1539 return E;
1540}
1541
1542CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1543 void *Buffer =
1544 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1545 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1546}
1547
Chris Lattner1b926492006-08-23 06:42:10 +00001548/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1549/// corresponds to, e.g. "<<=".
1550const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1551 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001552 case BO_PtrMemD: return ".*";
1553 case BO_PtrMemI: return "->*";
1554 case BO_Mul: return "*";
1555 case BO_Div: return "/";
1556 case BO_Rem: return "%";
1557 case BO_Add: return "+";
1558 case BO_Sub: return "-";
1559 case BO_Shl: return "<<";
1560 case BO_Shr: return ">>";
1561 case BO_LT: return "<";
1562 case BO_GT: return ">";
1563 case BO_LE: return "<=";
1564 case BO_GE: return ">=";
1565 case BO_EQ: return "==";
1566 case BO_NE: return "!=";
1567 case BO_And: return "&";
1568 case BO_Xor: return "^";
1569 case BO_Or: return "|";
1570 case BO_LAnd: return "&&";
1571 case BO_LOr: return "||";
1572 case BO_Assign: return "=";
1573 case BO_MulAssign: return "*=";
1574 case BO_DivAssign: return "/=";
1575 case BO_RemAssign: return "%=";
1576 case BO_AddAssign: return "+=";
1577 case BO_SubAssign: return "-=";
1578 case BO_ShlAssign: return "<<=";
1579 case BO_ShrAssign: return ">>=";
1580 case BO_AndAssign: return "&=";
1581 case BO_XorAssign: return "^=";
1582 case BO_OrAssign: return "|=";
1583 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001584 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001585
David Blaikiee4d798f2012-01-20 21:50:17 +00001586 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001587}
Steve Naroff47500512007-04-19 23:00:49 +00001588
John McCalle3027922010-08-25 11:45:40 +00001589BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001590BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1591 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001592 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001593 case OO_Plus: return BO_Add;
1594 case OO_Minus: return BO_Sub;
1595 case OO_Star: return BO_Mul;
1596 case OO_Slash: return BO_Div;
1597 case OO_Percent: return BO_Rem;
1598 case OO_Caret: return BO_Xor;
1599 case OO_Amp: return BO_And;
1600 case OO_Pipe: return BO_Or;
1601 case OO_Equal: return BO_Assign;
1602 case OO_Less: return BO_LT;
1603 case OO_Greater: return BO_GT;
1604 case OO_PlusEqual: return BO_AddAssign;
1605 case OO_MinusEqual: return BO_SubAssign;
1606 case OO_StarEqual: return BO_MulAssign;
1607 case OO_SlashEqual: return BO_DivAssign;
1608 case OO_PercentEqual: return BO_RemAssign;
1609 case OO_CaretEqual: return BO_XorAssign;
1610 case OO_AmpEqual: return BO_AndAssign;
1611 case OO_PipeEqual: return BO_OrAssign;
1612 case OO_LessLess: return BO_Shl;
1613 case OO_GreaterGreater: return BO_Shr;
1614 case OO_LessLessEqual: return BO_ShlAssign;
1615 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1616 case OO_EqualEqual: return BO_EQ;
1617 case OO_ExclaimEqual: return BO_NE;
1618 case OO_LessEqual: return BO_LE;
1619 case OO_GreaterEqual: return BO_GE;
1620 case OO_AmpAmp: return BO_LAnd;
1621 case OO_PipePipe: return BO_LOr;
1622 case OO_Comma: return BO_Comma;
1623 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001624 }
1625}
1626
1627OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1628 static const OverloadedOperatorKind OverOps[] = {
1629 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1630 OO_Star, OO_Slash, OO_Percent,
1631 OO_Plus, OO_Minus,
1632 OO_LessLess, OO_GreaterGreater,
1633 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1634 OO_EqualEqual, OO_ExclaimEqual,
1635 OO_Amp,
1636 OO_Caret,
1637 OO_Pipe,
1638 OO_AmpAmp,
1639 OO_PipePipe,
1640 OO_Equal, OO_StarEqual,
1641 OO_SlashEqual, OO_PercentEqual,
1642 OO_PlusEqual, OO_MinusEqual,
1643 OO_LessLessEqual, OO_GreaterGreaterEqual,
1644 OO_AmpEqual, OO_CaretEqual,
1645 OO_PipeEqual,
1646 OO_Comma
1647 };
1648 return OverOps[Opc];
1649}
1650
Ted Kremenekac034612010-04-13 23:39:13 +00001651InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +00001652 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001653 SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001654 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001655 false, false),
Ted Kremenekac034612010-04-13 23:39:13 +00001656 InitExprs(C, numInits),
Sebastian Redlc83ed822012-02-17 08:42:25 +00001657 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0)
1658{
1659 sawArrayRangeDesignator(false);
1660 setInitializesStdInitializerList(false);
Ted Kremenek013041e2010-02-19 01:50:18 +00001661 for (unsigned I = 0; I != numInits; ++I) {
1662 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001663 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001664 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001665 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001666 if (initExprs[I]->isInstantiationDependent())
1667 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001668 if (initExprs[I]->containsUnexpandedParameterPack())
1669 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001670 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001671
Ted Kremenekac034612010-04-13 23:39:13 +00001672 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001673}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001674
Ted Kremenekac034612010-04-13 23:39:13 +00001675void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001676 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001677 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001678}
1679
Ted Kremenekac034612010-04-13 23:39:13 +00001680void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001681 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001682}
1683
Ted Kremenekac034612010-04-13 23:39:13 +00001684Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001685 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001686 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001687 InitExprs.back() = expr;
1688 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001689 }
Mike Stump11289f42009-09-09 15:08:12 +00001690
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001691 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1692 InitExprs[Init] = expr;
1693 return Result;
1694}
1695
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001696void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001697 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001698 ArrayFillerOrUnionFieldInit = filler;
1699 // Fill out any "holes" in the array due to designated initializers.
1700 Expr **inits = getInits();
1701 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1702 if (inits[i] == 0)
1703 inits[i] = filler;
1704}
1705
Richard Smith9ec1e482012-04-15 02:50:59 +00001706bool InitListExpr::isStringLiteralInit() const {
1707 if (getNumInits() != 1)
1708 return false;
1709 const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(getType());
1710 if (!CAT || !CAT->getElementType()->isIntegerType())
1711 return false;
1712 const Expr *Init = getInit(0)->IgnoreParenImpCasts();
1713 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1714}
1715
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001716SourceRange InitListExpr::getSourceRange() const {
1717 if (SyntacticForm)
1718 return SyntacticForm->getSourceRange();
1719 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1720 if (Beg.isInvalid()) {
1721 // Find the first non-null initializer.
1722 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1723 E = InitExprs.end();
1724 I != E; ++I) {
1725 if (Stmt *S = *I) {
1726 Beg = S->getLocStart();
1727 break;
1728 }
1729 }
1730 }
1731 if (End.isInvalid()) {
1732 // Find the first non-null initializer from the end.
1733 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1734 E = InitExprs.rend();
1735 I != E; ++I) {
1736 if (Stmt *S = *I) {
1737 End = S->getSourceRange().getEnd();
1738 break;
1739 }
1740 }
1741 }
1742 return SourceRange(Beg, End);
1743}
1744
Steve Naroff991e99d2008-09-04 15:31:07 +00001745/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001746///
John McCallc833dea2012-02-17 03:32:35 +00001747const FunctionProtoType *BlockExpr::getFunctionType() const {
1748 // The block pointer is never sugared, but the function type might be.
1749 return cast<BlockPointerType>(getType())
1750 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001751}
1752
Mike Stump11289f42009-09-09 15:08:12 +00001753SourceLocation BlockExpr::getCaretLocation() const {
1754 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001755}
Mike Stump11289f42009-09-09 15:08:12 +00001756const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001757 return TheBlock->getBody();
1758}
Mike Stump11289f42009-09-09 15:08:12 +00001759Stmt *BlockExpr::getBody() {
1760 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001761}
Steve Naroff415d3d52008-10-08 17:01:13 +00001762
1763
Chris Lattner1ec5f562007-06-27 05:38:08 +00001764//===----------------------------------------------------------------------===//
1765// Generic Expression Routines
1766//===----------------------------------------------------------------------===//
1767
Chris Lattner237f2752009-02-14 07:37:35 +00001768/// isUnusedResultAWarning - Return true if this immediate expression should
1769/// be warned about if the result is unused. If so, fill in Loc and Ranges
1770/// with location to warn on and the source range[s] to report with the
1771/// warning.
Eli Friedmanc11535c2012-05-24 00:47:05 +00001772bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1773 SourceRange &R1, SourceRange &R2,
1774 ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001775 // Don't warn if the expr is type dependent. The type could end up
1776 // instantiating to void.
1777 if (isTypeDependent())
1778 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001779
Chris Lattner1ec5f562007-06-27 05:38:08 +00001780 switch (getStmtClass()) {
1781 default:
John McCallc493a732010-03-12 07:11:26 +00001782 if (getType()->isVoidType())
1783 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001784 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001785 Loc = getExprLoc();
1786 R1 = getSourceRange();
1787 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001788 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001789 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001790 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001791 case GenericSelectionExprClass:
1792 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmanc11535c2012-05-24 00:47:05 +00001793 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001794 case UnaryOperatorClass: {
1795 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001796
Chris Lattner1ec5f562007-06-27 05:38:08 +00001797 switch (UO->getOpcode()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001798 case UO_Plus:
1799 case UO_Minus:
1800 case UO_AddrOf:
1801 case UO_Not:
1802 case UO_LNot:
1803 case UO_Deref:
1804 break;
John McCalle3027922010-08-25 11:45:40 +00001805 case UO_PostInc:
1806 case UO_PostDec:
1807 case UO_PreInc:
1808 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001809 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001810 case UO_Real:
1811 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001812 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001813 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1814 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001815 return false;
1816 break;
John McCalle3027922010-08-25 11:45:40 +00001817 case UO_Extension:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001818 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001819 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00001820 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001821 Loc = UO->getOperatorLoc();
1822 R1 = UO->getSubExpr()->getSourceRange();
1823 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001824 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001825 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001826 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001827 switch (BO->getOpcode()) {
1828 default:
1829 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001830 // Consider the RHS of comma for side effects. LHS was checked by
1831 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001832 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001833 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1834 // lvalue-ness) of an assignment written in a macro.
1835 if (IntegerLiteral *IE =
1836 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1837 if (IE->getValue() == 0)
1838 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001839 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001840 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001841 case BO_LAnd:
1842 case BO_LOr:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001843 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1844 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001845 return false;
1846 break;
John McCall1e3715a2010-02-16 04:10:53 +00001847 }
Chris Lattner237f2752009-02-14 07:37:35 +00001848 if (BO->isAssignmentOp())
1849 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001850 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001851 Loc = BO->getOperatorLoc();
1852 R1 = BO->getLHS()->getSourceRange();
1853 R2 = BO->getRHS()->getSourceRange();
1854 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001855 }
Chris Lattner86928112007-08-25 02:00:02 +00001856 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001857 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001858 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001859 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001860
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001861 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00001862 // If only one of the LHS or RHS is a warning, the operator might
1863 // be being used for control flow. Only warn if both the LHS and
1864 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001865 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00001866 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremeneke96dad92011-03-01 20:34:48 +00001867 return false;
1868 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00001869 return true;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001870 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001871 }
1872
Chris Lattnera44d1162007-06-27 05:58:59 +00001873 case MemberExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001874 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001875 Loc = cast<MemberExpr>(this)->getMemberLoc();
1876 R1 = SourceRange(Loc, Loc);
1877 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1878 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001879
Chris Lattner1ec5f562007-06-27 05:38:08 +00001880 case ArraySubscriptExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001881 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001882 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1883 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1884 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1885 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001886
Chandler Carruth46339472011-08-17 09:49:44 +00001887 case CXXOperatorCallExprClass: {
1888 // We warn about operator== and operator!= even when user-defined operator
1889 // overloads as there is no reasonable way to define these such that they
1890 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1891 // warning: these operators are commonly typo'ed, and so warning on them
1892 // provides additional value as well. If this list is updated,
1893 // DiagnoseUnusedComparison should be as well.
1894 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1895 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00001896 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001897 WarnE = this;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00001898 Loc = Op->getOperatorLoc();
1899 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00001900 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00001901 }
Chandler Carruth46339472011-08-17 09:49:44 +00001902
1903 // Fallthrough for generic call handling.
1904 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001905 case CallExprClass:
Richard Smithc67fdd42012-03-07 08:35:16 +00001906 case CXXMemberCallExprClass:
1907 case UserDefinedLiteralClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001908 // If this is a direct call, get the callee.
1909 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001910 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001911 // If the callee has attribute pure, const, or warn_unused_result, warn
1912 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001913 //
1914 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1915 // updated to match for QoI.
1916 if (FD->getAttr<WarnUnusedResultAttr>() ||
1917 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001918 WarnE = this;
Chris Lattner1a6babf2009-10-13 04:53:48 +00001919 Loc = CE->getCallee()->getLocStart();
1920 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001921
Chris Lattner1a6babf2009-10-13 04:53:48 +00001922 if (unsigned NumArgs = CE->getNumArgs())
1923 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1924 CE->getArg(NumArgs-1)->getLocEnd());
1925 return true;
1926 }
Chris Lattner237f2752009-02-14 07:37:35 +00001927 }
1928 return false;
1929 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001930
1931 case CXXTemporaryObjectExprClass:
1932 case CXXConstructExprClass:
1933 return false;
1934
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001935 case ObjCMessageExprClass: {
1936 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikiebbafb8a2012-03-11 07:00:24 +00001937 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCall31168b02011-06-15 23:02:42 +00001938 ME->isInstanceMessage() &&
1939 !ME->getType()->isVoidType() &&
1940 ME->getSelector().getIdentifierInfoForSlot(0) &&
1941 ME->getSelector().getIdentifierInfoForSlot(0)
1942 ->getName().startswith("init")) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001943 WarnE = this;
John McCall31168b02011-06-15 23:02:42 +00001944 Loc = getExprLoc();
1945 R1 = ME->getSourceRange();
1946 return true;
1947 }
1948
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001949 const ObjCMethodDecl *MD = ME->getMethodDecl();
1950 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmanc11535c2012-05-24 00:47:05 +00001951 WarnE = this;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001952 Loc = getExprLoc();
1953 return true;
1954 }
Chris Lattner237f2752009-02-14 07:37:35 +00001955 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001956 }
Mike Stump11289f42009-09-09 15:08:12 +00001957
John McCallb7bd14f2010-12-02 01:19:52 +00001958 case ObjCPropertyRefExprClass:
Eli Friedmanc11535c2012-05-24 00:47:05 +00001959 WarnE = this;
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001960 Loc = getExprLoc();
1961 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001962 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00001963
John McCallfe96e0b2011-11-06 09:01:30 +00001964 case PseudoObjectExprClass: {
1965 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1966
1967 // Only complain about things that have the form of a getter.
1968 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1969 isa<BinaryOperator>(PO->getSyntacticForm()))
1970 return false;
1971
Eli Friedmanc11535c2012-05-24 00:47:05 +00001972 WarnE = this;
John McCallfe96e0b2011-11-06 09:01:30 +00001973 Loc = getExprLoc();
1974 R1 = getSourceRange();
1975 return true;
1976 }
1977
Chris Lattner944d3062008-07-26 19:51:01 +00001978 case StmtExprClass: {
1979 // Statement exprs don't logically have side effects themselves, but are
1980 // sometimes used in macros in ways that give them a type that is unused.
1981 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1982 // however, if the result of the stmt expr is dead, we don't want to emit a
1983 // warning.
1984 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001985 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00001986 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00001987 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001988 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1989 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmanc11535c2012-05-24 00:47:05 +00001990 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001991 }
Mike Stump11289f42009-09-09 15:08:12 +00001992
John McCallc493a732010-03-12 07:11:26 +00001993 if (getType()->isVoidType())
1994 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00001995 WarnE = this;
Chris Lattner237f2752009-02-14 07:37:35 +00001996 Loc = cast<StmtExpr>(this)->getLParenLoc();
1997 R1 = getSourceRange();
1998 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001999 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002000 case CStyleCastExprClass: {
Eli Friedmanf92f6452012-05-24 21:05:41 +00002001 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmanc11535c2012-05-24 00:47:05 +00002002 // volatile lvalue.
Eli Friedmanf92f6452012-05-24 21:05:41 +00002003 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmanc11535c2012-05-24 00:47:05 +00002004 if (CE->getCastKind() == CK_ToVoid) {
2005 if (CE->getSubExpr()->isGLValue() &&
Eli Friedmanf92f6452012-05-24 21:05:41 +00002006 CE->getSubExpr()->getType().isVolatileQualified()) {
2007 const DeclRefExpr *DRE =
2008 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2009 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2010 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2011 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2012 R1, R2, Ctx);
2013 }
2014 }
Chris Lattner2706a552009-07-28 18:25:28 +00002015 return false;
Eli Friedmanc11535c2012-05-24 00:47:05 +00002016 }
Eli Friedmanf92f6452012-05-24 21:05:41 +00002017
Eli Friedmanc11535c2012-05-24 00:47:05 +00002018 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson6aa50392009-11-17 17:11:23 +00002019 // Otherwise, the result of the cast is unused.
Eli Friedmanc11535c2012-05-24 00:47:05 +00002020 if (CE->getCastKind() == CK_ConstructorConversion)
2021 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedmanf92f6452012-05-24 21:05:41 +00002022
Eli Friedmanc11535c2012-05-24 00:47:05 +00002023 WarnE = this;
Eli Friedmanf92f6452012-05-24 21:05:41 +00002024 if (const CXXFunctionalCastExpr *CXXCE =
2025 dyn_cast<CXXFunctionalCastExpr>(this)) {
2026 Loc = CXXCE->getTypeBeginLoc();
2027 R1 = CXXCE->getSubExpr()->getSourceRange();
2028 } else {
2029 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2030 Loc = CStyleCE->getLParenLoc();
2031 R1 = CStyleCE->getSubExpr()->getSourceRange();
2032 }
Chris Lattner237f2752009-02-14 07:37:35 +00002033 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00002034 }
Eli Friedmanc11535c2012-05-24 00:47:05 +00002035 case ImplicitCastExprClass: {
2036 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedmanca8da1d2008-05-19 21:24:43 +00002037
Eli Friedmanc11535c2012-05-24 00:47:05 +00002038 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2039 if (ICE->getCastKind() == CK_LValueToRValue &&
2040 ICE->getSubExpr()->getType().isVolatileQualified())
2041 return false;
2042
2043 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2044 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002045 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002046 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002047 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002048
2049 case CXXNewExprClass:
2050 // FIXME: In theory, there might be new expressions that don't have side
2051 // effects (e.g. a placement new with an uninitialized POD).
2052 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00002053 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00002054 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00002055 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002056 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00002057 case ExprWithCleanupsClass:
2058 return (cast<ExprWithCleanups>(this)
Eli Friedmanc11535c2012-05-24 00:47:05 +00002059 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00002060 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00002061}
2062
Fariborz Jahanian07735332009-02-22 18:40:18 +00002063/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00002064/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002065bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00002066 const Expr *E = IgnoreParens();
2067 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00002068 default:
2069 return false;
2070 case ObjCIvarRefExprClass:
2071 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00002072 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002073 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002074 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002075 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00002076 case MaterializeTemporaryExprClass:
2077 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2078 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00002079 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002080 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00002081 case DeclRefExprClass: {
John McCall113bee02012-03-10 09:33:50 +00002082 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00002083
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002084 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2085 if (VD->hasGlobalStorage())
2086 return true;
2087 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00002088 // dereferencing to a pointer is always a gc'able candidate,
2089 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00002090 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00002091 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002092 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00002093 return false;
2094 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00002095 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00002096 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00002097 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002098 }
2099 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00002100 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00002101 }
2102}
Sebastian Redlce354af2010-09-10 20:55:33 +00002103
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002104bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2105 if (isTypeDependent())
2106 return false;
John McCall086a4642010-11-24 05:12:34 +00002107 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00002108}
2109
John McCall0009fcc2011-04-26 20:42:42 +00002110QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00002111 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00002112
2113 // Bound member expressions are always one of these possibilities:
2114 // x->m x.m x->*y x.*y
2115 // (possibly parenthesized)
2116
2117 expr = expr->IgnoreParens();
2118 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2119 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2120 return mem->getMemberDecl()->getType();
2121 }
2122
2123 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2124 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2125 ->getPointeeType();
2126 assert(type->isFunctionType());
2127 return type;
2128 }
2129
2130 assert(isa<UnresolvedMemberExpr>(expr));
2131 return QualType();
2132}
2133
Ted Kremenekfff70962008-01-17 16:57:34 +00002134Expr* Expr::IgnoreParens() {
2135 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002136 while (true) {
2137 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2138 E = P->getSubExpr();
2139 continue;
2140 }
2141 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2142 if (P->getOpcode() == UO_Extension) {
2143 E = P->getSubExpr();
2144 continue;
2145 }
2146 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002147 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2148 if (!P->isResultDependent()) {
2149 E = P->getResultExpr();
2150 continue;
2151 }
2152 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002153 return E;
2154 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002155}
2156
Chris Lattnerf2660962008-02-13 01:02:39 +00002157/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2158/// or CastExprs or ImplicitCastExprs, returning their operand.
2159Expr *Expr::IgnoreParenCasts() {
2160 Expr *E = this;
2161 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00002162 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002163 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002164 continue;
2165 }
2166 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002167 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002168 continue;
2169 }
2170 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2171 if (P->getOpcode() == UO_Extension) {
2172 E = P->getSubExpr();
2173 continue;
2174 }
2175 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002176 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2177 if (!P->isResultDependent()) {
2178 E = P->getResultExpr();
2179 continue;
2180 }
2181 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002182 if (MaterializeTemporaryExpr *Materialize
2183 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2184 E = Materialize->GetTemporaryExpr();
2185 continue;
2186 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002187 if (SubstNonTypeTemplateParmExpr *NTTP
2188 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2189 E = NTTP->getReplacement();
2190 continue;
2191 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002192 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002193 }
2194}
2195
John McCall5a4ce8b2010-12-04 08:24:19 +00002196/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2197/// casts. This is intended purely as a temporary workaround for code
2198/// that hasn't yet been rewritten to do the right thing about those
2199/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002200Expr *Expr::IgnoreParenLValueCasts() {
2201 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002202 while (true) {
John McCall34376a62010-12-04 03:47:34 +00002203 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2204 E = P->getSubExpr();
2205 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00002206 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002207 if (P->getCastKind() == CK_LValueToRValue) {
2208 E = P->getSubExpr();
2209 continue;
2210 }
John McCall5a4ce8b2010-12-04 08:24:19 +00002211 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2212 if (P->getOpcode() == UO_Extension) {
2213 E = P->getSubExpr();
2214 continue;
2215 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002216 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2217 if (!P->isResultDependent()) {
2218 E = P->getResultExpr();
2219 continue;
2220 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002221 } else if (MaterializeTemporaryExpr *Materialize
2222 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2223 E = Materialize->GetTemporaryExpr();
2224 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002225 } else if (SubstNonTypeTemplateParmExpr *NTTP
2226 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2227 E = NTTP->getReplacement();
2228 continue;
John McCall34376a62010-12-04 03:47:34 +00002229 }
2230 break;
2231 }
2232 return E;
2233}
2234
John McCalleebc8322010-05-05 22:59:52 +00002235Expr *Expr::IgnoreParenImpCasts() {
2236 Expr *E = this;
2237 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00002238 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002239 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002240 continue;
2241 }
2242 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002243 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002244 continue;
2245 }
2246 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2247 if (P->getOpcode() == UO_Extension) {
2248 E = P->getSubExpr();
2249 continue;
2250 }
2251 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002252 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2253 if (!P->isResultDependent()) {
2254 E = P->getResultExpr();
2255 continue;
2256 }
2257 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002258 if (MaterializeTemporaryExpr *Materialize
2259 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2260 E = Materialize->GetTemporaryExpr();
2261 continue;
2262 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002263 if (SubstNonTypeTemplateParmExpr *NTTP
2264 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2265 E = NTTP->getReplacement();
2266 continue;
2267 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002268 return E;
John McCalleebc8322010-05-05 22:59:52 +00002269 }
2270}
2271
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002272Expr *Expr::IgnoreConversionOperator() {
2273 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002274 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002275 return MCE->getImplicitObjectArgument();
2276 }
2277 return this;
2278}
2279
Chris Lattneref26c772009-03-13 17:28:01 +00002280/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2281/// value (including ptr->int casts of the same size). Strip off any
2282/// ParenExpr or CastExprs, returning their operand.
2283Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2284 Expr *E = this;
2285 while (true) {
2286 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2287 E = P->getSubExpr();
2288 continue;
2289 }
Mike Stump11289f42009-09-09 15:08:12 +00002290
Chris Lattneref26c772009-03-13 17:28:01 +00002291 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2292 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002293 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002294 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002295
Chris Lattneref26c772009-03-13 17:28:01 +00002296 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2297 E = SE;
2298 continue;
2299 }
Mike Stump11289f42009-09-09 15:08:12 +00002300
Abramo Bagnara932e3932010-10-15 07:51:18 +00002301 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002302 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002303 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002304 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002305 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2306 E = SE;
2307 continue;
2308 }
2309 }
Mike Stump11289f42009-09-09 15:08:12 +00002310
Abramo Bagnara932e3932010-10-15 07:51:18 +00002311 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2312 if (P->getOpcode() == UO_Extension) {
2313 E = P->getSubExpr();
2314 continue;
2315 }
2316 }
2317
Peter Collingbourne91147592011-04-15 00:35:48 +00002318 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2319 if (!P->isResultDependent()) {
2320 E = P->getResultExpr();
2321 continue;
2322 }
2323 }
2324
Douglas Gregor6a40b082011-09-08 17:56:33 +00002325 if (SubstNonTypeTemplateParmExpr *NTTP
2326 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2327 E = NTTP->getReplacement();
2328 continue;
2329 }
2330
Chris Lattneref26c772009-03-13 17:28:01 +00002331 return E;
2332 }
2333}
2334
Douglas Gregord196a582009-12-14 19:27:10 +00002335bool Expr::isDefaultArgument() const {
2336 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002337 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2338 E = M->GetTemporaryExpr();
2339
Douglas Gregord196a582009-12-14 19:27:10 +00002340 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2341 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002342
Douglas Gregord196a582009-12-14 19:27:10 +00002343 return isa<CXXDefaultArgExpr>(E);
2344}
Chris Lattneref26c772009-03-13 17:28:01 +00002345
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002346/// \brief Skip over any no-op casts and any temporary-binding
2347/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002348static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002349 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2350 E = M->GetTemporaryExpr();
2351
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002352 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002353 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002354 E = ICE->getSubExpr();
2355 else
2356 break;
2357 }
2358
2359 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2360 E = BE->getSubExpr();
2361
2362 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002363 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002364 E = ICE->getSubExpr();
2365 else
2366 break;
2367 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002368
2369 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002370}
2371
John McCall7a626f62010-09-15 10:14:12 +00002372/// isTemporaryObject - Determines if this expression produces a
2373/// temporary of the given class type.
2374bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2375 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2376 return false;
2377
Anders Carlsson66bbf502010-11-28 16:40:49 +00002378 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002379
John McCall02dc8c72010-09-15 20:59:13 +00002380 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002381 if (!E->Classify(C).isPRValue()) {
2382 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002383 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002384 return false;
2385 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002386
John McCallf4ee1dd2010-09-16 06:57:56 +00002387 // Black-list a few cases which yield pr-values of class type that don't
2388 // refer to temporaries of that type:
2389
2390 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002391 if (isa<ImplicitCastExpr>(E)) {
2392 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2393 case CK_DerivedToBase:
2394 case CK_UncheckedDerivedToBase:
2395 return false;
2396 default:
2397 break;
2398 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002399 }
2400
John McCallf4ee1dd2010-09-16 06:57:56 +00002401 // - member expressions (all)
2402 if (isa<MemberExpr>(E))
2403 return false;
2404
Eli Friedman13ffdd82012-06-15 23:51:06 +00002405 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2406 if (BO->isPtrMemOp())
2407 return false;
2408
John McCallc07a0c72011-02-17 10:25:35 +00002409 // - opaque values (all)
2410 if (isa<OpaqueValueExpr>(E))
2411 return false;
2412
John McCall7a626f62010-09-15 10:14:12 +00002413 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002414}
2415
Douglas Gregor25b7e052011-03-02 21:06:53 +00002416bool Expr::isImplicitCXXThis() const {
2417 const Expr *E = this;
2418
2419 // Strip away parentheses and casts we don't care about.
2420 while (true) {
2421 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2422 E = Paren->getSubExpr();
2423 continue;
2424 }
2425
2426 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2427 if (ICE->getCastKind() == CK_NoOp ||
2428 ICE->getCastKind() == CK_LValueToRValue ||
2429 ICE->getCastKind() == CK_DerivedToBase ||
2430 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2431 E = ICE->getSubExpr();
2432 continue;
2433 }
2434 }
2435
2436 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2437 if (UnOp->getOpcode() == UO_Extension) {
2438 E = UnOp->getSubExpr();
2439 continue;
2440 }
2441 }
2442
Douglas Gregorfe314812011-06-21 17:03:29 +00002443 if (const MaterializeTemporaryExpr *M
2444 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2445 E = M->GetTemporaryExpr();
2446 continue;
2447 }
2448
Douglas Gregor25b7e052011-03-02 21:06:53 +00002449 break;
2450 }
2451
2452 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2453 return This->isImplicit();
2454
2455 return false;
2456}
2457
Douglas Gregor4619e432008-12-05 23:32:09 +00002458/// hasAnyTypeDependentArguments - Determines if any of the expressions
2459/// in Exprs is type-dependent.
Ahmed Charlesb24b9aa2012-02-25 11:00:22 +00002460bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2461 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor4619e432008-12-05 23:32:09 +00002462 if (Exprs[I]->isTypeDependent())
2463 return true;
2464
2465 return false;
2466}
2467
John McCall8b0f4ff2010-08-02 21:13:48 +00002468bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002469 // This function is attempting whether an expression is an initializer
2470 // which can be evaluated at compile-time. isEvaluatable handles most
2471 // of the cases, but it can't deal with some initializer-specific
2472 // expressions, and it can't deal with aggregates; we deal with those here,
2473 // and fall back to isEvaluatable for the other cases.
2474
John McCall8b0f4ff2010-08-02 21:13:48 +00002475 // If we ever capture reference-binding directly in the AST, we can
2476 // kill the second parameter.
2477
2478 if (IsForRef) {
2479 EvalResult Result;
2480 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2481 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002482
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002483 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002484 default: break;
Richard Smith941aae02011-12-09 06:47:34 +00002485 case IntegerLiteralClass:
2486 case FloatingLiteralClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002487 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002488 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002489 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002490 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002491 case CXXTemporaryObjectExprClass:
2492 case CXXConstructExprClass: {
2493 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002494
2495 // Only if it's
Richard Smithd62306a2011-11-10 06:34:14 +00002496 if (CE->getConstructor()->isTrivial()) {
2497 // 1) an application of the trivial default constructor or
2498 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002499
Richard Smithd62306a2011-11-10 06:34:14 +00002500 // 2) an elidable trivial copy construction of an operand which is
2501 // itself a constant initializer. Note that we consider the
2502 // operand on its own, *not* as a reference binding.
2503 if (CE->isElidable() &&
2504 CE->getArg(0)->isConstantInitializer(Ctx, false))
2505 return true;
2506 }
2507
2508 // 3) a foldable constexpr constructor.
2509 break;
John McCall81c9cea2010-08-01 21:51:45 +00002510 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002511 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002512 // This handles gcc's extension that allows global initializers like
2513 // "struct x {int x;} x = (struct x) {};".
2514 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002515 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002516 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002517 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002518 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002519 // FIXME: This doesn't deal with fields with reference types correctly.
2520 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2521 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002522 const InitListExpr *Exp = cast<InitListExpr>(this);
2523 unsigned numInits = Exp->getNumInits();
2524 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002525 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002526 return false;
2527 }
Eli Friedman384da272009-01-25 03:12:18 +00002528 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002529 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002530 case ImplicitValueInitExprClass:
2531 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002532 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002533 return cast<ParenExpr>(this)->getSubExpr()
2534 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002535 case GenericSelectionExprClass:
2536 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2537 return false;
2538 return cast<GenericSelectionExpr>(this)->getResultExpr()
2539 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002540 case ChooseExprClass:
2541 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2542 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002543 case UnaryOperatorClass: {
2544 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002545 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002546 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002547 break;
2548 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002549 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002550 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002551 case ImplicitCastExprClass:
Richard Smith161f09a2011-12-06 22:44:34 +00002552 case CStyleCastExprClass: {
2553 const CastExpr *CE = cast<CastExpr>(this);
2554
David Chisnallfa35df62012-01-16 17:27:18 +00002555 // If we're promoting an integer to an _Atomic type then this is constant
2556 // if the integer is constant. We also need to check the converse in case
2557 // someone does something like:
2558 //
2559 // int a = (_Atomic(int))42;
2560 //
2561 // I doubt anyone would write code like this directly, but it's quite
2562 // possible as the result of macro expansions.
2563 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2564 CE->getCastKind() == CK_AtomicToNonAtomic)
2565 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2566
Richard Smith161f09a2011-12-06 22:44:34 +00002567 // Handle bitcasts of vector constants.
2568 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2569 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2570
Eli Friedman13ec75b2011-12-21 00:43:02 +00002571 // Handle misc casts we want to ignore.
2572 // FIXME: Is it really safe to ignore all these?
2573 if (CE->getCastKind() == CK_NoOp ||
2574 CE->getCastKind() == CK_LValueToRValue ||
2575 CE->getCastKind() == CK_ToUnion ||
2576 CE->getCastKind() == CK_ConstructorConversion)
Richard Smith161f09a2011-12-06 22:44:34 +00002577 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2578
Eli Friedman384da272009-01-25 03:12:18 +00002579 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002580 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002581 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002582 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregorfe314812011-06-21 17:03:29 +00002583 ->isConstantInitializer(Ctx, false);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002584 }
Eli Friedman384da272009-01-25 03:12:18 +00002585 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002586}
2587
Douglas Gregor1be329d2012-02-23 07:33:15 +00002588namespace {
2589 /// \brief Look for a call to a non-trivial function within an expression.
2590 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2591 {
2592 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2593
2594 bool NonTrivial;
2595
2596 public:
2597 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregor6427a5e2012-02-23 07:44:18 +00002598 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor1be329d2012-02-23 07:33:15 +00002599
2600 bool hasNonTrivialCall() const { return NonTrivial; }
2601
2602 void VisitCallExpr(CallExpr *E) {
2603 if (CXXMethodDecl *Method
2604 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2605 if (Method->isTrivial()) {
2606 // Recurse to children of the call.
2607 Inherited::VisitStmt(E);
2608 return;
2609 }
2610 }
2611
2612 NonTrivial = true;
2613 }
2614
2615 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2616 if (E->getConstructor()->isTrivial()) {
2617 // Recurse to children of the call.
2618 Inherited::VisitStmt(E);
2619 return;
2620 }
2621
2622 NonTrivial = true;
2623 }
2624
2625 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2626 if (E->getTemporary()->getDestructor()->isTrivial()) {
2627 Inherited::VisitStmt(E);
2628 return;
2629 }
2630
2631 NonTrivial = true;
2632 }
2633 };
2634}
2635
2636bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2637 NonTrivialCallFinder Finder(Ctx);
2638 Finder.Visit(this);
2639 return Finder.hasNonTrivialCall();
2640}
2641
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002642/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2643/// pointer constant or not, as well as the specific kind of constant detected.
2644/// Null pointer constants can be integer constant expressions with the
2645/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2646/// (a GNU extension).
2647Expr::NullPointerConstantKind
2648Expr::isNullPointerConstant(ASTContext &Ctx,
2649 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00002650 if (isValueDependent()) {
2651 switch (NPC) {
2652 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00002653 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00002654 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002655 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2656 return NPCK_ZeroInteger;
2657 else
2658 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00002659
Douglas Gregor56751b52009-09-25 04:25:58 +00002660 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002661 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00002662 }
2663 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002664
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002665 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002666 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00002667 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002668 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002669 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002670 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002671 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002672 Pointee->isVoidType() && // to void*
2673 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002674 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002675 }
Steve Naroffada7d422007-05-20 17:54:12 +00002676 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002677 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2678 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002679 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002680 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2681 // Accept ((void*)0) as a null pointer constant, as many other
2682 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002683 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00002684 } else if (const GenericSelectionExpr *GE =
2685 dyn_cast<GenericSelectionExpr>(this)) {
2686 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002687 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002688 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002689 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002690 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002691 } else if (isa<GNUNullExpr>(this)) {
2692 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002693 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00002694 } else if (const MaterializeTemporaryExpr *M
2695 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2696 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00002697 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2698 if (const Expr *Source = OVE->getSourceExpr())
2699 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00002700 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002701
Sebastian Redl576fd422009-05-10 18:38:11 +00002702 // C++0x nullptr_t is always a null pointer constant.
2703 if (getType()->isNullPtrType())
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002704 return NPCK_CXX0X_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00002705
Fariborz Jahanian3567c422010-09-27 22:42:37 +00002706 if (const RecordType *UT = getType()->getAsUnionType())
2707 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2708 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2709 const Expr *InitExpr = CLE->getInitializer();
2710 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2711 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2712 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002713 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002714 if (!getType()->isIntegerType() ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00002715 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002716 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00002717
Chris Lattner1abbd412007-06-08 17:58:43 +00002718 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith98a0a492012-02-14 21:38:30 +00002719 // test for the value 0. Don't use the C++11 constant expression semantics
2720 // for this, for now; once the dust settles on core issue 903, we might only
2721 // allow a literal 0 here in C++11 mode.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002722 if (Ctx.getLangOpts().CPlusPlus0x) {
Richard Smith98a0a492012-02-14 21:38:30 +00002723 if (!isCXX98IntegralConstantExpr(Ctx))
2724 return NPCK_NotNull;
2725 } else {
2726 if (!isIntegerConstantExpr(Ctx))
2727 return NPCK_NotNull;
2728 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002729
Richard Smith98a0a492012-02-14 21:38:30 +00002730 return (EvaluateKnownConstInt(Ctx) == 0) ? NPCK_ZeroInteger : NPCK_NotNull;
Steve Naroff218bc2b2007-05-04 21:54:46 +00002731}
Steve Narofff7a5da12007-07-28 23:10:27 +00002732
John McCall34376a62010-12-04 03:47:34 +00002733/// \brief If this expression is an l-value for an Objective C
2734/// property, find the underlying property reference expression.
2735const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2736 const Expr *E = this;
2737 while (true) {
2738 assert((E->getValueKind() == VK_LValue &&
2739 E->getObjectKind() == OK_ObjCProperty) &&
2740 "expression is not a property reference");
2741 E = E->IgnoreParenCasts();
2742 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2743 if (BO->getOpcode() == BO_Comma) {
2744 E = BO->getRHS();
2745 continue;
2746 }
2747 }
2748
2749 break;
2750 }
2751
2752 return cast<ObjCPropertyRefExpr>(E);
2753}
2754
Douglas Gregor71235ec2009-05-02 02:18:30 +00002755FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002756 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002757
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002758 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002759 if (ICE->getCastKind() == CK_LValueToRValue ||
2760 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002761 E = ICE->getSubExpr()->IgnoreParens();
2762 else
2763 break;
2764 }
2765
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002766 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002767 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002768 if (Field->isBitField())
2769 return Field;
2770
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00002771 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2772 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2773 if (Field->isBitField())
2774 return Field;
2775
Eli Friedman609ada22011-07-13 02:05:57 +00002776 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00002777 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2778 return BinOp->getLHS()->getBitField();
2779
Eli Friedman609ada22011-07-13 02:05:57 +00002780 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2781 return BinOp->getRHS()->getBitField();
2782 }
2783
Douglas Gregor71235ec2009-05-02 02:18:30 +00002784 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002785}
2786
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002787bool Expr::refersToVectorElement() const {
2788 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002789
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002790 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002791 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002792 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002793 E = ICE->getSubExpr()->IgnoreParens();
2794 else
2795 break;
2796 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002797
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002798 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2799 return ASE->getBase()->getType()->isVectorType();
2800
2801 if (isa<ExtVectorElementExpr>(E))
2802 return true;
2803
2804 return false;
2805}
2806
Chris Lattnerb8211f62009-02-16 22:14:05 +00002807/// isArrow - Return true if the base expression is a pointer to vector,
2808/// return false if the base expression is a vector.
2809bool ExtVectorElementExpr::isArrow() const {
2810 return getBase()->getType()->isPointerType();
2811}
2812
Nate Begemance4d7fc2008-04-18 23:10:10 +00002813unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002814 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002815 return VT->getNumElements();
2816 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002817}
2818
Nate Begemanf322eab2008-05-09 06:41:27 +00002819/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002820bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002821 // FIXME: Refactor this code to an accessor on the AST node which returns the
2822 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002823 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002824
2825 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002826 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002827 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002828
Nate Begeman7e5185b2009-01-18 02:01:21 +00002829 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002830 if (Comp[0] == 's' || Comp[0] == 'S')
2831 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002832
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002833 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002834 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002835 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002836
Steve Naroff0d595ca2007-07-30 03:29:09 +00002837 return false;
2838}
Chris Lattner885b4952007-08-02 23:36:59 +00002839
Nate Begemanf322eab2008-05-09 06:41:27 +00002840/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002841void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002842 SmallVectorImpl<unsigned> &Elts) const {
2843 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002844 if (Comp[0] == 's' || Comp[0] == 'S')
2845 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002846
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002847 bool isHi = Comp == "hi";
2848 bool isLo = Comp == "lo";
2849 bool isEven = Comp == "even";
2850 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002851
Nate Begemanf322eab2008-05-09 06:41:27 +00002852 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2853 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002854
Nate Begemanf322eab2008-05-09 06:41:27 +00002855 if (isHi)
2856 Index = e + i;
2857 else if (isLo)
2858 Index = i;
2859 else if (isEven)
2860 Index = 2 * i;
2861 else if (isOdd)
2862 Index = 2 * i + 1;
2863 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002864 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002865
Nate Begemand3862152008-05-13 21:03:02 +00002866 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002867 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002868}
2869
Douglas Gregor9a129192010-04-21 00:45:42 +00002870ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002871 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002872 SourceLocation LBracLoc,
2873 SourceLocation SuperLoc,
2874 bool IsInstanceSuper,
2875 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002876 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002877 ArrayRef<SourceLocation> SelLocs,
2878 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002879 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00002880 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002881 SourceLocation RBracLoc,
2882 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00002883 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002884 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00002885 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002886 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002887 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2888 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00002889 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002890 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
2891 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002892{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002893 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00002894 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002895}
2896
Douglas Gregor9a129192010-04-21 00:45:42 +00002897ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002898 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002899 SourceLocation LBracLoc,
2900 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002901 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002902 ArrayRef<SourceLocation> SelLocs,
2903 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002904 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00002905 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002906 SourceLocation RBracLoc,
2907 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00002908 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002909 T->isDependentType(), T->isInstantiationDependentType(),
2910 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002911 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2912 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00002913 Kind(Class),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002914 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002915 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002916{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002917 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00002918 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002919}
2920
Douglas Gregor9a129192010-04-21 00:45:42 +00002921ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002922 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002923 SourceLocation LBracLoc,
2924 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002925 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002926 ArrayRef<SourceLocation> SelLocs,
2927 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002928 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00002929 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002930 SourceLocation RBracLoc,
2931 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00002932 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002933 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002934 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002935 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002936 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2937 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00002938 Kind(Instance),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002939 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002940 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002941{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002942 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00002943 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002944}
2945
2946void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2947 ArrayRef<SourceLocation> SelLocs,
2948 SelectorLocationsKind SelLocsK) {
2949 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00002950 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00002951 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00002952 if (Args[I]->isTypeDependent())
2953 ExprBits.TypeDependent = true;
2954 if (Args[I]->isValueDependent())
2955 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00002956 if (Args[I]->isInstantiationDependent())
2957 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00002958 if (Args[I]->containsUnexpandedParameterPack())
2959 ExprBits.ContainsUnexpandedParameterPack = true;
2960
2961 MyArgs[I] = Args[I];
2962 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002963
Benjamin Kramer2325b242012-02-20 00:20:48 +00002964 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00002965 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00002966 if (SelLocsK == SelLoc_NonStandard)
2967 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
2968 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00002969}
2970
Douglas Gregor9a129192010-04-21 00:45:42 +00002971ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002972 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002973 SourceLocation LBracLoc,
2974 SourceLocation SuperLoc,
2975 bool IsInstanceSuper,
2976 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002977 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00002978 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002979 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00002980 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002981 SourceLocation RBracLoc,
2982 bool isImplicit) {
2983 assert((!SelLocs.empty() || isImplicit) &&
2984 "No selector locs for non-implicit message");
2985 ObjCMessageExpr *Mem;
2986 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
2987 if (isImplicit)
2988 Mem = alloc(Context, Args.size(), 0);
2989 else
2990 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00002991 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002992 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002993 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00002994}
2995
2996ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002997 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002998 SourceLocation LBracLoc,
2999 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003000 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003001 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003002 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003003 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003004 SourceLocation RBracLoc,
3005 bool isImplicit) {
3006 assert((!SelLocs.empty() || isImplicit) &&
3007 "No selector locs for non-implicit message");
3008 ObjCMessageExpr *Mem;
3009 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3010 if (isImplicit)
3011 Mem = alloc(Context, Args.size(), 0);
3012 else
3013 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003014 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003015 SelLocs, SelLocsK, Method, Args, RBracLoc,
3016 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003017}
3018
3019ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003020 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003021 SourceLocation LBracLoc,
3022 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003023 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003024 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003025 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003026 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003027 SourceLocation RBracLoc,
3028 bool isImplicit) {
3029 assert((!SelLocs.empty() || isImplicit) &&
3030 "No selector locs for non-implicit message");
3031 ObjCMessageExpr *Mem;
3032 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3033 if (isImplicit)
3034 Mem = alloc(Context, Args.size(), 0);
3035 else
3036 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003037 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003038 SelLocs, SelLocsK, Method, Args, RBracLoc,
3039 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003040}
3041
Alexis Hunta8136cc2010-05-05 15:23:54 +00003042ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003043 unsigned NumArgs,
3044 unsigned NumStoredSelLocs) {
3045 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003046 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3047}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003048
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003049ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3050 ArrayRef<Expr *> Args,
3051 SourceLocation RBraceLoc,
3052 ArrayRef<SourceLocation> SelLocs,
3053 Selector Sel,
3054 SelectorLocationsKind &SelLocsK) {
3055 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3056 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3057 : 0;
3058 return alloc(C, Args.size(), NumStoredSelLocs);
3059}
3060
3061ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3062 unsigned NumArgs,
3063 unsigned NumStoredSelLocs) {
3064 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3065 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3066 return (ObjCMessageExpr *)C.Allocate(Size,
3067 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3068}
3069
3070void ObjCMessageExpr::getSelectorLocs(
3071 SmallVectorImpl<SourceLocation> &SelLocs) const {
3072 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3073 SelLocs.push_back(getSelectorLoc(i));
3074}
3075
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003076SourceRange ObjCMessageExpr::getReceiverRange() const {
3077 switch (getReceiverKind()) {
3078 case Instance:
3079 return getInstanceReceiver()->getSourceRange();
3080
3081 case Class:
3082 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3083
3084 case SuperInstance:
3085 case SuperClass:
3086 return getSuperLoc();
3087 }
3088
David Blaikiee4d798f2012-01-20 21:50:17 +00003089 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003090}
3091
Douglas Gregor9a129192010-04-21 00:45:42 +00003092Selector ObjCMessageExpr::getSelector() const {
3093 if (HasMethod)
3094 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3095 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003096 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003097}
3098
3099ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3100 switch (getReceiverKind()) {
3101 case Instance:
3102 if (const ObjCObjectPointerType *Ptr
3103 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
3104 return Ptr->getInterfaceDecl();
3105 break;
3106
3107 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00003108 if (const ObjCObjectType *Ty
3109 = getClassReceiver()->getAs<ObjCObjectType>())
3110 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00003111 break;
3112
3113 case SuperInstance:
3114 if (const ObjCObjectPointerType *Ptr
3115 = getSuperType()->getAs<ObjCObjectPointerType>())
3116 return Ptr->getInterfaceDecl();
3117 break;
3118
3119 case SuperClass:
Argyrios Kyrtzidis1b9747f2011-01-25 00:03:48 +00003120 if (const ObjCObjectType *Iface
3121 = getSuperType()->getAs<ObjCObjectType>())
3122 return Iface->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00003123 break;
3124 }
3125
3126 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00003127}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003128
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003129StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003130 switch (getBridgeKind()) {
3131 case OBC_Bridge:
3132 return "__bridge";
3133 case OBC_BridgeTransfer:
3134 return "__bridge_transfer";
3135 case OBC_BridgeRetained:
3136 return "__bridge_retained";
3137 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003138
3139 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003140}
3141
Jay Foad39c79802011-01-12 09:06:06 +00003142bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smithcaf33902011-10-10 18:28:20 +00003143 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00003144}
3145
Douglas Gregora6e053e2010-12-15 01:34:56 +00003146ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3147 QualType Type, SourceLocation BLoc,
3148 SourceLocation RP)
3149 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3150 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003151 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003152 Type->containsUnexpandedParameterPack()),
3153 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3154{
3155 SubExprs = new (C) Stmt*[nexpr];
3156 for (unsigned i = 0; i < nexpr; i++) {
3157 if (args[i]->isTypeDependent())
3158 ExprBits.TypeDependent = true;
3159 if (args[i]->isValueDependent())
3160 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003161 if (args[i]->isInstantiationDependent())
3162 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003163 if (args[i]->containsUnexpandedParameterPack())
3164 ExprBits.ContainsUnexpandedParameterPack = true;
3165
3166 SubExprs[i] = args[i];
3167 }
3168}
3169
Nate Begeman48745922009-08-12 02:28:50 +00003170void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3171 unsigned NumExprs) {
3172 if (SubExprs) C.Deallocate(SubExprs);
3173
3174 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00003175 this->NumExprs = NumExprs;
3176 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00003177}
Nate Begeman48745922009-08-12 02:28:50 +00003178
Peter Collingbourne91147592011-04-15 00:35:48 +00003179GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3180 SourceLocation GenericLoc, Expr *ControllingExpr,
3181 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3182 unsigned NumAssocs, SourceLocation DefaultLoc,
3183 SourceLocation RParenLoc,
3184 bool ContainsUnexpandedParameterPack,
3185 unsigned ResultIndex)
3186 : Expr(GenericSelectionExprClass,
3187 AssocExprs[ResultIndex]->getType(),
3188 AssocExprs[ResultIndex]->getValueKind(),
3189 AssocExprs[ResultIndex]->getObjectKind(),
3190 AssocExprs[ResultIndex]->isTypeDependent(),
3191 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003192 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003193 ContainsUnexpandedParameterPack),
3194 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3195 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3196 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3197 RParenLoc(RParenLoc) {
3198 SubExprs[CONTROLLING] = ControllingExpr;
3199 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3200 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3201}
3202
3203GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3204 SourceLocation GenericLoc, Expr *ControllingExpr,
3205 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3206 unsigned NumAssocs, SourceLocation DefaultLoc,
3207 SourceLocation RParenLoc,
3208 bool ContainsUnexpandedParameterPack)
3209 : Expr(GenericSelectionExprClass,
3210 Context.DependentTy,
3211 VK_RValue,
3212 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003213 /*isTypeDependent=*/true,
3214 /*isValueDependent=*/true,
3215 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003216 ContainsUnexpandedParameterPack),
3217 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3218 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3219 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3220 RParenLoc(RParenLoc) {
3221 SubExprs[CONTROLLING] = ControllingExpr;
3222 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3223 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3224}
3225
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003226//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003227// DesignatedInitExpr
3228//===----------------------------------------------------------------------===//
3229
Chandler Carruth631abd92011-06-16 06:47:06 +00003230IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003231 assert(Kind == FieldDesignator && "Only valid on a field designator");
3232 if (Field.NameOrField & 0x01)
3233 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3234 else
3235 return getField()->getIdentifier();
3236}
3237
Alexis Hunta8136cc2010-05-05 15:23:54 +00003238DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003239 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003240 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003241 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003242 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00003243 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003244 unsigned NumIndexExprs,
3245 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003246 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003247 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003248 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003249 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003250 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003251 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3252 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003253 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003254
3255 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003256 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003257 *Child++ = Init;
3258
3259 // Copy the designators and their subexpressions, computing
3260 // value-dependence along the way.
3261 unsigned IndexIdx = 0;
3262 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003263 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003264
3265 if (this->Designators[I].isArrayDesignator()) {
3266 // Compute type- and value-dependence.
3267 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003268 if (Index->isTypeDependent() || Index->isValueDependent())
3269 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003270 if (Index->isInstantiationDependent())
3271 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003272 // Propagate unexpanded parameter packs.
3273 if (Index->containsUnexpandedParameterPack())
3274 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003275
3276 // Copy the index expressions into permanent storage.
3277 *Child++ = IndexExprs[IndexIdx++];
3278 } else if (this->Designators[I].isArrayRangeDesignator()) {
3279 // Compute type- and value-dependence.
3280 Expr *Start = IndexExprs[IndexIdx];
3281 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003282 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003283 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003284 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003285 ExprBits.InstantiationDependent = true;
3286 } else if (Start->isInstantiationDependent() ||
3287 End->isInstantiationDependent()) {
3288 ExprBits.InstantiationDependent = true;
3289 }
3290
Douglas Gregora6e053e2010-12-15 01:34:56 +00003291 // Propagate unexpanded parameter packs.
3292 if (Start->containsUnexpandedParameterPack() ||
3293 End->containsUnexpandedParameterPack())
3294 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003295
3296 // Copy the start/end expressions into permanent storage.
3297 *Child++ = IndexExprs[IndexIdx++];
3298 *Child++ = IndexExprs[IndexIdx++];
3299 }
3300 }
3301
3302 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003303}
3304
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003305DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00003306DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003307 unsigned NumDesignators,
3308 Expr **IndexExprs, unsigned NumIndexExprs,
3309 SourceLocation ColonOrEqualLoc,
3310 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003311 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003312 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003313 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003314 ColonOrEqualLoc, UsesColonSyntax,
3315 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003316}
3317
Mike Stump11289f42009-09-09 15:08:12 +00003318DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003319 unsigned NumIndexExprs) {
3320 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3321 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3322 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3323}
3324
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003325void DesignatedInitExpr::setDesignators(ASTContext &C,
3326 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003327 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003328 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003329 NumDesignators = NumDesigs;
3330 for (unsigned I = 0; I != NumDesigs; ++I)
3331 Designators[I] = Desigs[I];
3332}
3333
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003334SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3335 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3336 if (size() == 1)
3337 return DIE->getDesignator(0)->getSourceRange();
3338 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3339 DIE->getDesignator(size()-1)->getEndLocation());
3340}
3341
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003342SourceRange DesignatedInitExpr::getSourceRange() const {
3343 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003344 Designator &First =
3345 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003346 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003347 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003348 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3349 else
3350 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3351 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003352 StartLoc =
3353 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003354 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3355}
3356
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003357Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3358 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3359 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3360 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003361 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3362 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3363}
3364
3365Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00003366 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003367 "Requires array range designator");
3368 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3369 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003370 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3371 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3372}
3373
3374Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00003375 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003376 "Requires array range designator");
3377 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3378 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003379 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3380 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3381}
3382
Douglas Gregord5846a12009-04-15 06:41:24 +00003383/// \brief Replaces the designator at index @p Idx with the series
3384/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003385void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003386 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003387 const Designator *Last) {
3388 unsigned NumNewDesignators = Last - First;
3389 if (NumNewDesignators == 0) {
3390 std::copy_backward(Designators + Idx + 1,
3391 Designators + NumDesignators,
3392 Designators + Idx);
3393 --NumNewDesignators;
3394 return;
3395 } else if (NumNewDesignators == 1) {
3396 Designators[Idx] = *First;
3397 return;
3398 }
3399
Mike Stump11289f42009-09-09 15:08:12 +00003400 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003401 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003402 std::copy(Designators, Designators + Idx, NewDesignators);
3403 std::copy(First, Last, NewDesignators + Idx);
3404 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3405 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003406 Designators = NewDesignators;
3407 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3408}
3409
Mike Stump11289f42009-09-09 15:08:12 +00003410ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00003411 Expr **exprs, unsigned nexprs,
Sebastian Redla9351792012-02-11 23:51:47 +00003412 SourceLocation rparenloc)
3413 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003414 false, false, false, false),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003415 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00003416 Exprs = new (C) Stmt*[nexprs];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003417 for (unsigned i = 0; i != nexprs; ++i) {
3418 if (exprs[i]->isTypeDependent())
3419 ExprBits.TypeDependent = true;
3420 if (exprs[i]->isValueDependent())
3421 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003422 if (exprs[i]->isInstantiationDependent())
3423 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003424 if (exprs[i]->containsUnexpandedParameterPack())
3425 ExprBits.ContainsUnexpandedParameterPack = true;
3426
Nate Begeman5ec4b312009-08-10 23:49:36 +00003427 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003428 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003429}
3430
John McCall1bf58462011-02-16 08:02:54 +00003431const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3432 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3433 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003434 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3435 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003436 e = cast<CXXConstructExpr>(e)->getArg(0);
3437 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3438 e = ice->getSubExpr();
3439 return cast<OpaqueValueExpr>(e);
3440}
3441
John McCallfe96e0b2011-11-06 09:01:30 +00003442PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3443 unsigned numSemanticExprs) {
3444 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3445 (1 + numSemanticExprs) * sizeof(Expr*),
3446 llvm::alignOf<PseudoObjectExpr>());
3447 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3448}
3449
3450PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3451 : Expr(PseudoObjectExprClass, shell) {
3452 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3453}
3454
3455PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3456 ArrayRef<Expr*> semantics,
3457 unsigned resultIndex) {
3458 assert(syntax && "no syntactic expression!");
3459 assert(semantics.size() && "no semantic expressions!");
3460
3461 QualType type;
3462 ExprValueKind VK;
3463 if (resultIndex == NoResult) {
3464 type = C.VoidTy;
3465 VK = VK_RValue;
3466 } else {
3467 assert(resultIndex < semantics.size());
3468 type = semantics[resultIndex]->getType();
3469 VK = semantics[resultIndex]->getValueKind();
3470 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3471 }
3472
3473 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3474 (1 + semantics.size()) * sizeof(Expr*),
3475 llvm::alignOf<PseudoObjectExpr>());
3476 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3477 resultIndex);
3478}
3479
3480PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3481 Expr *syntax, ArrayRef<Expr*> semantics,
3482 unsigned resultIndex)
3483 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3484 /*filled in at end of ctor*/ false, false, false, false) {
3485 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3486 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3487
3488 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3489 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3490 getSubExprsBuffer()[i] = E;
3491
3492 if (E->isTypeDependent())
3493 ExprBits.TypeDependent = true;
3494 if (E->isValueDependent())
3495 ExprBits.ValueDependent = true;
3496 if (E->isInstantiationDependent())
3497 ExprBits.InstantiationDependent = true;
3498 if (E->containsUnexpandedParameterPack())
3499 ExprBits.ContainsUnexpandedParameterPack = true;
3500
3501 if (isa<OpaqueValueExpr>(E))
3502 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3503 "opaque-value semantic expressions for pseudo-object "
3504 "operations must have sources");
3505 }
3506}
3507
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003508//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003509// ExprIterator.
3510//===----------------------------------------------------------------------===//
3511
3512Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3513Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3514Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3515const Expr* ConstExprIterator::operator[](size_t idx) const {
3516 return cast<Expr>(I[idx]);
3517}
3518const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3519const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3520
3521//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003522// Child Iterators for iterating over subexpressions/substatements
3523//===----------------------------------------------------------------------===//
3524
Peter Collingbournee190dee2011-03-11 19:24:49 +00003525// UnaryExprOrTypeTraitExpr
3526Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003527 // If this is of a type and the type is a VLA type (and not a typedef), the
3528 // size expression of the VLA needs to be treated as an executable expression.
3529 // Why isn't this weirdness documented better in StmtIterator?
3530 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003531 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003532 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003533 return child_range(child_iterator(T), child_iterator());
3534 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003535 }
John McCallbd066782011-02-09 08:16:59 +00003536 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003537}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003538
Steve Naroffd54978b2007-09-18 23:55:05 +00003539// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00003540Stmt::child_range ObjCMessageExpr::children() {
3541 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003542 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003543 begin = reinterpret_cast<Stmt **>(this + 1);
3544 else
3545 begin = reinterpret_cast<Stmt **>(getArgs());
3546 return child_range(begin,
3547 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003548}
3549
Ted Kremeneke65b0862012-03-06 20:05:56 +00003550ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3551 QualType T, ObjCMethodDecl *Method,
3552 SourceRange SR)
3553 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3554 false, false, false, false),
3555 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3556{
3557 Expr **SaveElements = getElements();
3558 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3559 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3560 ExprBits.ValueDependent = true;
3561 if (Elements[I]->isInstantiationDependent())
3562 ExprBits.InstantiationDependent = true;
3563 if (Elements[I]->containsUnexpandedParameterPack())
3564 ExprBits.ContainsUnexpandedParameterPack = true;
3565
3566 SaveElements[I] = Elements[I];
3567 }
3568}
3569
3570ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3571 llvm::ArrayRef<Expr *> Elements,
3572 QualType T, ObjCMethodDecl * Method,
3573 SourceRange SR) {
3574 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3575 + Elements.size() * sizeof(Expr *));
3576 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3577}
3578
3579ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3580 unsigned NumElements) {
3581
3582 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3583 + NumElements * sizeof(Expr *));
3584 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3585}
3586
3587ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3588 ArrayRef<ObjCDictionaryElement> VK,
3589 bool HasPackExpansions,
3590 QualType T, ObjCMethodDecl *method,
3591 SourceRange SR)
3592 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3593 false, false),
3594 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3595 DictWithObjectsMethod(method)
3596{
3597 KeyValuePair *KeyValues = getKeyValues();
3598 ExpansionData *Expansions = getExpansionData();
3599 for (unsigned I = 0; I < NumElements; I++) {
3600 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3601 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3602 ExprBits.ValueDependent = true;
3603 if (VK[I].Key->isInstantiationDependent() ||
3604 VK[I].Value->isInstantiationDependent())
3605 ExprBits.InstantiationDependent = true;
3606 if (VK[I].EllipsisLoc.isInvalid() &&
3607 (VK[I].Key->containsUnexpandedParameterPack() ||
3608 VK[I].Value->containsUnexpandedParameterPack()))
3609 ExprBits.ContainsUnexpandedParameterPack = true;
3610
3611 KeyValues[I].Key = VK[I].Key;
3612 KeyValues[I].Value = VK[I].Value;
3613 if (Expansions) {
3614 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3615 if (VK[I].NumExpansions)
3616 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3617 else
3618 Expansions[I].NumExpansionsPlusOne = 0;
3619 }
3620 }
3621}
3622
3623ObjCDictionaryLiteral *
3624ObjCDictionaryLiteral::Create(ASTContext &C,
3625 ArrayRef<ObjCDictionaryElement> VK,
3626 bool HasPackExpansions,
3627 QualType T, ObjCMethodDecl *method,
3628 SourceRange SR) {
3629 unsigned ExpansionsSize = 0;
3630 if (HasPackExpansions)
3631 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3632
3633 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3634 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3635 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3636}
3637
3638ObjCDictionaryLiteral *
3639ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3640 bool HasPackExpansions) {
3641 unsigned ExpansionsSize = 0;
3642 if (HasPackExpansions)
3643 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3644 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3645 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3646 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3647 HasPackExpansions);
3648}
3649
3650ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
3651 Expr *base,
3652 Expr *key, QualType T,
3653 ObjCMethodDecl *getMethod,
3654 ObjCMethodDecl *setMethod,
3655 SourceLocation RB) {
3656 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
3657 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
3658 OK_ObjCSubscript,
3659 getMethod, setMethod, RB);
3660}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00003661
3662AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3663 QualType t, AtomicOp op, SourceLocation RP)
3664 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3665 false, false, false, false),
3666 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3667{
Richard Smithaa22a8c2012-04-10 22:49:28 +00003668 assert(nexpr == getNumSubExprs(op) && "wrong number of subexpressions");
Eli Friedman8d3e43f2011-10-14 22:48:56 +00003669 for (unsigned i = 0; i < nexpr; i++) {
3670 if (args[i]->isTypeDependent())
3671 ExprBits.TypeDependent = true;
3672 if (args[i]->isValueDependent())
3673 ExprBits.ValueDependent = true;
3674 if (args[i]->isInstantiationDependent())
3675 ExprBits.InstantiationDependent = true;
3676 if (args[i]->containsUnexpandedParameterPack())
3677 ExprBits.ContainsUnexpandedParameterPack = true;
3678
3679 SubExprs[i] = args[i];
3680 }
3681}
Richard Smithaa22a8c2012-04-10 22:49:28 +00003682
3683unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
3684 switch (Op) {
Richard Smithfeea8832012-04-12 05:08:17 +00003685 case AO__c11_atomic_init:
3686 case AO__c11_atomic_load:
3687 case AO__atomic_load_n:
Richard Smithaa22a8c2012-04-10 22:49:28 +00003688 return 2;
Richard Smithfeea8832012-04-12 05:08:17 +00003689
3690 case AO__c11_atomic_store:
3691 case AO__c11_atomic_exchange:
3692 case AO__atomic_load:
3693 case AO__atomic_store:
3694 case AO__atomic_store_n:
3695 case AO__atomic_exchange_n:
3696 case AO__c11_atomic_fetch_add:
3697 case AO__c11_atomic_fetch_sub:
3698 case AO__c11_atomic_fetch_and:
3699 case AO__c11_atomic_fetch_or:
3700 case AO__c11_atomic_fetch_xor:
3701 case AO__atomic_fetch_add:
3702 case AO__atomic_fetch_sub:
3703 case AO__atomic_fetch_and:
3704 case AO__atomic_fetch_or:
3705 case AO__atomic_fetch_xor:
Richard Smithd65cee92012-04-13 06:31:38 +00003706 case AO__atomic_fetch_nand:
Richard Smithfeea8832012-04-12 05:08:17 +00003707 case AO__atomic_add_fetch:
3708 case AO__atomic_sub_fetch:
3709 case AO__atomic_and_fetch:
3710 case AO__atomic_or_fetch:
3711 case AO__atomic_xor_fetch:
Richard Smithd65cee92012-04-13 06:31:38 +00003712 case AO__atomic_nand_fetch:
Richard Smithaa22a8c2012-04-10 22:49:28 +00003713 return 3;
Richard Smithfeea8832012-04-12 05:08:17 +00003714
3715 case AO__atomic_exchange:
3716 return 4;
3717
3718 case AO__c11_atomic_compare_exchange_strong:
3719 case AO__c11_atomic_compare_exchange_weak:
Richard Smithaa22a8c2012-04-10 22:49:28 +00003720 return 5;
Richard Smithfeea8832012-04-12 05:08:17 +00003721
3722 case AO__atomic_compare_exchange:
3723 case AO__atomic_compare_exchange_n:
3724 return 6;
Richard Smithaa22a8c2012-04-10 22:49:28 +00003725 }
3726 llvm_unreachable("unknown atomic op");
3727}