blob: 1c906789ab7af8bd12b55c64db9d51affadbaaf8 [file] [log] [blame]
Ted Kremeneke3a0c142007-08-24 20:21:10 +00001//===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Ted Kremeneke3a0c142007-08-24 20:21:10 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the subclesses of Expr class declared in ExprCXX.h
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000014#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000015#include "clang/AST/Attr.h"
Douglas Gregor993603d2008-11-14 16:09:21 +000016#include "clang/AST/DeclCXX.h"
Douglas Gregora727cb92009-06-30 22:34:41 +000017#include "clang/AST/DeclTemplate.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000018#include "clang/AST/ExprCXX.h"
Douglas Gregor651fe5e2010-02-24 23:40:28 +000019#include "clang/AST/TypeLoc.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000020#include "clang/Basic/IdentifierTable.h"
Ted Kremeneke3a0c142007-08-24 20:21:10 +000021using namespace clang;
22
Douglas Gregor9da64192010-04-26 22:37:10 +000023
Ted Kremeneke3a0c142007-08-24 20:21:10 +000024//===----------------------------------------------------------------------===//
25// Child Iterators for iterating over subexpressions/substatements
26//===----------------------------------------------------------------------===//
27
Richard Smithef8bf432012-08-13 20:08:14 +000028bool CXXTypeidExpr::isPotentiallyEvaluated() const {
29 if (isTypeOperand())
30 return false;
31
32 // C++11 [expr.typeid]p3:
33 // When typeid is applied to an expression other than a glvalue of
34 // polymorphic class type, [...] the expression is an unevaluated operand.
35 const Expr *E = getExprOperand();
36 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
37 if (RD->isPolymorphic() && E->isGLValue())
38 return true;
39
40 return false;
41}
42
David Majnemer143c55e2013-09-27 07:04:31 +000043QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
Douglas Gregor9da64192010-04-26 22:37:10 +000044 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000045 Qualifiers Quals;
46 return Context.getUnqualifiedArrayType(
47 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Douglas Gregor9da64192010-04-26 22:37:10 +000048}
49
David Majnemer143c55e2013-09-27 07:04:31 +000050QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
Francois Pichet9f4f2072010-09-08 12:20:18 +000051 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000052 Qualifiers Quals;
53 return Context.getUnqualifiedArrayType(
54 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Francois Pichet9f4f2072010-09-08 12:20:18 +000055}
56
Nico Webercf4ff5862012-10-11 10:13:44 +000057// static
David Majnemer59c0ec22013-09-07 06:59:46 +000058UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT,
59 bool *RDHasMultipleGUIDsPtr) {
Nico Webercf4ff5862012-10-11 10:13:44 +000060 // Optionally remove one level of pointer, reference or array indirection.
61 const Type *Ty = QT.getTypePtr();
62 if (QT->isPointerType() || QT->isReferenceType())
63 Ty = QT->getPointeeType().getTypePtr();
64 else if (QT->isArrayType())
David Majnemer68c880b2013-09-27 07:57:34 +000065 Ty = Ty->getBaseElementTypeUnsafe();
Nico Webercf4ff5862012-10-11 10:13:44 +000066
67 // Loop all record redeclaration looking for an uuid attribute.
68 CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
David Majnemer59c0ec22013-09-07 06:59:46 +000069 if (!RD)
Craig Topper36250ad2014-05-12 05:36:57 +000070 return nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +000071
David Majnemer5d22e7e2013-09-07 07:11:04 +000072 // __uuidof can grab UUIDs from template arguments.
David Majnemer59c0ec22013-09-07 06:59:46 +000073 if (ClassTemplateSpecializationDecl *CTSD =
74 dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
75 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
Craig Topper36250ad2014-05-12 05:36:57 +000076 UuidAttr *UuidForRD = nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +000077
78 for (unsigned I = 0, N = TAL.size(); I != N; ++I) {
79 const TemplateArgument &TA = TAL[I];
80 bool SeenMultipleGUIDs = false;
81
Craig Topper36250ad2014-05-12 05:36:57 +000082 UuidAttr *UuidForTA = nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +000083 if (TA.getKind() == TemplateArgument::Type)
84 UuidForTA = GetUuidAttrOfType(TA.getAsType(), &SeenMultipleGUIDs);
85 else if (TA.getKind() == TemplateArgument::Declaration)
86 UuidForTA =
87 GetUuidAttrOfType(TA.getAsDecl()->getType(), &SeenMultipleGUIDs);
88
89 // If the template argument has a UUID, there are three cases:
90 // - This is the first UUID seen for this RecordDecl.
David Majnemer37c921e2013-09-07 20:21:47 +000091 // - This is a different UUID than previously seen for this RecordDecl.
92 // - This is the same UUID than previously seen for this RecordDecl.
David Majnemer59c0ec22013-09-07 06:59:46 +000093 if (UuidForTA) {
94 if (!UuidForRD)
95 UuidForRD = UuidForTA;
96 else if (UuidForRD != UuidForTA)
97 SeenMultipleGUIDs = true;
98 }
99
100 // Seeing multiple UUIDs means that we couldn't find a UUID
101 if (SeenMultipleGUIDs) {
102 if (RDHasMultipleGUIDsPtr)
103 *RDHasMultipleGUIDsPtr = true;
Craig Topper36250ad2014-05-12 05:36:57 +0000104 return nullptr;
David Majnemer59c0ec22013-09-07 06:59:46 +0000105 }
106 }
107
108 return UuidForRD;
David Majnemer5d22e7e2013-09-07 07:11:04 +0000109 }
110
Aaron Ballman86c93902014-03-06 23:45:36 +0000111 for (auto I : RD->redecls())
112 if (auto Uuid = I->getAttr<UuidAttr>())
David Majnemer5d22e7e2013-09-07 07:11:04 +0000113 return Uuid;
Nico Webercf4ff5862012-10-11 10:13:44 +0000114
Craig Topper36250ad2014-05-12 05:36:57 +0000115 return nullptr;
Nico Webercf4ff5862012-10-11 10:13:44 +0000116}
117
David Majnemer8eaab6f2013-08-13 06:32:20 +0000118StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const {
119 StringRef Uuid;
120 if (isTypeOperand())
David Majnemer143c55e2013-09-27 07:04:31 +0000121 Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand(Context))->getGuid();
David Majnemer8eaab6f2013-08-13 06:32:20 +0000122 else {
123 // Special case: __uuidof(0) means an all-zero GUID.
124 Expr *Op = getExprOperand();
125 if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
126 Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
127 else
128 Uuid = "00000000-0000-0000-0000-000000000000";
129 }
130 return Uuid;
131}
132
Douglas Gregor747eb782010-07-08 06:14:04 +0000133// CXXScalarValueInitExpr
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000134SourceLocation CXXScalarValueInitExpr::getLocStart() const {
135 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregor2b88c112010-09-08 00:15:04 +0000136}
137
Sebastian Redlbd150f42008-11-21 19:14:01 +0000138// CXXNewExpr
Craig Toppera31a8822013-08-22 07:09:37 +0000139CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
140 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Sebastian Redl6047f072012-02-16 12:22:20 +0000141 bool usualArrayDeleteWantsSize,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000142 ArrayRef<Expr*> placementArgs,
Sebastian Redl6047f072012-02-16 12:22:20 +0000143 SourceRange typeIdParens, Expr *arraySize,
144 InitializationStyle initializationStyle,
145 Expr *initializer, QualType ty,
146 TypeSourceInfo *allocatedTypeInfo,
David Blaikie7b97aef2012-11-07 00:12:38 +0000147 SourceRange Range, SourceRange directInitRange)
John McCall7decc9e2010-11-18 06:31:45 +0000148 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000149 ty->isDependentType(), ty->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000150 ty->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000151 ty->containsUnexpandedParameterPack()),
Craig Topper36250ad2014-05-12 05:36:57 +0000152 SubExprs(nullptr), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
Sebastian Redl6047f072012-02-16 12:22:20 +0000153 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
David Blaikie7b97aef2012-11-07 00:12:38 +0000154 Range(Range), DirectInitRange(directInitRange),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000155 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Craig Topper36250ad2014-05-12 05:36:57 +0000156 assert((initializer != nullptr || initializationStyle == NoInit) &&
Sebastian Redl6047f072012-02-16 12:22:20 +0000157 "Only NoInit can have no initializer.");
158 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
Craig Topper36250ad2014-05-12 05:36:57 +0000159 AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(),
160 initializer != nullptr);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000161 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000162 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000163 if (arraySize->isInstantiationDependent())
164 ExprBits.InstantiationDependent = true;
165
Douglas Gregora6e053e2010-12-15 01:34:56 +0000166 if (arraySize->containsUnexpandedParameterPack())
167 ExprBits.ContainsUnexpandedParameterPack = true;
168
Sebastian Redl351bb782008-12-02 14:43:59 +0000169 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000170 }
171
Sebastian Redl6047f072012-02-16 12:22:20 +0000172 if (initializer) {
173 if (initializer->isInstantiationDependent())
174 ExprBits.InstantiationDependent = true;
175
176 if (initializer->containsUnexpandedParameterPack())
177 ExprBits.ContainsUnexpandedParameterPack = true;
178
179 SubExprs[i++] = initializer;
180 }
181
Benjamin Kramerc215e762012-08-24 11:54:20 +0000182 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000183 if (placementArgs[j]->isInstantiationDependent())
184 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000185 if (placementArgs[j]->containsUnexpandedParameterPack())
186 ExprBits.ContainsUnexpandedParameterPack = true;
187
Sebastian Redlbd150f42008-11-21 19:14:01 +0000188 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000189 }
David Blaikie3a0de212012-11-08 22:53:48 +0000190
191 switch (getInitializationStyle()) {
192 case CallInit:
193 this->Range.setEnd(DirectInitRange.getEnd()); break;
194 case ListInit:
195 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000196 default:
197 if (TypeIdParens.isValid())
198 this->Range.setEnd(TypeIdParens.getEnd());
199 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000200 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000201}
202
Craig Toppera31a8822013-08-22 07:09:37 +0000203void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000204 unsigned numPlaceArgs, bool hasInitializer){
Craig Topper36250ad2014-05-12 05:36:57 +0000205 assert(SubExprs == nullptr && "SubExprs already allocated");
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000206 Array = isArray;
207 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000208
209 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000210 SubExprs = new (C) Stmt*[TotalSize];
211}
212
Craig Toppera31a8822013-08-22 07:09:37 +0000213bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
John McCall75f94982011-03-07 03:12:35 +0000214 return getOperatorNew()->getType()->
Sebastian Redl31ad7542011-03-13 17:09:40 +0000215 castAs<FunctionProtoType>()->isNothrow(Ctx);
John McCall75f94982011-03-07 03:12:35 +0000216}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000217
Sebastian Redlbd150f42008-11-21 19:14:01 +0000218// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000219QualType CXXDeleteExpr::getDestroyedType() const {
220 const Expr *Arg = getArgument();
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000221 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000222 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000223
224 if (ArgType->isDependentType() && !ArgType->isPointerType())
225 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000226
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000227 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000228}
229
Douglas Gregorad8a3362009-09-04 17:36:40 +0000230// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000231PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
232 : Type(Info)
233{
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000234 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000235}
236
Craig Toppera31a8822013-08-22 07:09:37 +0000237CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000238 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
239 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
240 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
241 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000242 : Expr(CXXPseudoDestructorExprClass,
Reid Kleckner78af0702013-08-27 23:08:25 +0000243 Context.getPointerType(Context.getFunctionType(
244 Context.VoidTy, None,
245 FunctionProtoType::ExtProtoInfo(
246 Context.getDefaultCallingConvention(false, true)))),
John McCalldb40c7f2010-12-14 08:05:40 +0000247 VK_RValue, OK_Ordinary,
248 /*isTypeDependent=*/(Base->isTypeDependent() ||
249 (DestroyedType.getTypeSourceInfo() &&
250 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000251 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000252 (Base->isInstantiationDependent() ||
253 (QualifierLoc &&
254 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
255 (ScopeType &&
256 ScopeType->getType()->isInstantiationDependentType()) ||
257 (DestroyedType.getTypeSourceInfo() &&
258 DestroyedType.getTypeSourceInfo()->getType()
259 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000260 // ContainsUnexpandedParameterPack
261 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000262 (QualifierLoc &&
263 QualifierLoc.getNestedNameSpecifier()
264 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000265 (ScopeType &&
266 ScopeType->getType()->containsUnexpandedParameterPack()) ||
267 (DestroyedType.getTypeSourceInfo() &&
268 DestroyedType.getTypeSourceInfo()->getType()
269 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000270 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000271 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000272 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
273 DestroyedType(DestroyedType) { }
274
Douglas Gregor678f90d2010-02-25 01:56:36 +0000275QualType CXXPseudoDestructorExpr::getDestroyedType() const {
276 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
277 return TInfo->getType();
278
279 return QualType();
280}
281
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000282SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000283 SourceLocation End = DestroyedType.getLocation();
284 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000285 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000286 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000287}
288
John McCalld14a8642009-11-21 08:51:07 +0000289// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000290UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000291UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000292 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000293 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000294 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000295 const DeclarationNameInfo &NameInfo,
296 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000297 const TemplateArgumentListInfo *Args,
298 UnresolvedSetIterator Begin,
299 UnresolvedSetIterator End)
John McCalle66edc12009-11-24 19:00:30 +0000300{
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000301 assert(Args || TemplateKWLoc.isValid());
302 unsigned num_args = Args ? Args->size() : 0;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000303 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000304 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000305 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
306 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000307 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000308 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000309}
310
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000311UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000312UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000313 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000314 unsigned NumTemplateArgs) {
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000315 std::size_t size = sizeof(UnresolvedLookupExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000316 if (HasTemplateKWAndArgsInfo)
317 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000318
Chris Lattner5c0b4052010-10-30 05:14:06 +0000319 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000320 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000321 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000322 return E;
323}
324
Craig Toppera31a8822013-08-22 07:09:37 +0000325OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000326 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000327 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000328 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000329 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000330 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000331 UnresolvedSetIterator End,
332 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000333 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000334 bool KnownContainsUnexpandedParameterPack)
335 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
336 KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000337 (KnownInstantiationDependent ||
338 NameInfo.isInstantiationDependent() ||
339 (QualifierLoc &&
340 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000341 (KnownContainsUnexpandedParameterPack ||
342 NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor0da1d432011-02-28 20:01:57 +0000343 (QualifierLoc &&
344 QualifierLoc.getNestedNameSpecifier()
345 ->containsUnexpandedParameterPack()))),
Benjamin Kramerb73f76b2012-02-26 20:37:14 +0000346 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
Craig Topper36250ad2014-05-12 05:36:57 +0000347 Results(nullptr), NumResults(End - Begin),
348 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
349 TemplateKWLoc.isValid()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000350 NumResults = End - Begin;
351 if (NumResults) {
352 // Determine whether this expression is type-dependent.
353 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
354 if ((*I)->getDeclContext()->isDependentContext() ||
355 isa<UnresolvedUsingValueDecl>(*I)) {
356 ExprBits.TypeDependent = true;
357 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000358 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000359 }
360 }
361
362 Results = static_cast<DeclAccessPair *>(
363 C.Allocate(sizeof(DeclAccessPair) * NumResults,
364 llvm::alignOf<DeclAccessPair>()));
365 memcpy(Results, &*Begin.getIterator(),
366 NumResults * sizeof(DeclAccessPair));
367 }
368
369 // If we have explicit template arguments, check for dependent
370 // template arguments and whether they contain any unexpanded pack
371 // expansions.
372 if (TemplateArgs) {
373 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000374 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000375 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000376 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
377 Dependent,
378 InstantiationDependent,
379 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000380
381 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000382 ExprBits.TypeDependent = true;
383 ExprBits.ValueDependent = true;
384 }
385 if (InstantiationDependent)
386 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000387 if (ContainsUnexpandedParameterPack)
388 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000389 } else if (TemplateKWLoc.isValid()) {
390 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000391 }
392
393 if (isTypeDependent())
394 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000395}
396
Craig Toppera31a8822013-08-22 07:09:37 +0000397void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000398 UnresolvedSetIterator Begin,
399 UnresolvedSetIterator End) {
Craig Topper36250ad2014-05-12 05:36:57 +0000400 assert(!Results && "Results already initialized!");
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000401 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000402 if (NumResults) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000403 Results = static_cast<DeclAccessPair *>(
404 C.Allocate(sizeof(DeclAccessPair) * NumResults,
405
406 llvm::alignOf<DeclAccessPair>()));
407 memcpy(Results, &*Begin.getIterator(),
408 NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000409 }
410}
411
John McCall8c12dc42010-04-22 18:44:12 +0000412CXXRecordDecl *OverloadExpr::getNamingClass() const {
413 if (isa<UnresolvedLookupExpr>(this))
414 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
415 else
416 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
417}
418
John McCall8cd78132009-11-19 22:55:06 +0000419// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000420DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000421 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000422 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000423 const DeclarationNameInfo &NameInfo,
424 const TemplateArgumentListInfo *Args)
425 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
426 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000427 (NameInfo.isInstantiationDependent() ||
428 (QualifierLoc &&
429 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000430 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000431 (QualifierLoc &&
432 QualifierLoc.getNestedNameSpecifier()
433 ->containsUnexpandedParameterPack()))),
434 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Craig Topper36250ad2014-05-12 05:36:57 +0000435 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000436{
437 if (Args) {
438 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000439 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000440 bool ContainsUnexpandedParameterPack
441 = ExprBits.ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000442 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
443 Dependent,
444 InstantiationDependent,
445 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000446 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000447 } else if (TemplateKWLoc.isValid()) {
448 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000449 }
450}
451
John McCalle66edc12009-11-24 19:00:30 +0000452DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000453DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000454 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000455 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000456 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000457 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000458 assert(QualifierLoc && "should be created for dependent qualifiers");
John McCalle66edc12009-11-24 19:00:30 +0000459 std::size_t size = sizeof(DependentScopeDeclRefExpr);
John McCalle66edc12009-11-24 19:00:30 +0000460 if (Args)
Abramo Bagnara7945c982012-01-27 09:46:47 +0000461 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
462 else if (TemplateKWLoc.isValid())
463 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000464 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000465 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
466 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000467}
468
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000469DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000470DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000471 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000472 unsigned NumTemplateArgs) {
473 std::size_t size = sizeof(DependentScopeDeclRefExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000474 if (HasTemplateKWAndArgsInfo)
475 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000476 void *Mem = C.Allocate(size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000477 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000478 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000479 SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000480 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000481 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000482 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000483}
484
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000485SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000486 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000487 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
488 return Loc;
489}
490
491SourceLocation CXXConstructExpr::getLocEnd() const {
492 if (isa<CXXTemporaryObjectExpr>(this))
493 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000494
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000495 if (ParenOrBraceRange.isValid())
496 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000497
498 SourceLocation End = Loc;
499 for (unsigned I = getNumArgs(); I > 0; --I) {
500 const Expr *Arg = getArg(I-1);
501 if (!Arg->isDefaultArgument()) {
502 SourceLocation NewEnd = Arg->getLocEnd();
503 if (NewEnd.isValid()) {
504 End = NewEnd;
505 break;
506 }
507 }
508 }
509
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000510 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000511}
512
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000513SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000514 OverloadedOperatorKind Kind = getOperator();
515 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
516 if (getNumArgs() == 1)
517 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000518 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000519 else
520 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000521 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000522 } else if (Kind == OO_Arrow) {
523 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000524 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000525 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000526 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000527 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000528 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000529 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000530 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000531 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000532 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000533 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000534 }
535}
536
Ted Kremenek98a24e32011-03-30 17:41:19 +0000537Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000538 const Expr *Callee = getCallee()->IgnoreParens();
539 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000540 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000541 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
542 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
543 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000544
545 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000546 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000547}
548
Ted Kremenek98a24e32011-03-30 17:41:19 +0000549CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
550 if (const MemberExpr *MemExpr =
551 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
552 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
553
554 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000555 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000556}
557
558
David Blaikiec0f58662012-05-03 16:25:49 +0000559CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000560 Expr* ThisArg = getImplicitObjectArgument();
561 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000562 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000563
564 if (ThisArg->getType()->isAnyPointerType())
565 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
566
567 return ThisArg->getType()->getAsCXXRecordDecl();
568}
569
Douglas Gregoref986e82009-11-12 15:31:47 +0000570
Douglas Gregore200adc2008-10-27 19:41:14 +0000571//===----------------------------------------------------------------------===//
572// Named casts
573//===----------------------------------------------------------------------===//
574
575/// getCastName - Get the name of the C++ cast being used, e.g.,
576/// "static_cast", "dynamic_cast", "reinterpret_cast", or
577/// "const_cast". The returned pointer must not be freed.
578const char *CXXNamedCastExpr::getCastName() const {
579 switch (getStmtClass()) {
580 case CXXStaticCastExprClass: return "static_cast";
581 case CXXDynamicCastExprClass: return "dynamic_cast";
582 case CXXReinterpretCastExprClass: return "reinterpret_cast";
583 case CXXConstCastExprClass: return "const_cast";
584 default: return "<invalid cast>";
585 }
586}
Douglas Gregordd04d332009-01-16 18:33:17 +0000587
Craig Toppera31a8822013-08-22 07:09:37 +0000588CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000589 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000590 CastKind K, Expr *Op,
591 const CXXCastPath *BasePath,
592 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000593 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000594 SourceLocation RParenLoc,
595 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000596 unsigned PathSize = (BasePath ? BasePath->size() : 0);
597 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
598 + PathSize * sizeof(CXXBaseSpecifier*));
599 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000600 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000601 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000602 if (PathSize) E->setCastPath(*BasePath);
603 return E;
604}
605
Craig Toppera31a8822013-08-22 07:09:37 +0000606CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000607 unsigned PathSize) {
608 void *Buffer =
609 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
610 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
611}
612
Craig Toppera31a8822013-08-22 07:09:37 +0000613CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000614 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000615 CastKind K, Expr *Op,
616 const CXXCastPath *BasePath,
617 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000618 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000619 SourceLocation RParenLoc,
620 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000621 unsigned PathSize = (BasePath ? BasePath->size() : 0);
622 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
623 + PathSize * sizeof(CXXBaseSpecifier*));
624 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000625 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000626 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000627 if (PathSize) E->setCastPath(*BasePath);
628 return E;
629}
630
Craig Toppera31a8822013-08-22 07:09:37 +0000631CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000632 unsigned PathSize) {
633 void *Buffer =
634 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
635 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
636}
637
Anders Carlsson267c0c92011-04-11 01:43:55 +0000638/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
639/// to always be null. For example:
640///
641/// struct A { };
642/// struct B final : A { };
643/// struct C { };
644///
645/// C *f(B* b) { return dynamic_cast<C*>(b); }
646bool CXXDynamicCastExpr::isAlwaysNull() const
647{
648 QualType SrcType = getSubExpr()->getType();
649 QualType DestType = getType();
650
651 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
652 SrcType = SrcPTy->getPointeeType();
653 DestType = DestType->castAs<PointerType>()->getPointeeType();
654 }
655
Alexis Hunt78e2b912012-06-19 23:44:55 +0000656 if (DestType->isVoidType())
657 return false;
658
Anders Carlsson267c0c92011-04-11 01:43:55 +0000659 const CXXRecordDecl *SrcRD =
660 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
661
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000662 if (!SrcRD->hasAttr<FinalAttr>())
663 return false;
664
Anders Carlsson267c0c92011-04-11 01:43:55 +0000665 const CXXRecordDecl *DestRD =
666 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
667
668 return !DestRD->isDerivedFrom(SrcRD);
669}
670
John McCallcf142162010-08-07 06:22:56 +0000671CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000672CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
673 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000674 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000675 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000676 SourceLocation RParenLoc,
677 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000678 unsigned PathSize = (BasePath ? BasePath->size() : 0);
679 void *Buffer =
680 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
681 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000682 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000683 RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000684 if (PathSize) E->setCastPath(*BasePath);
685 return E;
686}
687
688CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000689CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000690 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
691 + PathSize * sizeof(CXXBaseSpecifier*));
692 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
693}
694
Craig Toppera31a8822013-08-22 07:09:37 +0000695CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000696 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000697 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000698 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000699 SourceLocation RParenLoc,
700 SourceRange AngleBrackets) {
701 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000702}
703
Craig Toppera31a8822013-08-22 07:09:37 +0000704CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000705 return new (C) CXXConstCastExpr(EmptyShell());
706}
707
708CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000709CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000710 TypeSourceInfo *Written, CastKind K, Expr *Op,
711 const CXXCastPath *BasePath,
712 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000713 unsigned PathSize = (BasePath ? BasePath->size() : 0);
714 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
715 + PathSize * sizeof(CXXBaseSpecifier*));
716 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000717 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
John McCallcf142162010-08-07 06:22:56 +0000718 if (PathSize) E->setCastPath(*BasePath);
719 return E;
720}
721
722CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000723CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
John McCallcf142162010-08-07 06:22:56 +0000724 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
725 + PathSize * sizeof(CXXBaseSpecifier*));
726 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
727}
728
Eli Friedman89fe0d52013-08-15 22:02:56 +0000729SourceLocation CXXFunctionalCastExpr::getLocStart() const {
730 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
731}
732
733SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
734 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
735}
736
Richard Smithc67fdd42012-03-07 08:35:16 +0000737UserDefinedLiteral::LiteralOperatorKind
738UserDefinedLiteral::getLiteralOperatorKind() const {
739 if (getNumArgs() == 0)
740 return LOK_Template;
741 if (getNumArgs() == 2)
742 return LOK_String;
743
744 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
745 QualType ParamTy =
746 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
747 if (ParamTy->isPointerType())
748 return LOK_Raw;
749 if (ParamTy->isAnyCharacterType())
750 return LOK_Character;
751 if (ParamTy->isIntegerType())
752 return LOK_Integer;
753 if (ParamTy->isFloatingType())
754 return LOK_Floating;
755
756 llvm_unreachable("unknown kind of literal operator");
757}
758
759Expr *UserDefinedLiteral::getCookedLiteral() {
760#ifndef NDEBUG
761 LiteralOperatorKind LOK = getLiteralOperatorKind();
762 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
763#endif
764 return getArg(0);
765}
766
767const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
768 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
769}
John McCallcf142162010-08-07 06:22:56 +0000770
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000771CXXDefaultArgExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000772CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
Douglas Gregor033f6752009-12-23 23:03:06 +0000773 ParmVarDecl *Param, Expr *SubExpr) {
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000774 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
Douglas Gregor033f6752009-12-23 23:03:06 +0000775 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
776 SubExpr);
Douglas Gregor25ab25f2009-12-23 18:19:08 +0000777}
778
Craig Toppera31a8822013-08-22 07:09:37 +0000779CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000780 FieldDecl *Field, QualType T)
781 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
782 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
783 ? VK_XValue
784 : VK_RValue,
785 /*FIXME*/ OK_Ordinary, false, false, false, false),
786 Field(Field), Loc(Loc) {
787 assert(Field->hasInClassInitializer());
788}
789
Craig Toppera31a8822013-08-22 07:09:37 +0000790CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000791 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000792 return new (C) CXXTemporary(Destructor);
793}
794
Craig Toppera31a8822013-08-22 07:09:37 +0000795CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000796 CXXTemporary *Temp,
797 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000798 assert((SubExpr->getType()->isRecordType() ||
799 SubExpr->getType()->isArrayType()) &&
800 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000801
Douglas Gregora6e053e2010-12-15 01:34:56 +0000802 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000803}
804
Craig Toppera31a8822013-08-22 07:09:37 +0000805CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000806 CXXConstructorDecl *Cons,
Douglas Gregor2b88c112010-09-08 00:15:04 +0000807 TypeSourceInfo *Type,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000808 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000809 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000810 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000811 bool ListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000812 bool ZeroInitialization)
Douglas Gregor2b88c112010-09-08 00:15:04 +0000813 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
814 Type->getType().getNonReferenceType(),
815 Type->getTypeLoc().getBeginLoc(),
Benjamin Kramerc215e762012-08-24 11:54:20 +0000816 Cons, false, Args,
Richard Smithd59b8322012-12-19 01:39:02 +0000817 HadMultipleCandidates,
818 ListInitialization, ZeroInitialization,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000819 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
Chandler Carruth01718152010-10-25 08:47:36 +0000820 Type(Type) {
Douglas Gregor2b88c112010-09-08 00:15:04 +0000821}
822
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000823SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
824 return Type->getTypeLoc().getBeginLoc();
825}
826
827SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000828 SourceLocation Loc = getParenOrBraceRange().getEnd();
829 if (Loc.isInvalid() && getNumArgs())
830 Loc = getArg(getNumArgs()-1)->getLocEnd();
831 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000832}
Anders Carlsson6f287832009-04-21 02:22:11 +0000833
Craig Toppera31a8822013-08-22 07:09:37 +0000834CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000835 SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000836 CXXConstructorDecl *D, bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000837 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000838 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000839 bool ListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000840 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000841 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000842 SourceRange ParenOrBraceRange) {
Douglas Gregor85dabae2009-12-16 01:38:02 +0000843 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000844 Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000845 HadMultipleCandidates, ListInitialization,
846 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000847 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000848}
849
Craig Toppera31a8822013-08-22 07:09:37 +0000850CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
851 QualType T, SourceLocation Loc,
Anders Carlsson4b2434d2009-05-30 20:56:46 +0000852 CXXConstructorDecl *D, bool elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000853 ArrayRef<Expr*> args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000854 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000855 bool ListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000856 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000857 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000858 SourceRange ParenOrBraceRange)
Douglas Gregora6e053e2010-12-15 01:34:56 +0000859 : Expr(SC, T, VK_RValue, OK_Ordinary,
860 T->isDependentType(), T->isDependentType(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000861 T->isInstantiationDependentType(),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000862 T->containsUnexpandedParameterPack()),
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000863 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
864 NumArgs(args.size()),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000865 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
Sebastian Redla9351792012-02-11 23:51:47 +0000866 ListInitialization(ListInitialization),
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000867 ZeroInitialization(ZeroInitialization),
Craig Topper36250ad2014-05-12 05:36:57 +0000868 ConstructKind(ConstructKind), Args(nullptr)
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000869{
870 if (NumArgs) {
Benjamin Kramerc215e762012-08-24 11:54:20 +0000871 Args = new (C) Stmt*[args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000872
Benjamin Kramerc215e762012-08-24 11:54:20 +0000873 for (unsigned i = 0; i != args.size(); ++i) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000874 assert(args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000875
876 if (args[i]->isValueDependent())
877 ExprBits.ValueDependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000878 if (args[i]->isInstantiationDependent())
879 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000880 if (args[i]->containsUnexpandedParameterPack())
881 ExprBits.ContainsUnexpandedParameterPack = true;
882
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000883 Args[i] = args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000884 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000885 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000886}
887
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000888LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000889 LambdaCaptureKind Kind, VarDecl *Var,
890 SourceLocation EllipsisLoc)
Richard Smithba71c082013-05-16 06:20:58 +0000891 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
Douglas Gregore31e6062012-02-07 10:09:13 +0000892{
893 unsigned Bits = 0;
894 if (Implicit)
895 Bits |= Capture_Implicit;
896
897 switch (Kind) {
Craig Topper36250ad2014-05-12 05:36:57 +0000898 case LCK_This:
899 assert(!Var && "'this' capture cannot have a variable!");
Douglas Gregore31e6062012-02-07 10:09:13 +0000900 break;
901
902 case LCK_ByCopy:
903 Bits |= Capture_ByCopy;
904 // Fall through
905 case LCK_ByRef:
906 assert(Var && "capture must have a variable!");
907 break;
908 }
Richard Smithba71c082013-05-16 06:20:58 +0000909 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000910}
911
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000912LambdaCaptureKind LambdaCapture::getCaptureKind() const {
Richard Smithba71c082013-05-16 06:20:58 +0000913 Decl *D = DeclAndBits.getPointer();
914 if (!D)
Douglas Gregore31e6062012-02-07 10:09:13 +0000915 return LCK_This;
916
Richard Smithba71c082013-05-16 06:20:58 +0000917 return (DeclAndBits.getInt() & Capture_ByCopy) ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000918}
919
James Dennettddd36ff2013-08-09 23:08:25 +0000920LambdaExpr::LambdaExpr(QualType T,
Douglas Gregore31e6062012-02-07 10:09:13 +0000921 SourceRange IntroducerRange,
922 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000923 SourceLocation CaptureDefaultLoc,
924 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000925 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000926 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000927 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000928 ArrayRef<VarDecl *> ArrayIndexVars,
929 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000930 SourceLocation ClosingBrace,
931 bool ContainsUnexpandedParameterPack)
Douglas Gregore31e6062012-02-07 10:09:13 +0000932 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
933 T->isDependentType(), T->isDependentType(), T->isDependentType(),
Richard Smith2589b9802012-07-25 03:56:55 +0000934 ContainsUnexpandedParameterPack),
Douglas Gregore31e6062012-02-07 10:09:13 +0000935 IntroducerRange(IntroducerRange),
James Dennettddd36ff2013-08-09 23:08:25 +0000936 CaptureDefaultLoc(CaptureDefaultLoc),
Douglas Gregore5561632012-02-13 17:20:40 +0000937 NumCaptures(Captures.size()),
Douglas Gregore31e6062012-02-07 10:09:13 +0000938 CaptureDefault(CaptureDefault),
939 ExplicitParams(ExplicitParams),
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000940 ExplicitResultType(ExplicitResultType),
Douglas Gregore31e6062012-02-07 10:09:13 +0000941 ClosingBrace(ClosingBrace)
942{
943 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000944 CXXRecordDecl *Class = getLambdaClass();
945 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000946
947 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000948
949 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000950 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000951 Data.NumCaptures = NumCaptures;
952 Data.NumExplicitCaptures = 0;
953 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
954 Capture *ToCapture = Data.Captures;
955 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
956 if (Captures[I].isExplicit())
957 ++Data.NumExplicitCaptures;
958
959 *ToCapture++ = Captures[I];
960 }
961
962 // Copy initialization expressions for the non-static data members.
963 Stmt **Stored = getStoredStmts();
964 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
965 *Stored++ = CaptureInits[I];
966
967 // Copy the body of the lambda.
968 *Stored++ = getCallOperator()->getBody();
969
970 // Copy the array index variables, if any.
971 HasArrayIndexVars = !ArrayIndexVars.empty();
972 if (HasArrayIndexVars) {
973 assert(ArrayIndexStarts.size() == NumCaptures);
974 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
975 sizeof(VarDecl *) * ArrayIndexVars.size());
976 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
977 sizeof(unsigned) * Captures.size());
978 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
Douglas Gregor6f88e5e2012-02-21 04:17:39 +0000979 }
Douglas Gregore31e6062012-02-07 10:09:13 +0000980}
981
Craig Toppera31a8822013-08-22 07:09:37 +0000982LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
Douglas Gregore31e6062012-02-07 10:09:13 +0000983 CXXRecordDecl *Class,
984 SourceRange IntroducerRange,
985 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000986 SourceLocation CaptureDefaultLoc,
987 ArrayRef<Capture> Captures,
Douglas Gregore31e6062012-02-07 10:09:13 +0000988 bool ExplicitParams,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +0000989 bool ExplicitResultType,
Douglas Gregore31e6062012-02-07 10:09:13 +0000990 ArrayRef<Expr *> CaptureInits,
Douglas Gregore5561632012-02-13 17:20:40 +0000991 ArrayRef<VarDecl *> ArrayIndexVars,
992 ArrayRef<unsigned> ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +0000993 SourceLocation ClosingBrace,
994 bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000995 // Determine the type of the expression (i.e., the type of the
996 // function object we're creating).
997 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000998
Douglas Gregore5561632012-02-13 17:20:40 +0000999 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
Richard Smithc7520bf2012-08-21 05:42:49 +00001000 if (!ArrayIndexVars.empty()) {
1001 Size += sizeof(unsigned) * (Captures.size() + 1);
1002 // Realign for following VarDecl array.
Richard Smitha75e1cf2012-08-21 18:18:06 +00001003 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
Richard Smithc7520bf2012-08-21 05:42:49 +00001004 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
1005 }
Douglas Gregore5561632012-02-13 17:20:40 +00001006 void *Mem = Context.Allocate(Size);
James Dennettddd36ff2013-08-09 23:08:25 +00001007 return new (Mem) LambdaExpr(T, IntroducerRange,
1008 CaptureDefault, CaptureDefaultLoc, Captures,
1009 ExplicitParams, ExplicitResultType,
Douglas Gregor0c46b2b2012-02-13 22:00:16 +00001010 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
Richard Smith2589b9802012-07-25 03:56:55 +00001011 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +00001012}
1013
Craig Toppera31a8822013-08-22 07:09:37 +00001014LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1015 unsigned NumCaptures,
Douglas Gregor99ae8062012-02-14 17:54:36 +00001016 unsigned NumArrayIndexVars) {
1017 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
1018 if (NumArrayIndexVars)
1019 Size += sizeof(VarDecl) * NumArrayIndexVars
1020 + sizeof(unsigned) * (NumCaptures + 1);
1021 void *Mem = C.Allocate(Size);
1022 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1023}
1024
Douglas Gregorc8a73492012-02-13 15:44:47 +00001025LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001026 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001027}
1028
1029LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001030 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001031}
1032
James Dennett1575cb42014-05-27 19:13:04 +00001033LambdaExpr::capture_range LambdaExpr::captures() const {
1034 return capture_range(capture_begin(), capture_end());
1035}
1036
Douglas Gregorc8a73492012-02-13 15:44:47 +00001037LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1038 return capture_begin();
1039}
1040
1041LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1042 struct CXXRecordDecl::LambdaDefinitionData &Data
1043 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001044 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001045}
1046
James Dennett1575cb42014-05-27 19:13:04 +00001047LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1048 return capture_range(explicit_capture_begin(), explicit_capture_end());
1049}
1050
Douglas Gregorc8a73492012-02-13 15:44:47 +00001051LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1052 return explicit_capture_end();
1053}
1054
1055LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1056 return capture_end();
1057}
1058
James Dennett1575cb42014-05-27 19:13:04 +00001059LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1060 return capture_range(implicit_capture_begin(), implicit_capture_end());
1061}
1062
Douglas Gregor54fcea62012-02-13 16:35:30 +00001063ArrayRef<VarDecl *>
1064LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
Douglas Gregore5561632012-02-13 17:20:40 +00001065 assert(HasArrayIndexVars && "No array index-var data?");
Douglas Gregor54fcea62012-02-13 16:35:30 +00001066
1067 unsigned Index = Iter - capture_init_begin();
Matt Beaumont-Gayf2ee0672012-02-13 19:29:45 +00001068 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1069 "Capture index out-of-range");
Douglas Gregore5561632012-02-13 17:20:40 +00001070 VarDecl **IndexVars = getArrayIndexVars();
1071 unsigned *IndexStarts = getArrayIndexStarts();
Douglas Gregor54fcea62012-02-13 16:35:30 +00001072 return ArrayRef<VarDecl *>(IndexVars + IndexStarts[Index],
1073 IndexVars + IndexStarts[Index + 1]);
1074}
1075
Douglas Gregore31e6062012-02-07 10:09:13 +00001076CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1077 return getType()->getAsCXXRecordDecl();
1078}
1079
1080CXXMethodDecl *LambdaExpr::getCallOperator() const {
1081 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001082 return Record->getLambdaCallOperator();
1083}
1084
1085TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1086 CXXRecordDecl *Record = getLambdaClass();
1087 return Record->getGenericLambdaTemplateParameterList();
1088
Douglas Gregore31e6062012-02-07 10:09:13 +00001089}
1090
Douglas Gregor99ae8062012-02-14 17:54:36 +00001091CompoundStmt *LambdaExpr::getBody() const {
1092 if (!getStoredStmts()[NumCaptures])
1093 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1094
1095 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1096}
1097
Douglas Gregore31e6062012-02-07 10:09:13 +00001098bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001099 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001100}
1101
John McCall28fc7092011-11-10 05:35:25 +00001102ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1103 ArrayRef<CleanupObject> objects)
John McCall5d413782010-12-06 08:20:24 +00001104 : Expr(ExprWithCleanupsClass, subexpr->getType(),
John McCall7decc9e2010-11-18 06:31:45 +00001105 subexpr->getValueKind(), subexpr->getObjectKind(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001106 subexpr->isTypeDependent(), subexpr->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001107 subexpr->isInstantiationDependent(),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001108 subexpr->containsUnexpandedParameterPack()),
John McCall28fc7092011-11-10 05:35:25 +00001109 SubExpr(subexpr) {
1110 ExprWithCleanupsBits.NumObjects = objects.size();
1111 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1112 getObjectsBuffer()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001113}
1114
Craig Toppera31a8822013-08-22 07:09:37 +00001115ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
John McCall28fc7092011-11-10 05:35:25 +00001116 ArrayRef<CleanupObject> objects) {
1117 size_t size = sizeof(ExprWithCleanups)
1118 + objects.size() * sizeof(CleanupObject);
1119 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1120 return new (buffer) ExprWithCleanups(subexpr, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001121}
1122
John McCall28fc7092011-11-10 05:35:25 +00001123ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1124 : Expr(ExprWithCleanupsClass, empty) {
1125 ExprWithCleanupsBits.NumObjects = numObjects;
1126}
Chris Lattnercba86142010-05-10 00:25:06 +00001127
Craig Toppera31a8822013-08-22 07:09:37 +00001128ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1129 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001130 unsigned numObjects) {
1131 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1132 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1133 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001134}
1135
Douglas Gregor2b88c112010-09-08 00:15:04 +00001136CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001137 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001138 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001139 SourceLocation RParenLoc)
Douglas Gregor2b88c112010-09-08 00:15:04 +00001140 : Expr(CXXUnresolvedConstructExprClass,
1141 Type->getType().getNonReferenceType(),
Douglas Gregor6336f292011-07-08 15:50:43 +00001142 (Type->getType()->isLValueReferenceType() ? VK_LValue
1143 :Type->getType()->isRValueReferenceType()? VK_XValue
1144 :VK_RValue),
1145 OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001146 Type->getType()->isDependentType(), true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001147 Type->getType()->containsUnexpandedParameterPack()),
Douglas Gregor2b88c112010-09-08 00:15:04 +00001148 Type(Type),
Douglas Gregorce934142009-05-20 18:46:25 +00001149 LParenLoc(LParenLoc),
1150 RParenLoc(RParenLoc),
Benjamin Kramerc215e762012-08-24 11:54:20 +00001151 NumArgs(Args.size()) {
Douglas Gregorce934142009-05-20 18:46:25 +00001152 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
Benjamin Kramerc215e762012-08-24 11:54:20 +00001153 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001154 if (Args[I]->containsUnexpandedParameterPack())
1155 ExprBits.ContainsUnexpandedParameterPack = true;
1156
1157 StoredArgs[I] = Args[I];
1158 }
Douglas Gregorce934142009-05-20 18:46:25 +00001159}
1160
1161CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001162CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001163 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001164 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001165 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001166 SourceLocation RParenLoc) {
1167 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
Benjamin Kramerc215e762012-08-24 11:54:20 +00001168 sizeof(Expr *) * Args.size());
1169 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001170}
1171
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001172CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001173CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001174 Stmt::EmptyShell Empty;
1175 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1176 sizeof(Expr *) * NumArgs);
1177 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1178}
1179
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001180SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1181 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001182}
1183
Craig Toppera31a8822013-08-22 07:09:37 +00001184CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001185 Expr *Base, QualType BaseType,
1186 bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001187 SourceLocation OperatorLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001188 NestedNameSpecifierLoc QualifierLoc,
1189 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001190 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001191 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001192 const TemplateArgumentListInfo *TemplateArgs)
John McCall7decc9e2010-11-18 06:31:45 +00001193 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001194 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001195 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001196 (QualifierLoc &&
1197 QualifierLoc.getNestedNameSpecifier()
1198 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001199 MemberNameInfo.containsUnexpandedParameterPack())),
John McCall2d74de92009-12-01 22:10:20 +00001200 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Craig Topper36250ad2014-05-12 05:36:57 +00001201 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1202 TemplateKWLoc.isValid()),
Douglas Gregore16af532011-02-28 18:50:33 +00001203 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregor308047d2009-09-09 00:23:06 +00001204 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001205 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001206 if (TemplateArgs) {
1207 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001208 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001209 bool ContainsUnexpandedParameterPack = false;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001210 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1211 Dependent,
1212 InstantiationDependent,
1213 ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001214 if (ContainsUnexpandedParameterPack)
1215 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001216 } else if (TemplateKWLoc.isValid()) {
1217 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001218 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001219}
1220
Craig Toppera31a8822013-08-22 07:09:37 +00001221CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001222 Expr *Base, QualType BaseType,
1223 bool IsArrow,
1224 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001225 NestedNameSpecifierLoc QualifierLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001226 NamedDecl *FirstQualifierFoundInScope,
1227 DeclarationNameInfo MemberNameInfo)
1228 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001229 VK_LValue, OK_Ordinary, true, true, true,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001230 ((Base && Base->containsUnexpandedParameterPack()) ||
Douglas Gregore16af532011-02-28 18:50:33 +00001231 (QualifierLoc &&
1232 QualifierLoc.getNestedNameSpecifier()->
1233 containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +00001234 MemberNameInfo.containsUnexpandedParameterPack())),
1235 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001236 HasTemplateKWAndArgsInfo(false),
1237 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001238 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1239 MemberNameInfo(MemberNameInfo) { }
1240
John McCall8cd78132009-11-19 22:55:06 +00001241CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001242CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001243 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001244 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001245 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001246 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001247 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001248 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001249 const TemplateArgumentListInfo *TemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001250 if (!TemplateArgs && !TemplateKWLoc.isValid())
John McCall2d74de92009-12-01 22:10:20 +00001251 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1252 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001253 QualifierLoc,
John McCall2d74de92009-12-01 22:10:20 +00001254 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001255 MemberNameInfo);
Mike Stump11289f42009-09-09 15:08:12 +00001256
Abramo Bagnara7945c982012-01-27 09:46:47 +00001257 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1258 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1259 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001260
Chris Lattner5c0b4052010-10-30 05:14:06 +00001261 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
John McCall2d74de92009-12-01 22:10:20 +00001262 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1263 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001264 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001265 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001266 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001267 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001268}
1269
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001270CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001271CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001272 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001273 unsigned NumTemplateArgs) {
Abramo Bagnara7945c982012-01-27 09:46:47 +00001274 if (!HasTemplateKWAndArgsInfo)
Craig Topper36250ad2014-05-12 05:36:57 +00001275 return new (C) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001276 0, SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +00001277 NestedNameSpecifierLoc(),
1278 nullptr, DeclarationNameInfo());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001279
1280 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
Abramo Bagnara7945c982012-01-27 09:46:47 +00001281 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Chris Lattner5c0b4052010-10-30 05:14:06 +00001282 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001283 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001284 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001285 0, SourceLocation(),
1286 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001287 SourceLocation(), nullptr,
1288 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001289 E->HasTemplateKWAndArgsInfo = true;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001290 return E;
1291}
1292
Douglas Gregor0da1d432011-02-28 20:01:57 +00001293bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001294 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001295 return true;
1296
Douglas Gregor25b7e052011-03-02 21:06:53 +00001297 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001298}
1299
John McCall0009fcc2011-04-26 20:42:42 +00001300static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1301 UnresolvedSetIterator end) {
1302 do {
1303 NamedDecl *decl = *begin;
1304 if (isa<UnresolvedUsingValueDecl>(decl))
1305 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001306
1307 // Unresolved member expressions should only contain methods and
1308 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001309 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1310 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001311 return false;
1312 } while (++begin != end);
1313
1314 return true;
1315}
1316
Craig Toppera31a8822013-08-22 07:09:37 +00001317UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001318 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001319 Expr *Base, QualType BaseType,
1320 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001321 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001322 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001323 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001324 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001325 const TemplateArgumentListInfo *TemplateArgs,
1326 UnresolvedSetIterator Begin,
1327 UnresolvedSetIterator End)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001328 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1329 MemberNameInfo, TemplateArgs, Begin, End,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001330 // Dependent
1331 ((Base && Base->isTypeDependent()) ||
1332 BaseType->isDependentType()),
Douglas Gregor678d76c2011-07-01 01:22:09 +00001333 ((Base && Base->isInstantiationDependent()) ||
1334 BaseType->isInstantiationDependentType()),
Douglas Gregora6e053e2010-12-15 01:34:56 +00001335 // Contains unexpanded parameter pack
1336 ((Base && Base->containsUnexpandedParameterPack()) ||
1337 BaseType->containsUnexpandedParameterPack())),
John McCall1acbbb52010-02-02 06:20:04 +00001338 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1339 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001340
1341 // Check whether all of the members are non-static member functions,
1342 // and if so, mark give this bound-member type instead of overload type.
1343 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1344 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001345}
1346
Douglas Gregor0da1d432011-02-28 20:01:57 +00001347bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001348 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001349 return true;
1350
Douglas Gregor25b7e052011-03-02 21:06:53 +00001351 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001352}
1353
John McCall10eae182009-11-30 22:42:35 +00001354UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001355UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001356 Expr *Base, QualType BaseType, bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001357 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001358 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001359 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001360 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001361 const TemplateArgumentListInfo *TemplateArgs,
1362 UnresolvedSetIterator Begin,
1363 UnresolvedSetIterator End) {
John McCall10eae182009-11-30 22:42:35 +00001364 std::size_t size = sizeof(UnresolvedMemberExpr);
1365 if (TemplateArgs)
Abramo Bagnara7945c982012-01-27 09:46:47 +00001366 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1367 else if (TemplateKWLoc.isValid())
1368 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
John McCall10eae182009-11-30 22:42:35 +00001369
Chris Lattner5c0b4052010-10-30 05:14:06 +00001370 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Douglas Gregorc69978f2010-05-23 19:36:40 +00001371 return new (Mem) UnresolvedMemberExpr(C,
Douglas Gregora6e053e2010-12-15 01:34:56 +00001372 HasUnresolvedUsing, Base, BaseType,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001373 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001374 MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001375}
1376
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001377UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001378UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1379 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001380 unsigned NumTemplateArgs) {
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001381 std::size_t size = sizeof(UnresolvedMemberExpr);
Abramo Bagnara7945c982012-01-27 09:46:47 +00001382 if (HasTemplateKWAndArgsInfo)
1383 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001384
Chris Lattner5c0b4052010-10-30 05:14:06 +00001385 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001386 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001387 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001388 return E;
1389}
1390
John McCall58cc69d2010-01-27 01:50:18 +00001391CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1392 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1393
1394 // If there was a nested name specifier, it names the naming class.
1395 // It can't be dependent: after all, we were actually able to do the
1396 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001397 CXXRecordDecl *Record = nullptr;
John McCall1acbbb52010-02-02 06:20:04 +00001398 if (getQualifier()) {
John McCall424cec92011-01-19 06:33:43 +00001399 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001400 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001401 Record = T->getAsCXXRecordDecl();
1402 assert(Record && "qualifier in member expression does not name record");
1403 }
John McCall58cc69d2010-01-27 01:50:18 +00001404 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001405 else {
John McCall58cc69d2010-01-27 01:50:18 +00001406 QualType BaseType = getBaseType().getNonReferenceType();
1407 if (isArrow()) {
1408 const PointerType *PT = BaseType->getAs<PointerType>();
1409 assert(PT && "base of arrow member access is not pointer");
1410 BaseType = PT->getPointeeType();
1411 }
1412
Douglas Gregor9262f472010-04-27 18:19:34 +00001413 Record = BaseType->getAsCXXRecordDecl();
1414 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001415 }
1416
Douglas Gregor9262f472010-04-27 18:19:34 +00001417 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001418}
1419
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001420SubstNonTypeTemplateParmPackExpr::
1421SubstNonTypeTemplateParmPackExpr(QualType T,
1422 NonTypeTemplateParmDecl *Param,
1423 SourceLocation NameLoc,
1424 const TemplateArgument &ArgPack)
1425 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
Douglas Gregor678d76c2011-07-01 01:22:09 +00001426 true, true, true, true),
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001427 Param(Param), Arguments(ArgPack.pack_begin()),
1428 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1429
1430TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1431 return TemplateArgument(Arguments, NumArguments);
1432}
1433
Richard Smithb15fe3a2012-09-12 00:56:43 +00001434FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1435 SourceLocation NameLoc,
1436 unsigned NumParams,
1437 Decl * const *Params)
1438 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1439 true, true, true, true),
1440 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1441 if (Params)
1442 std::uninitialized_copy(Params, Params + NumParams,
1443 reinterpret_cast<Decl**>(this+1));
1444}
1445
1446FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001447FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001448 ParmVarDecl *ParamPack, SourceLocation NameLoc,
Dmitri Gribenkof8579502013-01-12 19:30:44 +00001449 ArrayRef<Decl *> Params) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001450 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1451 sizeof(ParmVarDecl*) * Params.size()))
1452 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1453}
1454
1455FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001456FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1457 unsigned NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001458 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1459 sizeof(ParmVarDecl*) * NumParams))
Craig Topper36250ad2014-05-12 05:36:57 +00001460 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001461}
1462
David Majnemerdaff3702014-05-01 17:50:17 +00001463void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1464 unsigned ManglingNumber) {
1465 // We only need extra state if we have to remember more than just the Stmt.
1466 if (!ExtendedBy)
1467 return;
1468
1469 // We may need to allocate extra storage for the mangling number and the
1470 // extended-by ValueDecl.
1471 if (!State.is<ExtraState *>()) {
1472 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1473 ES->Temporary = State.get<Stmt *>();
1474 State = ES;
1475 }
1476
1477 auto ES = State.get<ExtraState *>();
1478 ES->ExtendingDecl = ExtendedBy;
1479 ES->ManglingNumber = ManglingNumber;
1480}
1481
Douglas Gregor29c42f22012-02-24 07:38:34 +00001482TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1483 ArrayRef<TypeSourceInfo *> Args,
1484 SourceLocation RParenLoc,
1485 bool Value)
1486 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1487 /*TypeDependent=*/false,
1488 /*ValueDependent=*/false,
1489 /*InstantiationDependent=*/false,
1490 /*ContainsUnexpandedParameterPack=*/false),
1491 Loc(Loc), RParenLoc(RParenLoc)
1492{
1493 TypeTraitExprBits.Kind = Kind;
1494 TypeTraitExprBits.Value = Value;
1495 TypeTraitExprBits.NumArgs = Args.size();
1496
1497 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1498
1499 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1500 if (Args[I]->getType()->isDependentType())
1501 setValueDependent(true);
1502 if (Args[I]->getType()->isInstantiationDependentType())
1503 setInstantiationDependent(true);
1504 if (Args[I]->getType()->containsUnexpandedParameterPack())
1505 setContainsUnexpandedParameterPack(true);
1506
1507 ToArgs[I] = Args[I];
1508 }
1509}
1510
Craig Toppera31a8822013-08-22 07:09:37 +00001511TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001512 SourceLocation Loc,
1513 TypeTrait Kind,
1514 ArrayRef<TypeSourceInfo *> Args,
1515 SourceLocation RParenLoc,
1516 bool Value) {
1517 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1518 void *Mem = C.Allocate(Size);
1519 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1520}
1521
Craig Toppera31a8822013-08-22 07:09:37 +00001522TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001523 unsigned NumArgs) {
1524 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1525 void *Mem = C.Allocate(Size);
1526 return new (Mem) TypeTraitExpr(EmptyShell());
1527}
1528
David Blaikie68e081d2011-12-20 02:48:34 +00001529void ArrayTypeTraitExpr::anchor() { }