Skip to content

Commit a6e383b

Browse files
committed
Allow implicit void return for global main, closes #254
For example, this is now a complete legal program in pure Cpp2 (with `-p`) with fully conformant Cpp1 code gen: `main: () = std::cout << "Hello world";`
1 parent 8d2f274 commit a6e383b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

source/cppfront.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2959,7 +2959,7 @@ class cppfront
29592959

29602960
//-----------------------------------------------------------------------
29612961
//
2962-
auto emit(function_type_node const& n, token const* ident) -> void
2962+
auto emit(function_type_node const& n, token const* ident, bool is_main) -> void
29632963
{
29642964
assert(n.parameters);
29652965
emit(*n.parameters);
@@ -2972,7 +2972,10 @@ class cppfront
29722972
//}
29732973

29742974
if (n.returns.index() == function_type_node::empty) {
2975-
if (ident) {
2975+
if (is_main) {
2976+
printer.print_cpp2( " -> int", n.position() );
2977+
}
2978+
else if (ident) {
29762979
printer.print_cpp2( " -> void", n.position() );
29772980
}
29782981
}
@@ -3049,6 +3052,8 @@ class cppfront
30493052
auto& func = std::get<declaration_node::function>(n.type);
30503053
assert(func);
30513054

3055+
auto is_main = !n.parent_scope && *n.identifier->identifier == "main";
3056+
30523057
current_function.push_back({});
30533058
auto guard = finally([&]{ current_function.pop_back(); });
30543059

@@ -3057,7 +3062,7 @@ class cppfront
30573062
if (capture_intro != "") {
30583063
assert (!n.identifier);
30593064
printer.print_cpp2(capture_intro, n.position());
3060-
emit( *func, nullptr );
3065+
emit( *func, nullptr, is_main);
30613066
}
30623067
else {
30633068
assert (n.identifier);
@@ -3066,7 +3071,7 @@ class cppfront
30663071
}
30673072
printer.print_cpp2( "auto ", n.position() );
30683073
printer.print_cpp2( *n.identifier->identifier, n.identifier->position() );
3069-
emit( *func, n.identifier->identifier );
3074+
emit( *func, n.identifier->identifier, is_main );
30703075
}
30713076

30723077
// Function declaration

0 commit comments

Comments
 (0)