blob: a0d6113811232e0b662a421c210a1d77823858bb [file] [log] [blame]
Eugene Zelenko821f6982017-11-18 01:47:41 +00001//===- ExprCXX.cpp - (C++) Expression AST Node Implementation -------------===//
Ted Kremeneke3a0c142007-08-24 20:21:10 +00002//
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
Eugene Zelenko821f6982017-11-18 01:47:41 +000014#include "clang/AST/ExprCXX.h"
Benjamin Kramer4ab984e2012-07-04 20:19:54 +000015#include "clang/AST/ASTContext.h"
Benjamin Kramerea70eb32012-12-01 15:09:41 +000016#include "clang/AST/Attr.h"
Eugene Zelenko821f6982017-11-18 01:47:41 +000017#include "clang/AST/Decl.h"
18#include "clang/AST/DeclAccessPair.h"
19#include "clang/AST/DeclBase.h"
Douglas Gregor993603d2008-11-14 16:09:21 +000020#include "clang/AST/DeclCXX.h"
Eugene Zelenko821f6982017-11-18 01:47:41 +000021#include "clang/AST/DeclarationName.h"
22#include "clang/AST/Expr.h"
23#include "clang/AST/LambdaCapture.h"
24#include "clang/AST/NestedNameSpecifier.h"
25#include "clang/AST/TemplateBase.h"
26#include "clang/AST/Type.h"
Douglas Gregor651fe5e2010-02-24 23:40:28 +000027#include "clang/AST/TypeLoc.h"
Eugene Zelenko821f6982017-11-18 01:47:41 +000028#include "clang/Basic/LLVM.h"
29#include "clang/Basic/OperatorKinds.h"
30#include "clang/Basic/SourceLocation.h"
31#include "clang/Basic/Specifiers.h"
32#include "llvm/ADT/ArrayRef.h"
33#include "llvm/Support/Casting.h"
34#include "llvm/Support/ErrorHandling.h"
35#include <cassert>
36#include <cstddef>
37#include <cstring>
38#include <memory>
Ted Kremeneke3a0c142007-08-24 20:21:10 +000039
Eugene Zelenko821f6982017-11-18 01:47:41 +000040using namespace clang;
Douglas Gregor9da64192010-04-26 22:37:10 +000041
Ted Kremeneke3a0c142007-08-24 20:21:10 +000042//===----------------------------------------------------------------------===//
43// Child Iterators for iterating over subexpressions/substatements
44//===----------------------------------------------------------------------===//
45
Richard Smith66094432016-10-20 00:55:15 +000046bool CXXOperatorCallExpr::isInfixBinaryOp() const {
47 // An infix binary operator is any operator with two arguments other than
48 // operator() and operator[]. Note that none of these operators can have
49 // default arguments, so it suffices to check the number of argument
50 // expressions.
51 if (getNumArgs() != 2)
52 return false;
53
54 switch (getOperator()) {
55 case OO_Call: case OO_Subscript:
56 return false;
57 default:
58 return true;
59 }
60}
61
Richard Smithef8bf432012-08-13 20:08:14 +000062bool CXXTypeidExpr::isPotentiallyEvaluated() const {
63 if (isTypeOperand())
64 return false;
65
66 // C++11 [expr.typeid]p3:
67 // When typeid is applied to an expression other than a glvalue of
68 // polymorphic class type, [...] the expression is an unevaluated operand.
69 const Expr *E = getExprOperand();
70 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
71 if (RD->isPolymorphic() && E->isGLValue())
72 return true;
73
74 return false;
75}
76
David Majnemer143c55e2013-09-27 07:04:31 +000077QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
Douglas Gregor9da64192010-04-26 22:37:10 +000078 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000079 Qualifiers Quals;
80 return Context.getUnqualifiedArrayType(
81 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Douglas Gregor9da64192010-04-26 22:37:10 +000082}
83
David Majnemer143c55e2013-09-27 07:04:31 +000084QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
Francois Pichet9f4f2072010-09-08 12:20:18 +000085 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000086 Qualifiers Quals;
87 return Context.getUnqualifiedArrayType(
88 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Francois Pichet9f4f2072010-09-08 12:20:18 +000089}
90
Douglas Gregor747eb782010-07-08 06:14:04 +000091// CXXScalarValueInitExpr
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +000092SourceLocation CXXScalarValueInitExpr::getLocStart() const {
93 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
Douglas Gregor2b88c112010-09-08 00:15:04 +000094}
95
Sebastian Redlbd150f42008-11-21 19:14:01 +000096// CXXNewExpr
Craig Toppera31a8822013-08-22 07:09:37 +000097CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
98 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
Richard Smithb2f0f052016-10-10 18:54:32 +000099 bool PassAlignment, bool usualArrayDeleteWantsSize,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000100 ArrayRef<Expr*> placementArgs,
Sebastian Redl6047f072012-02-16 12:22:20 +0000101 SourceRange typeIdParens, Expr *arraySize,
102 InitializationStyle initializationStyle,
103 Expr *initializer, QualType ty,
104 TypeSourceInfo *allocatedTypeInfo,
David Blaikie7b97aef2012-11-07 00:12:38 +0000105 SourceRange Range, SourceRange directInitRange)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000106 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary, ty->isDependentType(),
107 ty->isDependentType(), ty->isInstantiationDependentType(),
108 ty->containsUnexpandedParameterPack()),
109 OperatorNew(operatorNew), OperatorDelete(operatorDelete),
110 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
111 Range(Range), DirectInitRange(directInitRange), GlobalNew(globalNew),
112 PassAlignment(PassAlignment),
113 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
Craig Topper36250ad2014-05-12 05:36:57 +0000114 assert((initializer != nullptr || initializationStyle == NoInit) &&
Sebastian Redl6047f072012-02-16 12:22:20 +0000115 "Only NoInit can have no initializer.");
116 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
Craig Topper36250ad2014-05-12 05:36:57 +0000117 AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(),
118 initializer != nullptr);
Sebastian Redlbd150f42008-11-21 19:14:01 +0000119 unsigned i = 0;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000120 if (Array) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000121 if (arraySize->isInstantiationDependent())
122 ExprBits.InstantiationDependent = true;
123
Douglas Gregora6e053e2010-12-15 01:34:56 +0000124 if (arraySize->containsUnexpandedParameterPack())
125 ExprBits.ContainsUnexpandedParameterPack = true;
126
Sebastian Redl351bb782008-12-02 14:43:59 +0000127 SubExprs[i++] = arraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000128 }
129
Sebastian Redl6047f072012-02-16 12:22:20 +0000130 if (initializer) {
131 if (initializer->isInstantiationDependent())
132 ExprBits.InstantiationDependent = true;
133
134 if (initializer->containsUnexpandedParameterPack())
135 ExprBits.ContainsUnexpandedParameterPack = true;
136
137 SubExprs[i++] = initializer;
138 }
139
Benjamin Kramerc215e762012-08-24 11:54:20 +0000140 for (unsigned j = 0; j != placementArgs.size(); ++j) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000141 if (placementArgs[j]->isInstantiationDependent())
142 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000143 if (placementArgs[j]->containsUnexpandedParameterPack())
144 ExprBits.ContainsUnexpandedParameterPack = true;
145
Sebastian Redlbd150f42008-11-21 19:14:01 +0000146 SubExprs[i++] = placementArgs[j];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000147 }
David Blaikie3a0de212012-11-08 22:53:48 +0000148
149 switch (getInitializationStyle()) {
150 case CallInit:
151 this->Range.setEnd(DirectInitRange.getEnd()); break;
152 case ListInit:
153 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000154 default:
155 if (TypeIdParens.isValid())
156 this->Range.setEnd(TypeIdParens.getEnd());
157 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000158 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000159}
160
Craig Toppera31a8822013-08-22 07:09:37 +0000161void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
Sebastian Redl6047f072012-02-16 12:22:20 +0000162 unsigned numPlaceArgs, bool hasInitializer){
Craig Topper36250ad2014-05-12 05:36:57 +0000163 assert(SubExprs == nullptr && "SubExprs already allocated");
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000164 Array = isArray;
165 NumPlacementArgs = numPlaceArgs;
Sebastian Redl6047f072012-02-16 12:22:20 +0000166
167 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000168 SubExprs = new (C) Stmt*[TotalSize];
169}
170
Craig Toppera31a8822013-08-22 07:09:37 +0000171bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
Richard Smith902a0232015-02-14 01:52:20 +0000172 return getOperatorNew()->getType()->castAs<FunctionProtoType>()->isNothrow(
173 Ctx) &&
174 !getOperatorNew()->isReservedGlobalPlacementOperator();
John McCall75f94982011-03-07 03:12:35 +0000175}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000176
Sebastian Redlbd150f42008-11-21 19:14:01 +0000177// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000178QualType CXXDeleteExpr::getDestroyedType() const {
179 const Expr *Arg = getArgument();
Richard Smith5b349582017-10-13 01:55:36 +0000180
181 // For a destroying operator delete, we may have implicitly converted the
182 // pointer type to the type of the parameter of the 'operator delete'
183 // function.
184 while (auto *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
185 if (ICE->getCastKind() == CK_DerivedToBase ||
186 ICE->getCastKind() == CK_UncheckedDerivedToBase ||
187 ICE->getCastKind() == CK_NoOp) {
188 assert((ICE->getCastKind() == CK_NoOp ||
189 getOperatorDelete()->isDestroyingOperatorDelete()) &&
190 "only a destroying operator delete can have a converted arg");
191 Arg = ICE->getSubExpr();
192 } else
193 break;
194 }
195
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000196 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000197 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000198
199 if (ArgType->isDependentType() && !ArgType->isPointerType())
200 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000201
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000202 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000203}
204
Douglas Gregorad8a3362009-09-04 17:36:40 +0000205// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000206PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000207 : Type(Info) {
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000208 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000209}
210
Craig Toppera31a8822013-08-22 07:09:37 +0000211CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000212 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
213 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
214 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
215 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000216 : Expr(CXXPseudoDestructorExprClass,
David Majnemerced8bdf2015-02-25 17:36:15 +0000217 Context.BoundMemberTy,
John McCalldb40c7f2010-12-14 08:05:40 +0000218 VK_RValue, OK_Ordinary,
219 /*isTypeDependent=*/(Base->isTypeDependent() ||
220 (DestroyedType.getTypeSourceInfo() &&
221 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000222 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000223 (Base->isInstantiationDependent() ||
224 (QualifierLoc &&
225 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
226 (ScopeType &&
227 ScopeType->getType()->isInstantiationDependentType()) ||
228 (DestroyedType.getTypeSourceInfo() &&
229 DestroyedType.getTypeSourceInfo()->getType()
230 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000231 // ContainsUnexpandedParameterPack
232 (Base->containsUnexpandedParameterPack() ||
Douglas Gregora6ce6082011-02-25 18:19:59 +0000233 (QualifierLoc &&
234 QualifierLoc.getNestedNameSpecifier()
235 ->containsUnexpandedParameterPack()) ||
Douglas Gregora6e053e2010-12-15 01:34:56 +0000236 (ScopeType &&
237 ScopeType->getType()->containsUnexpandedParameterPack()) ||
238 (DestroyedType.getTypeSourceInfo() &&
239 DestroyedType.getTypeSourceInfo()->getType()
240 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000241 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000242 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000243 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
Eugene Zelenko821f6982017-11-18 01:47:41 +0000244 DestroyedType(DestroyedType) {}
John McCalldb40c7f2010-12-14 08:05:40 +0000245
Douglas Gregor678f90d2010-02-25 01:56:36 +0000246QualType CXXPseudoDestructorExpr::getDestroyedType() const {
247 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
248 return TInfo->getType();
249
250 return QualType();
251}
252
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000253SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000254 SourceLocation End = DestroyedType.getLocation();
255 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000256 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000257 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000258}
259
John McCalld14a8642009-11-21 08:51:07 +0000260// UnresolvedLookupExpr
John McCalle66edc12009-11-24 19:00:30 +0000261UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000262UnresolvedLookupExpr::Create(const ASTContext &C,
John McCall58cc69d2010-01-27 01:50:18 +0000263 CXXRecordDecl *NamingClass,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000264 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000265 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000266 const DeclarationNameInfo &NameInfo,
267 bool ADL,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000268 const TemplateArgumentListInfo *Args,
269 UnresolvedSetIterator Begin,
Eugene Zelenko821f6982017-11-18 01:47:41 +0000270 UnresolvedSetIterator End) {
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000271 assert(Args || TemplateKWLoc.isValid());
272 unsigned num_args = Args ? Args->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +0000273
274 std::size_t Size =
275 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(1,
276 num_args);
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000277 void *Mem = C.Allocate(Size, alignof(UnresolvedLookupExpr));
Abramo Bagnara7945c982012-01-27 09:46:47 +0000278 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
279 TemplateKWLoc, NameInfo,
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000280 ADL, /*Overload*/ true, Args,
Richard Smithb6626742012-10-18 17:56:02 +0000281 Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000282}
283
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000284UnresolvedLookupExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000285UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000286 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +0000287 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000288 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
289 std::size_t Size =
290 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
291 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000292 void *Mem = C.Allocate(Size, alignof(UnresolvedLookupExpr));
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000293 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +0000294 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000295 return E;
296}
297
Craig Toppera31a8822013-08-22 07:09:37 +0000298OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000299 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000300 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000301 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000302 const TemplateArgumentListInfo *TemplateArgs,
Douglas Gregorc69978f2010-05-23 19:36:40 +0000303 UnresolvedSetIterator Begin,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000304 UnresolvedSetIterator End,
305 bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000306 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000307 bool KnownContainsUnexpandedParameterPack)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000308 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
309 KnownDependent,
310 (KnownInstantiationDependent ||
311 NameInfo.isInstantiationDependent() ||
312 (QualifierLoc &&
313 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
314 (KnownContainsUnexpandedParameterPack ||
315 NameInfo.containsUnexpandedParameterPack() ||
316 (QualifierLoc &&
317 QualifierLoc.getNestedNameSpecifier()
318 ->containsUnexpandedParameterPack()))),
319 NameInfo(NameInfo), QualifierLoc(QualifierLoc), NumResults(End - Begin),
320 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
321 TemplateKWLoc.isValid()) {
Douglas Gregora6e053e2010-12-15 01:34:56 +0000322 NumResults = End - Begin;
323 if (NumResults) {
324 // Determine whether this expression is type-dependent.
325 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
326 if ((*I)->getDeclContext()->isDependentContext() ||
327 isa<UnresolvedUsingValueDecl>(*I)) {
328 ExprBits.TypeDependent = true;
329 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000330 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000331 }
332 }
333
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000334 Results = static_cast<DeclAccessPair *>(C.Allocate(
335 sizeof(DeclAccessPair) * NumResults, alignof(DeclAccessPair)));
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000336 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregora6e053e2010-12-15 01:34:56 +0000337 }
338
339 // If we have explicit template arguments, check for dependent
340 // template arguments and whether they contain any unexpanded pack
341 // expansions.
342 if (TemplateArgs) {
343 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000344 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000345 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +0000346 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
347 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
348 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000349
350 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000351 ExprBits.TypeDependent = true;
352 ExprBits.ValueDependent = true;
353 }
354 if (InstantiationDependent)
355 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000356 if (ContainsUnexpandedParameterPack)
357 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000358 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000359 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000360 }
361
362 if (isTypeDependent())
363 setType(C.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000364}
365
Craig Toppera31a8822013-08-22 07:09:37 +0000366void OverloadExpr::initializeResults(const ASTContext &C,
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000367 UnresolvedSetIterator Begin,
368 UnresolvedSetIterator End) {
Craig Topper36250ad2014-05-12 05:36:57 +0000369 assert(!Results && "Results already initialized!");
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000370 NumResults = End - Begin;
Douglas Gregorc69978f2010-05-23 19:36:40 +0000371 if (NumResults) {
Benjamin Kramerc3f89252016-10-20 14:27:22 +0000372 Results = static_cast<DeclAccessPair *>(
373 C.Allocate(sizeof(DeclAccessPair) * NumResults,
374
375 alignof(DeclAccessPair)));
376 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregorc69978f2010-05-23 19:36:40 +0000377 }
378}
379
John McCall8c12dc42010-04-22 18:44:12 +0000380CXXRecordDecl *OverloadExpr::getNamingClass() const {
381 if (isa<UnresolvedLookupExpr>(this))
382 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
383 else
384 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
385}
386
John McCall8cd78132009-11-19 22:55:06 +0000387// DependentScopeDeclRefExpr
Douglas Gregora6e053e2010-12-15 01:34:56 +0000388DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000389 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000390 SourceLocation TemplateKWLoc,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000391 const DeclarationNameInfo &NameInfo,
392 const TemplateArgumentListInfo *Args)
393 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
394 true, true,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000395 (NameInfo.isInstantiationDependent() ||
396 (QualifierLoc &&
397 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000398 (NameInfo.containsUnexpandedParameterPack() ||
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000399 (QualifierLoc &&
400 QualifierLoc.getNestedNameSpecifier()
401 ->containsUnexpandedParameterPack()))),
402 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
Craig Topper36250ad2014-05-12 05:36:57 +0000403 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000404{
405 if (Args) {
406 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000407 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000408 bool ContainsUnexpandedParameterPack
409 = ExprBits.ContainsUnexpandedParameterPack;
James Y Knighte7d82282015-12-29 18:15:14 +0000410 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
411 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
412 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000413 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000414 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000415 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
416 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000417 }
418}
419
John McCalle66edc12009-11-24 19:00:30 +0000420DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000421DependentScopeDeclRefExpr::Create(const ASTContext &C,
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000422 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000423 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000424 const DeclarationNameInfo &NameInfo,
John McCalle66edc12009-11-24 19:00:30 +0000425 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000426 assert(QualifierLoc && "should be created for dependent qualifiers");
James Y Knighte7d82282015-12-29 18:15:14 +0000427 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
428 std::size_t Size =
429 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
430 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
431 void *Mem = C.Allocate(Size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000432 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
433 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000434}
435
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000436DependentScopeDeclRefExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000437DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000438 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000439 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000440 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
441 std::size_t Size =
442 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
443 HasTemplateKWAndArgsInfo, NumTemplateArgs);
444 void *Mem = C.Allocate(Size);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000445 DependentScopeDeclRefExpr *E
Douglas Gregor3a43fd62011-02-25 20:49:16 +0000446 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
Abramo Bagnara7945c982012-01-27 09:46:47 +0000447 SourceLocation(),
Craig Topper36250ad2014-05-12 05:36:57 +0000448 DeclarationNameInfo(), nullptr);
Abramo Bagnara7945c982012-01-27 09:46:47 +0000449 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000450 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000451}
452
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000453SourceLocation CXXConstructExpr::getLocStart() const {
John McCall701417a2011-02-21 06:23:05 +0000454 if (isa<CXXTemporaryObjectExpr>(this))
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000455 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
456 return Loc;
457}
458
459SourceLocation CXXConstructExpr::getLocEnd() const {
460 if (isa<CXXTemporaryObjectExpr>(this))
461 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
John McCall701417a2011-02-21 06:23:05 +0000462
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000463 if (ParenOrBraceRange.isValid())
464 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000465
466 SourceLocation End = Loc;
467 for (unsigned I = getNumArgs(); I > 0; --I) {
468 const Expr *Arg = getArg(I-1);
469 if (!Arg->isDefaultArgument()) {
470 SourceLocation NewEnd = Arg->getLocEnd();
471 if (NewEnd.isValid()) {
472 End = NewEnd;
473 break;
474 }
475 }
476 }
477
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000478 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000479}
480
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000481SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000482 OverloadedOperatorKind Kind = getOperator();
483 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
484 if (getNumArgs() == 1)
485 // Prefix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000486 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000487 else
488 // Postfix operator
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000489 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000490 } else if (Kind == OO_Arrow) {
491 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000492 } else if (Kind == OO_Call) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000493 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000494 } else if (Kind == OO_Subscript) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000495 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000496 } else if (getNumArgs() == 1) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000497 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000498 } else if (getNumArgs() == 2) {
Argyrios Kyrtzidis5f20a7e2012-05-01 22:19:11 +0000499 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
Douglas Gregor993603d2008-11-14 16:09:21 +0000500 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000501 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000502 }
503}
504
Ted Kremenek98a24e32011-03-30 17:41:19 +0000505Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000506 const Expr *Callee = getCallee()->IgnoreParens();
507 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000508 return MemExpr->getBase();
Jordan Rose16fe35e2012-08-03 23:08:39 +0000509 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
510 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
511 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000512
513 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000514 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000515}
516
Ted Kremenek98a24e32011-03-30 17:41:19 +0000517CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
518 if (const MemberExpr *MemExpr =
519 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
520 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
521
522 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000523 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000524}
525
David Blaikiec0f58662012-05-03 16:25:49 +0000526CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000527 Expr* ThisArg = getImplicitObjectArgument();
528 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000529 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000530
531 if (ThisArg->getType()->isAnyPointerType())
532 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
533
534 return ThisArg->getType()->getAsCXXRecordDecl();
535}
536
Douglas Gregore200adc2008-10-27 19:41:14 +0000537//===----------------------------------------------------------------------===//
538// Named casts
539//===----------------------------------------------------------------------===//
540
541/// getCastName - Get the name of the C++ cast being used, e.g.,
542/// "static_cast", "dynamic_cast", "reinterpret_cast", or
543/// "const_cast". The returned pointer must not be freed.
544const char *CXXNamedCastExpr::getCastName() const {
545 switch (getStmtClass()) {
546 case CXXStaticCastExprClass: return "static_cast";
547 case CXXDynamicCastExprClass: return "dynamic_cast";
548 case CXXReinterpretCastExprClass: return "reinterpret_cast";
549 case CXXConstCastExprClass: return "const_cast";
550 default: return "<invalid cast>";
551 }
552}
Douglas Gregordd04d332009-01-16 18:33:17 +0000553
Craig Toppera31a8822013-08-22 07:09:37 +0000554CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000555 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000556 CastKind K, Expr *Op,
557 const CXXCastPath *BasePath,
558 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000559 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000560 SourceLocation RParenLoc,
561 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000562 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000563 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000564 CXXStaticCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000565 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000566 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000567 if (PathSize)
568 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
569 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000570 return E;
571}
572
Craig Toppera31a8822013-08-22 07:09:37 +0000573CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000574 unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000575 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000576 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
577}
578
Craig Toppera31a8822013-08-22 07:09:37 +0000579CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000580 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000581 CastKind K, Expr *Op,
582 const CXXCastPath *BasePath,
583 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000584 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000585 SourceLocation RParenLoc,
586 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000587 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000588 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000589 CXXDynamicCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000590 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000591 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000592 if (PathSize)
593 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
594 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000595 return E;
596}
597
Craig Toppera31a8822013-08-22 07:09:37 +0000598CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000599 unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000600 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000601 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
602}
603
Anders Carlsson267c0c92011-04-11 01:43:55 +0000604/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
605/// to always be null. For example:
606///
607/// struct A { };
608/// struct B final : A { };
609/// struct C { };
610///
611/// C *f(B* b) { return dynamic_cast<C*>(b); }
612bool CXXDynamicCastExpr::isAlwaysNull() const
613{
614 QualType SrcType = getSubExpr()->getType();
615 QualType DestType = getType();
616
617 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
618 SrcType = SrcPTy->getPointeeType();
619 DestType = DestType->castAs<PointerType>()->getPointeeType();
620 }
621
Alexis Hunt78e2b912012-06-19 23:44:55 +0000622 if (DestType->isVoidType())
623 return false;
624
Anders Carlsson267c0c92011-04-11 01:43:55 +0000625 const CXXRecordDecl *SrcRD =
626 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
627
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000628 if (!SrcRD->hasAttr<FinalAttr>())
629 return false;
630
Anders Carlsson267c0c92011-04-11 01:43:55 +0000631 const CXXRecordDecl *DestRD =
632 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
633
634 return !DestRD->isDerivedFrom(SrcRD);
635}
636
John McCallcf142162010-08-07 06:22:56 +0000637CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000638CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
639 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000640 const CXXCastPath *BasePath,
Douglas Gregor4478f852011-01-12 22:41:29 +0000641 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000642 SourceLocation RParenLoc,
643 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000644 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000645 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000646 CXXReinterpretCastExpr *E =
Douglas Gregor4478f852011-01-12 22:41:29 +0000647 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000648 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000649 if (PathSize)
650 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
651 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000652 return E;
653}
654
655CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000656CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000657 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000658 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
659}
660
Craig Toppera31a8822013-08-22 07:09:37 +0000661CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000662 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000663 TypeSourceInfo *WrittenTy,
Douglas Gregor4478f852011-01-12 22:41:29 +0000664 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000665 SourceLocation RParenLoc,
666 SourceRange AngleBrackets) {
667 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000668}
669
Craig Toppera31a8822013-08-22 07:09:37 +0000670CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000671 return new (C) CXXConstCastExpr(EmptyShell());
672}
673
674CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000675CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000676 TypeSourceInfo *Written, CastKind K, Expr *Op,
677 const CXXCastPath *BasePath,
678 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000679 unsigned PathSize = (BasePath ? BasePath->size() : 0);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000680 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000681 CXXFunctionalCastExpr *E =
Eli Friedman89fe0d52013-08-15 22:02:56 +0000682 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000683 if (PathSize)
684 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
685 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000686 return E;
687}
688
689CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000690CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
James Y Knight1d75c5e2015-12-30 02:27:28 +0000691 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000692 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
693}
694
Eli Friedman89fe0d52013-08-15 22:02:56 +0000695SourceLocation CXXFunctionalCastExpr::getLocStart() const {
696 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
697}
698
699SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
700 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
701}
702
Richard Smithc67fdd42012-03-07 08:35:16 +0000703UserDefinedLiteral::LiteralOperatorKind
704UserDefinedLiteral::getLiteralOperatorKind() const {
705 if (getNumArgs() == 0)
706 return LOK_Template;
707 if (getNumArgs() == 2)
708 return LOK_String;
709
710 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
711 QualType ParamTy =
712 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
713 if (ParamTy->isPointerType())
714 return LOK_Raw;
715 if (ParamTy->isAnyCharacterType())
716 return LOK_Character;
717 if (ParamTy->isIntegerType())
718 return LOK_Integer;
719 if (ParamTy->isFloatingType())
720 return LOK_Floating;
721
722 llvm_unreachable("unknown kind of literal operator");
723}
724
725Expr *UserDefinedLiteral::getCookedLiteral() {
726#ifndef NDEBUG
727 LiteralOperatorKind LOK = getLiteralOperatorKind();
728 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
729#endif
730 return getArg(0);
731}
732
733const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
734 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
735}
John McCallcf142162010-08-07 06:22:56 +0000736
Craig Toppera31a8822013-08-22 07:09:37 +0000737CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
Richard Smith852c9db2013-04-20 22:23:05 +0000738 FieldDecl *Field, QualType T)
739 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
740 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
741 ? VK_XValue
742 : VK_RValue,
743 /*FIXME*/ OK_Ordinary, false, false, false, false),
744 Field(Field), Loc(Loc) {
745 assert(Field->hasInClassInitializer());
746}
747
Craig Toppera31a8822013-08-22 07:09:37 +0000748CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000749 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000750 return new (C) CXXTemporary(Destructor);
751}
752
Craig Toppera31a8822013-08-22 07:09:37 +0000753CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000754 CXXTemporary *Temp,
755 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000756 assert((SubExpr->getType()->isRecordType() ||
757 SubExpr->getType()->isArrayType()) &&
758 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000759
Douglas Gregora6e053e2010-12-15 01:34:56 +0000760 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000761}
762
Craig Toppera31a8822013-08-22 07:09:37 +0000763CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
Anders Carlsson56c5bd82009-04-24 05:23:13 +0000764 CXXConstructorDecl *Cons,
Richard Smith60437622017-02-09 19:17:44 +0000765 QualType Type,
766 TypeSourceInfo *TSI,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000767 ArrayRef<Expr*> Args,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000768 SourceRange ParenOrBraceRange,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000769 bool HadMultipleCandidates,
Richard Smithd59b8322012-12-19 01:39:02 +0000770 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000771 bool StdInitListInitialization,
Douglas Gregor199db362010-04-27 20:36:09 +0000772 bool ZeroInitialization)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000773 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, Type,
774 TSI->getTypeLoc().getBeginLoc(), Cons, false, Args,
775 HadMultipleCandidates, ListInitialization,
776 StdInitListInitialization, ZeroInitialization,
777 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
778 Type(TSI) {}
Douglas Gregor2b88c112010-09-08 00:15:04 +0000779
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000780SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
781 return Type->getTypeLoc().getBeginLoc();
782}
783
784SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000785 SourceLocation Loc = getParenOrBraceRange().getEnd();
786 if (Loc.isInvalid() && getNumArgs())
787 Loc = getArg(getNumArgs()-1)->getLocEnd();
788 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000789}
Anders Carlsson6f287832009-04-21 02:22:11 +0000790
Craig Toppera31a8822013-08-22 07:09:37 +0000791CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor85dabae2009-12-16 01:38:02 +0000792 SourceLocation Loc,
Richard Smithc2bebe92016-05-11 20:37:46 +0000793 CXXConstructorDecl *Ctor,
794 bool Elidable,
Benjamin Kramerc215e762012-08-24 11:54:20 +0000795 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000796 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000797 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000798 bool StdInitListInitialization,
Douglas Gregor7ae2d772010-01-31 09:12:51 +0000799 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000800 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000801 SourceRange ParenOrBraceRange) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000802 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc,
Richard Smithc83bf822016-06-10 00:58:19 +0000803 Ctor, Elidable, Args,
Sebastian Redla9351792012-02-11 23:51:47 +0000804 HadMultipleCandidates, ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000805 StdInitListInitialization,
Sebastian Redla9351792012-02-11 23:51:47 +0000806 ZeroInitialization, ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000807 ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +0000808}
809
Craig Toppera31a8822013-08-22 07:09:37 +0000810CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
811 QualType T, SourceLocation Loc,
Richard Smithc83bf822016-06-10 00:58:19 +0000812 CXXConstructorDecl *Ctor,
Richard Smithc2bebe92016-05-11 20:37:46 +0000813 bool Elidable,
814 ArrayRef<Expr*> Args,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000815 bool HadMultipleCandidates,
Sebastian Redla9351792012-02-11 23:51:47 +0000816 bool ListInitialization,
Richard Smithf8adcdc2014-07-17 05:12:35 +0000817 bool StdInitListInitialization,
Abramo Bagnara635ed24e2011-10-05 07:56:41 +0000818 bool ZeroInitialization,
Chandler Carruth01718152010-10-25 08:47:36 +0000819 ConstructionKind ConstructKind,
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000820 SourceRange ParenOrBraceRange)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000821 : Expr(SC, T, VK_RValue, OK_Ordinary,
822 T->isDependentType(), T->isDependentType(),
823 T->isInstantiationDependentType(),
824 T->containsUnexpandedParameterPack()),
825 Constructor(Ctor), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
826 NumArgs(Args.size()), Elidable(Elidable),
827 HadMultipleCandidates(HadMultipleCandidates),
828 ListInitialization(ListInitialization),
829 StdInitListInitialization(StdInitListInitialization),
830 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind) {
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000831 if (NumArgs) {
Richard Smithc2bebe92016-05-11 20:37:46 +0000832 this->Args = new (C) Stmt*[Args.size()];
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000833
Richard Smithc2bebe92016-05-11 20:37:46 +0000834 for (unsigned i = 0; i != Args.size(); ++i) {
835 assert(Args[i] && "NULL argument in CXXConstructExpr");
Douglas Gregora6e053e2010-12-15 01:34:56 +0000836
Richard Smithc2bebe92016-05-11 20:37:46 +0000837 if (Args[i]->isValueDependent())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000838 ExprBits.ValueDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000839 if (Args[i]->isInstantiationDependent())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000840 ExprBits.InstantiationDependent = true;
Richard Smithc2bebe92016-05-11 20:37:46 +0000841 if (Args[i]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000842 ExprBits.ContainsUnexpandedParameterPack = true;
843
Richard Smithc2bebe92016-05-11 20:37:46 +0000844 this->Args[i] = Args[i];
Anders Carlsson0781ce72009-04-23 02:32:43 +0000845 }
Douglas Gregor4f4b1862009-12-16 18:50:27 +0000846 }
Anders Carlsson0781ce72009-04-23 02:32:43 +0000847}
848
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000849LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +0000850 LambdaCaptureKind Kind, VarDecl *Var,
851 SourceLocation EllipsisLoc)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000852 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000853 unsigned Bits = 0;
854 if (Implicit)
855 Bits |= Capture_Implicit;
856
857 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +0000858 case LCK_StarThis:
859 Bits |= Capture_ByCopy;
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +0000860 LLVM_FALLTHROUGH;
Craig Topper36250ad2014-05-12 05:36:57 +0000861 case LCK_This:
862 assert(!Var && "'this' capture cannot have a variable!");
John Brawn771721c2016-06-15 14:14:51 +0000863 Bits |= Capture_This;
Douglas Gregore31e6062012-02-07 10:09:13 +0000864 break;
865
866 case LCK_ByCopy:
867 Bits |= Capture_ByCopy;
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +0000868 LLVM_FALLTHROUGH;
Douglas Gregore31e6062012-02-07 10:09:13 +0000869 case LCK_ByRef:
870 assert(Var && "capture must have a variable!");
871 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000872 case LCK_VLAType:
873 assert(!Var && "VLA type capture cannot have a variable!");
Alexey Bataev39c81e22014-08-28 04:28:19 +0000874 break;
Douglas Gregore31e6062012-02-07 10:09:13 +0000875 }
John Brawn771721c2016-06-15 14:14:51 +0000876 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +0000877}
878
Benjamin Kramerf3ca26982014-05-10 16:31:55 +0000879LambdaCaptureKind LambdaCapture::getCaptureKind() const {
John Brawn771721c2016-06-15 14:14:51 +0000880 if (capturesVLAType())
Faisal Validc6b5962016-03-21 09:25:37 +0000881 return LCK_VLAType;
John Brawn771721c2016-06-15 14:14:51 +0000882 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
883 if (capturesThis())
Faisal Validc6b5962016-03-21 09:25:37 +0000884 return CapByCopy ? LCK_StarThis : LCK_This;
Alexey Bataev39c81e22014-08-28 04:28:19 +0000885 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +0000886}
887
James Y Knighte00a67e2015-12-31 04:18:25 +0000888LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
Douglas Gregore31e6062012-02-07 10:09:13 +0000889 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +0000890 SourceLocation CaptureDefaultLoc,
James Y Knighte00a67e2015-12-31 04:18:25 +0000891 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
892 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
Richard Smith2589b9802012-07-25 03:56:55 +0000893 SourceLocation ClosingBrace,
894 bool ContainsUnexpandedParameterPack)
James Y Knighte00a67e2015-12-31 04:18:25 +0000895 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
896 T->isDependentType(), T->isDependentType(),
897 ContainsUnexpandedParameterPack),
898 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
899 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
900 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
901 ClosingBrace(ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000902 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +0000903 CXXRecordDecl *Class = getLambdaClass();
904 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Douglas Gregorc8a73492012-02-13 15:44:47 +0000905
906 // FIXME: Propagate "has unexpanded parameter pack" bit.
Douglas Gregore5561632012-02-13 17:20:40 +0000907
908 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +0000909 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +0000910 Data.NumCaptures = NumCaptures;
911 Data.NumExplicitCaptures = 0;
James Y Knighte00a67e2015-12-31 04:18:25 +0000912 Data.Captures =
913 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
914 LambdaCapture *ToCapture = Data.Captures;
Douglas Gregore5561632012-02-13 17:20:40 +0000915 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
916 if (Captures[I].isExplicit())
917 ++Data.NumExplicitCaptures;
918
919 *ToCapture++ = Captures[I];
920 }
921
922 // Copy initialization expressions for the non-static data members.
923 Stmt **Stored = getStoredStmts();
924 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
925 *Stored++ = CaptureInits[I];
926
927 // Copy the body of the lambda.
928 *Stored++ = getCallOperator()->getBody();
Douglas Gregore31e6062012-02-07 10:09:13 +0000929}
930
James Y Knighte00a67e2015-12-31 04:18:25 +0000931LambdaExpr *LambdaExpr::Create(
932 const ASTContext &Context, CXXRecordDecl *Class,
933 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
934 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
935 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
James Y Knighte00a67e2015-12-31 04:18:25 +0000936 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +0000937 // Determine the type of the expression (i.e., the type of the
938 // function object we're creating).
939 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +0000940
Richard Smith30e304e2016-12-14 00:03:17 +0000941 unsigned Size = totalSizeToAlloc<Stmt *>(Captures.size() + 1);
Douglas Gregore5561632012-02-13 17:20:40 +0000942 void *Mem = Context.Allocate(Size);
Richard Smith30e304e2016-12-14 00:03:17 +0000943 return new (Mem)
944 LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
945 Captures, ExplicitParams, ExplicitResultType, CaptureInits,
946 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +0000947}
948
Craig Toppera31a8822013-08-22 07:09:37 +0000949LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
Richard Smith30e304e2016-12-14 00:03:17 +0000950 unsigned NumCaptures) {
951 unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
Douglas Gregor99ae8062012-02-14 17:54:36 +0000952 void *Mem = C.Allocate(Size);
Richard Smith30e304e2016-12-14 00:03:17 +0000953 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +0000954}
955
James Dennettdd2ffea22015-05-07 18:48:18 +0000956bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
957 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
958 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
959}
960
Douglas Gregorc8a73492012-02-13 15:44:47 +0000961LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000962 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000963}
964
965LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +0000966 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000967}
968
James Dennett1575cb42014-05-27 19:13:04 +0000969LambdaExpr::capture_range LambdaExpr::captures() const {
970 return capture_range(capture_begin(), capture_end());
971}
972
Douglas Gregorc8a73492012-02-13 15:44:47 +0000973LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
974 return capture_begin();
975}
976
977LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
978 struct CXXRecordDecl::LambdaDefinitionData &Data
979 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +0000980 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +0000981}
982
James Dennett1575cb42014-05-27 19:13:04 +0000983LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
984 return capture_range(explicit_capture_begin(), explicit_capture_end());
985}
986
Douglas Gregorc8a73492012-02-13 15:44:47 +0000987LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
988 return explicit_capture_end();
989}
990
991LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
992 return capture_end();
993}
994
James Dennett1575cb42014-05-27 19:13:04 +0000995LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
996 return capture_range(implicit_capture_begin(), implicit_capture_end());
997}
998
Douglas Gregore31e6062012-02-07 10:09:13 +0000999CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1000 return getType()->getAsCXXRecordDecl();
1001}
1002
1003CXXMethodDecl *LambdaExpr::getCallOperator() const {
1004 CXXRecordDecl *Record = getLambdaClass();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001005 return Record->getLambdaCallOperator();
1006}
1007
1008TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1009 CXXRecordDecl *Record = getLambdaClass();
1010 return Record->getGenericLambdaTemplateParameterList();
1011
Douglas Gregore31e6062012-02-07 10:09:13 +00001012}
1013
Douglas Gregor99ae8062012-02-14 17:54:36 +00001014CompoundStmt *LambdaExpr::getBody() const {
James Y Knight53c76162015-07-17 18:21:37 +00001015 // FIXME: this mutation in getBody is bogus. It should be
1016 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1017 // don't understand, that doesn't work.
Douglas Gregor99ae8062012-02-14 17:54:36 +00001018 if (!getStoredStmts()[NumCaptures])
Eugene Zelenko821f6982017-11-18 01:47:41 +00001019 *const_cast<Stmt **>(&getStoredStmts()[NumCaptures]) =
James Y Knight53c76162015-07-17 18:21:37 +00001020 getCallOperator()->getBody();
1021
James Y Knighte00a67e2015-12-31 04:18:25 +00001022 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001023}
1024
Douglas Gregore31e6062012-02-07 10:09:13 +00001025bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001026 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001027}
1028
John McCall28fc7092011-11-10 05:35:25 +00001029ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001030 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001031 ArrayRef<CleanupObject> objects)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001032 : Expr(ExprWithCleanupsClass, subexpr->getType(),
1033 subexpr->getValueKind(), subexpr->getObjectKind(),
1034 subexpr->isTypeDependent(), subexpr->isValueDependent(),
1035 subexpr->isInstantiationDependent(),
1036 subexpr->containsUnexpandedParameterPack()),
1037 SubExpr(subexpr) {
Tim Shen4a05bb82016-06-21 20:29:17 +00001038 ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
John McCall28fc7092011-11-10 05:35:25 +00001039 ExprWithCleanupsBits.NumObjects = objects.size();
1040 for (unsigned i = 0, e = objects.size(); i != e; ++i)
James Y Knighte00a67e2015-12-31 04:18:25 +00001041 getTrailingObjects<CleanupObject>()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001042}
1043
Craig Toppera31a8822013-08-22 07:09:37 +00001044ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001045 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001046 ArrayRef<CleanupObject> objects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001047 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001048 alignof(ExprWithCleanups));
Tim Shen4a05bb82016-06-21 20:29:17 +00001049 return new (buffer)
1050 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001051}
1052
John McCall28fc7092011-11-10 05:35:25 +00001053ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001054 : Expr(ExprWithCleanupsClass, empty) {
John McCall28fc7092011-11-10 05:35:25 +00001055 ExprWithCleanupsBits.NumObjects = numObjects;
1056}
Chris Lattnercba86142010-05-10 00:25:06 +00001057
Craig Toppera31a8822013-08-22 07:09:37 +00001058ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1059 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001060 unsigned numObjects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001061 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001062 alignof(ExprWithCleanups));
John McCall28fc7092011-11-10 05:35:25 +00001063 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001064}
1065
Douglas Gregor2b88c112010-09-08 00:15:04 +00001066CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001067 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001068 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001069 SourceLocation RParenLoc)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001070 : Expr(CXXUnresolvedConstructExprClass,
1071 Type->getType().getNonReferenceType(),
1072 (Type->getType()->isLValueReferenceType()
1073 ? VK_LValue
1074 : Type->getType()->isRValueReferenceType() ? VK_XValue
1075 : VK_RValue),
1076 OK_Ordinary,
1077 Type->getType()->isDependentType() ||
1078 Type->getType()->getContainedDeducedType(),
1079 true, true, Type->getType()->containsUnexpandedParameterPack()),
1080 Type(Type), LParenLoc(LParenLoc), RParenLoc(RParenLoc),
1081 NumArgs(Args.size()) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001082 Expr **StoredArgs = getTrailingObjects<Expr *>();
Benjamin Kramerc215e762012-08-24 11:54:20 +00001083 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001084 if (Args[I]->containsUnexpandedParameterPack())
1085 ExprBits.ContainsUnexpandedParameterPack = true;
1086
1087 StoredArgs[I] = Args[I];
1088 }
Douglas Gregorce934142009-05-20 18:46:25 +00001089}
1090
1091CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001092CXXUnresolvedConstructExpr::Create(const ASTContext &C,
Douglas Gregor2b88c112010-09-08 00:15:04 +00001093 TypeSourceInfo *Type,
Douglas Gregorce934142009-05-20 18:46:25 +00001094 SourceLocation LParenLoc,
Benjamin Kramerc215e762012-08-24 11:54:20 +00001095 ArrayRef<Expr*> Args,
Douglas Gregorce934142009-05-20 18:46:25 +00001096 SourceLocation RParenLoc) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001097 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
Benjamin Kramerc215e762012-08-24 11:54:20 +00001098 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001099}
1100
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001101CXXUnresolvedConstructExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001102CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001103 Stmt::EmptyShell Empty;
James Y Knighte00a67e2015-12-31 04:18:25 +00001104 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001105 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1106}
1107
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +00001108SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1109 return Type->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001110}
1111
James Y Knighte7d82282015-12-29 18:15:14 +00001112CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1113 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1114 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1115 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1116 DeclarationNameInfo MemberNameInfo,
1117 const TemplateArgumentListInfo *TemplateArgs)
1118 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1119 OK_Ordinary, true, true, true,
1120 ((Base && Base->containsUnexpandedParameterPack()) ||
1121 (QualifierLoc &&
1122 QualifierLoc.getNestedNameSpecifier()
1123 ->containsUnexpandedParameterPack()) ||
1124 MemberNameInfo.containsUnexpandedParameterPack())),
1125 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1126 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1127 TemplateKWLoc.isValid()),
1128 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1129 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1130 MemberNameInfo(MemberNameInfo) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001131 if (TemplateArgs) {
1132 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001133 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001134 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +00001135 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1136 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1137 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001138 if (ContainsUnexpandedParameterPack)
1139 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001140 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +00001141 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1142 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001143 }
Douglas Gregor308047d2009-09-09 00:23:06 +00001144}
1145
John McCall8cd78132009-11-19 22:55:06 +00001146CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001147CXXDependentScopeMemberExpr::Create(const ASTContext &C,
John McCall2d74de92009-12-01 22:10:20 +00001148 Expr *Base, QualType BaseType, bool IsArrow,
Douglas Gregor308047d2009-09-09 00:23:06 +00001149 SourceLocation OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001150 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001151 SourceLocation TemplateKWLoc,
Douglas Gregor308047d2009-09-09 00:23:06 +00001152 NamedDecl *FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001153 DeclarationNameInfo MemberNameInfo,
John McCall6b51f282009-11-23 01:53:49 +00001154 const TemplateArgumentListInfo *TemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001155 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001156 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
James Y Knighte7d82282015-12-29 18:15:14 +00001157 std::size_t Size =
1158 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1159 HasTemplateKWAndArgsInfo, NumTemplateArgs);
John McCall6b51f282009-11-23 01:53:49 +00001160
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001161 void *Mem = C.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
John McCall2d74de92009-12-01 22:10:20 +00001162 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1163 IsArrow, OperatorLoc,
Douglas Gregore16af532011-02-28 18:50:33 +00001164 QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001165 TemplateKWLoc,
John McCall2d74de92009-12-01 22:10:20 +00001166 FirstQualifierFoundInScope,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001167 MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001168}
1169
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001170CXXDependentScopeMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001171CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001172 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001173 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001174 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1175 std::size_t Size =
1176 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1177 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001178 void *Mem = C.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001179 CXXDependentScopeMemberExpr *E
Craig Topper36250ad2014-05-12 05:36:57 +00001180 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
Eugene Zelenko821f6982017-11-18 01:47:41 +00001181 false, SourceLocation(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00001182 NestedNameSpecifierLoc(),
Craig Topper36250ad2014-05-12 05:36:57 +00001183 SourceLocation(), nullptr,
1184 DeclarationNameInfo(), nullptr);
James Y Knighte7d82282015-12-29 18:15:14 +00001185 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001186 return E;
1187}
1188
Douglas Gregor0da1d432011-02-28 20:01:57 +00001189bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001190 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001191 return true;
1192
Douglas Gregor25b7e052011-03-02 21:06:53 +00001193 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001194}
1195
John McCall0009fcc2011-04-26 20:42:42 +00001196static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1197 UnresolvedSetIterator end) {
1198 do {
1199 NamedDecl *decl = *begin;
1200 if (isa<UnresolvedUsingValueDecl>(decl))
1201 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001202
1203 // Unresolved member expressions should only contain methods and
1204 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001205 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1206 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001207 return false;
1208 } while (++begin != end);
1209
1210 return true;
1211}
1212
Craig Toppera31a8822013-08-22 07:09:37 +00001213UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
John McCall10eae182009-11-30 22:42:35 +00001214 bool HasUnresolvedUsing,
John McCall2d74de92009-12-01 22:10:20 +00001215 Expr *Base, QualType BaseType,
1216 bool IsArrow,
John McCall10eae182009-11-30 22:42:35 +00001217 SourceLocation OperatorLoc,
Douglas Gregor0da1d432011-02-28 20:01:57 +00001218 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +00001219 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +00001220 const DeclarationNameInfo &MemberNameInfo,
Douglas Gregor30a4f4c2010-05-23 18:57:34 +00001221 const TemplateArgumentListInfo *TemplateArgs,
1222 UnresolvedSetIterator Begin,
1223 UnresolvedSetIterator End)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001224 : OverloadExpr(
1225 UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1226 MemberNameInfo, TemplateArgs, Begin, End,
1227 // Dependent
1228 ((Base && Base->isTypeDependent()) || BaseType->isDependentType()),
1229 ((Base && Base->isInstantiationDependent()) ||
1230 BaseType->isInstantiationDependentType()),
1231 // Contains unexpanded parameter pack
1232 ((Base && Base->containsUnexpandedParameterPack()) ||
1233 BaseType->containsUnexpandedParameterPack())),
1234 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing), Base(Base),
1235 BaseType(BaseType), OperatorLoc(OperatorLoc) {
John McCall0009fcc2011-04-26 20:42:42 +00001236 // Check whether all of the members are non-static member functions,
1237 // and if so, mark give this bound-member type instead of overload type.
1238 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1239 setType(C.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001240}
1241
Douglas Gregor0da1d432011-02-28 20:01:57 +00001242bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001243 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001244 return true;
1245
Douglas Gregor25b7e052011-03-02 21:06:53 +00001246 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001247}
1248
James Y Knighte7d82282015-12-29 18:15:14 +00001249UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1250 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1251 bool IsArrow, SourceLocation OperatorLoc,
1252 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1253 const DeclarationNameInfo &MemberNameInfo,
1254 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1255 UnresolvedSetIterator End) {
1256 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1257 std::size_t Size =
1258 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1259 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
John McCall10eae182009-11-30 22:42:35 +00001260
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001261 void *Mem = C.Allocate(Size, alignof(UnresolvedMemberExpr));
James Y Knighte7d82282015-12-29 18:15:14 +00001262 return new (Mem) UnresolvedMemberExpr(
1263 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1264 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001265}
1266
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001267UnresolvedMemberExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001268UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1269 bool HasTemplateKWAndArgsInfo,
Douglas Gregor87866ce2011-02-04 12:01:24 +00001270 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001271 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1272 std::size_t Size =
1273 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1274 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001275
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001276 void *Mem = C.Allocate(Size, alignof(UnresolvedMemberExpr));
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001277 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
Abramo Bagnara7945c982012-01-27 09:46:47 +00001278 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001279 return E;
1280}
1281
John McCall58cc69d2010-01-27 01:50:18 +00001282CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1283 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1284
1285 // If there was a nested name specifier, it names the naming class.
1286 // It can't be dependent: after all, we were actually able to do the
1287 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001288 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001289 auto *NNS = getQualifier();
1290 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001291 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001292 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001293 Record = T->getAsCXXRecordDecl();
1294 assert(Record && "qualifier in member expression does not name record");
1295 }
John McCall58cc69d2010-01-27 01:50:18 +00001296 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001297 else {
John McCall58cc69d2010-01-27 01:50:18 +00001298 QualType BaseType = getBaseType().getNonReferenceType();
1299 if (isArrow()) {
1300 const PointerType *PT = BaseType->getAs<PointerType>();
1301 assert(PT && "base of arrow member access is not pointer");
1302 BaseType = PT->getPointeeType();
1303 }
1304
Douglas Gregor9262f472010-04-27 18:19:34 +00001305 Record = BaseType->getAsCXXRecordDecl();
1306 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001307 }
1308
Douglas Gregor9262f472010-04-27 18:19:34 +00001309 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001310}
1311
Richard Smithd784e682015-09-23 21:41:42 +00001312SizeOfPackExpr *
1313SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1314 NamedDecl *Pack, SourceLocation PackLoc,
1315 SourceLocation RParenLoc,
1316 Optional<unsigned> Length,
1317 ArrayRef<TemplateArgument> PartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001318 void *Storage =
1319 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
Richard Smithd784e682015-09-23 21:41:42 +00001320 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1321 PackLoc, RParenLoc, Length, PartialArgs);
1322}
1323
1324SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1325 unsigned NumPartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001326 void *Storage =
1327 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
Richard Smithd784e682015-09-23 21:41:42 +00001328 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1329}
1330
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001331SubstNonTypeTemplateParmPackExpr::
1332SubstNonTypeTemplateParmPackExpr(QualType T,
1333 NonTypeTemplateParmDecl *Param,
1334 SourceLocation NameLoc,
1335 const TemplateArgument &ArgPack)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001336 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
1337 true, true, true, true),
1338 Param(Param), Arguments(ArgPack.pack_begin()),
1339 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) {}
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001340
1341TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +00001342 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001343}
1344
Richard Smithb15fe3a2012-09-12 00:56:43 +00001345FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1346 SourceLocation NameLoc,
1347 unsigned NumParams,
James Y Knight48fefa32015-09-30 14:04:23 +00001348 ParmVarDecl *const *Params)
1349 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1350 true, true),
1351 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001352 if (Params)
1353 std::uninitialized_copy(Params, Params + NumParams,
James Y Knighte00a67e2015-12-31 04:18:25 +00001354 getTrailingObjects<ParmVarDecl *>());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001355}
1356
1357FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001358FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001359 ParmVarDecl *ParamPack, SourceLocation NameLoc,
James Y Knight48fefa32015-09-30 14:04:23 +00001360 ArrayRef<ParmVarDecl *> Params) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001361 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(Params.size())))
1362 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001363}
1364
1365FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001366FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1367 unsigned NumParams) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001368 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(NumParams)))
1369 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001370}
1371
David Majnemerdaff3702014-05-01 17:50:17 +00001372void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1373 unsigned ManglingNumber) {
1374 // We only need extra state if we have to remember more than just the Stmt.
1375 if (!ExtendedBy)
1376 return;
1377
1378 // We may need to allocate extra storage for the mangling number and the
1379 // extended-by ValueDecl.
1380 if (!State.is<ExtraState *>()) {
1381 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1382 ES->Temporary = State.get<Stmt *>();
1383 State = ES;
1384 }
1385
1386 auto ES = State.get<ExtraState *>();
1387 ES->ExtendingDecl = ExtendedBy;
1388 ES->ManglingNumber = ManglingNumber;
1389}
1390
Douglas Gregor29c42f22012-02-24 07:38:34 +00001391TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1392 ArrayRef<TypeSourceInfo *> Args,
1393 SourceLocation RParenLoc,
1394 bool Value)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001395 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1396 /*TypeDependent=*/false,
1397 /*ValueDependent=*/false,
1398 /*InstantiationDependent=*/false,
1399 /*ContainsUnexpandedParameterPack=*/false),
1400 Loc(Loc), RParenLoc(RParenLoc) {
Douglas Gregor29c42f22012-02-24 07:38:34 +00001401 TypeTraitExprBits.Kind = Kind;
1402 TypeTraitExprBits.Value = Value;
1403 TypeTraitExprBits.NumArgs = Args.size();
1404
James Y Knighte00a67e2015-12-31 04:18:25 +00001405 TypeSourceInfo **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1406
Douglas Gregor29c42f22012-02-24 07:38:34 +00001407 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1408 if (Args[I]->getType()->isDependentType())
1409 setValueDependent(true);
1410 if (Args[I]->getType()->isInstantiationDependentType())
1411 setInstantiationDependent(true);
1412 if (Args[I]->getType()->containsUnexpandedParameterPack())
1413 setContainsUnexpandedParameterPack(true);
1414
1415 ToArgs[I] = Args[I];
1416 }
1417}
1418
Craig Toppera31a8822013-08-22 07:09:37 +00001419TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001420 SourceLocation Loc,
1421 TypeTrait Kind,
1422 ArrayRef<TypeSourceInfo *> Args,
1423 SourceLocation RParenLoc,
1424 bool Value) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001425 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001426 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1427}
1428
Craig Toppera31a8822013-08-22 07:09:37 +00001429TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001430 unsigned NumArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001431 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001432 return new (Mem) TypeTraitExpr(EmptyShell());
1433}
1434
Eugene Zelenko821f6982017-11-18 01:47:41 +00001435void ArrayTypeTraitExpr::anchor() {}