@@ -1667,115 +1667,112 @@ class SourceFileScope {
1667
1667
}
1668
1668
};
1669
1669
1670
- } // end anonymous namespace
1670
+ // An RAII object that constructs a \c SILGenModule instance.
1671
+ // On destruction, delayed definitions are automatically emitted.
1672
+ class SILGenModuleRAII {
1673
+ SILGenModule SGM;
1671
1674
1672
- llvm::Expected< bool >
1673
- SILGenSourceFileRequest::evaluate (Evaluator &evaluator,
1674
- SILGenModule * SGM, SourceFile * sf) const {
1675
- SourceFileScope scope (*SGM, sf);
1676
- for (Decl *D : sf-> getTopLevelDecls ()) {
1677
- FrontendStatsTracer StatsTracer (SGM-> getASTContext (). Stats , " SILgen-decl" , D);
1678
- SGM-> visit (D);
1679
- }
1675
+ public:
1676
+ void emitSourceFile (SourceFile *sf) {
1677
+ SourceFileScope scope ( SGM, sf);
1678
+ for (Decl *D : sf-> getTopLevelDecls ()) {
1679
+ FrontendStatsTracer StatsTracer (SGM. getASTContext (). Stats ,
1680
+ " SILgen-decl" , D);
1681
+ SGM. visit (D);
1682
+ }
1680
1683
1681
- for (TypeDecl *TD : sf->LocalTypeDecls ) {
1682
- FrontendStatsTracer StatsTracer (SGM->getASTContext ().Stats , " SILgen-tydecl" , TD);
1683
- // FIXME: Delayed parsing would prevent these types from being added to the
1684
- // module in the first place.
1685
- if (TD->getDeclContext ()->getInnermostSkippedFunctionContext ())
1686
- continue ;
1687
- SGM->visit (TD);
1684
+ for (TypeDecl *TD : sf->LocalTypeDecls ) {
1685
+ FrontendStatsTracer StatsTracer (SGM.getASTContext ().Stats ,
1686
+ " SILgen-tydecl" , TD);
1687
+ // FIXME: Delayed parsing would prevent these types from being added to
1688
+ // the module in the first place.
1689
+ if (TD->getDeclContext ()->getInnermostSkippedFunctionContext ())
1690
+ continue ;
1691
+ SGM.visit (TD);
1692
+ }
1688
1693
}
1689
- return true ;
1690
- }
1691
1694
1692
- // ===----------------------------------------------------------------------===//
1693
- // SILModule::constructSIL method implementation
1694
- // ===----------------------------------------------------------------------===//
1695
+ SILGenModuleRAII (SILModule &M, ModuleDecl *SM) : SGM{M, SM} {}
1696
+
1697
+ ~SILGenModuleRAII () {
1698
+ // Emit any delayed definitions that were forced.
1699
+ // Emitting these may in turn force more definitions, so we have to take
1700
+ // care to keep pumping the queues.
1701
+ while (!SGM.forcedFunctions .empty ()
1702
+ || !SGM.pendingConformances .empty ()) {
1703
+ while (!SGM.forcedFunctions .empty ()) {
1704
+ auto &front = SGM.forcedFunctions .front ();
1705
+ front.second .emitter (SGM.getFunction (front.first , ForDefinition));
1706
+ SGM.forcedFunctions .pop_front ();
1707
+ }
1708
+ while (!SGM.pendingConformances .empty ()) {
1709
+ SGM.getWitnessTable (SGM.pendingConformances .front ());
1710
+ SGM.pendingConformances .pop_front ();
1711
+ }
1712
+ }
1713
+ }
1714
+ };
1715
+ } // end anonymous namespace
1695
1716
1696
- std::unique_ptr<SILModule>
1697
- SILModule::constructSIL (ModuleDecl *mod, TypeConverter &tc,
1698
- const SILOptions &options, FileUnit *SF) {
1699
- FrontendStatsTracer tracer (mod->getASTContext ().Stats , " SILGen" );
1700
- const DeclContext *DC;
1701
- if (SF) {
1702
- DC = SF;
1703
- } else {
1704
- DC = mod;
1717
+ llvm::Expected<std::unique_ptr<SILModule>>
1718
+ SILGenSourceFileRequest::evaluate (Evaluator &evaluator,
1719
+ SILGenDescriptor desc) const {
1720
+ auto *unit = desc.context .get <FileUnit *>();
1721
+ auto *mod = unit->getParentModule ();
1722
+ auto M = std::unique_ptr<SILModule>(
1723
+ new SILModule (mod, desc.conv , desc.opts , unit, /* wholeModule*/ false ));
1724
+ SILGenModuleRAII scope (*M, mod);
1725
+
1726
+ if (auto *file = dyn_cast<SourceFile>(unit)) {
1727
+ scope.emitSourceFile (file);
1728
+ } else if (auto *file = dyn_cast<SerializedASTFile>(unit)) {
1729
+ if (file->isSIB ())
1730
+ M->getSILLoader ()->getAllForModule (mod->getName (), file);
1705
1731
}
1706
1732
1707
- std::unique_ptr<SILModule> M (
1708
- new SILModule (mod, tc, options, DC, /* wholeModule*/ SF == nullptr ));
1709
- SILGenModule SGM (*M, mod);
1710
-
1711
- if (SF) {
1712
- if (auto *file = dyn_cast<SourceFile>(SF)) {
1713
- (void )evaluateOrDefault (file->getASTContext ().evaluator ,
1714
- SILGenSourceFileRequest{&SGM, file}, false );
1715
- } else if (auto *file = dyn_cast<SerializedASTFile>(SF)) {
1716
- if (file->isSIB ())
1717
- M->getSILLoader ()->getAllForModule (mod->getName (), file);
1718
- }
1719
- } else {
1720
- for (auto file : mod->getFiles ()) {
1721
- auto nextSF = dyn_cast<SourceFile>(file);
1722
- if (!nextSF || nextSF->ASTStage != SourceFile::TypeChecked)
1723
- continue ;
1724
- (void )evaluateOrDefault (nextSF->getASTContext ().evaluator ,
1725
- SILGenSourceFileRequest{&SGM, nextSF}, false );
1726
- }
1733
+ return std::move (M);
1734
+ }
1727
1735
1728
- // Also make sure to process any intermediate files that may contain SIL
1729
- bool hasSIB = std::any_of (mod->getFiles ().begin (),
1730
- mod->getFiles ().end (),
1731
- [](const FileUnit *File) -> bool {
1732
- auto *SASTF = dyn_cast<SerializedASTFile>(File);
1733
- return SASTF && SASTF->isSIB ();
1734
- });
1735
- if (hasSIB)
1736
- M->getSILLoader ()->getAllForModule (mod->getName (), nullptr );
1736
+ llvm::Expected<std::unique_ptr<SILModule>>
1737
+ SILGenWholeModuleRequest::evaluate (Evaluator &evaluator,
1738
+ SILGenDescriptor desc) const {
1739
+ auto *mod = desc.context .get <ModuleDecl *>();
1740
+ auto M = std::unique_ptr<SILModule>(
1741
+ new SILModule (mod, desc.conv , desc.opts , mod, /* wholeModule*/ true ));
1742
+ SILGenModuleRAII scope (*M, mod);
1743
+
1744
+ for (auto file : mod->getFiles ()) {
1745
+ auto nextSF = dyn_cast<SourceFile>(file);
1746
+ if (!nextSF || nextSF->ASTStage != SourceFile::TypeChecked)
1747
+ continue ;
1748
+ scope.emitSourceFile (nextSF);
1737
1749
}
1738
1750
1739
- // Emit any delayed definitions that were forced.
1740
- // Emitting these may in turn force more definitions, so we have to take care
1741
- // to keep pumping the queues.
1742
- while (!SGM.forcedFunctions .empty ()
1743
- || !SGM.pendingConformances .empty ()) {
1744
- while (!SGM.forcedFunctions .empty ()) {
1745
- auto &front = SGM.forcedFunctions .front ();
1746
- front.second .emitter (SGM.getFunction (front.first , ForDefinition));
1747
- SGM.forcedFunctions .pop_front ();
1748
- }
1749
- while (!SGM.pendingConformances .empty ()) {
1750
- SGM.getWitnessTable (SGM.pendingConformances .front ());
1751
- SGM.pendingConformances .pop_front ();
1752
- }
1753
- }
1751
+ // Also make sure to process any intermediate files that may contain SIL
1752
+ bool hasSIB = std::any_of (mod->getFiles ().begin (),
1753
+ mod->getFiles ().end (),
1754
+ [](const FileUnit *File) -> bool {
1755
+ auto *SASTF = dyn_cast<SerializedASTFile>(File);
1756
+ return SASTF && SASTF->isSIB ();
1757
+ });
1758
+ if (hasSIB)
1759
+ M->getSILLoader ()->getAllForModule (mod->getName (), nullptr );
1754
1760
1755
- return M ;
1761
+ return std::move (M) ;
1756
1762
}
1757
1763
1758
1764
std::unique_ptr<SILModule>
1759
1765
swift::performSILGeneration (ModuleDecl *mod, Lowering::TypeConverter &tc,
1760
1766
const SILOptions &options) {
1761
1767
auto desc = SILGenDescriptor::forWholeModule (mod, tc, options);
1762
- return llvm::cantFail (mod->getASTContext ().evaluator (GenerateSILRequest{desc}));
1768
+ return llvm::cantFail (
1769
+ mod->getASTContext ().evaluator (SILGenWholeModuleRequest{desc}));
1763
1770
}
1764
1771
1765
1772
std::unique_ptr<SILModule>
1766
1773
swift::performSILGeneration (FileUnit &sf, Lowering::TypeConverter &tc,
1767
1774
const SILOptions &options) {
1768
1775
auto desc = SILGenDescriptor::forFile (sf, tc, options);
1769
- return llvm::cantFail (sf.getASTContext ().evaluator (GenerateSILRequest{desc}));
1770
- }
1771
-
1772
- llvm::Expected<std::unique_ptr<SILModule>>
1773
- GenerateSILRequest::evaluate (Evaluator &evaluator, SILGenDescriptor sgd) const {
1774
- if (auto *MD = sgd.context .dyn_cast <ModuleDecl *>()) {
1775
- return SILModule::constructSIL (MD, sgd.conv , sgd.opts , nullptr );
1776
- } else {
1777
- auto *SF = sgd.context .get <FileUnit *>();
1778
- return SILModule::constructSIL (SF->getParentModule (),
1779
- sgd.conv , sgd.opts , SF);
1780
- }
1776
+ return llvm::cantFail (
1777
+ sf.getASTContext ().evaluator (SILGenSourceFileRequest{desc}));
1781
1778
}
0 commit comments