update gencpp to latest and adjsut gen_src.pp accordingly.

This commit is contained in:
2024-12-13 11:50:40 -05:00
parent daa820b134
commit ad813d0335
17 changed files with 26015 additions and 24762 deletions
+88 -86
View File
@@ -5,9 +5,9 @@
#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS
#define GEN_ENFORCE_STRING_CODE_TYPES
#define GEN_EXPOSE_BACKEND
#include "gencpp/gen.cpp"
#include "gencpp/gen.builder.cpp"
#include "gencpp/gen.scanner.cpp"
#define GEN_IMPLEMENTATION
// #define GEN_DONT_USE_FATAL
#include "gencpp/gen.hpp"
using namespace gen;
#ifdef GEN_SYSTEM_WINDOWS
@@ -24,18 +24,11 @@ using namespace gen;
#pragma endregion Directories
inline
CodeBody parse_file( char const* path ) {
FileContents content = file_read_contents( GlobalAllocator, true, path );
CodeBody code = parse_global_body( StrC { content.size, (char const*)content.data });
return code;
}
inline
void git_restore_file( char const* path )
{
#define git_restore_cmd "git restore "
String command = String::make( GlobalAllocator, git_restore_cmd );
StrBuilder command = StrBuilder::make( GlobalAllocator, git_restore_cmd );
command.append( path );
log_fmt("Running git restore on: %s", path);
system(command);
@@ -51,7 +44,7 @@ void format_file( char const* path )
#define cf_format_inplace "-i "
#define cf_style "-style=file:" "./scripts/.clang-format "
#define cf_verbose "-verbose "
String command = String::make( GlobalAllocator, clang_format );
StrBuilder command = StrBuilder::make( GlobalAllocator, clang_format );
command.append( cf_format_inplace );
command.append( cf_style );
command.append( cf_verbose );
@@ -80,33 +73,30 @@ Array<Odin_AstKind> get_odin_ast_kinds()
++ done_once;
}
CodeType t_char_const_ptr = parse_type(code(char const*));
CodeTypename t_char_const_ptr = parse_type(code(char const*));
CodeBody ast_kinds_header = parse_file( path_codegen "ast_kinds.hpp" );
for ( Code code = ast_kinds_header.begin(); code != ast_kinds_header.end(); ++ code )
{
switch (code->Type)
{
using namespace ECode;
case Comment:
case Preprocess_Pragma:
case CT_Comment:
case CT_Preprocess_Pragma:
// Ignore
continue;
case Variable:
case CT_Variable:
{
Odin_AstKind entry { {nullptr}, {} };
Odin_AstKind entry {};
CodeVar var = code.cast<CodeVar>();
CodeVar var = cast(CodeVar, code);
if ( ! var->ValueType.is_equal( t_char_const_ptr ) )
{
__debugbreak();
log_failure("Expected all globally defined variables to be char cons* type");
return kinds;
}
if ( ! var->Value || ! var->Value->Content )
{
__debugbreak();
log_failure("Expected all globally defined variable to have a string assigned to it");
return kinds;
}
@@ -116,9 +106,8 @@ Array<Odin_AstKind> get_odin_ast_kinds()
++ code;
// Grab the definition
if ( code->Type != Struct && code->Type != Typedef )
if ( code->Type != CT_Struct && code->Type != CT_Typedef )
{
__debugbreak();
log_failure("Expected a struct or typedef for the entry definition");
return kinds;
}
@@ -128,8 +117,7 @@ Array<Odin_AstKind> get_odin_ast_kinds()
}
continue;
case Struct:
__debugbreak();
case CT_Struct:
log_failure("Expected a description definition as char const* first");
return kinds;
break;
@@ -151,13 +139,13 @@ Array<Code> get_odin_type_kinds()
CodeBody ast_types_header = parse_file( path_codegen "type_kinds.hpp" );
for ( Code code = ast_types_header.begin(); code != ast_types_header.end(); ++ code ) switch (code->Type)
{
case ECode::Comment:
case ECode::Preprocess_Pragma:
case CT_Comment:
case CT_Preprocess_Pragma:
// Ignore
continue;
case ECode::Typedef:
case ECode::Struct:
case CT_Typedef:
case CT_Struct:
{
types.append(code);
}
@@ -170,7 +158,7 @@ int gen_main()
gen::init();
log_fmt("Generating code for Odin's src\n");
StrC str_GB_STATIC_ASSERT = txt("GB_STATIC_ASSERT(");
Str str_GB_STATIC_ASSERT = txt("GB_STATIC_ASSERT(");
PreprocessorDefines.append( get_cached_string(str_GB_STATIC_ASSERT) );
// Remove TOKEN_KINDS usage in tokenizer.cpp
@@ -181,61 +169,64 @@ int gen_main()
{
char scratch_mem[kilobytes(32)];
Arena scratch = Arena::init_from_memory( scratch_mem, sizeof(scratch_mem) );
file_read_contents( scratch, zero_terminate, path_codegen "token_kinds.csv" );
file_read_contents( scratch, file_zero_terminate, path_codegen "token_kinds.csv" );
csv_parse( &csv_nodes, scratch_mem, GlobalAllocator, false );
}
Array<ADT_Node> enum_strs = csv_nodes.nodes[0].nodes;
Array<ADT_Node> str_strs = csv_nodes.nodes[1].nodes;
String enum_entries = String::make_reserve( GlobalAllocator, kilobytes(32) );
String to_str_entries = String::make_reserve( GlobalAllocator, kilobytes(32) );
StrBuilder enum_entries = StrBuilder::make_reserve( GlobalAllocator, kilobytes(32) );
StrBuilder to_str_entries = StrBuilder::make_reserve( GlobalAllocator, kilobytes(32) );
to_str_entries.append(txt("{"));
for (uw idx = 0; idx < enum_strs.num(); idx++)
for (usize idx = 0; idx < enum_strs.num(); idx++)
{
char const* enum_str = enum_strs[idx].string;
StrC entry_to_str = to_str(str_strs [idx].string);
Str entry_to_str = to_str_from_c_str(str_strs [idx].string);
#pragma push_macro("cast")
#undef cast
enum_entries.append_fmt( "Token_%s,\n", enum_str );
to_str_entries.append( token_fmt( "str", (StrC)entry_to_str, stringize(
to_str_entries.append( token_fmt( "str", (Str)entry_to_str, stringize(
{ cast(u8 *) "<str>", gb_size_of("<str>") -1 },\n
)));
#pragma pop_macro("cast")
}
to_str_entries.append(txt("}"));
char const* path_tokenizer = path_src "tokenizer.cpp";
git_restore_file( path_tokenizer );
CodeBody src_tokenizer_cpp = parse_file( path_src "tokenizer.cpp" );
CodeBody body = def_body( ECode::Global_Body );
CodeBody body = def_body( CT_Global_Body );
body.append( def_comment(txt("NOTICE(github: Ed94): This is a generated variant of tokenizer.cpp using <repo_root>/codegen/gen_src.cpp")));
body.append(fmt_newline);
for (Code code = src_tokenizer_cpp.begin(); code != src_tokenizer_cpp.end(); ++ code) switch (code->Type)
{
case ECode::Preprocess_Define:
case CT_Preprocess_Define:
if ( code->Name.starts_with( txt("TOKEN_KINDS"))) {
// Skip, we don't want it.
continue;
}
continue;
case ECode::Enum:
case CT_Enum:
{
if ( code->Name.starts_with(txt("TokenKind")))
{
CodeEnum enum_code = code.cast<CodeEnum>();
CodeEnum enum_code = cast(CodeEnum, code);
enum_code->Body = untyped_str(enum_entries);
}
body.append(code);
}
continue;
case ECode::Variable:
case CT_Variable:
if ( code->Name.starts_with(txt("token_strings")))
{
CodeVar var = code.cast<CodeVar>();
CodeVar var = cast(CodeVar, code);
var->Value = untyped_str(to_str_entries);
}
body.append(code);
@@ -259,7 +250,7 @@ int gen_main()
char const* path_parser = path_src "parser.hpp";
git_restore_file( path_parser );
CodeBody src_parser_header = parse_file( path_src "parser.hpp" );
CodeBody body = def_body( ECode::Global_Body );
CodeBody body = def_body( CT_Global_Body );
body.append( def_comment(txt("NOTICE(github: Ed94): This is a generated variant of parser.hpp using <repo_root>/codegen/gen_src.cpp")));
body.append(fmt_newline);
@@ -270,7 +261,7 @@ int gen_main()
{
switch (code->Type)
{
case ECode::Preprocess_Define:
case CT_Preprocess_Define:
if ( code->Name.starts_with( txt("AST_KINDS"))) {
// Skip, we don't want it.
continue;
@@ -284,26 +275,26 @@ int gen_main()
body.append(code);
continue;
case ECode::Untyped:
case CT_Untyped:
if (code->Content.starts_with(txt("AST_KINDS")))
continue;
body.append(code);
continue;
case ECode::Enum:
case CT_Enum:
{
if (code->Name.starts_with( txt("AstKind")))
{
// Swap with generated variant
CodeBody swap_body = def_body( ECode::Enum_Body );
CodeBody swap_body = def_body( CT_Enum_Body );
{
swap_body.append( code_str(Ast_Invalid,));
for (Odin_AstKind& kind : ast_kinds)
swap_body.append( untyped_str( String::fmt_buf(GlobalAllocator, "Ast_%S,", kind.def->Name )));
swap_body.append( untyped_str( StrBuilder::fmt_buf(GlobalAllocator, "Ast_%S,", kind.def->Name )));
swap_body.append( code_str(Ast_COUNT));
}
CodeEnum swapped_enum = code.cast<CodeEnum>().duplicate();
CodeEnum swapped_enum = cast(CodeEnum, code).duplicate();
swapped_enum->Body = swap_body;
body.append(swapped_enum);
}
@@ -312,24 +303,32 @@ int gen_main()
}
continue;
case ECode::Variable:
case CT_Variable:
{
if (code->Name.starts_with(txt("ast_strings")))
{
// Swap with generated table
String generated_table = String::make_reserve(GlobalAllocator, kilobytes(32));
StrBuilder generated_table = StrBuilder::make_reserve(GlobalAllocator, kilobytes(32));
{
#pragma push_macro("cast")
#undef cast
for (Odin_AstKind& kind : ast_kinds)
generated_table.append(token_fmt("desc", (StrC)kind.desc, stringize(
generated_table.append(token_fmt("desc", (Str)kind.desc, stringize(
{ cast(u8 *) <desc>, gb_size_of(<desc>) -1 },
)));
#pragma pop_macro("cast")
}
CodeVar swapped_table = code.cast<CodeVar>().duplicate();
swapped_table->Value = code_fmt( "kinds", (StrC)generated_table, stringize(
CodeVar swapped_table = cast(CodeVar, code).duplicate();
#pragma push_macro("cast")
#undef cast
swapped_table->Value = code_fmt( "kinds", (Str)generated_table, stringize(
{
{cast(u8 *)"invalid node", gb_size_of("invalid node")},\n
{ cast(u8 *)"invalid node", gb_size_of("invalid node")},\n
<kinds>
}));
#pragma pop_macro("cast")
body.append(swapped_table);
body.append(fmt_newline);
@@ -339,7 +338,7 @@ int gen_main()
for (Odin_AstKind& kind : ast_kinds)
{
Code def = kind.def.duplicate();
def->Name = get_cached_string( String::fmt_buf(GlobalAllocator, "Ast%S", kind.def->Name));
def->Name = get_cached_string( StrBuilder::fmt_buf(GlobalAllocator, "Ast%S", kind.def->Name));
body.append( def );
body.append(fmt_newline);
}
@@ -349,15 +348,15 @@ int gen_main()
if (code->Name.starts_with(txt("ast_variant_sizes")))
{
// Swap with generated table
String generated_table = String::make_reserve(GlobalAllocator, kilobytes(32));
StrBuilder generated_table = StrBuilder::make_reserve(GlobalAllocator, kilobytes(32));
{
for (Odin_AstKind& kind : ast_kinds)
generated_table.append(token_fmt( "name", (StrC)kind.def->Name, stringize(
generated_table.append(token_fmt( "name", (Str)kind.def->Name, stringize(
gb_size_of(Ast<name>),\n
)));
}
CodeVar swapped_table = code.cast<CodeVar>().duplicate();
swapped_table->Value = code_fmt( "kinds", (StrC)generated_table, stringize(
CodeVar swapped_table = cast(CodeVar, code).duplicate();
swapped_table->Value = code_fmt( "kinds", (Str)generated_table, stringize(
{
0,\n
<kinds>
@@ -369,21 +368,21 @@ int gen_main()
}
continue;
case ECode::Struct:
case CT_Struct:
{
CodeStruct code_struct = code.cast<CodeStruct>();
CodeStruct code_struct = cast(CodeStruct, code);
if (code->Name.starts_with(txt("Ast")))
for (Code ast_code : code_struct->Body) switch (ast_code->Type)
{
case ECode::Union:
case CT_Union:
{
// Swap out the union's contents with the generated member definitions
CodeBody body_swap = def_body(ECode::Union_Body);
CodeBody body_swap = def_body(CT_Union_Body);
for (Odin_AstKind kind : ast_kinds)
body_swap.append( parse_variable( token_fmt( "name", (StrC)kind.def->Name, stringize(
body_swap.append( parse_variable( token_fmt( "name", (Str)kind.def->Name, stringize(
Ast<name> <name>;
))));
ast_code->Body = rcast(AST*, body_swap.ast);
ast_code->Body = body_swap;
}
break;
default:
@@ -412,7 +411,7 @@ int gen_main()
char const* path_types = path_src "types.cpp";
git_restore_file( path_types );
CodeBody src_types_cpp = parse_file( path_src "types.cpp" );
CodeBody body = def_body( ECode::Global_Body );
CodeBody body = def_body( CT_Global_Body );
body.append( def_comment(txt("NOTICE(github: Ed94): This is a generated variant of types.cpp using <repo_root>/codegen/gen_src.cpp")));
body.append(fmt_newline);
@@ -421,7 +420,7 @@ int gen_main()
for (Code code = src_types_cpp.begin(); code != src_types_cpp.end(); ++ code) switch (code->Type)
{
case ECode::Preprocess_Define:
case CT_Preprocess_Define:
{
if ( code->Name.starts_with( txt("TYPE_KINDS"))) {
// Skip, we don't want it.
@@ -437,19 +436,19 @@ int gen_main()
}
continue;
case ECode::Enum:
case CT_Enum:
{
if ( code->Name.starts_with( txt("TypeKind")))
{
CodeBody swap_body = def_body( ECode::Enum_Body);
CodeBody swap_body = def_body( CT_Enum_Body);
{
swap_body.append( code_str(Type_Invalid, ));
{
for (Code type : type_kinds)
swap_body.append( untyped_str( String::fmt_buf(GlobalAllocator, "Type_%S,", type->Name )));
swap_body.append( untyped_str( StrBuilder::fmt_buf(GlobalAllocator, "Type_%S,", type->Name )));
swap_body.append( code_str(Type_COUNT));
}
CodeEnum swapped_enum = code.cast<CodeEnum>().duplicate();
CodeEnum swapped_enum = cast(CodeEnum, code).duplicate();
swapped_enum->Body = swap_body;
body.append(swapped_enum);
}
@@ -459,24 +458,27 @@ int gen_main()
}
continue;
case ECode::Variable:
case CT_Variable:
{
if (code->Name.starts_with(txt("type_strings")))
{
#pragma push_macro("cast")
#undef cast
// Swap with generated table
String generated_table = String::make_reserve(GlobalAllocator, kilobytes(32));
StrBuilder generated_table = StrBuilder::make_reserve(GlobalAllocator, kilobytes(32));
{
for (Code type : type_kinds)
generated_table.append(token_fmt("type", (StrC)type->Name, stringize(
generated_table.append(token_fmt("type", (Str)type->Name, stringize(
{ cast(u8 *) "<type>", gb_size_of("<type>") -1 },
)));
}
CodeVar swapped_table = code.cast<CodeVar>().duplicate();
swapped_table->Value = code_fmt( "types", (StrC)generated_table, stringize(
CodeVar swapped_table = ((CodeVar)code).duplicate();
swapped_table->Value = code_fmt( "types", (Str)generated_table, stringize(
{
{cast(u8 *)"invalid node", gb_size_of("invalid node")},\n
{ cast(u8 *)"invalid node", gb_size_of("invalid node")},\n
<types>
}));
#pragma pop_macro("cast")
body.append(swapped_table);
body.append(fmt_newline);
@@ -486,7 +488,7 @@ int gen_main()
for (Code type : type_kinds)
{
Code def = type.duplicate();
def->Name = get_cached_string( String::fmt_buf(GlobalAllocator, "Type%S", type->Name));
def->Name = get_cached_string( StrBuilder::fmt_buf(GlobalAllocator, "Type%S", type->Name));
body.append( def );
body.append(fmt_newline);
}
@@ -497,21 +499,21 @@ int gen_main()
}
continue;
case ECode::Struct:
case CT_Struct:
{
CodeStruct code_struct = code.cast<CodeStruct>();
if ( String::are_equal(code->Name, txt("Type")))
CodeStruct code_struct = cast(CodeStruct, code);
if ( str_are_equal(code->Name, txt("Type")))
for (Code type_code : code_struct->Body) switch (type_code->Type)
{
case ECode::Union:
case CT_Union:
{
// Swap out the union's contents with the generated member definitions
CodeBody body_swap = def_body(ECode::Union_Body);
CodeBody body_swap = def_body(CT_Union_Body);
for (Code type : type_kinds)
body_swap.append( parse_variable( token_fmt( "name", (StrC)type->Name, stringize(
body_swap.append( parse_variable( token_fmt( "name", (Str)type->Name, stringize(
Type<name> <name>;
))));
type_code->Body = rcast(AST*, body_swap.ast);
type_code->Body = body_swap;
}
break;
default: