From e2fc4001d3e6041bcb1a713bc9e258091dd03cf3 Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Sun, 9 Apr 2023 16:05:05 -0700 Subject: [PATCH] Require that a member object before a 'this' base object must be private, closes #336 --- source/cppfront.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/source/cppfront.cpp b/source/cppfront.cpp index d694c85caa..87974b2d72 100644 --- a/source/cppfront.cpp +++ b/source/cppfront.cpp @@ -4928,7 +4928,8 @@ class cppfront decl->get_decl_if_type_scope_object_name_before_a_base_type(*decl->name()) || decl->has_name("this") ; - if (emit_as_base) { + if (emit_as_base) + { if (decl->has_name("this")) { if (printer.get_phase() == printer.phase1_type_defs_func_decls) { printer.print_cpp2( @@ -4938,7 +4939,20 @@ class cppfront separator = ","; } } - else { + else + { + if ( + decl->access + && *decl->access != "public" + ) + { + errors.emplace_back( + decl->position(), + "a member object that appears before a 'this' base type object must be private (the default)" + ); + return; + } + if (printer.get_phase() == printer.phase1_type_defs_func_decls) { printer.print_cpp2( separator + " private cpp2::store_as_base<\"" @@ -4952,7 +4966,8 @@ class cppfront } } // Then we'll switch to start the body == other members - else { + else + { if (printer.get_phase() == printer.phase1_type_defs_func_decls) { if (!started_body) { printer.print_cpp2(" {", compound_stmt->position());