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);
58 const UuidAttr *CXXUuidofExpr::GetUuidAttrOfType(QualType QT,
59 bool *RDHasMultipleGUIDsPtr) {
60 // Optionally remove one level of pointer, reference or array indirection.
61 const Type *Ty = QT.getTypePtr();
62 if (QT->isPointerType() || QT->isReferenceType())
63 Ty = QT->getPointeeType().getTypePtr();
64 else if (QT->isArrayType())
65 Ty = Ty->getBaseElementTypeUnsafe();
67 const CXXRecordDecl *RD = Ty->getAsCXXRecordDecl();
71 if (const UuidAttr *Uuid = RD->getMostRecentDecl()->getAttr<UuidAttr>())
74 // __uuidof can grab UUIDs from template arguments.
75 if (const ClassTemplateSpecializationDecl *CTSD =
76 dyn_cast<ClassTemplateSpecializationDecl>(RD)) {
77 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
78 const UuidAttr *UuidForRD = nullptr;
80 for (const TemplateArgument &TA : TAL.asArray()) {
81 bool SeenMultipleGUIDs = false;
83 const UuidAttr *UuidForTA = nullptr;
84 if (TA.getKind() == TemplateArgument::Type)
85 UuidForTA = GetUuidAttrOfType(TA.getAsType(), &SeenMultipleGUIDs);
86 else if (TA.getKind() == TemplateArgument::Declaration)
88 GetUuidAttrOfType(TA.getAsDecl()->getType(), &SeenMultipleGUIDs);
90 // If the template argument has a UUID, there are three cases:
91 // - This is the first UUID seen for this RecordDecl.
92 // - This is a different UUID than previously seen for this RecordDecl.
93 // - This is the same UUID than previously seen for this RecordDecl.
96 UuidForRD = UuidForTA;
97 else if (UuidForRD != UuidForTA)
98 SeenMultipleGUIDs = true;
101 // Seeing multiple UUIDs means that we couldn't find a UUID
102 if (SeenMultipleGUIDs) {
103 if (RDHasMultipleGUIDsPtr)
104 *RDHasMultipleGUIDsPtr = true;
115 StringRef CXXUuidofExpr::getUuidAsStringRef(ASTContext &Context) const {
118 Uuid = CXXUuidofExpr::GetUuidAttrOfType(getTypeOperand(Context))->getGuid();
120 // Special case: __uuidof(0) means an all-zero GUID.
121 Expr *Op = getExprOperand();
122 if (!Op->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull))
123 Uuid = CXXUuidofExpr::GetUuidAttrOfType(Op->getType())->getGuid();
125 Uuid = "00000000-0000-0000-0000-000000000000";
130 // CXXScalarValueInitExpr
131 SourceLocation CXXScalarValueInitExpr::getLocStart() const {
132 return TypeInfo ? TypeInfo->getTypeLoc().getBeginLoc() : RParenLoc;
136 CXXNewExpr::CXXNewExpr(const ASTContext &C, bool globalNew,
137 FunctionDecl *operatorNew, FunctionDecl *operatorDelete,
138 bool usualArrayDeleteWantsSize,
139 ArrayRef<Expr*> placementArgs,
140 SourceRange typeIdParens, Expr *arraySize,
141 InitializationStyle initializationStyle,
142 Expr *initializer, QualType ty,
143 TypeSourceInfo *allocatedTypeInfo,
144 SourceRange Range, SourceRange directInitRange)
145 : Expr(CXXNewExprClass, ty, VK_RValue, OK_Ordinary,
146 ty->isDependentType(), ty->isDependentType(),
147 ty->isInstantiationDependentType(),
148 ty->containsUnexpandedParameterPack()),
149 SubExprs(nullptr), OperatorNew(operatorNew), OperatorDelete(operatorDelete),
150 AllocatedTypeInfo(allocatedTypeInfo), TypeIdParens(typeIdParens),
151 Range(Range), DirectInitRange(directInitRange),
152 GlobalNew(globalNew), UsualArrayDeleteWantsSize(usualArrayDeleteWantsSize) {
153 assert((initializer != nullptr || initializationStyle == NoInit) &&
154 "Only NoInit can have no initializer.");
155 StoredInitializationStyle = initializer ? initializationStyle + 1 : 0;
156 AllocateArgsArray(C, arraySize != nullptr, placementArgs.size(),
157 initializer != nullptr);
160 if (arraySize->isInstantiationDependent())
161 ExprBits.InstantiationDependent = true;
163 if (arraySize->containsUnexpandedParameterPack())
164 ExprBits.ContainsUnexpandedParameterPack = true;
166 SubExprs[i++] = arraySize;
170 if (initializer->isInstantiationDependent())
171 ExprBits.InstantiationDependent = true;
173 if (initializer->containsUnexpandedParameterPack())
174 ExprBits.ContainsUnexpandedParameterPack = true;
176 SubExprs[i++] = initializer;
179 for (unsigned j = 0; j != placementArgs.size(); ++j) {
180 if (placementArgs[j]->isInstantiationDependent())
181 ExprBits.InstantiationDependent = true;
182 if (placementArgs[j]->containsUnexpandedParameterPack())
183 ExprBits.ContainsUnexpandedParameterPack = true;
185 SubExprs[i++] = placementArgs[j];
188 switch (getInitializationStyle()) {
190 this->Range.setEnd(DirectInitRange.getEnd()); break;
192 this->Range.setEnd(getInitializer()->getSourceRange().getEnd()); break;
194 if (TypeIdParens.isValid())
195 this->Range.setEnd(TypeIdParens.getEnd());
200 void CXXNewExpr::AllocateArgsArray(const ASTContext &C, bool isArray,
201 unsigned numPlaceArgs, bool hasInitializer){
202 assert(SubExprs == nullptr && "SubExprs already allocated");
204 NumPlacementArgs = numPlaceArgs;
206 unsigned TotalSize = Array + hasInitializer + NumPlacementArgs;
207 SubExprs = new (C) Stmt*[TotalSize];
210 bool CXXNewExpr::shouldNullCheckAllocation(const ASTContext &Ctx) const {
211 return getOperatorNew()->getType()->castAs<FunctionProtoType>()->isNothrow(
213 !getOperatorNew()->isReservedGlobalPlacementOperator();
217 QualType CXXDeleteExpr::getDestroyedType() const {
218 const Expr *Arg = getArgument();
219 // The type-to-delete may not be a pointer if it's a dependent type.
220 const QualType ArgType = Arg->getType();
222 if (ArgType->isDependentType() && !ArgType->isPointerType())
225 return ArgType->getAs<PointerType>()->getPointeeType();
228 // CXXPseudoDestructorExpr
229 PseudoDestructorTypeStorage::PseudoDestructorTypeStorage(TypeSourceInfo *Info)
232 Location = Info->getTypeLoc().getLocalSourceRange().getBegin();
235 CXXPseudoDestructorExpr::CXXPseudoDestructorExpr(const ASTContext &Context,
236 Expr *Base, bool isArrow, SourceLocation OperatorLoc,
237 NestedNameSpecifierLoc QualifierLoc, TypeSourceInfo *ScopeType,
238 SourceLocation ColonColonLoc, SourceLocation TildeLoc,
239 PseudoDestructorTypeStorage DestroyedType)
240 : Expr(CXXPseudoDestructorExprClass,
241 Context.BoundMemberTy,
242 VK_RValue, OK_Ordinary,
243 /*isTypeDependent=*/(Base->isTypeDependent() ||
244 (DestroyedType.getTypeSourceInfo() &&
245 DestroyedType.getTypeSourceInfo()->getType()->isDependentType())),
246 /*isValueDependent=*/Base->isValueDependent(),
247 (Base->isInstantiationDependent() ||
249 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) ||
251 ScopeType->getType()->isInstantiationDependentType()) ||
252 (DestroyedType.getTypeSourceInfo() &&
253 DestroyedType.getTypeSourceInfo()->getType()
254 ->isInstantiationDependentType())),
255 // ContainsUnexpandedParameterPack
256 (Base->containsUnexpandedParameterPack() ||
258 QualifierLoc.getNestedNameSpecifier()
259 ->containsUnexpandedParameterPack()) ||
261 ScopeType->getType()->containsUnexpandedParameterPack()) ||
262 (DestroyedType.getTypeSourceInfo() &&
263 DestroyedType.getTypeSourceInfo()->getType()
264 ->containsUnexpandedParameterPack()))),
265 Base(static_cast<Stmt *>(Base)), IsArrow(isArrow),
266 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
267 ScopeType(ScopeType), ColonColonLoc(ColonColonLoc), TildeLoc(TildeLoc),
268 DestroyedType(DestroyedType) { }
270 QualType CXXPseudoDestructorExpr::getDestroyedType() const {
271 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
272 return TInfo->getType();
277 SourceLocation CXXPseudoDestructorExpr::getLocEnd() const {
278 SourceLocation End = DestroyedType.getLocation();
279 if (TypeSourceInfo *TInfo = DestroyedType.getTypeSourceInfo())
280 End = TInfo->getTypeLoc().getLocalSourceRange().getEnd();
284 // UnresolvedLookupExpr
285 UnresolvedLookupExpr *
286 UnresolvedLookupExpr::Create(const ASTContext &C,
287 CXXRecordDecl *NamingClass,
288 NestedNameSpecifierLoc QualifierLoc,
289 SourceLocation TemplateKWLoc,
290 const DeclarationNameInfo &NameInfo,
292 const TemplateArgumentListInfo *Args,
293 UnresolvedSetIterator Begin,
294 UnresolvedSetIterator End)
296 assert(Args || TemplateKWLoc.isValid());
297 unsigned num_args = Args ? Args->size() : 0;
298 void *Mem = C.Allocate(sizeof(UnresolvedLookupExpr) +
299 ASTTemplateKWAndArgsInfo::sizeFor(num_args));
300 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
301 TemplateKWLoc, NameInfo,
302 ADL, /*Overload*/ true, Args,
306 UnresolvedLookupExpr *
307 UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
308 bool HasTemplateKWAndArgsInfo,
309 unsigned NumTemplateArgs) {
310 std::size_t size = sizeof(UnresolvedLookupExpr);
311 if (HasTemplateKWAndArgsInfo)
312 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
314 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedLookupExpr>());
315 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
316 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
320 OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
321 NestedNameSpecifierLoc QualifierLoc,
322 SourceLocation TemplateKWLoc,
323 const DeclarationNameInfo &NameInfo,
324 const TemplateArgumentListInfo *TemplateArgs,
325 UnresolvedSetIterator Begin,
326 UnresolvedSetIterator End,
328 bool KnownInstantiationDependent,
329 bool KnownContainsUnexpandedParameterPack)
330 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
332 (KnownInstantiationDependent ||
333 NameInfo.isInstantiationDependent() ||
335 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
336 (KnownContainsUnexpandedParameterPack ||
337 NameInfo.containsUnexpandedParameterPack() ||
339 QualifierLoc.getNestedNameSpecifier()
340 ->containsUnexpandedParameterPack()))),
341 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
342 Results(nullptr), NumResults(End - Begin),
343 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
344 TemplateKWLoc.isValid()) {
345 NumResults = End - Begin;
347 // Determine whether this expression is type-dependent.
348 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
349 if ((*I)->getDeclContext()->isDependentContext() ||
350 isa<UnresolvedUsingValueDecl>(*I)) {
351 ExprBits.TypeDependent = true;
352 ExprBits.ValueDependent = true;
353 ExprBits.InstantiationDependent = true;
357 Results = static_cast<DeclAccessPair *>(
358 C.Allocate(sizeof(DeclAccessPair) * NumResults,
359 llvm::alignOf<DeclAccessPair>()));
360 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
363 // If we have explicit template arguments, check for dependent
364 // template arguments and whether they contain any unexpanded pack
367 bool Dependent = false;
368 bool InstantiationDependent = false;
369 bool ContainsUnexpandedParameterPack = false;
370 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
372 InstantiationDependent,
373 ContainsUnexpandedParameterPack);
376 ExprBits.TypeDependent = true;
377 ExprBits.ValueDependent = true;
379 if (InstantiationDependent)
380 ExprBits.InstantiationDependent = true;
381 if (ContainsUnexpandedParameterPack)
382 ExprBits.ContainsUnexpandedParameterPack = true;
383 } else if (TemplateKWLoc.isValid()) {
384 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
387 if (isTypeDependent())
388 setType(C.DependentTy);
391 void OverloadExpr::initializeResults(const ASTContext &C,
392 UnresolvedSetIterator Begin,
393 UnresolvedSetIterator End) {
394 assert(!Results && "Results already initialized!");
395 NumResults = End - Begin;
397 Results = static_cast<DeclAccessPair *>(
398 C.Allocate(sizeof(DeclAccessPair) * NumResults,
400 llvm::alignOf<DeclAccessPair>()));
401 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
405 CXXRecordDecl *OverloadExpr::getNamingClass() const {
406 if (isa<UnresolvedLookupExpr>(this))
407 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
409 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
412 // DependentScopeDeclRefExpr
413 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
414 NestedNameSpecifierLoc QualifierLoc,
415 SourceLocation TemplateKWLoc,
416 const DeclarationNameInfo &NameInfo,
417 const TemplateArgumentListInfo *Args)
418 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
420 (NameInfo.isInstantiationDependent() ||
422 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
423 (NameInfo.containsUnexpandedParameterPack() ||
425 QualifierLoc.getNestedNameSpecifier()
426 ->containsUnexpandedParameterPack()))),
427 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
428 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
431 bool Dependent = true;
432 bool InstantiationDependent = true;
433 bool ContainsUnexpandedParameterPack
434 = ExprBits.ContainsUnexpandedParameterPack;
435 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *Args,
437 InstantiationDependent,
438 ContainsUnexpandedParameterPack);
439 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
440 } else if (TemplateKWLoc.isValid()) {
441 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
445 DependentScopeDeclRefExpr *
446 DependentScopeDeclRefExpr::Create(const ASTContext &C,
447 NestedNameSpecifierLoc QualifierLoc,
448 SourceLocation TemplateKWLoc,
449 const DeclarationNameInfo &NameInfo,
450 const TemplateArgumentListInfo *Args) {
451 assert(QualifierLoc && "should be created for dependent qualifiers");
452 std::size_t size = sizeof(DependentScopeDeclRefExpr);
454 size += ASTTemplateKWAndArgsInfo::sizeFor(Args->size());
455 else if (TemplateKWLoc.isValid())
456 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
457 void *Mem = C.Allocate(size);
458 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
459 TemplateKWLoc, NameInfo, Args);
462 DependentScopeDeclRefExpr *
463 DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
464 bool HasTemplateKWAndArgsInfo,
465 unsigned NumTemplateArgs) {
466 std::size_t size = sizeof(DependentScopeDeclRefExpr);
467 if (HasTemplateKWAndArgsInfo)
468 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
469 void *Mem = C.Allocate(size);
470 DependentScopeDeclRefExpr *E
471 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
473 DeclarationNameInfo(), nullptr);
474 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
478 SourceLocation CXXConstructExpr::getLocStart() const {
479 if (isa<CXXTemporaryObjectExpr>(this))
480 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
484 SourceLocation CXXConstructExpr::getLocEnd() const {
485 if (isa<CXXTemporaryObjectExpr>(this))
486 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
488 if (ParenOrBraceRange.isValid())
489 return ParenOrBraceRange.getEnd();
491 SourceLocation End = Loc;
492 for (unsigned I = getNumArgs(); I > 0; --I) {
493 const Expr *Arg = getArg(I-1);
494 if (!Arg->isDefaultArgument()) {
495 SourceLocation NewEnd = Arg->getLocEnd();
496 if (NewEnd.isValid()) {
506 SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
507 OverloadedOperatorKind Kind = getOperator();
508 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
509 if (getNumArgs() == 1)
511 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
514 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
515 } else if (Kind == OO_Arrow) {
516 return getArg(0)->getSourceRange();
517 } else if (Kind == OO_Call) {
518 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
519 } else if (Kind == OO_Subscript) {
520 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
521 } else if (getNumArgs() == 1) {
522 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
523 } else if (getNumArgs() == 2) {
524 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
526 return getOperatorLoc();
530 Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
531 const Expr *Callee = getCallee()->IgnoreParens();
532 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
533 return MemExpr->getBase();
534 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
535 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
538 // FIXME: Will eventually need to cope with member pointers.
542 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
543 if (const MemberExpr *MemExpr =
544 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
545 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
547 // FIXME: Will eventually need to cope with member pointers.
552 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
553 Expr* ThisArg = getImplicitObjectArgument();
557 if (ThisArg->getType()->isAnyPointerType())
558 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
560 return ThisArg->getType()->getAsCXXRecordDecl();
564 //===----------------------------------------------------------------------===//
566 //===----------------------------------------------------------------------===//
568 /// getCastName - Get the name of the C++ cast being used, e.g.,
569 /// "static_cast", "dynamic_cast", "reinterpret_cast", or
570 /// "const_cast". The returned pointer must not be freed.
571 const char *CXXNamedCastExpr::getCastName() const {
572 switch (getStmtClass()) {
573 case CXXStaticCastExprClass: return "static_cast";
574 case CXXDynamicCastExprClass: return "dynamic_cast";
575 case CXXReinterpretCastExprClass: return "reinterpret_cast";
576 case CXXConstCastExprClass: return "const_cast";
577 default: return "<invalid cast>";
581 CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
583 CastKind K, Expr *Op,
584 const CXXCastPath *BasePath,
585 TypeSourceInfo *WrittenTy,
587 SourceLocation RParenLoc,
588 SourceRange AngleBrackets) {
589 unsigned PathSize = (BasePath ? BasePath->size() : 0);
590 void *Buffer = C.Allocate(sizeof(CXXStaticCastExpr)
591 + PathSize * sizeof(CXXBaseSpecifier*));
592 CXXStaticCastExpr *E =
593 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
594 RParenLoc, AngleBrackets);
595 if (PathSize) E->setCastPath(*BasePath);
599 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
602 C.Allocate(sizeof(CXXStaticCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
603 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
606 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
608 CastKind K, Expr *Op,
609 const CXXCastPath *BasePath,
610 TypeSourceInfo *WrittenTy,
612 SourceLocation RParenLoc,
613 SourceRange AngleBrackets) {
614 unsigned PathSize = (BasePath ? BasePath->size() : 0);
615 void *Buffer = C.Allocate(sizeof(CXXDynamicCastExpr)
616 + PathSize * sizeof(CXXBaseSpecifier*));
617 CXXDynamicCastExpr *E =
618 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
619 RParenLoc, AngleBrackets);
620 if (PathSize) E->setCastPath(*BasePath);
624 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
627 C.Allocate(sizeof(CXXDynamicCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
628 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
631 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
632 /// to always be null. For example:
635 /// struct B final : A { };
638 /// C *f(B* b) { return dynamic_cast<C*>(b); }
639 bool CXXDynamicCastExpr::isAlwaysNull() const
641 QualType SrcType = getSubExpr()->getType();
642 QualType DestType = getType();
644 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
645 SrcType = SrcPTy->getPointeeType();
646 DestType = DestType->castAs<PointerType>()->getPointeeType();
649 if (DestType->isVoidType())
652 const CXXRecordDecl *SrcRD =
653 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
655 if (!SrcRD->hasAttr<FinalAttr>())
658 const CXXRecordDecl *DestRD =
659 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
661 return !DestRD->isDerivedFrom(SrcRD);
664 CXXReinterpretCastExpr *
665 CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
666 ExprValueKind VK, CastKind K, Expr *Op,
667 const CXXCastPath *BasePath,
668 TypeSourceInfo *WrittenTy, SourceLocation L,
669 SourceLocation RParenLoc,
670 SourceRange AngleBrackets) {
671 unsigned PathSize = (BasePath ? BasePath->size() : 0);
673 C.Allocate(sizeof(CXXReinterpretCastExpr) + PathSize * sizeof(CXXBaseSpecifier*));
674 CXXReinterpretCastExpr *E =
675 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
676 RParenLoc, AngleBrackets);
677 if (PathSize) E->setCastPath(*BasePath);
681 CXXReinterpretCastExpr *
682 CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
683 void *Buffer = C.Allocate(sizeof(CXXReinterpretCastExpr)
684 + PathSize * sizeof(CXXBaseSpecifier*));
685 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
688 CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
689 ExprValueKind VK, Expr *Op,
690 TypeSourceInfo *WrittenTy,
692 SourceLocation RParenLoc,
693 SourceRange AngleBrackets) {
694 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
697 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
698 return new (C) CXXConstCastExpr(EmptyShell());
701 CXXFunctionalCastExpr *
702 CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
703 TypeSourceInfo *Written, CastKind K, Expr *Op,
704 const CXXCastPath *BasePath,
705 SourceLocation L, SourceLocation R) {
706 unsigned PathSize = (BasePath ? BasePath->size() : 0);
707 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
708 + PathSize * sizeof(CXXBaseSpecifier*));
709 CXXFunctionalCastExpr *E =
710 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
711 if (PathSize) E->setCastPath(*BasePath);
715 CXXFunctionalCastExpr *
716 CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
717 void *Buffer = C.Allocate(sizeof(CXXFunctionalCastExpr)
718 + PathSize * sizeof(CXXBaseSpecifier*));
719 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
722 SourceLocation CXXFunctionalCastExpr::getLocStart() const {
723 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
726 SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
727 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
730 UserDefinedLiteral::LiteralOperatorKind
731 UserDefinedLiteral::getLiteralOperatorKind() const {
732 if (getNumArgs() == 0)
734 if (getNumArgs() == 2)
737 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
739 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
740 if (ParamTy->isPointerType())
742 if (ParamTy->isAnyCharacterType())
743 return LOK_Character;
744 if (ParamTy->isIntegerType())
746 if (ParamTy->isFloatingType())
749 llvm_unreachable("unknown kind of literal operator");
752 Expr *UserDefinedLiteral::getCookedLiteral() {
754 LiteralOperatorKind LOK = getLiteralOperatorKind();
755 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
760 const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
761 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
765 CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
766 ParmVarDecl *Param, Expr *SubExpr) {
767 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
768 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
772 CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
773 FieldDecl *Field, QualType T)
774 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
775 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
778 /*FIXME*/ OK_Ordinary, false, false, false, false),
779 Field(Field), Loc(Loc) {
780 assert(Field->hasInClassInitializer());
783 CXXTemporary *CXXTemporary::Create(const ASTContext &C,
784 const CXXDestructorDecl *Destructor) {
785 return new (C) CXXTemporary(Destructor);
788 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
791 assert((SubExpr->getType()->isRecordType() ||
792 SubExpr->getType()->isArrayType()) &&
793 "Expression bound to a temporary must have record or array type!");
795 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
798 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
799 CXXConstructorDecl *Cons,
800 TypeSourceInfo *Type,
801 ArrayRef<Expr*> Args,
802 SourceRange ParenOrBraceRange,
803 bool HadMultipleCandidates,
804 bool ListInitialization,
805 bool StdInitListInitialization,
806 bool ZeroInitialization)
807 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
808 Type->getType().getNonReferenceType(),
809 Type->getTypeLoc().getBeginLoc(),
811 HadMultipleCandidates,
813 StdInitListInitialization,
815 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
819 SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
820 return Type->getTypeLoc().getBeginLoc();
823 SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
824 SourceLocation Loc = getParenOrBraceRange().getEnd();
825 if (Loc.isInvalid() && getNumArgs())
826 Loc = getArg(getNumArgs()-1)->getLocEnd();
830 CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
832 CXXConstructorDecl *D, bool Elidable,
833 ArrayRef<Expr*> Args,
834 bool HadMultipleCandidates,
835 bool ListInitialization,
836 bool StdInitListInitialization,
837 bool ZeroInitialization,
838 ConstructionKind ConstructKind,
839 SourceRange ParenOrBraceRange) {
840 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
842 HadMultipleCandidates, ListInitialization,
843 StdInitListInitialization,
844 ZeroInitialization, ConstructKind,
848 CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
849 QualType T, SourceLocation Loc,
850 CXXConstructorDecl *D, bool elidable,
851 ArrayRef<Expr*> args,
852 bool HadMultipleCandidates,
853 bool ListInitialization,
854 bool StdInitListInitialization,
855 bool ZeroInitialization,
856 ConstructionKind ConstructKind,
857 SourceRange ParenOrBraceRange)
858 : Expr(SC, T, VK_RValue, OK_Ordinary,
859 T->isDependentType(), T->isDependentType(),
860 T->isInstantiationDependentType(),
861 T->containsUnexpandedParameterPack()),
862 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
863 NumArgs(args.size()),
864 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
865 ListInitialization(ListInitialization),
866 StdInitListInitialization(StdInitListInitialization),
867 ZeroInitialization(ZeroInitialization),
868 ConstructKind(ConstructKind), Args(nullptr)
871 Args = new (C) Stmt*[args.size()];
873 for (unsigned i = 0; i != args.size(); ++i) {
874 assert(args[i] && "NULL argument in CXXConstructExpr");
876 if (args[i]->isValueDependent())
877 ExprBits.ValueDependent = true;
878 if (args[i]->isInstantiationDependent())
879 ExprBits.InstantiationDependent = true;
880 if (args[i]->containsUnexpandedParameterPack())
881 ExprBits.ContainsUnexpandedParameterPack = true;
888 LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
889 LambdaCaptureKind Kind, VarDecl *Var,
890 SourceLocation EllipsisLoc)
891 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
895 Bits |= Capture_Implicit;
899 assert(!Var && "'this' capture cannot have a variable!");
903 Bits |= Capture_ByCopy;
906 assert(Var && "capture must have a variable!");
909 assert(!Var && "VLA type capture cannot have a variable!");
910 Bits |= Capture_ByCopy;
913 DeclAndBits.setInt(Bits);
916 LambdaCaptureKind LambdaCapture::getCaptureKind() const {
917 Decl *D = DeclAndBits.getPointer();
918 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
920 return CapByCopy ? LCK_VLAType : LCK_This;
922 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
925 LambdaExpr::LambdaExpr(QualType T,
926 SourceRange IntroducerRange,
927 LambdaCaptureDefault CaptureDefault,
928 SourceLocation CaptureDefaultLoc,
929 ArrayRef<Capture> Captures,
931 bool ExplicitResultType,
932 ArrayRef<Expr *> CaptureInits,
933 ArrayRef<VarDecl *> ArrayIndexVars,
934 ArrayRef<unsigned> ArrayIndexStarts,
935 SourceLocation ClosingBrace,
936 bool ContainsUnexpandedParameterPack)
937 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
938 T->isDependentType(), T->isDependentType(), T->isDependentType(),
939 ContainsUnexpandedParameterPack),
940 IntroducerRange(IntroducerRange),
941 CaptureDefaultLoc(CaptureDefaultLoc),
942 NumCaptures(Captures.size()),
943 CaptureDefault(CaptureDefault),
944 ExplicitParams(ExplicitParams),
945 ExplicitResultType(ExplicitResultType),
946 ClosingBrace(ClosingBrace)
948 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
949 CXXRecordDecl *Class = getLambdaClass();
950 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
952 // FIXME: Propagate "has unexpanded parameter pack" bit.
955 const ASTContext &Context = Class->getASTContext();
956 Data.NumCaptures = NumCaptures;
957 Data.NumExplicitCaptures = 0;
958 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
959 Capture *ToCapture = Data.Captures;
960 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
961 if (Captures[I].isExplicit())
962 ++Data.NumExplicitCaptures;
964 *ToCapture++ = Captures[I];
967 // Copy initialization expressions for the non-static data members.
968 Stmt **Stored = getStoredStmts();
969 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
970 *Stored++ = CaptureInits[I];
972 // Copy the body of the lambda.
973 *Stored++ = getCallOperator()->getBody();
975 // Copy the array index variables, if any.
976 HasArrayIndexVars = !ArrayIndexVars.empty();
977 if (HasArrayIndexVars) {
978 assert(ArrayIndexStarts.size() == NumCaptures);
979 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
980 sizeof(VarDecl *) * ArrayIndexVars.size());
981 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
982 sizeof(unsigned) * Captures.size());
983 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
987 LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
988 CXXRecordDecl *Class,
989 SourceRange IntroducerRange,
990 LambdaCaptureDefault CaptureDefault,
991 SourceLocation CaptureDefaultLoc,
992 ArrayRef<Capture> Captures,
994 bool ExplicitResultType,
995 ArrayRef<Expr *> CaptureInits,
996 ArrayRef<VarDecl *> ArrayIndexVars,
997 ArrayRef<unsigned> ArrayIndexStarts,
998 SourceLocation ClosingBrace,
999 bool ContainsUnexpandedParameterPack) {
1000 // Determine the type of the expression (i.e., the type of the
1001 // function object we're creating).
1002 QualType T = Context.getTypeDeclType(Class);
1004 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
1005 if (!ArrayIndexVars.empty()) {
1006 Size += sizeof(unsigned) * (Captures.size() + 1);
1007 // Realign for following VarDecl array.
1008 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
1009 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
1011 void *Mem = Context.Allocate(Size);
1012 return new (Mem) LambdaExpr(T, IntroducerRange,
1013 CaptureDefault, CaptureDefaultLoc, Captures,
1014 ExplicitParams, ExplicitResultType,
1015 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
1016 ClosingBrace, ContainsUnexpandedParameterPack);
1019 LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1020 unsigned NumCaptures,
1021 unsigned NumArrayIndexVars) {
1022 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
1023 if (NumArrayIndexVars)
1024 Size += sizeof(VarDecl) * NumArrayIndexVars
1025 + sizeof(unsigned) * (NumCaptures + 1);
1026 void *Mem = C.Allocate(Size);
1027 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1030 bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
1031 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
1032 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
1035 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
1036 return getLambdaClass()->getLambdaData().Captures;
1039 LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
1040 return capture_begin() + NumCaptures;
1043 LambdaExpr::capture_range LambdaExpr::captures() const {
1044 return capture_range(capture_begin(), capture_end());
1047 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1048 return capture_begin();
1051 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1052 struct CXXRecordDecl::LambdaDefinitionData &Data
1053 = getLambdaClass()->getLambdaData();
1054 return Data.Captures + Data.NumExplicitCaptures;
1057 LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1058 return capture_range(explicit_capture_begin(), explicit_capture_end());
1061 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1062 return explicit_capture_end();
1065 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1066 return capture_end();
1069 LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1070 return capture_range(implicit_capture_begin(), implicit_capture_end());
1074 LambdaExpr::getCaptureInitIndexVars(capture_init_iterator Iter) const {
1075 assert(HasArrayIndexVars && "No array index-var data?");
1077 unsigned Index = Iter - capture_init_begin();
1078 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1079 "Capture index out-of-range");
1080 VarDecl **IndexVars = getArrayIndexVars();
1081 unsigned *IndexStarts = getArrayIndexStarts();
1082 return llvm::makeArrayRef(IndexVars + IndexStarts[Index],
1083 IndexVars + IndexStarts[Index + 1]);
1086 CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1087 return getType()->getAsCXXRecordDecl();
1090 CXXMethodDecl *LambdaExpr::getCallOperator() const {
1091 CXXRecordDecl *Record = getLambdaClass();
1092 return Record->getLambdaCallOperator();
1095 TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1096 CXXRecordDecl *Record = getLambdaClass();
1097 return Record->getGenericLambdaTemplateParameterList();
1101 CompoundStmt *LambdaExpr::getBody() const {
1102 if (!getStoredStmts()[NumCaptures])
1103 getStoredStmts()[NumCaptures] = getCallOperator()->getBody();
1105 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1108 bool LambdaExpr::isMutable() const {
1109 return !getCallOperator()->isConst();
1112 ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1113 ArrayRef<CleanupObject> objects)
1114 : Expr(ExprWithCleanupsClass, subexpr->getType(),
1115 subexpr->getValueKind(), subexpr->getObjectKind(),
1116 subexpr->isTypeDependent(), subexpr->isValueDependent(),
1117 subexpr->isInstantiationDependent(),
1118 subexpr->containsUnexpandedParameterPack()),
1120 ExprWithCleanupsBits.NumObjects = objects.size();
1121 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1122 getObjectsBuffer()[i] = objects[i];
1125 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
1126 ArrayRef<CleanupObject> objects) {
1127 size_t size = sizeof(ExprWithCleanups)
1128 + objects.size() * sizeof(CleanupObject);
1129 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1130 return new (buffer) ExprWithCleanups(subexpr, objects);
1133 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1134 : Expr(ExprWithCleanupsClass, empty) {
1135 ExprWithCleanupsBits.NumObjects = numObjects;
1138 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1140 unsigned numObjects) {
1141 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1142 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1143 return new (buffer) ExprWithCleanups(empty, numObjects);
1146 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
1147 SourceLocation LParenLoc,
1148 ArrayRef<Expr*> Args,
1149 SourceLocation RParenLoc)
1150 : Expr(CXXUnresolvedConstructExprClass,
1151 Type->getType().getNonReferenceType(),
1152 (Type->getType()->isLValueReferenceType() ? VK_LValue
1153 :Type->getType()->isRValueReferenceType()? VK_XValue
1156 Type->getType()->isDependentType(), true, true,
1157 Type->getType()->containsUnexpandedParameterPack()),
1159 LParenLoc(LParenLoc),
1160 RParenLoc(RParenLoc),
1161 NumArgs(Args.size()) {
1162 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
1163 for (unsigned I = 0; I != Args.size(); ++I) {
1164 if (Args[I]->containsUnexpandedParameterPack())
1165 ExprBits.ContainsUnexpandedParameterPack = true;
1167 StoredArgs[I] = Args[I];
1171 CXXUnresolvedConstructExpr *
1172 CXXUnresolvedConstructExpr::Create(const ASTContext &C,
1173 TypeSourceInfo *Type,
1174 SourceLocation LParenLoc,
1175 ArrayRef<Expr*> Args,
1176 SourceLocation RParenLoc) {
1177 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1178 sizeof(Expr *) * Args.size());
1179 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
1182 CXXUnresolvedConstructExpr *
1183 CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
1184 Stmt::EmptyShell Empty;
1185 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1186 sizeof(Expr *) * NumArgs);
1187 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1190 SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1191 return Type->getTypeLoc().getBeginLoc();
1194 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
1195 Expr *Base, QualType BaseType,
1197 SourceLocation OperatorLoc,
1198 NestedNameSpecifierLoc QualifierLoc,
1199 SourceLocation TemplateKWLoc,
1200 NamedDecl *FirstQualifierFoundInScope,
1201 DeclarationNameInfo MemberNameInfo,
1202 const TemplateArgumentListInfo *TemplateArgs)
1203 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
1204 VK_LValue, OK_Ordinary, true, true, true,
1205 ((Base && Base->containsUnexpandedParameterPack()) ||
1207 QualifierLoc.getNestedNameSpecifier()
1208 ->containsUnexpandedParameterPack()) ||
1209 MemberNameInfo.containsUnexpandedParameterPack())),
1210 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1211 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1212 TemplateKWLoc.isValid()),
1213 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1214 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1215 MemberNameInfo(MemberNameInfo) {
1217 bool Dependent = true;
1218 bool InstantiationDependent = true;
1219 bool ContainsUnexpandedParameterPack = false;
1220 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs,
1222 InstantiationDependent,
1223 ContainsUnexpandedParameterPack);
1224 if (ContainsUnexpandedParameterPack)
1225 ExprBits.ContainsUnexpandedParameterPack = true;
1226 } else if (TemplateKWLoc.isValid()) {
1227 getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
1231 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(const ASTContext &C,
1232 Expr *Base, QualType BaseType,
1234 SourceLocation OperatorLoc,
1235 NestedNameSpecifierLoc QualifierLoc,
1236 NamedDecl *FirstQualifierFoundInScope,
1237 DeclarationNameInfo MemberNameInfo)
1238 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy,
1239 VK_LValue, OK_Ordinary, true, true, true,
1240 ((Base && Base->containsUnexpandedParameterPack()) ||
1242 QualifierLoc.getNestedNameSpecifier()->
1243 containsUnexpandedParameterPack()) ||
1244 MemberNameInfo.containsUnexpandedParameterPack())),
1245 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1246 HasTemplateKWAndArgsInfo(false),
1247 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1248 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1249 MemberNameInfo(MemberNameInfo) { }
1251 CXXDependentScopeMemberExpr *
1252 CXXDependentScopeMemberExpr::Create(const ASTContext &C,
1253 Expr *Base, QualType BaseType, bool IsArrow,
1254 SourceLocation OperatorLoc,
1255 NestedNameSpecifierLoc QualifierLoc,
1256 SourceLocation TemplateKWLoc,
1257 NamedDecl *FirstQualifierFoundInScope,
1258 DeclarationNameInfo MemberNameInfo,
1259 const TemplateArgumentListInfo *TemplateArgs) {
1260 if (!TemplateArgs && !TemplateKWLoc.isValid())
1261 return new (C) CXXDependentScopeMemberExpr(C, Base, BaseType,
1262 IsArrow, OperatorLoc,
1264 FirstQualifierFoundInScope,
1267 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1268 std::size_t size = sizeof(CXXDependentScopeMemberExpr)
1269 + ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
1271 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
1272 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1273 IsArrow, OperatorLoc,
1276 FirstQualifierFoundInScope,
1277 MemberNameInfo, TemplateArgs);
1280 CXXDependentScopeMemberExpr *
1281 CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
1282 bool HasTemplateKWAndArgsInfo,
1283 unsigned NumTemplateArgs) {
1284 if (!HasTemplateKWAndArgsInfo)
1285 return new (C) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
1286 0, SourceLocation(),
1287 NestedNameSpecifierLoc(),
1288 nullptr, DeclarationNameInfo());
1290 std::size_t size = sizeof(CXXDependentScopeMemberExpr) +
1291 ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
1292 void *Mem = C.Allocate(size, llvm::alignOf<CXXDependentScopeMemberExpr>());
1293 CXXDependentScopeMemberExpr *E
1294 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
1295 0, SourceLocation(),
1296 NestedNameSpecifierLoc(),
1297 SourceLocation(), nullptr,
1298 DeclarationNameInfo(), nullptr);
1299 E->HasTemplateKWAndArgsInfo = true;
1303 bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1307 return cast<Expr>(Base)->isImplicitCXXThis();
1310 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1311 UnresolvedSetIterator end) {
1313 NamedDecl *decl = *begin;
1314 if (isa<UnresolvedUsingValueDecl>(decl))
1317 // Unresolved member expressions should only contain methods and
1318 // method templates.
1319 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1322 } while (++begin != end);
1327 UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
1328 bool HasUnresolvedUsing,
1329 Expr *Base, QualType BaseType,
1331 SourceLocation OperatorLoc,
1332 NestedNameSpecifierLoc QualifierLoc,
1333 SourceLocation TemplateKWLoc,
1334 const DeclarationNameInfo &MemberNameInfo,
1335 const TemplateArgumentListInfo *TemplateArgs,
1336 UnresolvedSetIterator Begin,
1337 UnresolvedSetIterator End)
1338 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1339 MemberNameInfo, TemplateArgs, Begin, End,
1341 ((Base && Base->isTypeDependent()) ||
1342 BaseType->isDependentType()),
1343 ((Base && Base->isInstantiationDependent()) ||
1344 BaseType->isInstantiationDependentType()),
1345 // Contains unexpanded parameter pack
1346 ((Base && Base->containsUnexpandedParameterPack()) ||
1347 BaseType->containsUnexpandedParameterPack())),
1348 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1349 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
1351 // Check whether all of the members are non-static member functions,
1352 // and if so, mark give this bound-member type instead of overload type.
1353 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1354 setType(C.BoundMemberTy);
1357 bool UnresolvedMemberExpr::isImplicitAccess() const {
1361 return cast<Expr>(Base)->isImplicitCXXThis();
1364 UnresolvedMemberExpr *
1365 UnresolvedMemberExpr::Create(const ASTContext &C, bool HasUnresolvedUsing,
1366 Expr *Base, QualType BaseType, bool IsArrow,
1367 SourceLocation OperatorLoc,
1368 NestedNameSpecifierLoc QualifierLoc,
1369 SourceLocation TemplateKWLoc,
1370 const DeclarationNameInfo &MemberNameInfo,
1371 const TemplateArgumentListInfo *TemplateArgs,
1372 UnresolvedSetIterator Begin,
1373 UnresolvedSetIterator End) {
1374 std::size_t size = sizeof(UnresolvedMemberExpr);
1376 size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size());
1377 else if (TemplateKWLoc.isValid())
1378 size += ASTTemplateKWAndArgsInfo::sizeFor(0);
1380 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
1381 return new (Mem) UnresolvedMemberExpr(C,
1382 HasUnresolvedUsing, Base, BaseType,
1383 IsArrow, OperatorLoc, QualifierLoc, TemplateKWLoc,
1384 MemberNameInfo, TemplateArgs, Begin, End);
1387 UnresolvedMemberExpr *
1388 UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1389 bool HasTemplateKWAndArgsInfo,
1390 unsigned NumTemplateArgs) {
1391 std::size_t size = sizeof(UnresolvedMemberExpr);
1392 if (HasTemplateKWAndArgsInfo)
1393 size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs);
1395 void *Mem = C.Allocate(size, llvm::alignOf<UnresolvedMemberExpr>());
1396 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
1397 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
1401 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1402 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1404 // If there was a nested name specifier, it names the naming class.
1405 // It can't be dependent: after all, we were actually able to do the
1407 CXXRecordDecl *Record = nullptr;
1408 auto *NNS = getQualifier();
1409 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
1410 const Type *T = getQualifier()->getAsType();
1411 assert(T && "qualifier in member expression does not name type");
1412 Record = T->getAsCXXRecordDecl();
1413 assert(Record && "qualifier in member expression does not name record");
1415 // Otherwise the naming class must have been the base class.
1417 QualType BaseType = getBaseType().getNonReferenceType();
1419 const PointerType *PT = BaseType->getAs<PointerType>();
1420 assert(PT && "base of arrow member access is not pointer");
1421 BaseType = PT->getPointeeType();
1424 Record = BaseType->getAsCXXRecordDecl();
1425 assert(Record && "base of member expression does not name record");
1431 SubstNonTypeTemplateParmPackExpr::
1432 SubstNonTypeTemplateParmPackExpr(QualType T,
1433 NonTypeTemplateParmDecl *Param,
1434 SourceLocation NameLoc,
1435 const TemplateArgument &ArgPack)
1436 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
1437 true, true, true, true),
1438 Param(Param), Arguments(ArgPack.pack_begin()),
1439 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1441 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1442 return TemplateArgument(Arguments, NumArguments);
1445 FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1446 SourceLocation NameLoc,
1448 Decl * const *Params)
1449 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary,
1450 true, true, true, true),
1451 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1453 std::uninitialized_copy(Params, Params + NumParams,
1454 reinterpret_cast<Decl**>(this+1));
1457 FunctionParmPackExpr *
1458 FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
1459 ParmVarDecl *ParamPack, SourceLocation NameLoc,
1460 ArrayRef<Decl *> Params) {
1461 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1462 sizeof(ParmVarDecl*) * Params.size()))
1463 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1466 FunctionParmPackExpr *
1467 FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1468 unsigned NumParams) {
1469 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1470 sizeof(ParmVarDecl*) * NumParams))
1471 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
1474 void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1475 unsigned ManglingNumber) {
1476 // We only need extra state if we have to remember more than just the Stmt.
1480 // We may need to allocate extra storage for the mangling number and the
1481 // extended-by ValueDecl.
1482 if (!State.is<ExtraState *>()) {
1483 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1484 ES->Temporary = State.get<Stmt *>();
1488 auto ES = State.get<ExtraState *>();
1489 ES->ExtendingDecl = ExtendedBy;
1490 ES->ManglingNumber = ManglingNumber;
1493 TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1494 ArrayRef<TypeSourceInfo *> Args,
1495 SourceLocation RParenLoc,
1497 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1498 /*TypeDependent=*/false,
1499 /*ValueDependent=*/false,
1500 /*InstantiationDependent=*/false,
1501 /*ContainsUnexpandedParameterPack=*/false),
1502 Loc(Loc), RParenLoc(RParenLoc)
1504 TypeTraitExprBits.Kind = Kind;
1505 TypeTraitExprBits.Value = Value;
1506 TypeTraitExprBits.NumArgs = Args.size();
1508 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1510 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1511 if (Args[I]->getType()->isDependentType())
1512 setValueDependent(true);
1513 if (Args[I]->getType()->isInstantiationDependentType())
1514 setInstantiationDependent(true);
1515 if (Args[I]->getType()->containsUnexpandedParameterPack())
1516 setContainsUnexpandedParameterPack(true);
1518 ToArgs[I] = Args[I];
1522 TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
1525 ArrayRef<TypeSourceInfo *> Args,
1526 SourceLocation RParenLoc,
1528 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1529 void *Mem = C.Allocate(Size);
1530 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1533 TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
1535 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1536 void *Mem = C.Allocate(Size);
1537 return new (Mem) TypeTraitExpr(EmptyShell());
1540 void ArrayTypeTraitExpr::anchor() { }