@@ -2016,134 +2016,93 @@ void SILGenModule::visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
20162016 // Nothing to do for #error/#warning; they've already been emitted.
20172017}
20182018
2019- namespace {
2019+ void SILGenModule::emitSourceFile (SourceFile *sf) {
2020+ // Type-check the file if we haven't already.
2021+ performTypeChecking (*sf);
20202022
2021- // An RAII object that constructs a \c SILGenModule instance.
2022- // On destruction, delayed definitions are automatically emitted.
2023- class SILGenModuleRAII {
2024- SILGenModule SGM;
2025-
2026- public:
2027- void emitSourceFile (SourceFile *sf) {
2028- // Type-check the file if we haven't already.
2029- performTypeChecking (*sf);
2030-
2031- if (sf->isScriptMode ()) {
2032- SGM.emitEntryPoint (sf);
2033- }
2034-
2035- for (auto *D : sf->getTopLevelDecls ()) {
2036- // Emit auxiliary decls.
2037- D->visitAuxiliaryDecls ([&](Decl *auxiliaryDecl) {
2038- FrontendStatsTracer StatsTracer (SGM.getASTContext ().Stats ,
2039- " SILgen-decl" , auxiliaryDecl);
2040- SGM.visit (auxiliaryDecl);
2041- });
2023+ if (sf->isScriptMode ()) {
2024+ emitEntryPoint (sf);
2025+ }
20422026
2043- FrontendStatsTracer StatsTracer (SGM.getASTContext ().Stats ,
2044- " SILgen-decl" , D);
2045- SGM.visit (D);
2046- }
2027+ for (auto *D : sf->getTopLevelDecls ()) {
2028+ // Emit auxiliary decls.
2029+ D->visitAuxiliaryDecls ([&](Decl *auxiliaryDecl) {
2030+ visit (auxiliaryDecl);
2031+ });
20472032
2048- // FIXME: Visit macro-generated extensions separately.
2049- //
2050- // The code below that visits auxiliary decls of the top-level
2051- // decls in the source file does not work for nested types with
2052- // attached conformance macros:
2053- // ```
2054- // struct Outer {
2055- // @AddConformance struct Inner {}
2056- // }
2057- // ```
2058- // Because the attached-to decl is not at the top-level. To fix this,
2059- // visit the macro-generated conformances that are recorded in the
2060- // synthesized file unit to cover all macro-generated extension decls.
2061- if (auto *synthesizedFile = sf->getSynthesizedFile ()) {
2062- for (auto *D : synthesizedFile->getTopLevelDecls ()) {
2063- if (!isa<ExtensionDecl>(D))
2064- continue ;
2065-
2066- auto *sf = D->getInnermostDeclContext ()->getParentSourceFile ();
2067- if (sf->getFulfilledMacroRole () != MacroRole::Conformance &&
2068- sf->getFulfilledMacroRole () != MacroRole::Extension)
2069- continue ;
2070-
2071- FrontendStatsTracer StatsTracer (SGM.getASTContext ().Stats ,
2072- " SILgen-decl" , D);
2073- SGM.visit (D);
2074- }
2075- }
2033+ visit (D);
2034+ }
20762035
2077- for (Decl *D : sf->getHoistedDecls ()) {
2078- FrontendStatsTracer StatsTracer (SGM.getASTContext ().Stats ,
2079- " SILgen-decl" , D);
2080- SGM.visit (D);
2081- }
2036+ // FIXME: Visit macro-generated extensions separately.
2037+ //
2038+ // The code below that visits auxiliary decls of the top-level
2039+ // decls in the source file does not work for nested types with
2040+ // attached conformance macros:
2041+ // ```
2042+ // struct Outer {
2043+ // @AddConformance struct Inner {}
2044+ // }
2045+ // ```
2046+ // Because the attached-to decl is not at the top-level. To fix this,
2047+ // visit the macro-generated conformances that are recorded in the
2048+ // synthesized file unit to cover all macro-generated extension decls.
2049+ if (auto *synthesizedFile = sf->getSynthesizedFile ()) {
2050+ for (auto *D : synthesizedFile->getTopLevelDecls ()) {
2051+ if (!isa<ExtensionDecl>(D))
2052+ continue ;
20822053
2083- for (TypeDecl *TD : sf->getLocalTypeDecls ()) {
2084- FrontendStatsTracer StatsTracer (SGM.getASTContext ().Stats ,
2085- " SILgen-tydecl" , TD);
2086- // FIXME: Delayed parsing would prevent these types from being added to
2087- // the module in the first place.
2088- if (TD->getDeclContext ()->getInnermostSkippedFunctionContext ())
2054+ auto *sf = D->getInnermostDeclContext ()->getParentSourceFile ();
2055+ if (sf->getFulfilledMacroRole () != MacroRole::Conformance &&
2056+ sf->getFulfilledMacroRole () != MacroRole::Extension)
20892057 continue ;
2090- SGM.visit (TD);
2091- }
20922058
2093- // If the source file contains an artificial main, emit the implicit
2094- // top-level code.
2095- if (auto *mainDecl = sf->getMainDecl ()) {
2096- if (isa<FuncDecl>(mainDecl) &&
2097- static_cast <FuncDecl *>(mainDecl)->hasAsync ())
2098- emitSILFunctionDefinition (
2099- SILDeclRef::getAsyncMainDeclEntryPoint (mainDecl));
2100- emitSILFunctionDefinition (SILDeclRef::getMainDeclEntryPoint (mainDecl));
2059+ visit (D);
21012060 }
21022061 }
21032062
2104- void emitSymbolSource (SymbolSource Source) {
2105- switch (Source.kind ) {
2106- case SymbolSource::Kind::SIL:
2107- emitSILFunctionDefinition (Source.getSILDeclRef ());
2108- break ;
2109- case SymbolSource::Kind::Global:
2110- SGM.addGlobalVariable (Source.getGlobal ());
2111- break ;
2112- case SymbolSource::Kind::IR:
2113- llvm_unreachable (" Unimplemented: Emission of LinkEntities" );
2114- case SymbolSource::Kind::Unknown:
2115- case SymbolSource::Kind::LinkerDirective:
2116- // Nothing to do
2117- break ;
2118- }
2063+ for (Decl *D : sf->getHoistedDecls ()) {
2064+ visit (D);
21192065 }
21202066
2121- void emitSILFunctionDefinition (SILDeclRef ref) {
2122- SGM.emitFunctionDefinition (ref, SGM.getFunction (ref, ForDefinition));
2067+ for (TypeDecl *TD : sf->getLocalTypeDecls ()) {
2068+ // FIXME: Delayed parsing would prevent these types from being added to
2069+ // the module in the first place.
2070+ if (TD->getDeclContext ()->getInnermostSkippedFunctionContext ())
2071+ continue ;
2072+ visit (TD);
21232073 }
21242074
2125- explicit SILGenModuleRAII (SILModule &M) : SGM{M, M.getSwiftModule ()} {}
2126-
2127- ~SILGenModuleRAII () {
2128- // Emit any delayed definitions that were forced.
2129- // Emitting these may in turn force more definitions, so we have to take
2130- // care to keep pumping the queues.
2131- while (!SGM.pendingForcedFunctions .empty ()
2132- || !SGM.pendingConformances .empty ()) {
2133- while (!SGM.pendingForcedFunctions .empty ()) {
2134- auto &front = SGM.pendingForcedFunctions .front ();
2135- SGM.emitFunctionDefinition (
2136- front, SGM.getEmittedFunction (front, ForDefinition));
2137- SGM.pendingForcedFunctions .pop_front ();
2138- }
2139- while (!SGM.pendingConformances .empty ()) {
2140- (void )SGM.getWitnessTable (SGM.pendingConformances .front ());
2141- SGM.pendingConformances .pop_front ();
2142- }
2075+ // If the source file contains an artificial main, emit the implicit
2076+ // top-level code.
2077+ if (auto *mainDecl = sf->getMainDecl ()) {
2078+ if (isa<FuncDecl>(mainDecl) &&
2079+ static_cast <FuncDecl *>(mainDecl)->hasAsync ()) {
2080+ auto ref = SILDeclRef::getAsyncMainDeclEntryPoint (mainDecl);
2081+ emitFunctionDefinition (ref, getFunction (ref, ForDefinition));
21432082 }
2083+ auto ref = SILDeclRef::getMainDeclEntryPoint (mainDecl);
2084+ emitFunctionDefinition (ref, getFunction (ref, ForDefinition));
21442085 }
2145- };
2146- } // end anonymous namespace
2086+ }
2087+
2088+ void SILGenModule::emitSymbolSource (SymbolSource Source) {
2089+ switch (Source.kind ) {
2090+ case SymbolSource::Kind::SIL: {
2091+ auto ref = Source.getSILDeclRef ();
2092+ emitFunctionDefinition (ref, getFunction (ref, ForDefinition));
2093+ break ;
2094+ }
2095+ case SymbolSource::Kind::Global:
2096+ addGlobalVariable (Source.getGlobal ());
2097+ break ;
2098+ case SymbolSource::Kind::IR:
2099+ llvm_unreachable (" Unimplemented: Emission of LinkEntities" );
2100+ case SymbolSource::Kind::Unknown:
2101+ case SymbolSource::Kind::LinkerDirective:
2102+ // Nothing to do
2103+ break ;
2104+ }
2105+ }
21472106
21482107std::unique_ptr<SILModule>
21492108ASTLoweringRequest::evaluate (Evaluator &evaluator,
@@ -2156,28 +2115,31 @@ ASTLoweringRequest::evaluate(Evaluator &evaluator,
21562115 auto silMod = SILModule::createEmptyModule (desc.context , desc.conv ,
21572116 desc.opts , desc.irgenOptions );
21582117
2118+ auto &ctx = silMod->getASTContext ();
2119+ FrontendStatsTracer tracer (ctx.Stats , " SILGen" );
2120+
21592121 // If all function bodies are being skipped there's no reason to do any
21602122 // SIL generation.
21612123 if (desc.opts .SkipFunctionBodies == FunctionBodySkipping::All)
21622124 return silMod;
21632125
21642126 // Skip emitting SIL if there's been any compilation errors
2165- if (silMod-> getASTContext () .hadError () &&
2166- silMod-> getASTContext () .LangOpts .AllowModuleWithCompilerErrors )
2127+ if (ctx .hadError () &&
2128+ ctx .LangOpts .AllowModuleWithCompilerErrors )
21672129 return silMod;
21682130
2169- SILGenModuleRAII scope (*silMod);
2131+ SILGenModule SGM (*silMod, silMod-> getSwiftModule () );
21702132
21712133 // Emit a specific set of SILDeclRefs if needed.
21722134 if (auto Sources = desc.SourcesToEmit ) {
21732135 for (auto Source : *Sources)
2174- scope .emitSymbolSource (std::move (Source));
2136+ SGM .emitSymbolSource (std::move (Source));
21752137 }
21762138
21772139 // Emit any whole-files needed.
21782140 for (auto file : desc.getFilesToEmit ()) {
21792141 if (auto *nextSF = dyn_cast<SourceFile>(file))
2180- scope .emitSourceFile (nextSF);
2142+ SGM .emitSourceFile (nextSF);
21812143 }
21822144
21832145 // Also make sure to process any intermediate files that may contain SIL.
@@ -2191,6 +2153,23 @@ ASTLoweringRequest::evaluate(Evaluator &evaluator,
21912153 primary);
21922154 }
21932155
2156+ // Emit any delayed definitions that were forced.
2157+ // Emitting these may in turn force more definitions, so we have to take
2158+ // care to keep pumping the queues.
2159+ while (!SGM.pendingForcedFunctions .empty ()
2160+ || !SGM.pendingConformances .empty ()) {
2161+ while (!SGM.pendingForcedFunctions .empty ()) {
2162+ auto &front = SGM.pendingForcedFunctions .front ();
2163+ SGM.emitFunctionDefinition (
2164+ front, SGM.getEmittedFunction (front, ForDefinition));
2165+ SGM.pendingForcedFunctions .pop_front ();
2166+ }
2167+ while (!SGM.pendingConformances .empty ()) {
2168+ (void )SGM.getWitnessTable (SGM.pendingConformances .front ());
2169+ SGM.pendingConformances .pop_front ();
2170+ }
2171+ }
2172+
21942173 return silMod;
21952174}
21962175
0 commit comments