blob: a92dafb273275c4bd50eaf743b7dc6f6f02cc735 [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"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/Lexer.h"
Richard Smith7a614d82011-06-11 17:19:42 +000025#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000027#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000029#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000031#include <algorithm>
Eli Friedman64f45a22011-11-01 02:23:42 +000032#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000033using namespace clang;
34
Chris Lattner2b334bb2010-04-16 23:34:13 +000035/// isKnownToHaveBooleanValue - Return true if this is an integer expression
36/// that is known to return 0 or 1. This happens for _Bool/bool expressions
37/// but also int expressions which are produced by things like comparisons in
38/// C.
39bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000040 const Expr *E = IgnoreParens();
41
Chris Lattner2b334bb2010-04-16 23:34:13 +000042 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000043 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000044 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000045 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000046
Peter Collingbournef111d932011-04-15 00:35:48 +000047 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000048 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000049 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000050 return UO->getSubExpr()->isKnownToHaveBooleanValue();
51 default:
52 return false;
53 }
54 }
Sean Huntc3021132010-05-05 15:23:54 +000055
John McCall6907fbe2010-06-12 01:56:02 +000056 // Only look through implicit casts. If the user writes
57 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000058 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000059 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000060
Peter Collingbournef111d932011-04-15 00:35:48 +000061 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000062 switch (BO->getOpcode()) {
63 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000064 case BO_LT: // Relational operators.
65 case BO_GT:
66 case BO_LE:
67 case BO_GE:
68 case BO_EQ: // Equality operators.
69 case BO_NE:
70 case BO_LAnd: // AND operator.
71 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000072 return true;
Sean Huntc3021132010-05-05 15:23:54 +000073
John McCall2de56d12010-08-25 11:45:40 +000074 case BO_And: // Bitwise AND operator.
75 case BO_Xor: // Bitwise XOR operator.
76 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000077 // Handle things like (x==2)|(y==12).
78 return BO->getLHS()->isKnownToHaveBooleanValue() &&
79 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000080
John McCall2de56d12010-08-25 11:45:40 +000081 case BO_Comma:
82 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000083 return BO->getRHS()->isKnownToHaveBooleanValue();
84 }
85 }
Sean Huntc3021132010-05-05 15:23:54 +000086
Peter Collingbournef111d932011-04-15 00:35:48 +000087 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000088 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
89 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000090
Chris Lattner2b334bb2010-04-16 23:34:13 +000091 return false;
92}
93
John McCall63c00d72011-02-09 08:16:59 +000094// Amusing macro metaprogramming hack: check whether a class provides
95// a more specific implementation of getExprLoc().
96namespace {
97 /// This implementation is used when a class provides a custom
98 /// implementation of getExprLoc.
99 template <class E, class T>
100 SourceLocation getExprLocImpl(const Expr *expr,
101 SourceLocation (T::*v)() const) {
102 return static_cast<const E*>(expr)->getExprLoc();
103 }
104
105 /// This implementation is used when a class doesn't provide
106 /// a custom implementation of getExprLoc. Overload resolution
107 /// should pick it over the implementation above because it's
108 /// more specialized according to function template partial ordering.
109 template <class E>
110 SourceLocation getExprLocImpl(const Expr *expr,
111 SourceLocation (Expr::*v)() const) {
112 return static_cast<const E*>(expr)->getSourceRange().getBegin();
113 }
114}
115
116SourceLocation Expr::getExprLoc() const {
117 switch (getStmtClass()) {
118 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
119#define ABSTRACT_STMT(type)
120#define STMT(type, base) \
121 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
122#define EXPR(type, base) \
123 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
124#include "clang/AST/StmtNodes.inc"
125 }
126 llvm_unreachable("unknown statement kind");
John McCall63c00d72011-02-09 08:16:59 +0000127}
128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129//===----------------------------------------------------------------------===//
130// Primary Expressions.
131//===----------------------------------------------------------------------===//
132
Douglas Gregor561f8122011-07-01 01:22:09 +0000133/// \brief Compute the type-, value-, and instantiation-dependence of a
134/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000135/// based on the declaration being referenced.
136static void computeDeclRefDependence(NamedDecl *D, QualType T,
137 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000138 bool &ValueDependent,
139 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000140 TypeDependent = false;
141 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000142 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000143
144 // (TD) C++ [temp.dep.expr]p3:
145 // An id-expression is type-dependent if it contains:
146 //
Sean Huntc3021132010-05-05 15:23:54 +0000147 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000148 //
149 // (VD) C++ [temp.dep.constexpr]p2:
150 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000151
Douglas Gregor0da76df2009-11-23 11:41:28 +0000152 // (TD) - an identifier that was declared with dependent type
153 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000154 if (T->isDependentType()) {
155 TypeDependent = true;
156 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000157 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000158 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000159 } else if (T->isInstantiationDependentType()) {
160 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000161 }
Douglas Gregord967e312011-01-19 21:52:31 +0000162
Douglas Gregor0da76df2009-11-23 11:41:28 +0000163 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000164 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000165 == DeclarationName::CXXConversionFunctionName) {
166 QualType T = D->getDeclName().getCXXNameType();
167 if (T->isDependentType()) {
168 TypeDependent = true;
169 ValueDependent = true;
170 InstantiationDependent = true;
171 return;
172 }
173
174 if (T->isInstantiationDependentType())
175 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000176 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000177
Douglas Gregor0da76df2009-11-23 11:41:28 +0000178 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000179 if (isa<NonTypeTemplateParmDecl>(D)) {
180 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000181 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000182 return;
183 }
184
Douglas Gregor0da76df2009-11-23 11:41:28 +0000185 // (VD) - a constant with integral or enumeration type and is
186 // initialized with an expression that is value-dependent.
Richard Smithdb1822c2011-11-08 01:31:09 +0000187 // (VD) - a constant with literal type and is initialized with an
188 // expression that is value-dependent [C++11].
189 // (VD) - FIXME: Missing from the standard:
190 // - an entity with reference type and is initialized with an
191 // expression that is value-dependent [C++11]
Douglas Gregord967e312011-01-19 21:52:31 +0000192 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smithdb1822c2011-11-08 01:31:09 +0000193 if ((D->getASTContext().getLangOptions().CPlusPlus0x ?
194 Var->getType()->isLiteralType() :
195 Var->getType()->isIntegralOrEnumerationType()) &&
196 (Var->getType().getCVRQualifiers() == Qualifiers::Const ||
197 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000198 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000199 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000200 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000201 InstantiationDependent = true;
202 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000203 }
204
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000205 // (VD) - FIXME: Missing from the standard:
206 // - a member function or a static data member of the current
207 // instantiation
Richard Smithdb1822c2011-11-08 01:31:09 +0000208 if (Var->isStaticDataMember() &&
209 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000210 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000211 InstantiationDependent = true;
212 }
Douglas Gregord967e312011-01-19 21:52:31 +0000213
214 return;
215 }
216
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000217 // (VD) - FIXME: Missing from the standard:
218 // - a member function or a static data member of the current
219 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000220 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
221 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000222 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000223 }
Douglas Gregord967e312011-01-19 21:52:31 +0000224}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000225
Douglas Gregord967e312011-01-19 21:52:31 +0000226void DeclRefExpr::computeDependence() {
227 bool TypeDependent = false;
228 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000229 bool InstantiationDependent = false;
230 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
231 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000232
233 // (TD) C++ [temp.dep.expr]p3:
234 // An id-expression is type-dependent if it contains:
235 //
236 // and
237 //
238 // (VD) C++ [temp.dep.constexpr]p2:
239 // An identifier is value-dependent if it is:
240 if (!TypeDependent && !ValueDependent &&
241 hasExplicitTemplateArgs() &&
242 TemplateSpecializationType::anyDependentTemplateArguments(
243 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000244 getNumTemplateArgs(),
245 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000246 TypeDependent = true;
247 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000248 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000249 }
250
251 ExprBits.TypeDependent = TypeDependent;
252 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000253 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000254
Douglas Gregor10738d32010-12-23 23:51:58 +0000255 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000256 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000257 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000258}
259
Chandler Carruth3aa81402011-05-01 23:48:14 +0000260DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000261 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000262 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000263 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000264 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000265 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000266 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
267 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000268 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000269 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000270 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
271 if (FoundD)
272 getInternalFoundDecl() = FoundD;
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000273 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Douglas Gregor561f8122011-07-01 01:22:09 +0000274 if (TemplateArgs) {
275 bool Dependent = false;
276 bool InstantiationDependent = false;
277 bool ContainsUnexpandedParameterPack = false;
278 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
279 InstantiationDependent,
280 ContainsUnexpandedParameterPack);
281 if (InstantiationDependent)
282 setInstantiationDependent(true);
283 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000284 DeclRefExprBits.HadMultipleCandidates = 0;
285
Abramo Bagnara25777432010-08-11 22:01:17 +0000286 computeDependence();
287}
288
Douglas Gregora2813ce2009-10-23 18:54:35 +0000289DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000290 NestedNameSpecifierLoc QualifierLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000291 ValueDecl *D,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000292 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000293 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000294 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000295 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000296 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000297 return Create(Context, QualifierLoc, D,
Abramo Bagnara25777432010-08-11 22:01:17 +0000298 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000299 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000300}
301
302DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000303 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000304 ValueDecl *D,
305 const DeclarationNameInfo &NameInfo,
306 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000307 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000308 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000309 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000310 // Filter out cases where the found Decl is the same as the value refenenced.
311 if (D == FoundD)
312 FoundD = 0;
313
Douglas Gregora2813ce2009-10-23 18:54:35 +0000314 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000315 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000316 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000317 if (FoundD)
318 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000319 if (TemplateArgs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000320 Size += ASTTemplateArgumentListInfo::sizeFor(*TemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000321
Chris Lattner32488542010-10-30 05:14:06 +0000322 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth3aa81402011-05-01 23:48:14 +0000323 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
324 T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000325}
326
Chandler Carruth3aa81402011-05-01 23:48:14 +0000327DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000328 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000329 bool HasFoundDecl,
Douglas Gregordef03542011-02-04 12:01:24 +0000330 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000331 unsigned NumTemplateArgs) {
332 std::size_t Size = sizeof(DeclRefExpr);
333 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000334 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000335 if (HasFoundDecl)
336 Size += sizeof(NamedDecl *);
Douglas Gregordef03542011-02-04 12:01:24 +0000337 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000338 Size += ASTTemplateArgumentListInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000339
Chris Lattner32488542010-10-30 05:14:06 +0000340 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000341 return new (Mem) DeclRefExpr(EmptyShell());
342}
343
Douglas Gregora2813ce2009-10-23 18:54:35 +0000344SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000345 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000346 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000347 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000348 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000349 R.setEnd(getRAngleLoc());
350 return R;
351}
352
Anders Carlsson3a082d82009-09-08 18:24:21 +0000353// FIXME: Maybe this should use DeclPrinter with a special "print predefined
354// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000355std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
356 ASTContext &Context = CurrentDecl->getASTContext();
357
Anders Carlsson3a082d82009-09-08 18:24:21 +0000358 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000359 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000360 return FD->getNameAsString();
361
362 llvm::SmallString<256> Name;
363 llvm::raw_svector_ostream Out(Name);
364
365 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000366 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000367 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000368 if (MD->isStatic())
369 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000370 }
371
372 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000373
374 std::string Proto = FD->getQualifiedNameAsString(Policy);
375
John McCall183700f2009-09-21 23:43:11 +0000376 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000377 const FunctionProtoType *FT = 0;
378 if (FD->hasWrittenPrototype())
379 FT = dyn_cast<FunctionProtoType>(AFT);
380
381 Proto += "(";
382 if (FT) {
383 llvm::raw_string_ostream POut(Proto);
384 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
385 if (i) POut << ", ";
386 std::string Param;
387 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
388 POut << Param;
389 }
390
391 if (FT->isVariadic()) {
392 if (FD->getNumParams()) POut << ", ";
393 POut << "...";
394 }
395 }
396 Proto += ")";
397
Sam Weinig4eadcc52009-12-27 01:38:20 +0000398 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
399 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
400 if (ThisQuals.hasConst())
401 Proto += " const";
402 if (ThisQuals.hasVolatile())
403 Proto += " volatile";
404 }
405
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000406 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
407 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000408
409 Out << Proto;
410
411 Out.flush();
412 return Name.str().str();
413 }
414 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
415 llvm::SmallString<256> Name;
416 llvm::raw_svector_ostream Out(Name);
417 Out << (MD->isInstanceMethod() ? '-' : '+');
418 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000419
420 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
421 // a null check to avoid a crash.
422 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000423 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000424
Anders Carlsson3a082d82009-09-08 18:24:21 +0000425 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000426 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
427 Out << '(' << CID << ')';
428
Anders Carlsson3a082d82009-09-08 18:24:21 +0000429 Out << ' ';
430 Out << MD->getSelector().getAsString();
431 Out << ']';
432
433 Out.flush();
434 return Name.str().str();
435 }
436 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
437 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
438 return "top level";
439 }
440 return "";
441}
442
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000443void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
444 if (hasAllocation())
445 C.Deallocate(pVal);
446
447 BitWidth = Val.getBitWidth();
448 unsigned NumWords = Val.getNumWords();
449 const uint64_t* Words = Val.getRawData();
450 if (NumWords > 1) {
451 pVal = new (C) uint64_t[NumWords];
452 std::copy(Words, Words + NumWords, pVal);
453 } else if (NumWords == 1)
454 VAL = Words[0];
455 else
456 VAL = 0;
457}
458
459IntegerLiteral *
460IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
461 QualType type, SourceLocation l) {
462 return new (C) IntegerLiteral(C, V, type, l);
463}
464
465IntegerLiteral *
466IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
467 return new (C) IntegerLiteral(Empty);
468}
469
470FloatingLiteral *
471FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
472 bool isexact, QualType Type, SourceLocation L) {
473 return new (C) FloatingLiteral(C, V, isexact, Type, L);
474}
475
476FloatingLiteral *
477FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000478 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000479}
480
Chris Lattnerda8249e2008-06-07 22:13:43 +0000481/// getValueAsApproximateDouble - This returns the value as an inaccurate
482/// double. Note that this may cause loss of precision, but is useful for
483/// debugging dumps, etc.
484double FloatingLiteral::getValueAsApproximateDouble() const {
485 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000486 bool ignored;
487 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
488 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000489 return V.convertToDouble();
490}
491
Eli Friedmand97927d2012-01-06 20:42:20 +0000492int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
493 int CharByteWidth;
Eli Friedman64f45a22011-11-01 02:23:42 +0000494 switch(k) {
495 case Ascii:
496 case UTF8:
497 CharByteWidth = target.getCharWidth();
498 break;
499 case Wide:
500 CharByteWidth = target.getWCharWidth();
501 break;
502 case UTF16:
503 CharByteWidth = target.getChar16Width();
504 break;
505 case UTF32:
506 CharByteWidth = target.getChar32Width();
507 }
508 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
509 CharByteWidth /= 8;
510 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
511 && "character byte widths supported are 1, 2, and 4 only");
512 return CharByteWidth;
513}
514
Chris Lattner5f9e2722011-07-23 10:55:15 +0000515StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000516 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000517 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000518 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000519 // Allocate enough space for the StringLiteral plus an array of locations for
520 // any concatenated string tokens.
521 void *Mem = C.Allocate(sizeof(StringLiteral)+
522 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000523 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000524 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Reid Spencer5f016e22007-07-11 17:01:13 +0000526 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000527 SL->setString(C,Str,Kind,Pascal);
528
Chris Lattner2085fd62009-02-18 06:40:38 +0000529 SL->TokLocs[0] = Loc[0];
530 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000531
Chris Lattner726e1682009-02-18 05:49:11 +0000532 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000533 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
534 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000535}
536
Douglas Gregor673ecd62009-04-15 16:35:07 +0000537StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
538 void *Mem = C.Allocate(sizeof(StringLiteral)+
539 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000540 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000541 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000542 SL->CharByteWidth = 0;
543 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000544 SL->NumConcatenated = NumStrs;
545 return SL;
546}
547
Eli Friedman64f45a22011-11-01 02:23:42 +0000548void StringLiteral::setString(ASTContext &C, StringRef Str,
549 StringKind Kind, bool IsPascal) {
550 //FIXME: we assume that the string data comes from a target that uses the same
551 // code unit size and endianess for the type of string.
552 this->Kind = Kind;
553 this->IsPascal = IsPascal;
554
555 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
556 assert((Str.size()%CharByteWidth == 0)
557 && "size of data must be multiple of CharByteWidth");
558 Length = Str.size()/CharByteWidth;
559
560 switch(CharByteWidth) {
561 case 1: {
562 char *AStrData = new (C) char[Length];
563 std::memcpy(AStrData,Str.data(),Str.size());
564 StrData.asChar = AStrData;
565 break;
566 }
567 case 2: {
568 uint16_t *AStrData = new (C) uint16_t[Length];
569 std::memcpy(AStrData,Str.data(),Str.size());
570 StrData.asUInt16 = AStrData;
571 break;
572 }
573 case 4: {
574 uint32_t *AStrData = new (C) uint32_t[Length];
575 std::memcpy(AStrData,Str.data(),Str.size());
576 StrData.asUInt32 = AStrData;
577 break;
578 }
579 default:
580 assert(false && "unsupported CharByteWidth");
581 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000582}
583
Chris Lattner08f92e32010-11-17 07:37:15 +0000584/// getLocationOfByte - Return a source location that points to the specified
585/// byte of this string literal.
586///
587/// Strings are amazingly complex. They can be formed from multiple tokens and
588/// can have escape sequences in them in addition to the usual trigraph and
589/// escaped newline business. This routine handles this complexity.
590///
591SourceLocation StringLiteral::
592getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
593 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregor5cee1192011-07-27 05:40:30 +0000594 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
595
Chris Lattner08f92e32010-11-17 07:37:15 +0000596 // Loop over all of the tokens in this string until we find the one that
597 // contains the byte we're looking for.
598 unsigned TokNo = 0;
599 while (1) {
600 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
601 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
602
603 // Get the spelling of the string so that we can get the data that makes up
604 // the string literal, not the identifier for the macro it is potentially
605 // expanded through.
606 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
607
608 // Re-lex the token to get its length and original spelling.
609 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
610 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000611 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000612 if (Invalid)
613 return StrTokSpellingLoc;
614
615 const char *StrData = Buffer.data()+LocInfo.second;
616
617 // Create a langops struct and enable trigraphs. This is sufficient for
618 // relexing tokens.
619 LangOptions LangOpts;
620 LangOpts.Trigraphs = true;
621
622 // Create a lexer starting at the beginning of this token.
623 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
624 Buffer.end());
625 Token TheTok;
626 TheLexer.LexFromRawLexer(TheTok);
627
628 // Use the StringLiteralParser to compute the length of the string in bytes.
629 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
630 unsigned TokNumBytes = SLP.GetStringLength();
631
632 // If the byte is in this token, return the location of the byte.
633 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000634 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000635 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
636
637 // Now that we know the offset of the token in the spelling, use the
638 // preprocessor to get the offset in the original source.
639 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
640 }
641
642 // Move to the next string token.
643 ++TokNo;
644 ByteNo -= TokNumBytes;
645 }
646}
647
648
649
Reid Spencer5f016e22007-07-11 17:01:13 +0000650/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
651/// corresponds to, e.g. "sizeof" or "[pre]++".
652const char *UnaryOperator::getOpcodeStr(Opcode Op) {
653 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000654 case UO_PostInc: return "++";
655 case UO_PostDec: return "--";
656 case UO_PreInc: return "++";
657 case UO_PreDec: return "--";
658 case UO_AddrOf: return "&";
659 case UO_Deref: return "*";
660 case UO_Plus: return "+";
661 case UO_Minus: return "-";
662 case UO_Not: return "~";
663 case UO_LNot: return "!";
664 case UO_Real: return "__real";
665 case UO_Imag: return "__imag";
666 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000668 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000669}
670
John McCall2de56d12010-08-25 11:45:40 +0000671UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000672UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
673 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000674 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000675 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
676 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
677 case OO_Amp: return UO_AddrOf;
678 case OO_Star: return UO_Deref;
679 case OO_Plus: return UO_Plus;
680 case OO_Minus: return UO_Minus;
681 case OO_Tilde: return UO_Not;
682 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000683 }
684}
685
686OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
687 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000688 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
689 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
690 case UO_AddrOf: return OO_Amp;
691 case UO_Deref: return OO_Star;
692 case UO_Plus: return OO_Plus;
693 case UO_Minus: return OO_Minus;
694 case UO_Not: return OO_Tilde;
695 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000696 default: return OO_None;
697 }
698}
699
700
Reid Spencer5f016e22007-07-11 17:01:13 +0000701//===----------------------------------------------------------------------===//
702// Postfix Operators.
703//===----------------------------------------------------------------------===//
704
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000705CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
706 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000707 SourceLocation rparenloc)
708 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000709 fn->isTypeDependent(),
710 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000711 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000712 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000713 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000714
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000715 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000716 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000717 for (unsigned i = 0; i != numargs; ++i) {
718 if (args[i]->isTypeDependent())
719 ExprBits.TypeDependent = true;
720 if (args[i]->isValueDependent())
721 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000722 if (args[i]->isInstantiationDependent())
723 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000724 if (args[i]->containsUnexpandedParameterPack())
725 ExprBits.ContainsUnexpandedParameterPack = true;
726
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000727 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000728 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000729
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000730 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000731 RParenLoc = rparenloc;
732}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000733
Ted Kremenek668bf912009-02-09 20:51:47 +0000734CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000735 QualType t, ExprValueKind VK, SourceLocation rparenloc)
736 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000737 fn->isTypeDependent(),
738 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000739 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000740 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000741 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000742
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000743 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000744 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000745 for (unsigned i = 0; i != numargs; ++i) {
746 if (args[i]->isTypeDependent())
747 ExprBits.TypeDependent = true;
748 if (args[i]->isValueDependent())
749 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000750 if (args[i]->isInstantiationDependent())
751 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000752 if (args[i]->containsUnexpandedParameterPack())
753 ExprBits.ContainsUnexpandedParameterPack = true;
754
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000755 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000756 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000757
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000758 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 RParenLoc = rparenloc;
760}
761
Mike Stump1eb44332009-09-09 15:08:12 +0000762CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
763 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000764 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000765 SubExprs = new (C) Stmt*[PREARGS_START];
766 CallExprBits.NumPreArgs = 0;
767}
768
769CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
770 EmptyShell Empty)
771 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
772 // FIXME: Why do we allocate this?
773 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
774 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000775}
776
Nuno Lopesd20254f2009-12-20 23:11:08 +0000777Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000778 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000779
780 while (SubstNonTypeTemplateParmExpr *NTTP
781 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
782 CEE = NTTP->getReplacement()->IgnoreParenCasts();
783 }
784
Sebastian Redl20012152010-09-10 20:55:30 +0000785 // If we're calling a dereference, look at the pointer instead.
786 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
787 if (BO->isPtrMemOp())
788 CEE = BO->getRHS()->IgnoreParenCasts();
789 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
790 if (UO->getOpcode() == UO_Deref)
791 CEE = UO->getSubExpr()->IgnoreParenCasts();
792 }
Chris Lattner6346f962009-07-17 15:46:27 +0000793 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000794 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000795 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
796 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000797
798 return 0;
799}
800
Nuno Lopesd20254f2009-12-20 23:11:08 +0000801FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000802 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000803}
804
Chris Lattnerd18b3292007-12-28 05:25:02 +0000805/// setNumArgs - This changes the number of arguments present in this call.
806/// Any orphaned expressions are deleted by this, and any new operands are set
807/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000808void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000809 // No change, just return.
810 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Chris Lattnerd18b3292007-12-28 05:25:02 +0000812 // If shrinking # arguments, just delete the extras and forgot them.
813 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000814 this->NumArgs = NumArgs;
815 return;
816 }
817
818 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000819 unsigned NumPreArgs = getNumPreArgs();
820 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000821 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000822 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000823 NewSubExprs[i] = SubExprs[i];
824 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000825 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
826 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +0000827 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000828
Douglas Gregor88c9a462009-04-17 21:46:47 +0000829 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +0000830 SubExprs = NewSubExprs;
831 this->NumArgs = NumArgs;
832}
833
Chris Lattnercb888962008-10-06 05:00:53 +0000834/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
835/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +0000836unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000837 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +0000838 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000839 // ImplicitCastExpr.
840 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
841 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +0000842 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000843
Steve Naroffc4f8e8b2008-01-31 01:07:12 +0000844 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
845 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +0000846 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Anders Carlssonbcba2012008-01-31 02:13:57 +0000848 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
849 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +0000850 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000851
Douglas Gregor4fcd3992008-11-21 15:30:19 +0000852 if (!FDecl->getIdentifier())
853 return 0;
854
Douglas Gregor7814e6d2009-09-12 00:22:50 +0000855 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +0000856}
Anders Carlssonbcba2012008-01-31 02:13:57 +0000857
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000858QualType CallExpr::getCallReturnType() const {
859 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000860 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000861 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +0000862 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000863 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +0000864 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
865 // This should never be overloaded and so should never return null.
866 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000867
John McCall864c0412011-04-26 20:42:42 +0000868 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +0000869 return FnType->getResultType();
870}
Chris Lattnercb888962008-10-06 05:00:53 +0000871
John McCall2882eca2011-02-21 06:23:05 +0000872SourceRange CallExpr::getSourceRange() const {
873 if (isa<CXXOperatorCallExpr>(this))
874 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
875
876 SourceLocation begin = getCallee()->getLocStart();
877 if (begin.isInvalid() && getNumArgs() > 0)
878 begin = getArg(0)->getLocStart();
879 SourceLocation end = getRParenLoc();
880 if (end.isInvalid() && getNumArgs() > 0)
881 end = getArg(getNumArgs() - 1)->getLocEnd();
882 return SourceRange(begin, end);
883}
884
Sean Huntc3021132010-05-05 15:23:54 +0000885OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000886 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +0000887 TypeSourceInfo *tsi,
888 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000889 Expr** exprsPtr, unsigned numExprs,
890 SourceLocation RParenLoc) {
891 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +0000892 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000893 sizeof(Expr*) * numExprs);
894
895 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
896 exprsPtr, numExprs, RParenLoc);
897}
898
899OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
900 unsigned numComps, unsigned numExprs) {
901 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
902 sizeof(OffsetOfNode) * numComps +
903 sizeof(Expr*) * numExprs);
904 return new (Mem) OffsetOfExpr(numComps, numExprs);
905}
906
Sean Huntc3021132010-05-05 15:23:54 +0000907OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000908 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +0000909 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000910 Expr** exprsPtr, unsigned numExprs,
911 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +0000912 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
913 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000914 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000915 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000916 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +0000917 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
918 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000919{
920 for(unsigned i = 0; i < numComps; ++i) {
921 setComponent(i, compsPtr[i]);
922 }
Sean Huntc3021132010-05-05 15:23:54 +0000923
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000924 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000925 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
926 ExprBits.ValueDependent = true;
927 if (exprsPtr[i]->containsUnexpandedParameterPack())
928 ExprBits.ContainsUnexpandedParameterPack = true;
929
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000930 setIndexExpr(i, exprsPtr[i]);
931 }
932}
933
934IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
935 assert(getKind() == Field || getKind() == Identifier);
936 if (getKind() == Field)
937 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +0000938
Douglas Gregor8ecdb652010-04-28 22:16:22 +0000939 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
940}
941
Mike Stump1eb44332009-09-09 15:08:12 +0000942MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000943 NestedNameSpecifierLoc QualifierLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +0000944 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +0000945 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +0000946 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +0000947 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +0000948 QualType ty,
949 ExprValueKind vk,
950 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000951 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +0000952
Douglas Gregor40d96a62011-02-28 21:54:11 +0000953 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +0000954 founddecl.getDecl() != memberdecl ||
955 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +0000956 if (hasQualOrFound)
957 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000958
John McCalld5532b62009-11-23 01:53:49 +0000959 if (targs)
Argyrios Kyrtzidisb0c3e092011-09-22 20:07:03 +0000960 Size += ASTTemplateArgumentListInfo::sizeFor(*targs);
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Chris Lattner32488542010-10-30 05:14:06 +0000962 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +0000963 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
964 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +0000965
966 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +0000967 // FIXME: Wrong. We should be looking at the member declaration we found.
968 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +0000969 E->setValueDependent(true);
970 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +0000971 E->setInstantiationDependent(true);
972 }
973 else if (QualifierLoc &&
974 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
975 E->setInstantiationDependent(true);
976
John McCall6bb80172010-03-30 21:47:33 +0000977 E->HasQualifierOrFoundDecl = true;
978
979 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +0000980 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +0000981 NQ->FoundDecl = founddecl;
982 }
983
984 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +0000985 bool Dependent = false;
986 bool InstantiationDependent = false;
987 bool ContainsUnexpandedParameterPack = false;
John McCall6bb80172010-03-30 21:47:33 +0000988 E->HasExplicitTemplateArgumentList = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000989 E->getExplicitTemplateArgs().initializeFrom(*targs, Dependent,
990 InstantiationDependent,
991 ContainsUnexpandedParameterPack);
992 if (InstantiationDependent)
993 E->setInstantiationDependent(true);
John McCall6bb80172010-03-30 21:47:33 +0000994 }
995
996 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +0000997}
998
Douglas Gregor75e85042011-03-02 21:06:53 +0000999SourceRange MemberExpr::getSourceRange() const {
1000 SourceLocation StartLoc;
1001 if (isImplicitAccess()) {
1002 if (hasQualifier())
1003 StartLoc = getQualifierLoc().getBeginLoc();
1004 else
1005 StartLoc = MemberLoc;
1006 } else {
1007 // FIXME: We don't want this to happen. Rather, we should be able to
1008 // detect all kinds of implicit accesses more cleanly.
1009 StartLoc = getBase()->getLocStart();
1010 if (StartLoc.isInvalid())
1011 StartLoc = MemberLoc;
1012 }
1013
1014 SourceLocation EndLoc =
1015 HasExplicitTemplateArgumentList? getRAngleLoc()
1016 : getMemberNameInfo().getEndLoc();
1017
1018 return SourceRange(StartLoc, EndLoc);
1019}
1020
John McCall1d9b3b22011-09-09 05:25:32 +00001021void CastExpr::CheckCastConsistency() const {
1022 switch (getCastKind()) {
1023 case CK_DerivedToBase:
1024 case CK_UncheckedDerivedToBase:
1025 case CK_DerivedToBaseMemberPointer:
1026 case CK_BaseToDerived:
1027 case CK_BaseToDerivedMemberPointer:
1028 assert(!path_empty() && "Cast kind should have a base path!");
1029 break;
1030
1031 case CK_CPointerToObjCPointerCast:
1032 assert(getType()->isObjCObjectPointerType());
1033 assert(getSubExpr()->getType()->isPointerType());
1034 goto CheckNoBasePath;
1035
1036 case CK_BlockPointerToObjCPointerCast:
1037 assert(getType()->isObjCObjectPointerType());
1038 assert(getSubExpr()->getType()->isBlockPointerType());
1039 goto CheckNoBasePath;
1040
1041 case CK_BitCast:
1042 // Arbitrary casts to C pointer types count as bitcasts.
1043 // Otherwise, we should only have block and ObjC pointer casts
1044 // here if they stay within the type kind.
1045 if (!getType()->isPointerType()) {
1046 assert(getType()->isObjCObjectPointerType() ==
1047 getSubExpr()->getType()->isObjCObjectPointerType());
1048 assert(getType()->isBlockPointerType() ==
1049 getSubExpr()->getType()->isBlockPointerType());
1050 }
1051 goto CheckNoBasePath;
1052
1053 case CK_AnyPointerToBlockPointerCast:
1054 assert(getType()->isBlockPointerType());
1055 assert(getSubExpr()->getType()->isAnyPointerType() &&
1056 !getSubExpr()->getType()->isBlockPointerType());
1057 goto CheckNoBasePath;
1058
1059 // These should not have an inheritance path.
1060 case CK_Dynamic:
1061 case CK_ToUnion:
1062 case CK_ArrayToPointerDecay:
1063 case CK_FunctionToPointerDecay:
1064 case CK_NullToMemberPointer:
1065 case CK_NullToPointer:
1066 case CK_ConstructorConversion:
1067 case CK_IntegralToPointer:
1068 case CK_PointerToIntegral:
1069 case CK_ToVoid:
1070 case CK_VectorSplat:
1071 case CK_IntegralCast:
1072 case CK_IntegralToFloating:
1073 case CK_FloatingToIntegral:
1074 case CK_FloatingCast:
1075 case CK_ObjCObjectLValueCast:
1076 case CK_FloatingRealToComplex:
1077 case CK_FloatingComplexToReal:
1078 case CK_FloatingComplexCast:
1079 case CK_FloatingComplexToIntegralComplex:
1080 case CK_IntegralRealToComplex:
1081 case CK_IntegralComplexToReal:
1082 case CK_IntegralComplexCast:
1083 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001084 case CK_ARCProduceObject:
1085 case CK_ARCConsumeObject:
1086 case CK_ARCReclaimReturnedObject:
1087 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001088 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1089 goto CheckNoBasePath;
1090
1091 case CK_Dependent:
1092 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001093 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001094 case CK_AtomicToNonAtomic:
1095 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001096 case CK_PointerToBoolean:
1097 case CK_IntegralToBoolean:
1098 case CK_FloatingToBoolean:
1099 case CK_MemberPointerToBoolean:
1100 case CK_FloatingComplexToBoolean:
1101 case CK_IntegralComplexToBoolean:
1102 case CK_LValueBitCast: // -> bool&
1103 case CK_UserDefinedConversion: // operator bool()
1104 CheckNoBasePath:
1105 assert(path_empty() && "Cast kind should not have a base path!");
1106 break;
1107 }
1108}
1109
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001110const char *CastExpr::getCastKindName() const {
1111 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001112 case CK_Dependent:
1113 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001114 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001115 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001116 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001117 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001118 case CK_LValueToRValue:
1119 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001120 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001121 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001122 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001123 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001124 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001125 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001126 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001127 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001128 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001129 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001130 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001131 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001132 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001133 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001134 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001135 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001136 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001137 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001138 case CK_NullToPointer:
1139 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001140 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001141 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001142 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001143 return "DerivedToBaseMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001144 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001145 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001146 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001147 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001148 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001149 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001150 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001151 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001152 case CK_PointerToBoolean:
1153 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001154 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001155 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001156 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001157 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001158 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001159 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001160 case CK_IntegralToBoolean:
1161 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001162 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001163 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001164 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001165 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001166 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001167 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001168 case CK_FloatingToBoolean:
1169 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001170 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001171 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001172 case CK_CPointerToObjCPointerCast:
1173 return "CPointerToObjCPointerCast";
1174 case CK_BlockPointerToObjCPointerCast:
1175 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001176 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001177 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001178 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001179 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001180 case CK_FloatingRealToComplex:
1181 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001182 case CK_FloatingComplexToReal:
1183 return "FloatingComplexToReal";
1184 case CK_FloatingComplexToBoolean:
1185 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001186 case CK_FloatingComplexCast:
1187 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001188 case CK_FloatingComplexToIntegralComplex:
1189 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001190 case CK_IntegralRealToComplex:
1191 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001192 case CK_IntegralComplexToReal:
1193 return "IntegralComplexToReal";
1194 case CK_IntegralComplexToBoolean:
1195 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001196 case CK_IntegralComplexCast:
1197 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001198 case CK_IntegralComplexToFloatingComplex:
1199 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001200 case CK_ARCConsumeObject:
1201 return "ARCConsumeObject";
1202 case CK_ARCProduceObject:
1203 return "ARCProduceObject";
1204 case CK_ARCReclaimReturnedObject:
1205 return "ARCReclaimReturnedObject";
1206 case CK_ARCExtendBlockObject:
1207 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001208 case CK_AtomicToNonAtomic:
1209 return "AtomicToNonAtomic";
1210 case CK_NonAtomicToAtomic:
1211 return "NonAtomicToAtomic";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001212 }
Mike Stump1eb44332009-09-09 15:08:12 +00001213
John McCall2bb5d002010-11-13 09:02:35 +00001214 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001215}
1216
Douglas Gregor6eef5192009-12-14 19:27:10 +00001217Expr *CastExpr::getSubExprAsWritten() {
1218 Expr *SubExpr = 0;
1219 CastExpr *E = this;
1220 do {
1221 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001222
1223 // Skip through reference binding to temporary.
1224 if (MaterializeTemporaryExpr *Materialize
1225 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1226 SubExpr = Materialize->GetTemporaryExpr();
1227
Douglas Gregor6eef5192009-12-14 19:27:10 +00001228 // Skip any temporary bindings; they're implicit.
1229 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1230 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001231
Douglas Gregor6eef5192009-12-14 19:27:10 +00001232 // Conversions by constructor and conversion functions have a
1233 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001234 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001235 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001236 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001237 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001238
Douglas Gregor6eef5192009-12-14 19:27:10 +00001239 // If the subexpression we're left with is an implicit cast, look
1240 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001241 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1242
Douglas Gregor6eef5192009-12-14 19:27:10 +00001243 return SubExpr;
1244}
1245
John McCallf871d0c2010-08-07 06:22:56 +00001246CXXBaseSpecifier **CastExpr::path_buffer() {
1247 switch (getStmtClass()) {
1248#define ABSTRACT_STMT(x)
1249#define CASTEXPR(Type, Base) \
1250 case Stmt::Type##Class: \
1251 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1252#define STMT(Type, Base)
1253#include "clang/AST/StmtNodes.inc"
1254 default:
1255 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001256 }
1257}
1258
1259void CastExpr::setCastPath(const CXXCastPath &Path) {
1260 assert(Path.size() == path_size());
1261 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1262}
1263
1264ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1265 CastKind Kind, Expr *Operand,
1266 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001267 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001268 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1269 void *Buffer =
1270 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1271 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001272 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001273 if (PathSize) E->setCastPath(*BasePath);
1274 return E;
1275}
1276
1277ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1278 unsigned PathSize) {
1279 void *Buffer =
1280 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1281 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1282}
1283
1284
1285CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001286 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001287 const CXXCastPath *BasePath,
1288 TypeSourceInfo *WrittenTy,
1289 SourceLocation L, SourceLocation R) {
1290 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1291 void *Buffer =
1292 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1293 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001294 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001295 if (PathSize) E->setCastPath(*BasePath);
1296 return E;
1297}
1298
1299CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1300 void *Buffer =
1301 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1302 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1303}
1304
Reid Spencer5f016e22007-07-11 17:01:13 +00001305/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1306/// corresponds to, e.g. "<<=".
1307const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1308 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001309 case BO_PtrMemD: return ".*";
1310 case BO_PtrMemI: return "->*";
1311 case BO_Mul: return "*";
1312 case BO_Div: return "/";
1313 case BO_Rem: return "%";
1314 case BO_Add: return "+";
1315 case BO_Sub: return "-";
1316 case BO_Shl: return "<<";
1317 case BO_Shr: return ">>";
1318 case BO_LT: return "<";
1319 case BO_GT: return ">";
1320 case BO_LE: return "<=";
1321 case BO_GE: return ">=";
1322 case BO_EQ: return "==";
1323 case BO_NE: return "!=";
1324 case BO_And: return "&";
1325 case BO_Xor: return "^";
1326 case BO_Or: return "|";
1327 case BO_LAnd: return "&&";
1328 case BO_LOr: return "||";
1329 case BO_Assign: return "=";
1330 case BO_MulAssign: return "*=";
1331 case BO_DivAssign: return "/=";
1332 case BO_RemAssign: return "%=";
1333 case BO_AddAssign: return "+=";
1334 case BO_SubAssign: return "-=";
1335 case BO_ShlAssign: return "<<=";
1336 case BO_ShrAssign: return ">>=";
1337 case BO_AndAssign: return "&=";
1338 case BO_XorAssign: return "^=";
1339 case BO_OrAssign: return "|=";
1340 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001342
David Blaikie30263482012-01-20 21:50:17 +00001343 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001344}
1345
John McCall2de56d12010-08-25 11:45:40 +00001346BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001347BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1348 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001349 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001350 case OO_Plus: return BO_Add;
1351 case OO_Minus: return BO_Sub;
1352 case OO_Star: return BO_Mul;
1353 case OO_Slash: return BO_Div;
1354 case OO_Percent: return BO_Rem;
1355 case OO_Caret: return BO_Xor;
1356 case OO_Amp: return BO_And;
1357 case OO_Pipe: return BO_Or;
1358 case OO_Equal: return BO_Assign;
1359 case OO_Less: return BO_LT;
1360 case OO_Greater: return BO_GT;
1361 case OO_PlusEqual: return BO_AddAssign;
1362 case OO_MinusEqual: return BO_SubAssign;
1363 case OO_StarEqual: return BO_MulAssign;
1364 case OO_SlashEqual: return BO_DivAssign;
1365 case OO_PercentEqual: return BO_RemAssign;
1366 case OO_CaretEqual: return BO_XorAssign;
1367 case OO_AmpEqual: return BO_AndAssign;
1368 case OO_PipeEqual: return BO_OrAssign;
1369 case OO_LessLess: return BO_Shl;
1370 case OO_GreaterGreater: return BO_Shr;
1371 case OO_LessLessEqual: return BO_ShlAssign;
1372 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1373 case OO_EqualEqual: return BO_EQ;
1374 case OO_ExclaimEqual: return BO_NE;
1375 case OO_LessEqual: return BO_LE;
1376 case OO_GreaterEqual: return BO_GE;
1377 case OO_AmpAmp: return BO_LAnd;
1378 case OO_PipePipe: return BO_LOr;
1379 case OO_Comma: return BO_Comma;
1380 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001381 }
1382}
1383
1384OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1385 static const OverloadedOperatorKind OverOps[] = {
1386 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1387 OO_Star, OO_Slash, OO_Percent,
1388 OO_Plus, OO_Minus,
1389 OO_LessLess, OO_GreaterGreater,
1390 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1391 OO_EqualEqual, OO_ExclaimEqual,
1392 OO_Amp,
1393 OO_Caret,
1394 OO_Pipe,
1395 OO_AmpAmp,
1396 OO_PipePipe,
1397 OO_Equal, OO_StarEqual,
1398 OO_SlashEqual, OO_PercentEqual,
1399 OO_PlusEqual, OO_MinusEqual,
1400 OO_LessLessEqual, OO_GreaterGreaterEqual,
1401 OO_AmpEqual, OO_CaretEqual,
1402 OO_PipeEqual,
1403 OO_Comma
1404 };
1405 return OverOps[Opc];
1406}
1407
Ted Kremenek709210f2010-04-13 23:39:13 +00001408InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001409 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001410 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001411 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001412 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001413 InitExprs(C, numInits),
Mike Stump1eb44332009-09-09 15:08:12 +00001414 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidis4423ac02011-04-21 00:27:41 +00001415 HadArrayRangeDesignator(false)
Sean Huntc3021132010-05-05 15:23:54 +00001416{
Ted Kremenekba7bc552010-02-19 01:50:18 +00001417 for (unsigned I = 0; I != numInits; ++I) {
1418 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001419 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001420 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001421 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001422 if (initExprs[I]->isInstantiationDependent())
1423 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001424 if (initExprs[I]->containsUnexpandedParameterPack())
1425 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001426 }
Sean Huntc3021132010-05-05 15:23:54 +00001427
Ted Kremenek709210f2010-04-13 23:39:13 +00001428 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001429}
Reid Spencer5f016e22007-07-11 17:01:13 +00001430
Ted Kremenek709210f2010-04-13 23:39:13 +00001431void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001432 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001433 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001434}
1435
Ted Kremenek709210f2010-04-13 23:39:13 +00001436void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001437 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001438}
1439
Ted Kremenek709210f2010-04-13 23:39:13 +00001440Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001441 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001442 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001443 InitExprs.back() = expr;
1444 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001445 }
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Douglas Gregor4c678342009-01-28 21:54:33 +00001447 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1448 InitExprs[Init] = expr;
1449 return Result;
1450}
1451
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001452void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001453 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001454 ArrayFillerOrUnionFieldInit = filler;
1455 // Fill out any "holes" in the array due to designated initializers.
1456 Expr **inits = getInits();
1457 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1458 if (inits[i] == 0)
1459 inits[i] = filler;
1460}
1461
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001462SourceRange InitListExpr::getSourceRange() const {
1463 if (SyntacticForm)
1464 return SyntacticForm->getSourceRange();
1465 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1466 if (Beg.isInvalid()) {
1467 // Find the first non-null initializer.
1468 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1469 E = InitExprs.end();
1470 I != E; ++I) {
1471 if (Stmt *S = *I) {
1472 Beg = S->getLocStart();
1473 break;
1474 }
1475 }
1476 }
1477 if (End.isInvalid()) {
1478 // Find the first non-null initializer from the end.
1479 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1480 E = InitExprs.rend();
1481 I != E; ++I) {
1482 if (Stmt *S = *I) {
1483 End = S->getSourceRange().getEnd();
1484 break;
1485 }
1486 }
1487 }
1488 return SourceRange(Beg, End);
1489}
1490
Steve Naroffbfdcae62008-09-04 15:31:07 +00001491/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001492///
1493const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenek6217b802009-07-29 21:53:49 +00001494 return getType()->getAs<BlockPointerType>()->
John McCall183700f2009-09-21 23:43:11 +00001495 getPointeeType()->getAs<FunctionType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001496}
1497
Mike Stump1eb44332009-09-09 15:08:12 +00001498SourceLocation BlockExpr::getCaretLocation() const {
1499 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001500}
Mike Stump1eb44332009-09-09 15:08:12 +00001501const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001502 return TheBlock->getBody();
1503}
Mike Stump1eb44332009-09-09 15:08:12 +00001504Stmt *BlockExpr::getBody() {
1505 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001506}
Steve Naroff56ee6892008-10-08 17:01:13 +00001507
1508
Reid Spencer5f016e22007-07-11 17:01:13 +00001509//===----------------------------------------------------------------------===//
1510// Generic Expression Routines
1511//===----------------------------------------------------------------------===//
1512
Chris Lattner026dc962009-02-14 07:37:35 +00001513/// isUnusedResultAWarning - Return true if this immediate expression should
1514/// be warned about if the result is unused. If so, fill in Loc and Ranges
1515/// with location to warn on and the source range[s] to report with the
1516/// warning.
1517bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stumpdf317bf2009-11-03 23:25:48 +00001518 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001519 // Don't warn if the expr is type dependent. The type could end up
1520 // instantiating to void.
1521 if (isTypeDependent())
1522 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 switch (getStmtClass()) {
1525 default:
John McCall0faede62010-03-12 07:11:26 +00001526 if (getType()->isVoidType())
1527 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001528 Loc = getExprLoc();
1529 R1 = getSourceRange();
1530 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001531 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001532 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stumpdf317bf2009-11-03 23:25:48 +00001533 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001534 case GenericSelectionExprClass:
1535 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1536 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001537 case UnaryOperatorClass: {
1538 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001539
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 switch (UO->getOpcode()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001541 default: break;
John McCall2de56d12010-08-25 11:45:40 +00001542 case UO_PostInc:
1543 case UO_PostDec:
1544 case UO_PreInc:
1545 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001546 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001547 case UO_Deref:
Reid Spencer5f016e22007-07-11 17:01:13 +00001548 // Dereferencing a volatile pointer is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001549 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001550 return false;
1551 break;
John McCall2de56d12010-08-25 11:45:40 +00001552 case UO_Real:
1553 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001554 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001555 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1556 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001557 return false;
1558 break;
John McCall2de56d12010-08-25 11:45:40 +00001559 case UO_Extension:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001560 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001561 }
Chris Lattner026dc962009-02-14 07:37:35 +00001562 Loc = UO->getOperatorLoc();
1563 R1 = UO->getSubExpr()->getSourceRange();
1564 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001566 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001567 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001568 switch (BO->getOpcode()) {
1569 default:
1570 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001571 // Consider the RHS of comma for side effects. LHS was checked by
1572 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001573 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001574 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1575 // lvalue-ness) of an assignment written in a macro.
1576 if (IntegerLiteral *IE =
1577 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1578 if (IE->getValue() == 0)
1579 return false;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001580 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1581 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001582 case BO_LAnd:
1583 case BO_LOr:
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001584 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1585 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1586 return false;
1587 break;
John McCallbf0ee352010-02-16 04:10:53 +00001588 }
Chris Lattner026dc962009-02-14 07:37:35 +00001589 if (BO->isAssignmentOp())
1590 return false;
1591 Loc = BO->getOperatorLoc();
1592 R1 = BO->getLHS()->getSourceRange();
1593 R2 = BO->getRHS()->getSourceRange();
1594 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001595 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001596 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001597 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001598 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001599 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001600
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001601 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001602 // If only one of the LHS or RHS is a warning, the operator might
1603 // be being used for control flow. Only warn if both the LHS and
1604 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001605 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001606 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1607 return false;
1608 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001609 return true;
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001610 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001611 }
1612
Reid Spencer5f016e22007-07-11 17:01:13 +00001613 case MemberExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001614 // If the base pointer or element is to a volatile pointer/field, accessing
1615 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001616 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001617 return false;
1618 Loc = cast<MemberExpr>(this)->getMemberLoc();
1619 R1 = SourceRange(Loc, Loc);
1620 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1621 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001622
Reid Spencer5f016e22007-07-11 17:01:13 +00001623 case ArraySubscriptExprClass:
1624 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner026dc962009-02-14 07:37:35 +00001625 // it is a side effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001626 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001627 return false;
1628 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1629 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1630 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1631 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001632
Chandler Carruth9b106832011-08-17 09:49:44 +00001633 case CXXOperatorCallExprClass: {
1634 // We warn about operator== and operator!= even when user-defined operator
1635 // overloads as there is no reasonable way to define these such that they
1636 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1637 // warning: these operators are commonly typo'ed, and so warning on them
1638 // provides additional value as well. If this list is updated,
1639 // DiagnoseUnusedComparison should be as well.
1640 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1641 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001642 Op->getOperator() == OO_ExclaimEqual) {
1643 Loc = Op->getOperatorLoc();
1644 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001645 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001646 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001647
1648 // Fallthrough for generic call handling.
1649 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001650 case CallExprClass:
Eli Friedman852871a2009-04-29 16:35:53 +00001651 case CXXMemberCallExprClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001652 // If this is a direct call, get the callee.
1653 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001654 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001655 // If the callee has attribute pure, const, or warn_unused_result, warn
1656 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001657 //
1658 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1659 // updated to match for QoI.
1660 if (FD->getAttr<WarnUnusedResultAttr>() ||
1661 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1662 Loc = CE->getCallee()->getLocStart();
1663 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001665 if (unsigned NumArgs = CE->getNumArgs())
1666 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1667 CE->getArg(NumArgs-1)->getLocEnd());
1668 return true;
1669 }
Chris Lattner026dc962009-02-14 07:37:35 +00001670 }
1671 return false;
1672 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001673
1674 case CXXTemporaryObjectExprClass:
1675 case CXXConstructExprClass:
1676 return false;
1677
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001678 case ObjCMessageExprClass: {
1679 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCallf85e1932011-06-15 23:02:42 +00001680 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1681 ME->isInstanceMessage() &&
1682 !ME->getType()->isVoidType() &&
1683 ME->getSelector().getIdentifierInfoForSlot(0) &&
1684 ME->getSelector().getIdentifierInfoForSlot(0)
1685 ->getName().startswith("init")) {
1686 Loc = getExprLoc();
1687 R1 = ME->getSourceRange();
1688 return true;
1689 }
1690
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001691 const ObjCMethodDecl *MD = ME->getMethodDecl();
1692 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1693 Loc = getExprLoc();
1694 return true;
1695 }
Chris Lattner026dc962009-02-14 07:37:35 +00001696 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001697 }
Mike Stump1eb44332009-09-09 15:08:12 +00001698
John McCall12f78a62010-12-02 01:19:52 +00001699 case ObjCPropertyRefExprClass:
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001700 Loc = getExprLoc();
1701 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001702 return true;
John McCall12f78a62010-12-02 01:19:52 +00001703
John McCall4b9c2d22011-11-06 09:01:30 +00001704 case PseudoObjectExprClass: {
1705 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1706
1707 // Only complain about things that have the form of a getter.
1708 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1709 isa<BinaryOperator>(PO->getSyntacticForm()))
1710 return false;
1711
1712 Loc = getExprLoc();
1713 R1 = getSourceRange();
1714 return true;
1715 }
1716
Chris Lattner611b2ec2008-07-26 19:51:01 +00001717 case StmtExprClass: {
1718 // Statement exprs don't logically have side effects themselves, but are
1719 // sometimes used in macros in ways that give them a type that is unused.
1720 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1721 // however, if the result of the stmt expr is dead, we don't want to emit a
1722 // warning.
1723 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001724 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001725 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stumpdf317bf2009-11-03 23:25:48 +00001726 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001727 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1728 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1729 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1730 }
Mike Stump1eb44332009-09-09 15:08:12 +00001731
John McCall0faede62010-03-12 07:11:26 +00001732 if (getType()->isVoidType())
1733 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001734 Loc = cast<StmtExpr>(this)->getLParenLoc();
1735 R1 = getSourceRange();
1736 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001737 }
Douglas Gregor6eec8e82008-10-28 15:36:24 +00001738 case CStyleCastExprClass:
Chris Lattnerfb846642009-07-28 18:25:28 +00001739 // If this is an explicit cast to void, allow it. People do this when they
1740 // think they know what they're doing :).
Chris Lattner026dc962009-02-14 07:37:35 +00001741 if (getType()->isVoidType())
Chris Lattnerfb846642009-07-28 18:25:28 +00001742 return false;
Chris Lattner026dc962009-02-14 07:37:35 +00001743 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1744 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1745 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001746 case CXXFunctionalCastExprClass: {
John McCall0faede62010-03-12 07:11:26 +00001747 if (getType()->isVoidType())
1748 return false;
Anders Carlsson58beed92009-11-17 17:11:23 +00001749 const CastExpr *CE = cast<CastExpr>(this);
Sean Huntc3021132010-05-05 15:23:54 +00001750
Anders Carlsson58beed92009-11-17 17:11:23 +00001751 // If this is a cast to void or a constructor conversion, check the operand.
1752 // Otherwise, the result of the cast is unused.
John McCall2de56d12010-08-25 11:45:40 +00001753 if (CE->getCastKind() == CK_ToVoid ||
1754 CE->getCastKind() == CK_ConstructorConversion)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001755 return (cast<CastExpr>(this)->getSubExpr()
1756 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner026dc962009-02-14 07:37:35 +00001757 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1758 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1759 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00001760 }
Mike Stump1eb44332009-09-09 15:08:12 +00001761
Eli Friedman4be1f472008-05-19 21:24:43 +00001762 case ImplicitCastExprClass:
1763 // Check the operand, since implicit casts are inserted by Sema
Mike Stumpdf317bf2009-11-03 23:25:48 +00001764 return (cast<ImplicitCastExpr>(this)
1765 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedman4be1f472008-05-19 21:24:43 +00001766
Chris Lattner04421082008-04-08 04:40:51 +00001767 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001768 return (cast<CXXDefaultArgExpr>(this)
1769 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001770
1771 case CXXNewExprClass:
1772 // FIXME: In theory, there might be new expressions that don't have side
1773 // effects (e.g. a placement new with an uninitialized POD).
1774 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001775 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00001776 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00001777 return (cast<CXXBindTemporaryExpr>(this)
1778 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00001779 case ExprWithCleanupsClass:
1780 return (cast<ExprWithCleanups>(this)
Mike Stumpdf317bf2009-11-03 23:25:48 +00001781 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00001782 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001783}
1784
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001785/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00001786/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001787bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00001788 const Expr *E = IgnoreParens();
1789 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001790 default:
1791 return false;
1792 case ObjCIvarRefExprClass:
1793 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00001794 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001795 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001796 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001797 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00001798 case MaterializeTemporaryExprClass:
1799 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1800 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00001801 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001802 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001803 case BlockDeclRefExprClass:
Douglas Gregora2813ce2009-10-23 18:54:35 +00001804 case DeclRefExprClass: {
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00001805
1806 const Decl *D;
1807 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1808 D = BDRE->getDecl();
1809 else
1810 D = cast<DeclRefExpr>(E)->getDecl();
1811
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001812 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1813 if (VD->hasGlobalStorage())
1814 return true;
1815 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00001816 // dereferencing to a pointer is always a gc'able candidate,
1817 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00001818 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00001819 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001820 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001821 return false;
1822 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001823 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00001824 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00001825 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001826 }
1827 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00001828 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00001829 }
1830}
Sebastian Redl369e51f2010-09-10 20:55:33 +00001831
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001832bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1833 if (isTypeDependent())
1834 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00001835 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00001836}
1837
John McCall864c0412011-04-26 20:42:42 +00001838QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00001839 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00001840
1841 // Bound member expressions are always one of these possibilities:
1842 // x->m x.m x->*y x.*y
1843 // (possibly parenthesized)
1844
1845 expr = expr->IgnoreParens();
1846 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1847 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1848 return mem->getMemberDecl()->getType();
1849 }
1850
1851 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1852 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1853 ->getPointeeType();
1854 assert(type->isFunctionType());
1855 return type;
1856 }
1857
1858 assert(isa<UnresolvedMemberExpr>(expr));
1859 return QualType();
1860}
1861
Sebastian Redl369e51f2010-09-10 20:55:33 +00001862static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1863 Expr::CanThrowResult CT2) {
1864 // CanThrowResult constants are ordered so that the maximum is the correct
1865 // merge result.
1866 return CT1 > CT2 ? CT1 : CT2;
1867}
1868
1869static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1870 Expr *E = const_cast<Expr*>(CE);
1871 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall7502c1d2011-02-13 04:07:26 +00001872 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redl369e51f2010-09-10 20:55:33 +00001873 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1874 }
1875 return R;
1876}
1877
Richard Smith7a614d82011-06-11 17:19:42 +00001878static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1879 const Decl *D,
Sebastian Redl369e51f2010-09-10 20:55:33 +00001880 bool NullThrows = true) {
1881 if (!D)
1882 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1883
1884 // See if we can get a function type from the decl somehow.
1885 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1886 if (!VD) // If we have no clue what we're calling, assume the worst.
1887 return Expr::CT_Can;
1888
Sebastian Redl5221d8f2010-09-10 22:34:40 +00001889 // As an extension, we assume that __attribute__((nothrow)) functions don't
1890 // throw.
1891 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1892 return Expr::CT_Cannot;
1893
Sebastian Redl369e51f2010-09-10 20:55:33 +00001894 QualType T = VD->getType();
1895 const FunctionProtoType *FT;
1896 if ((FT = T->getAs<FunctionProtoType>())) {
1897 } else if (const PointerType *PT = T->getAs<PointerType>())
1898 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1899 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1900 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1901 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1902 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1903 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1904 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1905
1906 if (!FT)
1907 return Expr::CT_Can;
1908
Richard Smith7a614d82011-06-11 17:19:42 +00001909 if (FT->getExceptionSpecType() == EST_Delayed) {
1910 assert(isa<CXXConstructorDecl>(D) &&
1911 "only constructor exception specs can be unknown");
1912 Ctx.getDiagnostics().Report(E->getLocStart(),
1913 diag::err_exception_spec_unknown)
1914 << E->getSourceRange();
1915 return Expr::CT_Can;
1916 }
1917
Sebastian Redl8026f6d2011-03-13 17:09:40 +00001918 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redl369e51f2010-09-10 20:55:33 +00001919}
1920
1921static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1922 if (DC->isTypeDependent())
1923 return Expr::CT_Dependent;
1924
Sebastian Redl295995c2010-09-10 20:55:47 +00001925 if (!DC->getTypeAsWritten()->isReferenceType())
1926 return Expr::CT_Cannot;
1927
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001928 if (DC->getSubExpr()->isTypeDependent())
1929 return Expr::CT_Dependent;
1930
Sebastian Redl369e51f2010-09-10 20:55:33 +00001931 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1932}
1933
1934static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1935 const CXXTypeidExpr *DC) {
1936 if (DC->isTypeOperand())
1937 return Expr::CT_Cannot;
1938
1939 Expr *Op = DC->getExprOperand();
1940 if (Op->isTypeDependent())
1941 return Expr::CT_Dependent;
1942
1943 const RecordType *RT = Op->getType()->getAs<RecordType>();
1944 if (!RT)
1945 return Expr::CT_Cannot;
1946
1947 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1948 return Expr::CT_Cannot;
1949
1950 if (Op->Classify(C).isPRValue())
1951 return Expr::CT_Cannot;
1952
1953 return Expr::CT_Can;
1954}
1955
1956Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1957 // C++ [expr.unary.noexcept]p3:
1958 // [Can throw] if in a potentially-evaluated context the expression would
1959 // contain:
1960 switch (getStmtClass()) {
1961 case CXXThrowExprClass:
1962 // - a potentially evaluated throw-expression
1963 return CT_Can;
1964
1965 case CXXDynamicCastExprClass: {
1966 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1967 // where T is a reference type, that requires a run-time check
1968 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1969 if (CT == CT_Can)
1970 return CT;
1971 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1972 }
1973
1974 case CXXTypeidExprClass:
1975 // - a potentially evaluated typeid expression applied to a glvalue
1976 // expression whose type is a polymorphic class type
1977 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1978
1979 // - a potentially evaluated call to a function, member function, function
1980 // pointer, or member function pointer that does not have a non-throwing
1981 // exception-specification
1982 case CallExprClass:
1983 case CXXOperatorCallExprClass:
1984 case CXXMemberCallExprClass: {
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001985 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001986 CanThrowResult CT;
1987 if (isTypeDependent())
1988 CT = CT_Dependent;
Eli Friedmanebc93e1762011-05-12 02:11:32 +00001989 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1990 CT = CT_Cannot;
Eli Friedmanbe57cf42011-05-11 05:22:44 +00001991 else
Richard Smith7a614d82011-06-11 17:19:42 +00001992 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redl369e51f2010-09-10 20:55:33 +00001993 if (CT == CT_Can)
1994 return CT;
1995 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1996 }
1997
Sebastian Redl295995c2010-09-10 20:55:47 +00001998 case CXXConstructExprClass:
1999 case CXXTemporaryObjectExprClass: {
Richard Smith7a614d82011-06-11 17:19:42 +00002000 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl369e51f2010-09-10 20:55:33 +00002001 cast<CXXConstructExpr>(this)->getConstructor());
2002 if (CT == CT_Can)
2003 return CT;
2004 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2005 }
2006
2007 case CXXNewExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002008 CanThrowResult CT;
2009 if (isTypeDependent())
2010 CT = CT_Dependent;
2011 else
2012 CT = MergeCanThrow(
Richard Smith7a614d82011-06-11 17:19:42 +00002013 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
2014 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redl369e51f2010-09-10 20:55:33 +00002015 /*NullThrows*/false));
2016 if (CT == CT_Can)
2017 return CT;
2018 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2019 }
2020
2021 case CXXDeleteExprClass: {
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002022 CanThrowResult CT;
2023 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
2024 if (DTy.isNull() || DTy->isDependentType()) {
2025 CT = CT_Dependent;
2026 } else {
Richard Smith7a614d82011-06-11 17:19:42 +00002027 CT = CanCalleeThrow(C, this,
2028 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002029 if (const RecordType *RT = DTy->getAs<RecordType>()) {
2030 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith7a614d82011-06-11 17:19:42 +00002031 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002032 }
Eli Friedmanbe57cf42011-05-11 05:22:44 +00002033 if (CT == CT_Can)
2034 return CT;
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002035 }
2036 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2037 }
2038
2039 case CXXBindTemporaryExprClass: {
2040 // The bound temporary has to be destroyed again, which might throw.
Richard Smith7a614d82011-06-11 17:19:42 +00002041 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redl0b34cf72010-09-10 23:27:10 +00002042 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
2043 if (CT == CT_Can)
2044 return CT;
Sebastian Redl369e51f2010-09-10 20:55:33 +00002045 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2046 }
2047
2048 // ObjC message sends are like function calls, but never have exception
2049 // specs.
2050 case ObjCMessageExprClass:
2051 case ObjCPropertyRefExprClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002052 return CT_Can;
2053
2054 // Many other things have subexpressions, so we have to test those.
2055 // Some are simple:
2056 case ParenExprClass:
2057 case MemberExprClass:
2058 case CXXReinterpretCastExprClass:
2059 case CXXConstCastExprClass:
2060 case ConditionalOperatorClass:
2061 case CompoundLiteralExprClass:
2062 case ExtVectorElementExprClass:
2063 case InitListExprClass:
2064 case DesignatedInitExprClass:
2065 case ParenListExprClass:
2066 case VAArgExprClass:
2067 case CXXDefaultArgExprClass:
John McCall4765fa02010-12-06 08:20:24 +00002068 case ExprWithCleanupsClass:
Sebastian Redl369e51f2010-09-10 20:55:33 +00002069 case ObjCIvarRefExprClass:
2070 case ObjCIsaExprClass:
2071 case ShuffleVectorExprClass:
2072 return CanSubExprsThrow(C, this);
2073
2074 // Some might be dependent for other reasons.
2075 case UnaryOperatorClass:
2076 case ArraySubscriptExprClass:
2077 case ImplicitCastExprClass:
2078 case CStyleCastExprClass:
2079 case CXXStaticCastExprClass:
2080 case CXXFunctionalCastExprClass:
2081 case BinaryOperatorClass:
Douglas Gregor03e80032011-06-21 17:03:29 +00002082 case CompoundAssignOperatorClass:
2083 case MaterializeTemporaryExprClass: {
Sebastian Redl369e51f2010-09-10 20:55:33 +00002084 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2085 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2086 }
2087
2088 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2089 case StmtExprClass:
2090 return CT_Can;
2091
2092 case ChooseExprClass:
2093 if (isTypeDependent() || isValueDependent())
2094 return CT_Dependent;
2095 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2096
Peter Collingbournef111d932011-04-15 00:35:48 +00002097 case GenericSelectionExprClass:
2098 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2099 return CT_Dependent;
2100 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2101
Sebastian Redl369e51f2010-09-10 20:55:33 +00002102 // Some expressions are always dependent.
2103 case DependentScopeDeclRefExprClass:
2104 case CXXUnresolvedConstructExprClass:
2105 case CXXDependentScopeMemberExprClass:
2106 return CT_Dependent;
2107
2108 default:
2109 // All other expressions don't have subexpressions, or else they are
2110 // unevaluated.
2111 return CT_Cannot;
2112 }
2113}
2114
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002115Expr* Expr::IgnoreParens() {
2116 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002117 while (true) {
2118 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2119 E = P->getSubExpr();
2120 continue;
2121 }
2122 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2123 if (P->getOpcode() == UO_Extension) {
2124 E = P->getSubExpr();
2125 continue;
2126 }
2127 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002128 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2129 if (!P->isResultDependent()) {
2130 E = P->getResultExpr();
2131 continue;
2132 }
2133 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002134 return E;
2135 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002136}
2137
Chris Lattner56f34942008-02-13 01:02:39 +00002138/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2139/// or CastExprs or ImplicitCastExprs, returning their operand.
2140Expr *Expr::IgnoreParenCasts() {
2141 Expr *E = this;
2142 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002143 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002144 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002145 continue;
2146 }
2147 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002148 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002149 continue;
2150 }
2151 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2152 if (P->getOpcode() == UO_Extension) {
2153 E = P->getSubExpr();
2154 continue;
2155 }
2156 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002157 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2158 if (!P->isResultDependent()) {
2159 E = P->getResultExpr();
2160 continue;
2161 }
2162 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002163 if (MaterializeTemporaryExpr *Materialize
2164 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2165 E = Materialize->GetTemporaryExpr();
2166 continue;
2167 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002168 if (SubstNonTypeTemplateParmExpr *NTTP
2169 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2170 E = NTTP->getReplacement();
2171 continue;
2172 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002173 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002174 }
2175}
2176
John McCall9c5d70c2010-12-04 08:24:19 +00002177/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2178/// casts. This is intended purely as a temporary workaround for code
2179/// that hasn't yet been rewritten to do the right thing about those
2180/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002181Expr *Expr::IgnoreParenLValueCasts() {
2182 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002183 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002184 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2185 E = P->getSubExpr();
2186 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002187 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002188 if (P->getCastKind() == CK_LValueToRValue) {
2189 E = P->getSubExpr();
2190 continue;
2191 }
John McCall9c5d70c2010-12-04 08:24:19 +00002192 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2193 if (P->getOpcode() == UO_Extension) {
2194 E = P->getSubExpr();
2195 continue;
2196 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002197 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2198 if (!P->isResultDependent()) {
2199 E = P->getResultExpr();
2200 continue;
2201 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002202 } else if (MaterializeTemporaryExpr *Materialize
2203 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2204 E = Materialize->GetTemporaryExpr();
2205 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002206 } else if (SubstNonTypeTemplateParmExpr *NTTP
2207 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2208 E = NTTP->getReplacement();
2209 continue;
John McCallf6a16482010-12-04 03:47:34 +00002210 }
2211 break;
2212 }
2213 return E;
2214}
2215
John McCall2fc46bf2010-05-05 22:59:52 +00002216Expr *Expr::IgnoreParenImpCasts() {
2217 Expr *E = this;
2218 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002219 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002220 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002221 continue;
2222 }
2223 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002224 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002225 continue;
2226 }
2227 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2228 if (P->getOpcode() == UO_Extension) {
2229 E = P->getSubExpr();
2230 continue;
2231 }
2232 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002233 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2234 if (!P->isResultDependent()) {
2235 E = P->getResultExpr();
2236 continue;
2237 }
2238 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002239 if (MaterializeTemporaryExpr *Materialize
2240 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2241 E = Materialize->GetTemporaryExpr();
2242 continue;
2243 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002244 if (SubstNonTypeTemplateParmExpr *NTTP
2245 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2246 E = NTTP->getReplacement();
2247 continue;
2248 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002249 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002250 }
2251}
2252
Hans Wennborg2f072b42011-06-09 17:06:51 +00002253Expr *Expr::IgnoreConversionOperator() {
2254 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002255 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002256 return MCE->getImplicitObjectArgument();
2257 }
2258 return this;
2259}
2260
Chris Lattnerecdd8412009-03-13 17:28:01 +00002261/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2262/// value (including ptr->int casts of the same size). Strip off any
2263/// ParenExpr or CastExprs, returning their operand.
2264Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2265 Expr *E = this;
2266 while (true) {
2267 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2268 E = P->getSubExpr();
2269 continue;
2270 }
Mike Stump1eb44332009-09-09 15:08:12 +00002271
Chris Lattnerecdd8412009-03-13 17:28:01 +00002272 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2273 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002274 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002275 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002276
Chris Lattnerecdd8412009-03-13 17:28:01 +00002277 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2278 E = SE;
2279 continue;
2280 }
Mike Stump1eb44332009-09-09 15:08:12 +00002281
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002282 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002283 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002284 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002285 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002286 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2287 E = SE;
2288 continue;
2289 }
2290 }
Mike Stump1eb44332009-09-09 15:08:12 +00002291
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002292 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2293 if (P->getOpcode() == UO_Extension) {
2294 E = P->getSubExpr();
2295 continue;
2296 }
2297 }
2298
Peter Collingbournef111d932011-04-15 00:35:48 +00002299 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2300 if (!P->isResultDependent()) {
2301 E = P->getResultExpr();
2302 continue;
2303 }
2304 }
2305
Douglas Gregorc0244c52011-09-08 17:56:33 +00002306 if (SubstNonTypeTemplateParmExpr *NTTP
2307 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2308 E = NTTP->getReplacement();
2309 continue;
2310 }
2311
Chris Lattnerecdd8412009-03-13 17:28:01 +00002312 return E;
2313 }
2314}
2315
Douglas Gregor6eef5192009-12-14 19:27:10 +00002316bool Expr::isDefaultArgument() const {
2317 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002318 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2319 E = M->GetTemporaryExpr();
2320
Douglas Gregor6eef5192009-12-14 19:27:10 +00002321 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2322 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002323
Douglas Gregor6eef5192009-12-14 19:27:10 +00002324 return isa<CXXDefaultArgExpr>(E);
2325}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002326
Douglas Gregor2f599792010-04-02 18:24:57 +00002327/// \brief Skip over any no-op casts and any temporary-binding
2328/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002329static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002330 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2331 E = M->GetTemporaryExpr();
2332
Douglas Gregor2f599792010-04-02 18:24:57 +00002333 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002334 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002335 E = ICE->getSubExpr();
2336 else
2337 break;
2338 }
2339
2340 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2341 E = BE->getSubExpr();
2342
2343 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002344 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002345 E = ICE->getSubExpr();
2346 else
2347 break;
2348 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002349
2350 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002351}
2352
John McCall558d2ab2010-09-15 10:14:12 +00002353/// isTemporaryObject - Determines if this expression produces a
2354/// temporary of the given class type.
2355bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2356 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2357 return false;
2358
Anders Carlssonf8b30152010-11-28 16:40:49 +00002359 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002360
John McCall58277b52010-09-15 20:59:13 +00002361 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002362 if (!E->Classify(C).isPRValue()) {
2363 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002364 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002365 return false;
2366 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002367
John McCall19e60ad2010-09-16 06:57:56 +00002368 // Black-list a few cases which yield pr-values of class type that don't
2369 // refer to temporaries of that type:
2370
2371 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002372 if (isa<ImplicitCastExpr>(E)) {
2373 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2374 case CK_DerivedToBase:
2375 case CK_UncheckedDerivedToBase:
2376 return false;
2377 default:
2378 break;
2379 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002380 }
2381
John McCall19e60ad2010-09-16 06:57:56 +00002382 // - member expressions (all)
2383 if (isa<MemberExpr>(E))
2384 return false;
2385
John McCall56ca35d2011-02-17 10:25:35 +00002386 // - opaque values (all)
2387 if (isa<OpaqueValueExpr>(E))
2388 return false;
2389
John McCall558d2ab2010-09-15 10:14:12 +00002390 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002391}
2392
Douglas Gregor75e85042011-03-02 21:06:53 +00002393bool Expr::isImplicitCXXThis() const {
2394 const Expr *E = this;
2395
2396 // Strip away parentheses and casts we don't care about.
2397 while (true) {
2398 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2399 E = Paren->getSubExpr();
2400 continue;
2401 }
2402
2403 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2404 if (ICE->getCastKind() == CK_NoOp ||
2405 ICE->getCastKind() == CK_LValueToRValue ||
2406 ICE->getCastKind() == CK_DerivedToBase ||
2407 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2408 E = ICE->getSubExpr();
2409 continue;
2410 }
2411 }
2412
2413 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2414 if (UnOp->getOpcode() == UO_Extension) {
2415 E = UnOp->getSubExpr();
2416 continue;
2417 }
2418 }
2419
Douglas Gregor03e80032011-06-21 17:03:29 +00002420 if (const MaterializeTemporaryExpr *M
2421 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2422 E = M->GetTemporaryExpr();
2423 continue;
2424 }
2425
Douglas Gregor75e85042011-03-02 21:06:53 +00002426 break;
2427 }
2428
2429 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2430 return This->isImplicit();
2431
2432 return false;
2433}
2434
Douglas Gregor898574e2008-12-05 23:32:09 +00002435/// hasAnyTypeDependentArguments - Determines if any of the expressions
2436/// in Exprs is type-dependent.
2437bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2438 for (unsigned I = 0; I < NumExprs; ++I)
2439 if (Exprs[I]->isTypeDependent())
2440 return true;
2441
2442 return false;
2443}
2444
2445/// hasAnyValueDependentArguments - Determines if any of the expressions
2446/// in Exprs is value-dependent.
2447bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2448 for (unsigned I = 0; I < NumExprs; ++I)
2449 if (Exprs[I]->isValueDependent())
2450 return true;
2451
2452 return false;
2453}
2454
John McCall4204f072010-08-02 21:13:48 +00002455bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002456 // This function is attempting whether an expression is an initializer
2457 // which can be evaluated at compile-time. isEvaluatable handles most
2458 // of the cases, but it can't deal with some initializer-specific
2459 // expressions, and it can't deal with aggregates; we deal with those here,
2460 // and fall back to isEvaluatable for the other cases.
2461
John McCall4204f072010-08-02 21:13:48 +00002462 // If we ever capture reference-binding directly in the AST, we can
2463 // kill the second parameter.
2464
2465 if (IsForRef) {
2466 EvalResult Result;
2467 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2468 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002469
Anders Carlssone8a32b82008-11-24 05:23:59 +00002470 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002471 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002472 case IntegerLiteralClass:
2473 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002474 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002475 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002476 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002477 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002478 case CXXTemporaryObjectExprClass:
2479 case CXXConstructExprClass: {
2480 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002481
2482 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002483 if (CE->getConstructor()->isTrivial()) {
2484 // 1) an application of the trivial default constructor or
2485 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002486
Richard Smith180f4792011-11-10 06:34:14 +00002487 // 2) an elidable trivial copy construction of an operand which is
2488 // itself a constant initializer. Note that we consider the
2489 // operand on its own, *not* as a reference binding.
2490 if (CE->isElidable() &&
2491 CE->getArg(0)->isConstantInitializer(Ctx, false))
2492 return true;
2493 }
2494
2495 // 3) a foldable constexpr constructor.
2496 break;
John McCallb4b9b152010-08-01 21:51:45 +00002497 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002498 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002499 // This handles gcc's extension that allows global initializers like
2500 // "struct x {int x;} x = (struct x) {};".
2501 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002502 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002503 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002504 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002505 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002506 // FIXME: This doesn't deal with fields with reference types correctly.
2507 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2508 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002509 const InitListExpr *Exp = cast<InitListExpr>(this);
2510 unsigned numInits = Exp->getNumInits();
2511 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002512 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002513 return false;
2514 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002515 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002516 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002517 case ImplicitValueInitExprClass:
2518 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002519 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002520 return cast<ParenExpr>(this)->getSubExpr()
2521 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002522 case GenericSelectionExprClass:
2523 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2524 return false;
2525 return cast<GenericSelectionExpr>(this)->getResultExpr()
2526 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002527 case ChooseExprClass:
2528 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2529 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002530 case UnaryOperatorClass: {
2531 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002532 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002533 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002534 break;
2535 }
John McCall4204f072010-08-02 21:13:48 +00002536 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002537 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002538 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002539 case CStyleCastExprClass: {
2540 const CastExpr *CE = cast<CastExpr>(this);
2541
David Chisnall7a7ee302012-01-16 17:27:18 +00002542 // If we're promoting an integer to an _Atomic type then this is constant
2543 // if the integer is constant. We also need to check the converse in case
2544 // someone does something like:
2545 //
2546 // int a = (_Atomic(int))42;
2547 //
2548 // I doubt anyone would write code like this directly, but it's quite
2549 // possible as the result of macro expansions.
2550 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2551 CE->getCastKind() == CK_AtomicToNonAtomic)
2552 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2553
Richard Smithd62ca372011-12-06 22:44:34 +00002554 // Handle bitcasts of vector constants.
2555 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2556 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2557
Eli Friedman6bd97192011-12-21 00:43:02 +00002558 // Handle misc casts we want to ignore.
2559 // FIXME: Is it really safe to ignore all these?
2560 if (CE->getCastKind() == CK_NoOp ||
2561 CE->getCastKind() == CK_LValueToRValue ||
2562 CE->getCastKind() == CK_ToUnion ||
2563 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002564 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2565
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002566 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002567 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002568 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002569 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002570 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002571 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002572 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002573}
2574
Chandler Carruth82214a82011-02-18 23:54:50 +00002575/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2576/// pointer constant or not, as well as the specific kind of constant detected.
2577/// Null pointer constants can be integer constant expressions with the
2578/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2579/// (a GNU extension).
2580Expr::NullPointerConstantKind
2581Expr::isNullPointerConstant(ASTContext &Ctx,
2582 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002583 if (isValueDependent()) {
2584 switch (NPC) {
2585 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002586 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002587 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002588 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2589 return NPCK_ZeroInteger;
2590 else
2591 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002592
Douglas Gregorce940492009-09-25 04:25:58 +00002593 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002594 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002595 }
2596 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002597
Sebastian Redl07779722008-10-31 14:43:28 +00002598 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002599 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl6215dee2008-11-04 11:45:54 +00002600 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002601 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002602 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002603 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002604 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002605 Pointee->isVoidType() && // to void*
2606 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002607 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002608 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002609 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002610 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2611 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002612 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002613 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2614 // Accept ((void*)0) as a null pointer constant, as many other
2615 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002616 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002617 } else if (const GenericSelectionExpr *GE =
2618 dyn_cast<GenericSelectionExpr>(this)) {
2619 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002620 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002621 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002622 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002623 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002624 } else if (isa<GNUNullExpr>(this)) {
2625 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002626 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002627 } else if (const MaterializeTemporaryExpr *M
2628 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2629 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002630 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2631 if (const Expr *Source = OVE->getSourceExpr())
2632 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002633 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002634
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002635 // C++0x nullptr_t is always a null pointer constant.
2636 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002637 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002638
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002639 if (const RecordType *UT = getType()->getAsUnionType())
2640 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2641 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2642 const Expr *InitExpr = CLE->getInitializer();
2643 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2644 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2645 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002646 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002647 if (!getType()->isIntegerType() ||
Fariborz Jahanian56fc0d12009-10-06 00:09:31 +00002648 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002649 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002650
Reid Spencer5f016e22007-07-11 17:01:13 +00002651 // If we have an integer constant expression, we need to *evaluate* it and
2652 // test for the value 0.
Eli Friedman09de1762009-04-25 22:37:12 +00002653 llvm::APSInt Result;
Chandler Carruth82214a82011-02-18 23:54:50 +00002654 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2655
2656 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Reid Spencer5f016e22007-07-11 17:01:13 +00002657}
Steve Naroff31a45842007-07-28 23:10:27 +00002658
John McCallf6a16482010-12-04 03:47:34 +00002659/// \brief If this expression is an l-value for an Objective C
2660/// property, find the underlying property reference expression.
2661const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2662 const Expr *E = this;
2663 while (true) {
2664 assert((E->getValueKind() == VK_LValue &&
2665 E->getObjectKind() == OK_ObjCProperty) &&
2666 "expression is not a property reference");
2667 E = E->IgnoreParenCasts();
2668 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2669 if (BO->getOpcode() == BO_Comma) {
2670 E = BO->getRHS();
2671 continue;
2672 }
2673 }
2674
2675 break;
2676 }
2677
2678 return cast<ObjCPropertyRefExpr>(E);
2679}
2680
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002681FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002682 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002683
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002684 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002685 if (ICE->getCastKind() == CK_LValueToRValue ||
2686 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002687 E = ICE->getSubExpr()->IgnoreParens();
2688 else
2689 break;
2690 }
2691
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002692 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002693 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002694 if (Field->isBitField())
2695 return Field;
2696
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002697 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2698 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2699 if (Field->isBitField())
2700 return Field;
2701
Eli Friedman42068e92011-07-13 02:05:57 +00002702 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002703 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2704 return BinOp->getLHS()->getBitField();
2705
Eli Friedman42068e92011-07-13 02:05:57 +00002706 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2707 return BinOp->getRHS()->getBitField();
2708 }
2709
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002710 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002711}
2712
Anders Carlsson09380262010-01-31 17:18:49 +00002713bool Expr::refersToVectorElement() const {
2714 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002715
Anders Carlsson09380262010-01-31 17:18:49 +00002716 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002717 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002718 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002719 E = ICE->getSubExpr()->IgnoreParens();
2720 else
2721 break;
2722 }
Sean Huntc3021132010-05-05 15:23:54 +00002723
Anders Carlsson09380262010-01-31 17:18:49 +00002724 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2725 return ASE->getBase()->getType()->isVectorType();
2726
2727 if (isa<ExtVectorElementExpr>(E))
2728 return true;
2729
2730 return false;
2731}
2732
Chris Lattner2140e902009-02-16 22:14:05 +00002733/// isArrow - Return true if the base expression is a pointer to vector,
2734/// return false if the base expression is a vector.
2735bool ExtVectorElementExpr::isArrow() const {
2736 return getBase()->getType()->isPointerType();
2737}
2738
Nate Begeman213541a2008-04-18 23:10:10 +00002739unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002740 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002741 return VT->getNumElements();
2742 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002743}
2744
Nate Begeman8a997642008-05-09 06:41:27 +00002745/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002746bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002747 // FIXME: Refactor this code to an accessor on the AST node which returns the
2748 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002749 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002750
2751 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002752 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002753 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002754
Nate Begeman190d6a22009-01-18 02:01:21 +00002755 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002756 if (Comp[0] == 's' || Comp[0] == 'S')
2757 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002758
Daniel Dunbar15027422009-10-17 23:53:04 +00002759 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002760 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002761 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002762
Steve Narofffec0b492007-07-30 03:29:09 +00002763 return false;
2764}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002765
Nate Begeman8a997642008-05-09 06:41:27 +00002766/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002767void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002768 SmallVectorImpl<unsigned> &Elts) const {
2769 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002770 if (Comp[0] == 's' || Comp[0] == 'S')
2771 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002772
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002773 bool isHi = Comp == "hi";
2774 bool isLo = Comp == "lo";
2775 bool isEven = Comp == "even";
2776 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002777
Nate Begeman8a997642008-05-09 06:41:27 +00002778 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2779 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002780
Nate Begeman8a997642008-05-09 06:41:27 +00002781 if (isHi)
2782 Index = e + i;
2783 else if (isLo)
2784 Index = i;
2785 else if (isEven)
2786 Index = 2 * i;
2787 else if (isOdd)
2788 Index = 2 * i + 1;
2789 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002790 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002791
Nate Begeman3b8d1162008-05-13 21:03:02 +00002792 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002793 }
Nate Begeman8a997642008-05-09 06:41:27 +00002794}
2795
Douglas Gregor04badcf2010-04-21 00:45:42 +00002796ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002797 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002798 SourceLocation LBracLoc,
2799 SourceLocation SuperLoc,
2800 bool IsInstanceSuper,
2801 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002802 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002803 ArrayRef<SourceLocation> SelLocs,
2804 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002805 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002806 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002807 SourceLocation RBracLoc,
2808 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002809 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002810 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002811 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002812 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002813 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2814 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002815 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002816 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
2817 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002818{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002819 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002820 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002821}
2822
Douglas Gregor04badcf2010-04-21 00:45:42 +00002823ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002824 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002825 SourceLocation LBracLoc,
2826 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002827 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002828 ArrayRef<SourceLocation> SelLocs,
2829 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002830 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002831 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002832 SourceLocation RBracLoc,
2833 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002834 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002835 T->isDependentType(), T->isInstantiationDependentType(),
2836 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002837 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2838 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002839 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002840 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002841 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002842{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002843 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002844 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002845}
2846
Douglas Gregor04badcf2010-04-21 00:45:42 +00002847ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002848 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002849 SourceLocation LBracLoc,
2850 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002851 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002852 ArrayRef<SourceLocation> SelLocs,
2853 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002854 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002855 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002856 SourceLocation RBracLoc,
2857 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002858 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002859 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002860 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002861 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002862 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2863 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002864 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002865 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002866 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002867{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002868 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002869 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002870}
2871
2872void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2873 ArrayRef<SourceLocation> SelLocs,
2874 SelectorLocationsKind SelLocsK) {
2875 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00002876 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002877 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002878 if (Args[I]->isTypeDependent())
2879 ExprBits.TypeDependent = true;
2880 if (Args[I]->isValueDependent())
2881 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002882 if (Args[I]->isInstantiationDependent())
2883 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002884 if (Args[I]->containsUnexpandedParameterPack())
2885 ExprBits.ContainsUnexpandedParameterPack = true;
2886
2887 MyArgs[I] = Args[I];
2888 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002889
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00002890 if (!isImplicit()) {
2891 SelLocsKind = SelLocsK;
2892 if (SelLocsK == SelLoc_NonStandard)
2893 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
2894 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002895}
2896
Douglas Gregor04badcf2010-04-21 00:45:42 +00002897ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002898 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002899 SourceLocation LBracLoc,
2900 SourceLocation SuperLoc,
2901 bool IsInstanceSuper,
2902 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002903 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002904 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002905 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002906 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002907 SourceLocation RBracLoc,
2908 bool isImplicit) {
2909 assert((!SelLocs.empty() || isImplicit) &&
2910 "No selector locs for non-implicit message");
2911 ObjCMessageExpr *Mem;
2912 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
2913 if (isImplicit)
2914 Mem = alloc(Context, Args.size(), 0);
2915 else
2916 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00002917 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002918 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002919 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002920}
2921
2922ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002923 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002924 SourceLocation LBracLoc,
2925 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002926 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002927 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002928 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002929 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002930 SourceLocation RBracLoc,
2931 bool isImplicit) {
2932 assert((!SelLocs.empty() || isImplicit) &&
2933 "No selector locs for non-implicit message");
2934 ObjCMessageExpr *Mem;
2935 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
2936 if (isImplicit)
2937 Mem = alloc(Context, Args.size(), 0);
2938 else
2939 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002940 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002941 SelLocs, SelLocsK, Method, Args, RBracLoc,
2942 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002943}
2944
2945ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002946 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002947 SourceLocation LBracLoc,
2948 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002949 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002950 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002951 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002952 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002953 SourceLocation RBracLoc,
2954 bool isImplicit) {
2955 assert((!SelLocs.empty() || isImplicit) &&
2956 "No selector locs for non-implicit message");
2957 ObjCMessageExpr *Mem;
2958 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
2959 if (isImplicit)
2960 Mem = alloc(Context, Args.size(), 0);
2961 else
2962 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002963 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002964 SelLocs, SelLocsK, Method, Args, RBracLoc,
2965 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002966}
2967
Sean Huntc3021132010-05-05 15:23:54 +00002968ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002969 unsigned NumArgs,
2970 unsigned NumStoredSelLocs) {
2971 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002972 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2973}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00002974
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002975ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2976 ArrayRef<Expr *> Args,
2977 SourceLocation RBraceLoc,
2978 ArrayRef<SourceLocation> SelLocs,
2979 Selector Sel,
2980 SelectorLocationsKind &SelLocsK) {
2981 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
2982 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
2983 : 0;
2984 return alloc(C, Args.size(), NumStoredSelLocs);
2985}
2986
2987ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
2988 unsigned NumArgs,
2989 unsigned NumStoredSelLocs) {
2990 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
2991 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
2992 return (ObjCMessageExpr *)C.Allocate(Size,
2993 llvm::AlignOf<ObjCMessageExpr>::Alignment);
2994}
2995
2996void ObjCMessageExpr::getSelectorLocs(
2997 SmallVectorImpl<SourceLocation> &SelLocs) const {
2998 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
2999 SelLocs.push_back(getSelectorLoc(i));
3000}
3001
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003002SourceRange ObjCMessageExpr::getReceiverRange() const {
3003 switch (getReceiverKind()) {
3004 case Instance:
3005 return getInstanceReceiver()->getSourceRange();
3006
3007 case Class:
3008 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3009
3010 case SuperInstance:
3011 case SuperClass:
3012 return getSuperLoc();
3013 }
3014
David Blaikie30263482012-01-20 21:50:17 +00003015 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003016}
3017
Douglas Gregor04badcf2010-04-21 00:45:42 +00003018Selector ObjCMessageExpr::getSelector() const {
3019 if (HasMethod)
3020 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3021 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003022 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003023}
3024
3025ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3026 switch (getReceiverKind()) {
3027 case Instance:
3028 if (const ObjCObjectPointerType *Ptr
3029 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
3030 return Ptr->getInterfaceDecl();
3031 break;
3032
3033 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00003034 if (const ObjCObjectType *Ty
3035 = getClassReceiver()->getAs<ObjCObjectType>())
3036 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003037 break;
3038
3039 case SuperInstance:
3040 if (const ObjCObjectPointerType *Ptr
3041 = getSuperType()->getAs<ObjCObjectPointerType>())
3042 return Ptr->getInterfaceDecl();
3043 break;
3044
3045 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00003046 if (const ObjCObjectType *Iface
3047 = getSuperType()->getAs<ObjCObjectType>())
3048 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003049 break;
3050 }
3051
3052 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003053}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003054
Chris Lattner5f9e2722011-07-23 10:55:15 +00003055StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003056 switch (getBridgeKind()) {
3057 case OBC_Bridge:
3058 return "__bridge";
3059 case OBC_BridgeTransfer:
3060 return "__bridge_transfer";
3061 case OBC_BridgeRetained:
3062 return "__bridge_retained";
3063 }
David Blaikie30263482012-01-20 21:50:17 +00003064
3065 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003066}
3067
Jay Foad4ba2a172011-01-12 09:06:06 +00003068bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003069 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003070}
3071
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003072ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3073 QualType Type, SourceLocation BLoc,
3074 SourceLocation RP)
3075 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3076 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003077 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003078 Type->containsUnexpandedParameterPack()),
3079 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3080{
3081 SubExprs = new (C) Stmt*[nexpr];
3082 for (unsigned i = 0; i < nexpr; i++) {
3083 if (args[i]->isTypeDependent())
3084 ExprBits.TypeDependent = true;
3085 if (args[i]->isValueDependent())
3086 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003087 if (args[i]->isInstantiationDependent())
3088 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003089 if (args[i]->containsUnexpandedParameterPack())
3090 ExprBits.ContainsUnexpandedParameterPack = true;
3091
3092 SubExprs[i] = args[i];
3093 }
3094}
3095
Nate Begeman888376a2009-08-12 02:28:50 +00003096void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3097 unsigned NumExprs) {
3098 if (SubExprs) C.Deallocate(SubExprs);
3099
3100 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003101 this->NumExprs = NumExprs;
3102 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003103}
Nate Begeman888376a2009-08-12 02:28:50 +00003104
Peter Collingbournef111d932011-04-15 00:35:48 +00003105GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3106 SourceLocation GenericLoc, Expr *ControllingExpr,
3107 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3108 unsigned NumAssocs, SourceLocation DefaultLoc,
3109 SourceLocation RParenLoc,
3110 bool ContainsUnexpandedParameterPack,
3111 unsigned ResultIndex)
3112 : Expr(GenericSelectionExprClass,
3113 AssocExprs[ResultIndex]->getType(),
3114 AssocExprs[ResultIndex]->getValueKind(),
3115 AssocExprs[ResultIndex]->getObjectKind(),
3116 AssocExprs[ResultIndex]->isTypeDependent(),
3117 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003118 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003119 ContainsUnexpandedParameterPack),
3120 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3121 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3122 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3123 RParenLoc(RParenLoc) {
3124 SubExprs[CONTROLLING] = ControllingExpr;
3125 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3126 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3127}
3128
3129GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3130 SourceLocation GenericLoc, Expr *ControllingExpr,
3131 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3132 unsigned NumAssocs, SourceLocation DefaultLoc,
3133 SourceLocation RParenLoc,
3134 bool ContainsUnexpandedParameterPack)
3135 : Expr(GenericSelectionExprClass,
3136 Context.DependentTy,
3137 VK_RValue,
3138 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003139 /*isTypeDependent=*/true,
3140 /*isValueDependent=*/true,
3141 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003142 ContainsUnexpandedParameterPack),
3143 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3144 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3145 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3146 RParenLoc(RParenLoc) {
3147 SubExprs[CONTROLLING] = ControllingExpr;
3148 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3149 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3150}
3151
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003152//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003153// DesignatedInitExpr
3154//===----------------------------------------------------------------------===//
3155
Chandler Carruthb1138242011-06-16 06:47:06 +00003156IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003157 assert(Kind == FieldDesignator && "Only valid on a field designator");
3158 if (Field.NameOrField & 0x01)
3159 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3160 else
3161 return getField()->getIdentifier();
3162}
3163
Sean Huntc3021132010-05-05 15:23:54 +00003164DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003165 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003166 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003167 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003168 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003169 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003170 unsigned NumIndexExprs,
3171 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003172 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003173 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003174 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003175 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003176 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003177 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3178 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003179 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003180
3181 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003182 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003183 *Child++ = Init;
3184
3185 // Copy the designators and their subexpressions, computing
3186 // value-dependence along the way.
3187 unsigned IndexIdx = 0;
3188 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003189 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003190
3191 if (this->Designators[I].isArrayDesignator()) {
3192 // Compute type- and value-dependence.
3193 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003194 if (Index->isTypeDependent() || Index->isValueDependent())
3195 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003196 if (Index->isInstantiationDependent())
3197 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003198 // Propagate unexpanded parameter packs.
3199 if (Index->containsUnexpandedParameterPack())
3200 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003201
3202 // Copy the index expressions into permanent storage.
3203 *Child++ = IndexExprs[IndexIdx++];
3204 } else if (this->Designators[I].isArrayRangeDesignator()) {
3205 // Compute type- and value-dependence.
3206 Expr *Start = IndexExprs[IndexIdx];
3207 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003208 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003209 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003210 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003211 ExprBits.InstantiationDependent = true;
3212 } else if (Start->isInstantiationDependent() ||
3213 End->isInstantiationDependent()) {
3214 ExprBits.InstantiationDependent = true;
3215 }
3216
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003217 // Propagate unexpanded parameter packs.
3218 if (Start->containsUnexpandedParameterPack() ||
3219 End->containsUnexpandedParameterPack())
3220 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003221
3222 // Copy the start/end expressions into permanent storage.
3223 *Child++ = IndexExprs[IndexIdx++];
3224 *Child++ = IndexExprs[IndexIdx++];
3225 }
3226 }
3227
3228 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003229}
3230
Douglas Gregor05c13a32009-01-22 00:58:24 +00003231DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003232DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003233 unsigned NumDesignators,
3234 Expr **IndexExprs, unsigned NumIndexExprs,
3235 SourceLocation ColonOrEqualLoc,
3236 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003237 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003238 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003239 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003240 ColonOrEqualLoc, UsesColonSyntax,
3241 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003242}
3243
Mike Stump1eb44332009-09-09 15:08:12 +00003244DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003245 unsigned NumIndexExprs) {
3246 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3247 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3248 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3249}
3250
Douglas Gregor319d57f2010-01-06 23:17:19 +00003251void DesignatedInitExpr::setDesignators(ASTContext &C,
3252 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003253 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003254 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003255 NumDesignators = NumDesigs;
3256 for (unsigned I = 0; I != NumDesigs; ++I)
3257 Designators[I] = Desigs[I];
3258}
3259
Abramo Bagnara24f46742011-03-16 15:08:46 +00003260SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3261 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3262 if (size() == 1)
3263 return DIE->getDesignator(0)->getSourceRange();
3264 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3265 DIE->getDesignator(size()-1)->getEndLocation());
3266}
3267
Douglas Gregor05c13a32009-01-22 00:58:24 +00003268SourceRange DesignatedInitExpr::getSourceRange() const {
3269 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003270 Designator &First =
3271 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003272 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003273 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003274 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3275 else
3276 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3277 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003278 StartLoc =
3279 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003280 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3281}
3282
Douglas Gregor05c13a32009-01-22 00:58:24 +00003283Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3284 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3285 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3286 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003287 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3288 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3289}
3290
3291Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003292 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003293 "Requires array range designator");
3294 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3295 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003296 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3297 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3298}
3299
3300Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003301 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003302 "Requires array range designator");
3303 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3304 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003305 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3306 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3307}
3308
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003309/// \brief Replaces the designator at index @p Idx with the series
3310/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003311void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003312 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003313 const Designator *Last) {
3314 unsigned NumNewDesignators = Last - First;
3315 if (NumNewDesignators == 0) {
3316 std::copy_backward(Designators + Idx + 1,
3317 Designators + NumDesignators,
3318 Designators + Idx);
3319 --NumNewDesignators;
3320 return;
3321 } else if (NumNewDesignators == 1) {
3322 Designators[Idx] = *First;
3323 return;
3324 }
3325
Mike Stump1eb44332009-09-09 15:08:12 +00003326 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003327 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003328 std::copy(Designators, Designators + Idx, NewDesignators);
3329 std::copy(First, Last, NewDesignators + Idx);
3330 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3331 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003332 Designators = NewDesignators;
3333 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3334}
3335
Mike Stump1eb44332009-09-09 15:08:12 +00003336ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003337 Expr **exprs, unsigned nexprs,
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003338 SourceLocation rparenloc, QualType T)
3339 : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003340 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003341 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Manuel Klimek0d9106f2011-06-22 20:02:16 +00003342 assert(!T.isNull() && "ParenListExpr must have a valid type");
Nate Begeman2ef13e52009-08-10 23:49:36 +00003343 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003344 for (unsigned i = 0; i != nexprs; ++i) {
3345 if (exprs[i]->isTypeDependent())
3346 ExprBits.TypeDependent = true;
3347 if (exprs[i]->isValueDependent())
3348 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003349 if (exprs[i]->isInstantiationDependent())
3350 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003351 if (exprs[i]->containsUnexpandedParameterPack())
3352 ExprBits.ContainsUnexpandedParameterPack = true;
3353
Nate Begeman2ef13e52009-08-10 23:49:36 +00003354 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003355 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003356}
3357
John McCalle996ffd2011-02-16 08:02:54 +00003358const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3359 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3360 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003361 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3362 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003363 e = cast<CXXConstructExpr>(e)->getArg(0);
3364 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3365 e = ice->getSubExpr();
3366 return cast<OpaqueValueExpr>(e);
3367}
3368
John McCall4b9c2d22011-11-06 09:01:30 +00003369PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3370 unsigned numSemanticExprs) {
3371 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3372 (1 + numSemanticExprs) * sizeof(Expr*),
3373 llvm::alignOf<PseudoObjectExpr>());
3374 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3375}
3376
3377PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3378 : Expr(PseudoObjectExprClass, shell) {
3379 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3380}
3381
3382PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3383 ArrayRef<Expr*> semantics,
3384 unsigned resultIndex) {
3385 assert(syntax && "no syntactic expression!");
3386 assert(semantics.size() && "no semantic expressions!");
3387
3388 QualType type;
3389 ExprValueKind VK;
3390 if (resultIndex == NoResult) {
3391 type = C.VoidTy;
3392 VK = VK_RValue;
3393 } else {
3394 assert(resultIndex < semantics.size());
3395 type = semantics[resultIndex]->getType();
3396 VK = semantics[resultIndex]->getValueKind();
3397 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3398 }
3399
3400 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3401 (1 + semantics.size()) * sizeof(Expr*),
3402 llvm::alignOf<PseudoObjectExpr>());
3403 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3404 resultIndex);
3405}
3406
3407PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3408 Expr *syntax, ArrayRef<Expr*> semantics,
3409 unsigned resultIndex)
3410 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3411 /*filled in at end of ctor*/ false, false, false, false) {
3412 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3413 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3414
3415 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3416 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3417 getSubExprsBuffer()[i] = E;
3418
3419 if (E->isTypeDependent())
3420 ExprBits.TypeDependent = true;
3421 if (E->isValueDependent())
3422 ExprBits.ValueDependent = true;
3423 if (E->isInstantiationDependent())
3424 ExprBits.InstantiationDependent = true;
3425 if (E->containsUnexpandedParameterPack())
3426 ExprBits.ContainsUnexpandedParameterPack = true;
3427
3428 if (isa<OpaqueValueExpr>(E))
3429 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3430 "opaque-value semantic expressions for pseudo-object "
3431 "operations must have sources");
3432 }
3433}
3434
Douglas Gregor05c13a32009-01-22 00:58:24 +00003435//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003436// ExprIterator.
3437//===----------------------------------------------------------------------===//
3438
3439Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3440Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3441Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3442const Expr* ConstExprIterator::operator[](size_t idx) const {
3443 return cast<Expr>(I[idx]);
3444}
3445const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3446const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3447
3448//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003449// Child Iterators for iterating over subexpressions/substatements
3450//===----------------------------------------------------------------------===//
3451
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003452// UnaryExprOrTypeTraitExpr
3453Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003454 // If this is of a type and the type is a VLA type (and not a typedef), the
3455 // size expression of the VLA needs to be treated as an executable expression.
3456 // Why isn't this weirdness documented better in StmtIterator?
3457 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003458 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003459 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003460 return child_range(child_iterator(T), child_iterator());
3461 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003462 }
John McCall63c00d72011-02-09 08:16:59 +00003463 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003464}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003465
Steve Naroff563477d2007-09-18 23:55:05 +00003466// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003467Stmt::child_range ObjCMessageExpr::children() {
3468 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003469 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003470 begin = reinterpret_cast<Stmt **>(this + 1);
3471 else
3472 begin = reinterpret_cast<Stmt **>(getArgs());
3473 return child_range(begin,
3474 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003475}
3476
Steve Naroff4eb206b2008-09-03 18:15:37 +00003477// Blocks
John McCall6b5a61b2011-02-07 10:33:21 +00003478BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003479 SourceLocation l, bool ByRef,
John McCall6b5a61b2011-02-07 10:33:21 +00003480 bool constAdded)
Douglas Gregor561f8122011-07-01 01:22:09 +00003481 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregora779d9c2011-01-19 21:32:01 +00003482 d->isParameterPack()),
John McCall6b5a61b2011-02-07 10:33:21 +00003483 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregora779d9c2011-01-19 21:32:01 +00003484{
Douglas Gregord967e312011-01-19 21:52:31 +00003485 bool TypeDependent = false;
3486 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +00003487 bool InstantiationDependent = false;
3488 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3489 InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +00003490 ExprBits.TypeDependent = TypeDependent;
3491 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +00003492 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregora779d9c2011-01-19 21:32:01 +00003493}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003494
3495
3496AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3497 QualType t, AtomicOp op, SourceLocation RP)
3498 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3499 false, false, false, false),
3500 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3501{
3502 for (unsigned i = 0; i < nexpr; i++) {
3503 if (args[i]->isTypeDependent())
3504 ExprBits.TypeDependent = true;
3505 if (args[i]->isValueDependent())
3506 ExprBits.ValueDependent = true;
3507 if (args[i]->isInstantiationDependent())
3508 ExprBits.InstantiationDependent = true;
3509 if (args[i]->containsUnexpandedParameterPack())
3510 ExprBits.ContainsUnexpandedParameterPack = true;
3511
3512 SubExprs[i] = args[i];
3513 }
3514}