mirror of
https://github.com/Ed94/gencpp.git
synced 2025-06-14 18:51:47 -07:00
Introduced the general context struct for gencpp
This commit is contained in:
@ -589,7 +589,7 @@ do \
|
||||
}
|
||||
|
||||
// Used to track which functions need generic selectors.
|
||||
Array(CodeFn) code_c_interface = array_init_reserve<CodeFn>(GlobalAllocator, 16);
|
||||
Array(CodeFn) code_c_interface = array_init_reserve<CodeFn>(FallbackAllocator, 16);
|
||||
|
||||
CodeBody parsed_ast = parse_file( path_base "components/ast.hpp" );
|
||||
CodeBody ast = def_body(CT_Global_Body);
|
||||
@ -647,9 +647,9 @@ do \
|
||||
{
|
||||
Str old_prefix = txt("code_");
|
||||
Str actual_name = { fn->Name.Ptr + old_prefix.Len, fn->Name.Len - old_prefix.Len };
|
||||
StrBuilder new_name = StrBuilder::fmt_buf(GlobalAllocator, "code__%S", actual_name );
|
||||
StrBuilder new_name = StrBuilder::fmt_buf(FallbackAllocator, "code__%S", actual_name );
|
||||
|
||||
fn->Name = get_cached_string(new_name);
|
||||
fn->Name = cache_str(new_name);
|
||||
code_c_interface.append(fn);
|
||||
}
|
||||
ast.append(entry);
|
||||
@ -702,7 +702,7 @@ R"(#define AST_ArrSpecs_Cap \
|
||||
( \
|
||||
AST_POD_Size \
|
||||
- sizeof(Code) \
|
||||
- sizeof(StringCached) \
|
||||
- sizeof(StrCached) \
|
||||
- sizeof(Code) * 2 \
|
||||
- sizeof(Token*) \
|
||||
- sizeof(Code) \
|
||||
@ -791,17 +791,17 @@ R"(#define AST_ArrSpecs_Cap \
|
||||
default: gen_generic_selection (Fail case) \
|
||||
) GEN_RESOLVED_FUNCTION_CALL( code, ... ) \
|
||||
*/
|
||||
StrBuilder generic_selector = StrBuilder::make_reserve(GlobalAllocator, kilobytes(2));
|
||||
StrBuilder generic_selector = StrBuilder::make_reserve(FallbackAllocator, kilobytes(2));
|
||||
for ( CodeFn fn : code_c_interface )
|
||||
{
|
||||
generic_selector.clear();
|
||||
Str private_prefix = txt("code__");
|
||||
Str actual_name = { fn->Name.Ptr + private_prefix.Len, fn->Name.Len - private_prefix.Len };
|
||||
StrBuilder interface_name = StrBuilder::fmt_buf(GlobalAllocator, "code_%S", actual_name );
|
||||
StrBuilder interface_name = StrBuilder::fmt_buf(FallbackAllocator, "code_%S", actual_name );
|
||||
|
||||
// Resolve generic's arguments
|
||||
b32 has_args = fn->Params->NumEntries > 1;
|
||||
StrBuilder params_str = StrBuilder::make_reserve(GlobalAllocator, 32);
|
||||
StrBuilder params_str = StrBuilder::make_reserve(FallbackAllocator, 32);
|
||||
for (CodeParams param = fn->Params->Next; param != fn->Params.end(); ++ param) {
|
||||
// We skip the first parameter as its always going to be the code for selection
|
||||
if (param->Next == nullptr) {
|
||||
@ -931,8 +931,8 @@ R"(#define <interface_name>( code ) _Generic( (code), \
|
||||
|
||||
if (prev && prev->Name.is_equal(entry->Name)) {
|
||||
// rename second definition so there isn't a symbol conflict
|
||||
StrBuilder postfix_arr = StrBuilder::fmt_buf(GlobalAllocator, "%S_arr", entry->Name);
|
||||
entry->Name = get_cached_string(postfix_arr.to_str());
|
||||
StrBuilder postfix_arr = StrBuilder::fmt_buf(FallbackAllocator, "%S_arr", entry->Name);
|
||||
entry->Name = cache_str(postfix_arr.to_str());
|
||||
postfix_arr.free();
|
||||
}
|
||||
|
||||
@ -942,11 +942,11 @@ R"(#define <interface_name>( code ) _Generic( (code), \
|
||||
// Convert the definition to use a default struct: https://vxtwitter.com/vkrajacic/status/1749816169736073295
|
||||
Str prefix = txt("def_");
|
||||
Str actual_name = { fn->Name.Ptr + prefix.Len, fn->Name.Len - prefix.Len };
|
||||
Str new_name = StrBuilder::fmt_buf(GlobalAllocator, "def__%S", actual_name ).to_str();
|
||||
Str new_name = StrBuilder::fmt_buf(FallbackAllocator, "def__%S", actual_name ).to_str();
|
||||
|
||||
// Resolve define's arguments
|
||||
b32 has_args = fn->Params->NumEntries > 1;
|
||||
StrBuilder params_str = StrBuilder::make_reserve(GlobalAllocator, 32);
|
||||
StrBuilder params_str = StrBuilder::make_reserve(FallbackAllocator, 32);
|
||||
for (CodeParams other_param = fn->Params; other_param != opt_param; ++ other_param) {
|
||||
if ( other_param == opt_param ) {
|
||||
params_str.append_fmt( "%S", other_param->Name );
|
||||
@ -970,7 +970,7 @@ R"(#define <interface_name>( code ) _Generic( (code), \
|
||||
, tmpl_fn_macro
|
||||
));
|
||||
|
||||
fn->Name = get_cached_string(new_name);
|
||||
fn->Name = cache_str(new_name);
|
||||
interface.append(fn);
|
||||
interface.append(fn_macro);
|
||||
if (entry->Next && entry->Next->Type != CT_NewLine) {
|
||||
@ -1021,9 +1021,9 @@ R"(#define <interface_name>( code ) _Generic( (code), \
|
||||
{
|
||||
Str old_prefix = txt("code_");
|
||||
Str actual_name = { fn->Name.Ptr + old_prefix.Len, fn->Name.Len - old_prefix.Len };
|
||||
StrBuilder new_name = StrBuilder::fmt_buf(GlobalAllocator, "code__%S", actual_name );
|
||||
StrBuilder new_name = StrBuilder::fmt_buf(FallbackAllocator, "code__%S", actual_name );
|
||||
|
||||
fn->Name = get_cached_string(new_name);
|
||||
fn->Name = cache_str(new_name);
|
||||
}
|
||||
inlines.append(entry);
|
||||
}
|
||||
@ -1176,9 +1176,9 @@ R"(#define <interface_name>( code ) _Generic( (code), \
|
||||
{
|
||||
Str old_prefix = txt("code_");
|
||||
Str actual_name = { fn->Name.Ptr + old_prefix.Len, fn->Name.Len - old_prefix.Len };
|
||||
StrBuilder new_name = StrBuilder::fmt_buf(GlobalAllocator, "code__%S", actual_name );
|
||||
StrBuilder new_name = StrBuilder::fmt_buf(FallbackAllocator, "code__%S", actual_name );
|
||||
|
||||
fn->Name = get_cached_string(new_name);
|
||||
fn->Name = cache_str(new_name);
|
||||
}
|
||||
src_ast.append(entry);
|
||||
}
|
||||
@ -1219,8 +1219,8 @@ R"(#define <interface_name>( code ) _Generic( (code), \
|
||||
)
|
||||
{
|
||||
// rename second definition so there isn't a symbol conflict
|
||||
StrBuilder postfix_arr = StrBuilder::fmt_buf(GlobalAllocator, "%S_arr", fn->Name);
|
||||
fn->Name = get_cached_string(postfix_arr.to_str());
|
||||
StrBuilder postfix_arr = StrBuilder::fmt_buf(FallbackAllocator, "%S_arr", fn->Name);
|
||||
fn->Name = cache_str(postfix_arr.to_str());
|
||||
postfix_arr.free();
|
||||
}
|
||||
|
||||
@ -1228,9 +1228,9 @@ R"(#define <interface_name>( code ) _Generic( (code), \
|
||||
{
|
||||
Str prefix = txt("def_");
|
||||
Str actual_name = { fn->Name.Ptr + prefix.Len, fn->Name.Len - prefix.Len };
|
||||
Str new_name = StrBuilder::fmt_buf(GlobalAllocator, "def__%S", actual_name ).to_str();
|
||||
Str new_name = StrBuilder::fmt_buf(FallbackAllocator, "def__%S", actual_name ).to_str();
|
||||
|
||||
fn->Name = get_cached_string(new_name);
|
||||
fn->Name = cache_str(new_name);
|
||||
}
|
||||
|
||||
src_upfront.append(fn);
|
||||
|
@ -224,7 +224,7 @@ word StrBuilder, gen_StrBuilder
|
||||
|
||||
namespace strbuilder_, gen_strbuilder_
|
||||
|
||||
word StringCached, gen_StringCached
|
||||
word StrCached, gen_StringCached
|
||||
|
||||
word StringTable, gen_StringTable
|
||||
|
||||
@ -401,7 +401,7 @@ word init, gen_init
|
||||
word deinit, gen_deinit
|
||||
word reset, gen_reset
|
||||
|
||||
word get_cached_string, gen_get_cached_string
|
||||
word cache_str, gen_get_cached_string
|
||||
|
||||
word make_code, gen_make_code
|
||||
|
||||
@ -450,8 +450,8 @@ word PreprocessorDefines, gen_PreprocessorDefines
|
||||
|
||||
// Backend
|
||||
|
||||
word GlobalAllocator, gen_GlobalAllocator
|
||||
word Global_AllocatorBuckets, gen_Global_AllocatorBuckets
|
||||
word FallbackAllocator, gen_GlobalAllocator
|
||||
word Fallback_AllocatorBuckets, gen_Global_AllocatorBuckets
|
||||
word CodePools, gen_CodePools
|
||||
word StringArenas, gen_StringArenas
|
||||
word StringCache, gen_StringCache
|
||||
@ -518,7 +518,7 @@ word _adt_get_field, gen__adt_get_field
|
||||
word _csv_write_record, gen__csv_write_record
|
||||
word _csv_write_header, gen__csv_write_header
|
||||
|
||||
word Global_Allocator_Proc, gen_Global_Allocator_Proc
|
||||
word fallback_allocator_proc, gen_Global_Allocator_Proc
|
||||
word define_constants, gen_define_constants
|
||||
word operator__validate, gen_operator__validate
|
||||
|
||||
|
@ -42,8 +42,8 @@ CodeBody gen_array_base()
|
||||
|
||||
CodeBody gen_array( Str type, Str array_name )
|
||||
{
|
||||
StrBuilder array_type = StrBuilder::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr );
|
||||
StrBuilder fn = StrBuilder::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr );
|
||||
StrBuilder array_type = StrBuilder::fmt_buf( FallbackAllocator, "%.*s", array_name.Len, array_name.Ptr );
|
||||
StrBuilder fn = StrBuilder::fmt_buf( FallbackAllocator, "%.*s", array_name.Len, array_name.Ptr );
|
||||
// c_str_to_lower(fn.Data);
|
||||
|
||||
#pragma push_macro( "GEN_ASSERT" )
|
||||
@ -375,7 +375,7 @@ CodeBody gen_array( Str type, Str array_name )
|
||||
#pragma pop_macro( "forceinline" )
|
||||
|
||||
++ Array_DefinitionCounter;
|
||||
Str slot_str = StrBuilder::fmt_buf(GlobalAllocator, "%d", Array_DefinitionCounter).to_str();
|
||||
Str slot_str = StrBuilder::fmt_buf(FallbackAllocator, "%d", Array_DefinitionCounter).to_str();
|
||||
|
||||
Code generic_interface_slot = untyped_str(token_fmt( "type", type, "array_type", (Str)array_type, "slot", (Str)slot_str,
|
||||
R"(#define GENERIC_SLOT_<slot>__array_init <type>, <array_type>_init
|
||||
@ -399,13 +399,13 @@ R"(#define GENERIC_SLOT_<slot>__array_init <type>, <array_type>_i
|
||||
));
|
||||
|
||||
return def_global_body( args(
|
||||
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( GlobalAllocator, "region %SB", array_type ))),
|
||||
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( FallbackAllocator, "region %SB", array_type ))),
|
||||
fmt_newline,
|
||||
generic_interface_slot,
|
||||
fmt_newline,
|
||||
result,
|
||||
fmt_newline,
|
||||
def_pragma( strbuilder_to_str(strbuilder_fmt_buf( GlobalAllocator, "endregion %SB", array_type ))),
|
||||
def_pragma( strbuilder_to_str(strbuilder_fmt_buf( FallbackAllocator, "endregion %SB", array_type ))),
|
||||
fmt_newline
|
||||
));
|
||||
};
|
||||
|
@ -30,16 +30,16 @@ R"(#define HashTable(_type) struct _type
|
||||
CodeBody gen_hashtable( Str type, Str hashtable_name )
|
||||
{
|
||||
|
||||
StrBuilder tbl_type = {(char*) hashtable_name.duplicate(GlobalAllocator).Ptr};
|
||||
StrBuilder fn = tbl_type.duplicate(GlobalAllocator);
|
||||
StrBuilder tbl_type = {(char*) hashtable_name.duplicate(FallbackAllocator).Ptr};
|
||||
StrBuilder fn = tbl_type.duplicate(FallbackAllocator);
|
||||
// c_str_to_lower(fn.Data);
|
||||
|
||||
StrBuilder name_lower = StrBuilder::make( GlobalAllocator, hashtable_name );
|
||||
StrBuilder name_lower = StrBuilder::make( FallbackAllocator, hashtable_name );
|
||||
// c_str_to_lower( name_lower.Data );
|
||||
|
||||
StrBuilder hashtable_entry = StrBuilder::fmt_buf( GlobalAllocator, "HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
|
||||
StrBuilder entry_array_name = StrBuilder::fmt_buf( GlobalAllocator, "Arr_HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
|
||||
StrBuilder entry_array_fn_ns = StrBuilder::fmt_buf( GlobalAllocator, "arr_hte_%.*s", name_lower.length(), name_lower.Data );
|
||||
StrBuilder hashtable_entry = StrBuilder::fmt_buf( FallbackAllocator, "HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
|
||||
StrBuilder entry_array_name = StrBuilder::fmt_buf( FallbackAllocator, "Arr_HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
|
||||
StrBuilder entry_array_fn_ns = StrBuilder::fmt_buf( FallbackAllocator, "arr_hte_%.*s", name_lower.length(), name_lower.Data );
|
||||
|
||||
CodeBody hashtable_types = parse_global_body( token_fmt(
|
||||
"type", (Str) type,
|
||||
@ -372,7 +372,7 @@ CodeBody gen_hashtable( Str type, Str hashtable_name )
|
||||
#pragma pop_macro( "forceinline" )
|
||||
|
||||
++ HashTable_DefinitionCounter;
|
||||
Str slot_str = StrBuilder::fmt_buf(GlobalAllocator, "%d", HashTable_DefinitionCounter).to_str();
|
||||
Str slot_str = StrBuilder::fmt_buf(FallbackAllocator, "%d", HashTable_DefinitionCounter).to_str();
|
||||
|
||||
Code generic_interface_slot = untyped_str(token_fmt( "type", type, "tbl_type", (Str)tbl_type, "slot", (Str)slot_str,
|
||||
R"(#define GENERIC_SLOT_<slot>__hashtable_init <type>, <tbl_type>_init
|
||||
@ -400,7 +400,7 @@ R"(#define GENERIC_SLOT_<slot>__hashtable_init <type>, <tbl_type>_
|
||||
, type.Len, type.Ptr );
|
||||
|
||||
return def_global_body(args(
|
||||
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( GlobalAllocator, "region %SB", tbl_type ))),
|
||||
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( FallbackAllocator, "region %SB", tbl_type ))),
|
||||
fmt_newline,
|
||||
generic_interface_slot,
|
||||
fmt_newline,
|
||||
@ -409,7 +409,7 @@ R"(#define GENERIC_SLOT_<slot>__hashtable_init <type>, <tbl_type>_
|
||||
entry_array,
|
||||
hashtable_def,
|
||||
fmt_newline,
|
||||
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( GlobalAllocator, "endregion %SB", tbl_type ))),
|
||||
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( FallbackAllocator, "endregion %SB", tbl_type ))),
|
||||
fmt_newline
|
||||
));
|
||||
}
|
||||
|
@ -84,7 +84,7 @@ Code gen_generic_selection_function_macro( s32 num_slots, Str macro_name, Generi
|
||||
) GEN_RESOLVED_FUNCTION_CALL( selector_arg )
|
||||
*/
|
||||
local_persist
|
||||
StrBuilder define_builder = StrBuilder::make_reserve(GlobalAllocator, kilobytes(64));
|
||||
StrBuilder define_builder = StrBuilder::make_reserve(FallbackAllocator, kilobytes(64));
|
||||
define_builder.clear();
|
||||
|
||||
Str macro_begin;
|
||||
@ -104,7 +104,7 @@ R"(#define <macro_name>(selector_arg, ...) _Generic( (selector_arg), \
|
||||
|
||||
for ( s32 slot = 1; slot <= num_slots; ++ slot )
|
||||
{
|
||||
Str slot_str = StrBuilder::fmt_buf(GlobalAllocator, "%d", slot).to_str();
|
||||
Str slot_str = StrBuilder::fmt_buf(FallbackAllocator, "%d", slot).to_str();
|
||||
define_builder.append( token_fmt( "macro_name", macro_name, "slot", slot_str,
|
||||
R"(GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_<slot>__<macro_name> ) \
|
||||
)"
|
||||
@ -147,9 +147,9 @@ CodeFn rename_function_to_unique_symbol(CodeFn fn, Str optional_prefix = txt("")
|
||||
|
||||
// Add prefix if provided
|
||||
if (optional_prefix.Len)
|
||||
new_name = strbuilder_fmt_buf(GlobalAllocator, "%S_%S_", optional_prefix, old_name);
|
||||
new_name = strbuilder_fmt_buf(FallbackAllocator, "%S_%S_", optional_prefix, old_name);
|
||||
else
|
||||
new_name = strbuilder_fmt_buf(GlobalAllocator, "%S_", old_name);
|
||||
new_name = strbuilder_fmt_buf(FallbackAllocator, "%S_", old_name);
|
||||
|
||||
// Add return type to the signature
|
||||
if (fn->ReturnType)
|
||||
@ -211,8 +211,8 @@ bool swap_pragma_region_implementation( Str region_name, SwapContentProc* swap_c
|
||||
bool found = false;
|
||||
CodePragma possible_region = cast(CodePragma, entry_iter);
|
||||
|
||||
StrBuilder region_sig = strbuilder_fmt_buf(GlobalAllocator, "region %s", region_name.Ptr);
|
||||
StrBuilder endregion_sig = strbuilder_fmt_buf(GlobalAllocator, "endregion %s", region_name.Ptr);
|
||||
StrBuilder region_sig = strbuilder_fmt_buf(FallbackAllocator, "region %s", region_name.Ptr);
|
||||
StrBuilder endregion_sig = strbuilder_fmt_buf(FallbackAllocator, "endregion %s", region_name.Ptr);
|
||||
if ( possible_region->Content.contains(region_sig))
|
||||
{
|
||||
found = true;
|
||||
|
Reference in New Issue
Block a user