blob: b30f785ba8f5483af003412bb588bbd3c4c4c12a [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Ted Kremeneke3a0c142007-08-24 20:21:10 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the subclesses of Expr class declared in ExprCXX.h
10//
11//===----------------------------------------------------------------------===//
12
Eugene Zelenko821f6982017-11-18 01:47:41 +000013#include "clang/AST/ExprCXX.h"
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"
Eugene Zelenko821f6982017-11-18 01:47:41 +000016#include "clang/AST/Decl.h"
17#include "clang/AST/DeclAccessPair.h"
18#include "clang/AST/DeclBase.h"
Douglas Gregor993603d2008-11-14 16:09:21 +000019#include "clang/AST/DeclCXX.h"
Eugene Zelenko821f6982017-11-18 01:47:41 +000020#include "clang/AST/DeclarationName.h"
21#include "clang/AST/Expr.h"
22#include "clang/AST/LambdaCapture.h"
23#include "clang/AST/NestedNameSpecifier.h"
24#include "clang/AST/TemplateBase.h"
25#include "clang/AST/Type.h"
Douglas Gregor651fe5e2010-02-24 23:40:28 +000026#include "clang/AST/TypeLoc.h"
Eugene Zelenko821f6982017-11-18 01:47:41 +000027#include "clang/Basic/LLVM.h"
28#include "clang/Basic/OperatorKinds.h"
29#include "clang/Basic/SourceLocation.h"
30#include "clang/Basic/Specifiers.h"
31#include "llvm/ADT/ArrayRef.h"
32#include "llvm/Support/Casting.h"
33#include "llvm/Support/ErrorHandling.h"
34#include <cassert>
35#include <cstddef>
36#include <cstring>
37#include <memory>
Ted Kremeneke3a0c142007-08-24 20:21:10 +000038
Eugene Zelenko821f6982017-11-18 01:47:41 +000039using namespace clang;
Douglas Gregor9da64192010-04-26 22:37:10 +000040
Ted Kremeneke3a0c142007-08-24 20:21:10 +000041//===----------------------------------------------------------------------===//
42// Child Iterators for iterating over subexpressions/substatements
43//===----------------------------------------------------------------------===//
44
Richard Smith66094432016-10-20 00:55:15 +000045bool CXXOperatorCallExpr::isInfixBinaryOp() const {
46 // An infix binary operator is any operator with two arguments other than
47 // operator() and operator[]. Note that none of these operators can have
48 // default arguments, so it suffices to check the number of argument
49 // expressions.
50 if (getNumArgs() != 2)
51 return false;
52
53 switch (getOperator()) {
54 case OO_Call: case OO_Subscript:
55 return false;
56 default:
57 return true;
58 }
59}
60
Richard Smithef8bf432012-08-13 20:08:14 +000061bool CXXTypeidExpr::isPotentiallyEvaluated() const {
62 if (isTypeOperand())
63 return false;
64
65 // C++11 [expr.typeid]p3:
66 // When typeid is applied to an expression other than a glvalue of
67 // polymorphic class type, [...] the expression is an unevaluated operand.
68 const Expr *E = getExprOperand();
69 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
70 if (RD->isPolymorphic() && E->isGLValue())
71 return true;
72
73 return false;
74}
75
David Majnemer143c55e2013-09-27 07:04:31 +000076QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
Douglas Gregor9da64192010-04-26 22:37:10 +000077 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000078 Qualifiers Quals;
79 return Context.getUnqualifiedArrayType(
80 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Douglas Gregor9da64192010-04-26 22:37:10 +000081}
82
David Majnemer143c55e2013-09-27 07:04:31 +000083QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
Francois Pichet9f4f2072010-09-08 12:20:18 +000084 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
David Majnemer143c55e2013-09-27 07:04:31 +000085 Qualifiers Quals;
86 return Context.getUnqualifiedArrayType(
87 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
Francois Pichet9f4f2072010-09-08 12:20:18 +000088}
89
Douglas Gregor747eb782010-07-08 06:14:04 +000090// CXXScalarValueInitExpr
Stephen Kelly724e9e52018-08-09 20:05:03 +000091SourceLocation CXXScalarValueInitExpr::getBeginLoc() const {
Bruno Ricci030dab42019-01-08 16:08:54 +000092 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : getRParenLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +000093}
94
Sebastian Redlbd150f42008-11-21 19:14:01 +000095// CXXNewExpr
Bruno Ricci9b6dfac2019-01-07 15:04:45 +000096CXXNewExpr::CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
97 FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
98 bool UsualArrayDeleteWantsSize,
99 ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
Richard Smithb9fb1212019-05-06 03:47:15 +0000100 Optional<Expr *> ArraySize,
101 InitializationStyle InitializationStyle,
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000102 Expr *Initializer, QualType Ty,
103 TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
104 SourceRange DirectInitRange)
105 : Expr(CXXNewExprClass, Ty, VK_RValue, OK_Ordinary, Ty->isDependentType(),
106 Ty->isDependentType(), Ty->isInstantiationDependentType(),
107 Ty->containsUnexpandedParameterPack()),
108 OperatorNew(OperatorNew), OperatorDelete(OperatorDelete),
109 AllocatedTypeInfo(AllocatedTypeInfo), Range(Range),
110 DirectInitRange(DirectInitRange) {
Fangrui Song6907ce22018-07-30 19:24:48 +0000111
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000112 assert((Initializer != nullptr || InitializationStyle == NoInit) &&
113 "Only NoInit can have no initializer!");
114
115 CXXNewExprBits.IsGlobalNew = IsGlobalNew;
Richard Smithb9fb1212019-05-06 03:47:15 +0000116 CXXNewExprBits.IsArray = ArraySize.hasValue();
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000117 CXXNewExprBits.ShouldPassAlignment = ShouldPassAlignment;
118 CXXNewExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
119 CXXNewExprBits.StoredInitializationStyle =
120 Initializer ? InitializationStyle + 1 : 0;
121 bool IsParenTypeId = TypeIdParens.isValid();
122 CXXNewExprBits.IsParenTypeId = IsParenTypeId;
123 CXXNewExprBits.NumPlacementArgs = PlacementArgs.size();
124
125 if (ArraySize) {
Richard Smithb9fb1212019-05-06 03:47:15 +0000126 if (Expr *SizeExpr = *ArraySize) {
127 if (SizeExpr->isInstantiationDependent())
128 ExprBits.InstantiationDependent = true;
129 if (SizeExpr->containsUnexpandedParameterPack())
130 ExprBits.ContainsUnexpandedParameterPack = true;
131 }
Douglas Gregora6e053e2010-12-15 01:34:56 +0000132
Richard Smithb9fb1212019-05-06 03:47:15 +0000133 getTrailingObjects<Stmt *>()[arraySizeOffset()] = *ArraySize;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000134 }
135
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000136 if (Initializer) {
137 if (Initializer->isInstantiationDependent())
Sebastian Redl6047f072012-02-16 12:22:20 +0000138 ExprBits.InstantiationDependent = true;
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000139 if (Initializer->containsUnexpandedParameterPack())
Sebastian Redl6047f072012-02-16 12:22:20 +0000140 ExprBits.ContainsUnexpandedParameterPack = true;
141
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000142 getTrailingObjects<Stmt *>()[initExprOffset()] = Initializer;
Sebastian Redl6047f072012-02-16 12:22:20 +0000143 }
144
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000145 for (unsigned I = 0; I != PlacementArgs.size(); ++I) {
146 if (PlacementArgs[I]->isInstantiationDependent())
Douglas Gregor678d76c2011-07-01 01:22:09 +0000147 ExprBits.InstantiationDependent = true;
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000148 if (PlacementArgs[I]->containsUnexpandedParameterPack())
Douglas Gregora6e053e2010-12-15 01:34:56 +0000149 ExprBits.ContainsUnexpandedParameterPack = true;
150
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000151 getTrailingObjects<Stmt *>()[placementNewArgsOffset() + I] =
152 PlacementArgs[I];
Douglas Gregora6e053e2010-12-15 01:34:56 +0000153 }
David Blaikie3a0de212012-11-08 22:53:48 +0000154
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000155 if (IsParenTypeId)
156 getTrailingObjects<SourceRange>()[0] = TypeIdParens;
157
David Blaikie3a0de212012-11-08 22:53:48 +0000158 switch (getInitializationStyle()) {
159 case CallInit:
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000160 this->Range.setEnd(DirectInitRange.getEnd());
161 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000162 case ListInit:
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000163 this->Range.setEnd(getInitializer()->getSourceRange().getEnd());
164 break;
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000165 default:
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000166 if (IsParenTypeId)
Eli Friedman2dcbdc02013-06-17 22:35:10 +0000167 this->Range.setEnd(TypeIdParens.getEnd());
168 break;
David Blaikie3a0de212012-11-08 22:53:48 +0000169 }
Sebastian Redlbd150f42008-11-21 19:14:01 +0000170}
171
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000172CXXNewExpr::CXXNewExpr(EmptyShell Empty, bool IsArray,
173 unsigned NumPlacementArgs, bool IsParenTypeId)
174 : Expr(CXXNewExprClass, Empty) {
175 CXXNewExprBits.IsArray = IsArray;
176 CXXNewExprBits.NumPlacementArgs = NumPlacementArgs;
177 CXXNewExprBits.IsParenTypeId = IsParenTypeId;
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000178}
179
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000180CXXNewExpr *
181CXXNewExpr::Create(const ASTContext &Ctx, bool IsGlobalNew,
182 FunctionDecl *OperatorNew, FunctionDecl *OperatorDelete,
183 bool ShouldPassAlignment, bool UsualArrayDeleteWantsSize,
184 ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
Richard Smithb9fb1212019-05-06 03:47:15 +0000185 Optional<Expr *> ArraySize,
186 InitializationStyle InitializationStyle, Expr *Initializer,
187 QualType Ty, TypeSourceInfo *AllocatedTypeInfo,
188 SourceRange Range, SourceRange DirectInitRange) {
189 bool IsArray = ArraySize.hasValue();
Bruno Ricci9b6dfac2019-01-07 15:04:45 +0000190 bool HasInit = Initializer != nullptr;
191 unsigned NumPlacementArgs = PlacementArgs.size();
192 bool IsParenTypeId = TypeIdParens.isValid();
193 void *Mem =
194 Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
195 IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
196 alignof(CXXNewExpr));
197 return new (Mem)
198 CXXNewExpr(IsGlobalNew, OperatorNew, OperatorDelete, ShouldPassAlignment,
199 UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens,
200 ArraySize, InitializationStyle, Initializer, Ty,
201 AllocatedTypeInfo, Range, DirectInitRange);
202}
203
204CXXNewExpr *CXXNewExpr::CreateEmpty(const ASTContext &Ctx, bool IsArray,
205 bool HasInit, unsigned NumPlacementArgs,
206 bool IsParenTypeId) {
207 void *Mem =
208 Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
209 IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
210 alignof(CXXNewExpr));
211 return new (Mem)
212 CXXNewExpr(EmptyShell(), IsArray, NumPlacementArgs, IsParenTypeId);
213}
214
215bool CXXNewExpr::shouldNullCheckAllocation() const {
216 return getOperatorNew()
217 ->getType()
218 ->castAs<FunctionProtoType>()
219 ->isNothrow() &&
Richard Smith902a0232015-02-14 01:52:20 +0000220 !getOperatorNew()->isReservedGlobalPlacementOperator();
John McCall75f94982011-03-07 03:12:35 +0000221}
Chris Lattnerabfb58d2010-05-10 01:22:27 +0000222
Sebastian Redlbd150f42008-11-21 19:14:01 +0000223// CXXDeleteExpr
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000224QualType CXXDeleteExpr::getDestroyedType() const {
225 const Expr *Arg = getArgument();
Richard Smith5b349582017-10-13 01:55:36 +0000226
227 // For a destroying operator delete, we may have implicitly converted the
228 // pointer type to the type of the parameter of the 'operator delete'
229 // function.
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000230 while (const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
Richard Smith5b349582017-10-13 01:55:36 +0000231 if (ICE->getCastKind() == CK_DerivedToBase ||
232 ICE->getCastKind() == CK_UncheckedDerivedToBase ||
233 ICE->getCastKind() == CK_NoOp) {
234 assert((ICE->getCastKind() == CK_NoOp ||
235 getOperatorDelete()->isDestroyingOperatorDelete()) &&
236 "only a destroying operator delete can have a converted arg");
237 Arg = ICE->getSubExpr();
238 } else
239 break;
240 }
241
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000242 // The type-to-delete may not be a pointer if it's a dependent type.
Craig Silverstein3b9936f2010-10-20 00:56:01 +0000243 const QualType ArgType = Arg->getType();
Craig Silverstein9e448da2010-11-16 07:16:25 +0000244
245 if (ArgType->isDependentType() && !ArgType->isPointerType())
246 return QualType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000247
Craig Silverstein20f7ab72010-10-20 00:38:15 +0000248 return ArgType->getAs<PointerType>()->getPointeeType();
Douglas Gregor6ed2fee2010-09-14 22:55:20 +0000249}
250
Douglas Gregorad8a3362009-09-04 17:36:40 +0000251// CXXPseudoDestructorExpr
Douglas Gregor678f90d2010-02-25 01:56:36 +0000252PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
Eugene Zelenko821f6982017-11-18 01:47:41 +0000253 : Type(Info) {
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000254 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
Douglas Gregor678f90d2010-02-25 01:56:36 +0000255}
256
Craig Toppera31a8822013-08-22 07:09:37 +0000257CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000258 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
Fangrui Song6907ce22018-07-30 19:24:48 +0000259 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
260 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
Douglas Gregora6ce6082011-02-25 18:19:59 +0000261 PseudoDestructorTypeStorage DestroyedType)
John McCalldb40c7f2010-12-14 08:05:40 +0000262 : Expr(CXXPseudoDestructorExprClass,
David Majnemerced8bdf2015-02-25 17:36:15 +0000263 Context.BoundMemberTy,
John McCalldb40c7f2010-12-14 08:05:40 +0000264 VK_RValue, OK_Ordinary,
265 /*isTypeDependent=*/(Base->isTypeDependent() ||
266 (DestroyedType.getTypeSourceInfo() &&
267 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000268 /*isValueDependent=*/Base->isValueDependent(),
Douglas Gregor678d76c2011-07-01 01:22:09 +0000269 (Base->isInstantiationDependent() ||
270 (QualifierLoc &&
271 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
272 (ScopeType &&
273 ScopeType->getType()->isInstantiationDependentType()) ||
274 (DestroyedType.getTypeSourceInfo() &&
275 DestroyedType.getTypeSourceInfo()->getType()
276 ->isInstantiationDependentType())),
Douglas Gregora6e053e2010-12-15 01:34:56 +0000277 // ContainsUnexpandedParameterPack
278 (Base->containsUnexpandedParameterPack() ||
Fangrui Song6907ce22018-07-30 19:24:48 +0000279 (QualifierLoc &&
Douglas Gregora6ce6082011-02-25 18:19:59 +0000280 QualifierLoc.getNestedNameSpecifier()
281 ->containsUnexpandedParameterPack()) ||
Fangrui Song6907ce22018-07-30 19:24:48 +0000282 (ScopeType &&
Douglas Gregora6e053e2010-12-15 01:34:56 +0000283 ScopeType->getType()->containsUnexpandedParameterPack()) ||
284 (DestroyedType.getTypeSourceInfo() &&
285 DestroyedType.getTypeSourceInfo()->getType()
286 ->containsUnexpandedParameterPack()))),
John McCalldb40c7f2010-12-14 08:05:40 +0000287 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
Douglas Gregora6ce6082011-02-25 18:19:59 +0000288 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
John McCalldb40c7f2010-12-14 08:05:40 +0000289 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
Eugene Zelenko821f6982017-11-18 01:47:41 +0000290 DestroyedType(DestroyedType) {}
John McCalldb40c7f2010-12-14 08:05:40 +0000291
Douglas Gregor678f90d2010-02-25 01:56:36 +0000292QualType CXXPseudoDestructorExpr::getDestroyedType() const {
293 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
294 return TInfo->getType();
Fangrui Song6907ce22018-07-30 19:24:48 +0000295
Douglas Gregor678f90d2010-02-25 01:56:36 +0000296 return QualType();
297}
298
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000299SourceLocation CXXPseudoDestructorExpr::getEndLoc() const {
Douglas Gregor678f90d2010-02-25 01:56:36 +0000300 SourceLocation End = DestroyedType.getLocation();
301 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
Abramo Bagnara1108e7b2010-05-20 10:00:11 +0000302 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000303 return End;
Douglas Gregor651fe5e2010-02-24 23:40:28 +0000304}
305
John McCalld14a8642009-11-21 08:51:07 +0000306// UnresolvedLookupExpr
Bruno Riccid7628d92019-01-09 15:43:19 +0000307UnresolvedLookupExpr::UnresolvedLookupExpr(
308 const ASTContext &Context, CXXRecordDecl *NamingClass,
309 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
310 const DeclarationNameInfo &NameInfo, bool RequiresADL, bool Overloaded,
311 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
312 UnresolvedSetIterator End)
313 : OverloadExpr(UnresolvedLookupExprClass, Context, QualifierLoc,
314 TemplateKWLoc, NameInfo, TemplateArgs, Begin, End, false,
315 false, false),
316 NamingClass(NamingClass) {
317 UnresolvedLookupExprBits.RequiresADL = RequiresADL;
318 UnresolvedLookupExprBits.Overloaded = Overloaded;
319}
320
321UnresolvedLookupExpr::UnresolvedLookupExpr(EmptyShell Empty,
322 unsigned NumResults,
323 bool HasTemplateKWAndArgsInfo)
324 : OverloadExpr(UnresolvedLookupExprClass, Empty, NumResults,
325 HasTemplateKWAndArgsInfo) {}
326
327UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
328 const ASTContext &Context, CXXRecordDecl *NamingClass,
329 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
330 bool RequiresADL, bool Overloaded, UnresolvedSetIterator Begin,
331 UnresolvedSetIterator End) {
332 unsigned NumResults = End - Begin;
333 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
334 TemplateArgumentLoc>(NumResults, 0, 0);
335 void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
336 return new (Mem) UnresolvedLookupExpr(Context, NamingClass, QualifierLoc,
337 SourceLocation(), NameInfo, RequiresADL,
338 Overloaded, nullptr, Begin, End);
339}
340
341UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
342 const ASTContext &Context, CXXRecordDecl *NamingClass,
343 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
344 const DeclarationNameInfo &NameInfo, bool RequiresADL,
345 const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin,
346 UnresolvedSetIterator End) {
Abramo Bagnara65f7c3d2012-02-06 14:31:00 +0000347 assert(Args || TemplateKWLoc.isValid());
Bruno Riccid7628d92019-01-09 15:43:19 +0000348 unsigned NumResults = End - Begin;
349 unsigned NumTemplateArgs = Args ? Args->size() : 0;
350 unsigned Size =
351 totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
352 TemplateArgumentLoc>(NumResults, 1, NumTemplateArgs);
353 void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
354 return new (Mem) UnresolvedLookupExpr(Context, NamingClass, QualifierLoc,
355 TemplateKWLoc, NameInfo, RequiresADL,
356 /*Overloaded*/ true, Args, Begin, End);
John McCalle66edc12009-11-24 19:00:30 +0000357}
358
Bruno Riccid7628d92019-01-09 15:43:19 +0000359UnresolvedLookupExpr *UnresolvedLookupExpr::CreateEmpty(
360 const ASTContext &Context, unsigned NumResults,
361 bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000362 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
Bruno Riccid7628d92019-01-09 15:43:19 +0000363 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
364 TemplateArgumentLoc>(
365 NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
366 void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
367 return new (Mem)
368 UnresolvedLookupExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
Argyrios Kyrtzidis58e01ad2010-06-25 09:03:34 +0000369}
370
Bruno Riccid7628d92019-01-09 15:43:19 +0000371OverloadExpr::OverloadExpr(StmtClass SC, const ASTContext &Context,
Douglas Gregor0da1d432011-02-28 20:01:57 +0000372 NestedNameSpecifierLoc QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000373 SourceLocation TemplateKWLoc,
Abramo Bagnarad6d2f182010-08-11 22:01:17 +0000374 const DeclarationNameInfo &NameInfo,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000375 const TemplateArgumentListInfo *TemplateArgs,
Fangrui Song6907ce22018-07-30 19:24:48 +0000376 UnresolvedSetIterator Begin,
Bruno Riccid7628d92019-01-09 15:43:19 +0000377 UnresolvedSetIterator End, bool KnownDependent,
Douglas Gregor678d76c2011-07-01 01:22:09 +0000378 bool KnownInstantiationDependent,
Douglas Gregora6e053e2010-12-15 01:34:56 +0000379 bool KnownContainsUnexpandedParameterPack)
Bruno Riccid7628d92019-01-09 15:43:19 +0000380 : Expr(
381 SC, Context.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
382 KnownDependent,
383 (KnownInstantiationDependent || NameInfo.isInstantiationDependent() ||
384 (QualifierLoc &&
Eugene Zelenko821f6982017-11-18 01:47:41 +0000385 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
Bruno Riccid7628d92019-01-09 15:43:19 +0000386 (KnownContainsUnexpandedParameterPack ||
387 NameInfo.containsUnexpandedParameterPack() ||
388 (QualifierLoc && QualifierLoc.getNestedNameSpecifier()
389 ->containsUnexpandedParameterPack()))),
390 NameInfo(NameInfo), QualifierLoc(QualifierLoc) {
391 unsigned NumResults = End - Begin;
392 OverloadExprBits.NumResults = NumResults;
393 OverloadExprBits.HasTemplateKWAndArgsInfo =
394 (TemplateArgs != nullptr ) || TemplateKWLoc.isValid();
395
Douglas Gregora6e053e2010-12-15 01:34:56 +0000396 if (NumResults) {
397 // Determine whether this expression is type-dependent.
398 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
399 if ((*I)->getDeclContext()->isDependentContext() ||
400 isa<UnresolvedUsingValueDecl>(*I)) {
401 ExprBits.TypeDependent = true;
402 ExprBits.ValueDependent = true;
Richard Smith47726b22012-08-13 21:29:18 +0000403 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000404 }
405 }
406
Bruno Riccid7628d92019-01-09 15:43:19 +0000407 // Copy the results to the trailing array past UnresolvedLookupExpr
408 // or UnresolvedMemberExpr.
409 DeclAccessPair *Results = getTrailingResults();
Benjamin Kramer04ec7e32015-02-01 20:31:36 +0000410 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
Douglas Gregora6e053e2010-12-15 01:34:56 +0000411 }
412
413 // If we have explicit template arguments, check for dependent
414 // template arguments and whether they contain any unexpanded pack
415 // expansions.
416 if (TemplateArgs) {
417 bool Dependent = false;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000418 bool InstantiationDependent = false;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000419 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +0000420 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
421 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
422 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000423
424 if (Dependent) {
Douglas Gregor678d76c2011-07-01 01:22:09 +0000425 ExprBits.TypeDependent = true;
426 ExprBits.ValueDependent = true;
427 }
428 if (InstantiationDependent)
429 ExprBits.InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000430 if (ContainsUnexpandedParameterPack)
431 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000432 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000433 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000434 }
435
436 if (isTypeDependent())
Bruno Riccid7628d92019-01-09 15:43:19 +0000437 setType(Context.DependentTy);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +0000438}
439
Bruno Riccid7628d92019-01-09 15:43:19 +0000440OverloadExpr::OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
441 bool HasTemplateKWAndArgsInfo)
442 : Expr(SC, Empty) {
443 OverloadExprBits.NumResults = NumResults;
444 OverloadExprBits.HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
John McCall8c12dc42010-04-22 18:44:12 +0000445}
446
John McCall8cd78132009-11-19 22:55:06 +0000447// DependentScopeDeclRefExpr
Bruno Ricci49ee9642019-01-07 14:27:04 +0000448DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(
449 QualType Ty, NestedNameSpecifierLoc QualifierLoc,
450 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
451 const TemplateArgumentListInfo *Args)
452 : Expr(
453 DependentScopeDeclRefExprClass, Ty, VK_LValue, OK_Ordinary, true,
454 true,
455 (NameInfo.isInstantiationDependent() ||
456 (QualifierLoc &&
457 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
458 (NameInfo.containsUnexpandedParameterPack() ||
459 (QualifierLoc && QualifierLoc.getNestedNameSpecifier()
460 ->containsUnexpandedParameterPack()))),
461 QualifierLoc(QualifierLoc), NameInfo(NameInfo) {
462 DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
463 (Args != nullptr) || TemplateKWLoc.isValid();
Douglas Gregora6e053e2010-12-15 01:34:56 +0000464 if (Args) {
465 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +0000466 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +0000467 bool ContainsUnexpandedParameterPack
468 = ExprBits.ContainsUnexpandedParameterPack;
James Y Knighte7d82282015-12-29 18:15:14 +0000469 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
470 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
471 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000472 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
Abramo Bagnara7945c982012-01-27 09:46:47 +0000473 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +0000474 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
475 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +0000476 }
477}
478
Bruno Ricci49ee9642019-01-07 14:27:04 +0000479DependentScopeDeclRefExpr *DependentScopeDeclRefExpr::Create(
480 const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
481 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
482 const TemplateArgumentListInfo *Args) {
Reid Kleckner916ac4d2013-10-15 18:38:02 +0000483 assert(QualifierLoc && "should be created for dependent qualifiers");
James Y Knighte7d82282015-12-29 18:15:14 +0000484 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
485 std::size_t Size =
486 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
487 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
Bruno Ricci49ee9642019-01-07 14:27:04 +0000488 void *Mem = Context.Allocate(Size);
489 return new (Mem) DependentScopeDeclRefExpr(Context.DependentTy, QualifierLoc,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000490 TemplateKWLoc, NameInfo, Args);
John McCalle66edc12009-11-24 19:00:30 +0000491}
492
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000493DependentScopeDeclRefExpr *
Bruno Ricci49ee9642019-01-07 14:27:04 +0000494DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &Context,
Abramo Bagnara7945c982012-01-27 09:46:47 +0000495 bool HasTemplateKWAndArgsInfo,
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000496 unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +0000497 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
498 std::size_t Size =
499 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
500 HasTemplateKWAndArgsInfo, NumTemplateArgs);
Bruno Ricci49ee9642019-01-07 14:27:04 +0000501 void *Mem = Context.Allocate(Size);
502 auto *E = new (Mem) DependentScopeDeclRefExpr(
503 QualType(), NestedNameSpecifierLoc(), SourceLocation(),
504 DeclarationNameInfo(), nullptr);
505 E->DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
506 HasTemplateKWAndArgsInfo;
Douglas Gregor87866ce2011-02-04 12:01:24 +0000507 return E;
Argyrios Kyrtzidiscd444d1a2010-06-28 09:31:56 +0000508}
509
Stephen Kelly724e9e52018-08-09 20:05:03 +0000510SourceLocation CXXConstructExpr::getBeginLoc() const {
John McCall701417a2011-02-21 06:23:05 +0000511 if (isa<CXXTemporaryObjectExpr>(this))
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000512 return cast<CXXTemporaryObjectExpr>(this)->getBeginLoc();
Bruno Ricciddb8f6b2018-12-22 14:39:30 +0000513 return getLocation();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000514}
515
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000516SourceLocation CXXConstructExpr::getEndLoc() const {
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000517 if (isa<CXXTemporaryObjectExpr>(this))
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000518 return cast<CXXTemporaryObjectExpr>(this)->getEndLoc();
John McCall701417a2011-02-21 06:23:05 +0000519
Enea Zaffanella76e98fe2013-09-07 05:49:53 +0000520 if (ParenOrBraceRange.isValid())
521 return ParenOrBraceRange.getEnd();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000522
Bruno Ricciddb8f6b2018-12-22 14:39:30 +0000523 SourceLocation End = getLocation();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000524 for (unsigned I = getNumArgs(); I > 0; --I) {
525 const Expr *Arg = getArg(I-1);
526 if (!Arg->isDefaultArgument()) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000527 SourceLocation NewEnd = Arg->getEndLoc();
Douglas Gregor15417cf2010-11-03 00:35:38 +0000528 if (NewEnd.isValid()) {
529 End = NewEnd;
530 break;
531 }
532 }
533 }
534
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000535 return End;
Ted Kremenek49ace5c2009-12-23 04:00:48 +0000536}
537
Bruno Riccic5885cf2018-12-21 15:20:32 +0000538CXXOperatorCallExpr::CXXOperatorCallExpr(OverloadedOperatorKind OpKind,
539 Expr *Fn, ArrayRef<Expr *> Args,
540 QualType Ty, ExprValueKind VK,
541 SourceLocation OperatorLoc,
542 FPOptions FPFeatures,
543 ADLCallKind UsesADL)
544 : CallExpr(CXXOperatorCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
Bruno Riccifeb19232018-12-21 16:51:57 +0000545 OperatorLoc, /*MinNumArgs=*/0, UsesADL) {
546 CXXOperatorCallExprBits.OperatorKind = OpKind;
547 CXXOperatorCallExprBits.FPFeatures = FPFeatures.getInt();
Bruno Riccic4ba5462018-12-21 17:54:51 +0000548 assert(
549 (CXXOperatorCallExprBits.OperatorKind == static_cast<unsigned>(OpKind)) &&
550 "OperatorKind overflow!");
Bruno Riccifeb19232018-12-21 16:51:57 +0000551 assert((CXXOperatorCallExprBits.FPFeatures == FPFeatures.getInt()) &&
552 "FPFeatures overflow!");
Bruno Riccic5885cf2018-12-21 15:20:32 +0000553 Range = getSourceRangeImpl();
554}
555
556CXXOperatorCallExpr::CXXOperatorCallExpr(unsigned NumArgs, EmptyShell Empty)
557 : CallExpr(CXXOperatorCallExprClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
558
559CXXOperatorCallExpr *CXXOperatorCallExpr::Create(
560 const ASTContext &Ctx, OverloadedOperatorKind OpKind, Expr *Fn,
561 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
562 SourceLocation OperatorLoc, FPOptions FPFeatures, ADLCallKind UsesADL) {
563 // Allocate storage for the trailing objects of CallExpr.
564 unsigned NumArgs = Args.size();
565 unsigned SizeOfTrailingObjects =
566 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
567 void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
568 alignof(CXXOperatorCallExpr));
569 return new (Mem) CXXOperatorCallExpr(OpKind, Fn, Args, Ty, VK, OperatorLoc,
570 FPFeatures, UsesADL);
571}
572
573CXXOperatorCallExpr *CXXOperatorCallExpr::CreateEmpty(const ASTContext &Ctx,
574 unsigned NumArgs,
575 EmptyShell Empty) {
576 // Allocate storage for the trailing objects of CallExpr.
577 unsigned SizeOfTrailingObjects =
578 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
579 void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
580 alignof(CXXOperatorCallExpr));
581 return new (Mem) CXXOperatorCallExpr(NumArgs, Empty);
582}
583
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000584SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
Douglas Gregor993603d2008-11-14 16:09:21 +0000585 OverloadedOperatorKind Kind = getOperator();
586 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
587 if (getNumArgs() == 1)
588 // Prefix operator
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000589 return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000590 else
591 // Postfix operator
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000592 return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
Chandler Carruthf20ec9232011-04-02 09:47:38 +0000593 } else if (Kind == OO_Arrow) {
594 return getArg(0)->getSourceRange();
Douglas Gregor993603d2008-11-14 16:09:21 +0000595 } else if (Kind == OO_Call) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000596 return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000597 } else if (Kind == OO_Subscript) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000598 return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000599 } else if (getNumArgs() == 1) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000600 return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000601 } else if (getNumArgs() == 2) {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000602 return SourceRange(getArg(0)->getBeginLoc(), getArg(1)->getEndLoc());
Douglas Gregor993603d2008-11-14 16:09:21 +0000603 } else {
Argyrios Kyrtzidisd8e07692012-04-30 22:12:22 +0000604 return getOperatorLoc();
Douglas Gregor993603d2008-11-14 16:09:21 +0000605 }
606}
607
Bruno Riccic5885cf2018-12-21 15:20:32 +0000608CXXMemberCallExpr::CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args,
609 QualType Ty, ExprValueKind VK,
610 SourceLocation RP, unsigned MinNumArgs)
611 : CallExpr(CXXMemberCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK, RP,
612 MinNumArgs, NotADL) {}
613
614CXXMemberCallExpr::CXXMemberCallExpr(unsigned NumArgs, EmptyShell Empty)
615 : CallExpr(CXXMemberCallExprClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
616
617CXXMemberCallExpr *CXXMemberCallExpr::Create(const ASTContext &Ctx, Expr *Fn,
618 ArrayRef<Expr *> Args, QualType Ty,
619 ExprValueKind VK,
620 SourceLocation RP,
621 unsigned MinNumArgs) {
622 // Allocate storage for the trailing objects of CallExpr.
623 unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
624 unsigned SizeOfTrailingObjects =
625 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
626 void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
627 alignof(CXXMemberCallExpr));
628 return new (Mem) CXXMemberCallExpr(Fn, Args, Ty, VK, RP, MinNumArgs);
629}
630
631CXXMemberCallExpr *CXXMemberCallExpr::CreateEmpty(const ASTContext &Ctx,
632 unsigned NumArgs,
633 EmptyShell Empty) {
634 // Allocate storage for the trailing objects of CallExpr.
635 unsigned SizeOfTrailingObjects =
636 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
637 void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
638 alignof(CXXMemberCallExpr));
639 return new (Mem) CXXMemberCallExpr(NumArgs, Empty);
640}
641
Ted Kremenek98a24e32011-03-30 17:41:19 +0000642Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
Jordan Rose16fe35e2012-08-03 23:08:39 +0000643 const Expr *Callee = getCallee()->IgnoreParens();
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000644 if (const auto *MemExpr = dyn_cast<MemberExpr>(Callee))
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000645 return MemExpr->getBase();
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000646 if (const auto *BO = dyn_cast<BinaryOperator>(Callee))
Jordan Rose16fe35e2012-08-03 23:08:39 +0000647 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
648 return BO->getLHS();
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000649
650 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000651 return nullptr;
Douglas Gregor97fd6e22008-12-22 05:46:06 +0000652}
653
Ted Kremenek98a24e32011-03-30 17:41:19 +0000654CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000655 if (const auto *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
Ted Kremenek98a24e32011-03-30 17:41:19 +0000656 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
657
658 // FIXME: Will eventually need to cope with member pointers.
Craig Topper36250ad2014-05-12 05:36:57 +0000659 return nullptr;
Ted Kremenek98a24e32011-03-30 17:41:19 +0000660}
661
David Blaikiec0f58662012-05-03 16:25:49 +0000662CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
Chandler Carruth00426b42010-10-27 06:55:41 +0000663 Expr* ThisArg = getImplicitObjectArgument();
664 if (!ThisArg)
Craig Topper36250ad2014-05-12 05:36:57 +0000665 return nullptr;
Chandler Carruth00426b42010-10-27 06:55:41 +0000666
667 if (ThisArg->getType()->isAnyPointerType())
668 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
669
670 return ThisArg->getType()->getAsCXXRecordDecl();
671}
672
Douglas Gregore200adc2008-10-27 19:41:14 +0000673//===----------------------------------------------------------------------===//
674// Named casts
675//===----------------------------------------------------------------------===//
676
677/// getCastName - Get the name of the C++ cast being used, e.g.,
678/// "static_cast", "dynamic_cast", "reinterpret_cast", or
679/// "const_cast". The returned pointer must not be freed.
680const char *CXXNamedCastExpr::getCastName() const {
681 switch (getStmtClass()) {
682 case CXXStaticCastExprClass: return "static_cast";
683 case CXXDynamicCastExprClass: return "dynamic_cast";
684 case CXXReinterpretCastExprClass: return "reinterpret_cast";
685 case CXXConstCastExprClass: return "const_cast";
686 default: return "<invalid cast>";
687 }
688}
Douglas Gregordd04d332009-01-16 18:33:17 +0000689
Craig Toppera31a8822013-08-22 07:09:37 +0000690CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000691 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000692 CastKind K, Expr *Op,
693 const CXXCastPath *BasePath,
694 TypeSourceInfo *WrittenTy,
Fangrui Song6907ce22018-07-30 19:24:48 +0000695 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000696 SourceLocation RParenLoc,
697 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000698 unsigned PathSize = (BasePath ? BasePath->size() : 0);
Bruno Ricci49391652019-01-09 16:41:33 +0000699 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000700 auto *E =
701 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
702 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000703 if (PathSize)
704 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
705 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000706 return E;
707}
708
Craig Toppera31a8822013-08-22 07:09:37 +0000709CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000710 unsigned PathSize) {
Bruno Ricci49391652019-01-09 16:41:33 +0000711 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000712 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
713}
714
Craig Toppera31a8822013-08-22 07:09:37 +0000715CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000716 ExprValueKind VK,
John McCallcf142162010-08-07 06:22:56 +0000717 CastKind K, Expr *Op,
718 const CXXCastPath *BasePath,
719 TypeSourceInfo *WrittenTy,
Fangrui Song6907ce22018-07-30 19:24:48 +0000720 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000721 SourceLocation RParenLoc,
722 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000723 unsigned PathSize = (BasePath ? BasePath->size() : 0);
Bruno Ricci49391652019-01-09 16:41:33 +0000724 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000725 auto *E =
726 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
727 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000728 if (PathSize)
729 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
730 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000731 return E;
732}
733
Craig Toppera31a8822013-08-22 07:09:37 +0000734CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
John McCallcf142162010-08-07 06:22:56 +0000735 unsigned PathSize) {
Bruno Ricci49391652019-01-09 16:41:33 +0000736 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000737 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
738}
739
Anders Carlsson267c0c92011-04-11 01:43:55 +0000740/// isAlwaysNull - Return whether the result of the dynamic_cast is proven
741/// to always be null. For example:
742///
743/// struct A { };
744/// struct B final : A { };
745/// struct C { };
746///
747/// C *f(B* b) { return dynamic_cast<C*>(b); }
748bool CXXDynamicCastExpr::isAlwaysNull() const
749{
750 QualType SrcType = getSubExpr()->getType();
751 QualType DestType = getType();
752
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000753 if (const auto *SrcPTy = SrcType->getAs<PointerType>()) {
Anders Carlsson267c0c92011-04-11 01:43:55 +0000754 SrcType = SrcPTy->getPointeeType();
755 DestType = DestType->castAs<PointerType>()->getPointeeType();
756 }
757
Alexis Hunt78e2b912012-06-19 23:44:55 +0000758 if (DestType->isVoidType())
759 return false;
760
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000761 const auto *SrcRD =
762 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
Anders Carlsson267c0c92011-04-11 01:43:55 +0000763
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000764 if (!SrcRD->hasAttr<FinalAttr>())
765 return false;
766
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000767 const auto *DestRD =
768 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
Anders Carlsson267c0c92011-04-11 01:43:55 +0000769
770 return !DestRD->isDerivedFrom(SrcRD);
771}
772
John McCallcf142162010-08-07 06:22:56 +0000773CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000774CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
775 ExprValueKind VK, CastKind K, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000776 const CXXCastPath *BasePath,
Fangrui Song6907ce22018-07-30 19:24:48 +0000777 TypeSourceInfo *WrittenTy, SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000778 SourceLocation RParenLoc,
779 SourceRange AngleBrackets) {
John McCallcf142162010-08-07 06:22:56 +0000780 unsigned PathSize = (BasePath ? BasePath->size() : 0);
Bruno Ricci49391652019-01-09 16:41:33 +0000781 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000782 auto *E =
783 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
784 RParenLoc, AngleBrackets);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000785 if (PathSize)
786 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
787 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000788 return E;
789}
790
791CXXReinterpretCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000792CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
Bruno Ricci49391652019-01-09 16:41:33 +0000793 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000794 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
795}
796
Craig Toppera31a8822013-08-22 07:09:37 +0000797CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
John McCall7decc9e2010-11-18 06:31:45 +0000798 ExprValueKind VK, Expr *Op,
John McCallcf142162010-08-07 06:22:56 +0000799 TypeSourceInfo *WrittenTy,
Fangrui Song6907ce22018-07-30 19:24:48 +0000800 SourceLocation L,
Fariborz Jahanianf0738712013-02-22 22:02:53 +0000801 SourceLocation RParenLoc,
802 SourceRange AngleBrackets) {
803 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
John McCallcf142162010-08-07 06:22:56 +0000804}
805
Craig Toppera31a8822013-08-22 07:09:37 +0000806CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
John McCallcf142162010-08-07 06:22:56 +0000807 return new (C) CXXConstCastExpr(EmptyShell());
808}
809
810CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000811CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
Eli Friedman89fe0d52013-08-15 22:02:56 +0000812 TypeSourceInfo *Written, CastKind K, Expr *Op,
813 const CXXCastPath *BasePath,
814 SourceLocation L, SourceLocation R) {
John McCallcf142162010-08-07 06:22:56 +0000815 unsigned PathSize = (BasePath ? BasePath->size() : 0);
Bruno Ricci49391652019-01-09 16:41:33 +0000816 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
Eugene Zelenkobc5858b2018-04-10 22:54:42 +0000817 auto *E =
818 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
James Y Knight1d75c5e2015-12-30 02:27:28 +0000819 if (PathSize)
820 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
821 E->getTrailingObjects<CXXBaseSpecifier *>());
John McCallcf142162010-08-07 06:22:56 +0000822 return E;
823}
824
825CXXFunctionalCastExpr *
Craig Toppera31a8822013-08-22 07:09:37 +0000826CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
Bruno Ricci49391652019-01-09 16:41:33 +0000827 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
John McCallcf142162010-08-07 06:22:56 +0000828 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
829}
830
Stephen Kelly724e9e52018-08-09 20:05:03 +0000831SourceLocation CXXFunctionalCastExpr::getBeginLoc() const {
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000832 return getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
Eli Friedman89fe0d52013-08-15 22:02:56 +0000833}
834
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000835SourceLocation CXXFunctionalCastExpr::getEndLoc() const {
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000836 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getEndLoc();
Eli Friedman89fe0d52013-08-15 22:02:56 +0000837}
838
Bruno Riccic5885cf2018-12-21 15:20:32 +0000839UserDefinedLiteral::UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args,
840 QualType Ty, ExprValueKind VK,
841 SourceLocation LitEndLoc,
842 SourceLocation SuffixLoc)
843 : CallExpr(UserDefinedLiteralClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
844 LitEndLoc, /*MinNumArgs=*/0, NotADL),
845 UDSuffixLoc(SuffixLoc) {}
846
847UserDefinedLiteral::UserDefinedLiteral(unsigned NumArgs, EmptyShell Empty)
848 : CallExpr(UserDefinedLiteralClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
849
850UserDefinedLiteral *UserDefinedLiteral::Create(const ASTContext &Ctx, Expr *Fn,
851 ArrayRef<Expr *> Args,
852 QualType Ty, ExprValueKind VK,
853 SourceLocation LitEndLoc,
854 SourceLocation SuffixLoc) {
855 // Allocate storage for the trailing objects of CallExpr.
856 unsigned NumArgs = Args.size();
857 unsigned SizeOfTrailingObjects =
858 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
859 void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
860 alignof(UserDefinedLiteral));
861 return new (Mem) UserDefinedLiteral(Fn, Args, Ty, VK, LitEndLoc, SuffixLoc);
862}
863
864UserDefinedLiteral *UserDefinedLiteral::CreateEmpty(const ASTContext &Ctx,
865 unsigned NumArgs,
866 EmptyShell Empty) {
867 // Allocate storage for the trailing objects of CallExpr.
868 unsigned SizeOfTrailingObjects =
869 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
870 void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
871 alignof(UserDefinedLiteral));
872 return new (Mem) UserDefinedLiteral(NumArgs, Empty);
873}
874
Richard Smithc67fdd42012-03-07 08:35:16 +0000875UserDefinedLiteral::LiteralOperatorKind
876UserDefinedLiteral::getLiteralOperatorKind() const {
877 if (getNumArgs() == 0)
878 return LOK_Template;
879 if (getNumArgs() == 2)
880 return LOK_String;
881
882 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
883 QualType ParamTy =
884 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
885 if (ParamTy->isPointerType())
886 return LOK_Raw;
887 if (ParamTy->isAnyCharacterType())
888 return LOK_Character;
889 if (ParamTy->isIntegerType())
890 return LOK_Integer;
891 if (ParamTy->isFloatingType())
892 return LOK_Floating;
893
894 llvm_unreachable("unknown kind of literal operator");
895}
896
897Expr *UserDefinedLiteral::getCookedLiteral() {
898#ifndef NDEBUG
899 LiteralOperatorKind LOK = getLiteralOperatorKind();
900 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
901#endif
902 return getArg(0);
903}
904
905const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
906 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
907}
John McCallcf142162010-08-07 06:22:56 +0000908
Bruno Riccifd66eb82018-11-17 13:02:47 +0000909CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &Ctx, SourceLocation Loc,
Eric Fiselier708afb52019-05-16 21:04:15 +0000910 FieldDecl *Field, QualType Ty,
911 DeclContext *UsedContext)
Bruno Riccifd66eb82018-11-17 13:02:47 +0000912 : Expr(CXXDefaultInitExprClass, Ty.getNonLValueExprType(Ctx),
913 Ty->isLValueReferenceType() ? VK_LValue : Ty->isRValueReferenceType()
Richard Smith852c9db2013-04-20 22:23:05 +0000914 ? VK_XValue
915 : VK_RValue,
916 /*FIXME*/ OK_Ordinary, false, false, false, false),
Eric Fiselier708afb52019-05-16 21:04:15 +0000917 Field(Field), UsedContext(UsedContext) {
Bruno Riccifd66eb82018-11-17 13:02:47 +0000918 CXXDefaultInitExprBits.Loc = Loc;
Richard Smith852c9db2013-04-20 22:23:05 +0000919 assert(Field->hasInClassInitializer());
920}
921
Craig Toppera31a8822013-08-22 07:09:37 +0000922CXXTemporary *CXXTemporary::Create(const ASTContext &C,
Anders Carlssonffda6062009-05-30 20:34:37 +0000923 const CXXDestructorDecl *Destructor) {
Anders Carlsson73b836b2009-05-30 22:38:53 +0000924 return new (C) CXXTemporary(Destructor);
925}
926
Craig Toppera31a8822013-08-22 07:09:37 +0000927CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
Anders Carlsson993a4b32009-05-30 20:03:25 +0000928 CXXTemporary *Temp,
929 Expr* SubExpr) {
Peter Collingbournefbef4c82011-11-27 22:09:28 +0000930 assert((SubExpr->getType()->isRecordType() ||
931 SubExpr->getType()->isArrayType()) &&
932 "Expression bound to a temporary must have record or array type!");
Anders Carlsson993a4b32009-05-30 20:03:25 +0000933
Douglas Gregora6e053e2010-12-15 01:34:56 +0000934 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
Anders Carlsson993a4b32009-05-30 20:03:25 +0000935}
936
Bruno Ricciddb8f6b2018-12-22 14:39:30 +0000937CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(
938 CXXConstructorDecl *Cons, QualType Ty, TypeSourceInfo *TSI,
939 ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
940 bool HadMultipleCandidates, bool ListInitialization,
941 bool StdInitListInitialization, bool ZeroInitialization)
942 : CXXConstructExpr(
943 CXXTemporaryObjectExprClass, Ty, TSI->getTypeLoc().getBeginLoc(),
944 Cons, /* Elidable=*/false, Args, HadMultipleCandidates,
945 ListInitialization, StdInitListInitialization, ZeroInitialization,
946 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
947 TSI(TSI) {}
948
949CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(EmptyShell Empty,
950 unsigned NumArgs)
951 : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty, NumArgs) {}
952
953CXXTemporaryObjectExpr *CXXTemporaryObjectExpr::Create(
954 const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
955 TypeSourceInfo *TSI, ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
956 bool HadMultipleCandidates, bool ListInitialization,
957 bool StdInitListInitialization, bool ZeroInitialization) {
958 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
959 void *Mem =
960 Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
961 alignof(CXXTemporaryObjectExpr));
962 return new (Mem) CXXTemporaryObjectExpr(
963 Cons, Ty, TSI, Args, ParenOrBraceRange, HadMultipleCandidates,
964 ListInitialization, StdInitListInitialization, ZeroInitialization);
965}
966
967CXXTemporaryObjectExpr *
968CXXTemporaryObjectExpr::CreateEmpty(const ASTContext &Ctx, unsigned NumArgs) {
969 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
970 void *Mem =
971 Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
972 alignof(CXXTemporaryObjectExpr));
973 return new (Mem) CXXTemporaryObjectExpr(EmptyShell(), NumArgs);
974}
Douglas Gregor2b88c112010-09-08 00:15:04 +0000975
Stephen Kelly724e9e52018-08-09 20:05:03 +0000976SourceLocation CXXTemporaryObjectExpr::getBeginLoc() const {
Bruno Ricciddb8f6b2018-12-22 14:39:30 +0000977 return getTypeSourceInfo()->getTypeLoc().getBeginLoc();
Erik Verbruggen11a2ecc2012-12-25 14:51:39 +0000978}
979
Stephen Kelly02a67ba2018-08-09 20:05:47 +0000980SourceLocation CXXTemporaryObjectExpr::getEndLoc() const {
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000981 SourceLocation Loc = getParenOrBraceRange().getEnd();
982 if (Loc.isInvalid() && getNumArgs())
Stephen Kelly1c301dc2018-08-09 21:09:38 +0000983 Loc = getArg(getNumArgs() - 1)->getEndLoc();
Argyrios Kyrtzidis623ecfd2013-09-11 23:23:27 +0000984 return Loc;
Douglas Gregordd04d332009-01-16 18:33:17 +0000985}
Anders Carlsson6f287832009-04-21 02:22:11 +0000986
Bruno Ricciddb8f6b2018-12-22 14:39:30 +0000987CXXConstructExpr *CXXConstructExpr::Create(
988 const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
989 CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
990 bool HadMultipleCandidates, bool ListInitialization,
991 bool StdInitListInitialization, bool ZeroInitialization,
992 ConstructionKind ConstructKind, SourceRange ParenOrBraceRange) {
993 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
994 void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
995 alignof(CXXConstructExpr));
996 return new (Mem) CXXConstructExpr(
997 CXXConstructExprClass, Ty, Loc, Ctor, Elidable, Args,
998 HadMultipleCandidates, ListInitialization, StdInitListInitialization,
999 ZeroInitialization, ConstructKind, ParenOrBraceRange);
Anders Carlsson0781ce72009-04-23 02:32:43 +00001000}
1001
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001002CXXConstructExpr *CXXConstructExpr::CreateEmpty(const ASTContext &Ctx,
1003 unsigned NumArgs) {
1004 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
1005 void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
1006 alignof(CXXConstructExpr));
1007 return new (Mem)
1008 CXXConstructExpr(CXXConstructExprClass, EmptyShell(), NumArgs);
1009}
Fangrui Song6907ce22018-07-30 19:24:48 +00001010
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001011CXXConstructExpr::CXXConstructExpr(
1012 StmtClass SC, QualType Ty, SourceLocation Loc, CXXConstructorDecl *Ctor,
1013 bool Elidable, ArrayRef<Expr *> Args, bool HadMultipleCandidates,
1014 bool ListInitialization, bool StdInitListInitialization,
1015 bool ZeroInitialization, ConstructionKind ConstructKind,
1016 SourceRange ParenOrBraceRange)
1017 : Expr(SC, Ty, VK_RValue, OK_Ordinary, Ty->isDependentType(),
1018 Ty->isDependentType(), Ty->isInstantiationDependentType(),
1019 Ty->containsUnexpandedParameterPack()),
1020 Constructor(Ctor), ParenOrBraceRange(ParenOrBraceRange),
1021 NumArgs(Args.size()) {
1022 CXXConstructExprBits.Elidable = Elidable;
1023 CXXConstructExprBits.HadMultipleCandidates = HadMultipleCandidates;
1024 CXXConstructExprBits.ListInitialization = ListInitialization;
1025 CXXConstructExprBits.StdInitListInitialization = StdInitListInitialization;
1026 CXXConstructExprBits.ZeroInitialization = ZeroInitialization;
1027 CXXConstructExprBits.ConstructionKind = ConstructKind;
1028 CXXConstructExprBits.Loc = Loc;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001029
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001030 Stmt **TrailingArgs = getTrailingArgs();
1031 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1032 assert(Args[I] && "NULL argument in CXXConstructExpr!");
Fangrui Song6907ce22018-07-30 19:24:48 +00001033
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001034 if (Args[I]->isValueDependent())
1035 ExprBits.ValueDependent = true;
1036 if (Args[I]->isInstantiationDependent())
1037 ExprBits.InstantiationDependent = true;
1038 if (Args[I]->containsUnexpandedParameterPack())
1039 ExprBits.ContainsUnexpandedParameterPack = true;
1040
1041 TrailingArgs[I] = Args[I];
Douglas Gregor4f4b1862009-12-16 18:50:27 +00001042 }
Anders Carlsson0781ce72009-04-23 02:32:43 +00001043}
1044
Bruno Ricciddb8f6b2018-12-22 14:39:30 +00001045CXXConstructExpr::CXXConstructExpr(StmtClass SC, EmptyShell Empty,
1046 unsigned NumArgs)
1047 : Expr(SC, Empty), NumArgs(NumArgs) {}
1048
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001049LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
Douglas Gregore31e6062012-02-07 10:09:13 +00001050 LambdaCaptureKind Kind, VarDecl *Var,
1051 SourceLocation EllipsisLoc)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001052 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001053 unsigned Bits = 0;
1054 if (Implicit)
1055 Bits |= Capture_Implicit;
Fangrui Song6907ce22018-07-30 19:24:48 +00001056
Douglas Gregore31e6062012-02-07 10:09:13 +00001057 switch (Kind) {
Faisal Validc6b5962016-03-21 09:25:37 +00001058 case LCK_StarThis:
1059 Bits |= Capture_ByCopy;
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +00001060 LLVM_FALLTHROUGH;
Craig Topper36250ad2014-05-12 05:36:57 +00001061 case LCK_This:
1062 assert(!Var && "'this' capture cannot have a variable!");
John Brawn771721c2016-06-15 14:14:51 +00001063 Bits |= Capture_This;
Douglas Gregore31e6062012-02-07 10:09:13 +00001064 break;
1065
1066 case LCK_ByCopy:
1067 Bits |= Capture_ByCopy;
Adrian Prantlf3b3ccd2017-12-19 22:06:11 +00001068 LLVM_FALLTHROUGH;
Douglas Gregore31e6062012-02-07 10:09:13 +00001069 case LCK_ByRef:
1070 assert(Var && "capture must have a variable!");
1071 break;
Alexey Bataev39c81e22014-08-28 04:28:19 +00001072 case LCK_VLAType:
1073 assert(!Var && "VLA type capture cannot have a variable!");
Alexey Bataev39c81e22014-08-28 04:28:19 +00001074 break;
Douglas Gregore31e6062012-02-07 10:09:13 +00001075 }
John Brawn771721c2016-06-15 14:14:51 +00001076 DeclAndBits.setInt(Bits);
Douglas Gregore31e6062012-02-07 10:09:13 +00001077}
1078
Benjamin Kramerf3ca26982014-05-10 16:31:55 +00001079LambdaCaptureKind LambdaCapture::getCaptureKind() const {
John Brawn771721c2016-06-15 14:14:51 +00001080 if (capturesVLAType())
Faisal Validc6b5962016-03-21 09:25:37 +00001081 return LCK_VLAType;
John Brawn771721c2016-06-15 14:14:51 +00001082 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
1083 if (capturesThis())
Faisal Validc6b5962016-03-21 09:25:37 +00001084 return CapByCopy ? LCK_StarThis : LCK_This;
Alexey Bataev39c81e22014-08-28 04:28:19 +00001085 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
Douglas Gregore31e6062012-02-07 10:09:13 +00001086}
1087
James Y Knighte00a67e2015-12-31 04:18:25 +00001088LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
Douglas Gregore31e6062012-02-07 10:09:13 +00001089 LambdaCaptureDefault CaptureDefault,
James Dennettddd36ff2013-08-09 23:08:25 +00001090 SourceLocation CaptureDefaultLoc,
James Y Knighte00a67e2015-12-31 04:18:25 +00001091 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
1092 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
Richard Smith2589b9802012-07-25 03:56:55 +00001093 SourceLocation ClosingBrace,
1094 bool ContainsUnexpandedParameterPack)
James Y Knighte00a67e2015-12-31 04:18:25 +00001095 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
1096 T->isDependentType(), T->isDependentType(),
1097 ContainsUnexpandedParameterPack),
1098 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
1099 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
1100 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
1101 ClosingBrace(ClosingBrace) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001102 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
Douglas Gregorc8a73492012-02-13 15:44:47 +00001103 CXXRecordDecl *Class = getLambdaClass();
1104 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
Fangrui Song6907ce22018-07-30 19:24:48 +00001105
Douglas Gregorc8a73492012-02-13 15:44:47 +00001106 // FIXME: Propagate "has unexpanded parameter pack" bit.
Fangrui Song6907ce22018-07-30 19:24:48 +00001107
Douglas Gregore5561632012-02-13 17:20:40 +00001108 // Copy captures.
Craig Toppera31a8822013-08-22 07:09:37 +00001109 const ASTContext &Context = Class->getASTContext();
Douglas Gregore5561632012-02-13 17:20:40 +00001110 Data.NumCaptures = NumCaptures;
1111 Data.NumExplicitCaptures = 0;
James Y Knighte00a67e2015-12-31 04:18:25 +00001112 Data.Captures =
1113 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
1114 LambdaCapture *ToCapture = Data.Captures;
Douglas Gregore5561632012-02-13 17:20:40 +00001115 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
1116 if (Captures[I].isExplicit())
1117 ++Data.NumExplicitCaptures;
Fangrui Song6907ce22018-07-30 19:24:48 +00001118
Douglas Gregore5561632012-02-13 17:20:40 +00001119 *ToCapture++ = Captures[I];
1120 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001121
Douglas Gregore5561632012-02-13 17:20:40 +00001122 // Copy initialization expressions for the non-static data members.
1123 Stmt **Stored = getStoredStmts();
1124 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
1125 *Stored++ = CaptureInits[I];
Fangrui Song6907ce22018-07-30 19:24:48 +00001126
Douglas Gregore5561632012-02-13 17:20:40 +00001127 // Copy the body of the lambda.
1128 *Stored++ = getCallOperator()->getBody();
Douglas Gregore31e6062012-02-07 10:09:13 +00001129}
1130
James Y Knighte00a67e2015-12-31 04:18:25 +00001131LambdaExpr *LambdaExpr::Create(
1132 const ASTContext &Context, CXXRecordDecl *Class,
1133 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
1134 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
1135 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
James Y Knighte00a67e2015-12-31 04:18:25 +00001136 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
Douglas Gregore31e6062012-02-07 10:09:13 +00001137 // Determine the type of the expression (i.e., the type of the
1138 // function object we're creating).
1139 QualType T = Context.getTypeDeclType(Class);
Douglas Gregore31e6062012-02-07 10:09:13 +00001140
Richard Smith30e304e2016-12-14 00:03:17 +00001141 unsigned Size = totalSizeToAlloc<Stmt *>(Captures.size() + 1);
Douglas Gregore5561632012-02-13 17:20:40 +00001142 void *Mem = Context.Allocate(Size);
Richard Smith30e304e2016-12-14 00:03:17 +00001143 return new (Mem)
1144 LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
1145 Captures, ExplicitParams, ExplicitResultType, CaptureInits,
1146 ClosingBrace, ContainsUnexpandedParameterPack);
Douglas Gregore31e6062012-02-07 10:09:13 +00001147}
1148
Craig Toppera31a8822013-08-22 07:09:37 +00001149LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
Richard Smith30e304e2016-12-14 00:03:17 +00001150 unsigned NumCaptures) {
1151 unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001152 void *Mem = C.Allocate(Size);
Richard Smith30e304e2016-12-14 00:03:17 +00001153 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001154}
1155
James Dennettdd2ffea22015-05-07 18:48:18 +00001156bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
1157 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
1158 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
1159}
1160
Douglas Gregorc8a73492012-02-13 15:44:47 +00001161LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001162 return getLambdaClass()->getLambdaData().Captures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001163}
1164
1165LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
Douglas Gregore5561632012-02-13 17:20:40 +00001166 return capture_begin() + NumCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001167}
1168
James Dennett1575cb42014-05-27 19:13:04 +00001169LambdaExpr::capture_range LambdaExpr::captures() const {
1170 return capture_range(capture_begin(), capture_end());
1171}
1172
Douglas Gregorc8a73492012-02-13 15:44:47 +00001173LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1174 return capture_begin();
1175}
1176
1177LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1178 struct CXXRecordDecl::LambdaDefinitionData &Data
1179 = getLambdaClass()->getLambdaData();
Douglas Gregore5561632012-02-13 17:20:40 +00001180 return Data.Captures + Data.NumExplicitCaptures;
Douglas Gregorc8a73492012-02-13 15:44:47 +00001181}
1182
James Dennett1575cb42014-05-27 19:13:04 +00001183LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1184 return capture_range(explicit_capture_begin(), explicit_capture_end());
1185}
1186
Douglas Gregorc8a73492012-02-13 15:44:47 +00001187LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1188 return explicit_capture_end();
1189}
1190
1191LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1192 return capture_end();
1193}
1194
James Dennett1575cb42014-05-27 19:13:04 +00001195LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1196 return capture_range(implicit_capture_begin(), implicit_capture_end());
1197}
1198
Douglas Gregore31e6062012-02-07 10:09:13 +00001199CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1200 return getType()->getAsCXXRecordDecl();
1201}
1202
1203CXXMethodDecl *LambdaExpr::getCallOperator() const {
1204 CXXRecordDecl *Record = getLambdaClass();
Fangrui Song6907ce22018-07-30 19:24:48 +00001205 return Record->getLambdaCallOperator();
Faisal Vali2b391ab2013-09-26 19:54:12 +00001206}
1207
1208TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1209 CXXRecordDecl *Record = getLambdaClass();
1210 return Record->getGenericLambdaTemplateParameterList();
Hamza Sood8205a812019-05-04 10:49:46 +00001211}
Faisal Vali2b391ab2013-09-26 19:54:12 +00001212
Hamza Sood8205a812019-05-04 10:49:46 +00001213ArrayRef<NamedDecl *> LambdaExpr::getExplicitTemplateParameters() const {
1214 const CXXRecordDecl *Record = getLambdaClass();
1215 return Record->getLambdaExplicitTemplateParameters();
Douglas Gregore31e6062012-02-07 10:09:13 +00001216}
1217
Douglas Gregor99ae8062012-02-14 17:54:36 +00001218CompoundStmt *LambdaExpr::getBody() const {
James Y Knight53c76162015-07-17 18:21:37 +00001219 // FIXME: this mutation in getBody is bogus. It should be
1220 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1221 // don't understand, that doesn't work.
Douglas Gregor99ae8062012-02-14 17:54:36 +00001222 if (!getStoredStmts()[NumCaptures])
Eugene Zelenko821f6982017-11-18 01:47:41 +00001223 *const_cast<Stmt **>(&getStoredStmts()[NumCaptures]) =
James Y Knight53c76162015-07-17 18:21:37 +00001224 getCallOperator()->getBody();
1225
James Y Knighte00a67e2015-12-31 04:18:25 +00001226 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
Douglas Gregor99ae8062012-02-14 17:54:36 +00001227}
1228
Douglas Gregore31e6062012-02-07 10:09:13 +00001229bool LambdaExpr::isMutable() const {
David Blaikief5697e52012-08-10 00:55:35 +00001230 return !getCallOperator()->isConst();
Douglas Gregore31e6062012-02-07 10:09:13 +00001231}
1232
John McCall28fc7092011-11-10 05:35:25 +00001233ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001234 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001235 ArrayRef<CleanupObject> objects)
Bill Wendling7c44da22018-10-31 03:48:47 +00001236 : FullExpr(ExprWithCleanupsClass, subexpr) {
Tim Shen4a05bb82016-06-21 20:29:17 +00001237 ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
John McCall28fc7092011-11-10 05:35:25 +00001238 ExprWithCleanupsBits.NumObjects = objects.size();
1239 for (unsigned i = 0, e = objects.size(); i != e; ++i)
James Y Knighte00a67e2015-12-31 04:18:25 +00001240 getTrailingObjects<CleanupObject>()[i] = objects[i];
Anders Carlssondefc6442009-04-24 22:47:04 +00001241}
1242
Craig Toppera31a8822013-08-22 07:09:37 +00001243ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
Tim Shen4a05bb82016-06-21 20:29:17 +00001244 bool CleanupsHaveSideEffects,
John McCall28fc7092011-11-10 05:35:25 +00001245 ArrayRef<CleanupObject> objects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001246 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001247 alignof(ExprWithCleanups));
Tim Shen4a05bb82016-06-21 20:29:17 +00001248 return new (buffer)
1249 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
Chris Lattnercba86142010-05-10 00:25:06 +00001250}
1251
John McCall28fc7092011-11-10 05:35:25 +00001252ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
Bill Wendling7c44da22018-10-31 03:48:47 +00001253 : FullExpr(ExprWithCleanupsClass, empty) {
John McCall28fc7092011-11-10 05:35:25 +00001254 ExprWithCleanupsBits.NumObjects = numObjects;
1255}
Chris Lattnercba86142010-05-10 00:25:06 +00001256
Craig Toppera31a8822013-08-22 07:09:37 +00001257ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1258 EmptyShell empty,
John McCall28fc7092011-11-10 05:35:25 +00001259 unsigned numObjects) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001260 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
Benjamin Kramerc3f89252016-10-20 14:27:22 +00001261 alignof(ExprWithCleanups));
John McCall28fc7092011-11-10 05:35:25 +00001262 return new (buffer) ExprWithCleanups(empty, numObjects);
Anders Carlsson73b836b2009-05-30 22:38:53 +00001263}
1264
Bruno Ricci49ee9642019-01-07 14:27:04 +00001265CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *TSI,
1266 SourceLocation LParenLoc,
1267 ArrayRef<Expr *> Args,
1268 SourceLocation RParenLoc)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001269 : Expr(CXXUnresolvedConstructExprClass,
Bruno Ricci49ee9642019-01-07 14:27:04 +00001270 TSI->getType().getNonReferenceType(),
1271 (TSI->getType()->isLValueReferenceType()
Eugene Zelenko821f6982017-11-18 01:47:41 +00001272 ? VK_LValue
Bruno Ricci49ee9642019-01-07 14:27:04 +00001273 : TSI->getType()->isRValueReferenceType() ? VK_XValue
1274 : VK_RValue),
Eugene Zelenko821f6982017-11-18 01:47:41 +00001275 OK_Ordinary,
Bruno Ricci49ee9642019-01-07 14:27:04 +00001276 TSI->getType()->isDependentType() ||
1277 TSI->getType()->getContainedDeducedType(),
1278 true, true, TSI->getType()->containsUnexpandedParameterPack()),
1279 TSI(TSI), LParenLoc(LParenLoc), RParenLoc(RParenLoc) {
1280 CXXUnresolvedConstructExprBits.NumArgs = Args.size();
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001281 auto **StoredArgs = getTrailingObjects<Expr *>();
Benjamin Kramerc215e762012-08-24 11:54:20 +00001282 for (unsigned I = 0; I != Args.size(); ++I) {
Douglas Gregora6e053e2010-12-15 01:34:56 +00001283 if (Args[I]->containsUnexpandedParameterPack())
1284 ExprBits.ContainsUnexpandedParameterPack = true;
1285
1286 StoredArgs[I] = Args[I];
1287 }
Douglas Gregorce934142009-05-20 18:46:25 +00001288}
1289
Bruno Ricci49ee9642019-01-07 14:27:04 +00001290CXXUnresolvedConstructExpr *CXXUnresolvedConstructExpr::Create(
1291 const ASTContext &Context, TypeSourceInfo *TSI, SourceLocation LParenLoc,
1292 ArrayRef<Expr *> Args, SourceLocation RParenLoc) {
1293 void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
1294 return new (Mem) CXXUnresolvedConstructExpr(TSI, LParenLoc, Args, RParenLoc);
Douglas Gregorce934142009-05-20 18:46:25 +00001295}
1296
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001297CXXUnresolvedConstructExpr *
Bruno Ricci49ee9642019-01-07 14:27:04 +00001298CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &Context,
1299 unsigned NumArgs) {
1300 void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
1301 return new (Mem) CXXUnresolvedConstructExpr(EmptyShell(), NumArgs);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001302}
1303
Stephen Kelly724e9e52018-08-09 20:05:03 +00001304SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const {
Bruno Ricci49ee9642019-01-07 14:27:04 +00001305 return TSI->getTypeLoc().getBeginLoc();
Douglas Gregor2b88c112010-09-08 00:15:04 +00001306}
1307
James Y Knighte7d82282015-12-29 18:15:14 +00001308CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001309 const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
James Y Knighte7d82282015-12-29 18:15:14 +00001310 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1311 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1312 DeclarationNameInfo MemberNameInfo,
1313 const TemplateArgumentListInfo *TemplateArgs)
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001314 : Expr(CXXDependentScopeMemberExprClass, Ctx.DependentTy, VK_LValue,
James Y Knighte7d82282015-12-29 18:15:14 +00001315 OK_Ordinary, true, true, true,
1316 ((Base && Base->containsUnexpandedParameterPack()) ||
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001317 (QualifierLoc && QualifierLoc.getNestedNameSpecifier()
1318 ->containsUnexpandedParameterPack()) ||
James Y Knighte7d82282015-12-29 18:15:14 +00001319 MemberNameInfo.containsUnexpandedParameterPack())),
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001320 Base(Base), BaseType(BaseType), QualifierLoc(QualifierLoc),
James Y Knighte7d82282015-12-29 18:15:14 +00001321 MemberNameInfo(MemberNameInfo) {
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001322 CXXDependentScopeMemberExprBits.IsArrow = IsArrow;
1323 CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
1324 (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
1325 CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
1326 FirstQualifierFoundInScope != nullptr;
1327 CXXDependentScopeMemberExprBits.OperatorLoc = OperatorLoc;
1328
Douglas Gregora6e053e2010-12-15 01:34:56 +00001329 if (TemplateArgs) {
1330 bool Dependent = true;
Douglas Gregor678d76c2011-07-01 01:22:09 +00001331 bool InstantiationDependent = true;
Douglas Gregora6e053e2010-12-15 01:34:56 +00001332 bool ContainsUnexpandedParameterPack = false;
James Y Knighte7d82282015-12-29 18:15:14 +00001333 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1334 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1335 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001336 if (ContainsUnexpandedParameterPack)
1337 ExprBits.ContainsUnexpandedParameterPack = true;
Abramo Bagnara7945c982012-01-27 09:46:47 +00001338 } else if (TemplateKWLoc.isValid()) {
James Y Knighte7d82282015-12-29 18:15:14 +00001339 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1340 TemplateKWLoc);
Douglas Gregora6e053e2010-12-15 01:34:56 +00001341 }
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001342
1343 if (hasFirstQualifierFoundInScope())
1344 *getTrailingObjects<NamedDecl *>() = FirstQualifierFoundInScope;
Douglas Gregor308047d2009-09-09 00:23:06 +00001345}
1346
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001347CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1348 EmptyShell Empty, bool HasTemplateKWAndArgsInfo,
1349 bool HasFirstQualifierFoundInScope)
1350 : Expr(CXXDependentScopeMemberExprClass, Empty) {
1351 CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
1352 HasTemplateKWAndArgsInfo;
1353 CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
1354 HasFirstQualifierFoundInScope;
1355}
1356
1357CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::Create(
1358 const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
1359 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1360 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1361 DeclarationNameInfo MemberNameInfo,
1362 const TemplateArgumentListInfo *TemplateArgs) {
1363 bool HasTemplateKWAndArgsInfo =
1364 (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
Abramo Bagnara7945c982012-01-27 09:46:47 +00001365 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001366 bool HasFirstQualifierFoundInScope = FirstQualifierFoundInScope != nullptr;
John McCall6b51f282009-11-23 01:53:49 +00001367
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001368 unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
1369 TemplateArgumentLoc, NamedDecl *>(
1370 HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
1371
1372 void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
1373 return new (Mem) CXXDependentScopeMemberExpr(
1374 Ctx, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
1375 FirstQualifierFoundInScope, MemberNameInfo, TemplateArgs);
Douglas Gregor308047d2009-09-09 00:23:06 +00001376}
1377
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001378CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::CreateEmpty(
1379 const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo,
1380 unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope) {
James Y Knighte7d82282015-12-29 18:15:14 +00001381 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
Argyrios Kyrtzidisbfcacee2010-06-24 08:57:31 +00001382
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001383 unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
1384 TemplateArgumentLoc, NamedDecl *>(
1385 HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
Fangrui Song6907ce22018-07-30 19:24:48 +00001386
Bruno Ricci2e6dc532019-01-08 14:17:00 +00001387 void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
1388 return new (Mem) CXXDependentScopeMemberExpr(
1389 EmptyShell(), HasTemplateKWAndArgsInfo, HasFirstQualifierFoundInScope);
Douglas Gregor0da1d432011-02-28 20:01:57 +00001390}
1391
John McCall0009fcc2011-04-26 20:42:42 +00001392static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1393 UnresolvedSetIterator end) {
1394 do {
1395 NamedDecl *decl = *begin;
1396 if (isa<UnresolvedUsingValueDecl>(decl))
1397 return false;
John McCall0009fcc2011-04-26 20:42:42 +00001398
1399 // Unresolved member expressions should only contain methods and
1400 // method templates.
Alp Tokera2794f92014-01-22 07:29:52 +00001401 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1402 ->isStatic())
John McCall0009fcc2011-04-26 20:42:42 +00001403 return false;
1404 } while (++begin != end);
1405
1406 return true;
1407}
1408
Bruno Riccid7628d92019-01-09 15:43:19 +00001409UnresolvedMemberExpr::UnresolvedMemberExpr(
1410 const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
1411 QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
1412 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1413 const DeclarationNameInfo &MemberNameInfo,
1414 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1415 UnresolvedSetIterator End)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001416 : OverloadExpr(
Bruno Riccid7628d92019-01-09 15:43:19 +00001417 UnresolvedMemberExprClass, Context, QualifierLoc, TemplateKWLoc,
Eugene Zelenko821f6982017-11-18 01:47:41 +00001418 MemberNameInfo, TemplateArgs, Begin, End,
1419 // Dependent
1420 ((Base && Base->isTypeDependent()) || BaseType->isDependentType()),
1421 ((Base && Base->isInstantiationDependent()) ||
1422 BaseType->isInstantiationDependentType()),
1423 // Contains unexpanded parameter pack
1424 ((Base && Base->containsUnexpandedParameterPack()) ||
1425 BaseType->containsUnexpandedParameterPack())),
Bruno Riccid7628d92019-01-09 15:43:19 +00001426 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
1427 UnresolvedMemberExprBits.IsArrow = IsArrow;
1428 UnresolvedMemberExprBits.HasUnresolvedUsing = HasUnresolvedUsing;
1429
John McCall0009fcc2011-04-26 20:42:42 +00001430 // Check whether all of the members are non-static member functions,
1431 // and if so, mark give this bound-member type instead of overload type.
1432 if (hasOnlyNonStaticMemberFunctions(Begin, End))
Bruno Riccid7628d92019-01-09 15:43:19 +00001433 setType(Context.BoundMemberTy);
John McCall10eae182009-11-30 22:42:35 +00001434}
1435
Bruno Riccid7628d92019-01-09 15:43:19 +00001436UnresolvedMemberExpr::UnresolvedMemberExpr(EmptyShell Empty,
1437 unsigned NumResults,
1438 bool HasTemplateKWAndArgsInfo)
1439 : OverloadExpr(UnresolvedMemberExprClass, Empty, NumResults,
1440 HasTemplateKWAndArgsInfo) {}
1441
Douglas Gregor0da1d432011-02-28 20:01:57 +00001442bool UnresolvedMemberExpr::isImplicitAccess() const {
Craig Topper36250ad2014-05-12 05:36:57 +00001443 if (!Base)
Douglas Gregor0da1d432011-02-28 20:01:57 +00001444 return true;
Fangrui Song6907ce22018-07-30 19:24:48 +00001445
Douglas Gregor25b7e052011-03-02 21:06:53 +00001446 return cast<Expr>(Base)->isImplicitCXXThis();
Douglas Gregor0da1d432011-02-28 20:01:57 +00001447}
1448
James Y Knighte7d82282015-12-29 18:15:14 +00001449UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
Bruno Riccid7628d92019-01-09 15:43:19 +00001450 const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
1451 QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
James Y Knighte7d82282015-12-29 18:15:14 +00001452 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1453 const DeclarationNameInfo &MemberNameInfo,
1454 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1455 UnresolvedSetIterator End) {
Bruno Riccid7628d92019-01-09 15:43:19 +00001456 unsigned NumResults = End - Begin;
James Y Knighte7d82282015-12-29 18:15:14 +00001457 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
Bruno Riccid7628d92019-01-09 15:43:19 +00001458 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1459 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
1460 TemplateArgumentLoc>(
1461 NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
1462 void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
James Y Knighte7d82282015-12-29 18:15:14 +00001463 return new (Mem) UnresolvedMemberExpr(
Bruno Riccid7628d92019-01-09 15:43:19 +00001464 Context, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc,
1465 QualifierLoc, TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
John McCall10eae182009-11-30 22:42:35 +00001466}
1467
Bruno Riccid7628d92019-01-09 15:43:19 +00001468UnresolvedMemberExpr *UnresolvedMemberExpr::CreateEmpty(
1469 const ASTContext &Context, unsigned NumResults,
1470 bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
James Y Knighte7d82282015-12-29 18:15:14 +00001471 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
Bruno Riccid7628d92019-01-09 15:43:19 +00001472 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
1473 TemplateArgumentLoc>(
1474 NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
1475 void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
1476 return new (Mem)
1477 UnresolvedMemberExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
Argyrios Kyrtzidisb8d3c632010-06-25 09:03:26 +00001478}
1479
Bruno Riccid7628d92019-01-09 15:43:19 +00001480CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() {
John McCall58cc69d2010-01-27 01:50:18 +00001481 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1482
1483 // If there was a nested name specifier, it names the naming class.
1484 // It can't be dependent: after all, we were actually able to do the
1485 // lookup.
Craig Topper36250ad2014-05-12 05:36:57 +00001486 CXXRecordDecl *Record = nullptr;
Nikola Smiljanic67860242014-09-26 00:28:20 +00001487 auto *NNS = getQualifier();
1488 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
John McCall424cec92011-01-19 06:33:43 +00001489 const Type *T = getQualifier()->getAsType();
John McCall58cc69d2010-01-27 01:50:18 +00001490 assert(T && "qualifier in member expression does not name type");
Douglas Gregor9262f472010-04-27 18:19:34 +00001491 Record = T->getAsCXXRecordDecl();
1492 assert(Record && "qualifier in member expression does not name record");
1493 }
John McCall58cc69d2010-01-27 01:50:18 +00001494 // Otherwise the naming class must have been the base class.
Douglas Gregor9262f472010-04-27 18:19:34 +00001495 else {
John McCall58cc69d2010-01-27 01:50:18 +00001496 QualType BaseType = getBaseType().getNonReferenceType();
1497 if (isArrow()) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001498 const auto *PT = BaseType->getAs<PointerType>();
John McCall58cc69d2010-01-27 01:50:18 +00001499 assert(PT && "base of arrow member access is not pointer");
1500 BaseType = PT->getPointeeType();
1501 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001502
Douglas Gregor9262f472010-04-27 18:19:34 +00001503 Record = BaseType->getAsCXXRecordDecl();
1504 assert(Record && "base of member expression does not name record");
John McCall58cc69d2010-01-27 01:50:18 +00001505 }
Fangrui Song6907ce22018-07-30 19:24:48 +00001506
Douglas Gregor9262f472010-04-27 18:19:34 +00001507 return Record;
John McCall58cc69d2010-01-27 01:50:18 +00001508}
1509
Richard Smithd784e682015-09-23 21:41:42 +00001510SizeOfPackExpr *
1511SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1512 NamedDecl *Pack, SourceLocation PackLoc,
1513 SourceLocation RParenLoc,
1514 Optional<unsigned> Length,
1515 ArrayRef<TemplateArgument> PartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001516 void *Storage =
1517 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
Richard Smithd784e682015-09-23 21:41:42 +00001518 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1519 PackLoc, RParenLoc, Length, PartialArgs);
1520}
1521
1522SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1523 unsigned NumPartialArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001524 void *Storage =
1525 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
Richard Smithd784e682015-09-23 21:41:42 +00001526 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1527}
1528
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001529SubstNonTypeTemplateParmPackExpr::
Fangrui Song6907ce22018-07-30 19:24:48 +00001530SubstNonTypeTemplateParmPackExpr(QualType T,
Richard Smithf1f20e62018-02-14 02:07:53 +00001531 ExprValueKind ValueKind,
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001532 NonTypeTemplateParmDecl *Param,
1533 SourceLocation NameLoc,
1534 const TemplateArgument &ArgPack)
Richard Smithf1f20e62018-02-14 02:07:53 +00001535 : Expr(SubstNonTypeTemplateParmPackExprClass, T, ValueKind, OK_Ordinary,
Eugene Zelenko821f6982017-11-18 01:47:41 +00001536 true, true, true, true),
1537 Param(Param), Arguments(ArgPack.pack_begin()),
1538 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) {}
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001539
1540TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
Benjamin Kramercce63472015-08-05 09:40:22 +00001541 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
Douglas Gregorcdbc5392011-01-15 01:15:58 +00001542}
1543
Richard Smithb2997f52019-05-21 20:10:50 +00001544FunctionParmPackExpr::FunctionParmPackExpr(QualType T, VarDecl *ParamPack,
Richard Smithb15fe3a2012-09-12 00:56:43 +00001545 SourceLocation NameLoc,
1546 unsigned NumParams,
Richard Smithb2997f52019-05-21 20:10:50 +00001547 VarDecl *const *Params)
James Y Knight48fefa32015-09-30 14:04:23 +00001548 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1549 true, true),
1550 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
Richard Smithb15fe3a2012-09-12 00:56:43 +00001551 if (Params)
1552 std::uninitialized_copy(Params, Params + NumParams,
Richard Smithb2997f52019-05-21 20:10:50 +00001553 getTrailingObjects<VarDecl *>());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001554}
1555
1556FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001557FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
Richard Smithb2997f52019-05-21 20:10:50 +00001558 VarDecl *ParamPack, SourceLocation NameLoc,
1559 ArrayRef<VarDecl *> Params) {
1560 return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(Params.size())))
James Y Knighte00a67e2015-12-31 04:18:25 +00001561 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
Richard Smithb15fe3a2012-09-12 00:56:43 +00001562}
1563
1564FunctionParmPackExpr *
Craig Toppera31a8822013-08-22 07:09:37 +00001565FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1566 unsigned NumParams) {
Richard Smithb2997f52019-05-21 20:10:50 +00001567 return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(NumParams)))
James Y Knighte00a67e2015-12-31 04:18:25 +00001568 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
Richard Smithb15fe3a2012-09-12 00:56:43 +00001569}
1570
David Majnemerdaff3702014-05-01 17:50:17 +00001571void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1572 unsigned ManglingNumber) {
1573 // We only need extra state if we have to remember more than just the Stmt.
1574 if (!ExtendedBy)
1575 return;
1576
1577 // We may need to allocate extra storage for the mangling number and the
1578 // extended-by ValueDecl.
1579 if (!State.is<ExtraState *>()) {
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001580 auto *ES = new (ExtendedBy->getASTContext()) ExtraState;
David Majnemerdaff3702014-05-01 17:50:17 +00001581 ES->Temporary = State.get<Stmt *>();
1582 State = ES;
1583 }
1584
1585 auto ES = State.get<ExtraState *>();
1586 ES->ExtendingDecl = ExtendedBy;
1587 ES->ManglingNumber = ManglingNumber;
1588}
1589
Douglas Gregor29c42f22012-02-24 07:38:34 +00001590TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1591 ArrayRef<TypeSourceInfo *> Args,
1592 SourceLocation RParenLoc,
1593 bool Value)
Eugene Zelenko821f6982017-11-18 01:47:41 +00001594 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1595 /*TypeDependent=*/false,
1596 /*ValueDependent=*/false,
1597 /*InstantiationDependent=*/false,
1598 /*ContainsUnexpandedParameterPack=*/false),
1599 Loc(Loc), RParenLoc(RParenLoc) {
Douglas Gregor29c42f22012-02-24 07:38:34 +00001600 TypeTraitExprBits.Kind = Kind;
1601 TypeTraitExprBits.Value = Value;
1602 TypeTraitExprBits.NumArgs = Args.size();
1603
Eugene Zelenkobc5858b2018-04-10 22:54:42 +00001604 auto **ToArgs = getTrailingObjects<TypeSourceInfo *>();
James Y Knighte00a67e2015-12-31 04:18:25 +00001605
Douglas Gregor29c42f22012-02-24 07:38:34 +00001606 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1607 if (Args[I]->getType()->isDependentType())
1608 setValueDependent(true);
1609 if (Args[I]->getType()->isInstantiationDependentType())
1610 setInstantiationDependent(true);
1611 if (Args[I]->getType()->containsUnexpandedParameterPack())
1612 setContainsUnexpandedParameterPack(true);
Fangrui Song6907ce22018-07-30 19:24:48 +00001613
Douglas Gregor29c42f22012-02-24 07:38:34 +00001614 ToArgs[I] = Args[I];
1615 }
1616}
1617
Craig Toppera31a8822013-08-22 07:09:37 +00001618TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
Fangrui Song6907ce22018-07-30 19:24:48 +00001619 SourceLocation Loc,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001620 TypeTrait Kind,
1621 ArrayRef<TypeSourceInfo *> Args,
1622 SourceLocation RParenLoc,
1623 bool Value) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001624 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001625 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1626}
1627
Craig Toppera31a8822013-08-22 07:09:37 +00001628TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
Douglas Gregor29c42f22012-02-24 07:38:34 +00001629 unsigned NumArgs) {
James Y Knighte00a67e2015-12-31 04:18:25 +00001630 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
Douglas Gregor29c42f22012-02-24 07:38:34 +00001631 return new (Mem) TypeTraitExpr(EmptyShell());
1632}
Bruno Riccic5885cf2018-12-21 15:20:32 +00001633
1634CUDAKernelCallExpr::CUDAKernelCallExpr(Expr *Fn, CallExpr *Config,
1635 ArrayRef<Expr *> Args, QualType Ty,
1636 ExprValueKind VK, SourceLocation RP,
1637 unsigned MinNumArgs)
1638 : CallExpr(CUDAKernelCallExprClass, Fn, /*PreArgs=*/Config, Args, Ty, VK,
1639 RP, MinNumArgs, NotADL) {}
1640
1641CUDAKernelCallExpr::CUDAKernelCallExpr(unsigned NumArgs, EmptyShell Empty)
1642 : CallExpr(CUDAKernelCallExprClass, /*NumPreArgs=*/END_PREARG, NumArgs,
1643 Empty) {}
1644
1645CUDAKernelCallExpr *
1646CUDAKernelCallExpr::Create(const ASTContext &Ctx, Expr *Fn, CallExpr *Config,
1647 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
1648 SourceLocation RP, unsigned MinNumArgs) {
1649 // Allocate storage for the trailing objects of CallExpr.
1650 unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
1651 unsigned SizeOfTrailingObjects =
1652 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/END_PREARG, NumArgs);
1653 void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
1654 alignof(CUDAKernelCallExpr));
1655 return new (Mem) CUDAKernelCallExpr(Fn, Config, Args, Ty, VK, RP, MinNumArgs);
1656}
1657
1658CUDAKernelCallExpr *CUDAKernelCallExpr::CreateEmpty(const ASTContext &Ctx,
1659 unsigned NumArgs,
1660 EmptyShell Empty) {
1661 // Allocate storage for the trailing objects of CallExpr.
1662 unsigned SizeOfTrailingObjects =
1663 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/END_PREARG, NumArgs);
1664 void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
1665 alignof(CUDAKernelCallExpr));
1666 return new (Mem) CUDAKernelCallExpr(NumArgs, Empty);
1667}