Fixes to serialization, reduced Define_CodeType macro

Now the execution code is generated in bootstrap/singleheader gen.
This commit is contained in:
Edward R. Gonzalez 2023-08-06 17:19:57 -04:00
parent 97750388ad
commit a4f9596d3b
5 changed files with 163 additions and 148 deletions

View File

@ -308,9 +308,7 @@ String AST::to_string()
if ( Specs )
{
CodeSpecifiers specs = cast<CodeSpecifiers>();
for ( SpecifierT spec : specs )
for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
{
if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) );
@ -348,9 +346,7 @@ String AST::to_string()
if ( Specs )
{
CodeSpecifiers specs = cast<CodeSpecifiers>();
for ( SpecifierT spec : specs )
for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
{
if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) );
@ -404,9 +400,7 @@ String AST::to_string()
if ( Specs )
{
CodeSpecifiers specs = cast<CodeSpecifiers>();
for ( SpecifierT spec : specs )
for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
{
if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) );
@ -441,9 +435,7 @@ String AST::to_string()
if ( Specs )
{
CodeSpecifiers specs = cast<CodeSpecifiers>();
for ( SpecifierT spec : specs )
for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
{
if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) );
@ -463,9 +455,7 @@ String AST::to_string()
else
result.append_fmt( "operator %s()", ValueType->to_string() );
CodeSpecifiers specs = cast<CodeSpecifiers>();
for ( SpecifierT spec : specs )
for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
{
if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) );
@ -487,9 +477,7 @@ String AST::to_string()
{
result.append_fmt( "operator %s()", ValueType->to_string() );
CodeSpecifiers specs = cast<CodeSpecifiers>();
for ( SpecifierT spec : specs )
for ( SpecifierT spec : Specs->cast<CodeSpecifiers>() )
{
if ( ESpecifier::is_trailing( spec ) )
result.append_fmt( " %s", (char const*)ESpecifier::to_str( spec ) );
@ -570,7 +558,7 @@ String AST::to_string()
s32 left = NumEntries;
while ( left-- )
{
if ( ESpecifier::is_trailing( ArrSpecs[idx]) )
if ( ESpecifier::is_trailing( ArrSpecs[idx]) && ArrSpecs[idx] != ESpecifier::Const )
{
idx++;
continue;

View File

@ -524,29 +524,15 @@ struct CodeStruct
AST_Struct* ast;
};
#define Define_CodeType( Typename ) \
struct Code##Typename \
{ \
Using_Code( Code##Typename ); \
AST* raw() \
{ \
return rcast( AST*, ast ); \
} \
operator Code() \
{ \
return * rcast( Code*, this ); \
} \
AST_##Typename* operator->() \
{ \
if ( ast == nullptr ) \
{ \
log_failure("Attempt to dereference a nullptr!"); \
return nullptr; \
} \
return ast; \
} \
AST_##Typename* ast; \
}
#define Define_CodeType( Typename ) \
struct Code##Typename \
{ \
Using_Code( Code ## Typename ); \
AST* raw(); \
operator Code(); \
AST_##Typename* operator->(); \
AST_##Typename* ast; \
}
Define_CodeType( Attributes );
Define_CodeType( Comment );

View File

@ -78,6 +78,25 @@ Typename::operator bool()
return ast != nullptr; \
}
#define Define_CodeType_Impl( Typename ) \
AST* Code##Typename::raw() \
{ \
return rcast( AST*, ast ); \
} \
Code##Typename::operator Code() \
{ \
return *rcast( Code*, this ); \
} \
AST_##Typename* Code##Typename::operator->() \
{ \
if ( ast == nullptr ) \
{ \
log_failure( "Attempt to dereference a nullptr!" ); \
return nullptr; \
} \
return ast; \
} \
Define_CodeImpl( Code );
Define_CodeImpl( CodeBody );
Define_CodeImpl( CodeAttributes );
@ -105,7 +124,31 @@ Define_CodeImpl( CodeTypedef );
Define_CodeImpl( CodeUnion );
Define_CodeImpl( CodeUsing );
Define_CodeImpl( CodeVar );
Define_CodeType_Impl( Attributes );
Define_CodeType_Impl( Comment );
Define_CodeType_Impl( Define );
Define_CodeType_Impl( Enum );
Define_CodeType_Impl( Exec );
Define_CodeType_Impl( Extern );
Define_CodeType_Impl( Include );
Define_CodeType_Impl( Friend );
Define_CodeType_Impl( Fn );
Define_CodeType_Impl( Module );
Define_CodeType_Impl( NS );
Define_CodeType_Impl( Operator );
Define_CodeType_Impl( OpCast );
Define_CodeType_Impl( Pragma );
Define_CodeType_Impl( PreprocessCond );
Define_CodeType_Impl( Template );
Define_CodeType_Impl( Type );
Define_CodeType_Impl( Typedef );
Define_CodeType_Impl( Union );
Define_CodeType_Impl( Using );
Define_CodeType_Impl( Var );
#undef Define_CodeImpl
#undef Define_CodeType_Impl
#define Define_AST_Cast( typename ) \
AST::operator Code ## typename() \

View File

@ -410,6 +410,26 @@ CodeBody gen_ast_inlines()
{
return ast != nullptr;
}
);
char const* codetype_impl_tmpl = stringize(
AST* Code<typename>::raw()
{
return rcast( AST*, ast );
}
Code<typename>::operator Code()
{
return *rcast( Code*, this );
}
AST_<typename>* Code<typename>::operator->()
{
if ( ast == nullptr )
{
log_failure( "Attempt to dereference a nullptr!" );
return nullptr;
}
return ast;
}
\n
);
@ -430,6 +450,7 @@ CodeBody gen_ast_inlines()
CodeBody impl_code_op = parse_global_body( token_fmt( "typename", StrC name(CodeOperator), code_impl_tmpl ));
CodeBody impl_code_opcast = parse_global_body( token_fmt( "typename", StrC name(CodeOpCast), code_impl_tmpl ));
CodeBody impl_code_param = parse_global_body( token_fmt( "typename", StrC name(CodeParam), code_impl_tmpl ));
CodeBody impl_code_pragma = parse_global_body( token_fmt( "typename", StrC name(CodePragma), code_impl_tmpl ));
CodeBody impl_code_precond = parse_global_body( token_fmt( "typename", StrC name(CodePreprocessCond), code_impl_tmpl ));
CodeBody impl_code_specs = parse_global_body( token_fmt( "typename", StrC name(CodeSpecifiers), code_impl_tmpl ));
CodeBody impl_code_struct = parse_global_body( token_fmt( "typename", StrC name(CodeStruct), code_impl_tmpl ));
@ -440,74 +461,73 @@ CodeBody gen_ast_inlines()
CodeBody impl_code_using = parse_global_body( token_fmt( "typename", StrC name(CodeUsing), code_impl_tmpl ));
CodeBody impl_code_var = parse_global_body( token_fmt( "typename", StrC name(CodeVar), code_impl_tmpl ));
char const* ast_cast_tmpl = stringize(
impl_code_attr. append( parse_global_body( token_fmt( "typename", StrC name(Attributes), codetype_impl_tmpl )));
impl_code_cmt. append( parse_global_body( token_fmt( "typename", StrC name(Comment), codetype_impl_tmpl )));
impl_code_define. append( parse_global_body( token_fmt( "typename", StrC name(Define), codetype_impl_tmpl )));
impl_code_enum. append( parse_global_body( token_fmt( "typename", StrC name(Enum), codetype_impl_tmpl )));
impl_code_exec. append( parse_global_body( token_fmt( "typename", StrC name(Exec), codetype_impl_tmpl )));
impl_code_extern. append( parse_global_body( token_fmt( "typename", StrC name(Extern), codetype_impl_tmpl )));
impl_code_include.append( parse_global_body( token_fmt( "typename", StrC name(Include), codetype_impl_tmpl )));
impl_code_friend. append( parse_global_body( token_fmt( "typename", StrC name(Friend), codetype_impl_tmpl )));
impl_code_fn. append( parse_global_body( token_fmt( "typename", StrC name(Fn), codetype_impl_tmpl )));
impl_code_module. append( parse_global_body( token_fmt( "typename", StrC name(Module), codetype_impl_tmpl )));
impl_code_ns. append( parse_global_body( token_fmt( "typename", StrC name(NS), codetype_impl_tmpl )));
impl_code_op. append( parse_global_body( token_fmt( "typename", StrC name(Operator), codetype_impl_tmpl )));
impl_code_opcast. append( parse_global_body( token_fmt( "typename", StrC name(OpCast), codetype_impl_tmpl )));
impl_code_pragma .append( parse_global_body( token_fmt( "typename", StrC name(Pragma), codetype_impl_tmpl )));
impl_code_precond.append( parse_global_body( token_fmt( "typename", StrC name(PreprocessCond), codetype_impl_tmpl )));
impl_code_tmpl. append( parse_global_body( token_fmt( "typename", StrC name(Template), codetype_impl_tmpl )));
impl_code_type. append( parse_global_body( token_fmt( "typename", StrC name(Type), codetype_impl_tmpl )));
impl_code_typedef.append( parse_global_body( token_fmt( "typename", StrC name(Typedef), codetype_impl_tmpl )));
impl_code_union. append( parse_global_body( token_fmt( "typename", StrC name(Union), codetype_impl_tmpl )));
impl_code_using. append( parse_global_body( token_fmt( "typename", StrC name(Using), codetype_impl_tmpl )));
impl_code_var. append( parse_global_body( token_fmt( "typename", StrC name(Var), codetype_impl_tmpl )));
char const* cast_tmpl = stringize(
AST::operator Code<typename>()
{
return { rcast( AST_<typename>*, this ) };
}
);
CodeOpCast impl_astcast_body = parse_operator_cast( token_fmt( "typename", StrC name(Body), ast_cast_tmpl ));
CodeOpCast impl_astcast_attribute = parse_operator_cast( token_fmt( "typename", StrC name(Attributes), ast_cast_tmpl ));
CodeOpCast impl_astcast_cmt = parse_operator_cast( token_fmt( "typename", StrC name(Comment), ast_cast_tmpl ));
CodeOpCast impl_astcast_class = parse_operator_cast( token_fmt( "typename", StrC name(Class), ast_cast_tmpl ));
CodeOpCast impl_astcast_define = parse_operator_cast( token_fmt( "typename", StrC name(Define), ast_cast_tmpl ));
CodeOpCast impl_astcast_enum = parse_operator_cast( token_fmt( "typename", StrC name(Enum), ast_cast_tmpl ));
CodeOpCast impl_astcast_exec = parse_operator_cast( token_fmt( "typename", StrC name(Exec), ast_cast_tmpl ));
CodeOpCast impl_astcast_extern = parse_operator_cast( token_fmt( "typename", StrC name(Extern), ast_cast_tmpl ));
CodeOpCast impl_astcast_friend = parse_operator_cast( token_fmt( "typename", StrC name(Friend), ast_cast_tmpl ));
CodeOpCast impl_astcast_fn = parse_operator_cast( token_fmt( "typename", StrC name(Fn), ast_cast_tmpl ));
CodeOpCast impl_astcast_module = parse_operator_cast( token_fmt( "typename", StrC name(Module), ast_cast_tmpl ));
CodeOpCast impl_astcast_ns = parse_operator_cast( token_fmt( "typename", StrC name(NS), ast_cast_tmpl ));
CodeOpCast impl_astcast_op = parse_operator_cast( token_fmt( "typename", StrC name(Operator), ast_cast_tmpl ));
CodeOpCast impl_astcast_opcast = parse_operator_cast( token_fmt( "typename", StrC name(OpCast), ast_cast_tmpl ));
CodeOpCast impl_astcast_param = parse_operator_cast( token_fmt( "typename", StrC name(Param), ast_cast_tmpl ));
CodeOpCast impl_astcast_precond = parse_operator_cast( token_fmt( "typename", StrC name(PreprocessCond), ast_cast_tmpl ));
CodeOpCast impl_astcast_specs = parse_operator_cast( token_fmt( "typename", StrC name(Specifiers), ast_cast_tmpl ));
CodeOpCast impl_astcast_struct = parse_operator_cast( token_fmt( "typename", StrC name(Struct), ast_cast_tmpl ));
CodeOpCast impl_astcast_tmpl = parse_operator_cast( token_fmt( "typename", StrC name(Template), ast_cast_tmpl ));
CodeOpCast impl_astcast_type = parse_operator_cast( token_fmt( "typename", StrC name(Type), ast_cast_tmpl ));
CodeOpCast impl_astcast_typedef = parse_operator_cast( token_fmt( "typename", StrC name(Typedef), ast_cast_tmpl ));
CodeOpCast impl_astcast_union = parse_operator_cast( token_fmt( "typename", StrC name(Union), ast_cast_tmpl ));
CodeOpCast impl_astcast_using = parse_operator_cast( token_fmt( "typename", StrC name(Using), ast_cast_tmpl ));
CodeOpCast impl_astcast_var = parse_operator_cast( token_fmt( "typename", StrC name(Var), ast_cast_tmpl ));
char const* code_cast_tmpl = stringize(
Code::operator Code<type>() const
Code::operator Code<typename>() const
{
return { (AST_<type>*) ast };
return { (AST_<typename>*) ast };
}
);
CodeOpCast impl_codecast_attr = parse_operator_cast( token_fmt( "type", StrC name(CodeAttributes), code_cast_tmpl ) );
CodeOpCast impl_codecast_cmt = parse_operator_cast( token_fmt( "type", StrC name(CodeComment), code_cast_tmpl ) );
CodeOpCast impl_codecast_class = parse_operator_cast( token_fmt( "type", StrC name(CodeClass), code_cast_tmpl ) );
CodeOpCast impl_codecast_define = parse_operator_cast( token_fmt( "type", StrC name(CodeDefine), code_cast_tmpl ) );
CodeOpCast impl_codecast_enum = parse_operator_cast( token_fmt( "type", StrC name(CodeEnum), code_cast_tmpl ) );
CodeOpCast impl_codecast_exec = parse_operator_cast( token_fmt( "type", StrC name(CodeExec), code_cast_tmpl ) );
CodeOpCast impl_codecast_extern = parse_operator_cast( token_fmt( "type", StrC name(CodeExtern), code_cast_tmpl ) );
CodeOpCast impl_codecast_friend = parse_operator_cast( token_fmt( "type", StrC name(CodeFriend), code_cast_tmpl ) );
CodeOpCast impl_codecast_fn = parse_operator_cast( token_fmt( "type", StrC name(CodeFn), code_cast_tmpl ) );
CodeOpCast impl_codecast_module = parse_operator_cast( token_fmt( "type", StrC name(CodeModule), code_cast_tmpl ) );
CodeOpCast impl_codecast_ns = parse_operator_cast( token_fmt( "type", StrC name(CodeNS), code_cast_tmpl ) );
CodeOpCast impl_codecast_op = parse_operator_cast( token_fmt( "type", StrC name(CodeOperator), code_cast_tmpl ) );
CodeOpCast impl_codecast_opCast = parse_operator_cast( token_fmt( "type", StrC name(CodeOpCast), code_cast_tmpl ) );
CodeOpCast impl_codecast_param = parse_operator_cast( token_fmt( "type", StrC name(CodeParam), code_cast_tmpl ) );
CodeOpCast impl_codecast_precond = parse_operator_cast( token_fmt( "type", StrC name(CodePreprocessCond), code_cast_tmpl ) );
CodeOpCast impl_codecast_specs = parse_operator_cast( token_fmt( "type", StrC name(CodeSpecifiers), code_cast_tmpl ) );
CodeOpCast impl_codecast_struct = parse_operator_cast( token_fmt( "type", StrC name(CodeStruct), code_cast_tmpl ) );
CodeOpCast impl_codecast_template= parse_operator_cast( token_fmt( "type", StrC name(CodeTemplate), code_cast_tmpl ) );
CodeOpCast impl_codecast_type = parse_operator_cast( token_fmt( "type", StrC name(CodeType), code_cast_tmpl ) );
CodeOpCast impl_codecast_typedef = parse_operator_cast( token_fmt( "type", StrC name(CodeTypedef), code_cast_tmpl ) );
CodeOpCast impl_codecast_union = parse_operator_cast( token_fmt( "type", StrC name(CodeUnion), code_cast_tmpl ) );
CodeOpCast impl_codecast_using = parse_operator_cast( token_fmt( "type", StrC name(CodeUsing), code_cast_tmpl ) );
CodeOpCast impl_codecast_var = parse_operator_cast( token_fmt( "type", StrC name(CodeVar), code_cast_tmpl ) );
CodeBody impl_cast_body = parse_global_body( token_fmt( "typename", StrC name(Body), cast_tmpl ));
CodeBody impl_cast_attribute = parse_global_body( token_fmt( "typename", StrC name(Attributes), cast_tmpl ));
CodeBody impl_cast_cmt = parse_global_body( token_fmt( "typename", StrC name(Comment), cast_tmpl ));
CodeBody impl_cast_class = parse_global_body( token_fmt( "typename", StrC name(Class), cast_tmpl ));
CodeBody impl_cast_define = parse_global_body( token_fmt( "typename", StrC name(Define), cast_tmpl ));
CodeBody impl_cast_enum = parse_global_body( token_fmt( "typename", StrC name(Enum), cast_tmpl ));
CodeBody impl_cast_exec = parse_global_body( token_fmt( "typename", StrC name(Exec), cast_tmpl ));
CodeBody impl_cast_extern = parse_global_body( token_fmt( "typename", StrC name(Extern), cast_tmpl ));
CodeBody impl_cast_friend = parse_global_body( token_fmt( "typename", StrC name(Friend), cast_tmpl ));
CodeBody impl_cast_fn = parse_global_body( token_fmt( "typename", StrC name(Fn), cast_tmpl ));
CodeBody impl_cast_include = parse_global_body( token_fmt( "typename", StrC name(Include), cast_tmpl ));
CodeBody impl_cast_module = parse_global_body( token_fmt( "typename", StrC name(Module), cast_tmpl ));
CodeBody impl_cast_ns = parse_global_body( token_fmt( "typename", StrC name(NS), cast_tmpl ));
CodeBody impl_cast_op = parse_global_body( token_fmt( "typename", StrC name(Operator), cast_tmpl ));
CodeBody impl_cast_opcast = parse_global_body( token_fmt( "typename", StrC name(OpCast), cast_tmpl ));
CodeBody impl_cast_param = parse_global_body( token_fmt( "typename", StrC name(Param), cast_tmpl ));
CodeBody impl_cast_pragma = parse_global_body( token_fmt( "typename", StrC name(Pragma), cast_tmpl ));
CodeBody impl_cast_precond = parse_global_body( token_fmt( "typename", StrC name(PreprocessCond), cast_tmpl ));
CodeBody impl_cast_specs = parse_global_body( token_fmt( "typename", StrC name(Specifiers), cast_tmpl ));
CodeBody impl_cast_struct = parse_global_body( token_fmt( "typename", StrC name(Struct), cast_tmpl ));
CodeBody impl_cast_tmpl = parse_global_body( token_fmt( "typename", StrC name(Template), cast_tmpl ));
CodeBody impl_cast_type = parse_global_body( token_fmt( "typename", StrC name(Type), cast_tmpl ));
CodeBody impl_cast_typedef = parse_global_body( token_fmt( "typename", StrC name(Typedef), cast_tmpl ));
CodeBody impl_cast_union = parse_global_body( token_fmt( "typename", StrC name(Union), cast_tmpl ));
CodeBody impl_cast_using = parse_global_body( token_fmt( "typename", StrC name(Using), cast_tmpl ));
CodeBody impl_cast_var = parse_global_body( token_fmt( "typename", StrC name(Var), cast_tmpl ));
CodeBody result = def_global_body( args(
def_pragma( txt_StrC("region generated code inline implementation")),
fmt_newline,
impl_code,
impl_code_body,
impl_code_attr,
impl_code_cmt,
impl_code_class,
impl_code_define,
@ -516,11 +536,13 @@ CodeBody gen_ast_inlines()
impl_code_extern,
impl_code_friend,
impl_code_fn,
impl_code_include,
impl_code_module,
impl_code_ns,
impl_code_op,
impl_code_opcast,
impl_code_param,
impl_code_pragma,
impl_code_precond,
impl_code_specs,
impl_code_struct,
@ -533,60 +555,36 @@ CodeBody gen_ast_inlines()
fmt_newline,
def_pragma( txt_StrC("endregion generated code inline implementation")),
fmt_newline,
def_pragma( txt_StrC("region generated AST cast implementation")),
def_pragma( txt_StrC("region generated AST/Code cast implementation")),
fmt_newline,
impl_astcast_body,
impl_astcast_cmt,
impl_astcast_class,
impl_astcast_define,
impl_astcast_enum,
impl_astcast_exec,
impl_astcast_extern,
impl_astcast_friend,
impl_astcast_fn,
impl_astcast_module,
impl_astcast_ns,
impl_astcast_op,
impl_astcast_opcast,
impl_astcast_param,
impl_astcast_precond,
impl_astcast_specs,
impl_astcast_struct,
impl_astcast_tmpl,
impl_astcast_type,
impl_astcast_typedef,
impl_astcast_union,
impl_astcast_using,
impl_astcast_var,
impl_cast_body,
impl_cast_attribute,
impl_cast_cmt,
impl_cast_class,
impl_cast_define,
impl_cast_enum,
impl_cast_exec,
impl_cast_extern,
impl_cast_friend,
impl_cast_fn,
impl_cast_include,
impl_cast_module,
impl_cast_ns,
impl_cast_op,
impl_cast_opcast,
impl_cast_param,
impl_cast_pragma,
impl_cast_precond,
impl_cast_specs,
impl_cast_struct,
impl_cast_tmpl,
impl_cast_type,
impl_cast_typedef,
impl_cast_union,
impl_cast_using,
impl_cast_var,
fmt_newline,
def_pragma( txt_StrC("endregion generated AST cast implementation")),
fmt_newline,
def_pragma( txt_StrC("region Code cast implementation")),
impl_codecast_attr,
impl_codecast_cmt,
impl_codecast_class,
impl_codecast_define,
impl_codecast_enum,
impl_codecast_exec,
impl_codecast_extern,
impl_codecast_friend,
impl_codecast_fn,
impl_codecast_module,
impl_codecast_ns,
impl_codecast_op,
impl_codecast_opCast,
impl_codecast_param,
impl_codecast_precond,
impl_codecast_specs,
impl_codecast_struct,
impl_codecast_template,
impl_codecast_type,
impl_codecast_typedef,
impl_codecast_union,
impl_codecast_using,
impl_codecast_var,
fmt_newline,
def_pragma( txt_StrC("endregion Code cast implementation")),
def_pragma( txt_StrC("endregion generated AST/Code cast implementation")),
fmt_newline
));

View File

@ -102,7 +102,7 @@ IndentCaseBlocks: false
IndentCaseLabels: true
IndentExternBlock: AfterExternBlock
IndentGotoLabels: true
IndentPPDirectives: AfterHash
IndentPPDirectives: None
IndentRequires: true
IndentWidth: 4
IndentWrappedFunctionNames: true