blob: f2f77367d8d9364862566b0f3af874b419aedf68 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregor25d0a0f2012-02-23 07:33:15 +000021#include "clang/AST/EvaluatedExprVisitor.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000024#include "clang/Lex/LiteralSupport.h"
25#include "clang/Lex/Lexer.h"
Richard Smith7a614d82011-06-11 17:19:42 +000026#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000027#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000029#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000030#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000031#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000032#include <algorithm>
Eli Friedman64f45a22011-11-01 02:23:42 +000033#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000034using namespace clang;
35
Rafael Espindola8d852e32012-06-27 18:18:05 +000036const CXXRecordDecl *Expr::getBestDynamicClassType() const {
Rafael Espindola632fbaa2012-06-28 01:56:38 +000037 const Expr *E = ignoreParenBaseCasts();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000038
39 QualType DerivedType = E->getType();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000040 if (const PointerType *PTy = DerivedType->getAs<PointerType>())
41 DerivedType = PTy->getPointeeType();
42
43 const RecordType *Ty = DerivedType->castAs<RecordType>();
Rafael Espindola0b4fe502012-06-26 17:45:31 +000044 Decl *D = Ty->getDecl();
45 return cast<CXXRecordDecl>(D);
46}
47
Chris Lattner2b334bb2010-04-16 23:34:13 +000048/// isKnownToHaveBooleanValue - Return true if this is an integer expression
49/// that is known to return 0 or 1. This happens for _Bool/bool expressions
50/// but also int expressions which are produced by things like comparisons in
51/// C.
52bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000053 const Expr *E = IgnoreParens();
54
Chris Lattner2b334bb2010-04-16 23:34:13 +000055 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000056 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000057 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000058 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000059
Peter Collingbournef111d932011-04-15 00:35:48 +000060 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000061 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000062 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000063 return UO->getSubExpr()->isKnownToHaveBooleanValue();
64 default:
65 return false;
66 }
67 }
Sean Huntc3021132010-05-05 15:23:54 +000068
John McCall6907fbe2010-06-12 01:56:02 +000069 // Only look through implicit casts. If the user writes
70 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000071 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000072 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000073
Peter Collingbournef111d932011-04-15 00:35:48 +000074 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000075 switch (BO->getOpcode()) {
76 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000077 case BO_LT: // Relational operators.
78 case BO_GT:
79 case BO_LE:
80 case BO_GE:
81 case BO_EQ: // Equality operators.
82 case BO_NE:
83 case BO_LAnd: // AND operator.
84 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000085 return true;
Sean Huntc3021132010-05-05 15:23:54 +000086
John McCall2de56d12010-08-25 11:45:40 +000087 case BO_And: // Bitwise AND operator.
88 case BO_Xor: // Bitwise XOR operator.
89 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000090 // Handle things like (x==2)|(y==12).
91 return BO->getLHS()->isKnownToHaveBooleanValue() &&
92 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000093
John McCall2de56d12010-08-25 11:45:40 +000094 case BO_Comma:
95 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000096 return BO->getRHS()->isKnownToHaveBooleanValue();
97 }
98 }
Sean Huntc3021132010-05-05 15:23:54 +000099
Peter Collingbournef111d932011-04-15 00:35:48 +0000100 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +0000101 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
102 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +0000103
Chris Lattner2b334bb2010-04-16 23:34:13 +0000104 return false;
105}
106
John McCall63c00d72011-02-09 08:16:59 +0000107// Amusing macro metaprogramming hack: check whether a class provides
108// a more specific implementation of getExprLoc().
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000109//
110// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCall63c00d72011-02-09 08:16:59 +0000111namespace {
112 /// This implementation is used when a class provides a custom
113 /// implementation of getExprLoc.
114 template <class E, class T>
115 SourceLocation getExprLocImpl(const Expr *expr,
116 SourceLocation (T::*v)() const) {
117 return static_cast<const E*>(expr)->getExprLoc();
118 }
119
120 /// This implementation is used when a class doesn't provide
121 /// a custom implementation of getExprLoc. Overload resolution
122 /// should pick it over the implementation above because it's
123 /// more specialized according to function template partial ordering.
124 template <class E>
125 SourceLocation getExprLocImpl(const Expr *expr,
126 SourceLocation (Expr::*v)() const) {
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000127 return static_cast<const E*>(expr)->getLocStart();
John McCall63c00d72011-02-09 08:16:59 +0000128 }
129}
130
131SourceLocation Expr::getExprLoc() const {
132 switch (getStmtClass()) {
133 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
134#define ABSTRACT_STMT(type)
135#define STMT(type, base) \
136 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
137#define EXPR(type, base) \
138 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
139#include "clang/AST/StmtNodes.inc"
140 }
141 llvm_unreachable("unknown statement kind");
John McCall63c00d72011-02-09 08:16:59 +0000142}
143
Reid Spencer5f016e22007-07-11 17:01:13 +0000144//===----------------------------------------------------------------------===//
145// Primary Expressions.
146//===----------------------------------------------------------------------===//
147
Douglas Gregor561f8122011-07-01 01:22:09 +0000148/// \brief Compute the type-, value-, and instantiation-dependence of a
149/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000150/// based on the declaration being referenced.
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000151static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T,
Douglas Gregord967e312011-01-19 21:52:31 +0000152 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000153 bool &ValueDependent,
154 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000155 TypeDependent = false;
156 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000157 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000158
159 // (TD) C++ [temp.dep.expr]p3:
160 // An id-expression is type-dependent if it contains:
161 //
Sean Huntc3021132010-05-05 15:23:54 +0000162 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000163 //
164 // (VD) C++ [temp.dep.constexpr]p2:
165 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000166
Douglas Gregor0da76df2009-11-23 11:41:28 +0000167 // (TD) - an identifier that was declared with dependent type
168 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000169 if (T->isDependentType()) {
170 TypeDependent = true;
171 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000172 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000173 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000174 } else if (T->isInstantiationDependentType()) {
175 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000176 }
Douglas Gregord967e312011-01-19 21:52:31 +0000177
Douglas Gregor0da76df2009-11-23 11:41:28 +0000178 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000179 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000180 == DeclarationName::CXXConversionFunctionName) {
181 QualType T = D->getDeclName().getCXXNameType();
182 if (T->isDependentType()) {
183 TypeDependent = true;
184 ValueDependent = true;
185 InstantiationDependent = true;
186 return;
187 }
188
189 if (T->isInstantiationDependentType())
190 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000191 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000192
Douglas Gregor0da76df2009-11-23 11:41:28 +0000193 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000194 if (isa<NonTypeTemplateParmDecl>(D)) {
195 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000196 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000197 return;
198 }
199
Douglas Gregor0da76df2009-11-23 11:41:28 +0000200 // (VD) - a constant with integral or enumeration type and is
201 // initialized with an expression that is value-dependent.
Richard Smithdb1822c2011-11-08 01:31:09 +0000202 // (VD) - a constant with literal type and is initialized with an
203 // expression that is value-dependent [C++11].
204 // (VD) - FIXME: Missing from the standard:
205 // - an entity with reference type and is initialized with an
206 // expression that is value-dependent [C++11]
Douglas Gregord967e312011-01-19 21:52:31 +0000207 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000208 if ((Ctx.getLangOpts().CPlusPlus0x ?
Richard Smithdb1822c2011-11-08 01:31:09 +0000209 Var->getType()->isLiteralType() :
210 Var->getType()->isIntegralOrEnumerationType()) &&
211 (Var->getType().getCVRQualifiers() == Qualifiers::Const ||
212 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000213 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000214 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000215 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000216 InstantiationDependent = true;
217 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000218 }
219
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000220 // (VD) - FIXME: Missing from the standard:
221 // - a member function or a static data member of the current
222 // instantiation
Richard Smithdb1822c2011-11-08 01:31:09 +0000223 if (Var->isStaticDataMember() &&
224 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000225 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000226 InstantiationDependent = true;
227 }
Douglas Gregord967e312011-01-19 21:52:31 +0000228
229 return;
230 }
231
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000232 // (VD) - FIXME: Missing from the standard:
233 // - a member function or a static data member of the current
234 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000235 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
236 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000237 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000238 }
Douglas Gregord967e312011-01-19 21:52:31 +0000239}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000240
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000241void DeclRefExpr::computeDependence(ASTContext &Ctx) {
Douglas Gregord967e312011-01-19 21:52:31 +0000242 bool TypeDependent = false;
243 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000244 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000245 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
246 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000247
248 // (TD) C++ [temp.dep.expr]p3:
249 // An id-expression is type-dependent if it contains:
250 //
251 // and
252 //
253 // (VD) C++ [temp.dep.constexpr]p2:
254 // An identifier is value-dependent if it is:
255 if (!TypeDependent && !ValueDependent &&
256 hasExplicitTemplateArgs() &&
257 TemplateSpecializationType::anyDependentTemplateArguments(
258 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000259 getNumTemplateArgs(),
260 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000261 TypeDependent = true;
262 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000263 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000264 }
265
266 ExprBits.TypeDependent = TypeDependent;
267 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000268 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000269
Douglas Gregor10738d32010-12-23 23:51:58 +0000270 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000271 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000272 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000273}
274
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000275DeclRefExpr::DeclRefExpr(ASTContext &Ctx,
276 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000277 SourceLocation TemplateKWLoc,
John McCallf4b88a42012-03-10 09:33:50 +0000278 ValueDecl *D, bool RefersToEnclosingLocal,
279 const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000280 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000281 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000282 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000283 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000284 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
285 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000286 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000287 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000288 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
289 if (FoundD)
290 getInternalFoundDecl() = FoundD;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000291 DeclRefExprBits.HasTemplateKWAndArgsInfo
292 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCallf4b88a42012-03-10 09:33:50 +0000293 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor561f8122011-07-01 01:22:09 +0000294 if (TemplateArgs) {
295 bool Dependent = false;
296 bool InstantiationDependent = false;
297 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000298 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
299 Dependent,
300 InstantiationDependent,
301 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +0000302 if (InstantiationDependent)
303 setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000304 } else if (TemplateKWLoc.isValid()) {
305 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor561f8122011-07-01 01:22:09 +0000306 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000307 DeclRefExprBits.HadMultipleCandidates = 0;
308
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000309 computeDependence(Ctx);
Abramo Bagnara25777432010-08-11 22:01:17 +0000310}
311
Douglas Gregora2813ce2009-10-23 18:54:35 +0000312DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000313 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000314 SourceLocation TemplateKWLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000315 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000316 bool RefersToEnclosingLocal,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000317 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000318 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000319 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000320 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000321 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000322 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000323 RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000324 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000325 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000326}
327
328DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000329 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000330 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000331 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000332 bool RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000333 const DeclarationNameInfo &NameInfo,
334 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000335 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000336 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000337 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000338 // Filter out cases where the found Decl is the same as the value refenenced.
339 if (D == FoundD)
340 FoundD = 0;
341
Douglas Gregora2813ce2009-10-23 18:54:35 +0000342 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000343 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000344 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000345 if (FoundD)
346 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000347 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000348 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
349 else if (TemplateKWLoc.isValid())
350 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000351
Chris Lattner32488542010-10-30 05:14:06 +0000352 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000353 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000354 RefersToEnclosingLocal,
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000355 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000356}
357
Chandler Carruth3aa81402011-05-01 23:48:14 +0000358DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000359 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000360 bool HasFoundDecl,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000361 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000362 unsigned NumTemplateArgs) {
363 std::size_t Size = sizeof(DeclRefExpr);
364 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000365 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000366 if (HasFoundDecl)
367 Size += sizeof(NamedDecl *);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000368 if (HasTemplateKWAndArgsInfo)
369 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000370
Chris Lattner32488542010-10-30 05:14:06 +0000371 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000372 return new (Mem) DeclRefExpr(EmptyShell());
373}
374
Douglas Gregora2813ce2009-10-23 18:54:35 +0000375SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000376 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000377 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000378 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000379 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000380 R.setEnd(getRAngleLoc());
381 return R;
382}
Daniel Dunbar396ec672012-03-09 15:39:15 +0000383SourceLocation DeclRefExpr::getLocStart() const {
384 if (hasQualifier())
385 return getQualifierLoc().getBeginLoc();
386 return getNameInfo().getLocStart();
387}
388SourceLocation DeclRefExpr::getLocEnd() const {
389 if (hasExplicitTemplateArgs())
390 return getRAngleLoc();
391 return getNameInfo().getLocEnd();
392}
Douglas Gregora2813ce2009-10-23 18:54:35 +0000393
Anders Carlsson3a082d82009-09-08 18:24:21 +0000394// FIXME: Maybe this should use DeclPrinter with a special "print predefined
395// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000396std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
397 ASTContext &Context = CurrentDecl->getASTContext();
398
Anders Carlsson3a082d82009-09-08 18:24:21 +0000399 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000400 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000401 return FD->getNameAsString();
402
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000403 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000404 llvm::raw_svector_ostream Out(Name);
405
406 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000407 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000408 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000409 if (MD->isStatic())
410 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000411 }
412
David Blaikie4e4d0842012-03-11 07:00:24 +0000413 PrintingPolicy Policy(Context.getLangOpts());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000414 std::string Proto = FD->getQualifiedNameAsString(Policy);
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000415 llvm::raw_string_ostream POut(Proto);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000416
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000417 const FunctionDecl *Decl = FD;
418 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
419 Decl = Pattern;
420 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000421 const FunctionProtoType *FT = 0;
422 if (FD->hasWrittenPrototype())
423 FT = dyn_cast<FunctionProtoType>(AFT);
424
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000425 POut << "(";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000426 if (FT) {
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000427 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000428 if (i) POut << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000429 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000430 }
431
432 if (FT->isVariadic()) {
433 if (FD->getNumParams()) POut << ", ";
434 POut << "...";
435 }
436 }
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000437 POut << ")";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000438
Sam Weinig4eadcc52009-12-27 01:38:20 +0000439 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
440 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
441 if (ThisQuals.hasConst())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000442 POut << " const";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000443 if (ThisQuals.hasVolatile())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000444 POut << " volatile";
445 RefQualifierKind Ref = MD->getRefQualifier();
446 if (Ref == RQ_LValue)
447 POut << " &";
448 else if (Ref == RQ_RValue)
449 POut << " &&";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000450 }
451
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000452 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
453 SpecsTy Specs;
454 const DeclContext *Ctx = FD->getDeclContext();
455 while (Ctx && isa<NamedDecl>(Ctx)) {
456 const ClassTemplateSpecializationDecl *Spec
457 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
458 if (Spec && !Spec->isExplicitSpecialization())
459 Specs.push_back(Spec);
460 Ctx = Ctx->getParent();
461 }
462
463 std::string TemplateParams;
464 llvm::raw_string_ostream TOut(TemplateParams);
465 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
466 I != E; ++I) {
467 const TemplateParameterList *Params
468 = (*I)->getSpecializedTemplate()->getTemplateParameters();
469 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
470 assert(Params->size() == Args.size());
471 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
472 StringRef Param = Params->getParam(i)->getName();
473 if (Param.empty()) continue;
474 TOut << Param << " = ";
475 Args.get(i).print(Policy, TOut);
476 TOut << ", ";
477 }
478 }
479
480 FunctionTemplateSpecializationInfo *FSI
481 = FD->getTemplateSpecializationInfo();
482 if (FSI && !FSI->isExplicitSpecialization()) {
483 const TemplateParameterList* Params
484 = FSI->getTemplate()->getTemplateParameters();
485 const TemplateArgumentList* Args = FSI->TemplateArguments;
486 assert(Params->size() == Args->size());
487 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
488 StringRef Param = Params->getParam(i)->getName();
489 if (Param.empty()) continue;
490 TOut << Param << " = ";
491 Args->get(i).print(Policy, TOut);
492 TOut << ", ";
493 }
494 }
495
496 TOut.flush();
497 if (!TemplateParams.empty()) {
498 // remove the trailing comma and space
499 TemplateParams.resize(TemplateParams.size() - 2);
500 POut << " [" << TemplateParams << "]";
501 }
502
503 POut.flush();
504
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000505 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
506 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000507
508 Out << Proto;
509
510 Out.flush();
511 return Name.str().str();
512 }
513 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000514 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000515 llvm::raw_svector_ostream Out(Name);
516 Out << (MD->isInstanceMethod() ? '-' : '+');
517 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000518
519 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
520 // a null check to avoid a crash.
521 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000522 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000523
Anders Carlsson3a082d82009-09-08 18:24:21 +0000524 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000525 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000526 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000527
Anders Carlsson3a082d82009-09-08 18:24:21 +0000528 Out << ' ';
529 Out << MD->getSelector().getAsString();
530 Out << ']';
531
532 Out.flush();
533 return Name.str().str();
534 }
535 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
536 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
537 return "top level";
538 }
539 return "";
540}
541
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000542void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
543 if (hasAllocation())
544 C.Deallocate(pVal);
545
546 BitWidth = Val.getBitWidth();
547 unsigned NumWords = Val.getNumWords();
548 const uint64_t* Words = Val.getRawData();
549 if (NumWords > 1) {
550 pVal = new (C) uint64_t[NumWords];
551 std::copy(Words, Words + NumWords, pVal);
552 } else if (NumWords == 1)
553 VAL = Words[0];
554 else
555 VAL = 0;
556}
557
Benjamin Kramer478851c2012-07-04 17:04:04 +0000558IntegerLiteral::IntegerLiteral(ASTContext &C, const llvm::APInt &V,
559 QualType type, SourceLocation l)
560 : Expr(IntegerLiteralClass, type, VK_RValue, OK_Ordinary, false, false,
561 false, false),
562 Loc(l) {
563 assert(type->isIntegerType() && "Illegal type in IntegerLiteral");
564 assert(V.getBitWidth() == C.getIntWidth(type) &&
565 "Integer type is not the correct size for constant.");
566 setValue(C, V);
567}
568
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000569IntegerLiteral *
570IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
571 QualType type, SourceLocation l) {
572 return new (C) IntegerLiteral(C, V, type, l);
573}
574
575IntegerLiteral *
576IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
577 return new (C) IntegerLiteral(Empty);
578}
579
Benjamin Kramer478851c2012-07-04 17:04:04 +0000580FloatingLiteral::FloatingLiteral(ASTContext &C, const llvm::APFloat &V,
581 bool isexact, QualType Type, SourceLocation L)
582 : Expr(FloatingLiteralClass, Type, VK_RValue, OK_Ordinary, false, false,
583 false, false), Loc(L) {
584 FloatingLiteralBits.IsIEEE =
585 &C.getTargetInfo().getLongDoubleFormat() == &llvm::APFloat::IEEEquad;
586 FloatingLiteralBits.IsExact = isexact;
587 setValue(C, V);
588}
589
590FloatingLiteral::FloatingLiteral(ASTContext &C, EmptyShell Empty)
591 : Expr(FloatingLiteralClass, Empty) {
592 FloatingLiteralBits.IsIEEE =
593 &C.getTargetInfo().getLongDoubleFormat() == &llvm::APFloat::IEEEquad;
594 FloatingLiteralBits.IsExact = false;
595}
596
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000597FloatingLiteral *
598FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
599 bool isexact, QualType Type, SourceLocation L) {
600 return new (C) FloatingLiteral(C, V, isexact, Type, L);
601}
602
603FloatingLiteral *
604FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000605 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000606}
607
Chris Lattnerda8249e2008-06-07 22:13:43 +0000608/// getValueAsApproximateDouble - This returns the value as an inaccurate
609/// double. Note that this may cause loss of precision, but is useful for
610/// debugging dumps, etc.
611double FloatingLiteral::getValueAsApproximateDouble() const {
612 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000613 bool ignored;
614 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
615 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000616 return V.convertToDouble();
617}
618
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000619int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000620 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000621 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000622 case Ascii:
623 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000624 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000625 break;
626 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000627 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000628 break;
629 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000630 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000631 break;
632 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000633 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000634 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000635 }
636 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
637 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000638 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000639 && "character byte widths supported are 1, 2, and 4 only");
640 return CharByteWidth;
641}
642
Chris Lattner5f9e2722011-07-23 10:55:15 +0000643StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000644 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000645 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000646 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000647 // Allocate enough space for the StringLiteral plus an array of locations for
648 // any concatenated string tokens.
649 void *Mem = C.Allocate(sizeof(StringLiteral)+
650 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000651 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000652 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000653
Reid Spencer5f016e22007-07-11 17:01:13 +0000654 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000655 SL->setString(C,Str,Kind,Pascal);
656
Chris Lattner2085fd62009-02-18 06:40:38 +0000657 SL->TokLocs[0] = Loc[0];
658 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000659
Chris Lattner726e1682009-02-18 05:49:11 +0000660 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000661 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
662 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000663}
664
Douglas Gregor673ecd62009-04-15 16:35:07 +0000665StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
666 void *Mem = C.Allocate(sizeof(StringLiteral)+
667 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000668 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000669 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000670 SL->CharByteWidth = 0;
671 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000672 SL->NumConcatenated = NumStrs;
673 return SL;
674}
675
Richard Trieu8ab09da2012-06-13 20:25:24 +0000676void StringLiteral::outputString(raw_ostream &OS) {
677 switch (getKind()) {
678 case Ascii: break; // no prefix.
679 case Wide: OS << 'L'; break;
680 case UTF8: OS << "u8"; break;
681 case UTF16: OS << 'u'; break;
682 case UTF32: OS << 'U'; break;
683 }
684 OS << '"';
685 static const char Hex[] = "0123456789ABCDEF";
686
687 unsigned LastSlashX = getLength();
688 for (unsigned I = 0, N = getLength(); I != N; ++I) {
689 switch (uint32_t Char = getCodeUnit(I)) {
690 default:
691 // FIXME: Convert UTF-8 back to codepoints before rendering.
692
693 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
694 // Leave invalid surrogates alone; we'll use \x for those.
695 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
696 Char <= 0xdbff) {
697 uint32_t Trail = getCodeUnit(I + 1);
698 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
699 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
700 ++I;
701 }
702 }
703
704 if (Char > 0xff) {
705 // If this is a wide string, output characters over 0xff using \x
706 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
707 // codepoint: use \x escapes for invalid codepoints.
708 if (getKind() == Wide ||
709 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
710 // FIXME: Is this the best way to print wchar_t?
711 OS << "\\x";
712 int Shift = 28;
713 while ((Char >> Shift) == 0)
714 Shift -= 4;
715 for (/**/; Shift >= 0; Shift -= 4)
716 OS << Hex[(Char >> Shift) & 15];
717 LastSlashX = I;
718 break;
719 }
720
721 if (Char > 0xffff)
722 OS << "\\U00"
723 << Hex[(Char >> 20) & 15]
724 << Hex[(Char >> 16) & 15];
725 else
726 OS << "\\u";
727 OS << Hex[(Char >> 12) & 15]
728 << Hex[(Char >> 8) & 15]
729 << Hex[(Char >> 4) & 15]
730 << Hex[(Char >> 0) & 15];
731 break;
732 }
733
734 // If we used \x... for the previous character, and this character is a
735 // hexadecimal digit, prevent it being slurped as part of the \x.
736 if (LastSlashX + 1 == I) {
737 switch (Char) {
738 case '0': case '1': case '2': case '3': case '4':
739 case '5': case '6': case '7': case '8': case '9':
740 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
741 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
742 OS << "\"\"";
743 }
744 }
745
746 assert(Char <= 0xff &&
747 "Characters above 0xff should already have been handled.");
748
749 if (isprint(Char))
750 OS << (char)Char;
751 else // Output anything hard as an octal escape.
752 OS << '\\'
753 << (char)('0' + ((Char >> 6) & 7))
754 << (char)('0' + ((Char >> 3) & 7))
755 << (char)('0' + ((Char >> 0) & 7));
756 break;
757 // Handle some common non-printable cases to make dumps prettier.
758 case '\\': OS << "\\\\"; break;
759 case '"': OS << "\\\""; break;
760 case '\n': OS << "\\n"; break;
761 case '\t': OS << "\\t"; break;
762 case '\a': OS << "\\a"; break;
763 case '\b': OS << "\\b"; break;
764 }
765 }
766 OS << '"';
767}
768
Eli Friedman64f45a22011-11-01 02:23:42 +0000769void StringLiteral::setString(ASTContext &C, StringRef Str,
770 StringKind Kind, bool IsPascal) {
771 //FIXME: we assume that the string data comes from a target that uses the same
772 // code unit size and endianess for the type of string.
773 this->Kind = Kind;
774 this->IsPascal = IsPascal;
775
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000776 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000777 assert((Str.size()%CharByteWidth == 0)
778 && "size of data must be multiple of CharByteWidth");
779 Length = Str.size()/CharByteWidth;
780
781 switch(CharByteWidth) {
782 case 1: {
783 char *AStrData = new (C) char[Length];
784 std::memcpy(AStrData,Str.data(),Str.size());
785 StrData.asChar = AStrData;
786 break;
787 }
788 case 2: {
789 uint16_t *AStrData = new (C) uint16_t[Length];
790 std::memcpy(AStrData,Str.data(),Str.size());
791 StrData.asUInt16 = AStrData;
792 break;
793 }
794 case 4: {
795 uint32_t *AStrData = new (C) uint32_t[Length];
796 std::memcpy(AStrData,Str.data(),Str.size());
797 StrData.asUInt32 = AStrData;
798 break;
799 }
800 default:
801 assert(false && "unsupported CharByteWidth");
802 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000803}
804
Chris Lattner08f92e32010-11-17 07:37:15 +0000805/// getLocationOfByte - Return a source location that points to the specified
806/// byte of this string literal.
807///
808/// Strings are amazingly complex. They can be formed from multiple tokens and
809/// can have escape sequences in them in addition to the usual trigraph and
810/// escaped newline business. This routine handles this complexity.
811///
812SourceLocation StringLiteral::
813getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
814 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000815 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
816 "Only narrow string literals are currently supported");
Douglas Gregor5cee1192011-07-27 05:40:30 +0000817
Chris Lattner08f92e32010-11-17 07:37:15 +0000818 // Loop over all of the tokens in this string until we find the one that
819 // contains the byte we're looking for.
820 unsigned TokNo = 0;
821 while (1) {
822 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
823 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
824
825 // Get the spelling of the string so that we can get the data that makes up
826 // the string literal, not the identifier for the macro it is potentially
827 // expanded through.
828 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
829
830 // Re-lex the token to get its length and original spelling.
831 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
832 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000833 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000834 if (Invalid)
835 return StrTokSpellingLoc;
836
837 const char *StrData = Buffer.data()+LocInfo.second;
838
Chris Lattner08f92e32010-11-17 07:37:15 +0000839 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +0000840 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
841 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +0000842 Token TheTok;
843 TheLexer.LexFromRawLexer(TheTok);
844
845 // Use the StringLiteralParser to compute the length of the string in bytes.
846 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
847 unsigned TokNumBytes = SLP.GetStringLength();
848
849 // If the byte is in this token, return the location of the byte.
850 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000851 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000852 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
853
854 // Now that we know the offset of the token in the spelling, use the
855 // preprocessor to get the offset in the original source.
856 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
857 }
858
859 // Move to the next string token.
860 ++TokNo;
861 ByteNo -= TokNumBytes;
862 }
863}
864
865
866
Reid Spencer5f016e22007-07-11 17:01:13 +0000867/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
868/// corresponds to, e.g. "sizeof" or "[pre]++".
869const char *UnaryOperator::getOpcodeStr(Opcode Op) {
870 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000871 case UO_PostInc: return "++";
872 case UO_PostDec: return "--";
873 case UO_PreInc: return "++";
874 case UO_PreDec: return "--";
875 case UO_AddrOf: return "&";
876 case UO_Deref: return "*";
877 case UO_Plus: return "+";
878 case UO_Minus: return "-";
879 case UO_Not: return "~";
880 case UO_LNot: return "!";
881 case UO_Real: return "__real";
882 case UO_Imag: return "__imag";
883 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000885 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000886}
887
John McCall2de56d12010-08-25 11:45:40 +0000888UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000889UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
890 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000891 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000892 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
893 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
894 case OO_Amp: return UO_AddrOf;
895 case OO_Star: return UO_Deref;
896 case OO_Plus: return UO_Plus;
897 case OO_Minus: return UO_Minus;
898 case OO_Tilde: return UO_Not;
899 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000900 }
901}
902
903OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
904 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000905 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
906 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
907 case UO_AddrOf: return OO_Amp;
908 case UO_Deref: return OO_Star;
909 case UO_Plus: return OO_Plus;
910 case UO_Minus: return OO_Minus;
911 case UO_Not: return OO_Tilde;
912 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000913 default: return OO_None;
914 }
915}
916
917
Reid Spencer5f016e22007-07-11 17:01:13 +0000918//===----------------------------------------------------------------------===//
919// Postfix Operators.
920//===----------------------------------------------------------------------===//
921
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000922CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
923 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000924 SourceLocation rparenloc)
925 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000926 fn->isTypeDependent(),
927 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000928 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000929 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000930 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000932 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000933 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000934 for (unsigned i = 0; i != numargs; ++i) {
935 if (args[i]->isTypeDependent())
936 ExprBits.TypeDependent = true;
937 if (args[i]->isValueDependent())
938 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000939 if (args[i]->isInstantiationDependent())
940 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000941 if (args[i]->containsUnexpandedParameterPack())
942 ExprBits.ContainsUnexpandedParameterPack = true;
943
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000944 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000945 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000946
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000947 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000948 RParenLoc = rparenloc;
949}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000950
Ted Kremenek668bf912009-02-09 20:51:47 +0000951CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000952 QualType t, ExprValueKind VK, SourceLocation rparenloc)
953 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000954 fn->isTypeDependent(),
955 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000956 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000957 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000958 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000959
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000960 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000961 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000962 for (unsigned i = 0; i != numargs; ++i) {
963 if (args[i]->isTypeDependent())
964 ExprBits.TypeDependent = true;
965 if (args[i]->isValueDependent())
966 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000967 if (args[i]->isInstantiationDependent())
968 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000969 if (args[i]->containsUnexpandedParameterPack())
970 ExprBits.ContainsUnexpandedParameterPack = true;
971
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000972 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000973 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000974
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000975 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 RParenLoc = rparenloc;
977}
978
Mike Stump1eb44332009-09-09 15:08:12 +0000979CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
980 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000981 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000982 SubExprs = new (C) Stmt*[PREARGS_START];
983 CallExprBits.NumPreArgs = 0;
984}
985
986CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
987 EmptyShell Empty)
988 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
989 // FIXME: Why do we allocate this?
990 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
991 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000992}
993
Nuno Lopesd20254f2009-12-20 23:11:08 +0000994Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000995 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000996
997 while (SubstNonTypeTemplateParmExpr *NTTP
998 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
999 CEE = NTTP->getReplacement()->IgnoreParenCasts();
1000 }
1001
Sebastian Redl20012152010-09-10 20:55:30 +00001002 // If we're calling a dereference, look at the pointer instead.
1003 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
1004 if (BO->isPtrMemOp())
1005 CEE = BO->getRHS()->IgnoreParenCasts();
1006 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
1007 if (UO->getOpcode() == UO_Deref)
1008 CEE = UO->getSubExpr()->IgnoreParenCasts();
1009 }
Chris Lattner6346f962009-07-17 15:46:27 +00001010 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +00001011 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +00001012 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
1013 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +00001014
1015 return 0;
1016}
1017
Nuno Lopesd20254f2009-12-20 23:11:08 +00001018FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +00001019 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +00001020}
1021
Chris Lattnerd18b3292007-12-28 05:25:02 +00001022/// setNumArgs - This changes the number of arguments present in this call.
1023/// Any orphaned expressions are deleted by this, and any new operands are set
1024/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +00001025void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001026 // No change, just return.
1027 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Chris Lattnerd18b3292007-12-28 05:25:02 +00001029 // If shrinking # arguments, just delete the extras and forgot them.
1030 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +00001031 this->NumArgs = NumArgs;
1032 return;
1033 }
1034
1035 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001036 unsigned NumPreArgs = getNumPreArgs();
1037 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +00001038 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001039 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001040 NewSubExprs[i] = SubExprs[i];
1041 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001042 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1043 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001044 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001045
Douglas Gregor88c9a462009-04-17 21:46:47 +00001046 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +00001047 SubExprs = NewSubExprs;
1048 this->NumArgs = NumArgs;
1049}
1050
Chris Lattnercb888962008-10-06 05:00:53 +00001051/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1052/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +00001053unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001054 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +00001055 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001056 // ImplicitCastExpr.
1057 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1058 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +00001059 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001061 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1062 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +00001063 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Anders Carlssonbcba2012008-01-31 02:13:57 +00001065 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1066 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +00001067 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001068
Douglas Gregor4fcd3992008-11-21 15:30:19 +00001069 if (!FDecl->getIdentifier())
1070 return 0;
1071
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001072 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +00001073}
Anders Carlssonbcba2012008-01-31 02:13:57 +00001074
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001075QualType CallExpr::getCallReturnType() const {
1076 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001077 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001078 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001079 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001080 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +00001081 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1082 // This should never be overloaded and so should never return null.
1083 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001084
John McCall864c0412011-04-26 20:42:42 +00001085 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001086 return FnType->getResultType();
1087}
Chris Lattnercb888962008-10-06 05:00:53 +00001088
John McCall2882eca2011-02-21 06:23:05 +00001089SourceRange CallExpr::getSourceRange() const {
1090 if (isa<CXXOperatorCallExpr>(this))
1091 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
1092
1093 SourceLocation begin = getCallee()->getLocStart();
1094 if (begin.isInvalid() && getNumArgs() > 0)
1095 begin = getArg(0)->getLocStart();
1096 SourceLocation end = getRParenLoc();
1097 if (end.isInvalid() && getNumArgs() > 0)
1098 end = getArg(getNumArgs() - 1)->getLocEnd();
1099 return SourceRange(begin, end);
1100}
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001101SourceLocation CallExpr::getLocStart() const {
1102 if (isa<CXXOperatorCallExpr>(this))
1103 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getBegin();
1104
1105 SourceLocation begin = getCallee()->getLocStart();
1106 if (begin.isInvalid() && getNumArgs() > 0)
1107 begin = getArg(0)->getLocStart();
1108 return begin;
1109}
1110SourceLocation CallExpr::getLocEnd() const {
1111 if (isa<CXXOperatorCallExpr>(this))
1112 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getEnd();
1113
1114 SourceLocation end = getRParenLoc();
1115 if (end.isInvalid() && getNumArgs() > 0)
1116 end = getArg(getNumArgs() - 1)->getLocEnd();
1117 return end;
1118}
John McCall2882eca2011-02-21 06:23:05 +00001119
Sean Huntc3021132010-05-05 15:23:54 +00001120OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001121 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +00001122 TypeSourceInfo *tsi,
1123 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001124 Expr** exprsPtr, unsigned numExprs,
1125 SourceLocation RParenLoc) {
1126 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +00001127 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001128 sizeof(Expr*) * numExprs);
1129
1130 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
1131 exprsPtr, numExprs, RParenLoc);
1132}
1133
1134OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
1135 unsigned numComps, unsigned numExprs) {
1136 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1137 sizeof(OffsetOfNode) * numComps +
1138 sizeof(Expr*) * numExprs);
1139 return new (Mem) OffsetOfExpr(numComps, numExprs);
1140}
1141
Sean Huntc3021132010-05-05 15:23:54 +00001142OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001143 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +00001144 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001145 Expr** exprsPtr, unsigned numExprs,
1146 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001147 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1148 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001149 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001150 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001151 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001152 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
1153 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001154{
1155 for(unsigned i = 0; i < numComps; ++i) {
1156 setComponent(i, compsPtr[i]);
1157 }
Sean Huntc3021132010-05-05 15:23:54 +00001158
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001159 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001160 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
1161 ExprBits.ValueDependent = true;
1162 if (exprsPtr[i]->containsUnexpandedParameterPack())
1163 ExprBits.ContainsUnexpandedParameterPack = true;
1164
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001165 setIndexExpr(i, exprsPtr[i]);
1166 }
1167}
1168
1169IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1170 assert(getKind() == Field || getKind() == Identifier);
1171 if (getKind() == Field)
1172 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001173
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001174 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1175}
1176
Mike Stump1eb44332009-09-09 15:08:12 +00001177MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001178 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001179 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001180 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001181 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001182 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001183 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001184 QualType ty,
1185 ExprValueKind vk,
1186 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001187 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001188
Douglas Gregor40d96a62011-02-28 21:54:11 +00001189 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001190 founddecl.getDecl() != memberdecl ||
1191 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001192 if (hasQualOrFound)
1193 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001194
John McCalld5532b62009-11-23 01:53:49 +00001195 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001196 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1197 else if (TemplateKWLoc.isValid())
1198 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Chris Lattner32488542010-10-30 05:14:06 +00001200 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001201 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1202 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001203
1204 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001205 // FIXME: Wrong. We should be looking at the member declaration we found.
1206 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001207 E->setValueDependent(true);
1208 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001209 E->setInstantiationDependent(true);
1210 }
1211 else if (QualifierLoc &&
1212 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1213 E->setInstantiationDependent(true);
1214
John McCall6bb80172010-03-30 21:47:33 +00001215 E->HasQualifierOrFoundDecl = true;
1216
1217 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001218 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001219 NQ->FoundDecl = founddecl;
1220 }
1221
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001222 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1223
John McCall6bb80172010-03-30 21:47:33 +00001224 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001225 bool Dependent = false;
1226 bool InstantiationDependent = false;
1227 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001228 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1229 Dependent,
1230 InstantiationDependent,
1231 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001232 if (InstantiationDependent)
1233 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001234 } else if (TemplateKWLoc.isValid()) {
1235 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001236 }
1237
1238 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001239}
1240
Douglas Gregor75e85042011-03-02 21:06:53 +00001241SourceRange MemberExpr::getSourceRange() const {
Daniel Dunbar396ec672012-03-09 15:39:15 +00001242 return SourceRange(getLocStart(), getLocEnd());
1243}
1244SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001245 if (isImplicitAccess()) {
1246 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001247 return getQualifierLoc().getBeginLoc();
1248 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001249 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001250
Daniel Dunbar396ec672012-03-09 15:39:15 +00001251 // FIXME: We don't want this to happen. Rather, we should be able to
1252 // detect all kinds of implicit accesses more cleanly.
1253 SourceLocation BaseStartLoc = getBase()->getLocStart();
1254 if (BaseStartLoc.isValid())
1255 return BaseStartLoc;
1256 return MemberLoc;
1257}
1258SourceLocation MemberExpr::getLocEnd() const {
1259 if (hasExplicitTemplateArgs())
1260 return getRAngleLoc();
1261 return getMemberNameInfo().getEndLoc();
Douglas Gregor75e85042011-03-02 21:06:53 +00001262}
1263
John McCall1d9b3b22011-09-09 05:25:32 +00001264void CastExpr::CheckCastConsistency() const {
1265 switch (getCastKind()) {
1266 case CK_DerivedToBase:
1267 case CK_UncheckedDerivedToBase:
1268 case CK_DerivedToBaseMemberPointer:
1269 case CK_BaseToDerived:
1270 case CK_BaseToDerivedMemberPointer:
1271 assert(!path_empty() && "Cast kind should have a base path!");
1272 break;
1273
1274 case CK_CPointerToObjCPointerCast:
1275 assert(getType()->isObjCObjectPointerType());
1276 assert(getSubExpr()->getType()->isPointerType());
1277 goto CheckNoBasePath;
1278
1279 case CK_BlockPointerToObjCPointerCast:
1280 assert(getType()->isObjCObjectPointerType());
1281 assert(getSubExpr()->getType()->isBlockPointerType());
1282 goto CheckNoBasePath;
1283
John McCall4d4e5c12012-02-15 01:22:51 +00001284 case CK_ReinterpretMemberPointer:
1285 assert(getType()->isMemberPointerType());
1286 assert(getSubExpr()->getType()->isMemberPointerType());
1287 goto CheckNoBasePath;
1288
John McCall1d9b3b22011-09-09 05:25:32 +00001289 case CK_BitCast:
1290 // Arbitrary casts to C pointer types count as bitcasts.
1291 // Otherwise, we should only have block and ObjC pointer casts
1292 // here if they stay within the type kind.
1293 if (!getType()->isPointerType()) {
1294 assert(getType()->isObjCObjectPointerType() ==
1295 getSubExpr()->getType()->isObjCObjectPointerType());
1296 assert(getType()->isBlockPointerType() ==
1297 getSubExpr()->getType()->isBlockPointerType());
1298 }
1299 goto CheckNoBasePath;
1300
1301 case CK_AnyPointerToBlockPointerCast:
1302 assert(getType()->isBlockPointerType());
1303 assert(getSubExpr()->getType()->isAnyPointerType() &&
1304 !getSubExpr()->getType()->isBlockPointerType());
1305 goto CheckNoBasePath;
1306
Douglas Gregorac1303e2012-02-22 05:02:47 +00001307 case CK_CopyAndAutoreleaseBlockObject:
1308 assert(getType()->isBlockPointerType());
1309 assert(getSubExpr()->getType()->isBlockPointerType());
1310 goto CheckNoBasePath;
1311
John McCall1d9b3b22011-09-09 05:25:32 +00001312 // These should not have an inheritance path.
1313 case CK_Dynamic:
1314 case CK_ToUnion:
1315 case CK_ArrayToPointerDecay:
1316 case CK_FunctionToPointerDecay:
1317 case CK_NullToMemberPointer:
1318 case CK_NullToPointer:
1319 case CK_ConstructorConversion:
1320 case CK_IntegralToPointer:
1321 case CK_PointerToIntegral:
1322 case CK_ToVoid:
1323 case CK_VectorSplat:
1324 case CK_IntegralCast:
1325 case CK_IntegralToFloating:
1326 case CK_FloatingToIntegral:
1327 case CK_FloatingCast:
1328 case CK_ObjCObjectLValueCast:
1329 case CK_FloatingRealToComplex:
1330 case CK_FloatingComplexToReal:
1331 case CK_FloatingComplexCast:
1332 case CK_FloatingComplexToIntegralComplex:
1333 case CK_IntegralRealToComplex:
1334 case CK_IntegralComplexToReal:
1335 case CK_IntegralComplexCast:
1336 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001337 case CK_ARCProduceObject:
1338 case CK_ARCConsumeObject:
1339 case CK_ARCReclaimReturnedObject:
1340 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001341 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1342 goto CheckNoBasePath;
1343
1344 case CK_Dependent:
1345 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001346 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001347 case CK_AtomicToNonAtomic:
1348 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001349 case CK_PointerToBoolean:
1350 case CK_IntegralToBoolean:
1351 case CK_FloatingToBoolean:
1352 case CK_MemberPointerToBoolean:
1353 case CK_FloatingComplexToBoolean:
1354 case CK_IntegralComplexToBoolean:
1355 case CK_LValueBitCast: // -> bool&
1356 case CK_UserDefinedConversion: // operator bool()
1357 CheckNoBasePath:
1358 assert(path_empty() && "Cast kind should not have a base path!");
1359 break;
1360 }
1361}
1362
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001363const char *CastExpr::getCastKindName() const {
1364 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001365 case CK_Dependent:
1366 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001367 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001368 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001369 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001370 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001371 case CK_LValueToRValue:
1372 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001373 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001374 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001375 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001376 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001377 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001378 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001379 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001380 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001381 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001382 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001383 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001384 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001385 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001386 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001387 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001388 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001389 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001390 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001391 case CK_NullToPointer:
1392 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001393 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001394 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001395 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001396 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001397 case CK_ReinterpretMemberPointer:
1398 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001399 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001400 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001401 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001402 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001403 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001404 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001405 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001406 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001407 case CK_PointerToBoolean:
1408 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001409 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001410 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001411 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001412 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001413 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001414 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001415 case CK_IntegralToBoolean:
1416 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001417 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001418 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001419 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001420 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001421 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001422 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001423 case CK_FloatingToBoolean:
1424 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001425 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001426 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001427 case CK_CPointerToObjCPointerCast:
1428 return "CPointerToObjCPointerCast";
1429 case CK_BlockPointerToObjCPointerCast:
1430 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001431 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001432 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001433 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001434 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001435 case CK_FloatingRealToComplex:
1436 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001437 case CK_FloatingComplexToReal:
1438 return "FloatingComplexToReal";
1439 case CK_FloatingComplexToBoolean:
1440 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001441 case CK_FloatingComplexCast:
1442 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001443 case CK_FloatingComplexToIntegralComplex:
1444 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001445 case CK_IntegralRealToComplex:
1446 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001447 case CK_IntegralComplexToReal:
1448 return "IntegralComplexToReal";
1449 case CK_IntegralComplexToBoolean:
1450 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001451 case CK_IntegralComplexCast:
1452 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001453 case CK_IntegralComplexToFloatingComplex:
1454 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001455 case CK_ARCConsumeObject:
1456 return "ARCConsumeObject";
1457 case CK_ARCProduceObject:
1458 return "ARCProduceObject";
1459 case CK_ARCReclaimReturnedObject:
1460 return "ARCReclaimReturnedObject";
1461 case CK_ARCExtendBlockObject:
1462 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001463 case CK_AtomicToNonAtomic:
1464 return "AtomicToNonAtomic";
1465 case CK_NonAtomicToAtomic:
1466 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001467 case CK_CopyAndAutoreleaseBlockObject:
1468 return "CopyAndAutoreleaseBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001469 }
Mike Stump1eb44332009-09-09 15:08:12 +00001470
John McCall2bb5d002010-11-13 09:02:35 +00001471 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001472}
1473
Douglas Gregor6eef5192009-12-14 19:27:10 +00001474Expr *CastExpr::getSubExprAsWritten() {
1475 Expr *SubExpr = 0;
1476 CastExpr *E = this;
1477 do {
1478 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001479
1480 // Skip through reference binding to temporary.
1481 if (MaterializeTemporaryExpr *Materialize
1482 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1483 SubExpr = Materialize->GetTemporaryExpr();
1484
Douglas Gregor6eef5192009-12-14 19:27:10 +00001485 // Skip any temporary bindings; they're implicit.
1486 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1487 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001488
Douglas Gregor6eef5192009-12-14 19:27:10 +00001489 // Conversions by constructor and conversion functions have a
1490 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001491 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001492 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001493 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001494 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001495
Douglas Gregor6eef5192009-12-14 19:27:10 +00001496 // If the subexpression we're left with is an implicit cast, look
1497 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001498 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1499
Douglas Gregor6eef5192009-12-14 19:27:10 +00001500 return SubExpr;
1501}
1502
John McCallf871d0c2010-08-07 06:22:56 +00001503CXXBaseSpecifier **CastExpr::path_buffer() {
1504 switch (getStmtClass()) {
1505#define ABSTRACT_STMT(x)
1506#define CASTEXPR(Type, Base) \
1507 case Stmt::Type##Class: \
1508 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1509#define STMT(Type, Base)
1510#include "clang/AST/StmtNodes.inc"
1511 default:
1512 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001513 }
1514}
1515
1516void CastExpr::setCastPath(const CXXCastPath &Path) {
1517 assert(Path.size() == path_size());
1518 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1519}
1520
1521ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1522 CastKind Kind, Expr *Operand,
1523 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001524 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001525 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1526 void *Buffer =
1527 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1528 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001529 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001530 if (PathSize) E->setCastPath(*BasePath);
1531 return E;
1532}
1533
1534ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1535 unsigned PathSize) {
1536 void *Buffer =
1537 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1538 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1539}
1540
1541
1542CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001543 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001544 const CXXCastPath *BasePath,
1545 TypeSourceInfo *WrittenTy,
1546 SourceLocation L, SourceLocation R) {
1547 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1548 void *Buffer =
1549 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1550 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001551 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001552 if (PathSize) E->setCastPath(*BasePath);
1553 return E;
1554}
1555
1556CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1557 void *Buffer =
1558 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1559 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1560}
1561
Reid Spencer5f016e22007-07-11 17:01:13 +00001562/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1563/// corresponds to, e.g. "<<=".
1564const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1565 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001566 case BO_PtrMemD: return ".*";
1567 case BO_PtrMemI: return "->*";
1568 case BO_Mul: return "*";
1569 case BO_Div: return "/";
1570 case BO_Rem: return "%";
1571 case BO_Add: return "+";
1572 case BO_Sub: return "-";
1573 case BO_Shl: return "<<";
1574 case BO_Shr: return ">>";
1575 case BO_LT: return "<";
1576 case BO_GT: return ">";
1577 case BO_LE: return "<=";
1578 case BO_GE: return ">=";
1579 case BO_EQ: return "==";
1580 case BO_NE: return "!=";
1581 case BO_And: return "&";
1582 case BO_Xor: return "^";
1583 case BO_Or: return "|";
1584 case BO_LAnd: return "&&";
1585 case BO_LOr: return "||";
1586 case BO_Assign: return "=";
1587 case BO_MulAssign: return "*=";
1588 case BO_DivAssign: return "/=";
1589 case BO_RemAssign: return "%=";
1590 case BO_AddAssign: return "+=";
1591 case BO_SubAssign: return "-=";
1592 case BO_ShlAssign: return "<<=";
1593 case BO_ShrAssign: return ">>=";
1594 case BO_AndAssign: return "&=";
1595 case BO_XorAssign: return "^=";
1596 case BO_OrAssign: return "|=";
1597 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001598 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001599
David Blaikie30263482012-01-20 21:50:17 +00001600 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001601}
1602
John McCall2de56d12010-08-25 11:45:40 +00001603BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001604BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1605 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001606 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001607 case OO_Plus: return BO_Add;
1608 case OO_Minus: return BO_Sub;
1609 case OO_Star: return BO_Mul;
1610 case OO_Slash: return BO_Div;
1611 case OO_Percent: return BO_Rem;
1612 case OO_Caret: return BO_Xor;
1613 case OO_Amp: return BO_And;
1614 case OO_Pipe: return BO_Or;
1615 case OO_Equal: return BO_Assign;
1616 case OO_Less: return BO_LT;
1617 case OO_Greater: return BO_GT;
1618 case OO_PlusEqual: return BO_AddAssign;
1619 case OO_MinusEqual: return BO_SubAssign;
1620 case OO_StarEqual: return BO_MulAssign;
1621 case OO_SlashEqual: return BO_DivAssign;
1622 case OO_PercentEqual: return BO_RemAssign;
1623 case OO_CaretEqual: return BO_XorAssign;
1624 case OO_AmpEqual: return BO_AndAssign;
1625 case OO_PipeEqual: return BO_OrAssign;
1626 case OO_LessLess: return BO_Shl;
1627 case OO_GreaterGreater: return BO_Shr;
1628 case OO_LessLessEqual: return BO_ShlAssign;
1629 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1630 case OO_EqualEqual: return BO_EQ;
1631 case OO_ExclaimEqual: return BO_NE;
1632 case OO_LessEqual: return BO_LE;
1633 case OO_GreaterEqual: return BO_GE;
1634 case OO_AmpAmp: return BO_LAnd;
1635 case OO_PipePipe: return BO_LOr;
1636 case OO_Comma: return BO_Comma;
1637 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001638 }
1639}
1640
1641OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1642 static const OverloadedOperatorKind OverOps[] = {
1643 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1644 OO_Star, OO_Slash, OO_Percent,
1645 OO_Plus, OO_Minus,
1646 OO_LessLess, OO_GreaterGreater,
1647 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1648 OO_EqualEqual, OO_ExclaimEqual,
1649 OO_Amp,
1650 OO_Caret,
1651 OO_Pipe,
1652 OO_AmpAmp,
1653 OO_PipePipe,
1654 OO_Equal, OO_StarEqual,
1655 OO_SlashEqual, OO_PercentEqual,
1656 OO_PlusEqual, OO_MinusEqual,
1657 OO_LessLessEqual, OO_GreaterGreaterEqual,
1658 OO_AmpEqual, OO_CaretEqual,
1659 OO_PipeEqual,
1660 OO_Comma
1661 };
1662 return OverOps[Opc];
1663}
1664
Ted Kremenek709210f2010-04-13 23:39:13 +00001665InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001666 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001667 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001668 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001669 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001670 InitExprs(C, numInits),
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001671 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0)
1672{
1673 sawArrayRangeDesignator(false);
1674 setInitializesStdInitializerList(false);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001675 for (unsigned I = 0; I != numInits; ++I) {
1676 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001677 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001678 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001679 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001680 if (initExprs[I]->isInstantiationDependent())
1681 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001682 if (initExprs[I]->containsUnexpandedParameterPack())
1683 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001684 }
Sean Huntc3021132010-05-05 15:23:54 +00001685
Ted Kremenek709210f2010-04-13 23:39:13 +00001686 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001687}
Reid Spencer5f016e22007-07-11 17:01:13 +00001688
Ted Kremenek709210f2010-04-13 23:39:13 +00001689void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001690 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001691 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001692}
1693
Ted Kremenek709210f2010-04-13 23:39:13 +00001694void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001695 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001696}
1697
Ted Kremenek709210f2010-04-13 23:39:13 +00001698Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001699 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001700 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001701 InitExprs.back() = expr;
1702 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001703 }
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Douglas Gregor4c678342009-01-28 21:54:33 +00001705 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1706 InitExprs[Init] = expr;
1707 return Result;
1708}
1709
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001710void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001711 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001712 ArrayFillerOrUnionFieldInit = filler;
1713 // Fill out any "holes" in the array due to designated initializers.
1714 Expr **inits = getInits();
1715 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1716 if (inits[i] == 0)
1717 inits[i] = filler;
1718}
1719
Richard Smithfe587202012-04-15 02:50:59 +00001720bool InitListExpr::isStringLiteralInit() const {
1721 if (getNumInits() != 1)
1722 return false;
1723 const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(getType());
1724 if (!CAT || !CAT->getElementType()->isIntegerType())
1725 return false;
1726 const Expr *Init = getInit(0)->IgnoreParenImpCasts();
1727 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1728}
1729
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001730SourceRange InitListExpr::getSourceRange() const {
1731 if (SyntacticForm)
1732 return SyntacticForm->getSourceRange();
1733 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1734 if (Beg.isInvalid()) {
1735 // Find the first non-null initializer.
1736 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1737 E = InitExprs.end();
1738 I != E; ++I) {
1739 if (Stmt *S = *I) {
1740 Beg = S->getLocStart();
1741 break;
1742 }
1743 }
1744 }
1745 if (End.isInvalid()) {
1746 // Find the first non-null initializer from the end.
1747 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1748 E = InitExprs.rend();
1749 I != E; ++I) {
1750 if (Stmt *S = *I) {
1751 End = S->getSourceRange().getEnd();
1752 break;
1753 }
1754 }
1755 }
1756 return SourceRange(Beg, End);
1757}
1758
Steve Naroffbfdcae62008-09-04 15:31:07 +00001759/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001760///
John McCalla345edb2012-02-17 03:32:35 +00001761const FunctionProtoType *BlockExpr::getFunctionType() const {
1762 // The block pointer is never sugared, but the function type might be.
1763 return cast<BlockPointerType>(getType())
1764 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001765}
1766
Mike Stump1eb44332009-09-09 15:08:12 +00001767SourceLocation BlockExpr::getCaretLocation() const {
1768 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001769}
Mike Stump1eb44332009-09-09 15:08:12 +00001770const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001771 return TheBlock->getBody();
1772}
Mike Stump1eb44332009-09-09 15:08:12 +00001773Stmt *BlockExpr::getBody() {
1774 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001775}
Steve Naroff56ee6892008-10-08 17:01:13 +00001776
1777
Reid Spencer5f016e22007-07-11 17:01:13 +00001778//===----------------------------------------------------------------------===//
1779// Generic Expression Routines
1780//===----------------------------------------------------------------------===//
1781
Chris Lattner026dc962009-02-14 07:37:35 +00001782/// isUnusedResultAWarning - Return true if this immediate expression should
1783/// be warned about if the result is unused. If so, fill in Loc and Ranges
1784/// with location to warn on and the source range[s] to report with the
1785/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001786bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1787 SourceRange &R1, SourceRange &R2,
1788 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001789 // Don't warn if the expr is type dependent. The type could end up
1790 // instantiating to void.
1791 if (isTypeDependent())
1792 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Reid Spencer5f016e22007-07-11 17:01:13 +00001794 switch (getStmtClass()) {
1795 default:
John McCall0faede62010-03-12 07:11:26 +00001796 if (getType()->isVoidType())
1797 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001798 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001799 Loc = getExprLoc();
1800 R1 = getSourceRange();
1801 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001802 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001803 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001804 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001805 case GenericSelectionExprClass:
1806 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001807 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001808 case UnaryOperatorClass: {
1809 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Reid Spencer5f016e22007-07-11 17:01:13 +00001811 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001812 case UO_Plus:
1813 case UO_Minus:
1814 case UO_AddrOf:
1815 case UO_Not:
1816 case UO_LNot:
1817 case UO_Deref:
1818 break;
John McCall2de56d12010-08-25 11:45:40 +00001819 case UO_PostInc:
1820 case UO_PostDec:
1821 case UO_PreInc:
1822 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001823 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001824 case UO_Real:
1825 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001826 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001827 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1828 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001829 return false;
1830 break;
John McCall2de56d12010-08-25 11:45:40 +00001831 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00001832 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 }
Eli Friedmana6115062012-05-24 00:47:05 +00001834 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001835 Loc = UO->getOperatorLoc();
1836 R1 = UO->getSubExpr()->getSourceRange();
1837 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001839 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001840 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001841 switch (BO->getOpcode()) {
1842 default:
1843 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001844 // Consider the RHS of comma for side effects. LHS was checked by
1845 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001846 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001847 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1848 // lvalue-ness) of an assignment written in a macro.
1849 if (IntegerLiteral *IE =
1850 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1851 if (IE->getValue() == 0)
1852 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001853 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001854 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001855 case BO_LAnd:
1856 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00001857 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1858 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001859 return false;
1860 break;
John McCallbf0ee352010-02-16 04:10:53 +00001861 }
Chris Lattner026dc962009-02-14 07:37:35 +00001862 if (BO->isAssignmentOp())
1863 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001864 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001865 Loc = BO->getOperatorLoc();
1866 R1 = BO->getLHS()->getSourceRange();
1867 R2 = BO->getRHS()->getSourceRange();
1868 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001869 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001870 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001871 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001872 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001873 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001874
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001875 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001876 // If only one of the LHS or RHS is a warning, the operator might
1877 // be being used for control flow. Only warn if both the LHS and
1878 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001879 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00001880 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001881 return false;
1882 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001883 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00001884 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001885 }
1886
Reid Spencer5f016e22007-07-11 17:01:13 +00001887 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001888 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001889 Loc = cast<MemberExpr>(this)->getMemberLoc();
1890 R1 = SourceRange(Loc, Loc);
1891 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1892 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001893
Reid Spencer5f016e22007-07-11 17:01:13 +00001894 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001895 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001896 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1897 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1898 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1899 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001900
Chandler Carruth9b106832011-08-17 09:49:44 +00001901 case CXXOperatorCallExprClass: {
1902 // We warn about operator== and operator!= even when user-defined operator
1903 // overloads as there is no reasonable way to define these such that they
1904 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1905 // warning: these operators are commonly typo'ed, and so warning on them
1906 // provides additional value as well. If this list is updated,
1907 // DiagnoseUnusedComparison should be as well.
1908 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1909 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001910 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmana6115062012-05-24 00:47:05 +00001911 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001912 Loc = Op->getOperatorLoc();
1913 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001914 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001915 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001916
1917 // Fallthrough for generic call handling.
1918 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001919 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00001920 case CXXMemberCallExprClass:
1921 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001922 // If this is a direct call, get the callee.
1923 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001924 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001925 // If the callee has attribute pure, const, or warn_unused_result, warn
1926 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001927 //
1928 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1929 // updated to match for QoI.
1930 if (FD->getAttr<WarnUnusedResultAttr>() ||
1931 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001932 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001933 Loc = CE->getCallee()->getLocStart();
1934 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001936 if (unsigned NumArgs = CE->getNumArgs())
1937 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1938 CE->getArg(NumArgs-1)->getLocEnd());
1939 return true;
1940 }
Chris Lattner026dc962009-02-14 07:37:35 +00001941 }
1942 return false;
1943 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001944
1945 case CXXTemporaryObjectExprClass:
1946 case CXXConstructExprClass:
1947 return false;
1948
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001949 case ObjCMessageExprClass: {
1950 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00001951 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001952 ME->isInstanceMessage() &&
1953 !ME->getType()->isVoidType() &&
1954 ME->getSelector().getIdentifierInfoForSlot(0) &&
1955 ME->getSelector().getIdentifierInfoForSlot(0)
1956 ->getName().startswith("init")) {
Eli Friedmana6115062012-05-24 00:47:05 +00001957 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00001958 Loc = getExprLoc();
1959 R1 = ME->getSourceRange();
1960 return true;
1961 }
1962
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001963 const ObjCMethodDecl *MD = ME->getMethodDecl();
1964 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001965 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001966 Loc = getExprLoc();
1967 return true;
1968 }
Chris Lattner026dc962009-02-14 07:37:35 +00001969 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001970 }
Mike Stump1eb44332009-09-09 15:08:12 +00001971
John McCall12f78a62010-12-02 01:19:52 +00001972 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001973 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001974 Loc = getExprLoc();
1975 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001976 return true;
John McCall12f78a62010-12-02 01:19:52 +00001977
John McCall4b9c2d22011-11-06 09:01:30 +00001978 case PseudoObjectExprClass: {
1979 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1980
1981 // Only complain about things that have the form of a getter.
1982 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1983 isa<BinaryOperator>(PO->getSyntacticForm()))
1984 return false;
1985
Eli Friedmana6115062012-05-24 00:47:05 +00001986 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00001987 Loc = getExprLoc();
1988 R1 = getSourceRange();
1989 return true;
1990 }
1991
Chris Lattner611b2ec2008-07-26 19:51:01 +00001992 case StmtExprClass: {
1993 // Statement exprs don't logically have side effects themselves, but are
1994 // sometimes used in macros in ways that give them a type that is unused.
1995 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1996 // however, if the result of the stmt expr is dead, we don't want to emit a
1997 // warning.
1998 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001999 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00002000 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00002001 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002002 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
2003 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00002004 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00002005 }
Mike Stump1eb44332009-09-09 15:08:12 +00002006
John McCall0faede62010-03-12 07:11:26 +00002007 if (getType()->isVoidType())
2008 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002009 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00002010 Loc = cast<StmtExpr>(this)->getLParenLoc();
2011 R1 = getSourceRange();
2012 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00002013 }
Eli Friedmana6115062012-05-24 00:47:05 +00002014 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00002015 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00002016 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00002017 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00002018 if (CE->getCastKind() == CK_ToVoid) {
2019 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00002020 CE->getSubExpr()->getType().isVolatileQualified()) {
2021 const DeclRefExpr *DRE =
2022 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
2023 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
2024 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
2025 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
2026 R1, R2, Ctx);
2027 }
2028 }
Chris Lattnerfb846642009-07-28 18:25:28 +00002029 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00002030 }
Eli Friedman4059da82012-05-24 21:05:41 +00002031
Eli Friedmana6115062012-05-24 00:47:05 +00002032 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00002033 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00002034 if (CE->getCastKind() == CK_ConstructorConversion)
2035 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00002036
Eli Friedmana6115062012-05-24 00:47:05 +00002037 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00002038 if (const CXXFunctionalCastExpr *CXXCE =
2039 dyn_cast<CXXFunctionalCastExpr>(this)) {
2040 Loc = CXXCE->getTypeBeginLoc();
2041 R1 = CXXCE->getSubExpr()->getSourceRange();
2042 } else {
2043 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2044 Loc = CStyleCE->getLParenLoc();
2045 R1 = CStyleCE->getSubExpr()->getSourceRange();
2046 }
Chris Lattner026dc962009-02-14 07:37:35 +00002047 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00002048 }
Eli Friedmana6115062012-05-24 00:47:05 +00002049 case ImplicitCastExprClass: {
2050 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00002051
Eli Friedmana6115062012-05-24 00:47:05 +00002052 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2053 if (ICE->getCastKind() == CK_LValueToRValue &&
2054 ICE->getSubExpr()->getType().isVolatileQualified())
2055 return false;
2056
2057 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2058 }
Chris Lattner04421082008-04-08 04:40:51 +00002059 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002060 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002061 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002062
2063 case CXXNewExprClass:
2064 // FIXME: In theory, there might be new expressions that don't have side
2065 // effects (e.g. a placement new with an uninitialized POD).
2066 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002067 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00002068 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002069 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002070 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00002071 case ExprWithCleanupsClass:
2072 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002073 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002074 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002075}
2076
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002077/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00002078/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002079bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00002080 const Expr *E = IgnoreParens();
2081 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002082 default:
2083 return false;
2084 case ObjCIvarRefExprClass:
2085 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00002086 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002087 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002088 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002089 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00002090 case MaterializeTemporaryExprClass:
2091 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2092 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00002093 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002094 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00002095 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00002096 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00002097
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002098 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2099 if (VD->hasGlobalStorage())
2100 return true;
2101 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00002102 // dereferencing to a pointer is always a gc'able candidate,
2103 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002104 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00002105 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002106 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002107 return false;
2108 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00002109 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00002110 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002111 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002112 }
2113 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002114 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002115 }
2116}
Sebastian Redl369e51f2010-09-10 20:55:33 +00002117
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002118bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2119 if (isTypeDependent())
2120 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00002121 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002122}
2123
John McCall864c0412011-04-26 20:42:42 +00002124QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00002125 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00002126
2127 // Bound member expressions are always one of these possibilities:
2128 // x->m x.m x->*y x.*y
2129 // (possibly parenthesized)
2130
2131 expr = expr->IgnoreParens();
2132 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2133 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2134 return mem->getMemberDecl()->getType();
2135 }
2136
2137 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2138 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2139 ->getPointeeType();
2140 assert(type->isFunctionType());
2141 return type;
2142 }
2143
2144 assert(isa<UnresolvedMemberExpr>(expr));
2145 return QualType();
2146}
2147
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002148Expr* Expr::IgnoreParens() {
2149 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002150 while (true) {
2151 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2152 E = P->getSubExpr();
2153 continue;
2154 }
2155 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2156 if (P->getOpcode() == UO_Extension) {
2157 E = P->getSubExpr();
2158 continue;
2159 }
2160 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002161 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2162 if (!P->isResultDependent()) {
2163 E = P->getResultExpr();
2164 continue;
2165 }
2166 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002167 return E;
2168 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002169}
2170
Chris Lattner56f34942008-02-13 01:02:39 +00002171/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2172/// or CastExprs or ImplicitCastExprs, returning their operand.
2173Expr *Expr::IgnoreParenCasts() {
2174 Expr *E = this;
2175 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002176 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002177 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002178 continue;
2179 }
2180 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002181 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002182 continue;
2183 }
2184 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2185 if (P->getOpcode() == UO_Extension) {
2186 E = P->getSubExpr();
2187 continue;
2188 }
2189 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002190 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2191 if (!P->isResultDependent()) {
2192 E = P->getResultExpr();
2193 continue;
2194 }
2195 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002196 if (MaterializeTemporaryExpr *Materialize
2197 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2198 E = Materialize->GetTemporaryExpr();
2199 continue;
2200 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002201 if (SubstNonTypeTemplateParmExpr *NTTP
2202 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2203 E = NTTP->getReplacement();
2204 continue;
2205 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002206 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002207 }
2208}
2209
John McCall9c5d70c2010-12-04 08:24:19 +00002210/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2211/// casts. This is intended purely as a temporary workaround for code
2212/// that hasn't yet been rewritten to do the right thing about those
2213/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002214Expr *Expr::IgnoreParenLValueCasts() {
2215 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002216 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002217 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2218 E = P->getSubExpr();
2219 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002220 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002221 if (P->getCastKind() == CK_LValueToRValue) {
2222 E = P->getSubExpr();
2223 continue;
2224 }
John McCall9c5d70c2010-12-04 08:24:19 +00002225 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2226 if (P->getOpcode() == UO_Extension) {
2227 E = P->getSubExpr();
2228 continue;
2229 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002230 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2231 if (!P->isResultDependent()) {
2232 E = P->getResultExpr();
2233 continue;
2234 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002235 } else if (MaterializeTemporaryExpr *Materialize
2236 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2237 E = Materialize->GetTemporaryExpr();
2238 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002239 } else if (SubstNonTypeTemplateParmExpr *NTTP
2240 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2241 E = NTTP->getReplacement();
2242 continue;
John McCallf6a16482010-12-04 03:47:34 +00002243 }
2244 break;
2245 }
2246 return E;
2247}
Rafael Espindola632fbaa2012-06-28 01:56:38 +00002248
2249Expr *Expr::ignoreParenBaseCasts() {
2250 Expr *E = this;
2251 while (true) {
2252 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2253 E = P->getSubExpr();
2254 continue;
2255 }
2256 if (CastExpr *CE = dyn_cast<CastExpr>(E)) {
2257 if (CE->getCastKind() == CK_DerivedToBase ||
2258 CE->getCastKind() == CK_UncheckedDerivedToBase ||
2259 CE->getCastKind() == CK_NoOp) {
2260 E = CE->getSubExpr();
2261 continue;
2262 }
2263 }
2264
2265 return E;
2266 }
2267}
2268
John McCall2fc46bf2010-05-05 22:59:52 +00002269Expr *Expr::IgnoreParenImpCasts() {
2270 Expr *E = this;
2271 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002272 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002273 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002274 continue;
2275 }
2276 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002277 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002278 continue;
2279 }
2280 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2281 if (P->getOpcode() == UO_Extension) {
2282 E = P->getSubExpr();
2283 continue;
2284 }
2285 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002286 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2287 if (!P->isResultDependent()) {
2288 E = P->getResultExpr();
2289 continue;
2290 }
2291 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002292 if (MaterializeTemporaryExpr *Materialize
2293 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2294 E = Materialize->GetTemporaryExpr();
2295 continue;
2296 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002297 if (SubstNonTypeTemplateParmExpr *NTTP
2298 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2299 E = NTTP->getReplacement();
2300 continue;
2301 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002302 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002303 }
2304}
2305
Hans Wennborg2f072b42011-06-09 17:06:51 +00002306Expr *Expr::IgnoreConversionOperator() {
2307 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002308 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002309 return MCE->getImplicitObjectArgument();
2310 }
2311 return this;
2312}
2313
Chris Lattnerecdd8412009-03-13 17:28:01 +00002314/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2315/// value (including ptr->int casts of the same size). Strip off any
2316/// ParenExpr or CastExprs, returning their operand.
2317Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2318 Expr *E = this;
2319 while (true) {
2320 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2321 E = P->getSubExpr();
2322 continue;
2323 }
Mike Stump1eb44332009-09-09 15:08:12 +00002324
Chris Lattnerecdd8412009-03-13 17:28:01 +00002325 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2326 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002327 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002328 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002329
Chris Lattnerecdd8412009-03-13 17:28:01 +00002330 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2331 E = SE;
2332 continue;
2333 }
Mike Stump1eb44332009-09-09 15:08:12 +00002334
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002335 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002336 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002337 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002338 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002339 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2340 E = SE;
2341 continue;
2342 }
2343 }
Mike Stump1eb44332009-09-09 15:08:12 +00002344
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002345 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2346 if (P->getOpcode() == UO_Extension) {
2347 E = P->getSubExpr();
2348 continue;
2349 }
2350 }
2351
Peter Collingbournef111d932011-04-15 00:35:48 +00002352 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2353 if (!P->isResultDependent()) {
2354 E = P->getResultExpr();
2355 continue;
2356 }
2357 }
2358
Douglas Gregorc0244c52011-09-08 17:56:33 +00002359 if (SubstNonTypeTemplateParmExpr *NTTP
2360 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2361 E = NTTP->getReplacement();
2362 continue;
2363 }
2364
Chris Lattnerecdd8412009-03-13 17:28:01 +00002365 return E;
2366 }
2367}
2368
Douglas Gregor6eef5192009-12-14 19:27:10 +00002369bool Expr::isDefaultArgument() const {
2370 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002371 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2372 E = M->GetTemporaryExpr();
2373
Douglas Gregor6eef5192009-12-14 19:27:10 +00002374 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2375 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002376
Douglas Gregor6eef5192009-12-14 19:27:10 +00002377 return isa<CXXDefaultArgExpr>(E);
2378}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002379
Douglas Gregor2f599792010-04-02 18:24:57 +00002380/// \brief Skip over any no-op casts and any temporary-binding
2381/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002382static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002383 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2384 E = M->GetTemporaryExpr();
2385
Douglas Gregor2f599792010-04-02 18:24:57 +00002386 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002387 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002388 E = ICE->getSubExpr();
2389 else
2390 break;
2391 }
2392
2393 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2394 E = BE->getSubExpr();
2395
2396 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002397 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002398 E = ICE->getSubExpr();
2399 else
2400 break;
2401 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002402
2403 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002404}
2405
John McCall558d2ab2010-09-15 10:14:12 +00002406/// isTemporaryObject - Determines if this expression produces a
2407/// temporary of the given class type.
2408bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2409 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2410 return false;
2411
Anders Carlssonf8b30152010-11-28 16:40:49 +00002412 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002413
John McCall58277b52010-09-15 20:59:13 +00002414 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002415 if (!E->Classify(C).isPRValue()) {
2416 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002417 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002418 return false;
2419 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002420
John McCall19e60ad2010-09-16 06:57:56 +00002421 // Black-list a few cases which yield pr-values of class type that don't
2422 // refer to temporaries of that type:
2423
2424 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002425 if (isa<ImplicitCastExpr>(E)) {
2426 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2427 case CK_DerivedToBase:
2428 case CK_UncheckedDerivedToBase:
2429 return false;
2430 default:
2431 break;
2432 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002433 }
2434
John McCall19e60ad2010-09-16 06:57:56 +00002435 // - member expressions (all)
2436 if (isa<MemberExpr>(E))
2437 return false;
2438
Eli Friedman32f498a2012-06-15 23:51:06 +00002439 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2440 if (BO->isPtrMemOp())
2441 return false;
2442
John McCall56ca35d2011-02-17 10:25:35 +00002443 // - opaque values (all)
2444 if (isa<OpaqueValueExpr>(E))
2445 return false;
2446
John McCall558d2ab2010-09-15 10:14:12 +00002447 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002448}
2449
Douglas Gregor75e85042011-03-02 21:06:53 +00002450bool Expr::isImplicitCXXThis() const {
2451 const Expr *E = this;
2452
2453 // Strip away parentheses and casts we don't care about.
2454 while (true) {
2455 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2456 E = Paren->getSubExpr();
2457 continue;
2458 }
2459
2460 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2461 if (ICE->getCastKind() == CK_NoOp ||
2462 ICE->getCastKind() == CK_LValueToRValue ||
2463 ICE->getCastKind() == CK_DerivedToBase ||
2464 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2465 E = ICE->getSubExpr();
2466 continue;
2467 }
2468 }
2469
2470 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2471 if (UnOp->getOpcode() == UO_Extension) {
2472 E = UnOp->getSubExpr();
2473 continue;
2474 }
2475 }
2476
Douglas Gregor03e80032011-06-21 17:03:29 +00002477 if (const MaterializeTemporaryExpr *M
2478 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2479 E = M->GetTemporaryExpr();
2480 continue;
2481 }
2482
Douglas Gregor75e85042011-03-02 21:06:53 +00002483 break;
2484 }
2485
2486 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2487 return This->isImplicit();
2488
2489 return false;
2490}
2491
Douglas Gregor898574e2008-12-05 23:32:09 +00002492/// hasAnyTypeDependentArguments - Determines if any of the expressions
2493/// in Exprs is type-dependent.
Ahmed Charles13a140c2012-02-25 11:00:22 +00002494bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2495 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002496 if (Exprs[I]->isTypeDependent())
2497 return true;
2498
2499 return false;
2500}
2501
John McCall4204f072010-08-02 21:13:48 +00002502bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002503 // This function is attempting whether an expression is an initializer
2504 // which can be evaluated at compile-time. isEvaluatable handles most
2505 // of the cases, but it can't deal with some initializer-specific
2506 // expressions, and it can't deal with aggregates; we deal with those here,
2507 // and fall back to isEvaluatable for the other cases.
2508
John McCall4204f072010-08-02 21:13:48 +00002509 // If we ever capture reference-binding directly in the AST, we can
2510 // kill the second parameter.
2511
2512 if (IsForRef) {
2513 EvalResult Result;
2514 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2515 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002516
Anders Carlssone8a32b82008-11-24 05:23:59 +00002517 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002518 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002519 case IntegerLiteralClass:
2520 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002521 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002522 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002523 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002524 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002525 case CXXTemporaryObjectExprClass:
2526 case CXXConstructExprClass: {
2527 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002528
2529 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002530 if (CE->getConstructor()->isTrivial()) {
2531 // 1) an application of the trivial default constructor or
2532 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002533
Richard Smith180f4792011-11-10 06:34:14 +00002534 // 2) an elidable trivial copy construction of an operand which is
2535 // itself a constant initializer. Note that we consider the
2536 // operand on its own, *not* as a reference binding.
2537 if (CE->isElidable() &&
2538 CE->getArg(0)->isConstantInitializer(Ctx, false))
2539 return true;
2540 }
2541
2542 // 3) a foldable constexpr constructor.
2543 break;
John McCallb4b9b152010-08-01 21:51:45 +00002544 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002545 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002546 // This handles gcc's extension that allows global initializers like
2547 // "struct x {int x;} x = (struct x) {};".
2548 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002549 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002550 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002551 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002552 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002553 // FIXME: This doesn't deal with fields with reference types correctly.
2554 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2555 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002556 const InitListExpr *Exp = cast<InitListExpr>(this);
2557 unsigned numInits = Exp->getNumInits();
2558 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002559 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002560 return false;
2561 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002562 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002563 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002564 case ImplicitValueInitExprClass:
2565 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002566 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002567 return cast<ParenExpr>(this)->getSubExpr()
2568 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002569 case GenericSelectionExprClass:
2570 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2571 return false;
2572 return cast<GenericSelectionExpr>(this)->getResultExpr()
2573 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002574 case ChooseExprClass:
2575 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2576 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002577 case UnaryOperatorClass: {
2578 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002579 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002580 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002581 break;
2582 }
John McCall4204f072010-08-02 21:13:48 +00002583 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002584 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002585 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002586 case CStyleCastExprClass: {
2587 const CastExpr *CE = cast<CastExpr>(this);
2588
David Chisnall7a7ee302012-01-16 17:27:18 +00002589 // If we're promoting an integer to an _Atomic type then this is constant
2590 // if the integer is constant. We also need to check the converse in case
2591 // someone does something like:
2592 //
2593 // int a = (_Atomic(int))42;
2594 //
2595 // I doubt anyone would write code like this directly, but it's quite
2596 // possible as the result of macro expansions.
2597 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2598 CE->getCastKind() == CK_AtomicToNonAtomic)
2599 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2600
Richard Smithd62ca372011-12-06 22:44:34 +00002601 // Handle bitcasts of vector constants.
2602 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2603 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2604
Eli Friedman6bd97192011-12-21 00:43:02 +00002605 // Handle misc casts we want to ignore.
2606 // FIXME: Is it really safe to ignore all these?
2607 if (CE->getCastKind() == CK_NoOp ||
2608 CE->getCastKind() == CK_LValueToRValue ||
2609 CE->getCastKind() == CK_ToUnion ||
2610 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002611 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2612
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002613 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002614 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002615 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002616 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002617 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002618 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002619 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002620}
2621
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002622namespace {
2623 /// \brief Look for a call to a non-trivial function within an expression.
2624 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2625 {
2626 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2627
2628 bool NonTrivial;
2629
2630 public:
2631 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002632 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002633
2634 bool hasNonTrivialCall() const { return NonTrivial; }
2635
2636 void VisitCallExpr(CallExpr *E) {
2637 if (CXXMethodDecl *Method
2638 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2639 if (Method->isTrivial()) {
2640 // Recurse to children of the call.
2641 Inherited::VisitStmt(E);
2642 return;
2643 }
2644 }
2645
2646 NonTrivial = true;
2647 }
2648
2649 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2650 if (E->getConstructor()->isTrivial()) {
2651 // Recurse to children of the call.
2652 Inherited::VisitStmt(E);
2653 return;
2654 }
2655
2656 NonTrivial = true;
2657 }
2658
2659 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2660 if (E->getTemporary()->getDestructor()->isTrivial()) {
2661 Inherited::VisitStmt(E);
2662 return;
2663 }
2664
2665 NonTrivial = true;
2666 }
2667 };
2668}
2669
2670bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2671 NonTrivialCallFinder Finder(Ctx);
2672 Finder.Visit(this);
2673 return Finder.hasNonTrivialCall();
2674}
2675
Chandler Carruth82214a82011-02-18 23:54:50 +00002676/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2677/// pointer constant or not, as well as the specific kind of constant detected.
2678/// Null pointer constants can be integer constant expressions with the
2679/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2680/// (a GNU extension).
2681Expr::NullPointerConstantKind
2682Expr::isNullPointerConstant(ASTContext &Ctx,
2683 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002684 if (isValueDependent()) {
2685 switch (NPC) {
2686 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002687 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002688 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002689 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2690 return NPCK_ZeroInteger;
2691 else
2692 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002693
Douglas Gregorce940492009-09-25 04:25:58 +00002694 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002695 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002696 }
2697 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002698
Sebastian Redl07779722008-10-31 14:43:28 +00002699 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002700 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002701 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002702 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002703 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002704 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002705 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002706 Pointee->isVoidType() && // to void*
2707 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002708 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002709 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002710 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002711 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2712 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002713 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002714 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2715 // Accept ((void*)0) as a null pointer constant, as many other
2716 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002717 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002718 } else if (const GenericSelectionExpr *GE =
2719 dyn_cast<GenericSelectionExpr>(this)) {
2720 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002721 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002722 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002723 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002724 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002725 } else if (isa<GNUNullExpr>(this)) {
2726 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002727 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002728 } else if (const MaterializeTemporaryExpr *M
2729 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2730 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002731 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2732 if (const Expr *Source = OVE->getSourceExpr())
2733 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002734 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002735
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002736 // C++0x nullptr_t is always a null pointer constant.
2737 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002738 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002739
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002740 if (const RecordType *UT = getType()->getAsUnionType())
2741 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2742 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2743 const Expr *InitExpr = CLE->getInitializer();
2744 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2745 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2746 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002747 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002748 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002749 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002750 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002751
Reid Spencer5f016e22007-07-11 17:01:13 +00002752 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith70488e22012-02-14 21:38:30 +00002753 // test for the value 0. Don't use the C++11 constant expression semantics
2754 // for this, for now; once the dust settles on core issue 903, we might only
2755 // allow a literal 0 here in C++11 mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002756 if (Ctx.getLangOpts().CPlusPlus0x) {
Richard Smith70488e22012-02-14 21:38:30 +00002757 if (!isCXX98IntegralConstantExpr(Ctx))
2758 return NPCK_NotNull;
2759 } else {
2760 if (!isIntegerConstantExpr(Ctx))
2761 return NPCK_NotNull;
2762 }
Chandler Carruth82214a82011-02-18 23:54:50 +00002763
Richard Smith70488e22012-02-14 21:38:30 +00002764 return (EvaluateKnownConstInt(Ctx) == 0) ? NPCK_ZeroInteger : NPCK_NotNull;
Reid Spencer5f016e22007-07-11 17:01:13 +00002765}
Steve Naroff31a45842007-07-28 23:10:27 +00002766
John McCallf6a16482010-12-04 03:47:34 +00002767/// \brief If this expression is an l-value for an Objective C
2768/// property, find the underlying property reference expression.
2769const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2770 const Expr *E = this;
2771 while (true) {
2772 assert((E->getValueKind() == VK_LValue &&
2773 E->getObjectKind() == OK_ObjCProperty) &&
2774 "expression is not a property reference");
2775 E = E->IgnoreParenCasts();
2776 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2777 if (BO->getOpcode() == BO_Comma) {
2778 E = BO->getRHS();
2779 continue;
2780 }
2781 }
2782
2783 break;
2784 }
2785
2786 return cast<ObjCPropertyRefExpr>(E);
2787}
2788
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002789FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002790 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002791
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002792 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002793 if (ICE->getCastKind() == CK_LValueToRValue ||
2794 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002795 E = ICE->getSubExpr()->IgnoreParens();
2796 else
2797 break;
2798 }
2799
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002800 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002801 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002802 if (Field->isBitField())
2803 return Field;
2804
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002805 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2806 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2807 if (Field->isBitField())
2808 return Field;
2809
Eli Friedman42068e92011-07-13 02:05:57 +00002810 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002811 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2812 return BinOp->getLHS()->getBitField();
2813
Eli Friedman42068e92011-07-13 02:05:57 +00002814 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2815 return BinOp->getRHS()->getBitField();
2816 }
2817
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002818 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002819}
2820
Anders Carlsson09380262010-01-31 17:18:49 +00002821bool Expr::refersToVectorElement() const {
2822 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002823
Anders Carlsson09380262010-01-31 17:18:49 +00002824 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002825 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002826 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002827 E = ICE->getSubExpr()->IgnoreParens();
2828 else
2829 break;
2830 }
Sean Huntc3021132010-05-05 15:23:54 +00002831
Anders Carlsson09380262010-01-31 17:18:49 +00002832 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2833 return ASE->getBase()->getType()->isVectorType();
2834
2835 if (isa<ExtVectorElementExpr>(E))
2836 return true;
2837
2838 return false;
2839}
2840
Chris Lattner2140e902009-02-16 22:14:05 +00002841/// isArrow - Return true if the base expression is a pointer to vector,
2842/// return false if the base expression is a vector.
2843bool ExtVectorElementExpr::isArrow() const {
2844 return getBase()->getType()->isPointerType();
2845}
2846
Nate Begeman213541a2008-04-18 23:10:10 +00002847unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002848 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002849 return VT->getNumElements();
2850 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002851}
2852
Nate Begeman8a997642008-05-09 06:41:27 +00002853/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002854bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002855 // FIXME: Refactor this code to an accessor on the AST node which returns the
2856 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002857 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002858
2859 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002860 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002861 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002862
Nate Begeman190d6a22009-01-18 02:01:21 +00002863 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002864 if (Comp[0] == 's' || Comp[0] == 'S')
2865 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002866
Daniel Dunbar15027422009-10-17 23:53:04 +00002867 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002868 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002869 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002870
Steve Narofffec0b492007-07-30 03:29:09 +00002871 return false;
2872}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002873
Nate Begeman8a997642008-05-09 06:41:27 +00002874/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002875void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002876 SmallVectorImpl<unsigned> &Elts) const {
2877 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002878 if (Comp[0] == 's' || Comp[0] == 'S')
2879 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002880
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002881 bool isHi = Comp == "hi";
2882 bool isLo = Comp == "lo";
2883 bool isEven = Comp == "even";
2884 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002885
Nate Begeman8a997642008-05-09 06:41:27 +00002886 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2887 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002888
Nate Begeman8a997642008-05-09 06:41:27 +00002889 if (isHi)
2890 Index = e + i;
2891 else if (isLo)
2892 Index = i;
2893 else if (isEven)
2894 Index = 2 * i;
2895 else if (isOdd)
2896 Index = 2 * i + 1;
2897 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002898 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002899
Nate Begeman3b8d1162008-05-13 21:03:02 +00002900 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002901 }
Nate Begeman8a997642008-05-09 06:41:27 +00002902}
2903
Douglas Gregor04badcf2010-04-21 00:45:42 +00002904ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002905 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002906 SourceLocation LBracLoc,
2907 SourceLocation SuperLoc,
2908 bool IsInstanceSuper,
2909 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002910 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002911 ArrayRef<SourceLocation> SelLocs,
2912 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002913 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002914 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002915 SourceLocation RBracLoc,
2916 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002917 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002918 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002919 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002920 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002921 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2922 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002923 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002924 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
2925 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002926{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002927 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002928 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002929}
2930
Douglas Gregor04badcf2010-04-21 00:45:42 +00002931ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002932 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002933 SourceLocation LBracLoc,
2934 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002935 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002936 ArrayRef<SourceLocation> SelLocs,
2937 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002938 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002939 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002940 SourceLocation RBracLoc,
2941 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002942 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002943 T->isDependentType(), T->isInstantiationDependentType(),
2944 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002945 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2946 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002947 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002948 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002949 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002950{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002951 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002952 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002953}
2954
Douglas Gregor04badcf2010-04-21 00:45:42 +00002955ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002956 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002957 SourceLocation LBracLoc,
2958 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002959 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002960 ArrayRef<SourceLocation> SelLocs,
2961 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002962 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002963 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002964 SourceLocation RBracLoc,
2965 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002966 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002967 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002968 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002969 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002970 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2971 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002972 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002973 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002974 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002975{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002976 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002977 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002978}
2979
2980void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2981 ArrayRef<SourceLocation> SelLocs,
2982 SelectorLocationsKind SelLocsK) {
2983 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00002984 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002985 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002986 if (Args[I]->isTypeDependent())
2987 ExprBits.TypeDependent = true;
2988 if (Args[I]->isValueDependent())
2989 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002990 if (Args[I]->isInstantiationDependent())
2991 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002992 if (Args[I]->containsUnexpandedParameterPack())
2993 ExprBits.ContainsUnexpandedParameterPack = true;
2994
2995 MyArgs[I] = Args[I];
2996 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002997
Benjamin Kramer19562c92012-02-20 00:20:48 +00002998 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00002999 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00003000 if (SelLocsK == SelLoc_NonStandard)
3001 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
3002 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00003003}
3004
Douglas Gregor04badcf2010-04-21 00:45:42 +00003005ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003006 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003007 SourceLocation LBracLoc,
3008 SourceLocation SuperLoc,
3009 bool IsInstanceSuper,
3010 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00003011 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003012 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003013 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003014 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003015 SourceLocation RBracLoc,
3016 bool isImplicit) {
3017 assert((!SelLocs.empty() || isImplicit) &&
3018 "No selector locs for non-implicit message");
3019 ObjCMessageExpr *Mem;
3020 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3021 if (isImplicit)
3022 Mem = alloc(Context, Args.size(), 0);
3023 else
3024 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00003025 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003026 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003027 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003028}
3029
3030ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003031 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003032 SourceLocation LBracLoc,
3033 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00003034 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003035 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003036 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003037 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003038 SourceLocation RBracLoc,
3039 bool isImplicit) {
3040 assert((!SelLocs.empty() || isImplicit) &&
3041 "No selector locs for non-implicit message");
3042 ObjCMessageExpr *Mem;
3043 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3044 if (isImplicit)
3045 Mem = alloc(Context, Args.size(), 0);
3046 else
3047 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003048 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003049 SelLocs, SelLocsK, Method, Args, RBracLoc,
3050 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003051}
3052
3053ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00003054 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003055 SourceLocation LBracLoc,
3056 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00003057 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003058 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00003059 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003060 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003061 SourceLocation RBracLoc,
3062 bool isImplicit) {
3063 assert((!SelLocs.empty() || isImplicit) &&
3064 "No selector locs for non-implicit message");
3065 ObjCMessageExpr *Mem;
3066 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3067 if (isImplicit)
3068 Mem = alloc(Context, Args.size(), 0);
3069 else
3070 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003071 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003072 SelLocs, SelLocsK, Method, Args, RBracLoc,
3073 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003074}
3075
Sean Huntc3021132010-05-05 15:23:54 +00003076ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003077 unsigned NumArgs,
3078 unsigned NumStoredSelLocs) {
3079 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003080 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3081}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003082
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003083ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3084 ArrayRef<Expr *> Args,
3085 SourceLocation RBraceLoc,
3086 ArrayRef<SourceLocation> SelLocs,
3087 Selector Sel,
3088 SelectorLocationsKind &SelLocsK) {
3089 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3090 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3091 : 0;
3092 return alloc(C, Args.size(), NumStoredSelLocs);
3093}
3094
3095ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3096 unsigned NumArgs,
3097 unsigned NumStoredSelLocs) {
3098 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3099 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3100 return (ObjCMessageExpr *)C.Allocate(Size,
3101 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3102}
3103
3104void ObjCMessageExpr::getSelectorLocs(
3105 SmallVectorImpl<SourceLocation> &SelLocs) const {
3106 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3107 SelLocs.push_back(getSelectorLoc(i));
3108}
3109
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003110SourceRange ObjCMessageExpr::getReceiverRange() const {
3111 switch (getReceiverKind()) {
3112 case Instance:
3113 return getInstanceReceiver()->getSourceRange();
3114
3115 case Class:
3116 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3117
3118 case SuperInstance:
3119 case SuperClass:
3120 return getSuperLoc();
3121 }
3122
David Blaikie30263482012-01-20 21:50:17 +00003123 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003124}
3125
Douglas Gregor04badcf2010-04-21 00:45:42 +00003126Selector ObjCMessageExpr::getSelector() const {
3127 if (HasMethod)
3128 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3129 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003130 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003131}
3132
3133ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3134 switch (getReceiverKind()) {
3135 case Instance:
3136 if (const ObjCObjectPointerType *Ptr
3137 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
3138 return Ptr->getInterfaceDecl();
3139 break;
3140
3141 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00003142 if (const ObjCObjectType *Ty
3143 = getClassReceiver()->getAs<ObjCObjectType>())
3144 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003145 break;
3146
3147 case SuperInstance:
3148 if (const ObjCObjectPointerType *Ptr
3149 = getSuperType()->getAs<ObjCObjectPointerType>())
3150 return Ptr->getInterfaceDecl();
3151 break;
3152
3153 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00003154 if (const ObjCObjectType *Iface
3155 = getSuperType()->getAs<ObjCObjectType>())
3156 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003157 break;
3158 }
3159
3160 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003161}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003162
Chris Lattner5f9e2722011-07-23 10:55:15 +00003163StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003164 switch (getBridgeKind()) {
3165 case OBC_Bridge:
3166 return "__bridge";
3167 case OBC_BridgeTransfer:
3168 return "__bridge_transfer";
3169 case OBC_BridgeRetained:
3170 return "__bridge_retained";
3171 }
David Blaikie30263482012-01-20 21:50:17 +00003172
3173 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003174}
3175
Jay Foad4ba2a172011-01-12 09:06:06 +00003176bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003177 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003178}
3179
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003180ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3181 QualType Type, SourceLocation BLoc,
3182 SourceLocation RP)
3183 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3184 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003185 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003186 Type->containsUnexpandedParameterPack()),
3187 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3188{
3189 SubExprs = new (C) Stmt*[nexpr];
3190 for (unsigned i = 0; i < nexpr; i++) {
3191 if (args[i]->isTypeDependent())
3192 ExprBits.TypeDependent = true;
3193 if (args[i]->isValueDependent())
3194 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003195 if (args[i]->isInstantiationDependent())
3196 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003197 if (args[i]->containsUnexpandedParameterPack())
3198 ExprBits.ContainsUnexpandedParameterPack = true;
3199
3200 SubExprs[i] = args[i];
3201 }
3202}
3203
Nate Begeman888376a2009-08-12 02:28:50 +00003204void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3205 unsigned NumExprs) {
3206 if (SubExprs) C.Deallocate(SubExprs);
3207
3208 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003209 this->NumExprs = NumExprs;
3210 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003211}
Nate Begeman888376a2009-08-12 02:28:50 +00003212
Peter Collingbournef111d932011-04-15 00:35:48 +00003213GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3214 SourceLocation GenericLoc, Expr *ControllingExpr,
3215 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3216 unsigned NumAssocs, SourceLocation DefaultLoc,
3217 SourceLocation RParenLoc,
3218 bool ContainsUnexpandedParameterPack,
3219 unsigned ResultIndex)
3220 : Expr(GenericSelectionExprClass,
3221 AssocExprs[ResultIndex]->getType(),
3222 AssocExprs[ResultIndex]->getValueKind(),
3223 AssocExprs[ResultIndex]->getObjectKind(),
3224 AssocExprs[ResultIndex]->isTypeDependent(),
3225 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003226 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003227 ContainsUnexpandedParameterPack),
3228 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3229 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3230 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3231 RParenLoc(RParenLoc) {
3232 SubExprs[CONTROLLING] = ControllingExpr;
3233 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3234 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3235}
3236
3237GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3238 SourceLocation GenericLoc, Expr *ControllingExpr,
3239 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3240 unsigned NumAssocs, SourceLocation DefaultLoc,
3241 SourceLocation RParenLoc,
3242 bool ContainsUnexpandedParameterPack)
3243 : Expr(GenericSelectionExprClass,
3244 Context.DependentTy,
3245 VK_RValue,
3246 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003247 /*isTypeDependent=*/true,
3248 /*isValueDependent=*/true,
3249 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003250 ContainsUnexpandedParameterPack),
3251 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3252 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3253 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3254 RParenLoc(RParenLoc) {
3255 SubExprs[CONTROLLING] = ControllingExpr;
3256 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3257 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3258}
3259
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003260//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003261// DesignatedInitExpr
3262//===----------------------------------------------------------------------===//
3263
Chandler Carruthb1138242011-06-16 06:47:06 +00003264IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003265 assert(Kind == FieldDesignator && "Only valid on a field designator");
3266 if (Field.NameOrField & 0x01)
3267 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3268 else
3269 return getField()->getIdentifier();
3270}
3271
Sean Huntc3021132010-05-05 15:23:54 +00003272DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003273 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003274 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003275 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003276 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003277 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003278 unsigned NumIndexExprs,
3279 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003280 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003281 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003282 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003283 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003284 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003285 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3286 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003287 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003288
3289 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003290 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003291 *Child++ = Init;
3292
3293 // Copy the designators and their subexpressions, computing
3294 // value-dependence along the way.
3295 unsigned IndexIdx = 0;
3296 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003297 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003298
3299 if (this->Designators[I].isArrayDesignator()) {
3300 // Compute type- and value-dependence.
3301 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003302 if (Index->isTypeDependent() || Index->isValueDependent())
3303 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003304 if (Index->isInstantiationDependent())
3305 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003306 // Propagate unexpanded parameter packs.
3307 if (Index->containsUnexpandedParameterPack())
3308 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003309
3310 // Copy the index expressions into permanent storage.
3311 *Child++ = IndexExprs[IndexIdx++];
3312 } else if (this->Designators[I].isArrayRangeDesignator()) {
3313 // Compute type- and value-dependence.
3314 Expr *Start = IndexExprs[IndexIdx];
3315 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003316 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003317 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003318 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003319 ExprBits.InstantiationDependent = true;
3320 } else if (Start->isInstantiationDependent() ||
3321 End->isInstantiationDependent()) {
3322 ExprBits.InstantiationDependent = true;
3323 }
3324
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003325 // Propagate unexpanded parameter packs.
3326 if (Start->containsUnexpandedParameterPack() ||
3327 End->containsUnexpandedParameterPack())
3328 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003329
3330 // Copy the start/end expressions into permanent storage.
3331 *Child++ = IndexExprs[IndexIdx++];
3332 *Child++ = IndexExprs[IndexIdx++];
3333 }
3334 }
3335
3336 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003337}
3338
Douglas Gregor05c13a32009-01-22 00:58:24 +00003339DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003340DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003341 unsigned NumDesignators,
3342 Expr **IndexExprs, unsigned NumIndexExprs,
3343 SourceLocation ColonOrEqualLoc,
3344 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003345 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003346 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003347 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003348 ColonOrEqualLoc, UsesColonSyntax,
3349 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003350}
3351
Mike Stump1eb44332009-09-09 15:08:12 +00003352DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003353 unsigned NumIndexExprs) {
3354 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3355 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3356 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3357}
3358
Douglas Gregor319d57f2010-01-06 23:17:19 +00003359void DesignatedInitExpr::setDesignators(ASTContext &C,
3360 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003361 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003362 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003363 NumDesignators = NumDesigs;
3364 for (unsigned I = 0; I != NumDesigs; ++I)
3365 Designators[I] = Desigs[I];
3366}
3367
Abramo Bagnara24f46742011-03-16 15:08:46 +00003368SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3369 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3370 if (size() == 1)
3371 return DIE->getDesignator(0)->getSourceRange();
3372 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3373 DIE->getDesignator(size()-1)->getEndLocation());
3374}
3375
Douglas Gregor05c13a32009-01-22 00:58:24 +00003376SourceRange DesignatedInitExpr::getSourceRange() const {
3377 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003378 Designator &First =
3379 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003380 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003381 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003382 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3383 else
3384 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3385 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003386 StartLoc =
3387 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003388 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3389}
3390
Douglas Gregor05c13a32009-01-22 00:58:24 +00003391Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3392 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3393 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3394 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003395 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3396 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3397}
3398
3399Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003400 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003401 "Requires array range designator");
3402 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3403 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003404 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3405 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3406}
3407
3408Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003409 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003410 "Requires array range designator");
3411 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3412 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003413 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3414 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3415}
3416
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003417/// \brief Replaces the designator at index @p Idx with the series
3418/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003419void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003420 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003421 const Designator *Last) {
3422 unsigned NumNewDesignators = Last - First;
3423 if (NumNewDesignators == 0) {
3424 std::copy_backward(Designators + Idx + 1,
3425 Designators + NumDesignators,
3426 Designators + Idx);
3427 --NumNewDesignators;
3428 return;
3429 } else if (NumNewDesignators == 1) {
3430 Designators[Idx] = *First;
3431 return;
3432 }
3433
Mike Stump1eb44332009-09-09 15:08:12 +00003434 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003435 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003436 std::copy(Designators, Designators + Idx, NewDesignators);
3437 std::copy(First, Last, NewDesignators + Idx);
3438 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3439 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003440 Designators = NewDesignators;
3441 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3442}
3443
Mike Stump1eb44332009-09-09 15:08:12 +00003444ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003445 Expr **exprs, unsigned nexprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003446 SourceLocation rparenloc)
3447 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003448 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003449 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00003450 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003451 for (unsigned i = 0; i != nexprs; ++i) {
3452 if (exprs[i]->isTypeDependent())
3453 ExprBits.TypeDependent = true;
3454 if (exprs[i]->isValueDependent())
3455 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003456 if (exprs[i]->isInstantiationDependent())
3457 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003458 if (exprs[i]->containsUnexpandedParameterPack())
3459 ExprBits.ContainsUnexpandedParameterPack = true;
3460
Nate Begeman2ef13e52009-08-10 23:49:36 +00003461 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003462 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003463}
3464
John McCalle996ffd2011-02-16 08:02:54 +00003465const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3466 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3467 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003468 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3469 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003470 e = cast<CXXConstructExpr>(e)->getArg(0);
3471 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3472 e = ice->getSubExpr();
3473 return cast<OpaqueValueExpr>(e);
3474}
3475
John McCall4b9c2d22011-11-06 09:01:30 +00003476PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3477 unsigned numSemanticExprs) {
3478 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3479 (1 + numSemanticExprs) * sizeof(Expr*),
3480 llvm::alignOf<PseudoObjectExpr>());
3481 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3482}
3483
3484PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3485 : Expr(PseudoObjectExprClass, shell) {
3486 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3487}
3488
3489PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3490 ArrayRef<Expr*> semantics,
3491 unsigned resultIndex) {
3492 assert(syntax && "no syntactic expression!");
3493 assert(semantics.size() && "no semantic expressions!");
3494
3495 QualType type;
3496 ExprValueKind VK;
3497 if (resultIndex == NoResult) {
3498 type = C.VoidTy;
3499 VK = VK_RValue;
3500 } else {
3501 assert(resultIndex < semantics.size());
3502 type = semantics[resultIndex]->getType();
3503 VK = semantics[resultIndex]->getValueKind();
3504 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3505 }
3506
3507 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3508 (1 + semantics.size()) * sizeof(Expr*),
3509 llvm::alignOf<PseudoObjectExpr>());
3510 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3511 resultIndex);
3512}
3513
3514PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3515 Expr *syntax, ArrayRef<Expr*> semantics,
3516 unsigned resultIndex)
3517 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3518 /*filled in at end of ctor*/ false, false, false, false) {
3519 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3520 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3521
3522 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3523 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3524 getSubExprsBuffer()[i] = E;
3525
3526 if (E->isTypeDependent())
3527 ExprBits.TypeDependent = true;
3528 if (E->isValueDependent())
3529 ExprBits.ValueDependent = true;
3530 if (E->isInstantiationDependent())
3531 ExprBits.InstantiationDependent = true;
3532 if (E->containsUnexpandedParameterPack())
3533 ExprBits.ContainsUnexpandedParameterPack = true;
3534
3535 if (isa<OpaqueValueExpr>(E))
3536 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3537 "opaque-value semantic expressions for pseudo-object "
3538 "operations must have sources");
3539 }
3540}
3541
Douglas Gregor05c13a32009-01-22 00:58:24 +00003542//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003543// ExprIterator.
3544//===----------------------------------------------------------------------===//
3545
3546Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3547Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3548Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3549const Expr* ConstExprIterator::operator[](size_t idx) const {
3550 return cast<Expr>(I[idx]);
3551}
3552const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3553const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3554
3555//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003556// Child Iterators for iterating over subexpressions/substatements
3557//===----------------------------------------------------------------------===//
3558
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003559// UnaryExprOrTypeTraitExpr
3560Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003561 // If this is of a type and the type is a VLA type (and not a typedef), the
3562 // size expression of the VLA needs to be treated as an executable expression.
3563 // Why isn't this weirdness documented better in StmtIterator?
3564 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003565 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003566 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003567 return child_range(child_iterator(T), child_iterator());
3568 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003569 }
John McCall63c00d72011-02-09 08:16:59 +00003570 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003571}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003572
Steve Naroff563477d2007-09-18 23:55:05 +00003573// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003574Stmt::child_range ObjCMessageExpr::children() {
3575 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003576 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003577 begin = reinterpret_cast<Stmt **>(this + 1);
3578 else
3579 begin = reinterpret_cast<Stmt **>(getArgs());
3580 return child_range(begin,
3581 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003582}
3583
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003584ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3585 QualType T, ObjCMethodDecl *Method,
3586 SourceRange SR)
3587 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3588 false, false, false, false),
3589 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3590{
3591 Expr **SaveElements = getElements();
3592 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3593 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3594 ExprBits.ValueDependent = true;
3595 if (Elements[I]->isInstantiationDependent())
3596 ExprBits.InstantiationDependent = true;
3597 if (Elements[I]->containsUnexpandedParameterPack())
3598 ExprBits.ContainsUnexpandedParameterPack = true;
3599
3600 SaveElements[I] = Elements[I];
3601 }
3602}
3603
3604ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3605 llvm::ArrayRef<Expr *> Elements,
3606 QualType T, ObjCMethodDecl * Method,
3607 SourceRange SR) {
3608 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3609 + Elements.size() * sizeof(Expr *));
3610 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3611}
3612
3613ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3614 unsigned NumElements) {
3615
3616 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3617 + NumElements * sizeof(Expr *));
3618 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3619}
3620
3621ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3622 ArrayRef<ObjCDictionaryElement> VK,
3623 bool HasPackExpansions,
3624 QualType T, ObjCMethodDecl *method,
3625 SourceRange SR)
3626 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3627 false, false),
3628 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3629 DictWithObjectsMethod(method)
3630{
3631 KeyValuePair *KeyValues = getKeyValues();
3632 ExpansionData *Expansions = getExpansionData();
3633 for (unsigned I = 0; I < NumElements; I++) {
3634 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3635 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3636 ExprBits.ValueDependent = true;
3637 if (VK[I].Key->isInstantiationDependent() ||
3638 VK[I].Value->isInstantiationDependent())
3639 ExprBits.InstantiationDependent = true;
3640 if (VK[I].EllipsisLoc.isInvalid() &&
3641 (VK[I].Key->containsUnexpandedParameterPack() ||
3642 VK[I].Value->containsUnexpandedParameterPack()))
3643 ExprBits.ContainsUnexpandedParameterPack = true;
3644
3645 KeyValues[I].Key = VK[I].Key;
3646 KeyValues[I].Value = VK[I].Value;
3647 if (Expansions) {
3648 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3649 if (VK[I].NumExpansions)
3650 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3651 else
3652 Expansions[I].NumExpansionsPlusOne = 0;
3653 }
3654 }
3655}
3656
3657ObjCDictionaryLiteral *
3658ObjCDictionaryLiteral::Create(ASTContext &C,
3659 ArrayRef<ObjCDictionaryElement> VK,
3660 bool HasPackExpansions,
3661 QualType T, ObjCMethodDecl *method,
3662 SourceRange SR) {
3663 unsigned ExpansionsSize = 0;
3664 if (HasPackExpansions)
3665 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3666
3667 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3668 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3669 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3670}
3671
3672ObjCDictionaryLiteral *
3673ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3674 bool HasPackExpansions) {
3675 unsigned ExpansionsSize = 0;
3676 if (HasPackExpansions)
3677 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3678 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3679 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3680 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3681 HasPackExpansions);
3682}
3683
3684ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
3685 Expr *base,
3686 Expr *key, QualType T,
3687 ObjCMethodDecl *getMethod,
3688 ObjCMethodDecl *setMethod,
3689 SourceLocation RB) {
3690 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
3691 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
3692 OK_ObjCSubscript,
3693 getMethod, setMethod, RB);
3694}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003695
3696AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3697 QualType t, AtomicOp op, SourceLocation RP)
3698 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3699 false, false, false, false),
3700 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3701{
Richard Smithe1b2abc2012-04-10 22:49:28 +00003702 assert(nexpr == getNumSubExprs(op) && "wrong number of subexpressions");
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003703 for (unsigned i = 0; i < nexpr; i++) {
3704 if (args[i]->isTypeDependent())
3705 ExprBits.TypeDependent = true;
3706 if (args[i]->isValueDependent())
3707 ExprBits.ValueDependent = true;
3708 if (args[i]->isInstantiationDependent())
3709 ExprBits.InstantiationDependent = true;
3710 if (args[i]->containsUnexpandedParameterPack())
3711 ExprBits.ContainsUnexpandedParameterPack = true;
3712
3713 SubExprs[i] = args[i];
3714 }
3715}
Richard Smithe1b2abc2012-04-10 22:49:28 +00003716
3717unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
3718 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00003719 case AO__c11_atomic_init:
3720 case AO__c11_atomic_load:
3721 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003722 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00003723
3724 case AO__c11_atomic_store:
3725 case AO__c11_atomic_exchange:
3726 case AO__atomic_load:
3727 case AO__atomic_store:
3728 case AO__atomic_store_n:
3729 case AO__atomic_exchange_n:
3730 case AO__c11_atomic_fetch_add:
3731 case AO__c11_atomic_fetch_sub:
3732 case AO__c11_atomic_fetch_and:
3733 case AO__c11_atomic_fetch_or:
3734 case AO__c11_atomic_fetch_xor:
3735 case AO__atomic_fetch_add:
3736 case AO__atomic_fetch_sub:
3737 case AO__atomic_fetch_and:
3738 case AO__atomic_fetch_or:
3739 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00003740 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00003741 case AO__atomic_add_fetch:
3742 case AO__atomic_sub_fetch:
3743 case AO__atomic_and_fetch:
3744 case AO__atomic_or_fetch:
3745 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00003746 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003747 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00003748
3749 case AO__atomic_exchange:
3750 return 4;
3751
3752 case AO__c11_atomic_compare_exchange_strong:
3753 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003754 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00003755
3756 case AO__atomic_compare_exchange:
3757 case AO__atomic_compare_exchange_n:
3758 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00003759 }
3760 llvm_unreachable("unknown atomic op");
3761}