From 49ee964e190fdf8611057eb4403539fb0e3e594b Mon Sep 17 00:00:00 2001 From: Bruno Ricci Date: Mon, 7 Jan 2019 14:27:04 +0000 Subject: [PATCH] [AST][NFC] Pack DependentScopeDeclRefExpr and CXXUnresolvedConstructExpr Use the newly available space in the bit-fields of Stmt. This saves 1 pointer per DependentScopeDeclRefExpr/CXXUnresolvedConstructExpr. Additionally rename "TypeSourceInfo *Type;" to "TypeSourceInfo *TSI;" as was done in D56022 (r350003) (but this is an internal detail anyway), and clang-format both classes. NFC. llvm-svn: 350525 --- clang/include/clang/AST/ExprCXX.h | 93 +++++++++--------- clang/include/clang/AST/Stmt.h | 24 +++++ clang/lib/AST/ExprCXX.cpp | 110 ++++++++++------------ clang/lib/Serialization/ASTReaderStmt.cpp | 2 +- clang/lib/Serialization/ASTWriterStmt.cpp | 4 +- 5 files changed, 123 insertions(+), 110 deletions(-) diff --git a/clang/include/clang/AST/ExprCXX.h b/clang/include/clang/AST/ExprCXX.h index 3f22d8e9c525..f7d5d13637c6 100644 --- a/clang/include/clang/AST/ExprCXX.h +++ b/clang/include/clang/AST/ExprCXX.h @@ -2936,6 +2936,10 @@ class DependentScopeDeclRefExpr final private llvm::TrailingObjects { + friend class ASTStmtReader; + friend class ASTStmtWriter; + friend TrailingObjects; + /// The nested-name-specifier that qualifies this unresolved /// declaration name. NestedNameSpecifierLoc QualifierLoc; @@ -2943,32 +2947,26 @@ class DependentScopeDeclRefExpr final /// The name of the entity we will be referencing. DeclarationNameInfo NameInfo; - /// Whether the name includes info for explicit template - /// keyword and arguments. - bool HasTemplateKWAndArgsInfo; - - DependentScopeDeclRefExpr(QualType T, - NestedNameSpecifierLoc QualifierLoc, + DependentScopeDeclRefExpr(QualType Ty, NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo, const TemplateArgumentListInfo *Args); size_t numTrailingObjects(OverloadToken) const { - return HasTemplateKWAndArgsInfo ? 1 : 0; + return hasTemplateKWAndArgsInfo(); + } + + bool hasTemplateKWAndArgsInfo() const { + return DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo; } public: - friend class ASTStmtReader; - friend class ASTStmtWriter; - friend TrailingObjects; + static DependentScopeDeclRefExpr * + Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, + SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo, + const TemplateArgumentListInfo *TemplateArgs); - static DependentScopeDeclRefExpr *Create(const ASTContext &C, - NestedNameSpecifierLoc QualifierLoc, - SourceLocation TemplateKWLoc, - const DeclarationNameInfo &NameInfo, - const TemplateArgumentListInfo *TemplateArgs); - - static DependentScopeDeclRefExpr *CreateEmpty(const ASTContext &C, + static DependentScopeDeclRefExpr *CreateEmpty(const ASTContext &Context, bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs); @@ -2996,21 +2994,24 @@ public: /// Retrieve the location of the template keyword preceding /// this name, if any. SourceLocation getTemplateKeywordLoc() const { - if (!HasTemplateKWAndArgsInfo) return SourceLocation(); + if (!hasTemplateKWAndArgsInfo()) + return SourceLocation(); return getTrailingObjects()->TemplateKWLoc; } /// Retrieve the location of the left angle bracket starting the /// explicit template argument list following the name, if any. SourceLocation getLAngleLoc() const { - if (!HasTemplateKWAndArgsInfo) return SourceLocation(); + if (!hasTemplateKWAndArgsInfo()) + return SourceLocation(); return getTrailingObjects()->LAngleLoc; } /// Retrieve the location of the right angle bracket ending the /// explicit template argument list following the name, if any. SourceLocation getRAngleLoc() const { - if (!HasTemplateKWAndArgsInfo) return SourceLocation(); + if (!hasTemplateKWAndArgsInfo()) + return SourceLocation(); return getTrailingObjects()->RAngleLoc; } @@ -3164,7 +3165,7 @@ class CXXUnresolvedConstructExpr final friend TrailingObjects; /// The type being constructed. - TypeSourceInfo *Type = nullptr; + TypeSourceInfo *TSI; /// The location of the left parentheses ('('). SourceLocation LParenLoc; @@ -3172,34 +3173,31 @@ class CXXUnresolvedConstructExpr final /// The location of the right parentheses (')'). SourceLocation RParenLoc; - /// The number of arguments used to construct the type. - unsigned NumArgs; - - CXXUnresolvedConstructExpr(TypeSourceInfo *Type, - SourceLocation LParenLoc, - ArrayRef Args, - SourceLocation RParenLoc); + CXXUnresolvedConstructExpr(TypeSourceInfo *TSI, SourceLocation LParenLoc, + ArrayRef Args, SourceLocation RParenLoc); CXXUnresolvedConstructExpr(EmptyShell Empty, unsigned NumArgs) - : Expr(CXXUnresolvedConstructExprClass, Empty), NumArgs(NumArgs) {} + : Expr(CXXUnresolvedConstructExprClass, Empty) { + CXXUnresolvedConstructExprBits.NumArgs = NumArgs; + } public: - static CXXUnresolvedConstructExpr *Create(const ASTContext &C, + static CXXUnresolvedConstructExpr *Create(const ASTContext &Context, TypeSourceInfo *Type, SourceLocation LParenLoc, - ArrayRef Args, + ArrayRef Args, SourceLocation RParenLoc); - static CXXUnresolvedConstructExpr *CreateEmpty(const ASTContext &C, + static CXXUnresolvedConstructExpr *CreateEmpty(const ASTContext &Context, unsigned NumArgs); /// Retrieve the type that is being constructed, as specified /// in the source code. - QualType getTypeAsWritten() const { return Type->getType(); } + QualType getTypeAsWritten() const { return TSI->getType(); } /// Retrieve the type source information for the type being /// constructed. - TypeSourceInfo *getTypeSourceInfo() const { return Type; } + TypeSourceInfo *getTypeSourceInfo() const { return TSI; } /// Retrieve the location of the left parentheses ('(') that /// precedes the argument list. @@ -3217,46 +3215,43 @@ public: bool isListInitialization() const { return LParenLoc.isInvalid(); } /// Retrieve the number of arguments. - unsigned arg_size() const { return NumArgs; } + unsigned arg_size() const { return CXXUnresolvedConstructExprBits.NumArgs; } using arg_iterator = Expr **; using arg_range = llvm::iterator_range; arg_iterator arg_begin() { return getTrailingObjects(); } - arg_iterator arg_end() { return arg_begin() + NumArgs; } + arg_iterator arg_end() { return arg_begin() + arg_size(); } arg_range arguments() { return arg_range(arg_begin(), arg_end()); } using const_arg_iterator = const Expr* const *; using const_arg_range = llvm::iterator_range; const_arg_iterator arg_begin() const { return getTrailingObjects(); } - const_arg_iterator arg_end() const { - return arg_begin() + NumArgs; - } + const_arg_iterator arg_end() const { return arg_begin() + arg_size(); } const_arg_range arguments() const { return const_arg_range(arg_begin(), arg_end()); } Expr *getArg(unsigned I) { - assert(I < NumArgs && "Argument index out-of-range"); - return *(arg_begin() + I); + assert(I < arg_size() && "Argument index out-of-range"); + return arg_begin()[I]; } const Expr *getArg(unsigned I) const { - assert(I < NumArgs && "Argument index out-of-range"); - return *(arg_begin() + I); + assert(I < arg_size() && "Argument index out-of-range"); + return arg_begin()[I]; } void setArg(unsigned I, Expr *E) { - assert(I < NumArgs && "Argument index out-of-range"); - *(arg_begin() + I) = E; + assert(I < arg_size() && "Argument index out-of-range"); + arg_begin()[I] = E; } SourceLocation getBeginLoc() const LLVM_READONLY; - SourceLocation getEndLoc() const LLVM_READONLY { - if (!RParenLoc.isValid() && NumArgs > 0) - return getArg(NumArgs - 1)->getEndLoc(); + if (!RParenLoc.isValid() && arg_size() > 0) + return getArg(arg_size() - 1)->getEndLoc(); return RParenLoc; } @@ -3267,7 +3262,7 @@ public: // Iterators child_range children() { auto **begin = reinterpret_cast(arg_begin()); - return child_range(begin, begin + NumArgs); + return child_range(begin, begin + arg_size()); } }; diff --git a/clang/include/clang/AST/Stmt.h b/clang/include/clang/AST/Stmt.h index c83af5162642..4515d527a92e 100644 --- a/clang/include/clang/AST/Stmt.h +++ b/clang/include/clang/AST/Stmt.h @@ -655,6 +655,18 @@ protected: unsigned NumArgs : 32 - 8 - 1 - NumExprBits; }; + class DependentScopeDeclRefExprBitfields { + friend class ASTStmtReader; + friend class ASTStmtWriter; + friend class DependentScopeDeclRefExpr; + + unsigned : NumExprBits; + + /// Whether the name includes info for explicit template + /// keyword and arguments. + unsigned HasTemplateKWAndArgsInfo : 1; + }; + class CXXConstructExprBitfields { friend class ASTStmtReader; friend class CXXConstructExpr; @@ -683,6 +695,16 @@ protected: unsigned NumObjects : 32 - 1 - NumExprBits; }; + class CXXUnresolvedConstructExprBitfields { + friend class ASTStmtReader; + friend class CXXUnresolvedConstructExpr; + + unsigned : NumExprBits; + + /// The number of arguments used to construct the type. + unsigned NumArgs; + }; + //===--- C++ Coroutines TS bitfields classes ---===// class CoawaitExprBitfields { @@ -765,8 +787,10 @@ protected: CXXDefaultInitExprBitfields CXXDefaultInitExprBits; CXXDeleteExprBitfields CXXDeleteExprBits; TypeTraitExprBitfields TypeTraitExprBits; + DependentScopeDeclRefExprBitfields DependentScopeDeclRefExprBits; CXXConstructExprBitfields CXXConstructExprBits; ExprWithCleanupsBitfields ExprWithCleanupsBits; + CXXUnresolvedConstructExprBitfields CXXUnresolvedConstructExprBits; // C++ Coroutines TS expressions CoawaitExprBitfields CoawaitBits; diff --git a/clang/lib/AST/ExprCXX.cpp b/clang/lib/AST/ExprCXX.cpp index 83af29a638a6..7aa80c0392af 100644 --- a/clang/lib/AST/ExprCXX.cpp +++ b/clang/lib/AST/ExprCXX.cpp @@ -385,23 +385,22 @@ CXXRecordDecl *OverloadExpr::getNamingClass() const { } // DependentScopeDeclRefExpr -DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T, - NestedNameSpecifierLoc QualifierLoc, - SourceLocation TemplateKWLoc, - const DeclarationNameInfo &NameInfo, - const TemplateArgumentListInfo *Args) - : Expr(DependentScopeDeclRefExprClass, T, VK_LValue, OK_Ordinary, - true, true, - (NameInfo.isInstantiationDependent() || - (QualifierLoc && - QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())), - (NameInfo.containsUnexpandedParameterPack() || - (QualifierLoc && - QualifierLoc.getNestedNameSpecifier() - ->containsUnexpandedParameterPack()))), - QualifierLoc(QualifierLoc), NameInfo(NameInfo), - HasTemplateKWAndArgsInfo(Args != nullptr || TemplateKWLoc.isValid()) -{ +DependentScopeDeclRefExpr::DependentScopeDeclRefExpr( + QualType Ty, NestedNameSpecifierLoc QualifierLoc, + SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo, + const TemplateArgumentListInfo *Args) + : Expr( + DependentScopeDeclRefExprClass, Ty, VK_LValue, OK_Ordinary, true, + true, + (NameInfo.isInstantiationDependent() || + (QualifierLoc && + QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent())), + (NameInfo.containsUnexpandedParameterPack() || + (QualifierLoc && QualifierLoc.getNestedNameSpecifier() + ->containsUnexpandedParameterPack()))), + QualifierLoc(QualifierLoc), NameInfo(NameInfo) { + DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo = + (Args != nullptr) || TemplateKWLoc.isValid(); if (Args) { bool Dependent = true; bool InstantiationDependent = true; @@ -417,36 +416,34 @@ DependentScopeDeclRefExpr::DependentScopeDeclRefExpr(QualType T, } } -DependentScopeDeclRefExpr * -DependentScopeDeclRefExpr::Create(const ASTContext &C, - NestedNameSpecifierLoc QualifierLoc, - SourceLocation TemplateKWLoc, - const DeclarationNameInfo &NameInfo, - const TemplateArgumentListInfo *Args) { +DependentScopeDeclRefExpr *DependentScopeDeclRefExpr::Create( + const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, + SourceLocation TemplateKWLoc, const DeclarationNameInfo &NameInfo, + const TemplateArgumentListInfo *Args) { assert(QualifierLoc && "should be created for dependent qualifiers"); bool HasTemplateKWAndArgsInfo = Args || TemplateKWLoc.isValid(); std::size_t Size = totalSizeToAlloc( HasTemplateKWAndArgsInfo, Args ? Args->size() : 0); - void *Mem = C.Allocate(Size); - return new (Mem) DependentScopeDeclRefExpr(C.DependentTy, QualifierLoc, + void *Mem = Context.Allocate(Size); + return new (Mem) DependentScopeDeclRefExpr(Context.DependentTy, QualifierLoc, TemplateKWLoc, NameInfo, Args); } DependentScopeDeclRefExpr * -DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &C, +DependentScopeDeclRefExpr::CreateEmpty(const ASTContext &Context, bool HasTemplateKWAndArgsInfo, unsigned NumTemplateArgs) { assert(NumTemplateArgs == 0 || HasTemplateKWAndArgsInfo); std::size_t Size = totalSizeToAlloc( HasTemplateKWAndArgsInfo, NumTemplateArgs); - void *Mem = C.Allocate(Size); - auto *E = - new (Mem) DependentScopeDeclRefExpr(QualType(), NestedNameSpecifierLoc(), - SourceLocation(), - DeclarationNameInfo(), nullptr); - E->HasTemplateKWAndArgsInfo = HasTemplateKWAndArgsInfo; + void *Mem = Context.Allocate(Size); + auto *E = new (Mem) DependentScopeDeclRefExpr( + QualType(), NestedNameSpecifierLoc(), SourceLocation(), + DeclarationNameInfo(), nullptr); + E->DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo = + HasTemplateKWAndArgsInfo; return E; } @@ -1216,22 +1213,22 @@ ExprWithCleanups *ExprWithCleanups::Create(const ASTContext &C, return new (buffer) ExprWithCleanups(empty, numObjects); } -CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type, - SourceLocation LParenLoc, - ArrayRef Args, - SourceLocation RParenLoc) +CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *TSI, + SourceLocation LParenLoc, + ArrayRef Args, + SourceLocation RParenLoc) : Expr(CXXUnresolvedConstructExprClass, - Type->getType().getNonReferenceType(), - (Type->getType()->isLValueReferenceType() + TSI->getType().getNonReferenceType(), + (TSI->getType()->isLValueReferenceType() ? VK_LValue - : Type->getType()->isRValueReferenceType() ? VK_XValue - : VK_RValue), + : TSI->getType()->isRValueReferenceType() ? VK_XValue + : VK_RValue), OK_Ordinary, - Type->getType()->isDependentType() || - Type->getType()->getContainedDeducedType(), - true, true, Type->getType()->containsUnexpandedParameterPack()), - Type(Type), LParenLoc(LParenLoc), RParenLoc(RParenLoc), - NumArgs(Args.size()) { + TSI->getType()->isDependentType() || + TSI->getType()->getContainedDeducedType(), + true, true, TSI->getType()->containsUnexpandedParameterPack()), + TSI(TSI), LParenLoc(LParenLoc), RParenLoc(RParenLoc) { + CXXUnresolvedConstructExprBits.NumArgs = Args.size(); auto **StoredArgs = getTrailingObjects(); for (unsigned I = 0; I != Args.size(); ++I) { if (Args[I]->containsUnexpandedParameterPack()) @@ -1241,25 +1238,22 @@ CXXUnresolvedConstructExpr::CXXUnresolvedConstructExpr(TypeSourceInfo *Type, } } -CXXUnresolvedConstructExpr * -CXXUnresolvedConstructExpr::Create(const ASTContext &C, - TypeSourceInfo *Type, - SourceLocation LParenLoc, - ArrayRef Args, - SourceLocation RParenLoc) { - void *Mem = C.Allocate(totalSizeToAlloc(Args.size())); - return new (Mem) CXXUnresolvedConstructExpr(Type, LParenLoc, Args, RParenLoc); +CXXUnresolvedConstructExpr *CXXUnresolvedConstructExpr::Create( + const ASTContext &Context, TypeSourceInfo *TSI, SourceLocation LParenLoc, + ArrayRef Args, SourceLocation RParenLoc) { + void *Mem = Context.Allocate(totalSizeToAlloc(Args.size())); + return new (Mem) CXXUnresolvedConstructExpr(TSI, LParenLoc, Args, RParenLoc); } CXXUnresolvedConstructExpr * -CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &C, unsigned NumArgs) { - Stmt::EmptyShell Empty; - void *Mem = C.Allocate(totalSizeToAlloc(NumArgs)); - return new (Mem) CXXUnresolvedConstructExpr(Empty, NumArgs); +CXXUnresolvedConstructExpr::CreateEmpty(const ASTContext &Context, + unsigned NumArgs) { + void *Mem = Context.Allocate(totalSizeToAlloc(NumArgs)); + return new (Mem) CXXUnresolvedConstructExpr(EmptyShell(), NumArgs); } SourceLocation CXXUnresolvedConstructExpr::getBeginLoc() const { - return Type->getTypeLoc().getBeginLoc(); + return TSI->getTypeLoc().getBeginLoc(); } CXXDependentScopeMemberExpr::CXXDependentScopeMemberExpr( diff --git a/clang/lib/Serialization/ASTReaderStmt.cpp b/clang/lib/Serialization/ASTReaderStmt.cpp index f2c623eb6b00..e789e20f5762 100644 --- a/clang/lib/Serialization/ASTReaderStmt.cpp +++ b/clang/lib/Serialization/ASTReaderStmt.cpp @@ -1612,7 +1612,7 @@ ASTStmtReader::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E) { Record.skipInts(1); for (unsigned I = 0, N = E->arg_size(); I != N; ++I) E->setArg(I, Record.readSubExpr()); - E->Type = GetTypeSourceInfo(); + E->TSI = GetTypeSourceInfo(); E->setLParenLoc(ReadSourceLocation()); E->setRParenLoc(ReadSourceLocation()); } diff --git a/clang/lib/Serialization/ASTWriterStmt.cpp b/clang/lib/Serialization/ASTWriterStmt.cpp index f0e1e10b5eb2..26a01706dcff 100644 --- a/clang/lib/Serialization/ASTWriterStmt.cpp +++ b/clang/lib/Serialization/ASTWriterStmt.cpp @@ -1583,8 +1583,8 @@ ASTStmtWriter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { // Don't emit anything here, HasTemplateKWAndArgsInfo must be // emitted first. - Record.push_back(E->HasTemplateKWAndArgsInfo); - if (E->HasTemplateKWAndArgsInfo) { + Record.push_back(E->DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo); + if (E->DependentScopeDeclRefExprBits.HasTemplateKWAndArgsInfo) { const ASTTemplateKWAndArgsInfo &ArgInfo = *E->getTrailingObjects(); Record.push_back(ArgInfo.NumTemplateArgs);