@@ -5316,30 +5316,29 @@ class cppfront
5316
5316
5317
5317
for (auto & stmt : compound_stmt->statements )
5318
5318
{
5319
- if (!stmt->is_declaration ()) {
5320
- // We will already have emitted an error for this in sema.check
5321
- return ;
5322
- }
5323
- auto & decl = std::get<statement_node::declaration>(stmt->statement );
5324
- assert (decl);
5325
- assert (decl->name ());
5326
-
5327
- auto emit_as_base =
5328
- decl->get_decl_if_type_scope_object_name_before_a_base_type (*decl->name ());
5329
-
5330
- if (emit_as_base) {
5331
- printer.print_extra (
5332
- " \n struct "
5333
- + print_to_string (*decl->parent_declaration ->name ())
5334
- + " _"
5335
- + decl->name ()->to_string ()
5336
- + " _as_base { "
5337
- + print_to_string ( *decl->get_object_type () )
5338
- + " "
5339
- + decl->name ()->to_string ()
5340
- + " ; };"
5341
- );
5342
- found = true ;
5319
+ if (stmt->is_declaration ())
5320
+ {
5321
+ auto & decl = std::get<statement_node::declaration>(stmt->statement );
5322
+ assert (decl);
5323
+ assert (decl->name ());
5324
+
5325
+ auto emit_as_base =
5326
+ decl->get_decl_if_type_scope_object_name_before_a_base_type (*decl->name ());
5327
+
5328
+ if (emit_as_base) {
5329
+ printer.print_extra (
5330
+ " \n struct "
5331
+ + print_to_string (*decl->parent_declaration ->name ())
5332
+ + " _"
5333
+ + decl->name ()->to_string ()
5334
+ + " _as_base { "
5335
+ + print_to_string ( *decl->get_object_type () )
5336
+ + " "
5337
+ + decl->name ()->to_string ()
5338
+ + " ; };"
5339
+ );
5340
+ found = true ;
5341
+ }
5343
5342
}
5344
5343
}
5345
5344
@@ -5438,21 +5437,51 @@ class cppfront
5438
5437
}
5439
5438
5440
5439
// Type definition
5441
-
5442
5440
auto separator = std::string{" :" };
5443
5441
auto started_body = false ;
5442
+ auto saved_for_body = std::vector<std::pair<std::string, source_position>>{};
5444
5443
auto found_constructor = false ;
5445
5444
auto found_that_constructor = false ;
5446
5445
assert (compound_stmt);
5447
5446
5447
+ auto start_body = [&]{
5448
+ if (!started_body) {
5449
+ printer.print_cpp2 (" {" , compound_stmt->position ());
5450
+ started_body = true ;
5451
+ for (auto & [line, pos] : saved_for_body) {
5452
+ printer.print_cpp2 (line + " \n " , pos);
5453
+ }
5454
+ }
5455
+ };
5456
+
5448
5457
for (auto & stmt : compound_stmt->statements )
5449
5458
{
5450
5459
assert (stmt);
5451
- if (!stmt->is_declaration ()) {
5460
+ if (
5461
+ !stmt->is_declaration ()
5462
+ && !stmt->is_using ()
5463
+ )
5464
+ {
5452
5465
// We will already have emitted an error for this in sema.check
5453
5466
return ;
5454
5467
}
5455
5468
5469
+ // If it's a using statement, save it up if we haven't started the body yet
5470
+
5471
+ if (stmt->is_using ()) {
5472
+ auto & use = std::get<statement_node::using_>(stmt->statement );
5473
+ assert (use);
5474
+ if (started_body) {
5475
+ emit (*use);
5476
+ }
5477
+ else {
5478
+ saved_for_body.emplace_back ( print_to_string (*use), use->position () );
5479
+ }
5480
+ continue ;
5481
+ }
5482
+
5483
+ // Else it's a declaration...
5484
+
5456
5485
auto & decl = std::get<statement_node::declaration>(stmt->statement );
5457
5486
assert (decl);
5458
5487
@@ -5507,21 +5536,16 @@ class cppfront
5507
5536
else
5508
5537
{
5509
5538
if (printer.get_phase () == printer.phase1_type_defs_func_decls ) {
5510
- if (!started_body) {
5511
- printer.print_cpp2 (" {" , compound_stmt->position ());
5512
- started_body = true ;
5513
- }
5539
+ start_body ();
5514
5540
}
5515
5541
emit (*decl);
5516
5542
}
5517
5543
}
5518
5544
5519
- // Ensure we emit the { even if there are only bases in the type
5520
5545
if (printer.get_phase () == printer.phase1_type_defs_func_decls )
5521
5546
{
5522
- if (!started_body) {
5523
- printer.print_cpp2 (" {" , compound_stmt->position ());
5524
- }
5547
+ // Ensure we emit the { even if there are only bases in the type
5548
+ start_body ();
5525
5549
5526
5550
auto id = print_to_string (*n.identifier );
5527
5551
auto indent = static_cast <size_t >(
@@ -5583,16 +5607,23 @@ class cppfront
5583
5607
assert (compound_stmt);
5584
5608
for (auto & stmt : compound_stmt->statements ) {
5585
5609
assert (stmt);
5586
- if (!stmt->is_declaration ()) {
5610
+ if (stmt->is_declaration ()) {
5611
+ auto & decl = std::get<statement_node::declaration>(stmt->statement );
5612
+ assert (decl);
5613
+ emit (*decl);
5614
+ }
5615
+ else if (stmt->is_using ()) {
5616
+ auto & use = std::get<statement_node::using_>(stmt->statement );
5617
+ assert (use);
5618
+ emit (*use);
5619
+ }
5620
+ else {
5587
5621
errors.emplace_back (
5588
5622
stmt->position (),
5589
- " a namespace scope must contain only declarations, not other code"
5623
+ " a namespace scope must contain only declarations or 'using' statements , not other code"
5590
5624
);
5591
5625
return ;
5592
5626
}
5593
- auto & decl = std::get<statement_node::declaration>(stmt->statement );
5594
- assert (decl);
5595
- emit (*decl);
5596
5627
}
5597
5628
5598
5629
printer.print_cpp2 (" }\n " , compound_stmt->close_brace );
0 commit comments