Skip to content

Commit 99873b3

Browse files
committed
[NFC] [AST] Introduce Decl::isInAnotherModuleUnit and Decl::shouldEmitInExternalSource
Motivated by the review process in #75912. This can also help to simplify the code slightly.
1 parent 1645976 commit 99873b3

File tree

7 files changed

+29
-28
lines changed

7 files changed

+29
-28
lines changed

clang/include/clang/AST/DeclBase.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,13 @@ class alignas(8) Decl {
670670
/// Whether this declaration comes from another module unit.
671671
bool isInAnotherModuleUnit() const;
672672

673+
/// Whether the definition of the declaration should be emitted in external
674+
/// sources.
675+
bool shouldEmitInExternalSource() const;
676+
677+
/// Whether this declaration comes from a named module;
678+
bool isInNamedModule() const;
679+
673680
/// Whether this declaration comes from explicit global module.
674681
bool isFromExplicitGlobalModule() const;
675682

clang/lib/AST/ASTContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12018,7 +12018,7 @@ bool ASTContext::DeclMustBeEmitted(const Decl *D) {
1201812018
return false;
1201912019

1202012020
// Variables in other module units shouldn't be forced to be emitted.
12021-
if (VD->isInAnotherModuleUnit())
12021+
if (VD->shouldEmitInExternalSource())
1202212022
return false;
1202312023

1202412024
// Variables that can be needed in other TUs are required.

clang/lib/AST/Decl.cpp

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,13 +1174,6 @@ Linkage NamedDecl::getLinkageInternal() const {
11741174
.getLinkage();
11751175
}
11761176

1177-
/// Determine whether D is attached to a named module.
1178-
static bool isInNamedModule(const NamedDecl *D) {
1179-
if (auto *M = D->getOwningModule())
1180-
return M->isNamedModule();
1181-
return false;
1182-
}
1183-
11841177
static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) {
11851178
// FIXME: Handle isModulePrivate.
11861179
switch (D->getModuleOwnershipKind()) {
@@ -1190,7 +1183,7 @@ static bool isExportedFromModuleInterfaceUnit(const NamedDecl *D) {
11901183
return false;
11911184
case Decl::ModuleOwnershipKind::Visible:
11921185
case Decl::ModuleOwnershipKind::VisibleWhenImported:
1193-
return isInNamedModule(D);
1186+
return D->isInNamedModule();
11941187
}
11951188
llvm_unreachable("unexpected module ownership kind");
11961189
}
@@ -1208,7 +1201,7 @@ Linkage NamedDecl::getFormalLinkage() const {
12081201
// [basic.namespace.general]/p2
12091202
// A namespace is never attached to a named module and never has a name with
12101203
// module linkage.
1211-
if (isInNamedModule(this) && InternalLinkage == Linkage::External &&
1204+
if (isInNamedModule() && InternalLinkage == Linkage::External &&
12121205
!isExportedFromModuleInterfaceUnit(
12131206
cast<NamedDecl>(this->getCanonicalDecl())) &&
12141207
!isa<NamespaceDecl>(this))

clang/lib/AST/DeclBase.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,23 +1100,22 @@ bool Decl::isInExportDeclContext() const {
11001100
bool Decl::isInAnotherModuleUnit() const {
11011101
auto *M = getOwningModule();
11021102

1103-
if (!M)
1103+
if (!M || !M->isNamedModule())
11041104
return false;
11051105

1106-
M = M->getTopLevelModule();
1107-
// FIXME: It is problematic if the header module lives in another module
1108-
// unit. Consider to fix this by techniques like
1109-
// ExternalASTSource::hasExternalDefinitions.
1110-
if (M->isHeaderLikeModule())
1111-
return false;
1106+
return M != getASTContext().getCurrentNamedModule();
1107+
}
11121108

1113-
// A global module without parent implies that we're parsing the global
1114-
// module. So it can't be in another module unit.
1115-
if (M->isGlobalModule())
1109+
bool Decl::shouldEmitInExternalSource() const {
1110+
ExternalASTSource *Source = getASTContext().getExternalSource();
1111+
if (!Source)
11161112
return false;
11171113

1118-
assert(M->isNamedModule() && "New module kind?");
1119-
return M != getASTContext().getCurrentNamedModule();
1114+
return Source->hasExternalDefinitions(this) == ExternalASTSource::EK_Always;
1115+
}
1116+
1117+
bool Decl::isInNamedModule() const {
1118+
return getOwningModule() && getOwningModule()->isNamedModule();
11201119
}
11211120

11221121
bool Decl::isFromExplicitGlobalModule() const {

clang/lib/CodeGen/CGVTables.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1200,7 +1200,7 @@ bool CodeGenVTables::isVTableExternal(const CXXRecordDecl *RD) {
12001200
assert(Def && "The body of the key function is not assigned to Def?");
12011201
// If the non-inline key function comes from another module unit, the vtable
12021202
// must be defined there.
1203-
return Def->isInAnotherModuleUnit() && !Def->isInlineSpecified();
1203+
return Def->shouldEmitInExternalSource() && !Def->isInlineSpecified();
12041204
}
12051205

12061206
/// Given that we're currently at the end of the translation unit, and

clang/lib/Sema/SemaDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10244,7 +10244,7 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
1024410244
// check at the end of the TU (or when the PMF starts) to see that we
1024510245
// have a definition at that point.
1024610246
if (isInline && !D.isFunctionDefinition() && getLangOpts().CPlusPlus20 &&
10247-
NewFD->hasOwningModule() && NewFD->getOwningModule()->isNamedModule()) {
10247+
NewFD->isInNamedModule()) {
1024810248
PendingInlineFuncDecls.insert(NewFD);
1024910249
}
1025010250
}

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6508,10 +6508,12 @@ void ASTRecordWriter::AddCXXDefinitionData(const CXXRecordDecl *D) {
65086508
// computed.
65096509
Record->push_back(D->getODRHash());
65106510

6511-
bool ModulesDebugInfo =
6512-
Writer->Context->getLangOpts().ModulesDebugInfo && !D->isDependentType();
6513-
Record->push_back(ModulesDebugInfo);
6514-
if (ModulesDebugInfo)
6511+
bool ModulesCodegen =
6512+
!D->isDependentType() &&
6513+
(Writer->Context->getLangOpts().ModulesDebugInfo ||
6514+
D->isInNamedModule());
6515+
Record->push_back(ModulesCodegen);
6516+
if (ModulesCodegen)
65156517
Writer->AddDeclRef(D, Writer->ModularCodegenDecls);
65166518

65176519
// IsLambda bit is already saved.

0 commit comments

Comments
 (0)