blob: a358693a52c72b307c1604795a1d6866f7e2b270 [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>
Chris Lattner1b926492006-08-23 06:42:10 +000032using namespace clang;
33
Chris Lattner4ebae652010-04-16 23:34:13 +000034/// isKnownToHaveBooleanValue - Return true if this is an integer expression
35/// that is known to return 0 or 1. This happens for _Bool/bool expressions
36/// but also int expressions which are produced by things like comparisons in
37/// C.
38bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbourne91147592011-04-15 00:35:48 +000039 const Expr *E = IgnoreParens();
40
Chris Lattner4ebae652010-04-16 23:34:13 +000041 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbourne91147592011-04-15 00:35:48 +000042 if (E->getType()->isBooleanType()) return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000043 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbourne91147592011-04-15 00:35:48 +000044 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Alexis Hunta8136cc2010-05-05 15:23:54 +000045
Peter Collingbourne91147592011-04-15 00:35:48 +000046 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +000047 switch (UO->getOpcode()) {
John McCalle3027922010-08-25 11:45:40 +000048 case UO_Plus:
Chris Lattner4ebae652010-04-16 23:34:13 +000049 return UO->getSubExpr()->isKnownToHaveBooleanValue();
50 default:
51 return false;
52 }
53 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000054
John McCall45d30c32010-06-12 01:56:02 +000055 // Only look through implicit casts. If the user writes
56 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbourne91147592011-04-15 00:35:48 +000057 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +000058 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000059
Peter Collingbourne91147592011-04-15 00:35:48 +000060 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner4ebae652010-04-16 23:34:13 +000061 switch (BO->getOpcode()) {
62 default: return false;
John McCalle3027922010-08-25 11:45:40 +000063 case BO_LT: // Relational operators.
64 case BO_GT:
65 case BO_LE:
66 case BO_GE:
67 case BO_EQ: // Equality operators.
68 case BO_NE:
69 case BO_LAnd: // AND operator.
70 case BO_LOr: // Logical OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +000071 return true;
Alexis Hunta8136cc2010-05-05 15:23:54 +000072
John McCalle3027922010-08-25 11:45:40 +000073 case BO_And: // Bitwise AND operator.
74 case BO_Xor: // Bitwise XOR operator.
75 case BO_Or: // Bitwise OR operator.
Chris Lattner4ebae652010-04-16 23:34:13 +000076 // Handle things like (x==2)|(y==12).
77 return BO->getLHS()->isKnownToHaveBooleanValue() &&
78 BO->getRHS()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000079
John McCalle3027922010-08-25 11:45:40 +000080 case BO_Comma:
81 case BO_Assign:
Chris Lattner4ebae652010-04-16 23:34:13 +000082 return BO->getRHS()->isKnownToHaveBooleanValue();
83 }
84 }
Alexis Hunta8136cc2010-05-05 15:23:54 +000085
Peter Collingbourne91147592011-04-15 00:35:48 +000086 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner4ebae652010-04-16 23:34:13 +000087 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
88 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Alexis Hunta8136cc2010-05-05 15:23:54 +000089
Chris Lattner4ebae652010-04-16 23:34:13 +000090 return false;
91}
92
John McCallbd066782011-02-09 08:16:59 +000093// Amusing macro metaprogramming hack: check whether a class provides
94// a more specific implementation of getExprLoc().
95namespace {
96 /// This implementation is used when a class provides a custom
97 /// implementation of getExprLoc.
98 template <class E, class T>
99 SourceLocation getExprLocImpl(const Expr *expr,
100 SourceLocation (T::*v)() const) {
101 return static_cast<const E*>(expr)->getExprLoc();
102 }
103
104 /// This implementation is used when a class doesn't provide
105 /// a custom implementation of getExprLoc. Overload resolution
106 /// should pick it over the implementation above because it's
107 /// more specialized according to function template partial ordering.
108 template <class E>
109 SourceLocation getExprLocImpl(const Expr *expr,
110 SourceLocation (Expr::*v)() const) {
111 return static_cast<const E*>(expr)->getSourceRange().getBegin();
112 }
113}
114
115SourceLocation Expr::getExprLoc() const {
116 switch (getStmtClass()) {
117 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
118#define ABSTRACT_STMT(type)
119#define STMT(type, base) \
120 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
121#define EXPR(type, base) \
122 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
123#include "clang/AST/StmtNodes.inc"
124 }
125 llvm_unreachable("unknown statement kind");
126 return SourceLocation();
127}
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.
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000187 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
Douglas Gregorb90df602010-06-16 00:17:44 +0000188 if (Var->getType()->isIntegralOrEnumerationType() &&
Douglas Gregor5fcb51c2010-01-15 16:21:02 +0000189 Var->getType().getCVRQualifiers() == Qualifiers::Const) {
Sebastian Redl5ca79842010-02-01 20:16:42 +0000190 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000191 if (Init->isValueDependent()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000192 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000193 InstantiationDependent = true;
194 }
Douglas Gregor0e4de762010-05-11 08:41:30 +0000195 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000196
Douglas Gregor0e4de762010-05-11 08:41:30 +0000197 // (VD) - FIXME: Missing from the standard:
198 // - a member function or a static data member of the current
199 // instantiation
200 else if (Var->isStaticDataMember() &&
Douglas Gregor678d76c2011-07-01 01:22:09 +0000201 Var->getDeclContext()->isDependentContext()) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000202 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000203 InstantiationDependent = true;
204 }
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000205
206 return;
207 }
208
Douglas Gregor0e4de762010-05-11 08:41:30 +0000209 // (VD) - FIXME: Missing from the standard:
210 // - a member function or a static data member of the current
211 // instantiation
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000212 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
213 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000214 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000215 return;
216 }
217}
Douglas Gregora6e053e2010-12-15 01:34:56 +0000218
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000219void DeclRefExpr::computeDependence() {
220 bool TypeDependent = false;
221 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000222 bool InstantiationDependent = false;
223 computeDeclRefDependence(getDecl(), getType(), TypeDependent, ValueDependent,
224 InstantiationDependent);
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000225
226 // (TD) C++ [temp.dep.expr]p3:
227 // An id-expression is type-dependent if it contains:
228 //
229 // and
230 //
231 // (VD) C++ [temp.dep.constexpr]p2:
232 // An identifier is value-dependent if it is:
233 if (!TypeDependent && !ValueDependent &&
234 hasExplicitTemplateArgs() &&
235 TemplateSpecializationType::anyDependentTemplateArguments(
236 getTemplateArgs(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000237 getNumTemplateArgs(),
238 InstantiationDependent)) {
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000239 TypeDependent = true;
240 ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000241 InstantiationDependent = true;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000242 }
243
244 ExprBits.TypeDependent = TypeDependent;
245 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000246 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000247
Douglas Gregorda3cc0d2010-12-23 23:51:58 +0000248 // Is the declaration a parameter pack?
Douglas Gregorf144f4f2011-01-19 21:52:31 +0000249 if (getDecl()->isParameterPack())
Douglas Gregor3c6bd2a2011-01-05 21:11:38 +0000250 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregored6c7442009-11-23 11:41:28 +0000251}
252
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000253DeclRefExpr::DeclRefExpr(NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000254 ValueDecl *D, const DeclarationNameInfo &NameInfo,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000255 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000256 const TemplateArgumentListInfo *TemplateArgs,
John McCall7decc9e2010-11-18 06:31:45 +0000257 QualType T, ExprValueKind VK)
Douglas Gregor678d76c2011-07-01 01:22:09 +0000258 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruth0e439962011-05-01 21:29:53 +0000259 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
260 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruthe68f2612011-05-01 21:55:21 +0000261 if (QualifierLoc)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000262 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000263 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
264 if (FoundD)
265 getInternalFoundDecl() = FoundD;
Chandler Carruth0e439962011-05-01 21:29:53 +0000266 DeclRefExprBits.HasExplicitTemplateArgs = TemplateArgs ? 1 : 0;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000267 if (TemplateArgs) {
268 bool Dependent = false;
269 bool InstantiationDependent = false;
270 bool ContainsUnexpandedParameterPack = false;
271 getExplicitTemplateArgs().initializeFrom(*TemplateArgs, Dependent,
272 InstantiationDependent,
273 ContainsUnexpandedParameterPack);
274 if (InstantiationDependent)
275 setInstantiationDependent(true);
276 }
277
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000278 computeDependence();
279}
280
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000281DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000282 NestedNameSpecifierLoc QualifierLoc,
John McCallce546572009-12-08 09:08:17 +0000283 ValueDecl *D,
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000284 SourceLocation NameLoc,
Douglas Gregored6c7442009-11-23 11:41:28 +0000285 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000286 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000287 NamedDecl *FoundD,
Douglas Gregored6c7442009-11-23 11:41:28 +0000288 const TemplateArgumentListInfo *TemplateArgs) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000289 return Create(Context, QualifierLoc, D,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000290 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000291 T, VK, FoundD, TemplateArgs);
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000292}
293
294DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregorea972d32011-02-28 21:54:11 +0000295 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000296 ValueDecl *D,
297 const DeclarationNameInfo &NameInfo,
298 QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000299 ExprValueKind VK,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000300 NamedDecl *FoundD,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000301 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000302 // Filter out cases where the found Decl is the same as the value refenenced.
303 if (D == FoundD)
304 FoundD = 0;
305
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000306 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregorea972d32011-02-28 21:54:11 +0000307 if (QualifierLoc != 0)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000308 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000309 if (FoundD)
310 Size += sizeof(NamedDecl *);
John McCall6b51f282009-11-23 01:53:49 +0000311 if (TemplateArgs)
Argyrios Kyrtzidisde6aa082011-09-22 20:07:03 +0000312 Size += ASTTemplateArgumentListInfo::sizeFor(*TemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000313
Chris Lattner5c0b4052010-10-30 05:14:06 +0000314 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000315 return new (Mem) DeclRefExpr(QualifierLoc, D, NameInfo, FoundD, TemplateArgs,
316 T, VK);
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000317}
318
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000319DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000320 bool HasQualifier,
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000321 bool HasFoundDecl,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000322 bool HasExplicitTemplateArgs,
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000323 unsigned NumTemplateArgs) {
324 std::size_t Size = sizeof(DeclRefExpr);
325 if (HasQualifier)
Chandler Carruthbbf65b02011-05-01 22:14:37 +0000326 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000327 if (HasFoundDecl)
328 Size += sizeof(NamedDecl *);
Douglas Gregor87866ce2011-02-04 12:01:24 +0000329 if (HasExplicitTemplateArgs)
Argyrios Kyrtzidisde6aa082011-09-22 20:07:03 +0000330 Size += ASTTemplateArgumentListInfo::sizeFor(NumTemplateArgs);
Chandler Carruth8d26bb02011-05-01 23:48:14 +0000331
Chris Lattner5c0b4052010-10-30 05:14:06 +0000332 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis1985bb32010-07-08 13:09:47 +0000333 return new (Mem) DeclRefExpr(EmptyShell());
334}
335
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000336SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000337 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000338 if (hasQualifier())
Douglas Gregorea972d32011-02-28 21:54:11 +0000339 R.setBegin(getQualifierLoc().getBeginLoc());
John McCallb3774b52010-08-19 23:49:38 +0000340 if (hasExplicitTemplateArgs())
Douglas Gregor4bd90e52009-10-23 18:54:35 +0000341 R.setEnd(getRAngleLoc());
342 return R;
343}
344
Anders Carlsson2fb08242009-09-08 18:24:21 +0000345// FIXME: Maybe this should use DeclPrinter with a special "print predefined
346// expr" policy instead.
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000347std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
348 ASTContext &Context = CurrentDecl->getASTContext();
349
Anders Carlsson2fb08242009-09-08 18:24:21 +0000350 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000351 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000352 return FD->getNameAsString();
353
354 llvm::SmallString<256> Name;
355 llvm::raw_svector_ostream Out(Name);
356
357 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson5bd8d192010-02-11 18:20:28 +0000358 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson2fb08242009-09-08 18:24:21 +0000359 Out << "virtual ";
Sam Weinig4e83bd22009-12-27 01:38:20 +0000360 if (MD->isStatic())
361 Out << "static ";
Anders Carlsson2fb08242009-09-08 18:24:21 +0000362 }
363
364 PrintingPolicy Policy(Context.getLangOptions());
Anders Carlsson2fb08242009-09-08 18:24:21 +0000365
366 std::string Proto = FD->getQualifiedNameAsString(Policy);
367
John McCall9dd450b2009-09-21 23:43:11 +0000368 const FunctionType *AFT = FD->getType()->getAs<FunctionType>();
Anders Carlsson2fb08242009-09-08 18:24:21 +0000369 const FunctionProtoType *FT = 0;
370 if (FD->hasWrittenPrototype())
371 FT = dyn_cast<FunctionProtoType>(AFT);
372
373 Proto += "(";
374 if (FT) {
375 llvm::raw_string_ostream POut(Proto);
376 for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
377 if (i) POut << ", ";
378 std::string Param;
379 FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy);
380 POut << Param;
381 }
382
383 if (FT->isVariadic()) {
384 if (FD->getNumParams()) POut << ", ";
385 POut << "...";
386 }
387 }
388 Proto += ")";
389
Sam Weinig4e83bd22009-12-27 01:38:20 +0000390 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
391 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
392 if (ThisQuals.hasConst())
393 Proto += " const";
394 if (ThisQuals.hasVolatile())
395 Proto += " volatile";
396 }
397
Sam Weinigd060ed42009-12-06 23:55:13 +0000398 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
399 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson2fb08242009-09-08 18:24:21 +0000400
401 Out << Proto;
402
403 Out.flush();
404 return Name.str().str();
405 }
406 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
407 llvm::SmallString<256> Name;
408 llvm::raw_svector_ostream Out(Name);
409 Out << (MD->isInstanceMethod() ? '-' : '+');
410 Out << '[';
Ted Kremenek361ffd92010-03-18 21:23:08 +0000411
412 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
413 // a null check to avoid a crash.
414 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000415 Out << ID;
Ted Kremenek361ffd92010-03-18 21:23:08 +0000416
Anders Carlsson2fb08242009-09-08 18:24:21 +0000417 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramerb11416d2010-04-17 09:33:03 +0000418 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
419 Out << '(' << CID << ')';
420
Anders Carlsson2fb08242009-09-08 18:24:21 +0000421 Out << ' ';
422 Out << MD->getSelector().getAsString();
423 Out << ']';
424
425 Out.flush();
426 return Name.str().str();
427 }
428 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
429 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
430 return "top level";
431 }
432 return "";
433}
434
Argyrios Kyrtzidis43b20572010-08-28 09:06:06 +0000435void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
436 if (hasAllocation())
437 C.Deallocate(pVal);
438
439 BitWidth = Val.getBitWidth();
440 unsigned NumWords = Val.getNumWords();
441 const uint64_t* Words = Val.getRawData();
442 if (NumWords > 1) {
443 pVal = new (C) uint64_t[NumWords];
444 std::copy(Words, Words + NumWords, pVal);
445 } else if (NumWords == 1)
446 VAL = Words[0];
447 else
448 VAL = 0;
449}
450
451IntegerLiteral *
452IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
453 QualType type, SourceLocation l) {
454 return new (C) IntegerLiteral(C, V, type, l);
455}
456
457IntegerLiteral *
458IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
459 return new (C) IntegerLiteral(Empty);
460}
461
462FloatingLiteral *
463FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
464 bool isexact, QualType Type, SourceLocation L) {
465 return new (C) FloatingLiteral(C, V, isexact, Type, L);
466}
467
468FloatingLiteral *
469FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
470 return new (C) FloatingLiteral(Empty);
471}
472
Chris Lattnera0173132008-06-07 22:13:43 +0000473/// getValueAsApproximateDouble - This returns the value as an inaccurate
474/// double. Note that this may cause loss of precision, but is useful for
475/// debugging dumps, etc.
476double FloatingLiteral::getValueAsApproximateDouble() const {
477 llvm::APFloat V = getValue();
Dale Johannesenc48814b2008-10-09 23:02:32 +0000478 bool ignored;
479 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
480 &ignored);
Chris Lattnera0173132008-06-07 22:13:43 +0000481 return V.convertToDouble();
482}
483
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000484StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregorfb65e592011-07-27 05:40:30 +0000485 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump11289f42009-09-09 15:08:12 +0000486 const SourceLocation *Loc,
Anders Carlssona3905812009-03-15 18:34:13 +0000487 unsigned NumStrs) {
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000488 // Allocate enough space for the StringLiteral plus an array of locations for
489 // any concatenated string tokens.
490 void *Mem = C.Allocate(sizeof(StringLiteral)+
491 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000492 llvm::alignOf<StringLiteral>());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000493 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000494
Steve Naroffdf7855b2007-02-21 23:46:25 +0000495 // OPTIMIZE: could allocate this appended to the StringLiteral.
Jay Foad9a6b0982011-06-21 15:13:30 +0000496 char *AStrData = new (C, 1) char[Str.size()];
497 memcpy(AStrData, Str.data(), Str.size());
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000498 SL->StrData = AStrData;
Jay Foad9a6b0982011-06-21 15:13:30 +0000499 SL->ByteLength = Str.size();
Douglas Gregorfb65e592011-07-27 05:40:30 +0000500 SL->Kind = Kind;
Anders Carlsson75245402011-04-14 00:40:03 +0000501 SL->IsPascal = Pascal;
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000502 SL->TokLocs[0] = Loc[0];
503 SL->NumConcatenated = NumStrs;
Chris Lattnerd3e98952006-10-06 05:22:26 +0000504
Chris Lattner630970d2009-02-18 05:49:11 +0000505 if (NumStrs != 1)
Chris Lattnerf83b5af2009-02-18 06:40:38 +0000506 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
507 return SL;
Chris Lattner630970d2009-02-18 05:49:11 +0000508}
509
Douglas Gregor958dfc92009-04-15 16:35:07 +0000510StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
511 void *Mem = C.Allocate(sizeof(StringLiteral)+
512 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner5c0b4052010-10-30 05:14:06 +0000513 llvm::alignOf<StringLiteral>());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000514 StringLiteral *SL = new (Mem) StringLiteral(QualType());
515 SL->StrData = 0;
516 SL->ByteLength = 0;
517 SL->NumConcatenated = NumStrs;
518 return SL;
519}
520
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000521void StringLiteral::setString(ASTContext &C, StringRef Str) {
Daniel Dunbar36217882009-09-22 03:27:33 +0000522 char *AStrData = new (C, 1) char[Str.size()];
523 memcpy(AStrData, Str.data(), Str.size());
Douglas Gregor958dfc92009-04-15 16:35:07 +0000524 StrData = AStrData;
Daniel Dunbar36217882009-09-22 03:27:33 +0000525 ByteLength = Str.size();
Douglas Gregor958dfc92009-04-15 16:35:07 +0000526}
527
Chris Lattnere925d612010-11-17 07:37:15 +0000528/// getLocationOfByte - Return a source location that points to the specified
529/// byte of this string literal.
530///
531/// Strings are amazingly complex. They can be formed from multiple tokens and
532/// can have escape sequences in them in addition to the usual trigraph and
533/// escaped newline business. This routine handles this complexity.
534///
535SourceLocation StringLiteral::
536getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
537 const LangOptions &Features, const TargetInfo &Target) const {
Douglas Gregorfb65e592011-07-27 05:40:30 +0000538 assert(Kind == StringLiteral::Ascii && "This only works for ASCII strings");
539
Chris Lattnere925d612010-11-17 07:37:15 +0000540 // Loop over all of the tokens in this string until we find the one that
541 // contains the byte we're looking for.
542 unsigned TokNo = 0;
543 while (1) {
544 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
545 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
546
547 // Get the spelling of the string so that we can get the data that makes up
548 // the string literal, not the identifier for the macro it is potentially
549 // expanded through.
550 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
551
552 // Re-lex the token to get its length and original spelling.
553 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
554 bool Invalid = false;
Chris Lattner0e62c1c2011-07-23 10:55:15 +0000555 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattnere925d612010-11-17 07:37:15 +0000556 if (Invalid)
557 return StrTokSpellingLoc;
558
559 const char *StrData = Buffer.data()+LocInfo.second;
560
561 // Create a langops struct and enable trigraphs. This is sufficient for
562 // relexing tokens.
563 LangOptions LangOpts;
564 LangOpts.Trigraphs = true;
565
566 // Create a lexer starting at the beginning of this token.
567 Lexer TheLexer(StrTokSpellingLoc, Features, Buffer.begin(), StrData,
568 Buffer.end());
569 Token TheTok;
570 TheLexer.LexFromRawLexer(TheTok);
571
572 // Use the StringLiteralParser to compute the length of the string in bytes.
573 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
574 unsigned TokNumBytes = SLP.GetStringLength();
575
576 // If the byte is in this token, return the location of the byte.
577 if (ByteNo < TokNumBytes ||
Hans Wennborg77d1abe2011-06-30 20:17:41 +0000578 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattnere925d612010-11-17 07:37:15 +0000579 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
580
581 // Now that we know the offset of the token in the spelling, use the
582 // preprocessor to get the offset in the original source.
583 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
584 }
585
586 // Move to the next string token.
587 ++TokNo;
588 ByteNo -= TokNumBytes;
589 }
590}
591
592
593
Chris Lattner1b926492006-08-23 06:42:10 +0000594/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
595/// corresponds to, e.g. "sizeof" or "[pre]++".
596const char *UnaryOperator::getOpcodeStr(Opcode Op) {
597 switch (Op) {
David Blaikie83d382b2011-09-23 05:06:16 +0000598 default: llvm_unreachable("Unknown unary operator");
John McCalle3027922010-08-25 11:45:40 +0000599 case UO_PostInc: return "++";
600 case UO_PostDec: return "--";
601 case UO_PreInc: return "++";
602 case UO_PreDec: return "--";
603 case UO_AddrOf: return "&";
604 case UO_Deref: return "*";
605 case UO_Plus: return "+";
606 case UO_Minus: return "-";
607 case UO_Not: return "~";
608 case UO_LNot: return "!";
609 case UO_Real: return "__real";
610 case UO_Imag: return "__imag";
611 case UO_Extension: return "__extension__";
Chris Lattner1b926492006-08-23 06:42:10 +0000612 }
613}
614
John McCalle3027922010-08-25 11:45:40 +0000615UnaryOperatorKind
Douglas Gregor084d8552009-03-13 23:49:33 +0000616UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
617 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +0000618 default: llvm_unreachable("No unary operator for overloaded function");
John McCalle3027922010-08-25 11:45:40 +0000619 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
620 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
621 case OO_Amp: return UO_AddrOf;
622 case OO_Star: return UO_Deref;
623 case OO_Plus: return UO_Plus;
624 case OO_Minus: return UO_Minus;
625 case OO_Tilde: return UO_Not;
626 case OO_Exclaim: return UO_LNot;
Douglas Gregor084d8552009-03-13 23:49:33 +0000627 }
628}
629
630OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
631 switch (Opc) {
John McCalle3027922010-08-25 11:45:40 +0000632 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
633 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
634 case UO_AddrOf: return OO_Amp;
635 case UO_Deref: return OO_Star;
636 case UO_Plus: return OO_Plus;
637 case UO_Minus: return OO_Minus;
638 case UO_Not: return OO_Tilde;
639 case UO_LNot: return OO_Exclaim;
Douglas Gregor084d8552009-03-13 23:49:33 +0000640 default: return OO_None;
641 }
642}
643
644
Chris Lattner0eedafe2006-08-24 04:56:27 +0000645//===----------------------------------------------------------------------===//
646// Postfix Operators.
647//===----------------------------------------------------------------------===//
Chris Lattnere165d942006-08-24 04:40:38 +0000648
Peter Collingbourne3a347252011-02-08 21:18:02 +0000649CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
650 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCall7decc9e2010-11-18 06:31:45 +0000651 SourceLocation rparenloc)
652 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000653 fn->isTypeDependent(),
654 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000655 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000656 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000657 NumArgs(numargs) {
Mike Stump11289f42009-09-09 15:08:12 +0000658
Peter Collingbourne3a347252011-02-08 21:18:02 +0000659 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregor993603d2008-11-14 16:09:21 +0000660 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000661 for (unsigned i = 0; i != numargs; ++i) {
662 if (args[i]->isTypeDependent())
663 ExprBits.TypeDependent = true;
664 if (args[i]->isValueDependent())
665 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000666 if (args[i]->isInstantiationDependent())
667 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000668 if (args[i]->containsUnexpandedParameterPack())
669 ExprBits.ContainsUnexpandedParameterPack = true;
670
Peter Collingbourne3a347252011-02-08 21:18:02 +0000671 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000672 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000673
Peter Collingbourne3a347252011-02-08 21:18:02 +0000674 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor993603d2008-11-14 16:09:21 +0000675 RParenLoc = rparenloc;
676}
Nate Begeman1e36a852008-01-17 17:46:27 +0000677
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000678CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCall7decc9e2010-11-18 06:31:45 +0000679 QualType t, ExprValueKind VK, SourceLocation rparenloc)
680 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000681 fn->isTypeDependent(),
682 fn->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000683 fn->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000684 fn->containsUnexpandedParameterPack()),
Douglas Gregor4619e432008-12-05 23:32:09 +0000685 NumArgs(numargs) {
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000686
Peter Collingbourne3a347252011-02-08 21:18:02 +0000687 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek85e92ec2007-08-24 18:13:47 +0000688 SubExprs[FN] = fn;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000689 for (unsigned i = 0; i != numargs; ++i) {
690 if (args[i]->isTypeDependent())
691 ExprBits.TypeDependent = true;
692 if (args[i]->isValueDependent())
693 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000694 if (args[i]->isInstantiationDependent())
695 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000696 if (args[i]->containsUnexpandedParameterPack())
697 ExprBits.ContainsUnexpandedParameterPack = true;
698
Peter Collingbourne3a347252011-02-08 21:18:02 +0000699 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000700 }
Ted Kremenekd7b4f402009-02-09 20:51:47 +0000701
Peter Collingbourne3a347252011-02-08 21:18:02 +0000702 CallExprBits.NumPreArgs = 0;
Chris Lattner9b3b9a12007-06-27 06:08:24 +0000703 RParenLoc = rparenloc;
Chris Lattnere165d942006-08-24 04:40:38 +0000704}
705
Mike Stump11289f42009-09-09 15:08:12 +0000706CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
707 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000708 // FIXME: Why do we allocate this?
Peter Collingbourne3a347252011-02-08 21:18:02 +0000709 SubExprs = new (C) Stmt*[PREARGS_START];
710 CallExprBits.NumPreArgs = 0;
711}
712
713CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
714 EmptyShell Empty)
715 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
716 // FIXME: Why do we allocate this?
717 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
718 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregore20a2e52009-04-15 17:43:59 +0000719}
720
Nuno Lopes518e3702009-12-20 23:11:08 +0000721Decl *CallExpr::getCalleeDecl() {
John McCalle3ca8eb2011-09-13 23:08:34 +0000722 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregore0e96302011-09-06 21:41:04 +0000723
724 while (SubstNonTypeTemplateParmExpr *NTTP
725 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
726 CEE = NTTP->getReplacement()->IgnoreParenCasts();
727 }
728
Sebastian Redl2b1832e2010-09-10 20:55:30 +0000729 // If we're calling a dereference, look at the pointer instead.
730 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
731 if (BO->isPtrMemOp())
732 CEE = BO->getRHS()->IgnoreParenCasts();
733 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
734 if (UO->getOpcode() == UO_Deref)
735 CEE = UO->getSubExpr()->IgnoreParenCasts();
736 }
Chris Lattner52301912009-07-17 15:46:27 +0000737 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopes518e3702009-12-20 23:11:08 +0000738 return DRE->getDecl();
Nuno Lopesc095b532009-12-24 00:28:18 +0000739 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
740 return ME->getMemberDecl();
Zhongxing Xu3c8fa972009-07-17 07:29:51 +0000741
742 return 0;
743}
744
Nuno Lopes518e3702009-12-20 23:11:08 +0000745FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattner3a6af3d2009-12-21 01:10:56 +0000746 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopes518e3702009-12-20 23:11:08 +0000747}
748
Chris Lattnere4407ed2007-12-28 05:25:02 +0000749/// setNumArgs - This changes the number of arguments present in this call.
750/// Any orphaned expressions are deleted by this, and any new operands are set
751/// to null.
Ted Kremenek5a201952009-02-07 01:47:29 +0000752void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000753 // No change, just return.
754 if (NumArgs == getNumArgs()) return;
Mike Stump11289f42009-09-09 15:08:12 +0000755
Chris Lattnere4407ed2007-12-28 05:25:02 +0000756 // If shrinking # arguments, just delete the extras and forgot them.
757 if (NumArgs < getNumArgs()) {
Chris Lattnere4407ed2007-12-28 05:25:02 +0000758 this->NumArgs = NumArgs;
759 return;
760 }
761
762 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000763 unsigned NumPreArgs = getNumPreArgs();
764 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnere4407ed2007-12-28 05:25:02 +0000765 // Copy over args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000766 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000767 NewSubExprs[i] = SubExprs[i];
768 // Null out new args.
Peter Collingbourne3a347252011-02-08 21:18:02 +0000769 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
770 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnere4407ed2007-12-28 05:25:02 +0000771 NewSubExprs[i] = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000772
Douglas Gregorba6e5572009-04-17 21:46:47 +0000773 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnere4407ed2007-12-28 05:25:02 +0000774 SubExprs = NewSubExprs;
775 this->NumArgs = NumArgs;
776}
777
Chris Lattner01ff98a2008-10-06 05:00:53 +0000778/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
779/// not, return 0.
Jay Foad39c79802011-01-12 09:06:06 +0000780unsigned CallExpr::isBuiltinCall(const ASTContext &Context) const {
Steve Narofff6e3b3292008-01-31 01:07:12 +0000781 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump11289f42009-09-09 15:08:12 +0000782 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Narofff6e3b3292008-01-31 01:07:12 +0000783 // ImplicitCastExpr.
784 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
785 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattner01ff98a2008-10-06 05:00:53 +0000786 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000787
Steve Narofff6e3b3292008-01-31 01:07:12 +0000788 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
789 if (!DRE)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000790 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000791
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000792 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
793 if (!FDecl)
Chris Lattner01ff98a2008-10-06 05:00:53 +0000794 return 0;
Mike Stump11289f42009-09-09 15:08:12 +0000795
Douglas Gregor9eb16ea2008-11-21 15:30:19 +0000796 if (!FDecl->getIdentifier())
797 return 0;
798
Douglas Gregor15fc9562009-09-12 00:22:50 +0000799 return FDecl->getBuiltinID();
Chris Lattner01ff98a2008-10-06 05:00:53 +0000800}
Anders Carlssonfbcf6762008-01-31 02:13:57 +0000801
Anders Carlsson00a27592009-05-26 04:57:27 +0000802QualType CallExpr::getCallReturnType() const {
803 QualType CalleeType = getCallee()->getType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000804 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000805 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenekc23c7e62009-07-29 21:53:49 +0000806 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson00a27592009-05-26 04:57:27 +0000807 CalleeType = BPT->getPointeeType();
John McCall0009fcc2011-04-26 20:42:42 +0000808 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
809 // This should never be overloaded and so should never return null.
810 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor603d81b2010-07-13 08:18:22 +0000811
John McCall0009fcc2011-04-26 20:42:42 +0000812 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson00a27592009-05-26 04:57:27 +0000813 return FnType->getResultType();
814}
Chris Lattner01ff98a2008-10-06 05:00:53 +0000815
John McCall701417a2011-02-21 06:23:05 +0000816SourceRange CallExpr::getSourceRange() const {
817 if (isa<CXXOperatorCallExpr>(this))
818 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
819
820 SourceLocation begin = getCallee()->getLocStart();
821 if (begin.isInvalid() && getNumArgs() > 0)
822 begin = getArg(0)->getLocStart();
823 SourceLocation end = getRParenLoc();
824 if (end.isInvalid() && getNumArgs() > 0)
825 end = getArg(getNumArgs() - 1)->getLocEnd();
826 return SourceRange(begin, end);
827}
828
Alexis Hunta8136cc2010-05-05 15:23:54 +0000829OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000830 SourceLocation OperatorLoc,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000831 TypeSourceInfo *tsi,
832 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000833 Expr** exprsPtr, unsigned numExprs,
834 SourceLocation RParenLoc) {
835 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Alexis Hunta8136cc2010-05-05 15:23:54 +0000836 sizeof(OffsetOfNode) * numComps +
Douglas Gregor882211c2010-04-28 22:16:22 +0000837 sizeof(Expr*) * numExprs);
838
839 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
840 exprsPtr, numExprs, RParenLoc);
841}
842
843OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
844 unsigned numComps, unsigned numExprs) {
845 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
846 sizeof(OffsetOfNode) * numComps +
847 sizeof(Expr*) * numExprs);
848 return new (Mem) OffsetOfExpr(numComps, numExprs);
849}
850
Alexis Hunta8136cc2010-05-05 15:23:54 +0000851OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor882211c2010-04-28 22:16:22 +0000852 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Alexis Hunta8136cc2010-05-05 15:23:54 +0000853 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor882211c2010-04-28 22:16:22 +0000854 Expr** exprsPtr, unsigned numExprs,
855 SourceLocation RParenLoc)
John McCall7decc9e2010-11-18 06:31:45 +0000856 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
857 /*TypeDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000858 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000859 tsi->getType()->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000860 tsi->getType()->containsUnexpandedParameterPack()),
Alexis Hunta8136cc2010-05-05 15:23:54 +0000861 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
862 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor882211c2010-04-28 22:16:22 +0000863{
864 for(unsigned i = 0; i < numComps; ++i) {
865 setComponent(i, compsPtr[i]);
866 }
Alexis Hunta8136cc2010-05-05 15:23:54 +0000867
Douglas Gregor882211c2010-04-28 22:16:22 +0000868 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000869 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
870 ExprBits.ValueDependent = true;
871 if (exprsPtr[i]->containsUnexpandedParameterPack())
872 ExprBits.ContainsUnexpandedParameterPack = true;
873
Douglas Gregor882211c2010-04-28 22:16:22 +0000874 setIndexExpr(i, exprsPtr[i]);
875 }
876}
877
878IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
879 assert(getKind() == Field || getKind() == Identifier);
880 if (getKind() == Field)
881 return getField()->getIdentifier();
Alexis Hunta8136cc2010-05-05 15:23:54 +0000882
Douglas Gregor882211c2010-04-28 22:16:22 +0000883 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
884}
885
Mike Stump11289f42009-09-09 15:08:12 +0000886MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregorea972d32011-02-28 21:54:11 +0000887 NestedNameSpecifierLoc QualifierLoc,
Eli Friedman2cfcef62009-12-04 06:40:45 +0000888 ValueDecl *memberdecl,
John McCalla8ae2222010-04-06 21:38:20 +0000889 DeclAccessPair founddecl,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000890 DeclarationNameInfo nameinfo,
John McCall6b51f282009-11-23 01:53:49 +0000891 const TemplateArgumentListInfo *targs,
John McCall7decc9e2010-11-18 06:31:45 +0000892 QualType ty,
893 ExprValueKind vk,
894 ExprObjectKind ok) {
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000895 std::size_t Size = sizeof(MemberExpr);
John McCall16df1e52010-03-30 21:47:33 +0000896
Douglas Gregorea972d32011-02-28 21:54:11 +0000897 bool hasQualOrFound = (QualifierLoc ||
John McCalla8ae2222010-04-06 21:38:20 +0000898 founddecl.getDecl() != memberdecl ||
899 founddecl.getAccess() != memberdecl->getAccess());
John McCall16df1e52010-03-30 21:47:33 +0000900 if (hasQualOrFound)
901 Size += sizeof(MemberNameQualifier);
Mike Stump11289f42009-09-09 15:08:12 +0000902
John McCall6b51f282009-11-23 01:53:49 +0000903 if (targs)
Argyrios Kyrtzidisde6aa082011-09-22 20:07:03 +0000904 Size += ASTTemplateArgumentListInfo::sizeFor(*targs);
Mike Stump11289f42009-09-09 15:08:12 +0000905
Chris Lattner5c0b4052010-10-30 05:14:06 +0000906 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCall7decc9e2010-11-18 06:31:45 +0000907 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
908 ty, vk, ok);
John McCall16df1e52010-03-30 21:47:33 +0000909
910 if (hasQualOrFound) {
Douglas Gregorea972d32011-02-28 21:54:11 +0000911 // FIXME: Wrong. We should be looking at the member declaration we found.
912 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall16df1e52010-03-30 21:47:33 +0000913 E->setValueDependent(true);
914 E->setTypeDependent(true);
Douglas Gregor678d76c2011-07-01 01:22:09 +0000915 E->setInstantiationDependent(true);
916 }
917 else if (QualifierLoc &&
918 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
919 E->setInstantiationDependent(true);
920
John McCall16df1e52010-03-30 21:47:33 +0000921 E->HasQualifierOrFoundDecl = true;
922
923 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregorea972d32011-02-28 21:54:11 +0000924 NQ->QualifierLoc = QualifierLoc;
John McCall16df1e52010-03-30 21:47:33 +0000925 NQ->FoundDecl = founddecl;
926 }
927
928 if (targs) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000929 bool Dependent = false;
930 bool InstantiationDependent = false;
931 bool ContainsUnexpandedParameterPack = false;
John McCall16df1e52010-03-30 21:47:33 +0000932 E->HasExplicitTemplateArgumentList = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000933 E->getExplicitTemplateArgs().initializeFrom(*targs, Dependent,
934 InstantiationDependent,
935 ContainsUnexpandedParameterPack);
936 if (InstantiationDependent)
937 E->setInstantiationDependent(true);
John McCall16df1e52010-03-30 21:47:33 +0000938 }
939
940 return E;
Douglas Gregorf405d7e2009-08-31 23:41:50 +0000941}
942
Douglas Gregor25b7e052011-03-02 21:06:53 +0000943SourceRange MemberExpr::getSourceRange() const {
944 SourceLocation StartLoc;
945 if (isImplicitAccess()) {
946 if (hasQualifier())
947 StartLoc = getQualifierLoc().getBeginLoc();
948 else
949 StartLoc = MemberLoc;
950 } else {
951 // FIXME: We don't want this to happen. Rather, we should be able to
952 // detect all kinds of implicit accesses more cleanly.
953 StartLoc = getBase()->getLocStart();
954 if (StartLoc.isInvalid())
955 StartLoc = MemberLoc;
956 }
957
958 SourceLocation EndLoc =
959 HasExplicitTemplateArgumentList? getRAngleLoc()
960 : getMemberNameInfo().getEndLoc();
961
962 return SourceRange(StartLoc, EndLoc);
963}
964
John McCall9320b872011-09-09 05:25:32 +0000965void CastExpr::CheckCastConsistency() const {
966 switch (getCastKind()) {
967 case CK_DerivedToBase:
968 case CK_UncheckedDerivedToBase:
969 case CK_DerivedToBaseMemberPointer:
970 case CK_BaseToDerived:
971 case CK_BaseToDerivedMemberPointer:
972 assert(!path_empty() && "Cast kind should have a base path!");
973 break;
974
975 case CK_CPointerToObjCPointerCast:
976 assert(getType()->isObjCObjectPointerType());
977 assert(getSubExpr()->getType()->isPointerType());
978 goto CheckNoBasePath;
979
980 case CK_BlockPointerToObjCPointerCast:
981 assert(getType()->isObjCObjectPointerType());
982 assert(getSubExpr()->getType()->isBlockPointerType());
983 goto CheckNoBasePath;
984
985 case CK_BitCast:
986 // Arbitrary casts to C pointer types count as bitcasts.
987 // Otherwise, we should only have block and ObjC pointer casts
988 // here if they stay within the type kind.
989 if (!getType()->isPointerType()) {
990 assert(getType()->isObjCObjectPointerType() ==
991 getSubExpr()->getType()->isObjCObjectPointerType());
992 assert(getType()->isBlockPointerType() ==
993 getSubExpr()->getType()->isBlockPointerType());
994 }
995 goto CheckNoBasePath;
996
997 case CK_AnyPointerToBlockPointerCast:
998 assert(getType()->isBlockPointerType());
999 assert(getSubExpr()->getType()->isAnyPointerType() &&
1000 !getSubExpr()->getType()->isBlockPointerType());
1001 goto CheckNoBasePath;
1002
1003 // These should not have an inheritance path.
1004 case CK_Dynamic:
1005 case CK_ToUnion:
1006 case CK_ArrayToPointerDecay:
1007 case CK_FunctionToPointerDecay:
1008 case CK_NullToMemberPointer:
1009 case CK_NullToPointer:
1010 case CK_ConstructorConversion:
1011 case CK_IntegralToPointer:
1012 case CK_PointerToIntegral:
1013 case CK_ToVoid:
1014 case CK_VectorSplat:
1015 case CK_IntegralCast:
1016 case CK_IntegralToFloating:
1017 case CK_FloatingToIntegral:
1018 case CK_FloatingCast:
1019 case CK_ObjCObjectLValueCast:
1020 case CK_FloatingRealToComplex:
1021 case CK_FloatingComplexToReal:
1022 case CK_FloatingComplexCast:
1023 case CK_FloatingComplexToIntegralComplex:
1024 case CK_IntegralRealToComplex:
1025 case CK_IntegralComplexToReal:
1026 case CK_IntegralComplexCast:
1027 case CK_IntegralComplexToFloatingComplex:
John McCall2d637d22011-09-10 06:18:15 +00001028 case CK_ARCProduceObject:
1029 case CK_ARCConsumeObject:
1030 case CK_ARCReclaimReturnedObject:
1031 case CK_ARCExtendBlockObject:
John McCall9320b872011-09-09 05:25:32 +00001032 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1033 goto CheckNoBasePath;
1034
1035 case CK_Dependent:
1036 case CK_LValueToRValue:
1037 case CK_GetObjCProperty:
1038 case CK_NoOp:
1039 case CK_PointerToBoolean:
1040 case CK_IntegralToBoolean:
1041 case CK_FloatingToBoolean:
1042 case CK_MemberPointerToBoolean:
1043 case CK_FloatingComplexToBoolean:
1044 case CK_IntegralComplexToBoolean:
1045 case CK_LValueBitCast: // -> bool&
1046 case CK_UserDefinedConversion: // operator bool()
1047 CheckNoBasePath:
1048 assert(path_empty() && "Cast kind should not have a base path!");
1049 break;
1050 }
1051}
1052
Anders Carlsson496335e2009-09-03 00:59:21 +00001053const char *CastExpr::getCastKindName() const {
1054 switch (getCastKind()) {
John McCall8cb679e2010-11-15 09:13:47 +00001055 case CK_Dependent:
1056 return "Dependent";
John McCalle3027922010-08-25 11:45:40 +00001057 case CK_BitCast:
Anders Carlsson496335e2009-09-03 00:59:21 +00001058 return "BitCast";
John McCalle3027922010-08-25 11:45:40 +00001059 case CK_LValueBitCast:
Douglas Gregor51954272010-07-13 23:17:26 +00001060 return "LValueBitCast";
John McCallf3735e02010-12-01 04:43:34 +00001061 case CK_LValueToRValue:
1062 return "LValueToRValue";
John McCall34376a62010-12-04 03:47:34 +00001063 case CK_GetObjCProperty:
1064 return "GetObjCProperty";
John McCalle3027922010-08-25 11:45:40 +00001065 case CK_NoOp:
Anders Carlsson496335e2009-09-03 00:59:21 +00001066 return "NoOp";
John McCalle3027922010-08-25 11:45:40 +00001067 case CK_BaseToDerived:
Anders Carlssona70ad932009-11-12 16:43:42 +00001068 return "BaseToDerived";
John McCalle3027922010-08-25 11:45:40 +00001069 case CK_DerivedToBase:
Anders Carlsson496335e2009-09-03 00:59:21 +00001070 return "DerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001071 case CK_UncheckedDerivedToBase:
John McCalld9c7c6562010-03-30 23:58:03 +00001072 return "UncheckedDerivedToBase";
John McCalle3027922010-08-25 11:45:40 +00001073 case CK_Dynamic:
Anders Carlsson496335e2009-09-03 00:59:21 +00001074 return "Dynamic";
John McCalle3027922010-08-25 11:45:40 +00001075 case CK_ToUnion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001076 return "ToUnion";
John McCalle3027922010-08-25 11:45:40 +00001077 case CK_ArrayToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001078 return "ArrayToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001079 case CK_FunctionToPointerDecay:
Anders Carlsson496335e2009-09-03 00:59:21 +00001080 return "FunctionToPointerDecay";
John McCalle3027922010-08-25 11:45:40 +00001081 case CK_NullToMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001082 return "NullToMemberPointer";
John McCalle84af4e2010-11-13 01:35:44 +00001083 case CK_NullToPointer:
1084 return "NullToPointer";
John McCalle3027922010-08-25 11:45:40 +00001085 case CK_BaseToDerivedMemberPointer:
Anders Carlsson496335e2009-09-03 00:59:21 +00001086 return "BaseToDerivedMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001087 case CK_DerivedToBaseMemberPointer:
Anders Carlsson3f0db2b2009-10-30 00:46:35 +00001088 return "DerivedToBaseMemberPointer";
John McCalle3027922010-08-25 11:45:40 +00001089 case CK_UserDefinedConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001090 return "UserDefinedConversion";
John McCalle3027922010-08-25 11:45:40 +00001091 case CK_ConstructorConversion:
Anders Carlsson496335e2009-09-03 00:59:21 +00001092 return "ConstructorConversion";
John McCalle3027922010-08-25 11:45:40 +00001093 case CK_IntegralToPointer:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001094 return "IntegralToPointer";
John McCalle3027922010-08-25 11:45:40 +00001095 case CK_PointerToIntegral:
Anders Carlsson7cd39e02009-09-15 04:48:33 +00001096 return "PointerToIntegral";
John McCall8cb679e2010-11-15 09:13:47 +00001097 case CK_PointerToBoolean:
1098 return "PointerToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001099 case CK_ToVoid:
Anders Carlssonef918ac2009-10-16 02:35:04 +00001100 return "ToVoid";
John McCalle3027922010-08-25 11:45:40 +00001101 case CK_VectorSplat:
Anders Carlsson43d70f82009-10-16 05:23:41 +00001102 return "VectorSplat";
John McCalle3027922010-08-25 11:45:40 +00001103 case CK_IntegralCast:
Anders Carlsson094c4592009-10-18 18:12:03 +00001104 return "IntegralCast";
John McCall8cb679e2010-11-15 09:13:47 +00001105 case CK_IntegralToBoolean:
1106 return "IntegralToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001107 case CK_IntegralToFloating:
Anders Carlsson094c4592009-10-18 18:12:03 +00001108 return "IntegralToFloating";
John McCalle3027922010-08-25 11:45:40 +00001109 case CK_FloatingToIntegral:
Anders Carlsson094c4592009-10-18 18:12:03 +00001110 return "FloatingToIntegral";
John McCalle3027922010-08-25 11:45:40 +00001111 case CK_FloatingCast:
Benjamin Kramerbeb873d2009-10-18 19:02:15 +00001112 return "FloatingCast";
John McCall8cb679e2010-11-15 09:13:47 +00001113 case CK_FloatingToBoolean:
1114 return "FloatingToBoolean";
John McCalle3027922010-08-25 11:45:40 +00001115 case CK_MemberPointerToBoolean:
Anders Carlsson7fa434c2009-11-23 20:04:44 +00001116 return "MemberPointerToBoolean";
John McCall9320b872011-09-09 05:25:32 +00001117 case CK_CPointerToObjCPointerCast:
1118 return "CPointerToObjCPointerCast";
1119 case CK_BlockPointerToObjCPointerCast:
1120 return "BlockPointerToObjCPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001121 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanianffe912c2009-12-11 22:40:48 +00001122 return "AnyPointerToBlockPointerCast";
John McCalle3027922010-08-25 11:45:40 +00001123 case CK_ObjCObjectLValueCast:
Douglas Gregor8b2d2fe2010-08-07 11:51:51 +00001124 return "ObjCObjectLValueCast";
John McCallc5e62b42010-11-13 09:02:35 +00001125 case CK_FloatingRealToComplex:
1126 return "FloatingRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001127 case CK_FloatingComplexToReal:
1128 return "FloatingComplexToReal";
1129 case CK_FloatingComplexToBoolean:
1130 return "FloatingComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001131 case CK_FloatingComplexCast:
1132 return "FloatingComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001133 case CK_FloatingComplexToIntegralComplex:
1134 return "FloatingComplexToIntegralComplex";
John McCallc5e62b42010-11-13 09:02:35 +00001135 case CK_IntegralRealToComplex:
1136 return "IntegralRealToComplex";
John McCalld7646252010-11-14 08:17:51 +00001137 case CK_IntegralComplexToReal:
1138 return "IntegralComplexToReal";
1139 case CK_IntegralComplexToBoolean:
1140 return "IntegralComplexToBoolean";
John McCallc5e62b42010-11-13 09:02:35 +00001141 case CK_IntegralComplexCast:
1142 return "IntegralComplexCast";
John McCalld7646252010-11-14 08:17:51 +00001143 case CK_IntegralComplexToFloatingComplex:
1144 return "IntegralComplexToFloatingComplex";
John McCall2d637d22011-09-10 06:18:15 +00001145 case CK_ARCConsumeObject:
1146 return "ARCConsumeObject";
1147 case CK_ARCProduceObject:
1148 return "ARCProduceObject";
1149 case CK_ARCReclaimReturnedObject:
1150 return "ARCReclaimReturnedObject";
1151 case CK_ARCExtendBlockObject:
1152 return "ARCCExtendBlockObject";
Anders Carlsson496335e2009-09-03 00:59:21 +00001153 }
Mike Stump11289f42009-09-09 15:08:12 +00001154
John McCallc5e62b42010-11-13 09:02:35 +00001155 llvm_unreachable("Unhandled cast kind!");
Anders Carlsson496335e2009-09-03 00:59:21 +00001156 return 0;
1157}
1158
Douglas Gregord196a582009-12-14 19:27:10 +00001159Expr *CastExpr::getSubExprAsWritten() {
1160 Expr *SubExpr = 0;
1161 CastExpr *E = this;
1162 do {
1163 SubExpr = E->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00001164
1165 // Skip through reference binding to temporary.
1166 if (MaterializeTemporaryExpr *Materialize
1167 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1168 SubExpr = Materialize->GetTemporaryExpr();
1169
Douglas Gregord196a582009-12-14 19:27:10 +00001170 // Skip any temporary bindings; they're implicit.
1171 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1172 SubExpr = Binder->getSubExpr();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001173
Douglas Gregord196a582009-12-14 19:27:10 +00001174 // Conversions by constructor and conversion functions have a
1175 // subexpression describing the call; strip it off.
John McCalle3027922010-08-25 11:45:40 +00001176 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001177 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCalle3027922010-08-25 11:45:40 +00001178 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregord196a582009-12-14 19:27:10 +00001179 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Alexis Hunta8136cc2010-05-05 15:23:54 +00001180
Douglas Gregord196a582009-12-14 19:27:10 +00001181 // If the subexpression we're left with is an implicit cast, look
1182 // through that, too.
Alexis Hunta8136cc2010-05-05 15:23:54 +00001183 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1184
Douglas Gregord196a582009-12-14 19:27:10 +00001185 return SubExpr;
1186}
1187
John McCallcf142162010-08-07 06:22:56 +00001188CXXBaseSpecifier **CastExpr::path_buffer() {
1189 switch (getStmtClass()) {
1190#define ABSTRACT_STMT(x)
1191#define CASTEXPR(Type, Base) \
1192 case Stmt::Type##Class: \
1193 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1194#define STMT(Type, Base)
1195#include "clang/AST/StmtNodes.inc"
1196 default:
1197 llvm_unreachable("non-cast expressions not possible here");
1198 return 0;
1199 }
1200}
1201
1202void CastExpr::setCastPath(const CXXCastPath &Path) {
1203 assert(Path.size() == path_size());
1204 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1205}
1206
1207ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1208 CastKind Kind, Expr *Operand,
1209 const CXXCastPath *BasePath,
John McCall2536c6d2010-08-25 10:28:54 +00001210 ExprValueKind VK) {
John McCallcf142162010-08-07 06:22:56 +00001211 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1212 void *Buffer =
1213 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1214 ImplicitCastExpr *E =
John McCall2536c6d2010-08-25 10:28:54 +00001215 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallcf142162010-08-07 06:22:56 +00001216 if (PathSize) E->setCastPath(*BasePath);
1217 return E;
1218}
1219
1220ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1221 unsigned PathSize) {
1222 void *Buffer =
1223 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1224 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1225}
1226
1227
1228CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00001229 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +00001230 const CXXCastPath *BasePath,
1231 TypeSourceInfo *WrittenTy,
1232 SourceLocation L, SourceLocation R) {
1233 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1234 void *Buffer =
1235 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1236 CStyleCastExpr *E =
John McCall7decc9e2010-11-18 06:31:45 +00001237 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallcf142162010-08-07 06:22:56 +00001238 if (PathSize) E->setCastPath(*BasePath);
1239 return E;
1240}
1241
1242CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1243 void *Buffer =
1244 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1245 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1246}
1247
Chris Lattner1b926492006-08-23 06:42:10 +00001248/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1249/// corresponds to, e.g. "<<=".
1250const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1251 switch (Op) {
John McCalle3027922010-08-25 11:45:40 +00001252 case BO_PtrMemD: return ".*";
1253 case BO_PtrMemI: return "->*";
1254 case BO_Mul: return "*";
1255 case BO_Div: return "/";
1256 case BO_Rem: return "%";
1257 case BO_Add: return "+";
1258 case BO_Sub: return "-";
1259 case BO_Shl: return "<<";
1260 case BO_Shr: return ">>";
1261 case BO_LT: return "<";
1262 case BO_GT: return ">";
1263 case BO_LE: return "<=";
1264 case BO_GE: return ">=";
1265 case BO_EQ: return "==";
1266 case BO_NE: return "!=";
1267 case BO_And: return "&";
1268 case BO_Xor: return "^";
1269 case BO_Or: return "|";
1270 case BO_LAnd: return "&&";
1271 case BO_LOr: return "||";
1272 case BO_Assign: return "=";
1273 case BO_MulAssign: return "*=";
1274 case BO_DivAssign: return "/=";
1275 case BO_RemAssign: return "%=";
1276 case BO_AddAssign: return "+=";
1277 case BO_SubAssign: return "-=";
1278 case BO_ShlAssign: return "<<=";
1279 case BO_ShrAssign: return ">>=";
1280 case BO_AndAssign: return "&=";
1281 case BO_XorAssign: return "^=";
1282 case BO_OrAssign: return "|=";
1283 case BO_Comma: return ",";
Chris Lattner1b926492006-08-23 06:42:10 +00001284 }
Douglas Gregor0f60e9a2009-03-12 22:51:37 +00001285
1286 return "";
Chris Lattner1b926492006-08-23 06:42:10 +00001287}
Steve Naroff47500512007-04-19 23:00:49 +00001288
John McCalle3027922010-08-25 11:45:40 +00001289BinaryOperatorKind
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001290BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1291 switch (OO) {
David Blaikie83d382b2011-09-23 05:06:16 +00001292 default: llvm_unreachable("Not an overloadable binary operator");
John McCalle3027922010-08-25 11:45:40 +00001293 case OO_Plus: return BO_Add;
1294 case OO_Minus: return BO_Sub;
1295 case OO_Star: return BO_Mul;
1296 case OO_Slash: return BO_Div;
1297 case OO_Percent: return BO_Rem;
1298 case OO_Caret: return BO_Xor;
1299 case OO_Amp: return BO_And;
1300 case OO_Pipe: return BO_Or;
1301 case OO_Equal: return BO_Assign;
1302 case OO_Less: return BO_LT;
1303 case OO_Greater: return BO_GT;
1304 case OO_PlusEqual: return BO_AddAssign;
1305 case OO_MinusEqual: return BO_SubAssign;
1306 case OO_StarEqual: return BO_MulAssign;
1307 case OO_SlashEqual: return BO_DivAssign;
1308 case OO_PercentEqual: return BO_RemAssign;
1309 case OO_CaretEqual: return BO_XorAssign;
1310 case OO_AmpEqual: return BO_AndAssign;
1311 case OO_PipeEqual: return BO_OrAssign;
1312 case OO_LessLess: return BO_Shl;
1313 case OO_GreaterGreater: return BO_Shr;
1314 case OO_LessLessEqual: return BO_ShlAssign;
1315 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1316 case OO_EqualEqual: return BO_EQ;
1317 case OO_ExclaimEqual: return BO_NE;
1318 case OO_LessEqual: return BO_LE;
1319 case OO_GreaterEqual: return BO_GE;
1320 case OO_AmpAmp: return BO_LAnd;
1321 case OO_PipePipe: return BO_LOr;
1322 case OO_Comma: return BO_Comma;
1323 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor1baf54e2009-03-13 18:40:31 +00001324 }
1325}
1326
1327OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1328 static const OverloadedOperatorKind OverOps[] = {
1329 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1330 OO_Star, OO_Slash, OO_Percent,
1331 OO_Plus, OO_Minus,
1332 OO_LessLess, OO_GreaterGreater,
1333 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1334 OO_EqualEqual, OO_ExclaimEqual,
1335 OO_Amp,
1336 OO_Caret,
1337 OO_Pipe,
1338 OO_AmpAmp,
1339 OO_PipePipe,
1340 OO_Equal, OO_StarEqual,
1341 OO_SlashEqual, OO_PercentEqual,
1342 OO_PlusEqual, OO_MinusEqual,
1343 OO_LessLessEqual, OO_GreaterGreaterEqual,
1344 OO_AmpEqual, OO_CaretEqual,
1345 OO_PipeEqual,
1346 OO_Comma
1347 };
1348 return OverOps[Opc];
1349}
1350
Ted Kremenekac034612010-04-13 23:39:13 +00001351InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner07d754a2008-10-26 23:43:26 +00001352 Expr **initExprs, unsigned numInits,
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001353 SourceLocation rbraceloc)
Douglas Gregora6e053e2010-12-15 01:34:56 +00001354 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001355 false, false),
Ted Kremenekac034612010-04-13 23:39:13 +00001356 InitExprs(C, numInits),
Mike Stump11289f42009-09-09 15:08:12 +00001357 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0),
Argyrios Kyrtzidisb2ed28e2011-04-21 00:27:41 +00001358 HadArrayRangeDesignator(false)
Alexis Hunta8136cc2010-05-05 15:23:54 +00001359{
Ted Kremenek013041e2010-02-19 01:50:18 +00001360 for (unsigned I = 0; I != numInits; ++I) {
1361 if (initExprs[I]->isTypeDependent())
John McCall925b16622010-10-26 08:39:16 +00001362 ExprBits.TypeDependent = true;
Ted Kremenek013041e2010-02-19 01:50:18 +00001363 if (initExprs[I]->isValueDependent())
John McCall925b16622010-10-26 08:39:16 +00001364 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001365 if (initExprs[I]->isInstantiationDependent())
1366 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001367 if (initExprs[I]->containsUnexpandedParameterPack())
1368 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregordeebf6e2009-11-19 23:25:22 +00001369 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00001370
Ted Kremenekac034612010-04-13 23:39:13 +00001371 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson4692db02007-08-31 04:56:16 +00001372}
Chris Lattner1ec5f562007-06-27 05:38:08 +00001373
Ted Kremenekac034612010-04-13 23:39:13 +00001374void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001375 if (NumInits > InitExprs.size())
Ted Kremenekac034612010-04-13 23:39:13 +00001376 InitExprs.reserve(C, NumInits);
Douglas Gregor6d00c992009-03-20 23:58:33 +00001377}
1378
Ted Kremenekac034612010-04-13 23:39:13 +00001379void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekac034612010-04-13 23:39:13 +00001380 InitExprs.resize(C, NumInits, 0);
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001381}
1382
Ted Kremenekac034612010-04-13 23:39:13 +00001383Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenek013041e2010-02-19 01:50:18 +00001384 if (Init >= InitExprs.size()) {
Ted Kremenekac034612010-04-13 23:39:13 +00001385 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenek013041e2010-02-19 01:50:18 +00001386 InitExprs.back() = expr;
1387 return 0;
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001388 }
Mike Stump11289f42009-09-09 15:08:12 +00001389
Douglas Gregor347f7ea2009-01-28 21:54:33 +00001390 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1391 InitExprs[Init] = expr;
1392 return Result;
1393}
1394
Argyrios Kyrtzidis446bcf22011-04-21 20:03:38 +00001395void InitListExpr::setArrayFiller(Expr *filler) {
1396 ArrayFillerOrUnionFieldInit = filler;
1397 // Fill out any "holes" in the array due to designated initializers.
1398 Expr **inits = getInits();
1399 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1400 if (inits[i] == 0)
1401 inits[i] = filler;
1402}
1403
Ted Kremenek16e6026f2010-11-09 02:11:40 +00001404SourceRange InitListExpr::getSourceRange() const {
1405 if (SyntacticForm)
1406 return SyntacticForm->getSourceRange();
1407 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1408 if (Beg.isInvalid()) {
1409 // Find the first non-null initializer.
1410 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1411 E = InitExprs.end();
1412 I != E; ++I) {
1413 if (Stmt *S = *I) {
1414 Beg = S->getLocStart();
1415 break;
1416 }
1417 }
1418 }
1419 if (End.isInvalid()) {
1420 // Find the first non-null initializer from the end.
1421 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1422 E = InitExprs.rend();
1423 I != E; ++I) {
1424 if (Stmt *S = *I) {
1425 End = S->getSourceRange().getEnd();
1426 break;
1427 }
1428 }
1429 }
1430 return SourceRange(Beg, End);
1431}
1432
Steve Naroff991e99d2008-09-04 15:31:07 +00001433/// getFunctionType - Return the underlying function type for this block.
Steve Naroffc540d662008-09-03 18:15:37 +00001434///
1435const FunctionType *BlockExpr::getFunctionType() const {
Ted Kremenekc23c7e62009-07-29 21:53:49 +00001436 return getType()->getAs<BlockPointerType>()->
John McCall9dd450b2009-09-21 23:43:11 +00001437 getPointeeType()->getAs<FunctionType>();
Steve Naroffc540d662008-09-03 18:15:37 +00001438}
1439
Mike Stump11289f42009-09-09 15:08:12 +00001440SourceLocation BlockExpr::getCaretLocation() const {
1441 return TheBlock->getCaretLocation();
Steve Naroff415d3d52008-10-08 17:01:13 +00001442}
Mike Stump11289f42009-09-09 15:08:12 +00001443const Stmt *BlockExpr::getBody() const {
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001444 return TheBlock->getBody();
1445}
Mike Stump11289f42009-09-09 15:08:12 +00001446Stmt *BlockExpr::getBody() {
1447 return TheBlock->getBody();
Douglas Gregore3dcb2d2009-04-18 00:02:19 +00001448}
Steve Naroff415d3d52008-10-08 17:01:13 +00001449
1450
Chris Lattner1ec5f562007-06-27 05:38:08 +00001451//===----------------------------------------------------------------------===//
1452// Generic Expression Routines
1453//===----------------------------------------------------------------------===//
1454
Chris Lattner237f2752009-02-14 07:37:35 +00001455/// isUnusedResultAWarning - Return true if this immediate expression should
1456/// be warned about if the result is unused. If so, fill in Loc and Ranges
1457/// with location to warn on and the source range[s] to report with the
1458/// warning.
1459bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1,
Mike Stump53f9ded2009-11-03 23:25:48 +00001460 SourceRange &R2, ASTContext &Ctx) const {
Anders Carlsson789e2cc2009-05-15 23:10:19 +00001461 // Don't warn if the expr is type dependent. The type could end up
1462 // instantiating to void.
1463 if (isTypeDependent())
1464 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001465
Chris Lattner1ec5f562007-06-27 05:38:08 +00001466 switch (getStmtClass()) {
1467 default:
John McCallc493a732010-03-12 07:11:26 +00001468 if (getType()->isVoidType())
1469 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001470 Loc = getExprLoc();
1471 R1 = getSourceRange();
1472 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001473 case ParenExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001474 return cast<ParenExpr>(this)->getSubExpr()->
Mike Stump53f9ded2009-11-03 23:25:48 +00001475 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Peter Collingbourne91147592011-04-15 00:35:48 +00001476 case GenericSelectionExprClass:
1477 return cast<GenericSelectionExpr>(this)->getResultExpr()->
1478 isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001479 case UnaryOperatorClass: {
1480 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump11289f42009-09-09 15:08:12 +00001481
Chris Lattner1ec5f562007-06-27 05:38:08 +00001482 switch (UO->getOpcode()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001483 default: break;
John McCalle3027922010-08-25 11:45:40 +00001484 case UO_PostInc:
1485 case UO_PostDec:
1486 case UO_PreInc:
1487 case UO_PreDec: // ++/--
Chris Lattner237f2752009-02-14 07:37:35 +00001488 return false; // Not a warning.
John McCalle3027922010-08-25 11:45:40 +00001489 case UO_Deref:
Chris Lattnera44d1162007-06-27 05:58:59 +00001490 // Dereferencing a volatile pointer is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001491 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001492 return false;
1493 break;
John McCalle3027922010-08-25 11:45:40 +00001494 case UO_Real:
1495 case UO_Imag:
Chris Lattnera44d1162007-06-27 05:58:59 +00001496 // accessing a piece of a volatile complex is a side-effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001497 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1498 .isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001499 return false;
1500 break;
John McCalle3027922010-08-25 11:45:40 +00001501 case UO_Extension:
Mike Stump53f9ded2009-11-03 23:25:48 +00001502 return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Chris Lattner1ec5f562007-06-27 05:38:08 +00001503 }
Chris Lattner237f2752009-02-14 07:37:35 +00001504 Loc = UO->getOperatorLoc();
1505 R1 = UO->getSubExpr()->getSourceRange();
1506 return true;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001507 }
Chris Lattnerae7a8342007-12-01 06:07:34 +00001508 case BinaryOperatorClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001509 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenek43a9c962010-04-07 18:49:21 +00001510 switch (BO->getOpcode()) {
1511 default:
1512 break;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001513 // Consider the RHS of comma for side effects. LHS was checked by
1514 // Sema::CheckCommaOperands.
John McCalle3027922010-08-25 11:45:40 +00001515 case BO_Comma:
Ted Kremenek43a9c962010-04-07 18:49:21 +00001516 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1517 // lvalue-ness) of an assignment written in a macro.
1518 if (IntegerLiteral *IE =
1519 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1520 if (IE->getValue() == 0)
1521 return false;
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001522 return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1523 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCalle3027922010-08-25 11:45:40 +00001524 case BO_LAnd:
1525 case BO_LOr:
Argyrios Kyrtzidis639ffb02010-06-30 10:53:14 +00001526 if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) ||
1527 !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1528 return false;
1529 break;
John McCall1e3715a2010-02-16 04:10:53 +00001530 }
Chris Lattner237f2752009-02-14 07:37:35 +00001531 if (BO->isAssignmentOp())
1532 return false;
1533 Loc = BO->getOperatorLoc();
1534 R1 = BO->getLHS()->getSourceRange();
1535 R2 = BO->getRHS()->getSourceRange();
1536 return true;
Chris Lattnerae7a8342007-12-01 06:07:34 +00001537 }
Chris Lattner86928112007-08-25 02:00:02 +00001538 case CompoundAssignOperatorClass:
Douglas Gregor0bbe94d2010-05-08 22:41:50 +00001539 case VAArgExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001540 return false;
Chris Lattner1ec5f562007-06-27 05:38:08 +00001541
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001542 case ConditionalOperatorClass: {
Ted Kremeneke96dad92011-03-01 20:34:48 +00001543 // If only one of the LHS or RHS is a warning, the operator might
1544 // be being used for control flow. Only warn if both the LHS and
1545 // RHS are warnings.
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001546 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Ted Kremeneke96dad92011-03-01 20:34:48 +00001547 if (!Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx))
1548 return false;
1549 if (!Exp->getLHS())
Chris Lattner237f2752009-02-14 07:37:35 +00001550 return true;
Ted Kremeneke96dad92011-03-01 20:34:48 +00001551 return Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Fariborz Jahanian9fac54d2007-12-01 19:58:28 +00001552 }
1553
Chris Lattnera44d1162007-06-27 05:58:59 +00001554 case MemberExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001555 // If the base pointer or element is to a volatile pointer/field, accessing
1556 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001557 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001558 return false;
1559 Loc = cast<MemberExpr>(this)->getMemberLoc();
1560 R1 = SourceRange(Loc, Loc);
1561 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1562 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001563
Chris Lattner1ec5f562007-06-27 05:38:08 +00001564 case ArraySubscriptExprClass:
Chris Lattnera44d1162007-06-27 05:58:59 +00001565 // If the base pointer or element is to a volatile pointer/field, accessing
Chris Lattner237f2752009-02-14 07:37:35 +00001566 // it is a side effect.
Mike Stump53f9ded2009-11-03 23:25:48 +00001567 if (Ctx.getCanonicalType(getType()).isVolatileQualified())
Chris Lattner237f2752009-02-14 07:37:35 +00001568 return false;
1569 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1570 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1571 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1572 return true;
Eli Friedman824f8c12008-05-27 15:24:04 +00001573
Chandler Carruth46339472011-08-17 09:49:44 +00001574 case CXXOperatorCallExprClass: {
1575 // We warn about operator== and operator!= even when user-defined operator
1576 // overloads as there is no reasonable way to define these such that they
1577 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1578 // warning: these operators are commonly typo'ed, and so warning on them
1579 // provides additional value as well. If this list is updated,
1580 // DiagnoseUnusedComparison should be as well.
1581 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1582 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00001583 Op->getOperator() == OO_ExclaimEqual) {
1584 Loc = Op->getOperatorLoc();
1585 R1 = Op->getSourceRange();
Chandler Carruth46339472011-08-17 09:49:44 +00001586 return true;
Matt Beaumont-Gaydcaacaa2011-09-19 18:51:20 +00001587 }
Chandler Carruth46339472011-08-17 09:49:44 +00001588
1589 // Fallthrough for generic call handling.
1590 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001591 case CallExprClass:
Eli Friedmandebdc1d2009-04-29 16:35:53 +00001592 case CXXMemberCallExprClass: {
Chris Lattner237f2752009-02-14 07:37:35 +00001593 // If this is a direct call, get the callee.
1594 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopes518e3702009-12-20 23:11:08 +00001595 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner237f2752009-02-14 07:37:35 +00001596 // If the callee has attribute pure, const, or warn_unused_result, warn
1597 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattner1a6babf2009-10-13 04:53:48 +00001598 //
1599 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1600 // updated to match for QoI.
1601 if (FD->getAttr<WarnUnusedResultAttr>() ||
1602 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
1603 Loc = CE->getCallee()->getLocStart();
1604 R1 = CE->getCallee()->getSourceRange();
Mike Stump11289f42009-09-09 15:08:12 +00001605
Chris Lattner1a6babf2009-10-13 04:53:48 +00001606 if (unsigned NumArgs = CE->getNumArgs())
1607 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1608 CE->getArg(NumArgs-1)->getLocEnd());
1609 return true;
1610 }
Chris Lattner237f2752009-02-14 07:37:35 +00001611 }
1612 return false;
1613 }
Anders Carlsson6aa50392009-11-17 17:11:23 +00001614
1615 case CXXTemporaryObjectExprClass:
1616 case CXXConstructExprClass:
1617 return false;
1618
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001619 case ObjCMessageExprClass: {
1620 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
John McCall31168b02011-06-15 23:02:42 +00001621 if (Ctx.getLangOptions().ObjCAutoRefCount &&
1622 ME->isInstanceMessage() &&
1623 !ME->getType()->isVoidType() &&
1624 ME->getSelector().getIdentifierInfoForSlot(0) &&
1625 ME->getSelector().getIdentifierInfoForSlot(0)
1626 ->getName().startswith("init")) {
1627 Loc = getExprLoc();
1628 R1 = ME->getSourceRange();
1629 return true;
1630 }
1631
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001632 const ObjCMethodDecl *MD = ME->getMethodDecl();
1633 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
1634 Loc = getExprLoc();
1635 return true;
1636 }
Chris Lattner237f2752009-02-14 07:37:35 +00001637 return false;
Fariborz Jahanian5cab26d2010-03-30 18:22:15 +00001638 }
Mike Stump11289f42009-09-09 15:08:12 +00001639
John McCallb7bd14f2010-12-02 01:19:52 +00001640 case ObjCPropertyRefExprClass:
Chris Lattnerd37f61c2009-08-16 16:51:50 +00001641 Loc = getExprLoc();
1642 R1 = getSourceRange();
Chris Lattnerd8b800a2009-08-16 16:45:18 +00001643 return true;
John McCallb7bd14f2010-12-02 01:19:52 +00001644
Chris Lattner944d3062008-07-26 19:51:01 +00001645 case StmtExprClass: {
1646 // Statement exprs don't logically have side effects themselves, but are
1647 // sometimes used in macros in ways that give them a type that is unused.
1648 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1649 // however, if the result of the stmt expr is dead, we don't want to emit a
1650 // warning.
1651 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001652 if (!CS->body_empty()) {
Chris Lattner944d3062008-07-26 19:51:01 +00001653 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Mike Stump53f9ded2009-11-03 23:25:48 +00001654 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
Argyrios Kyrtzidis90963412010-09-19 21:21:10 +00001655 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1656 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
1657 return E->isUnusedResultAWarning(Loc, R1, R2, Ctx);
1658 }
Mike Stump11289f42009-09-09 15:08:12 +00001659
John McCallc493a732010-03-12 07:11:26 +00001660 if (getType()->isVoidType())
1661 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001662 Loc = cast<StmtExpr>(this)->getLParenLoc();
1663 R1 = getSourceRange();
1664 return true;
Chris Lattner944d3062008-07-26 19:51:01 +00001665 }
Douglas Gregorf19b2312008-10-28 15:36:24 +00001666 case CStyleCastExprClass:
Chris Lattner2706a552009-07-28 18:25:28 +00001667 // If this is an explicit cast to void, allow it. People do this when they
1668 // think they know what they're doing :).
Chris Lattner237f2752009-02-14 07:37:35 +00001669 if (getType()->isVoidType())
Chris Lattner2706a552009-07-28 18:25:28 +00001670 return false;
Chris Lattner237f2752009-02-14 07:37:35 +00001671 Loc = cast<CStyleCastExpr>(this)->getLParenLoc();
1672 R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange();
1673 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001674 case CXXFunctionalCastExprClass: {
John McCallc493a732010-03-12 07:11:26 +00001675 if (getType()->isVoidType())
1676 return false;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001677 const CastExpr *CE = cast<CastExpr>(this);
Alexis Hunta8136cc2010-05-05 15:23:54 +00001678
Anders Carlsson6aa50392009-11-17 17:11:23 +00001679 // If this is a cast to void or a constructor conversion, check the operand.
1680 // Otherwise, the result of the cast is unused.
John McCalle3027922010-08-25 11:45:40 +00001681 if (CE->getCastKind() == CK_ToVoid ||
1682 CE->getCastKind() == CK_ConstructorConversion)
Mike Stump53f9ded2009-11-03 23:25:48 +00001683 return (cast<CastExpr>(this)->getSubExpr()
1684 ->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Chris Lattner237f2752009-02-14 07:37:35 +00001685 Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc();
1686 R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange();
1687 return true;
Anders Carlsson6aa50392009-11-17 17:11:23 +00001688 }
Mike Stump11289f42009-09-09 15:08:12 +00001689
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001690 case ImplicitCastExprClass:
1691 // Check the operand, since implicit casts are inserted by Sema
Mike Stump53f9ded2009-11-03 23:25:48 +00001692 return (cast<ImplicitCastExpr>(this)
1693 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Eli Friedmanca8da1d2008-05-19 21:24:43 +00001694
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00001695 case CXXDefaultArgExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001696 return (cast<CXXDefaultArgExpr>(this)
1697 ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001698
1699 case CXXNewExprClass:
1700 // FIXME: In theory, there might be new expressions that don't have side
1701 // effects (e.g. a placement new with an uninitialized POD).
1702 case CXXDeleteExprClass:
Chris Lattner237f2752009-02-14 07:37:35 +00001703 return false;
Anders Carlssone80ccac2009-08-16 04:11:06 +00001704 case CXXBindTemporaryExprClass:
Mike Stump53f9ded2009-11-03 23:25:48 +00001705 return (cast<CXXBindTemporaryExpr>(this)
1706 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
John McCall5d413782010-12-06 08:20:24 +00001707 case ExprWithCleanupsClass:
1708 return (cast<ExprWithCleanups>(this)
Mike Stump53f9ded2009-11-03 23:25:48 +00001709 ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx));
Sebastian Redlbd150f42008-11-21 19:14:01 +00001710 }
Chris Lattner1ec5f562007-06-27 05:38:08 +00001711}
1712
Fariborz Jahanian07735332009-02-22 18:40:18 +00001713/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian063c7722009-09-08 23:38:54 +00001714/// returns true, if it is; false otherwise.
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001715bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbourne91147592011-04-15 00:35:48 +00001716 const Expr *E = IgnoreParens();
1717 switch (E->getStmtClass()) {
Fariborz Jahanian07735332009-02-22 18:40:18 +00001718 default:
1719 return false;
1720 case ObjCIvarRefExprClass:
1721 return true;
Fariborz Jahanian392124c2009-02-23 18:59:50 +00001722 case Expr::UnaryOperatorClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001723 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001724 case ImplicitCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001725 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregorfe314812011-06-21 17:03:29 +00001726 case MaterializeTemporaryExprClass:
1727 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
1728 ->isOBJCGCCandidate(Ctx);
Fariborz Jahaniana16904b2009-05-05 23:28:21 +00001729 case CStyleCastExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001730 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor4bd90e52009-10-23 18:54:35 +00001731 case DeclRefExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00001732 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001733 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1734 if (VD->hasGlobalStorage())
1735 return true;
1736 QualType T = VD->getType();
Fariborz Jahaniancceedbf2009-09-16 18:09:18 +00001737 // dereferencing to a pointer is always a gc'able candidate,
1738 // unless it is __weak.
Daniel Dunbar4782a6e2009-09-17 06:31:17 +00001739 return T->isPointerType() &&
John McCall8ccfcb52009-09-24 19:53:00 +00001740 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001741 }
Fariborz Jahanian07735332009-02-22 18:40:18 +00001742 return false;
1743 }
Douglas Gregorf405d7e2009-08-31 23:41:50 +00001744 case MemberExprClass: {
Peter Collingbourne91147592011-04-15 00:35:48 +00001745 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanianc6d98002009-06-01 21:29:32 +00001746 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001747 }
1748 case ArraySubscriptExprClass:
Peter Collingbourne91147592011-04-15 00:35:48 +00001749 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian07735332009-02-22 18:40:18 +00001750 }
1751}
Sebastian Redlce354af2010-09-10 20:55:33 +00001752
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001753bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
1754 if (isTypeDependent())
1755 return false;
John McCall086a4642010-11-24 05:12:34 +00001756 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidisca766292010-11-01 18:49:26 +00001757}
1758
John McCall0009fcc2011-04-26 20:42:42 +00001759QualType Expr::findBoundMemberType(const Expr *expr) {
1760 assert(expr->getType()->isSpecificPlaceholderType(BuiltinType::BoundMember));
1761
1762 // Bound member expressions are always one of these possibilities:
1763 // x->m x.m x->*y x.*y
1764 // (possibly parenthesized)
1765
1766 expr = expr->IgnoreParens();
1767 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
1768 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
1769 return mem->getMemberDecl()->getType();
1770 }
1771
1772 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
1773 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
1774 ->getPointeeType();
1775 assert(type->isFunctionType());
1776 return type;
1777 }
1778
1779 assert(isa<UnresolvedMemberExpr>(expr));
1780 return QualType();
1781}
1782
Sebastian Redlce354af2010-09-10 20:55:33 +00001783static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1,
1784 Expr::CanThrowResult CT2) {
1785 // CanThrowResult constants are ordered so that the maximum is the correct
1786 // merge result.
1787 return CT1 > CT2 ? CT1 : CT2;
1788}
1789
1790static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) {
1791 Expr *E = const_cast<Expr*>(CE);
1792 Expr::CanThrowResult R = Expr::CT_Cannot;
John McCall8322c3a2011-02-13 04:07:26 +00001793 for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) {
Sebastian Redlce354af2010-09-10 20:55:33 +00001794 R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C));
1795 }
1796 return R;
1797}
1798
Richard Smith938f40b2011-06-11 17:19:42 +00001799static Expr::CanThrowResult CanCalleeThrow(ASTContext &Ctx, const Expr *E,
1800 const Decl *D,
Sebastian Redlce354af2010-09-10 20:55:33 +00001801 bool NullThrows = true) {
1802 if (!D)
1803 return NullThrows ? Expr::CT_Can : Expr::CT_Cannot;
1804
1805 // See if we can get a function type from the decl somehow.
1806 const ValueDecl *VD = dyn_cast<ValueDecl>(D);
1807 if (!VD) // If we have no clue what we're calling, assume the worst.
1808 return Expr::CT_Can;
1809
Sebastian Redlb8a76c42010-09-10 22:34:40 +00001810 // As an extension, we assume that __attribute__((nothrow)) functions don't
1811 // throw.
1812 if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>())
1813 return Expr::CT_Cannot;
1814
Sebastian Redlce354af2010-09-10 20:55:33 +00001815 QualType T = VD->getType();
1816 const FunctionProtoType *FT;
1817 if ((FT = T->getAs<FunctionProtoType>())) {
1818 } else if (const PointerType *PT = T->getAs<PointerType>())
1819 FT = PT->getPointeeType()->getAs<FunctionProtoType>();
1820 else if (const ReferenceType *RT = T->getAs<ReferenceType>())
1821 FT = RT->getPointeeType()->getAs<FunctionProtoType>();
1822 else if (const MemberPointerType *MT = T->getAs<MemberPointerType>())
1823 FT = MT->getPointeeType()->getAs<FunctionProtoType>();
1824 else if (const BlockPointerType *BT = T->getAs<BlockPointerType>())
1825 FT = BT->getPointeeType()->getAs<FunctionProtoType>();
1826
1827 if (!FT)
1828 return Expr::CT_Can;
1829
Richard Smith938f40b2011-06-11 17:19:42 +00001830 if (FT->getExceptionSpecType() == EST_Delayed) {
1831 assert(isa<CXXConstructorDecl>(D) &&
1832 "only constructor exception specs can be unknown");
1833 Ctx.getDiagnostics().Report(E->getLocStart(),
1834 diag::err_exception_spec_unknown)
1835 << E->getSourceRange();
1836 return Expr::CT_Can;
1837 }
1838
Sebastian Redl31ad7542011-03-13 17:09:40 +00001839 return FT->isNothrow(Ctx) ? Expr::CT_Cannot : Expr::CT_Can;
Sebastian Redlce354af2010-09-10 20:55:33 +00001840}
1841
1842static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) {
1843 if (DC->isTypeDependent())
1844 return Expr::CT_Dependent;
1845
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001846 if (!DC->getTypeAsWritten()->isReferenceType())
1847 return Expr::CT_Cannot;
1848
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001849 if (DC->getSubExpr()->isTypeDependent())
1850 return Expr::CT_Dependent;
1851
Sebastian Redlce354af2010-09-10 20:55:33 +00001852 return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot;
1853}
1854
1855static Expr::CanThrowResult CanTypeidThrow(ASTContext &C,
1856 const CXXTypeidExpr *DC) {
1857 if (DC->isTypeOperand())
1858 return Expr::CT_Cannot;
1859
1860 Expr *Op = DC->getExprOperand();
1861 if (Op->isTypeDependent())
1862 return Expr::CT_Dependent;
1863
1864 const RecordType *RT = Op->getType()->getAs<RecordType>();
1865 if (!RT)
1866 return Expr::CT_Cannot;
1867
1868 if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic())
1869 return Expr::CT_Cannot;
1870
1871 if (Op->Classify(C).isPRValue())
1872 return Expr::CT_Cannot;
1873
1874 return Expr::CT_Can;
1875}
1876
1877Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const {
1878 // C++ [expr.unary.noexcept]p3:
1879 // [Can throw] if in a potentially-evaluated context the expression would
1880 // contain:
1881 switch (getStmtClass()) {
1882 case CXXThrowExprClass:
1883 // - a potentially evaluated throw-expression
1884 return CT_Can;
1885
1886 case CXXDynamicCastExprClass: {
1887 // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v),
1888 // where T is a reference type, that requires a run-time check
1889 CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this));
1890 if (CT == CT_Can)
1891 return CT;
1892 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1893 }
1894
1895 case CXXTypeidExprClass:
1896 // - a potentially evaluated typeid expression applied to a glvalue
1897 // expression whose type is a polymorphic class type
1898 return CanTypeidThrow(C, cast<CXXTypeidExpr>(this));
1899
1900 // - a potentially evaluated call to a function, member function, function
1901 // pointer, or member function pointer that does not have a non-throwing
1902 // exception-specification
1903 case CallExprClass:
1904 case CXXOperatorCallExprClass:
1905 case CXXMemberCallExprClass: {
Eli Friedman622e4fc2011-05-12 02:11:32 +00001906 const CallExpr *CE = cast<CallExpr>(this);
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001907 CanThrowResult CT;
1908 if (isTypeDependent())
1909 CT = CT_Dependent;
Eli Friedman622e4fc2011-05-12 02:11:32 +00001910 else if (isa<CXXPseudoDestructorExpr>(CE->getCallee()->IgnoreParens()))
1911 CT = CT_Cannot;
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001912 else
Richard Smith938f40b2011-06-11 17:19:42 +00001913 CT = CanCalleeThrow(C, this, CE->getCalleeDecl());
Sebastian Redlce354af2010-09-10 20:55:33 +00001914 if (CT == CT_Can)
1915 return CT;
1916 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1917 }
1918
Sebastian Redl5f0180d2010-09-10 20:55:47 +00001919 case CXXConstructExprClass:
1920 case CXXTemporaryObjectExprClass: {
Richard Smith938f40b2011-06-11 17:19:42 +00001921 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redlce354af2010-09-10 20:55:33 +00001922 cast<CXXConstructExpr>(this)->getConstructor());
1923 if (CT == CT_Can)
1924 return CT;
1925 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1926 }
1927
1928 case CXXNewExprClass: {
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001929 CanThrowResult CT;
1930 if (isTypeDependent())
1931 CT = CT_Dependent;
1932 else
1933 CT = MergeCanThrow(
Richard Smith938f40b2011-06-11 17:19:42 +00001934 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getOperatorNew()),
1935 CanCalleeThrow(C, this, cast<CXXNewExpr>(this)->getConstructor(),
Sebastian Redlce354af2010-09-10 20:55:33 +00001936 /*NullThrows*/false));
1937 if (CT == CT_Can)
1938 return CT;
1939 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1940 }
1941
1942 case CXXDeleteExprClass: {
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001943 CanThrowResult CT;
1944 QualType DTy = cast<CXXDeleteExpr>(this)->getDestroyedType();
1945 if (DTy.isNull() || DTy->isDependentType()) {
1946 CT = CT_Dependent;
1947 } else {
Richard Smith938f40b2011-06-11 17:19:42 +00001948 CT = CanCalleeThrow(C, this,
1949 cast<CXXDeleteExpr>(this)->getOperatorDelete());
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001950 if (const RecordType *RT = DTy->getAs<RecordType>()) {
1951 const CXXRecordDecl *RD = cast<CXXRecordDecl>(RT->getDecl());
Richard Smith938f40b2011-06-11 17:19:42 +00001952 CT = MergeCanThrow(CT, CanCalleeThrow(C, this, RD->getDestructor()));
Sebastian Redla8bac372010-09-10 23:27:10 +00001953 }
Eli Friedmanc6587cc2011-05-11 05:22:44 +00001954 if (CT == CT_Can)
1955 return CT;
Sebastian Redla8bac372010-09-10 23:27:10 +00001956 }
1957 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1958 }
1959
1960 case CXXBindTemporaryExprClass: {
1961 // The bound temporary has to be destroyed again, which might throw.
Richard Smith938f40b2011-06-11 17:19:42 +00001962 CanThrowResult CT = CanCalleeThrow(C, this,
Sebastian Redla8bac372010-09-10 23:27:10 +00001963 cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor());
1964 if (CT == CT_Can)
1965 return CT;
Sebastian Redlce354af2010-09-10 20:55:33 +00001966 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
1967 }
1968
1969 // ObjC message sends are like function calls, but never have exception
1970 // specs.
1971 case ObjCMessageExprClass:
1972 case ObjCPropertyRefExprClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001973 return CT_Can;
1974
1975 // Many other things have subexpressions, so we have to test those.
1976 // Some are simple:
1977 case ParenExprClass:
1978 case MemberExprClass:
1979 case CXXReinterpretCastExprClass:
1980 case CXXConstCastExprClass:
1981 case ConditionalOperatorClass:
1982 case CompoundLiteralExprClass:
1983 case ExtVectorElementExprClass:
1984 case InitListExprClass:
1985 case DesignatedInitExprClass:
1986 case ParenListExprClass:
1987 case VAArgExprClass:
1988 case CXXDefaultArgExprClass:
John McCall5d413782010-12-06 08:20:24 +00001989 case ExprWithCleanupsClass:
Sebastian Redlce354af2010-09-10 20:55:33 +00001990 case ObjCIvarRefExprClass:
1991 case ObjCIsaExprClass:
1992 case ShuffleVectorExprClass:
1993 return CanSubExprsThrow(C, this);
1994
1995 // Some might be dependent for other reasons.
1996 case UnaryOperatorClass:
1997 case ArraySubscriptExprClass:
1998 case ImplicitCastExprClass:
1999 case CStyleCastExprClass:
2000 case CXXStaticCastExprClass:
2001 case CXXFunctionalCastExprClass:
2002 case BinaryOperatorClass:
Douglas Gregorfe314812011-06-21 17:03:29 +00002003 case CompoundAssignOperatorClass:
2004 case MaterializeTemporaryExprClass: {
Sebastian Redlce354af2010-09-10 20:55:33 +00002005 CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot;
2006 return MergeCanThrow(CT, CanSubExprsThrow(C, this));
2007 }
2008
2009 // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms.
2010 case StmtExprClass:
2011 return CT_Can;
2012
2013 case ChooseExprClass:
2014 if (isTypeDependent() || isValueDependent())
2015 return CT_Dependent;
2016 return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C);
2017
Peter Collingbourne91147592011-04-15 00:35:48 +00002018 case GenericSelectionExprClass:
2019 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2020 return CT_Dependent;
2021 return cast<GenericSelectionExpr>(this)->getResultExpr()->CanThrow(C);
2022
Sebastian Redlce354af2010-09-10 20:55:33 +00002023 // Some expressions are always dependent.
2024 case DependentScopeDeclRefExprClass:
2025 case CXXUnresolvedConstructExprClass:
2026 case CXXDependentScopeMemberExprClass:
2027 return CT_Dependent;
2028
2029 default:
2030 // All other expressions don't have subexpressions, or else they are
2031 // unevaluated.
2032 return CT_Cannot;
2033 }
2034}
2035
Ted Kremenekfff70962008-01-17 16:57:34 +00002036Expr* Expr::IgnoreParens() {
2037 Expr* E = this;
Abramo Bagnara932e3932010-10-15 07:51:18 +00002038 while (true) {
2039 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2040 E = P->getSubExpr();
2041 continue;
2042 }
2043 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2044 if (P->getOpcode() == UO_Extension) {
2045 E = P->getSubExpr();
2046 continue;
2047 }
2048 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002049 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2050 if (!P->isResultDependent()) {
2051 E = P->getResultExpr();
2052 continue;
2053 }
2054 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002055 return E;
2056 }
Ted Kremenekfff70962008-01-17 16:57:34 +00002057}
2058
Chris Lattnerf2660962008-02-13 01:02:39 +00002059/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2060/// or CastExprs or ImplicitCastExprs, returning their operand.
2061Expr *Expr::IgnoreParenCasts() {
2062 Expr *E = this;
2063 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00002064 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002065 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002066 continue;
2067 }
2068 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattnerf2660962008-02-13 01:02:39 +00002069 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002070 continue;
2071 }
2072 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2073 if (P->getOpcode() == UO_Extension) {
2074 E = P->getSubExpr();
2075 continue;
2076 }
2077 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002078 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2079 if (!P->isResultDependent()) {
2080 E = P->getResultExpr();
2081 continue;
2082 }
2083 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002084 if (MaterializeTemporaryExpr *Materialize
2085 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2086 E = Materialize->GetTemporaryExpr();
2087 continue;
2088 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002089 if (SubstNonTypeTemplateParmExpr *NTTP
2090 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2091 E = NTTP->getReplacement();
2092 continue;
2093 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002094 return E;
Chris Lattnerf2660962008-02-13 01:02:39 +00002095 }
2096}
2097
John McCall5a4ce8b2010-12-04 08:24:19 +00002098/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2099/// casts. This is intended purely as a temporary workaround for code
2100/// that hasn't yet been rewritten to do the right thing about those
2101/// casts, and may disappear along with the last internal use.
John McCall34376a62010-12-04 03:47:34 +00002102Expr *Expr::IgnoreParenLValueCasts() {
2103 Expr *E = this;
John McCall5a4ce8b2010-12-04 08:24:19 +00002104 while (true) {
John McCall34376a62010-12-04 03:47:34 +00002105 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2106 E = P->getSubExpr();
2107 continue;
John McCall5a4ce8b2010-12-04 08:24:19 +00002108 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002109 if (P->getCastKind() == CK_LValueToRValue) {
2110 E = P->getSubExpr();
2111 continue;
2112 }
John McCall5a4ce8b2010-12-04 08:24:19 +00002113 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2114 if (P->getOpcode() == UO_Extension) {
2115 E = P->getSubExpr();
2116 continue;
2117 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002118 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2119 if (!P->isResultDependent()) {
2120 E = P->getResultExpr();
2121 continue;
2122 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002123 } else if (MaterializeTemporaryExpr *Materialize
2124 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2125 E = Materialize->GetTemporaryExpr();
2126 continue;
Douglas Gregor6a40b082011-09-08 17:56:33 +00002127 } else if (SubstNonTypeTemplateParmExpr *NTTP
2128 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2129 E = NTTP->getReplacement();
2130 continue;
John McCall34376a62010-12-04 03:47:34 +00002131 }
2132 break;
2133 }
2134 return E;
2135}
2136
John McCalleebc8322010-05-05 22:59:52 +00002137Expr *Expr::IgnoreParenImpCasts() {
2138 Expr *E = this;
2139 while (true) {
Abramo Bagnara932e3932010-10-15 07:51:18 +00002140 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002141 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002142 continue;
2143 }
2144 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCalleebc8322010-05-05 22:59:52 +00002145 E = P->getSubExpr();
Abramo Bagnara932e3932010-10-15 07:51:18 +00002146 continue;
2147 }
2148 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2149 if (P->getOpcode() == UO_Extension) {
2150 E = P->getSubExpr();
2151 continue;
2152 }
2153 }
Peter Collingbourne91147592011-04-15 00:35:48 +00002154 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2155 if (!P->isResultDependent()) {
2156 E = P->getResultExpr();
2157 continue;
2158 }
2159 }
Douglas Gregorfe314812011-06-21 17:03:29 +00002160 if (MaterializeTemporaryExpr *Materialize
2161 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2162 E = Materialize->GetTemporaryExpr();
2163 continue;
2164 }
Douglas Gregor6a40b082011-09-08 17:56:33 +00002165 if (SubstNonTypeTemplateParmExpr *NTTP
2166 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2167 E = NTTP->getReplacement();
2168 continue;
2169 }
Abramo Bagnara932e3932010-10-15 07:51:18 +00002170 return E;
John McCalleebc8322010-05-05 22:59:52 +00002171 }
2172}
2173
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002174Expr *Expr::IgnoreConversionOperator() {
2175 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth4352b0b2011-06-21 17:22:09 +00002176 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborgde2e67e2011-06-09 17:06:51 +00002177 return MCE->getImplicitObjectArgument();
2178 }
2179 return this;
2180}
2181
Chris Lattneref26c772009-03-13 17:28:01 +00002182/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2183/// value (including ptr->int casts of the same size). Strip off any
2184/// ParenExpr or CastExprs, returning their operand.
2185Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2186 Expr *E = this;
2187 while (true) {
2188 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2189 E = P->getSubExpr();
2190 continue;
2191 }
Mike Stump11289f42009-09-09 15:08:12 +00002192
Chris Lattneref26c772009-03-13 17:28:01 +00002193 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2194 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregorb90df602010-06-16 00:17:44 +00002195 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattneref26c772009-03-13 17:28:01 +00002196 Expr *SE = P->getSubExpr();
Mike Stump11289f42009-09-09 15:08:12 +00002197
Chris Lattneref26c772009-03-13 17:28:01 +00002198 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2199 E = SE;
2200 continue;
2201 }
Mike Stump11289f42009-09-09 15:08:12 +00002202
Abramo Bagnara932e3932010-10-15 07:51:18 +00002203 if ((E->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002204 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnara932e3932010-10-15 07:51:18 +00002205 (SE->getType()->isPointerType() ||
Douglas Gregor6972a622010-06-16 00:35:25 +00002206 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattneref26c772009-03-13 17:28:01 +00002207 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2208 E = SE;
2209 continue;
2210 }
2211 }
Mike Stump11289f42009-09-09 15:08:12 +00002212
Abramo Bagnara932e3932010-10-15 07:51:18 +00002213 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2214 if (P->getOpcode() == UO_Extension) {
2215 E = P->getSubExpr();
2216 continue;
2217 }
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 }
2226
Douglas Gregor6a40b082011-09-08 17:56:33 +00002227 if (SubstNonTypeTemplateParmExpr *NTTP
2228 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2229 E = NTTP->getReplacement();
2230 continue;
2231 }
2232
Chris Lattneref26c772009-03-13 17:28:01 +00002233 return E;
2234 }
2235}
2236
Douglas Gregord196a582009-12-14 19:27:10 +00002237bool Expr::isDefaultArgument() const {
2238 const Expr *E = this;
Douglas Gregorfe314812011-06-21 17:03:29 +00002239 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2240 E = M->GetTemporaryExpr();
2241
Douglas Gregord196a582009-12-14 19:27:10 +00002242 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2243 E = ICE->getSubExprAsWritten();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002244
Douglas Gregord196a582009-12-14 19:27:10 +00002245 return isa<CXXDefaultArgExpr>(E);
2246}
Chris Lattneref26c772009-03-13 17:28:01 +00002247
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002248/// \brief Skip over any no-op casts and any temporary-binding
2249/// expressions.
Anders Carlsson66bbf502010-11-28 16:40:49 +00002250static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregorfe314812011-06-21 17:03:29 +00002251 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2252 E = M->GetTemporaryExpr();
2253
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002254 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002255 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002256 E = ICE->getSubExpr();
2257 else
2258 break;
2259 }
2260
2261 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2262 E = BE->getSubExpr();
2263
2264 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCalle3027922010-08-25 11:45:40 +00002265 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002266 E = ICE->getSubExpr();
2267 else
2268 break;
2269 }
Anders Carlsson66bbf502010-11-28 16:40:49 +00002270
2271 return E->IgnoreParens();
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002272}
2273
John McCall7a626f62010-09-15 10:14:12 +00002274/// isTemporaryObject - Determines if this expression produces a
2275/// temporary of the given class type.
2276bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2277 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2278 return false;
2279
Anders Carlsson66bbf502010-11-28 16:40:49 +00002280 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002281
John McCall02dc8c72010-09-15 20:59:13 +00002282 // Temporaries are by definition pr-values of class type.
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002283 if (!E->Classify(C).isPRValue()) {
2284 // In this context, property reference is a message call and is pr-value.
John McCallb7bd14f2010-12-02 01:19:52 +00002285 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahanian30e8d582010-09-27 17:30:38 +00002286 return false;
2287 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002288
John McCallf4ee1dd2010-09-16 06:57:56 +00002289 // Black-list a few cases which yield pr-values of class type that don't
2290 // refer to temporaries of that type:
2291
2292 // - implicit derived-to-base conversions
John McCall7a626f62010-09-15 10:14:12 +00002293 if (isa<ImplicitCastExpr>(E)) {
2294 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2295 case CK_DerivedToBase:
2296 case CK_UncheckedDerivedToBase:
2297 return false;
2298 default:
2299 break;
2300 }
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002301 }
2302
John McCallf4ee1dd2010-09-16 06:57:56 +00002303 // - member expressions (all)
2304 if (isa<MemberExpr>(E))
2305 return false;
2306
John McCallc07a0c72011-02-17 10:25:35 +00002307 // - opaque values (all)
2308 if (isa<OpaqueValueExpr>(E))
2309 return false;
2310
John McCall7a626f62010-09-15 10:14:12 +00002311 return true;
Douglas Gregor45cf7e32010-04-02 18:24:57 +00002312}
2313
Douglas Gregor25b7e052011-03-02 21:06:53 +00002314bool Expr::isImplicitCXXThis() const {
2315 const Expr *E = this;
2316
2317 // Strip away parentheses and casts we don't care about.
2318 while (true) {
2319 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2320 E = Paren->getSubExpr();
2321 continue;
2322 }
2323
2324 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2325 if (ICE->getCastKind() == CK_NoOp ||
2326 ICE->getCastKind() == CK_LValueToRValue ||
2327 ICE->getCastKind() == CK_DerivedToBase ||
2328 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2329 E = ICE->getSubExpr();
2330 continue;
2331 }
2332 }
2333
2334 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2335 if (UnOp->getOpcode() == UO_Extension) {
2336 E = UnOp->getSubExpr();
2337 continue;
2338 }
2339 }
2340
Douglas Gregorfe314812011-06-21 17:03:29 +00002341 if (const MaterializeTemporaryExpr *M
2342 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2343 E = M->GetTemporaryExpr();
2344 continue;
2345 }
2346
Douglas Gregor25b7e052011-03-02 21:06:53 +00002347 break;
2348 }
2349
2350 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2351 return This->isImplicit();
2352
2353 return false;
2354}
2355
Douglas Gregor4619e432008-12-05 23:32:09 +00002356/// hasAnyTypeDependentArguments - Determines if any of the expressions
2357/// in Exprs is type-dependent.
2358bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) {
2359 for (unsigned I = 0; I < NumExprs; ++I)
2360 if (Exprs[I]->isTypeDependent())
2361 return true;
2362
2363 return false;
2364}
2365
2366/// hasAnyValueDependentArguments - Determines if any of the expressions
2367/// in Exprs is value-dependent.
2368bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) {
2369 for (unsigned I = 0; I < NumExprs; ++I)
2370 if (Exprs[I]->isValueDependent())
2371 return true;
2372
2373 return false;
2374}
2375
John McCall8b0f4ff2010-08-02 21:13:48 +00002376bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedman384da272009-01-25 03:12:18 +00002377 // This function is attempting whether an expression is an initializer
2378 // which can be evaluated at compile-time. isEvaluatable handles most
2379 // of the cases, but it can't deal with some initializer-specific
2380 // expressions, and it can't deal with aggregates; we deal with those here,
2381 // and fall back to isEvaluatable for the other cases.
2382
John McCall8b0f4ff2010-08-02 21:13:48 +00002383 // If we ever capture reference-binding directly in the AST, we can
2384 // kill the second parameter.
2385
2386 if (IsForRef) {
2387 EvalResult Result;
2388 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2389 }
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002390
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002391 switch (getStmtClass()) {
Eli Friedman384da272009-01-25 03:12:18 +00002392 default: break;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002393 case StringLiteralClass:
Steve Naroff7cae42b2009-07-10 23:34:53 +00002394 case ObjCStringLiteralClass:
Chris Lattnerd7e7b8e2009-02-24 22:18:39 +00002395 case ObjCEncodeExprClass:
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002396 return true;
John McCall81c9cea2010-08-01 21:51:45 +00002397 case CXXTemporaryObjectExprClass:
2398 case CXXConstructExprClass: {
2399 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall8b0f4ff2010-08-02 21:13:48 +00002400
2401 // Only if it's
2402 // 1) an application of the trivial default constructor or
John McCall81c9cea2010-08-01 21:51:45 +00002403 if (!CE->getConstructor()->isTrivial()) return false;
John McCall8b0f4ff2010-08-02 21:13:48 +00002404 if (!CE->getNumArgs()) return true;
2405
2406 // 2) an elidable trivial copy construction of an operand which is
2407 // itself a constant initializer. Note that we consider the
2408 // operand on its own, *not* as a reference binding.
2409 return CE->isElidable() &&
2410 CE->getArg(0)->isConstantInitializer(Ctx, false);
John McCall81c9cea2010-08-01 21:51:45 +00002411 }
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002412 case CompoundLiteralExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002413 // This handles gcc's extension that allows global initializers like
2414 // "struct x {int x;} x = (struct x) {};".
2415 // FIXME: This accepts other cases it shouldn't!
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002416 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall8b0f4ff2010-08-02 21:13:48 +00002417 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman2f2bdeb2009-01-18 03:20:47 +00002418 }
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002419 case InitListExprClass: {
Eli Friedmancf7cbe72009-02-20 02:36:22 +00002420 // FIXME: This doesn't deal with fields with reference types correctly.
2421 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2422 // to bitfields.
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002423 const InitListExpr *Exp = cast<InitListExpr>(this);
2424 unsigned numInits = Exp->getNumInits();
2425 for (unsigned i = 0; i < numInits; i++) {
John McCall8b0f4ff2010-08-02 21:13:48 +00002426 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002427 return false;
2428 }
Eli Friedman384da272009-01-25 03:12:18 +00002429 return true;
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002430 }
Douglas Gregor0202cb42009-01-29 17:44:32 +00002431 case ImplicitValueInitExprClass:
2432 return true;
Chris Lattner3eb172a2009-10-13 07:14:16 +00002433 case ParenExprClass:
John McCall8b0f4ff2010-08-02 21:13:48 +00002434 return cast<ParenExpr>(this)->getSubExpr()
2435 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbourne91147592011-04-15 00:35:48 +00002436 case GenericSelectionExprClass:
2437 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2438 return false;
2439 return cast<GenericSelectionExpr>(this)->getResultExpr()
2440 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnarab59a5b62010-09-27 07:13:32 +00002441 case ChooseExprClass:
2442 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2443 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedman384da272009-01-25 03:12:18 +00002444 case UnaryOperatorClass: {
2445 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002446 if (Exp->getOpcode() == UO_Extension)
John McCall8b0f4ff2010-08-02 21:13:48 +00002447 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedman384da272009-01-25 03:12:18 +00002448 break;
2449 }
Chris Lattner3eb172a2009-10-13 07:14:16 +00002450 case BinaryOperatorClass: {
2451 // Special case &&foo - &&bar. It would be nice to generalize this somehow
2452 // but this handles the common case.
2453 const BinaryOperator *Exp = cast<BinaryOperator>(this);
John McCalle3027922010-08-25 11:45:40 +00002454 if (Exp->getOpcode() == BO_Sub &&
Chris Lattner3eb172a2009-10-13 07:14:16 +00002455 isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) &&
2456 isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx)))
2457 return true;
2458 break;
2459 }
John McCall8b0f4ff2010-08-02 21:13:48 +00002460 case CXXFunctionalCastExprClass:
John McCall81c9cea2010-08-01 21:51:45 +00002461 case CXXStaticCastExprClass:
Chris Lattner1f02e052009-04-21 05:19:11 +00002462 case ImplicitCastExprClass:
Eli Friedman384da272009-01-25 03:12:18 +00002463 case CStyleCastExprClass:
2464 // Handle casts with a destination that's a struct or union; this
2465 // deals with both the gcc no-op struct cast extension and the
2466 // cast-to-union extension.
2467 if (getType()->isRecordType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002468 return cast<CastExpr>(this)->getSubExpr()
2469 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002470
Chris Lattnera2f9bd52009-10-13 22:12:09 +00002471 // Integer->integer casts can be handled here, which is important for
2472 // things like (int)(&&x-&&y). Scary but true.
2473 if (getType()->isIntegerType() &&
2474 cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType())
John McCall8b0f4ff2010-08-02 21:13:48 +00002475 return cast<CastExpr>(this)->getSubExpr()
2476 ->isConstantInitializer(Ctx, false);
Alexis Hunta8136cc2010-05-05 15:23:54 +00002477
Eli Friedman384da272009-01-25 03:12:18 +00002478 break;
Douglas Gregorfe314812011-06-21 17:03:29 +00002479
2480 case MaterializeTemporaryExprClass:
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002481 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregorfe314812011-06-21 17:03:29 +00002482 ->isConstantInitializer(Ctx, false);
Anders Carlssona7c5eb72008-11-24 05:23:59 +00002483 }
Eli Friedman384da272009-01-25 03:12:18 +00002484 return isEvaluatable(Ctx);
Steve Naroffb03f5942007-09-02 20:30:18 +00002485}
2486
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002487/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2488/// pointer constant or not, as well as the specific kind of constant detected.
2489/// Null pointer constants can be integer constant expressions with the
2490/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2491/// (a GNU extension).
2492Expr::NullPointerConstantKind
2493Expr::isNullPointerConstant(ASTContext &Ctx,
2494 NullPointerConstantValueDependence NPC) const {
Douglas Gregor56751b52009-09-25 04:25:58 +00002495 if (isValueDependent()) {
2496 switch (NPC) {
2497 case NPC_NeverValueDependent:
David Blaikie83d382b2011-09-23 05:06:16 +00002498 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregor56751b52009-09-25 04:25:58 +00002499 // If the unthinkable happens, fall through to the safest alternative.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002500
Douglas Gregor56751b52009-09-25 04:25:58 +00002501 case NPC_ValueDependentIsNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002502 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2503 return NPCK_ZeroInteger;
2504 else
2505 return NPCK_NotNull;
Alexis Hunta8136cc2010-05-05 15:23:54 +00002506
Douglas Gregor56751b52009-09-25 04:25:58 +00002507 case NPC_ValueDependentIsNotNull:
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002508 return NPCK_NotNull;
Douglas Gregor56751b52009-09-25 04:25:58 +00002509 }
2510 }
Daniel Dunbarebc51402009-09-18 08:46:16 +00002511
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002512 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis3bab3d22008-08-18 23:01:59 +00002513 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
Sebastian Redl273ce562008-11-04 11:45:54 +00002514 if (!Ctx.getLangOptions().CPlusPlus) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002515 // Check that it is a cast to void*.
Ted Kremenekc23c7e62009-07-29 21:53:49 +00002516 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002517 QualType Pointee = PT->getPointeeType();
John McCall8ccfcb52009-09-24 19:53:00 +00002518 if (!Pointee.hasQualifiers() &&
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002519 Pointee->isVoidType() && // to void*
2520 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregor56751b52009-09-25 04:25:58 +00002521 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl72b8aef2008-10-31 14:43:28 +00002522 }
Steve Naroffada7d422007-05-20 17:54:12 +00002523 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002524 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2525 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregor56751b52009-09-25 04:25:58 +00002526 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff4871fe02008-01-14 16:10:57 +00002527 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2528 // Accept ((void*)0) as a null pointer constant, as many other
2529 // implementations do.
Douglas Gregor56751b52009-09-25 04:25:58 +00002530 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbourne91147592011-04-15 00:35:48 +00002531 } else if (const GenericSelectionExpr *GE =
2532 dyn_cast<GenericSelectionExpr>(this)) {
2533 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump11289f42009-09-09 15:08:12 +00002534 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner58258242008-04-10 02:22:51 +00002535 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00002536 // See through default argument expressions
Douglas Gregor56751b52009-09-25 04:25:58 +00002537 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor3be4b122008-11-29 04:51:27 +00002538 } else if (isa<GNUNullExpr>(this)) {
2539 // The GNU __null extension is always a null pointer constant.
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002540 return NPCK_GNUNull;
Douglas Gregorfe314812011-06-21 17:03:29 +00002541 } else if (const MaterializeTemporaryExpr *M
2542 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2543 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroff09035312008-01-14 02:53:34 +00002544 }
Douglas Gregor3be4b122008-11-29 04:51:27 +00002545
Sebastian Redl576fd422009-05-10 18:38:11 +00002546 // C++0x nullptr_t is always a null pointer constant.
2547 if (getType()->isNullPtrType())
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002548 return NPCK_CXX0X_nullptr;
Sebastian Redl576fd422009-05-10 18:38:11 +00002549
Fariborz Jahanian3567c422010-09-27 22:42:37 +00002550 if (const RecordType *UT = getType()->getAsUnionType())
2551 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2552 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2553 const Expr *InitExpr = CLE->getInitializer();
2554 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2555 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2556 }
Steve Naroff4871fe02008-01-14 16:10:57 +00002557 // This expression must be an integer type.
Alexis Hunta8136cc2010-05-05 15:23:54 +00002558 if (!getType()->isIntegerType() ||
Fariborz Jahanian333bb732009-10-06 00:09:31 +00002559 (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType()))
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002560 return NPCK_NotNull;
Mike Stump11289f42009-09-09 15:08:12 +00002561
Chris Lattner1abbd412007-06-08 17:58:43 +00002562 // If we have an integer constant expression, we need to *evaluate* it and
2563 // test for the value 0.
Eli Friedman7524de12009-04-25 22:37:12 +00002564 llvm::APSInt Result;
Chandler Carrutha8bea4b2011-02-18 23:54:50 +00002565 bool IsNull = isIntegerConstantExpr(Result, Ctx) && Result == 0;
2566
2567 return (IsNull ? NPCK_ZeroInteger : NPCK_NotNull);
Steve Naroff218bc2b2007-05-04 21:54:46 +00002568}
Steve Narofff7a5da12007-07-28 23:10:27 +00002569
John McCall34376a62010-12-04 03:47:34 +00002570/// \brief If this expression is an l-value for an Objective C
2571/// property, find the underlying property reference expression.
2572const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2573 const Expr *E = this;
2574 while (true) {
2575 assert((E->getValueKind() == VK_LValue &&
2576 E->getObjectKind() == OK_ObjCProperty) &&
2577 "expression is not a property reference");
2578 E = E->IgnoreParenCasts();
2579 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2580 if (BO->getOpcode() == BO_Comma) {
2581 E = BO->getRHS();
2582 continue;
2583 }
2584 }
2585
2586 break;
2587 }
2588
2589 return cast<ObjCPropertyRefExpr>(E);
2590}
2591
Douglas Gregor71235ec2009-05-02 02:18:30 +00002592FieldDecl *Expr::getBitField() {
Douglas Gregor19623dc2009-07-06 15:38:40 +00002593 Expr *E = this->IgnoreParens();
Douglas Gregor71235ec2009-05-02 02:18:30 +00002594
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002595 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall34376a62010-12-04 03:47:34 +00002596 if (ICE->getCastKind() == CK_LValueToRValue ||
2597 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregor65eb86e2010-01-29 19:14:02 +00002598 E = ICE->getSubExpr()->IgnoreParens();
2599 else
2600 break;
2601 }
2602
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002603 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor2eedc3a2008-12-20 23:49:58 +00002604 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor71235ec2009-05-02 02:18:30 +00002605 if (Field->isBitField())
2606 return Field;
2607
Argyrios Kyrtzidisd3f00542010-10-30 19:52:22 +00002608 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2609 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2610 if (Field->isBitField())
2611 return Field;
2612
Eli Friedman609ada22011-07-13 02:05:57 +00002613 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor71235ec2009-05-02 02:18:30 +00002614 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2615 return BinOp->getLHS()->getBitField();
2616
Eli Friedman609ada22011-07-13 02:05:57 +00002617 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2618 return BinOp->getRHS()->getBitField();
2619 }
2620
Douglas Gregor71235ec2009-05-02 02:18:30 +00002621 return 0;
Douglas Gregor8e1cf602008-10-29 00:13:59 +00002622}
2623
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002624bool Expr::refersToVectorElement() const {
2625 const Expr *E = this->IgnoreParens();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002626
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002627 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2536c6d2010-08-25 10:28:54 +00002628 if (ICE->getValueKind() != VK_RValue &&
John McCalle3027922010-08-25 11:45:40 +00002629 ICE->getCastKind() == CK_NoOp)
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002630 E = ICE->getSubExpr()->IgnoreParens();
2631 else
2632 break;
2633 }
Alexis Hunta8136cc2010-05-05 15:23:54 +00002634
Anders Carlsson8abde4b2010-01-31 17:18:49 +00002635 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2636 return ASE->getBase()->getType()->isVectorType();
2637
2638 if (isa<ExtVectorElementExpr>(E))
2639 return true;
2640
2641 return false;
2642}
2643
Chris Lattnerb8211f62009-02-16 22:14:05 +00002644/// isArrow - Return true if the base expression is a pointer to vector,
2645/// return false if the base expression is a vector.
2646bool ExtVectorElementExpr::isArrow() const {
2647 return getBase()->getType()->isPointerType();
2648}
2649
Nate Begemance4d7fc2008-04-18 23:10:10 +00002650unsigned ExtVectorElementExpr::getNumElements() const {
John McCall9dd450b2009-09-21 23:43:11 +00002651 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begemanf322eab2008-05-09 06:41:27 +00002652 return VT->getNumElements();
2653 return 1;
Chris Lattner177bd452007-08-03 16:00:20 +00002654}
2655
Nate Begemanf322eab2008-05-09 06:41:27 +00002656/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begemance4d7fc2008-04-18 23:10:10 +00002657bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbarcb2a0562009-10-18 02:09:09 +00002658 // FIXME: Refactor this code to an accessor on the AST node which returns the
2659 // "type" of component access, and share with code below and in Sema.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002660 StringRef Comp = Accessor->getName();
Nate Begeman7e5185b2009-01-18 02:01:21 +00002661
2662 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002663 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman7e5185b2009-01-18 02:01:21 +00002664 return false;
Mike Stump11289f42009-09-09 15:08:12 +00002665
Nate Begeman7e5185b2009-01-18 02:01:21 +00002666 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002667 if (Comp[0] == 's' || Comp[0] == 'S')
2668 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002669
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002670 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002671 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Naroff0d595ca2007-07-30 03:29:09 +00002672 return true;
Daniel Dunbar125c9c92009-10-17 23:53:04 +00002673
Steve Naroff0d595ca2007-07-30 03:29:09 +00002674 return false;
2675}
Chris Lattner885b4952007-08-02 23:36:59 +00002676
Nate Begemanf322eab2008-05-09 06:41:27 +00002677/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begemand3862152008-05-13 21:03:02 +00002678void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002679 SmallVectorImpl<unsigned> &Elts) const {
2680 StringRef Comp = Accessor->getName();
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002681 if (Comp[0] == 's' || Comp[0] == 'S')
2682 Comp = Comp.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00002683
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002684 bool isHi = Comp == "hi";
2685 bool isLo = Comp == "lo";
2686 bool isEven = Comp == "even";
2687 bool isOdd = Comp == "odd";
Mike Stump11289f42009-09-09 15:08:12 +00002688
Nate Begemanf322eab2008-05-09 06:41:27 +00002689 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2690 uint64_t Index;
Mike Stump11289f42009-09-09 15:08:12 +00002691
Nate Begemanf322eab2008-05-09 06:41:27 +00002692 if (isHi)
2693 Index = e + i;
2694 else if (isLo)
2695 Index = i;
2696 else if (isEven)
2697 Index = 2 * i;
2698 else if (isOdd)
2699 Index = 2 * i + 1;
2700 else
Daniel Dunbarce5a0b32009-10-18 02:09:31 +00002701 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattner885b4952007-08-02 23:36:59 +00002702
Nate Begemand3862152008-05-13 21:03:02 +00002703 Elts.push_back(Index);
Chris Lattner885b4952007-08-02 23:36:59 +00002704 }
Nate Begemanf322eab2008-05-09 06:41:27 +00002705}
2706
Douglas Gregor9a129192010-04-21 00:45:42 +00002707ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002708 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002709 SourceLocation LBracLoc,
2710 SourceLocation SuperLoc,
2711 bool IsInstanceSuper,
2712 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002713 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002714 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002715 ObjCMethodDecl *Method,
2716 Expr **Args, unsigned NumArgs,
2717 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002718 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002719 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor678d76c2011-07-01 01:22:09 +00002720 /*InstantiationDependent=*/false,
Douglas Gregora6e053e2010-12-15 01:34:56 +00002721 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002722 NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass),
John McCall31168b02011-06-15 23:02:42 +00002723 HasMethod(Method != 0), IsDelegateInitCall(false), SuperLoc(SuperLoc),
Douglas Gregor9a129192010-04-21 00:45:42 +00002724 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2725 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002726 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorde4827d2010-03-08 16:40:19 +00002727{
Douglas Gregor9a129192010-04-21 00:45:42 +00002728 setReceiverPointer(SuperType.getAsOpaquePtr());
2729 if (NumArgs)
2730 memcpy(getArgs(), Args, NumArgs * sizeof(Expr *));
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002731}
2732
Douglas Gregor9a129192010-04-21 00:45:42 +00002733ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002734 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002735 SourceLocation LBracLoc,
2736 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002737 Selector Sel,
2738 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002739 ObjCMethodDecl *Method,
2740 Expr **Args, unsigned NumArgs,
2741 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002742 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002743 T->isDependentType(), T->isInstantiationDependentType(),
2744 T->containsUnexpandedParameterPack()),
John McCall31168b02011-06-15 23:02:42 +00002745 NumArgs(NumArgs), Kind(Class),
2746 HasMethod(Method != 0), IsDelegateInitCall(false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002747 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2748 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002749 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002750{
2751 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002752 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002753 for (unsigned I = 0; I != NumArgs; ++I) {
2754 if (Args[I]->isTypeDependent())
2755 ExprBits.TypeDependent = true;
2756 if (Args[I]->isValueDependent())
2757 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00002758 if (Args[I]->isInstantiationDependent())
2759 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00002760 if (Args[I]->containsUnexpandedParameterPack())
2761 ExprBits.ContainsUnexpandedParameterPack = true;
2762
2763 MyArgs[I] = Args[I];
2764 }
Ted Kremeneka3a37ae2008-06-24 15:50:53 +00002765}
2766
Douglas Gregor9a129192010-04-21 00:45:42 +00002767ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002768 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002769 SourceLocation LBracLoc,
2770 Expr *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002771 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002772 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002773 ObjCMethodDecl *Method,
2774 Expr **Args, unsigned NumArgs,
2775 SourceLocation RBracLoc)
John McCall7decc9e2010-11-18 06:31:45 +00002776 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002777 Receiver->isTypeDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002778 Receiver->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002779 Receiver->containsUnexpandedParameterPack()),
John McCall31168b02011-06-15 23:02:42 +00002780 NumArgs(NumArgs), Kind(Instance),
2781 HasMethod(Method != 0), IsDelegateInitCall(false),
Douglas Gregor9a129192010-04-21 00:45:42 +00002782 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2783 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002784 SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor9a129192010-04-21 00:45:42 +00002785{
2786 setReceiverPointer(Receiver);
Douglas Gregora3efea12011-01-03 19:04:46 +00002787 Expr **MyArgs = getArgs();
Douglas Gregora6e053e2010-12-15 01:34:56 +00002788 for (unsigned I = 0; I != NumArgs; ++I) {
2789 if (Args[I]->isTypeDependent())
2790 ExprBits.TypeDependent = true;
2791 if (Args[I]->isValueDependent())
2792 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00002793 if (Args[I]->isInstantiationDependent())
2794 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00002795 if (Args[I]->containsUnexpandedParameterPack())
2796 ExprBits.ContainsUnexpandedParameterPack = true;
2797
2798 MyArgs[I] = Args[I];
2799 }
Chris Lattner7ec71da2009-04-26 00:44:05 +00002800}
2801
Douglas Gregor9a129192010-04-21 00:45:42 +00002802ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002803 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002804 SourceLocation LBracLoc,
2805 SourceLocation SuperLoc,
2806 bool IsInstanceSuper,
2807 QualType SuperType,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002808 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002809 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002810 ObjCMethodDecl *Method,
2811 Expr **Args, unsigned NumArgs,
2812 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002813 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002814 NumArgs * sizeof(Expr *);
2815 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
John McCall7decc9e2010-11-18 06:31:45 +00002816 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002817 SuperType, Sel, SelLoc, Method, Args,NumArgs,
Douglas Gregor9a129192010-04-21 00:45:42 +00002818 RBracLoc);
2819}
2820
2821ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002822 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002823 SourceLocation LBracLoc,
2824 TypeSourceInfo *Receiver,
Alexis Hunta8136cc2010-05-05 15:23:54 +00002825 Selector Sel,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002826 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002827 ObjCMethodDecl *Method,
2828 Expr **Args, unsigned NumArgs,
2829 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002830 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002831 NumArgs * sizeof(Expr *);
2832 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002833 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2834 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002835}
2836
2837ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +00002838 ExprValueKind VK,
Douglas Gregor9a129192010-04-21 00:45:42 +00002839 SourceLocation LBracLoc,
2840 Expr *Receiver,
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002841 Selector Sel,
2842 SourceLocation SelLoc,
Douglas Gregor9a129192010-04-21 00:45:42 +00002843 ObjCMethodDecl *Method,
2844 Expr **Args, unsigned NumArgs,
2845 SourceLocation RBracLoc) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002846 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002847 NumArgs * sizeof(Expr *);
2848 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
Argyrios Kyrtzidisd0039e52010-12-10 20:08:27 +00002849 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc,
2850 Method, Args, NumArgs, RBracLoc);
Douglas Gregor9a129192010-04-21 00:45:42 +00002851}
2852
Alexis Hunta8136cc2010-05-05 15:23:54 +00002853ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Douglas Gregor9a129192010-04-21 00:45:42 +00002854 unsigned NumArgs) {
Alexis Hunta8136cc2010-05-05 15:23:54 +00002855 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
Douglas Gregor9a129192010-04-21 00:45:42 +00002856 NumArgs * sizeof(Expr *);
2857 void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment);
2858 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
2859}
Argyrios Kyrtzidis4d754a52010-12-10 20:08:30 +00002860
2861SourceRange ObjCMessageExpr::getReceiverRange() const {
2862 switch (getReceiverKind()) {
2863 case Instance:
2864 return getInstanceReceiver()->getSourceRange();
2865
2866 case Class:
2867 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
2868
2869 case SuperInstance:
2870 case SuperClass:
2871 return getSuperLoc();
2872 }
2873
2874 return SourceLocation();
2875}
2876
Douglas Gregor9a129192010-04-21 00:45:42 +00002877Selector ObjCMessageExpr::getSelector() const {
2878 if (HasMethod)
2879 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
2880 ->getSelector();
Alexis Hunta8136cc2010-05-05 15:23:54 +00002881 return Selector(SelectorOrMethod);
Douglas Gregor9a129192010-04-21 00:45:42 +00002882}
2883
2884ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
2885 switch (getReceiverKind()) {
2886 case Instance:
2887 if (const ObjCObjectPointerType *Ptr
2888 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
2889 return Ptr->getInterfaceDecl();
2890 break;
2891
2892 case Class:
John McCall8b07ec22010-05-15 11:32:37 +00002893 if (const ObjCObjectType *Ty
2894 = getClassReceiver()->getAs<ObjCObjectType>())
2895 return Ty->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002896 break;
2897
2898 case SuperInstance:
2899 if (const ObjCObjectPointerType *Ptr
2900 = getSuperType()->getAs<ObjCObjectPointerType>())
2901 return Ptr->getInterfaceDecl();
2902 break;
2903
2904 case SuperClass:
Argyrios Kyrtzidis1b9747f2011-01-25 00:03:48 +00002905 if (const ObjCObjectType *Iface
2906 = getSuperType()->getAs<ObjCObjectType>())
2907 return Iface->getInterface();
Douglas Gregor9a129192010-04-21 00:45:42 +00002908 break;
2909 }
2910
2911 return 0;
Ted Kremenek2c809302010-02-11 22:41:21 +00002912}
Chris Lattner7ec71da2009-04-26 00:44:05 +00002913
Chris Lattner0e62c1c2011-07-23 10:55:15 +00002914StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCall31168b02011-06-15 23:02:42 +00002915 switch (getBridgeKind()) {
2916 case OBC_Bridge:
2917 return "__bridge";
2918 case OBC_BridgeTransfer:
2919 return "__bridge_transfer";
2920 case OBC_BridgeRetained:
2921 return "__bridge_retained";
2922 }
2923
2924 return "__bridge";
2925}
2926
Jay Foad39c79802011-01-12 09:06:06 +00002927bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Eli Friedman1c4a1752009-04-26 19:19:15 +00002928 return getCond()->EvaluateAsInt(C) != 0;
Chris Lattner35e564e2007-10-25 00:29:32 +00002929}
2930
Douglas Gregora6e053e2010-12-15 01:34:56 +00002931ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
2932 QualType Type, SourceLocation BLoc,
2933 SourceLocation RP)
2934 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
2935 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002936 Type->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00002937 Type->containsUnexpandedParameterPack()),
2938 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
2939{
2940 SubExprs = new (C) Stmt*[nexpr];
2941 for (unsigned i = 0; i < nexpr; i++) {
2942 if (args[i]->isTypeDependent())
2943 ExprBits.TypeDependent = true;
2944 if (args[i]->isValueDependent())
2945 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00002946 if (args[i]->isInstantiationDependent())
2947 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00002948 if (args[i]->containsUnexpandedParameterPack())
2949 ExprBits.ContainsUnexpandedParameterPack = true;
2950
2951 SubExprs[i] = args[i];
2952 }
2953}
2954
Nate Begeman48745922009-08-12 02:28:50 +00002955void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
2956 unsigned NumExprs) {
2957 if (SubExprs) C.Deallocate(SubExprs);
2958
2959 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregora3c55902009-04-16 00:01:45 +00002960 this->NumExprs = NumExprs;
2961 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump11289f42009-09-09 15:08:12 +00002962}
Nate Begeman48745922009-08-12 02:28:50 +00002963
Peter Collingbourne91147592011-04-15 00:35:48 +00002964GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2965 SourceLocation GenericLoc, Expr *ControllingExpr,
2966 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2967 unsigned NumAssocs, SourceLocation DefaultLoc,
2968 SourceLocation RParenLoc,
2969 bool ContainsUnexpandedParameterPack,
2970 unsigned ResultIndex)
2971 : Expr(GenericSelectionExprClass,
2972 AssocExprs[ResultIndex]->getType(),
2973 AssocExprs[ResultIndex]->getValueKind(),
2974 AssocExprs[ResultIndex]->getObjectKind(),
2975 AssocExprs[ResultIndex]->isTypeDependent(),
2976 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00002977 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbourne91147592011-04-15 00:35:48 +00002978 ContainsUnexpandedParameterPack),
2979 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
2980 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
2981 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
2982 RParenLoc(RParenLoc) {
2983 SubExprs[CONTROLLING] = ControllingExpr;
2984 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
2985 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
2986}
2987
2988GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
2989 SourceLocation GenericLoc, Expr *ControllingExpr,
2990 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
2991 unsigned NumAssocs, SourceLocation DefaultLoc,
2992 SourceLocation RParenLoc,
2993 bool ContainsUnexpandedParameterPack)
2994 : Expr(GenericSelectionExprClass,
2995 Context.DependentTy,
2996 VK_RValue,
2997 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00002998 /*isTypeDependent=*/true,
2999 /*isValueDependent=*/true,
3000 /*isInstantiationDependent=*/true,
Peter Collingbourne91147592011-04-15 00:35:48 +00003001 ContainsUnexpandedParameterPack),
3002 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3003 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3004 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3005 RParenLoc(RParenLoc) {
3006 SubExprs[CONTROLLING] = ControllingExpr;
3007 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3008 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3009}
3010
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003011//===----------------------------------------------------------------------===//
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003012// DesignatedInitExpr
3013//===----------------------------------------------------------------------===//
3014
Chandler Carruth631abd92011-06-16 06:47:06 +00003015IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003016 assert(Kind == FieldDesignator && "Only valid on a field designator");
3017 if (Field.NameOrField & 0x01)
3018 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3019 else
3020 return getField()->getIdentifier();
3021}
3022
Alexis Hunta8136cc2010-05-05 15:23:54 +00003023DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003024 unsigned NumDesignators,
Douglas Gregord5846a12009-04-15 06:41:24 +00003025 const Designator *Designators,
Mike Stump11289f42009-09-09 15:08:12 +00003026 SourceLocation EqualOrColonLoc,
Douglas Gregord5846a12009-04-15 06:41:24 +00003027 bool GNUSyntax,
Mike Stump11289f42009-09-09 15:08:12 +00003028 Expr **IndexExprs,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003029 unsigned NumIndexExprs,
3030 Expr *Init)
Mike Stump11289f42009-09-09 15:08:12 +00003031 : Expr(DesignatedInitExprClass, Ty,
John McCall7decc9e2010-11-18 06:31:45 +00003032 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003033 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00003034 Init->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003035 Init->containsUnexpandedParameterPack()),
Mike Stump11289f42009-09-09 15:08:12 +00003036 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3037 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003038 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003039
3040 // Record the initializer itself.
John McCall8322c3a2011-02-13 04:07:26 +00003041 child_range Child = children();
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003042 *Child++ = Init;
3043
3044 // Copy the designators and their subexpressions, computing
3045 // value-dependence along the way.
3046 unsigned IndexIdx = 0;
3047 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregord5846a12009-04-15 06:41:24 +00003048 this->Designators[I] = Designators[I];
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003049
3050 if (this->Designators[I].isArrayDesignator()) {
3051 // Compute type- and value-dependence.
3052 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003053 if (Index->isTypeDependent() || Index->isValueDependent())
3054 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003055 if (Index->isInstantiationDependent())
3056 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003057 // Propagate unexpanded parameter packs.
3058 if (Index->containsUnexpandedParameterPack())
3059 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003060
3061 // Copy the index expressions into permanent storage.
3062 *Child++ = IndexExprs[IndexIdx++];
3063 } else if (this->Designators[I].isArrayRangeDesignator()) {
3064 // Compute type- and value-dependence.
3065 Expr *Start = IndexExprs[IndexIdx];
3066 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003067 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor678d76c2011-07-01 01:22:09 +00003068 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00003069 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003070 ExprBits.InstantiationDependent = true;
3071 } else if (Start->isInstantiationDependent() ||
3072 End->isInstantiationDependent()) {
3073 ExprBits.InstantiationDependent = true;
3074 }
3075
Douglas Gregora6e053e2010-12-15 01:34:56 +00003076 // Propagate unexpanded parameter packs.
3077 if (Start->containsUnexpandedParameterPack() ||
3078 End->containsUnexpandedParameterPack())
3079 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003080
3081 // Copy the start/end expressions into permanent storage.
3082 *Child++ = IndexExprs[IndexIdx++];
3083 *Child++ = IndexExprs[IndexIdx++];
3084 }
3085 }
3086
3087 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregord5846a12009-04-15 06:41:24 +00003088}
3089
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003090DesignatedInitExpr *
Mike Stump11289f42009-09-09 15:08:12 +00003091DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003092 unsigned NumDesignators,
3093 Expr **IndexExprs, unsigned NumIndexExprs,
3094 SourceLocation ColonOrEqualLoc,
3095 bool UsesColonSyntax, Expr *Init) {
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003096 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroff99c0cdf2009-01-27 23:20:32 +00003097 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003098 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregorca1aeec2009-05-21 23:17:49 +00003099 ColonOrEqualLoc, UsesColonSyntax,
3100 IndexExprs, NumIndexExprs, Init);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003101}
3102
Mike Stump11289f42009-09-09 15:08:12 +00003103DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregor38676d52009-04-16 00:55:48 +00003104 unsigned NumIndexExprs) {
3105 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3106 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3107 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3108}
3109
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003110void DesignatedInitExpr::setDesignators(ASTContext &C,
3111 const Designator *Desigs,
Douglas Gregor38676d52009-04-16 00:55:48 +00003112 unsigned NumDesigs) {
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003113 Designators = new (C) Designator[NumDesigs];
Douglas Gregor38676d52009-04-16 00:55:48 +00003114 NumDesignators = NumDesigs;
3115 for (unsigned I = 0; I != NumDesigs; ++I)
3116 Designators[I] = Desigs[I];
3117}
3118
Abramo Bagnara22f8cd72011-03-16 15:08:46 +00003119SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3120 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3121 if (size() == 1)
3122 return DIE->getDesignator(0)->getSourceRange();
3123 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3124 DIE->getDesignator(size()-1)->getEndLocation());
3125}
3126
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003127SourceRange DesignatedInitExpr::getSourceRange() const {
3128 SourceLocation StartLoc;
Chris Lattner8ba22472009-02-16 22:33:34 +00003129 Designator &First =
3130 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003131 if (First.isFieldDesignator()) {
Douglas Gregor5c7c9cb2009-03-28 00:41:23 +00003132 if (GNUSyntax)
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003133 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3134 else
3135 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3136 } else
Chris Lattner8ba22472009-02-16 22:33:34 +00003137 StartLoc =
3138 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003139 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3140}
3141
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003142Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3143 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3144 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3145 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003146 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3147 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3148}
3149
3150Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00003151 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003152 "Requires array range designator");
3153 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3154 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003155 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3156 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3157}
3158
3159Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump11289f42009-09-09 15:08:12 +00003160 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003161 "Requires array range designator");
3162 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3163 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003164 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3165 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3166}
3167
Douglas Gregord5846a12009-04-15 06:41:24 +00003168/// \brief Replaces the designator at index @p Idx with the series
3169/// of designators in [First, Last).
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003170void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump11289f42009-09-09 15:08:12 +00003171 const Designator *First,
Douglas Gregord5846a12009-04-15 06:41:24 +00003172 const Designator *Last) {
3173 unsigned NumNewDesignators = Last - First;
3174 if (NumNewDesignators == 0) {
3175 std::copy_backward(Designators + Idx + 1,
3176 Designators + NumDesignators,
3177 Designators + Idx);
3178 --NumNewDesignators;
3179 return;
3180 } else if (NumNewDesignators == 1) {
3181 Designators[Idx] = *First;
3182 return;
3183 }
3184
Mike Stump11289f42009-09-09 15:08:12 +00003185 Designator *NewDesignators
Douglas Gregor03e8bdc2010-01-06 23:17:19 +00003186 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregord5846a12009-04-15 06:41:24 +00003187 std::copy(Designators, Designators + Idx, NewDesignators);
3188 std::copy(First, Last, NewDesignators + Idx);
3189 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3190 NewDesignators + Idx + NumNewDesignators);
Douglas Gregord5846a12009-04-15 06:41:24 +00003191 Designators = NewDesignators;
3192 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3193}
3194
Mike Stump11289f42009-09-09 15:08:12 +00003195ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman5ec4b312009-08-10 23:49:36 +00003196 Expr **exprs, unsigned nexprs,
Manuel Klimekf2b4b692011-06-22 20:02:16 +00003197 SourceLocation rparenloc, QualType T)
3198 : Expr(ParenListExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00003199 false, false, false, false),
Douglas Gregora6e053e2010-12-15 01:34:56 +00003200 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Manuel Klimekf2b4b692011-06-22 20:02:16 +00003201 assert(!T.isNull() && "ParenListExpr must have a valid type");
Nate Begeman5ec4b312009-08-10 23:49:36 +00003202 Exprs = new (C) Stmt*[nexprs];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003203 for (unsigned i = 0; i != nexprs; ++i) {
3204 if (exprs[i]->isTypeDependent())
3205 ExprBits.TypeDependent = true;
3206 if (exprs[i]->isValueDependent())
3207 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003208 if (exprs[i]->isInstantiationDependent())
3209 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00003210 if (exprs[i]->containsUnexpandedParameterPack())
3211 ExprBits.ContainsUnexpandedParameterPack = true;
3212
Nate Begeman5ec4b312009-08-10 23:49:36 +00003213 Exprs[i] = exprs[i];
Douglas Gregora6e053e2010-12-15 01:34:56 +00003214 }
Nate Begeman5ec4b312009-08-10 23:49:36 +00003215}
3216
John McCall1bf58462011-02-16 08:02:54 +00003217const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3218 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3219 e = ewc->getSubExpr();
Douglas Gregorfe314812011-06-21 17:03:29 +00003220 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3221 e = m->GetTemporaryExpr();
John McCall1bf58462011-02-16 08:02:54 +00003222 e = cast<CXXConstructExpr>(e)->getArg(0);
3223 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3224 e = ice->getSubExpr();
3225 return cast<OpaqueValueExpr>(e);
3226}
3227
Douglas Gregore4a0bb72009-01-22 00:58:24 +00003228//===----------------------------------------------------------------------===//
Ted Kremenek5778acf2008-10-27 18:40:21 +00003229// ExprIterator.
3230//===----------------------------------------------------------------------===//
3231
3232Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3233Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3234Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3235const Expr* ConstExprIterator::operator[](size_t idx) const {
3236 return cast<Expr>(I[idx]);
3237}
3238const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3239const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3240
3241//===----------------------------------------------------------------------===//
Ted Kremenek85e92ec2007-08-24 18:13:47 +00003242// Child Iterators for iterating over subexpressions/substatements
3243//===----------------------------------------------------------------------===//
3244
Peter Collingbournee190dee2011-03-11 19:24:49 +00003245// UnaryExprOrTypeTraitExpr
3246Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl6f282892008-11-11 17:56:53 +00003247 // If this is of a type and the type is a VLA type (and not a typedef), the
3248 // size expression of the VLA needs to be treated as an executable expression.
3249 // Why isn't this weirdness documented better in StmtIterator?
3250 if (isArgumentType()) {
John McCall424cec92011-01-19 06:33:43 +00003251 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl6f282892008-11-11 17:56:53 +00003252 getArgumentType().getTypePtr()))
John McCallbd066782011-02-09 08:16:59 +00003253 return child_range(child_iterator(T), child_iterator());
3254 return child_range();
Sebastian Redl6f282892008-11-11 17:56:53 +00003255 }
John McCallbd066782011-02-09 08:16:59 +00003256 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek04746ce2007-10-18 23:28:49 +00003257}
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00003258
Steve Naroffd54978b2007-09-18 23:55:05 +00003259// ObjCMessageExpr
John McCallbd066782011-02-09 08:16:59 +00003260Stmt::child_range ObjCMessageExpr::children() {
3261 Stmt **begin;
Douglas Gregor9a129192010-04-21 00:45:42 +00003262 if (getReceiverKind() == Instance)
John McCallbd066782011-02-09 08:16:59 +00003263 begin = reinterpret_cast<Stmt **>(this + 1);
3264 else
3265 begin = reinterpret_cast<Stmt **>(getArgs());
3266 return child_range(begin,
3267 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroffd54978b2007-09-18 23:55:05 +00003268}
3269
Steve Naroffc540d662008-09-03 18:15:37 +00003270// Blocks
John McCall351762c2011-02-07 10:33:21 +00003271BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK,
Douglas Gregor476e3022011-01-19 21:32:01 +00003272 SourceLocation l, bool ByRef,
John McCall351762c2011-02-07 10:33:21 +00003273 bool constAdded)
Douglas Gregor678d76c2011-07-01 01:22:09 +00003274 : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, false,
Douglas Gregor476e3022011-01-19 21:32:01 +00003275 d->isParameterPack()),
John McCall351762c2011-02-07 10:33:21 +00003276 D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded)
Douglas Gregor476e3022011-01-19 21:32:01 +00003277{
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003278 bool TypeDependent = false;
3279 bool ValueDependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003280 bool InstantiationDependent = false;
3281 computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent,
3282 InstantiationDependent);
Douglas Gregorf144f4f2011-01-19 21:52:31 +00003283 ExprBits.TypeDependent = TypeDependent;
3284 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor678d76c2011-07-01 01:22:09 +00003285 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregor476e3022011-01-19 21:32:01 +00003286}