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;
300 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(1,
302 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedLookupExpr>());
303 return new (Mem) UnresolvedLookupExpr(C, NamingClass, QualifierLoc,
304 TemplateKWLoc, NameInfo,
305 ADL, /*Overload*/ true, Args,
309 UnresolvedLookupExpr *
310 UnresolvedLookupExpr::CreateEmpty(const ASTContext &C,
311 bool HasTemplateKWAndArgsInfo,
312 unsigned NumTemplateArgs) {
313 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
315 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
316 HasTemplateKWAndArgsInfo, NumTemplateArgs);
317 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedLookupExpr>());
318 UnresolvedLookupExpr *E = new (Mem) UnresolvedLookupExpr(EmptyShell());
319 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
323 OverloadExpr::OverloadExpr(StmtClass K, const ASTContext &C,
324 NestedNameSpecifierLoc QualifierLoc,
325 SourceLocation TemplateKWLoc,
326 const DeclarationNameInfo &NameInfo,
327 const TemplateArgumentListInfo *TemplateArgs,
328 UnresolvedSetIterator Begin,
329 UnresolvedSetIterator End,
331 bool KnownInstantiationDependent,
332 bool KnownContainsUnexpandedParameterPack)
333 : Expr(K, C.OverloadTy, VK_LValue, OK_Ordinary, KnownDependent,
335 (KnownInstantiationDependent ||
336 NameInfo.isInstantiationDependent() ||
338 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
339 (KnownContainsUnexpandedParameterPack ||
340 NameInfo.containsUnexpandedParameterPack() ||
342 QualifierLoc.getNestedNameSpecifier()
343 ->containsUnexpandedParameterPack()))),
344 NameInfo(NameInfo), QualifierLoc(QualifierLoc),
345 Results(nullptr), NumResults(End - Begin),
346 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
347 TemplateKWLoc.isValid()) {
348 NumResults = End - Begin;
350 // Determine whether this expression is type-dependent.
351 for (UnresolvedSetImpl::const_iterator I = Begin; I != End; ++I) {
352 if ((*I)->getDeclContext()->isDependentContext() ||
353 isa<UnresolvedUsingValueDecl>(*I)) {
354 ExprBits.TypeDependent = true;
355 ExprBits.ValueDependent = true;
356 ExprBits.InstantiationDependent = true;
360 Results = static_cast<DeclAccessPair *>(
361 C.Allocate(sizeof(DeclAccessPair) * NumResults,
362 llvm::alignOf<DeclAccessPair>()));
363 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
366 // If we have explicit template arguments, check for dependent
367 // template arguments and whether they contain any unexpanded pack
370 bool Dependent = false;
371 bool InstantiationDependent = false;
372 bool ContainsUnexpandedParameterPack = false;
373 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(
374 TemplateKWLoc, *TemplateArgs, getTrailingTemplateArgumentLoc(),
375 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
378 ExprBits.TypeDependent = true;
379 ExprBits.ValueDependent = true;
381 if (InstantiationDependent)
382 ExprBits.InstantiationDependent = true;
383 if (ContainsUnexpandedParameterPack)
384 ExprBits.ContainsUnexpandedParameterPack = true;
385 } else if (TemplateKWLoc.isValid()) {
386 getTrailingASTTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc);
389 if (isTypeDependent())
390 setType(C.DependentTy);
393 void OverloadExpr::initializeResults(const ASTContext &C,
394 UnresolvedSetIterator Begin,
395 UnresolvedSetIterator End) {
396 assert(!Results && "Results already initialized!");
397 NumResults = End - Begin;
399 Results = static_cast<DeclAccessPair *>(
400 C.Allocate(sizeof(DeclAccessPair) * NumResults,
402 llvm::alignOf<DeclAccessPair>()));
403 memcpy(Results, Begin.I, NumResults * sizeof(DeclAccessPair));
407 CXXRecordDecl *OverloadExpr::getNamingClass() const {
408 if (isa<UnresolvedLookupExpr>(this))
409 return cast<UnresolvedLookupExpr>(this)->getNamingClass();
411 return cast<UnresolvedMemberExpr>(this)->getNamingClass();
414 // DependentScopeDeclRefExpr
415 DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T,
416 NestedNameSpecifierLoc QualifierLoc,
417 SourceLocation TemplateKWLoc,
418 const DeclarationNameInfo &NameInfo,
419 const TemplateArgumentListInfo *Args)
420 : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary,
422 (NameInfo.isInstantiationDependent() ||
424 QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())),
425 (NameInfo.containsUnexpandedParameterPack() ||
427 QualifierLoc.getNestedNameSpecifier()
428 ->containsUnexpandedParameterPack()))),
429 QualifierLoc(QualifierLoc), NameInfo(NameInfo),
430 HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid())
433 bool Dependent = true;
434 bool InstantiationDependent = true;
435 bool ContainsUnexpandedParameterPack
436 = ExprBits.ContainsUnexpandedParameterPack;
437 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
438 TemplateKWLoc, *Args, getTrailingObjects<TemplateArgumentLoc>(),
439 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
440 ExprBits.ContainsUnexpandedParameterPack = ContainsUnexpandedParameterPack;
441 } else if (TemplateKWLoc.isValid()) {
442 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
447 DependentScopeDeclRefExpr *
448 DependentScopeDeclRefExpr::Create(const ASTContext &C,
449 NestedNameSpecifierLoc QualifierLoc,
450 SourceLocation TemplateKWLoc,
451 const DeclarationNameInfo &NameInfo,
452 const TemplateArgumentListInfo *Args) {
453 assert(QualifierLoc && "should be created for dependent qualifiers");
454 bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid();
456 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
457 HasTemplateKWAndArgsInfo, Args ? Args->size() : 0);
458 void *Mem = C.Allocate(Size);
459 return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc,
460 TemplateKWLoc, NameInfo, Args);
463 DependentScopeDeclRefExpr *
464 DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C,
465 bool HasTemplateKWAndArgsInfo,
466 unsigned NumTemplateArgs) {
467 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
469 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
470 HasTemplateKWAndArgsInfo, NumTemplateArgs);
471 void *Mem = C.Allocate(Size);
472 DependentScopeDeclRefExpr *E
473 = new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(),
475 DeclarationNameInfo(), nullptr);
476 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
480 SourceLocation CXXConstructExpr::getLocStart() const {
481 if (isa<CXXTemporaryObjectExpr>(this))
482 return cast<CXXTemporaryObjectExpr>(this)->getLocStart();
486 SourceLocation CXXConstructExpr::getLocEnd() const {
487 if (isa<CXXTemporaryObjectExpr>(this))
488 return cast<CXXTemporaryObjectExpr>(this)->getLocEnd();
490 if (ParenOrBraceRange.isValid())
491 return ParenOrBraceRange.getEnd();
493 SourceLocation End = Loc;
494 for (unsigned I = getNumArgs(); I > 0; --I) {
495 const Expr *Arg = getArg(I-1);
496 if (!Arg->isDefaultArgument()) {
497 SourceLocation NewEnd = Arg->getLocEnd();
498 if (NewEnd.isValid()) {
508 SourceRange CXXOperatorCallExpr::getSourceRangeImpl() const {
509 OverloadedOperatorKind Kind = getOperator();
510 if (Kind == OO_PlusPlus || Kind == OO_MinusMinus) {
511 if (getNumArgs() == 1)
513 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
516 return SourceRange(getArg(0)->getLocStart(), getOperatorLoc());
517 } else if (Kind == OO_Arrow) {
518 return getArg(0)->getSourceRange();
519 } else if (Kind == OO_Call) {
520 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
521 } else if (Kind == OO_Subscript) {
522 return SourceRange(getArg(0)->getLocStart(), getRParenLoc());
523 } else if (getNumArgs() == 1) {
524 return SourceRange(getOperatorLoc(), getArg(0)->getLocEnd());
525 } else if (getNumArgs() == 2) {
526 return SourceRange(getArg(0)->getLocStart(), getArg(1)->getLocEnd());
528 return getOperatorLoc();
532 Expr *CXXMemberCallExpr::getImplicitObjectArgument() const {
533 const Expr *Callee = getCallee()->IgnoreParens();
534 if (const MemberExpr *MemExpr = dyn_cast<MemberExpr>(Callee))
535 return MemExpr->getBase();
536 if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(Callee))
537 if (BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI)
540 // FIXME: Will eventually need to cope with member pointers.
544 CXXMethodDecl *CXXMemberCallExpr::getMethodDecl() const {
545 if (const MemberExpr *MemExpr =
546 dyn_cast<MemberExpr>(getCallee()->IgnoreParens()))
547 return cast<CXXMethodDecl>(MemExpr->getMemberDecl());
549 // FIXME: Will eventually need to cope with member pointers.
554 CXXRecordDecl *CXXMemberCallExpr::getRecordDecl() const {
555 Expr* ThisArg = getImplicitObjectArgument();
559 if (ThisArg->getType()->isAnyPointerType())
560 return ThisArg->getType()->getPointeeType()->getAsCXXRecordDecl();
562 return ThisArg->getType()->getAsCXXRecordDecl();
566 //===----------------------------------------------------------------------===//
568 //===----------------------------------------------------------------------===//
570 /// getCastName - Get the name of the C++ cast being used, e.g.,
571 /// "static_cast", "dynamic_cast", "reinterpret_cast", or
572 /// "const_cast". The returned pointer must not be freed.
573 const char *CXXNamedCastExpr::getCastName() const {
574 switch (getStmtClass()) {
575 case CXXStaticCastExprClass: return "static_cast";
576 case CXXDynamicCastExprClass: return "dynamic_cast";
577 case CXXReinterpretCastExprClass: return "reinterpret_cast";
578 case CXXConstCastExprClass: return "const_cast";
579 default: return "<invalid cast>";
583 CXXStaticCastExpr *CXXStaticCastExpr::Create(const ASTContext &C, QualType T,
585 CastKind K, Expr *Op,
586 const CXXCastPath *BasePath,
587 TypeSourceInfo *WrittenTy,
589 SourceLocation RParenLoc,
590 SourceRange AngleBrackets) {
591 unsigned PathSize = (BasePath ? BasePath->size() : 0);
592 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
593 CXXStaticCastExpr *E =
594 new (Buffer) CXXStaticCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
595 RParenLoc, AngleBrackets);
597 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
598 E->getTrailingObjects<CXXBaseSpecifier *>());
602 CXXStaticCastExpr *CXXStaticCastExpr::CreateEmpty(const ASTContext &C,
604 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
605 return new (Buffer) CXXStaticCastExpr(EmptyShell(), PathSize);
608 CXXDynamicCastExpr *CXXDynamicCastExpr::Create(const ASTContext &C, QualType T,
610 CastKind K, Expr *Op,
611 const CXXCastPath *BasePath,
612 TypeSourceInfo *WrittenTy,
614 SourceLocation RParenLoc,
615 SourceRange AngleBrackets) {
616 unsigned PathSize = (BasePath ? BasePath->size() : 0);
617 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
618 CXXDynamicCastExpr *E =
619 new (Buffer) CXXDynamicCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
620 RParenLoc, AngleBrackets);
622 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
623 E->getTrailingObjects<CXXBaseSpecifier *>());
627 CXXDynamicCastExpr *CXXDynamicCastExpr::CreateEmpty(const ASTContext &C,
629 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
630 return new (Buffer) CXXDynamicCastExpr(EmptyShell(), PathSize);
633 /// isAlwaysNull - Return whether the result of the dynamic_cast is proven
634 /// to always be null. For example:
637 /// struct B final : A { };
640 /// C *f(B* b) { return dynamic_cast<C*>(b); }
641 bool CXXDynamicCastExpr::isAlwaysNull() const
643 QualType SrcType = getSubExpr()->getType();
644 QualType DestType = getType();
646 if (const PointerType *SrcPTy = SrcType->getAs<PointerType>()) {
647 SrcType = SrcPTy->getPointeeType();
648 DestType = DestType->castAs<PointerType>()->getPointeeType();
651 if (DestType->isVoidType())
654 const CXXRecordDecl *SrcRD =
655 cast<CXXRecordDecl>(SrcType->castAs<RecordType>()->getDecl());
657 if (!SrcRD->hasAttr<FinalAttr>())
660 const CXXRecordDecl *DestRD =
661 cast<CXXRecordDecl>(DestType->castAs<RecordType>()->getDecl());
663 return !DestRD->isDerivedFrom(SrcRD);
666 CXXReinterpretCastExpr *
667 CXXReinterpretCastExpr::Create(const ASTContext &C, QualType T,
668 ExprValueKind VK, CastKind K, Expr *Op,
669 const CXXCastPath *BasePath,
670 TypeSourceInfo *WrittenTy, SourceLocation L,
671 SourceLocation RParenLoc,
672 SourceRange AngleBrackets) {
673 unsigned PathSize = (BasePath ? BasePath->size() : 0);
674 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
675 CXXReinterpretCastExpr *E =
676 new (Buffer) CXXReinterpretCastExpr(T, VK, K, Op, PathSize, WrittenTy, L,
677 RParenLoc, AngleBrackets);
679 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
680 E->getTrailingObjects<CXXBaseSpecifier *>());
684 CXXReinterpretCastExpr *
685 CXXReinterpretCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
686 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
687 return new (Buffer) CXXReinterpretCastExpr(EmptyShell(), PathSize);
690 CXXConstCastExpr *CXXConstCastExpr::Create(const ASTContext &C, QualType T,
691 ExprValueKind VK, Expr *Op,
692 TypeSourceInfo *WrittenTy,
694 SourceLocation RParenLoc,
695 SourceRange AngleBrackets) {
696 return new (C) CXXConstCastExpr(T, VK, Op, WrittenTy, L, RParenLoc, AngleBrackets);
699 CXXConstCastExpr *CXXConstCastExpr::CreateEmpty(const ASTContext &C) {
700 return new (C) CXXConstCastExpr(EmptyShell());
703 CXXFunctionalCastExpr *
704 CXXFunctionalCastExpr::Create(const ASTContext &C, QualType T, ExprValueKind VK,
705 TypeSourceInfo *Written, CastKind K, Expr *Op,
706 const CXXCastPath *BasePath,
707 SourceLocation L, SourceLocation R) {
708 unsigned PathSize = (BasePath ? BasePath->size() : 0);
709 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
710 CXXFunctionalCastExpr *E =
711 new (Buffer) CXXFunctionalCastExpr(T, VK, Written, K, Op, PathSize, L, R);
713 std::uninitialized_copy_n(BasePath->data(), BasePath->size(),
714 E->getTrailingObjects<CXXBaseSpecifier *>());
718 CXXFunctionalCastExpr *
719 CXXFunctionalCastExpr::CreateEmpty(const ASTContext &C, unsigned PathSize) {
720 void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize));
721 return new (Buffer) CXXFunctionalCastExpr(EmptyShell(), PathSize);
724 SourceLocation CXXFunctionalCastExpr::getLocStart() const {
725 return getTypeInfoAsWritten()->getTypeLoc().getLocStart();
728 SourceLocation CXXFunctionalCastExpr::getLocEnd() const {
729 return RParenLoc.isValid() ? RParenLoc : getSubExpr()->getLocEnd();
732 UserDefinedLiteral::LiteralOperatorKind
733 UserDefinedLiteral::getLiteralOperatorKind() const {
734 if (getNumArgs() == 0)
736 if (getNumArgs() == 2)
739 assert(getNumArgs() == 1 && "unexpected #args in literal operator call");
741 cast<FunctionDecl>(getCalleeDecl())->getParamDecl(0)->getType();
742 if (ParamTy->isPointerType())
744 if (ParamTy->isAnyCharacterType())
745 return LOK_Character;
746 if (ParamTy->isIntegerType())
748 if (ParamTy->isFloatingType())
751 llvm_unreachable("unknown kind of literal operator");
754 Expr *UserDefinedLiteral::getCookedLiteral() {
756 LiteralOperatorKind LOK = getLiteralOperatorKind();
757 assert(LOK != LOK_Template && LOK != LOK_Raw && "not a cooked literal");
762 const IdentifierInfo *UserDefinedLiteral::getUDSuffix() const {
763 return cast<FunctionDecl>(getCalleeDecl())->getLiteralIdentifier();
767 CXXDefaultArgExpr::Create(const ASTContext &C, SourceLocation Loc,
768 ParmVarDecl *Param, Expr *SubExpr) {
769 void *Mem = C.Allocate(sizeof(CXXDefaultArgExpr) + sizeof(Stmt *));
770 return new (Mem) CXXDefaultArgExpr(CXXDefaultArgExprClass, Loc, Param,
774 CXXDefaultInitExpr::CXXDefaultInitExpr(const ASTContext &C, SourceLocation Loc,
775 FieldDecl *Field, QualType T)
776 : Expr(CXXDefaultInitExprClass, T.getNonLValueExprType(C),
777 T->isLValueReferenceType() ? VK_LValue : T->isRValueReferenceType()
780 /*FIXME*/ OK_Ordinary, false, false, false, false),
781 Field(Field), Loc(Loc) {
782 assert(Field->hasInClassInitializer());
785 CXXTemporary *CXXTemporary::Create(const ASTContext &C,
786 const CXXDestructorDecl *Destructor) {
787 return new (C) CXXTemporary(Destructor);
790 CXXBindTemporaryExpr *CXXBindTemporaryExpr::Create(const ASTContext &C,
793 assert((SubExpr->getType()->isRecordType() ||
794 SubExpr->getType()->isArrayType()) &&
795 "Expression bound to a temporary must have record or array type!");
797 return new (C) CXXBindTemporaryExpr(Temp, SubExpr);
800 CXXTemporaryObjectExpr::CXXTemporaryObjectExpr(const ASTContext &C,
801 CXXConstructorDecl *Cons,
802 TypeSourceInfo *Type,
803 ArrayRef<Expr*> Args,
804 SourceRange ParenOrBraceRange,
805 bool HadMultipleCandidates,
806 bool ListInitialization,
807 bool StdInitListInitialization,
808 bool ZeroInitialization)
809 : CXXConstructExpr(C, CXXTemporaryObjectExprClass,
810 Type->getType().getNonReferenceType(),
811 Type->getTypeLoc().getBeginLoc(),
813 HadMultipleCandidates,
815 StdInitListInitialization,
817 CXXConstructExpr::CK_Complete, ParenOrBraceRange),
821 SourceLocation CXXTemporaryObjectExpr::getLocStart() const {
822 return Type->getTypeLoc().getBeginLoc();
825 SourceLocation CXXTemporaryObjectExpr::getLocEnd() const {
826 SourceLocation Loc = getParenOrBraceRange().getEnd();
827 if (Loc.isInvalid() && getNumArgs())
828 Loc = getArg(getNumArgs()-1)->getLocEnd();
832 CXXConstructExpr *CXXConstructExpr::Create(const ASTContext &C, QualType T,
834 CXXConstructorDecl *D, bool Elidable,
835 ArrayRef<Expr*> Args,
836 bool HadMultipleCandidates,
837 bool ListInitialization,
838 bool StdInitListInitialization,
839 bool ZeroInitialization,
840 ConstructionKind ConstructKind,
841 SourceRange ParenOrBraceRange) {
842 return new (C) CXXConstructExpr(C, CXXConstructExprClass, T, Loc, D,
844 HadMultipleCandidates, ListInitialization,
845 StdInitListInitialization,
846 ZeroInitialization, ConstructKind,
850 CXXConstructExpr::CXXConstructExpr(const ASTContext &C, StmtClass SC,
851 QualType T, SourceLocation Loc,
852 CXXConstructorDecl *D, bool elidable,
853 ArrayRef<Expr*> args,
854 bool HadMultipleCandidates,
855 bool ListInitialization,
856 bool StdInitListInitialization,
857 bool ZeroInitialization,
858 ConstructionKind ConstructKind,
859 SourceRange ParenOrBraceRange)
860 : Expr(SC, T, VK_RValue, OK_Ordinary,
861 T->isDependentType(), T->isDependentType(),
862 T->isInstantiationDependentType(),
863 T->containsUnexpandedParameterPack()),
864 Constructor(D), Loc(Loc), ParenOrBraceRange(ParenOrBraceRange),
865 NumArgs(args.size()),
866 Elidable(elidable), HadMultipleCandidates(HadMultipleCandidates),
867 ListInitialization(ListInitialization),
868 StdInitListInitialization(StdInitListInitialization),
869 ZeroInitialization(ZeroInitialization),
870 ConstructKind(ConstructKind), Args(nullptr)
873 Args = new (C) Stmt*[args.size()];
875 for (unsigned i = 0; i != args.size(); ++i) {
876 assert(args[i] && "NULL argument in CXXConstructExpr");
878 if (args[i]->isValueDependent())
879 ExprBits.ValueDependent = true;
880 if (args[i]->isInstantiationDependent())
881 ExprBits.InstantiationDependent = true;
882 if (args[i]->containsUnexpandedParameterPack())
883 ExprBits.ContainsUnexpandedParameterPack = true;
890 LambdaCapture::LambdaCapture(SourceLocation Loc, bool Implicit,
891 LambdaCaptureKind Kind, VarDecl *Var,
892 SourceLocation EllipsisLoc)
893 : DeclAndBits(Var, 0), Loc(Loc), EllipsisLoc(EllipsisLoc)
897 Bits |= Capture_Implicit;
901 assert(!Var && "'this' capture cannot have a variable!");
905 Bits |= Capture_ByCopy;
908 assert(Var && "capture must have a variable!");
911 assert(!Var && "VLA type capture cannot have a variable!");
912 Bits |= Capture_ByCopy;
915 DeclAndBits.setInt(Bits);
918 LambdaCaptureKind LambdaCapture::getCaptureKind() const {
919 Decl *D = DeclAndBits.getPointer();
920 bool CapByCopy = DeclAndBits.getInt() & Capture_ByCopy;
922 return CapByCopy ? LCK_VLAType : LCK_This;
924 return CapByCopy ? LCK_ByCopy : LCK_ByRef;
927 LambdaExpr::LambdaExpr(QualType T,
928 SourceRange IntroducerRange,
929 LambdaCaptureDefault CaptureDefault,
930 SourceLocation CaptureDefaultLoc,
931 ArrayRef<Capture> Captures,
933 bool ExplicitResultType,
934 ArrayRef<Expr *> CaptureInits,
935 ArrayRef<VarDecl *> ArrayIndexVars,
936 ArrayRef<unsigned> ArrayIndexStarts,
937 SourceLocation ClosingBrace,
938 bool ContainsUnexpandedParameterPack)
939 : Expr(LambdaExprClass, T, VK_RValue, OK_Ordinary,
940 T->isDependentType(), T->isDependentType(), T->isDependentType(),
941 ContainsUnexpandedParameterPack),
942 IntroducerRange(IntroducerRange),
943 CaptureDefaultLoc(CaptureDefaultLoc),
944 NumCaptures(Captures.size()),
945 CaptureDefault(CaptureDefault),
946 ExplicitParams(ExplicitParams),
947 ExplicitResultType(ExplicitResultType),
948 ClosingBrace(ClosingBrace)
950 assert(CaptureInits.size() == Captures.size() && "Wrong number of arguments");
951 CXXRecordDecl *Class = getLambdaClass();
952 CXXRecordDecl::LambdaDefinitionData &Data = Class->getLambdaData();
954 // FIXME: Propagate "has unexpanded parameter pack" bit.
957 const ASTContext &Context = Class->getASTContext();
958 Data.NumCaptures = NumCaptures;
959 Data.NumExplicitCaptures = 0;
960 Data.Captures = (Capture *)Context.Allocate(sizeof(Capture) * NumCaptures);
961 Capture *ToCapture = Data.Captures;
962 for (unsigned I = 0, N = Captures.size(); I != N; ++I) {
963 if (Captures[I].isExplicit())
964 ++Data.NumExplicitCaptures;
966 *ToCapture++ = Captures[I];
969 // Copy initialization expressions for the non-static data members.
970 Stmt **Stored = getStoredStmts();
971 for (unsigned I = 0, N = CaptureInits.size(); I != N; ++I)
972 *Stored++ = CaptureInits[I];
974 // Copy the body of the lambda.
975 *Stored++ = getCallOperator()->getBody();
977 // Copy the array index variables, if any.
978 HasArrayIndexVars = !ArrayIndexVars.empty();
979 if (HasArrayIndexVars) {
980 assert(ArrayIndexStarts.size() == NumCaptures);
981 memcpy(getArrayIndexVars(), ArrayIndexVars.data(),
982 sizeof(VarDecl *) * ArrayIndexVars.size());
983 memcpy(getArrayIndexStarts(), ArrayIndexStarts.data(),
984 sizeof(unsigned) * Captures.size());
985 getArrayIndexStarts()[Captures.size()] = ArrayIndexVars.size();
989 LambdaExpr *LambdaExpr::Create(const ASTContext &Context,
990 CXXRecordDecl *Class,
991 SourceRange IntroducerRange,
992 LambdaCaptureDefault CaptureDefault,
993 SourceLocation CaptureDefaultLoc,
994 ArrayRef<Capture> Captures,
996 bool ExplicitResultType,
997 ArrayRef<Expr *> CaptureInits,
998 ArrayRef<VarDecl *> ArrayIndexVars,
999 ArrayRef<unsigned> ArrayIndexStarts,
1000 SourceLocation ClosingBrace,
1001 bool ContainsUnexpandedParameterPack) {
1002 // Determine the type of the expression (i.e., the type of the
1003 // function object we're creating).
1004 QualType T = Context.getTypeDeclType(Class);
1006 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (Captures.size() + 1);
1007 if (!ArrayIndexVars.empty()) {
1008 Size += sizeof(unsigned) * (Captures.size() + 1);
1009 // Realign for following VarDecl array.
1010 Size = llvm::RoundUpToAlignment(Size, llvm::alignOf<VarDecl*>());
1011 Size += sizeof(VarDecl *) * ArrayIndexVars.size();
1013 void *Mem = Context.Allocate(Size);
1014 return new (Mem) LambdaExpr(T, IntroducerRange,
1015 CaptureDefault, CaptureDefaultLoc, Captures,
1016 ExplicitParams, ExplicitResultType,
1017 CaptureInits, ArrayIndexVars, ArrayIndexStarts,
1018 ClosingBrace, ContainsUnexpandedParameterPack);
1021 LambdaExpr *LambdaExpr::CreateDeserialized(const ASTContext &C,
1022 unsigned NumCaptures,
1023 unsigned NumArrayIndexVars) {
1024 unsigned Size = sizeof(LambdaExpr) + sizeof(Stmt *) * (NumCaptures + 1);
1025 if (NumArrayIndexVars)
1026 Size += sizeof(VarDecl) * NumArrayIndexVars
1027 + sizeof(unsigned) * (NumCaptures + 1);
1028 void *Mem = C.Allocate(Size);
1029 return new (Mem) LambdaExpr(EmptyShell(), NumCaptures, NumArrayIndexVars > 0);
1032 bool LambdaExpr::isInitCapture(const LambdaCapture *C) const {
1033 return (C->capturesVariable() && C->getCapturedVar()->isInitCapture() &&
1034 (getCallOperator() == C->getCapturedVar()->getDeclContext()));
1037 LambdaExpr::capture_iterator LambdaExpr::capture_begin() const {
1038 return getLambdaClass()->getLambdaData().Captures;
1041 LambdaExpr::capture_iterator LambdaExpr::capture_end() const {
1042 return capture_begin() + NumCaptures;
1045 LambdaExpr::capture_range LambdaExpr::captures() const {
1046 return capture_range(capture_begin(), capture_end());
1049 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_begin() const {
1050 return capture_begin();
1053 LambdaExpr::capture_iterator LambdaExpr::explicit_capture_end() const {
1054 struct CXXRecordDecl::LambdaDefinitionData &Data
1055 = getLambdaClass()->getLambdaData();
1056 return Data.Captures + Data.NumExplicitCaptures;
1059 LambdaExpr::capture_range LambdaExpr::explicit_captures() const {
1060 return capture_range(explicit_capture_begin(), explicit_capture_end());
1063 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_begin() const {
1064 return explicit_capture_end();
1067 LambdaExpr::capture_iterator LambdaExpr::implicit_capture_end() const {
1068 return capture_end();
1071 LambdaExpr::capture_range LambdaExpr::implicit_captures() const {
1072 return capture_range(implicit_capture_begin(), implicit_capture_end());
1076 LambdaExpr::getCaptureInitIndexVars(const_capture_init_iterator Iter) const {
1077 assert(HasArrayIndexVars && "No array index-var data?");
1079 unsigned Index = Iter - capture_init_begin();
1080 assert(Index < getLambdaClass()->getLambdaData().NumCaptures &&
1081 "Capture index out-of-range");
1082 VarDecl *const *IndexVars = getArrayIndexVars();
1083 const unsigned *IndexStarts = getArrayIndexStarts();
1084 return llvm::makeArrayRef(IndexVars + IndexStarts[Index],
1085 IndexVars + IndexStarts[Index + 1]);
1088 CXXRecordDecl *LambdaExpr::getLambdaClass() const {
1089 return getType()->getAsCXXRecordDecl();
1092 CXXMethodDecl *LambdaExpr::getCallOperator() const {
1093 CXXRecordDecl *Record = getLambdaClass();
1094 return Record->getLambdaCallOperator();
1097 TemplateParameterList *LambdaExpr::getTemplateParameterList() const {
1098 CXXRecordDecl *Record = getLambdaClass();
1099 return Record->getGenericLambdaTemplateParameterList();
1103 CompoundStmt *LambdaExpr::getBody() const {
1104 // FIXME: this mutation in getBody is bogus. It should be
1105 // initialized in ASTStmtReader::VisitLambdaExpr, but for reasons I
1106 // don't understand, that doesn't work.
1107 if (!getStoredStmts()[NumCaptures])
1108 *const_cast<clang::Stmt **>(&getStoredStmts()[NumCaptures]) =
1109 getCallOperator()->getBody();
1111 return reinterpret_cast<CompoundStmt *>(getStoredStmts()[NumCaptures]);
1114 bool LambdaExpr::isMutable() const {
1115 return !getCallOperator()->isConst();
1118 ExprWithCleanups::ExprWithCleanups(Expr *subexpr,
1119 ArrayRef<CleanupObject> objects)
1120 : Expr(ExprWithCleanupsClass, subexpr->getType(),
1121 subexpr->getValueKind(), subexpr->getObjectKind(),
1122 subexpr->isTypeDependent(), subexpr->isValueDependent(),
1123 subexpr->isInstantiationDependent(),
1124 subexpr->containsUnexpandedParameterPack()),
1126 ExprWithCleanupsBits.NumObjects = objects.size();
1127 for (unsigned i = 0, e = objects.size(); i != e; ++i)
1128 getObjectsBuffer()[i] = objects[i];
1131 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, Expr *subexpr,
1132 ArrayRef<CleanupObject> objects) {
1133 size_t size = sizeof(ExprWithCleanups)
1134 + objects.size() * sizeof(CleanupObject);
1135 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1136 return new (buffer) ExprWithCleanups(subexpr, objects);
1139 ExprWithCleanups::ExprWithCleanups(EmptyShell empty, unsigned numObjects)
1140 : Expr(ExprWithCleanupsClass, empty) {
1141 ExprWithCleanupsBits.NumObjects = numObjects;
1144 ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C,
1146 unsigned numObjects) {
1147 size_t size = sizeof(ExprWithCleanups) + numObjects * sizeof(CleanupObject);
1148 void *buffer = C.Allocate(size, llvm::alignOf<ExprWithCleanups>());
1149 return new (buffer) ExprWithCleanups(empty, numObjects);
1152 CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type,
1153 SourceLocation LParenLoc,
1154 ArrayRef<Expr*> Args,
1155 SourceLocation RParenLoc)
1156 : Expr(CXXUnresolvedConstructExprClass,
1157 Type->getType().getNonReferenceType(),
1158 (Type->getType()->isLValueReferenceType() ? VK_LValue
1159 :Type->getType()->isRValueReferenceType()? VK_XValue
1162 Type->getType()->isDependentType(), true, true,
1163 Type->getType()->containsUnexpandedParameterPack()),
1165 LParenLoc(LParenLoc),
1166 RParenLoc(RParenLoc),
1167 NumArgs(Args.size()) {
1168 Stmt **StoredArgs = reinterpret_cast<Stmt **>(this + 1);
1169 for (unsigned I = 0; I != Args.size(); ++I) {
1170 if (Args[I]->containsUnexpandedParameterPack())
1171 ExprBits.ContainsUnexpandedParameterPack = true;
1173 StoredArgs[I] = Args[I];
1177 CXXUnresolvedConstructExpr *
1178 CXXUnresolvedConstructExpr::Create(const ASTContext &C,
1179 TypeSourceInfo *Type,
1180 SourceLocation LParenLoc,
1181 ArrayRef<Expr*> Args,
1182 SourceLocation RParenLoc) {
1183 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1184 sizeof(Expr *) * Args.size());
1185 return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc);
1188 CXXUnresolvedConstructExpr *
1189 CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) {
1190 Stmt::EmptyShell Empty;
1191 void *Mem = C.Allocate(sizeof(CXXUnresolvedConstructExpr) +
1192 sizeof(Expr *) * NumArgs);
1193 return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs);
1196 SourceLocation CXXUnresolvedConstructExpr::getLocStart() const {
1197 return Type->getTypeLoc().getBeginLoc();
1200 CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr(
1201 const ASTContext &C, Expr *Base, QualType BaseType, bool IsArrow,
1202 SourceLocation OperatorLoc, NestedNameSpecifierLoc QualifierLoc,
1203 SourceLocation TemplateKWLoc, NamedDecl *FirstQualifierFoundInScope,
1204 DeclarationNameInfo MemberNameInfo,
1205 const TemplateArgumentListInfo *TemplateArgs)
1206 : Expr(CXXDependentScopeMemberExprClass, C.DependentTy, VK_LValue,
1207 OK_Ordinary, true, true, true,
1208 ((Base && Base->containsUnexpandedParameterPack()) ||
1210 QualifierLoc.getNestedNameSpecifier()
1211 ->containsUnexpandedParameterPack()) ||
1212 MemberNameInfo.containsUnexpandedParameterPack())),
1213 Base(Base), BaseType(BaseType), IsArrow(IsArrow),
1214 HasTemplateKWAndArgsInfo(TemplateArgs != nullptr ||
1215 TemplateKWLoc.isValid()),
1216 OperatorLoc(OperatorLoc), QualifierLoc(QualifierLoc),
1217 FirstQualifierFoundInScope(FirstQualifierFoundInScope),
1218 MemberNameInfo(MemberNameInfo) {
1220 bool Dependent = true;
1221 bool InstantiationDependent = true;
1222 bool ContainsUnexpandedParameterPack = false;
1223 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1224 TemplateKWLoc, *TemplateArgs, getTrailingObjects<TemplateArgumentLoc>(),
1225 Dependent, InstantiationDependent, ContainsUnexpandedParameterPack);
1226 if (ContainsUnexpandedParameterPack)
1227 ExprBits.ContainsUnexpandedParameterPack = true;
1228 } else if (TemplateKWLoc.isValid()) {
1229 getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom(
1234 CXXDependentScopeMemberExpr *
1235 CXXDependentScopeMemberExpr::Create(const ASTContext &C,
1236 Expr *Base, QualType BaseType, bool IsArrow,
1237 SourceLocation OperatorLoc,
1238 NestedNameSpecifierLoc QualifierLoc,
1239 SourceLocation TemplateKWLoc,
1240 NamedDecl *FirstQualifierFoundInScope,
1241 DeclarationNameInfo MemberNameInfo,
1242 const TemplateArgumentListInfo *TemplateArgs) {
1243 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1244 unsigned NumTemplateArgs = TemplateArgs ? TemplateArgs->size() : 0;
1246 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1247 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1249 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
1250 return new (Mem) CXXDependentScopeMemberExpr(C, Base, BaseType,
1251 IsArrow, OperatorLoc,
1254 FirstQualifierFoundInScope,
1255 MemberNameInfo, TemplateArgs);
1258 CXXDependentScopeMemberExpr *
1259 CXXDependentScopeMemberExpr::CreateEmpty(const ASTContext &C,
1260 bool HasTemplateKWAndArgsInfo,
1261 unsigned NumTemplateArgs) {
1262 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1264 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1265 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1266 void *Mem = C.Allocate(Size, llvm::alignOf<CXXDependentScopeMemberExpr>());
1267 CXXDependentScopeMemberExpr *E
1268 = new (Mem) CXXDependentScopeMemberExpr(C, nullptr, QualType(),
1269 0, SourceLocation(),
1270 NestedNameSpecifierLoc(),
1271 SourceLocation(), nullptr,
1272 DeclarationNameInfo(), nullptr);
1273 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
1277 bool CXXDependentScopeMemberExpr::isImplicitAccess() const {
1281 return cast<Expr>(Base)->isImplicitCXXThis();
1284 static bool hasOnlyNonStaticMemberFunctions(UnresolvedSetIterator begin,
1285 UnresolvedSetIterator end) {
1287 NamedDecl *decl = *begin;
1288 if (isa<UnresolvedUsingValueDecl>(decl))
1291 // Unresolved member expressions should only contain methods and
1292 // method templates.
1293 if (cast<CXXMethodDecl>(decl->getUnderlyingDecl()->getAsFunction())
1296 } while (++begin != end);
1301 UnresolvedMemberExpr::UnresolvedMemberExpr(const ASTContext &C,
1302 bool HasUnresolvedUsing,
1303 Expr *Base, QualType BaseType,
1305 SourceLocation OperatorLoc,
1306 NestedNameSpecifierLoc QualifierLoc,
1307 SourceLocation TemplateKWLoc,
1308 const DeclarationNameInfo &MemberNameInfo,
1309 const TemplateArgumentListInfo *TemplateArgs,
1310 UnresolvedSetIterator Begin,
1311 UnresolvedSetIterator End)
1312 : OverloadExpr(UnresolvedMemberExprClass, C, QualifierLoc, TemplateKWLoc,
1313 MemberNameInfo, TemplateArgs, Begin, End,
1315 ((Base && Base->isTypeDependent()) ||
1316 BaseType->isDependentType()),
1317 ((Base && Base->isInstantiationDependent()) ||
1318 BaseType->isInstantiationDependentType()),
1319 // Contains unexpanded parameter pack
1320 ((Base && Base->containsUnexpandedParameterPack()) ||
1321 BaseType->containsUnexpandedParameterPack())),
1322 IsArrow(IsArrow), HasUnresolvedUsing(HasUnresolvedUsing),
1323 Base(Base), BaseType(BaseType), OperatorLoc(OperatorLoc) {
1325 // Check whether all of the members are non-static member functions,
1326 // and if so, mark give this bound-member type instead of overload type.
1327 if (hasOnlyNonStaticMemberFunctions(Begin, End))
1328 setType(C.BoundMemberTy);
1331 bool UnresolvedMemberExpr::isImplicitAccess() const {
1335 return cast<Expr>(Base)->isImplicitCXXThis();
1338 UnresolvedMemberExpr *UnresolvedMemberExpr::Create(
1339 const ASTContext &C, bool HasUnresolvedUsing, Expr *Base, QualType BaseType,
1340 bool IsArrow, SourceLocation OperatorLoc,
1341 NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc,
1342 const DeclarationNameInfo &MemberNameInfo,
1343 const TemplateArgumentListInfo *TemplateArgs, UnresolvedSetIterator Begin,
1344 UnresolvedSetIterator End) {
1345 bool HasTemplateKWAndArgsInfo = TemplateArgs || TemplateKWLoc.isValid();
1347 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1348 HasTemplateKWAndArgsInfo, TemplateArgs ? TemplateArgs->size() : 0);
1350 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
1351 return new (Mem) UnresolvedMemberExpr(
1352 C, HasUnresolvedUsing, Base, BaseType, IsArrow, OperatorLoc, QualifierLoc,
1353 TemplateKWLoc, MemberNameInfo, TemplateArgs, Begin, End);
1356 UnresolvedMemberExpr *
1357 UnresolvedMemberExpr::CreateEmpty(const ASTContext &C,
1358 bool HasTemplateKWAndArgsInfo,
1359 unsigned NumTemplateArgs) {
1360 assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo);
1362 totalSizeToAlloc<ASTTemplateKWAndArgsInfo, TemplateArgumentLoc>(
1363 HasTemplateKWAndArgsInfo, NumTemplateArgs);
1365 void *Mem = C.Allocate(Size, llvm::alignOf<UnresolvedMemberExpr>());
1366 UnresolvedMemberExpr *E = new (Mem) UnresolvedMemberExpr(EmptyShell());
1367 E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo;
1371 CXXRecordDecl *UnresolvedMemberExpr::getNamingClass() const {
1372 // Unlike for UnresolvedLookupExpr, it is very easy to re-derive this.
1374 // If there was a nested name specifier, it names the naming class.
1375 // It can't be dependent: after all, we were actually able to do the
1377 CXXRecordDecl *Record = nullptr;
1378 auto *NNS = getQualifier();
1379 if (NNS && NNS->getKind() != NestedNameSpecifier::Super) {
1380 const Type *T = getQualifier()->getAsType();
1381 assert(T && "qualifier in member expression does not name type");
1382 Record = T->getAsCXXRecordDecl();
1383 assert(Record && "qualifier in member expression does not name record");
1385 // Otherwise the naming class must have been the base class.
1387 QualType BaseType = getBaseType().getNonReferenceType();
1389 const PointerType *PT = BaseType->getAs<PointerType>();
1390 assert(PT && "base of arrow member access is not pointer");
1391 BaseType = PT->getPointeeType();
1394 Record = BaseType->getAsCXXRecordDecl();
1395 assert(Record && "base of member expression does not name record");
1402 SizeOfPackExpr::Create(ASTContext &Context, SourceLocation OperatorLoc,
1403 NamedDecl *Pack, SourceLocation PackLoc,
1404 SourceLocation RParenLoc,
1405 Optional<unsigned> Length,
1406 ArrayRef<TemplateArgument> PartialArgs) {
1407 void *Storage = Context.Allocate(
1408 sizeof(SizeOfPackExpr) + sizeof(TemplateArgument) * PartialArgs.size());
1409 return new (Storage) SizeOfPackExpr(Context.getSizeType(), OperatorLoc, Pack,
1410 PackLoc, RParenLoc, Length, PartialArgs);
1413 SizeOfPackExpr *SizeOfPackExpr::CreateDeserialized(ASTContext &Context,
1414 unsigned NumPartialArgs) {
1415 void *Storage = Context.Allocate(
1416 sizeof(SizeOfPackExpr) + sizeof(TemplateArgument) * NumPartialArgs);
1417 return new (Storage) SizeOfPackExpr(EmptyShell(), NumPartialArgs);
1420 SubstNonTypeTemplateParmPackExpr::
1421 SubstNonTypeTemplateParmPackExpr(QualType T,
1422 NonTypeTemplateParmDecl *Param,
1423 SourceLocation NameLoc,
1424 const TemplateArgument &ArgPack)
1425 : Expr(SubstNonTypeTemplateParmPackExprClass, T, VK_RValue, OK_Ordinary,
1426 true, true, true, true),
1427 Param(Param), Arguments(ArgPack.pack_begin()),
1428 NumArguments(ArgPack.pack_size()), NameLoc(NameLoc) { }
1430 TemplateArgument SubstNonTypeTemplateParmPackExpr::getArgumentPack() const {
1431 return TemplateArgument(llvm::makeArrayRef(Arguments, NumArguments));
1434 FunctionParmPackExpr::FunctionParmPackExpr(QualType T, ParmVarDecl *ParamPack,
1435 SourceLocation NameLoc,
1437 ParmVarDecl *const *Params)
1438 : Expr(FunctionParmPackExprClass, T, VK_LValue, OK_Ordinary, true, true,
1440 ParamPack(ParamPack), NameLoc(NameLoc), NumParameters(NumParams) {
1442 std::uninitialized_copy(Params, Params + NumParams,
1443 reinterpret_cast<ParmVarDecl **>(this + 1));
1446 FunctionParmPackExpr *
1447 FunctionParmPackExpr::Create(const ASTContext &Context, QualType T,
1448 ParmVarDecl *ParamPack, SourceLocation NameLoc,
1449 ArrayRef<ParmVarDecl *> Params) {
1450 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1451 sizeof(ParmVarDecl*) * Params.size()))
1452 FunctionParmPackExpr(T, ParamPack, NameLoc, Params.size(), Params.data());
1455 FunctionParmPackExpr *
1456 FunctionParmPackExpr::CreateEmpty(const ASTContext &Context,
1457 unsigned NumParams) {
1458 return new (Context.Allocate(sizeof(FunctionParmPackExpr) +
1459 sizeof(ParmVarDecl*) * NumParams))
1460 FunctionParmPackExpr(QualType(), nullptr, SourceLocation(), 0, nullptr);
1463 void MaterializeTemporaryExpr::setExtendingDecl(const ValueDecl *ExtendedBy,
1464 unsigned ManglingNumber) {
1465 // We only need extra state if we have to remember more than just the Stmt.
1469 // We may need to allocate extra storage for the mangling number and the
1470 // extended-by ValueDecl.
1471 if (!State.is<ExtraState *>()) {
1472 auto ES = new (ExtendedBy->getASTContext()) ExtraState;
1473 ES->Temporary = State.get<Stmt *>();
1477 auto ES = State.get<ExtraState *>();
1478 ES->ExtendingDecl = ExtendedBy;
1479 ES->ManglingNumber = ManglingNumber;
1482 TypeTraitExpr::TypeTraitExpr(QualType T, SourceLocation Loc, TypeTrait Kind,
1483 ArrayRef<TypeSourceInfo *> Args,
1484 SourceLocation RParenLoc,
1486 : Expr(TypeTraitExprClass, T, VK_RValue, OK_Ordinary,
1487 /*TypeDependent=*/false,
1488 /*ValueDependent=*/false,
1489 /*InstantiationDependent=*/false,
1490 /*ContainsUnexpandedParameterPack=*/false),
1491 Loc(Loc), RParenLoc(RParenLoc)
1493 TypeTraitExprBits.Kind = Kind;
1494 TypeTraitExprBits.Value = Value;
1495 TypeTraitExprBits.NumArgs = Args.size();
1497 TypeSourceInfo **ToArgs = getTypeSourceInfos();
1499 for (unsigned I = 0, N = Args.size(); I != N; ++I) {
1500 if (Args[I]->getType()->isDependentType())
1501 setValueDependent(true);
1502 if (Args[I]->getType()->isInstantiationDependentType())
1503 setInstantiationDependent(true);
1504 if (Args[I]->getType()->containsUnexpandedParameterPack())
1505 setContainsUnexpandedParameterPack(true);
1507 ToArgs[I] = Args[I];
1511 TypeTraitExpr *TypeTraitExpr::Create(const ASTContext &C, QualType T,
1514 ArrayRef<TypeSourceInfo *> Args,
1515 SourceLocation RParenLoc,
1517 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * Args.size();
1518 void *Mem = C.Allocate(Size);
1519 return new (Mem) TypeTraitExpr(T, Loc, Kind, Args, RParenLoc, Value);
1522 TypeTraitExpr *TypeTraitExpr::CreateDeserialized(const ASTContext &C,
1524 unsigned Size = sizeof(TypeTraitExpr) + sizeof(TypeSourceInfo*) * NumArgs;
1525 void *Mem = C.Allocate(Size);
1526 return new (Mem) TypeTraitExpr(EmptyShell());
1529 void ArrayTypeTraitExpr::anchor() { }