blob: f95ca17306729f1c336515f3004a28995444577f [file] [log] [blame]
Chris Lattner1b926492006-08-23 06:42:10 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner1b926492006-08-23 06:42:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbar6e8aa532008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor96ee7892009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner5c4664e2007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattner86ee2862008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor9a657932008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregorded2d7b2009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Chris Lattner5e9a8782006-11-04 06:21:51 +000022#include "clang/AST/StmtVisitor.h"
Chris Lattnere925d612010-11-17 07:37:15 +000023#include "clang/Lex/LiteralSupport.h"
24#include "clang/Lex/Lexer.h"
Richard Smith938f40b2011-06-11 17:19:42 +000025#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner15ba9492009-06-14 01:54:56 +000026#include "clang/Basic/Builtins.h"
Chris Lattnere925d612010-11-17 07:37:15 +000027#include "clang/Basic/SourceManager.h"
Chris Lattnera7944d82007-11-27 18:22:04 +000028#include "clang/Basic/TargetInfo.h"
Douglas Gregor0840cc02009-11-01 20:32:48 +000029#include "llvm/Support/ErrorHandling.h"
Anders Carlsson2fb08242009-09-08 18:24:21 +000030#include "llvm/Support/raw_ostream.h"
Douglas Gregord5846a12009-04-15 06:41:24 +000031#include <algorithm>
Eli Friedmanfcec6302011-11-01 02:23:42 +000032#include <cstring>
Chris Lattner1b926492006-08-23 06:42:10 +000033using namespace clang;
34
Chris Lattner4ebae652010-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 Collingbourne91147592011-04-15 00:35:48 +000040 const Expr *E = IgnoreParens();
41
Chris Lattner4ebae652010-04-16 23:34:13 +000042 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbourne91147592011-04-15 00:35:48 +000043 if (E->getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000044 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbourne91147592011-04-15 00:35:48 +000045 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +000046
Peter Collingbourne91147592011-04-15 00:35:48 +000047 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +000048 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +000049 case UO_Plus:
Chris Lattner4ebae652010-04-16 23:34:13 +000050 return UO->getSubExpr()->isKnownToHaveBooleanValue();
51 default:
52 return false;
53 }
54 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000055
John McCall45d30c32010-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 Collingbourne91147592011-04-15 00:35:48 +000058 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +000059 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000060
Peter Collingbourne91147592011-04-15 00:35:48 +000061 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +000062 switch (BO->getOpcode()) {
63 default: return false;
John McCalle3027922010-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 Lattner4ebae652010-04-16 23:34:13 +000072 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000073
John McCalle3027922010-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 Lattner4ebae652010-04-16 23:34:13 +000077 // Handle things like (x==2)|(y==12).
78 return BO->getLHS()->isKnownToHaveBooleanValue() &&
79 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000080
John McCalle3027922010-08-25 11:45:40 +000081 case BO_Comma:
82 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +000083 return BO->getRHS()->isKnownToHaveBooleanValue();
84 }
85 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000086
Peter Collingbourne91147592011-04-15 00:35:48 +000087 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +000088 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
89 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000090
Chris Lattner4ebae652010-04-16 23:34:13 +000091 return false;
92}
93
John McCallbd066782011-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 McCallbd066782011-02-09 08:16:59 +0000127}
128
Chris Lattner0eedafe2006-08-24 04:56:27 +0000129//===----------------------------------------------------------------------===//
130// Primary Expressions.
131//===----------------------------------------------------------------------===//
132
Douglas Gregor678d76c2011-07-01 01:22:09 +0000133/// \brief Compute the type-, value-, and instantiation-dependence of a
134/// declaration reference
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000135/// based on the declaration being referenced.
136static void computeDeclRefDependence(NamedDecl *D, QualType T,
137 bool &TypeDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000138 bool &ValueDependent,
139 bool &InstantiationDependent) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000140 TypeDependent = false;
141 ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000142 InstantiationDependent = false;
Douglas Gregored6c7442009-11-23 11:41:28 +0000143
144 // (TD) C++ [temp.dep.expr]p3:
145 // An id-expression is type-dependent if it contains:
146 //
Alexis Hunta8136cc2010-05-05 15:23:54 +0000147 // and
Douglas Gregored6c7442009-11-23 11:41:28 +0000148 //
149 // (VD) C++ [temp.dep.constexpr]p2:
150 // An identifier is value-dependent if it is:
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000151
Douglas Gregored6c7442009-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 Gregorf144f4f2011-01-19 21:52:31 +0000154 if (T->isDependentType()) {
155 TypeDependent = true;
156 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000157 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000158 return;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000159 } else if (T->isInstantiationDependentType()) {
160 InstantiationDependent = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000161 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000162
Douglas Gregored6c7442009-11-23 11:41:28 +0000163 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000164 if (D->getDeclName().getNameKind()
Douglas Gregor678d76c2011-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 Gregored6c7442009-11-23 11:41:28 +0000176 }
Douglas Gregor678d76c2011-07-01 01:22:09 +0000177
Douglas Gregored6c7442009-11-23 11:41:28 +0000178 // (VD) - the name of a non-type template parameter,
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000179 if (isa<NonTypeTemplateParmDecl>(D)) {
180 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000181 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000182 return;
183 }
184
Douglas Gregored6c7442009-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 Smithec8dcd22011-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 Gregorf144f4f2011-01-19 21:52:31 +0000192 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Richard Smithec8dcd22011-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 Redl5ca79842010-02-01 20:16:42 +0000198 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000199 if (Init->isValueDependent()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000200 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000201 InstantiationDependent = true;
202 }
Richard Smithec8dcd22011-11-08 01:31:09 +0000203 }
204
Douglas Gregor0e4de762010-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 Smithec8dcd22011-11-08 01:31:09 +0000208 if (Var->isStaticDataMember() &&
209 Var->getDeclContext()->isDependentContext()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000210 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000211 InstantiationDependent = true;
212 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000213
214 return;
215 }
216
Douglas Gregor0e4de762010-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 Gregorf144f4f2011-01-19 21:52:31 +0000220 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
221 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000222 InstantiationDependent = true;
Richard Smithec8dcd22011-11-08 01:31:09 +0000223 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000224}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000225
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000226void DeclRefExpr::computeDependence() {
227 bool TypeDependent = false;
228 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000229 bool InstantiationDependent = false;
230 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
231 InstantiationDependent);
Douglas Gregorf144f4f2011-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 Gregor678d76c2011-07-01 01:22:09 +0000244 getNumTemplateArgs(),
245 InstantiationDependent)) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000246 TypeDependent = true;
247 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000248 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000249 }
250
251 ExprBits.TypeDependent = TypeDependent;
252 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000253 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000254
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000255 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000256 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000257 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000258}
259
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000260DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000261 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000262 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000263 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000264 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000265 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000266 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000267 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
268 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000269 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000270 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000271 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
272 if (FoundD)
273 getInternalFoundDecl() = FoundD;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000274 DeclRefExprBits.HasTemplateKWAndArgsInfo
275 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000276 if (TemplateArgs) {
277 bool Dependent = false;
278 bool InstantiationDependent = false;
279 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000280 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
281 Dependent,
282 InstantiationDependent,
283 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000284 if (InstantiationDependent)
285 setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000286 } else if (TemplateKWLoc.isValid()) {
287 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000288 }
Benjamin Kramer138ef9c2011-10-10 12:54:05 +0000289 DeclRefExprBits.HadMultipleCandidates = 0;
290
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000291 computeDependence();
292}
293
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000294DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000295 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000296 SourceLocation TemplateKWLoc,
John McCallce546572009-12-08 09:08:17 +0000297 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000298 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000299 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000300 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000301 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000302 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +0000303 return Create(Context, QualifierLoc, TemplateKWLoc, D,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000304 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000305 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000306}
307
308DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000309 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000310 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000311 ValueDecl *D,
312 const DeclarationNameInfo &NameInfo,
313 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000314 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000315 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000316 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000317 // Filter out cases where the found Decl is the same as the value refenenced.
318 if (D == FoundD)
319 FoundD = 0;
320
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000321 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregorea972d32011-02-28 21:54:11 +0000322 if (QualifierLoc != 0)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000323 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000324 if (FoundD)
325 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000326 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000327 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
328 else if (TemplateKWLoc.isValid())
329 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000330
Chris Lattner5c0b4052010-10-30 05:14:06 +0000331 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000332 return new (Mem) DeclRefExpr(QualifierLoc, TemplateKWLoc, D, NameInfo,
333 FoundD, TemplateArgs, T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000334}
335
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000336DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000337 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000338 bool HasFoundDecl,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000339 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000340 unsigned NumTemplateArgs) {
341 std::size_t Size = sizeof(DeclRefExpr);
342 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000343 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000344 if (HasFoundDecl)
345 Size += sizeof(NamedDecl *);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000346 if (HasTemplateKWAndArgsInfo)
347 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000348
Chris Lattner5c0b4052010-10-30 05:14:06 +0000349 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000350 return new (Mem) DeclRefExpr(EmptyShell());
351}
352
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000353SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000354 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000355 if (hasQualifier())
Douglas Gregorea972d32011-02-28 21:54:11 +0000356 R.setBegin(getQualifierLoc().getBeginLoc());
John McCallb3774b52010-08-19 23:49:38 +0000357 if (hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000358 R.setEnd(getRAngleLoc());
359 return R;
360}
361
Anders Carlsson2fb08242009-09-08 18:24:21 +0000362// FIXME: Maybe this should use DeclPrinter with a special "print predefined
363// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000364std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
365 ASTContext &Context = CurrentDecl->getASTContext();
366
Anders Carlsson2fb08242009-09-08 18:24:21 +0000367 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000368 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000369 return FD->getNameAsString();
370
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000371 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000372 llvm::raw_svector_ostream Out(Name);
373
374 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000375 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000376 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000377 if (MD->isStatic())
378 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000379 }
380
381 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000382
383 std::string Proto = FD->getQualifiedNameAsString(Policy);
384
John McCall9dd450b2009-09-21 23:43:11 +0000385 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000386 const FunctionProtoType *FT = 0;
387 if (FD->hasWrittenPrototype())
388 FT = dyn_cast<FunctionProtoType>(AFT);
389
390 Proto += "(";
391 if (FT) {
392 llvm::raw_string_ostream POut(Proto);
393 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
394 if (i) POut << ", ";
395 std::string Param;
396 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
397 POut << Param;
398 }
399
400 if (FT->isVariadic()) {
401 if (FD->getNumParams()) POut << ", ";
402 POut << "...";
403 }
404 }
405 Proto += ")";
406
Sam Weinig4e83bd22009-12-27 01:38:20 +0000407 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
408 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
409 if (ThisQuals.hasConst())
410 Proto += " const";
411 if (ThisQuals.hasVolatile())
412 Proto += " volatile";
413 }
414
Sam Weinigd060ed42009-12-06 23:55:13 +0000415 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
416 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000417
418 Out << Proto;
419
420 Out.flush();
421 return Name.str().str();
422 }
423 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000424 SmallString<256> Name;
Anders Carlsson2fb08242009-09-08 18:24:21 +0000425 llvm::raw_svector_ostream Out(Name);
426 Out << (MD->isInstanceMethod() ? '-' : '+');
427 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000428
429 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
430 // a null check to avoid a crash.
431 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb89514a2011-10-14 18:45:37 +0000432 Out << *ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000433
Anders Carlsson2fb08242009-09-08 18:24:21 +0000434 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000435 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramer2f569922012-02-07 11:57:45 +0000436 Out << '(' << *CID << ')';
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000437
Anders Carlsson2fb08242009-09-08 18:24:21 +0000438 Out << ' ';
439 Out << MD->getSelector().getAsString();
440 Out << ']';
441
442 Out.flush();
443 return Name.str().str();
444 }
445 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
446 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
447 return "top level";
448 }
449 return "";
450}
451
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000452void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
453 if (hasAllocation())
454 C.Deallocate(pVal);
455
456 BitWidth = Val.getBitWidth();
457 unsigned NumWords = Val.getNumWords();
458 const uint64_t* Words = Val.getRawData();
459 if (NumWords > 1) {
460 pVal = new (C) uint64_t[NumWords];
461 std::copy(Words, Words + NumWords, pVal);
462 } else if (NumWords == 1)
463 VAL = Words[0];
464 else
465 VAL = 0;
466}
467
468IntegerLiteral *
469IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
470 QualType type, SourceLocation l) {
471 return new (C) IntegerLiteral(C, V, type, l);
472}
473
474IntegerLiteral *
475IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
476 return new (C) IntegerLiteral(Empty);
477}
478
479FloatingLiteral *
480FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
481 bool isexact, QualType Type, SourceLocation L) {
482 return new (C) FloatingLiteral(C, V, isexact, Type, L);
483}
484
485FloatingLiteral *
486FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka428f5b22012-01-10 22:40:09 +0000487 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000488}
489
Chris Lattnera0173132008-06-07 22:13:43 +0000490/// getValueAsApproximateDouble - This returns the value as an inaccurate
491/// double. Note that this may cause loss of precision, but is useful for
492/// debugging dumps, etc.
493double FloatingLiteral::getValueAsApproximateDouble() const {
494 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000495 bool ignored;
496 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
497 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000498 return V.convertToDouble();
499}
500
Eli Friedman48fd89a2012-01-06 20:42:20 +0000501int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
502 int CharByteWidth;
Eli Friedmanfcec6302011-11-01 02:23:42 +0000503 switch(k) {
504 case Ascii:
505 case UTF8:
506 CharByteWidth = target.getCharWidth();
507 break;
508 case Wide:
509 CharByteWidth = target.getWCharWidth();
510 break;
511 case UTF16:
512 CharByteWidth = target.getChar16Width();
513 break;
514 case UTF32:
515 CharByteWidth = target.getChar32Width();
516 }
517 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
518 CharByteWidth /= 8;
519 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
520 && "character byte widths supported are 1, 2, and 4 only");
521 return CharByteWidth;
522}
523
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000524StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000525 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000526 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000527 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000528 // Allocate enough space for the StringLiteral plus an array of locations for
529 // any concatenated string tokens.
530 void *Mem = C.Allocate(sizeof(StringLiteral)+
531 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000532 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000533 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000534
Steve Naroffdf7855b2007-02-21 23:46:25 +0000535 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedmanfcec6302011-11-01 02:23:42 +0000536 SL->setString(C,Str,Kind,Pascal);
537
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000538 SL->TokLocs[0] = Loc[0];
539 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000540
Chris Lattner630970d2009-02-18 05:49:11 +0000541 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000542 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
543 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000544}
545
Douglas Gregor958dfc92009-04-15 16:35:07 +0000546StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
547 void *Mem = C.Allocate(sizeof(StringLiteral)+
548 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000549 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000550 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedmanfcec6302011-11-01 02:23:42 +0000551 SL->CharByteWidth = 0;
552 SL->Length = 0;
Douglas Gregor958dfc92009-04-15 16:35:07 +0000553 SL->NumConcatenated = NumStrs;
554 return SL;
555}
556
Eli Friedmanfcec6302011-11-01 02:23:42 +0000557void StringLiteral::setString(ASTContext &C, StringRef Str,
558 StringKind Kind, bool IsPascal) {
559 //FIXME: we assume that the string data comes from a target that uses the same
560 // code unit size and endianess for the type of string.
561 this->Kind = Kind;
562 this->IsPascal = IsPascal;
563
564 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
565 assert((Str.size()%CharByteWidth == 0)
566 && "size of data must be multiple of CharByteWidth");
567 Length = Str.size()/CharByteWidth;
568
569 switch(CharByteWidth) {
570 case 1: {
571 char *AStrData = new (C) char[Length];
572 std::memcpy(AStrData,Str.data(),Str.size());
573 StrData.asChar = AStrData;
574 break;
575 }
576 case 2: {
577 uint16_t *AStrData = new (C) uint16_t[Length];
578 std::memcpy(AStrData,Str.data(),Str.size());
579 StrData.asUInt16 = AStrData;
580 break;
581 }
582 case 4: {
583 uint32_t *AStrData = new (C) uint32_t[Length];
584 std::memcpy(AStrData,Str.data(),Str.size());
585 StrData.asUInt32 = AStrData;
586 break;
587 }
588 default:
589 assert(false && "unsupported CharByteWidth");
590 }
Douglas Gregor958dfc92009-04-15 16:35:07 +0000591}
592
Chris Lattnere925d612010-11-17 07:37:15 +0000593/// getLocationOfByte - Return a source location that points to the specified
594/// byte of this string literal.
595///
596/// Strings are amazingly complex. They can be formed from multiple tokens and
597/// can have escape sequences in them in addition to the usual trigraph and
598/// escaped newline business. This routine handles this complexity.
599///
600SourceLocation StringLiteral::
601getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
602 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregorfb65e592011-07-27 05:40:30 +0000603 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
604
Chris Lattnere925d612010-11-17 07:37:15 +0000605 // Loop over all of the tokens in this string until we find the one that
606 // contains the byte we're looking for.
607 unsigned TokNo = 0;
608 while (1) {
609 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
610 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
611
612 // Get the spelling of the string so that we can get the data that makes up
613 // the string literal, not the identifier for the macro it is potentially
614 // expanded through.
615 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
616
617 // Re-lex the token to get its length and original spelling.
618 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
619 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000620 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +0000621 if (Invalid)
622 return StrTokSpellingLoc;
623
624 const char *StrData = Buffer.data()+LocInfo.second;
625
626 // Create a langops struct and enable trigraphs. This is sufficient for
627 // relexing tokens.
628 LangOptions LangOpts;
629 LangOpts.Trigraphs = true;
630
631 // Create a lexer starting at the beginning of this token.
632 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
633 Buffer.end());
634 Token TheTok;
635 TheLexer.LexFromRawLexer(TheTok);
636
637 // Use the StringLiteralParser to compute the length of the string in bytes.
638 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
639 unsigned TokNumBytes = SLP.GetStringLength();
640
641 // If the byte is in this token, return the location of the byte.
642 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +0000643 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +0000644 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
645
646 // Now that we know the offset of the token in the spelling, use the
647 // preprocessor to get the offset in the original source.
648 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
649 }
650
651 // Move to the next string token.
652 ++TokNo;
653 ByteNo -= TokNumBytes;
654 }
655}
656
657
658
Chris Lattner1b926492006-08-23 06:42:10 +0000659/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
660/// corresponds to, e.g. "sizeof" or "[pre]++".
661const char *UnaryOperator::getOpcodeStr(Opcode Op) {
662 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +0000663 case UO_PostInc: return "++";
664 case UO_PostDec: return "--";
665 case UO_PreInc: return "++";
666 case UO_PreDec: return "--";
667 case UO_AddrOf: return "&";
668 case UO_Deref: return "*";
669 case UO_Plus: return "+";
670 case UO_Minus: return "-";
671 case UO_Not: return "~";
672 case UO_LNot: return "!";
673 case UO_Real: return "__real";
674 case UO_Imag: return "__imag";
675 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000676 }
David Blaikief47fa302012-01-17 02:30:50 +0000677 llvm_unreachable("Unknown unary operator");
Chris Lattner1b926492006-08-23 06:42:10 +0000678}
679
John McCalle3027922010-08-25 11:45:40 +0000680UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000681UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
682 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +0000683 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000684 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
685 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
686 case OO_Amp: return UO_AddrOf;
687 case OO_Star: return UO_Deref;
688 case OO_Plus: return UO_Plus;
689 case OO_Minus: return UO_Minus;
690 case OO_Tilde: return UO_Not;
691 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000692 }
693}
694
695OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
696 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000697 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
698 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
699 case UO_AddrOf: return OO_Amp;
700 case UO_Deref: return OO_Star;
701 case UO_Plus: return OO_Plus;
702 case UO_Minus: return OO_Minus;
703 case UO_Not: return OO_Tilde;
704 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000705 default: return OO_None;
706 }
707}
708
709
Chris Lattner0eedafe2006-08-24 04:56:27 +0000710//===----------------------------------------------------------------------===//
711// Postfix Operators.
712//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000713
Peter Collingbourne3a347252011-02-08 21:18:02 +0000714CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
715 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCall7decc9e2010-11-18 06:31:45 +0000716 SourceLocation rparenloc)
717 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000718 fn->isTypeDependent(),
719 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000720 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000721 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000722 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000723
Peter Collingbourne3a347252011-02-08 21:18:02 +0000724 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +0000725 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000726 for (unsigned i = 0; i != numargs; ++i) {
727 if (args[i]->isTypeDependent())
728 ExprBits.TypeDependent = true;
729 if (args[i]->isValueDependent())
730 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000731 if (args[i]->isInstantiationDependent())
732 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000733 if (args[i]->containsUnexpandedParameterPack())
734 ExprBits.ContainsUnexpandedParameterPack = true;
735
Peter Collingbourne3a347252011-02-08 21:18:02 +0000736 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000737 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000738
Peter Collingbourne3a347252011-02-08 21:18:02 +0000739 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +0000740 RParenLoc = rparenloc;
741}
Nate Begeman1e36a852008-01-17 17:46:27 +0000742
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000743CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCall7decc9e2010-11-18 06:31:45 +0000744 QualType t, ExprValueKind VK, SourceLocation rparenloc)
745 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000746 fn->isTypeDependent(),
747 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000748 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000749 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000750 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000751
Peter Collingbourne3a347252011-02-08 21:18:02 +0000752 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000753 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000754 for (unsigned i = 0; i != numargs; ++i) {
755 if (args[i]->isTypeDependent())
756 ExprBits.TypeDependent = true;
757 if (args[i]->isValueDependent())
758 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000759 if (args[i]->isInstantiationDependent())
760 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000761 if (args[i]->containsUnexpandedParameterPack())
762 ExprBits.ContainsUnexpandedParameterPack = true;
763
Peter Collingbourne3a347252011-02-08 21:18:02 +0000764 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000765 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000766
Peter Collingbourne3a347252011-02-08 21:18:02 +0000767 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000768 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000769}
770
Mike Stump11289f42009-09-09 15:08:12 +0000771CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
772 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000773 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +0000774 SubExprs = new (C) Stmt*[PREARGS_START];
775 CallExprBits.NumPreArgs = 0;
776}
777
778CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
779 EmptyShell Empty)
780 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
781 // FIXME: Why do we allocate this?
782 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
783 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +0000784}
785
Nuno Lopes518e3702009-12-20 23:11:08 +0000786Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +0000787 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +0000788
789 while (SubstNonTypeTemplateParmExpr *NTTP
790 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
791 CEE = NTTP->getReplacement()->IgnoreParenCasts();
792 }
793
Sebastian Redl2b1832e2010-09-10 20:55:30 +0000794 // If we're calling a dereference, look at the pointer instead.
795 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
796 if (BO->isPtrMemOp())
797 CEE = BO->getRHS()->IgnoreParenCasts();
798 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
799 if (UO->getOpcode() == UO_Deref)
800 CEE = UO->getSubExpr()->IgnoreParenCasts();
801 }
Chris Lattner52301912009-07-17 15:46:27 +0000802 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000803 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000804 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
805 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000806
807 return 0;
808}
809
Nuno Lopes518e3702009-12-20 23:11:08 +0000810FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000811 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000812}
813
Chris Lattnere4407ed2007-12-28 05:25:02 +0000814/// setNumArgs - This changes the number of arguments present in this call.
815/// Any orphaned expressions are deleted by this, and any new operands are set
816/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000817void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000818 // No change, just return.
819 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000820
Chris Lattnere4407ed2007-12-28 05:25:02 +0000821 // If shrinking # arguments, just delete the extras and forgot them.
822 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000823 this->NumArgs = NumArgs;
824 return;
825 }
826
827 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000828 unsigned NumPreArgs = getNumPreArgs();
829 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000830 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000831 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000832 NewSubExprs[i] = SubExprs[i];
833 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000834 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
835 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000836 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000837
Douglas Gregorba6e5572009-04-17 21:46:47 +0000838 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000839 SubExprs = NewSubExprs;
840 this->NumArgs = NumArgs;
841}
842
Chris Lattner01ff98a2008-10-06 05:00:53 +0000843/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
844/// not, return 0.
Richard Smithd62306a2011-11-10 06:34:14 +0000845unsigned CallExpr::isBuiltinCall() const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000846 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000847 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000848 // ImplicitCastExpr.
849 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
850 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000851 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000852
Steve Narofff6e3b3292008-01-31 01:07:12 +0000853 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
854 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000855 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000856
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000857 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
858 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000859 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000860
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000861 if (!FDecl->getIdentifier())
862 return 0;
863
Douglas Gregor15fc9562009-09-12 00:22:50 +0000864 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000865}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000866
Anders Carlsson00a27592009-05-26 04:57:27 +0000867QualType CallExpr::getCallReturnType() const {
868 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000869 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000870 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000871 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000872 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +0000873 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
874 // This should never be overloaded and so should never return null.
875 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +0000876
John McCall0009fcc2011-04-26 20:42:42 +0000877 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000878 return FnType->getResultType();
879}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000880
John McCall701417a2011-02-21 06:23:05 +0000881SourceRange CallExpr::getSourceRange() const {
882 if (isa<CXXOperatorCallExpr>(this))
883 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
884
885 SourceLocation begin = getCallee()->getLocStart();
886 if (begin.isInvalid() && getNumArgs() > 0)
887 begin = getArg(0)->getLocStart();
888 SourceLocation end = getRParenLoc();
889 if (end.isInvalid() && getNumArgs() > 0)
890 end = getArg(getNumArgs() - 1)->getLocEnd();
891 return SourceRange(begin, end);
892}
893
Alexis Hunta8136cc2010-05-05 15:23:54 +0000894OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000895 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000896 TypeSourceInfo *tsi,
897 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000898 Expr** exprsPtr, unsigned numExprs,
899 SourceLocation RParenLoc) {
900 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000901 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000902 sizeof(Expr*) * numExprs);
903
904 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
905 exprsPtr, numExprs, RParenLoc);
906}
907
908OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
909 unsigned numComps, unsigned numExprs) {
910 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
911 sizeof(OffsetOfNode) * numComps +
912 sizeof(Expr*) * numExprs);
913 return new (Mem) OffsetOfExpr(numComps, numExprs);
914}
915
Alexis Hunta8136cc2010-05-05 15:23:54 +0000916OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000917 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000918 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000919 Expr** exprsPtr, unsigned numExprs,
920 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +0000921 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
922 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000923 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000924 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000925 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000926 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
927 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000928{
929 for(unsigned i = 0; i < numComps; ++i) {
930 setComponent(i, compsPtr[i]);
931 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000932
Douglas Gregor882211c2010-04-28 22:16:22 +0000933 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000934 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
935 ExprBits.ValueDependent = true;
936 if (exprsPtr[i]->containsUnexpandedParameterPack())
937 ExprBits.ContainsUnexpandedParameterPack = true;
938
Douglas Gregor882211c2010-04-28 22:16:22 +0000939 setIndexExpr(i, exprsPtr[i]);
940 }
941}
942
943IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
944 assert(getKind() == Field || getKind() == Identifier);
945 if (getKind() == Field)
946 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000947
Douglas Gregor882211c2010-04-28 22:16:22 +0000948 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
949}
950
Mike Stump11289f42009-09-09 15:08:12 +0000951MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +0000952 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000953 SourceLocation TemplateKWLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000954 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000955 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000956 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +0000957 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +0000958 QualType ty,
959 ExprValueKind vk,
960 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000961 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000962
Douglas Gregorea972d32011-02-28 21:54:11 +0000963 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +0000964 founddecl.getDecl() != memberdecl ||
965 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000966 if (hasQualOrFound)
967 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000968
John McCall6b51f282009-11-23 01:53:49 +0000969 if (targs)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000970 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
971 else if (TemplateKWLoc.isValid())
972 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump11289f42009-09-09 15:08:12 +0000973
Chris Lattner5c0b4052010-10-30 05:14:06 +0000974 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +0000975 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
976 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +0000977
978 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000979 // FIXME: Wrong. We should be looking at the member declaration we found.
980 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +0000981 E->setValueDependent(true);
982 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000983 E->setInstantiationDependent(true);
984 }
985 else if (QualifierLoc &&
986 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
987 E->setInstantiationDependent(true);
988
John McCall16df1e52010-03-30 21:47:33 +0000989 E->HasQualifierOrFoundDecl = true;
990
991 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +0000992 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +0000993 NQ->FoundDecl = founddecl;
994 }
995
Abramo Bagnara7945c982012-01-27 09:46:47 +0000996 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
997
John McCall16df1e52010-03-30 21:47:33 +0000998 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000999 bool Dependent = false;
1000 bool InstantiationDependent = false;
1001 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001002 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1003 Dependent,
1004 InstantiationDependent,
1005 ContainsUnexpandedParameterPack);
Douglas Gregor678d76c2011-07-01 01:22:09 +00001006 if (InstantiationDependent)
1007 E->setInstantiationDependent(true);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001008 } else if (TemplateKWLoc.isValid()) {
1009 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall16df1e52010-03-30 21:47:33 +00001010 }
1011
1012 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001013}
1014
Douglas Gregor25b7e052011-03-02 21:06:53 +00001015SourceRange MemberExpr::getSourceRange() const {
1016 SourceLocation StartLoc;
1017 if (isImplicitAccess()) {
1018 if (hasQualifier())
1019 StartLoc = getQualifierLoc().getBeginLoc();
1020 else
1021 StartLoc = MemberLoc;
1022 } else {
1023 // FIXME: We don't want this to happen. Rather, we should be able to
1024 // detect all kinds of implicit accesses more cleanly.
1025 StartLoc = getBase()->getLocStart();
1026 if (StartLoc.isInvalid())
1027 StartLoc = MemberLoc;
1028 }
Abramo Bagnara7945c982012-01-27 09:46:47 +00001029
1030 SourceLocation EndLoc = hasExplicitTemplateArgs()
1031 ? getRAngleLoc() : getMemberNameInfo().getEndLoc();
1032
Douglas Gregor25b7e052011-03-02 21:06:53 +00001033 return SourceRange(StartLoc, EndLoc);
1034}
1035
John McCall9320b872011-09-09 05:25:32 +00001036void CastExpr::CheckCastConsistency() const {
1037 switch (getCastKind()) {
1038 case CK_DerivedToBase:
1039 case CK_UncheckedDerivedToBase:
1040 case CK_DerivedToBaseMemberPointer:
1041 case CK_BaseToDerived:
1042 case CK_BaseToDerivedMemberPointer:
1043 assert(!path_empty() && "Cast kind should have a base path!");
1044 break;
1045
1046 case CK_CPointerToObjCPointerCast:
1047 assert(getType()->isObjCObjectPointerType());
1048 assert(getSubExpr()->getType()->isPointerType());
1049 goto CheckNoBasePath;
1050
1051 case CK_BlockPointerToObjCPointerCast:
1052 assert(getType()->isObjCObjectPointerType());
1053 assert(getSubExpr()->getType()->isBlockPointerType());
1054 goto CheckNoBasePath;
1055
John McCallc62bb392012-02-15 01:22:51 +00001056 case CK_ReinterpretMemberPointer:
1057 assert(getType()->isMemberPointerType());
1058 assert(getSubExpr()->getType()->isMemberPointerType());
1059 goto CheckNoBasePath;
1060
John McCall9320b872011-09-09 05:25:32 +00001061 case CK_BitCast:
1062 // Arbitrary casts to C pointer types count as bitcasts.
1063 // Otherwise, we should only have block and ObjC pointer casts
1064 // here if they stay within the type kind.
1065 if (!getType()->isPointerType()) {
1066 assert(getType()->isObjCObjectPointerType() ==
1067 getSubExpr()->getType()->isObjCObjectPointerType());
1068 assert(getType()->isBlockPointerType() ==
1069 getSubExpr()->getType()->isBlockPointerType());
1070 }
1071 goto CheckNoBasePath;
1072
1073 case CK_AnyPointerToBlockPointerCast:
1074 assert(getType()->isBlockPointerType());
1075 assert(getSubExpr()->getType()->isAnyPointerType() &&
1076 !getSubExpr()->getType()->isBlockPointerType());
1077 goto CheckNoBasePath;
1078
Douglas Gregored90df32012-02-22 05:02:47 +00001079 case CK_CopyAndAutoreleaseBlockObject:
1080 assert(getType()->isBlockPointerType());
1081 assert(getSubExpr()->getType()->isBlockPointerType());
1082 goto CheckNoBasePath;
1083
John McCall9320b872011-09-09 05:25:32 +00001084 // These should not have an inheritance path.
1085 case CK_Dynamic:
1086 case CK_ToUnion:
1087 case CK_ArrayToPointerDecay:
1088 case CK_FunctionToPointerDecay:
1089 case CK_NullToMemberPointer:
1090 case CK_NullToPointer:
1091 case CK_ConstructorConversion:
1092 case CK_IntegralToPointer:
1093 case CK_PointerToIntegral:
1094 case CK_ToVoid:
1095 case CK_VectorSplat:
1096 case CK_IntegralCast:
1097 case CK_IntegralToFloating:
1098 case CK_FloatingToIntegral:
1099 case CK_FloatingCast:
1100 case CK_ObjCObjectLValueCast:
1101 case CK_FloatingRealToComplex:
1102 case CK_FloatingComplexToReal:
1103 case CK_FloatingComplexCast:
1104 case CK_FloatingComplexToIntegralComplex:
1105 case CK_IntegralRealToComplex:
1106 case CK_IntegralComplexToReal:
1107 case CK_IntegralComplexCast:
1108 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001109 case CK_ARCProduceObject:
1110 case CK_ARCConsumeObject:
1111 case CK_ARCReclaimReturnedObject:
1112 case CK_ARCExtendBlockObject:
John McCall9320b872011-09-09 05:25:32 +00001113 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1114 goto CheckNoBasePath;
1115
1116 case CK_Dependent:
1117 case CK_LValueToRValue:
John McCall9320b872011-09-09 05:25:32 +00001118 case CK_NoOp:
David Chisnallfa35df62012-01-16 17:27:18 +00001119 case CK_AtomicToNonAtomic:
1120 case CK_NonAtomicToAtomic:
John McCall9320b872011-09-09 05:25:32 +00001121 case CK_PointerToBoolean:
1122 case CK_IntegralToBoolean:
1123 case CK_FloatingToBoolean:
1124 case CK_MemberPointerToBoolean:
1125 case CK_FloatingComplexToBoolean:
1126 case CK_IntegralComplexToBoolean:
1127 case CK_LValueBitCast: // -> bool&
1128 case CK_UserDefinedConversion: // operator bool()
1129 CheckNoBasePath:
1130 assert(path_empty() && "Cast kind should not have a base path!");
1131 break;
1132 }
1133}
1134
Anders Carlsson496335e2009-09-03 00:59:21 +00001135const char *CastExpr::getCastKindName() const {
1136 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001137 case CK_Dependent:
1138 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001139 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001140 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001141 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001142 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001143 case CK_LValueToRValue:
1144 return "LValueToRValue";
John McCalle3027922010-08-25 11:45:40 +00001145 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001146 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001147 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001148 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001149 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001150 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001151 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001152 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001153 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001154 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001155 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001156 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001157 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001158 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001159 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001160 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001161 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001162 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001163 case CK_NullToPointer:
1164 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001165 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001166 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001167 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001168 return "DerivedToBaseMemberPointer";
John McCallc62bb392012-02-15 01:22:51 +00001169 case CK_ReinterpretMemberPointer:
1170 return "ReinterpretMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001171 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001172 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001173 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001174 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001175 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001176 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001177 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001178 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001179 case CK_PointerToBoolean:
1180 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001181 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001182 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001183 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001184 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001185 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001186 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001187 case CK_IntegralToBoolean:
1188 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001189 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001190 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001191 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001192 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001193 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001194 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001195 case CK_FloatingToBoolean:
1196 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001197 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001198 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001199 case CK_CPointerToObjCPointerCast:
1200 return "CPointerToObjCPointerCast";
1201 case CK_BlockPointerToObjCPointerCast:
1202 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001203 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001204 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001205 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001206 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001207 case CK_FloatingRealToComplex:
1208 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001209 case CK_FloatingComplexToReal:
1210 return "FloatingComplexToReal";
1211 case CK_FloatingComplexToBoolean:
1212 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001213 case CK_FloatingComplexCast:
1214 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001215 case CK_FloatingComplexToIntegralComplex:
1216 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001217 case CK_IntegralRealToComplex:
1218 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001219 case CK_IntegralComplexToReal:
1220 return "IntegralComplexToReal";
1221 case CK_IntegralComplexToBoolean:
1222 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001223 case CK_IntegralComplexCast:
1224 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001225 case CK_IntegralComplexToFloatingComplex:
1226 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001227 case CK_ARCConsumeObject:
1228 return "ARCConsumeObject";
1229 case CK_ARCProduceObject:
1230 return "ARCProduceObject";
1231 case CK_ARCReclaimReturnedObject:
1232 return "ARCReclaimReturnedObject";
1233 case CK_ARCExtendBlockObject:
1234 return "ARCCExtendBlockObject";
David Chisnallfa35df62012-01-16 17:27:18 +00001235 case CK_AtomicToNonAtomic:
1236 return "AtomicToNonAtomic";
1237 case CK_NonAtomicToAtomic:
1238 return "NonAtomicToAtomic";
Douglas Gregored90df32012-02-22 05:02:47 +00001239 case CK_CopyAndAutoreleaseBlockObject:
1240 return "CopyAndAutoreleaseBlockObject";
Anders Carlsson496335e2009-09-03 00:59:21 +00001241 }
Mike Stump11289f42009-09-09 15:08:12 +00001242
John McCallc5e62b42010-11-13 09:02:35 +00001243 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001244}
1245
Douglas Gregord196a582009-12-14 19:27:10 +00001246Expr *CastExpr::getSubExprAsWritten() {
1247 Expr *SubExpr = 0;
1248 CastExpr *E = this;
1249 do {
1250 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001251
1252 // Skip through reference binding to temporary.
1253 if (MaterializeTemporaryExpr *Materialize
1254 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1255 SubExpr = Materialize->GetTemporaryExpr();
1256
Douglas Gregord196a582009-12-14 19:27:10 +00001257 // Skip any temporary bindings; they're implicit.
1258 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1259 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001260
Douglas Gregord196a582009-12-14 19:27:10 +00001261 // Conversions by constructor and conversion functions have a
1262 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001263 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001264 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001265 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001266 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001267
Douglas Gregord196a582009-12-14 19:27:10 +00001268 // If the subexpression we're left with is an implicit cast, look
1269 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001270 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1271
Douglas Gregord196a582009-12-14 19:27:10 +00001272 return SubExpr;
1273}
1274
John McCallcf142162010-08-07 06:22:56 +00001275CXXBaseSpecifier **CastExpr::path_buffer() {
1276 switch (getStmtClass()) {
1277#define ABSTRACT_STMT(x)
1278#define CASTEXPR(Type, Base) \
1279 case Stmt::Type##Class: \
1280 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1281#define STMT(Type, Base)
1282#include "clang/AST/StmtNodes.inc"
1283 default:
1284 llvm_unreachable("non-cast expressions not possible here");
John McCallcf142162010-08-07 06:22:56 +00001285 }
1286}
1287
1288void CastExpr::setCastPath(const CXXCastPath &Path) {
1289 assert(Path.size() == path_size());
1290 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1291}
1292
1293ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1294 CastKind Kind, Expr *Operand,
1295 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001296 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001297 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1298 void *Buffer =
1299 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1300 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001301 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001302 if (PathSize) E->setCastPath(*BasePath);
1303 return E;
1304}
1305
1306ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1307 unsigned PathSize) {
1308 void *Buffer =
1309 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1310 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1311}
1312
1313
1314CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001315 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001316 const CXXCastPath *BasePath,
1317 TypeSourceInfo *WrittenTy,
1318 SourceLocation L, SourceLocation R) {
1319 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1320 void *Buffer =
1321 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1322 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001323 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001324 if (PathSize) E->setCastPath(*BasePath);
1325 return E;
1326}
1327
1328CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1329 void *Buffer =
1330 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1331 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1332}
1333
Chris Lattner1b926492006-08-23 06:42:10 +00001334/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1335/// corresponds to, e.g. "<<=".
1336const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1337 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001338 case BO_PtrMemD: return ".*";
1339 case BO_PtrMemI: return "->*";
1340 case BO_Mul: return "*";
1341 case BO_Div: return "/";
1342 case BO_Rem: return "%";
1343 case BO_Add: return "+";
1344 case BO_Sub: return "-";
1345 case BO_Shl: return "<<";
1346 case BO_Shr: return ">>";
1347 case BO_LT: return "<";
1348 case BO_GT: return ">";
1349 case BO_LE: return "<=";
1350 case BO_GE: return ">=";
1351 case BO_EQ: return "==";
1352 case BO_NE: return "!=";
1353 case BO_And: return "&";
1354 case BO_Xor: return "^";
1355 case BO_Or: return "|";
1356 case BO_LAnd: return "&&";
1357 case BO_LOr: return "||";
1358 case BO_Assign: return "=";
1359 case BO_MulAssign: return "*=";
1360 case BO_DivAssign: return "/=";
1361 case BO_RemAssign: return "%=";
1362 case BO_AddAssign: return "+=";
1363 case BO_SubAssign: return "-=";
1364 case BO_ShlAssign: return "<<=";
1365 case BO_ShrAssign: return ">>=";
1366 case BO_AndAssign: return "&=";
1367 case BO_XorAssign: return "^=";
1368 case BO_OrAssign: return "|=";
1369 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001370 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001371
David Blaikiee4d798f2012-01-20 21:50:17 +00001372 llvm_unreachable("Invalid OpCode!");
Chris Lattner1b926492006-08-23 06:42:10 +00001373}
Steve Naroff47500512007-04-19 23:00:49 +00001374
John McCalle3027922010-08-25 11:45:40 +00001375BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001376BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1377 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001378 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001379 case OO_Plus: return BO_Add;
1380 case OO_Minus: return BO_Sub;
1381 case OO_Star: return BO_Mul;
1382 case OO_Slash: return BO_Div;
1383 case OO_Percent: return BO_Rem;
1384 case OO_Caret: return BO_Xor;
1385 case OO_Amp: return BO_And;
1386 case OO_Pipe: return BO_Or;
1387 case OO_Equal: return BO_Assign;
1388 case OO_Less: return BO_LT;
1389 case OO_Greater: return BO_GT;
1390 case OO_PlusEqual: return BO_AddAssign;
1391 case OO_MinusEqual: return BO_SubAssign;
1392 case OO_StarEqual: return BO_MulAssign;
1393 case OO_SlashEqual: return BO_DivAssign;
1394 case OO_PercentEqual: return BO_RemAssign;
1395 case OO_CaretEqual: return BO_XorAssign;
1396 case OO_AmpEqual: return BO_AndAssign;
1397 case OO_PipeEqual: return BO_OrAssign;
1398 case OO_LessLess: return BO_Shl;
1399 case OO_GreaterGreater: return BO_Shr;
1400 case OO_LessLessEqual: return BO_ShlAssign;
1401 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1402 case OO_EqualEqual: return BO_EQ;
1403 case OO_ExclaimEqual: return BO_NE;
1404 case OO_LessEqual: return BO_LE;
1405 case OO_GreaterEqual: return BO_GE;
1406 case OO_AmpAmp: return BO_LAnd;
1407 case OO_PipePipe: return BO_LOr;
1408 case OO_Comma: return BO_Comma;
1409 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001410 }
1411}
1412
1413OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1414 static const OverloadedOperatorKind OverOps[] = {
1415 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1416 OO_Star, OO_Slash, OO_Percent,
1417 OO_Plus, OO_Minus,
1418 OO_LessLess, OO_GreaterGreater,
1419 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1420 OO_EqualEqual, OO_ExclaimEqual,
1421 OO_Amp,
1422 OO_Caret,
1423 OO_Pipe,
1424 OO_AmpAmp,
1425 OO_PipePipe,
1426 OO_Equal, OO_StarEqual,
1427 OO_SlashEqual, OO_PercentEqual,
1428 OO_PlusEqual, OO_MinusEqual,
1429 OO_LessLessEqual, OO_GreaterGreaterEqual,
1430 OO_AmpEqual, OO_CaretEqual,
1431 OO_PipeEqual,
1432 OO_Comma
1433 };
1434 return OverOps[Opc];
1435}
1436
Ted Kremenekac034612010-04-13 23:39:13 +00001437InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +00001438 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001439 SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001440 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001441 false, false),
Ted Kremenekac034612010-04-13 23:39:13 +00001442 InitExprs(C, numInits),
Sebastian Redlc83ed822012-02-17 08:42:25 +00001443 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0)
1444{
1445 sawArrayRangeDesignator(false);
1446 setInitializesStdInitializerList(false);
Ted Kremenek013041e2010-02-19 01:50:18 +00001447 for (unsigned I = 0; I != numInits; ++I) {
1448 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001449 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001450 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001451 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001452 if (initExprs[I]->isInstantiationDependent())
1453 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001454 if (initExprs[I]->containsUnexpandedParameterPack())
1455 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001456 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001457
Ted Kremenekac034612010-04-13 23:39:13 +00001458 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001459}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001460
Ted Kremenekac034612010-04-13 23:39:13 +00001461void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001462 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001463 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001464}
1465
Ted Kremenekac034612010-04-13 23:39:13 +00001466void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001467 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001468}
1469
Ted Kremenekac034612010-04-13 23:39:13 +00001470Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001471 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001472 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001473 InitExprs.back() = expr;
1474 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001475 }
Mike Stump11289f42009-09-09 15:08:12 +00001476
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001477 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1478 InitExprs[Init] = expr;
1479 return Result;
1480}
1481
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001482void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidisd4590a5d2011-10-21 23:02:22 +00001483 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001484 ArrayFillerOrUnionFieldInit = filler;
1485 // Fill out any "holes" in the array due to designated initializers.
1486 Expr **inits = getInits();
1487 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1488 if (inits[i] == 0)
1489 inits[i] = filler;
1490}
1491
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001492SourceRange InitListExpr::getSourceRange() const {
1493 if (SyntacticForm)
1494 return SyntacticForm->getSourceRange();
1495 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1496 if (Beg.isInvalid()) {
1497 // Find the first non-null initializer.
1498 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1499 E = InitExprs.end();
1500 I != E; ++I) {
1501 if (Stmt *S = *I) {
1502 Beg = S->getLocStart();
1503 break;
1504 }
1505 }
1506 }
1507 if (End.isInvalid()) {
1508 // Find the first non-null initializer from the end.
1509 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1510 E = InitExprs.rend();
1511 I != E; ++I) {
1512 if (Stmt *S = *I) {
1513 End = S->getSourceRange().getEnd();
1514 break;
1515 }
1516 }
1517 }
1518 return SourceRange(Beg, End);
1519}
1520
Steve Naroff991e99d2008-09-04 15:31:07 +00001521/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001522///
John McCallc833dea2012-02-17 03:32:35 +00001523const FunctionProtoType *BlockExpr::getFunctionType() const {
1524 // The block pointer is never sugared, but the function type might be.
1525 return cast<BlockPointerType>(getType())
1526 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001527}
1528
Mike Stump11289f42009-09-09 15:08:12 +00001529SourceLocation BlockExpr::getCaretLocation() const {
1530 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001531}
Mike Stump11289f42009-09-09 15:08:12 +00001532const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001533 return TheBlock->getBody();
1534}
Mike Stump11289f42009-09-09 15:08:12 +00001535Stmt *BlockExpr::getBody() {
1536 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001537}
Steve Naroff415d3d52008-10-08 17:01:13 +00001538
1539
Chris Lattner1ec5f562007-06-27 05:38:08 +00001540//===----------------------------------------------------------------------===//
1541// Generic Expression Routines
1542//===----------------------------------------------------------------------===//
1543
Chris Lattner237f2752009-02-14 07:37:35 +00001544/// isUnusedResultAWarning - Return true if this immediate expression should
1545/// be warned about if the result is unused. If so, fill in Loc and Ranges
1546/// with location to warn on and the source range[s] to report with the
1547/// warning.
1548bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +00001549 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001550 // Don't warn if the expr is type dependent. The type could end up
1551 // instantiating to void.
1552 if (isTypeDependent())
1553 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001554
Chris Lattner1ec5f562007-06-27 05:38:08 +00001555 switch (getStmtClass()) {
1556 default:
John McCallc493a732010-03-12 07:11:26 +00001557 if (getType()->isVoidType())
1558 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001559 Loc = getExprLoc();
1560 R1 = getSourceRange();
1561 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001562 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001563 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +00001564 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001565 case GenericSelectionExprClass:
1566 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1567 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001568 case UnaryOperatorClass: {
1569 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001570
Chris Lattner1ec5f562007-06-27 05:38:08 +00001571 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001572 default: break;
John McCalle3027922010-08-25 11:45:40 +00001573 case UO_PostInc:
1574 case UO_PostDec:
1575 case UO_PreInc:
1576 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001577 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001578 case UO_Deref:
Chris Lattnera44d1162007-06-27 05:58:59 +00001579 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001580 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001581 return false;
1582 break;
John McCalle3027922010-08-25 11:45:40 +00001583 case UO_Real:
1584 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001585 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001586 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1587 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001588 return false;
1589 break;
John McCalle3027922010-08-25 11:45:40 +00001590 case UO_Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001591 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001592 }
Chris Lattner237f2752009-02-14 07:37:35 +00001593 Loc = UO->getOperatorLoc();
1594 R1 = UO->getSubExpr()->getSourceRange();
1595 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001596 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001597 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001598 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001599 switch (BO->getOpcode()) {
1600 default:
1601 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001602 // Consider the RHS of comma for side effects. LHS was checked by
1603 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001604 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001605 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1606 // lvalue-ness) of an assignment written in a macro.
1607 if (IntegerLiteral *IE =
1608 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1609 if (IE->getValue() == 0)
1610 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001611 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1612 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001613 case BO_LAnd:
1614 case BO_LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001615 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1616 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1617 return false;
1618 break;
John McCall1e3715a2010-02-16 04:10:53 +00001619 }
Chris Lattner237f2752009-02-14 07:37:35 +00001620 if (BO->isAssignmentOp())
1621 return false;
1622 Loc = BO->getOperatorLoc();
1623 R1 = BO->getLHS()->getSourceRange();
1624 R2 = BO->getRHS()->getSourceRange();
1625 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001626 }
Chris Lattner86928112007-08-25 02:00:02 +00001627 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001628 case VAArgExprClass:
Eli Friedmandf14b3a2011-10-11 02:20:01 +00001629 case AtomicExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001630 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001631
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001632 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00001633 // If only one of the LHS or RHS is a warning, the operator might
1634 // be being used for control flow. Only warn if both the LHS and
1635 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001636 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremeneke96dad92011-03-01 20:34:48 +00001637 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1638 return false;
1639 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00001640 return true;
Ted Kremeneke96dad92011-03-01 20:34:48 +00001641 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001642 }
1643
Chris Lattnera44d1162007-06-27 05:58:59 +00001644 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001645 // If the base pointer or element is to a volatile pointer/field, accessing
1646 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001647 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001648 return false;
1649 Loc = cast<MemberExpr>(this)->getMemberLoc();
1650 R1 = SourceRange(Loc, Loc);
1651 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1652 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001653
Chris Lattner1ec5f562007-06-27 05:38:08 +00001654 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001655 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001656 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001657 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001658 return false;
1659 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1660 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1661 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1662 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001663
Chandler Carruth46339472011-08-17 09:49:44 +00001664 case CXXOperatorCallExprClass: {
1665 // We warn about operator== and operator!= even when user-defined operator
1666 // overloads as there is no reasonable way to define these such that they
1667 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1668 // warning: these operators are commonly typo'ed, and so warning on them
1669 // provides additional value as well. If this list is updated,
1670 // DiagnoseUnusedComparison should be as well.
1671 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1672 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00001673 Op->getOperator() == OO_ExclaimEqual) {
1674 Loc = Op->getOperatorLoc();
1675 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00001676 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00001677 }
Chandler Carruth46339472011-08-17 09:49:44 +00001678
1679 // Fallthrough for generic call handling.
1680 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001681 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001682 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001683 // If this is a direct call, get the callee.
1684 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001685 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001686 // If the callee has attribute pure, const, or warn_unused_result, warn
1687 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001688 //
1689 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1690 // updated to match for QoI.
1691 if (FD->getAttr<WarnUnusedResultAttr>() ||
1692 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1693 Loc = CE->getCallee()->getLocStart();
1694 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001695
Chris Lattner1a6babf2009-10-13 04:53:48 +00001696 if (unsigned NumArgs = CE->getNumArgs())
1697 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1698 CE->getArg(NumArgs-1)->getLocEnd());
1699 return true;
1700 }
Chris Lattner237f2752009-02-14 07:37:35 +00001701 }
1702 return false;
1703 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001704
1705 case CXXTemporaryObjectExprClass:
1706 case CXXConstructExprClass:
1707 return false;
1708
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001709 case ObjCMessageExprClass: {
1710 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCall31168b02011-06-15 23:02:42 +00001711 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1712 ME->isInstanceMessage() &&
1713 !ME->getType()->isVoidType() &&
1714 ME->getSelector().getIdentifierInfoForSlot(0) &&
1715 ME->getSelector().getIdentifierInfoForSlot(0)
1716 ->getName().startswith("init")) {
1717 Loc = getExprLoc();
1718 R1 = ME->getSourceRange();
1719 return true;
1720 }
1721
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001722 const ObjCMethodDecl *MD = ME->getMethodDecl();
1723 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1724 Loc = getExprLoc();
1725 return true;
1726 }
Chris Lattner237f2752009-02-14 07:37:35 +00001727 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001728 }
Mike Stump11289f42009-09-09 15:08:12 +00001729
John McCallb7bd14f2010-12-02 01:19:52 +00001730 case ObjCPropertyRefExprClass:
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001731 Loc = getExprLoc();
1732 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001733 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00001734
John McCallfe96e0b2011-11-06 09:01:30 +00001735 case PseudoObjectExprClass: {
1736 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1737
1738 // Only complain about things that have the form of a getter.
1739 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1740 isa<BinaryOperator>(PO->getSyntacticForm()))
1741 return false;
1742
1743 Loc = getExprLoc();
1744 R1 = getSourceRange();
1745 return true;
1746 }
1747
Chris Lattner944d3062008-07-26 19:51:01 +00001748 case StmtExprClass: {
1749 // Statement exprs don't logically have side effects themselves, but are
1750 // sometimes used in macros in ways that give them a type that is unused.
1751 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1752 // however, if the result of the stmt expr is dead, we don't want to emit a
1753 // warning.
1754 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001755 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00001756 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001757 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001758 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1759 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1760 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1761 }
Mike Stump11289f42009-09-09 15:08:12 +00001762
John McCallc493a732010-03-12 07:11:26 +00001763 if (getType()->isVoidType())
1764 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001765 Loc = cast<StmtExpr>(this)->getLParenLoc();
1766 R1 = getSourceRange();
1767 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001768 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001769 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001770 // If this is an explicit cast to void, allow it. People do this when they
1771 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001772 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001773 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001774 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1775 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1776 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001777 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001778 if (getType()->isVoidType())
1779 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001780 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001781
Anders Carlsson6aa50392009-11-17 17:11:23 +00001782 // If this is a cast to void or a constructor conversion, check the operand.
1783 // Otherwise, the result of the cast is unused.
John McCalle3027922010-08-25 11:45:40 +00001784 if (CE->getCastKind() == CK_ToVoid ||
1785 CE->getCastKind() == CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001786 return (cast<CastExpr>(this)->getSubExpr()
1787 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001788 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1789 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1790 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001791 }
Mike Stump11289f42009-09-09 15:08:12 +00001792
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001793 case ImplicitCastExprClass:
1794 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001795 return (cast<ImplicitCastExpr>(this)
1796 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001797
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001798 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001799 return (cast<CXXDefaultArgExpr>(this)
1800 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001801
1802 case CXXNewExprClass:
1803 // FIXME: In theory, there might be new expressions that don't have side
1804 // effects (e.g. a placement new with an uninitialized POD).
1805 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001806 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001807 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001808 return (cast<CXXBindTemporaryExpr>(this)
1809 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00001810 case ExprWithCleanupsClass:
1811 return (cast<ExprWithCleanups>(this)
Mike Stump53f9ded2009-11-03 23:25:48 +00001812 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001813 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001814}
1815
Fariborz Jahanian07735332009-02-22 18:40:18 +00001816/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001817/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001818bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00001819 const Expr *E = IgnoreParens();
1820 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001821 default:
1822 return false;
1823 case ObjCIvarRefExprClass:
1824 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001825 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001826 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001827 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001828 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00001829 case MaterializeTemporaryExprClass:
1830 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1831 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001832 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001833 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00001834 case BlockDeclRefExprClass:
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001835 case DeclRefExprClass: {
Fariborz Jahanianc367b8f2011-09-23 18:57:30 +00001836
1837 const Decl *D;
1838 if (const BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(E))
1839 D = BDRE->getDecl();
1840 else
1841 D = cast<DeclRefExpr>(E)->getDecl();
1842
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001843 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1844 if (VD->hasGlobalStorage())
1845 return true;
1846 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001847 // dereferencing to a pointer is always a gc'able candidate,
1848 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001849 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001850 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001851 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001852 return false;
1853 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001854 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00001855 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001856 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001857 }
1858 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001859 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001860 }
1861}
Sebastian Redlce354af2010-09-10 20:55:33 +00001862
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001863bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1864 if (isTypeDependent())
1865 return false;
John McCall086a4642010-11-24 05:12:34 +00001866 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001867}
1868
John McCall0009fcc2011-04-26 20:42:42 +00001869QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle314e272011-10-18 21:02:43 +00001870 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall0009fcc2011-04-26 20:42:42 +00001871
1872 // Bound member expressions are always one of these possibilities:
1873 // x->m x.m x->*y x.*y
1874 // (possibly parenthesized)
1875
1876 expr = expr->IgnoreParens();
1877 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1878 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1879 return mem->getMemberDecl()->getType();
1880 }
1881
1882 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1883 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1884 ->getPointeeType();
1885 assert(type->isFunctionType());
1886 return type;
1887 }
1888
1889 assert(isa<UnresolvedMemberExpr>(expr));
1890 return QualType();
1891}
1892
Sebastian Redlce354af2010-09-10 20:55:33 +00001893static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1894 Expr::CanThrowResult CT2) {
1895 // CanThrowResult constants are ordered so that the maximum is the correct
1896 // merge result.
1897 return CT1 > CT2 ? CT1 : CT2;
1898}
1899
1900static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1901 Expr *E = const_cast<Expr*>(CE);
1902 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall8322c3a2011-02-13 04:07:26 +00001903 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redlce354af2010-09-10 20:55:33 +00001904 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1905 }
1906 return R;
1907}
1908
Richard Smith938f40b2011-06-11 17:19:42 +00001909static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1910 const Decl *D,
Sebastian Redlce354af2010-09-10 20:55:33 +00001911 bool NullThrows = true) {
1912 if (!D)
1913 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1914
1915 // See if we can get a function type from the decl somehow.
1916 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1917 if (!VD) // If we have no clue what we're calling, assume the worst.
1918 return Expr::CT_Can;
1919
Sebastian Redlb8a76c42010-09-10 22:34:40 +00001920 // As an extension, we assume that __attribute__((nothrow)) functions don't
1921 // throw.
1922 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1923 return Expr::CT_Cannot;
1924
Sebastian Redlce354af2010-09-10 20:55:33 +00001925 QualType T = VD->getType();
1926 const FunctionProtoType *FT;
1927 if ((FT = T->getAs<FunctionProtoType>())) {
1928 } else if (const PointerType *PT = T->getAs<PointerType>())
1929 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1930 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1931 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1932 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1933 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1934 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1935 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1936
1937 if (!FT)
1938 return Expr::CT_Can;
1939
Richard Smith938f40b2011-06-11 17:19:42 +00001940 if (FT->getExceptionSpecType() == EST_Delayed) {
1941 assert(isa<CXXConstructorDecl>(D) &&
1942 "only constructor exception specs can be unknown");
1943 Ctx.getDiagnostics().Report(E->getLocStart(),
1944 diag::err_exception_spec_unknown)
1945 << E->getSourceRange();
1946 return Expr::CT_Can;
1947 }
1948
Sebastian Redl31ad7542011-03-13 17:09:40 +00001949 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redlce354af2010-09-10 20:55:33 +00001950}
1951
1952static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1953 if (DC->isTypeDependent())
1954 return Expr::CT_Dependent;
1955
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001956 if (!DC->getTypeAsWritten()->isReferenceType())
1957 return Expr::CT_Cannot;
1958
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001959 if (DC->getSubExpr()->isTypeDependent())
1960 return Expr::CT_Dependent;
1961
Sebastian Redlce354af2010-09-10 20:55:33 +00001962 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1963}
1964
1965static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1966 const CXXTypeidExpr *DC) {
1967 if (DC->isTypeOperand())
1968 return Expr::CT_Cannot;
1969
1970 Expr *Op = DC->getExprOperand();
1971 if (Op->isTypeDependent())
1972 return Expr::CT_Dependent;
1973
1974 const RecordType *RT = Op->getType()->getAs<RecordType>();
1975 if (!RT)
1976 return Expr::CT_Cannot;
1977
1978 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1979 return Expr::CT_Cannot;
1980
1981 if (Op->Classify(C).isPRValue())
1982 return Expr::CT_Cannot;
1983
1984 return Expr::CT_Can;
1985}
1986
1987Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1988 // C++ [expr.unary.noexcept]p3:
1989 // [Can throw] if in a potentially-evaluated context the expression would
1990 // contain:
1991 switch (getStmtClass()) {
1992 case CXXThrowExprClass:
1993 // - a potentially evaluated throw-expression
1994 return CT_Can;
1995
1996 case CXXDynamicCastExprClass: {
1997 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1998 // where T is a reference type, that requires a run-time check
1999 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
2000 if (CT == CT_Can)
2001 return CT;
2002 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2003 }
2004
2005 case CXXTypeidExprClass:
2006 // - a potentially evaluated typeid expression applied to a glvalue
2007 // expression whose type is a polymorphic class type
2008 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
2009
2010 // - a potentially evaluated call to a function, member function, function
2011 // pointer, or member function pointer that does not have a non-throwing
2012 // exception-specification
2013 case CallExprClass:
Eli Friedman290e6ba2012-01-31 01:21:45 +00002014 case CXXMemberCallExprClass:
2015 case CXXOperatorCallExprClass: {
Eli Friedman622e4fc2011-05-12 02:11:32 +00002016 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanc6587cc2011-05-11 05:22:44 +00002017 CanThrowResult CT;
2018 if (isTypeDependent())
2019 CT = CT_Dependent;
Eli Friedman622e4fc2011-05-12 02:11:32 +00002020 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
2021 CT = CT_Cannot;
Eli Friedmanc6587cc2011-05-11 05:22:44 +00002022 else
Richard Smith938f40b2011-06-11 17:19:42 +00002023 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redlce354af2010-09-10 20:55:33 +00002024 if (CT == CT_Can)
2025 return CT;
2026 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2027 }
2028
Sebastian Redl5f0180d2010-09-10 20:55:47 +00002029 case CXXConstructExprClass:
2030 case CXXTemporaryObjectExprClass: {
Richard Smith938f40b2011-06-11 17:19:42 +00002031 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redlce354af2010-09-10 20:55:33 +00002032 cast<CXXConstructExpr>(this)->getConstructor());
2033 if (CT == CT_Can)
2034 return CT;
2035 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2036 }
2037
Douglas Gregore31e6062012-02-07 10:09:13 +00002038 case LambdaExprClass: {
2039 const LambdaExpr *Lambda = cast<LambdaExpr>(this);
2040 CanThrowResult CT = Expr::CT_Cannot;
2041 for (LambdaExpr::capture_init_iterator Cap = Lambda->capture_init_begin(),
2042 CapEnd = Lambda->capture_init_end();
2043 Cap != CapEnd; ++Cap)
2044 CT = MergeCanThrow(CT, (*Cap)->CanThrow(C));
2045 return CT;
2046 }
2047
Sebastian Redlce354af2010-09-10 20:55:33 +00002048 case CXXNewExprClass: {
Eli Friedmanc6587cc2011-05-11 05:22:44 +00002049 CanThrowResult CT;
2050 if (isTypeDependent())
2051 CT = CT_Dependent;
2052 else
Sebastian Redl6047f072012-02-16 12:22:20 +00002053 CT = CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew());
Sebastian Redlce354af2010-09-10 20:55:33 +00002054 if (CT == CT_Can)
2055 return CT;
2056 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2057 }
2058
2059 case CXXDeleteExprClass: {
Eli Friedmanc6587cc2011-05-11 05:22:44 +00002060 CanThrowResult CT;
2061 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
2062 if (DTy.isNull() || DTy->isDependentType()) {
2063 CT = CT_Dependent;
2064 } else {
Richard Smith938f40b2011-06-11 17:19:42 +00002065 CT = CanCalleeThrow(C, this,
2066 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanc6587cc2011-05-11 05:22:44 +00002067 if (const RecordType *RT = DTy->getAs<RecordType>()) {
2068 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith938f40b2011-06-11 17:19:42 +00002069 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redla8bac372010-09-10 23:27:10 +00002070 }
Eli Friedmanc6587cc2011-05-11 05:22:44 +00002071 if (CT == CT_Can)
2072 return CT;
Sebastian Redla8bac372010-09-10 23:27:10 +00002073 }
2074 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2075 }
2076
2077 case CXXBindTemporaryExprClass: {
2078 // The bound temporary has to be destroyed again, which might throw.
Richard Smith938f40b2011-06-11 17:19:42 +00002079 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redla8bac372010-09-10 23:27:10 +00002080 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
2081 if (CT == CT_Can)
2082 return CT;
Sebastian Redlce354af2010-09-10 20:55:33 +00002083 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2084 }
2085
2086 // ObjC message sends are like function calls, but never have exception
2087 // specs.
2088 case ObjCMessageExprClass:
2089 case ObjCPropertyRefExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00002090 return CT_Can;
2091
2092 // Many other things have subexpressions, so we have to test those.
2093 // Some are simple:
Sebastian Redlce354af2010-09-10 20:55:33 +00002094 case ConditionalOperatorClass:
2095 case CompoundLiteralExprClass:
Eli Friedman290e6ba2012-01-31 01:21:45 +00002096 case CXXConstCastExprClass:
2097 case CXXDefaultArgExprClass:
2098 case CXXReinterpretCastExprClass:
2099 case DesignatedInitExprClass:
2100 case ExprWithCleanupsClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00002101 case ExtVectorElementExprClass:
2102 case InitListExprClass:
Eli Friedman290e6ba2012-01-31 01:21:45 +00002103 case MemberExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00002104 case ObjCIsaExprClass:
Eli Friedman290e6ba2012-01-31 01:21:45 +00002105 case ObjCIvarRefExprClass:
2106 case ParenExprClass:
2107 case ParenListExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00002108 case ShuffleVectorExprClass:
Eli Friedman290e6ba2012-01-31 01:21:45 +00002109 case VAArgExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00002110 return CanSubExprsThrow(C, this);
2111
2112 // Some might be dependent for other reasons.
Sebastian Redlce354af2010-09-10 20:55:33 +00002113 case ArraySubscriptExprClass:
Eli Friedman290e6ba2012-01-31 01:21:45 +00002114 case BinaryOperatorClass:
2115 case CompoundAssignOperatorClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00002116 case CStyleCastExprClass:
2117 case CXXStaticCastExprClass:
2118 case CXXFunctionalCastExprClass:
Eli Friedman290e6ba2012-01-31 01:21:45 +00002119 case ImplicitCastExprClass:
2120 case MaterializeTemporaryExprClass:
2121 case UnaryOperatorClass: {
Sebastian Redlce354af2010-09-10 20:55:33 +00002122 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2123 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2124 }
2125
2126 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2127 case StmtExprClass:
2128 return CT_Can;
2129
2130 case ChooseExprClass:
2131 if (isTypeDependent() || isValueDependent())
2132 return CT_Dependent;
2133 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2134
Peter Collingbourne91147592011-04-15 00:35:48 +00002135 case GenericSelectionExprClass:
2136 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2137 return CT_Dependent;
2138 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2139
Sebastian Redlce354af2010-09-10 20:55:33 +00002140 // Some expressions are always dependent.
Sebastian Redlce354af2010-09-10 20:55:33 +00002141 case CXXDependentScopeMemberExprClass:
Eli Friedman290e6ba2012-01-31 01:21:45 +00002142 case CXXUnresolvedConstructExprClass:
2143 case DependentScopeDeclRefExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00002144 return CT_Dependent;
2145
Eli Friedman290e6ba2012-01-31 01:21:45 +00002146 case AtomicExprClass:
2147 case AsTypeExprClass:
2148 case BinaryConditionalOperatorClass:
2149 case BlockExprClass:
2150 case BlockDeclRefExprClass:
2151 case CUDAKernelCallExprClass:
2152 case DeclRefExprClass:
2153 case ObjCBridgedCastExprClass:
2154 case ObjCIndirectCopyRestoreExprClass:
2155 case ObjCProtocolExprClass:
2156 case ObjCSelectorExprClass:
2157 case OffsetOfExprClass:
2158 case PackExpansionExprClass:
2159 case PseudoObjectExprClass:
2160 case SubstNonTypeTemplateParmExprClass:
2161 case SubstNonTypeTemplateParmPackExprClass:
2162 case UnaryExprOrTypeTraitExprClass:
2163 case UnresolvedLookupExprClass:
2164 case UnresolvedMemberExprClass:
2165 // FIXME: Can any of the above throw? If so, when?
Sebastian Redlce354af2010-09-10 20:55:33 +00002166 return CT_Cannot;
Eli Friedman290e6ba2012-01-31 01:21:45 +00002167
2168 case AddrLabelExprClass:
2169 case ArrayTypeTraitExprClass:
2170 case BinaryTypeTraitExprClass:
2171 case CXXBoolLiteralExprClass:
2172 case CXXNoexceptExprClass:
2173 case CXXNullPtrLiteralExprClass:
2174 case CXXPseudoDestructorExprClass:
2175 case CXXScalarValueInitExprClass:
2176 case CXXThisExprClass:
2177 case CXXUuidofExprClass:
2178 case CharacterLiteralClass:
2179 case ExpressionTraitExprClass:
2180 case FloatingLiteralClass:
2181 case GNUNullExprClass:
2182 case ImaginaryLiteralClass:
2183 case ImplicitValueInitExprClass:
2184 case IntegerLiteralClass:
2185 case ObjCEncodeExprClass:
2186 case ObjCStringLiteralClass:
2187 case OpaqueValueExprClass:
2188 case PredefinedExprClass:
2189 case SizeOfPackExprClass:
2190 case StringLiteralClass:
2191 case UnaryTypeTraitExprClass:
2192 // These expressions can never throw.
2193 return CT_Cannot;
2194
2195#define STMT(CLASS, PARENT) case CLASS##Class:
2196#define STMT_RANGE(Base, First, Last)
2197#define LAST_STMT_RANGE(BASE, FIRST, LAST)
2198#define EXPR(CLASS, PARENT)
2199#define ABSTRACT_STMT(STMT)
2200#include "clang/AST/StmtNodes.inc"
2201 case NoStmtClass:
2202 llvm_unreachable("Invalid class for expression");
Sebastian Redlce354af2010-09-10 20:55:33 +00002203 }
Matt Beaumont-Gayecc05b02012-01-31 18:59:25 +00002204 llvm_unreachable("Bogus StmtClass");
Sebastian Redlce354af2010-09-10 20:55:33 +00002205}
2206
Ted Kremenekfff70962008-01-17 16:57:34 +00002207Expr* Expr::IgnoreParens() {
2208 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002209 while (true) {
2210 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2211 E = P->getSubExpr();
2212 continue;
2213 }
2214 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2215 if (P->getOpcode() == UO_Extension) {
2216 E = P->getSubExpr();
2217 continue;
2218 }
2219 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002220 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2221 if (!P->isResultDependent()) {
2222 E = P->getResultExpr();
2223 continue;
2224 }
2225 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002226 return E;
2227 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002228}
2229
Chris Lattnerf2660962008-02-13 01:02:39 +00002230/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2231/// or CastExprs or ImplicitCastExprs, returning their operand.
2232Expr *Expr::IgnoreParenCasts() {
2233 Expr *E = this;
2234 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00002235 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002236 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002237 continue;
2238 }
2239 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002240 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002241 continue;
2242 }
2243 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2244 if (P->getOpcode() == UO_Extension) {
2245 E = P->getSubExpr();
2246 continue;
2247 }
2248 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002249 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2250 if (!P->isResultDependent()) {
2251 E = P->getResultExpr();
2252 continue;
2253 }
2254 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002255 if (MaterializeTemporaryExpr *Materialize
2256 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2257 E = Materialize->GetTemporaryExpr();
2258 continue;
2259 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002260 if (SubstNonTypeTemplateParmExpr *NTTP
2261 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2262 E = NTTP->getReplacement();
2263 continue;
2264 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002265 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002266 }
2267}
2268
John McCall5a4ce8b2010-12-04 08:24:19 +00002269/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2270/// casts. This is intended purely as a temporary workaround for code
2271/// that hasn't yet been rewritten to do the right thing about those
2272/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002273Expr *Expr::IgnoreParenLValueCasts() {
2274 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002275 while (true) {
John McCall34376a62010-12-04 03:47:34 +00002276 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2277 E = P->getSubExpr();
2278 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00002279 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002280 if (P->getCastKind() == CK_LValueToRValue) {
2281 E = P->getSubExpr();
2282 continue;
2283 }
John McCall5a4ce8b2010-12-04 08:24:19 +00002284 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2285 if (P->getOpcode() == UO_Extension) {
2286 E = P->getSubExpr();
2287 continue;
2288 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002289 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2290 if (!P->isResultDependent()) {
2291 E = P->getResultExpr();
2292 continue;
2293 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002294 } else if (MaterializeTemporaryExpr *Materialize
2295 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2296 E = Materialize->GetTemporaryExpr();
2297 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002298 } else if (SubstNonTypeTemplateParmExpr *NTTP
2299 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2300 E = NTTP->getReplacement();
2301 continue;
John McCall34376a62010-12-04 03:47:34 +00002302 }
2303 break;
2304 }
2305 return E;
2306}
2307
John McCalleebc8322010-05-05 22:59:52 +00002308Expr *Expr::IgnoreParenImpCasts() {
2309 Expr *E = this;
2310 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00002311 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002312 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002313 continue;
2314 }
2315 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002316 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002317 continue;
2318 }
2319 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2320 if (P->getOpcode() == UO_Extension) {
2321 E = P->getSubExpr();
2322 continue;
2323 }
2324 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002325 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2326 if (!P->isResultDependent()) {
2327 E = P->getResultExpr();
2328 continue;
2329 }
2330 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002331 if (MaterializeTemporaryExpr *Materialize
2332 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2333 E = Materialize->GetTemporaryExpr();
2334 continue;
2335 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002336 if (SubstNonTypeTemplateParmExpr *NTTP
2337 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2338 E = NTTP->getReplacement();
2339 continue;
2340 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002341 return E;
John McCalleebc8322010-05-05 22:59:52 +00002342 }
2343}
2344
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002345Expr *Expr::IgnoreConversionOperator() {
2346 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002347 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002348 return MCE->getImplicitObjectArgument();
2349 }
2350 return this;
2351}
2352
Chris Lattneref26c772009-03-13 17:28:01 +00002353/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2354/// value (including ptr->int casts of the same size). Strip off any
2355/// ParenExpr or CastExprs, returning their operand.
2356Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2357 Expr *E = this;
2358 while (true) {
2359 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2360 E = P->getSubExpr();
2361 continue;
2362 }
Mike Stump11289f42009-09-09 15:08:12 +00002363
Chris Lattneref26c772009-03-13 17:28:01 +00002364 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2365 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002366 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002367 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002368
Chris Lattneref26c772009-03-13 17:28:01 +00002369 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2370 E = SE;
2371 continue;
2372 }
Mike Stump11289f42009-09-09 15:08:12 +00002373
Abramo Bagnara932e3932010-10-15 07:51:18 +00002374 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002375 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002376 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002377 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002378 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2379 E = SE;
2380 continue;
2381 }
2382 }
Mike Stump11289f42009-09-09 15:08:12 +00002383
Abramo Bagnara932e3932010-10-15 07:51:18 +00002384 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2385 if (P->getOpcode() == UO_Extension) {
2386 E = P->getSubExpr();
2387 continue;
2388 }
2389 }
2390
Peter Collingbourne91147592011-04-15 00:35:48 +00002391 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2392 if (!P->isResultDependent()) {
2393 E = P->getResultExpr();
2394 continue;
2395 }
2396 }
2397
Douglas Gregor6a40b082011-09-08 17:56:33 +00002398 if (SubstNonTypeTemplateParmExpr *NTTP
2399 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2400 E = NTTP->getReplacement();
2401 continue;
2402 }
2403
Chris Lattneref26c772009-03-13 17:28:01 +00002404 return E;
2405 }
2406}
2407
Douglas Gregord196a582009-12-14 19:27:10 +00002408bool Expr::isDefaultArgument() const {
2409 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002410 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2411 E = M->GetTemporaryExpr();
2412
Douglas Gregord196a582009-12-14 19:27:10 +00002413 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2414 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002415
Douglas Gregord196a582009-12-14 19:27:10 +00002416 return isa<CXXDefaultArgExpr>(E);
2417}
Chris Lattneref26c772009-03-13 17:28:01 +00002418
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002419/// \brief Skip over any no-op casts and any temporary-binding
2420/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002421static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002422 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2423 E = M->GetTemporaryExpr();
2424
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002425 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002426 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002427 E = ICE->getSubExpr();
2428 else
2429 break;
2430 }
2431
2432 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2433 E = BE->getSubExpr();
2434
2435 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002436 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002437 E = ICE->getSubExpr();
2438 else
2439 break;
2440 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002441
2442 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002443}
2444
John McCall7a626f62010-09-15 10:14:12 +00002445/// isTemporaryObject - Determines if this expression produces a
2446/// temporary of the given class type.
2447bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2448 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2449 return false;
2450
Anders Carlsson66bbf502010-11-28 16:40:49 +00002451 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002452
John McCall02dc8c72010-09-15 20:59:13 +00002453 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002454 if (!E->Classify(C).isPRValue()) {
2455 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002456 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002457 return false;
2458 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002459
John McCallf4ee1dd2010-09-16 06:57:56 +00002460 // Black-list a few cases which yield pr-values of class type that don't
2461 // refer to temporaries of that type:
2462
2463 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002464 if (isa<ImplicitCastExpr>(E)) {
2465 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2466 case CK_DerivedToBase:
2467 case CK_UncheckedDerivedToBase:
2468 return false;
2469 default:
2470 break;
2471 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002472 }
2473
John McCallf4ee1dd2010-09-16 06:57:56 +00002474 // - member expressions (all)
2475 if (isa<MemberExpr>(E))
2476 return false;
2477
John McCallc07a0c72011-02-17 10:25:35 +00002478 // - opaque values (all)
2479 if (isa<OpaqueValueExpr>(E))
2480 return false;
2481
John McCall7a626f62010-09-15 10:14:12 +00002482 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002483}
2484
Douglas Gregor25b7e052011-03-02 21:06:53 +00002485bool Expr::isImplicitCXXThis() const {
2486 const Expr *E = this;
2487
2488 // Strip away parentheses and casts we don't care about.
2489 while (true) {
2490 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2491 E = Paren->getSubExpr();
2492 continue;
2493 }
2494
2495 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2496 if (ICE->getCastKind() == CK_NoOp ||
2497 ICE->getCastKind() == CK_LValueToRValue ||
2498 ICE->getCastKind() == CK_DerivedToBase ||
2499 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2500 E = ICE->getSubExpr();
2501 continue;
2502 }
2503 }
2504
2505 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2506 if (UnOp->getOpcode() == UO_Extension) {
2507 E = UnOp->getSubExpr();
2508 continue;
2509 }
2510 }
2511
Douglas Gregorfe314812011-06-21 17:03:29 +00002512 if (const MaterializeTemporaryExpr *M
2513 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2514 E = M->GetTemporaryExpr();
2515 continue;
2516 }
2517
Douglas Gregor25b7e052011-03-02 21:06:53 +00002518 break;
2519 }
2520
2521 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2522 return This->isImplicit();
2523
2524 return false;
2525}
2526
Douglas Gregor4619e432008-12-05 23:32:09 +00002527/// hasAnyTypeDependentArguments - Determines if any of the expressions
2528/// in Exprs is type-dependent.
2529bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2530 for (unsigned I = 0; I < NumExprs; ++I)
2531 if (Exprs[I]->isTypeDependent())
2532 return true;
2533
2534 return false;
2535}
2536
2537/// hasAnyValueDependentArguments - Determines if any of the expressions
2538/// in Exprs is value-dependent.
2539bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2540 for (unsigned I = 0; I < NumExprs; ++I)
2541 if (Exprs[I]->isValueDependent())
2542 return true;
2543
2544 return false;
2545}
2546
John McCall8b0f4ff2010-08-02 21:13:48 +00002547bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002548 // This function is attempting whether an expression is an initializer
2549 // which can be evaluated at compile-time. isEvaluatable handles most
2550 // of the cases, but it can't deal with some initializer-specific
2551 // expressions, and it can't deal with aggregates; we deal with those here,
2552 // and fall back to isEvaluatable for the other cases.
2553
John McCall8b0f4ff2010-08-02 21:13:48 +00002554 // If we ever capture reference-binding directly in the AST, we can
2555 // kill the second parameter.
2556
2557 if (IsForRef) {
2558 EvalResult Result;
2559 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2560 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002561
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002562 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002563 default: break;
Richard Smith941aae02011-12-09 06:47:34 +00002564 case IntegerLiteralClass:
2565 case FloatingLiteralClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002566 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002567 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002568 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002569 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002570 case CXXTemporaryObjectExprClass:
2571 case CXXConstructExprClass: {
2572 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002573
2574 // Only if it's
Richard Smithd62306a2011-11-10 06:34:14 +00002575 if (CE->getConstructor()->isTrivial()) {
2576 // 1) an application of the trivial default constructor or
2577 if (!CE->getNumArgs()) return true;
John McCall8b0f4ff2010-08-02 21:13:48 +00002578
Richard Smithd62306a2011-11-10 06:34:14 +00002579 // 2) an elidable trivial copy construction of an operand which is
2580 // itself a constant initializer. Note that we consider the
2581 // operand on its own, *not* as a reference binding.
2582 if (CE->isElidable() &&
2583 CE->getArg(0)->isConstantInitializer(Ctx, false))
2584 return true;
2585 }
2586
2587 // 3) a foldable constexpr constructor.
2588 break;
John McCall81c9cea2010-08-01 21:51:45 +00002589 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002590 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002591 // This handles gcc's extension that allows global initializers like
2592 // "struct x {int x;} x = (struct x) {};".
2593 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002594 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002595 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002596 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002597 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002598 // FIXME: This doesn't deal with fields with reference types correctly.
2599 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2600 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002601 const InitListExpr *Exp = cast<InitListExpr>(this);
2602 unsigned numInits = Exp->getNumInits();
2603 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002604 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002605 return false;
2606 }
Eli Friedman384da272009-01-25 03:12:18 +00002607 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002608 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002609 case ImplicitValueInitExprClass:
2610 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002611 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002612 return cast<ParenExpr>(this)->getSubExpr()
2613 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002614 case GenericSelectionExprClass:
2615 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2616 return false;
2617 return cast<GenericSelectionExpr>(this)->getResultExpr()
2618 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002619 case ChooseExprClass:
2620 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2621 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002622 case UnaryOperatorClass: {
2623 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002624 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002625 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002626 break;
2627 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002628 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002629 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002630 case ImplicitCastExprClass:
Richard Smith161f09a2011-12-06 22:44:34 +00002631 case CStyleCastExprClass: {
2632 const CastExpr *CE = cast<CastExpr>(this);
2633
David Chisnallfa35df62012-01-16 17:27:18 +00002634 // If we're promoting an integer to an _Atomic type then this is constant
2635 // if the integer is constant. We also need to check the converse in case
2636 // someone does something like:
2637 //
2638 // int a = (_Atomic(int))42;
2639 //
2640 // I doubt anyone would write code like this directly, but it's quite
2641 // possible as the result of macro expansions.
2642 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2643 CE->getCastKind() == CK_AtomicToNonAtomic)
2644 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2645
Richard Smith161f09a2011-12-06 22:44:34 +00002646 // Handle bitcasts of vector constants.
2647 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2648 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2649
Eli Friedman13ec75b2011-12-21 00:43:02 +00002650 // Handle misc casts we want to ignore.
2651 // FIXME: Is it really safe to ignore all these?
2652 if (CE->getCastKind() == CK_NoOp ||
2653 CE->getCastKind() == CK_LValueToRValue ||
2654 CE->getCastKind() == CK_ToUnion ||
2655 CE->getCastKind() == CK_ConstructorConversion)
Richard Smith161f09a2011-12-06 22:44:34 +00002656 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2657
Eli Friedman384da272009-01-25 03:12:18 +00002658 break;
Richard Smith161f09a2011-12-06 22:44:34 +00002659 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002660 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002661 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregorfe314812011-06-21 17:03:29 +00002662 ->isConstantInitializer(Ctx, false);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002663 }
Eli Friedman384da272009-01-25 03:12:18 +00002664 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002665}
2666
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002667/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2668/// pointer constant or not, as well as the specific kind of constant detected.
2669/// Null pointer constants can be integer constant expressions with the
2670/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2671/// (a GNU extension).
2672Expr::NullPointerConstantKind
2673Expr::isNullPointerConstant(ASTContext &Ctx,
2674 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00002675 if (isValueDependent()) {
2676 switch (NPC) {
2677 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00002678 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00002679 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002680 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2681 return NPCK_ZeroInteger;
2682 else
2683 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00002684
Douglas Gregor56751b52009-09-25 04:25:58 +00002685 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002686 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00002687 }
2688 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002689
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002690 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002691 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00002692 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002693 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002694 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002695 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002696 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002697 Pointee->isVoidType() && // to void*
2698 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002699 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002700 }
Steve Naroffada7d422007-05-20 17:54:12 +00002701 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002702 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2703 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002704 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002705 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2706 // Accept ((void*)0) as a null pointer constant, as many other
2707 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002708 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00002709 } else if (const GenericSelectionExpr *GE =
2710 dyn_cast<GenericSelectionExpr>(this)) {
2711 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002712 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002713 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002714 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002715 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002716 } else if (isa<GNUNullExpr>(this)) {
2717 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002718 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00002719 } else if (const MaterializeTemporaryExpr *M
2720 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2721 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCallfe96e0b2011-11-06 09:01:30 +00002722 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2723 if (const Expr *Source = OVE->getSourceExpr())
2724 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00002725 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002726
Sebastian Redl576fd422009-05-10 18:38:11 +00002727 // C++0x nullptr_t is always a null pointer constant.
2728 if (getType()->isNullPtrType())
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002729 return NPCK_CXX0X_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00002730
Fariborz Jahanian3567c422010-09-27 22:42:37 +00002731 if (const RecordType *UT = getType()->getAsUnionType())
2732 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2733 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2734 const Expr *InitExpr = CLE->getInitializer();
2735 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2736 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2737 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002738 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002739 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002740 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002741 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00002742
Chris Lattner1abbd412007-06-08 17:58:43 +00002743 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith98a0a492012-02-14 21:38:30 +00002744 // test for the value 0. Don't use the C++11 constant expression semantics
2745 // for this, for now; once the dust settles on core issue 903, we might only
2746 // allow a literal 0 here in C++11 mode.
2747 if (Ctx.getLangOptions().CPlusPlus0x) {
2748 if (!isCXX98IntegralConstantExpr(Ctx))
2749 return NPCK_NotNull;
2750 } else {
2751 if (!isIntegerConstantExpr(Ctx))
2752 return NPCK_NotNull;
2753 }
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002754
Richard Smith98a0a492012-02-14 21:38:30 +00002755 return (EvaluateKnownConstInt(Ctx) == 0) ? NPCK_ZeroInteger : NPCK_NotNull;
Steve Naroff218bc2b2007-05-04 21:54:46 +00002756}
Steve Narofff7a5da12007-07-28 23:10:27 +00002757
John McCall34376a62010-12-04 03:47:34 +00002758/// \brief If this expression is an l-value for an Objective C
2759/// property, find the underlying property reference expression.
2760const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2761 const Expr *E = this;
2762 while (true) {
2763 assert((E->getValueKind() == VK_LValue &&
2764 E->getObjectKind() == OK_ObjCProperty) &&
2765 "expression is not a property reference");
2766 E = E->IgnoreParenCasts();
2767 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2768 if (BO->getOpcode() == BO_Comma) {
2769 E = BO->getRHS();
2770 continue;
2771 }
2772 }
2773
2774 break;
2775 }
2776
2777 return cast<ObjCPropertyRefExpr>(E);
2778}
2779
Douglas Gregor71235ec2009-05-02 02:18:30 +00002780FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002781 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002782
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002783 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002784 if (ICE->getCastKind() == CK_LValueToRValue ||
2785 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002786 E = ICE->getSubExpr()->IgnoreParens();
2787 else
2788 break;
2789 }
2790
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002791 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002792 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002793 if (Field->isBitField())
2794 return Field;
2795
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00002796 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2797 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2798 if (Field->isBitField())
2799 return Field;
2800
Eli Friedman609ada22011-07-13 02:05:57 +00002801 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00002802 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2803 return BinOp->getLHS()->getBitField();
2804
Eli Friedman609ada22011-07-13 02:05:57 +00002805 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2806 return BinOp->getRHS()->getBitField();
2807 }
2808
Douglas Gregor71235ec2009-05-02 02:18:30 +00002809 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002810}
2811
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002812bool Expr::refersToVectorElement() const {
2813 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002814
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002815 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002816 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002817 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002818 E = ICE->getSubExpr()->IgnoreParens();
2819 else
2820 break;
2821 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002822
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002823 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2824 return ASE->getBase()->getType()->isVectorType();
2825
2826 if (isa<ExtVectorElementExpr>(E))
2827 return true;
2828
2829 return false;
2830}
2831
Chris Lattnerb8211f62009-02-16 22:14:05 +00002832/// isArrow - Return true if the base expression is a pointer to vector,
2833/// return false if the base expression is a vector.
2834bool ExtVectorElementExpr::isArrow() const {
2835 return getBase()->getType()->isPointerType();
2836}
2837
Nate Begemance4d7fc2008-04-18 23:10:10 +00002838unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002839 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002840 return VT->getNumElements();
2841 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002842}
2843
Nate Begemanf322eab2008-05-09 06:41:27 +00002844/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002845bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002846 // FIXME: Refactor this code to an accessor on the AST node which returns the
2847 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002848 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002849
2850 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002851 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002852 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002853
Nate Begeman7e5185b2009-01-18 02:01:21 +00002854 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002855 if (Comp[0] == 's' || Comp[0] == 'S')
2856 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002857
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002858 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002859 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002860 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002861
Steve Naroff0d595ca2007-07-30 03:29:09 +00002862 return false;
2863}
Chris Lattner885b4952007-08-02 23:36:59 +00002864
Nate Begemanf322eab2008-05-09 06:41:27 +00002865/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002866void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002867 SmallVectorImpl<unsigned> &Elts) const {
2868 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002869 if (Comp[0] == 's' || Comp[0] == 'S')
2870 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002871
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002872 bool isHi = Comp == "hi";
2873 bool isLo = Comp == "lo";
2874 bool isEven = Comp == "even";
2875 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002876
Nate Begemanf322eab2008-05-09 06:41:27 +00002877 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2878 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002879
Nate Begemanf322eab2008-05-09 06:41:27 +00002880 if (isHi)
2881 Index = e + i;
2882 else if (isLo)
2883 Index = i;
2884 else if (isEven)
2885 Index = 2 * i;
2886 else if (isOdd)
2887 Index = 2 * i + 1;
2888 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002889 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002890
Nate Begemand3862152008-05-13 21:03:02 +00002891 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002892 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002893}
2894
Douglas Gregor9a129192010-04-21 00:45:42 +00002895ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002896 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002897 SourceLocation LBracLoc,
2898 SourceLocation SuperLoc,
2899 bool IsInstanceSuper,
2900 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002901 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002902 ArrayRef<SourceLocation> SelLocs,
2903 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002904 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00002905 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002906 SourceLocation RBracLoc,
2907 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00002908 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002909 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00002910 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002911 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002912 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2913 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00002914 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002915 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
2916 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002917{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002918 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00002919 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002920}
2921
Douglas Gregor9a129192010-04-21 00:45:42 +00002922ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002923 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002924 SourceLocation LBracLoc,
2925 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002926 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002927 ArrayRef<SourceLocation> SelLocs,
2928 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002929 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00002930 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002931 SourceLocation RBracLoc,
2932 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00002933 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002934 T->isDependentType(), T->isInstantiationDependentType(),
2935 T->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002936 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2937 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00002938 Kind(Class),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002939 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002940 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002941{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002942 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00002943 setReceiverPointer(Receiver);
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002944}
2945
Douglas Gregor9a129192010-04-21 00:45:42 +00002946ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002947 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002948 SourceLocation LBracLoc,
2949 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002950 Selector Sel,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002951 ArrayRef<SourceLocation> SelLocs,
2952 SelectorLocationsKind SelLocsK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002953 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00002954 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002955 SourceLocation RBracLoc,
2956 bool isImplicit)
John McCall7decc9e2010-11-18 06:31:45 +00002957 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002958 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002959 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002960 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor9a129192010-04-21 00:45:42 +00002961 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2962 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb98e3712011-10-03 06:36:55 +00002963 Kind(Instance),
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00002964 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002965 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002966{
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002967 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor9a129192010-04-21 00:45:42 +00002968 setReceiverPointer(Receiver);
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002969}
2970
2971void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2972 ArrayRef<SourceLocation> SelLocs,
2973 SelectorLocationsKind SelLocsK) {
2974 setNumArgs(Args.size());
Douglas Gregora3efea12011-01-03 19:04:46 +00002975 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00002976 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00002977 if (Args[I]->isTypeDependent())
2978 ExprBits.TypeDependent = true;
2979 if (Args[I]->isValueDependent())
2980 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00002981 if (Args[I]->isInstantiationDependent())
2982 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00002983 if (Args[I]->containsUnexpandedParameterPack())
2984 ExprBits.ContainsUnexpandedParameterPack = true;
2985
2986 MyArgs[I] = Args[I];
2987 }
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00002988
Benjamin Kramer2325b242012-02-20 00:20:48 +00002989 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00002990 if (!isImplicit()) {
Argyrios Kyrtzidis0037e082012-01-12 22:34:19 +00002991 if (SelLocsK == SelLoc_NonStandard)
2992 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
2993 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00002994}
2995
Douglas Gregor9a129192010-04-21 00:45:42 +00002996ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002997 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002998 SourceLocation LBracLoc,
2999 SourceLocation SuperLoc,
3000 bool IsInstanceSuper,
3001 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003002 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003003 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003004 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003005 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003006 SourceLocation RBracLoc,
3007 bool isImplicit) {
3008 assert((!SelLocs.empty() || isImplicit) &&
3009 "No selector locs for non-implicit message");
3010 ObjCMessageExpr *Mem;
3011 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3012 if (isImplicit)
3013 Mem = alloc(Context, Args.size(), 0);
3014 else
3015 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCall7decc9e2010-11-18 06:31:45 +00003016 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003017 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003018 Method, Args, RBracLoc, isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003019}
3020
3021ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003022 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003023 SourceLocation LBracLoc,
3024 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00003025 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003026 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003027 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003028 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003029 SourceLocation RBracLoc,
3030 bool isImplicit) {
3031 assert((!SelLocs.empty() || isImplicit) &&
3032 "No selector locs for non-implicit message");
3033 ObjCMessageExpr *Mem;
3034 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3035 if (isImplicit)
3036 Mem = alloc(Context, Args.size(), 0);
3037 else
3038 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003039 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003040 SelLocs, SelLocsK, Method, Args, RBracLoc,
3041 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003042}
3043
3044ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00003045 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00003046 SourceLocation LBracLoc,
3047 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00003048 Selector Sel,
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003049 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor9a129192010-04-21 00:45:42 +00003050 ObjCMethodDecl *Method,
Argyrios Kyrtzidis59ad1e32011-10-03 06:36:45 +00003051 ArrayRef<Expr *> Args,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003052 SourceLocation RBracLoc,
3053 bool isImplicit) {
3054 assert((!SelLocs.empty() || isImplicit) &&
3055 "No selector locs for non-implicit message");
3056 ObjCMessageExpr *Mem;
3057 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3058 if (isImplicit)
3059 Mem = alloc(Context, Args.size(), 0);
3060 else
3061 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidisf934ec82011-10-03 06:36:17 +00003062 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidisa80f1bf2012-01-12 02:34:39 +00003063 SelLocs, SelLocsK, Method, Args, RBracLoc,
3064 isImplicit);
Douglas Gregor9a129192010-04-21 00:45:42 +00003065}
3066
Alexis Hunta8136cc2010-05-05 15:23:54 +00003067ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003068 unsigned NumArgs,
3069 unsigned NumStoredSelLocs) {
3070 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor9a129192010-04-21 00:45:42 +00003071 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3072}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003073
Argyrios Kyrtzidisa6011e22011-10-03 06:36:51 +00003074ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3075 ArrayRef<Expr *> Args,
3076 SourceLocation RBraceLoc,
3077 ArrayRef<SourceLocation> SelLocs,
3078 Selector Sel,
3079 SelectorLocationsKind &SelLocsK) {
3080 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3081 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3082 : 0;
3083 return alloc(C, Args.size(), NumStoredSelLocs);
3084}
3085
3086ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3087 unsigned NumArgs,
3088 unsigned NumStoredSelLocs) {
3089 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3090 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3091 return (ObjCMessageExpr *)C.Allocate(Size,
3092 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3093}
3094
3095void ObjCMessageExpr::getSelectorLocs(
3096 SmallVectorImpl<SourceLocation> &SelLocs) const {
3097 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3098 SelLocs.push_back(getSelectorLoc(i));
3099}
3100
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003101SourceRange ObjCMessageExpr::getReceiverRange() const {
3102 switch (getReceiverKind()) {
3103 case Instance:
3104 return getInstanceReceiver()->getSourceRange();
3105
3106 case Class:
3107 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3108
3109 case SuperInstance:
3110 case SuperClass:
3111 return getSuperLoc();
3112 }
3113
David Blaikiee4d798f2012-01-20 21:50:17 +00003114 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00003115}
3116
Douglas Gregor9a129192010-04-21 00:45:42 +00003117Selector ObjCMessageExpr::getSelector() const {
3118 if (HasMethod)
3119 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3120 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00003121 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00003122}
3123
3124ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3125 switch (getReceiverKind()) {
3126 case Instance:
3127 if (const ObjCObjectPointerType *Ptr
3128 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
3129 return Ptr->getInterfaceDecl();
3130 break;
3131
3132 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00003133 if (const ObjCObjectType *Ty
3134 = getClassReceiver()->getAs<ObjCObjectType>())
3135 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00003136 break;
3137
3138 case SuperInstance:
3139 if (const ObjCObjectPointerType *Ptr
3140 = getSuperType()->getAs<ObjCObjectPointerType>())
3141 return Ptr->getInterfaceDecl();
3142 break;
3143
3144 case SuperClass:
Argyrios Kyrtzidis1b9747f2011-01-25 00:03:48 +00003145 if (const ObjCObjectType *Iface
3146 = getSuperType()->getAs<ObjCObjectType>())
3147 return Iface->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00003148 break;
3149 }
3150
3151 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00003152}
Chris Lattner7ec71da2009-04-26 00:44:05 +00003153
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003154StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00003155 switch (getBridgeKind()) {
3156 case OBC_Bridge:
3157 return "__bridge";
3158 case OBC_BridgeTransfer:
3159 return "__bridge_transfer";
3160 case OBC_BridgeRetained:
3161 return "__bridge_retained";
3162 }
David Blaikiee4d798f2012-01-20 21:50:17 +00003163
3164 llvm_unreachable("Invalid BridgeKind!");
John McCall31168b02011-06-15 23:02:42 +00003165}
3166
Jay Foad39c79802011-01-12 09:06:06 +00003167bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smithcaf33902011-10-10 18:28:20 +00003168 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00003169}
3170
Douglas Gregora6e053e2010-12-15 01:34:56 +00003171ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3172 QualType Type, SourceLocation BLoc,
3173 SourceLocation RP)
3174 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3175 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003176 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003177 Type->containsUnexpandedParameterPack()),
3178 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3179{
3180 SubExprs = new (C) Stmt*[nexpr];
3181 for (unsigned i = 0; i < nexpr; i++) {
3182 if (args[i]->isTypeDependent())
3183 ExprBits.TypeDependent = true;
3184 if (args[i]->isValueDependent())
3185 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003186 if (args[i]->isInstantiationDependent())
3187 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003188 if (args[i]->containsUnexpandedParameterPack())
3189 ExprBits.ContainsUnexpandedParameterPack = true;
3190
3191 SubExprs[i] = args[i];
3192 }
3193}
3194
Nate Begeman48745922009-08-12 02:28:50 +00003195void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3196 unsigned NumExprs) {
3197 if (SubExprs) C.Deallocate(SubExprs);
3198
3199 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00003200 this->NumExprs = NumExprs;
3201 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00003202}
Nate Begeman48745922009-08-12 02:28:50 +00003203
Peter Collingbourne91147592011-04-15 00:35:48 +00003204GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3205 SourceLocation GenericLoc, Expr *ControllingExpr,
3206 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3207 unsigned NumAssocs, SourceLocation DefaultLoc,
3208 SourceLocation RParenLoc,
3209 bool ContainsUnexpandedParameterPack,
3210 unsigned ResultIndex)
3211 : Expr(GenericSelectionExprClass,
3212 AssocExprs[ResultIndex]->getType(),
3213 AssocExprs[ResultIndex]->getValueKind(),
3214 AssocExprs[ResultIndex]->getObjectKind(),
3215 AssocExprs[ResultIndex]->isTypeDependent(),
3216 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003217 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00003218 ContainsUnexpandedParameterPack),
3219 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3220 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3221 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3222 RParenLoc(RParenLoc) {
3223 SubExprs[CONTROLLING] = ControllingExpr;
3224 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3225 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3226}
3227
3228GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3229 SourceLocation GenericLoc, Expr *ControllingExpr,
3230 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3231 unsigned NumAssocs, SourceLocation DefaultLoc,
3232 SourceLocation RParenLoc,
3233 bool ContainsUnexpandedParameterPack)
3234 : Expr(GenericSelectionExprClass,
3235 Context.DependentTy,
3236 VK_RValue,
3237 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003238 /*isTypeDependent=*/true,
3239 /*isValueDependent=*/true,
3240 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003241 ContainsUnexpandedParameterPack),
3242 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3243 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3244 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3245 RParenLoc(RParenLoc) {
3246 SubExprs[CONTROLLING] = ControllingExpr;
3247 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3248 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3249}
3250
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003251//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003252// DesignatedInitExpr
3253//===----------------------------------------------------------------------===//
3254
Chandler Carruth631abd92011-06-16 06:47:06 +00003255IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003256 assert(Kind == FieldDesignator && "Only valid on a field designator");
3257 if (Field.NameOrField & 0x01)
3258 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3259 else
3260 return getField()->getIdentifier();
3261}
3262
Alexis Hunta8136cc2010-05-05 15:23:54 +00003263DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003264 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003265 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003266 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003267 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00003268 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003269 unsigned NumIndexExprs,
3270 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003271 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003272 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003273 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003274 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003275 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003276 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3277 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003278 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003279
3280 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003281 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003282 *Child++ = Init;
3283
3284 // Copy the designators and their subexpressions, computing
3285 // value-dependence along the way.
3286 unsigned IndexIdx = 0;
3287 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003288 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003289
3290 if (this->Designators[I].isArrayDesignator()) {
3291 // Compute type- and value-dependence.
3292 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003293 if (Index->isTypeDependent() || Index->isValueDependent())
3294 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003295 if (Index->isInstantiationDependent())
3296 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003297 // Propagate unexpanded parameter packs.
3298 if (Index->containsUnexpandedParameterPack())
3299 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003300
3301 // Copy the index expressions into permanent storage.
3302 *Child++ = IndexExprs[IndexIdx++];
3303 } else if (this->Designators[I].isArrayRangeDesignator()) {
3304 // Compute type- and value-dependence.
3305 Expr *Start = IndexExprs[IndexIdx];
3306 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003307 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003308 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003309 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003310 ExprBits.InstantiationDependent = true;
3311 } else if (Start->isInstantiationDependent() ||
3312 End->isInstantiationDependent()) {
3313 ExprBits.InstantiationDependent = true;
3314 }
3315
Douglas Gregora6e053e2010-12-15 01:34:56 +00003316 // Propagate unexpanded parameter packs.
3317 if (Start->containsUnexpandedParameterPack() ||
3318 End->containsUnexpandedParameterPack())
3319 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003320
3321 // Copy the start/end expressions into permanent storage.
3322 *Child++ = IndexExprs[IndexIdx++];
3323 *Child++ = IndexExprs[IndexIdx++];
3324 }
3325 }
3326
3327 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003328}
3329
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003330DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00003331DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003332 unsigned NumDesignators,
3333 Expr **IndexExprs, unsigned NumIndexExprs,
3334 SourceLocation ColonOrEqualLoc,
3335 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003336 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003337 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003338 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003339 ColonOrEqualLoc, UsesColonSyntax,
3340 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003341}
3342
Mike Stump11289f42009-09-09 15:08:12 +00003343DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003344 unsigned NumIndexExprs) {
3345 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3346 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3347 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3348}
3349
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003350void DesignatedInitExpr::setDesignators(ASTContext &C,
3351 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003352 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003353 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003354 NumDesignators = NumDesigs;
3355 for (unsigned I = 0; I != NumDesigs; ++I)
3356 Designators[I] = Desigs[I];
3357}
3358
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003359SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3360 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3361 if (size() == 1)
3362 return DIE->getDesignator(0)->getSourceRange();
3363 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3364 DIE->getDesignator(size()-1)->getEndLocation());
3365}
3366
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003367SourceRange DesignatedInitExpr::getSourceRange() const {
3368 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003369 Designator &First =
3370 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003371 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003372 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003373 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3374 else
3375 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3376 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003377 StartLoc =
3378 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003379 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3380}
3381
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003382Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3383 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3384 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3385 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003386 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3387 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3388}
3389
3390Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00003391 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003392 "Requires array range designator");
3393 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3394 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003395 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3396 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3397}
3398
3399Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00003400 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003401 "Requires array range designator");
3402 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3403 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003404 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3405 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3406}
3407
Douglas Gregord5846a12009-04-15 06:41:24 +00003408/// \brief Replaces the designator at index @p Idx with the series
3409/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003410void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003411 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003412 const Designator *Last) {
3413 unsigned NumNewDesignators = Last - First;
3414 if (NumNewDesignators == 0) {
3415 std::copy_backward(Designators + Idx + 1,
3416 Designators + NumDesignators,
3417 Designators + Idx);
3418 --NumNewDesignators;
3419 return;
3420 } else if (NumNewDesignators == 1) {
3421 Designators[Idx] = *First;
3422 return;
3423 }
3424
Mike Stump11289f42009-09-09 15:08:12 +00003425 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003426 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003427 std::copy(Designators, Designators + Idx, NewDesignators);
3428 std::copy(First, Last, NewDesignators + Idx);
3429 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3430 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003431 Designators = NewDesignators;
3432 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3433}
3434
Mike Stump11289f42009-09-09 15:08:12 +00003435ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00003436 Expr **exprs, unsigned nexprs,
Sebastian Redla9351792012-02-11 23:51:47 +00003437 SourceLocation rparenloc)
3438 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003439 false, false, false, false),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003440 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Nate Begeman5ec4b312009-08-10 23:49:36 +00003441 Exprs = new (C) Stmt*[nexprs];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003442 for (unsigned i = 0; i != nexprs; ++i) {
3443 if (exprs[i]->isTypeDependent())
3444 ExprBits.TypeDependent = true;
3445 if (exprs[i]->isValueDependent())
3446 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003447 if (exprs[i]->isInstantiationDependent())
3448 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003449 if (exprs[i]->containsUnexpandedParameterPack())
3450 ExprBits.ContainsUnexpandedParameterPack = true;
3451
Nate Begeman5ec4b312009-08-10 23:49:36 +00003452 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003453 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003454}
3455
John McCall1bf58462011-02-16 08:02:54 +00003456const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3457 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3458 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003459 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3460 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003461 e = cast<CXXConstructExpr>(e)->getArg(0);
3462 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3463 e = ice->getSubExpr();
3464 return cast<OpaqueValueExpr>(e);
3465}
3466
John McCallfe96e0b2011-11-06 09:01:30 +00003467PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3468 unsigned numSemanticExprs) {
3469 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3470 (1 + numSemanticExprs) * sizeof(Expr*),
3471 llvm::alignOf<PseudoObjectExpr>());
3472 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3473}
3474
3475PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3476 : Expr(PseudoObjectExprClass, shell) {
3477 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3478}
3479
3480PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3481 ArrayRef<Expr*> semantics,
3482 unsigned resultIndex) {
3483 assert(syntax && "no syntactic expression!");
3484 assert(semantics.size() && "no semantic expressions!");
3485
3486 QualType type;
3487 ExprValueKind VK;
3488 if (resultIndex == NoResult) {
3489 type = C.VoidTy;
3490 VK = VK_RValue;
3491 } else {
3492 assert(resultIndex < semantics.size());
3493 type = semantics[resultIndex]->getType();
3494 VK = semantics[resultIndex]->getValueKind();
3495 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3496 }
3497
3498 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3499 (1 + semantics.size()) * sizeof(Expr*),
3500 llvm::alignOf<PseudoObjectExpr>());
3501 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3502 resultIndex);
3503}
3504
3505PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3506 Expr *syntax, ArrayRef<Expr*> semantics,
3507 unsigned resultIndex)
3508 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3509 /*filled in at end of ctor*/ false, false, false, false) {
3510 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3511 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3512
3513 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3514 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3515 getSubExprsBuffer()[i] = E;
3516
3517 if (E->isTypeDependent())
3518 ExprBits.TypeDependent = true;
3519 if (E->isValueDependent())
3520 ExprBits.ValueDependent = true;
3521 if (E->isInstantiationDependent())
3522 ExprBits.InstantiationDependent = true;
3523 if (E->containsUnexpandedParameterPack())
3524 ExprBits.ContainsUnexpandedParameterPack = true;
3525
3526 if (isa<OpaqueValueExpr>(E))
3527 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3528 "opaque-value semantic expressions for pseudo-object "
3529 "operations must have sources");
3530 }
3531}
3532
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003533//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003534// ExprIterator.
3535//===----------------------------------------------------------------------===//
3536
3537Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3538Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3539Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3540const Expr* ConstExprIterator::operator[](size_t idx) const {
3541 return cast<Expr>(I[idx]);
3542}
3543const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3544const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3545
3546//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003547// Child Iterators for iterating over subexpressions/substatements
3548//===----------------------------------------------------------------------===//
3549
Peter Collingbournee190dee2011-03-11 19:24:49 +00003550// UnaryExprOrTypeTraitExpr
3551Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003552 // If this is of a type and the type is a VLA type (and not a typedef), the
3553 // size expression of the VLA needs to be treated as an executable expression.
3554 // Why isn't this weirdness documented better in StmtIterator?
3555 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003556 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003557 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003558 return child_range(child_iterator(T), child_iterator());
3559 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003560 }
John McCallbd066782011-02-09 08:16:59 +00003561 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003562}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003563
Steve Naroffd54978b2007-09-18 23:55:05 +00003564// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00003565Stmt::child_range ObjCMessageExpr::children() {
3566 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003567 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003568 begin = reinterpret_cast<Stmt **>(this + 1);
3569 else
3570 begin = reinterpret_cast<Stmt **>(getArgs());
3571 return child_range(begin,
3572 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003573}
3574
Steve Naroffc540d662008-09-03 18:15:37 +00003575// Blocks
John McCall351762c2011-02-07 10:33:21 +00003576BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregor476e3022011-01-19 21:32:01 +00003577 SourceLocation l, bool ByRef,
John McCall351762c2011-02-07 10:33:21 +00003578 bool constAdded)
Douglas Gregor678d76c2011-07-01 01:22:09 +00003579 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregor476e3022011-01-19 21:32:01 +00003580 d->isParameterPack()),
John McCall351762c2011-02-07 10:33:21 +00003581 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregor476e3022011-01-19 21:32:01 +00003582{
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003583 bool TypeDependent = false;
3584 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003585 bool InstantiationDependent = false;
3586 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3587 InstantiationDependent);
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003588 ExprBits.TypeDependent = TypeDependent;
3589 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003590 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregor476e3022011-01-19 21:32:01 +00003591}
Eli Friedman8d3e43f2011-10-14 22:48:56 +00003592
3593
3594AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3595 QualType t, AtomicOp op, SourceLocation RP)
3596 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3597 false, false, false, false),
3598 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3599{
3600 for (unsigned i = 0; i < nexpr; i++) {
3601 if (args[i]->isTypeDependent())
3602 ExprBits.TypeDependent = true;
3603 if (args[i]->isValueDependent())
3604 ExprBits.ValueDependent = true;
3605 if (args[i]->isInstantiationDependent())
3606 ExprBits.InstantiationDependent = true;
3607 if (args[i]->containsUnexpandedParameterPack())
3608 ExprBits.ContainsUnexpandedParameterPack = true;
3609
3610 SubExprs[i] = args[i];
3611 }
3612}