1 //===- ExprCXX.cpp - (C++) Expression AST Node Implementation -------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the subclesses of Expr class declared in ExprCXX.h
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "clang/AST/ExprCXX.h"
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/ComputeDependence.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/DeclAccessPair.h"
19 #include "clang/AST/DeclBase.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/DeclarationName.h"
23 #include "clang/AST/DependenceFlags.h"
24 #include "clang/AST/Expr.h"
25 #include "clang/AST/LambdaCapture.h"
26 #include "clang/AST/NestedNameSpecifier.h"
27 #include "clang/AST/TemplateBase.h"
28 #include "clang/AST/Type.h"
29 #include "clang/AST/TypeLoc.h"
30 #include "clang/Basic/LLVM.h"
31 #include "clang/Basic/OperatorKinds.h"
32 #include "clang/Basic/SourceLocation.h"
33 #include "clang/Basic/Specifiers.h"
34 #include "llvm/ADT/ArrayRef.h"
35 #include "llvm/Support/Casting.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include <cassert>
38 #include <cstddef>
39 #include <cstring>
40 #include <memory>
41
42 using namespace clang;
43
44 //===----------------------------------------------------------------------===//
45 // Child Iterators for iterating over subexpressions/substatements
46 //===----------------------------------------------------------------------===//
47
isInfixBinaryOp() const48 bool CXXOperatorCallExpr::isInfixBinaryOp() const {
49 // An infix binary operator is any operator with two arguments other than
50 // operator() and operator[]. Note that none of these operators can have
51 // default arguments, so it suffices to check the number of argument
52 // expressions.
53 if (getNumArgs() != 2)
54 return false;
55
56 switch (getOperator()) {
57 case OO_Call: case OO_Subscript:
58 return false;
59 default:
60 return true;
61 }
62 }
63
64 CXXRewrittenBinaryOperator::DecomposedForm
getDecomposedForm() const65 CXXRewrittenBinaryOperator::getDecomposedForm() const {
66 DecomposedForm Result = {};
67 const Expr *E = getSemanticForm()->IgnoreImplicit();
68
69 // Remove an outer '!' if it exists (only happens for a '!=' rewrite).
70 bool SkippedNot = false;
71 if (auto *NotEq = dyn_cast<UnaryOperator>(E)) {
72 assert(NotEq->getOpcode() == UO_LNot);
73 E = NotEq->getSubExpr()->IgnoreImplicit();
74 SkippedNot = true;
75 }
76
77 // Decompose the outer binary operator.
78 if (auto *BO = dyn_cast<BinaryOperator>(E)) {
79 assert(!SkippedNot || BO->getOpcode() == BO_EQ);
80 Result.Opcode = SkippedNot ? BO_NE : BO->getOpcode();
81 Result.LHS = BO->getLHS();
82 Result.RHS = BO->getRHS();
83 Result.InnerBinOp = BO;
84 } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) {
85 assert(!SkippedNot || BO->getOperator() == OO_EqualEqual);
86 assert(BO->isInfixBinaryOp());
87 switch (BO->getOperator()) {
88 case OO_Less: Result.Opcode = BO_LT; break;
89 case OO_LessEqual: Result.Opcode = BO_LE; break;
90 case OO_Greater: Result.Opcode = BO_GT; break;
91 case OO_GreaterEqual: Result.Opcode = BO_GE; break;
92 case OO_Spaceship: Result.Opcode = BO_Cmp; break;
93 case OO_EqualEqual: Result.Opcode = SkippedNot ? BO_NE : BO_EQ; break;
94 default: llvm_unreachable("unexpected binop in rewritten operator expr");
95 }
96 Result.LHS = BO->getArg(0);
97 Result.RHS = BO->getArg(1);
98 Result.InnerBinOp = BO;
99 } else {
100 llvm_unreachable("unexpected rewritten operator form");
101 }
102
103 // Put the operands in the right order for == and !=, and canonicalize the
104 // <=> subexpression onto the LHS for all other forms.
105 if (isReversed())
106 std::swap(Result.LHS, Result.RHS);
107
108 // If this isn't a spaceship rewrite, we're done.
109 if (Result.Opcode == BO_EQ || Result.Opcode == BO_NE)
110 return Result;
111
112 // Otherwise, we expect a <=> to now be on the LHS.
113 E = Result.LHS->IgnoreImplicitAsWritten();
114 if (auto *BO = dyn_cast<BinaryOperator>(E)) {
115 assert(BO->getOpcode() == BO_Cmp);
116 Result.LHS = BO->getLHS();
117 Result.RHS = BO->getRHS();
118 Result.InnerBinOp = BO;
119 } else if (auto *BO = dyn_cast<CXXOperatorCallExpr>(E)) {
120 assert(BO->getOperator() == OO_Spaceship);
121 Result.LHS = BO->getArg(0);
122 Result.RHS = BO->getArg(1);
123 Result.InnerBinOp = BO;
124 } else {
125 llvm_unreachable("unexpected rewritten operator form");
126 }
127
128 // Put the comparison operands in the right order.
129 if (isReversed())
130 std::swap(Result.LHS, Result.RHS);
131 return Result;
132 }
133
isPotentiallyEvaluated() const134 bool CXXTypeidExpr::isPotentiallyEvaluated() const {
135 if (isTypeOperand())
136 return false;
137
138 // C++11 [expr.typeid]p3:
139 // When typeid is applied to an expression other than a glvalue of
140 // polymorphic class type, [...] the expression is an unevaluated operand.
141 const Expr *E = getExprOperand();
142 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
143 if (RD->isPolymorphic() && E->isGLValue())
144 return true;
145
146 return false;
147 }
148
getTypeOperand(ASTContext & Context) const149 QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
150 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
151 Qualifiers Quals;
152 return Context.getUnqualifiedArrayType(
153 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
154 }
155
getTypeOperand(ASTContext & Context) const156 QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
157 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
158 Qualifiers Quals;
159 return Context.getUnqualifiedArrayType(
160 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
161 }
162
163 // CXXScalarValueInitExpr
getBeginLoc() const164 SourceLocation CXXScalarValueInitExpr::getBeginLoc() const {
165 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : getRParenLoc();
166 }
167
168 // CXXNewExpr
CXXNewExpr(bool IsGlobalNew,FunctionDecl * OperatorNew,FunctionDecl * OperatorDelete,bool ShouldPassAlignment,bool UsualArrayDeleteWantsSize,ArrayRef<Expr * > PlacementArgs,SourceRange TypeIdParens,Optional<Expr * > ArraySize,InitializationStyle InitializationStyle,Expr * Initializer,QualType Ty,TypeSourceInfo * AllocatedTypeInfo,SourceRange Range,SourceRange DirectInitRange)169 CXXNewExpr::CXXNewExpr(bool IsGlobalNew, FunctionDecl *OperatorNew,
170 FunctionDecl *OperatorDelete, bool ShouldPassAlignment,
171 bool UsualArrayDeleteWantsSize,
172 ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
173 Optional<Expr *> ArraySize,
174 InitializationStyle InitializationStyle,
175 Expr *Initializer, QualType Ty,
176 TypeSourceInfo *AllocatedTypeInfo, SourceRange Range,
177 SourceRange DirectInitRange)
178 : Expr(CXXNewExprClass, Ty, VK_RValue, OK_Ordinary),
179 OperatorNew(OperatorNew), OperatorDelete(OperatorDelete),
180 AllocatedTypeInfo(AllocatedTypeInfo), Range(Range),
181 DirectInitRange(DirectInitRange) {
182
183 assert((Initializer != nullptr || InitializationStyle == NoInit) &&
184 "Only NoInit can have no initializer!");
185
186 CXXNewExprBits.IsGlobalNew = IsGlobalNew;
187 CXXNewExprBits.IsArray = ArraySize.hasValue();
188 CXXNewExprBits.ShouldPassAlignment = ShouldPassAlignment;
189 CXXNewExprBits.UsualArrayDeleteWantsSize = UsualArrayDeleteWantsSize;
190 CXXNewExprBits.StoredInitializationStyle =
191 Initializer ? InitializationStyle + 1 : 0;
192 bool IsParenTypeId = TypeIdParens.isValid();
193 CXXNewExprBits.IsParenTypeId = IsParenTypeId;
194 CXXNewExprBits.NumPlacementArgs = PlacementArgs.size();
195
196 if (ArraySize)
197 getTrailingObjects<Stmt *>()[arraySizeOffset()] = *ArraySize;
198 if (Initializer)
199 getTrailingObjects<Stmt *>()[initExprOffset()] = Initializer;
200 for (unsigned I = 0; I != PlacementArgs.size(); ++I)
201 getTrailingObjects<Stmt *>()[placementNewArgsOffset() + I] =
202 PlacementArgs[I];
203 if (IsParenTypeId)
204 getTrailingObjects<SourceRange>()[0] = TypeIdParens;
205
206 switch (getInitializationStyle()) {
207 case CallInit:
208 this->Range.setEnd(DirectInitRange.getEnd());
209 break;
210 case ListInit:
211 this->Range.setEnd(getInitializer()->getSourceRange().getEnd());
212 break;
213 default:
214 if (IsParenTypeId)
215 this->Range.setEnd(TypeIdParens.getEnd());
216 break;
217 }
218
219 setDependence(computeDependence(this));
220 }
221
CXXNewExpr(EmptyShell Empty,bool IsArray,unsigned NumPlacementArgs,bool IsParenTypeId)222 CXXNewExpr::CXXNewExpr(EmptyShell Empty, bool IsArray,
223 unsigned NumPlacementArgs, bool IsParenTypeId)
224 : Expr(CXXNewExprClass, Empty) {
225 CXXNewExprBits.IsArray = IsArray;
226 CXXNewExprBits.NumPlacementArgs = NumPlacementArgs;
227 CXXNewExprBits.IsParenTypeId = IsParenTypeId;
228 }
229
230 CXXNewExpr *
Create(const ASTContext & Ctx,bool IsGlobalNew,FunctionDecl * OperatorNew,FunctionDecl * OperatorDelete,bool ShouldPassAlignment,bool UsualArrayDeleteWantsSize,ArrayRef<Expr * > PlacementArgs,SourceRange TypeIdParens,Optional<Expr * > ArraySize,InitializationStyle InitializationStyle,Expr * Initializer,QualType Ty,TypeSourceInfo * AllocatedTypeInfo,SourceRange Range,SourceRange DirectInitRange)231 CXXNewExpr::Create(const ASTContext &Ctx, bool IsGlobalNew,
232 FunctionDecl *OperatorNew, FunctionDecl *OperatorDelete,
233 bool ShouldPassAlignment, bool UsualArrayDeleteWantsSize,
234 ArrayRef<Expr *> PlacementArgs, SourceRange TypeIdParens,
235 Optional<Expr *> ArraySize,
236 InitializationStyle InitializationStyle, Expr *Initializer,
237 QualType Ty, TypeSourceInfo *AllocatedTypeInfo,
238 SourceRange Range, SourceRange DirectInitRange) {
239 bool IsArray = ArraySize.hasValue();
240 bool HasInit = Initializer != nullptr;
241 unsigned NumPlacementArgs = PlacementArgs.size();
242 bool IsParenTypeId = TypeIdParens.isValid();
243 void *Mem =
244 Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
245 IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
246 alignof(CXXNewExpr));
247 return new (Mem)
248 CXXNewExpr(IsGlobalNew, OperatorNew, OperatorDelete, ShouldPassAlignment,
249 UsualArrayDeleteWantsSize, PlacementArgs, TypeIdParens,
250 ArraySize, InitializationStyle, Initializer, Ty,
251 AllocatedTypeInfo, Range, DirectInitRange);
252 }
253
CreateEmpty(const ASTContext & Ctx,bool IsArray,bool HasInit,unsigned NumPlacementArgs,bool IsParenTypeId)254 CXXNewExpr *CXXNewExpr::CreateEmpty(const ASTContext &Ctx, bool IsArray,
255 bool HasInit, unsigned NumPlacementArgs,
256 bool IsParenTypeId) {
257 void *Mem =
258 Ctx.Allocate(totalSizeToAlloc<Stmt *, SourceRange>(
259 IsArray + HasInit + NumPlacementArgs, IsParenTypeId),
260 alignof(CXXNewExpr));
261 return new (Mem)
262 CXXNewExpr(EmptyShell(), IsArray, NumPlacementArgs, IsParenTypeId);
263 }
264
shouldNullCheckAllocation() const265 bool CXXNewExpr::shouldNullCheckAllocation() const {
266 return getOperatorNew()
267 ->getType()
268 ->castAs<FunctionProtoType>()
269 ->isNothrow() &&
270 !getOperatorNew()->isReservedGlobalPlacementOperator();
271 }
272
273 // CXXDeleteExpr
getDestroyedType() const274 QualType CXXDeleteExpr::getDestroyedType() const {
275 const Expr *Arg = getArgument();
276
277 // For a destroying operator delete, we may have implicitly converted the
278 // pointer type to the type of the parameter of the 'operator delete'
279 // function.
280 while (const auto *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
281 if (ICE->getCastKind() == CK_DerivedToBase ||
282 ICE->getCastKind() == CK_UncheckedDerivedToBase ||
283 ICE->getCastKind() == CK_NoOp) {
284 assert((ICE->getCastKind() == CK_NoOp ||
285 getOperatorDelete()->isDestroyingOperatorDelete()) &&
286 "only a destroying operator delete can have a converted arg");
287 Arg = ICE->getSubExpr();
288 } else
289 break;
290 }
291
292 // The type-to-delete may not be a pointer if it's a dependent type.
293 const QualType ArgType = Arg->getType();
294
295 if (ArgType->isDependentType() && !ArgType->isPointerType())
296 return QualType();
297
298 return ArgType->castAs<PointerType>()->getPointeeType();
299 }
300
301 // CXXPseudoDestructorExpr
PseudoDestructorTypeStorage(TypeSourceInfo * Info)302 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
303 : Type(Info) {
304 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
305 }
306
CXXPseudoDestructorExpr(const ASTContext & Context,Expr * Base,bool isArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,TypeSourceInfo * ScopeType,SourceLocation ColonColonLoc,SourceLocation TildeLoc,PseudoDestructorTypeStorage DestroyedType)307 CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(
308 const ASTContext &Context, Expr *Base, bool isArrow,
309 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
310 TypeSourceInfo *ScopeType, SourceLocation ColonColonLoc,
311 SourceLocation TildeLoc, PseudoDestructorTypeStorage DestroyedType)
312 : Expr(CXXPseudoDestructorExprClass, Context.BoundMemberTy, VK_RValue,
313 OK_Ordinary),
314 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
315 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
316 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
317 DestroyedType(DestroyedType) {
318 setDependence(computeDependence(this));
319 }
320
getDestroyedType() const321 QualType CXXPseudoDestructorExpr::getDestroyedType() const {
322 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
323 return TInfo->getType();
324
325 return QualType();
326 }
327
getEndLoc() const328 SourceLocation CXXPseudoDestructorExpr::getEndLoc() const {
329 SourceLocation End = DestroyedType.getLocation();
330 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
331 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
332 return End;
333 }
334
335 // UnresolvedLookupExpr
UnresolvedLookupExpr(const ASTContext & Context,CXXRecordDecl * NamingClass,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,bool RequiresADL,bool Overloaded,const TemplateArgumentListInfo * TemplateArgs,UnresolvedSetIterator Begin,UnresolvedSetIterator End)336 UnresolvedLookupExpr::UnresolvedLookupExpr(
337 const ASTContext &Context, CXXRecordDecl *NamingClass,
338 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
339 const DeclarationNameInfo &NameInfo, bool RequiresADL, bool Overloaded,
340 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
341 UnresolvedSetIterator End)
342 : OverloadExpr(UnresolvedLookupExprClass, Context, QualifierLoc,
343 TemplateKWLoc, NameInfo, TemplateArgs, Begin, End, false,
344 false, false),
345 NamingClass(NamingClass) {
346 UnresolvedLookupExprBits.RequiresADL = RequiresADL;
347 UnresolvedLookupExprBits.Overloaded = Overloaded;
348 }
349
UnresolvedLookupExpr(EmptyShell Empty,unsigned NumResults,bool HasTemplateKWAndArgsInfo)350 UnresolvedLookupExpr::UnresolvedLookupExpr(EmptyShell Empty,
351 unsigned NumResults,
352 bool HasTemplateKWAndArgsInfo)
353 : OverloadExpr(UnresolvedLookupExprClass, Empty, NumResults,
354 HasTemplateKWAndArgsInfo) {}
355
Create(const ASTContext & Context,CXXRecordDecl * NamingClass,NestedNameSpecifierLoc QualifierLoc,const DeclarationNameInfo & NameInfo,bool RequiresADL,bool Overloaded,UnresolvedSetIterator Begin,UnresolvedSetIterator End)356 UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
357 const ASTContext &Context, CXXRecordDecl *NamingClass,
358 NestedNameSpecifierLoc QualifierLoc, const DeclarationNameInfo &NameInfo,
359 bool RequiresADL, bool Overloaded, UnresolvedSetIterator Begin,
360 UnresolvedSetIterator End) {
361 unsigned NumResults = End - Begin;
362 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
363 TemplateArgumentLoc>(NumResults, 0, 0);
364 void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
365 return new (Mem) UnresolvedLookupExpr(Context, NamingClass, QualifierLoc,
366 SourceLocation(), NameInfo, RequiresADL,
367 Overloaded, nullptr, Begin, End);
368 }
369
Create(const ASTContext & Context,CXXRecordDecl * NamingClass,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,bool RequiresADL,const TemplateArgumentListInfo * Args,UnresolvedSetIterator Begin,UnresolvedSetIterator End)370 UnresolvedLookupExpr *UnresolvedLookupExpr::Create(
371 const ASTContext &Context, CXXRecordDecl *NamingClass,
372 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
373 const DeclarationNameInfo &NameInfo, bool RequiresADL,
374 const TemplateArgumentListInfo *Args, UnresolvedSetIterator Begin,
375 UnresolvedSetIterator End) {
376 assert(Args || TemplateKWLoc.isValid());
377 unsigned NumResults = End - Begin;
378 unsigned NumTemplateArgs = Args ? Args->size() : 0;
379 unsigned Size =
380 totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
381 TemplateArgumentLoc>(NumResults, 1, NumTemplateArgs);
382 void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
383 return new (Mem) UnresolvedLookupExpr(Context, NamingClass, QualifierLoc,
384 TemplateKWLoc, NameInfo, RequiresADL,
385 /*Overloaded*/ true, Args, Begin, End);
386 }
387
CreateEmpty(const ASTContext & Context,unsigned NumResults,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs)388 UnresolvedLookupExpr *UnresolvedLookupExpr::CreateEmpty(
389 const ASTContext &Context, unsigned NumResults,
390 bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
391 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
392 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
393 TemplateArgumentLoc>(
394 NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
395 void *Mem = Context.Allocate(Size, alignof(UnresolvedLookupExpr));
396 return new (Mem)
397 UnresolvedLookupExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
398 }
399
OverloadExpr(StmtClass SC,const ASTContext & Context,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * TemplateArgs,UnresolvedSetIterator Begin,UnresolvedSetIterator End,bool KnownDependent,bool KnownInstantiationDependent,bool KnownContainsUnexpandedParameterPack)400 OverloadExpr::OverloadExpr(StmtClass SC, const ASTContext &Context,
401 NestedNameSpecifierLoc QualifierLoc,
402 SourceLocation TemplateKWLoc,
403 const DeclarationNameInfo &NameInfo,
404 const TemplateArgumentListInfo *TemplateArgs,
405 UnresolvedSetIterator Begin,
406 UnresolvedSetIterator End, bool KnownDependent,
407 bool KnownInstantiationDependent,
408 bool KnownContainsUnexpandedParameterPack)
409 : Expr(SC, Context.OverloadTy, VK_LValue, OK_Ordinary), NameInfo(NameInfo),
410 QualifierLoc(QualifierLoc) {
411 unsigned NumResults = End - Begin;
412 OverloadExprBits.NumResults = NumResults;
413 OverloadExprBits.HasTemplateKWAndArgsInfo =
414 (TemplateArgs != nullptr ) || TemplateKWLoc.isValid();
415
416 if (NumResults) {
417 // Copy the results to the trailing array past UnresolvedLookupExpr
418 // or UnresolvedMemberExpr.
419 DeclAccessPair *Results = getTrailingResults();
420 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
421 }
422
423 if (TemplateArgs) {
424 auto Deps = TemplateArgumentDependence::None;
425 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
426 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(), Deps);
427 } else if (TemplateKWLoc.isValid()) {
428 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
429 }
430
431 setDependence(computeDependence(this, KnownDependent,
432 KnownInstantiationDependent,
433 KnownContainsUnexpandedParameterPack));
434 if (isTypeDependent())
435 setType(Context.DependentTy);
436 }
437
OverloadExpr(StmtClass SC,EmptyShell Empty,unsigned NumResults,bool HasTemplateKWAndArgsInfo)438 OverloadExpr::OverloadExpr(StmtClass SC, EmptyShell Empty, unsigned NumResults,
439 bool HasTemplateKWAndArgsInfo)
440 : Expr(SC, Empty) {
441 OverloadExprBits.NumResults = NumResults;
442 OverloadExprBits.HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
443 }
444
445 // DependentScopeDeclRefExpr
DependentScopeDeclRefExpr(QualType Ty,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * Args)446 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(
447 QualType Ty, NestedNameSpecifierLoc QualifierLoc,
448 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
449 const TemplateArgumentListInfo *Args)
450 : Expr(DependentScopeDeclRefExprClass, Ty, VK_LValue, OK_Ordinary),
451 QualifierLoc(QualifierLoc), NameInfo(NameInfo) {
452 DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
453 (Args != nullptr) || TemplateKWLoc.isValid();
454 if (Args) {
455 auto Deps = TemplateArgumentDependence::None;
456 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
457 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(), Deps);
458 } else if (TemplateKWLoc.isValid()) {
459 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
460 TemplateKWLoc);
461 }
462 setDependence(computeDependence(this));
463 }
464
Create(const ASTContext & Context,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & NameInfo,const TemplateArgumentListInfo * Args)465 DependentScopeDeclRefExpr *DependentScopeDeclRefExpr::Create(
466 const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc,
467 SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo,
468 const TemplateArgumentListInfo *Args) {
469 assert(QualifierLoc && "should be created for dependent qualifiers");
470 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
471 std::size_t Size =
472 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
473 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
474 void *Mem = Context.Allocate(Size);
475 return new (Mem) DependentScopeDeclRefExpr(Context.DependentTy, QualifierLoc,
476 TemplateKWLoc, NameInfo, Args);
477 }
478
479 DependentScopeDeclRefExpr *
CreateEmpty(const ASTContext & Context,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs)480 DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &Context,
481 bool HasTemplateKWAndArgsInfo,
482 unsigned NumTemplateArgs) {
483 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
484 std::size_t Size =
485 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
486 HasTemplateKWAndArgsInfo, NumTemplateArgs);
487 void *Mem = Context.Allocate(Size);
488 auto *E = new (Mem) DependentScopeDeclRefExpr(
489 QualType(), NestedNameSpecifierLoc(), SourceLocation(),
490 DeclarationNameInfo(), nullptr);
491 E->DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo =
492 HasTemplateKWAndArgsInfo;
493 return E;
494 }
495
getBeginLoc() const496 SourceLocation CXXConstructExpr::getBeginLoc() const {
497 if (isa<CXXTemporaryObjectExpr>(this))
498 return cast<CXXTemporaryObjectExpr>(this)->getBeginLoc();
499 return getLocation();
500 }
501
getEndLoc() const502 SourceLocation CXXConstructExpr::getEndLoc() const {
503 if (isa<CXXTemporaryObjectExpr>(this))
504 return cast<CXXTemporaryObjectExpr>(this)->getEndLoc();
505
506 if (ParenOrBraceRange.isValid())
507 return ParenOrBraceRange.getEnd();
508
509 SourceLocation End = getLocation();
510 for (unsigned I = getNumArgs(); I > 0; --I) {
511 const Expr *Arg = getArg(I-1);
512 if (!Arg->isDefaultArgument()) {
513 SourceLocation NewEnd = Arg->getEndLoc();
514 if (NewEnd.isValid()) {
515 End = NewEnd;
516 break;
517 }
518 }
519 }
520
521 return End;
522 }
523
CXXOperatorCallExpr(OverloadedOperatorKind OpKind,Expr * Fn,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation OperatorLoc,FPOptionsOverride FPFeatures,ADLCallKind UsesADL)524 CXXOperatorCallExpr::CXXOperatorCallExpr(OverloadedOperatorKind OpKind,
525 Expr *Fn, ArrayRef<Expr *> Args,
526 QualType Ty, ExprValueKind VK,
527 SourceLocation OperatorLoc,
528 FPOptionsOverride FPFeatures,
529 ADLCallKind UsesADL)
530 : CallExpr(CXXOperatorCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
531 OperatorLoc, /*MinNumArgs=*/0, UsesADL) {
532 CXXOperatorCallExprBits.OperatorKind = OpKind;
533 assert(
534 (CXXOperatorCallExprBits.OperatorKind == static_cast<unsigned>(OpKind)) &&
535 "OperatorKind overflow!");
536 Range = getSourceRangeImpl();
537 Overrides = FPFeatures;
538 }
539
CXXOperatorCallExpr(unsigned NumArgs,EmptyShell Empty)540 CXXOperatorCallExpr::CXXOperatorCallExpr(unsigned NumArgs, EmptyShell Empty)
541 : CallExpr(CXXOperatorCallExprClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
542
543 CXXOperatorCallExpr *
Create(const ASTContext & Ctx,OverloadedOperatorKind OpKind,Expr * Fn,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation OperatorLoc,FPOptionsOverride FPFeatures,ADLCallKind UsesADL)544 CXXOperatorCallExpr::Create(const ASTContext &Ctx,
545 OverloadedOperatorKind OpKind, Expr *Fn,
546 ArrayRef<Expr *> Args, QualType Ty,
547 ExprValueKind VK, SourceLocation OperatorLoc,
548 FPOptionsOverride FPFeatures, ADLCallKind UsesADL) {
549 // Allocate storage for the trailing objects of CallExpr.
550 unsigned NumArgs = Args.size();
551 unsigned SizeOfTrailingObjects =
552 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
553 void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
554 alignof(CXXOperatorCallExpr));
555 return new (Mem) CXXOperatorCallExpr(OpKind, Fn, Args, Ty, VK, OperatorLoc,
556 FPFeatures, UsesADL);
557 }
558
CreateEmpty(const ASTContext & Ctx,unsigned NumArgs,EmptyShell Empty)559 CXXOperatorCallExpr *CXXOperatorCallExpr::CreateEmpty(const ASTContext &Ctx,
560 unsigned NumArgs,
561 EmptyShell Empty) {
562 // Allocate storage for the trailing objects of CallExpr.
563 unsigned SizeOfTrailingObjects =
564 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
565 void *Mem = Ctx.Allocate(sizeof(CXXOperatorCallExpr) + SizeOfTrailingObjects,
566 alignof(CXXOperatorCallExpr));
567 return new (Mem) CXXOperatorCallExpr(NumArgs, Empty);
568 }
569
getSourceRangeImpl() const570 SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
571 OverloadedOperatorKind Kind = getOperator();
572 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
573 if (getNumArgs() == 1)
574 // Prefix operator
575 return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
576 else
577 // Postfix operator
578 return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
579 } else if (Kind == OO_Arrow) {
580 return SourceRange(getArg(0)->getBeginLoc(), getOperatorLoc());
581 } else if (Kind == OO_Call) {
582 return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
583 } else if (Kind == OO_Subscript) {
584 return SourceRange(getArg(0)->getBeginLoc(), getRParenLoc());
585 } else if (getNumArgs() == 1) {
586 return SourceRange(getOperatorLoc(), getArg(0)->getEndLoc());
587 } else if (getNumArgs() == 2) {
588 return SourceRange(getArg(0)->getBeginLoc(), getArg(1)->getEndLoc());
589 } else {
590 return getOperatorLoc();
591 }
592 }
593
CXXMemberCallExpr(Expr * Fn,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation RP,unsigned MinNumArgs)594 CXXMemberCallExpr::CXXMemberCallExpr(Expr *Fn, ArrayRef<Expr *> Args,
595 QualType Ty, ExprValueKind VK,
596 SourceLocation RP, unsigned MinNumArgs)
597 : CallExpr(CXXMemberCallExprClass, Fn, /*PreArgs=*/{}, Args, Ty, VK, RP,
598 MinNumArgs, NotADL) {}
599
CXXMemberCallExpr(unsigned NumArgs,EmptyShell Empty)600 CXXMemberCallExpr::CXXMemberCallExpr(unsigned NumArgs, EmptyShell Empty)
601 : CallExpr(CXXMemberCallExprClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
602
Create(const ASTContext & Ctx,Expr * Fn,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation RP,unsigned MinNumArgs)603 CXXMemberCallExpr *CXXMemberCallExpr::Create(const ASTContext &Ctx, Expr *Fn,
604 ArrayRef<Expr *> Args, QualType Ty,
605 ExprValueKind VK,
606 SourceLocation RP,
607 unsigned MinNumArgs) {
608 // Allocate storage for the trailing objects of CallExpr.
609 unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
610 unsigned SizeOfTrailingObjects =
611 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
612 void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
613 alignof(CXXMemberCallExpr));
614 return new (Mem) CXXMemberCallExpr(Fn, Args, Ty, VK, RP, MinNumArgs);
615 }
616
CreateEmpty(const ASTContext & Ctx,unsigned NumArgs,EmptyShell Empty)617 CXXMemberCallExpr *CXXMemberCallExpr::CreateEmpty(const ASTContext &Ctx,
618 unsigned NumArgs,
619 EmptyShell Empty) {
620 // Allocate storage for the trailing objects of CallExpr.
621 unsigned SizeOfTrailingObjects =
622 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
623 void *Mem = Ctx.Allocate(sizeof(CXXMemberCallExpr) + SizeOfTrailingObjects,
624 alignof(CXXMemberCallExpr));
625 return new (Mem) CXXMemberCallExpr(NumArgs, Empty);
626 }
627
getImplicitObjectArgument() const628 Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
629 const Expr *Callee = getCallee()->IgnoreParens();
630 if (const auto *MemExpr = dyn_cast<MemberExpr>(Callee))
631 return MemExpr->getBase();
632 if (const auto *BO = dyn_cast<BinaryOperator>(Callee))
633 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
634 return BO->getLHS();
635
636 // FIXME: Will eventually need to cope with member pointers.
637 return nullptr;
638 }
639
getObjectType() const640 QualType CXXMemberCallExpr::getObjectType() const {
641 QualType Ty = getImplicitObjectArgument()->getType();
642 if (Ty->isPointerType())
643 Ty = Ty->getPointeeType();
644 return Ty;
645 }
646
getMethodDecl() const647 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
648 if (const auto *MemExpr = dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
649 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
650
651 // FIXME: Will eventually need to cope with member pointers.
652 return nullptr;
653 }
654
getRecordDecl() const655 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
656 Expr* ThisArg = getImplicitObjectArgument();
657 if (!ThisArg)
658 return nullptr;
659
660 if (ThisArg->getType()->isAnyPointerType())
661 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
662
663 return ThisArg->getType()->getAsCXXRecordDecl();
664 }
665
666 //===----------------------------------------------------------------------===//
667 // Named casts
668 //===----------------------------------------------------------------------===//
669
670 /// getCastName - Get the name of the C++ cast being used, e.g.,
671 /// "static_cast", "dynamic_cast", "reinterpret_cast", or
672 /// "const_cast". The returned pointer must not be freed.
getCastName() const673 const char *CXXNamedCastExpr::getCastName() const {
674 switch (getStmtClass()) {
675 case CXXStaticCastExprClass: return "static_cast";
676 case CXXDynamicCastExprClass: return "dynamic_cast";
677 case CXXReinterpretCastExprClass: return "reinterpret_cast";
678 case CXXConstCastExprClass: return "const_cast";
679 case CXXAddrspaceCastExprClass: return "addrspace_cast";
680 default: return "<invalid cast>";
681 }
682 }
683
Create(const ASTContext & C,QualType T,ExprValueKind VK,CastKind K,Expr * Op,const CXXCastPath * BasePath,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc,SourceRange AngleBrackets)684 CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
685 ExprValueKind VK,
686 CastKind K, Expr *Op,
687 const CXXCastPath *BasePath,
688 TypeSourceInfo *WrittenTy,
689 SourceLocation L,
690 SourceLocation RParenLoc,
691 SourceRange AngleBrackets) {
692 unsigned PathSize = (BasePath ? BasePath->size() : 0);
693 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
694 auto *E = new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
695 RParenLoc, AngleBrackets, C);
696 if (PathSize)
697 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
698 E->getTrailingObjects<CXXBaseSpecifier *>());
699 return E;
700 }
701
CreateEmpty(const ASTContext & C,unsigned PathSize)702 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
703 unsigned PathSize) {
704 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
705 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
706 }
707
Create(const ASTContext & C,QualType T,ExprValueKind VK,CastKind K,Expr * Op,const CXXCastPath * BasePath,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc,SourceRange AngleBrackets)708 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
709 ExprValueKind VK,
710 CastKind K, Expr *Op,
711 const CXXCastPath *BasePath,
712 TypeSourceInfo *WrittenTy,
713 SourceLocation L,
714 SourceLocation RParenLoc,
715 SourceRange AngleBrackets) {
716 unsigned PathSize = (BasePath ? BasePath->size() : 0);
717 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
718 auto *E = new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy,
719 L, RParenLoc, AngleBrackets, C);
720 if (PathSize)
721 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
722 E->getTrailingObjects<CXXBaseSpecifier *>());
723 return E;
724 }
725
CreateEmpty(const ASTContext & C,unsigned PathSize)726 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
727 unsigned PathSize) {
728 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
729 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
730 }
731
732 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
733 /// to always be null. For example:
734 ///
735 /// struct A { };
736 /// struct B final : A { };
737 /// struct C { };
738 ///
739 /// C *f(B* b) { return dynamic_cast<C*>(b); }
isAlwaysNull() const740 bool CXXDynamicCastExpr::isAlwaysNull() const
741 {
742 QualType SrcType = getSubExpr()->getType();
743 QualType DestType = getType();
744
745 if (const auto *SrcPTy = SrcType->getAs<PointerType>()) {
746 SrcType = SrcPTy->getPointeeType();
747 DestType = DestType->castAs<PointerType>()->getPointeeType();
748 }
749
750 if (DestType->isVoidType())
751 return false;
752
753 const auto *SrcRD =
754 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
755
756 if (!SrcRD->hasAttr<FinalAttr>())
757 return false;
758
759 const auto *DestRD =
760 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
761
762 return !DestRD->isDerivedFrom(SrcRD);
763 }
764
765 CXXReinterpretCastExpr *
Create(const ASTContext & C,QualType T,ExprValueKind VK,CastKind K,Expr * Op,const CXXCastPath * BasePath,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc,SourceRange AngleBrackets)766 CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
767 ExprValueKind VK, CastKind K, Expr *Op,
768 const CXXCastPath *BasePath,
769 TypeSourceInfo *WrittenTy, SourceLocation L,
770 SourceLocation RParenLoc,
771 SourceRange AngleBrackets) {
772 unsigned PathSize = (BasePath ? BasePath->size() : 0);
773 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
774 auto *E = new (Buffer) CXXReinterpretCastExpr(
775 T, VK, K, Op, PathSize, WrittenTy, L, RParenLoc, AngleBrackets, C);
776 if (PathSize)
777 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
778 E->getTrailingObjects<CXXBaseSpecifier *>());
779 return E;
780 }
781
782 CXXReinterpretCastExpr *
CreateEmpty(const ASTContext & C,unsigned PathSize)783 CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
784 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
785 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
786 }
787
Create(const ASTContext & C,QualType T,ExprValueKind VK,CastKind CK,Expr * Op,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc,SourceRange AngleBrackets)788 CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
789 ExprValueKind VK, CastKind CK,
790 Expr *Op, TypeSourceInfo *WrittenTy,
791 SourceLocation L,
792 SourceLocation RParenLoc,
793 SourceRange AngleBrackets) {
794 return new (C) CXXConstCastExpr(T, VK, CK, Op, WrittenTy, L, RParenLoc,
795 AngleBrackets, C);
796 }
797
CreateEmpty(const ASTContext & C)798 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
799 return new (C) CXXConstCastExpr(EmptyShell());
800 }
801
802 CXXAddrspaceCastExpr *
Create(const ASTContext & C,QualType T,ExprValueKind VK,CastKind K,Expr * Op,TypeSourceInfo * WrittenTy,SourceLocation L,SourceLocation RParenLoc,SourceRange AngleBrackets)803 CXXAddrspaceCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
804 CastKind K, Expr *Op, TypeSourceInfo *WrittenTy,
805 SourceLocation L, SourceLocation RParenLoc,
806 SourceRange AngleBrackets) {
807 return new (C) CXXAddrspaceCastExpr(T, VK, K, Op, WrittenTy, L, RParenLoc,
808 AngleBrackets, C);
809 }
810
CreateEmpty(const ASTContext & C)811 CXXAddrspaceCastExpr *CXXAddrspaceCastExpr::CreateEmpty(const ASTContext &C) {
812 return new (C) CXXAddrspaceCastExpr(EmptyShell());
813 }
814
815 CXXFunctionalCastExpr *
Create(const ASTContext & C,QualType T,ExprValueKind VK,TypeSourceInfo * Written,CastKind K,Expr * Op,const CXXCastPath * BasePath,SourceLocation L,SourceLocation R)816 CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
817 TypeSourceInfo *Written, CastKind K, Expr *Op,
818 const CXXCastPath *BasePath,
819 SourceLocation L, SourceLocation R) {
820 unsigned PathSize = (BasePath ? BasePath->size() : 0);
821 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
822 auto *E = new (Buffer)
823 CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R, C);
824 if (PathSize)
825 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
826 E->getTrailingObjects<CXXBaseSpecifier *>());
827 return E;
828 }
829
830 CXXFunctionalCastExpr *
CreateEmpty(const ASTContext & C,unsigned PathSize)831 CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
832 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
833 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
834 }
835
getBeginLoc() const836 SourceLocation CXXFunctionalCastExpr::getBeginLoc() const {
837 return getTypeInfoAsWritten()->getTypeLoc().getBeginLoc();
838 }
839
getEndLoc() const840 SourceLocation CXXFunctionalCastExpr::getEndLoc() const {
841 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getEndLoc();
842 }
843
UserDefinedLiteral(Expr * Fn,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation LitEndLoc,SourceLocation SuffixLoc)844 UserDefinedLiteral::UserDefinedLiteral(Expr *Fn, ArrayRef<Expr *> Args,
845 QualType Ty, ExprValueKind VK,
846 SourceLocation LitEndLoc,
847 SourceLocation SuffixLoc)
848 : CallExpr(UserDefinedLiteralClass, Fn, /*PreArgs=*/{}, Args, Ty, VK,
849 LitEndLoc, /*MinNumArgs=*/0, NotADL),
850 UDSuffixLoc(SuffixLoc) {}
851
UserDefinedLiteral(unsigned NumArgs,EmptyShell Empty)852 UserDefinedLiteral::UserDefinedLiteral(unsigned NumArgs, EmptyShell Empty)
853 : CallExpr(UserDefinedLiteralClass, /*NumPreArgs=*/0, NumArgs, Empty) {}
854
Create(const ASTContext & Ctx,Expr * Fn,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation LitEndLoc,SourceLocation SuffixLoc)855 UserDefinedLiteral *UserDefinedLiteral::Create(const ASTContext &Ctx, Expr *Fn,
856 ArrayRef<Expr *> Args,
857 QualType Ty, ExprValueKind VK,
858 SourceLocation LitEndLoc,
859 SourceLocation SuffixLoc) {
860 // Allocate storage for the trailing objects of CallExpr.
861 unsigned NumArgs = Args.size();
862 unsigned SizeOfTrailingObjects =
863 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
864 void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
865 alignof(UserDefinedLiteral));
866 return new (Mem) UserDefinedLiteral(Fn, Args, Ty, VK, LitEndLoc, SuffixLoc);
867 }
868
CreateEmpty(const ASTContext & Ctx,unsigned NumArgs,EmptyShell Empty)869 UserDefinedLiteral *UserDefinedLiteral::CreateEmpty(const ASTContext &Ctx,
870 unsigned NumArgs,
871 EmptyShell Empty) {
872 // Allocate storage for the trailing objects of CallExpr.
873 unsigned SizeOfTrailingObjects =
874 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/0, NumArgs);
875 void *Mem = Ctx.Allocate(sizeof(UserDefinedLiteral) + SizeOfTrailingObjects,
876 alignof(UserDefinedLiteral));
877 return new (Mem) UserDefinedLiteral(NumArgs, Empty);
878 }
879
880 UserDefinedLiteral::LiteralOperatorKind
getLiteralOperatorKind() const881 UserDefinedLiteral::getLiteralOperatorKind() const {
882 if (getNumArgs() == 0)
883 return LOK_Template;
884 if (getNumArgs() == 2)
885 return LOK_String;
886
887 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
888 QualType ParamTy =
889 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
890 if (ParamTy->isPointerType())
891 return LOK_Raw;
892 if (ParamTy->isAnyCharacterType())
893 return LOK_Character;
894 if (ParamTy->isIntegerType())
895 return LOK_Integer;
896 if (ParamTy->isFloatingType())
897 return LOK_Floating;
898
899 llvm_unreachable("unknown kind of literal operator");
900 }
901
getCookedLiteral()902 Expr *UserDefinedLiteral::getCookedLiteral() {
903 #ifndef NDEBUG
904 LiteralOperatorKind LOK = getLiteralOperatorKind();
905 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
906 #endif
907 return getArg(0);
908 }
909
getUDSuffix() const910 const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
911 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
912 }
913
CXXDefaultInitExpr(const ASTContext & Ctx,SourceLocation Loc,FieldDecl * Field,QualType Ty,DeclContext * UsedContext)914 CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &Ctx,
915 SourceLocation Loc, FieldDecl *Field,
916 QualType Ty, DeclContext *UsedContext)
917 : Expr(CXXDefaultInitExprClass, Ty.getNonLValueExprType(Ctx),
918 Ty->isLValueReferenceType()
919 ? VK_LValue
920 : Ty->isRValueReferenceType() ? VK_XValue : VK_RValue,
921 /*FIXME*/ OK_Ordinary),
922 Field(Field), UsedContext(UsedContext) {
923 CXXDefaultInitExprBits.Loc = Loc;
924 assert(Field->hasInClassInitializer());
925
926 setDependence(ExprDependence::None);
927 }
928
Create(const ASTContext & C,const CXXDestructorDecl * Destructor)929 CXXTemporary *CXXTemporary::Create(const ASTContext &C,
930 const CXXDestructorDecl *Destructor) {
931 return new (C) CXXTemporary(Destructor);
932 }
933
Create(const ASTContext & C,CXXTemporary * Temp,Expr * SubExpr)934 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
935 CXXTemporary *Temp,
936 Expr* SubExpr) {
937 assert((SubExpr->getType()->isRecordType() ||
938 SubExpr->getType()->isArrayType()) &&
939 "Expression bound to a temporary must have record or array type!");
940
941 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
942 }
943
CXXTemporaryObjectExpr(CXXConstructorDecl * Cons,QualType Ty,TypeSourceInfo * TSI,ArrayRef<Expr * > Args,SourceRange ParenOrBraceRange,bool HadMultipleCandidates,bool ListInitialization,bool StdInitListInitialization,bool ZeroInitialization)944 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(
945 CXXConstructorDecl *Cons, QualType Ty, TypeSourceInfo *TSI,
946 ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
947 bool HadMultipleCandidates, bool ListInitialization,
948 bool StdInitListInitialization, bool ZeroInitialization)
949 : CXXConstructExpr(
950 CXXTemporaryObjectExprClass, Ty, TSI->getTypeLoc().getBeginLoc(),
951 Cons, /* Elidable=*/false, Args, HadMultipleCandidates,
952 ListInitialization, StdInitListInitialization, ZeroInitialization,
953 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
954 TSI(TSI) {}
955
CXXTemporaryObjectExpr(EmptyShell Empty,unsigned NumArgs)956 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(EmptyShell Empty,
957 unsigned NumArgs)
958 : CXXConstructExpr(CXXTemporaryObjectExprClass, Empty, NumArgs) {}
959
Create(const ASTContext & Ctx,CXXConstructorDecl * Cons,QualType Ty,TypeSourceInfo * TSI,ArrayRef<Expr * > Args,SourceRange ParenOrBraceRange,bool HadMultipleCandidates,bool ListInitialization,bool StdInitListInitialization,bool ZeroInitialization)960 CXXTemporaryObjectExpr *CXXTemporaryObjectExpr::Create(
961 const ASTContext &Ctx, CXXConstructorDecl *Cons, QualType Ty,
962 TypeSourceInfo *TSI, ArrayRef<Expr *> Args, SourceRange ParenOrBraceRange,
963 bool HadMultipleCandidates, bool ListInitialization,
964 bool StdInitListInitialization, bool ZeroInitialization) {
965 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
966 void *Mem =
967 Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
968 alignof(CXXTemporaryObjectExpr));
969 return new (Mem) CXXTemporaryObjectExpr(
970 Cons, Ty, TSI, Args, ParenOrBraceRange, HadMultipleCandidates,
971 ListInitialization, StdInitListInitialization, ZeroInitialization);
972 }
973
974 CXXTemporaryObjectExpr *
CreateEmpty(const ASTContext & Ctx,unsigned NumArgs)975 CXXTemporaryObjectExpr::CreateEmpty(const ASTContext &Ctx, unsigned NumArgs) {
976 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
977 void *Mem =
978 Ctx.Allocate(sizeof(CXXTemporaryObjectExpr) + SizeOfTrailingObjects,
979 alignof(CXXTemporaryObjectExpr));
980 return new (Mem) CXXTemporaryObjectExpr(EmptyShell(), NumArgs);
981 }
982
getBeginLoc() const983 SourceLocation CXXTemporaryObjectExpr::getBeginLoc() const {
984 return getTypeSourceInfo()->getTypeLoc().getBeginLoc();
985 }
986
getEndLoc() const987 SourceLocation CXXTemporaryObjectExpr::getEndLoc() const {
988 SourceLocation Loc = getParenOrBraceRange().getEnd();
989 if (Loc.isInvalid() && getNumArgs())
990 Loc = getArg(getNumArgs() - 1)->getEndLoc();
991 return Loc;
992 }
993
Create(const ASTContext & Ctx,QualType Ty,SourceLocation Loc,CXXConstructorDecl * Ctor,bool Elidable,ArrayRef<Expr * > Args,bool HadMultipleCandidates,bool ListInitialization,bool StdInitListInitialization,bool ZeroInitialization,ConstructionKind ConstructKind,SourceRange ParenOrBraceRange)994 CXXConstructExpr *CXXConstructExpr::Create(
995 const ASTContext &Ctx, QualType Ty, SourceLocation Loc,
996 CXXConstructorDecl *Ctor, bool Elidable, ArrayRef<Expr *> Args,
997 bool HadMultipleCandidates, bool ListInitialization,
998 bool StdInitListInitialization, bool ZeroInitialization,
999 ConstructionKind ConstructKind, SourceRange ParenOrBraceRange) {
1000 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(Args.size());
1001 void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
1002 alignof(CXXConstructExpr));
1003 return new (Mem) CXXConstructExpr(
1004 CXXConstructExprClass, Ty, Loc, Ctor, Elidable, Args,
1005 HadMultipleCandidates, ListInitialization, StdInitListInitialization,
1006 ZeroInitialization, ConstructKind, ParenOrBraceRange);
1007 }
1008
CreateEmpty(const ASTContext & Ctx,unsigned NumArgs)1009 CXXConstructExpr *CXXConstructExpr::CreateEmpty(const ASTContext &Ctx,
1010 unsigned NumArgs) {
1011 unsigned SizeOfTrailingObjects = sizeOfTrailingObjects(NumArgs);
1012 void *Mem = Ctx.Allocate(sizeof(CXXConstructExpr) + SizeOfTrailingObjects,
1013 alignof(CXXConstructExpr));
1014 return new (Mem)
1015 CXXConstructExpr(CXXConstructExprClass, EmptyShell(), NumArgs);
1016 }
1017
CXXConstructExpr(StmtClass SC,QualType Ty,SourceLocation Loc,CXXConstructorDecl * Ctor,bool Elidable,ArrayRef<Expr * > Args,bool HadMultipleCandidates,bool ListInitialization,bool StdInitListInitialization,bool ZeroInitialization,ConstructionKind ConstructKind,SourceRange ParenOrBraceRange)1018 CXXConstructExpr::CXXConstructExpr(
1019 StmtClass SC, QualType Ty, SourceLocation Loc, CXXConstructorDecl *Ctor,
1020 bool Elidable, ArrayRef<Expr *> Args, bool HadMultipleCandidates,
1021 bool ListInitialization, bool StdInitListInitialization,
1022 bool ZeroInitialization, ConstructionKind ConstructKind,
1023 SourceRange ParenOrBraceRange)
1024 : Expr(SC, Ty, VK_RValue, OK_Ordinary), Constructor(Ctor),
1025 ParenOrBraceRange(ParenOrBraceRange), NumArgs(Args.size()) {
1026 CXXConstructExprBits.Elidable = Elidable;
1027 CXXConstructExprBits.HadMultipleCandidates = HadMultipleCandidates;
1028 CXXConstructExprBits.ListInitialization = ListInitialization;
1029 CXXConstructExprBits.StdInitListInitialization = StdInitListInitialization;
1030 CXXConstructExprBits.ZeroInitialization = ZeroInitialization;
1031 CXXConstructExprBits.ConstructionKind = ConstructKind;
1032 CXXConstructExprBits.Loc = Loc;
1033
1034 Stmt **TrailingArgs = getTrailingArgs();
1035 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1036 assert(Args[I] && "NULL argument in CXXConstructExpr!");
1037 TrailingArgs[I] = Args[I];
1038 }
1039
1040 setDependence(computeDependence(this));
1041 }
1042
CXXConstructExpr(StmtClass SC,EmptyShell Empty,unsigned NumArgs)1043 CXXConstructExpr::CXXConstructExpr(StmtClass SC, EmptyShell Empty,
1044 unsigned NumArgs)
1045 : Expr(SC, Empty), NumArgs(NumArgs) {}
1046
LambdaCapture(SourceLocation Loc,bool Implicit,LambdaCaptureKind Kind,VarDecl * Var,SourceLocation EllipsisLoc)1047 LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
1048 LambdaCaptureKind Kind, VarDecl *Var,
1049 SourceLocation EllipsisLoc)
1050 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) {
1051 unsigned Bits = 0;
1052 if (Implicit)
1053 Bits |= Capture_Implicit;
1054
1055 switch (Kind) {
1056 case LCK_StarThis:
1057 Bits |= Capture_ByCopy;
1058 LLVM_FALLTHROUGH;
1059 case LCK_This:
1060 assert(!Var && "'this' capture cannot have a variable!");
1061 Bits |= Capture_This;
1062 break;
1063
1064 case LCK_ByCopy:
1065 Bits |= Capture_ByCopy;
1066 LLVM_FALLTHROUGH;
1067 case LCK_ByRef:
1068 assert(Var && "capture must have a variable!");
1069 break;
1070 case LCK_VLAType:
1071 assert(!Var && "VLA type capture cannot have a variable!");
1072 break;
1073 }
1074 DeclAndBits.setInt(Bits);
1075 }
1076
getCaptureKind() const1077 LambdaCaptureKind LambdaCapture::getCaptureKind() const {
1078 if (capturesVLAType())
1079 return LCK_VLAType;
1080 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
1081 if (capturesThis())
1082 return CapByCopy ? LCK_StarThis : LCK_This;
1083 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
1084 }
1085
LambdaExpr(QualType T,SourceRange IntroducerRange,LambdaCaptureDefault CaptureDefault,SourceLocation CaptureDefaultLoc,bool ExplicitParams,bool ExplicitResultType,ArrayRef<Expr * > CaptureInits,SourceLocation ClosingBrace,bool ContainsUnexpandedParameterPack)1086 LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
1087 LambdaCaptureDefault CaptureDefault,
1088 SourceLocation CaptureDefaultLoc, bool ExplicitParams,
1089 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
1090 SourceLocation ClosingBrace,
1091 bool ContainsUnexpandedParameterPack)
1092 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary),
1093 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
1094 ClosingBrace(ClosingBrace) {
1095 LambdaExprBits.NumCaptures = CaptureInits.size();
1096 LambdaExprBits.CaptureDefault = CaptureDefault;
1097 LambdaExprBits.ExplicitParams = ExplicitParams;
1098 LambdaExprBits.ExplicitResultType = ExplicitResultType;
1099
1100 CXXRecordDecl *Class = getLambdaClass();
1101 (void)Class;
1102 assert(capture_size() == Class->capture_size() && "Wrong number of captures");
1103 assert(getCaptureDefault() == Class->getLambdaCaptureDefault());
1104
1105 // Copy initialization expressions for the non-static data members.
1106 Stmt **Stored = getStoredStmts();
1107 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
1108 *Stored++ = CaptureInits[I];
1109
1110 // Copy the body of the lambda.
1111 *Stored++ = getCallOperator()->getBody();
1112
1113 setDependence(computeDependence(this, ContainsUnexpandedParameterPack));
1114 }
1115
LambdaExpr(EmptyShell Empty,unsigned NumCaptures)1116 LambdaExpr::LambdaExpr(EmptyShell Empty, unsigned NumCaptures)
1117 : Expr(LambdaExprClass, Empty) {
1118 LambdaExprBits.NumCaptures = NumCaptures;
1119
1120 // Initially don't initialize the body of the LambdaExpr. The body will
1121 // be lazily deserialized when needed.
1122 getStoredStmts()[NumCaptures] = nullptr; // Not one past the end.
1123 }
1124
Create(const ASTContext & Context,CXXRecordDecl * Class,SourceRange IntroducerRange,LambdaCaptureDefault CaptureDefault,SourceLocation CaptureDefaultLoc,bool ExplicitParams,bool ExplicitResultType,ArrayRef<Expr * > CaptureInits,SourceLocation ClosingBrace,bool ContainsUnexpandedParameterPack)1125 LambdaExpr *LambdaExpr::Create(const ASTContext &Context, CXXRecordDecl *Class,
1126 SourceRange IntroducerRange,
1127 LambdaCaptureDefault CaptureDefault,
1128 SourceLocation CaptureDefaultLoc,
1129 bool ExplicitParams, bool ExplicitResultType,
1130 ArrayRef<Expr *> CaptureInits,
1131 SourceLocation ClosingBrace,
1132 bool ContainsUnexpandedParameterPack) {
1133 // Determine the type of the expression (i.e., the type of the
1134 // function object we're creating).
1135 QualType T = Context.getTypeDeclType(Class);
1136
1137 unsigned Size = totalSizeToAlloc<Stmt *>(CaptureInits.size() + 1);
1138 void *Mem = Context.Allocate(Size);
1139 return new (Mem)
1140 LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
1141 ExplicitParams, ExplicitResultType, CaptureInits, ClosingBrace,
1142 ContainsUnexpandedParameterPack);
1143 }
1144
CreateDeserialized(const ASTContext & C,unsigned NumCaptures)1145 LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1146 unsigned NumCaptures) {
1147 unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
1148 void *Mem = C.Allocate(Size);
1149 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
1150 }
1151
initBodyIfNeeded() const1152 void LambdaExpr::initBodyIfNeeded() const {
1153 if (!getStoredStmts()[capture_size()]) {
1154 auto *This = const_cast<LambdaExpr *>(this);
1155 This->getStoredStmts()[capture_size()] = getCallOperator()->getBody();
1156 }
1157 }
1158
getBody() const1159 Stmt *LambdaExpr::getBody() const {
1160 initBodyIfNeeded();
1161 return getStoredStmts()[capture_size()];
1162 }
1163
getCompoundStmtBody() const1164 const CompoundStmt *LambdaExpr::getCompoundStmtBody() const {
1165 Stmt *Body = getBody();
1166 if (const auto *CoroBody = dyn_cast<CoroutineBodyStmt>(Body))
1167 return cast<CompoundStmt>(CoroBody->getBody());
1168 return cast<CompoundStmt>(Body);
1169 }
1170
isInitCapture(const LambdaCapture * C) const1171 bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
1172 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
1173 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
1174 }
1175
capture_begin() const1176 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
1177 return getLambdaClass()->getLambdaData().Captures;
1178 }
1179
capture_end() const1180 LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
1181 return capture_begin() + capture_size();
1182 }
1183
captures() const1184 LambdaExpr::capture_range LambdaExpr::captures() const {
1185 return capture_range(capture_begin(), capture_end());
1186 }
1187
explicit_capture_begin() const1188 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1189 return capture_begin();
1190 }
1191
explicit_capture_end() const1192 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1193 struct CXXRecordDecl::LambdaDefinitionData &Data
1194 = getLambdaClass()->getLambdaData();
1195 return Data.Captures + Data.NumExplicitCaptures;
1196 }
1197
explicit_captures() const1198 LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1199 return capture_range(explicit_capture_begin(), explicit_capture_end());
1200 }
1201
implicit_capture_begin() const1202 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1203 return explicit_capture_end();
1204 }
1205
implicit_capture_end() const1206 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1207 return capture_end();
1208 }
1209
implicit_captures() const1210 LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1211 return capture_range(implicit_capture_begin(), implicit_capture_end());
1212 }
1213
getLambdaClass() const1214 CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1215 return getType()->getAsCXXRecordDecl();
1216 }
1217
getCallOperator() const1218 CXXMethodDecl *LambdaExpr::getCallOperator() const {
1219 CXXRecordDecl *Record = getLambdaClass();
1220 return Record->getLambdaCallOperator();
1221 }
1222
getDependentCallOperator() const1223 FunctionTemplateDecl *LambdaExpr::getDependentCallOperator() const {
1224 CXXRecordDecl *Record = getLambdaClass();
1225 return Record->getDependentLambdaCallOperator();
1226 }
1227
getTemplateParameterList() const1228 TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1229 CXXRecordDecl *Record = getLambdaClass();
1230 return Record->getGenericLambdaTemplateParameterList();
1231 }
1232
getExplicitTemplateParameters() const1233 ArrayRef<NamedDecl *> LambdaExpr::getExplicitTemplateParameters() const {
1234 const CXXRecordDecl *Record = getLambdaClass();
1235 return Record->getLambdaExplicitTemplateParameters();
1236 }
1237
isMutable() const1238 bool LambdaExpr::isMutable() const { return !getCallOperator()->isConst(); }
1239
children()1240 LambdaExpr::child_range LambdaExpr::children() {
1241 initBodyIfNeeded();
1242 return child_range(getStoredStmts(), getStoredStmts() + capture_size() + 1);
1243 }
1244
children() const1245 LambdaExpr::const_child_range LambdaExpr::children() const {
1246 initBodyIfNeeded();
1247 return const_child_range(getStoredStmts(),
1248 getStoredStmts() + capture_size() + 1);
1249 }
1250
ExprWithCleanups(Expr * subexpr,bool CleanupsHaveSideEffects,ArrayRef<CleanupObject> objects)1251 ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1252 bool CleanupsHaveSideEffects,
1253 ArrayRef<CleanupObject> objects)
1254 : FullExpr(ExprWithCleanupsClass, subexpr) {
1255 ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
1256 ExprWithCleanupsBits.NumObjects = objects.size();
1257 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1258 getTrailingObjects<CleanupObject>()[i] = objects[i];
1259 }
1260
Create(const ASTContext & C,Expr * subexpr,bool CleanupsHaveSideEffects,ArrayRef<CleanupObject> objects)1261 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
1262 bool CleanupsHaveSideEffects,
1263 ArrayRef<CleanupObject> objects) {
1264 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
1265 alignof(ExprWithCleanups));
1266 return new (buffer)
1267 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
1268 }
1269
ExprWithCleanups(EmptyShell empty,unsigned numObjects)1270 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1271 : FullExpr(ExprWithCleanupsClass, empty) {
1272 ExprWithCleanupsBits.NumObjects = numObjects;
1273 }
1274
Create(const ASTContext & C,EmptyShell empty,unsigned numObjects)1275 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1276 EmptyShell empty,
1277 unsigned numObjects) {
1278 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
1279 alignof(ExprWithCleanups));
1280 return new (buffer) ExprWithCleanups(empty, numObjects);
1281 }
1282
CXXUnresolvedConstructExpr(TypeSourceInfo * TSI,SourceLocation LParenLoc,ArrayRef<Expr * > Args,SourceLocation RParenLoc)1283 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *TSI,
1284 SourceLocation LParenLoc,
1285 ArrayRef<Expr *> Args,
1286 SourceLocation RParenLoc)
1287 : Expr(CXXUnresolvedConstructExprClass,
1288 TSI->getType().getNonReferenceType(),
1289 (TSI->getType()->isLValueReferenceType()
1290 ? VK_LValue
1291 : TSI->getType()->isRValueReferenceType() ? VK_XValue
1292 : VK_RValue),
1293 OK_Ordinary),
1294 TSI(TSI), LParenLoc(LParenLoc), RParenLoc(RParenLoc) {
1295 CXXUnresolvedConstructExprBits.NumArgs = Args.size();
1296 auto **StoredArgs = getTrailingObjects<Expr *>();
1297 for (unsigned I = 0; I != Args.size(); ++I)
1298 StoredArgs[I] = Args[I];
1299 setDependence(computeDependence(this));
1300 }
1301
Create(const ASTContext & Context,TypeSourceInfo * TSI,SourceLocation LParenLoc,ArrayRef<Expr * > Args,SourceLocation RParenLoc)1302 CXXUnresolvedConstructExpr *CXXUnresolvedConstructExpr::Create(
1303 const ASTContext &Context, TypeSourceInfo *TSI, SourceLocation LParenLoc,
1304 ArrayRef<Expr *> Args, SourceLocation RParenLoc) {
1305 void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
1306 return new (Mem) CXXUnresolvedConstructExpr(TSI, LParenLoc, Args, RParenLoc);
1307 }
1308
1309 CXXUnresolvedConstructExpr *
CreateEmpty(const ASTContext & Context,unsigned NumArgs)1310 CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &Context,
1311 unsigned NumArgs) {
1312 void *Mem = Context.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
1313 return new (Mem) CXXUnresolvedConstructExpr(EmptyShell(), NumArgs);
1314 }
1315
getBeginLoc() const1316 SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const {
1317 return TSI->getTypeLoc().getBeginLoc();
1318 }
1319
CXXDependentScopeMemberExpr(const ASTContext & Ctx,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,NamedDecl * FirstQualifierFoundInScope,DeclarationNameInfo MemberNameInfo,const TemplateArgumentListInfo * TemplateArgs)1320 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1321 const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
1322 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1323 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1324 DeclarationNameInfo MemberNameInfo,
1325 const TemplateArgumentListInfo *TemplateArgs)
1326 : Expr(CXXDependentScopeMemberExprClass, Ctx.DependentTy, VK_LValue,
1327 OK_Ordinary),
1328 Base(Base), BaseType(BaseType), QualifierLoc(QualifierLoc),
1329 MemberNameInfo(MemberNameInfo) {
1330 CXXDependentScopeMemberExprBits.IsArrow = IsArrow;
1331 CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
1332 (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
1333 CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
1334 FirstQualifierFoundInScope != nullptr;
1335 CXXDependentScopeMemberExprBits.OperatorLoc = OperatorLoc;
1336
1337 if (TemplateArgs) {
1338 auto Deps = TemplateArgumentDependence::None;
1339 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1340 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1341 Deps);
1342 } else if (TemplateKWLoc.isValid()) {
1343 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1344 TemplateKWLoc);
1345 }
1346
1347 if (hasFirstQualifierFoundInScope())
1348 *getTrailingObjects<NamedDecl *>() = FirstQualifierFoundInScope;
1349 setDependence(computeDependence(this));
1350 }
1351
CXXDependentScopeMemberExpr(EmptyShell Empty,bool HasTemplateKWAndArgsInfo,bool HasFirstQualifierFoundInScope)1352 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1353 EmptyShell Empty, bool HasTemplateKWAndArgsInfo,
1354 bool HasFirstQualifierFoundInScope)
1355 : Expr(CXXDependentScopeMemberExprClass, Empty) {
1356 CXXDependentScopeMemberExprBits.HasTemplateKWAndArgsInfo =
1357 HasTemplateKWAndArgsInfo;
1358 CXXDependentScopeMemberExprBits.HasFirstQualifierFoundInScope =
1359 HasFirstQualifierFoundInScope;
1360 }
1361
Create(const ASTContext & Ctx,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,NamedDecl * FirstQualifierFoundInScope,DeclarationNameInfo MemberNameInfo,const TemplateArgumentListInfo * TemplateArgs)1362 CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::Create(
1363 const ASTContext &Ctx, Expr *Base, QualType BaseType, bool IsArrow,
1364 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1365 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1366 DeclarationNameInfo MemberNameInfo,
1367 const TemplateArgumentListInfo *TemplateArgs) {
1368 bool HasTemplateKWAndArgsInfo =
1369 (TemplateArgs != nullptr) || TemplateKWLoc.isValid();
1370 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1371 bool HasFirstQualifierFoundInScope = FirstQualifierFoundInScope != nullptr;
1372
1373 unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
1374 TemplateArgumentLoc, NamedDecl *>(
1375 HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
1376
1377 void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
1378 return new (Mem) CXXDependentScopeMemberExpr(
1379 Ctx, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
1380 FirstQualifierFoundInScope, MemberNameInfo, TemplateArgs);
1381 }
1382
CreateEmpty(const ASTContext & Ctx,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs,bool HasFirstQualifierFoundInScope)1383 CXXDependentScopeMemberExpr *CXXDependentScopeMemberExpr::CreateEmpty(
1384 const ASTContext &Ctx, bool HasTemplateKWAndArgsInfo,
1385 unsigned NumTemplateArgs, bool HasFirstQualifierFoundInScope) {
1386 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1387
1388 unsigned Size = totalSizeToAlloc<ASTTemplateKWAndArgsInfo,
1389 TemplateArgumentLoc, NamedDecl *>(
1390 HasTemplateKWAndArgsInfo, NumTemplateArgs, HasFirstQualifierFoundInScope);
1391
1392 void *Mem = Ctx.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
1393 return new (Mem) CXXDependentScopeMemberExpr(
1394 EmptyShell(), HasTemplateKWAndArgsInfo, HasFirstQualifierFoundInScope);
1395 }
1396
hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,UnresolvedSetIterator end)1397 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1398 UnresolvedSetIterator end) {
1399 do {
1400 NamedDecl *decl = *begin;
1401 if (isa<UnresolvedUsingValueDecl>(decl))
1402 return false;
1403
1404 // Unresolved member expressions should only contain methods and
1405 // method templates.
1406 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1407 ->isStatic())
1408 return false;
1409 } while (++begin != end);
1410
1411 return true;
1412 }
1413
UnresolvedMemberExpr(const ASTContext & Context,bool HasUnresolvedUsing,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & MemberNameInfo,const TemplateArgumentListInfo * TemplateArgs,UnresolvedSetIterator Begin,UnresolvedSetIterator End)1414 UnresolvedMemberExpr::UnresolvedMemberExpr(
1415 const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
1416 QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
1417 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1418 const DeclarationNameInfo &MemberNameInfo,
1419 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1420 UnresolvedSetIterator End)
1421 : OverloadExpr(
1422 UnresolvedMemberExprClass, Context, QualifierLoc, TemplateKWLoc,
1423 MemberNameInfo, TemplateArgs, Begin, End,
1424 // Dependent
1425 ((Base && Base->isTypeDependent()) || BaseType->isDependentType()),
1426 ((Base && Base->isInstantiationDependent()) ||
1427 BaseType->isInstantiationDependentType()),
1428 // Contains unexpanded parameter pack
1429 ((Base && Base->containsUnexpandedParameterPack()) ||
1430 BaseType->containsUnexpandedParameterPack())),
1431 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
1432 UnresolvedMemberExprBits.IsArrow = IsArrow;
1433 UnresolvedMemberExprBits.HasUnresolvedUsing = HasUnresolvedUsing;
1434
1435 // Check whether all of the members are non-static member functions,
1436 // and if so, mark give this bound-member type instead of overload type.
1437 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1438 setType(Context.BoundMemberTy);
1439 }
1440
UnresolvedMemberExpr(EmptyShell Empty,unsigned NumResults,bool HasTemplateKWAndArgsInfo)1441 UnresolvedMemberExpr::UnresolvedMemberExpr(EmptyShell Empty,
1442 unsigned NumResults,
1443 bool HasTemplateKWAndArgsInfo)
1444 : OverloadExpr(UnresolvedMemberExprClass, Empty, NumResults,
1445 HasTemplateKWAndArgsInfo) {}
1446
isImplicitAccess() const1447 bool UnresolvedMemberExpr::isImplicitAccess() const {
1448 if (!Base)
1449 return true;
1450
1451 return cast<Expr>(Base)->isImplicitCXXThis();
1452 }
1453
Create(const ASTContext & Context,bool HasUnresolvedUsing,Expr * Base,QualType BaseType,bool IsArrow,SourceLocation OperatorLoc,NestedNameSpecifierLoc QualifierLoc,SourceLocation TemplateKWLoc,const DeclarationNameInfo & MemberNameInfo,const TemplateArgumentListInfo * TemplateArgs,UnresolvedSetIterator Begin,UnresolvedSetIterator End)1454 UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1455 const ASTContext &Context, bool HasUnresolvedUsing, Expr *Base,
1456 QualType BaseType, bool IsArrow, SourceLocation OperatorLoc,
1457 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1458 const DeclarationNameInfo &MemberNameInfo,
1459 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1460 UnresolvedSetIterator End) {
1461 unsigned NumResults = End - Begin;
1462 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1463 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1464 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
1465 TemplateArgumentLoc>(
1466 NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
1467 void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
1468 return new (Mem) UnresolvedMemberExpr(
1469 Context, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc,
1470 QualifierLoc, TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
1471 }
1472
CreateEmpty(const ASTContext & Context,unsigned NumResults,bool HasTemplateKWAndArgsInfo,unsigned NumTemplateArgs)1473 UnresolvedMemberExpr *UnresolvedMemberExpr::CreateEmpty(
1474 const ASTContext &Context, unsigned NumResults,
1475 bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) {
1476 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1477 unsigned Size = totalSizeToAlloc<DeclAccessPair, ASTTemplateKWAndArgsInfo,
1478 TemplateArgumentLoc>(
1479 NumResults, HasTemplateKWAndArgsInfo, NumTemplateArgs);
1480 void *Mem = Context.Allocate(Size, alignof(UnresolvedMemberExpr));
1481 return new (Mem)
1482 UnresolvedMemberExpr(EmptyShell(), NumResults, HasTemplateKWAndArgsInfo);
1483 }
1484
getNamingClass()1485 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() {
1486 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1487
1488 // If there was a nested name specifier, it names the naming class.
1489 // It can't be dependent: after all, we were actually able to do the
1490 // lookup.
1491 CXXRecordDecl *Record = nullptr;
1492 auto *NNS = getQualifier();
1493 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
1494 const Type *T = getQualifier()->getAsType();
1495 assert(T && "qualifier in member expression does not name type");
1496 Record = T->getAsCXXRecordDecl();
1497 assert(Record && "qualifier in member expression does not name record");
1498 }
1499 // Otherwise the naming class must have been the base class.
1500 else {
1501 QualType BaseType = getBaseType().getNonReferenceType();
1502 if (isArrow())
1503 BaseType = BaseType->castAs<PointerType>()->getPointeeType();
1504
1505 Record = BaseType->getAsCXXRecordDecl();
1506 assert(Record && "base of member expression does not name record");
1507 }
1508
1509 return Record;
1510 }
1511
1512 SizeOfPackExpr *
Create(ASTContext & Context,SourceLocation OperatorLoc,NamedDecl * Pack,SourceLocation PackLoc,SourceLocation RParenLoc,Optional<unsigned> Length,ArrayRef<TemplateArgument> PartialArgs)1513 SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1514 NamedDecl *Pack, SourceLocation PackLoc,
1515 SourceLocation RParenLoc,
1516 Optional<unsigned> Length,
1517 ArrayRef<TemplateArgument> PartialArgs) {
1518 void *Storage =
1519 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
1520 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1521 PackLoc, RParenLoc, Length, PartialArgs);
1522 }
1523
CreateDeserialized(ASTContext & Context,unsigned NumPartialArgs)1524 SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1525 unsigned NumPartialArgs) {
1526 void *Storage =
1527 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
1528 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1529 }
1530
SubstNonTypeTemplateParmPackExpr(QualType T,ExprValueKind ValueKind,NonTypeTemplateParmDecl * Param,SourceLocation NameLoc,const TemplateArgument & ArgPack)1531 SubstNonTypeTemplateParmPackExpr::SubstNonTypeTemplateParmPackExpr(
1532 QualType T, ExprValueKind ValueKind, NonTypeTemplateParmDecl *Param,
1533 SourceLocation NameLoc, const TemplateArgument &ArgPack)
1534 : Expr(SubstNonTypeTemplateParmPackExprClass, T, ValueKind, OK_Ordinary),
1535 Param(Param), Arguments(ArgPack.pack_begin()),
1536 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) {
1537 setDependence(ExprDependence::TypeValueInstantiation |
1538 ExprDependence::UnexpandedPack);
1539 }
1540
getArgumentPack() const1541 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1542 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
1543 }
1544
FunctionParmPackExpr(QualType T,VarDecl * ParamPack,SourceLocation NameLoc,unsigned NumParams,VarDecl * const * Params)1545 FunctionParmPackExpr::FunctionParmPackExpr(QualType T, VarDecl *ParamPack,
1546 SourceLocation NameLoc,
1547 unsigned NumParams,
1548 VarDecl *const *Params)
1549 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary),
1550 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1551 if (Params)
1552 std::uninitialized_copy(Params, Params + NumParams,
1553 getTrailingObjects<VarDecl *>());
1554 setDependence(ExprDependence::TypeValueInstantiation |
1555 ExprDependence::UnexpandedPack);
1556 }
1557
1558 FunctionParmPackExpr *
Create(const ASTContext & Context,QualType T,VarDecl * ParamPack,SourceLocation NameLoc,ArrayRef<VarDecl * > Params)1559 FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
1560 VarDecl *ParamPack, SourceLocation NameLoc,
1561 ArrayRef<VarDecl *> Params) {
1562 return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(Params.size())))
1563 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1564 }
1565
1566 FunctionParmPackExpr *
CreateEmpty(const ASTContext & Context,unsigned NumParams)1567 FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1568 unsigned NumParams) {
1569 return new (Context.Allocate(totalSizeToAlloc<VarDecl *>(NumParams)))
1570 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
1571 }
1572
MaterializeTemporaryExpr(QualType T,Expr * Temporary,bool BoundToLvalueReference,LifetimeExtendedTemporaryDecl * MTD)1573 MaterializeTemporaryExpr::MaterializeTemporaryExpr(
1574 QualType T, Expr *Temporary, bool BoundToLvalueReference,
1575 LifetimeExtendedTemporaryDecl *MTD)
1576 : Expr(MaterializeTemporaryExprClass, T,
1577 BoundToLvalueReference ? VK_LValue : VK_XValue, OK_Ordinary) {
1578 if (MTD) {
1579 State = MTD;
1580 MTD->ExprWithTemporary = Temporary;
1581 return;
1582 }
1583 State = Temporary;
1584 setDependence(computeDependence(this));
1585 }
1586
setExtendingDecl(ValueDecl * ExtendedBy,unsigned ManglingNumber)1587 void MaterializeTemporaryExpr::setExtendingDecl(ValueDecl *ExtendedBy,
1588 unsigned ManglingNumber) {
1589 // We only need extra state if we have to remember more than just the Stmt.
1590 if (!ExtendedBy)
1591 return;
1592
1593 // We may need to allocate extra storage for the mangling number and the
1594 // extended-by ValueDecl.
1595 if (!State.is<LifetimeExtendedTemporaryDecl *>())
1596 State = LifetimeExtendedTemporaryDecl::Create(
1597 cast<Expr>(State.get<Stmt *>()), ExtendedBy, ManglingNumber);
1598
1599 auto ES = State.get<LifetimeExtendedTemporaryDecl *>();
1600 ES->ExtendingDecl = ExtendedBy;
1601 ES->ManglingNumber = ManglingNumber;
1602 }
1603
TypeTraitExpr(QualType T,SourceLocation Loc,TypeTrait Kind,ArrayRef<TypeSourceInfo * > Args,SourceLocation RParenLoc,bool Value)1604 TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1605 ArrayRef<TypeSourceInfo *> Args,
1606 SourceLocation RParenLoc, bool Value)
1607 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary), Loc(Loc),
1608 RParenLoc(RParenLoc) {
1609 assert(Kind <= TT_Last && "invalid enum value!");
1610 TypeTraitExprBits.Kind = Kind;
1611 assert(static_cast<unsigned>(Kind) == TypeTraitExprBits.Kind &&
1612 "TypeTraitExprBits.Kind overflow!");
1613 TypeTraitExprBits.Value = Value;
1614 TypeTraitExprBits.NumArgs = Args.size();
1615 assert(Args.size() == TypeTraitExprBits.NumArgs &&
1616 "TypeTraitExprBits.NumArgs overflow!");
1617
1618 auto **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1619 for (unsigned I = 0, N = Args.size(); I != N; ++I)
1620 ToArgs[I] = Args[I];
1621
1622 setDependence(computeDependence(this));
1623 }
1624
Create(const ASTContext & C,QualType T,SourceLocation Loc,TypeTrait Kind,ArrayRef<TypeSourceInfo * > Args,SourceLocation RParenLoc,bool Value)1625 TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
1626 SourceLocation Loc,
1627 TypeTrait Kind,
1628 ArrayRef<TypeSourceInfo *> Args,
1629 SourceLocation RParenLoc,
1630 bool Value) {
1631 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
1632 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1633 }
1634
CreateDeserialized(const ASTContext & C,unsigned NumArgs)1635 TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
1636 unsigned NumArgs) {
1637 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
1638 return new (Mem) TypeTraitExpr(EmptyShell());
1639 }
1640
CUDAKernelCallExpr(Expr * Fn,CallExpr * Config,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation RP,unsigned MinNumArgs)1641 CUDAKernelCallExpr::CUDAKernelCallExpr(Expr *Fn, CallExpr *Config,
1642 ArrayRef<Expr *> Args, QualType Ty,
1643 ExprValueKind VK, SourceLocation RP,
1644 unsigned MinNumArgs)
1645 : CallExpr(CUDAKernelCallExprClass, Fn, /*PreArgs=*/Config, Args, Ty, VK,
1646 RP, MinNumArgs, NotADL) {}
1647
CUDAKernelCallExpr(unsigned NumArgs,EmptyShell Empty)1648 CUDAKernelCallExpr::CUDAKernelCallExpr(unsigned NumArgs, EmptyShell Empty)
1649 : CallExpr(CUDAKernelCallExprClass, /*NumPreArgs=*/END_PREARG, NumArgs,
1650 Empty) {}
1651
1652 CUDAKernelCallExpr *
Create(const ASTContext & Ctx,Expr * Fn,CallExpr * Config,ArrayRef<Expr * > Args,QualType Ty,ExprValueKind VK,SourceLocation RP,unsigned MinNumArgs)1653 CUDAKernelCallExpr::Create(const ASTContext &Ctx, Expr *Fn, CallExpr *Config,
1654 ArrayRef<Expr *> Args, QualType Ty, ExprValueKind VK,
1655 SourceLocation RP, unsigned MinNumArgs) {
1656 // Allocate storage for the trailing objects of CallExpr.
1657 unsigned NumArgs = std::max<unsigned>(Args.size(), MinNumArgs);
1658 unsigned SizeOfTrailingObjects =
1659 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/END_PREARG, NumArgs);
1660 void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
1661 alignof(CUDAKernelCallExpr));
1662 return new (Mem) CUDAKernelCallExpr(Fn, Config, Args, Ty, VK, RP, MinNumArgs);
1663 }
1664
CreateEmpty(const ASTContext & Ctx,unsigned NumArgs,EmptyShell Empty)1665 CUDAKernelCallExpr *CUDAKernelCallExpr::CreateEmpty(const ASTContext &Ctx,
1666 unsigned NumArgs,
1667 EmptyShell Empty) {
1668 // Allocate storage for the trailing objects of CallExpr.
1669 unsigned SizeOfTrailingObjects =
1670 CallExpr::sizeOfTrailingObjects(/*NumPreArgs=*/END_PREARG, NumArgs);
1671 void *Mem = Ctx.Allocate(sizeof(CUDAKernelCallExpr) + SizeOfTrailingObjects,
1672 alignof(CUDAKernelCallExpr));
1673 return new (Mem) CUDAKernelCallExpr(NumArgs, Empty);
1674 }
1675