@@ -1843,7 +1843,15 @@ synthesizeMainBody(AbstractFunctionDecl *fn, void *arg) {
1843
1843
return std::make_pair (body, /* typechecked=*/ false );
1844
1844
}
1845
1845
1846
- void AttributeChecker::visitMainTypeAttr (MainTypeAttr *attr) {
1846
+ FuncDecl *
1847
+ SynthesizeMainFunctionRequest::evaluate (Evaluator &evaluator,
1848
+ Decl *D) const {
1849
+ auto &context = D->getASTContext ();
1850
+
1851
+ MainTypeAttr *attr = D->getAttrs ().getAttribute <MainTypeAttr>();
1852
+ if (attr == nullptr )
1853
+ return nullptr ;
1854
+
1847
1855
auto *extension = dyn_cast<ExtensionDecl>(D);
1848
1856
1849
1857
IterableDeclContext *iterableDeclContext;
@@ -1870,10 +1878,10 @@ void AttributeChecker::visitMainTypeAttr(MainTypeAttr *attr) {
1870
1878
1871
1879
// The type cannot be generic.
1872
1880
if (nominal->isGenericContext ()) {
1873
- diagnose (attr->getLocation (),
1874
- diag::attr_generic_ApplicationMain_not_supported, 2 );
1881
+ context. Diags . diagnose (attr->getLocation (),
1882
+ diag::attr_generic_ApplicationMain_not_supported, 2 );
1875
1883
attr->setInvalid ();
1876
- return ;
1884
+ return nullptr ;
1877
1885
}
1878
1886
1879
1887
SourceFile *file = cast<SourceFile>(declContext->getModuleScopeContext ());
@@ -1890,7 +1898,6 @@ void AttributeChecker::visitMainTypeAttr(MainTypeAttr *attr) {
1890
1898
// usual type-checking. The alternative would be to directly call
1891
1899
// mainType.main() from the entry point, and that would require fully
1892
1900
// type-checking the call to mainType.main().
1893
- auto &context = D->getASTContext ();
1894
1901
1895
1902
auto resolution = resolveValueMember (
1896
1903
*declContext, nominal->getInterfaceType (), context.Id_main );
@@ -1918,10 +1925,11 @@ void AttributeChecker::visitMainTypeAttr(MainTypeAttr *attr) {
1918
1925
}
1919
1926
1920
1927
if (viableCandidates.size () != 1 ) {
1921
- diagnose (attr->getLocation (), diag::attr_MainType_without_main,
1922
- nominal->getBaseName ());
1928
+ context.Diags .diagnose (attr->getLocation (),
1929
+ diag::attr_MainType_without_main,
1930
+ nominal->getBaseName ());
1923
1931
attr->setInvalid ();
1924
- return ;
1932
+ return nullptr ;
1925
1933
}
1926
1934
mainFunction = viableCandidates[0 ];
1927
1935
}
@@ -1968,12 +1976,23 @@ void AttributeChecker::visitMainTypeAttr(MainTypeAttr *attr) {
1968
1976
// Of course, this function's body does not type-check.
1969
1977
file->DelayedFunctions .push_back (func);
1970
1978
1979
+ return func;
1980
+ }
1981
+
1982
+ void AttributeChecker::visitMainTypeAttr (MainTypeAttr *attr) {
1983
+ auto &context = D->getASTContext ();
1984
+
1985
+ SourceFile *file = D->getDeclContext ()->getParentSourceFile ();
1986
+ assert (file);
1987
+
1988
+ auto *func = evaluateOrDefault (context.evaluator ,
1989
+ SynthesizeMainFunctionRequest{D},
1990
+ nullptr );
1991
+
1971
1992
// Register the func as the main decl in the module. If there are multiples
1972
1993
// they will be diagnosed.
1973
- if (file->registerMainDecl (func, attr->getLocation ())) {
1994
+ if (file->registerMainDecl (func, attr->getLocation ()))
1974
1995
attr->setInvalid ();
1975
- return ;
1976
- }
1977
1996
}
1978
1997
1979
1998
// / Determine whether the given context is an extension to an Objective-C class
0 commit comments