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/ExprCXX.h"
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Attr.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/DeclarationName.h"
22 #include "clang/AST/Expr.h"
23 #include "clang/AST/LambdaCapture.h"
24 #include "clang/AST/NestedNameSpecifier.h"
25 #include "clang/AST/TemplateBase.h"
26 #include "clang/AST/Type.h"
27 #include "clang/AST/TypeLoc.h"
28 #include "clang/Basic/LLVM.h"
29 #include "clang/Basic/OperatorKinds.h"
30 #include "clang/Basic/SourceLocation.h"
31 #include "clang/Basic/Specifiers.h"
32 #include "llvm/ADT/ArrayRef.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/ErrorHandling.h"
40 using namespace clang;
42 //===----------------------------------------------------------------------===//
43 // Child Iterators for iterating over subexpressions/substatements
44 //===----------------------------------------------------------------------===//
46 bool CXXOperatorCallExpr::isInfixBinaryOp() const {
47 // An infix binary operator is any operator with two arguments other than
48 // operator() and operator[]. Note that none of these operators can have
49 // default arguments, so it suffices to check the number of argument
51 if (getNumArgs() != 2)
54 switch (getOperator()) {
55 case OO_Call: case OO_Subscript:
62 bool CXXTypeidExpr::isPotentiallyEvaluated() const {
66 // C++11 [expr.typeid]p3:
67 // When typeid is applied to an expression other than a glvalue of
68 // polymorphic class type, [...] the expression is an unevaluated operand.
69 const Expr *E = getExprOperand();
70 if (const CXXRecordDecl *RD = E->getType()->getAsCXXRecordDecl())
71 if (RD->isPolymorphic() && E->isGLValue())
77 QualType CXXTypeidExpr::getTypeOperand(ASTContext &Context) const {
78 assert(isTypeOperand() && "Cannot call getTypeOperand for typeid(expr)");
80 return Context.getUnqualifiedArrayType(
81 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
84 QualType CXXUuidofExpr::getTypeOperand(ASTContext &Context) const {
85 assert(isTypeOperand() && "Cannot call getTypeOperand for __uuidof(expr)");
87 return Context.getUnqualifiedArrayType(
88 Operand.get<TypeSourceInfo *>()->getType().getNonReferenceType(), Quals);
91 // CXXScalarValueInitExpr
92 SourceLocation CXXScalarValueInitExpr::getLocStart() const {
93 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
97 CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
98 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
99 bool PassAlignment, bool usualArrayDeleteWantsSize,
100 ArrayRef<Expr*> placementArgs,
101 SourceRange typeIdParens, Expr *arraySize,
102 InitializationStyle initializationStyle,
103 Expr *initializer, QualType ty,
104 TypeSourceInfo *allocatedTypeInfo,
105 SourceRange Range, SourceRange directInitRange)
106 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary, ty->isDependentType(),
107 ty->isDependentType(), ty->isInstantiationDependentType(),
108 ty->containsUnexpandedParameterPack()),
109 OperatorNew(operatorNew), OperatorDelete(operatorDelete),
110 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
111 Range(Range), DirectInitRange(directInitRange), GlobalNew(globalNew),
112 PassAlignment(PassAlignment),
113 UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
114 assert((initializer != nullptr || initializationStyle == NoInit) &&
115 "Only NoInit can have no initializer.");
116 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
117 AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(),
118 initializer != nullptr);
121 if (arraySize->isInstantiationDependent())
122 ExprBits.InstantiationDependent = true;
124 if (arraySize->containsUnexpandedParameterPack())
125 ExprBits.ContainsUnexpandedParameterPack = true;
127 SubExprs[i++] = arraySize;
131 if (initializer->isInstantiationDependent())
132 ExprBits.InstantiationDependent = true;
134 if (initializer->containsUnexpandedParameterPack())
135 ExprBits.ContainsUnexpandedParameterPack = true;
137 SubExprs[i++] = initializer;
140 for (unsigned j = 0; j != placementArgs.size(); ++j) {
141 if (placementArgs[j]->isInstantiationDependent())
142 ExprBits.InstantiationDependent = true;
143 if (placementArgs[j]->containsUnexpandedParameterPack())
144 ExprBits.ContainsUnexpandedParameterPack = true;
146 SubExprs[i++] = placementArgs[j];
149 switch (getInitializationStyle()) {
151 this->Range.setEnd(DirectInitRange.getEnd()); break;
153 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
155 if (TypeIdParens.isValid())
156 this->Range.setEnd(TypeIdParens.getEnd());
161 void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
162 unsigned numPlaceArgs, bool hasInitializer){
163 assert(SubExprs == nullptr && "SubExprs already allocated");
165 NumPlacementArgs = numPlaceArgs;
167 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
168 SubExprs = new (C) Stmt*[TotalSize];
171 bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
172 return getOperatorNew()->getType()->castAs<FunctionProtoType>()->isNothrow(
174 !getOperatorNew()->isReservedGlobalPlacementOperator();
178 QualType CXXDeleteExpr::getDestroyedType() const {
179 const Expr *Arg = getArgument();
181 // For a destroying operator delete, we may have implicitly converted the
182 // pointer type to the type of the parameter of the 'operator delete'
184 while (auto *ICE = dyn_cast<ImplicitCastExpr>(Arg)) {
185 if (ICE->getCastKind() == CK_DerivedToBase ||
186 ICE->getCastKind() == CK_UncheckedDerivedToBase ||
187 ICE->getCastKind() == CK_NoOp) {
188 assert((ICE->getCastKind() == CK_NoOp ||
189 getOperatorDelete()->isDestroyingOperatorDelete()) &&
190 "only a destroying operator delete can have a converted arg");
191 Arg = ICE->getSubExpr();
196 // The type-to-delete may not be a pointer if it's a dependent type.
197 const QualType ArgType = Arg->getType();
199 if (ArgType->isDependentType() && !ArgType->isPointerType())
202 return ArgType->getAs<PointerType>()->getPointeeType();
205 // CXXPseudoDestructorExpr
206 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
208 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
211 CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
212 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
213 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
214 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
215 PseudoDestructorTypeStorage DestroyedType)
216 : Expr(CXXPseudoDestructorExprClass,
217 Context.BoundMemberTy,
218 VK_RValue, OK_Ordinary,
219 /*isTypeDependent=*/(Base->isTypeDependent() ||
220 (DestroyedType.getTypeSourceInfo() &&
221 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
222 /*isValueDependent=*/Base->isValueDependent(),
223 (Base->isInstantiationDependent() ||
225 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
227 ScopeType->getType()->isInstantiationDependentType()) ||
228 (DestroyedType.getTypeSourceInfo() &&
229 DestroyedType.getTypeSourceInfo()->getType()
230 ->isInstantiationDependentType())),
231 // ContainsUnexpandedParameterPack
232 (Base->containsUnexpandedParameterPack() ||
234 QualifierLoc.getNestedNameSpecifier()
235 ->containsUnexpandedParameterPack()) ||
237 ScopeType->getType()->containsUnexpandedParameterPack()) ||
238 (DestroyedType.getTypeSourceInfo() &&
239 DestroyedType.getTypeSourceInfo()->getType()
240 ->containsUnexpandedParameterPack()))),
241 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
242 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
243 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
244 DestroyedType(DestroyedType) {}
246 QualType CXXPseudoDestructorExpr::getDestroyedType() const {
247 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
248 return TInfo->getType();
253 SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
254 SourceLocation End = DestroyedType.getLocation();
255 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
256 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
260 // UnresolvedLookupExpr
261 UnresolvedLookupExpr *
262 UnresolvedLookupExpr::Create(const ASTContext &C,
263 CXXRecordDecl *NamingClass,
264 NestedNameSpecifierLoc QualifierLoc,
265 SourceLocation TemplateKWLoc,
266 const DeclarationNameInfo &NameInfo,
268 const TemplateArgumentListInfo *Args,
269 UnresolvedSetIterator Begin,
270 UnresolvedSetIterator End) {
271 assert(Args || TemplateKWLoc.isValid());
272 unsigned num_args = Args ? Args->size() : 0;
275 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(1,
277 void *Mem = C.Allocate(Size, alignof(UnresolvedLookupExpr));
278 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
279 TemplateKWLoc, NameInfo,
280 ADL, /*Overload*/ true, Args,
284 UnresolvedLookupExpr *
285 UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
286 bool HasTemplateKWAndArgsInfo,
287 unsigned NumTemplateArgs) {
288 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
290 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
291 HasTemplateKWAndArgsInfo, NumTemplateArgs);
292 void *Mem = C.Allocate(Size, alignof(UnresolvedLookupExpr));
293 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
294 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
298 OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
299 NestedNameSpecifierLoc QualifierLoc,
300 SourceLocation TemplateKWLoc,
301 const DeclarationNameInfo &NameInfo,
302 const TemplateArgumentListInfo *TemplateArgs,
303 UnresolvedSetIterator Begin,
304 UnresolvedSetIterator End,
306 bool KnownInstantiationDependent,
307 bool KnownContainsUnexpandedParameterPack)
308 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
310 (KnownInstantiationDependent ||
311 NameInfo.isInstantiationDependent() ||
313 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
314 (KnownContainsUnexpandedParameterPack ||
315 NameInfo.containsUnexpandedParameterPack() ||
317 QualifierLoc.getNestedNameSpecifier()
318 ->containsUnexpandedParameterPack()))),
319 NameInfo(NameInfo), QualifierLoc(QualifierLoc), NumResults(End - Begin),
320 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
321 TemplateKWLoc.isValid()) {
322 NumResults = End - Begin;
324 // Determine whether this expression is type-dependent.
325 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
326 if ((*I)->getDeclContext()->isDependentContext() ||
327 isa<UnresolvedUsingValueDecl>(*I)) {
328 ExprBits.TypeDependent = true;
329 ExprBits.ValueDependent = true;
330 ExprBits.InstantiationDependent = true;
334 Results = static_cast<DeclAccessPair *>(C.Allocate(
335 sizeof(DeclAccessPair) * NumResults, alignof(DeclAccessPair)));
336 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
339 // If we have explicit template arguments, check for dependent
340 // template arguments and whether they contain any unexpanded pack
343 bool Dependent = false;
344 bool InstantiationDependent = false;
345 bool ContainsUnexpandedParameterPack = false;
346 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
347 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
348 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
351 ExprBits.TypeDependent = true;
352 ExprBits.ValueDependent = true;
354 if (InstantiationDependent)
355 ExprBits.InstantiationDependent = true;
356 if (ContainsUnexpandedParameterPack)
357 ExprBits.ContainsUnexpandedParameterPack = true;
358 } else if (TemplateKWLoc.isValid()) {
359 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
362 if (isTypeDependent())
363 setType(C.DependentTy);
366 void OverloadExpr::initializeResults(const ASTContext &C,
367 UnresolvedSetIterator Begin,
368 UnresolvedSetIterator End) {
369 assert(!Results && "Results already initialized!");
370 NumResults = End - Begin;
372 Results = static_cast<DeclAccessPair *>(
373 C.Allocate(sizeof(DeclAccessPair) * NumResults,
375 alignof(DeclAccessPair)));
376 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
380 CXXRecordDecl *OverloadExpr::getNamingClass() const {
381 if (isa<UnresolvedLookupExpr>(this))
382 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
384 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
387 // DependentScopeDeclRefExpr
388 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
389 NestedNameSpecifierLoc QualifierLoc,
390 SourceLocation TemplateKWLoc,
391 const DeclarationNameInfo &NameInfo,
392 const TemplateArgumentListInfo *Args)
393 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
395 (NameInfo.isInstantiationDependent() ||
397 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
398 (NameInfo.containsUnexpandedParameterPack() ||
400 QualifierLoc.getNestedNameSpecifier()
401 ->containsUnexpandedParameterPack()))),
402 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
403 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
406 bool Dependent = true;
407 bool InstantiationDependent = true;
408 bool ContainsUnexpandedParameterPack
409 = ExprBits.ContainsUnexpandedParameterPack;
410 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
411 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
412 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
413 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
414 } else if (TemplateKWLoc.isValid()) {
415 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
420 DependentScopeDeclRefExpr *
421 DependentScopeDeclRefExpr::Create(const ASTContext &C,
422 NestedNameSpecifierLoc QualifierLoc,
423 SourceLocation TemplateKWLoc,
424 const DeclarationNameInfo &NameInfo,
425 const TemplateArgumentListInfo *Args) {
426 assert(QualifierLoc && "should be created for dependent qualifiers");
427 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
429 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
430 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
431 void *Mem = C.Allocate(Size);
432 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
433 TemplateKWLoc, NameInfo, Args);
436 DependentScopeDeclRefExpr *
437 DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
438 bool HasTemplateKWAndArgsInfo,
439 unsigned NumTemplateArgs) {
440 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
442 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
443 HasTemplateKWAndArgsInfo, NumTemplateArgs);
444 void *Mem = C.Allocate(Size);
445 DependentScopeDeclRefExpr *E
446 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
448 DeclarationNameInfo(), nullptr);
449 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
453 SourceLocation CXXConstructExpr::getLocStart() const {
454 if (isa<CXXTemporaryObjectExpr>(this))
455 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
459 SourceLocation CXXConstructExpr::getLocEnd() const {
460 if (isa<CXXTemporaryObjectExpr>(this))
461 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
463 if (ParenOrBraceRange.isValid())
464 return ParenOrBraceRange.getEnd();
466 SourceLocation End = Loc;
467 for (unsigned I = getNumArgs(); I > 0; --I) {
468 const Expr *Arg = getArg(I-1);
469 if (!Arg->isDefaultArgument()) {
470 SourceLocation NewEnd = Arg->getLocEnd();
471 if (NewEnd.isValid()) {
481 SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
482 OverloadedOperatorKind Kind = getOperator();
483 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
484 if (getNumArgs() == 1)
486 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
489 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
490 } else if (Kind == OO_Arrow) {
491 return getArg(0)->getSourceRange();
492 } else if (Kind == OO_Call) {
493 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
494 } else if (Kind == OO_Subscript) {
495 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
496 } else if (getNumArgs() == 1) {
497 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
498 } else if (getNumArgs() == 2) {
499 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
501 return getOperatorLoc();
505 Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
506 const Expr *Callee = getCallee()->IgnoreParens();
507 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
508 return MemExpr->getBase();
509 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
510 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
513 // FIXME: Will eventually need to cope with member pointers.
517 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
518 if (const MemberExpr *MemExpr =
519 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
520 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
522 // FIXME: Will eventually need to cope with member pointers.
526 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
527 Expr* ThisArg = getImplicitObjectArgument();
531 if (ThisArg->getType()->isAnyPointerType())
532 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
534 return ThisArg->getType()->getAsCXXRecordDecl();
537 //===----------------------------------------------------------------------===//
539 //===----------------------------------------------------------------------===//
541 /// getCastName - Get the name of the C++ cast being used, e.g.,
542 /// "static_cast", "dynamic_cast", "reinterpret_cast", or
543 /// "const_cast". The returned pointer must not be freed.
544 const char *CXXNamedCastExpr::getCastName() const {
545 switch (getStmtClass()) {
546 case CXXStaticCastExprClass: return "static_cast";
547 case CXXDynamicCastExprClass: return "dynamic_cast";
548 case CXXReinterpretCastExprClass: return "reinterpret_cast";
549 case CXXConstCastExprClass: return "const_cast";
550 default: return "<invalid cast>";
554 CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
556 CastKind K, Expr *Op,
557 const CXXCastPath *BasePath,
558 TypeSourceInfo *WrittenTy,
560 SourceLocation RParenLoc,
561 SourceRange AngleBrackets) {
562 unsigned PathSize = (BasePath ? BasePath->size() : 0);
563 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
564 CXXStaticCastExpr *E =
565 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
566 RParenLoc, AngleBrackets);
568 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
569 E->getTrailingObjects<CXXBaseSpecifier *>());
573 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
575 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
576 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
579 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
581 CastKind K, Expr *Op,
582 const CXXCastPath *BasePath,
583 TypeSourceInfo *WrittenTy,
585 SourceLocation RParenLoc,
586 SourceRange AngleBrackets) {
587 unsigned PathSize = (BasePath ? BasePath->size() : 0);
588 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
589 CXXDynamicCastExpr *E =
590 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
591 RParenLoc, AngleBrackets);
593 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
594 E->getTrailingObjects<CXXBaseSpecifier *>());
598 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
600 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
601 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
604 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
605 /// to always be null. For example:
608 /// struct B final : A { };
611 /// C *f(B* b) { return dynamic_cast<C*>(b); }
612 bool CXXDynamicCastExpr::isAlwaysNull() const
614 QualType SrcType = getSubExpr()->getType();
615 QualType DestType = getType();
617 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
618 SrcType = SrcPTy->getPointeeType();
619 DestType = DestType->castAs<PointerType>()->getPointeeType();
622 if (DestType->isVoidType())
625 const CXXRecordDecl *SrcRD =
626 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
628 if (!SrcRD->hasAttr<FinalAttr>())
631 const CXXRecordDecl *DestRD =
632 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
634 return !DestRD->isDerivedFrom(SrcRD);
637 CXXReinterpretCastExpr *
638 CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
639 ExprValueKind VK, CastKind K, Expr *Op,
640 const CXXCastPath *BasePath,
641 TypeSourceInfo *WrittenTy, SourceLocation L,
642 SourceLocation RParenLoc,
643 SourceRange AngleBrackets) {
644 unsigned PathSize = (BasePath ? BasePath->size() : 0);
645 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
646 CXXReinterpretCastExpr *E =
647 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
648 RParenLoc, AngleBrackets);
650 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
651 E->getTrailingObjects<CXXBaseSpecifier *>());
655 CXXReinterpretCastExpr *
656 CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
657 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
658 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
661 CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
662 ExprValueKind VK, Expr *Op,
663 TypeSourceInfo *WrittenTy,
665 SourceLocation RParenLoc,
666 SourceRange AngleBrackets) {
667 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
670 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
671 return new (C) CXXConstCastExpr(EmptyShell());
674 CXXFunctionalCastExpr *
675 CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
676 TypeSourceInfo *Written, CastKind K, Expr *Op,
677 const CXXCastPath *BasePath,
678 SourceLocation L, SourceLocation R) {
679 unsigned PathSize = (BasePath ? BasePath->size() : 0);
680 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
681 CXXFunctionalCastExpr *E =
682 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
684 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
685 E->getTrailingObjects<CXXBaseSpecifier *>());
689 CXXFunctionalCastExpr *
690 CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
691 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
692 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
695 SourceLocation CXXFunctionalCastExpr::getLocStart() const {
696 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
699 SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
700 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
703 UserDefinedLiteral::LiteralOperatorKind
704 UserDefinedLiteral::getLiteralOperatorKind() const {
705 if (getNumArgs() == 0)
707 if (getNumArgs() == 2)
710 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
712 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
713 if (ParamTy->isPointerType())
715 if (ParamTy->isAnyCharacterType())
716 return LOK_Character;
717 if (ParamTy->isIntegerType())
719 if (ParamTy->isFloatingType())
722 llvm_unreachable("unknown kind of literal operator");
725 Expr *UserDefinedLiteral::getCookedLiteral() {
727 LiteralOperatorKind LOK = getLiteralOperatorKind();
728 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
733 const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
734 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
737 CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
738 FieldDecl *Field, QualType T)
739 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
740 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
743 /*FIXME*/ OK_Ordinary, false, false, false, false),
744 Field(Field), Loc(Loc) {
745 assert(Field->hasInClassInitializer());
748 CXXTemporary *CXXTemporary::Create(const ASTContext &C,
749 const CXXDestructorDecl *Destructor) {
750 return new (C) CXXTemporary(Destructor);
753 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
756 assert((SubExpr->getType()->isRecordType() ||
757 SubExpr->getType()->isArrayType()) &&
758 "Expression bound to a temporary must have record or array type!");
760 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
763 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
764 CXXConstructorDecl *Cons,
767 ArrayRef<Expr*> Args,
768 SourceRange ParenOrBraceRange,
769 bool HadMultipleCandidates,
770 bool ListInitialization,
771 bool StdInitListInitialization,
772 bool ZeroInitialization)
773 : CXXConstructExpr(C, CXXTemporaryObjectExprClass, Type,
774 TSI->getTypeLoc().getBeginLoc(), Cons, false, Args,
775 HadMultipleCandidates, ListInitialization,
776 StdInitListInitialization, ZeroInitialization,
777 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
780 SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
781 return Type->getTypeLoc().getBeginLoc();
784 SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
785 SourceLocation Loc = getParenOrBraceRange().getEnd();
786 if (Loc.isInvalid() && getNumArgs())
787 Loc = getArg(getNumArgs()-1)->getLocEnd();
791 CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
793 CXXConstructorDecl *Ctor,
795 ArrayRef<Expr*> Args,
796 bool HadMultipleCandidates,
797 bool ListInitialization,
798 bool StdInitListInitialization,
799 bool ZeroInitialization,
800 ConstructionKind ConstructKind,
801 SourceRange ParenOrBraceRange) {
802 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc,
803 Ctor, Elidable, Args,
804 HadMultipleCandidates, ListInitialization,
805 StdInitListInitialization,
806 ZeroInitialization, ConstructKind,
810 CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
811 QualType T, SourceLocation Loc,
812 CXXConstructorDecl *Ctor,
814 ArrayRef<Expr*> Args,
815 bool HadMultipleCandidates,
816 bool ListInitialization,
817 bool StdInitListInitialization,
818 bool ZeroInitialization,
819 ConstructionKind ConstructKind,
820 SourceRange ParenOrBraceRange)
821 : Expr(SC, T, VK_RValue, OK_Ordinary,
822 T->isDependentType(), T->isDependentType(),
823 T->isInstantiationDependentType(),
824 T->containsUnexpandedParameterPack()),
825 Constructor(Ctor), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
826 NumArgs(Args.size()), Elidable(Elidable),
827 HadMultipleCandidates(HadMultipleCandidates),
828 ListInitialization(ListInitialization),
829 StdInitListInitialization(StdInitListInitialization),
830 ZeroInitialization(ZeroInitialization), ConstructKind(ConstructKind) {
832 this->Args = new (C) Stmt*[Args.size()];
834 for (unsigned i = 0; i != Args.size(); ++i) {
835 assert(Args[i] && "NULL argument in CXXConstructExpr");
837 if (Args[i]->isValueDependent())
838 ExprBits.ValueDependent = true;
839 if (Args[i]->isInstantiationDependent())
840 ExprBits.InstantiationDependent = true;
841 if (Args[i]->containsUnexpandedParameterPack())
842 ExprBits.ContainsUnexpandedParameterPack = true;
844 this->Args[i] = Args[i];
849 LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
850 LambdaCaptureKind Kind, VarDecl *Var,
851 SourceLocation EllipsisLoc)
852 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc) {
855 Bits |= Capture_Implicit;
859 Bits |= Capture_ByCopy;
862 assert(!Var && "'this' capture cannot have a variable!");
863 Bits |= Capture_This;
867 Bits |= Capture_ByCopy;
870 assert(Var && "capture must have a variable!");
873 assert(!Var && "VLA type capture cannot have a variable!");
876 DeclAndBits.setInt(Bits);
879 LambdaCaptureKind LambdaCapture::getCaptureKind() const {
880 if (capturesVLAType())
882 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
884 return CapByCopy ? LCK_StarThis : LCK_This;
885 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
888 LambdaExpr::LambdaExpr(QualType T, SourceRange IntroducerRange,
889 LambdaCaptureDefault CaptureDefault,
890 SourceLocation CaptureDefaultLoc,
891 ArrayRef<LambdaCapture> Captures, bool ExplicitParams,
892 bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
893 SourceLocation ClosingBrace,
894 bool ContainsUnexpandedParameterPack)
895 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary, T->isDependentType(),
896 T->isDependentType(), T->isDependentType(),
897 ContainsUnexpandedParameterPack),
898 IntroducerRange(IntroducerRange), CaptureDefaultLoc(CaptureDefaultLoc),
899 NumCaptures(Captures.size()), CaptureDefault(CaptureDefault),
900 ExplicitParams(ExplicitParams), ExplicitResultType(ExplicitResultType),
901 ClosingBrace(ClosingBrace) {
902 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
903 CXXRecordDecl *Class = getLambdaClass();
904 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
906 // FIXME: Propagate "has unexpanded parameter pack" bit.
909 const ASTContext &Context = Class->getASTContext();
910 Data.NumCaptures = NumCaptures;
911 Data.NumExplicitCaptures = 0;
913 (LambdaCapture *)Context.Allocate(sizeof(LambdaCapture) * NumCaptures);
914 LambdaCapture *ToCapture = Data.Captures;
915 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
916 if (Captures[I].isExplicit())
917 ++Data.NumExplicitCaptures;
919 *ToCapture++ = Captures[I];
922 // Copy initialization expressions for the non-static data members.
923 Stmt **Stored = getStoredStmts();
924 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
925 *Stored++ = CaptureInits[I];
927 // Copy the body of the lambda.
928 *Stored++ = getCallOperator()->getBody();
931 LambdaExpr *LambdaExpr::Create(
932 const ASTContext &Context, CXXRecordDecl *Class,
933 SourceRange IntroducerRange, LambdaCaptureDefault CaptureDefault,
934 SourceLocation CaptureDefaultLoc, ArrayRef<LambdaCapture> Captures,
935 bool ExplicitParams, bool ExplicitResultType, ArrayRef<Expr *> CaptureInits,
936 SourceLocation ClosingBrace, bool ContainsUnexpandedParameterPack) {
937 // Determine the type of the expression (i.e., the type of the
938 // function object we're creating).
939 QualType T = Context.getTypeDeclType(Class);
941 unsigned Size = totalSizeToAlloc<Stmt *>(Captures.size() + 1);
942 void *Mem = Context.Allocate(Size);
944 LambdaExpr(T, IntroducerRange, CaptureDefault, CaptureDefaultLoc,
945 Captures, ExplicitParams, ExplicitResultType, CaptureInits,
946 ClosingBrace, ContainsUnexpandedParameterPack);
949 LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
950 unsigned NumCaptures) {
951 unsigned Size = totalSizeToAlloc<Stmt *>(NumCaptures + 1);
952 void *Mem = C.Allocate(Size);
953 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures);
956 bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
957 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
958 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
961 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
962 return getLambdaClass()->getLambdaData().Captures;
965 LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
966 return capture_begin() + NumCaptures;
969 LambdaExpr::capture_range LambdaExpr::captures() const {
970 return capture_range(capture_begin(), capture_end());
973 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
974 return capture_begin();
977 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
978 struct CXXRecordDecl::LambdaDefinitionData &Data
979 = getLambdaClass()->getLambdaData();
980 return Data.Captures + Data.NumExplicitCaptures;
983 LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
984 return capture_range(explicit_capture_begin(), explicit_capture_end());
987 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
988 return explicit_capture_end();
991 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
992 return capture_end();
995 LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
996 return capture_range(implicit_capture_begin(), implicit_capture_end());
999 CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1000 return getType()->getAsCXXRecordDecl();
1003 CXXMethodDecl *LambdaExpr::getCallOperator() const {
1004 CXXRecordDecl *Record = getLambdaClass();
1005 return Record->getLambdaCallOperator();
1008 TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1009 CXXRecordDecl *Record = getLambdaClass();
1010 return Record->getGenericLambdaTemplateParameterList();
1014 CompoundStmt *LambdaExpr::getBody() const {
1015 // FIXME: this mutation in getBody is bogus. It should be
1016 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1017 // don't understand, that doesn't work.
1018 if (!getStoredStmts()[NumCaptures])
1019 *const_cast<Stmt **>(&getStoredStmts()[NumCaptures]) =
1020 getCallOperator()->getBody();
1022 return static_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1025 bool LambdaExpr::isMutable() const {
1026 return !getCallOperator()->isConst();
1029 ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1030 bool CleanupsHaveSideEffects,
1031 ArrayRef<CleanupObject> objects)
1032 : Expr(ExprWithCleanupsClass, subexpr->getType(),
1033 subexpr->getValueKind(), subexpr->getObjectKind(),
1034 subexpr->isTypeDependent(), subexpr->isValueDependent(),
1035 subexpr->isInstantiationDependent(),
1036 subexpr->containsUnexpandedParameterPack()),
1038 ExprWithCleanupsBits.CleanupsHaveSideEffects = CleanupsHaveSideEffects;
1039 ExprWithCleanupsBits.NumObjects = objects.size();
1040 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1041 getTrailingObjects<CleanupObject>()[i] = objects[i];
1044 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
1045 bool CleanupsHaveSideEffects,
1046 ArrayRef<CleanupObject> objects) {
1047 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(objects.size()),
1048 alignof(ExprWithCleanups));
1050 ExprWithCleanups(subexpr, CleanupsHaveSideEffects, objects);
1053 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1054 : Expr(ExprWithCleanupsClass, empty) {
1055 ExprWithCleanupsBits.NumObjects = numObjects;
1058 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1060 unsigned numObjects) {
1061 void *buffer = C.Allocate(totalSizeToAlloc<CleanupObject>(numObjects),
1062 alignof(ExprWithCleanups));
1063 return new (buffer) ExprWithCleanups(empty, numObjects);
1066 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
1067 SourceLocation LParenLoc,
1068 ArrayRef<Expr*> Args,
1069 SourceLocation RParenLoc)
1070 : Expr(CXXUnresolvedConstructExprClass,
1071 Type->getType().getNonReferenceType(),
1072 (Type->getType()->isLValueReferenceType()
1074 : Type->getType()->isRValueReferenceType() ? VK_XValue
1077 Type->getType()->isDependentType() ||
1078 Type->getType()->getContainedDeducedType(),
1079 true, true, Type->getType()->containsUnexpandedParameterPack()),
1080 Type(Type), LParenLoc(LParenLoc), RParenLoc(RParenLoc),
1081 NumArgs(Args.size()) {
1082 Expr **StoredArgs = getTrailingObjects<Expr *>();
1083 for (unsigned I = 0; I != Args.size(); ++I) {
1084 if (Args[I]->containsUnexpandedParameterPack())
1085 ExprBits.ContainsUnexpandedParameterPack = true;
1087 StoredArgs[I] = Args[I];
1091 CXXUnresolvedConstructExpr *
1092 CXXUnresolvedConstructExpr::Create(const ASTContext &C,
1093 TypeSourceInfo *Type,
1094 SourceLocation LParenLoc,
1095 ArrayRef<Expr*> Args,
1096 SourceLocation RParenLoc) {
1097 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Args.size()));
1098 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
1101 CXXUnresolvedConstructExpr *
1102 CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
1103 Stmt::EmptyShell Empty;
1104 void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumArgs));
1105 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1108 SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1109 return Type->getTypeLoc().getBeginLoc();
1112 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1113 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1114 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1115 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1116 DeclarationNameInfo MemberNameInfo,
1117 const TemplateArgumentListInfo *TemplateArgs)
1118 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1119 OK_Ordinary, true, true, true,
1120 ((Base && Base->containsUnexpandedParameterPack()) ||
1122 QualifierLoc.getNestedNameSpecifier()
1123 ->containsUnexpandedParameterPack()) ||
1124 MemberNameInfo.containsUnexpandedParameterPack())),
1125 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1126 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1127 TemplateKWLoc.isValid()),
1128 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1129 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1130 MemberNameInfo(MemberNameInfo) {
1132 bool Dependent = true;
1133 bool InstantiationDependent = true;
1134 bool ContainsUnexpandedParameterPack = false;
1135 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1136 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1137 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
1138 if (ContainsUnexpandedParameterPack)
1139 ExprBits.ContainsUnexpandedParameterPack = true;
1140 } else if (TemplateKWLoc.isValid()) {
1141 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1146 CXXDependentScopeMemberExpr *
1147 CXXDependentScopeMemberExpr::Create(const ASTContext &C,
1148 Expr *Base, QualType BaseType, bool IsArrow,
1149 SourceLocation OperatorLoc,
1150 NestedNameSpecifierLoc QualifierLoc,
1151 SourceLocation TemplateKWLoc,
1152 NamedDecl *FirstQualifierFoundInScope,
1153 DeclarationNameInfo MemberNameInfo,
1154 const TemplateArgumentListInfo *TemplateArgs) {
1155 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1156 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1158 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1159 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1161 void *Mem = C.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
1162 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1163 IsArrow, OperatorLoc,
1166 FirstQualifierFoundInScope,
1167 MemberNameInfo, TemplateArgs);
1170 CXXDependentScopeMemberExpr *
1171 CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
1172 bool HasTemplateKWAndArgsInfo,
1173 unsigned NumTemplateArgs) {
1174 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1176 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1177 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1178 void *Mem = C.Allocate(Size, alignof(CXXDependentScopeMemberExpr));
1179 CXXDependentScopeMemberExpr *E
1180 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
1181 false, SourceLocation(),
1182 NestedNameSpecifierLoc(),
1183 SourceLocation(), nullptr,
1184 DeclarationNameInfo(), nullptr);
1185 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
1189 bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1193 return cast<Expr>(Base)->isImplicitCXXThis();
1196 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1197 UnresolvedSetIterator end) {
1199 NamedDecl *decl = *begin;
1200 if (isa<UnresolvedUsingValueDecl>(decl))
1203 // Unresolved member expressions should only contain methods and
1204 // method templates.
1205 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1208 } while (++begin != end);
1213 UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
1214 bool HasUnresolvedUsing,
1215 Expr *Base, QualType BaseType,
1217 SourceLocation OperatorLoc,
1218 NestedNameSpecifierLoc QualifierLoc,
1219 SourceLocation TemplateKWLoc,
1220 const DeclarationNameInfo &MemberNameInfo,
1221 const TemplateArgumentListInfo *TemplateArgs,
1222 UnresolvedSetIterator Begin,
1223 UnresolvedSetIterator End)
1225 UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1226 MemberNameInfo, TemplateArgs, Begin, End,
1228 ((Base && Base->isTypeDependent()) || BaseType->isDependentType()),
1229 ((Base && Base->isInstantiationDependent()) ||
1230 BaseType->isInstantiationDependentType()),
1231 // Contains unexpanded parameter pack
1232 ((Base && Base->containsUnexpandedParameterPack()) ||
1233 BaseType->containsUnexpandedParameterPack())),
1234 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing), Base(Base),
1235 BaseType(BaseType), OperatorLoc(OperatorLoc) {
1236 // Check whether all of the members are non-static member functions,
1237 // and if so, mark give this bound-member type instead of overload type.
1238 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1239 setType(C.BoundMemberTy);
1242 bool UnresolvedMemberExpr::isImplicitAccess() const {
1246 return cast<Expr>(Base)->isImplicitCXXThis();
1249 UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1250 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1251 bool IsArrow, SourceLocation OperatorLoc,
1252 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1253 const DeclarationNameInfo &MemberNameInfo,
1254 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1255 UnresolvedSetIterator End) {
1256 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1258 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1259 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
1261 void *Mem = C.Allocate(Size, alignof(UnresolvedMemberExpr));
1262 return new (Mem) UnresolvedMemberExpr(
1263 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1264 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
1267 UnresolvedMemberExpr *
1268 UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1269 bool HasTemplateKWAndArgsInfo,
1270 unsigned NumTemplateArgs) {
1271 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1273 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1274 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1276 void *Mem = C.Allocate(Size, alignof(UnresolvedMemberExpr));
1277 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
1278 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
1282 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1283 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1285 // If there was a nested name specifier, it names the naming class.
1286 // It can't be dependent: after all, we were actually able to do the
1288 CXXRecordDecl *Record = nullptr;
1289 auto *NNS = getQualifier();
1290 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
1291 const Type *T = getQualifier()->getAsType();
1292 assert(T && "qualifier in member expression does not name type");
1293 Record = T->getAsCXXRecordDecl();
1294 assert(Record && "qualifier in member expression does not name record");
1296 // Otherwise the naming class must have been the base class.
1298 QualType BaseType = getBaseType().getNonReferenceType();
1300 const PointerType *PT = BaseType->getAs<PointerType>();
1301 assert(PT && "base of arrow member access is not pointer");
1302 BaseType = PT->getPointeeType();
1305 Record = BaseType->getAsCXXRecordDecl();
1306 assert(Record && "base of member expression does not name record");
1313 SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1314 NamedDecl *Pack, SourceLocation PackLoc,
1315 SourceLocation RParenLoc,
1316 Optional<unsigned> Length,
1317 ArrayRef<TemplateArgument> PartialArgs) {
1319 Context.Allocate(totalSizeToAlloc<TemplateArgument>(PartialArgs.size()));
1320 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1321 PackLoc, RParenLoc, Length, PartialArgs);
1324 SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1325 unsigned NumPartialArgs) {
1327 Context.Allocate(totalSizeToAlloc<TemplateArgument>(NumPartialArgs));
1328 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1331 SubstNonTypeTemplateParmPackExpr::
1332 SubstNonTypeTemplateParmPackExpr(QualType T,
1333 NonTypeTemplateParmDecl *Param,
1334 SourceLocation NameLoc,
1335 const TemplateArgument &ArgPack)
1336 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
1337 true, true, true, true),
1338 Param(Param), Arguments(ArgPack.pack_begin()),
1339 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) {}
1341 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1342 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
1345 FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1346 SourceLocation NameLoc,
1348 ParmVarDecl *const *Params)
1349 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1351 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1353 std::uninitialized_copy(Params, Params + NumParams,
1354 getTrailingObjects<ParmVarDecl *>());
1357 FunctionParmPackExpr *
1358 FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
1359 ParmVarDecl *ParamPack, SourceLocation NameLoc,
1360 ArrayRef<ParmVarDecl *> Params) {
1361 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(Params.size())))
1362 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1365 FunctionParmPackExpr *
1366 FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1367 unsigned NumParams) {
1368 return new (Context.Allocate(totalSizeToAlloc<ParmVarDecl *>(NumParams)))
1369 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
1372 void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1373 unsigned ManglingNumber) {
1374 // We only need extra state if we have to remember more than just the Stmt.
1378 // We may need to allocate extra storage for the mangling number and the
1379 // extended-by ValueDecl.
1380 if (!State.is<ExtraState *>()) {
1381 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1382 ES->Temporary = State.get<Stmt *>();
1386 auto ES = State.get<ExtraState *>();
1387 ES->ExtendingDecl = ExtendedBy;
1388 ES->ManglingNumber = ManglingNumber;
1391 TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1392 ArrayRef<TypeSourceInfo *> Args,
1393 SourceLocation RParenLoc,
1395 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1396 /*TypeDependent=*/false,
1397 /*ValueDependent=*/false,
1398 /*InstantiationDependent=*/false,
1399 /*ContainsUnexpandedParameterPack=*/false),
1400 Loc(Loc), RParenLoc(RParenLoc) {
1401 TypeTraitExprBits.Kind = Kind;
1402 TypeTraitExprBits.Value = Value;
1403 TypeTraitExprBits.NumArgs = Args.size();
1405 TypeSourceInfo **ToArgs = getTrailingObjects<TypeSourceInfo *>();
1407 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1408 if (Args[I]->getType()->isDependentType())
1409 setValueDependent(true);
1410 if (Args[I]->getType()->isInstantiationDependentType())
1411 setInstantiationDependent(true);
1412 if (Args[I]->getType()->containsUnexpandedParameterPack())
1413 setContainsUnexpandedParameterPack(true);
1415 ToArgs[I] = Args[I];
1419 TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
1422 ArrayRef<TypeSourceInfo *> Args,
1423 SourceLocation RParenLoc,
1425 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(Args.size()));
1426 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1429 TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
1431 void *Mem = C.Allocate(totalSizeToAlloc<TypeSourceInfo *>(NumArgs));
1432 return new (Mem) TypeTraitExpr(EmptyShell());
1435 void ArrayTypeTraitExpr::anchor() {}