blob: 111f3384a8be87abc93176ba0c841b262d19486f [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
705 // Create a langops struct and enable trigraphs. This is sufficient for
706 // relexing tokens.
707 LangOptions LangOpts;
708 LangOpts.Trigraphs = true;
709
710 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +0000711 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
712 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +0000713 Token TheTok;
714 TheLexer.LexFromRawLexer(TheTok);
715
716 // Use the StringLiteralParser to compute the length of the string in bytes.
717 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
718 unsigned TokNumBytes = SLP.GetStringLength();
719
720 // If the byte is in this token, return the location of the byte.
721 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000722 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000723 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
724
725 // Now that we know the offset of the token in the spelling, use the
726 // preprocessor to get the offset in the original source.
727 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
728 }
729
730 // Move to the next string token.
731 ++TokNo;
732 ByteNo -= TokNumBytes;
733 }
734}
735
736
737
Reid Spencer5f016e22007-07-11 17:01:13 +0000738/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
739/// corresponds to, e.g. "sizeof" or "[pre]++".
740const char *UnaryOperator::getOpcodeStr(Opcode Op) {
741 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000742 case UO_PostInc: return "++";
743 case UO_PostDec: return "--";
744 case UO_PreInc: return "++";
745 case UO_PreDec: return "--";
746 case UO_AddrOf: return "&";
747 case UO_Deref: return "*";
748 case UO_Plus: return "+";
749 case UO_Minus: return "-";
750 case UO_Not: return "~";
751 case UO_LNot: return "!";
752 case UO_Real: return "__real";
753 case UO_Imag: return "__imag";
754 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000756 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000757}
758
John McCall2de56d12010-08-25 11:45:40 +0000759UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000760UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
761 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000762 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000763 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
764 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
765 case OO_Amp: return UO_AddrOf;
766 case OO_Star: return UO_Deref;
767 case OO_Plus: return UO_Plus;
768 case OO_Minus: return UO_Minus;
769 case OO_Tilde: return UO_Not;
770 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000771 }
772}
773
774OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
775 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000776 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
777 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
778 case UO_AddrOf: return OO_Amp;
779 case UO_Deref: return OO_Star;
780 case UO_Plus: return OO_Plus;
781 case UO_Minus: return OO_Minus;
782 case UO_Not: return OO_Tilde;
783 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000784 default: return OO_None;
785 }
786}
787
788
Reid Spencer5f016e22007-07-11 17:01:13 +0000789//===----------------------------------------------------------------------===//
790// Postfix Operators.
791//===----------------------------------------------------------------------===//
792
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000793CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
794 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000795 SourceLocation rparenloc)
796 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000797 fn->isTypeDependent(),
798 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000799 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000800 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000801 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000802
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000803 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000804 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000805 for (unsigned i = 0; i != numargs; ++i) {
806 if (args[i]->isTypeDependent())
807 ExprBits.TypeDependent = true;
808 if (args[i]->isValueDependent())
809 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000810 if (args[i]->isInstantiationDependent())
811 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000812 if (args[i]->containsUnexpandedParameterPack())
813 ExprBits.ContainsUnexpandedParameterPack = true;
814
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000815 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000816 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000817
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000818 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000819 RParenLoc = rparenloc;
820}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000821
Ted Kremenek668bf912009-02-09 20:51:47 +0000822CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000823 QualType t, ExprValueKind VK, SourceLocation rparenloc)
824 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000825 fn->isTypeDependent(),
826 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000827 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000828 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000829 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000830
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000831 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000832 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000833 for (unsigned i = 0; i != numargs; ++i) {
834 if (args[i]->isTypeDependent())
835 ExprBits.TypeDependent = true;
836 if (args[i]->isValueDependent())
837 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000838 if (args[i]->isInstantiationDependent())
839 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000840 if (args[i]->containsUnexpandedParameterPack())
841 ExprBits.ContainsUnexpandedParameterPack = true;
842
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000843 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000844 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000845
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000846 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000847 RParenLoc = rparenloc;
848}
849
Mike Stump1eb44332009-09-09 15:08:12 +0000850CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
851 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000852 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000853 SubExprs = new (C) Stmt*[PREARGS_START];
854 CallExprBits.NumPreArgs = 0;
855}
856
857CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
858 EmptyShell Empty)
859 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
860 // FIXME: Why do we allocate this?
861 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
862 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000863}
864
Nuno Lopesd20254f2009-12-20 23:11:08 +0000865Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000866 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000867
868 while (SubstNonTypeTemplateParmExpr *NTTP
869 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
870 CEE = NTTP->getReplacement()->IgnoreParenCasts();
871 }
872
Sebastian Redl20012152010-09-10 20:55:30 +0000873 // If we're calling a dereference, look at the pointer instead.
874 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
875 if (BO->isPtrMemOp())
876 CEE = BO->getRHS()->IgnoreParenCasts();
877 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
878 if (UO->getOpcode() == UO_Deref)
879 CEE = UO->getSubExpr()->IgnoreParenCasts();
880 }
Chris Lattner6346f962009-07-17 15:46:27 +0000881 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000882 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000883 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
884 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000885
886 return 0;
887}
888
Nuno Lopesd20254f2009-12-20 23:11:08 +0000889FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000890 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000891}
892
Chris Lattnerd18b3292007-12-28 05:25:02 +0000893/// setNumArgs - This changes the number of arguments present in this call.
894/// Any orphaned expressions are deleted by this, and any new operands are set
895/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000896void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000897 // No change, just return.
898 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000899
Chris Lattnerd18b3292007-12-28 05:25:02 +0000900 // If shrinking # arguments, just delete the extras and forgot them.
901 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000902 this->NumArgs = NumArgs;
903 return;
904 }
905
906 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000907 unsigned NumPreArgs = getNumPreArgs();
908 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000909 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000910 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000911 NewSubExprs[i] = SubExprs[i];
912 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000913 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
914 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000915 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Douglas Gregor88c9a462009-04-17 21:46:47 +0000917 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000918 SubExprs = NewSubExprs;
919 this->NumArgs = NumArgs;
920}
921
Chris Lattnercb888962008-10-06 05:00:53 +0000922/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
923/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +0000924unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000925 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000926 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000927 // ImplicitCastExpr.
928 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
929 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000930 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000932 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
933 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000934 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Anders Carlssonbcba2012008-01-31 02:13:57 +0000936 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
937 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000938 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000940 if (!FDecl->getIdentifier())
941 return 0;
942
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000943 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000944}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000945
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000946QualType CallExpr::getCallReturnType() const {
947 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000948 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000949 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000950 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000951 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000952 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
953 // This should never be overloaded and so should never return null.
954 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000955
John McCall864c0412011-04-26 20:42:42 +0000956 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000957 return FnType->getResultType();
958}
Chris Lattnercb888962008-10-06 05:00:53 +0000959
John McCall2882eca2011-02-21 06:23:05 +0000960SourceRange CallExpr::getSourceRange() const {
961 if (isa<CXXOperatorCallExpr>(this))
962 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
963
964 SourceLocation begin = getCallee()->getLocStart();
965 if (begin.isInvalid() && getNumArgs() > 0)
966 begin = getArg(0)->getLocStart();
967 SourceLocation end = getRParenLoc();
968 if (end.isInvalid() && getNumArgs() > 0)
969 end = getArg(getNumArgs() - 1)->getLocEnd();
970 return SourceRange(begin, end);
971}
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +0000972SourceLocation CallExpr::getLocStart() const {
973 if (isa<CXXOperatorCallExpr>(this))
974 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getBegin();
975
976 SourceLocation begin = getCallee()->getLocStart();
977 if (begin.isInvalid() && getNumArgs() > 0)
978 begin = getArg(0)->getLocStart();
979 return begin;
980}
981SourceLocation CallExpr::getLocEnd() const {
982 if (isa<CXXOperatorCallExpr>(this))
983 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getEnd();
984
985 SourceLocation end = getRParenLoc();
986 if (end.isInvalid() && getNumArgs() > 0)
987 end = getArg(getNumArgs() - 1)->getLocEnd();
988 return end;
989}
John McCall2882eca2011-02-21 06:23:05 +0000990
Sean Huntc3021132010-05-05 15:23:54 +0000991OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000992 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000993 TypeSourceInfo *tsi,
994 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000995 Expr** exprsPtr, unsigned numExprs,
996 SourceLocation RParenLoc) {
997 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000998 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000999 sizeof(Expr*) * numExprs);
1000
1001 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
1002 exprsPtr, numExprs, RParenLoc);
1003}
1004
1005OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
1006 unsigned numComps, unsigned numExprs) {
1007 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1008 sizeof(OffsetOfNode) * numComps +
1009 sizeof(Expr*) * numExprs);
1010 return new (Mem) OffsetOfExpr(numComps, numExprs);
1011}
1012
Sean Huntc3021132010-05-05 15:23:54 +00001013OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001014 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +00001015 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001016 Expr** exprsPtr, unsigned numExprs,
1017 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001018 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1019 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001020 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001021 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001022 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001023 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
1024 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001025{
1026 for(unsigned i = 0; i < numComps; ++i) {
1027 setComponent(i, compsPtr[i]);
1028 }
Sean Huntc3021132010-05-05 15:23:54 +00001029
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001030 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001031 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
1032 ExprBits.ValueDependent = true;
1033 if (exprsPtr[i]->containsUnexpandedParameterPack())
1034 ExprBits.ContainsUnexpandedParameterPack = true;
1035
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001036 setIndexExpr(i, exprsPtr[i]);
1037 }
1038}
1039
1040IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1041 assert(getKind() == Field || getKind() == Identifier);
1042 if (getKind() == Field)
1043 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001044
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001045 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1046}
1047
Mike Stump1eb44332009-09-09 15:08:12 +00001048MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001049 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001050 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001051 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001052 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001053 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001054 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001055 QualType ty,
1056 ExprValueKind vk,
1057 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001058 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001059
Douglas Gregor40d96a62011-02-28 21:54:11 +00001060 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001061 founddecl.getDecl() != memberdecl ||
1062 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001063 if (hasQualOrFound)
1064 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001065
John McCalld5532b62009-11-23 01:53:49 +00001066 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001067 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1068 else if (TemplateKWLoc.isValid())
1069 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Chris Lattner32488542010-10-30 05:14:06 +00001071 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001072 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1073 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001074
1075 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001076 // FIXME: Wrong. We should be looking at the member declaration we found.
1077 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001078 E->setValueDependent(true);
1079 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001080 E->setInstantiationDependent(true);
1081 }
1082 else if (QualifierLoc &&
1083 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1084 E->setInstantiationDependent(true);
1085
John McCall6bb80172010-03-30 21:47:33 +00001086 E->HasQualifierOrFoundDecl = true;
1087
1088 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001089 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001090 NQ->FoundDecl = founddecl;
1091 }
1092
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001093 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1094
John McCall6bb80172010-03-30 21:47:33 +00001095 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001096 bool Dependent = false;
1097 bool InstantiationDependent = false;
1098 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001099 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1100 Dependent,
1101 InstantiationDependent,
1102 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001103 if (InstantiationDependent)
1104 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001105 } else if (TemplateKWLoc.isValid()) {
1106 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001107 }
1108
1109 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001110}
1111
Douglas Gregor75e85042011-03-02 21:06:53 +00001112SourceRange MemberExpr::getSourceRange() const {
Daniel Dunbar396ec672012-03-09 15:39:15 +00001113 return SourceRange(getLocStart(), getLocEnd());
1114}
1115SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001116 if (isImplicitAccess()) {
1117 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001118 return getQualifierLoc().getBeginLoc();
1119 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001120 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001121
Daniel Dunbar396ec672012-03-09 15:39:15 +00001122 // FIXME: We don't want this to happen. Rather, we should be able to
1123 // detect all kinds of implicit accesses more cleanly.
1124 SourceLocation BaseStartLoc = getBase()->getLocStart();
1125 if (BaseStartLoc.isValid())
1126 return BaseStartLoc;
1127 return MemberLoc;
1128}
1129SourceLocation MemberExpr::getLocEnd() const {
1130 if (hasExplicitTemplateArgs())
1131 return getRAngleLoc();
1132 return getMemberNameInfo().getEndLoc();
Douglas Gregor75e85042011-03-02 21:06:53 +00001133}
1134
John McCall1d9b3b22011-09-09 05:25:32 +00001135void CastExpr::CheckCastConsistency() const {
1136 switch (getCastKind()) {
1137 case CK_DerivedToBase:
1138 case CK_UncheckedDerivedToBase:
1139 case CK_DerivedToBaseMemberPointer:
1140 case CK_BaseToDerived:
1141 case CK_BaseToDerivedMemberPointer:
1142 assert(!path_empty() && "Cast kind should have a base path!");
1143 break;
1144
1145 case CK_CPointerToObjCPointerCast:
1146 assert(getType()->isObjCObjectPointerType());
1147 assert(getSubExpr()->getType()->isPointerType());
1148 goto CheckNoBasePath;
1149
1150 case CK_BlockPointerToObjCPointerCast:
1151 assert(getType()->isObjCObjectPointerType());
1152 assert(getSubExpr()->getType()->isBlockPointerType());
1153 goto CheckNoBasePath;
1154
John McCall4d4e5c12012-02-15 01:22:51 +00001155 case CK_ReinterpretMemberPointer:
1156 assert(getType()->isMemberPointerType());
1157 assert(getSubExpr()->getType()->isMemberPointerType());
1158 goto CheckNoBasePath;
1159
John McCall1d9b3b22011-09-09 05:25:32 +00001160 case CK_BitCast:
1161 // Arbitrary casts to C pointer types count as bitcasts.
1162 // Otherwise, we should only have block and ObjC pointer casts
1163 // here if they stay within the type kind.
1164 if (!getType()->isPointerType()) {
1165 assert(getType()->isObjCObjectPointerType() ==
1166 getSubExpr()->getType()->isObjCObjectPointerType());
1167 assert(getType()->isBlockPointerType() ==
1168 getSubExpr()->getType()->isBlockPointerType());
1169 }
1170 goto CheckNoBasePath;
1171
1172 case CK_AnyPointerToBlockPointerCast:
1173 assert(getType()->isBlockPointerType());
1174 assert(getSubExpr()->getType()->isAnyPointerType() &&
1175 !getSubExpr()->getType()->isBlockPointerType());
1176 goto CheckNoBasePath;
1177
Douglas Gregorac1303e2012-02-22 05:02:47 +00001178 case CK_CopyAndAutoreleaseBlockObject:
1179 assert(getType()->isBlockPointerType());
1180 assert(getSubExpr()->getType()->isBlockPointerType());
1181 goto CheckNoBasePath;
1182
John McCall1d9b3b22011-09-09 05:25:32 +00001183 // These should not have an inheritance path.
1184 case CK_Dynamic:
1185 case CK_ToUnion:
1186 case CK_ArrayToPointerDecay:
1187 case CK_FunctionToPointerDecay:
1188 case CK_NullToMemberPointer:
1189 case CK_NullToPointer:
1190 case CK_ConstructorConversion:
1191 case CK_IntegralToPointer:
1192 case CK_PointerToIntegral:
1193 case CK_ToVoid:
1194 case CK_VectorSplat:
1195 case CK_IntegralCast:
1196 case CK_IntegralToFloating:
1197 case CK_FloatingToIntegral:
1198 case CK_FloatingCast:
1199 case CK_ObjCObjectLValueCast:
1200 case CK_FloatingRealToComplex:
1201 case CK_FloatingComplexToReal:
1202 case CK_FloatingComplexCast:
1203 case CK_FloatingComplexToIntegralComplex:
1204 case CK_IntegralRealToComplex:
1205 case CK_IntegralComplexToReal:
1206 case CK_IntegralComplexCast:
1207 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001208 case CK_ARCProduceObject:
1209 case CK_ARCConsumeObject:
1210 case CK_ARCReclaimReturnedObject:
1211 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001212 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1213 goto CheckNoBasePath;
1214
1215 case CK_Dependent:
1216 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001217 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001218 case CK_AtomicToNonAtomic:
1219 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001220 case CK_PointerToBoolean:
1221 case CK_IntegralToBoolean:
1222 case CK_FloatingToBoolean:
1223 case CK_MemberPointerToBoolean:
1224 case CK_FloatingComplexToBoolean:
1225 case CK_IntegralComplexToBoolean:
1226 case CK_LValueBitCast: // -> bool&
1227 case CK_UserDefinedConversion: // operator bool()
1228 CheckNoBasePath:
1229 assert(path_empty() && "Cast kind should not have a base path!");
1230 break;
1231 }
1232}
1233
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001234const char *CastExpr::getCastKindName() const {
1235 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001236 case CK_Dependent:
1237 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001238 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001239 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001240 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001241 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001242 case CK_LValueToRValue:
1243 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001244 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001245 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001246 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001247 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001248 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001249 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001250 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001251 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001252 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001253 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001254 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001255 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001256 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001257 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001258 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001259 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001260 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001261 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001262 case CK_NullToPointer:
1263 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001264 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001265 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001266 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001267 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001268 case CK_ReinterpretMemberPointer:
1269 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001270 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001271 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001272 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001273 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001274 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001275 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001276 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001277 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001278 case CK_PointerToBoolean:
1279 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001280 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001281 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001282 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001283 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001284 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001285 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001286 case CK_IntegralToBoolean:
1287 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001288 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001289 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001290 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001291 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001292 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001293 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001294 case CK_FloatingToBoolean:
1295 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001296 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001297 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001298 case CK_CPointerToObjCPointerCast:
1299 return "CPointerToObjCPointerCast";
1300 case CK_BlockPointerToObjCPointerCast:
1301 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001302 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001303 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001304 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001305 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001306 case CK_FloatingRealToComplex:
1307 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001308 case CK_FloatingComplexToReal:
1309 return "FloatingComplexToReal";
1310 case CK_FloatingComplexToBoolean:
1311 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001312 case CK_FloatingComplexCast:
1313 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001314 case CK_FloatingComplexToIntegralComplex:
1315 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001316 case CK_IntegralRealToComplex:
1317 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001318 case CK_IntegralComplexToReal:
1319 return "IntegralComplexToReal";
1320 case CK_IntegralComplexToBoolean:
1321 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001322 case CK_IntegralComplexCast:
1323 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001324 case CK_IntegralComplexToFloatingComplex:
1325 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001326 case CK_ARCConsumeObject:
1327 return "ARCConsumeObject";
1328 case CK_ARCProduceObject:
1329 return "ARCProduceObject";
1330 case CK_ARCReclaimReturnedObject:
1331 return "ARCReclaimReturnedObject";
1332 case CK_ARCExtendBlockObject:
1333 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001334 case CK_AtomicToNonAtomic:
1335 return "AtomicToNonAtomic";
1336 case CK_NonAtomicToAtomic:
1337 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001338 case CK_CopyAndAutoreleaseBlockObject:
1339 return "CopyAndAutoreleaseBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001340 }
Mike Stump1eb44332009-09-09 15:08:12 +00001341
John McCall2bb5d002010-11-13 09:02:35 +00001342 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001343}
1344
Douglas Gregor6eef5192009-12-14 19:27:10 +00001345Expr *CastExpr::getSubExprAsWritten() {
1346 Expr *SubExpr = 0;
1347 CastExpr *E = this;
1348 do {
1349 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001350
1351 // Skip through reference binding to temporary.
1352 if (MaterializeTemporaryExpr *Materialize
1353 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1354 SubExpr = Materialize->GetTemporaryExpr();
1355
Douglas Gregor6eef5192009-12-14 19:27:10 +00001356 // Skip any temporary bindings; they're implicit.
1357 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1358 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001359
Douglas Gregor6eef5192009-12-14 19:27:10 +00001360 // Conversions by constructor and conversion functions have a
1361 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001362 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001363 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001364 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001365 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001366
Douglas Gregor6eef5192009-12-14 19:27:10 +00001367 // If the subexpression we're left with is an implicit cast, look
1368 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001369 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1370
Douglas Gregor6eef5192009-12-14 19:27:10 +00001371 return SubExpr;
1372}
1373
John McCallf871d0c2010-08-07 06:22:56 +00001374CXXBaseSpecifier **CastExpr::path_buffer() {
1375 switch (getStmtClass()) {
1376#define ABSTRACT_STMT(x)
1377#define CASTEXPR(Type, Base) \
1378 case Stmt::Type##Class: \
1379 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1380#define STMT(Type, Base)
1381#include "clang/AST/StmtNodes.inc"
1382 default:
1383 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001384 }
1385}
1386
1387void CastExpr::setCastPath(const CXXCastPath &Path) {
1388 assert(Path.size() == path_size());
1389 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1390}
1391
1392ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1393 CastKind Kind, Expr *Operand,
1394 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001395 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001396 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1397 void *Buffer =
1398 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1399 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001400 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001401 if (PathSize) E->setCastPath(*BasePath);
1402 return E;
1403}
1404
1405ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1406 unsigned PathSize) {
1407 void *Buffer =
1408 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1409 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1410}
1411
1412
1413CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001414 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001415 const CXXCastPath *BasePath,
1416 TypeSourceInfo *WrittenTy,
1417 SourceLocation L, SourceLocation R) {
1418 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1419 void *Buffer =
1420 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1421 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001422 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001423 if (PathSize) E->setCastPath(*BasePath);
1424 return E;
1425}
1426
1427CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1428 void *Buffer =
1429 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1430 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1431}
1432
Reid Spencer5f016e22007-07-11 17:01:13 +00001433/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1434/// corresponds to, e.g. "<<=".
1435const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1436 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001437 case BO_PtrMemD: return ".*";
1438 case BO_PtrMemI: return "->*";
1439 case BO_Mul: return "*";
1440 case BO_Div: return "/";
1441 case BO_Rem: return "%";
1442 case BO_Add: return "+";
1443 case BO_Sub: return "-";
1444 case BO_Shl: return "<<";
1445 case BO_Shr: return ">>";
1446 case BO_LT: return "<";
1447 case BO_GT: return ">";
1448 case BO_LE: return "<=";
1449 case BO_GE: return ">=";
1450 case BO_EQ: return "==";
1451 case BO_NE: return "!=";
1452 case BO_And: return "&";
1453 case BO_Xor: return "^";
1454 case BO_Or: return "|";
1455 case BO_LAnd: return "&&";
1456 case BO_LOr: return "||";
1457 case BO_Assign: return "=";
1458 case BO_MulAssign: return "*=";
1459 case BO_DivAssign: return "/=";
1460 case BO_RemAssign: return "%=";
1461 case BO_AddAssign: return "+=";
1462 case BO_SubAssign: return "-=";
1463 case BO_ShlAssign: return "<<=";
1464 case BO_ShrAssign: return ">>=";
1465 case BO_AndAssign: return "&=";
1466 case BO_XorAssign: return "^=";
1467 case BO_OrAssign: return "|=";
1468 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001469 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001470
David Blaikie30263482012-01-20 21:50:17 +00001471 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001472}
1473
John McCall2de56d12010-08-25 11:45:40 +00001474BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001475BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1476 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001477 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001478 case OO_Plus: return BO_Add;
1479 case OO_Minus: return BO_Sub;
1480 case OO_Star: return BO_Mul;
1481 case OO_Slash: return BO_Div;
1482 case OO_Percent: return BO_Rem;
1483 case OO_Caret: return BO_Xor;
1484 case OO_Amp: return BO_And;
1485 case OO_Pipe: return BO_Or;
1486 case OO_Equal: return BO_Assign;
1487 case OO_Less: return BO_LT;
1488 case OO_Greater: return BO_GT;
1489 case OO_PlusEqual: return BO_AddAssign;
1490 case OO_MinusEqual: return BO_SubAssign;
1491 case OO_StarEqual: return BO_MulAssign;
1492 case OO_SlashEqual: return BO_DivAssign;
1493 case OO_PercentEqual: return BO_RemAssign;
1494 case OO_CaretEqual: return BO_XorAssign;
1495 case OO_AmpEqual: return BO_AndAssign;
1496 case OO_PipeEqual: return BO_OrAssign;
1497 case OO_LessLess: return BO_Shl;
1498 case OO_GreaterGreater: return BO_Shr;
1499 case OO_LessLessEqual: return BO_ShlAssign;
1500 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1501 case OO_EqualEqual: return BO_EQ;
1502 case OO_ExclaimEqual: return BO_NE;
1503 case OO_LessEqual: return BO_LE;
1504 case OO_GreaterEqual: return BO_GE;
1505 case OO_AmpAmp: return BO_LAnd;
1506 case OO_PipePipe: return BO_LOr;
1507 case OO_Comma: return BO_Comma;
1508 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001509 }
1510}
1511
1512OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1513 static const OverloadedOperatorKind OverOps[] = {
1514 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1515 OO_Star, OO_Slash, OO_Percent,
1516 OO_Plus, OO_Minus,
1517 OO_LessLess, OO_GreaterGreater,
1518 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1519 OO_EqualEqual, OO_ExclaimEqual,
1520 OO_Amp,
1521 OO_Caret,
1522 OO_Pipe,
1523 OO_AmpAmp,
1524 OO_PipePipe,
1525 OO_Equal, OO_StarEqual,
1526 OO_SlashEqual, OO_PercentEqual,
1527 OO_PlusEqual, OO_MinusEqual,
1528 OO_LessLessEqual, OO_GreaterGreaterEqual,
1529 OO_AmpEqual, OO_CaretEqual,
1530 OO_PipeEqual,
1531 OO_Comma
1532 };
1533 return OverOps[Opc];
1534}
1535
Ted Kremenek709210f2010-04-13 23:39:13 +00001536InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001537 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001538 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001539 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001540 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001541 InitExprs(C, numInits),
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001542 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0)
1543{
1544 sawArrayRangeDesignator(false);
1545 setInitializesStdInitializerList(false);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001546 for (unsigned I = 0; I != numInits; ++I) {
1547 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001548 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001549 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001550 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001551 if (initExprs[I]->isInstantiationDependent())
1552 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001553 if (initExprs[I]->containsUnexpandedParameterPack())
1554 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001555 }
Sean Huntc3021132010-05-05 15:23:54 +00001556
Ted Kremenek709210f2010-04-13 23:39:13 +00001557 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001558}
Reid Spencer5f016e22007-07-11 17:01:13 +00001559
Ted Kremenek709210f2010-04-13 23:39:13 +00001560void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001561 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001562 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001563}
1564
Ted Kremenek709210f2010-04-13 23:39:13 +00001565void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001566 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001567}
1568
Ted Kremenek709210f2010-04-13 23:39:13 +00001569Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001570 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001571 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001572 InitExprs.back() = expr;
1573 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001574 }
Mike Stump1eb44332009-09-09 15:08:12 +00001575
Douglas Gregor4c678342009-01-28 21:54:33 +00001576 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1577 InitExprs[Init] = expr;
1578 return Result;
1579}
1580
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001581void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001582 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001583 ArrayFillerOrUnionFieldInit = filler;
1584 // Fill out any "holes" in the array due to designated initializers.
1585 Expr **inits = getInits();
1586 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1587 if (inits[i] == 0)
1588 inits[i] = filler;
1589}
1590
Richard Smithfe587202012-04-15 02:50:59 +00001591bool InitListExpr::isStringLiteralInit() const {
1592 if (getNumInits() != 1)
1593 return false;
1594 const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(getType());
1595 if (!CAT || !CAT->getElementType()->isIntegerType())
1596 return false;
1597 const Expr *Init = getInit(0)->IgnoreParenImpCasts();
1598 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1599}
1600
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001601SourceRange InitListExpr::getSourceRange() const {
1602 if (SyntacticForm)
1603 return SyntacticForm->getSourceRange();
1604 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1605 if (Beg.isInvalid()) {
1606 // Find the first non-null initializer.
1607 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1608 E = InitExprs.end();
1609 I != E; ++I) {
1610 if (Stmt *S = *I) {
1611 Beg = S->getLocStart();
1612 break;
1613 }
1614 }
1615 }
1616 if (End.isInvalid()) {
1617 // Find the first non-null initializer from the end.
1618 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1619 E = InitExprs.rend();
1620 I != E; ++I) {
1621 if (Stmt *S = *I) {
1622 End = S->getSourceRange().getEnd();
1623 break;
1624 }
1625 }
1626 }
1627 return SourceRange(Beg, End);
1628}
1629
Steve Naroffbfdcae62008-09-04 15:31:07 +00001630/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001631///
John McCalla345edb2012-02-17 03:32:35 +00001632const FunctionProtoType *BlockExpr::getFunctionType() const {
1633 // The block pointer is never sugared, but the function type might be.
1634 return cast<BlockPointerType>(getType())
1635 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001636}
1637
Mike Stump1eb44332009-09-09 15:08:12 +00001638SourceLocation BlockExpr::getCaretLocation() const {
1639 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001640}
Mike Stump1eb44332009-09-09 15:08:12 +00001641const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001642 return TheBlock->getBody();
1643}
Mike Stump1eb44332009-09-09 15:08:12 +00001644Stmt *BlockExpr::getBody() {
1645 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001646}
Steve Naroff56ee6892008-10-08 17:01:13 +00001647
1648
Reid Spencer5f016e22007-07-11 17:01:13 +00001649//===----------------------------------------------------------------------===//
1650// Generic Expression Routines
1651//===----------------------------------------------------------------------===//
1652
Chris Lattner026dc962009-02-14 07:37:35 +00001653/// isUnusedResultAWarning - Return true if this immediate expression should
1654/// be warned about if the result is unused. If so, fill in Loc and Ranges
1655/// with location to warn on and the source range[s] to report with the
1656/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001657bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1658 SourceRange &R1, SourceRange &R2,
1659 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001660 // Don't warn if the expr is type dependent. The type could end up
1661 // instantiating to void.
1662 if (isTypeDependent())
1663 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 switch (getStmtClass()) {
1666 default:
John McCall0faede62010-03-12 07:11:26 +00001667 if (getType()->isVoidType())
1668 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001669 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001670 Loc = getExprLoc();
1671 R1 = getSourceRange();
1672 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001674 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001675 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001676 case GenericSelectionExprClass:
1677 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001678 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001679 case UnaryOperatorClass: {
1680 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001681
Reid Spencer5f016e22007-07-11 17:01:13 +00001682 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001683 case UO_Plus:
1684 case UO_Minus:
1685 case UO_AddrOf:
1686 case UO_Not:
1687 case UO_LNot:
1688 case UO_Deref:
1689 break;
John McCall2de56d12010-08-25 11:45:40 +00001690 case UO_PostInc:
1691 case UO_PostDec:
1692 case UO_PreInc:
1693 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001694 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001695 case UO_Real:
1696 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001697 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001698 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1699 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001700 return false;
1701 break;
John McCall2de56d12010-08-25 11:45:40 +00001702 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00001703 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 }
Eli Friedmana6115062012-05-24 00:47:05 +00001705 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001706 Loc = UO->getOperatorLoc();
1707 R1 = UO->getSubExpr()->getSourceRange();
1708 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001709 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001710 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001711 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001712 switch (BO->getOpcode()) {
1713 default:
1714 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001715 // Consider the RHS of comma for side effects. LHS was checked by
1716 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001717 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001718 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1719 // lvalue-ness) of an assignment written in a macro.
1720 if (IntegerLiteral *IE =
1721 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1722 if (IE->getValue() == 0)
1723 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001724 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001725 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001726 case BO_LAnd:
1727 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00001728 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1729 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001730 return false;
1731 break;
John McCallbf0ee352010-02-16 04:10:53 +00001732 }
Chris Lattner026dc962009-02-14 07:37:35 +00001733 if (BO->isAssignmentOp())
1734 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001735 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001736 Loc = BO->getOperatorLoc();
1737 R1 = BO->getLHS()->getSourceRange();
1738 R2 = BO->getRHS()->getSourceRange();
1739 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001740 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001741 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001742 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001743 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001744 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001745
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001746 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001747 // If only one of the LHS or RHS is a warning, the operator might
1748 // be being used for control flow. Only warn if both the LHS and
1749 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001750 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00001751 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001752 return false;
1753 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001754 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00001755 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001756 }
1757
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001759 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001760 Loc = cast<MemberExpr>(this)->getMemberLoc();
1761 R1 = SourceRange(Loc, Loc);
1762 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1763 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001764
Reid Spencer5f016e22007-07-11 17:01:13 +00001765 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001766 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001767 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1768 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1769 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1770 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001771
Chandler Carruth9b106832011-08-17 09:49:44 +00001772 case CXXOperatorCallExprClass: {
1773 // We warn about operator== and operator!= even when user-defined operator
1774 // overloads as there is no reasonable way to define these such that they
1775 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1776 // warning: these operators are commonly typo'ed, and so warning on them
1777 // provides additional value as well. If this list is updated,
1778 // DiagnoseUnusedComparison should be as well.
1779 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1780 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001781 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmana6115062012-05-24 00:47:05 +00001782 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001783 Loc = Op->getOperatorLoc();
1784 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001785 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001786 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001787
1788 // Fallthrough for generic call handling.
1789 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00001791 case CXXMemberCallExprClass:
1792 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001793 // If this is a direct call, get the callee.
1794 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001795 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001796 // If the callee has attribute pure, const, or warn_unused_result, warn
1797 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001798 //
1799 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1800 // updated to match for QoI.
1801 if (FD->getAttr<WarnUnusedResultAttr>() ||
1802 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001803 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001804 Loc = CE->getCallee()->getLocStart();
1805 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001807 if (unsigned NumArgs = CE->getNumArgs())
1808 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1809 CE->getArg(NumArgs-1)->getLocEnd());
1810 return true;
1811 }
Chris Lattner026dc962009-02-14 07:37:35 +00001812 }
1813 return false;
1814 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001815
1816 case CXXTemporaryObjectExprClass:
1817 case CXXConstructExprClass:
1818 return false;
1819
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001820 case ObjCMessageExprClass: {
1821 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00001822 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001823 ME->isInstanceMessage() &&
1824 !ME->getType()->isVoidType() &&
1825 ME->getSelector().getIdentifierInfoForSlot(0) &&
1826 ME->getSelector().getIdentifierInfoForSlot(0)
1827 ->getName().startswith("init")) {
Eli Friedmana6115062012-05-24 00:47:05 +00001828 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00001829 Loc = getExprLoc();
1830 R1 = ME->getSourceRange();
1831 return true;
1832 }
1833
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001834 const ObjCMethodDecl *MD = ME->getMethodDecl();
1835 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001836 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001837 Loc = getExprLoc();
1838 return true;
1839 }
Chris Lattner026dc962009-02-14 07:37:35 +00001840 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001841 }
Mike Stump1eb44332009-09-09 15:08:12 +00001842
John McCall12f78a62010-12-02 01:19:52 +00001843 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001844 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001845 Loc = getExprLoc();
1846 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001847 return true;
John McCall12f78a62010-12-02 01:19:52 +00001848
John McCall4b9c2d22011-11-06 09:01:30 +00001849 case PseudoObjectExprClass: {
1850 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1851
1852 // Only complain about things that have the form of a getter.
1853 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1854 isa<BinaryOperator>(PO->getSyntacticForm()))
1855 return false;
1856
Eli Friedmana6115062012-05-24 00:47:05 +00001857 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00001858 Loc = getExprLoc();
1859 R1 = getSourceRange();
1860 return true;
1861 }
1862
Chris Lattner611b2ec2008-07-26 19:51:01 +00001863 case StmtExprClass: {
1864 // Statement exprs don't logically have side effects themselves, but are
1865 // sometimes used in macros in ways that give them a type that is unused.
1866 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1867 // however, if the result of the stmt expr is dead, we don't want to emit a
1868 // warning.
1869 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001870 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001871 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00001872 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001873 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1874 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00001875 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001876 }
Mike Stump1eb44332009-09-09 15:08:12 +00001877
John McCall0faede62010-03-12 07:11:26 +00001878 if (getType()->isVoidType())
1879 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001880 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001881 Loc = cast<StmtExpr>(this)->getLParenLoc();
1882 R1 = getSourceRange();
1883 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001884 }
Eli Friedmana6115062012-05-24 00:47:05 +00001885 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00001886 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00001887 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00001888 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00001889 if (CE->getCastKind() == CK_ToVoid) {
1890 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00001891 CE->getSubExpr()->getType().isVolatileQualified()) {
1892 const DeclRefExpr *DRE =
1893 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
1894 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
1895 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
1896 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
1897 R1, R2, Ctx);
1898 }
1899 }
Chris Lattnerfb846642009-07-28 18:25:28 +00001900 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001901 }
Eli Friedman4059da82012-05-24 21:05:41 +00001902
Eli Friedmana6115062012-05-24 00:47:05 +00001903 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00001904 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00001905 if (CE->getCastKind() == CK_ConstructorConversion)
1906 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00001907
Eli Friedmana6115062012-05-24 00:47:05 +00001908 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00001909 if (const CXXFunctionalCastExpr *CXXCE =
1910 dyn_cast<CXXFunctionalCastExpr>(this)) {
1911 Loc = CXXCE->getTypeBeginLoc();
1912 R1 = CXXCE->getSubExpr()->getSourceRange();
1913 } else {
1914 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
1915 Loc = CStyleCE->getLParenLoc();
1916 R1 = CStyleCE->getSubExpr()->getSourceRange();
1917 }
Chris Lattner026dc962009-02-14 07:37:35 +00001918 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001919 }
Eli Friedmana6115062012-05-24 00:47:05 +00001920 case ImplicitCastExprClass: {
1921 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00001922
Eli Friedmana6115062012-05-24 00:47:05 +00001923 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
1924 if (ICE->getCastKind() == CK_LValueToRValue &&
1925 ICE->getSubExpr()->getType().isVolatileQualified())
1926 return false;
1927
1928 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
1929 }
Chris Lattner04421082008-04-08 04:40:51 +00001930 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001931 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00001932 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001933
1934 case CXXNewExprClass:
1935 // FIXME: In theory, there might be new expressions that don't have side
1936 // effects (e.g. a placement new with an uninitialized POD).
1937 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001938 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001939 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001940 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00001941 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001942 case ExprWithCleanupsClass:
1943 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00001944 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001945 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001946}
1947
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001948/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001949/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001950bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001951 const Expr *E = IgnoreParens();
1952 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001953 default:
1954 return false;
1955 case ObjCIvarRefExprClass:
1956 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001957 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001958 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001959 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001960 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001961 case MaterializeTemporaryExprClass:
1962 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1963 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001964 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001965 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00001966 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00001967 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001968
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001969 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1970 if (VD->hasGlobalStorage())
1971 return true;
1972 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001973 // dereferencing to a pointer is always a gc'able candidate,
1974 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001975 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001976 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001977 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001978 return false;
1979 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001980 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001981 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001982 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001983 }
1984 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001985 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001986 }
1987}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001988
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001989bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1990 if (isTypeDependent())
1991 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001992 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001993}
1994
John McCall864c0412011-04-26 20:42:42 +00001995QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00001996 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00001997
1998 // Bound member expressions are always one of these possibilities:
1999 // x->m x.m x->*y x.*y
2000 // (possibly parenthesized)
2001
2002 expr = expr->IgnoreParens();
2003 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2004 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2005 return mem->getMemberDecl()->getType();
2006 }
2007
2008 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2009 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2010 ->getPointeeType();
2011 assert(type->isFunctionType());
2012 return type;
2013 }
2014
2015 assert(isa<UnresolvedMemberExpr>(expr));
2016 return QualType();
2017}
2018
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002019Expr* Expr::IgnoreParens() {
2020 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002021 while (true) {
2022 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2023 E = P->getSubExpr();
2024 continue;
2025 }
2026 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2027 if (P->getOpcode() == UO_Extension) {
2028 E = P->getSubExpr();
2029 continue;
2030 }
2031 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002032 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2033 if (!P->isResultDependent()) {
2034 E = P->getResultExpr();
2035 continue;
2036 }
2037 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002038 return E;
2039 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002040}
2041
Chris Lattner56f34942008-02-13 01:02:39 +00002042/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2043/// or CastExprs or ImplicitCastExprs, returning their operand.
2044Expr *Expr::IgnoreParenCasts() {
2045 Expr *E = this;
2046 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002047 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002048 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002049 continue;
2050 }
2051 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002052 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002053 continue;
2054 }
2055 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2056 if (P->getOpcode() == UO_Extension) {
2057 E = P->getSubExpr();
2058 continue;
2059 }
2060 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002061 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2062 if (!P->isResultDependent()) {
2063 E = P->getResultExpr();
2064 continue;
2065 }
2066 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002067 if (MaterializeTemporaryExpr *Materialize
2068 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2069 E = Materialize->GetTemporaryExpr();
2070 continue;
2071 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002072 if (SubstNonTypeTemplateParmExpr *NTTP
2073 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2074 E = NTTP->getReplacement();
2075 continue;
2076 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002077 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002078 }
2079}
2080
John McCall9c5d70c2010-12-04 08:24:19 +00002081/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2082/// casts. This is intended purely as a temporary workaround for code
2083/// that hasn't yet been rewritten to do the right thing about those
2084/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002085Expr *Expr::IgnoreParenLValueCasts() {
2086 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002087 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002088 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2089 E = P->getSubExpr();
2090 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002091 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002092 if (P->getCastKind() == CK_LValueToRValue) {
2093 E = P->getSubExpr();
2094 continue;
2095 }
John McCall9c5d70c2010-12-04 08:24:19 +00002096 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2097 if (P->getOpcode() == UO_Extension) {
2098 E = P->getSubExpr();
2099 continue;
2100 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002101 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2102 if (!P->isResultDependent()) {
2103 E = P->getResultExpr();
2104 continue;
2105 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002106 } else if (MaterializeTemporaryExpr *Materialize
2107 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2108 E = Materialize->GetTemporaryExpr();
2109 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002110 } else if (SubstNonTypeTemplateParmExpr *NTTP
2111 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2112 E = NTTP->getReplacement();
2113 continue;
John McCallf6a16482010-12-04 03:47:34 +00002114 }
2115 break;
2116 }
2117 return E;
2118}
2119
John McCall2fc46bf2010-05-05 22:59:52 +00002120Expr *Expr::IgnoreParenImpCasts() {
2121 Expr *E = this;
2122 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002123 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002124 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002125 continue;
2126 }
2127 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002128 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002129 continue;
2130 }
2131 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2132 if (P->getOpcode() == UO_Extension) {
2133 E = P->getSubExpr();
2134 continue;
2135 }
2136 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002137 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2138 if (!P->isResultDependent()) {
2139 E = P->getResultExpr();
2140 continue;
2141 }
2142 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002143 if (MaterializeTemporaryExpr *Materialize
2144 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2145 E = Materialize->GetTemporaryExpr();
2146 continue;
2147 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002148 if (SubstNonTypeTemplateParmExpr *NTTP
2149 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2150 E = NTTP->getReplacement();
2151 continue;
2152 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002153 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002154 }
2155}
2156
Hans Wennborg2f072b42011-06-09 17:06:51 +00002157Expr *Expr::IgnoreConversionOperator() {
2158 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002159 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002160 return MCE->getImplicitObjectArgument();
2161 }
2162 return this;
2163}
2164
Chris Lattnerecdd8412009-03-13 17:28:01 +00002165/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2166/// value (including ptr->int casts of the same size). Strip off any
2167/// ParenExpr or CastExprs, returning their operand.
2168Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2169 Expr *E = this;
2170 while (true) {
2171 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2172 E = P->getSubExpr();
2173 continue;
2174 }
Mike Stump1eb44332009-09-09 15:08:12 +00002175
Chris Lattnerecdd8412009-03-13 17:28:01 +00002176 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2177 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002178 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002179 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002180
Chris Lattnerecdd8412009-03-13 17:28:01 +00002181 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2182 E = SE;
2183 continue;
2184 }
Mike Stump1eb44332009-09-09 15:08:12 +00002185
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002186 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002187 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002188 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002189 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002190 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2191 E = SE;
2192 continue;
2193 }
2194 }
Mike Stump1eb44332009-09-09 15:08:12 +00002195
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002196 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2197 if (P->getOpcode() == UO_Extension) {
2198 E = P->getSubExpr();
2199 continue;
2200 }
2201 }
2202
Peter Collingbournef111d932011-04-15 00:35:48 +00002203 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2204 if (!P->isResultDependent()) {
2205 E = P->getResultExpr();
2206 continue;
2207 }
2208 }
2209
Douglas Gregorc0244c52011-09-08 17:56:33 +00002210 if (SubstNonTypeTemplateParmExpr *NTTP
2211 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2212 E = NTTP->getReplacement();
2213 continue;
2214 }
2215
Chris Lattnerecdd8412009-03-13 17:28:01 +00002216 return E;
2217 }
2218}
2219
Douglas Gregor6eef5192009-12-14 19:27:10 +00002220bool Expr::isDefaultArgument() const {
2221 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002222 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2223 E = M->GetTemporaryExpr();
2224
Douglas Gregor6eef5192009-12-14 19:27:10 +00002225 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2226 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002227
Douglas Gregor6eef5192009-12-14 19:27:10 +00002228 return isa<CXXDefaultArgExpr>(E);
2229}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002230
Douglas Gregor2f599792010-04-02 18:24:57 +00002231/// \brief Skip over any no-op casts and any temporary-binding
2232/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002233static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002234 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2235 E = M->GetTemporaryExpr();
2236
Douglas Gregor2f599792010-04-02 18:24:57 +00002237 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002238 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002239 E = ICE->getSubExpr();
2240 else
2241 break;
2242 }
2243
2244 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2245 E = BE->getSubExpr();
2246
2247 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002248 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002249 E = ICE->getSubExpr();
2250 else
2251 break;
2252 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002253
2254 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002255}
2256
John McCall558d2ab2010-09-15 10:14:12 +00002257/// isTemporaryObject - Determines if this expression produces a
2258/// temporary of the given class type.
2259bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2260 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2261 return false;
2262
Anders Carlssonf8b30152010-11-28 16:40:49 +00002263 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002264
John McCall58277b52010-09-15 20:59:13 +00002265 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002266 if (!E->Classify(C).isPRValue()) {
2267 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002268 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002269 return false;
2270 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002271
John McCall19e60ad2010-09-16 06:57:56 +00002272 // Black-list a few cases which yield pr-values of class type that don't
2273 // refer to temporaries of that type:
2274
2275 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002276 if (isa<ImplicitCastExpr>(E)) {
2277 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2278 case CK_DerivedToBase:
2279 case CK_UncheckedDerivedToBase:
2280 return false;
2281 default:
2282 break;
2283 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002284 }
2285
John McCall19e60ad2010-09-16 06:57:56 +00002286 // - member expressions (all)
2287 if (isa<MemberExpr>(E))
2288 return false;
2289
John McCall56ca35d2011-02-17 10:25:35 +00002290 // - opaque values (all)
2291 if (isa<OpaqueValueExpr>(E))
2292 return false;
2293
John McCall558d2ab2010-09-15 10:14:12 +00002294 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002295}
2296
Douglas Gregor75e85042011-03-02 21:06:53 +00002297bool Expr::isImplicitCXXThis() const {
2298 const Expr *E = this;
2299
2300 // Strip away parentheses and casts we don't care about.
2301 while (true) {
2302 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2303 E = Paren->getSubExpr();
2304 continue;
2305 }
2306
2307 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2308 if (ICE->getCastKind() == CK_NoOp ||
2309 ICE->getCastKind() == CK_LValueToRValue ||
2310 ICE->getCastKind() == CK_DerivedToBase ||
2311 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2312 E = ICE->getSubExpr();
2313 continue;
2314 }
2315 }
2316
2317 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2318 if (UnOp->getOpcode() == UO_Extension) {
2319 E = UnOp->getSubExpr();
2320 continue;
2321 }
2322 }
2323
Douglas Gregor03e80032011-06-21 17:03:29 +00002324 if (const MaterializeTemporaryExpr *M
2325 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2326 E = M->GetTemporaryExpr();
2327 continue;
2328 }
2329
Douglas Gregor75e85042011-03-02 21:06:53 +00002330 break;
2331 }
2332
2333 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2334 return This->isImplicit();
2335
2336 return false;
2337}
2338
Douglas Gregor898574e2008-12-05 23:32:09 +00002339/// hasAnyTypeDependentArguments - Determines if any of the expressions
2340/// in Exprs is type-dependent.
Ahmed Charles13a140c2012-02-25 11:00:22 +00002341bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2342 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002343 if (Exprs[I]->isTypeDependent())
2344 return true;
2345
2346 return false;
2347}
2348
John McCall4204f072010-08-02 21:13:48 +00002349bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002350 // This function is attempting whether an expression is an initializer
2351 // which can be evaluated at compile-time. isEvaluatable handles most
2352 // of the cases, but it can't deal with some initializer-specific
2353 // expressions, and it can't deal with aggregates; we deal with those here,
2354 // and fall back to isEvaluatable for the other cases.
2355
John McCall4204f072010-08-02 21:13:48 +00002356 // If we ever capture reference-binding directly in the AST, we can
2357 // kill the second parameter.
2358
2359 if (IsForRef) {
2360 EvalResult Result;
2361 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2362 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002363
Anders Carlssone8a32b82008-11-24 05:23:59 +00002364 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002365 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002366 case IntegerLiteralClass:
2367 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002368 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002369 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002370 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002371 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002372 case CXXTemporaryObjectExprClass:
2373 case CXXConstructExprClass: {
2374 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002375
2376 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002377 if (CE->getConstructor()->isTrivial()) {
2378 // 1) an application of the trivial default constructor or
2379 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002380
Richard Smith180f4792011-11-10 06:34:14 +00002381 // 2) an elidable trivial copy construction of an operand which is
2382 // itself a constant initializer. Note that we consider the
2383 // operand on its own, *not* as a reference binding.
2384 if (CE->isElidable() &&
2385 CE->getArg(0)->isConstantInitializer(Ctx, false))
2386 return true;
2387 }
2388
2389 // 3) a foldable constexpr constructor.
2390 break;
John McCallb4b9b152010-08-01 21:51:45 +00002391 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002392 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002393 // This handles gcc's extension that allows global initializers like
2394 // "struct x {int x;} x = (struct x) {};".
2395 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002396 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002397 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002398 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002399 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002400 // FIXME: This doesn't deal with fields with reference types correctly.
2401 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2402 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002403 const InitListExpr *Exp = cast<InitListExpr>(this);
2404 unsigned numInits = Exp->getNumInits();
2405 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002406 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002407 return false;
2408 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002409 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002410 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002411 case ImplicitValueInitExprClass:
2412 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002413 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002414 return cast<ParenExpr>(this)->getSubExpr()
2415 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002416 case GenericSelectionExprClass:
2417 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2418 return false;
2419 return cast<GenericSelectionExpr>(this)->getResultExpr()
2420 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002421 case ChooseExprClass:
2422 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2423 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002424 case UnaryOperatorClass: {
2425 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002426 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002427 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002428 break;
2429 }
John McCall4204f072010-08-02 21:13:48 +00002430 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002431 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002432 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002433 case CStyleCastExprClass: {
2434 const CastExpr *CE = cast<CastExpr>(this);
2435
David Chisnall7a7ee302012-01-16 17:27:18 +00002436 // If we're promoting an integer to an _Atomic type then this is constant
2437 // if the integer is constant. We also need to check the converse in case
2438 // someone does something like:
2439 //
2440 // int a = (_Atomic(int))42;
2441 //
2442 // I doubt anyone would write code like this directly, but it's quite
2443 // possible as the result of macro expansions.
2444 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2445 CE->getCastKind() == CK_AtomicToNonAtomic)
2446 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2447
Richard Smithd62ca372011-12-06 22:44:34 +00002448 // Handle bitcasts of vector constants.
2449 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2450 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2451
Eli Friedman6bd97192011-12-21 00:43:02 +00002452 // Handle misc casts we want to ignore.
2453 // FIXME: Is it really safe to ignore all these?
2454 if (CE->getCastKind() == CK_NoOp ||
2455 CE->getCastKind() == CK_LValueToRValue ||
2456 CE->getCastKind() == CK_ToUnion ||
2457 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002458 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2459
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002460 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002461 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002462 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002463 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002464 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002465 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002466 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002467}
2468
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002469namespace {
2470 /// \brief Look for a call to a non-trivial function within an expression.
2471 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2472 {
2473 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2474
2475 bool NonTrivial;
2476
2477 public:
2478 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002479 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002480
2481 bool hasNonTrivialCall() const { return NonTrivial; }
2482
2483 void VisitCallExpr(CallExpr *E) {
2484 if (CXXMethodDecl *Method
2485 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2486 if (Method->isTrivial()) {
2487 // Recurse to children of the call.
2488 Inherited::VisitStmt(E);
2489 return;
2490 }
2491 }
2492
2493 NonTrivial = true;
2494 }
2495
2496 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2497 if (E->getConstructor()->isTrivial()) {
2498 // Recurse to children of the call.
2499 Inherited::VisitStmt(E);
2500 return;
2501 }
2502
2503 NonTrivial = true;
2504 }
2505
2506 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2507 if (E->getTemporary()->getDestructor()->isTrivial()) {
2508 Inherited::VisitStmt(E);
2509 return;
2510 }
2511
2512 NonTrivial = true;
2513 }
2514 };
2515}
2516
2517bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2518 NonTrivialCallFinder Finder(Ctx);
2519 Finder.Visit(this);
2520 return Finder.hasNonTrivialCall();
2521}
2522
Chandler Carruth82214a82011-02-18 23:54:50 +00002523/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2524/// pointer constant or not, as well as the specific kind of constant detected.
2525/// Null pointer constants can be integer constant expressions with the
2526/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2527/// (a GNU extension).
2528Expr::NullPointerConstantKind
2529Expr::isNullPointerConstant(ASTContext &Ctx,
2530 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002531 if (isValueDependent()) {
2532 switch (NPC) {
2533 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002534 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002535 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002536 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2537 return NPCK_ZeroInteger;
2538 else
2539 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002540
Douglas Gregorce940492009-09-25 04:25:58 +00002541 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002542 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002543 }
2544 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002545
Sebastian Redl07779722008-10-31 14:43:28 +00002546 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002547 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002548 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002549 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002550 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002551 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002552 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002553 Pointee->isVoidType() && // to void*
2554 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002555 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002556 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002557 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002558 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2559 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002560 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002561 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2562 // Accept ((void*)0) as a null pointer constant, as many other
2563 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002564 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002565 } else if (const GenericSelectionExpr *GE =
2566 dyn_cast<GenericSelectionExpr>(this)) {
2567 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002568 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002569 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002570 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002571 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002572 } else if (isa<GNUNullExpr>(this)) {
2573 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002574 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002575 } else if (const MaterializeTemporaryExpr *M
2576 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2577 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002578 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2579 if (const Expr *Source = OVE->getSourceExpr())
2580 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002581 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002582
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002583 // C++0x nullptr_t is always a null pointer constant.
2584 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002585 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002586
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002587 if (const RecordType *UT = getType()->getAsUnionType())
2588 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2589 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2590 const Expr *InitExpr = CLE->getInitializer();
2591 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2592 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2593 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002594 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002595 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002596 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002597 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002598
Reid Spencer5f016e22007-07-11 17:01:13 +00002599 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith70488e22012-02-14 21:38:30 +00002600 // test for the value 0. Don't use the C++11 constant expression semantics
2601 // for this, for now; once the dust settles on core issue 903, we might only
2602 // allow a literal 0 here in C++11 mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002603 if (Ctx.getLangOpts().CPlusPlus0x) {
Richard Smith70488e22012-02-14 21:38:30 +00002604 if (!isCXX98IntegralConstantExpr(Ctx))
2605 return NPCK_NotNull;
2606 } else {
2607 if (!isIntegerConstantExpr(Ctx))
2608 return NPCK_NotNull;
2609 }
Chandler Carruth82214a82011-02-18 23:54:50 +00002610
Richard Smith70488e22012-02-14 21:38:30 +00002611 return (EvaluateKnownConstInt(Ctx) == 0) ? NPCK_ZeroInteger : NPCK_NotNull;
Reid Spencer5f016e22007-07-11 17:01:13 +00002612}
Steve Naroff31a45842007-07-28 23:10:27 +00002613
John McCallf6a16482010-12-04 03:47:34 +00002614/// \brief If this expression is an l-value for an Objective C
2615/// property, find the underlying property reference expression.
2616const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2617 const Expr *E = this;
2618 while (true) {
2619 assert((E->getValueKind() == VK_LValue &&
2620 E->getObjectKind() == OK_ObjCProperty) &&
2621 "expression is not a property reference");
2622 E = E->IgnoreParenCasts();
2623 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2624 if (BO->getOpcode() == BO_Comma) {
2625 E = BO->getRHS();
2626 continue;
2627 }
2628 }
2629
2630 break;
2631 }
2632
2633 return cast<ObjCPropertyRefExpr>(E);
2634}
2635
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002636FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002637 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002638
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002639 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002640 if (ICE->getCastKind() == CK_LValueToRValue ||
2641 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002642 E = ICE->getSubExpr()->IgnoreParens();
2643 else
2644 break;
2645 }
2646
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002647 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002648 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002649 if (Field->isBitField())
2650 return Field;
2651
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002652 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2653 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2654 if (Field->isBitField())
2655 return Field;
2656
Eli Friedman42068e92011-07-13 02:05:57 +00002657 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002658 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2659 return BinOp->getLHS()->getBitField();
2660
Eli Friedman42068e92011-07-13 02:05:57 +00002661 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2662 return BinOp->getRHS()->getBitField();
2663 }
2664
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002665 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002666}
2667
Anders Carlsson09380262010-01-31 17:18:49 +00002668bool Expr::refersToVectorElement() const {
2669 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002670
Anders Carlsson09380262010-01-31 17:18:49 +00002671 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002672 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002673 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002674 E = ICE->getSubExpr()->IgnoreParens();
2675 else
2676 break;
2677 }
Sean Huntc3021132010-05-05 15:23:54 +00002678
Anders Carlsson09380262010-01-31 17:18:49 +00002679 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2680 return ASE->getBase()->getType()->isVectorType();
2681
2682 if (isa<ExtVectorElementExpr>(E))
2683 return true;
2684
2685 return false;
2686}
2687
Chris Lattner2140e902009-02-16 22:14:05 +00002688/// isArrow - Return true if the base expression is a pointer to vector,
2689/// return false if the base expression is a vector.
2690bool ExtVectorElementExpr::isArrow() const {
2691 return getBase()->getType()->isPointerType();
2692}
2693
Nate Begeman213541a2008-04-18 23:10:10 +00002694unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002695 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002696 return VT->getNumElements();
2697 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002698}
2699
Nate Begeman8a997642008-05-09 06:41:27 +00002700/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002701bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002702 // FIXME: Refactor this code to an accessor on the AST node which returns the
2703 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002704 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002705
2706 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002707 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002708 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002709
Nate Begeman190d6a22009-01-18 02:01:21 +00002710 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002711 if (Comp[0] == 's' || Comp[0] == 'S')
2712 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002713
Daniel Dunbar15027422009-10-17 23:53:04 +00002714 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002715 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002716 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002717
Steve Narofffec0b492007-07-30 03:29:09 +00002718 return false;
2719}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002720
Nate Begeman8a997642008-05-09 06:41:27 +00002721/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002722void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002723 SmallVectorImpl<unsigned> &Elts) const {
2724 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002725 if (Comp[0] == 's' || Comp[0] == 'S')
2726 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002727
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002728 bool isHi = Comp == "hi";
2729 bool isLo = Comp == "lo";
2730 bool isEven = Comp == "even";
2731 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002732
Nate Begeman8a997642008-05-09 06:41:27 +00002733 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2734 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002735
Nate Begeman8a997642008-05-09 06:41:27 +00002736 if (isHi)
2737 Index = e + i;
2738 else if (isLo)
2739 Index = i;
2740 else if (isEven)
2741 Index = 2 * i;
2742 else if (isOdd)
2743 Index = 2 * i + 1;
2744 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002745 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002746
Nate Begeman3b8d1162008-05-13 21:03:02 +00002747 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002748 }
Nate Begeman8a997642008-05-09 06:41:27 +00002749}
2750
Douglas Gregor04badcf2010-04-21 00:45:42 +00002751ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002752 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002753 SourceLocation LBracLoc,
2754 SourceLocation SuperLoc,
2755 bool IsInstanceSuper,
2756 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002757 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002758 ArrayRef<SourceLocation> SelLocs,
2759 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002760 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002761 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002762 SourceLocation RBracLoc,
2763 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002764 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002765 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002766 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002767 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002768 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2769 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002770 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002771 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
2772 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002773{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002774 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002775 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002776}
2777
Douglas Gregor04badcf2010-04-21 00:45:42 +00002778ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002779 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002780 SourceLocation LBracLoc,
2781 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002782 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002783 ArrayRef<SourceLocation> SelLocs,
2784 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002785 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002786 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002787 SourceLocation RBracLoc,
2788 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002789 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002790 T->isDependentType(), T->isInstantiationDependentType(),
2791 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002792 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2793 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002794 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002795 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002796 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002797{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002798 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002799 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002800}
2801
Douglas Gregor04badcf2010-04-21 00:45:42 +00002802ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002803 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002804 SourceLocation LBracLoc,
2805 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002806 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002807 ArrayRef<SourceLocation> SelLocs,
2808 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002809 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002810 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002811 SourceLocation RBracLoc,
2812 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002813 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002814 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002815 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002816 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002817 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2818 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002819 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002820 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002821 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002822{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002823 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002824 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002825}
2826
2827void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2828 ArrayRef<SourceLocation> SelLocs,
2829 SelectorLocationsKind SelLocsK) {
2830 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00002831 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002832 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002833 if (Args[I]->isTypeDependent())
2834 ExprBits.TypeDependent = true;
2835 if (Args[I]->isValueDependent())
2836 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002837 if (Args[I]->isInstantiationDependent())
2838 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002839 if (Args[I]->containsUnexpandedParameterPack())
2840 ExprBits.ContainsUnexpandedParameterPack = true;
2841
2842 MyArgs[I] = Args[I];
2843 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002844
Benjamin Kramer19562c92012-02-20 00:20:48 +00002845 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00002846 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00002847 if (SelLocsK == SelLoc_NonStandard)
2848 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
2849 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002850}
2851
Douglas Gregor04badcf2010-04-21 00:45:42 +00002852ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002853 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002854 SourceLocation LBracLoc,
2855 SourceLocation SuperLoc,
2856 bool IsInstanceSuper,
2857 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002858 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002859 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002860 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002861 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002862 SourceLocation RBracLoc,
2863 bool isImplicit) {
2864 assert((!SelLocs.empty() || isImplicit) &&
2865 "No selector locs for non-implicit message");
2866 ObjCMessageExpr *Mem;
2867 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
2868 if (isImplicit)
2869 Mem = alloc(Context, Args.size(), 0);
2870 else
2871 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00002872 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002873 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002874 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002875}
2876
2877ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002878 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002879 SourceLocation LBracLoc,
2880 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002881 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002882 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002883 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002884 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002885 SourceLocation RBracLoc,
2886 bool isImplicit) {
2887 assert((!SelLocs.empty() || isImplicit) &&
2888 "No selector locs for non-implicit message");
2889 ObjCMessageExpr *Mem;
2890 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
2891 if (isImplicit)
2892 Mem = alloc(Context, Args.size(), 0);
2893 else
2894 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002895 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002896 SelLocs, SelLocsK, Method, Args, RBracLoc,
2897 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002898}
2899
2900ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002901 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002902 SourceLocation LBracLoc,
2903 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002904 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002905 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002906 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002907 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002908 SourceLocation RBracLoc,
2909 bool isImplicit) {
2910 assert((!SelLocs.empty() || isImplicit) &&
2911 "No selector locs for non-implicit message");
2912 ObjCMessageExpr *Mem;
2913 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
2914 if (isImplicit)
2915 Mem = alloc(Context, Args.size(), 0);
2916 else
2917 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002918 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002919 SelLocs, SelLocsK, Method, Args, RBracLoc,
2920 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002921}
2922
Sean Huntc3021132010-05-05 15:23:54 +00002923ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002924 unsigned NumArgs,
2925 unsigned NumStoredSelLocs) {
2926 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002927 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2928}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002929
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002930ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2931 ArrayRef<Expr *> Args,
2932 SourceLocation RBraceLoc,
2933 ArrayRef<SourceLocation> SelLocs,
2934 Selector Sel,
2935 SelectorLocationsKind &SelLocsK) {
2936 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
2937 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
2938 : 0;
2939 return alloc(C, Args.size(), NumStoredSelLocs);
2940}
2941
2942ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2943 unsigned NumArgs,
2944 unsigned NumStoredSelLocs) {
2945 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2946 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
2947 return (ObjCMessageExpr *)C.Allocate(Size,
2948 llvm::AlignOf<ObjCMessageExpr>::Alignment);
2949}
2950
2951void ObjCMessageExpr::getSelectorLocs(
2952 SmallVectorImpl<SourceLocation> &SelLocs) const {
2953 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
2954 SelLocs.push_back(getSelectorLoc(i));
2955}
2956
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002957SourceRange ObjCMessageExpr::getReceiverRange() const {
2958 switch (getReceiverKind()) {
2959 case Instance:
2960 return getInstanceReceiver()->getSourceRange();
2961
2962 case Class:
2963 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2964
2965 case SuperInstance:
2966 case SuperClass:
2967 return getSuperLoc();
2968 }
2969
David Blaikie30263482012-01-20 21:50:17 +00002970 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002971}
2972
Douglas Gregor04badcf2010-04-21 00:45:42 +00002973Selector ObjCMessageExpr::getSelector() const {
2974 if (HasMethod)
2975 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2976 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00002977 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002978}
2979
2980ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2981 switch (getReceiverKind()) {
2982 case Instance:
2983 if (const ObjCObjectPointerType *Ptr
2984 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2985 return Ptr->getInterfaceDecl();
2986 break;
2987
2988 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00002989 if (const ObjCObjectType *Ty
2990 = getClassReceiver()->getAs<ObjCObjectType>())
2991 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00002992 break;
2993
2994 case SuperInstance:
2995 if (const ObjCObjectPointerType *Ptr
2996 = getSuperType()->getAs<ObjCObjectPointerType>())
2997 return Ptr->getInterfaceDecl();
2998 break;
2999
3000 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00003001 if (const ObjCObjectType *Iface
3002 = getSuperType()->getAs<ObjCObjectType>())
3003 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003004 break;
3005 }
3006
3007 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003008}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003009
Chris Lattner5f9e2722011-07-23 10:55:15 +00003010StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003011 switch (getBridgeKind()) {
3012 case OBC_Bridge:
3013 return "__bridge";
3014 case OBC_BridgeTransfer:
3015 return "__bridge_transfer";
3016 case OBC_BridgeRetained:
3017 return "__bridge_retained";
3018 }
David Blaikie30263482012-01-20 21:50:17 +00003019
3020 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003021}
3022
Jay Foad4ba2a172011-01-12 09:06:06 +00003023bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003024 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003025}
3026
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003027ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3028 QualType Type, SourceLocation BLoc,
3029 SourceLocation RP)
3030 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3031 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003032 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003033 Type->containsUnexpandedParameterPack()),
3034 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3035{
3036 SubExprs = new (C) Stmt*[nexpr];
3037 for (unsigned i = 0; i < nexpr; i++) {
3038 if (args[i]->isTypeDependent())
3039 ExprBits.TypeDependent = true;
3040 if (args[i]->isValueDependent())
3041 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003042 if (args[i]->isInstantiationDependent())
3043 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003044 if (args[i]->containsUnexpandedParameterPack())
3045 ExprBits.ContainsUnexpandedParameterPack = true;
3046
3047 SubExprs[i] = args[i];
3048 }
3049}
3050
Nate Begeman888376a2009-08-12 02:28:50 +00003051void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3052 unsigned NumExprs) {
3053 if (SubExprs) C.Deallocate(SubExprs);
3054
3055 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003056 this->NumExprs = NumExprs;
3057 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003058}
Nate Begeman888376a2009-08-12 02:28:50 +00003059
Peter Collingbournef111d932011-04-15 00:35:48 +00003060GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3061 SourceLocation GenericLoc, Expr *ControllingExpr,
3062 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3063 unsigned NumAssocs, SourceLocation DefaultLoc,
3064 SourceLocation RParenLoc,
3065 bool ContainsUnexpandedParameterPack,
3066 unsigned ResultIndex)
3067 : Expr(GenericSelectionExprClass,
3068 AssocExprs[ResultIndex]->getType(),
3069 AssocExprs[ResultIndex]->getValueKind(),
3070 AssocExprs[ResultIndex]->getObjectKind(),
3071 AssocExprs[ResultIndex]->isTypeDependent(),
3072 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003073 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003074 ContainsUnexpandedParameterPack),
3075 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3076 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3077 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3078 RParenLoc(RParenLoc) {
3079 SubExprs[CONTROLLING] = ControllingExpr;
3080 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3081 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3082}
3083
3084GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3085 SourceLocation GenericLoc, Expr *ControllingExpr,
3086 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3087 unsigned NumAssocs, SourceLocation DefaultLoc,
3088 SourceLocation RParenLoc,
3089 bool ContainsUnexpandedParameterPack)
3090 : Expr(GenericSelectionExprClass,
3091 Context.DependentTy,
3092 VK_RValue,
3093 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003094 /*isTypeDependent=*/true,
3095 /*isValueDependent=*/true,
3096 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003097 ContainsUnexpandedParameterPack),
3098 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3099 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3100 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3101 RParenLoc(RParenLoc) {
3102 SubExprs[CONTROLLING] = ControllingExpr;
3103 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3104 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3105}
3106
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003107//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003108// DesignatedInitExpr
3109//===----------------------------------------------------------------------===//
3110
Chandler Carruthb1138242011-06-16 06:47:06 +00003111IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003112 assert(Kind == FieldDesignator && "Only valid on a field designator");
3113 if (Field.NameOrField & 0x01)
3114 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3115 else
3116 return getField()->getIdentifier();
3117}
3118
Sean Huntc3021132010-05-05 15:23:54 +00003119DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003120 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003121 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003122 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003123 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003124 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003125 unsigned NumIndexExprs,
3126 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003127 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003128 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003129 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003130 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003131 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003132 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3133 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003134 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003135
3136 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003137 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003138 *Child++ = Init;
3139
3140 // Copy the designators and their subexpressions, computing
3141 // value-dependence along the way.
3142 unsigned IndexIdx = 0;
3143 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003144 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003145
3146 if (this->Designators[I].isArrayDesignator()) {
3147 // Compute type- and value-dependence.
3148 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003149 if (Index->isTypeDependent() || Index->isValueDependent())
3150 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003151 if (Index->isInstantiationDependent())
3152 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003153 // Propagate unexpanded parameter packs.
3154 if (Index->containsUnexpandedParameterPack())
3155 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003156
3157 // Copy the index expressions into permanent storage.
3158 *Child++ = IndexExprs[IndexIdx++];
3159 } else if (this->Designators[I].isArrayRangeDesignator()) {
3160 // Compute type- and value-dependence.
3161 Expr *Start = IndexExprs[IndexIdx];
3162 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003163 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003164 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003165 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003166 ExprBits.InstantiationDependent = true;
3167 } else if (Start->isInstantiationDependent() ||
3168 End->isInstantiationDependent()) {
3169 ExprBits.InstantiationDependent = true;
3170 }
3171
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003172 // Propagate unexpanded parameter packs.
3173 if (Start->containsUnexpandedParameterPack() ||
3174 End->containsUnexpandedParameterPack())
3175 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003176
3177 // Copy the start/end expressions into permanent storage.
3178 *Child++ = IndexExprs[IndexIdx++];
3179 *Child++ = IndexExprs[IndexIdx++];
3180 }
3181 }
3182
3183 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003184}
3185
Douglas Gregor05c13a32009-01-22 00:58:24 +00003186DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003187DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003188 unsigned NumDesignators,
3189 Expr **IndexExprs, unsigned NumIndexExprs,
3190 SourceLocation ColonOrEqualLoc,
3191 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003192 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003193 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003194 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003195 ColonOrEqualLoc, UsesColonSyntax,
3196 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003197}
3198
Mike Stump1eb44332009-09-09 15:08:12 +00003199DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003200 unsigned NumIndexExprs) {
3201 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3202 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3203 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3204}
3205
Douglas Gregor319d57f2010-01-06 23:17:19 +00003206void DesignatedInitExpr::setDesignators(ASTContext &C,
3207 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003208 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003209 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003210 NumDesignators = NumDesigs;
3211 for (unsigned I = 0; I != NumDesigs; ++I)
3212 Designators[I] = Desigs[I];
3213}
3214
Abramo Bagnara24f46742011-03-16 15:08:46 +00003215SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3216 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3217 if (size() == 1)
3218 return DIE->getDesignator(0)->getSourceRange();
3219 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3220 DIE->getDesignator(size()-1)->getEndLocation());
3221}
3222
Douglas Gregor05c13a32009-01-22 00:58:24 +00003223SourceRange DesignatedInitExpr::getSourceRange() const {
3224 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003225 Designator &First =
3226 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003227 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003228 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003229 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3230 else
3231 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3232 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003233 StartLoc =
3234 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003235 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3236}
3237
Douglas Gregor05c13a32009-01-22 00:58:24 +00003238Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3239 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3240 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3241 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003242 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3243 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3244}
3245
3246Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003247 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003248 "Requires array range designator");
3249 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3250 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003251 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3252 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3253}
3254
3255Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003256 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003257 "Requires array range designator");
3258 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3259 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003260 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3261 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3262}
3263
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003264/// \brief Replaces the designator at index @p Idx with the series
3265/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003266void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003267 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003268 const Designator *Last) {
3269 unsigned NumNewDesignators = Last - First;
3270 if (NumNewDesignators == 0) {
3271 std::copy_backward(Designators + Idx + 1,
3272 Designators + NumDesignators,
3273 Designators + Idx);
3274 --NumNewDesignators;
3275 return;
3276 } else if (NumNewDesignators == 1) {
3277 Designators[Idx] = *First;
3278 return;
3279 }
3280
Mike Stump1eb44332009-09-09 15:08:12 +00003281 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003282 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003283 std::copy(Designators, Designators + Idx, NewDesignators);
3284 std::copy(First, Last, NewDesignators + Idx);
3285 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3286 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003287 Designators = NewDesignators;
3288 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3289}
3290
Mike Stump1eb44332009-09-09 15:08:12 +00003291ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003292 Expr **exprs, unsigned nexprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003293 SourceLocation rparenloc)
3294 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003295 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003296 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00003297 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003298 for (unsigned i = 0; i != nexprs; ++i) {
3299 if (exprs[i]->isTypeDependent())
3300 ExprBits.TypeDependent = true;
3301 if (exprs[i]->isValueDependent())
3302 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003303 if (exprs[i]->isInstantiationDependent())
3304 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003305 if (exprs[i]->containsUnexpandedParameterPack())
3306 ExprBits.ContainsUnexpandedParameterPack = true;
3307
Nate Begeman2ef13e52009-08-10 23:49:36 +00003308 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003309 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003310}
3311
John McCalle996ffd2011-02-16 08:02:54 +00003312const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3313 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3314 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003315 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3316 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003317 e = cast<CXXConstructExpr>(e)->getArg(0);
3318 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3319 e = ice->getSubExpr();
3320 return cast<OpaqueValueExpr>(e);
3321}
3322
John McCall4b9c2d22011-11-06 09:01:30 +00003323PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3324 unsigned numSemanticExprs) {
3325 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3326 (1 + numSemanticExprs) * sizeof(Expr*),
3327 llvm::alignOf<PseudoObjectExpr>());
3328 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3329}
3330
3331PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3332 : Expr(PseudoObjectExprClass, shell) {
3333 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3334}
3335
3336PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3337 ArrayRef<Expr*> semantics,
3338 unsigned resultIndex) {
3339 assert(syntax && "no syntactic expression!");
3340 assert(semantics.size() && "no semantic expressions!");
3341
3342 QualType type;
3343 ExprValueKind VK;
3344 if (resultIndex == NoResult) {
3345 type = C.VoidTy;
3346 VK = VK_RValue;
3347 } else {
3348 assert(resultIndex < semantics.size());
3349 type = semantics[resultIndex]->getType();
3350 VK = semantics[resultIndex]->getValueKind();
3351 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3352 }
3353
3354 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3355 (1 + semantics.size()) * sizeof(Expr*),
3356 llvm::alignOf<PseudoObjectExpr>());
3357 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3358 resultIndex);
3359}
3360
3361PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3362 Expr *syntax, ArrayRef<Expr*> semantics,
3363 unsigned resultIndex)
3364 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3365 /*filled in at end of ctor*/ false, false, false, false) {
3366 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3367 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3368
3369 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3370 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3371 getSubExprsBuffer()[i] = E;
3372
3373 if (E->isTypeDependent())
3374 ExprBits.TypeDependent = true;
3375 if (E->isValueDependent())
3376 ExprBits.ValueDependent = true;
3377 if (E->isInstantiationDependent())
3378 ExprBits.InstantiationDependent = true;
3379 if (E->containsUnexpandedParameterPack())
3380 ExprBits.ContainsUnexpandedParameterPack = true;
3381
3382 if (isa<OpaqueValueExpr>(E))
3383 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3384 "opaque-value semantic expressions for pseudo-object "
3385 "operations must have sources");
3386 }
3387}
3388
Douglas Gregor05c13a32009-01-22 00:58:24 +00003389//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003390// ExprIterator.
3391//===----------------------------------------------------------------------===//
3392
3393Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3394Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3395Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3396const Expr* ConstExprIterator::operator[](size_t idx) const {
3397 return cast<Expr>(I[idx]);
3398}
3399const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3400const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3401
3402//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003403// Child Iterators for iterating over subexpressions/substatements
3404//===----------------------------------------------------------------------===//
3405
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003406// UnaryExprOrTypeTraitExpr
3407Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003408 // If this is of a type and the type is a VLA type (and not a typedef), the
3409 // size expression of the VLA needs to be treated as an executable expression.
3410 // Why isn't this weirdness documented better in StmtIterator?
3411 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003412 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003413 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003414 return child_range(child_iterator(T), child_iterator());
3415 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003416 }
John McCall63c00d72011-02-09 08:16:59 +00003417 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003418}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003419
Steve Naroff563477d2007-09-18 23:55:05 +00003420// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003421Stmt::child_range ObjCMessageExpr::children() {
3422 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003423 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003424 begin = reinterpret_cast<Stmt **>(this + 1);
3425 else
3426 begin = reinterpret_cast<Stmt **>(getArgs());
3427 return child_range(begin,
3428 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003429}
3430
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003431ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3432 QualType T, ObjCMethodDecl *Method,
3433 SourceRange SR)
3434 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3435 false, false, false, false),
3436 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3437{
3438 Expr **SaveElements = getElements();
3439 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3440 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3441 ExprBits.ValueDependent = true;
3442 if (Elements[I]->isInstantiationDependent())
3443 ExprBits.InstantiationDependent = true;
3444 if (Elements[I]->containsUnexpandedParameterPack())
3445 ExprBits.ContainsUnexpandedParameterPack = true;
3446
3447 SaveElements[I] = Elements[I];
3448 }
3449}
3450
3451ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3452 llvm::ArrayRef<Expr *> Elements,
3453 QualType T, ObjCMethodDecl * Method,
3454 SourceRange SR) {
3455 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3456 + Elements.size() * sizeof(Expr *));
3457 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3458}
3459
3460ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3461 unsigned NumElements) {
3462
3463 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3464 + NumElements * sizeof(Expr *));
3465 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3466}
3467
3468ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3469 ArrayRef<ObjCDictionaryElement> VK,
3470 bool HasPackExpansions,
3471 QualType T, ObjCMethodDecl *method,
3472 SourceRange SR)
3473 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3474 false, false),
3475 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3476 DictWithObjectsMethod(method)
3477{
3478 KeyValuePair *KeyValues = getKeyValues();
3479 ExpansionData *Expansions = getExpansionData();
3480 for (unsigned I = 0; I < NumElements; I++) {
3481 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3482 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3483 ExprBits.ValueDependent = true;
3484 if (VK[I].Key->isInstantiationDependent() ||
3485 VK[I].Value->isInstantiationDependent())
3486 ExprBits.InstantiationDependent = true;
3487 if (VK[I].EllipsisLoc.isInvalid() &&
3488 (VK[I].Key->containsUnexpandedParameterPack() ||
3489 VK[I].Value->containsUnexpandedParameterPack()))
3490 ExprBits.ContainsUnexpandedParameterPack = true;
3491
3492 KeyValues[I].Key = VK[I].Key;
3493 KeyValues[I].Value = VK[I].Value;
3494 if (Expansions) {
3495 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3496 if (VK[I].NumExpansions)
3497 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3498 else
3499 Expansions[I].NumExpansionsPlusOne = 0;
3500 }
3501 }
3502}
3503
3504ObjCDictionaryLiteral *
3505ObjCDictionaryLiteral::Create(ASTContext &C,
3506 ArrayRef<ObjCDictionaryElement> VK,
3507 bool HasPackExpansions,
3508 QualType T, ObjCMethodDecl *method,
3509 SourceRange SR) {
3510 unsigned ExpansionsSize = 0;
3511 if (HasPackExpansions)
3512 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3513
3514 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3515 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3516 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3517}
3518
3519ObjCDictionaryLiteral *
3520ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3521 bool HasPackExpansions) {
3522 unsigned ExpansionsSize = 0;
3523 if (HasPackExpansions)
3524 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3525 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3526 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3527 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3528 HasPackExpansions);
3529}
3530
3531ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
3532 Expr *base,
3533 Expr *key, QualType T,
3534 ObjCMethodDecl *getMethod,
3535 ObjCMethodDecl *setMethod,
3536 SourceLocation RB) {
3537 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
3538 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
3539 OK_ObjCSubscript,
3540 getMethod, setMethod, RB);
3541}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003542
3543AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3544 QualType t, AtomicOp op, SourceLocation RP)
3545 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3546 false, false, false, false),
3547 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3548{
Richard Smithe1b2abc2012-04-10 22:49:28 +00003549 assert(nexpr == getNumSubExprs(op) && "wrong number of subexpressions");
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003550 for (unsigned i = 0; i < nexpr; i++) {
3551 if (args[i]->isTypeDependent())
3552 ExprBits.TypeDependent = true;
3553 if (args[i]->isValueDependent())
3554 ExprBits.ValueDependent = true;
3555 if (args[i]->isInstantiationDependent())
3556 ExprBits.InstantiationDependent = true;
3557 if (args[i]->containsUnexpandedParameterPack())
3558 ExprBits.ContainsUnexpandedParameterPack = true;
3559
3560 SubExprs[i] = args[i];
3561 }
3562}
Richard Smithe1b2abc2012-04-10 22:49:28 +00003563
3564unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
3565 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00003566 case AO__c11_atomic_init:
3567 case AO__c11_atomic_load:
3568 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003569 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00003570
3571 case AO__c11_atomic_store:
3572 case AO__c11_atomic_exchange:
3573 case AO__atomic_load:
3574 case AO__atomic_store:
3575 case AO__atomic_store_n:
3576 case AO__atomic_exchange_n:
3577 case AO__c11_atomic_fetch_add:
3578 case AO__c11_atomic_fetch_sub:
3579 case AO__c11_atomic_fetch_and:
3580 case AO__c11_atomic_fetch_or:
3581 case AO__c11_atomic_fetch_xor:
3582 case AO__atomic_fetch_add:
3583 case AO__atomic_fetch_sub:
3584 case AO__atomic_fetch_and:
3585 case AO__atomic_fetch_or:
3586 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00003587 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00003588 case AO__atomic_add_fetch:
3589 case AO__atomic_sub_fetch:
3590 case AO__atomic_and_fetch:
3591 case AO__atomic_or_fetch:
3592 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00003593 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003594 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00003595
3596 case AO__atomic_exchange:
3597 return 4;
3598
3599 case AO__c11_atomic_compare_exchange_strong:
3600 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003601 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00003602
3603 case AO__atomic_compare_exchange:
3604 case AO__atomic_compare_exchange_n:
3605 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00003606 }
3607 llvm_unreachable("unknown atomic op");
3608}