blob: 3de4b5771a252164eb696bceffd0e2b43c53320e [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Expr.cpp - Expression AST Node Implementation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Expr class and subclasses.
11//
12//===----------------------------------------------------------------------===//
13
Daniel Dunbarc4a1dea2008-08-11 05:35:13 +000014#include "clang/AST/Expr.h"
Douglas Gregor0979c802009-08-31 21:41:48 +000015#include "clang/AST/ExprCXX.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000016#include "clang/AST/APValue.h"
Chris Lattner2eadfb62007-07-15 23:32:58 +000017#include "clang/AST/ASTContext.h"
Chris Lattnera4d55d82008-10-06 06:40:35 +000018#include "clang/AST/DeclObjC.h"
Douglas Gregor98cd5992008-10-21 23:43:52 +000019#include "clang/AST/DeclCXX.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000020#include "clang/AST/DeclTemplate.h"
Douglas Gregor25d0a0f2012-02-23 07:33:15 +000021#include "clang/AST/EvaluatedExprVisitor.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/AST/StmtVisitor.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000024#include "clang/Lex/LiteralSupport.h"
25#include "clang/Lex/Lexer.h"
Richard Smith7a614d82011-06-11 17:19:42 +000026#include "clang/Sema/SemaDiagnostic.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000027#include "clang/Basic/Builtins.h"
Chris Lattner08f92e32010-11-17 07:37:15 +000028#include "clang/Basic/SourceManager.h"
Chris Lattnerda5a6b62007-11-27 18:22:04 +000029#include "clang/Basic/TargetInfo.h"
Douglas Gregorcf3293e2009-11-01 20:32:48 +000030#include "llvm/Support/ErrorHandling.h"
Anders Carlsson3a082d82009-09-08 18:24:21 +000031#include "llvm/Support/raw_ostream.h"
Douglas Gregorffb4b6e2009-04-15 06:41:24 +000032#include <algorithm>
Eli Friedman64f45a22011-11-01 02:23:42 +000033#include <cstring>
Reid Spencer5f016e22007-07-11 17:01:13 +000034using namespace clang;
35
Chris Lattner2b334bb2010-04-16 23:34:13 +000036/// isKnownToHaveBooleanValue - Return true if this is an integer expression
37/// that is known to return 0 or 1. This happens for _Bool/bool expressions
38/// but also int expressions which are produced by things like comparisons in
39/// C.
40bool Expr::isKnownToHaveBooleanValue() const {
Peter Collingbournef111d932011-04-15 00:35:48 +000041 const Expr *E = IgnoreParens();
42
Chris Lattner2b334bb2010-04-16 23:34:13 +000043 // If this value has _Bool type, it is obvious 0/1.
Peter Collingbournef111d932011-04-15 00:35:48 +000044 if (E->getType()->isBooleanType()) return true;
Sean Huntc3021132010-05-05 15:23:54 +000045 // If this is a non-scalar-integer type, we don't care enough to try.
Peter Collingbournef111d932011-04-15 00:35:48 +000046 if (!E->getType()->isIntegralOrEnumerationType()) return false;
Sean Huntc3021132010-05-05 15:23:54 +000047
Peter Collingbournef111d932011-04-15 00:35:48 +000048 if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000049 switch (UO->getOpcode()) {
John McCall2de56d12010-08-25 11:45:40 +000050 case UO_Plus:
Chris Lattner2b334bb2010-04-16 23:34:13 +000051 return UO->getSubExpr()->isKnownToHaveBooleanValue();
52 default:
53 return false;
54 }
55 }
Sean Huntc3021132010-05-05 15:23:54 +000056
John McCall6907fbe2010-06-12 01:56:02 +000057 // Only look through implicit casts. If the user writes
58 // '(int) (a && b)' treat it as an arbitrary int.
Peter Collingbournef111d932011-04-15 00:35:48 +000059 if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000060 return CE->getSubExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000061
Peter Collingbournef111d932011-04-15 00:35:48 +000062 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
Chris Lattner2b334bb2010-04-16 23:34:13 +000063 switch (BO->getOpcode()) {
64 default: return false;
John McCall2de56d12010-08-25 11:45:40 +000065 case BO_LT: // Relational operators.
66 case BO_GT:
67 case BO_LE:
68 case BO_GE:
69 case BO_EQ: // Equality operators.
70 case BO_NE:
71 case BO_LAnd: // AND operator.
72 case BO_LOr: // Logical OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000073 return true;
Sean Huntc3021132010-05-05 15:23:54 +000074
John McCall2de56d12010-08-25 11:45:40 +000075 case BO_And: // Bitwise AND operator.
76 case BO_Xor: // Bitwise XOR operator.
77 case BO_Or: // Bitwise OR operator.
Chris Lattner2b334bb2010-04-16 23:34:13 +000078 // Handle things like (x==2)|(y==12).
79 return BO->getLHS()->isKnownToHaveBooleanValue() &&
80 BO->getRHS()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000081
John McCall2de56d12010-08-25 11:45:40 +000082 case BO_Comma:
83 case BO_Assign:
Chris Lattner2b334bb2010-04-16 23:34:13 +000084 return BO->getRHS()->isKnownToHaveBooleanValue();
85 }
86 }
Sean Huntc3021132010-05-05 15:23:54 +000087
Peter Collingbournef111d932011-04-15 00:35:48 +000088 if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E))
Chris Lattner2b334bb2010-04-16 23:34:13 +000089 return CO->getTrueExpr()->isKnownToHaveBooleanValue() &&
90 CO->getFalseExpr()->isKnownToHaveBooleanValue();
Sean Huntc3021132010-05-05 15:23:54 +000091
Chris Lattner2b334bb2010-04-16 23:34:13 +000092 return false;
93}
94
John McCall63c00d72011-02-09 08:16:59 +000095// Amusing macro metaprogramming hack: check whether a class provides
96// a more specific implementation of getExprLoc().
Daniel Dunbar90e25a82012-03-09 15:39:19 +000097//
98// See also Stmt.cpp:{getLocStart(),getLocEnd()}.
John McCall63c00d72011-02-09 08:16:59 +000099namespace {
100 /// This implementation is used when a class provides a custom
101 /// implementation of getExprLoc.
102 template <class E, class T>
103 SourceLocation getExprLocImpl(const Expr *expr,
104 SourceLocation (T::*v)() const) {
105 return static_cast<const E*>(expr)->getExprLoc();
106 }
107
108 /// This implementation is used when a class doesn't provide
109 /// a custom implementation of getExprLoc. Overload resolution
110 /// should pick it over the implementation above because it's
111 /// more specialized according to function template partial ordering.
112 template <class E>
113 SourceLocation getExprLocImpl(const Expr *expr,
114 SourceLocation (Expr::*v)() const) {
Daniel Dunbar90e25a82012-03-09 15:39:19 +0000115 return static_cast<const E*>(expr)->getLocStart();
John McCall63c00d72011-02-09 08:16:59 +0000116 }
117}
118
119SourceLocation Expr::getExprLoc() const {
120 switch (getStmtClass()) {
121 case Stmt::NoStmtClass: llvm_unreachable("statement without class");
122#define ABSTRACT_STMT(type)
123#define STMT(type, base) \
124 case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break;
125#define EXPR(type, base) \
126 case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc);
127#include "clang/AST/StmtNodes.inc"
128 }
129 llvm_unreachable("unknown statement kind");
John McCall63c00d72011-02-09 08:16:59 +0000130}
131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132//===----------------------------------------------------------------------===//
133// Primary Expressions.
134//===----------------------------------------------------------------------===//
135
Douglas Gregor561f8122011-07-01 01:22:09 +0000136/// \brief Compute the type-, value-, and instantiation-dependence of a
137/// declaration reference
Douglas Gregord967e312011-01-19 21:52:31 +0000138/// based on the declaration being referenced.
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000139static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T,
Douglas Gregord967e312011-01-19 21:52:31 +0000140 bool &TypeDependent,
Douglas Gregor561f8122011-07-01 01:22:09 +0000141 bool &ValueDependent,
142 bool &InstantiationDependent) {
Douglas Gregord967e312011-01-19 21:52:31 +0000143 TypeDependent = false;
144 ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000145 InstantiationDependent = false;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000146
147 // (TD) C++ [temp.dep.expr]p3:
148 // An id-expression is type-dependent if it contains:
149 //
Sean Huntc3021132010-05-05 15:23:54 +0000150 // and
Douglas Gregor0da76df2009-11-23 11:41:28 +0000151 //
152 // (VD) C++ [temp.dep.constexpr]p2:
153 // An identifier is value-dependent if it is:
Douglas Gregord967e312011-01-19 21:52:31 +0000154
Douglas Gregor0da76df2009-11-23 11:41:28 +0000155 // (TD) - an identifier that was declared with dependent type
156 // (VD) - a name declared with a dependent type,
Douglas Gregord967e312011-01-19 21:52:31 +0000157 if (T->isDependentType()) {
158 TypeDependent = true;
159 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000160 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000161 return;
Douglas Gregor561f8122011-07-01 01:22:09 +0000162 } else if (T->isInstantiationDependentType()) {
163 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000164 }
Douglas Gregord967e312011-01-19 21:52:31 +0000165
Douglas Gregor0da76df2009-11-23 11:41:28 +0000166 // (TD) - a conversion-function-id that specifies a dependent type
Douglas Gregord967e312011-01-19 21:52:31 +0000167 if (D->getDeclName().getNameKind()
Douglas Gregor561f8122011-07-01 01:22:09 +0000168 == DeclarationName::CXXConversionFunctionName) {
169 QualType T = D->getDeclName().getCXXNameType();
170 if (T->isDependentType()) {
171 TypeDependent = true;
172 ValueDependent = true;
173 InstantiationDependent = true;
174 return;
175 }
176
177 if (T->isInstantiationDependentType())
178 InstantiationDependent = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000179 }
Douglas Gregor561f8122011-07-01 01:22:09 +0000180
Douglas Gregor0da76df2009-11-23 11:41:28 +0000181 // (VD) - the name of a non-type template parameter,
Douglas Gregord967e312011-01-19 21:52:31 +0000182 if (isa<NonTypeTemplateParmDecl>(D)) {
183 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000184 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000185 return;
186 }
187
Douglas Gregor0da76df2009-11-23 11:41:28 +0000188 // (VD) - a constant with integral or enumeration type and is
189 // initialized with an expression that is value-dependent.
Richard Smithdb1822c2011-11-08 01:31:09 +0000190 // (VD) - a constant with literal type and is initialized with an
191 // expression that is value-dependent [C++11].
192 // (VD) - FIXME: Missing from the standard:
193 // - an entity with reference type and is initialized with an
194 // expression that is value-dependent [C++11]
Douglas Gregord967e312011-01-19 21:52:31 +0000195 if (VarDecl *Var = dyn_cast<VarDecl>(D)) {
David Blaikie4e4d0842012-03-11 07:00:24 +0000196 if ((Ctx.getLangOpts().CPlusPlus0x ?
Richard Smithdb1822c2011-11-08 01:31:09 +0000197 Var->getType()->isLiteralType() :
198 Var->getType()->isIntegralOrEnumerationType()) &&
199 (Var->getType().getCVRQualifiers() == Qualifiers::Const ||
200 Var->getType()->isReferenceType())) {
Sebastian Redl31310a22010-02-01 20:16:42 +0000201 if (const Expr *Init = Var->getAnyInitializer())
Douglas Gregor561f8122011-07-01 01:22:09 +0000202 if (Init->isValueDependent()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000203 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000204 InstantiationDependent = true;
205 }
Richard Smithdb1822c2011-11-08 01:31:09 +0000206 }
207
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000208 // (VD) - FIXME: Missing from the standard:
209 // - a member function or a static data member of the current
210 // instantiation
Richard Smithdb1822c2011-11-08 01:31:09 +0000211 if (Var->isStaticDataMember() &&
212 Var->getDeclContext()->isDependentContext()) {
Douglas Gregord967e312011-01-19 21:52:31 +0000213 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000214 InstantiationDependent = true;
215 }
Douglas Gregord967e312011-01-19 21:52:31 +0000216
217 return;
218 }
219
Douglas Gregorbb6e73f2010-05-11 08:41:30 +0000220 // (VD) - FIXME: Missing from the standard:
221 // - a member function or a static data member of the current
222 // instantiation
Douglas Gregord967e312011-01-19 21:52:31 +0000223 if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) {
224 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000225 InstantiationDependent = true;
Richard Smithdb1822c2011-11-08 01:31:09 +0000226 }
Douglas Gregord967e312011-01-19 21:52:31 +0000227}
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000228
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000229void DeclRefExpr::computeDependence(ASTContext &Ctx) {
Douglas Gregord967e312011-01-19 21:52:31 +0000230 bool TypeDependent = false;
231 bool ValueDependent = false;
Douglas Gregor561f8122011-07-01 01:22:09 +0000232 bool InstantiationDependent = false;
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000233 computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent,
234 ValueDependent, InstantiationDependent);
Douglas Gregord967e312011-01-19 21:52:31 +0000235
236 // (TD) C++ [temp.dep.expr]p3:
237 // An id-expression is type-dependent if it contains:
238 //
239 // and
240 //
241 // (VD) C++ [temp.dep.constexpr]p2:
242 // An identifier is value-dependent if it is:
243 if (!TypeDependent && !ValueDependent &&
244 hasExplicitTemplateArgs() &&
245 TemplateSpecializationType::anyDependentTemplateArguments(
246 getTemplateArgs(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000247 getNumTemplateArgs(),
248 InstantiationDependent)) {
Douglas Gregord967e312011-01-19 21:52:31 +0000249 TypeDependent = true;
250 ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000251 InstantiationDependent = true;
Douglas Gregord967e312011-01-19 21:52:31 +0000252 }
253
254 ExprBits.TypeDependent = TypeDependent;
255 ExprBits.ValueDependent = ValueDependent;
Douglas Gregor561f8122011-07-01 01:22:09 +0000256 ExprBits.InstantiationDependent = InstantiationDependent;
Douglas Gregord967e312011-01-19 21:52:31 +0000257
Douglas Gregor10738d32010-12-23 23:51:58 +0000258 // Is the declaration a parameter pack?
Douglas Gregord967e312011-01-19 21:52:31 +0000259 if (getDecl()->isParameterPack())
Douglas Gregor1fe85ea2011-01-05 21:11:38 +0000260 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor0da76df2009-11-23 11:41:28 +0000261}
262
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000263DeclRefExpr::DeclRefExpr(ASTContext &Ctx,
264 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000265 SourceLocation TemplateKWLoc,
John McCallf4b88a42012-03-10 09:33:50 +0000266 ValueDecl *D, bool RefersToEnclosingLocal,
267 const DeclarationNameInfo &NameInfo,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000268 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000269 const TemplateArgumentListInfo *TemplateArgs,
John McCallf89e55a2010-11-18 06:31:45 +0000270 QualType T, ExprValueKind VK)
Douglas Gregor561f8122011-07-01 01:22:09 +0000271 : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false),
Chandler Carruthcb66cff2011-05-01 21:29:53 +0000272 D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) {
273 DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0;
Chandler Carruth7e740bd2011-05-01 21:55:21 +0000274 if (QualifierLoc)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000275 getInternalQualifierLoc() = QualifierLoc;
Chandler Carruth3aa81402011-05-01 23:48:14 +0000276 DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0;
277 if (FoundD)
278 getInternalFoundDecl() = FoundD;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000279 DeclRefExprBits.HasTemplateKWAndArgsInfo
280 = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0;
John McCallf4b88a42012-03-10 09:33:50 +0000281 DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal;
Douglas Gregor561f8122011-07-01 01:22:09 +0000282 if (TemplateArgs) {
283 bool Dependent = false;
284 bool InstantiationDependent = false;
285 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000286 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
287 Dependent,
288 InstantiationDependent,
289 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +0000290 if (InstantiationDependent)
291 setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000292 } else if (TemplateKWLoc.isValid()) {
293 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregor561f8122011-07-01 01:22:09 +0000294 }
Benjamin Kramerb8da98a2011-10-10 12:54:05 +0000295 DeclRefExprBits.HadMultipleCandidates = 0;
296
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000297 computeDependence(Ctx);
Abramo Bagnara25777432010-08-11 22:01:17 +0000298}
299
Douglas Gregora2813ce2009-10-23 18:54:35 +0000300DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000301 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000302 SourceLocation TemplateKWLoc,
John McCalldbd872f2009-12-08 09:08:17 +0000303 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000304 bool RefersToEnclosingLocal,
Douglas Gregora2813ce2009-10-23 18:54:35 +0000305 SourceLocation NameLoc,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000306 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000307 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000308 NamedDecl *FoundD,
Douglas Gregor0da76df2009-11-23 11:41:28 +0000309 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000310 return Create(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000311 RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000312 DeclarationNameInfo(D->getDeclName(), NameLoc),
Chandler Carruth3aa81402011-05-01 23:48:14 +0000313 T, VK, FoundD, TemplateArgs);
Abramo Bagnara25777432010-08-11 22:01:17 +0000314}
315
316DeclRefExpr *DeclRefExpr::Create(ASTContext &Context,
Douglas Gregor40d96a62011-02-28 21:54:11 +0000317 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000318 SourceLocation TemplateKWLoc,
Abramo Bagnara25777432010-08-11 22:01:17 +0000319 ValueDecl *D,
John McCallf4b88a42012-03-10 09:33:50 +0000320 bool RefersToEnclosingLocal,
Abramo Bagnara25777432010-08-11 22:01:17 +0000321 const DeclarationNameInfo &NameInfo,
322 QualType T,
John McCallf89e55a2010-11-18 06:31:45 +0000323 ExprValueKind VK,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000324 NamedDecl *FoundD,
Abramo Bagnara25777432010-08-11 22:01:17 +0000325 const TemplateArgumentListInfo *TemplateArgs) {
Chandler Carruth3aa81402011-05-01 23:48:14 +0000326 // Filter out cases where the found Decl is the same as the value refenenced.
327 if (D == FoundD)
328 FoundD = 0;
329
Douglas Gregora2813ce2009-10-23 18:54:35 +0000330 std::size_t Size = sizeof(DeclRefExpr);
Douglas Gregor40d96a62011-02-28 21:54:11 +0000331 if (QualifierLoc != 0)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000332 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000333 if (FoundD)
334 Size += sizeof(NamedDecl *);
John McCalld5532b62009-11-23 01:53:49 +0000335 if (TemplateArgs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000336 Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
337 else if (TemplateKWLoc.isValid())
338 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000339
Chris Lattner32488542010-10-30 05:14:06 +0000340 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000341 return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D,
John McCallf4b88a42012-03-10 09:33:50 +0000342 RefersToEnclosingLocal,
Daniel Dunbar3d13c5a2012-03-09 01:51:51 +0000343 NameInfo, FoundD, TemplateArgs, T, VK);
Douglas Gregora2813ce2009-10-23 18:54:35 +0000344}
345
Chandler Carruth3aa81402011-05-01 23:48:14 +0000346DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context,
Douglas Gregordef03542011-02-04 12:01:24 +0000347 bool HasQualifier,
Chandler Carruth3aa81402011-05-01 23:48:14 +0000348 bool HasFoundDecl,
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000349 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000350 unsigned NumTemplateArgs) {
351 std::size_t Size = sizeof(DeclRefExpr);
352 if (HasQualifier)
Chandler Carruth6857c3e2011-05-01 22:14:37 +0000353 Size += sizeof(NestedNameSpecifierLoc);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000354 if (HasFoundDecl)
355 Size += sizeof(NamedDecl *);
Abramo Bagnarae4b92762012-01-27 09:46:47 +0000356 if (HasTemplateKWAndArgsInfo)
357 Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chandler Carruth3aa81402011-05-01 23:48:14 +0000358
Chris Lattner32488542010-10-30 05:14:06 +0000359 void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>());
Argyrios Kyrtzidis663e3802010-07-08 13:09:47 +0000360 return new (Mem) DeclRefExpr(EmptyShell());
361}
362
Douglas Gregora2813ce2009-10-23 18:54:35 +0000363SourceRange DeclRefExpr::getSourceRange() const {
Abramo Bagnara25777432010-08-11 22:01:17 +0000364 SourceRange R = getNameInfo().getSourceRange();
Douglas Gregora2813ce2009-10-23 18:54:35 +0000365 if (hasQualifier())
Douglas Gregor40d96a62011-02-28 21:54:11 +0000366 R.setBegin(getQualifierLoc().getBeginLoc());
John McCall096832c2010-08-19 23:49:38 +0000367 if (hasExplicitTemplateArgs())
Douglas Gregora2813ce2009-10-23 18:54:35 +0000368 R.setEnd(getRAngleLoc());
369 return R;
370}
Daniel Dunbar396ec672012-03-09 15:39:15 +0000371SourceLocation DeclRefExpr::getLocStart() const {
372 if (hasQualifier())
373 return getQualifierLoc().getBeginLoc();
374 return getNameInfo().getLocStart();
375}
376SourceLocation DeclRefExpr::getLocEnd() const {
377 if (hasExplicitTemplateArgs())
378 return getRAngleLoc();
379 return getNameInfo().getLocEnd();
380}
Douglas Gregora2813ce2009-10-23 18:54:35 +0000381
Anders Carlsson3a082d82009-09-08 18:24:21 +0000382// FIXME: Maybe this should use DeclPrinter with a special "print predefined
383// expr" policy instead.
Anders Carlsson848fa642010-02-11 18:20:28 +0000384std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) {
385 ASTContext &Context = CurrentDecl->getASTContext();
386
Anders Carlsson3a082d82009-09-08 18:24:21 +0000387 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000388 if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000389 return FD->getNameAsString();
390
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000391 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000392 llvm::raw_svector_ostream Out(Name);
393
394 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
Anders Carlsson848fa642010-02-11 18:20:28 +0000395 if (MD->isVirtual() && IT != PrettyFunctionNoVirtual)
Anders Carlsson3a082d82009-09-08 18:24:21 +0000396 Out << "virtual ";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000397 if (MD->isStatic())
398 Out << "static ";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000399 }
400
David Blaikie4e4d0842012-03-11 07:00:24 +0000401 PrintingPolicy Policy(Context.getLangOpts());
Anders Carlsson3a082d82009-09-08 18:24:21 +0000402 std::string Proto = FD->getQualifiedNameAsString(Policy);
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000403 llvm::raw_string_ostream POut(Proto);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000404
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000405 const FunctionDecl *Decl = FD;
406 if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern())
407 Decl = Pattern;
408 const FunctionType *AFT = Decl->getType()->getAs<FunctionType>();
Anders Carlsson3a082d82009-09-08 18:24:21 +0000409 const FunctionProtoType *FT = 0;
410 if (FD->hasWrittenPrototype())
411 FT = dyn_cast<FunctionProtoType>(AFT);
412
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000413 POut << "(";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000414 if (FT) {
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000415 for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) {
Anders Carlsson3a082d82009-09-08 18:24:21 +0000416 if (i) POut << ", ";
Argyrios Kyrtzidis7ad5c992012-05-05 04:20:37 +0000417 POut << Decl->getParamDecl(i)->getType().stream(Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000418 }
419
420 if (FT->isVariadic()) {
421 if (FD->getNumParams()) POut << ", ";
422 POut << "...";
423 }
424 }
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000425 POut << ")";
Anders Carlsson3a082d82009-09-08 18:24:21 +0000426
Sam Weinig4eadcc52009-12-27 01:38:20 +0000427 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) {
428 Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers());
429 if (ThisQuals.hasConst())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000430 POut << " const";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000431 if (ThisQuals.hasVolatile())
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000432 POut << " volatile";
433 RefQualifierKind Ref = MD->getRefQualifier();
434 if (Ref == RQ_LValue)
435 POut << " &";
436 else if (Ref == RQ_RValue)
437 POut << " &&";
Sam Weinig4eadcc52009-12-27 01:38:20 +0000438 }
439
Douglas Gregorabf65ce2012-04-10 20:14:15 +0000440 typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy;
441 SpecsTy Specs;
442 const DeclContext *Ctx = FD->getDeclContext();
443 while (Ctx && isa<NamedDecl>(Ctx)) {
444 const ClassTemplateSpecializationDecl *Spec
445 = dyn_cast<ClassTemplateSpecializationDecl>(Ctx);
446 if (Spec && !Spec->isExplicitSpecialization())
447 Specs.push_back(Spec);
448 Ctx = Ctx->getParent();
449 }
450
451 std::string TemplateParams;
452 llvm::raw_string_ostream TOut(TemplateParams);
453 for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend();
454 I != E; ++I) {
455 const TemplateParameterList *Params
456 = (*I)->getSpecializedTemplate()->getTemplateParameters();
457 const TemplateArgumentList &Args = (*I)->getTemplateArgs();
458 assert(Params->size() == Args.size());
459 for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) {
460 StringRef Param = Params->getParam(i)->getName();
461 if (Param.empty()) continue;
462 TOut << Param << " = ";
463 Args.get(i).print(Policy, TOut);
464 TOut << ", ";
465 }
466 }
467
468 FunctionTemplateSpecializationInfo *FSI
469 = FD->getTemplateSpecializationInfo();
470 if (FSI && !FSI->isExplicitSpecialization()) {
471 const TemplateParameterList* Params
472 = FSI->getTemplate()->getTemplateParameters();
473 const TemplateArgumentList* Args = FSI->TemplateArguments;
474 assert(Params->size() == Args->size());
475 for (unsigned i = 0, e = Params->size(); i != e; ++i) {
476 StringRef Param = Params->getParam(i)->getName();
477 if (Param.empty()) continue;
478 TOut << Param << " = ";
479 Args->get(i).print(Policy, TOut);
480 TOut << ", ";
481 }
482 }
483
484 TOut.flush();
485 if (!TemplateParams.empty()) {
486 // remove the trailing comma and space
487 TemplateParams.resize(TemplateParams.size() - 2);
488 POut << " [" << TemplateParams << "]";
489 }
490
491 POut.flush();
492
Sam Weinig3a1ce1e2009-12-06 23:55:13 +0000493 if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD))
494 AFT->getResultType().getAsStringInternal(Proto, Policy);
Anders Carlsson3a082d82009-09-08 18:24:21 +0000495
496 Out << Proto;
497
498 Out.flush();
499 return Name.str().str();
500 }
501 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) {
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000502 SmallString<256> Name;
Anders Carlsson3a082d82009-09-08 18:24:21 +0000503 llvm::raw_svector_ostream Out(Name);
504 Out << (MD->isInstanceMethod() ? '-' : '+');
505 Out << '[';
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000506
507 // For incorrect code, there might not be an ObjCInterfaceDecl. Do
508 // a null check to avoid a crash.
509 if (const ObjCInterfaceDecl *ID = MD->getClassInterface())
Benjamin Kramerb8989f22011-10-14 18:45:37 +0000510 Out << *ID;
Ted Kremenekb03d33e2010-03-18 21:23:08 +0000511
Anders Carlsson3a082d82009-09-08 18:24:21 +0000512 if (const ObjCCategoryImplDecl *CID =
Benjamin Kramer900fc632010-04-17 09:33:03 +0000513 dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext()))
Benjamin Kramerf9780592012-02-07 11:57:45 +0000514 Out << '(' << *CID << ')';
Benjamin Kramer900fc632010-04-17 09:33:03 +0000515
Anders Carlsson3a082d82009-09-08 18:24:21 +0000516 Out << ' ';
517 Out << MD->getSelector().getAsString();
518 Out << ']';
519
520 Out.flush();
521 return Name.str().str();
522 }
523 if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) {
524 // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string.
525 return "top level";
526 }
527 return "";
528}
529
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000530void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) {
531 if (hasAllocation())
532 C.Deallocate(pVal);
533
534 BitWidth = Val.getBitWidth();
535 unsigned NumWords = Val.getNumWords();
536 const uint64_t* Words = Val.getRawData();
537 if (NumWords > 1) {
538 pVal = new (C) uint64_t[NumWords];
539 std::copy(Words, Words + NumWords, pVal);
540 } else if (NumWords == 1)
541 VAL = Words[0];
542 else
543 VAL = 0;
544}
545
546IntegerLiteral *
547IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V,
548 QualType type, SourceLocation l) {
549 return new (C) IntegerLiteral(C, V, type, l);
550}
551
552IntegerLiteral *
553IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) {
554 return new (C) IntegerLiteral(Empty);
555}
556
557FloatingLiteral *
558FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V,
559 bool isexact, QualType Type, SourceLocation L) {
560 return new (C) FloatingLiteral(C, V, isexact, Type, L);
561}
562
563FloatingLiteral *
564FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) {
Akira Hatanaka31dfd642012-01-10 22:40:09 +0000565 return new (C) FloatingLiteral(C, Empty);
Argyrios Kyrtzidis9996a7f2010-08-28 09:06:06 +0000566}
567
Chris Lattnerda8249e2008-06-07 22:13:43 +0000568/// getValueAsApproximateDouble - This returns the value as an inaccurate
569/// double. Note that this may cause loss of precision, but is useful for
570/// debugging dumps, etc.
571double FloatingLiteral::getValueAsApproximateDouble() const {
572 llvm::APFloat V = getValue();
Dale Johannesenee5a7002008-10-09 23:02:32 +0000573 bool ignored;
574 V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven,
575 &ignored);
Chris Lattnerda8249e2008-06-07 22:13:43 +0000576 return V.convertToDouble();
577}
578
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000579int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) {
Eli Friedmanfd819782012-02-29 20:59:56 +0000580 int CharByteWidth = 0;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000581 switch(k) {
Eli Friedman64f45a22011-11-01 02:23:42 +0000582 case Ascii:
583 case UTF8:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000584 CharByteWidth = target.getCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000585 break;
586 case Wide:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000587 CharByteWidth = target.getWCharWidth();
Eli Friedman64f45a22011-11-01 02:23:42 +0000588 break;
589 case UTF16:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000590 CharByteWidth = target.getChar16Width();
Eli Friedman64f45a22011-11-01 02:23:42 +0000591 break;
592 case UTF32:
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000593 CharByteWidth = target.getChar32Width();
Eli Friedmanfd819782012-02-29 20:59:56 +0000594 break;
Eli Friedman64f45a22011-11-01 02:23:42 +0000595 }
596 assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
597 CharByteWidth /= 8;
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000598 assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4)
Eli Friedman64f45a22011-11-01 02:23:42 +0000599 && "character byte widths supported are 1, 2, and 4 only");
600 return CharByteWidth;
601}
602
Chris Lattner5f9e2722011-07-23 10:55:15 +0000603StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str,
Douglas Gregor5cee1192011-07-27 05:40:30 +0000604 StringKind Kind, bool Pascal, QualType Ty,
Mike Stump1eb44332009-09-09 15:08:12 +0000605 const SourceLocation *Loc,
Anders Carlssona135fb42009-03-15 18:34:13 +0000606 unsigned NumStrs) {
Chris Lattner2085fd62009-02-18 06:40:38 +0000607 // Allocate enough space for the StringLiteral plus an array of locations for
608 // any concatenated string tokens.
609 void *Mem = C.Allocate(sizeof(StringLiteral)+
610 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000611 llvm::alignOf<StringLiteral>());
Chris Lattner2085fd62009-02-18 06:40:38 +0000612 StringLiteral *SL = new (Mem) StringLiteral(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000613
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 // OPTIMIZE: could allocate this appended to the StringLiteral.
Eli Friedman64f45a22011-11-01 02:23:42 +0000615 SL->setString(C,Str,Kind,Pascal);
616
Chris Lattner2085fd62009-02-18 06:40:38 +0000617 SL->TokLocs[0] = Loc[0];
618 SL->NumConcatenated = NumStrs;
Reid Spencer5f016e22007-07-11 17:01:13 +0000619
Chris Lattner726e1682009-02-18 05:49:11 +0000620 if (NumStrs != 1)
Chris Lattner2085fd62009-02-18 06:40:38 +0000621 memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1));
622 return SL;
Chris Lattner726e1682009-02-18 05:49:11 +0000623}
624
Douglas Gregor673ecd62009-04-15 16:35:07 +0000625StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) {
626 void *Mem = C.Allocate(sizeof(StringLiteral)+
627 sizeof(SourceLocation)*(NumStrs-1),
Chris Lattner32488542010-10-30 05:14:06 +0000628 llvm::alignOf<StringLiteral>());
Douglas Gregor673ecd62009-04-15 16:35:07 +0000629 StringLiteral *SL = new (Mem) StringLiteral(QualType());
Eli Friedman64f45a22011-11-01 02:23:42 +0000630 SL->CharByteWidth = 0;
631 SL->Length = 0;
Douglas Gregor673ecd62009-04-15 16:35:07 +0000632 SL->NumConcatenated = NumStrs;
633 return SL;
634}
635
Richard Trieu8ab09da2012-06-13 20:25:24 +0000636void StringLiteral::outputString(raw_ostream &OS) {
637 switch (getKind()) {
638 case Ascii: break; // no prefix.
639 case Wide: OS << 'L'; break;
640 case UTF8: OS << "u8"; break;
641 case UTF16: OS << 'u'; break;
642 case UTF32: OS << 'U'; break;
643 }
644 OS << '"';
645 static const char Hex[] = "0123456789ABCDEF";
646
647 unsigned LastSlashX = getLength();
648 for (unsigned I = 0, N = getLength(); I != N; ++I) {
649 switch (uint32_t Char = getCodeUnit(I)) {
650 default:
651 // FIXME: Convert UTF-8 back to codepoints before rendering.
652
653 // Convert UTF-16 surrogate pairs back to codepoints before rendering.
654 // Leave invalid surrogates alone; we'll use \x for those.
655 if (getKind() == UTF16 && I != N - 1 && Char >= 0xd800 &&
656 Char <= 0xdbff) {
657 uint32_t Trail = getCodeUnit(I + 1);
658 if (Trail >= 0xdc00 && Trail <= 0xdfff) {
659 Char = 0x10000 + ((Char - 0xd800) << 10) + (Trail - 0xdc00);
660 ++I;
661 }
662 }
663
664 if (Char > 0xff) {
665 // If this is a wide string, output characters over 0xff using \x
666 // escapes. Otherwise, this is a UTF-16 or UTF-32 string, and Char is a
667 // codepoint: use \x escapes for invalid codepoints.
668 if (getKind() == Wide ||
669 (Char >= 0xd800 && Char <= 0xdfff) || Char >= 0x110000) {
670 // FIXME: Is this the best way to print wchar_t?
671 OS << "\\x";
672 int Shift = 28;
673 while ((Char >> Shift) == 0)
674 Shift -= 4;
675 for (/**/; Shift >= 0; Shift -= 4)
676 OS << Hex[(Char >> Shift) & 15];
677 LastSlashX = I;
678 break;
679 }
680
681 if (Char > 0xffff)
682 OS << "\\U00"
683 << Hex[(Char >> 20) & 15]
684 << Hex[(Char >> 16) & 15];
685 else
686 OS << "\\u";
687 OS << Hex[(Char >> 12) & 15]
688 << Hex[(Char >> 8) & 15]
689 << Hex[(Char >> 4) & 15]
690 << Hex[(Char >> 0) & 15];
691 break;
692 }
693
694 // If we used \x... for the previous character, and this character is a
695 // hexadecimal digit, prevent it being slurped as part of the \x.
696 if (LastSlashX + 1 == I) {
697 switch (Char) {
698 case '0': case '1': case '2': case '3': case '4':
699 case '5': case '6': case '7': case '8': case '9':
700 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
701 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
702 OS << "\"\"";
703 }
704 }
705
706 assert(Char <= 0xff &&
707 "Characters above 0xff should already have been handled.");
708
709 if (isprint(Char))
710 OS << (char)Char;
711 else // Output anything hard as an octal escape.
712 OS << '\\'
713 << (char)('0' + ((Char >> 6) & 7))
714 << (char)('0' + ((Char >> 3) & 7))
715 << (char)('0' + ((Char >> 0) & 7));
716 break;
717 // Handle some common non-printable cases to make dumps prettier.
718 case '\\': OS << "\\\\"; break;
719 case '"': OS << "\\\""; break;
720 case '\n': OS << "\\n"; break;
721 case '\t': OS << "\\t"; break;
722 case '\a': OS << "\\a"; break;
723 case '\b': OS << "\\b"; break;
724 }
725 }
726 OS << '"';
727}
728
Eli Friedman64f45a22011-11-01 02:23:42 +0000729void StringLiteral::setString(ASTContext &C, StringRef Str,
730 StringKind Kind, bool IsPascal) {
731 //FIXME: we assume that the string data comes from a target that uses the same
732 // code unit size and endianess for the type of string.
733 this->Kind = Kind;
734 this->IsPascal = IsPascal;
735
Nick Lewycky0fd7f4d2012-02-24 09:07:53 +0000736 CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind);
Eli Friedman64f45a22011-11-01 02:23:42 +0000737 assert((Str.size()%CharByteWidth == 0)
738 && "size of data must be multiple of CharByteWidth");
739 Length = Str.size()/CharByteWidth;
740
741 switch(CharByteWidth) {
742 case 1: {
743 char *AStrData = new (C) char[Length];
744 std::memcpy(AStrData,Str.data(),Str.size());
745 StrData.asChar = AStrData;
746 break;
747 }
748 case 2: {
749 uint16_t *AStrData = new (C) uint16_t[Length];
750 std::memcpy(AStrData,Str.data(),Str.size());
751 StrData.asUInt16 = AStrData;
752 break;
753 }
754 case 4: {
755 uint32_t *AStrData = new (C) uint32_t[Length];
756 std::memcpy(AStrData,Str.data(),Str.size());
757 StrData.asUInt32 = AStrData;
758 break;
759 }
760 default:
761 assert(false && "unsupported CharByteWidth");
762 }
Douglas Gregor673ecd62009-04-15 16:35:07 +0000763}
764
Chris Lattner08f92e32010-11-17 07:37:15 +0000765/// getLocationOfByte - Return a source location that points to the specified
766/// byte of this string literal.
767///
768/// Strings are amazingly complex. They can be formed from multiple tokens and
769/// can have escape sequences in them in addition to the usual trigraph and
770/// escaped newline business. This routine handles this complexity.
771///
772SourceLocation StringLiteral::
773getLocationOfByte(unsigned ByteNo, const SourceManager &SM,
774 const LangOptions &Features, const TargetInfo &Target) const {
Richard Smithdf9ef1b2012-06-13 05:37:23 +0000775 assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) &&
776 "Only narrow string literals are currently supported");
Douglas Gregor5cee1192011-07-27 05:40:30 +0000777
Chris Lattner08f92e32010-11-17 07:37:15 +0000778 // Loop over all of the tokens in this string until we find the one that
779 // contains the byte we're looking for.
780 unsigned TokNo = 0;
781 while (1) {
782 assert(TokNo < getNumConcatenated() && "Invalid byte number!");
783 SourceLocation StrTokLoc = getStrTokenLoc(TokNo);
784
785 // Get the spelling of the string so that we can get the data that makes up
786 // the string literal, not the identifier for the macro it is potentially
787 // expanded through.
788 SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc);
789
790 // Re-lex the token to get its length and original spelling.
791 std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc);
792 bool Invalid = false;
Chris Lattner5f9e2722011-07-23 10:55:15 +0000793 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Chris Lattner08f92e32010-11-17 07:37:15 +0000794 if (Invalid)
795 return StrTokSpellingLoc;
796
797 const char *StrData = Buffer.data()+LocInfo.second;
798
Chris Lattner08f92e32010-11-17 07:37:15 +0000799 // Create a lexer starting at the beginning of this token.
Argyrios Kyrtzidisdf875582012-05-11 21:39:18 +0000800 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features,
801 Buffer.begin(), StrData, Buffer.end());
Chris Lattner08f92e32010-11-17 07:37:15 +0000802 Token TheTok;
803 TheLexer.LexFromRawLexer(TheTok);
804
805 // Use the StringLiteralParser to compute the length of the string in bytes.
806 StringLiteralParser SLP(&TheTok, 1, SM, Features, Target);
807 unsigned TokNumBytes = SLP.GetStringLength();
808
809 // If the byte is in this token, return the location of the byte.
810 if (ByteNo < TokNumBytes ||
Hans Wennborg935a70c2011-06-30 20:17:41 +0000811 (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) {
Chris Lattner08f92e32010-11-17 07:37:15 +0000812 unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo);
813
814 // Now that we know the offset of the token in the spelling, use the
815 // preprocessor to get the offset in the original source.
816 return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features);
817 }
818
819 // Move to the next string token.
820 ++TokNo;
821 ByteNo -= TokNumBytes;
822 }
823}
824
825
826
Reid Spencer5f016e22007-07-11 17:01:13 +0000827/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
828/// corresponds to, e.g. "sizeof" or "[pre]++".
829const char *UnaryOperator::getOpcodeStr(Opcode Op) {
830 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +0000831 case UO_PostInc: return "++";
832 case UO_PostDec: return "--";
833 case UO_PreInc: return "++";
834 case UO_PreDec: return "--";
835 case UO_AddrOf: return "&";
836 case UO_Deref: return "*";
837 case UO_Plus: return "+";
838 case UO_Minus: return "-";
839 case UO_Not: return "~";
840 case UO_LNot: return "!";
841 case UO_Real: return "__real";
842 case UO_Imag: return "__imag";
843 case UO_Extension: return "__extension__";
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 }
David Blaikie561d3ab2012-01-17 02:30:50 +0000845 llvm_unreachable("Unknown unary operator");
Reid Spencer5f016e22007-07-11 17:01:13 +0000846}
847
John McCall2de56d12010-08-25 11:45:40 +0000848UnaryOperatorKind
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000849UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) {
850 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +0000851 default: llvm_unreachable("No unary operator for overloaded function");
John McCall2de56d12010-08-25 11:45:40 +0000852 case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc;
853 case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec;
854 case OO_Amp: return UO_AddrOf;
855 case OO_Star: return UO_Deref;
856 case OO_Plus: return UO_Plus;
857 case OO_Minus: return UO_Minus;
858 case OO_Tilde: return UO_Not;
859 case OO_Exclaim: return UO_LNot;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000860 }
861}
862
863OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) {
864 switch (Opc) {
John McCall2de56d12010-08-25 11:45:40 +0000865 case UO_PostInc: case UO_PreInc: return OO_PlusPlus;
866 case UO_PostDec: case UO_PreDec: return OO_MinusMinus;
867 case UO_AddrOf: return OO_Amp;
868 case UO_Deref: return OO_Star;
869 case UO_Plus: return OO_Plus;
870 case UO_Minus: return OO_Minus;
871 case UO_Not: return OO_Tilde;
872 case UO_LNot: return OO_Exclaim;
Douglas Gregorbc736fc2009-03-13 23:49:33 +0000873 default: return OO_None;
874 }
875}
876
877
Reid Spencer5f016e22007-07-11 17:01:13 +0000878//===----------------------------------------------------------------------===//
879// Postfix Operators.
880//===----------------------------------------------------------------------===//
881
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000882CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs,
883 Expr **args, unsigned numargs, QualType t, ExprValueKind VK,
John McCallf89e55a2010-11-18 06:31:45 +0000884 SourceLocation rparenloc)
885 : Expr(SC, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000886 fn->isTypeDependent(),
887 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000888 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000889 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000890 NumArgs(numargs) {
Mike Stump1eb44332009-09-09 15:08:12 +0000891
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000892 SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs];
Douglas Gregorb4609802008-11-14 16:09:21 +0000893 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000894 for (unsigned i = 0; i != numargs; ++i) {
895 if (args[i]->isTypeDependent())
896 ExprBits.TypeDependent = true;
897 if (args[i]->isValueDependent())
898 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000899 if (args[i]->isInstantiationDependent())
900 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000901 if (args[i]->containsUnexpandedParameterPack())
902 ExprBits.ContainsUnexpandedParameterPack = true;
903
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000904 SubExprs[i+PREARGS_START+NumPreArgs] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000905 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000906
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000907 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregorb4609802008-11-14 16:09:21 +0000908 RParenLoc = rparenloc;
909}
Nate Begemane2ce1d92008-01-17 17:46:27 +0000910
Ted Kremenek668bf912009-02-09 20:51:47 +0000911CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs,
John McCallf89e55a2010-11-18 06:31:45 +0000912 QualType t, ExprValueKind VK, SourceLocation rparenloc)
913 : Expr(CallExprClass, t, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000914 fn->isTypeDependent(),
915 fn->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +0000916 fn->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000917 fn->containsUnexpandedParameterPack()),
Douglas Gregor898574e2008-12-05 23:32:09 +0000918 NumArgs(numargs) {
Ted Kremenek668bf912009-02-09 20:51:47 +0000919
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000920 SubExprs = new (C) Stmt*[numargs+PREARGS_START];
Ted Kremenek77ed8e42007-08-24 18:13:47 +0000921 SubExprs[FN] = fn;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000922 for (unsigned i = 0; i != numargs; ++i) {
923 if (args[i]->isTypeDependent())
924 ExprBits.TypeDependent = true;
925 if (args[i]->isValueDependent())
926 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +0000927 if (args[i]->isInstantiationDependent())
928 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000929 if (args[i]->containsUnexpandedParameterPack())
930 ExprBits.ContainsUnexpandedParameterPack = true;
931
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000932 SubExprs[i+PREARGS_START] = args[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000933 }
Ted Kremenek668bf912009-02-09 20:51:47 +0000934
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000935 CallExprBits.NumPreArgs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000936 RParenLoc = rparenloc;
937}
938
Mike Stump1eb44332009-09-09 15:08:12 +0000939CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty)
940 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +0000941 // FIXME: Why do we allocate this?
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000942 SubExprs = new (C) Stmt*[PREARGS_START];
943 CallExprBits.NumPreArgs = 0;
944}
945
946CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs,
947 EmptyShell Empty)
948 : Expr(SC, Empty), SubExprs(0), NumArgs(0) {
949 // FIXME: Why do we allocate this?
950 SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs];
951 CallExprBits.NumPreArgs = NumPreArgs;
Douglas Gregor1f0d0132009-04-15 17:43:59 +0000952}
953
Nuno Lopesd20254f2009-12-20 23:11:08 +0000954Decl *CallExpr::getCalleeDecl() {
John McCalle8683d62011-09-13 23:08:34 +0000955 Expr *CEE = getCallee()->IgnoreParenImpCasts();
Douglas Gregor1ddc9c42011-09-06 21:41:04 +0000956
957 while (SubstNonTypeTemplateParmExpr *NTTP
958 = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) {
959 CEE = NTTP->getReplacement()->IgnoreParenCasts();
960 }
961
Sebastian Redl20012152010-09-10 20:55:30 +0000962 // If we're calling a dereference, look at the pointer instead.
963 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) {
964 if (BO->isPtrMemOp())
965 CEE = BO->getRHS()->IgnoreParenCasts();
966 } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) {
967 if (UO->getOpcode() == UO_Deref)
968 CEE = UO->getSubExpr()->IgnoreParenCasts();
969 }
Chris Lattner6346f962009-07-17 15:46:27 +0000970 if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE))
Nuno Lopesd20254f2009-12-20 23:11:08 +0000971 return DRE->getDecl();
Nuno Lopescb1c77f2009-12-24 00:28:18 +0000972 if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE))
973 return ME->getMemberDecl();
Zhongxing Xua0042542009-07-17 07:29:51 +0000974
975 return 0;
976}
977
Nuno Lopesd20254f2009-12-20 23:11:08 +0000978FunctionDecl *CallExpr::getDirectCallee() {
Chris Lattnercaabf9b2009-12-21 01:10:56 +0000979 return dyn_cast_or_null<FunctionDecl>(getCalleeDecl());
Nuno Lopesd20254f2009-12-20 23:11:08 +0000980}
981
Chris Lattnerd18b3292007-12-28 05:25:02 +0000982/// setNumArgs - This changes the number of arguments present in this call.
983/// Any orphaned expressions are deleted by this, and any new operands are set
984/// to null.
Ted Kremenek8189cde2009-02-07 01:47:29 +0000985void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000986 // No change, just return.
987 if (NumArgs == getNumArgs()) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Chris Lattnerd18b3292007-12-28 05:25:02 +0000989 // If shrinking # arguments, just delete the extras and forgot them.
990 if (NumArgs < getNumArgs()) {
Chris Lattnerd18b3292007-12-28 05:25:02 +0000991 this->NumArgs = NumArgs;
992 return;
993 }
994
995 // Otherwise, we are growing the # arguments. New an bigger argument array.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000996 unsigned NumPreArgs = getNumPreArgs();
997 Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs];
Chris Lattnerd18b3292007-12-28 05:25:02 +0000998 // Copy over args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +0000999 for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001000 NewSubExprs[i] = SubExprs[i];
1001 // Null out new args.
Peter Collingbournecc324ad2011-02-08 21:18:02 +00001002 for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs;
1003 i != NumArgs+PREARGS_START+NumPreArgs; ++i)
Chris Lattnerd18b3292007-12-28 05:25:02 +00001004 NewSubExprs[i] = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Douglas Gregor88c9a462009-04-17 21:46:47 +00001006 if (SubExprs) C.Deallocate(SubExprs);
Chris Lattnerd18b3292007-12-28 05:25:02 +00001007 SubExprs = NewSubExprs;
1008 this->NumArgs = NumArgs;
1009}
1010
Chris Lattnercb888962008-10-06 05:00:53 +00001011/// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If
1012/// not, return 0.
Richard Smith180f4792011-11-10 06:34:14 +00001013unsigned CallExpr::isBuiltinCall() const {
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001014 // All simple function calls (e.g. func()) are implicitly cast to pointer to
Mike Stump1eb44332009-09-09 15:08:12 +00001015 // function. As a result, we try and obtain the DeclRefExpr from the
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001016 // ImplicitCastExpr.
1017 const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee());
1018 if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()).
Chris Lattnercb888962008-10-06 05:00:53 +00001019 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Steve Naroffc4f8e8b2008-01-31 01:07:12 +00001021 const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr());
1022 if (!DRE)
Chris Lattnercb888962008-10-06 05:00:53 +00001023 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Anders Carlssonbcba2012008-01-31 02:13:57 +00001025 const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl());
1026 if (!FDecl)
Chris Lattnercb888962008-10-06 05:00:53 +00001027 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Douglas Gregor4fcd3992008-11-21 15:30:19 +00001029 if (!FDecl->getIdentifier())
1030 return 0;
1031
Douglas Gregor7814e6d2009-09-12 00:22:50 +00001032 return FDecl->getBuiltinID();
Chris Lattnercb888962008-10-06 05:00:53 +00001033}
Anders Carlssonbcba2012008-01-31 02:13:57 +00001034
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001035QualType CallExpr::getCallReturnType() const {
1036 QualType CalleeType = getCallee()->getType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001037 if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001038 CalleeType = FnTypePtr->getPointeeType();
Ted Kremenek6217b802009-07-29 21:53:49 +00001039 else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>())
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001040 CalleeType = BPT->getPointeeType();
John McCall864c0412011-04-26 20:42:42 +00001041 else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember))
1042 // This should never be overloaded and so should never return null.
1043 CalleeType = Expr::findBoundMemberType(getCallee());
Douglas Gregor5291c3c2010-07-13 08:18:22 +00001044
John McCall864c0412011-04-26 20:42:42 +00001045 const FunctionType *FnType = CalleeType->castAs<FunctionType>();
Anders Carlsson6dde78f2009-05-26 04:57:27 +00001046 return FnType->getResultType();
1047}
Chris Lattnercb888962008-10-06 05:00:53 +00001048
John McCall2882eca2011-02-21 06:23:05 +00001049SourceRange CallExpr::getSourceRange() const {
1050 if (isa<CXXOperatorCallExpr>(this))
1051 return cast<CXXOperatorCallExpr>(this)->getSourceRange();
1052
1053 SourceLocation begin = getCallee()->getLocStart();
1054 if (begin.isInvalid() && getNumArgs() > 0)
1055 begin = getArg(0)->getLocStart();
1056 SourceLocation end = getRParenLoc();
1057 if (end.isInvalid() && getNumArgs() > 0)
1058 end = getArg(getNumArgs() - 1)->getLocEnd();
1059 return SourceRange(begin, end);
1060}
Daniel Dunbar8fbc6d22012-03-09 15:39:24 +00001061SourceLocation CallExpr::getLocStart() const {
1062 if (isa<CXXOperatorCallExpr>(this))
1063 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getBegin();
1064
1065 SourceLocation begin = getCallee()->getLocStart();
1066 if (begin.isInvalid() && getNumArgs() > 0)
1067 begin = getArg(0)->getLocStart();
1068 return begin;
1069}
1070SourceLocation CallExpr::getLocEnd() const {
1071 if (isa<CXXOperatorCallExpr>(this))
1072 return cast<CXXOperatorCallExpr>(this)->getSourceRange().getEnd();
1073
1074 SourceLocation end = getRParenLoc();
1075 if (end.isInvalid() && getNumArgs() > 0)
1076 end = getArg(getNumArgs() - 1)->getLocEnd();
1077 return end;
1078}
John McCall2882eca2011-02-21 06:23:05 +00001079
Sean Huntc3021132010-05-05 15:23:54 +00001080OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001081 SourceLocation OperatorLoc,
Sean Huntc3021132010-05-05 15:23:54 +00001082 TypeSourceInfo *tsi,
1083 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001084 Expr** exprsPtr, unsigned numExprs,
1085 SourceLocation RParenLoc) {
1086 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
Sean Huntc3021132010-05-05 15:23:54 +00001087 sizeof(OffsetOfNode) * numComps +
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001088 sizeof(Expr*) * numExprs);
1089
1090 return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps,
1091 exprsPtr, numExprs, RParenLoc);
1092}
1093
1094OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C,
1095 unsigned numComps, unsigned numExprs) {
1096 void *Mem = C.Allocate(sizeof(OffsetOfExpr) +
1097 sizeof(OffsetOfNode) * numComps +
1098 sizeof(Expr*) * numExprs);
1099 return new (Mem) OffsetOfExpr(numComps, numExprs);
1100}
1101
Sean Huntc3021132010-05-05 15:23:54 +00001102OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001103 SourceLocation OperatorLoc, TypeSourceInfo *tsi,
Sean Huntc3021132010-05-05 15:23:54 +00001104 OffsetOfNode* compsPtr, unsigned numComps,
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001105 Expr** exprsPtr, unsigned numExprs,
1106 SourceLocation RParenLoc)
John McCallf89e55a2010-11-18 06:31:45 +00001107 : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary,
1108 /*TypeDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001109 /*ValueDependent=*/tsi->getType()->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00001110 tsi->getType()->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001111 tsi->getType()->containsUnexpandedParameterPack()),
Sean Huntc3021132010-05-05 15:23:54 +00001112 OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi),
1113 NumComps(numComps), NumExprs(numExprs)
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001114{
1115 for(unsigned i = 0; i < numComps; ++i) {
1116 setComponent(i, compsPtr[i]);
1117 }
Sean Huntc3021132010-05-05 15:23:54 +00001118
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001119 for(unsigned i = 0; i < numExprs; ++i) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001120 if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent())
1121 ExprBits.ValueDependent = true;
1122 if (exprsPtr[i]->containsUnexpandedParameterPack())
1123 ExprBits.ContainsUnexpandedParameterPack = true;
1124
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001125 setIndexExpr(i, exprsPtr[i]);
1126 }
1127}
1128
1129IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const {
1130 assert(getKind() == Field || getKind() == Identifier);
1131 if (getKind() == Field)
1132 return getField()->getIdentifier();
Sean Huntc3021132010-05-05 15:23:54 +00001133
Douglas Gregor8ecdb652010-04-28 22:16:22 +00001134 return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask);
1135}
1136
Mike Stump1eb44332009-09-09 15:08:12 +00001137MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow,
Douglas Gregor40d96a62011-02-28 21:54:11 +00001138 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001139 SourceLocation TemplateKWLoc,
Eli Friedmanf595cc42009-12-04 06:40:45 +00001140 ValueDecl *memberdecl,
John McCall161755a2010-04-06 21:38:20 +00001141 DeclAccessPair founddecl,
Abramo Bagnara25777432010-08-11 22:01:17 +00001142 DeclarationNameInfo nameinfo,
John McCalld5532b62009-11-23 01:53:49 +00001143 const TemplateArgumentListInfo *targs,
John McCallf89e55a2010-11-18 06:31:45 +00001144 QualType ty,
1145 ExprValueKind vk,
1146 ExprObjectKind ok) {
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001147 std::size_t Size = sizeof(MemberExpr);
John McCall6bb80172010-03-30 21:47:33 +00001148
Douglas Gregor40d96a62011-02-28 21:54:11 +00001149 bool hasQualOrFound = (QualifierLoc ||
John McCall161755a2010-04-06 21:38:20 +00001150 founddecl.getDecl() != memberdecl ||
1151 founddecl.getAccess() != memberdecl->getAccess());
John McCall6bb80172010-03-30 21:47:33 +00001152 if (hasQualOrFound)
1153 Size += sizeof(MemberNameQualifier);
Mike Stump1eb44332009-09-09 15:08:12 +00001154
John McCalld5532b62009-11-23 01:53:49 +00001155 if (targs)
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001156 Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size());
1157 else if (TemplateKWLoc.isValid())
1158 Size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Chris Lattner32488542010-10-30 05:14:06 +00001160 void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>());
John McCallf89e55a2010-11-18 06:31:45 +00001161 MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo,
1162 ty, vk, ok);
John McCall6bb80172010-03-30 21:47:33 +00001163
1164 if (hasQualOrFound) {
Douglas Gregor40d96a62011-02-28 21:54:11 +00001165 // FIXME: Wrong. We should be looking at the member declaration we found.
1166 if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) {
John McCall6bb80172010-03-30 21:47:33 +00001167 E->setValueDependent(true);
1168 E->setTypeDependent(true);
Douglas Gregor561f8122011-07-01 01:22:09 +00001169 E->setInstantiationDependent(true);
1170 }
1171 else if (QualifierLoc &&
1172 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())
1173 E->setInstantiationDependent(true);
1174
John McCall6bb80172010-03-30 21:47:33 +00001175 E->HasQualifierOrFoundDecl = true;
1176
1177 MemberNameQualifier *NQ = E->getMemberQualifier();
Douglas Gregor40d96a62011-02-28 21:54:11 +00001178 NQ->QualifierLoc = QualifierLoc;
John McCall6bb80172010-03-30 21:47:33 +00001179 NQ->FoundDecl = founddecl;
1180 }
1181
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001182 E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid());
1183
John McCall6bb80172010-03-30 21:47:33 +00001184 if (targs) {
Douglas Gregor561f8122011-07-01 01:22:09 +00001185 bool Dependent = false;
1186 bool InstantiationDependent = false;
1187 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001188 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs,
1189 Dependent,
1190 InstantiationDependent,
1191 ContainsUnexpandedParameterPack);
Douglas Gregor561f8122011-07-01 01:22:09 +00001192 if (InstantiationDependent)
1193 E->setInstantiationDependent(true);
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001194 } else if (TemplateKWLoc.isValid()) {
1195 E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
John McCall6bb80172010-03-30 21:47:33 +00001196 }
1197
1198 return E;
Douglas Gregor83f6faf2009-08-31 23:41:50 +00001199}
1200
Douglas Gregor75e85042011-03-02 21:06:53 +00001201SourceRange MemberExpr::getSourceRange() const {
Daniel Dunbar396ec672012-03-09 15:39:15 +00001202 return SourceRange(getLocStart(), getLocEnd());
1203}
1204SourceLocation MemberExpr::getLocStart() const {
Douglas Gregor75e85042011-03-02 21:06:53 +00001205 if (isImplicitAccess()) {
1206 if (hasQualifier())
Daniel Dunbar396ec672012-03-09 15:39:15 +00001207 return getQualifierLoc().getBeginLoc();
1208 return MemberLoc;
Douglas Gregor75e85042011-03-02 21:06:53 +00001209 }
Abramo Bagnarae4b92762012-01-27 09:46:47 +00001210
Daniel Dunbar396ec672012-03-09 15:39:15 +00001211 // FIXME: We don't want this to happen. Rather, we should be able to
1212 // detect all kinds of implicit accesses more cleanly.
1213 SourceLocation BaseStartLoc = getBase()->getLocStart();
1214 if (BaseStartLoc.isValid())
1215 return BaseStartLoc;
1216 return MemberLoc;
1217}
1218SourceLocation MemberExpr::getLocEnd() const {
1219 if (hasExplicitTemplateArgs())
1220 return getRAngleLoc();
1221 return getMemberNameInfo().getEndLoc();
Douglas Gregor75e85042011-03-02 21:06:53 +00001222}
1223
John McCall1d9b3b22011-09-09 05:25:32 +00001224void CastExpr::CheckCastConsistency() const {
1225 switch (getCastKind()) {
1226 case CK_DerivedToBase:
1227 case CK_UncheckedDerivedToBase:
1228 case CK_DerivedToBaseMemberPointer:
1229 case CK_BaseToDerived:
1230 case CK_BaseToDerivedMemberPointer:
1231 assert(!path_empty() && "Cast kind should have a base path!");
1232 break;
1233
1234 case CK_CPointerToObjCPointerCast:
1235 assert(getType()->isObjCObjectPointerType());
1236 assert(getSubExpr()->getType()->isPointerType());
1237 goto CheckNoBasePath;
1238
1239 case CK_BlockPointerToObjCPointerCast:
1240 assert(getType()->isObjCObjectPointerType());
1241 assert(getSubExpr()->getType()->isBlockPointerType());
1242 goto CheckNoBasePath;
1243
John McCall4d4e5c12012-02-15 01:22:51 +00001244 case CK_ReinterpretMemberPointer:
1245 assert(getType()->isMemberPointerType());
1246 assert(getSubExpr()->getType()->isMemberPointerType());
1247 goto CheckNoBasePath;
1248
John McCall1d9b3b22011-09-09 05:25:32 +00001249 case CK_BitCast:
1250 // Arbitrary casts to C pointer types count as bitcasts.
1251 // Otherwise, we should only have block and ObjC pointer casts
1252 // here if they stay within the type kind.
1253 if (!getType()->isPointerType()) {
1254 assert(getType()->isObjCObjectPointerType() ==
1255 getSubExpr()->getType()->isObjCObjectPointerType());
1256 assert(getType()->isBlockPointerType() ==
1257 getSubExpr()->getType()->isBlockPointerType());
1258 }
1259 goto CheckNoBasePath;
1260
1261 case CK_AnyPointerToBlockPointerCast:
1262 assert(getType()->isBlockPointerType());
1263 assert(getSubExpr()->getType()->isAnyPointerType() &&
1264 !getSubExpr()->getType()->isBlockPointerType());
1265 goto CheckNoBasePath;
1266
Douglas Gregorac1303e2012-02-22 05:02:47 +00001267 case CK_CopyAndAutoreleaseBlockObject:
1268 assert(getType()->isBlockPointerType());
1269 assert(getSubExpr()->getType()->isBlockPointerType());
1270 goto CheckNoBasePath;
1271
John McCall1d9b3b22011-09-09 05:25:32 +00001272 // These should not have an inheritance path.
1273 case CK_Dynamic:
1274 case CK_ToUnion:
1275 case CK_ArrayToPointerDecay:
1276 case CK_FunctionToPointerDecay:
1277 case CK_NullToMemberPointer:
1278 case CK_NullToPointer:
1279 case CK_ConstructorConversion:
1280 case CK_IntegralToPointer:
1281 case CK_PointerToIntegral:
1282 case CK_ToVoid:
1283 case CK_VectorSplat:
1284 case CK_IntegralCast:
1285 case CK_IntegralToFloating:
1286 case CK_FloatingToIntegral:
1287 case CK_FloatingCast:
1288 case CK_ObjCObjectLValueCast:
1289 case CK_FloatingRealToComplex:
1290 case CK_FloatingComplexToReal:
1291 case CK_FloatingComplexCast:
1292 case CK_FloatingComplexToIntegralComplex:
1293 case CK_IntegralRealToComplex:
1294 case CK_IntegralComplexToReal:
1295 case CK_IntegralComplexCast:
1296 case CK_IntegralComplexToFloatingComplex:
John McCall33e56f32011-09-10 06:18:15 +00001297 case CK_ARCProduceObject:
1298 case CK_ARCConsumeObject:
1299 case CK_ARCReclaimReturnedObject:
1300 case CK_ARCExtendBlockObject:
John McCall1d9b3b22011-09-09 05:25:32 +00001301 assert(!getType()->isBooleanType() && "unheralded conversion to bool");
1302 goto CheckNoBasePath;
1303
1304 case CK_Dependent:
1305 case CK_LValueToRValue:
John McCall1d9b3b22011-09-09 05:25:32 +00001306 case CK_NoOp:
David Chisnall7a7ee302012-01-16 17:27:18 +00001307 case CK_AtomicToNonAtomic:
1308 case CK_NonAtomicToAtomic:
John McCall1d9b3b22011-09-09 05:25:32 +00001309 case CK_PointerToBoolean:
1310 case CK_IntegralToBoolean:
1311 case CK_FloatingToBoolean:
1312 case CK_MemberPointerToBoolean:
1313 case CK_FloatingComplexToBoolean:
1314 case CK_IntegralComplexToBoolean:
1315 case CK_LValueBitCast: // -> bool&
1316 case CK_UserDefinedConversion: // operator bool()
1317 CheckNoBasePath:
1318 assert(path_empty() && "Cast kind should not have a base path!");
1319 break;
1320 }
1321}
1322
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001323const char *CastExpr::getCastKindName() const {
1324 switch (getCastKind()) {
John McCalldaa8e4e2010-11-15 09:13:47 +00001325 case CK_Dependent:
1326 return "Dependent";
John McCall2de56d12010-08-25 11:45:40 +00001327 case CK_BitCast:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001328 return "BitCast";
John McCall2de56d12010-08-25 11:45:40 +00001329 case CK_LValueBitCast:
Douglas Gregore39a3892010-07-13 23:17:26 +00001330 return "LValueBitCast";
John McCall0ae287a2010-12-01 04:43:34 +00001331 case CK_LValueToRValue:
1332 return "LValueToRValue";
John McCall2de56d12010-08-25 11:45:40 +00001333 case CK_NoOp:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001334 return "NoOp";
John McCall2de56d12010-08-25 11:45:40 +00001335 case CK_BaseToDerived:
Anders Carlsson11de6de2009-11-12 16:43:42 +00001336 return "BaseToDerived";
John McCall2de56d12010-08-25 11:45:40 +00001337 case CK_DerivedToBase:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001338 return "DerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001339 case CK_UncheckedDerivedToBase:
John McCall23cba802010-03-30 23:58:03 +00001340 return "UncheckedDerivedToBase";
John McCall2de56d12010-08-25 11:45:40 +00001341 case CK_Dynamic:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001342 return "Dynamic";
John McCall2de56d12010-08-25 11:45:40 +00001343 case CK_ToUnion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001344 return "ToUnion";
John McCall2de56d12010-08-25 11:45:40 +00001345 case CK_ArrayToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001346 return "ArrayToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001347 case CK_FunctionToPointerDecay:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001348 return "FunctionToPointerDecay";
John McCall2de56d12010-08-25 11:45:40 +00001349 case CK_NullToMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001350 return "NullToMemberPointer";
John McCall404cd162010-11-13 01:35:44 +00001351 case CK_NullToPointer:
1352 return "NullToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001353 case CK_BaseToDerivedMemberPointer:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001354 return "BaseToDerivedMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001355 case CK_DerivedToBaseMemberPointer:
Anders Carlsson1a31a182009-10-30 00:46:35 +00001356 return "DerivedToBaseMemberPointer";
John McCall4d4e5c12012-02-15 01:22:51 +00001357 case CK_ReinterpretMemberPointer:
1358 return "ReinterpretMemberPointer";
John McCall2de56d12010-08-25 11:45:40 +00001359 case CK_UserDefinedConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001360 return "UserDefinedConversion";
John McCall2de56d12010-08-25 11:45:40 +00001361 case CK_ConstructorConversion:
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001362 return "ConstructorConversion";
John McCall2de56d12010-08-25 11:45:40 +00001363 case CK_IntegralToPointer:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001364 return "IntegralToPointer";
John McCall2de56d12010-08-25 11:45:40 +00001365 case CK_PointerToIntegral:
Anders Carlsson7f9e6462009-09-15 04:48:33 +00001366 return "PointerToIntegral";
John McCalldaa8e4e2010-11-15 09:13:47 +00001367 case CK_PointerToBoolean:
1368 return "PointerToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001369 case CK_ToVoid:
Anders Carlssonebeaf202009-10-16 02:35:04 +00001370 return "ToVoid";
John McCall2de56d12010-08-25 11:45:40 +00001371 case CK_VectorSplat:
Anders Carlsson16a89042009-10-16 05:23:41 +00001372 return "VectorSplat";
John McCall2de56d12010-08-25 11:45:40 +00001373 case CK_IntegralCast:
Anders Carlsson82debc72009-10-18 18:12:03 +00001374 return "IntegralCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001375 case CK_IntegralToBoolean:
1376 return "IntegralToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001377 case CK_IntegralToFloating:
Anders Carlsson82debc72009-10-18 18:12:03 +00001378 return "IntegralToFloating";
John McCall2de56d12010-08-25 11:45:40 +00001379 case CK_FloatingToIntegral:
Anders Carlsson82debc72009-10-18 18:12:03 +00001380 return "FloatingToIntegral";
John McCall2de56d12010-08-25 11:45:40 +00001381 case CK_FloatingCast:
Benjamin Kramerc6b29162009-10-18 19:02:15 +00001382 return "FloatingCast";
John McCalldaa8e4e2010-11-15 09:13:47 +00001383 case CK_FloatingToBoolean:
1384 return "FloatingToBoolean";
John McCall2de56d12010-08-25 11:45:40 +00001385 case CK_MemberPointerToBoolean:
Anders Carlssonbc0e0782009-11-23 20:04:44 +00001386 return "MemberPointerToBoolean";
John McCall1d9b3b22011-09-09 05:25:32 +00001387 case CK_CPointerToObjCPointerCast:
1388 return "CPointerToObjCPointerCast";
1389 case CK_BlockPointerToObjCPointerCast:
1390 return "BlockPointerToObjCPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001391 case CK_AnyPointerToBlockPointerCast:
Fariborz Jahanian3b27f1a2009-12-11 22:40:48 +00001392 return "AnyPointerToBlockPointerCast";
John McCall2de56d12010-08-25 11:45:40 +00001393 case CK_ObjCObjectLValueCast:
Douglas Gregor569c3162010-08-07 11:51:51 +00001394 return "ObjCObjectLValueCast";
John McCall2bb5d002010-11-13 09:02:35 +00001395 case CK_FloatingRealToComplex:
1396 return "FloatingRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001397 case CK_FloatingComplexToReal:
1398 return "FloatingComplexToReal";
1399 case CK_FloatingComplexToBoolean:
1400 return "FloatingComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001401 case CK_FloatingComplexCast:
1402 return "FloatingComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001403 case CK_FloatingComplexToIntegralComplex:
1404 return "FloatingComplexToIntegralComplex";
John McCall2bb5d002010-11-13 09:02:35 +00001405 case CK_IntegralRealToComplex:
1406 return "IntegralRealToComplex";
John McCallf3ea8cf2010-11-14 08:17:51 +00001407 case CK_IntegralComplexToReal:
1408 return "IntegralComplexToReal";
1409 case CK_IntegralComplexToBoolean:
1410 return "IntegralComplexToBoolean";
John McCall2bb5d002010-11-13 09:02:35 +00001411 case CK_IntegralComplexCast:
1412 return "IntegralComplexCast";
John McCallf3ea8cf2010-11-14 08:17:51 +00001413 case CK_IntegralComplexToFloatingComplex:
1414 return "IntegralComplexToFloatingComplex";
John McCall33e56f32011-09-10 06:18:15 +00001415 case CK_ARCConsumeObject:
1416 return "ARCConsumeObject";
1417 case CK_ARCProduceObject:
1418 return "ARCProduceObject";
1419 case CK_ARCReclaimReturnedObject:
1420 return "ARCReclaimReturnedObject";
1421 case CK_ARCExtendBlockObject:
1422 return "ARCCExtendBlockObject";
David Chisnall7a7ee302012-01-16 17:27:18 +00001423 case CK_AtomicToNonAtomic:
1424 return "AtomicToNonAtomic";
1425 case CK_NonAtomicToAtomic:
1426 return "NonAtomicToAtomic";
Douglas Gregorac1303e2012-02-22 05:02:47 +00001427 case CK_CopyAndAutoreleaseBlockObject:
1428 return "CopyAndAutoreleaseBlockObject";
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001429 }
Mike Stump1eb44332009-09-09 15:08:12 +00001430
John McCall2bb5d002010-11-13 09:02:35 +00001431 llvm_unreachable("Unhandled cast kind!");
Anders Carlssonf8ec55a2009-09-03 00:59:21 +00001432}
1433
Douglas Gregor6eef5192009-12-14 19:27:10 +00001434Expr *CastExpr::getSubExprAsWritten() {
1435 Expr *SubExpr = 0;
1436 CastExpr *E = this;
1437 do {
1438 SubExpr = E->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00001439
1440 // Skip through reference binding to temporary.
1441 if (MaterializeTemporaryExpr *Materialize
1442 = dyn_cast<MaterializeTemporaryExpr>(SubExpr))
1443 SubExpr = Materialize->GetTemporaryExpr();
1444
Douglas Gregor6eef5192009-12-14 19:27:10 +00001445 // Skip any temporary bindings; they're implicit.
1446 if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr))
1447 SubExpr = Binder->getSubExpr();
Sean Huntc3021132010-05-05 15:23:54 +00001448
Douglas Gregor6eef5192009-12-14 19:27:10 +00001449 // Conversions by constructor and conversion functions have a
1450 // subexpression describing the call; strip it off.
John McCall2de56d12010-08-25 11:45:40 +00001451 if (E->getCastKind() == CK_ConstructorConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001452 SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0);
John McCall2de56d12010-08-25 11:45:40 +00001453 else if (E->getCastKind() == CK_UserDefinedConversion)
Douglas Gregor6eef5192009-12-14 19:27:10 +00001454 SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument();
Sean Huntc3021132010-05-05 15:23:54 +00001455
Douglas Gregor6eef5192009-12-14 19:27:10 +00001456 // If the subexpression we're left with is an implicit cast, look
1457 // through that, too.
Sean Huntc3021132010-05-05 15:23:54 +00001458 } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr)));
1459
Douglas Gregor6eef5192009-12-14 19:27:10 +00001460 return SubExpr;
1461}
1462
John McCallf871d0c2010-08-07 06:22:56 +00001463CXXBaseSpecifier **CastExpr::path_buffer() {
1464 switch (getStmtClass()) {
1465#define ABSTRACT_STMT(x)
1466#define CASTEXPR(Type, Base) \
1467 case Stmt::Type##Class: \
1468 return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1);
1469#define STMT(Type, Base)
1470#include "clang/AST/StmtNodes.inc"
1471 default:
1472 llvm_unreachable("non-cast expressions not possible here");
John McCallf871d0c2010-08-07 06:22:56 +00001473 }
1474}
1475
1476void CastExpr::setCastPath(const CXXCastPath &Path) {
1477 assert(Path.size() == path_size());
1478 memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*));
1479}
1480
1481ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T,
1482 CastKind Kind, Expr *Operand,
1483 const CXXCastPath *BasePath,
John McCall5baba9d2010-08-25 10:28:54 +00001484 ExprValueKind VK) {
John McCallf871d0c2010-08-07 06:22:56 +00001485 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1486 void *Buffer =
1487 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1488 ImplicitCastExpr *E =
John McCall5baba9d2010-08-25 10:28:54 +00001489 new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK);
John McCallf871d0c2010-08-07 06:22:56 +00001490 if (PathSize) E->setCastPath(*BasePath);
1491 return E;
1492}
1493
1494ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C,
1495 unsigned PathSize) {
1496 void *Buffer =
1497 C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1498 return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize);
1499}
1500
1501
1502CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00001503 ExprValueKind VK, CastKind K, Expr *Op,
John McCallf871d0c2010-08-07 06:22:56 +00001504 const CXXCastPath *BasePath,
1505 TypeSourceInfo *WrittenTy,
1506 SourceLocation L, SourceLocation R) {
1507 unsigned PathSize = (BasePath ? BasePath->size() : 0);
1508 void *Buffer =
1509 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1510 CStyleCastExpr *E =
John McCallf89e55a2010-11-18 06:31:45 +00001511 new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R);
John McCallf871d0c2010-08-07 06:22:56 +00001512 if (PathSize) E->setCastPath(*BasePath);
1513 return E;
1514}
1515
1516CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) {
1517 void *Buffer =
1518 C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
1519 return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize);
1520}
1521
Reid Spencer5f016e22007-07-11 17:01:13 +00001522/// getOpcodeStr - Turn an Opcode enum value into the punctuation char it
1523/// corresponds to, e.g. "<<=".
1524const char *BinaryOperator::getOpcodeStr(Opcode Op) {
1525 switch (Op) {
John McCall2de56d12010-08-25 11:45:40 +00001526 case BO_PtrMemD: return ".*";
1527 case BO_PtrMemI: return "->*";
1528 case BO_Mul: return "*";
1529 case BO_Div: return "/";
1530 case BO_Rem: return "%";
1531 case BO_Add: return "+";
1532 case BO_Sub: return "-";
1533 case BO_Shl: return "<<";
1534 case BO_Shr: return ">>";
1535 case BO_LT: return "<";
1536 case BO_GT: return ">";
1537 case BO_LE: return "<=";
1538 case BO_GE: return ">=";
1539 case BO_EQ: return "==";
1540 case BO_NE: return "!=";
1541 case BO_And: return "&";
1542 case BO_Xor: return "^";
1543 case BO_Or: return "|";
1544 case BO_LAnd: return "&&";
1545 case BO_LOr: return "||";
1546 case BO_Assign: return "=";
1547 case BO_MulAssign: return "*=";
1548 case BO_DivAssign: return "/=";
1549 case BO_RemAssign: return "%=";
1550 case BO_AddAssign: return "+=";
1551 case BO_SubAssign: return "-=";
1552 case BO_ShlAssign: return "<<=";
1553 case BO_ShrAssign: return ">>=";
1554 case BO_AndAssign: return "&=";
1555 case BO_XorAssign: return "^=";
1556 case BO_OrAssign: return "|=";
1557 case BO_Comma: return ",";
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 }
Douglas Gregorbaf53482009-03-12 22:51:37 +00001559
David Blaikie30263482012-01-20 21:50:17 +00001560 llvm_unreachable("Invalid OpCode!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001561}
1562
John McCall2de56d12010-08-25 11:45:40 +00001563BinaryOperatorKind
Douglas Gregor063daf62009-03-13 18:40:31 +00001564BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) {
1565 switch (OO) {
David Blaikieb219cfc2011-09-23 05:06:16 +00001566 default: llvm_unreachable("Not an overloadable binary operator");
John McCall2de56d12010-08-25 11:45:40 +00001567 case OO_Plus: return BO_Add;
1568 case OO_Minus: return BO_Sub;
1569 case OO_Star: return BO_Mul;
1570 case OO_Slash: return BO_Div;
1571 case OO_Percent: return BO_Rem;
1572 case OO_Caret: return BO_Xor;
1573 case OO_Amp: return BO_And;
1574 case OO_Pipe: return BO_Or;
1575 case OO_Equal: return BO_Assign;
1576 case OO_Less: return BO_LT;
1577 case OO_Greater: return BO_GT;
1578 case OO_PlusEqual: return BO_AddAssign;
1579 case OO_MinusEqual: return BO_SubAssign;
1580 case OO_StarEqual: return BO_MulAssign;
1581 case OO_SlashEqual: return BO_DivAssign;
1582 case OO_PercentEqual: return BO_RemAssign;
1583 case OO_CaretEqual: return BO_XorAssign;
1584 case OO_AmpEqual: return BO_AndAssign;
1585 case OO_PipeEqual: return BO_OrAssign;
1586 case OO_LessLess: return BO_Shl;
1587 case OO_GreaterGreater: return BO_Shr;
1588 case OO_LessLessEqual: return BO_ShlAssign;
1589 case OO_GreaterGreaterEqual: return BO_ShrAssign;
1590 case OO_EqualEqual: return BO_EQ;
1591 case OO_ExclaimEqual: return BO_NE;
1592 case OO_LessEqual: return BO_LE;
1593 case OO_GreaterEqual: return BO_GE;
1594 case OO_AmpAmp: return BO_LAnd;
1595 case OO_PipePipe: return BO_LOr;
1596 case OO_Comma: return BO_Comma;
1597 case OO_ArrowStar: return BO_PtrMemI;
Douglas Gregor063daf62009-03-13 18:40:31 +00001598 }
1599}
1600
1601OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) {
1602 static const OverloadedOperatorKind OverOps[] = {
1603 /* .* Cannot be overloaded */OO_None, OO_ArrowStar,
1604 OO_Star, OO_Slash, OO_Percent,
1605 OO_Plus, OO_Minus,
1606 OO_LessLess, OO_GreaterGreater,
1607 OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual,
1608 OO_EqualEqual, OO_ExclaimEqual,
1609 OO_Amp,
1610 OO_Caret,
1611 OO_Pipe,
1612 OO_AmpAmp,
1613 OO_PipePipe,
1614 OO_Equal, OO_StarEqual,
1615 OO_SlashEqual, OO_PercentEqual,
1616 OO_PlusEqual, OO_MinusEqual,
1617 OO_LessLessEqual, OO_GreaterGreaterEqual,
1618 OO_AmpEqual, OO_CaretEqual,
1619 OO_PipeEqual,
1620 OO_Comma
1621 };
1622 return OverOps[Opc];
1623}
1624
Ted Kremenek709210f2010-04-13 23:39:13 +00001625InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc,
Chris Lattner418f6c72008-10-26 23:43:26 +00001626 Expr **initExprs, unsigned numInits,
Douglas Gregor4c678342009-01-28 21:54:33 +00001627 SourceLocation rbraceloc)
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001628 : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false,
Douglas Gregor561f8122011-07-01 01:22:09 +00001629 false, false),
Ted Kremenek709210f2010-04-13 23:39:13 +00001630 InitExprs(C, numInits),
Sebastian Redl32cf1f22012-02-17 08:42:25 +00001631 LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0)
1632{
1633 sawArrayRangeDesignator(false);
1634 setInitializesStdInitializerList(false);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001635 for (unsigned I = 0; I != numInits; ++I) {
1636 if (initExprs[I]->isTypeDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001637 ExprBits.TypeDependent = true;
Ted Kremenekba7bc552010-02-19 01:50:18 +00001638 if (initExprs[I]->isValueDependent())
John McCall8e6285a2010-10-26 08:39:16 +00001639 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00001640 if (initExprs[I]->isInstantiationDependent())
1641 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00001642 if (initExprs[I]->containsUnexpandedParameterPack())
1643 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor73460a32009-11-19 23:25:22 +00001644 }
Sean Huntc3021132010-05-05 15:23:54 +00001645
Ted Kremenek709210f2010-04-13 23:39:13 +00001646 InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits);
Anders Carlsson66b5a8a2007-08-31 04:56:16 +00001647}
Reid Spencer5f016e22007-07-11 17:01:13 +00001648
Ted Kremenek709210f2010-04-13 23:39:13 +00001649void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001650 if (NumInits > InitExprs.size())
Ted Kremenek709210f2010-04-13 23:39:13 +00001651 InitExprs.reserve(C, NumInits);
Douglas Gregorfa219202009-03-20 23:58:33 +00001652}
1653
Ted Kremenek709210f2010-04-13 23:39:13 +00001654void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001655 InitExprs.resize(C, NumInits, 0);
Douglas Gregor4c678342009-01-28 21:54:33 +00001656}
1657
Ted Kremenek709210f2010-04-13 23:39:13 +00001658Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) {
Ted Kremenekba7bc552010-02-19 01:50:18 +00001659 if (Init >= InitExprs.size()) {
Ted Kremenek709210f2010-04-13 23:39:13 +00001660 InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0);
Ted Kremenekba7bc552010-02-19 01:50:18 +00001661 InitExprs.back() = expr;
1662 return 0;
Douglas Gregor4c678342009-01-28 21:54:33 +00001663 }
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Douglas Gregor4c678342009-01-28 21:54:33 +00001665 Expr *Result = cast_or_null<Expr>(InitExprs[Init]);
1666 InitExprs[Init] = expr;
1667 return Result;
1668}
1669
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001670void InitListExpr::setArrayFiller(Expr *filler) {
Argyrios Kyrtzidis21f77cd2011-10-21 23:02:22 +00001671 assert(!hasArrayFiller() && "Filler already set!");
Argyrios Kyrtzidis3e8dc2a2011-04-21 20:03:38 +00001672 ArrayFillerOrUnionFieldInit = filler;
1673 // Fill out any "holes" in the array due to designated initializers.
1674 Expr **inits = getInits();
1675 for (unsigned i = 0, e = getNumInits(); i != e; ++i)
1676 if (inits[i] == 0)
1677 inits[i] = filler;
1678}
1679
Richard Smithfe587202012-04-15 02:50:59 +00001680bool InitListExpr::isStringLiteralInit() const {
1681 if (getNumInits() != 1)
1682 return false;
1683 const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(getType());
1684 if (!CAT || !CAT->getElementType()->isIntegerType())
1685 return false;
1686 const Expr *Init = getInit(0)->IgnoreParenImpCasts();
1687 return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init);
1688}
1689
Ted Kremenekc4ba51f2010-11-09 02:11:40 +00001690SourceRange InitListExpr::getSourceRange() const {
1691 if (SyntacticForm)
1692 return SyntacticForm->getSourceRange();
1693 SourceLocation Beg = LBraceLoc, End = RBraceLoc;
1694 if (Beg.isInvalid()) {
1695 // Find the first non-null initializer.
1696 for (InitExprsTy::const_iterator I = InitExprs.begin(),
1697 E = InitExprs.end();
1698 I != E; ++I) {
1699 if (Stmt *S = *I) {
1700 Beg = S->getLocStart();
1701 break;
1702 }
1703 }
1704 }
1705 if (End.isInvalid()) {
1706 // Find the first non-null initializer from the end.
1707 for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(),
1708 E = InitExprs.rend();
1709 I != E; ++I) {
1710 if (Stmt *S = *I) {
1711 End = S->getSourceRange().getEnd();
1712 break;
1713 }
1714 }
1715 }
1716 return SourceRange(Beg, End);
1717}
1718
Steve Naroffbfdcae62008-09-04 15:31:07 +00001719/// getFunctionType - Return the underlying function type for this block.
Steve Naroff4eb206b2008-09-03 18:15:37 +00001720///
John McCalla345edb2012-02-17 03:32:35 +00001721const FunctionProtoType *BlockExpr::getFunctionType() const {
1722 // The block pointer is never sugared, but the function type might be.
1723 return cast<BlockPointerType>(getType())
1724 ->getPointeeType()->castAs<FunctionProtoType>();
Steve Naroff4eb206b2008-09-03 18:15:37 +00001725}
1726
Mike Stump1eb44332009-09-09 15:08:12 +00001727SourceLocation BlockExpr::getCaretLocation() const {
1728 return TheBlock->getCaretLocation();
Steve Naroff56ee6892008-10-08 17:01:13 +00001729}
Mike Stump1eb44332009-09-09 15:08:12 +00001730const Stmt *BlockExpr::getBody() const {
Douglas Gregor72971342009-04-18 00:02:19 +00001731 return TheBlock->getBody();
1732}
Mike Stump1eb44332009-09-09 15:08:12 +00001733Stmt *BlockExpr::getBody() {
1734 return TheBlock->getBody();
Douglas Gregor72971342009-04-18 00:02:19 +00001735}
Steve Naroff56ee6892008-10-08 17:01:13 +00001736
1737
Reid Spencer5f016e22007-07-11 17:01:13 +00001738//===----------------------------------------------------------------------===//
1739// Generic Expression Routines
1740//===----------------------------------------------------------------------===//
1741
Chris Lattner026dc962009-02-14 07:37:35 +00001742/// isUnusedResultAWarning - Return true if this immediate expression should
1743/// be warned about if the result is unused. If so, fill in Loc and Ranges
1744/// with location to warn on and the source range[s] to report with the
1745/// warning.
Eli Friedmana6115062012-05-24 00:47:05 +00001746bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc,
1747 SourceRange &R1, SourceRange &R2,
1748 ASTContext &Ctx) const {
Anders Carlssonffce2df2009-05-15 23:10:19 +00001749 // Don't warn if the expr is type dependent. The type could end up
1750 // instantiating to void.
1751 if (isTypeDependent())
1752 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001753
Reid Spencer5f016e22007-07-11 17:01:13 +00001754 switch (getStmtClass()) {
1755 default:
John McCall0faede62010-03-12 07:11:26 +00001756 if (getType()->isVoidType())
1757 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001758 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001759 Loc = getExprLoc();
1760 R1 = getSourceRange();
1761 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001762 case ParenExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001763 return cast<ParenExpr>(this)->getSubExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001764 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Peter Collingbournef111d932011-04-15 00:35:48 +00001765 case GenericSelectionExprClass:
1766 return cast<GenericSelectionExpr>(this)->getResultExpr()->
Eli Friedmana6115062012-05-24 00:47:05 +00001767 isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001768 case UnaryOperatorClass: {
1769 const UnaryOperator *UO = cast<UnaryOperator>(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001770
Reid Spencer5f016e22007-07-11 17:01:13 +00001771 switch (UO->getOpcode()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001772 case UO_Plus:
1773 case UO_Minus:
1774 case UO_AddrOf:
1775 case UO_Not:
1776 case UO_LNot:
1777 case UO_Deref:
1778 break;
John McCall2de56d12010-08-25 11:45:40 +00001779 case UO_PostInc:
1780 case UO_PostDec:
1781 case UO_PreInc:
1782 case UO_PreDec: // ++/--
Chris Lattner026dc962009-02-14 07:37:35 +00001783 return false; // Not a warning.
John McCall2de56d12010-08-25 11:45:40 +00001784 case UO_Real:
1785 case UO_Imag:
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 // accessing a piece of a volatile complex is a side-effect.
Mike Stumpdf317bf2009-11-03 23:25:48 +00001787 if (Ctx.getCanonicalType(UO->getSubExpr()->getType())
1788 .isVolatileQualified())
Chris Lattner026dc962009-02-14 07:37:35 +00001789 return false;
1790 break;
John McCall2de56d12010-08-25 11:45:40 +00001791 case UO_Extension:
Eli Friedmana6115062012-05-24 00:47:05 +00001792 return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Reid Spencer5f016e22007-07-11 17:01:13 +00001793 }
Eli Friedmana6115062012-05-24 00:47:05 +00001794 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001795 Loc = UO->getOperatorLoc();
1796 R1 = UO->getSubExpr()->getSourceRange();
1797 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 }
Chris Lattnere7716e62007-12-01 06:07:34 +00001799 case BinaryOperatorClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001800 const BinaryOperator *BO = cast<BinaryOperator>(this);
Ted Kremenekc46a2462010-04-07 18:49:21 +00001801 switch (BO->getOpcode()) {
1802 default:
1803 break;
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001804 // Consider the RHS of comma for side effects. LHS was checked by
1805 // Sema::CheckCommaOperands.
John McCall2de56d12010-08-25 11:45:40 +00001806 case BO_Comma:
Ted Kremenekc46a2462010-04-07 18:49:21 +00001807 // ((foo = <blah>), 0) is an idiom for hiding the result (and
1808 // lvalue-ness) of an assignment written in a macro.
1809 if (IntegerLiteral *IE =
1810 dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens()))
1811 if (IE->getValue() == 0)
1812 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001813 return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001814 // Consider '||', '&&' to have side effects if the LHS or RHS does.
John McCall2de56d12010-08-25 11:45:40 +00001815 case BO_LAnd:
1816 case BO_LOr:
Eli Friedmana6115062012-05-24 00:47:05 +00001817 if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) ||
1818 !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Argyrios Kyrtzidis25973452010-06-30 10:53:14 +00001819 return false;
1820 break;
John McCallbf0ee352010-02-16 04:10:53 +00001821 }
Chris Lattner026dc962009-02-14 07:37:35 +00001822 if (BO->isAssignmentOp())
1823 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001824 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001825 Loc = BO->getOperatorLoc();
1826 R1 = BO->getLHS()->getSourceRange();
1827 R2 = BO->getRHS()->getSourceRange();
1828 return true;
Chris Lattnere7716e62007-12-01 06:07:34 +00001829 }
Chris Lattnereb14fe82007-08-25 02:00:02 +00001830 case CompoundAssignOperatorClass:
Douglas Gregorc6dfe192010-05-08 22:41:50 +00001831 case VAArgExprClass:
Eli Friedman276b0612011-10-11 02:20:01 +00001832 case AtomicExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00001833 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001834
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001835 case ConditionalOperatorClass: {
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001836 // If only one of the LHS or RHS is a warning, the operator might
1837 // be being used for control flow. Only warn if both the LHS and
1838 // RHS are warnings.
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001839 const ConditionalOperator *Exp = cast<ConditionalOperator>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00001840 if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx))
Ted Kremenekfb7cb352011-03-01 20:34:48 +00001841 return false;
1842 if (!Exp->getLHS())
Chris Lattner026dc962009-02-14 07:37:35 +00001843 return true;
Eli Friedmana6115062012-05-24 00:47:05 +00001844 return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Fariborz Jahanianab38e4b2007-12-01 19:58:28 +00001845 }
1846
Reid Spencer5f016e22007-07-11 17:01:13 +00001847 case MemberExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001848 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001849 Loc = cast<MemberExpr>(this)->getMemberLoc();
1850 R1 = SourceRange(Loc, Loc);
1851 R2 = cast<MemberExpr>(this)->getBase()->getSourceRange();
1852 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001853
Reid Spencer5f016e22007-07-11 17:01:13 +00001854 case ArraySubscriptExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001855 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001856 Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc();
1857 R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange();
1858 R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange();
1859 return true;
Eli Friedman211f6ad2008-05-27 15:24:04 +00001860
Chandler Carruth9b106832011-08-17 09:49:44 +00001861 case CXXOperatorCallExprClass: {
1862 // We warn about operator== and operator!= even when user-defined operator
1863 // overloads as there is no reasonable way to define these such that they
1864 // have non-trivial, desirable side-effects. See the -Wunused-comparison
1865 // warning: these operators are commonly typo'ed, and so warning on them
1866 // provides additional value as well. If this list is updated,
1867 // DiagnoseUnusedComparison should be as well.
1868 const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this);
1869 if (Op->getOperator() == OO_EqualEqual ||
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001870 Op->getOperator() == OO_ExclaimEqual) {
Eli Friedmana6115062012-05-24 00:47:05 +00001871 WarnE = this;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001872 Loc = Op->getOperatorLoc();
1873 R1 = Op->getSourceRange();
Chandler Carruth9b106832011-08-17 09:49:44 +00001874 return true;
Matt Beaumont-Gay6e521832011-09-19 18:51:20 +00001875 }
Chandler Carruth9b106832011-08-17 09:49:44 +00001876
1877 // Fallthrough for generic call handling.
1878 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001879 case CallExprClass:
Richard Smith9fcce652012-03-07 08:35:16 +00001880 case CXXMemberCallExprClass:
1881 case UserDefinedLiteralClass: {
Chris Lattner026dc962009-02-14 07:37:35 +00001882 // If this is a direct call, get the callee.
1883 const CallExpr *CE = cast<CallExpr>(this);
Nuno Lopesd20254f2009-12-20 23:11:08 +00001884 if (const Decl *FD = CE->getCalleeDecl()) {
Chris Lattner026dc962009-02-14 07:37:35 +00001885 // If the callee has attribute pure, const, or warn_unused_result, warn
1886 // about it. void foo() { strlen("bar"); } should warn.
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001887 //
1888 // Note: If new cases are added here, DiagnoseUnusedExprResult should be
1889 // updated to match for QoI.
1890 if (FD->getAttr<WarnUnusedResultAttr>() ||
1891 FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001892 WarnE = this;
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001893 Loc = CE->getCallee()->getLocStart();
1894 R1 = CE->getCallee()->getSourceRange();
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Chris Lattnerbc8d42c2009-10-13 04:53:48 +00001896 if (unsigned NumArgs = CE->getNumArgs())
1897 R2 = SourceRange(CE->getArg(0)->getLocStart(),
1898 CE->getArg(NumArgs-1)->getLocEnd());
1899 return true;
1900 }
Chris Lattner026dc962009-02-14 07:37:35 +00001901 }
1902 return false;
1903 }
Anders Carlsson58beed92009-11-17 17:11:23 +00001904
1905 case CXXTemporaryObjectExprClass:
1906 case CXXConstructExprClass:
1907 return false;
1908
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001909 case ObjCMessageExprClass: {
1910 const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this);
David Blaikie4e4d0842012-03-11 07:00:24 +00001911 if (Ctx.getLangOpts().ObjCAutoRefCount &&
John McCallf85e1932011-06-15 23:02:42 +00001912 ME->isInstanceMessage() &&
1913 !ME->getType()->isVoidType() &&
1914 ME->getSelector().getIdentifierInfoForSlot(0) &&
1915 ME->getSelector().getIdentifierInfoForSlot(0)
1916 ->getName().startswith("init")) {
Eli Friedmana6115062012-05-24 00:47:05 +00001917 WarnE = this;
John McCallf85e1932011-06-15 23:02:42 +00001918 Loc = getExprLoc();
1919 R1 = ME->getSourceRange();
1920 return true;
1921 }
1922
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001923 const ObjCMethodDecl *MD = ME->getMethodDecl();
1924 if (MD && MD->getAttr<WarnUnusedResultAttr>()) {
Eli Friedmana6115062012-05-24 00:47:05 +00001925 WarnE = this;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001926 Loc = getExprLoc();
1927 return true;
1928 }
Chris Lattner026dc962009-02-14 07:37:35 +00001929 return false;
Fariborz Jahanianf0317742010-03-30 18:22:15 +00001930 }
Mike Stump1eb44332009-09-09 15:08:12 +00001931
John McCall12f78a62010-12-02 01:19:52 +00001932 case ObjCPropertyRefExprClass:
Eli Friedmana6115062012-05-24 00:47:05 +00001933 WarnE = this;
Chris Lattner5e94a0d2009-08-16 16:51:50 +00001934 Loc = getExprLoc();
1935 R1 = getSourceRange();
Chris Lattnera50089e2009-08-16 16:45:18 +00001936 return true;
John McCall12f78a62010-12-02 01:19:52 +00001937
John McCall4b9c2d22011-11-06 09:01:30 +00001938 case PseudoObjectExprClass: {
1939 const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this);
1940
1941 // Only complain about things that have the form of a getter.
1942 if (isa<UnaryOperator>(PO->getSyntacticForm()) ||
1943 isa<BinaryOperator>(PO->getSyntacticForm()))
1944 return false;
1945
Eli Friedmana6115062012-05-24 00:47:05 +00001946 WarnE = this;
John McCall4b9c2d22011-11-06 09:01:30 +00001947 Loc = getExprLoc();
1948 R1 = getSourceRange();
1949 return true;
1950 }
1951
Chris Lattner611b2ec2008-07-26 19:51:01 +00001952 case StmtExprClass: {
1953 // Statement exprs don't logically have side effects themselves, but are
1954 // sometimes used in macros in ways that give them a type that is unused.
1955 // For example ({ blah; foo(); }) will end up with a type if foo has a type.
1956 // however, if the result of the stmt expr is dead, we don't want to emit a
1957 // warning.
1958 const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt();
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001959 if (!CS->body_empty()) {
Chris Lattner611b2ec2008-07-26 19:51:01 +00001960 if (const Expr *E = dyn_cast<Expr>(CS->body_back()))
Eli Friedmana6115062012-05-24 00:47:05 +00001961 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001962 if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back()))
1963 if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt()))
Eli Friedmana6115062012-05-24 00:47:05 +00001964 return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Argyrios Kyrtzidisd2827af2010-09-19 21:21:10 +00001965 }
Mike Stump1eb44332009-09-09 15:08:12 +00001966
John McCall0faede62010-03-12 07:11:26 +00001967 if (getType()->isVoidType())
1968 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001969 WarnE = this;
Chris Lattner026dc962009-02-14 07:37:35 +00001970 Loc = cast<StmtExpr>(this)->getLParenLoc();
1971 R1 = getSourceRange();
1972 return true;
Chris Lattner611b2ec2008-07-26 19:51:01 +00001973 }
Eli Friedmana6115062012-05-24 00:47:05 +00001974 case CStyleCastExprClass: {
Eli Friedman4059da82012-05-24 21:05:41 +00001975 // Ignore an explicit cast to void unless the operand is a non-trivial
Eli Friedmana6115062012-05-24 00:47:05 +00001976 // volatile lvalue.
Eli Friedman4059da82012-05-24 21:05:41 +00001977 const CastExpr *CE = cast<CastExpr>(this);
Eli Friedmana6115062012-05-24 00:47:05 +00001978 if (CE->getCastKind() == CK_ToVoid) {
1979 if (CE->getSubExpr()->isGLValue() &&
Eli Friedman4059da82012-05-24 21:05:41 +00001980 CE->getSubExpr()->getType().isVolatileQualified()) {
1981 const DeclRefExpr *DRE =
1982 dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens());
1983 if (!(DRE && isa<VarDecl>(DRE->getDecl()) &&
1984 cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) {
1985 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc,
1986 R1, R2, Ctx);
1987 }
1988 }
Chris Lattnerfb846642009-07-28 18:25:28 +00001989 return false;
Eli Friedmana6115062012-05-24 00:47:05 +00001990 }
Eli Friedman4059da82012-05-24 21:05:41 +00001991
Eli Friedmana6115062012-05-24 00:47:05 +00001992 // If this is a cast to a constructor conversion, check the operand.
Anders Carlsson58beed92009-11-17 17:11:23 +00001993 // Otherwise, the result of the cast is unused.
Eli Friedmana6115062012-05-24 00:47:05 +00001994 if (CE->getCastKind() == CK_ConstructorConversion)
1995 return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
Eli Friedman4059da82012-05-24 21:05:41 +00001996
Eli Friedmana6115062012-05-24 00:47:05 +00001997 WarnE = this;
Eli Friedman4059da82012-05-24 21:05:41 +00001998 if (const CXXFunctionalCastExpr *CXXCE =
1999 dyn_cast<CXXFunctionalCastExpr>(this)) {
2000 Loc = CXXCE->getTypeBeginLoc();
2001 R1 = CXXCE->getSubExpr()->getSourceRange();
2002 } else {
2003 const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this);
2004 Loc = CStyleCE->getLParenLoc();
2005 R1 = CStyleCE->getSubExpr()->getSourceRange();
2006 }
Chris Lattner026dc962009-02-14 07:37:35 +00002007 return true;
Anders Carlsson58beed92009-11-17 17:11:23 +00002008 }
Eli Friedmana6115062012-05-24 00:47:05 +00002009 case ImplicitCastExprClass: {
2010 const CastExpr *ICE = cast<ImplicitCastExpr>(this);
Eli Friedman4be1f472008-05-19 21:24:43 +00002011
Eli Friedmana6115062012-05-24 00:47:05 +00002012 // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect.
2013 if (ICE->getCastKind() == CK_LValueToRValue &&
2014 ICE->getSubExpr()->getType().isVolatileQualified())
2015 return false;
2016
2017 return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx);
2018 }
Chris Lattner04421082008-04-08 04:40:51 +00002019 case CXXDefaultArgExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002020 return (cast<CXXDefaultArgExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002021 ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002022
2023 case CXXNewExprClass:
2024 // FIXME: In theory, there might be new expressions that don't have side
2025 // effects (e.g. a placement new with an uninitialized POD).
2026 case CXXDeleteExprClass:
Chris Lattner026dc962009-02-14 07:37:35 +00002027 return false;
Anders Carlsson2d46eb22009-08-16 04:11:06 +00002028 case CXXBindTemporaryExprClass:
Mike Stumpdf317bf2009-11-03 23:25:48 +00002029 return (cast<CXXBindTemporaryExpr>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002030 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
John McCall4765fa02010-12-06 08:20:24 +00002031 case ExprWithCleanupsClass:
2032 return (cast<ExprWithCleanups>(this)
Eli Friedmana6115062012-05-24 00:47:05 +00002033 ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx));
Sebastian Redl4c5d3202008-11-21 19:14:01 +00002034 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002035}
2036
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002037/// isOBJCGCCandidate - Check if an expression is objc gc'able.
Fariborz Jahanian7f4f86a2009-09-08 23:38:54 +00002038/// returns true, if it is; false otherwise.
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002039bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const {
Peter Collingbournef111d932011-04-15 00:35:48 +00002040 const Expr *E = IgnoreParens();
2041 switch (E->getStmtClass()) {
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002042 default:
2043 return false;
2044 case ObjCIvarRefExprClass:
2045 return true;
Fariborz Jahanian207c5212009-02-23 18:59:50 +00002046 case Expr::UnaryOperatorClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002047 return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002048 case ImplicitCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002049 return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregor03e80032011-06-21 17:03:29 +00002050 case MaterializeTemporaryExprClass:
2051 return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr()
2052 ->isOBJCGCCandidate(Ctx);
Fariborz Jahanian06b89122009-05-05 23:28:21 +00002053 case CStyleCastExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002054 return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx);
Douglas Gregora2813ce2009-10-23 18:54:35 +00002055 case DeclRefExprClass: {
John McCallf4b88a42012-03-10 09:33:50 +00002056 const Decl *D = cast<DeclRefExpr>(E)->getDecl();
Fariborz Jahaniane3f83492011-09-23 18:57:30 +00002057
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002058 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
2059 if (VD->hasGlobalStorage())
2060 return true;
2061 QualType T = VD->getType();
Fariborz Jahanian59a53fa2009-09-16 18:09:18 +00002062 // dereferencing to a pointer is always a gc'able candidate,
2063 // unless it is __weak.
Daniel Dunbar7e88a602009-09-17 06:31:17 +00002064 return T->isPointerType() &&
John McCall0953e762009-09-24 19:53:00 +00002065 (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002066 }
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002067 return false;
2068 }
Douglas Gregor83f6faf2009-08-31 23:41:50 +00002069 case MemberExprClass: {
Peter Collingbournef111d932011-04-15 00:35:48 +00002070 const MemberExpr *M = cast<MemberExpr>(E);
Fariborz Jahanian102e3902009-06-01 21:29:32 +00002071 return M->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002072 }
2073 case ArraySubscriptExprClass:
Peter Collingbournef111d932011-04-15 00:35:48 +00002074 return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx);
Fariborz Jahanian44baa8a2009-02-22 18:40:18 +00002075 }
2076}
Sebastian Redl369e51f2010-09-10 20:55:33 +00002077
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002078bool Expr::isBoundMemberFunction(ASTContext &Ctx) const {
2079 if (isTypeDependent())
2080 return false;
John McCall7eb0a9e2010-11-24 05:12:34 +00002081 return ClassifyLValue(Ctx) == Expr::LV_MemberFunction;
Argyrios Kyrtzidis11ab7902010-11-01 18:49:26 +00002082}
2083
John McCall864c0412011-04-26 20:42:42 +00002084QualType Expr::findBoundMemberType(const Expr *expr) {
John McCalle0a22d02011-10-18 21:02:43 +00002085 assert(expr->hasPlaceholderType(BuiltinType::BoundMember));
John McCall864c0412011-04-26 20:42:42 +00002086
2087 // Bound member expressions are always one of these possibilities:
2088 // x->m x.m x->*y x.*y
2089 // (possibly parenthesized)
2090
2091 expr = expr->IgnoreParens();
2092 if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) {
2093 assert(isa<CXXMethodDecl>(mem->getMemberDecl()));
2094 return mem->getMemberDecl()->getType();
2095 }
2096
2097 if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) {
2098 QualType type = op->getRHS()->getType()->castAs<MemberPointerType>()
2099 ->getPointeeType();
2100 assert(type->isFunctionType());
2101 return type;
2102 }
2103
2104 assert(isa<UnresolvedMemberExpr>(expr));
2105 return QualType();
2106}
2107
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002108Expr* Expr::IgnoreParens() {
2109 Expr* E = this;
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002110 while (true) {
2111 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
2112 E = P->getSubExpr();
2113 continue;
2114 }
2115 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2116 if (P->getOpcode() == UO_Extension) {
2117 E = P->getSubExpr();
2118 continue;
2119 }
2120 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002121 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2122 if (!P->isResultDependent()) {
2123 E = P->getResultExpr();
2124 continue;
2125 }
2126 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002127 return E;
2128 }
Ted Kremenek4e99a5f2008-01-17 16:57:34 +00002129}
2130
Chris Lattner56f34942008-02-13 01:02:39 +00002131/// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr
2132/// or CastExprs or ImplicitCastExprs, returning their operand.
2133Expr *Expr::IgnoreParenCasts() {
2134 Expr *E = this;
2135 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002136 if (ParenExpr* P = dyn_cast<ParenExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002137 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002138 continue;
2139 }
2140 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
Chris Lattner56f34942008-02-13 01:02:39 +00002141 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002142 continue;
2143 }
2144 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2145 if (P->getOpcode() == UO_Extension) {
2146 E = P->getSubExpr();
2147 continue;
2148 }
2149 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002150 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2151 if (!P->isResultDependent()) {
2152 E = P->getResultExpr();
2153 continue;
2154 }
2155 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002156 if (MaterializeTemporaryExpr *Materialize
2157 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2158 E = Materialize->GetTemporaryExpr();
2159 continue;
2160 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002161 if (SubstNonTypeTemplateParmExpr *NTTP
2162 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2163 E = NTTP->getReplacement();
2164 continue;
2165 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002166 return E;
Chris Lattner56f34942008-02-13 01:02:39 +00002167 }
2168}
2169
John McCall9c5d70c2010-12-04 08:24:19 +00002170/// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue
2171/// casts. This is intended purely as a temporary workaround for code
2172/// that hasn't yet been rewritten to do the right thing about those
2173/// casts, and may disappear along with the last internal use.
John McCallf6a16482010-12-04 03:47:34 +00002174Expr *Expr::IgnoreParenLValueCasts() {
2175 Expr *E = this;
John McCall9c5d70c2010-12-04 08:24:19 +00002176 while (true) {
John McCallf6a16482010-12-04 03:47:34 +00002177 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2178 E = P->getSubExpr();
2179 continue;
John McCall9c5d70c2010-12-04 08:24:19 +00002180 } else if (CastExpr *P = dyn_cast<CastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002181 if (P->getCastKind() == CK_LValueToRValue) {
2182 E = P->getSubExpr();
2183 continue;
2184 }
John McCall9c5d70c2010-12-04 08:24:19 +00002185 } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2186 if (P->getOpcode() == UO_Extension) {
2187 E = P->getSubExpr();
2188 continue;
2189 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002190 } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2191 if (!P->isResultDependent()) {
2192 E = P->getResultExpr();
2193 continue;
2194 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002195 } else if (MaterializeTemporaryExpr *Materialize
2196 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2197 E = Materialize->GetTemporaryExpr();
2198 continue;
Douglas Gregorc0244c52011-09-08 17:56:33 +00002199 } else if (SubstNonTypeTemplateParmExpr *NTTP
2200 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2201 E = NTTP->getReplacement();
2202 continue;
John McCallf6a16482010-12-04 03:47:34 +00002203 }
2204 break;
2205 }
2206 return E;
2207}
2208
John McCall2fc46bf2010-05-05 22:59:52 +00002209Expr *Expr::IgnoreParenImpCasts() {
2210 Expr *E = this;
2211 while (true) {
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002212 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002213 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002214 continue;
2215 }
2216 if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2fc46bf2010-05-05 22:59:52 +00002217 E = P->getSubExpr();
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002218 continue;
2219 }
2220 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2221 if (P->getOpcode() == UO_Extension) {
2222 E = P->getSubExpr();
2223 continue;
2224 }
2225 }
Peter Collingbournef111d932011-04-15 00:35:48 +00002226 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2227 if (!P->isResultDependent()) {
2228 E = P->getResultExpr();
2229 continue;
2230 }
2231 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002232 if (MaterializeTemporaryExpr *Materialize
2233 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2234 E = Materialize->GetTemporaryExpr();
2235 continue;
2236 }
Douglas Gregorc0244c52011-09-08 17:56:33 +00002237 if (SubstNonTypeTemplateParmExpr *NTTP
2238 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2239 E = NTTP->getReplacement();
2240 continue;
2241 }
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002242 return E;
John McCall2fc46bf2010-05-05 22:59:52 +00002243 }
2244}
2245
Hans Wennborg2f072b42011-06-09 17:06:51 +00002246Expr *Expr::IgnoreConversionOperator() {
2247 if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) {
Chandler Carruth14d251c2011-06-21 17:22:09 +00002248 if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl()))
Hans Wennborg2f072b42011-06-09 17:06:51 +00002249 return MCE->getImplicitObjectArgument();
2250 }
2251 return this;
2252}
2253
Chris Lattnerecdd8412009-03-13 17:28:01 +00002254/// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the
2255/// value (including ptr->int casts of the same size). Strip off any
2256/// ParenExpr or CastExprs, returning their operand.
2257Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) {
2258 Expr *E = this;
2259 while (true) {
2260 if (ParenExpr *P = dyn_cast<ParenExpr>(E)) {
2261 E = P->getSubExpr();
2262 continue;
2263 }
Mike Stump1eb44332009-09-09 15:08:12 +00002264
Chris Lattnerecdd8412009-03-13 17:28:01 +00002265 if (CastExpr *P = dyn_cast<CastExpr>(E)) {
2266 // We ignore integer <-> casts that are of the same width, ptr<->ptr and
Douglas Gregor2ade35e2010-06-16 00:17:44 +00002267 // ptr<->int casts of the same width. We also ignore all identity casts.
Chris Lattnerecdd8412009-03-13 17:28:01 +00002268 Expr *SE = P->getSubExpr();
Mike Stump1eb44332009-09-09 15:08:12 +00002269
Chris Lattnerecdd8412009-03-13 17:28:01 +00002270 if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) {
2271 E = SE;
2272 continue;
2273 }
Mike Stump1eb44332009-09-09 15:08:12 +00002274
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002275 if ((E->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002276 E->getType()->isIntegralType(Ctx)) &&
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002277 (SE->getType()->isPointerType() ||
Douglas Gregor9d3347a2010-06-16 00:35:25 +00002278 SE->getType()->isIntegralType(Ctx)) &&
Chris Lattnerecdd8412009-03-13 17:28:01 +00002279 Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) {
2280 E = SE;
2281 continue;
2282 }
2283 }
Mike Stump1eb44332009-09-09 15:08:12 +00002284
Abramo Bagnarab9eb35c2010-10-15 07:51:18 +00002285 if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) {
2286 if (P->getOpcode() == UO_Extension) {
2287 E = P->getSubExpr();
2288 continue;
2289 }
2290 }
2291
Peter Collingbournef111d932011-04-15 00:35:48 +00002292 if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) {
2293 if (!P->isResultDependent()) {
2294 E = P->getResultExpr();
2295 continue;
2296 }
2297 }
2298
Douglas Gregorc0244c52011-09-08 17:56:33 +00002299 if (SubstNonTypeTemplateParmExpr *NTTP
2300 = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) {
2301 E = NTTP->getReplacement();
2302 continue;
2303 }
2304
Chris Lattnerecdd8412009-03-13 17:28:01 +00002305 return E;
2306 }
2307}
2308
Douglas Gregor6eef5192009-12-14 19:27:10 +00002309bool Expr::isDefaultArgument() const {
2310 const Expr *E = this;
Douglas Gregor03e80032011-06-21 17:03:29 +00002311 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2312 E = M->GetTemporaryExpr();
2313
Douglas Gregor6eef5192009-12-14 19:27:10 +00002314 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
2315 E = ICE->getSubExprAsWritten();
Sean Huntc3021132010-05-05 15:23:54 +00002316
Douglas Gregor6eef5192009-12-14 19:27:10 +00002317 return isa<CXXDefaultArgExpr>(E);
2318}
Chris Lattnerecdd8412009-03-13 17:28:01 +00002319
Douglas Gregor2f599792010-04-02 18:24:57 +00002320/// \brief Skip over any no-op casts and any temporary-binding
2321/// expressions.
Anders Carlssonf8b30152010-11-28 16:40:49 +00002322static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) {
Douglas Gregor03e80032011-06-21 17:03:29 +00002323 if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E))
2324 E = M->GetTemporaryExpr();
2325
Douglas Gregor2f599792010-04-02 18:24:57 +00002326 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002327 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002328 E = ICE->getSubExpr();
2329 else
2330 break;
2331 }
2332
2333 while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E))
2334 E = BE->getSubExpr();
2335
2336 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall2de56d12010-08-25 11:45:40 +00002337 if (ICE->getCastKind() == CK_NoOp)
Douglas Gregor2f599792010-04-02 18:24:57 +00002338 E = ICE->getSubExpr();
2339 else
2340 break;
2341 }
Anders Carlssonf8b30152010-11-28 16:40:49 +00002342
2343 return E->IgnoreParens();
Douglas Gregor2f599792010-04-02 18:24:57 +00002344}
2345
John McCall558d2ab2010-09-15 10:14:12 +00002346/// isTemporaryObject - Determines if this expression produces a
2347/// temporary of the given class type.
2348bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const {
2349 if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy)))
2350 return false;
2351
Anders Carlssonf8b30152010-11-28 16:40:49 +00002352 const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this);
Douglas Gregor2f599792010-04-02 18:24:57 +00002353
John McCall58277b52010-09-15 20:59:13 +00002354 // Temporaries are by definition pr-values of class type.
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002355 if (!E->Classify(C).isPRValue()) {
2356 // In this context, property reference is a message call and is pr-value.
John McCall12f78a62010-12-02 01:19:52 +00002357 if (!isa<ObjCPropertyRefExpr>(E))
Fariborz Jahaniandb148be2010-09-27 17:30:38 +00002358 return false;
2359 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002360
John McCall19e60ad2010-09-16 06:57:56 +00002361 // Black-list a few cases which yield pr-values of class type that don't
2362 // refer to temporaries of that type:
2363
2364 // - implicit derived-to-base conversions
John McCall558d2ab2010-09-15 10:14:12 +00002365 if (isa<ImplicitCastExpr>(E)) {
2366 switch (cast<ImplicitCastExpr>(E)->getCastKind()) {
2367 case CK_DerivedToBase:
2368 case CK_UncheckedDerivedToBase:
2369 return false;
2370 default:
2371 break;
2372 }
Douglas Gregor2f599792010-04-02 18:24:57 +00002373 }
2374
John McCall19e60ad2010-09-16 06:57:56 +00002375 // - member expressions (all)
2376 if (isa<MemberExpr>(E))
2377 return false;
2378
Eli Friedman32f498a2012-06-15 23:51:06 +00002379 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E))
2380 if (BO->isPtrMemOp())
2381 return false;
2382
John McCall56ca35d2011-02-17 10:25:35 +00002383 // - opaque values (all)
2384 if (isa<OpaqueValueExpr>(E))
2385 return false;
2386
John McCall558d2ab2010-09-15 10:14:12 +00002387 return true;
Douglas Gregor2f599792010-04-02 18:24:57 +00002388}
2389
Douglas Gregor75e85042011-03-02 21:06:53 +00002390bool Expr::isImplicitCXXThis() const {
2391 const Expr *E = this;
2392
2393 // Strip away parentheses and casts we don't care about.
2394 while (true) {
2395 if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) {
2396 E = Paren->getSubExpr();
2397 continue;
2398 }
2399
2400 if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
2401 if (ICE->getCastKind() == CK_NoOp ||
2402 ICE->getCastKind() == CK_LValueToRValue ||
2403 ICE->getCastKind() == CK_DerivedToBase ||
2404 ICE->getCastKind() == CK_UncheckedDerivedToBase) {
2405 E = ICE->getSubExpr();
2406 continue;
2407 }
2408 }
2409
2410 if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) {
2411 if (UnOp->getOpcode() == UO_Extension) {
2412 E = UnOp->getSubExpr();
2413 continue;
2414 }
2415 }
2416
Douglas Gregor03e80032011-06-21 17:03:29 +00002417 if (const MaterializeTemporaryExpr *M
2418 = dyn_cast<MaterializeTemporaryExpr>(E)) {
2419 E = M->GetTemporaryExpr();
2420 continue;
2421 }
2422
Douglas Gregor75e85042011-03-02 21:06:53 +00002423 break;
2424 }
2425
2426 if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E))
2427 return This->isImplicit();
2428
2429 return false;
2430}
2431
Douglas Gregor898574e2008-12-05 23:32:09 +00002432/// hasAnyTypeDependentArguments - Determines if any of the expressions
2433/// in Exprs is type-dependent.
Ahmed Charles13a140c2012-02-25 11:00:22 +00002434bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) {
2435 for (unsigned I = 0; I < Exprs.size(); ++I)
Douglas Gregor898574e2008-12-05 23:32:09 +00002436 if (Exprs[I]->isTypeDependent())
2437 return true;
2438
2439 return false;
2440}
2441
John McCall4204f072010-08-02 21:13:48 +00002442bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002443 // This function is attempting whether an expression is an initializer
2444 // which can be evaluated at compile-time. isEvaluatable handles most
2445 // of the cases, but it can't deal with some initializer-specific
2446 // expressions, and it can't deal with aggregates; we deal with those here,
2447 // and fall back to isEvaluatable for the other cases.
2448
John McCall4204f072010-08-02 21:13:48 +00002449 // If we ever capture reference-binding directly in the AST, we can
2450 // kill the second parameter.
2451
2452 if (IsForRef) {
2453 EvalResult Result;
2454 return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects;
2455 }
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002456
Anders Carlssone8a32b82008-11-24 05:23:59 +00002457 switch (getStmtClass()) {
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002458 default: break;
Richard Smith4ec40892011-12-09 06:47:34 +00002459 case IntegerLiteralClass:
2460 case FloatingLiteralClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002461 case StringLiteralClass:
Steve Naroff14108da2009-07-10 23:34:53 +00002462 case ObjCStringLiteralClass:
Chris Lattnereaf2bb82009-02-24 22:18:39 +00002463 case ObjCEncodeExprClass:
Anders Carlssone8a32b82008-11-24 05:23:59 +00002464 return true;
John McCallb4b9b152010-08-01 21:51:45 +00002465 case CXXTemporaryObjectExprClass:
2466 case CXXConstructExprClass: {
2467 const CXXConstructExpr *CE = cast<CXXConstructExpr>(this);
John McCall4204f072010-08-02 21:13:48 +00002468
2469 // Only if it's
Richard Smith180f4792011-11-10 06:34:14 +00002470 if (CE->getConstructor()->isTrivial()) {
2471 // 1) an application of the trivial default constructor or
2472 if (!CE->getNumArgs()) return true;
John McCall4204f072010-08-02 21:13:48 +00002473
Richard Smith180f4792011-11-10 06:34:14 +00002474 // 2) an elidable trivial copy construction of an operand which is
2475 // itself a constant initializer. Note that we consider the
2476 // operand on its own, *not* as a reference binding.
2477 if (CE->isElidable() &&
2478 CE->getArg(0)->isConstantInitializer(Ctx, false))
2479 return true;
2480 }
2481
2482 // 3) a foldable constexpr constructor.
2483 break;
John McCallb4b9b152010-08-01 21:51:45 +00002484 }
Nate Begeman59b5da62009-01-18 03:20:47 +00002485 case CompoundLiteralExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002486 // This handles gcc's extension that allows global initializers like
2487 // "struct x {int x;} x = (struct x) {};".
2488 // FIXME: This accepts other cases it shouldn't!
Nate Begeman59b5da62009-01-18 03:20:47 +00002489 const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
John McCall4204f072010-08-02 21:13:48 +00002490 return Exp->isConstantInitializer(Ctx, false);
Nate Begeman59b5da62009-01-18 03:20:47 +00002491 }
Anders Carlssone8a32b82008-11-24 05:23:59 +00002492 case InitListExprClass: {
Eli Friedman1f4a6db2009-02-20 02:36:22 +00002493 // FIXME: This doesn't deal with fields with reference types correctly.
2494 // FIXME: This incorrectly allows pointers cast to integers to be assigned
2495 // to bitfields.
Anders Carlssone8a32b82008-11-24 05:23:59 +00002496 const InitListExpr *Exp = cast<InitListExpr>(this);
2497 unsigned numInits = Exp->getNumInits();
2498 for (unsigned i = 0; i < numInits; i++) {
John McCall4204f072010-08-02 21:13:48 +00002499 if (!Exp->getInit(i)->isConstantInitializer(Ctx, false))
Anders Carlssone8a32b82008-11-24 05:23:59 +00002500 return false;
2501 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002502 return true;
Anders Carlssone8a32b82008-11-24 05:23:59 +00002503 }
Douglas Gregor3498bdb2009-01-29 17:44:32 +00002504 case ImplicitValueInitExprClass:
2505 return true;
Chris Lattner3ae9f482009-10-13 07:14:16 +00002506 case ParenExprClass:
John McCall4204f072010-08-02 21:13:48 +00002507 return cast<ParenExpr>(this)->getSubExpr()
2508 ->isConstantInitializer(Ctx, IsForRef);
Peter Collingbournef111d932011-04-15 00:35:48 +00002509 case GenericSelectionExprClass:
2510 if (cast<GenericSelectionExpr>(this)->isResultDependent())
2511 return false;
2512 return cast<GenericSelectionExpr>(this)->getResultExpr()
2513 ->isConstantInitializer(Ctx, IsForRef);
Abramo Bagnara5cadfab2010-09-27 07:13:32 +00002514 case ChooseExprClass:
2515 return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)
2516 ->isConstantInitializer(Ctx, IsForRef);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002517 case UnaryOperatorClass: {
2518 const UnaryOperator* Exp = cast<UnaryOperator>(this);
John McCall2de56d12010-08-25 11:45:40 +00002519 if (Exp->getOpcode() == UO_Extension)
John McCall4204f072010-08-02 21:13:48 +00002520 return Exp->getSubExpr()->isConstantInitializer(Ctx, false);
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002521 break;
2522 }
John McCall4204f072010-08-02 21:13:48 +00002523 case CXXFunctionalCastExprClass:
John McCallb4b9b152010-08-01 21:51:45 +00002524 case CXXStaticCastExprClass:
Chris Lattner81045d82009-04-21 05:19:11 +00002525 case ImplicitCastExprClass:
Richard Smithd62ca372011-12-06 22:44:34 +00002526 case CStyleCastExprClass: {
2527 const CastExpr *CE = cast<CastExpr>(this);
2528
David Chisnall7a7ee302012-01-16 17:27:18 +00002529 // If we're promoting an integer to an _Atomic type then this is constant
2530 // if the integer is constant. We also need to check the converse in case
2531 // someone does something like:
2532 //
2533 // int a = (_Atomic(int))42;
2534 //
2535 // I doubt anyone would write code like this directly, but it's quite
2536 // possible as the result of macro expansions.
2537 if (CE->getCastKind() == CK_NonAtomicToAtomic ||
2538 CE->getCastKind() == CK_AtomicToNonAtomic)
2539 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2540
Richard Smithd62ca372011-12-06 22:44:34 +00002541 // Handle bitcasts of vector constants.
2542 if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast)
2543 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2544
Eli Friedman6bd97192011-12-21 00:43:02 +00002545 // Handle misc casts we want to ignore.
2546 // FIXME: Is it really safe to ignore all these?
2547 if (CE->getCastKind() == CK_NoOp ||
2548 CE->getCastKind() == CK_LValueToRValue ||
2549 CE->getCastKind() == CK_ToUnion ||
2550 CE->getCastKind() == CK_ConstructorConversion)
Richard Smithd62ca372011-12-06 22:44:34 +00002551 return CE->getSubExpr()->isConstantInitializer(Ctx, false);
2552
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002553 break;
Richard Smithd62ca372011-12-06 22:44:34 +00002554 }
Douglas Gregor03e80032011-06-21 17:03:29 +00002555 case MaterializeTemporaryExprClass:
Chris Lattner5f9e2722011-07-23 10:55:15 +00002556 return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr()
Douglas Gregor03e80032011-06-21 17:03:29 +00002557 ->isConstantInitializer(Ctx, false);
Anders Carlssone8a32b82008-11-24 05:23:59 +00002558 }
Eli Friedmanc39dc9a2009-01-25 03:12:18 +00002559 return isEvaluatable(Ctx);
Steve Naroff38374b02007-09-02 20:30:18 +00002560}
2561
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002562namespace {
2563 /// \brief Look for a call to a non-trivial function within an expression.
2564 class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder>
2565 {
2566 typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited;
2567
2568 bool NonTrivial;
2569
2570 public:
2571 explicit NonTrivialCallFinder(ASTContext &Context)
Douglas Gregorb11e5252012-02-23 07:44:18 +00002572 : Inherited(Context), NonTrivial(false) { }
Douglas Gregor25d0a0f2012-02-23 07:33:15 +00002573
2574 bool hasNonTrivialCall() const { return NonTrivial; }
2575
2576 void VisitCallExpr(CallExpr *E) {
2577 if (CXXMethodDecl *Method
2578 = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) {
2579 if (Method->isTrivial()) {
2580 // Recurse to children of the call.
2581 Inherited::VisitStmt(E);
2582 return;
2583 }
2584 }
2585
2586 NonTrivial = true;
2587 }
2588
2589 void VisitCXXConstructExpr(CXXConstructExpr *E) {
2590 if (E->getConstructor()->isTrivial()) {
2591 // Recurse to children of the call.
2592 Inherited::VisitStmt(E);
2593 return;
2594 }
2595
2596 NonTrivial = true;
2597 }
2598
2599 void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2600 if (E->getTemporary()->getDestructor()->isTrivial()) {
2601 Inherited::VisitStmt(E);
2602 return;
2603 }
2604
2605 NonTrivial = true;
2606 }
2607 };
2608}
2609
2610bool Expr::hasNonTrivialCall(ASTContext &Ctx) {
2611 NonTrivialCallFinder Finder(Ctx);
2612 Finder.Visit(this);
2613 return Finder.hasNonTrivialCall();
2614}
2615
Chandler Carruth82214a82011-02-18 23:54:50 +00002616/// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null
2617/// pointer constant or not, as well as the specific kind of constant detected.
2618/// Null pointer constants can be integer constant expressions with the
2619/// value zero, casts of zero to void*, nullptr (C++0X), or __null
2620/// (a GNU extension).
2621Expr::NullPointerConstantKind
2622Expr::isNullPointerConstant(ASTContext &Ctx,
2623 NullPointerConstantValueDependence NPC) const {
Douglas Gregorce940492009-09-25 04:25:58 +00002624 if (isValueDependent()) {
2625 switch (NPC) {
2626 case NPC_NeverValueDependent:
David Blaikieb219cfc2011-09-23 05:06:16 +00002627 llvm_unreachable("Unexpected value dependent expression!");
Douglas Gregorce940492009-09-25 04:25:58 +00002628 case NPC_ValueDependentIsNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002629 if (isTypeDependent() || getType()->isIntegralType(Ctx))
2630 return NPCK_ZeroInteger;
2631 else
2632 return NPCK_NotNull;
Sean Huntc3021132010-05-05 15:23:54 +00002633
Douglas Gregorce940492009-09-25 04:25:58 +00002634 case NPC_ValueDependentIsNotNull:
Chandler Carruth82214a82011-02-18 23:54:50 +00002635 return NPCK_NotNull;
Douglas Gregorce940492009-09-25 04:25:58 +00002636 }
2637 }
Daniel Dunbarf515b222009-09-18 08:46:16 +00002638
Sebastian Redl07779722008-10-31 14:43:28 +00002639 // Strip off a cast to void*, if it exists. Except in C++.
Argyrios Kyrtzidis0835a3c2008-08-18 23:01:59 +00002640 if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00002641 if (!Ctx.getLangOpts().CPlusPlus) {
Sebastian Redl07779722008-10-31 14:43:28 +00002642 // Check that it is a cast to void*.
Ted Kremenek6217b802009-07-29 21:53:49 +00002643 if (const PointerType *PT = CE->getType()->getAs<PointerType>()) {
Sebastian Redl07779722008-10-31 14:43:28 +00002644 QualType Pointee = PT->getPointeeType();
John McCall0953e762009-09-24 19:53:00 +00002645 if (!Pointee.hasQualifiers() &&
Sebastian Redl07779722008-10-31 14:43:28 +00002646 Pointee->isVoidType() && // to void*
2647 CE->getSubExpr()->getType()->isIntegerType()) // from int.
Douglas Gregorce940492009-09-25 04:25:58 +00002648 return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Sebastian Redl07779722008-10-31 14:43:28 +00002649 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002650 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002651 } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
2652 // Ignore the ImplicitCastExpr type entirely.
Douglas Gregorce940492009-09-25 04:25:58 +00002653 return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Steve Naroffaa58f002008-01-14 16:10:57 +00002654 } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
2655 // Accept ((void*)0) as a null pointer constant, as many other
2656 // implementations do.
Douglas Gregorce940492009-09-25 04:25:58 +00002657 return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC);
Peter Collingbournef111d932011-04-15 00:35:48 +00002658 } else if (const GenericSelectionExpr *GE =
2659 dyn_cast<GenericSelectionExpr>(this)) {
2660 return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC);
Mike Stump1eb44332009-09-09 15:08:12 +00002661 } else if (const CXXDefaultArgExpr *DefaultArg
Chris Lattner8123a952008-04-10 02:22:51 +00002662 = dyn_cast<CXXDefaultArgExpr>(this)) {
Chris Lattner04421082008-04-08 04:40:51 +00002663 // See through default argument expressions
Douglas Gregorce940492009-09-25 04:25:58 +00002664 return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC);
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002665 } else if (isa<GNUNullExpr>(this)) {
2666 // The GNU __null extension is always a null pointer constant.
Chandler Carruth82214a82011-02-18 23:54:50 +00002667 return NPCK_GNUNull;
Douglas Gregor03e80032011-06-21 17:03:29 +00002668 } else if (const MaterializeTemporaryExpr *M
2669 = dyn_cast<MaterializeTemporaryExpr>(this)) {
2670 return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC);
John McCall4b9c2d22011-11-06 09:01:30 +00002671 } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) {
2672 if (const Expr *Source = OVE->getSourceExpr())
2673 return Source->isNullPointerConstant(Ctx, NPC);
Steve Naroffaaffbf72008-01-14 02:53:34 +00002674 }
Douglas Gregor2d8b2732008-11-29 04:51:27 +00002675
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002676 // C++0x nullptr_t is always a null pointer constant.
2677 if (getType()->isNullPtrType())
Chandler Carruth82214a82011-02-18 23:54:50 +00002678 return NPCK_CXX0X_nullptr;
Sebastian Redl6e8ed162009-05-10 18:38:11 +00002679
Fariborz Jahanianff3a0782010-09-27 22:42:37 +00002680 if (const RecordType *UT = getType()->getAsUnionType())
2681 if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>())
2682 if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){
2683 const Expr *InitExpr = CLE->getInitializer();
2684 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr))
2685 return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC);
2686 }
Steve Naroffaa58f002008-01-14 16:10:57 +00002687 // This expression must be an integer type.
Sean Huntc3021132010-05-05 15:23:54 +00002688 if (!getType()->isIntegerType() ||
David Blaikie4e4d0842012-03-11 07:00:24 +00002689 (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType()))
Chandler Carruth82214a82011-02-18 23:54:50 +00002690 return NPCK_NotNull;
Mike Stump1eb44332009-09-09 15:08:12 +00002691
Reid Spencer5f016e22007-07-11 17:01:13 +00002692 // If we have an integer constant expression, we need to *evaluate* it and
Richard Smith70488e22012-02-14 21:38:30 +00002693 // test for the value 0. Don't use the C++11 constant expression semantics
2694 // for this, for now; once the dust settles on core issue 903, we might only
2695 // allow a literal 0 here in C++11 mode.
David Blaikie4e4d0842012-03-11 07:00:24 +00002696 if (Ctx.getLangOpts().CPlusPlus0x) {
Richard Smith70488e22012-02-14 21:38:30 +00002697 if (!isCXX98IntegralConstantExpr(Ctx))
2698 return NPCK_NotNull;
2699 } else {
2700 if (!isIntegerConstantExpr(Ctx))
2701 return NPCK_NotNull;
2702 }
Chandler Carruth82214a82011-02-18 23:54:50 +00002703
Richard Smith70488e22012-02-14 21:38:30 +00002704 return (EvaluateKnownConstInt(Ctx) == 0) ? NPCK_ZeroInteger : NPCK_NotNull;
Reid Spencer5f016e22007-07-11 17:01:13 +00002705}
Steve Naroff31a45842007-07-28 23:10:27 +00002706
John McCallf6a16482010-12-04 03:47:34 +00002707/// \brief If this expression is an l-value for an Objective C
2708/// property, find the underlying property reference expression.
2709const ObjCPropertyRefExpr *Expr::getObjCProperty() const {
2710 const Expr *E = this;
2711 while (true) {
2712 assert((E->getValueKind() == VK_LValue &&
2713 E->getObjectKind() == OK_ObjCProperty) &&
2714 "expression is not a property reference");
2715 E = E->IgnoreParenCasts();
2716 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) {
2717 if (BO->getOpcode() == BO_Comma) {
2718 E = BO->getRHS();
2719 continue;
2720 }
2721 }
2722
2723 break;
2724 }
2725
2726 return cast<ObjCPropertyRefExpr>(E);
2727}
2728
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002729FieldDecl *Expr::getBitField() {
Douglas Gregor6f4a69a2009-07-06 15:38:40 +00002730 Expr *E = this->IgnoreParens();
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002731
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002732 while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCallf6a16482010-12-04 03:47:34 +00002733 if (ICE->getCastKind() == CK_LValueToRValue ||
2734 (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp))
Douglas Gregorde4b1d82010-01-29 19:14:02 +00002735 E = ICE->getSubExpr()->IgnoreParens();
2736 else
2737 break;
2738 }
2739
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002740 if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E))
Douglas Gregor86f19402008-12-20 23:49:58 +00002741 if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl()))
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002742 if (Field->isBitField())
2743 return Field;
2744
Argyrios Kyrtzidis0f279e72010-10-30 19:52:22 +00002745 if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E))
2746 if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl()))
2747 if (Field->isBitField())
2748 return Field;
2749
Eli Friedman42068e92011-07-13 02:05:57 +00002750 if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) {
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002751 if (BinOp->isAssignmentOp() && BinOp->getLHS())
2752 return BinOp->getLHS()->getBitField();
2753
Eli Friedman42068e92011-07-13 02:05:57 +00002754 if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS())
2755 return BinOp->getRHS()->getBitField();
2756 }
2757
Douglas Gregor33bbbc52009-05-02 02:18:30 +00002758 return 0;
Douglas Gregor27c8dc02008-10-29 00:13:59 +00002759}
2760
Anders Carlsson09380262010-01-31 17:18:49 +00002761bool Expr::refersToVectorElement() const {
2762 const Expr *E = this->IgnoreParens();
Sean Huntc3021132010-05-05 15:23:54 +00002763
Anders Carlsson09380262010-01-31 17:18:49 +00002764 while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) {
John McCall5baba9d2010-08-25 10:28:54 +00002765 if (ICE->getValueKind() != VK_RValue &&
John McCall2de56d12010-08-25 11:45:40 +00002766 ICE->getCastKind() == CK_NoOp)
Anders Carlsson09380262010-01-31 17:18:49 +00002767 E = ICE->getSubExpr()->IgnoreParens();
2768 else
2769 break;
2770 }
Sean Huntc3021132010-05-05 15:23:54 +00002771
Anders Carlsson09380262010-01-31 17:18:49 +00002772 if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E))
2773 return ASE->getBase()->getType()->isVectorType();
2774
2775 if (isa<ExtVectorElementExpr>(E))
2776 return true;
2777
2778 return false;
2779}
2780
Chris Lattner2140e902009-02-16 22:14:05 +00002781/// isArrow - Return true if the base expression is a pointer to vector,
2782/// return false if the base expression is a vector.
2783bool ExtVectorElementExpr::isArrow() const {
2784 return getBase()->getType()->isPointerType();
2785}
2786
Nate Begeman213541a2008-04-18 23:10:10 +00002787unsigned ExtVectorElementExpr::getNumElements() const {
John McCall183700f2009-09-21 23:43:11 +00002788 if (const VectorType *VT = getType()->getAs<VectorType>())
Nate Begeman8a997642008-05-09 06:41:27 +00002789 return VT->getNumElements();
2790 return 1;
Chris Lattner4d0ac882007-08-03 16:00:20 +00002791}
2792
Nate Begeman8a997642008-05-09 06:41:27 +00002793/// containsDuplicateElements - Return true if any element access is repeated.
Nate Begeman213541a2008-04-18 23:10:10 +00002794bool ExtVectorElementExpr::containsDuplicateElements() const {
Daniel Dunbara2b34eb2009-10-18 02:09:09 +00002795 // FIXME: Refactor this code to an accessor on the AST node which returns the
2796 // "type" of component access, and share with code below and in Sema.
Chris Lattner5f9e2722011-07-23 10:55:15 +00002797 StringRef Comp = Accessor->getName();
Nate Begeman190d6a22009-01-18 02:01:21 +00002798
2799 // Halving swizzles do not contain duplicate elements.
Daniel Dunbar15027422009-10-17 23:53:04 +00002800 if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd")
Nate Begeman190d6a22009-01-18 02:01:21 +00002801 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Nate Begeman190d6a22009-01-18 02:01:21 +00002803 // Advance past s-char prefix on hex swizzles.
Daniel Dunbar15027422009-10-17 23:53:04 +00002804 if (Comp[0] == 's' || Comp[0] == 'S')
2805 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002806
Daniel Dunbar15027422009-10-17 23:53:04 +00002807 for (unsigned i = 0, e = Comp.size(); i != e; ++i)
Chris Lattner5f9e2722011-07-23 10:55:15 +00002808 if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos)
Steve Narofffec0b492007-07-30 03:29:09 +00002809 return true;
Daniel Dunbar15027422009-10-17 23:53:04 +00002810
Steve Narofffec0b492007-07-30 03:29:09 +00002811 return false;
2812}
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002813
Nate Begeman8a997642008-05-09 06:41:27 +00002814/// getEncodedElementAccess - We encode the fields as a llvm ConstantArray.
Nate Begeman3b8d1162008-05-13 21:03:02 +00002815void ExtVectorElementExpr::getEncodedElementAccess(
Chris Lattner5f9e2722011-07-23 10:55:15 +00002816 SmallVectorImpl<unsigned> &Elts) const {
2817 StringRef Comp = Accessor->getName();
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002818 if (Comp[0] == 's' || Comp[0] == 'S')
2819 Comp = Comp.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00002820
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002821 bool isHi = Comp == "hi";
2822 bool isLo = Comp == "lo";
2823 bool isEven = Comp == "even";
2824 bool isOdd = Comp == "odd";
Mike Stump1eb44332009-09-09 15:08:12 +00002825
Nate Begeman8a997642008-05-09 06:41:27 +00002826 for (unsigned i = 0, e = getNumElements(); i != e; ++i) {
2827 uint64_t Index;
Mike Stump1eb44332009-09-09 15:08:12 +00002828
Nate Begeman8a997642008-05-09 06:41:27 +00002829 if (isHi)
2830 Index = e + i;
2831 else if (isLo)
2832 Index = i;
2833 else if (isEven)
2834 Index = 2 * i;
2835 else if (isOdd)
2836 Index = 2 * i + 1;
2837 else
Daniel Dunbar4b55b242009-10-18 02:09:31 +00002838 Index = ExtVectorType::getAccessorIdx(Comp[i]);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002839
Nate Begeman3b8d1162008-05-13 21:03:02 +00002840 Elts.push_back(Index);
Chris Lattnerb8f849d2007-08-02 23:36:59 +00002841 }
Nate Begeman8a997642008-05-09 06:41:27 +00002842}
2843
Douglas Gregor04badcf2010-04-21 00:45:42 +00002844ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002845 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002846 SourceLocation LBracLoc,
2847 SourceLocation SuperLoc,
2848 bool IsInstanceSuper,
2849 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002850 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002851 ArrayRef<SourceLocation> SelLocs,
2852 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002853 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002854 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002855 SourceLocation RBracLoc,
2856 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002857 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002858 /*TypeDependent=*/false, /*ValueDependent=*/false,
Douglas Gregor561f8122011-07-01 01:22:09 +00002859 /*InstantiationDependent=*/false,
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002860 /*ContainsUnexpandedParameterPack=*/false),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002861 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2862 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002863 Kind(IsInstanceSuper? SuperInstance : SuperClass),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002864 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
2865 SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregorc2350e52010-03-08 16:40:19 +00002866{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002867 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002868 setReceiverPointer(SuperType.getAsOpaquePtr());
Ted Kremenek4df728e2008-06-24 15:50:53 +00002869}
2870
Douglas Gregor04badcf2010-04-21 00:45:42 +00002871ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002872 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002873 SourceLocation LBracLoc,
2874 TypeSourceInfo *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002875 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002876 ArrayRef<SourceLocation> SelLocs,
2877 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002878 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002879 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002880 SourceLocation RBracLoc,
2881 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002882 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002883 T->isDependentType(), T->isInstantiationDependentType(),
2884 T->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002885 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2886 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002887 Kind(Class),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002888 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002889 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002890{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002891 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002892 setReceiverPointer(Receiver);
Ted Kremenek4df728e2008-06-24 15:50:53 +00002893}
2894
Douglas Gregor04badcf2010-04-21 00:45:42 +00002895ObjCMessageExpr::ObjCMessageExpr(QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002896 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002897 SourceLocation LBracLoc,
2898 Expr *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002899 Selector Sel,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002900 ArrayRef<SourceLocation> SelLocs,
2901 SelectorLocationsKind SelLocsK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002902 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002903 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002904 SourceLocation RBracLoc,
2905 bool isImplicit)
John McCallf89e55a2010-11-18 06:31:45 +00002906 : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002907 Receiver->isTypeDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00002908 Receiver->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002909 Receiver->containsUnexpandedParameterPack()),
Douglas Gregor04badcf2010-04-21 00:45:42 +00002910 SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method
2911 : Sel.getAsOpaquePtr())),
Argyrios Kyrtzidisb994e6c2011-10-03 06:36:55 +00002912 Kind(Instance),
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002913 HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit),
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002914 LBracLoc(LBracLoc), RBracLoc(RBracLoc)
Douglas Gregor04badcf2010-04-21 00:45:42 +00002915{
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002916 initArgsAndSelLocs(Args, SelLocs, SelLocsK);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002917 setReceiverPointer(Receiver);
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002918}
2919
2920void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args,
2921 ArrayRef<SourceLocation> SelLocs,
2922 SelectorLocationsKind SelLocsK) {
2923 setNumArgs(Args.size());
Douglas Gregoraa165f82011-01-03 19:04:46 +00002924 Expr **MyArgs = getArgs();
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002925 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002926 if (Args[I]->isTypeDependent())
2927 ExprBits.TypeDependent = true;
2928 if (Args[I]->isValueDependent())
2929 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00002930 if (Args[I]->isInstantiationDependent())
2931 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00002932 if (Args[I]->containsUnexpandedParameterPack())
2933 ExprBits.ContainsUnexpandedParameterPack = true;
2934
2935 MyArgs[I] = Args[I];
2936 }
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002937
Benjamin Kramer19562c92012-02-20 00:20:48 +00002938 SelLocsKind = SelLocsK;
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00002939 if (!isImplicit()) {
Argyrios Kyrtzidis0c6b8e32012-01-12 22:34:19 +00002940 if (SelLocsK == SelLoc_NonStandard)
2941 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
2942 }
Chris Lattner0389e6b2009-04-26 00:44:05 +00002943}
2944
Douglas Gregor04badcf2010-04-21 00:45:42 +00002945ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002946 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002947 SourceLocation LBracLoc,
2948 SourceLocation SuperLoc,
2949 bool IsInstanceSuper,
2950 QualType SuperType,
Sean Huntc3021132010-05-05 15:23:54 +00002951 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002952 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002953 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002954 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002955 SourceLocation RBracLoc,
2956 bool isImplicit) {
2957 assert((!SelLocs.empty() || isImplicit) &&
2958 "No selector locs for non-implicit message");
2959 ObjCMessageExpr *Mem;
2960 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
2961 if (isImplicit)
2962 Mem = alloc(Context, Args.size(), 0);
2963 else
2964 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
John McCallf89e55a2010-11-18 06:31:45 +00002965 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00002966 SuperType, Sel, SelLocs, SelLocsK,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002967 Method, Args, RBracLoc, isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002968}
2969
2970ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002971 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002972 SourceLocation LBracLoc,
2973 TypeSourceInfo *Receiver,
Sean Huntc3021132010-05-05 15:23:54 +00002974 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002975 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002976 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00002977 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002978 SourceLocation RBracLoc,
2979 bool isImplicit) {
2980 assert((!SelLocs.empty() || isImplicit) &&
2981 "No selector locs for non-implicit message");
2982 ObjCMessageExpr *Mem;
2983 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
2984 if (isImplicit)
2985 Mem = alloc(Context, Args.size(), 0);
2986 else
2987 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002988 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00002989 SelLocs, SelLocsK, Method, Args, RBracLoc,
2990 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00002991}
2992
2993ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T,
John McCallf89e55a2010-11-18 06:31:45 +00002994 ExprValueKind VK,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002995 SourceLocation LBracLoc,
2996 Expr *Receiver,
Argyrios Kyrtzidisf40f0d52010-12-10 20:08:27 +00002997 Selector Sel,
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00002998 ArrayRef<SourceLocation> SelLocs,
Douglas Gregor04badcf2010-04-21 00:45:42 +00002999 ObjCMethodDecl *Method,
Argyrios Kyrtzidis8d9ed792011-10-03 06:36:45 +00003000 ArrayRef<Expr *> Args,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003001 SourceLocation RBracLoc,
3002 bool isImplicit) {
3003 assert((!SelLocs.empty() || isImplicit) &&
3004 "No selector locs for non-implicit message");
3005 ObjCMessageExpr *Mem;
3006 SelectorLocationsKind SelLocsK = SelectorLocationsKind();
3007 if (isImplicit)
3008 Mem = alloc(Context, Args.size(), 0);
3009 else
3010 Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK);
Argyrios Kyrtzidis95137622011-10-03 06:36:17 +00003011 return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel,
Argyrios Kyrtzidis746f5bc2012-01-12 02:34:39 +00003012 SelLocs, SelLocsK, Method, Args, RBracLoc,
3013 isImplicit);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003014}
3015
Sean Huntc3021132010-05-05 15:23:54 +00003016ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context,
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003017 unsigned NumArgs,
3018 unsigned NumStoredSelLocs) {
3019 ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003020 return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs);
3021}
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003022
Argyrios Kyrtzidis20718082011-10-03 06:36:51 +00003023ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3024 ArrayRef<Expr *> Args,
3025 SourceLocation RBraceLoc,
3026 ArrayRef<SourceLocation> SelLocs,
3027 Selector Sel,
3028 SelectorLocationsKind &SelLocsK) {
3029 SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc);
3030 unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size()
3031 : 0;
3032 return alloc(C, Args.size(), NumStoredSelLocs);
3033}
3034
3035ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C,
3036 unsigned NumArgs,
3037 unsigned NumStoredSelLocs) {
3038 unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) +
3039 NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation);
3040 return (ObjCMessageExpr *)C.Allocate(Size,
3041 llvm::AlignOf<ObjCMessageExpr>::Alignment);
3042}
3043
3044void ObjCMessageExpr::getSelectorLocs(
3045 SmallVectorImpl<SourceLocation> &SelLocs) const {
3046 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
3047 SelLocs.push_back(getSelectorLoc(i));
3048}
3049
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003050SourceRange ObjCMessageExpr::getReceiverRange() const {
3051 switch (getReceiverKind()) {
3052 case Instance:
3053 return getInstanceReceiver()->getSourceRange();
3054
3055 case Class:
3056 return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange();
3057
3058 case SuperInstance:
3059 case SuperClass:
3060 return getSuperLoc();
3061 }
3062
David Blaikie30263482012-01-20 21:50:17 +00003063 llvm_unreachable("Invalid ReceiverKind!");
Argyrios Kyrtzidise005d192010-12-10 20:08:30 +00003064}
3065
Douglas Gregor04badcf2010-04-21 00:45:42 +00003066Selector ObjCMessageExpr::getSelector() const {
3067 if (HasMethod)
3068 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod)
3069 ->getSelector();
Sean Huntc3021132010-05-05 15:23:54 +00003070 return Selector(SelectorOrMethod);
Douglas Gregor04badcf2010-04-21 00:45:42 +00003071}
3072
3073ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const {
3074 switch (getReceiverKind()) {
3075 case Instance:
3076 if (const ObjCObjectPointerType *Ptr
3077 = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>())
3078 return Ptr->getInterfaceDecl();
3079 break;
3080
3081 case Class:
John McCallc12c5bb2010-05-15 11:32:37 +00003082 if (const ObjCObjectType *Ty
3083 = getClassReceiver()->getAs<ObjCObjectType>())
3084 return Ty->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003085 break;
3086
3087 case SuperInstance:
3088 if (const ObjCObjectPointerType *Ptr
3089 = getSuperType()->getAs<ObjCObjectPointerType>())
3090 return Ptr->getInterfaceDecl();
3091 break;
3092
3093 case SuperClass:
Argyrios Kyrtzidisee8a6ca2011-01-25 00:03:48 +00003094 if (const ObjCObjectType *Iface
3095 = getSuperType()->getAs<ObjCObjectType>())
3096 return Iface->getInterface();
Douglas Gregor04badcf2010-04-21 00:45:42 +00003097 break;
3098 }
3099
3100 return 0;
Ted Kremenekeb3b3242010-02-11 22:41:21 +00003101}
Chris Lattner0389e6b2009-04-26 00:44:05 +00003102
Chris Lattner5f9e2722011-07-23 10:55:15 +00003103StringRef ObjCBridgedCastExpr::getBridgeKindName() const {
John McCallf85e1932011-06-15 23:02:42 +00003104 switch (getBridgeKind()) {
3105 case OBC_Bridge:
3106 return "__bridge";
3107 case OBC_BridgeTransfer:
3108 return "__bridge_transfer";
3109 case OBC_BridgeRetained:
3110 return "__bridge_retained";
3111 }
David Blaikie30263482012-01-20 21:50:17 +00003112
3113 llvm_unreachable("Invalid BridgeKind!");
John McCallf85e1932011-06-15 23:02:42 +00003114}
3115
Jay Foad4ba2a172011-01-12 09:06:06 +00003116bool ChooseExpr::isConditionTrue(const ASTContext &C) const {
Richard Smitha6b8b2c2011-10-10 18:28:20 +00003117 return getCond()->EvaluateKnownConstInt(C) != 0;
Chris Lattner27437ca2007-10-25 00:29:32 +00003118}
3119
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003120ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr,
3121 QualType Type, SourceLocation BLoc,
3122 SourceLocation RP)
3123 : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary,
3124 Type->isDependentType(), Type->isDependentType(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003125 Type->isInstantiationDependentType(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003126 Type->containsUnexpandedParameterPack()),
3127 BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr)
3128{
3129 SubExprs = new (C) Stmt*[nexpr];
3130 for (unsigned i = 0; i < nexpr; i++) {
3131 if (args[i]->isTypeDependent())
3132 ExprBits.TypeDependent = true;
3133 if (args[i]->isValueDependent())
3134 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003135 if (args[i]->isInstantiationDependent())
3136 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003137 if (args[i]->containsUnexpandedParameterPack())
3138 ExprBits.ContainsUnexpandedParameterPack = true;
3139
3140 SubExprs[i] = args[i];
3141 }
3142}
3143
Nate Begeman888376a2009-08-12 02:28:50 +00003144void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs,
3145 unsigned NumExprs) {
3146 if (SubExprs) C.Deallocate(SubExprs);
3147
3148 SubExprs = new (C) Stmt* [NumExprs];
Douglas Gregor94cd5d12009-04-16 00:01:45 +00003149 this->NumExprs = NumExprs;
3150 memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs);
Mike Stump1eb44332009-09-09 15:08:12 +00003151}
Nate Begeman888376a2009-08-12 02:28:50 +00003152
Peter Collingbournef111d932011-04-15 00:35:48 +00003153GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3154 SourceLocation GenericLoc, Expr *ControllingExpr,
3155 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3156 unsigned NumAssocs, SourceLocation DefaultLoc,
3157 SourceLocation RParenLoc,
3158 bool ContainsUnexpandedParameterPack,
3159 unsigned ResultIndex)
3160 : Expr(GenericSelectionExprClass,
3161 AssocExprs[ResultIndex]->getType(),
3162 AssocExprs[ResultIndex]->getValueKind(),
3163 AssocExprs[ResultIndex]->getObjectKind(),
3164 AssocExprs[ResultIndex]->isTypeDependent(),
3165 AssocExprs[ResultIndex]->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003166 AssocExprs[ResultIndex]->isInstantiationDependent(),
Peter Collingbournef111d932011-04-15 00:35:48 +00003167 ContainsUnexpandedParameterPack),
3168 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3169 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3170 ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3171 RParenLoc(RParenLoc) {
3172 SubExprs[CONTROLLING] = ControllingExpr;
3173 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3174 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3175}
3176
3177GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context,
3178 SourceLocation GenericLoc, Expr *ControllingExpr,
3179 TypeSourceInfo **AssocTypes, Expr **AssocExprs,
3180 unsigned NumAssocs, SourceLocation DefaultLoc,
3181 SourceLocation RParenLoc,
3182 bool ContainsUnexpandedParameterPack)
3183 : Expr(GenericSelectionExprClass,
3184 Context.DependentTy,
3185 VK_RValue,
3186 OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003187 /*isTypeDependent=*/true,
3188 /*isValueDependent=*/true,
3189 /*isInstantiationDependent=*/true,
Peter Collingbournef111d932011-04-15 00:35:48 +00003190 ContainsUnexpandedParameterPack),
3191 AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]),
3192 SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs),
3193 ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc),
3194 RParenLoc(RParenLoc) {
3195 SubExprs[CONTROLLING] = ControllingExpr;
3196 std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes);
3197 std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR);
3198}
3199
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003200//===----------------------------------------------------------------------===//
Douglas Gregor05c13a32009-01-22 00:58:24 +00003201// DesignatedInitExpr
3202//===----------------------------------------------------------------------===//
3203
Chandler Carruthb1138242011-06-16 06:47:06 +00003204IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const {
Douglas Gregor05c13a32009-01-22 00:58:24 +00003205 assert(Kind == FieldDesignator && "Only valid on a field designator");
3206 if (Field.NameOrField & 0x01)
3207 return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01);
3208 else
3209 return getField()->getIdentifier();
3210}
3211
Sean Huntc3021132010-05-05 15:23:54 +00003212DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty,
Douglas Gregor319d57f2010-01-06 23:17:19 +00003213 unsigned NumDesignators,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003214 const Designator *Designators,
Mike Stump1eb44332009-09-09 15:08:12 +00003215 SourceLocation EqualOrColonLoc,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003216 bool GNUSyntax,
Mike Stump1eb44332009-09-09 15:08:12 +00003217 Expr **IndexExprs,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003218 unsigned NumIndexExprs,
3219 Expr *Init)
Mike Stump1eb44332009-09-09 15:08:12 +00003220 : Expr(DesignatedInitExprClass, Ty,
John McCallf89e55a2010-11-18 06:31:45 +00003221 Init->getValueKind(), Init->getObjectKind(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003222 Init->isTypeDependent(), Init->isValueDependent(),
Douglas Gregor561f8122011-07-01 01:22:09 +00003223 Init->isInstantiationDependent(),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003224 Init->containsUnexpandedParameterPack()),
Mike Stump1eb44332009-09-09 15:08:12 +00003225 EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax),
3226 NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003227 this->Designators = new (C) Designator[NumDesignators];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003228
3229 // Record the initializer itself.
John McCall7502c1d2011-02-13 04:07:26 +00003230 child_range Child = children();
Douglas Gregor9ea62762009-05-21 23:17:49 +00003231 *Child++ = Init;
3232
3233 // Copy the designators and their subexpressions, computing
3234 // value-dependence along the way.
3235 unsigned IndexIdx = 0;
3236 for (unsigned I = 0; I != NumDesignators; ++I) {
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003237 this->Designators[I] = Designators[I];
Douglas Gregor9ea62762009-05-21 23:17:49 +00003238
3239 if (this->Designators[I].isArrayDesignator()) {
3240 // Compute type- and value-dependence.
3241 Expr *Index = IndexExprs[IndexIdx];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003242 if (Index->isTypeDependent() || Index->isValueDependent())
3243 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003244 if (Index->isInstantiationDependent())
3245 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003246 // Propagate unexpanded parameter packs.
3247 if (Index->containsUnexpandedParameterPack())
3248 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003249
3250 // Copy the index expressions into permanent storage.
3251 *Child++ = IndexExprs[IndexIdx++];
3252 } else if (this->Designators[I].isArrayRangeDesignator()) {
3253 // Compute type- and value-dependence.
3254 Expr *Start = IndexExprs[IndexIdx];
3255 Expr *End = IndexExprs[IndexIdx + 1];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003256 if (Start->isTypeDependent() || Start->isValueDependent() ||
Douglas Gregor561f8122011-07-01 01:22:09 +00003257 End->isTypeDependent() || End->isValueDependent()) {
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003258 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003259 ExprBits.InstantiationDependent = true;
3260 } else if (Start->isInstantiationDependent() ||
3261 End->isInstantiationDependent()) {
3262 ExprBits.InstantiationDependent = true;
3263 }
3264
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003265 // Propagate unexpanded parameter packs.
3266 if (Start->containsUnexpandedParameterPack() ||
3267 End->containsUnexpandedParameterPack())
3268 ExprBits.ContainsUnexpandedParameterPack = true;
Douglas Gregor9ea62762009-05-21 23:17:49 +00003269
3270 // Copy the start/end expressions into permanent storage.
3271 *Child++ = IndexExprs[IndexIdx++];
3272 *Child++ = IndexExprs[IndexIdx++];
3273 }
3274 }
3275
3276 assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions");
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003277}
3278
Douglas Gregor05c13a32009-01-22 00:58:24 +00003279DesignatedInitExpr *
Mike Stump1eb44332009-09-09 15:08:12 +00003280DesignatedInitExpr::Create(ASTContext &C, Designator *Designators,
Douglas Gregor05c13a32009-01-22 00:58:24 +00003281 unsigned NumDesignators,
3282 Expr **IndexExprs, unsigned NumIndexExprs,
3283 SourceLocation ColonOrEqualLoc,
3284 bool UsesColonSyntax, Expr *Init) {
Steve Naroffc0ac4922009-01-27 23:20:32 +00003285 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00003286 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
Douglas Gregor319d57f2010-01-06 23:17:19 +00003287 return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators,
Douglas Gregor9ea62762009-05-21 23:17:49 +00003288 ColonOrEqualLoc, UsesColonSyntax,
3289 IndexExprs, NumIndexExprs, Init);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003290}
3291
Mike Stump1eb44332009-09-09 15:08:12 +00003292DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C,
Douglas Gregord077d752009-04-16 00:55:48 +00003293 unsigned NumIndexExprs) {
3294 void *Mem = C.Allocate(sizeof(DesignatedInitExpr) +
3295 sizeof(Stmt *) * (NumIndexExprs + 1), 8);
3296 return new (Mem) DesignatedInitExpr(NumIndexExprs + 1);
3297}
3298
Douglas Gregor319d57f2010-01-06 23:17:19 +00003299void DesignatedInitExpr::setDesignators(ASTContext &C,
3300 const Designator *Desigs,
Douglas Gregord077d752009-04-16 00:55:48 +00003301 unsigned NumDesigs) {
Douglas Gregor319d57f2010-01-06 23:17:19 +00003302 Designators = new (C) Designator[NumDesigs];
Douglas Gregord077d752009-04-16 00:55:48 +00003303 NumDesignators = NumDesigs;
3304 for (unsigned I = 0; I != NumDesigs; ++I)
3305 Designators[I] = Desigs[I];
3306}
3307
Abramo Bagnara24f46742011-03-16 15:08:46 +00003308SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const {
3309 DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this);
3310 if (size() == 1)
3311 return DIE->getDesignator(0)->getSourceRange();
3312 return SourceRange(DIE->getDesignator(0)->getStartLocation(),
3313 DIE->getDesignator(size()-1)->getEndLocation());
3314}
3315
Douglas Gregor05c13a32009-01-22 00:58:24 +00003316SourceRange DesignatedInitExpr::getSourceRange() const {
3317 SourceLocation StartLoc;
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003318 Designator &First =
3319 *const_cast<DesignatedInitExpr*>(this)->designators_begin();
Douglas Gregor05c13a32009-01-22 00:58:24 +00003320 if (First.isFieldDesignator()) {
Douglas Gregoreeae8f02009-03-28 00:41:23 +00003321 if (GNUSyntax)
Douglas Gregor05c13a32009-01-22 00:58:24 +00003322 StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc);
3323 else
3324 StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc);
3325 } else
Chris Lattnerd603eaa2009-02-16 22:33:34 +00003326 StartLoc =
3327 SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003328 return SourceRange(StartLoc, getInit()->getSourceRange().getEnd());
3329}
3330
Douglas Gregor05c13a32009-01-22 00:58:24 +00003331Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) {
3332 assert(D.Kind == Designator::ArrayDesignator && "Requires array designator");
3333 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3334 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003335 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3336 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3337}
3338
3339Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003340 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003341 "Requires array range designator");
3342 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3343 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003344 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3345 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1));
3346}
3347
3348Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) {
Mike Stump1eb44332009-09-09 15:08:12 +00003349 assert(D.Kind == Designator::ArrayRangeDesignator &&
Douglas Gregor05c13a32009-01-22 00:58:24 +00003350 "Requires array range designator");
3351 char* Ptr = static_cast<char*>(static_cast<void *>(this));
3352 Ptr += sizeof(DesignatedInitExpr);
Douglas Gregor05c13a32009-01-22 00:58:24 +00003353 Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr));
3354 return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2));
3355}
3356
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003357/// \brief Replaces the designator at index @p Idx with the series
3358/// of designators in [First, Last).
Douglas Gregor319d57f2010-01-06 23:17:19 +00003359void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx,
Mike Stump1eb44332009-09-09 15:08:12 +00003360 const Designator *First,
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003361 const Designator *Last) {
3362 unsigned NumNewDesignators = Last - First;
3363 if (NumNewDesignators == 0) {
3364 std::copy_backward(Designators + Idx + 1,
3365 Designators + NumDesignators,
3366 Designators + Idx);
3367 --NumNewDesignators;
3368 return;
3369 } else if (NumNewDesignators == 1) {
3370 Designators[Idx] = *First;
3371 return;
3372 }
3373
Mike Stump1eb44332009-09-09 15:08:12 +00003374 Designator *NewDesignators
Douglas Gregor319d57f2010-01-06 23:17:19 +00003375 = new (C) Designator[NumDesignators - 1 + NumNewDesignators];
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003376 std::copy(Designators, Designators + Idx, NewDesignators);
3377 std::copy(First, Last, NewDesignators + Idx);
3378 std::copy(Designators + Idx + 1, Designators + NumDesignators,
3379 NewDesignators + Idx + NumNewDesignators);
Douglas Gregorffb4b6e2009-04-15 06:41:24 +00003380 Designators = NewDesignators;
3381 NumDesignators = NumDesignators - 1 + NumNewDesignators;
3382}
3383
Mike Stump1eb44332009-09-09 15:08:12 +00003384ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc,
Nate Begeman2ef13e52009-08-10 23:49:36 +00003385 Expr **exprs, unsigned nexprs,
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00003386 SourceLocation rparenloc)
3387 : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary,
Douglas Gregor561f8122011-07-01 01:22:09 +00003388 false, false, false, false),
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003389 NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) {
Nate Begeman2ef13e52009-08-10 23:49:36 +00003390 Exprs = new (C) Stmt*[nexprs];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003391 for (unsigned i = 0; i != nexprs; ++i) {
3392 if (exprs[i]->isTypeDependent())
3393 ExprBits.TypeDependent = true;
3394 if (exprs[i]->isValueDependent())
3395 ExprBits.ValueDependent = true;
Douglas Gregor561f8122011-07-01 01:22:09 +00003396 if (exprs[i]->isInstantiationDependent())
3397 ExprBits.InstantiationDependent = true;
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003398 if (exprs[i]->containsUnexpandedParameterPack())
3399 ExprBits.ContainsUnexpandedParameterPack = true;
3400
Nate Begeman2ef13e52009-08-10 23:49:36 +00003401 Exprs[i] = exprs[i];
Douglas Gregorbebbe0d2010-12-15 01:34:56 +00003402 }
Nate Begeman2ef13e52009-08-10 23:49:36 +00003403}
3404
John McCalle996ffd2011-02-16 08:02:54 +00003405const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) {
3406 if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e))
3407 e = ewc->getSubExpr();
Douglas Gregor03e80032011-06-21 17:03:29 +00003408 if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e))
3409 e = m->GetTemporaryExpr();
John McCalle996ffd2011-02-16 08:02:54 +00003410 e = cast<CXXConstructExpr>(e)->getArg(0);
3411 while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e))
3412 e = ice->getSubExpr();
3413 return cast<OpaqueValueExpr>(e);
3414}
3415
John McCall4b9c2d22011-11-06 09:01:30 +00003416PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh,
3417 unsigned numSemanticExprs) {
3418 void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) +
3419 (1 + numSemanticExprs) * sizeof(Expr*),
3420 llvm::alignOf<PseudoObjectExpr>());
3421 return new(buffer) PseudoObjectExpr(sh, numSemanticExprs);
3422}
3423
3424PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs)
3425 : Expr(PseudoObjectExprClass, shell) {
3426 PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1;
3427}
3428
3429PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax,
3430 ArrayRef<Expr*> semantics,
3431 unsigned resultIndex) {
3432 assert(syntax && "no syntactic expression!");
3433 assert(semantics.size() && "no semantic expressions!");
3434
3435 QualType type;
3436 ExprValueKind VK;
3437 if (resultIndex == NoResult) {
3438 type = C.VoidTy;
3439 VK = VK_RValue;
3440 } else {
3441 assert(resultIndex < semantics.size());
3442 type = semantics[resultIndex]->getType();
3443 VK = semantics[resultIndex]->getValueKind();
3444 assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary);
3445 }
3446
3447 void *buffer = C.Allocate(sizeof(PseudoObjectExpr) +
3448 (1 + semantics.size()) * sizeof(Expr*),
3449 llvm::alignOf<PseudoObjectExpr>());
3450 return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics,
3451 resultIndex);
3452}
3453
3454PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK,
3455 Expr *syntax, ArrayRef<Expr*> semantics,
3456 unsigned resultIndex)
3457 : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary,
3458 /*filled in at end of ctor*/ false, false, false, false) {
3459 PseudoObjectExprBits.NumSubExprs = semantics.size() + 1;
3460 PseudoObjectExprBits.ResultIndex = resultIndex + 1;
3461
3462 for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) {
3463 Expr *E = (i == 0 ? syntax : semantics[i-1]);
3464 getSubExprsBuffer()[i] = E;
3465
3466 if (E->isTypeDependent())
3467 ExprBits.TypeDependent = true;
3468 if (E->isValueDependent())
3469 ExprBits.ValueDependent = true;
3470 if (E->isInstantiationDependent())
3471 ExprBits.InstantiationDependent = true;
3472 if (E->containsUnexpandedParameterPack())
3473 ExprBits.ContainsUnexpandedParameterPack = true;
3474
3475 if (isa<OpaqueValueExpr>(E))
3476 assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 &&
3477 "opaque-value semantic expressions for pseudo-object "
3478 "operations must have sources");
3479 }
3480}
3481
Douglas Gregor05c13a32009-01-22 00:58:24 +00003482//===----------------------------------------------------------------------===//
Ted Kremenekce2fc3a2008-10-27 18:40:21 +00003483// ExprIterator.
3484//===----------------------------------------------------------------------===//
3485
3486Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); }
3487Expr* ExprIterator::operator*() const { return cast<Expr>(*I); }
3488Expr* ExprIterator::operator->() const { return cast<Expr>(*I); }
3489const Expr* ConstExprIterator::operator[](size_t idx) const {
3490 return cast<Expr>(I[idx]);
3491}
3492const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); }
3493const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); }
3494
3495//===----------------------------------------------------------------------===//
Ted Kremenek77ed8e42007-08-24 18:13:47 +00003496// Child Iterators for iterating over subexpressions/substatements
3497//===----------------------------------------------------------------------===//
3498
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00003499// UnaryExprOrTypeTraitExpr
3500Stmt::child_range UnaryExprOrTypeTraitExpr::children() {
Sebastian Redl05189992008-11-11 17:56:53 +00003501 // If this is of a type and the type is a VLA type (and not a typedef), the
3502 // size expression of the VLA needs to be treated as an executable expression.
3503 // Why isn't this weirdness documented better in StmtIterator?
3504 if (isArgumentType()) {
John McCallf4c73712011-01-19 06:33:43 +00003505 if (const VariableArrayType* T = dyn_cast<VariableArrayType>(
Sebastian Redl05189992008-11-11 17:56:53 +00003506 getArgumentType().getTypePtr()))
John McCall63c00d72011-02-09 08:16:59 +00003507 return child_range(child_iterator(T), child_iterator());
3508 return child_range();
Sebastian Redl05189992008-11-11 17:56:53 +00003509 }
John McCall63c00d72011-02-09 08:16:59 +00003510 return child_range(&Argument.Ex, &Argument.Ex + 1);
Ted Kremenek9ac59282007-10-18 23:28:49 +00003511}
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003512
Steve Naroff563477d2007-09-18 23:55:05 +00003513// ObjCMessageExpr
John McCall63c00d72011-02-09 08:16:59 +00003514Stmt::child_range ObjCMessageExpr::children() {
3515 Stmt **begin;
Douglas Gregor04badcf2010-04-21 00:45:42 +00003516 if (getReceiverKind() == Instance)
John McCall63c00d72011-02-09 08:16:59 +00003517 begin = reinterpret_cast<Stmt **>(this + 1);
3518 else
3519 begin = reinterpret_cast<Stmt **>(getArgs());
3520 return child_range(begin,
3521 reinterpret_cast<Stmt **>(getArgs() + getNumArgs()));
Steve Naroff563477d2007-09-18 23:55:05 +00003522}
3523
Ted Kremenekebcb57a2012-03-06 20:05:56 +00003524ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements,
3525 QualType T, ObjCMethodDecl *Method,
3526 SourceRange SR)
3527 : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary,
3528 false, false, false, false),
3529 NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method)
3530{
3531 Expr **SaveElements = getElements();
3532 for (unsigned I = 0, N = Elements.size(); I != N; ++I) {
3533 if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent())
3534 ExprBits.ValueDependent = true;
3535 if (Elements[I]->isInstantiationDependent())
3536 ExprBits.InstantiationDependent = true;
3537 if (Elements[I]->containsUnexpandedParameterPack())
3538 ExprBits.ContainsUnexpandedParameterPack = true;
3539
3540 SaveElements[I] = Elements[I];
3541 }
3542}
3543
3544ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C,
3545 llvm::ArrayRef<Expr *> Elements,
3546 QualType T, ObjCMethodDecl * Method,
3547 SourceRange SR) {
3548 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3549 + Elements.size() * sizeof(Expr *));
3550 return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR);
3551}
3552
3553ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C,
3554 unsigned NumElements) {
3555
3556 void *Mem = C.Allocate(sizeof(ObjCArrayLiteral)
3557 + NumElements * sizeof(Expr *));
3558 return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements);
3559}
3560
3561ObjCDictionaryLiteral::ObjCDictionaryLiteral(
3562 ArrayRef<ObjCDictionaryElement> VK,
3563 bool HasPackExpansions,
3564 QualType T, ObjCMethodDecl *method,
3565 SourceRange SR)
3566 : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
3567 false, false),
3568 NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR),
3569 DictWithObjectsMethod(method)
3570{
3571 KeyValuePair *KeyValues = getKeyValues();
3572 ExpansionData *Expansions = getExpansionData();
3573 for (unsigned I = 0; I < NumElements; I++) {
3574 if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() ||
3575 VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent())
3576 ExprBits.ValueDependent = true;
3577 if (VK[I].Key->isInstantiationDependent() ||
3578 VK[I].Value->isInstantiationDependent())
3579 ExprBits.InstantiationDependent = true;
3580 if (VK[I].EllipsisLoc.isInvalid() &&
3581 (VK[I].Key->containsUnexpandedParameterPack() ||
3582 VK[I].Value->containsUnexpandedParameterPack()))
3583 ExprBits.ContainsUnexpandedParameterPack = true;
3584
3585 KeyValues[I].Key = VK[I].Key;
3586 KeyValues[I].Value = VK[I].Value;
3587 if (Expansions) {
3588 Expansions[I].EllipsisLoc = VK[I].EllipsisLoc;
3589 if (VK[I].NumExpansions)
3590 Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1;
3591 else
3592 Expansions[I].NumExpansionsPlusOne = 0;
3593 }
3594 }
3595}
3596
3597ObjCDictionaryLiteral *
3598ObjCDictionaryLiteral::Create(ASTContext &C,
3599 ArrayRef<ObjCDictionaryElement> VK,
3600 bool HasPackExpansions,
3601 QualType T, ObjCMethodDecl *method,
3602 SourceRange SR) {
3603 unsigned ExpansionsSize = 0;
3604 if (HasPackExpansions)
3605 ExpansionsSize = sizeof(ExpansionData) * VK.size();
3606
3607 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3608 sizeof(KeyValuePair) * VK.size() + ExpansionsSize);
3609 return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR);
3610}
3611
3612ObjCDictionaryLiteral *
3613ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements,
3614 bool HasPackExpansions) {
3615 unsigned ExpansionsSize = 0;
3616 if (HasPackExpansions)
3617 ExpansionsSize = sizeof(ExpansionData) * NumElements;
3618 void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) +
3619 sizeof(KeyValuePair) * NumElements + ExpansionsSize);
3620 return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements,
3621 HasPackExpansions);
3622}
3623
3624ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C,
3625 Expr *base,
3626 Expr *key, QualType T,
3627 ObjCMethodDecl *getMethod,
3628 ObjCMethodDecl *setMethod,
3629 SourceLocation RB) {
3630 void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr));
3631 return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue,
3632 OK_ObjCSubscript,
3633 getMethod, setMethod, RB);
3634}
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003635
3636AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr,
3637 QualType t, AtomicOp op, SourceLocation RP)
3638 : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary,
3639 false, false, false, false),
3640 NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op)
3641{
Richard Smithe1b2abc2012-04-10 22:49:28 +00003642 assert(nexpr == getNumSubExprs(op) && "wrong number of subexpressions");
Eli Friedmandfa64ba2011-10-14 22:48:56 +00003643 for (unsigned i = 0; i < nexpr; i++) {
3644 if (args[i]->isTypeDependent())
3645 ExprBits.TypeDependent = true;
3646 if (args[i]->isValueDependent())
3647 ExprBits.ValueDependent = true;
3648 if (args[i]->isInstantiationDependent())
3649 ExprBits.InstantiationDependent = true;
3650 if (args[i]->containsUnexpandedParameterPack())
3651 ExprBits.ContainsUnexpandedParameterPack = true;
3652
3653 SubExprs[i] = args[i];
3654 }
3655}
Richard Smithe1b2abc2012-04-10 22:49:28 +00003656
3657unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) {
3658 switch (Op) {
Richard Smithff34d402012-04-12 05:08:17 +00003659 case AO__c11_atomic_init:
3660 case AO__c11_atomic_load:
3661 case AO__atomic_load_n:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003662 return 2;
Richard Smithff34d402012-04-12 05:08:17 +00003663
3664 case AO__c11_atomic_store:
3665 case AO__c11_atomic_exchange:
3666 case AO__atomic_load:
3667 case AO__atomic_store:
3668 case AO__atomic_store_n:
3669 case AO__atomic_exchange_n:
3670 case AO__c11_atomic_fetch_add:
3671 case AO__c11_atomic_fetch_sub:
3672 case AO__c11_atomic_fetch_and:
3673 case AO__c11_atomic_fetch_or:
3674 case AO__c11_atomic_fetch_xor:
3675 case AO__atomic_fetch_add:
3676 case AO__atomic_fetch_sub:
3677 case AO__atomic_fetch_and:
3678 case AO__atomic_fetch_or:
3679 case AO__atomic_fetch_xor:
Richard Smith51b92402012-04-13 06:31:38 +00003680 case AO__atomic_fetch_nand:
Richard Smithff34d402012-04-12 05:08:17 +00003681 case AO__atomic_add_fetch:
3682 case AO__atomic_sub_fetch:
3683 case AO__atomic_and_fetch:
3684 case AO__atomic_or_fetch:
3685 case AO__atomic_xor_fetch:
Richard Smith51b92402012-04-13 06:31:38 +00003686 case AO__atomic_nand_fetch:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003687 return 3;
Richard Smithff34d402012-04-12 05:08:17 +00003688
3689 case AO__atomic_exchange:
3690 return 4;
3691
3692 case AO__c11_atomic_compare_exchange_strong:
3693 case AO__c11_atomic_compare_exchange_weak:
Richard Smithe1b2abc2012-04-10 22:49:28 +00003694 return 5;
Richard Smithff34d402012-04-12 05:08:17 +00003695
3696 case AO__atomic_compare_exchange:
3697 case AO__atomic_compare_exchange_n:
3698 return 6;
Richard Smithe1b2abc2012-04-10 22:49:28 +00003699 }
3700 llvm_unreachable("unknown atomic op");
3701}