Skip to content

Commit de19777

Browse files
committed
[AST] Retrieve @main type from module.
Added a convenience function to ModuleDecl to look up the entry point @main type if there is one.
1 parent ff428ed commit de19777

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

include/swift/AST/Module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,8 @@ class ModuleDecl
841841
return EntryPointInfo.hasEntryPoint();
842842
}
843843

844+
NominalTypeDecl *getMainTypeDecl() const;
845+
844846
/// Returns the associated clang module if one exists.
845847
const clang::Module *findUnderlyingClangModule() const;
846848

lib/AST/Module.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,22 @@ bool SourceFile::registerMainDecl(ValueDecl *mainDecl, SourceLoc diagLoc) {
18761876
return false;
18771877
}
18781878

1879+
NominalTypeDecl *ModuleDecl::getMainTypeDecl() const {
1880+
if (!EntryPointInfo.hasEntryPoint())
1881+
return nullptr;
1882+
auto *file = EntryPointInfo.getEntryPointFile();
1883+
if (!file)
1884+
return nullptr;
1885+
auto *mainDecl = file->getMainDecl();
1886+
if (!mainDecl)
1887+
return nullptr;
1888+
auto *func = dyn_cast<FuncDecl>(file->getMainDecl());
1889+
if (!func)
1890+
return nullptr;
1891+
auto *nominalType = dyn_cast<NominalTypeDecl>(func->getDeclContext());
1892+
return nominalType;
1893+
}
1894+
18791895
bool ModuleDecl::registerEntryPointFile(FileUnit *file, SourceLoc diagLoc,
18801896
Optional<ArtificialMainKind> kind) {
18811897
if (!EntryPointInfo.hasEntryPoint()) {

0 commit comments

Comments
 (0)