Skip to content

Commit 3eb2da7

Browse files
committed
[NFC] [C++20] [Modules] Simplify ActOnModuleImport by merging Path and Parition
Reviewed By: iains Differential Revision: https://reviews.llvm.org/D120793
1 parent 63c9aca commit 3eb2da7

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

clang/include/clang/Sema/Sema.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2994,11 +2994,11 @@ class Sema final {
29942994
/// \param ExportLoc The location of the 'export' keyword, if any.
29952995
/// \param ImportLoc The location of the 'import' keyword.
29962996
/// \param Path The module toplevel name as an access path.
2997-
/// \param Partition The module partition name as an access path.
2997+
/// \param IsPartition If the name is for a partition.
29982998
DeclResult ActOnModuleImport(SourceLocation StartLoc,
29992999
SourceLocation ExportLoc,
30003000
SourceLocation ImportLoc, ModuleIdPath Path,
3001-
ModuleIdPath Partition = {});
3001+
bool IsPartition = false);
30023002
DeclResult ActOnModuleImport(SourceLocation StartLoc,
30033003
SourceLocation ExportLoc,
30043004
SourceLocation ImportLoc, Module *M,

clang/lib/Parse/Parser.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,7 +2420,7 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
24202420

24212421
// For C++20 modules, we can have "name" or ":Partition name" as valid input.
24222422
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
2423-
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Partition;
2423+
bool IsPartition = false;
24242424
Module *HeaderUnit = nullptr;
24252425
if (Tok.is(tok::header_name)) {
24262426
// This is a header import that the preprocessor decided we should skip
@@ -2435,10 +2435,12 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
24352435
SourceLocation ColonLoc = ConsumeToken();
24362436
if (!getLangOpts().CPlusPlusModules)
24372437
Diag(ColonLoc, diag::err_unsupported_module_partition)
2438-
<< SourceRange(ColonLoc, Partition.back().second);
2438+
<< SourceRange(ColonLoc, Path.back().second);
24392439
// Recover by leaving partition empty.
2440-
else if (ParseModuleName(ColonLoc, Partition, /*IsImport*/ true))
2440+
else if (ParseModuleName(ColonLoc, Path, /*IsImport*/ true))
24412441
return nullptr;
2442+
else
2443+
IsPartition = true;
24422444
} else {
24432445
if (ParseModuleName(ImportLoc, Path, /*IsImport*/ true))
24442446
return nullptr;
@@ -2457,15 +2459,14 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
24572459

24582460
// Diagnose mis-imports.
24592461
bool SeenError = true;
2460-
bool HasPart = !Partition.empty();
24612462
switch (ImportState) {
24622463
case Sema::ModuleImportState::ImportAllowed:
24632464
SeenError = false;
24642465
break;
24652466
case Sema::ModuleImportState::FirstDecl:
24662467
case Sema::ModuleImportState::NotACXX20Module:
24672468
// We can only import a partition within a module purview.
2468-
if (HasPart)
2469+
if (IsPartition)
24692470
Diag(ImportLoc, diag::err_partition_import_outside_module);
24702471
else
24712472
SeenError = false;
@@ -2474,7 +2475,7 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
24742475
// We can only have pre-processor directives in the global module
24752476
// fragment. We can, however have a header unit import here.
24762477
if (!HeaderUnit)
2477-
Diag(ImportLoc, diag::err_import_in_wrong_fragment) << HasPart << 0;
2478+
Diag(ImportLoc, diag::err_import_in_wrong_fragment) << IsPartition << 0;
24782479
else
24792480
SeenError = false;
24802481
break;
@@ -2485,7 +2486,7 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
24852486
SeenError = false;
24862487
break;
24872488
case Sema::ModuleImportState::PrivateFragment:
2488-
Diag(ImportLoc, diag::err_import_in_wrong_fragment) << HasPart << 1;
2489+
Diag(ImportLoc, diag::err_import_in_wrong_fragment) << IsPartition << 1;
24892490
break;
24902491
}
24912492
if (SeenError) {
@@ -2497,9 +2498,9 @@ Decl *Parser::ParseModuleImport(SourceLocation AtLoc,
24972498
if (HeaderUnit)
24982499
Import =
24992500
Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, HeaderUnit);
2500-
else if (!Path.empty() || !Partition.empty())
2501+
else if (!Path.empty())
25012502
Import = Actions.ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Path,
2502-
Partition);
2503+
IsPartition);
25032504
ExpectAndConsumeSemi(diag::err_module_expected_semi);
25042505
if (Import.isInvalid())
25052506
return nullptr;

clang/lib/Sema/SemaModule.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,10 @@ Sema::ActOnPrivateModuleFragmentDecl(SourceLocation ModuleLoc,
365365
DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
366366
SourceLocation ExportLoc,
367367
SourceLocation ImportLoc, ModuleIdPath Path,
368-
ModuleIdPath Partition) {
368+
bool IsPartition) {
369369

370-
bool IsPartition = !Partition.empty();
371370
bool Cxx20Mode = getLangOpts().CPlusPlusModules || getLangOpts().ModulesTS;
372371
assert((!IsPartition || Cxx20Mode) && "partition seen in non-C++20 code?");
373-
assert((!IsPartition || Path.empty()) &&
374-
"trying to import a partition with its named module specified?");
375372

376373
// For a C++20 module name, flatten into a single identifier with the source
377374
// location of the first component.
@@ -386,9 +383,9 @@ DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
386383
// otherwise, the name of the importing named module.
387384
ModuleName = NamedMod->getPrimaryModuleInterfaceName().str();
388385
ModuleName += ":";
389-
ModuleName += stringFromPath(Partition);
390-
ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Partition[0].second};
391-
Partition = ModuleIdPath(ModuleNameLoc);
386+
ModuleName += stringFromPath(Path);
387+
ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
388+
Path = ModuleIdPath(ModuleNameLoc);
392389
} else if (Cxx20Mode) {
393390
ModuleName = stringFromPath(Path);
394391
ModuleNameLoc = {PP.getIdentifierInfo(ModuleName), Path[0].second};
@@ -410,13 +407,11 @@ DeclResult Sema::ActOnModuleImport(SourceLocation StartLoc,
410407
}
411408

412409
Module *Mod = getModuleLoader().loadModule(
413-
ImportLoc, IsPartition ? Partition : Path, Module::AllVisible,
414-
/*IsInclusionDirective=*/false);
410+
ImportLoc, Path, Module::AllVisible, /*IsInclusionDirective=*/false);
415411
if (!Mod)
416412
return true;
417413

418-
return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod,
419-
IsPartition ? Partition : Path);
414+
return ActOnModuleImport(StartLoc, ExportLoc, ImportLoc, Mod, Path);
420415
}
421416

422417
/// Determine whether \p D is lexically within an export-declaration.

0 commit comments

Comments
 (0)