[AST] Retrieve @main type from module.

Added a convenience function to ModuleDecl to look up the entry point
@main type if there is one.
This commit is contained in:
Nate Chandler
2022-09-23 15:26:22 -07:00
parent ff428ed20a
commit de19777da3
2 changed files with 18 additions and 0 deletions

View File

@@ -1876,6 +1876,22 @@ bool SourceFile::registerMainDecl(ValueDecl *mainDecl, SourceLoc diagLoc) {
return false;
}
NominalTypeDecl *ModuleDecl::getMainTypeDecl() const {
if (!EntryPointInfo.hasEntryPoint())
return nullptr;
auto *file = EntryPointInfo.getEntryPointFile();
if (!file)
return nullptr;
auto *mainDecl = file->getMainDecl();
if (!mainDecl)
return nullptr;
auto *func = dyn_cast<FuncDecl>(file->getMainDecl());
if (!func)
return nullptr;
auto *nominalType = dyn_cast<NominalTypeDecl>(func->getDeclContext());
return nominalType;
}
bool ModuleDecl::registerEntryPointFile(FileUnit *file, SourceLoc diagLoc,
Optional<ArtificialMainKind> kind) {
if (!EntryPointInfo.hasEntryPoint()) {