blob: 9d7a93a429e44887b76ad26067abe33bfaa354f7 [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
Chris Lattner2b334bb2010-04-16 23:34:13 +000036/// isKnownToHaveBooleanValue - Return true if this is an integer expression
37/// that is known to return 0 or 1. This happens for _Bool/bool expressions
38/// but also int expressions which are produced by things like comparisons in
39/// C.
40bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000041 const Expr *E = IgnoreParens();
42
Chris Lattner2b334bb2010-04-16 23:34:13 +000043 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000044 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000045 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000046 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000047
Peter Collingbournef111d932011-04-15 00:35:48 +000048 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000049 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000050 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000051 return UO->getSubExpr()->isKnownToHaveBooleanValue();
52 default:
53 return false;
54 }
55 }
Sean Huntc3021132010-05-05 15:23:54 +000056
John McCall6907fbe2010-06-12 01:56:02 +000057 // Only look through implicit casts. If the user writes
58 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000059 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000060 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000061
Peter Collingbournef111d932011-04-15 00:35:48 +000062 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000063 switch (BO->getOpcode()) {
64 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000065 case BO_LT: // Relational operators.
66 case BO_GT:
67 case BO_LE:
68 case BO_GE:
69 case BO_EQ: // Equality operators.
70 case BO_NE:
71 case BO_LAnd: // AND operator.
72 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000073 return true;
Sean Huntc3021132010-05-05 15:23:54 +000074
John McCall2de56d12010-08-25 11:45:40 +000075 case BO_And: // Bitwise AND operator.
76 case BO_Xor: // Bitwise XOR operator.
77 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000078 // Handle things like (x==2)|(y==12).
79 return BO->getLHS()->isKnownToHaveBooleanValue() &&
80 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000081
John McCall2de56d12010-08-25 11:45:40 +000082 case BO_Comma:
83 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000084 return BO->getRHS()->isKnownToHaveBooleanValue();
85 }
86 }
Sean Huntc3021132010-05-05 15:23:54 +000087
Peter Collingbournef111d932011-04-15 00:35:48 +000088 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000089 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
90 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000091
Chris Lattner2b334bb2010-04-16 23:34:13 +000092 return false;
93}
94
John McCall63c00d72011-02-09 08:16:59 +000095// Amusing macro metaprogramming hack: check whether a class provides
96// a more specific implementation of getExprLoc().
Daniel Dunbar90e25a82012-03-09 15:39:19 +000097//
98// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCall63c00d72011-02-09 08:16:59 +000099namespace {
100 /// This implementation is used when a class provides a custom
101 /// implementation of getExprLoc.
102 template <class E, class T>
103 SourceLocation getExprLocImpl(const Expr *expr,
104 SourceLocation (T::*v)() const) {
105 return static_cast<const E*>(expr)->getExprLoc();
106 }
107
108 /// This implementation is used when a class doesn't provide
109 /// a custom implementation of getExprLoc. Overload resolution
110 /// should pick it over the implementation above because it's
111 /// more specialized according to function template partial ordering.
112 template <class E>
113 SourceLocation getExprLocImpl(const Expr *expr,
114 SourceLocation (Expr::*v)() const) {
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000115 return static_cast<const E*>(expr)->getLocStart();
John McCall63c00d72011-02-09 08:16:59 +0000116 }
117}
118
119SourceLocation Expr::getExprLoc() const {
120 switch (getStmtClass()) {
121 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
122#define ABSTRACT_STMT(type)
123#define STMT(type, base) \
124 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
125#define EXPR(type, base) \
126 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
127#include "clang/AST/StmtNodes.inc"
128 }
129 llvm_unreachable("unknown statement kind");
John McCall63c00d72011-02-09 08:16:59 +0000130}
131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132//===----------------------------------------------------------------------===//
133// Primary Expressions.
134//===----------------------------------------------------------------------===//
135
Douglas Gregor561f8122011-07-01 01:22:09 +0000136/// \brief Compute the type-, value-, and instantiation-dependence of a
137/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000138/// based on the declaration being referenced.
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000139static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T,
Douglas Gregord967e312011-01-19 21:52:31 +0000140 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000141 bool &ValueDependent,
142 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000143 TypeDependent = false;
144 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000145 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000146
147 // (TD) C++ [temp.dep.expr]p3:
148 // An id-expression is type-dependent if it contains:
149 //
Sean Huntc3021132010-05-05 15:23:54 +0000150 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000151 //
152 // (VD) C++ [temp.dep.constexpr]p2:
153 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000154
Douglas Gregor0da76df2009-11-23 11:41:28 +0000155 // (TD) - an identifier that was declared with dependent type
156 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000157 if (T->isDependentType()) {
158 TypeDependent = true;
159 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000160 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000161 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000162 } else if (T->isInstantiationDependentType()) {
163 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000164 }
Douglas Gregord967e312011-01-19 21:52:31 +0000165
Douglas Gregor0da76df2009-11-23 11:41:28 +0000166 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000167 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000168 == DeclarationName::CXXConversionFunctionName) {
169 QualType T = D->getDeclName().getCXXNameType();
170 if (T->isDependentType()) {
171 TypeDependent = true;
172 ValueDependent = true;
173 InstantiationDependent = true;
174 return;
175 }
176
177 if (T->isInstantiationDependentType())
178 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000179 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000180
Douglas Gregor0da76df2009-11-23 11:41:28 +0000181 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000182 if (isa<NonTypeTemplateParmDecl>(D)) {
183 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000184 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000185 return;
186 }
187
Douglas Gregor0da76df2009-11-23 11:41:28 +0000188 // (VD) - a constant with integral or enumeration type and is
189 // initialized with an expression that is value-dependent.
Richard Smithdb1822c2011-11-08 01:31:09 +0000190 // (VD) - a constant with literal type and is initialized with an
191 // expression that is value-dependent [C++11].
192 // (VD) - FIXME: Missing from the standard:
193 // - an entity with reference type and is initialized with an
194 // expression that is value-dependent [C++11]
Douglas Gregord967e312011-01-19 21:52:31 +0000195 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000196 if ((Ctx.getLangOpts().CPlusPlus0x ?
Richard Smithdb1822c2011-11-08 01:31:09 +0000197 Var->getType()->isLiteralType() :
198 Var->getType()->isIntegralOrEnumerationType()) &&
199 (Var->getType().getCVRQualifiers() == Qualifiers::Const ||
200 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000201 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000202 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000203 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000204 InstantiationDependent = true;
205 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000206 }
207
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000208 // (VD) - FIXME: Missing from the standard:
209 // - a member function or a static data member of the current
210 // instantiation
Richard Smithdb1822c2011-11-08 01:31:09 +0000211 if (Var->isStaticDataMember() &&
212 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000213 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000214 InstantiationDependent = true;
215 }
Douglas Gregord967e312011-01-19 21:52:31 +0000216
217 return;
218 }
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
Douglas Gregord967e312011-01-19 21:52:31 +0000223 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
224 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000225 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000226 }
Douglas Gregord967e312011-01-19 21:52:31 +0000227}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000228
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000229void DeclRefExpr::computeDependence(ASTContext &Ctx) {
Douglas Gregord967e312011-01-19 21:52:31 +0000230 bool TypeDependent = false;
231 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000232 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000233 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
234 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000235
236 // (TD) C++ [temp.dep.expr]p3:
237 // An id-expression is type-dependent if it contains:
238 //
239 // and
240 //
241 // (VD) C++ [temp.dep.constexpr]p2:
242 // An identifier is value-dependent if it is:
243 if (!TypeDependent && !ValueDependent &&
244 hasExplicitTemplateArgs() &&
245 TemplateSpecializationType::anyDependentTemplateArguments(
246 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000247 getNumTemplateArgs(),
248 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000249 TypeDependent = true;
250 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000251 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000252 }
253
254 ExprBits.TypeDependent = TypeDependent;
255 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000256 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000257
Douglas Gregor10738d32010-12-23 23:51:58 +0000258 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000259 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000260 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000261}
262
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000263DeclRefExpr::DeclRefExpr(ASTContext &Ctx,
264 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000265 SourceLocation TemplateKWLoc,
John McCallf4b88a42012-03-10 09:33:50 +0000266 ValueDecl *D, bool RefersToEnclosingLocal,
267 const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000268 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000269 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000270 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000271 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000272 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
273 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000274 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000275 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000276 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
277 if (FoundD)
278 getInternalFoundDecl() = FoundD;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000279 DeclRefExprBits.HasTemplateKWAndArgsInfo
280 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCallf4b88a42012-03-10 09:33:50 +0000281 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor561f8122011-07-01 01:22:09 +0000282 if (TemplateArgs) {
283 bool Dependent = false;
284 bool InstantiationDependent = false;
285 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000286 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
287 Dependent,
288 InstantiationDependent,
289 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +0000290 if (InstantiationDependent)
291 setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000292 } else if (TemplateKWLoc.isValid()) {
293 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor561f8122011-07-01 01:22:09 +0000294 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000295 DeclRefExprBits.HadMultipleCandidates = 0;
296
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000297 computeDependence(Ctx);
Abramo Bagnara25777432010-08-11 22:01:17 +0000298}
299
Douglas Gregora2813ce2009-10-23 18:54:35 +0000300DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000301 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000302 SourceLocation TemplateKWLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000303 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000304 bool RefersToEnclosingLocal,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000305 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000306 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000307 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000308 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000309 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000310 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000311 RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000312 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000313 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000314}
315
316DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000317 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000318 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000319 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000320 bool RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000321 const DeclarationNameInfo &NameInfo,
322 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000323 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000324 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000325 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000326 // Filter out cases where the found Decl is the same as the value refenenced.
327 if (D == FoundD)
328 FoundD = 0;
329
Douglas Gregora2813ce2009-10-23 18:54:35 +0000330 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000331 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000332 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000333 if (FoundD)
334 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000335 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000336 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
337 else if (TemplateKWLoc.isValid())
338 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000339
Chris Lattner32488542010-10-30 05:14:06 +0000340 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000341 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000342 RefersToEnclosingLocal,
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000343 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000344}
345
Chandler Carruth3aa81402011-05-01 23:48:14 +0000346DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000347 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000348 bool HasFoundDecl,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000349 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000350 unsigned NumTemplateArgs) {
351 std::size_t Size = sizeof(DeclRefExpr);
352 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000353 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000354 if (HasFoundDecl)
355 Size += sizeof(NamedDecl *);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000356 if (HasTemplateKWAndArgsInfo)
357 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000358
Chris Lattner32488542010-10-30 05:14:06 +0000359 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000360 return new (Mem) DeclRefExpr(EmptyShell());
361}
362
Douglas Gregora2813ce2009-10-23 18:54:35 +0000363SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000364 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000365 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000366 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000367 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000368 R.setEnd(getRAngleLoc());
369 return R;
370}
Daniel Dunbar396ec672012-03-09 15:39:15 +0000371SourceLocation DeclRefExpr::getLocStart() const {
372 if (hasQualifier())
373 return getQualifierLoc().getBeginLoc();
374 return getNameInfo().getLocStart();
375}
376SourceLocation DeclRefExpr::getLocEnd() const {
377 if (hasExplicitTemplateArgs())
378 return getRAngleLoc();
379 return getNameInfo().getLocEnd();
380}
Douglas Gregora2813ce2009-10-23 18:54:35 +0000381
Anders Carlsson3a082d82009-09-08 18:24:21 +0000382// FIXME: Maybe this should use DeclPrinter with a special "print predefined
383// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000384std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
385 ASTContext &Context = CurrentDecl->getASTContext();
386
Anders Carlsson3a082d82009-09-08 18:24:21 +0000387 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000388 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000389 return FD->getNameAsString();
390
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000391 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000392 llvm::raw_svector_ostream Out(Name);
393
394 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000395 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000396 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000397 if (MD->isStatic())
398 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000399 }
400
David Blaikie4e4d0842012-03-11 07:00:24 +0000401 PrintingPolicy Policy(Context.getLangOpts());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000402 std::string Proto = FD->getQualifiedNameAsString(Policy);
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000403 llvm::raw_string_ostream POut(Proto);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000404
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000405 const FunctionDecl *Decl = FD;
406 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
407 Decl = Pattern;
408 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000409 const FunctionProtoType *FT = 0;
410 if (FD->hasWrittenPrototype())
411 FT = dyn_cast<FunctionProtoType>(AFT);
412
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000413 POut << "(";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000414 if (FT) {
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000415 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000416 if (i) POut << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000417 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000418 }
419
420 if (FT->isVariadic()) {
421 if (FD->getNumParams()) POut << ", ";
422 POut << "...";
423 }
424 }
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000425 POut << ")";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000426
Sam Weinig4eadcc52009-12-27 01:38:20 +0000427 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
428 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
429 if (ThisQuals.hasConst())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000430 POut << " const";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000431 if (ThisQuals.hasVolatile())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000432 POut << " volatile";
433 RefQualifierKind Ref = MD->getRefQualifier();
434 if (Ref == RQ_LValue)
435 POut << " &";
436 else if (Ref == RQ_RValue)
437 POut << " &&";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000438 }
439
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000440 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
441 SpecsTy Specs;
442 const DeclContext *Ctx = FD->getDeclContext();
443 while (Ctx && isa<NamedDecl>(Ctx)) {
444 const ClassTemplateSpecializationDecl *Spec
445 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
446 if (Spec && !Spec->isExplicitSpecialization())
447 Specs.push_back(Spec);
448 Ctx = Ctx->getParent();
449 }
450
451 std::string TemplateParams;
452 llvm::raw_string_ostream TOut(TemplateParams);
453 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
454 I != E; ++I) {
455 const TemplateParameterList *Params
456 = (*I)->getSpecializedTemplate()->getTemplateParameters();
457 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
458 assert(Params->size() == Args.size());
459 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
460 StringRef Param = Params->getParam(i)->getName();
461 if (Param.empty()) continue;
462 TOut << Param << " = ";
463 Args.get(i).print(Policy, TOut);
464 TOut << ", ";
465 }
466 }
467
468 FunctionTemplateSpecializationInfo *FSI
469 = FD->getTemplateSpecializationInfo();
470 if (FSI && !FSI->isExplicitSpecialization()) {
471 const TemplateParameterList* Params
472 = FSI->getTemplate()->getTemplateParameters();
473 const TemplateArgumentList* Args = FSI->TemplateArguments;
474 assert(Params->size() == Args->size());
475 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
476 StringRef Param = Params->getParam(i)->getName();
477 if (Param.empty()) continue;
478 TOut << Param << " = ";
479 Args->get(i).print(Policy, TOut);
480 TOut << ", ";
481 }
482 }
483
484 TOut.flush();
485 if (!TemplateParams.empty()) {
486 // remove the trailing comma and space
487 TemplateParams.resize(TemplateParams.size() - 2);
488 POut << " [" << TemplateParams << "]";
489 }
490
491 POut.flush();
492
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000493 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
494 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000495
496 Out << Proto;
497
498 Out.flush();
499 return Name.str().str();
500 }
501 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000502 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000503 llvm::raw_svector_ostream Out(Name);
504 Out << (MD->isInstanceMethod() ? '-' : '+');
505 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000506
507 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
508 // a null check to avoid a crash.
509 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000510 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000511
Anders Carlsson3a082d82009-09-08 18:24:21 +0000512 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000513 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000514 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000515
Anders Carlsson3a082d82009-09-08 18:24:21 +0000516 Out << ' ';
517 Out << MD->getSelector().getAsString();
518 Out << ']';
519
520 Out.flush();
521 return Name.str().str();
522 }
523 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
524 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
525 return "top level";
526 }
527 return "";
528}
529
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000530void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
531 if (hasAllocation())
532 C.Deallocate(pVal);
533
534 BitWidth = Val.getBitWidth();
535 unsigned NumWords = Val.getNumWords();
536 const uint64_t* Words = Val.getRawData();
537 if (NumWords > 1) {
538 pVal = new (C) uint64_t[NumWords];
539 std::copy(Words, Words + NumWords, pVal);
540 } else if (NumWords == 1)
541 VAL = Words[0];
542 else
543 VAL = 0;
544}
545
546IntegerLiteral *
547IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
548 QualType type, SourceLocation l) {
549 return new (C) IntegerLiteral(C, V, type, l);
550}
551
552IntegerLiteral *
553IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
554 return new (C) IntegerLiteral(Empty);
555}
556
557FloatingLiteral *
558FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
559 bool isexact, QualType Type, SourceLocation L) {
560 return new (C) FloatingLiteral(C, V, isexact, Type, L);
561}
562
563FloatingLiteral *
564FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000565 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000566}
567
Chris Lattnerda8249e2008-06-07 22:13:43 +0000568/// getValueAsApproximateDouble - This returns the value as an inaccurate
569/// double. Note that this may cause loss of precision, but is useful for
570/// debugging dumps, etc.
571double FloatingLiteral::getValueAsApproximateDouble() const {
572 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000573 bool ignored;
574 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
575 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000576 return V.convertToDouble();
577}
578
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000579int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000580 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000581 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000582 case Ascii:
583 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000584 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000585 break;
586 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000587 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000588 break;
589 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000590 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000591 break;
592 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000593 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000594 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000595 }
596 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
597 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000598 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000599 && "character byte widths supported are 1, 2, and 4 only");
600 return CharByteWidth;
601}
602
Chris Lattner5f9e2722011-07-23 10:55:15 +0000603StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000604 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000605 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000606 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000607 // Allocate enough space for the StringLiteral plus an array of locations for
608 // any concatenated string tokens.
609 void *Mem = C.Allocate(sizeof(StringLiteral)+
610 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000611 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000612 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000615 SL->setString(C,Str,Kind,Pascal);
616
Chris Lattner2085fd62009-02-18 06:40:38 +0000617 SL->TokLocs[0] = Loc[0];
618 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000619
Chris Lattner726e1682009-02-18 05:49:11 +0000620 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000621 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
622 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000623}
624
Douglas Gregor673ecd62009-04-15 16:35:07 +0000625StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
626 void *Mem = C.Allocate(sizeof(StringLiteral)+
627 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000628 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000629 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000630 SL->CharByteWidth = 0;
631 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000632 SL->NumConcatenated = NumStrs;
633 return SL;
634}
635
Eli Friedman64f45a22011-11-01 02:23:42 +0000636void StringLiteral::setString(ASTContext &C, StringRef Str,
637 StringKind Kind, bool IsPascal) {
638 //FIXME: we assume that the string data comes from a target that uses the same
639 // code unit size and endianess for the type of string.
640 this->Kind = Kind;
641 this->IsPascal = IsPascal;
642
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000643 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000644 assert((Str.size()%CharByteWidth == 0)
645 && "size of data must be multiple of CharByteWidth");
646 Length = Str.size()/CharByteWidth;
647
648 switch(CharByteWidth) {
649 case 1: {
650 char *AStrData = new (C) char[Length];
651 std::memcpy(AStrData,Str.data(),Str.size());
652 StrData.asChar = AStrData;
653 break;
654 }
655 case 2: {
656 uint16_t *AStrData = new (C) uint16_t[Length];
657 std::memcpy(AStrData,Str.data(),Str.size());
658 StrData.asUInt16 = AStrData;
659 break;
660 }
661 case 4: {
662 uint32_t *AStrData = new (C) uint32_t[Length];
663 std::memcpy(AStrData,Str.data(),Str.size());
664 StrData.asUInt32 = AStrData;
665 break;
666 }
667 default:
668 assert(false && "unsupported CharByteWidth");
669 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000670}
671
Chris Lattner08f92e32010-11-17 07:37:15 +0000672/// getLocationOfByte - Return a source location that points to the specified
673/// byte of this string literal.
674///
675/// Strings are amazingly complex. They can be formed from multiple tokens and
676/// can have escape sequences in them in addition to the usual trigraph and
677/// escaped newline business. This routine handles this complexity.
678///
679SourceLocation StringLiteral::
680getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
681 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000682 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
683
Chris Lattner08f92e32010-11-17 07:37:15 +0000684 // Loop over all of the tokens in this string until we find the one that
685 // contains the byte we're looking for.
686 unsigned TokNo = 0;
687 while (1) {
688 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
689 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
690
691 // Get the spelling of the string so that we can get the data that makes up
692 // the string literal, not the identifier for the macro it is potentially
693 // expanded through.
694 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
695
696 // Re-lex the token to get its length and original spelling.
697 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
698 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000699 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000700 if (Invalid)
701 return StrTokSpellingLoc;
702
703 const char *StrData = Buffer.data()+LocInfo.second;
704
Chris Lattner08f92e32010-11-17 07:37:15 +0000705 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +0000706 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
707 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +0000708 Token TheTok;
709 TheLexer.LexFromRawLexer(TheTok);
710
711 // Use the StringLiteralParser to compute the length of the string in bytes.
712 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
713 unsigned TokNumBytes = SLP.GetStringLength();
714
715 // If the byte is in this token, return the location of the byte.
716 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000717 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000718 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
719
720 // Now that we know the offset of the token in the spelling, use the
721 // preprocessor to get the offset in the original source.
722 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
723 }
724
725 // Move to the next string token.
726 ++TokNo;
727 ByteNo -= TokNumBytes;
728 }
729}
730
731
732
Reid Spencer5f016e22007-07-11 17:01:13 +0000733/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
734/// corresponds to, e.g. "sizeof" or "[pre]++".
735const char *UnaryOperator::getOpcodeStr(Opcode Op) {
736 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000737 case UO_PostInc: return "++";
738 case UO_PostDec: return "--";
739 case UO_PreInc: return "++";
740 case UO_PreDec: return "--";
741 case UO_AddrOf: return "&";
742 case UO_Deref: return "*";
743 case UO_Plus: return "+";
744 case UO_Minus: return "-";
745 case UO_Not: return "~";
746 case UO_LNot: return "!";
747 case UO_Real: return "__real";
748 case UO_Imag: return "__imag";
749 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000750 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000751 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000752}
753
John McCall2de56d12010-08-25 11:45:40 +0000754UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000755UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
756 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000757 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000758 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
759 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
760 case OO_Amp: return UO_AddrOf;
761 case OO_Star: return UO_Deref;
762 case OO_Plus: return UO_Plus;
763 case OO_Minus: return UO_Minus;
764 case OO_Tilde: return UO_Not;
765 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000766 }
767}
768
769OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
770 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000771 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
772 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
773 case UO_AddrOf: return OO_Amp;
774 case UO_Deref: return OO_Star;
775 case UO_Plus: return OO_Plus;
776 case UO_Minus: return OO_Minus;
777 case UO_Not: return OO_Tilde;
778 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000779 default: return OO_None;
780 }
781}
782
783
Reid Spencer5f016e22007-07-11 17:01:13 +0000784//===----------------------------------------------------------------------===//
785// Postfix Operators.
786//===----------------------------------------------------------------------===//
787
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000788CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
789 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000790 SourceLocation rparenloc)
791 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000792 fn->isTypeDependent(),
793 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000794 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000795 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000796 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000797
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000798 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000799 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000800 for (unsigned i = 0; i != numargs; ++i) {
801 if (args[i]->isTypeDependent())
802 ExprBits.TypeDependent = true;
803 if (args[i]->isValueDependent())
804 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000805 if (args[i]->isInstantiationDependent())
806 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000807 if (args[i]->containsUnexpandedParameterPack())
808 ExprBits.ContainsUnexpandedParameterPack = true;
809
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000810 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000811 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000812
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000813 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000814 RParenLoc = rparenloc;
815}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000816
Ted Kremenek668bf912009-02-09 20:51:47 +0000817CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000818 QualType t, ExprValueKind VK, SourceLocation rparenloc)
819 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000820 fn->isTypeDependent(),
821 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000822 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000823 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000824 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000825
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000826 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000827 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000828 for (unsigned i = 0; i != numargs; ++i) {
829 if (args[i]->isTypeDependent())
830 ExprBits.TypeDependent = true;
831 if (args[i]->isValueDependent())
832 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000833 if (args[i]->isInstantiationDependent())
834 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000835 if (args[i]->containsUnexpandedParameterPack())
836 ExprBits.ContainsUnexpandedParameterPack = true;
837
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000838 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000839 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000840
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000841 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 RParenLoc = rparenloc;
843}
844
Mike Stump1eb44332009-09-09 15:08:12 +0000845CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
846 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000847 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000848 SubExprs = new (C) Stmt*[PREARGS_START];
849 CallExprBits.NumPreArgs = 0;
850}
851
852CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
853 EmptyShell Empty)
854 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
855 // FIXME: Why do we allocate this?
856 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
857 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000858}
859
Nuno Lopesd20254f2009-12-20 23:11:08 +0000860Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000861 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000862
863 while (SubstNonTypeTemplateParmExpr *NTTP
864 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
865 CEE = NTTP->getReplacement()->IgnoreParenCasts();
866 }
867
Sebastian Redl20012152010-09-10 20:55:30 +0000868 // If we're calling a dereference, look at the pointer instead.
869 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
870 if (BO->isPtrMemOp())
871 CEE = BO->getRHS()->IgnoreParenCasts();
872 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
873 if (UO->getOpcode() == UO_Deref)
874 CEE = UO->getSubExpr()->IgnoreParenCasts();
875 }
Chris Lattner6346f962009-07-17 15:46:27 +0000876 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000877 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000878 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
879 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000880
881 return 0;
882}
883
Nuno Lopesd20254f2009-12-20 23:11:08 +0000884FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000885 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000886}
887
Chris Lattnerd18b3292007-12-28 05:25:02 +0000888/// setNumArgs - This changes the number of arguments present in this call.
889/// Any orphaned expressions are deleted by this, and any new operands are set
890/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000891void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000892 // No change, just return.
893 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000894
Chris Lattnerd18b3292007-12-28 05:25:02 +0000895 // If shrinking # arguments, just delete the extras and forgot them.
896 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000897 this->NumArgs = NumArgs;
898 return;
899 }
900
901 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000902 unsigned NumPreArgs = getNumPreArgs();
903 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000904 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000905 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000906 NewSubExprs[i] = SubExprs[i];
907 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000908 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
909 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000910 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Douglas Gregor88c9a462009-04-17 21:46:47 +0000912 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000913 SubExprs = NewSubExprs;
914 this->NumArgs = NumArgs;
915}
916
Chris Lattnercb888962008-10-06 05:00:53 +0000917/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
918/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +0000919unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000920 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000921 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000922 // ImplicitCastExpr.
923 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
924 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000925 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000927 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
928 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000929 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Anders Carlssonbcba2012008-01-31 02:13:57 +0000931 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
932 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000933 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000935 if (!FDecl->getIdentifier())
936 return 0;
937
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000938 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000939}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000940
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000941QualType CallExpr::getCallReturnType() const {
942 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000943 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000944 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000945 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000946 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000947 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
948 // This should never be overloaded and so should never return null.
949 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000950
John McCall864c0412011-04-26 20:42:42 +0000951 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000952 return FnType->getResultType();
953}
Chris Lattnercb888962008-10-06 05:00:53 +0000954
John McCall2882eca2011-02-21 06:23:05 +0000955SourceRange CallExpr::getSourceRange() const {
956 if (isa<CXXOperatorCallExpr>(this))
957 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
958
959 SourceLocation begin = getCallee()->getLocStart();
960 if (begin.isInvalid() && getNumArgs() > 0)
961 begin = getArg(0)->getLocStart();
962 SourceLocation end = getRParenLoc();
963 if (end.isInvalid() && getNumArgs() > 0)
964 end = getArg(getNumArgs() - 1)->getLocEnd();
965 return SourceRange(begin, end);
966}
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +0000967SourceLocation CallExpr::getLocStart() const {
968 if (isa<CXXOperatorCallExpr>(this))
969 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getBegin();
970
971 SourceLocation begin = getCallee()->getLocStart();
972 if (begin.isInvalid() && getNumArgs() > 0)
973 begin = getArg(0)->getLocStart();
974 return begin;
975}
976SourceLocation CallExpr::getLocEnd() const {
977 if (isa<CXXOperatorCallExpr>(this))
978 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getEnd();
979
980 SourceLocation end = getRParenLoc();
981 if (end.isInvalid() && getNumArgs() > 0)
982 end = getArg(getNumArgs() - 1)->getLocEnd();
983 return end;
984}
John McCall2882eca2011-02-21 06:23:05 +0000985
Sean Huntc3021132010-05-05 15:23:54 +0000986OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000987 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000988 TypeSourceInfo *tsi,
989 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000990 Expr** exprsPtr, unsigned numExprs,
991 SourceLocation RParenLoc) {
992 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000993 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000994 sizeof(Expr*) * numExprs);
995
996 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
997 exprsPtr, numExprs, RParenLoc);
998}
999
1000OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
1001 unsigned numComps, unsigned numExprs) {
1002 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1003 sizeof(OffsetOfNode) * numComps +
1004 sizeof(Expr*) * numExprs);
1005 return new (Mem) OffsetOfExpr(numComps, numExprs);
1006}
1007
Sean Huntc3021132010-05-05 15:23:54 +00001008OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001009 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +00001010 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001011 Expr** exprsPtr, unsigned numExprs,
1012 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001013 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1014 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001015 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001016 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001017 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001018 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
1019 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001020{
1021 for(unsigned i = 0; i < numComps; ++i) {
1022 setComponent(i, compsPtr[i]);
1023 }
Sean Huntc3021132010-05-05 15:23:54 +00001024
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001025 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001026 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
1027 ExprBits.ValueDependent = true;
1028 if (exprsPtr[i]->containsUnexpandedParameterPack())
1029 ExprBits.ContainsUnexpandedParameterPack = true;
1030
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001031 setIndexExpr(i, exprsPtr[i]);
1032 }
1033}
1034
1035IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1036 assert(getKind() == Field || getKind() == Identifier);
1037 if (getKind() == Field)
1038 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001039
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001040 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1041}
1042
Mike Stump1eb44332009-09-09 15:08:12 +00001043MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001044 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001045 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001046 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001047 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001048 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001049 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001050 QualType ty,
1051 ExprValueKind vk,
1052 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001053 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001054
Douglas Gregor40d96a62011-02-28 21:54:11 +00001055 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001056 founddecl.getDecl() != memberdecl ||
1057 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001058 if (hasQualOrFound)
1059 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001060
John McCalld5532b62009-11-23 01:53:49 +00001061 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001062 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1063 else if (TemplateKWLoc.isValid())
1064 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Chris Lattner32488542010-10-30 05:14:06 +00001066 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001067 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1068 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001069
1070 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001071 // FIXME: Wrong. We should be looking at the member declaration we found.
1072 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001073 E->setValueDependent(true);
1074 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001075 E->setInstantiationDependent(true);
1076 }
1077 else if (QualifierLoc &&
1078 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1079 E->setInstantiationDependent(true);
1080
John McCall6bb80172010-03-30 21:47:33 +00001081 E->HasQualifierOrFoundDecl = true;
1082
1083 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001084 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001085 NQ->FoundDecl = founddecl;
1086 }
1087
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001088 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1089
John McCall6bb80172010-03-30 21:47:33 +00001090 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001091 bool Dependent = false;
1092 bool InstantiationDependent = false;
1093 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001094 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1095 Dependent,
1096 InstantiationDependent,
1097 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001098 if (InstantiationDependent)
1099 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001100 } else if (TemplateKWLoc.isValid()) {
1101 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001102 }
1103
1104 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001105}
1106
Douglas Gregor75e85042011-03-02 21:06:53 +00001107SourceRange MemberExpr::getSourceRange() const {
Daniel Dunbar396ec672012-03-09 15:39:15 +00001108 return SourceRange(getLocStart(), getLocEnd());
1109}
1110SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001111 if (isImplicitAccess()) {
1112 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001113 return getQualifierLoc().getBeginLoc();
1114 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001115 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001116
Daniel Dunbar396ec672012-03-09 15:39:15 +00001117 // FIXME: We don't want this to happen. Rather, we should be able to
1118 // detect all kinds of implicit accesses more cleanly.
1119 SourceLocation BaseStartLoc = getBase()->getLocStart();
1120 if (BaseStartLoc.isValid())
1121 return BaseStartLoc;
1122 return MemberLoc;
1123}
1124SourceLocation MemberExpr::getLocEnd() const {
1125 if (hasExplicitTemplateArgs())
1126 return getRAngleLoc();
1127 return getMemberNameInfo().getEndLoc();
Douglas Gregor75e85042011-03-02 21:06:53 +00001128}
1129
John McCall1d9b3b22011-09-09 05:25:32 +00001130void CastExpr::CheckCastConsistency() const {
1131 switch (getCastKind()) {
1132 case CK_DerivedToBase:
1133 case CK_UncheckedDerivedToBase:
1134 case CK_DerivedToBaseMemberPointer:
1135 case CK_BaseToDerived:
1136 case CK_BaseToDerivedMemberPointer:
1137 assert(!path_empty() && "Cast kind should have a base path!");
1138 break;
1139
1140 case CK_CPointerToObjCPointerCast:
1141 assert(getType()->isObjCObjectPointerType());
1142 assert(getSubExpr()->getType()->isPointerType());
1143 goto CheckNoBasePath;
1144
1145 case CK_BlockPointerToObjCPointerCast:
1146 assert(getType()->isObjCObjectPointerType());
1147 assert(getSubExpr()->getType()->isBlockPointerType());
1148 goto CheckNoBasePath;
1149
John McCall4d4e5c12012-02-15 01:22:51 +00001150 case CK_ReinterpretMemberPointer:
1151 assert(getType()->isMemberPointerType());
1152 assert(getSubExpr()->getType()->isMemberPointerType());
1153 goto CheckNoBasePath;
1154
John McCall1d9b3b22011-09-09 05:25:32 +00001155 case CK_BitCast:
1156 // Arbitrary casts to C pointer types count as bitcasts.
1157 // Otherwise, we should only have block and ObjC pointer casts
1158 // here if they stay within the type kind.
1159 if (!getType()->isPointerType()) {
1160 assert(getType()->isObjCObjectPointerType() ==
1161 getSubExpr()->getType()->isObjCObjectPointerType());
1162 assert(getType()->isBlockPointerType() ==
1163 getSubExpr()->getType()->isBlockPointerType());
1164 }
1165 goto CheckNoBasePath;
1166
1167 case CK_AnyPointerToBlockPointerCast:
1168 assert(getType()->isBlockPointerType());
1169 assert(getSubExpr()->getType()->isAnyPointerType() &&
1170 !getSubExpr()->getType()->isBlockPointerType());
1171 goto CheckNoBasePath;
1172
Douglas Gregorac1303e2012-02-22 05:02:47 +00001173 case CK_CopyAndAutoreleaseBlockObject:
1174 assert(getType()->isBlockPointerType());
1175 assert(getSubExpr()->getType()->isBlockPointerType());
1176 goto CheckNoBasePath;
1177
John McCall1d9b3b22011-09-09 05:25:32 +00001178 // These should not have an inheritance path.
1179 case CK_Dynamic:
1180 case CK_ToUnion:
1181 case CK_ArrayToPointerDecay:
1182 case CK_FunctionToPointerDecay:
1183 case CK_NullToMemberPointer:
1184 case CK_NullToPointer:
1185 case CK_ConstructorConversion:
1186 case CK_IntegralToPointer:
1187 case CK_PointerToIntegral:
1188 case CK_ToVoid:
1189 case CK_VectorSplat:
1190 case CK_IntegralCast:
1191 case CK_IntegralToFloating:
1192 case CK_FloatingToIntegral:
1193 case CK_FloatingCast:
1194 case CK_ObjCObjectLValueCast:
1195 case CK_FloatingRealToComplex:
1196 case CK_FloatingComplexToReal:
1197 case CK_FloatingComplexCast:
1198 case CK_FloatingComplexToIntegralComplex:
1199 case CK_IntegralRealToComplex:
1200 case CK_IntegralComplexToReal:
1201 case CK_IntegralComplexCast:
1202 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001203 case CK_ARCProduceObject:
1204 case CK_ARCConsumeObject:
1205 case CK_ARCReclaimReturnedObject:
1206 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001207 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1208 goto CheckNoBasePath;
1209
1210 case CK_Dependent:
1211 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001212 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001213 case CK_AtomicToNonAtomic:
1214 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001215 case CK_PointerToBoolean:
1216 case CK_IntegralToBoolean:
1217 case CK_FloatingToBoolean:
1218 case CK_MemberPointerToBoolean:
1219 case CK_FloatingComplexToBoolean:
1220 case CK_IntegralComplexToBoolean:
1221 case CK_LValueBitCast: // -> bool&
1222 case CK_UserDefinedConversion: // operator bool()
1223 CheckNoBasePath:
1224 assert(path_empty() && "Cast kind should not have a base path!");
1225 break;
1226 }
1227}
1228
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001229const char *CastExpr::getCastKindName() const {
1230 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001231 case CK_Dependent:
1232 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001233 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001234 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001235 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001236 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001237 case CK_LValueToRValue:
1238 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001239 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001240 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001241 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001242 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001243 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001244 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001245 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001246 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001247 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001248 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001249 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001250 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001251 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001252 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001253 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001254 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001255 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001256 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001257 case CK_NullToPointer:
1258 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001259 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001260 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001261 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001262 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001263 case CK_ReinterpretMemberPointer:
1264 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001265 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001266 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001267 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001268 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001269 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001270 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001271 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001272 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001273 case CK_PointerToBoolean:
1274 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001275 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001276 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001277 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001278 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001279 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001280 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001281 case CK_IntegralToBoolean:
1282 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001283 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001284 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001285 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001286 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001287 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001288 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001289 case CK_FloatingToBoolean:
1290 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001291 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001292 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001293 case CK_CPointerToObjCPointerCast:
1294 return "CPointerToObjCPointerCast";
1295 case CK_BlockPointerToObjCPointerCast:
1296 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001297 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001298 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001299 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001300 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001301 case CK_FloatingRealToComplex:
1302 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001303 case CK_FloatingComplexToReal:
1304 return "FloatingComplexToReal";
1305 case CK_FloatingComplexToBoolean:
1306 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001307 case CK_FloatingComplexCast:
1308 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001309 case CK_FloatingComplexToIntegralComplex:
1310 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001311 case CK_IntegralRealToComplex:
1312 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001313 case CK_IntegralComplexToReal:
1314 return "IntegralComplexToReal";
1315 case CK_IntegralComplexToBoolean:
1316 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001317 case CK_IntegralComplexCast:
1318 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001319 case CK_IntegralComplexToFloatingComplex:
1320 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001321 case CK_ARCConsumeObject:
1322 return "ARCConsumeObject";
1323 case CK_ARCProduceObject:
1324 return "ARCProduceObject";
1325 case CK_ARCReclaimReturnedObject:
1326 return "ARCReclaimReturnedObject";
1327 case CK_ARCExtendBlockObject:
1328 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001329 case CK_AtomicToNonAtomic:
1330 return "AtomicToNonAtomic";
1331 case CK_NonAtomicToAtomic:
1332 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001333 case CK_CopyAndAutoreleaseBlockObject:
1334 return "CopyAndAutoreleaseBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001335 }
Mike Stump1eb44332009-09-09 15:08:12 +00001336
John McCall2bb5d002010-11-13 09:02:35 +00001337 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001338}
1339
Douglas Gregor6eef5192009-12-14 19:27:10 +00001340Expr *CastExpr::getSubExprAsWritten() {
1341 Expr *SubExpr = 0;
1342 CastExpr *E = this;
1343 do {
1344 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001345
1346 // Skip through reference binding to temporary.
1347 if (MaterializeTemporaryExpr *Materialize
1348 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1349 SubExpr = Materialize->GetTemporaryExpr();
1350
Douglas Gregor6eef5192009-12-14 19:27:10 +00001351 // Skip any temporary bindings; they're implicit.
1352 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1353 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001354
Douglas Gregor6eef5192009-12-14 19:27:10 +00001355 // Conversions by constructor and conversion functions have a
1356 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001357 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001358 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001359 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001360 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001361
Douglas Gregor6eef5192009-12-14 19:27:10 +00001362 // If the subexpression we're left with is an implicit cast, look
1363 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001364 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1365
Douglas Gregor6eef5192009-12-14 19:27:10 +00001366 return SubExpr;
1367}
1368
John McCallf871d0c2010-08-07 06:22:56 +00001369CXXBaseSpecifier **CastExpr::path_buffer() {
1370 switch (getStmtClass()) {
1371#define ABSTRACT_STMT(x)
1372#define CASTEXPR(Type, Base) \
1373 case Stmt::Type##Class: \
1374 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1375#define STMT(Type, Base)
1376#include "clang/AST/StmtNodes.inc"
1377 default:
1378 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001379 }
1380}
1381
1382void CastExpr::setCastPath(const CXXCastPath &Path) {
1383 assert(Path.size() == path_size());
1384 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1385}
1386
1387ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1388 CastKind Kind, Expr *Operand,
1389 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001390 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001391 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1392 void *Buffer =
1393 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1394 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001395 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001396 if (PathSize) E->setCastPath(*BasePath);
1397 return E;
1398}
1399
1400ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1401 unsigned PathSize) {
1402 void *Buffer =
1403 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1404 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1405}
1406
1407
1408CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001409 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001410 const CXXCastPath *BasePath,
1411 TypeSourceInfo *WrittenTy,
1412 SourceLocation L, SourceLocation R) {
1413 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1414 void *Buffer =
1415 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1416 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001417 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001418 if (PathSize) E->setCastPath(*BasePath);
1419 return E;
1420}
1421
1422CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1423 void *Buffer =
1424 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1425 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1426}
1427
Reid Spencer5f016e22007-07-11 17:01:13 +00001428/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1429/// corresponds to, e.g. "<<=".
1430const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1431 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001432 case BO_PtrMemD: return ".*";
1433 case BO_PtrMemI: return "->*";
1434 case BO_Mul: return "*";
1435 case BO_Div: return "/";
1436 case BO_Rem: return "%";
1437 case BO_Add: return "+";
1438 case BO_Sub: return "-";
1439 case BO_Shl: return "<<";
1440 case BO_Shr: return ">>";
1441 case BO_LT: return "<";
1442 case BO_GT: return ">";
1443 case BO_LE: return "<=";
1444 case BO_GE: return ">=";
1445 case BO_EQ: return "==";
1446 case BO_NE: return "!=";
1447 case BO_And: return "&";
1448 case BO_Xor: return "^";
1449 case BO_Or: return "|";
1450 case BO_LAnd: return "&&";
1451 case BO_LOr: return "||";
1452 case BO_Assign: return "=";
1453 case BO_MulAssign: return "*=";
1454 case BO_DivAssign: return "/=";
1455 case BO_RemAssign: return "%=";
1456 case BO_AddAssign: return "+=";
1457 case BO_SubAssign: return "-=";
1458 case BO_ShlAssign: return "<<=";
1459 case BO_ShrAssign: return ">>=";
1460 case BO_AndAssign: return "&=";
1461 case BO_XorAssign: return "^=";
1462 case BO_OrAssign: return "|=";
1463 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001464 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001465
David Blaikie30263482012-01-20 21:50:17 +00001466 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001467}
1468
John McCall2de56d12010-08-25 11:45:40 +00001469BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001470BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1471 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001472 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001473 case OO_Plus: return BO_Add;
1474 case OO_Minus: return BO_Sub;
1475 case OO_Star: return BO_Mul;
1476 case OO_Slash: return BO_Div;
1477 case OO_Percent: return BO_Rem;
1478 case OO_Caret: return BO_Xor;
1479 case OO_Amp: return BO_And;
1480 case OO_Pipe: return BO_Or;
1481 case OO_Equal: return BO_Assign;
1482 case OO_Less: return BO_LT;
1483 case OO_Greater: return BO_GT;
1484 case OO_PlusEqual: return BO_AddAssign;
1485 case OO_MinusEqual: return BO_SubAssign;
1486 case OO_StarEqual: return BO_MulAssign;
1487 case OO_SlashEqual: return BO_DivAssign;
1488 case OO_PercentEqual: return BO_RemAssign;
1489 case OO_CaretEqual: return BO_XorAssign;
1490 case OO_AmpEqual: return BO_AndAssign;
1491 case OO_PipeEqual: return BO_OrAssign;
1492 case OO_LessLess: return BO_Shl;
1493 case OO_GreaterGreater: return BO_Shr;
1494 case OO_LessLessEqual: return BO_ShlAssign;
1495 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1496 case OO_EqualEqual: return BO_EQ;
1497 case OO_ExclaimEqual: return BO_NE;
1498 case OO_LessEqual: return BO_LE;
1499 case OO_GreaterEqual: return BO_GE;
1500 case OO_AmpAmp: return BO_LAnd;
1501 case OO_PipePipe: return BO_LOr;
1502 case OO_Comma: return BO_Comma;
1503 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001504 }
1505}
1506
1507OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1508 static const OverloadedOperatorKind OverOps[] = {
1509 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1510 OO_Star, OO_Slash, OO_Percent,
1511 OO_Plus, OO_Minus,
1512 OO_LessLess, OO_GreaterGreater,
1513 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1514 OO_EqualEqual, OO_ExclaimEqual,
1515 OO_Amp,
1516 OO_Caret,
1517 OO_Pipe,
1518 OO_AmpAmp,
1519 OO_PipePipe,
1520 OO_Equal, OO_StarEqual,
1521 OO_SlashEqual, OO_PercentEqual,
1522 OO_PlusEqual, OO_MinusEqual,
1523 OO_LessLessEqual, OO_GreaterGreaterEqual,
1524 OO_AmpEqual, OO_CaretEqual,
1525 OO_PipeEqual,
1526 OO_Comma
1527 };
1528 return OverOps[Opc];
1529}
1530
Ted Kremenek709210f2010-04-13 23:39:13 +00001531InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001532 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001533 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001534 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001535 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001536 InitExprs(C, numInits),
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001537 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0)
1538{
1539 sawArrayRangeDesignator(false);
1540 setInitializesStdInitializerList(false);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001541 for (unsigned I = 0; I != numInits; ++I) {
1542 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001543 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001544 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001545 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001546 if (initExprs[I]->isInstantiationDependent())
1547 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001548 if (initExprs[I]->containsUnexpandedParameterPack())
1549 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001550 }
Sean Huntc3021132010-05-05 15:23:54 +00001551
Ted Kremenek709210f2010-04-13 23:39:13 +00001552 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001553}
Reid Spencer5f016e22007-07-11 17:01:13 +00001554
Ted Kremenek709210f2010-04-13 23:39:13 +00001555void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001556 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001557 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001558}
1559
Ted Kremenek709210f2010-04-13 23:39:13 +00001560void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001561 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001562}
1563
Ted Kremenek709210f2010-04-13 23:39:13 +00001564Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001565 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001566 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001567 InitExprs.back() = expr;
1568 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001569 }
Mike Stump1eb44332009-09-09 15:08:12 +00001570
Douglas Gregor4c678342009-01-28 21:54:33 +00001571 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1572 InitExprs[Init] = expr;
1573 return Result;
1574}
1575
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001576void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001577 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001578 ArrayFillerOrUnionFieldInit = filler;
1579 // Fill out any "holes" in the array due to designated initializers.
1580 Expr **inits = getInits();
1581 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1582 if (inits[i] == 0)
1583 inits[i] = filler;
1584}
1585
Richard Smithfe587202012-04-15 02:50:59 +00001586bool InitListExpr::isStringLiteralInit() const {
1587 if (getNumInits() != 1)
1588 return false;
1589 const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(getType());
1590 if (!CAT || !CAT->getElementType()->isIntegerType())
1591 return false;
1592 const Expr *Init = getInit(0)->IgnoreParenImpCasts();
1593 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1594}
1595
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001596SourceRange InitListExpr::getSourceRange() const {
1597 if (SyntacticForm)
1598 return SyntacticForm->getSourceRange();
1599 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1600 if (Beg.isInvalid()) {
1601 // Find the first non-null initializer.
1602 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1603 E = InitExprs.end();
1604 I != E; ++I) {
1605 if (Stmt *S = *I) {
1606 Beg = S->getLocStart();
1607 break;
1608 }
1609 }
1610 }
1611 if (End.isInvalid()) {
1612 // Find the first non-null initializer from the end.
1613 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1614 E = InitExprs.rend();
1615 I != E; ++I) {
1616 if (Stmt *S = *I) {
1617 End = S->getSourceRange().getEnd();
1618 break;
1619 }
1620 }
1621 }
1622 return SourceRange(Beg, End);
1623}
1624
Steve Naroffbfdcae62008-09-04 15:31:07 +00001625/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001626///
John McCalla345edb2012-02-17 03:32:35 +00001627const FunctionProtoType *BlockExpr::getFunctionType() const {
1628 // The block pointer is never sugared, but the function type might be.
1629 return cast<BlockPointerType>(getType())
1630 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001631}
1632
Mike Stump1eb44332009-09-09 15:08:12 +00001633SourceLocation BlockExpr::getCaretLocation() const {
1634 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001635}
Mike Stump1eb44332009-09-09 15:08:12 +00001636const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001637 return TheBlock->getBody();
1638}
Mike Stump1eb44332009-09-09 15:08:12 +00001639Stmt *BlockExpr::getBody() {
1640 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001641}
Steve Naroff56ee6892008-10-08 17:01:13 +00001642
1643
Reid Spencer5f016e22007-07-11 17:01:13 +00001644//===----------------------------------------------------------------------===//
1645// Generic Expression Routines
1646//===----------------------------------------------------------------------===//
1647
Chris Lattner026dc962009-02-14 07:37:35 +00001648/// isUnusedResultAWarning - Return true if this immediate expression should
1649/// be warned about if the result is unused. If so, fill in Loc and Ranges
1650/// with location to warn on and the source range[s] to report with the
1651/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001652bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1653 SourceRange &R1, SourceRange &R2,
1654 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001655 // Don't warn if the expr is type dependent. The type could end up
1656 // instantiating to void.
1657 if (isTypeDependent())
1658 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001659
Reid Spencer5f016e22007-07-11 17:01:13 +00001660 switch (getStmtClass()) {
1661 default:
John McCall0faede62010-03-12 07:11:26 +00001662 if (getType()->isVoidType())
1663 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001664 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001665 Loc = getExprLoc();
1666 R1 = getSourceRange();
1667 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001668 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001669 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001670 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001671 case GenericSelectionExprClass:
1672 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001673 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 case UnaryOperatorClass: {
1675 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001676
Reid Spencer5f016e22007-07-11 17:01:13 +00001677 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001678 case UO_Plus:
1679 case UO_Minus:
1680 case UO_AddrOf:
1681 case UO_Not:
1682 case UO_LNot:
1683 case UO_Deref:
1684 break;
John McCall2de56d12010-08-25 11:45:40 +00001685 case UO_PostInc:
1686 case UO_PostDec:
1687 case UO_PreInc:
1688 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001689 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001690 case UO_Real:
1691 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001692 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001693 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1694 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001695 return false;
1696 break;
John McCall2de56d12010-08-25 11:45:40 +00001697 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00001698 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001699 }
Eli Friedmana6115062012-05-24 00:47:05 +00001700 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001701 Loc = UO->getOperatorLoc();
1702 R1 = UO->getSubExpr()->getSourceRange();
1703 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001705 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001706 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001707 switch (BO->getOpcode()) {
1708 default:
1709 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001710 // Consider the RHS of comma for side effects. LHS was checked by
1711 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001712 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001713 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1714 // lvalue-ness) of an assignment written in a macro.
1715 if (IntegerLiteral *IE =
1716 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1717 if (IE->getValue() == 0)
1718 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001719 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001720 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001721 case BO_LAnd:
1722 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00001723 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1724 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001725 return false;
1726 break;
John McCallbf0ee352010-02-16 04:10:53 +00001727 }
Chris Lattner026dc962009-02-14 07:37:35 +00001728 if (BO->isAssignmentOp())
1729 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001730 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001731 Loc = BO->getOperatorLoc();
1732 R1 = BO->getLHS()->getSourceRange();
1733 R2 = BO->getRHS()->getSourceRange();
1734 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001735 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001736 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001737 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001738 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001739 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001740
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001741 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001742 // If only one of the LHS or RHS is a warning, the operator might
1743 // be being used for control flow. Only warn if both the LHS and
1744 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001745 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00001746 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001747 return false;
1748 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001749 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00001750 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001751 }
1752
Reid Spencer5f016e22007-07-11 17:01:13 +00001753 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001754 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001755 Loc = cast<MemberExpr>(this)->getMemberLoc();
1756 R1 = SourceRange(Loc, Loc);
1757 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1758 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Reid Spencer5f016e22007-07-11 17:01:13 +00001760 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001761 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001762 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1763 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1764 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1765 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001766
Chandler Carruth9b106832011-08-17 09:49:44 +00001767 case CXXOperatorCallExprClass: {
1768 // We warn about operator== and operator!= even when user-defined operator
1769 // overloads as there is no reasonable way to define these such that they
1770 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1771 // warning: these operators are commonly typo'ed, and so warning on them
1772 // provides additional value as well. If this list is updated,
1773 // DiagnoseUnusedComparison should be as well.
1774 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1775 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001776 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmana6115062012-05-24 00:47:05 +00001777 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001778 Loc = Op->getOperatorLoc();
1779 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001780 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001781 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001782
1783 // Fallthrough for generic call handling.
1784 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001785 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00001786 case CXXMemberCallExprClass:
1787 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001788 // If this is a direct call, get the callee.
1789 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001790 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001791 // If the callee has attribute pure, const, or warn_unused_result, warn
1792 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001793 //
1794 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1795 // updated to match for QoI.
1796 if (FD->getAttr<WarnUnusedResultAttr>() ||
1797 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001798 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001799 Loc = CE->getCallee()->getLocStart();
1800 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001801
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001802 if (unsigned NumArgs = CE->getNumArgs())
1803 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1804 CE->getArg(NumArgs-1)->getLocEnd());
1805 return true;
1806 }
Chris Lattner026dc962009-02-14 07:37:35 +00001807 }
1808 return false;
1809 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001810
1811 case CXXTemporaryObjectExprClass:
1812 case CXXConstructExprClass:
1813 return false;
1814
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001815 case ObjCMessageExprClass: {
1816 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00001817 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001818 ME->isInstanceMessage() &&
1819 !ME->getType()->isVoidType() &&
1820 ME->getSelector().getIdentifierInfoForSlot(0) &&
1821 ME->getSelector().getIdentifierInfoForSlot(0)
1822 ->getName().startswith("init")) {
Eli Friedmana6115062012-05-24 00:47:05 +00001823 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00001824 Loc = getExprLoc();
1825 R1 = ME->getSourceRange();
1826 return true;
1827 }
1828
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001829 const ObjCMethodDecl *MD = ME->getMethodDecl();
1830 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001831 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001832 Loc = getExprLoc();
1833 return true;
1834 }
Chris Lattner026dc962009-02-14 07:37:35 +00001835 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001836 }
Mike Stump1eb44332009-09-09 15:08:12 +00001837
John McCall12f78a62010-12-02 01:19:52 +00001838 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001839 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001840 Loc = getExprLoc();
1841 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001842 return true;
John McCall12f78a62010-12-02 01:19:52 +00001843
John McCall4b9c2d22011-11-06 09:01:30 +00001844 case PseudoObjectExprClass: {
1845 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1846
1847 // Only complain about things that have the form of a getter.
1848 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1849 isa<BinaryOperator>(PO->getSyntacticForm()))
1850 return false;
1851
Eli Friedmana6115062012-05-24 00:47:05 +00001852 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00001853 Loc = getExprLoc();
1854 R1 = getSourceRange();
1855 return true;
1856 }
1857
Chris Lattner611b2ec2008-07-26 19:51:01 +00001858 case StmtExprClass: {
1859 // Statement exprs don't logically have side effects themselves, but are
1860 // sometimes used in macros in ways that give them a type that is unused.
1861 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1862 // however, if the result of the stmt expr is dead, we don't want to emit a
1863 // warning.
1864 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001865 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001866 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00001867 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001868 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1869 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00001870 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001871 }
Mike Stump1eb44332009-09-09 15:08:12 +00001872
John McCall0faede62010-03-12 07:11:26 +00001873 if (getType()->isVoidType())
1874 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001875 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001876 Loc = cast<StmtExpr>(this)->getLParenLoc();
1877 R1 = getSourceRange();
1878 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001879 }
Eli Friedmana6115062012-05-24 00:47:05 +00001880 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00001881 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00001882 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00001883 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00001884 if (CE->getCastKind() == CK_ToVoid) {
1885 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00001886 CE->getSubExpr()->getType().isVolatileQualified()) {
1887 const DeclRefExpr *DRE =
1888 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
1889 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
1890 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
1891 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
1892 R1, R2, Ctx);
1893 }
1894 }
Chris Lattnerfb846642009-07-28 18:25:28 +00001895 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001896 }
Eli Friedman4059da82012-05-24 21:05:41 +00001897
Eli Friedmana6115062012-05-24 00:47:05 +00001898 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00001899 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00001900 if (CE->getCastKind() == CK_ConstructorConversion)
1901 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00001902
Eli Friedmana6115062012-05-24 00:47:05 +00001903 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00001904 if (const CXXFunctionalCastExpr *CXXCE =
1905 dyn_cast<CXXFunctionalCastExpr>(this)) {
1906 Loc = CXXCE->getTypeBeginLoc();
1907 R1 = CXXCE->getSubExpr()->getSourceRange();
1908 } else {
1909 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
1910 Loc = CStyleCE->getLParenLoc();
1911 R1 = CStyleCE->getSubExpr()->getSourceRange();
1912 }
Chris Lattner026dc962009-02-14 07:37:35 +00001913 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001914 }
Eli Friedmana6115062012-05-24 00:47:05 +00001915 case ImplicitCastExprClass: {
1916 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00001917
Eli Friedmana6115062012-05-24 00:47:05 +00001918 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
1919 if (ICE->getCastKind() == CK_LValueToRValue &&
1920 ICE->getSubExpr()->getType().isVolatileQualified())
1921 return false;
1922
1923 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
1924 }
Chris Lattner04421082008-04-08 04:40:51 +00001925 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001926 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00001927 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001928
1929 case CXXNewExprClass:
1930 // FIXME: In theory, there might be new expressions that don't have side
1931 // effects (e.g. a placement new with an uninitialized POD).
1932 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001933 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001934 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001935 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00001936 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001937 case ExprWithCleanupsClass:
1938 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00001939 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001940 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001941}
1942
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001943/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001944/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001945bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001946 const Expr *E = IgnoreParens();
1947 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001948 default:
1949 return false;
1950 case ObjCIvarRefExprClass:
1951 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001952 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001953 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001954 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001955 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001956 case MaterializeTemporaryExprClass:
1957 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1958 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001959 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001960 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001961 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00001962 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001963
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001964 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1965 if (VD->hasGlobalStorage())
1966 return true;
1967 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001968 // dereferencing to a pointer is always a gc'able candidate,
1969 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001970 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001971 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001972 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001973 return false;
1974 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001975 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001976 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001977 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001978 }
1979 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001980 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001981 }
1982}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001983
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001984bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1985 if (isTypeDependent())
1986 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001987 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001988}
1989
John McCall864c0412011-04-26 20:42:42 +00001990QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00001991 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00001992
1993 // Bound member expressions are always one of these possibilities:
1994 // x->m x.m x->*y x.*y
1995 // (possibly parenthesized)
1996
1997 expr = expr->IgnoreParens();
1998 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1999 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2000 return mem->getMemberDecl()->getType();
2001 }
2002
2003 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2004 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2005 ->getPointeeType();
2006 assert(type->isFunctionType());
2007 return type;
2008 }
2009
2010 assert(isa<UnresolvedMemberExpr>(expr));
2011 return QualType();
2012}
2013
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002014Expr* Expr::IgnoreParens() {
2015 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002016 while (true) {
2017 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2018 E = P->getSubExpr();
2019 continue;
2020 }
2021 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2022 if (P->getOpcode() == UO_Extension) {
2023 E = P->getSubExpr();
2024 continue;
2025 }
2026 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002027 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2028 if (!P->isResultDependent()) {
2029 E = P->getResultExpr();
2030 continue;
2031 }
2032 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002033 return E;
2034 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002035}
2036
Chris Lattner56f34942008-02-13 01:02:39 +00002037/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2038/// or CastExprs or ImplicitCastExprs, returning their operand.
2039Expr *Expr::IgnoreParenCasts() {
2040 Expr *E = this;
2041 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002042 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002043 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002044 continue;
2045 }
2046 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002047 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002048 continue;
2049 }
2050 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2051 if (P->getOpcode() == UO_Extension) {
2052 E = P->getSubExpr();
2053 continue;
2054 }
2055 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002056 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2057 if (!P->isResultDependent()) {
2058 E = P->getResultExpr();
2059 continue;
2060 }
2061 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002062 if (MaterializeTemporaryExpr *Materialize
2063 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2064 E = Materialize->GetTemporaryExpr();
2065 continue;
2066 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002067 if (SubstNonTypeTemplateParmExpr *NTTP
2068 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2069 E = NTTP->getReplacement();
2070 continue;
2071 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002072 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002073 }
2074}
2075
John McCall9c5d70c2010-12-04 08:24:19 +00002076/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2077/// casts. This is intended purely as a temporary workaround for code
2078/// that hasn't yet been rewritten to do the right thing about those
2079/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002080Expr *Expr::IgnoreParenLValueCasts() {
2081 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002082 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002083 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2084 E = P->getSubExpr();
2085 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002086 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002087 if (P->getCastKind() == CK_LValueToRValue) {
2088 E = P->getSubExpr();
2089 continue;
2090 }
John McCall9c5d70c2010-12-04 08:24:19 +00002091 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2092 if (P->getOpcode() == UO_Extension) {
2093 E = P->getSubExpr();
2094 continue;
2095 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002096 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2097 if (!P->isResultDependent()) {
2098 E = P->getResultExpr();
2099 continue;
2100 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002101 } else if (MaterializeTemporaryExpr *Materialize
2102 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2103 E = Materialize->GetTemporaryExpr();
2104 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002105 } else if (SubstNonTypeTemplateParmExpr *NTTP
2106 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2107 E = NTTP->getReplacement();
2108 continue;
John McCallf6a16482010-12-04 03:47:34 +00002109 }
2110 break;
2111 }
2112 return E;
2113}
2114
John McCall2fc46bf2010-05-05 22:59:52 +00002115Expr *Expr::IgnoreParenImpCasts() {
2116 Expr *E = this;
2117 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002118 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002119 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002120 continue;
2121 }
2122 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002123 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002124 continue;
2125 }
2126 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2127 if (P->getOpcode() == UO_Extension) {
2128 E = P->getSubExpr();
2129 continue;
2130 }
2131 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002132 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2133 if (!P->isResultDependent()) {
2134 E = P->getResultExpr();
2135 continue;
2136 }
2137 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002138 if (MaterializeTemporaryExpr *Materialize
2139 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2140 E = Materialize->GetTemporaryExpr();
2141 continue;
2142 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002143 if (SubstNonTypeTemplateParmExpr *NTTP
2144 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2145 E = NTTP->getReplacement();
2146 continue;
2147 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002148 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002149 }
2150}
2151
Hans Wennborg2f072b42011-06-09 17:06:51 +00002152Expr *Expr::IgnoreConversionOperator() {
2153 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002154 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002155 return MCE->getImplicitObjectArgument();
2156 }
2157 return this;
2158}
2159
Chris Lattnerecdd8412009-03-13 17:28:01 +00002160/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2161/// value (including ptr->int casts of the same size). Strip off any
2162/// ParenExpr or CastExprs, returning their operand.
2163Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2164 Expr *E = this;
2165 while (true) {
2166 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2167 E = P->getSubExpr();
2168 continue;
2169 }
Mike Stump1eb44332009-09-09 15:08:12 +00002170
Chris Lattnerecdd8412009-03-13 17:28:01 +00002171 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2172 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002173 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002174 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002175
Chris Lattnerecdd8412009-03-13 17:28:01 +00002176 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2177 E = SE;
2178 continue;
2179 }
Mike Stump1eb44332009-09-09 15:08:12 +00002180
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002181 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002182 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002183 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002184 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002185 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2186 E = SE;
2187 continue;
2188 }
2189 }
Mike Stump1eb44332009-09-09 15:08:12 +00002190
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002191 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2192 if (P->getOpcode() == UO_Extension) {
2193 E = P->getSubExpr();
2194 continue;
2195 }
2196 }
2197
Peter Collingbournef111d932011-04-15 00:35:48 +00002198 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2199 if (!P->isResultDependent()) {
2200 E = P->getResultExpr();
2201 continue;
2202 }
2203 }
2204
Douglas Gregorc0244c52011-09-08 17:56:33 +00002205 if (SubstNonTypeTemplateParmExpr *NTTP
2206 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2207 E = NTTP->getReplacement();
2208 continue;
2209 }
2210
Chris Lattnerecdd8412009-03-13 17:28:01 +00002211 return E;
2212 }
2213}
2214
Douglas Gregor6eef5192009-12-14 19:27:10 +00002215bool Expr::isDefaultArgument() const {
2216 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002217 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2218 E = M->GetTemporaryExpr();
2219
Douglas Gregor6eef5192009-12-14 19:27:10 +00002220 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2221 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002222
Douglas Gregor6eef5192009-12-14 19:27:10 +00002223 return isa<CXXDefaultArgExpr>(E);
2224}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002225
Douglas Gregor2f599792010-04-02 18:24:57 +00002226/// \brief Skip over any no-op casts and any temporary-binding
2227/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002228static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002229 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2230 E = M->GetTemporaryExpr();
2231
Douglas Gregor2f599792010-04-02 18:24:57 +00002232 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002233 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002234 E = ICE->getSubExpr();
2235 else
2236 break;
2237 }
2238
2239 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2240 E = BE->getSubExpr();
2241
2242 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002243 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002244 E = ICE->getSubExpr();
2245 else
2246 break;
2247 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002248
2249 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002250}
2251
John McCall558d2ab2010-09-15 10:14:12 +00002252/// isTemporaryObject - Determines if this expression produces a
2253/// temporary of the given class type.
2254bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2255 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2256 return false;
2257
Anders Carlssonf8b30152010-11-28 16:40:49 +00002258 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002259
John McCall58277b52010-09-15 20:59:13 +00002260 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002261 if (!E->Classify(C).isPRValue()) {
2262 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002263 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002264 return false;
2265 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002266
John McCall19e60ad2010-09-16 06:57:56 +00002267 // Black-list a few cases which yield pr-values of class type that don't
2268 // refer to temporaries of that type:
2269
2270 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002271 if (isa<ImplicitCastExpr>(E)) {
2272 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2273 case CK_DerivedToBase:
2274 case CK_UncheckedDerivedToBase:
2275 return false;
2276 default:
2277 break;
2278 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002279 }
2280
John McCall19e60ad2010-09-16 06:57:56 +00002281 // - member expressions (all)
2282 if (isa<MemberExpr>(E))
2283 return false;
2284
John McCall56ca35d2011-02-17 10:25:35 +00002285 // - opaque values (all)
2286 if (isa<OpaqueValueExpr>(E))
2287 return false;
2288
John McCall558d2ab2010-09-15 10:14:12 +00002289 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002290}
2291
Douglas Gregor75e85042011-03-02 21:06:53 +00002292bool Expr::isImplicitCXXThis() const {
2293 const Expr *E = this;
2294
2295 // Strip away parentheses and casts we don't care about.
2296 while (true) {
2297 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2298 E = Paren->getSubExpr();
2299 continue;
2300 }
2301
2302 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2303 if (ICE->getCastKind() == CK_NoOp ||
2304 ICE->getCastKind() == CK_LValueToRValue ||
2305 ICE->getCastKind() == CK_DerivedToBase ||
2306 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2307 E = ICE->getSubExpr();
2308 continue;
2309 }
2310 }
2311
2312 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2313 if (UnOp->getOpcode() == UO_Extension) {
2314 E = UnOp->getSubExpr();
2315 continue;
2316 }
2317 }
2318
Douglas Gregor03e80032011-06-21 17:03:29 +00002319 if (const MaterializeTemporaryExpr *M
2320 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2321 E = M->GetTemporaryExpr();
2322 continue;
2323 }
2324
Douglas Gregor75e85042011-03-02 21:06:53 +00002325 break;
2326 }
2327
2328 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2329 return This->isImplicit();
2330
2331 return false;
2332}
2333
Douglas Gregor898574e2008-12-05 23:32:09 +00002334/// hasAnyTypeDependentArguments - Determines if any of the expressions
2335/// in Exprs is type-dependent.
Ahmed Charles13a140c2012-02-25 11:00:22 +00002336bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2337 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002338 if (Exprs[I]->isTypeDependent())
2339 return true;
2340
2341 return false;
2342}
2343
John McCall4204f072010-08-02 21:13:48 +00002344bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002345 // This function is attempting whether an expression is an initializer
2346 // which can be evaluated at compile-time. isEvaluatable handles most
2347 // of the cases, but it can't deal with some initializer-specific
2348 // expressions, and it can't deal with aggregates; we deal with those here,
2349 // and fall back to isEvaluatable for the other cases.
2350
John McCall4204f072010-08-02 21:13:48 +00002351 // If we ever capture reference-binding directly in the AST, we can
2352 // kill the second parameter.
2353
2354 if (IsForRef) {
2355 EvalResult Result;
2356 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2357 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002358
Anders Carlssone8a32b82008-11-24 05:23:59 +00002359 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002360 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002361 case IntegerLiteralClass:
2362 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002363 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002364 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002365 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002366 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002367 case CXXTemporaryObjectExprClass:
2368 case CXXConstructExprClass: {
2369 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002370
2371 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002372 if (CE->getConstructor()->isTrivial()) {
2373 // 1) an application of the trivial default constructor or
2374 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002375
Richard Smith180f4792011-11-10 06:34:14 +00002376 // 2) an elidable trivial copy construction of an operand which is
2377 // itself a constant initializer. Note that we consider the
2378 // operand on its own, *not* as a reference binding.
2379 if (CE->isElidable() &&
2380 CE->getArg(0)->isConstantInitializer(Ctx, false))
2381 return true;
2382 }
2383
2384 // 3) a foldable constexpr constructor.
2385 break;
John McCallb4b9b152010-08-01 21:51:45 +00002386 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002387 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002388 // This handles gcc's extension that allows global initializers like
2389 // "struct x {int x;} x = (struct x) {};".
2390 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002391 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002392 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002393 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002394 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002395 // FIXME: This doesn't deal with fields with reference types correctly.
2396 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2397 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002398 const InitListExpr *Exp = cast<InitListExpr>(this);
2399 unsigned numInits = Exp->getNumInits();
2400 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002401 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002402 return false;
2403 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002404 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002405 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002406 case ImplicitValueInitExprClass:
2407 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002408 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002409 return cast<ParenExpr>(this)->getSubExpr()
2410 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002411 case GenericSelectionExprClass:
2412 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2413 return false;
2414 return cast<GenericSelectionExpr>(this)->getResultExpr()
2415 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002416 case ChooseExprClass:
2417 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2418 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002419 case UnaryOperatorClass: {
2420 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002421 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002422 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002423 break;
2424 }
John McCall4204f072010-08-02 21:13:48 +00002425 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002426 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002427 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002428 case CStyleCastExprClass: {
2429 const CastExpr *CE = cast<CastExpr>(this);
2430
David Chisnall7a7ee302012-01-16 17:27:18 +00002431 // If we're promoting an integer to an _Atomic type then this is constant
2432 // if the integer is constant. We also need to check the converse in case
2433 // someone does something like:
2434 //
2435 // int a = (_Atomic(int))42;
2436 //
2437 // I doubt anyone would write code like this directly, but it's quite
2438 // possible as the result of macro expansions.
2439 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2440 CE->getCastKind() == CK_AtomicToNonAtomic)
2441 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2442
Richard Smithd62ca372011-12-06 22:44:34 +00002443 // Handle bitcasts of vector constants.
2444 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2445 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2446
Eli Friedman6bd97192011-12-21 00:43:02 +00002447 // Handle misc casts we want to ignore.
2448 // FIXME: Is it really safe to ignore all these?
2449 if (CE->getCastKind() == CK_NoOp ||
2450 CE->getCastKind() == CK_LValueToRValue ||
2451 CE->getCastKind() == CK_ToUnion ||
2452 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002453 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2454
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002455 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002456 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002457 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002458 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002459 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002460 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002461 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002462}
2463
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002464namespace {
2465 /// \brief Look for a call to a non-trivial function within an expression.
2466 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2467 {
2468 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2469
2470 bool NonTrivial;
2471
2472 public:
2473 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002474 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002475
2476 bool hasNonTrivialCall() const { return NonTrivial; }
2477
2478 void VisitCallExpr(CallExpr *E) {
2479 if (CXXMethodDecl *Method
2480 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2481 if (Method->isTrivial()) {
2482 // Recurse to children of the call.
2483 Inherited::VisitStmt(E);
2484 return;
2485 }
2486 }
2487
2488 NonTrivial = true;
2489 }
2490
2491 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2492 if (E->getConstructor()->isTrivial()) {
2493 // Recurse to children of the call.
2494 Inherited::VisitStmt(E);
2495 return;
2496 }
2497
2498 NonTrivial = true;
2499 }
2500
2501 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2502 if (E->getTemporary()->getDestructor()->isTrivial()) {
2503 Inherited::VisitStmt(E);
2504 return;
2505 }
2506
2507 NonTrivial = true;
2508 }
2509 };
2510}
2511
2512bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2513 NonTrivialCallFinder Finder(Ctx);
2514 Finder.Visit(this);
2515 return Finder.hasNonTrivialCall();
2516}
2517
Chandler Carruth82214a82011-02-18 23:54:50 +00002518/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2519/// pointer constant or not, as well as the specific kind of constant detected.
2520/// Null pointer constants can be integer constant expressions with the
2521/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2522/// (a GNU extension).
2523Expr::NullPointerConstantKind
2524Expr::isNullPointerConstant(ASTContext &Ctx,
2525 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002526 if (isValueDependent()) {
2527 switch (NPC) {
2528 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002529 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002530 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002531 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2532 return NPCK_ZeroInteger;
2533 else
2534 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002535
Douglas Gregorce940492009-09-25 04:25:58 +00002536 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002537 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002538 }
2539 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002540
Sebastian Redl07779722008-10-31 14:43:28 +00002541 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002542 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002543 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002544 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002545 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002546 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002547 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002548 Pointee->isVoidType() && // to void*
2549 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002550 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002551 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002552 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002553 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2554 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002555 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002556 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2557 // Accept ((void*)0) as a null pointer constant, as many other
2558 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002559 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002560 } else if (const GenericSelectionExpr *GE =
2561 dyn_cast<GenericSelectionExpr>(this)) {
2562 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002563 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002564 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002565 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002566 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002567 } else if (isa<GNUNullExpr>(this)) {
2568 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002569 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002570 } else if (const MaterializeTemporaryExpr *M
2571 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2572 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002573 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2574 if (const Expr *Source = OVE->getSourceExpr())
2575 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002576 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002577
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002578 // C++0x nullptr_t is always a null pointer constant.
2579 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002580 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002581
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002582 if (const RecordType *UT = getType()->getAsUnionType())
2583 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2584 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2585 const Expr *InitExpr = CLE->getInitializer();
2586 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2587 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2588 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002589 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002590 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002591 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002592 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002593
Reid Spencer5f016e22007-07-11 17:01:13 +00002594 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith70488e22012-02-14 21:38:30 +00002595 // test for the value 0. Don't use the C++11 constant expression semantics
2596 // for this, for now; once the dust settles on core issue 903, we might only
2597 // allow a literal 0 here in C++11 mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002598 if (Ctx.getLangOpts().CPlusPlus0x) {
Richard Smith70488e22012-02-14 21:38:30 +00002599 if (!isCXX98IntegralConstantExpr(Ctx))
2600 return NPCK_NotNull;
2601 } else {
2602 if (!isIntegerConstantExpr(Ctx))
2603 return NPCK_NotNull;
2604 }
Chandler Carruth82214a82011-02-18 23:54:50 +00002605
Richard Smith70488e22012-02-14 21:38:30 +00002606 return (EvaluateKnownConstInt(Ctx) == 0) ? NPCK_ZeroInteger : NPCK_NotNull;
Reid Spencer5f016e22007-07-11 17:01:13 +00002607}
Steve Naroff31a45842007-07-28 23:10:27 +00002608
John McCallf6a16482010-12-04 03:47:34 +00002609/// \brief If this expression is an l-value for an Objective C
2610/// property, find the underlying property reference expression.
2611const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2612 const Expr *E = this;
2613 while (true) {
2614 assert((E->getValueKind() == VK_LValue &&
2615 E->getObjectKind() == OK_ObjCProperty) &&
2616 "expression is not a property reference");
2617 E = E->IgnoreParenCasts();
2618 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2619 if (BO->getOpcode() == BO_Comma) {
2620 E = BO->getRHS();
2621 continue;
2622 }
2623 }
2624
2625 break;
2626 }
2627
2628 return cast<ObjCPropertyRefExpr>(E);
2629}
2630
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002631FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002632 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002633
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002634 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002635 if (ICE->getCastKind() == CK_LValueToRValue ||
2636 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002637 E = ICE->getSubExpr()->IgnoreParens();
2638 else
2639 break;
2640 }
2641
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002642 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002643 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002644 if (Field->isBitField())
2645 return Field;
2646
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002647 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2648 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2649 if (Field->isBitField())
2650 return Field;
2651
Eli Friedman42068e92011-07-13 02:05:57 +00002652 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002653 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2654 return BinOp->getLHS()->getBitField();
2655
Eli Friedman42068e92011-07-13 02:05:57 +00002656 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2657 return BinOp->getRHS()->getBitField();
2658 }
2659
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002660 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002661}
2662
Anders Carlsson09380262010-01-31 17:18:49 +00002663bool Expr::refersToVectorElement() const {
2664 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002665
Anders Carlsson09380262010-01-31 17:18:49 +00002666 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002667 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002668 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002669 E = ICE->getSubExpr()->IgnoreParens();
2670 else
2671 break;
2672 }
Sean Huntc3021132010-05-05 15:23:54 +00002673
Anders Carlsson09380262010-01-31 17:18:49 +00002674 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2675 return ASE->getBase()->getType()->isVectorType();
2676
2677 if (isa<ExtVectorElementExpr>(E))
2678 return true;
2679
2680 return false;
2681}
2682
Chris Lattner2140e902009-02-16 22:14:05 +00002683/// isArrow - Return true if the base expression is a pointer to vector,
2684/// return false if the base expression is a vector.
2685bool ExtVectorElementExpr::isArrow() const {
2686 return getBase()->getType()->isPointerType();
2687}
2688
Nate Begeman213541a2008-04-18 23:10:10 +00002689unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002690 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002691 return VT->getNumElements();
2692 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002693}
2694
Nate Begeman8a997642008-05-09 06:41:27 +00002695/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002696bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002697 // FIXME: Refactor this code to an accessor on the AST node which returns the
2698 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002699 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002700
2701 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002702 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002703 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002704
Nate Begeman190d6a22009-01-18 02:01:21 +00002705 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002706 if (Comp[0] == 's' || Comp[0] == 'S')
2707 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002708
Daniel Dunbar15027422009-10-17 23:53:04 +00002709 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002710 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002711 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002712
Steve Narofffec0b492007-07-30 03:29:09 +00002713 return false;
2714}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002715
Nate Begeman8a997642008-05-09 06:41:27 +00002716/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002717void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002718 SmallVectorImpl<unsigned> &Elts) const {
2719 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002720 if (Comp[0] == 's' || Comp[0] == 'S')
2721 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002722
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002723 bool isHi = Comp == "hi";
2724 bool isLo = Comp == "lo";
2725 bool isEven = Comp == "even";
2726 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002727
Nate Begeman8a997642008-05-09 06:41:27 +00002728 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2729 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002730
Nate Begeman8a997642008-05-09 06:41:27 +00002731 if (isHi)
2732 Index = e + i;
2733 else if (isLo)
2734 Index = i;
2735 else if (isEven)
2736 Index = 2 * i;
2737 else if (isOdd)
2738 Index = 2 * i + 1;
2739 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002740 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002741
Nate Begeman3b8d1162008-05-13 21:03:02 +00002742 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002743 }
Nate Begeman8a997642008-05-09 06:41:27 +00002744}
2745
Douglas Gregor04badcf2010-04-21 00:45:42 +00002746ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002747 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002748 SourceLocation LBracLoc,
2749 SourceLocation SuperLoc,
2750 bool IsInstanceSuper,
2751 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002752 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002753 ArrayRef<SourceLocation> SelLocs,
2754 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002755 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002756 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002757 SourceLocation RBracLoc,
2758 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002759 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002760 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002761 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002762 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002763 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2764 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002765 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002766 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
2767 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002768{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002769 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002770 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002771}
2772
Douglas Gregor04badcf2010-04-21 00:45:42 +00002773ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002774 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002775 SourceLocation LBracLoc,
2776 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002777 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002778 ArrayRef<SourceLocation> SelLocs,
2779 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002780 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002781 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002782 SourceLocation RBracLoc,
2783 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002784 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002785 T->isDependentType(), T->isInstantiationDependentType(),
2786 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002787 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2788 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002789 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002790 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002791 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002792{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002793 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002794 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002795}
2796
Douglas Gregor04badcf2010-04-21 00:45:42 +00002797ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002798 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002799 SourceLocation LBracLoc,
2800 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002801 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002802 ArrayRef<SourceLocation> SelLocs,
2803 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002804 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002805 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002806 SourceLocation RBracLoc,
2807 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002808 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002809 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002810 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002811 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002812 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2813 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002814 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002815 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002816 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002817{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002818 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002819 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002820}
2821
2822void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2823 ArrayRef<SourceLocation> SelLocs,
2824 SelectorLocationsKind SelLocsK) {
2825 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00002826 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002827 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002828 if (Args[I]->isTypeDependent())
2829 ExprBits.TypeDependent = true;
2830 if (Args[I]->isValueDependent())
2831 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002832 if (Args[I]->isInstantiationDependent())
2833 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002834 if (Args[I]->containsUnexpandedParameterPack())
2835 ExprBits.ContainsUnexpandedParameterPack = true;
2836
2837 MyArgs[I] = Args[I];
2838 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002839
Benjamin Kramer19562c92012-02-20 00:20:48 +00002840 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00002841 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00002842 if (SelLocsK == SelLoc_NonStandard)
2843 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
2844 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002845}
2846
Douglas Gregor04badcf2010-04-21 00:45:42 +00002847ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002848 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002849 SourceLocation LBracLoc,
2850 SourceLocation SuperLoc,
2851 bool IsInstanceSuper,
2852 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002853 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002854 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002855 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002856 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002857 SourceLocation RBracLoc,
2858 bool isImplicit) {
2859 assert((!SelLocs.empty() || isImplicit) &&
2860 "No selector locs for non-implicit message");
2861 ObjCMessageExpr *Mem;
2862 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
2863 if (isImplicit)
2864 Mem = alloc(Context, Args.size(), 0);
2865 else
2866 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00002867 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002868 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002869 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002870}
2871
2872ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002873 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002874 SourceLocation LBracLoc,
2875 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002876 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002877 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002878 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002879 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002880 SourceLocation RBracLoc,
2881 bool isImplicit) {
2882 assert((!SelLocs.empty() || isImplicit) &&
2883 "No selector locs for non-implicit message");
2884 ObjCMessageExpr *Mem;
2885 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
2886 if (isImplicit)
2887 Mem = alloc(Context, Args.size(), 0);
2888 else
2889 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002890 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002891 SelLocs, SelLocsK, Method, Args, RBracLoc,
2892 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002893}
2894
2895ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002896 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002897 SourceLocation LBracLoc,
2898 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002899 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002900 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002901 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002902 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002903 SourceLocation RBracLoc,
2904 bool isImplicit) {
2905 assert((!SelLocs.empty() || isImplicit) &&
2906 "No selector locs for non-implicit message");
2907 ObjCMessageExpr *Mem;
2908 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
2909 if (isImplicit)
2910 Mem = alloc(Context, Args.size(), 0);
2911 else
2912 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002913 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002914 SelLocs, SelLocsK, Method, Args, RBracLoc,
2915 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002916}
2917
Sean Huntc3021132010-05-05 15:23:54 +00002918ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002919 unsigned NumArgs,
2920 unsigned NumStoredSelLocs) {
2921 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002922 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2923}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002924
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002925ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2926 ArrayRef<Expr *> Args,
2927 SourceLocation RBraceLoc,
2928 ArrayRef<SourceLocation> SelLocs,
2929 Selector Sel,
2930 SelectorLocationsKind &SelLocsK) {
2931 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
2932 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
2933 : 0;
2934 return alloc(C, Args.size(), NumStoredSelLocs);
2935}
2936
2937ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2938 unsigned NumArgs,
2939 unsigned NumStoredSelLocs) {
2940 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2941 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
2942 return (ObjCMessageExpr *)C.Allocate(Size,
2943 llvm::AlignOf<ObjCMessageExpr>::Alignment);
2944}
2945
2946void ObjCMessageExpr::getSelectorLocs(
2947 SmallVectorImpl<SourceLocation> &SelLocs) const {
2948 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
2949 SelLocs.push_back(getSelectorLoc(i));
2950}
2951
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002952SourceRange ObjCMessageExpr::getReceiverRange() const {
2953 switch (getReceiverKind()) {
2954 case Instance:
2955 return getInstanceReceiver()->getSourceRange();
2956
2957 case Class:
2958 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2959
2960 case SuperInstance:
2961 case SuperClass:
2962 return getSuperLoc();
2963 }
2964
David Blaikie30263482012-01-20 21:50:17 +00002965 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002966}
2967
Douglas Gregor04badcf2010-04-21 00:45:42 +00002968Selector ObjCMessageExpr::getSelector() const {
2969 if (HasMethod)
2970 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2971 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002972 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002973}
2974
2975ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2976 switch (getReceiverKind()) {
2977 case Instance:
2978 if (const ObjCObjectPointerType *Ptr
2979 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2980 return Ptr->getInterfaceDecl();
2981 break;
2982
2983 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002984 if (const ObjCObjectType *Ty
2985 = getClassReceiver()->getAs<ObjCObjectType>())
2986 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002987 break;
2988
2989 case SuperInstance:
2990 if (const ObjCObjectPointerType *Ptr
2991 = getSuperType()->getAs<ObjCObjectPointerType>())
2992 return Ptr->getInterfaceDecl();
2993 break;
2994
2995 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00002996 if (const ObjCObjectType *Iface
2997 = getSuperType()->getAs<ObjCObjectType>())
2998 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002999 break;
3000 }
3001
3002 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003003}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003004
Chris Lattner5f9e2722011-07-23 10:55:15 +00003005StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003006 switch (getBridgeKind()) {
3007 case OBC_Bridge:
3008 return "__bridge";
3009 case OBC_BridgeTransfer:
3010 return "__bridge_transfer";
3011 case OBC_BridgeRetained:
3012 return "__bridge_retained";
3013 }
David Blaikie30263482012-01-20 21:50:17 +00003014
3015 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003016}
3017
Jay Foad4ba2a172011-01-12 09:06:06 +00003018bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003019 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003020}
3021
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003022ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3023 QualType Type, SourceLocation BLoc,
3024 SourceLocation RP)
3025 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3026 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003027 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003028 Type->containsUnexpandedParameterPack()),
3029 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3030{
3031 SubExprs = new (C) Stmt*[nexpr];
3032 for (unsigned i = 0; i < nexpr; i++) {
3033 if (args[i]->isTypeDependent())
3034 ExprBits.TypeDependent = true;
3035 if (args[i]->isValueDependent())
3036 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003037 if (args[i]->isInstantiationDependent())
3038 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003039 if (args[i]->containsUnexpandedParameterPack())
3040 ExprBits.ContainsUnexpandedParameterPack = true;
3041
3042 SubExprs[i] = args[i];
3043 }
3044}
3045
Nate Begeman888376a2009-08-12 02:28:50 +00003046void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3047 unsigned NumExprs) {
3048 if (SubExprs) C.Deallocate(SubExprs);
3049
3050 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003051 this->NumExprs = NumExprs;
3052 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003053}
Nate Begeman888376a2009-08-12 02:28:50 +00003054
Peter Collingbournef111d932011-04-15 00:35:48 +00003055GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3056 SourceLocation GenericLoc, Expr *ControllingExpr,
3057 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3058 unsigned NumAssocs, SourceLocation DefaultLoc,
3059 SourceLocation RParenLoc,
3060 bool ContainsUnexpandedParameterPack,
3061 unsigned ResultIndex)
3062 : Expr(GenericSelectionExprClass,
3063 AssocExprs[ResultIndex]->getType(),
3064 AssocExprs[ResultIndex]->getValueKind(),
3065 AssocExprs[ResultIndex]->getObjectKind(),
3066 AssocExprs[ResultIndex]->isTypeDependent(),
3067 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003068 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003069 ContainsUnexpandedParameterPack),
3070 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3071 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3072 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3073 RParenLoc(RParenLoc) {
3074 SubExprs[CONTROLLING] = ControllingExpr;
3075 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3076 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3077}
3078
3079GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3080 SourceLocation GenericLoc, Expr *ControllingExpr,
3081 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3082 unsigned NumAssocs, SourceLocation DefaultLoc,
3083 SourceLocation RParenLoc,
3084 bool ContainsUnexpandedParameterPack)
3085 : Expr(GenericSelectionExprClass,
3086 Context.DependentTy,
3087 VK_RValue,
3088 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003089 /*isTypeDependent=*/true,
3090 /*isValueDependent=*/true,
3091 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003092 ContainsUnexpandedParameterPack),
3093 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3094 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3095 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3096 RParenLoc(RParenLoc) {
3097 SubExprs[CONTROLLING] = ControllingExpr;
3098 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3099 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3100}
3101
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003102//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003103// DesignatedInitExpr
3104//===----------------------------------------------------------------------===//
3105
Chandler Carruthb1138242011-06-16 06:47:06 +00003106IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003107 assert(Kind == FieldDesignator && "Only valid on a field designator");
3108 if (Field.NameOrField & 0x01)
3109 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3110 else
3111 return getField()->getIdentifier();
3112}
3113
Sean Huntc3021132010-05-05 15:23:54 +00003114DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003115 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003116 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003117 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003118 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003119 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003120 unsigned NumIndexExprs,
3121 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003122 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003123 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003124 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003125 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003126 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003127 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3128 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003129 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003130
3131 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003132 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003133 *Child++ = Init;
3134
3135 // Copy the designators and their subexpressions, computing
3136 // value-dependence along the way.
3137 unsigned IndexIdx = 0;
3138 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003139 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003140
3141 if (this->Designators[I].isArrayDesignator()) {
3142 // Compute type- and value-dependence.
3143 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003144 if (Index->isTypeDependent() || Index->isValueDependent())
3145 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003146 if (Index->isInstantiationDependent())
3147 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003148 // Propagate unexpanded parameter packs.
3149 if (Index->containsUnexpandedParameterPack())
3150 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003151
3152 // Copy the index expressions into permanent storage.
3153 *Child++ = IndexExprs[IndexIdx++];
3154 } else if (this->Designators[I].isArrayRangeDesignator()) {
3155 // Compute type- and value-dependence.
3156 Expr *Start = IndexExprs[IndexIdx];
3157 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003158 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003159 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003160 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003161 ExprBits.InstantiationDependent = true;
3162 } else if (Start->isInstantiationDependent() ||
3163 End->isInstantiationDependent()) {
3164 ExprBits.InstantiationDependent = true;
3165 }
3166
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003167 // Propagate unexpanded parameter packs.
3168 if (Start->containsUnexpandedParameterPack() ||
3169 End->containsUnexpandedParameterPack())
3170 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003171
3172 // Copy the start/end expressions into permanent storage.
3173 *Child++ = IndexExprs[IndexIdx++];
3174 *Child++ = IndexExprs[IndexIdx++];
3175 }
3176 }
3177
3178 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003179}
3180
Douglas Gregor05c13a32009-01-22 00:58:24 +00003181DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003182DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003183 unsigned NumDesignators,
3184 Expr **IndexExprs, unsigned NumIndexExprs,
3185 SourceLocation ColonOrEqualLoc,
3186 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003187 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003188 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003189 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003190 ColonOrEqualLoc, UsesColonSyntax,
3191 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003192}
3193
Mike Stump1eb44332009-09-09 15:08:12 +00003194DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003195 unsigned NumIndexExprs) {
3196 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3197 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3198 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3199}
3200
Douglas Gregor319d57f2010-01-06 23:17:19 +00003201void DesignatedInitExpr::setDesignators(ASTContext &C,
3202 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003203 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003204 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003205 NumDesignators = NumDesigs;
3206 for (unsigned I = 0; I != NumDesigs; ++I)
3207 Designators[I] = Desigs[I];
3208}
3209
Abramo Bagnara24f46742011-03-16 15:08:46 +00003210SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3211 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3212 if (size() == 1)
3213 return DIE->getDesignator(0)->getSourceRange();
3214 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3215 DIE->getDesignator(size()-1)->getEndLocation());
3216}
3217
Douglas Gregor05c13a32009-01-22 00:58:24 +00003218SourceRange DesignatedInitExpr::getSourceRange() const {
3219 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003220 Designator &First =
3221 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003222 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003223 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003224 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3225 else
3226 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3227 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003228 StartLoc =
3229 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003230 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3231}
3232
Douglas Gregor05c13a32009-01-22 00:58:24 +00003233Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3234 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3235 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3236 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003237 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3238 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3239}
3240
3241Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003242 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003243 "Requires array range designator");
3244 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3245 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003246 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3247 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3248}
3249
3250Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003251 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003252 "Requires array range designator");
3253 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3254 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003255 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3256 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3257}
3258
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003259/// \brief Replaces the designator at index @p Idx with the series
3260/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003261void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003262 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003263 const Designator *Last) {
3264 unsigned NumNewDesignators = Last - First;
3265 if (NumNewDesignators == 0) {
3266 std::copy_backward(Designators + Idx + 1,
3267 Designators + NumDesignators,
3268 Designators + Idx);
3269 --NumNewDesignators;
3270 return;
3271 } else if (NumNewDesignators == 1) {
3272 Designators[Idx] = *First;
3273 return;
3274 }
3275
Mike Stump1eb44332009-09-09 15:08:12 +00003276 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003277 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003278 std::copy(Designators, Designators + Idx, NewDesignators);
3279 std::copy(First, Last, NewDesignators + Idx);
3280 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3281 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003282 Designators = NewDesignators;
3283 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3284}
3285
Mike Stump1eb44332009-09-09 15:08:12 +00003286ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003287 Expr **exprs, unsigned nexprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003288 SourceLocation rparenloc)
3289 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003290 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003291 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00003292 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003293 for (unsigned i = 0; i != nexprs; ++i) {
3294 if (exprs[i]->isTypeDependent())
3295 ExprBits.TypeDependent = true;
3296 if (exprs[i]->isValueDependent())
3297 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003298 if (exprs[i]->isInstantiationDependent())
3299 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003300 if (exprs[i]->containsUnexpandedParameterPack())
3301 ExprBits.ContainsUnexpandedParameterPack = true;
3302
Nate Begeman2ef13e52009-08-10 23:49:36 +00003303 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003304 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003305}
3306
John McCalle996ffd2011-02-16 08:02:54 +00003307const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3308 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3309 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003310 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3311 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003312 e = cast<CXXConstructExpr>(e)->getArg(0);
3313 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3314 e = ice->getSubExpr();
3315 return cast<OpaqueValueExpr>(e);
3316}
3317
John McCall4b9c2d22011-11-06 09:01:30 +00003318PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3319 unsigned numSemanticExprs) {
3320 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3321 (1 + numSemanticExprs) * sizeof(Expr*),
3322 llvm::alignOf<PseudoObjectExpr>());
3323 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3324}
3325
3326PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3327 : Expr(PseudoObjectExprClass, shell) {
3328 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3329}
3330
3331PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3332 ArrayRef<Expr*> semantics,
3333 unsigned resultIndex) {
3334 assert(syntax && "no syntactic expression!");
3335 assert(semantics.size() && "no semantic expressions!");
3336
3337 QualType type;
3338 ExprValueKind VK;
3339 if (resultIndex == NoResult) {
3340 type = C.VoidTy;
3341 VK = VK_RValue;
3342 } else {
3343 assert(resultIndex < semantics.size());
3344 type = semantics[resultIndex]->getType();
3345 VK = semantics[resultIndex]->getValueKind();
3346 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3347 }
3348
3349 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3350 (1 + semantics.size()) * sizeof(Expr*),
3351 llvm::alignOf<PseudoObjectExpr>());
3352 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3353 resultIndex);
3354}
3355
3356PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3357 Expr *syntax, ArrayRef<Expr*> semantics,
3358 unsigned resultIndex)
3359 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3360 /*filled in at end of ctor*/ false, false, false, false) {
3361 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3362 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3363
3364 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3365 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3366 getSubExprsBuffer()[i] = E;
3367
3368 if (E->isTypeDependent())
3369 ExprBits.TypeDependent = true;
3370 if (E->isValueDependent())
3371 ExprBits.ValueDependent = true;
3372 if (E->isInstantiationDependent())
3373 ExprBits.InstantiationDependent = true;
3374 if (E->containsUnexpandedParameterPack())
3375 ExprBits.ContainsUnexpandedParameterPack = true;
3376
3377 if (isa<OpaqueValueExpr>(E))
3378 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3379 "opaque-value semantic expressions for pseudo-object "
3380 "operations must have sources");
3381 }
3382}
3383
Douglas Gregor05c13a32009-01-22 00:58:24 +00003384//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003385// ExprIterator.
3386//===----------------------------------------------------------------------===//
3387
3388Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3389Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3390Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3391const Expr* ConstExprIterator::operator[](size_t idx) const {
3392 return cast<Expr>(I[idx]);
3393}
3394const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3395const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3396
3397//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003398// Child Iterators for iterating over subexpressions/substatements
3399//===----------------------------------------------------------------------===//
3400
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003401// UnaryExprOrTypeTraitExpr
3402Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003403 // If this is of a type and the type is a VLA type (and not a typedef), the
3404 // size expression of the VLA needs to be treated as an executable expression.
3405 // Why isn't this weirdness documented better in StmtIterator?
3406 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003407 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003408 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003409 return child_range(child_iterator(T), child_iterator());
3410 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003411 }
John McCall63c00d72011-02-09 08:16:59 +00003412 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003413}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003414
Steve Naroff563477d2007-09-18 23:55:05 +00003415// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003416Stmt::child_range ObjCMessageExpr::children() {
3417 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003418 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003419 begin = reinterpret_cast<Stmt **>(this + 1);
3420 else
3421 begin = reinterpret_cast<Stmt **>(getArgs());
3422 return child_range(begin,
3423 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003424}
3425
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003426ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3427 QualType T, ObjCMethodDecl *Method,
3428 SourceRange SR)
3429 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3430 false, false, false, false),
3431 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3432{
3433 Expr **SaveElements = getElements();
3434 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3435 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3436 ExprBits.ValueDependent = true;
3437 if (Elements[I]->isInstantiationDependent())
3438 ExprBits.InstantiationDependent = true;
3439 if (Elements[I]->containsUnexpandedParameterPack())
3440 ExprBits.ContainsUnexpandedParameterPack = true;
3441
3442 SaveElements[I] = Elements[I];
3443 }
3444}
3445
3446ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3447 llvm::ArrayRef<Expr *> Elements,
3448 QualType T, ObjCMethodDecl * Method,
3449 SourceRange SR) {
3450 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3451 + Elements.size() * sizeof(Expr *));
3452 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3453}
3454
3455ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3456 unsigned NumElements) {
3457
3458 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3459 + NumElements * sizeof(Expr *));
3460 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3461}
3462
3463ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3464 ArrayRef<ObjCDictionaryElement> VK,
3465 bool HasPackExpansions,
3466 QualType T, ObjCMethodDecl *method,
3467 SourceRange SR)
3468 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3469 false, false),
3470 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3471 DictWithObjectsMethod(method)
3472{
3473 KeyValuePair *KeyValues = getKeyValues();
3474 ExpansionData *Expansions = getExpansionData();
3475 for (unsigned I = 0; I < NumElements; I++) {
3476 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3477 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3478 ExprBits.ValueDependent = true;
3479 if (VK[I].Key->isInstantiationDependent() ||
3480 VK[I].Value->isInstantiationDependent())
3481 ExprBits.InstantiationDependent = true;
3482 if (VK[I].EllipsisLoc.isInvalid() &&
3483 (VK[I].Key->containsUnexpandedParameterPack() ||
3484 VK[I].Value->containsUnexpandedParameterPack()))
3485 ExprBits.ContainsUnexpandedParameterPack = true;
3486
3487 KeyValues[I].Key = VK[I].Key;
3488 KeyValues[I].Value = VK[I].Value;
3489 if (Expansions) {
3490 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3491 if (VK[I].NumExpansions)
3492 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3493 else
3494 Expansions[I].NumExpansionsPlusOne = 0;
3495 }
3496 }
3497}
3498
3499ObjCDictionaryLiteral *
3500ObjCDictionaryLiteral::Create(ASTContext &C,
3501 ArrayRef<ObjCDictionaryElement> VK,
3502 bool HasPackExpansions,
3503 QualType T, ObjCMethodDecl *method,
3504 SourceRange SR) {
3505 unsigned ExpansionsSize = 0;
3506 if (HasPackExpansions)
3507 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3508
3509 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3510 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3511 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3512}
3513
3514ObjCDictionaryLiteral *
3515ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3516 bool HasPackExpansions) {
3517 unsigned ExpansionsSize = 0;
3518 if (HasPackExpansions)
3519 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3520 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3521 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3522 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3523 HasPackExpansions);
3524}
3525
3526ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
3527 Expr *base,
3528 Expr *key, QualType T,
3529 ObjCMethodDecl *getMethod,
3530 ObjCMethodDecl *setMethod,
3531 SourceLocation RB) {
3532 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
3533 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
3534 OK_ObjCSubscript,
3535 getMethod, setMethod, RB);
3536}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003537
3538AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3539 QualType t, AtomicOp op, SourceLocation RP)
3540 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3541 false, false, false, false),
3542 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3543{
Richard Smithe1b2abc2012-04-10 22:49:28 +00003544 assert(nexpr == getNumSubExprs(op) && "wrong number of subexpressions");
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003545 for (unsigned i = 0; i < nexpr; i++) {
3546 if (args[i]->isTypeDependent())
3547 ExprBits.TypeDependent = true;
3548 if (args[i]->isValueDependent())
3549 ExprBits.ValueDependent = true;
3550 if (args[i]->isInstantiationDependent())
3551 ExprBits.InstantiationDependent = true;
3552 if (args[i]->containsUnexpandedParameterPack())
3553 ExprBits.ContainsUnexpandedParameterPack = true;
3554
3555 SubExprs[i] = args[i];
3556 }
3557}
Richard Smithe1b2abc2012-04-10 22:49:28 +00003558
3559unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
3560 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00003561 case AO__c11_atomic_init:
3562 case AO__c11_atomic_load:
3563 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003564 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00003565
3566 case AO__c11_atomic_store:
3567 case AO__c11_atomic_exchange:
3568 case AO__atomic_load:
3569 case AO__atomic_store:
3570 case AO__atomic_store_n:
3571 case AO__atomic_exchange_n:
3572 case AO__c11_atomic_fetch_add:
3573 case AO__c11_atomic_fetch_sub:
3574 case AO__c11_atomic_fetch_and:
3575 case AO__c11_atomic_fetch_or:
3576 case AO__c11_atomic_fetch_xor:
3577 case AO__atomic_fetch_add:
3578 case AO__atomic_fetch_sub:
3579 case AO__atomic_fetch_and:
3580 case AO__atomic_fetch_or:
3581 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00003582 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00003583 case AO__atomic_add_fetch:
3584 case AO__atomic_sub_fetch:
3585 case AO__atomic_and_fetch:
3586 case AO__atomic_or_fetch:
3587 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00003588 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003589 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00003590
3591 case AO__atomic_exchange:
3592 return 4;
3593
3594 case AO__c11_atomic_compare_exchange_strong:
3595 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003596 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00003597
3598 case AO__atomic_compare_exchange:
3599 case AO__atomic_compare_exchange_n:
3600 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00003601 }
3602 llvm_unreachable("unknown atomic op");
3603}