@@ -2016,134 +2016,93 @@ void SILGenModule::visitPoundDiagnosticDecl(PoundDiagnosticDecl *PDD) {
2016
2016
// Nothing to do for #error/#warning; they've already been emitted.
2017
2017
}
2018
2018
2019
- namespace {
2019
+ void SILGenModule::emitSourceFile (SourceFile *sf) {
2020
+ // Type-check the file if we haven't already.
2021
+ performTypeChecking (*sf);
2020
2022
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
+ }
2042
2026
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
+ });
2047
2032
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
+ }
2076
2035
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 ;
2082
2053
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)
2089
2057
continue ;
2090
- SGM.visit (TD);
2091
- }
2092
2058
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);
2101
2060
}
2102
2061
}
2103
2062
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);
2119
2065
}
2120
2066
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);
2123
2073
}
2124
2074
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));
2143
2082
}
2083
+ auto ref = SILDeclRef::getMainDeclEntryPoint (mainDecl);
2084
+ emitFunctionDefinition (ref, getFunction (ref, ForDefinition));
2144
2085
}
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
+ }
2147
2106
2148
2107
std::unique_ptr<SILModule>
2149
2108
ASTLoweringRequest::evaluate (Evaluator &evaluator,
@@ -2156,28 +2115,31 @@ ASTLoweringRequest::evaluate(Evaluator &evaluator,
2156
2115
auto silMod = SILModule::createEmptyModule (desc.context , desc.conv ,
2157
2116
desc.opts , desc.irgenOptions );
2158
2117
2118
+ auto &ctx = silMod->getASTContext ();
2119
+ FrontendStatsTracer tracer (ctx.Stats , " SILGen" );
2120
+
2159
2121
// If all function bodies are being skipped there's no reason to do any
2160
2122
// SIL generation.
2161
2123
if (desc.opts .SkipFunctionBodies == FunctionBodySkipping::All)
2162
2124
return silMod;
2163
2125
2164
2126
// 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 )
2167
2129
return silMod;
2168
2130
2169
- SILGenModuleRAII scope (*silMod);
2131
+ SILGenModule SGM (*silMod, silMod-> getSwiftModule () );
2170
2132
2171
2133
// Emit a specific set of SILDeclRefs if needed.
2172
2134
if (auto Sources = desc.SourcesToEmit ) {
2173
2135
for (auto Source : *Sources)
2174
- scope .emitSymbolSource (std::move (Source));
2136
+ SGM .emitSymbolSource (std::move (Source));
2175
2137
}
2176
2138
2177
2139
// Emit any whole-files needed.
2178
2140
for (auto file : desc.getFilesToEmit ()) {
2179
2141
if (auto *nextSF = dyn_cast<SourceFile>(file))
2180
- scope .emitSourceFile (nextSF);
2142
+ SGM .emitSourceFile (nextSF);
2181
2143
}
2182
2144
2183
2145
// Also make sure to process any intermediate files that may contain SIL.
@@ -2191,6 +2153,23 @@ ASTLoweringRequest::evaluate(Evaluator &evaluator,
2191
2153
primary);
2192
2154
}
2193
2155
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
+
2194
2173
return silMod;
2195
2174
}
2196
2175
0 commit comments