1 //===--- ExprCXX.cpp - (C++) Expression AST Node Implementation -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the subclesses of Expr class declared in ExprCXX.h
12 //===----------------------------------------------------------------------===//
14 #include "clang/AST/ASTContext.h"
15 #include "clang/AST/Attr.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/DeclTemplate.h"
18 #include "clang/AST/ExprCXX.h"
19 #include "clang/AST/TypeLoc.h"
20 #include "clang/Basic/IdentifierTable.h"
21 using namespace clang;
24 //===----------------------------------------------------------------------===//
25 // Child Iterators for iterating over subexpressions/substatements
26 //===----------------------------------------------------------------------===//
28 bool CXXTypeidExpr::isPotentiallyEvaluated() const {
32 // C++11 [expr.typeid]p3:
33 // When typeid is applied to an expression other than a glvalue of
34 // polymorphic class type, [...] the expression is an unevaluated operand.
35 const Expr *E = getExprOperand();
36 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
37 if (RD->isPolymorphic() && E->isGLValue())
43 QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
44 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
46 return Context.getUnqualifiedArrayType(
47 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
50 QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
51 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
53 return Context.getUnqualifiedArrayType(
54 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
57 // CXXScalarValueInitExpr
58 SourceLocation CXXScalarValueInitExpr::getLocStart() const {
59 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
63 CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
64 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
65 bool usualArrayDeleteWantsSize,
66 ArrayRef<Expr*> placementArgs,
67 SourceRange typeIdParens, Expr *arraySize,
68 InitializationStyle initializationStyle,
69 Expr *initializer, QualType ty,
70 TypeSourceInfo *allocatedTypeInfo,
71 SourceRange Range, SourceRange directInitRange)
72 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
73 ty->isDependentType(), ty->isDependentType(),
74 ty->isInstantiationDependentType(),
75 ty->containsUnexpandedParameterPack()),
76 SubExprs(nullptr), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
77 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
78 Range(Range), DirectInitRange(directInitRange),
79 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
80 assert((initializer != nullptr || initializationStyle == NoInit) &&
81 "Only NoInit can have no initializer.");
82 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
83 AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(),
84 initializer != nullptr);
87 if (arraySize->isInstantiationDependent())
88 ExprBits.InstantiationDependent = true;
90 if (arraySize->containsUnexpandedParameterPack())
91 ExprBits.ContainsUnexpandedParameterPack = true;
93 SubExprs[i++] = arraySize;
97 if (initializer->isInstantiationDependent())
98 ExprBits.InstantiationDependent = true;
100 if (initializer->containsUnexpandedParameterPack())
101 ExprBits.ContainsUnexpandedParameterPack = true;
103 SubExprs[i++] = initializer;
106 for (unsigned j = 0; j != placementArgs.size(); ++j) {
107 if (placementArgs[j]->isInstantiationDependent())
108 ExprBits.InstantiationDependent = true;
109 if (placementArgs[j]->containsUnexpandedParameterPack())
110 ExprBits.ContainsUnexpandedParameterPack = true;
112 SubExprs[i++] = placementArgs[j];
115 switch (getInitializationStyle()) {
117 this->Range.setEnd(DirectInitRange.getEnd()); break;
119 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
121 if (TypeIdParens.isValid())
122 this->Range.setEnd(TypeIdParens.getEnd());
127 void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
128 unsigned numPlaceArgs, bool hasInitializer){
129 assert(SubExprs == nullptr && "SubExprs already allocated");
131 NumPlacementArgs = numPlaceArgs;
133 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
134 SubExprs = new (C) Stmt*[TotalSize];
137 bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
138 return getOperatorNew()->getType()->castAs<FunctionProtoType>()->isNothrow(
140 !getOperatorNew()->isReservedGlobalPlacementOperator();
144 QualType CXXDeleteExpr::getDestroyedType() const {
145 const Expr *Arg = getArgument();
146 // The type-to-delete may not be a pointer if it's a dependent type.
147 const QualType ArgType = Arg->getType();
149 if (ArgType->isDependentType() && !ArgType->isPointerType())
152 return ArgType->getAs<PointerType>()->getPointeeType();
155 // CXXPseudoDestructorExpr
156 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
159 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
162 CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
163 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
164 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
165 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
166 PseudoDestructorTypeStorage DestroyedType)
167 : Expr(CXXPseudoDestructorExprClass,
168 Context.BoundMemberTy,
169 VK_RValue, OK_Ordinary,
170 /*isTypeDependent=*/(Base->isTypeDependent() ||
171 (DestroyedType.getTypeSourceInfo() &&
172 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
173 /*isValueDependent=*/Base->isValueDependent(),
174 (Base->isInstantiationDependent() ||
176 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
178 ScopeType->getType()->isInstantiationDependentType()) ||
179 (DestroyedType.getTypeSourceInfo() &&
180 DestroyedType.getTypeSourceInfo()->getType()
181 ->isInstantiationDependentType())),
182 // ContainsUnexpandedParameterPack
183 (Base->containsUnexpandedParameterPack() ||
185 QualifierLoc.getNestedNameSpecifier()
186 ->containsUnexpandedParameterPack()) ||
188 ScopeType->getType()->containsUnexpandedParameterPack()) ||
189 (DestroyedType.getTypeSourceInfo() &&
190 DestroyedType.getTypeSourceInfo()->getType()
191 ->containsUnexpandedParameterPack()))),
192 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
193 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
194 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
195 DestroyedType(DestroyedType) { }
197 QualType CXXPseudoDestructorExpr::getDestroyedType() const {
198 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
199 return TInfo->getType();
204 SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
205 SourceLocation End = DestroyedType.getLocation();
206 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
207 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
211 // UnresolvedLookupExpr
212 UnresolvedLookupExpr *
213 UnresolvedLookupExpr::Create(const ASTContext &C,
214 CXXRecordDecl *NamingClass,
215 NestedNameSpecifierLoc QualifierLoc,
216 SourceLocation TemplateKWLoc,
217 const DeclarationNameInfo &NameInfo,
219 const TemplateArgumentListInfo *Args,
220 UnresolvedSetIterator Begin,
221 UnresolvedSetIterator End)
223 assert(Args || TemplateKWLoc.isValid());
224 unsigned num_args = Args ? Args->size() : 0;
227 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(1,
229 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedLookupExpr>());
230 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
231 TemplateKWLoc, NameInfo,
232 ADL, /*Overload*/ true, Args,
236 UnresolvedLookupExpr *
237 UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
238 bool HasTemplateKWAndArgsInfo,
239 unsigned NumTemplateArgs) {
240 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
242 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
243 HasTemplateKWAndArgsInfo, NumTemplateArgs);
244 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedLookupExpr>());
245 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
246 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
250 OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
251 NestedNameSpecifierLoc QualifierLoc,
252 SourceLocation TemplateKWLoc,
253 const DeclarationNameInfo &NameInfo,
254 const TemplateArgumentListInfo *TemplateArgs,
255 UnresolvedSetIterator Begin,
256 UnresolvedSetIterator End,
258 bool KnownInstantiationDependent,
259 bool KnownContainsUnexpandedParameterPack)
260 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
262 (KnownInstantiationDependent ||
263 NameInfo.isInstantiationDependent() ||
265 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
266 (KnownContainsUnexpandedParameterPack ||
267 NameInfo.containsUnexpandedParameterPack() ||
269 QualifierLoc.getNestedNameSpecifier()
270 ->containsUnexpandedParameterPack()))),
271 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
272 Results(nullptr), NumResults(End - Begin),
273 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
274 TemplateKWLoc.isValid()) {
275 NumResults = End - Begin;
277 // Determine whether this expression is type-dependent.
278 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
279 if ((*I)->getDeclContext()->isDependentContext() ||
280 isa<UnresolvedUsingValueDecl>(*I)) {
281 ExprBits.TypeDependent = true;
282 ExprBits.ValueDependent = true;
283 ExprBits.InstantiationDependent = true;
287 Results = static_cast<DeclAccessPair *>(
288 C.Allocate(sizeof(DeclAccessPair) * NumResults,
289 llvm::alignOf<DeclAccessPair>()));
290 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
293 // If we have explicit template arguments, check for dependent
294 // template arguments and whether they contain any unexpanded pack
297 bool Dependent = false;
298 bool InstantiationDependent = false;
299 bool ContainsUnexpandedParameterPack = false;
300 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
301 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
302 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
305 ExprBits.TypeDependent = true;
306 ExprBits.ValueDependent = true;
308 if (InstantiationDependent)
309 ExprBits.InstantiationDependent = true;
310 if (ContainsUnexpandedParameterPack)
311 ExprBits.ContainsUnexpandedParameterPack = true;
312 } else if (TemplateKWLoc.isValid()) {
313 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
316 if (isTypeDependent())
317 setType(C.DependentTy);
320 void OverloadExpr::initializeResults(const ASTContext &C,
321 UnresolvedSetIterator Begin,
322 UnresolvedSetIterator End) {
323 assert(!Results && "Results already initialized!");
324 NumResults = End - Begin;
326 Results = static_cast<DeclAccessPair *>(
327 C.Allocate(sizeof(DeclAccessPair) * NumResults,
329 llvm::alignOf<DeclAccessPair>()));
330 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
334 CXXRecordDecl *OverloadExpr::getNamingClass() const {
335 if (isa<UnresolvedLookupExpr>(this))
336 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
338 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
341 // DependentScopeDeclRefExpr
342 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
343 NestedNameSpecifierLoc QualifierLoc,
344 SourceLocation TemplateKWLoc,
345 const DeclarationNameInfo &NameInfo,
346 const TemplateArgumentListInfo *Args)
347 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
349 (NameInfo.isInstantiationDependent() ||
351 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
352 (NameInfo.containsUnexpandedParameterPack() ||
354 QualifierLoc.getNestedNameSpecifier()
355 ->containsUnexpandedParameterPack()))),
356 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
357 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
360 bool Dependent = true;
361 bool InstantiationDependent = true;
362 bool ContainsUnexpandedParameterPack
363 = ExprBits.ContainsUnexpandedParameterPack;
364 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
365 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
366 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
367 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
368 } else if (TemplateKWLoc.isValid()) {
369 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
374 DependentScopeDeclRefExpr *
375 DependentScopeDeclRefExpr::Create(const ASTContext &C,
376 NestedNameSpecifierLoc QualifierLoc,
377 SourceLocation TemplateKWLoc,
378 const DeclarationNameInfo &NameInfo,
379 const TemplateArgumentListInfo *Args) {
380 assert(QualifierLoc && "should be created for dependent qualifiers");
381 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
383 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
384 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
385 void *Mem = C.Allocate(Size);
386 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
387 TemplateKWLoc, NameInfo, Args);
390 DependentScopeDeclRefExpr *
391 DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
392 bool HasTemplateKWAndArgsInfo,
393 unsigned NumTemplateArgs) {
394 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
396 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
397 HasTemplateKWAndArgsInfo, NumTemplateArgs);
398 void *Mem = C.Allocate(Size);
399 DependentScopeDeclRefExpr *E
400 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
402 DeclarationNameInfo(), nullptr);
403 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
407 SourceLocation CXXConstructExpr::getLocStart() const {
408 if (isa<CXXTemporaryObjectExpr>(this))
409 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
413 SourceLocation CXXConstructExpr::getLocEnd() const {
414 if (isa<CXXTemporaryObjectExpr>(this))
415 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
417 if (ParenOrBraceRange.isValid())
418 return ParenOrBraceRange.getEnd();
420 SourceLocation End = Loc;
421 for (unsigned I = getNumArgs(); I > 0; --I) {
422 const Expr *Arg = getArg(I-1);
423 if (!Arg->isDefaultArgument()) {
424 SourceLocation NewEnd = Arg->getLocEnd();
425 if (NewEnd.isValid()) {
435 SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
436 OverloadedOperatorKind Kind = getOperator();
437 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
438 if (getNumArgs() == 1)
440 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
443 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
444 } else if (Kind == OO_Arrow) {
445 return getArg(0)->getSourceRange();
446 } else if (Kind == OO_Call) {
447 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
448 } else if (Kind == OO_Subscript) {
449 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
450 } else if (getNumArgs() == 1) {
451 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
452 } else if (getNumArgs() == 2) {
453 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
455 return getOperatorLoc();
459 Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
460 const Expr *Callee = getCallee()->IgnoreParens();
461 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
462 return MemExpr->getBase();
463 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
464 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
467 // FIXME: Will eventually need to cope with member pointers.
471 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
472 if (const MemberExpr *MemExpr =
473 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
474 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
476 // FIXME: Will eventually need to cope with member pointers.
481 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
482 Expr* ThisArg = getImplicitObjectArgument();
486 if (ThisArg->getType()->isAnyPointerType())
487 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
489 return ThisArg->getType()->getAsCXXRecordDecl();
493 //===----------------------------------------------------------------------===//
495 //===----------------------------------------------------------------------===//
497 /// getCastName - Get the name of the C++ cast being used, e.g.,
498 /// "static_cast", "dynamic_cast", "reinterpret_cast", or
499 /// "const_cast". The returned pointer must not be freed.
500 const char *CXXNamedCastExpr::getCastName() const {
501 switch (getStmtClass()) {
502 case CXXStaticCastExprClass: return "static_cast";
503 case CXXDynamicCastExprClass: return "dynamic_cast";
504 case CXXReinterpretCastExprClass: return "reinterpret_cast";
505 case CXXConstCastExprClass: return "const_cast";
506 default: return "<invalid cast>";
510 CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
512 CastKind K, Expr *Op,
513 const CXXCastPath *BasePath,
514 TypeSourceInfo *WrittenTy,
516 SourceLocation RParenLoc,
517 SourceRange AngleBrackets) {
518 unsigned PathSize = (BasePath ? BasePath->size() : 0);
519 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
520 CXXStaticCastExpr *E =
521 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
522 RParenLoc, AngleBrackets);
524 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
525 E->getTrailingObjects<CXXBaseSpecifier *>());
529 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
531 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
532 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
535 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
537 CastKind K, Expr *Op,
538 const CXXCastPath *BasePath,
539 TypeSourceInfo *WrittenTy,
541 SourceLocation RParenLoc,
542 SourceRange AngleBrackets) {
543 unsigned PathSize = (BasePath ? BasePath->size() : 0);
544 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
545 CXXDynamicCastExpr *E =
546 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
547 RParenLoc, AngleBrackets);
549 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
550 E->getTrailingObjects<CXXBaseSpecifier *>());
554 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
556 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
557 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
560 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
561 /// to always be null. For example:
564 /// struct B final : A { };
567 /// C *f(B* b) { return dynamic_cast<C*>(b); }
568 bool CXXDynamicCastExpr::isAlwaysNull() const
570 QualType SrcType = getSubExpr()->getType();
571 QualType DestType = getType();
573 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
574 SrcType = SrcPTy->getPointeeType();
575 DestType = DestType->castAs<PointerType>()->getPointeeType();
578 if (DestType->isVoidType())
581 const CXXRecordDecl *SrcRD =
582 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
584 if (!SrcRD->hasAttr<FinalAttr>())
587 const CXXRecordDecl *DestRD =
588 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
590 return !DestRD->isDerivedFrom(SrcRD);
593 CXXReinterpretCastExpr *
594 CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
595 ExprValueKind VK, CastKind K, Expr *Op,
596 const CXXCastPath *BasePath,
597 TypeSourceInfo *WrittenTy, SourceLocation L,
598 SourceLocation RParenLoc,
599 SourceRange AngleBrackets) {
600 unsigned PathSize = (BasePath ? BasePath->size() : 0);
601 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
602 CXXReinterpretCastExpr *E =
603 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
604 RParenLoc, AngleBrackets);
606 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
607 E->getTrailingObjects<CXXBaseSpecifier *>());
611 CXXReinterpretCastExpr *
612 CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
613 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
614 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
617 CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
618 ExprValueKind VK, Expr *Op,
619 TypeSourceInfo *WrittenTy,
621 SourceLocation RParenLoc,
622 SourceRange AngleBrackets) {
623 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
626 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
627 return new (C) CXXConstCastExpr(EmptyShell());
630 CXXFunctionalCastExpr *
631 CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
632 TypeSourceInfo *Written, CastKind K, Expr *Op,
633 const CXXCastPath *BasePath,
634 SourceLocation L, SourceLocation R) {
635 unsigned PathSize = (BasePath ? BasePath->size() : 0);
636 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
637 CXXFunctionalCastExpr *E =
638 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
640 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
641 E->getTrailingObjects<CXXBaseSpecifier *>());
645 CXXFunctionalCastExpr *
646 CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
647 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
648 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
651 SourceLocation CXXFunctionalCastExpr::getLocStart() const {
652 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
655 SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
656 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
659 UserDefinedLiteral::LiteralOperatorKind
660 UserDefinedLiteral::getLiteralOperatorKind() const {
661 if (getNumArgs() == 0)
663 if (getNumArgs() == 2)
666 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
668 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
669 if (ParamTy->isPointerType())
671 if (ParamTy->isAnyCharacterType())
672 return LOK_Character;
673 if (ParamTy->isIntegerType())
675 if (ParamTy->isFloatingType())
678 llvm_unreachable("unknown kind of literal operator");
681 Expr *UserDefinedLiteral::getCookedLiteral() {
683 LiteralOperatorKind LOK = getLiteralOperatorKind();
684 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
689 const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
690 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
693 CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
694 FieldDecl *Field, QualType T)
695 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
696 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
699 /*FIXME*/ OK_Ordinary, false, false, false, false),
700 Field(Field), Loc(Loc) {
701 assert(Field->hasInClassInitializer());
704 CXXTemporary *CXXTemporary::Create(const ASTContext &C,
705 const CXXDestructorDecl *Destructor) {
706 return new (C) CXXTemporary(Destructor);
709 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
712 assert((SubExpr->getType()->isRecordType() ||
713 SubExpr->getType()->isArrayType()) &&
714 "Expression bound to a temporary must have record or array type!");
716 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
719 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
720 CXXConstructorDecl *Cons,
721 TypeSourceInfo *Type,
722 ArrayRef<Expr*> Args,
723 SourceRange ParenOrBraceRange,
724 bool HadMultipleCandidates,
725 bool ListInitialization,
726 bool StdInitListInitialization,
727 bool ZeroInitialization)
728 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
729 Type->getType().getNonReferenceType(),
730 Type->getTypeLoc().getBeginLoc(),
732 HadMultipleCandidates,
734 StdInitListInitialization,
736 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
740 SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
741 return Type->getTypeLoc().getBeginLoc();
744 SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
745 SourceLocation Loc = getParenOrBraceRange().getEnd();
746 if (Loc.isInvalid() && getNumArgs())
747 Loc = getArg(getNumArgs()-1)->getLocEnd();
751 CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
753 CXXConstructorDecl *Ctor,
755 ArrayRef<Expr*> Args,
756 bool HadMultipleCandidates,
757 bool ListInitialization,
758 bool StdInitListInitialization,
759 bool ZeroInitialization,
760 ConstructionKind ConstructKind,
761 SourceRange ParenOrBraceRange) {
762 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc,
763 Ctor, Elidable, Args,
764 HadMultipleCandidates, ListInitialization,
765 StdInitListInitialization,
766 ZeroInitialization, ConstructKind,
770 CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
771 QualType T, SourceLocation Loc,
772 CXXConstructorDecl *Ctor,
774 ArrayRef<Expr*> Args,
775 bool HadMultipleCandidates,
776 bool ListInitialization,
777 bool StdInitListInitialization,
778 bool ZeroInitialization,
779 ConstructionKind ConstructKind,
780 SourceRange ParenOrBraceRange)
781 : Expr(SC, T, VK_RValue, OK_Ordinary,
782 T->isDependentType(), T->isDependentType(),
783 T->isInstantiationDependentType(),
784 T->containsUnexpandedParameterPack()),
785 Constructor(Ctor), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
786 NumArgs(Args.size()),
787 Elidable(Elidable), HadMultipleCandidates(HadMultipleCandidates),
788 ListInitialization(ListInitialization),
789 StdInitListInitialization(StdInitListInitialization),
790 ZeroInitialization(ZeroInitialization),
791 ConstructKind(ConstructKind), Args(nullptr)
794 this->Args = new (C) Stmt*[Args.size()];
796 for (unsigned i = 0; i != Args.size(); ++i) {
797 assert(Args[i] && "NULL argument in CXXConstructExpr");
799 if (Args[i]->isValueDependent())
800 ExprBits.ValueDependent = true;
801 if (Args[i]->isInstantiationDependent())
802 ExprBits.InstantiationDependent = true;
803 if (Args[i]->containsUnexpandedParameterPack())
804 ExprBits.ContainsUnexpandedParameterPack = true;
806 this->Args[i] = Args[i];
811 LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
812 LambdaCaptureKind Kind, VarDecl *Var,
813 SourceLocation EllipsisLoc)
814 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
818 Bits |= Capture_Implicit;
822 Bits |= Capture_ByCopy;
825 assert(!Var && "'this' capture cannot have a variable!");
826 Bits |= Capture_This;
830 Bits |= Capture_ByCopy;
833 assert(Var && "capture must have a variable!");
836 assert(!Var && "VLA type capture cannot have a variable!");
839 DeclAndBits.setInt(Bits);
842 LambdaCaptureKind LambdaCapture::getCaptureKind() const {
843 if (capturesVLAType())
845 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
847 return CapByCopy ? LCK_StarThis : LCK_This;
848 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
851 LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
852 LambdaCaptureDefault CaptureDefault,
853 SourceLocation CaptureDefaultLoc,
854 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
855 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
856 ArrayRef<VarDecl *> ArrayIndexVars,
857 ArrayRef<unsigned> ArrayIndexStarts,
858 SourceLocation ClosingBrace,
859 bool ContainsUnexpandedParameterPack)
860 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
861 T->isDependentType(), T->isDependentType(),
862 ContainsUnexpandedParameterPack),
863 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
864 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
865 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
866 ClosingBrace(ClosingBrace) {
867 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
868 CXXRecordDecl *Class = getLambdaClass();
869 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
871 // FIXME: Propagate "has unexpanded parameter pack" bit.
874 const ASTContext &Context = Class->getASTContext();
875 Data.NumCaptures = NumCaptures;
876 Data.NumExplicitCaptures = 0;
878 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
879 LambdaCapture *ToCapture = Data.Captures;
880 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
881 if (Captures[I].isExplicit())
882 ++Data.NumExplicitCaptures;
884 *ToCapture++ = Captures[I];
887 // Copy initialization expressions for the non-static data members.
888 Stmt **Stored = getStoredStmts();
889 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
890 *Stored++ = CaptureInits[I];
892 // Copy the body of the lambda.
893 *Stored++ = getCallOperator()->getBody();
895 // Copy the array index variables, if any.
896 HasArrayIndexVars = !ArrayIndexVars.empty();
897 if (HasArrayIndexVars) {
898 assert(ArrayIndexStarts.size() == NumCaptures);
899 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
900 sizeof(VarDecl *) * ArrayIndexVars.size());
901 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
902 sizeof(unsigned) * Captures.size());
903 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
907 LambdaExpr *LambdaExpr::Create(
908 const ASTContext &Context, CXXRecordDecl *Class,
909 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
910 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
911 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
912 ArrayRef<VarDecl *> ArrayIndexVars, ArrayRef<unsigned> ArrayIndexStarts,
913 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
914 // Determine the type of the expression (i.e., the type of the
915 // function object we're creating).
916 QualType T = Context.getTypeDeclType(Class);
918 unsigned Size = totalSizeToAlloc<Stmt *, unsigned, VarDecl *>(
919 Captures.size() + 1, ArrayIndexVars.empty() ? 0 : Captures.size() + 1,
920 ArrayIndexVars.size());
921 void *Mem = Context.Allocate(Size);
922 return new (Mem) LambdaExpr(T, IntroducerRange,
923 CaptureDefault, CaptureDefaultLoc, Captures,
924 ExplicitParams, ExplicitResultType,
925 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
926 ClosingBrace, ContainsUnexpandedParameterPack);
929 LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
930 unsigned NumCaptures,
931 unsigned NumArrayIndexVars) {
932 unsigned Size = totalSizeToAlloc<Stmt *, unsigned, VarDecl *>(
933 NumCaptures + 1, NumArrayIndexVars ? NumCaptures + 1 : 0,
935 void *Mem = C.Allocate(Size);
936 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
939 bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
940 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
941 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
944 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
945 return getLambdaClass()->getLambdaData().Captures;
948 LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
949 return capture_begin() + NumCaptures;
952 LambdaExpr::capture_range LambdaExpr::captures() const {
953 return capture_range(capture_begin(), capture_end());
956 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
957 return capture_begin();
960 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
961 struct CXXRecordDecl::LambdaDefinitionData &Data
962 = getLambdaClass()->getLambdaData();
963 return Data.Captures + Data.NumExplicitCaptures;
966 LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
967 return capture_range(explicit_capture_begin(), explicit_capture_end());
970 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
971 return explicit_capture_end();
974 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
975 return capture_end();
978 LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
979 return capture_range(implicit_capture_begin(), implicit_capture_end());
983 LambdaExpr::getCaptureInitIndexVars(const_capture_init_iterator Iter) const {
984 assert(HasArrayIndexVars && "No array index-var data?");
986 unsigned Index = Iter - capture_init_begin();
987 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
988 "Capture index out-of-range");
989 VarDecl *const *IndexVars = getArrayIndexVars();
990 const unsigned *IndexStarts = getArrayIndexStarts();
991 return llvm::makeArrayRef(IndexVars + IndexStarts[Index],
992 IndexVars + IndexStarts[Index + 1]);
995 CXXRecordDecl *LambdaExpr::getLambdaClass() const {
996 return getType()->getAsCXXRecordDecl();
999 CXXMethodDecl *LambdaExpr::getCallOperator() const {
1000 CXXRecordDecl *Record = getLambdaClass();
1001 return Record->getLambdaCallOperator();
1004 TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1005 CXXRecordDecl *Record = getLambdaClass();
1006 return Record->getGenericLambdaTemplateParameterList();
1010 CompoundStmt *LambdaExpr::getBody() const {
1011 // FIXME: this mutation in getBody is bogus. It should be
1012 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1013 // don't understand, that doesn't work.
1014 if (!getStoredStmts()[NumCaptures])
1015 *const_cast<clang::Stmt **>(&getStoredStmts()[NumCaptures]) =
1016 getCallOperator()->getBody();
1018 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1021 bool LambdaExpr::isMutable() const {
1022 return !getCallOperator()->isConst();
1025 ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1026 bool CleanupsHaveSideEffects,
1027 ArrayRef<CleanupObject> objects)
1028 : Expr(ExprWithCleanupsClass, subexpr->getType(),
1029 subexpr->getValueKind(), subexpr->getObjectKind(),
1030 subexpr->isTypeDependent(), subexpr->isValueDependent(),
1031 subexpr->isInstantiationDependent(),
1032 subexpr->containsUnexpandedParameterPack()),
1034 ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
1035 ExprWithCleanupsBits.NumObjects = objects.size();
1036 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1037 getTrailingObjects<CleanupObject>()[i] = objects[i];
1040 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
1041 bool CleanupsHaveSideEffects,
1042 ArrayRef<CleanupObject> objects) {
1043 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
1044 llvm::alignOf<ExprWithCleanups>());
1046 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
1049 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1050 : Expr(ExprWithCleanupsClass, empty) {
1051 ExprWithCleanupsBits.NumObjects = numObjects;
1054 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1056 unsigned numObjects) {
1057 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
1058 llvm::alignOf<ExprWithCleanups>());
1059 return new (buffer) ExprWithCleanups(empty, numObjects);
1062 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
1063 SourceLocation LParenLoc,
1064 ArrayRef<Expr*> Args,
1065 SourceLocation RParenLoc)
1066 : Expr(CXXUnresolvedConstructExprClass,
1067 Type->getType().getNonReferenceType(),
1068 (Type->getType()->isLValueReferenceType() ? VK_LValue
1069 :Type->getType()->isRValueReferenceType()? VK_XValue
1072 Type->getType()->isDependentType(), true, true,
1073 Type->getType()->containsUnexpandedParameterPack()),
1075 LParenLoc(LParenLoc),
1076 RParenLoc(RParenLoc),
1077 NumArgs(Args.size()) {
1078 Expr **StoredArgs = getTrailingObjects<Expr *>();
1079 for (unsigned I = 0; I != Args.size(); ++I) {
1080 if (Args[I]->containsUnexpandedParameterPack())
1081 ExprBits.ContainsUnexpandedParameterPack = true;
1083 StoredArgs[I] = Args[I];
1087 CXXUnresolvedConstructExpr *
1088 CXXUnresolvedConstructExpr::Create(const ASTContext &C,
1089 TypeSourceInfo *Type,
1090 SourceLocation LParenLoc,
1091 ArrayRef<Expr*> Args,
1092 SourceLocation RParenLoc) {
1093 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
1094 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
1097 CXXUnresolvedConstructExpr *
1098 CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
1099 Stmt::EmptyShell Empty;
1100 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
1101 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1104 SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1105 return Type->getTypeLoc().getBeginLoc();
1108 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1109 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1110 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1111 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1112 DeclarationNameInfo MemberNameInfo,
1113 const TemplateArgumentListInfo *TemplateArgs)
1114 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1115 OK_Ordinary, true, true, true,
1116 ((Base && Base->containsUnexpandedParameterPack()) ||
1118 QualifierLoc.getNestedNameSpecifier()
1119 ->containsUnexpandedParameterPack()) ||
1120 MemberNameInfo.containsUnexpandedParameterPack())),
1121 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1122 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1123 TemplateKWLoc.isValid()),
1124 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1125 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1126 MemberNameInfo(MemberNameInfo) {
1128 bool Dependent = true;
1129 bool InstantiationDependent = true;
1130 bool ContainsUnexpandedParameterPack = false;
1131 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1132 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1133 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
1134 if (ContainsUnexpandedParameterPack)
1135 ExprBits.ContainsUnexpandedParameterPack = true;
1136 } else if (TemplateKWLoc.isValid()) {
1137 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1142 CXXDependentScopeMemberExpr *
1143 CXXDependentScopeMemberExpr::Create(const ASTContext &C,
1144 Expr *Base, QualType BaseType, bool IsArrow,
1145 SourceLocation OperatorLoc,
1146 NestedNameSpecifierLoc QualifierLoc,
1147 SourceLocation TemplateKWLoc,
1148 NamedDecl *FirstQualifierFoundInScope,
1149 DeclarationNameInfo MemberNameInfo,
1150 const TemplateArgumentListInfo *TemplateArgs) {
1151 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1152 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1154 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1155 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1157 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
1158 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1159 IsArrow, OperatorLoc,
1162 FirstQualifierFoundInScope,
1163 MemberNameInfo, TemplateArgs);
1166 CXXDependentScopeMemberExpr *
1167 CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
1168 bool HasTemplateKWAndArgsInfo,
1169 unsigned NumTemplateArgs) {
1170 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1172 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1173 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1174 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
1175 CXXDependentScopeMemberExpr *E
1176 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
1177 0, SourceLocation(),
1178 NestedNameSpecifierLoc(),
1179 SourceLocation(), nullptr,
1180 DeclarationNameInfo(), nullptr);
1181 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
1185 bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1189 return cast<Expr>(Base)->isImplicitCXXThis();
1192 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1193 UnresolvedSetIterator end) {
1195 NamedDecl *decl = *begin;
1196 if (isa<UnresolvedUsingValueDecl>(decl))
1199 // Unresolved member expressions should only contain methods and
1200 // method templates.
1201 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1204 } while (++begin != end);
1209 UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
1210 bool HasUnresolvedUsing,
1211 Expr *Base, QualType BaseType,
1213 SourceLocation OperatorLoc,
1214 NestedNameSpecifierLoc QualifierLoc,
1215 SourceLocation TemplateKWLoc,
1216 const DeclarationNameInfo &MemberNameInfo,
1217 const TemplateArgumentListInfo *TemplateArgs,
1218 UnresolvedSetIterator Begin,
1219 UnresolvedSetIterator End)
1220 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1221 MemberNameInfo, TemplateArgs, Begin, End,
1223 ((Base && Base->isTypeDependent()) ||
1224 BaseType->isDependentType()),
1225 ((Base && Base->isInstantiationDependent()) ||
1226 BaseType->isInstantiationDependentType()),
1227 // Contains unexpanded parameter pack
1228 ((Base && Base->containsUnexpandedParameterPack()) ||
1229 BaseType->containsUnexpandedParameterPack())),
1230 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1231 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
1233 // Check whether all of the members are non-static member functions,
1234 // and if so, mark give this bound-member type instead of overload type.
1235 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1236 setType(C.BoundMemberTy);
1239 bool UnresolvedMemberExpr::isImplicitAccess() const {
1243 return cast<Expr>(Base)->isImplicitCXXThis();
1246 UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1247 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1248 bool IsArrow, SourceLocation OperatorLoc,
1249 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1250 const DeclarationNameInfo &MemberNameInfo,
1251 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1252 UnresolvedSetIterator End) {
1253 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1255 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1256 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
1258 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
1259 return new (Mem) UnresolvedMemberExpr(
1260 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1261 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
1264 UnresolvedMemberExpr *
1265 UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1266 bool HasTemplateKWAndArgsInfo,
1267 unsigned NumTemplateArgs) {
1268 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1270 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1271 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1273 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
1274 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
1275 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
1279 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1280 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1282 // If there was a nested name specifier, it names the naming class.
1283 // It can't be dependent: after all, we were actually able to do the
1285 CXXRecordDecl *Record = nullptr;
1286 auto *NNS = getQualifier();
1287 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
1288 const Type *T = getQualifier()->getAsType();
1289 assert(T && "qualifier in member expression does not name type");
1290 Record = T->getAsCXXRecordDecl();
1291 assert(Record && "qualifier in member expression does not name record");
1293 // Otherwise the naming class must have been the base class.
1295 QualType BaseType = getBaseType().getNonReferenceType();
1297 const PointerType *PT = BaseType->getAs<PointerType>();
1298 assert(PT && "base of arrow member access is not pointer");
1299 BaseType = PT->getPointeeType();
1302 Record = BaseType->getAsCXXRecordDecl();
1303 assert(Record && "base of member expression does not name record");
1310 SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1311 NamedDecl *Pack, SourceLocation PackLoc,
1312 SourceLocation RParenLoc,
1313 Optional<unsigned> Length,
1314 ArrayRef<TemplateArgument> PartialArgs) {
1316 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
1317 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1318 PackLoc, RParenLoc, Length, PartialArgs);
1321 SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1322 unsigned NumPartialArgs) {
1324 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
1325 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1328 SubstNonTypeTemplateParmPackExpr::
1329 SubstNonTypeTemplateParmPackExpr(QualType T,
1330 NonTypeTemplateParmDecl *Param,
1331 SourceLocation NameLoc,
1332 const TemplateArgument &ArgPack)
1333 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
1334 true, true, true, true),
1335 Param(Param), Arguments(ArgPack.pack_begin()),
1336 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1338 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1339 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
1342 FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1343 SourceLocation NameLoc,
1345 ParmVarDecl *const *Params)
1346 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1348 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1350 std::uninitialized_copy(Params, Params + NumParams,
1351 getTrailingObjects<ParmVarDecl *>());
1354 FunctionParmPackExpr *
1355 FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
1356 ParmVarDecl *ParamPack, SourceLocation NameLoc,
1357 ArrayRef<ParmVarDecl *> Params) {
1358 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(Params.size())))
1359 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1362 FunctionParmPackExpr *
1363 FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1364 unsigned NumParams) {
1365 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(NumParams)))
1366 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
1369 void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1370 unsigned ManglingNumber) {
1371 // We only need extra state if we have to remember more than just the Stmt.
1375 // We may need to allocate extra storage for the mangling number and the
1376 // extended-by ValueDecl.
1377 if (!State.is<ExtraState *>()) {
1378 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1379 ES->Temporary = State.get<Stmt *>();
1383 auto ES = State.get<ExtraState *>();
1384 ES->ExtendingDecl = ExtendedBy;
1385 ES->ManglingNumber = ManglingNumber;
1388 TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1389 ArrayRef<TypeSourceInfo *> Args,
1390 SourceLocation RParenLoc,
1392 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1393 /*TypeDependent=*/false,
1394 /*ValueDependent=*/false,
1395 /*InstantiationDependent=*/false,
1396 /*ContainsUnexpandedParameterPack=*/false),
1397 Loc(Loc), RParenLoc(RParenLoc)
1399 TypeTraitExprBits.Kind = Kind;
1400 TypeTraitExprBits.Value = Value;
1401 TypeTraitExprBits.NumArgs = Args.size();
1403 TypeSourceInfo **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1405 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1406 if (Args[I]->getType()->isDependentType())
1407 setValueDependent(true);
1408 if (Args[I]->getType()->isInstantiationDependentType())
1409 setInstantiationDependent(true);
1410 if (Args[I]->getType()->containsUnexpandedParameterPack())
1411 setContainsUnexpandedParameterPack(true);
1413 ToArgs[I] = Args[I];
1417 TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
1420 ArrayRef<TypeSourceInfo *> Args,
1421 SourceLocation RParenLoc,
1423 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
1424 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1427 TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
1429 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
1430 return new (Mem) TypeTraitExpr(EmptyShell());
1433 void ArrayTypeTraitExpr::anchor() { }