This commit is contained in:
2024-12-12 12:55:15 -05:00
parent 8d48da0b9e
commit e3172057d3
57 changed files with 2098 additions and 2522 deletions

View File

@ -24,24 +24,24 @@ constexpr char const* generation_notice =
"// This file was generated automatially by gencpp's c_library.cpp "
"(See: https://github.com/Ed94/gencpp)\n\n";
constexpr StrC roll_own_dependencies_guard_start = txt(R"(
constexpr Str roll_own_dependencies_guard_start = txt(R"(
//! If its desired to roll your own dependencies, define GEN_ROLL_OWN_DEPENDENCIES before including this file.
// Dependencies are derived from the c-zpl library: https://github.com/zpl-c/zpl
#ifndef GEN_ROLL_OWN_DEPENDENCIES
)");
constexpr StrC roll_own_dependencies_guard_end = txt(R"(
constexpr Str roll_own_dependencies_guard_end = txt(R"(
// GEN_ROLL_OWN_DEPENDENCIES
#endif
)");
constexpr StrC implementation_guard_start = txt(R"(
constexpr Str implementation_guard_start = txt(R"(
#pragma region GENCPP IMPLEMENTATION GUARD
#if defined(GEN_IMPLEMENTATION) && ! defined(GEN_IMPLEMENTED)
# define GEN_IMPLEMENTED
)");
constexpr StrC implementation_guard_end = txt(R"(
constexpr Str implementation_guard_end = txt(R"(
#endif
#pragma endregion GENCPP IMPLEMENTATION GUARD
)");
@ -87,7 +87,7 @@ int gen_main()
#pragma region Resolve Dependencies
Code header_platform = scan_file( path_base "dependencies/platform.hpp" );
Code header_macros = scan_file( path_base "dependencies/macros.hpp" );
Code header_generic_macros = scan_file( "components/generic_macros.hpp" );
Code header_generic_macros = scan_file( "components/generic_macros.h" );
Code header_basic_types = scan_file( path_base "dependencies/basic_types.hpp" );
Code header_debug = scan_file( path_base "dependencies/debug.hpp" );
Code header_string_ops = scan_file( path_base "dependencies/string_ops.hpp" );
@ -100,7 +100,7 @@ int gen_main()
{
case CT_Using:
{
log_fmt("REPLACE THIS MANUALLY: %SC\n", entry->Name);
log_fmt("REPLACE THIS MANUALLY: %S\n", entry->Name);
CodeUsing using_ver = cast(CodeUsing, entry);
CodeTypedef typedef_ver = def_typedef(using_ver->Name, using_ver->UnderlyingType);
@ -126,7 +126,7 @@ int gen_main()
if (fn->Specs) {
s32 constexpr_found = fn->Specs.remove( Spec_Constexpr );
if (constexpr_found > -1) {
//log_fmt("Found constexpr: %S\n", entry.to_string());
//log_fmt("Found constexpr: %SB\n", entry.to_string());
fn->Specs.append(Spec_Inline);
}
}
@ -238,7 +238,7 @@ do \
break;
case CT_Variable:
{
if ( strc_contains(entry->Name, txt("Msg_Invalid_Value")))
if ( str_contains(entry->Name, txt("Msg_Invalid_Value")))
{
CodeDefine define = def_define(entry->Name, entry->Value->Content);
header_printing.append(define);
@ -292,16 +292,16 @@ do \
case CT_Preprocess_IfNotDef:
{
//log_fmt("\nIFNDEF: %SC\n", entry->Content);
//log_fmt("\nIFNDEF: %S\n", entry->Content);
header_strings.append(entry);
}
break;
case CT_Struct_Fwd:
{
if ( entry->Name.is_equal(txt("String")) )
if ( entry->Name.is_equal(txt("StrBuilder")) )
{
CodeTypedef c_def = parse_typedef(code( typedef char* String; ));
CodeTypedef c_def = parse_typedef(code( typedef char* StrBuilder; ));
header_strings.append(c_def);
header_strings.append(fmt_newline);
++ entry;
@ -341,12 +341,12 @@ do \
case CT_Typedef:
{
StrC name_string_table = txt("StringTable");
Str name_string_table = txt("StringTable");
CodeTypedef td = cast(CodeTypedef, entry);
if (td->Name.contains(name_string_table))
{
CodeBody ht = gen_hashtable(txt("gen_StrC"), name_string_table);
CodeBody ht = gen_hashtable(txt("gen_Str"), name_string_table);
header_strings.append(ht);
break;
}
@ -388,8 +388,8 @@ do \
case CT_Union:
case CT_Union_Fwd:
{
StrC type_str = codetype_to_keyword_str(entry->Type);
StrC formated_tmpl = token_fmt_impl( 3,
Str type_str = codetype_to_keyword_str(entry->Type);
Str formated_tmpl = token_fmt_impl( 3,
"type", type_str
, "name", entry->Name,
stringize(
@ -490,8 +490,8 @@ do \
continue;
}
StrC type_str = codetype_to_keyword_str(entry->Type);
StrC formated_tmpl = token_fmt_impl( 3,
Str type_str = codetype_to_keyword_str(entry->Type);
Str formated_tmpl = token_fmt_impl( 3,
"type", type_str
, "name", entry->Name,
stringize(
@ -549,9 +549,9 @@ do \
{
CodeTypename type = using_ver->UnderlyingType;
CodeTypedef typedef_ver = parse_typedef(token_fmt(
"ReturnType", to_string(type->ReturnType).to_strc()
"ReturnType", to_string(type->ReturnType).to_str()
, "Name" , using_ver->Name
, "Parameters", to_string(type->Params).to_strc()
, "Parameters", to_string(type->Params).to_str()
, stringize(
typedef <ReturnType>( * <Name>)(<Parameters>);
)));
@ -578,7 +578,7 @@ do \
types.append(entry_td);
continue;
}
//log_fmt("Detected ENUM: %S", entry->Name);
//log_fmt("Detected ENUM: %SB", entry->Name);
convert_cpp_enum_to_c(cast(CodeEnum, entry), types);
}
break;
@ -645,9 +645,9 @@ do \
CodeFn fn = cast(CodeFn, entry);
if (fn->Name.starts_with(txt("code_")))
{
StrC old_prefix = txt("code_");
StrC actual_name = { fn->Name.Len - old_prefix.Len, fn->Name.Ptr + old_prefix.Len };
String new_name = String::fmt_buf(GlobalAllocator, "code__%SC", actual_name );
Str old_prefix = txt("code_");
Str actual_name = { fn->Name.Len - old_prefix.Len, fn->Name.Ptr + old_prefix.Len };
StrBuilder new_name = StrBuilder::fmt_buf(GlobalAllocator, "code__%S", actual_name );
fn->Name = get_cached_string(new_name);
code_c_interface.append(fn);
@ -694,21 +694,21 @@ do \
s32 constexpr_found = var->Specs ? var->Specs.remove( Spec_Constexpr ) : - 1;
if (constexpr_found > -1) {
//log_fmt("Found constexpr: %S\n", entry.to_string());
//log_fmt("Found constexpr: %SB\n", entry.to_string());
if (var->Name.contains(txt("AST_ArrSpecs_Cap")))
{
Code def = untyped_str(txt(
R"(#define AST_ArrSpecs_Cap \
( \
AST_POD_Size \
- sizeof(Code) \
- sizeof(StringCached) \
- sizeof(Code) * 2 \
- sizeof(Token*) \
- sizeof(Code) \
- sizeof(CodeType) \
- sizeof(ModuleFlag) \
- sizeof(u32) \
AST_POD_Size \
- sizeof(Code) \
- sizeof(StringCached) \
- sizeof(Code) * 2 \
- sizeof(Token*) \
- sizeof(Code) \
- sizeof(CodeType) \
- sizeof(ModuleFlag) \
- sizeof(u32) \
) \
/ sizeof(Specifier) - 1
)"
@ -729,7 +729,7 @@ R"(#define AST_ArrSpecs_Cap \
break;
}
StrC code_typenames[] = {
Str code_typenames[] = {
txt("Code"),
txt("CodeBody"),
txt("CodeAttributes"),
@ -791,24 +791,24 @@ R"(#define AST_ArrSpecs_Cap \
default: gen_generic_selection (Fail case) \
) GEN_RESOLVED_FUNCTION_CALL( code, ... ) \
*/
String generic_selector = String::make_reserve(GlobalAllocator, kilobytes(2));
StrBuilder generic_selector = StrBuilder::make_reserve(GlobalAllocator, kilobytes(2));
for ( CodeFn fn : code_c_interface )
{
generic_selector.clear();
StrC private_prefix = txt("code__");
StrC actual_name = { fn->Name.Len - private_prefix.Len, fn->Name.Ptr + private_prefix.Len };
String interface_name = String::fmt_buf(GlobalAllocator, "code_%SC", actual_name );
Str private_prefix = txt("code__");
Str actual_name = { fn->Name.Len - private_prefix.Len, fn->Name.Ptr + private_prefix.Len };
StrBuilder interface_name = StrBuilder::fmt_buf(GlobalAllocator, "code_%S", actual_name );
// Resolve generic's arguments
b32 has_args = fn->Params->NumEntries > 1;
String params_str = String::make_reserve(GlobalAllocator, 32);
StrBuilder params_str = StrBuilder::make_reserve(GlobalAllocator, 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) {
params_str.append_fmt( "%SC", param->Name );
params_str.append_fmt( "%S", param->Name );
continue;
}
params_str.append_fmt( "%SC, ", param->Name );
params_str.append_fmt( "%S, ", param->Name );
}
char const* tmpl_def_start = nullptr;
@ -824,14 +824,14 @@ R"(#define <interface_name>( code ) _Generic( (code), \
}
// Definition start
generic_selector.append( token_fmt(
"interface_name", interface_name.to_strc()
, "params", params_str.to_strc() // Only used if has_args
"interface_name", interface_name.to_str()
, "params", params_str.to_str() // Only used if has_args
, tmpl_def_start
));
// Append slots
for(StrC type : code_typenames ) {
generic_selector.append_fmt("%SC : %SC,\\\n", type, fn->Name );
for(Str type : code_typenames ) {
generic_selector.append_fmt("%S : %S,\\\n", type, fn->Name );
}
generic_selector.append(txt("default: gen_generic_selection_fail \\\n"));
@ -844,7 +844,7 @@ R"(#define <interface_name>( code ) _Generic( (code), \
}
// Definition end
generic_selector.append( token_fmt(
"params", params_str.to_strc()
"params", params_str.to_str()
, "type", fn->Params->ValueType->Name
, tmpl_def_end ) );
@ -890,8 +890,8 @@ R"(#define <interface_name>( code ) _Generic( (code), \
char conversion_buf[32] = {};
u64_to_str(size_of(AST_Body::_PAD_), conversion_buf, 10);
StrC arr_exp = union_entry->ValueType->ArrExpr->Content;
StrC cpp_size = to_strc_from_c_str(conversion_buf);
Str arr_exp = union_entry->ValueType->ArrExpr->Content;
Str cpp_size = to_str_from_c_str(conversion_buf);
union_entry->ValueType->ArrExpr = untyped_str( cpp_size );
union_entry->InlineCmt = untyped_str(token_fmt("arr_exp", arr_exp,
"// Had to hardcode _PAD_ because (<arr_exp>) was 67 bytes in C\n"
@ -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
String postfix_arr = String::fmt_buf(GlobalAllocator, "%SC_arr", entry->Name);
entry->Name = get_cached_string(postfix_arr.to_strc());
StrBuilder postfix_arr = StrBuilder::fmt_buf(GlobalAllocator, "%S_arr", entry->Name);
entry->Name = get_cached_string(postfix_arr.to_str());
postfix_arr.free();
}
@ -940,20 +940,20 @@ R"(#define <interface_name>( code ) _Generic( (code), \
for ( CodeParams opt_param : fn->Params ) if (opt_param->ValueType->Name.starts_with(txt("Opts_")))
{
// Convert the definition to use a default struct: https://vxtwitter.com/vkrajacic/status/1749816169736073295
StrC prefix = txt("def_");
StrC actual_name = { fn->Name.Len - prefix.Len, fn->Name.Ptr + prefix.Len };
StrC new_name = String::fmt_buf(GlobalAllocator, "def__%SC", actual_name ).to_strc();
Str prefix = txt("def_");
Str actual_name = { fn->Name.Len - prefix.Len, fn->Name.Ptr + prefix.Len };
Str new_name = StrBuilder::fmt_buf(GlobalAllocator, "def__%S", actual_name ).to_str();
// Resolve define's arguments
b32 has_args = fn->Params->NumEntries > 1;
String params_str = String::make_reserve(GlobalAllocator, 32);
StrBuilder params_str = StrBuilder::make_reserve(GlobalAllocator, 32);
for (CodeParams other_param = fn->Params; other_param != opt_param; ++ other_param) {
if ( other_param == opt_param ) {
params_str.append_fmt( "%SC", other_param->Name );
params_str.append_fmt( "%S", other_param->Name );
break;
}
// If there are arguments before the optional, prepare them here.
params_str.append_fmt( "%SC, ", other_param->Name );
params_str.append_fmt( "%S, ", other_param->Name );
}
char const* tmpl_fn_macro = nullptr;
if (params_str.length() > 0 ) {
@ -965,7 +965,7 @@ R"(#define <interface_name>( code ) _Generic( (code), \
Code fn_macro = untyped_str(token_fmt(
"def_name", fn->Name
, "def__name", new_name
, "params", params_str.to_strc()
, "params", params_str.to_str()
, "opts_type", opt_param->ValueType->Name
, tmpl_fn_macro
));
@ -1019,9 +1019,9 @@ R"(#define <interface_name>( code ) _Generic( (code), \
CodeFn fn = cast(CodeFn, entry);
if (fn->Name.starts_with(txt("code_")))
{
StrC old_prefix = txt("code_");
StrC actual_name = { fn->Name.Len - old_prefix.Len, fn->Name.Ptr + old_prefix.Len };
String new_name = String::fmt_buf(GlobalAllocator, "code__%SC", actual_name );
Str old_prefix = txt("code_");
Str actual_name = { fn->Name.Len - old_prefix.Len, fn->Name.Ptr + old_prefix.Len };
StrBuilder new_name = StrBuilder::fmt_buf(GlobalAllocator, "code__%S", actual_name );
fn->Name = get_cached_string(new_name);
}
@ -1174,9 +1174,9 @@ R"(#define <interface_name>( code ) _Generic( (code), \
CodeFn fn = cast(CodeFn, entry);
if (fn->Name.starts_with(txt("code_")))
{
StrC old_prefix = txt("code_");
StrC actual_name = { fn->Name.Len - old_prefix.Len, fn->Name.Ptr + old_prefix.Len };
String new_name = String::fmt_buf(GlobalAllocator, "code__%SC", actual_name );
Str old_prefix = txt("code_");
Str actual_name = { fn->Name.Len - old_prefix.Len, fn->Name.Ptr + old_prefix.Len };
StrBuilder new_name = StrBuilder::fmt_buf(GlobalAllocator, "code__%S", actual_name );
fn->Name = get_cached_string(new_name);
}
@ -1219,16 +1219,16 @@ R"(#define <interface_name>( code ) _Generic( (code), \
)
{
// rename second definition so there isn't a symbol conflict
String postfix_arr = String::fmt_buf(GlobalAllocator, "%SC_arr", fn->Name);
fn->Name = get_cached_string(postfix_arr.to_strc());
StrBuilder postfix_arr = StrBuilder::fmt_buf(GlobalAllocator, "%S_arr", fn->Name);
fn->Name = get_cached_string(postfix_arr.to_str());
postfix_arr.free();
}
for ( CodeParams opt_param : fn->Params ) if (opt_param->ValueType->Name.starts_with(txt("Opts_")))
{
StrC prefix = txt("def_");
StrC actual_name = { fn->Name.Len - prefix.Len, fn->Name.Ptr + prefix.Len };
StrC new_name = String::fmt_buf(GlobalAllocator, "def__%SC", actual_name ).to_strc();
Str prefix = txt("def_");
Str actual_name = { fn->Name.Len - prefix.Len, fn->Name.Ptr + prefix.Len };
Str new_name = StrBuilder::fmt_buf(GlobalAllocator, "def__%S", actual_name ).to_str();
fn->Name = get_cached_string(new_name);
}
@ -1319,7 +1319,7 @@ R"(#define <interface_name>( code ) _Generic( (code), \
Code define_ver = untyped_str(token_fmt(
"name", var->Name
, "value", var->Value->Content
, "type", var->ValueType.to_string().to_strc()
, "type", var->ValueType.to_string().to_str()
, "#define <name> (<type>) <value>\n"
));
src_lexer.append(define_ver);
@ -1364,7 +1364,7 @@ R"(#define <interface_name>( code ) _Generic( (code), \
Code define_ver = untyped_str(token_fmt(
"name", var->Name
, "value", var->Value->Content
, "type", var->ValueType.to_string().to_strc()
, "type", var->ValueType.to_string().to_str()
, "#define <name> (<type>) <value>\n"
));
src_parser.append(define_ver);
@ -1458,7 +1458,7 @@ R"(#define <interface_name>( code ) _Generic( (code), \
Code rf_src_parser = refactor_and_format(src_parser);
Code r_src_parsing = refactor(src_parsing_interface);
Code r_src_untyped = refactor(src_untyped);
CodeBody etoktype = gen_etoktype( path_base "enums/ETokType.csv", path_base "enums/AttributeTokens.csv", helper_use_c_definition );
Code rf_etoktype = refactor_and_format(etoktype);

View File

@ -167,13 +167,13 @@ namespace pool_, gen_pool_
// Printing
namespace str_, gen_str_
namespace c_str_, gen_c_str_
word PrintF_Buffer, gen_PrintF_Buffer
word Msg_Invalid_Value, gen_Msg_Invalid_Value
word log_fmt, gen_log_fmt
// String Ops
// StrBuilder Ops
namespace char_, gen_char_
@ -211,18 +211,18 @@ word crc64, gen_crc64
// Strings
word StrC, gen_StrC
word Str, gen_Str
word to_strc_from_c_str, gen_to_strc_from_c_str
word to_str_from_c_str, gen_to_str_from_c_str
namespace strc_, gen_strc_
namespace str_, gen_str_
word cast_to_strc, gen_cast_to_strc
word cast_to_str, gen_cast_to_str
word StringHeader, gen_StringHeader
word String, gen_String
word StrBuilderHeader, gen_StrBuilderHeader
word StrBuilder, gen_StrBuilder
namespace string_, gen_string_
namespace strbuilder_, gen_strbuilder_
word StringCached, gen_StringCached
@ -308,7 +308,7 @@ word operator_to_str, gen_operator_to_str
word Specifier, gen_Specifier
word spec_to_str, gen_spec_to_str
word spec_is_trailing, gen_spec_is_trailing
// word strc_to_specifier, gen_strc_to_specifier
// word str_to_specifier, gen_str_to_specifier
// AST
@ -523,7 +523,7 @@ word parser_deinit, gen_parser_deinit
word TokType, gen_TokType
word toktype_to_str, gen_toktype_to_str
// word strc_to_toktype, gen_strc_to_toktype
// word str_to_toktype, gen_str_to_toktype
word NullToken, gen_NullToken
namespace tok_, gen_tok_

View File

@ -40,11 +40,11 @@ CodeBody gen_array_base()
));
};
CodeBody gen_array( StrC type, StrC array_name )
CodeBody gen_array( Str type, Str array_name )
{
String array_type = String::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr );
String fn = String::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr );
// str_to_lower(fn.Data);
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 );
// c_str_to_lower(fn.Data);
#pragma push_macro( "GEN_ASSERT" )
#pragma push_macro( "rcast" )
@ -56,7 +56,7 @@ CodeBody gen_array( StrC type, StrC array_name )
#undef cast
#undef typeof
#undef forceinline
CodeBody result = parse_global_body( token_fmt( "array_type", (StrC)array_type, "fn", (StrC)fn, "type", (StrC)type
CodeBody result = parse_global_body( token_fmt( "array_type", (Str)array_type, "fn", (Str)fn, "type", (Str)type
, stringize(
typedef <type>* <array_type>;
@ -375,9 +375,9 @@ CodeBody gen_array( StrC type, StrC array_name )
#pragma pop_macro( "forceinline" )
++ Array_DefinitionCounter;
StrC slot_str = String::fmt_buf(GlobalAllocator, "%d", Array_DefinitionCounter).to_strc();
Str slot_str = StrBuilder::fmt_buf(GlobalAllocator, "%d", Array_DefinitionCounter).to_str();
Code generic_interface_slot = untyped_str(token_fmt( "type", type, "array_type", (StrC)array_type, "slot", (StrC)slot_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
#define GENERIC_SLOT_<slot>__array_init_reserve <type>, <array_type>_init_reserve
#define GENERIC_SLOT_<slot>__array_append <array_type>, <array_type>_append
@ -399,13 +399,13 @@ R"(#define GENERIC_SLOT_<slot>__array_init <type>, <array_type>_i
));
return def_global_body( args(
def_pragma( string_to_strc( string_fmt_buf( GlobalAllocator, "region %S", array_type ))),
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( GlobalAllocator, "region %SB", array_type ))),
fmt_newline,
generic_interface_slot,
fmt_newline,
result,
fmt_newline,
def_pragma( string_to_strc(string_fmt_buf( GlobalAllocator, "endregion %S", array_type ))),
def_pragma( strbuilder_to_str(strbuilder_fmt_buf( GlobalAllocator, "endregion %SB", array_type ))),
fmt_newline
));
};

View File

@ -27,24 +27,24 @@ R"(#define HashTable(_type) struct _type
return def_global_body(args(struct_def, define_type, define_critical_load_scale));
}
CodeBody gen_hashtable( StrC type, StrC hashtable_name )
CodeBody gen_hashtable( Str type, Str hashtable_name )
{
String tbl_type = {(char*) hashtable_name.duplicate(GlobalAllocator).Ptr};
String fn = tbl_type.duplicate(GlobalAllocator);
// str_to_lower(fn.Data);
StrBuilder tbl_type = {(char*) hashtable_name.duplicate(GlobalAllocator).Ptr};
StrBuilder fn = tbl_type.duplicate(GlobalAllocator);
// c_str_to_lower(fn.Data);
String name_lower = String::make( GlobalAllocator, hashtable_name );
// str_to_lower( name_lower.Data );
StrBuilder name_lower = StrBuilder::make( GlobalAllocator, hashtable_name );
// c_str_to_lower( name_lower.Data );
String hashtable_entry = String::fmt_buf( GlobalAllocator, "HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
String entry_array_name = String::fmt_buf( GlobalAllocator, "Arr_HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr );
String entry_array_fn_ns = String::fmt_buf( GlobalAllocator, "arr_hte_%.*s", name_lower.length(), 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 );
CodeBody hashtable_types = parse_global_body( token_fmt(
"type", (StrC) type,
"tbl_name", (StrC) hashtable_name,
"tbl_type", (StrC) tbl_type,
"type", (Str) type,
"tbl_name", (Str) hashtable_name,
"tbl_type", (Str) tbl_type,
stringize(
typedef struct HashTable_<type> <tbl_type>;
typedef struct HTE_<tbl_name> HTE_<tbl_name>;
@ -74,13 +74,13 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name )
#undef typeof
#undef forceinline
CodeBody hashtable_def = parse_global_body( token_fmt(
"type", (StrC) type,
"tbl_name", (StrC) hashtable_name,
"tbl_type", (StrC) tbl_type,
"fn", (StrC) fn,
"entry_type", (StrC) hashtable_entry,
"array_entry", (StrC) entry_array_name,
"fn_array", (StrC) entry_array_fn_ns,
"type", (Str) type,
"tbl_name", (Str) hashtable_name,
"tbl_type", (Str) tbl_type,
"fn", (Str) fn,
"entry_type", (Str) hashtable_entry,
"array_entry", (Str) entry_array_name,
"fn_array", (Str) entry_array_fn_ns,
stringize(
struct HashTable_<type> {
Array_ssize Hashes;
@ -372,9 +372,9 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name )
#pragma pop_macro( "forceinline" )
++ HashTable_DefinitionCounter;
StrC slot_str = String::fmt_buf(GlobalAllocator, "%d", HashTable_DefinitionCounter).to_strc();
Str slot_str = StrBuilder::fmt_buf(GlobalAllocator, "%d", HashTable_DefinitionCounter).to_str();
Code generic_interface_slot = untyped_str(token_fmt( "type", type, "tbl_type", (StrC)tbl_type, "slot", (StrC)slot_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
#define GENERIC_SLOT_<slot>__hashtable_init_reserve <type>, <tbl_type>_init_reserve
#define GENERIC_SLOT_<slot>__hashtable_clear <tbl_type>, <tbl_type>_clear
@ -395,12 +395,12 @@ R"(#define GENERIC_SLOT_<slot>__hashtable_init <type>, <tbl_type>_
)"
));
char const* cmt_str = str_fmt_buf( "Name: %.*s Type: %.*s"
char const* cmt_str = c_str_fmt_buf( "Name: %.*s Type: %.*s"
, tbl_type.length(), tbl_type.Data
, type.Len, type.Ptr );
return def_global_body(args(
def_pragma( string_to_strc( string_fmt_buf( GlobalAllocator, "region %S", tbl_type ))),
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( GlobalAllocator, "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( string_to_strc( string_fmt_buf( GlobalAllocator, "endregion %S", tbl_type ))),
def_pragma( strbuilder_to_str( strbuilder_fmt_buf( GlobalAllocator, "endregion %SB", tbl_type ))),
fmt_newline
));
}

View File

@ -3,9 +3,9 @@
// ____ _ ______ _ _ ____ _ __ _
// / ___} (_) | ____} | | (_) / __ \ | | | |(_)
// | | ___ ___ _ __ ___ _ __ _ ___ | |__ _ _ _ __ ___| |_ _ ___ _ __ | | | |_ _____ _ __ | | ___ __ _ __| | _ _ __ __ _
// | |{__ |/ _ \ '_ \ / _ \ '__} |/ __| | __} | | | '_ \ / __} __} |/ _ \| '_ \ | | | \ \ / / _ \ '_ \| |/ _ \ / _` |/ _` || | '_ \ / _` |
// | |__j | __/ | | | __/ | | | (__ | | | |_| | | | | (__| l_| | (_) | | | | | l__| |\ V / __/ | | | | (_) | (_| | (_| || | | | | (_| |
// \____/ \___}_l l_l\___}_l l_l\___| l_l \__,_l_l l_l\___}\__}_l\___/l_l l_l \____/ \_/ \___}_l l_l_l\___/ \__,_l\__,_l|_|_| |_|\__, |
// | |{__ |/ _ \ '_ \ / _ \ '__} |/ __| | __} | | | '_ \ / __} __} |/ _ \| '_ \ | | | \ \ / / _ \ '_ }| |/ _ \ / _` |/ _` || | '_ \ / _` |
// | |__j | __/ | | | __/ | | | (__ | | | |_| | | | | (__| l_| | (_) | | | | | l__| |\ V / __/ | | | (_) | (_| | (_| || | | | | (_| |
// \____/ \___}_l l_l\___}_l l_l\___| l_l \__,_l_l l_l\___}\__}_l\___/l_l l_l \____/ \_/ \___}_l l_l\___/ \__,_l\__,_l|_|_| |_|\__, |
// This implemnents macros for utilizing "The Naive Extendible _Generic Macro" explained in: __| |
// https://github.com/JacksonAllan/CC/blob/main/articles/Better_C_Generics_Part_1_The_Extendible_Generic.md {___/
// Since gencpp is used to generate the c-library, it was choosen over the more novel implementations to keep the macros as easy to understand and unobfuscated as possible.
@ -30,7 +30,7 @@
// Where GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER is specifically looking for that <comma> ,
#define GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( slot_exp ) GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER( slot_exp, GEN_GENERIC_SEL_ENTRY_TYPE( slot_exp, ): GEN_GENERIC_SEL_ENTRY_FUNCTION( slot_exp, ) GEN_COMMA_OPERATOR, , )
// ^ Selects the comma ^ is the type ^ is the function ^ Insert a comma
// The slot won't exist if that comma is not found. |
// The slot won't exist if that comma is not found.
// For the occastion where an expression didn't resolve to a selection option the "default: <value>" will be set to:
typedef struct GENCPP_NO_RESOLVED_GENERIC_SELECTION GENCPP_NO_RESOLVED_GENERIC_SELECTION;
@ -43,12 +43,12 @@ GENCPP_NO_RESOLVED_GENERIC_SELECTION const gen_generic_selection_fail = {0};
// Below are generated on demand for an overlaod depdendent on a type:
// ----------------------------------------------------------------------------------------------------------------------------------
#define GEN_FUNCTION_GENERIC_EXAMPLE( selector_arg ) _Generic( \
(selector_arg), /* Select Via Expression*/ \
/* Extendibility slots: */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
default: gen_generic_selection_fail \
#define GEN_FUNCTION_GENERIC_EXAMPLE( selector_arg ) _Generic( \
(selector_arg), /* Select Via Expression*/ \
/* Extendibility slots: */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_1__function_sig ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_2__function_sig ) \
default: gen_generic_selection_fail \
) GEN_RESOLVED_FUNCTION_CALL( selector_arg )
// ----------------------------------------------------------------------------------------------------------------------------------
@ -68,20 +68,21 @@ size_t gen_example_hash__P_long( long val ) { return val * 2654435761ull; }
#define GENERIC_SLOT_2_gen_example_hash long long, gen_example_hash__P_long_long
size_t gen_example_hash__P_long_long( long long val ) { return val * 2654435761ull; }
// If using an Editor with support for syntax hightlighting macros: HASH__ARGS_SIG_1 and HASH_ARGS_SIG_2 should show color highlighting indicating the slot is enabled,
// If using an Editor with support for syntax hightlighting macros:
// GENERIC_SLOT_1_gen_example_hash and GENERIC_SLOT_2_gen_example_hash should show color highlighting indicating the slot is enabled,
// or, "defined" for usage during the compilation pass that handles the _Generic instrinsic.
#define gen_hash_example( function_arguments ) _Generic( \
(function_arguments), /* Select Via Expression*/ \
/* Extendibility slots: */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_2 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_3 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_4 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_5 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_6 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_7 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( HASH__ARGS_SIG_8 ) \
default: gen_generic_selection_fail \
#define gen_hash_example( function_arguments ) _Generic( \
(function_arguments), /* Select Via Expression*/ \
/* Extendibility slots: */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_1_gen_example_hash ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_2_gen_example_hash ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_3_gen_example_hash ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_4_gen_example_hash ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_5_gen_example_hash ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_6_gen_example_hash ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_7_gen_example_hash ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_8_gen_example_hash ) \
default: gen_generic_selection_fail \
) GEN_RESOLVED_FUNCTION_CALL( function_arguments )
// Additional Variations:
@ -89,20 +90,20 @@ size_t gen_example_hash__P_long_long( long long val ) { return val * 2654435761u
// If the function takes more than one argument the following is used:
#define GEN_FUNCTION_GENERIC_EXAMPLE_VARADIC( selector_arg, ... ) _Generic( \
(selector_arg), \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_2 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_1__function_sig ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_2__function_sig ) \
/* ... */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT(FunctionID__ARGS_SIG_N ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT(GENERIC_SLOT_N__function_sig ) \
default: gen_generic_selection_fail \
) GEN_RESOLVED_FUNCTION_CALL( selector_arg, __VA_ARG__ )
// If the function does not take the arugment as a parameter:
#define GEN_FUNCTION_GENERIC_EXAMPLE_DIRECT_TYPE( selector_arg ) _Generic( \
( GEN_TYPE_TO_EXP(selector_arg) ), \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_2 ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_1__function_sig ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_2__function_sig ) \
/* ... */ \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT(FunctionID__ARGS_SIG_N ) \
GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT(GENERIC_SLOT_N__function_sig ) \
default: gen_generic_selection_fail \
) GEN_RESOLVED_FUNCTION_CALL()

View File

@ -18,13 +18,13 @@ void convert_cpp_enum_to_c( CodeEnum to_convert, CodeBody to_append )
#pragma pop_macro("enum_underlying")
}
b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& parsed_body, CodeBody& body )
b32 ignore_preprocess_cond_block( Str cond_sig, Code& entry_iter, CodeBody& parsed_body, CodeBody& body )
{
b32 found = false;
CodePreprocessCond cond = cast(CodePreprocessCond, entry_iter);
if ( cond->Content.is_equal(cond_sig) )
{
//log_fmt("Preprocess cond found: %SC\n", cond->Content);
//log_fmt("Preprocess cond found: %S\n", cond->Content);
found = true;
s32 depth = 1;
@ -72,7 +72,7 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& par
constexpr bool GenericSel_One_Arg = true;
enum GenericSelectionOpts : u32 { GenericSel_Default, GenericSel_By_Ref, GenericSel_Direct_Type };
Code gen_generic_selection_function_macro( s32 num_slots, StrC macro_name, GenericSelectionOpts opts = GenericSel_Default, bool one_arg = false )
Code gen_generic_selection_function_macro( s32 num_slots, Str macro_name, GenericSelectionOpts opts = GenericSel_Default, bool one_arg = false )
{
/* Implements:
#define GEN_FUNCTION_GENERIC_EXAMPLE( selector_arg, ... ) _Generic( \
@ -84,18 +84,18 @@ Code gen_generic_selection_function_macro( s32 num_slots, StrC macro_name, Gener
) GEN_RESOLVED_FUNCTION_CALL( selector_arg )
*/
local_persist
String define_builder = String::make_reserve(GlobalAllocator, kilobytes(64));
StrBuilder define_builder = StrBuilder::make_reserve(GlobalAllocator, kilobytes(64));
define_builder.clear();
StrC macro_begin;
Str macro_begin;
if (opts == GenericSel_Direct_Type) {
macro_begin = token_fmt( "macro_name", (StrC)macro_name,
macro_begin = token_fmt( "macro_name", (Str)macro_name,
R"(#define <macro_name>(selector_arg, ...) _Generic( (*(selector_arg*)NULL ), \
)"
);
}
else {
macro_begin = token_fmt( "macro_name", (StrC)macro_name,
macro_begin = token_fmt( "macro_name", (Str)macro_name,
R"(#define <macro_name>(selector_arg, ...) _Generic( (selector_arg), \
)"
);
@ -104,7 +104,7 @@ R"(#define <macro_name>(selector_arg, ...) _Generic( (selector_arg), \
for ( s32 slot = 1; slot <= num_slots; ++ slot )
{
StrC slot_str = String::fmt_buf(GlobalAllocator, "%d", slot).to_strc();
Str slot_str = StrBuilder::fmt_buf(GlobalAllocator, "%d", slot).to_str();
if (slot == num_slots && false)
{
define_builder.append( token_fmt( "macro_name", macro_name, "slot", slot_str,
@ -152,25 +152,25 @@ R"( GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_<slot>__<macro_name> )
// Add gap for next definition
define_builder.append(txt("\n\n"));
Code macro = untyped_str(define_builder.to_strc());
Code macro = untyped_str(define_builder.to_str());
return macro;
}
CodeFn rename_function_to_unique_symbol(CodeFn fn, StrC optional_prefix = txt(""))
CodeFn rename_function_to_unique_symbol(CodeFn fn, Str optional_prefix = txt(""))
{
// Get basic components for the name
StrC old_name = fn->Name;
String new_name;
Str old_name = fn->Name;
StrBuilder new_name;
// Add prefix if provided
if (optional_prefix.Len)
new_name = string_fmt_buf(GlobalAllocator, "%SC_%SC_", optional_prefix, old_name);
new_name = strbuilder_fmt_buf(GlobalAllocator, "%S_%S_", optional_prefix, old_name);
else
new_name = string_fmt_buf(GlobalAllocator, "%SC_", old_name);
new_name = strbuilder_fmt_buf(GlobalAllocator, "%S_", old_name);
// Add return type to the signature
if (fn->ReturnType)
new_name.append_fmt("_%SC", fn->ReturnType->Name);
new_name.append_fmt("_%S", fn->ReturnType->Name);
// Add parameter types to create a unique signature
bool first_param = true;
@ -198,11 +198,11 @@ CodeFn rename_function_to_unique_symbol(CodeFn fn, StrC optional_prefix = txt(""
continue;
}
new_name.append_fmt("%SC_", spec_to_str(spec));
new_name.append_fmt("%S_", spec_to_str(spec));
}
}
new_name.append_fmt("%SC", param->ValueType->Name);
new_name.append_fmt("%S", param->ValueType->Name);
}
}
@ -214,7 +214,7 @@ CodeFn rename_function_to_unique_symbol(CodeFn fn, StrC optional_prefix = txt(""
spec != end(fn->Specs);
++spec)
{
new_name.append_fmt("%SC_", spec_to_str(*spec));
new_name.append_fmt("%S_", spec_to_str(*spec));
}
}
@ -223,13 +223,13 @@ CodeFn rename_function_to_unique_symbol(CodeFn fn, StrC optional_prefix = txt(""
}
using SwapContentProc = CodeBody(void);
bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body )
bool swap_pragma_region_implementation( Str region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body )
{
bool found = false;
CodePragma possible_region = cast(CodePragma, entry_iter);
String region_sig = string_fmt_buf(GlobalAllocator, "region %s", region_name.Ptr);
String endregion_sig = string_fmt_buf(GlobalAllocator, "endregion %s", region_name.Ptr);
StrBuilder region_sig = strbuilder_fmt_buf(GlobalAllocator, "region %s", region_name.Ptr);
StrBuilder endregion_sig = strbuilder_fmt_buf(GlobalAllocator, "endregion %s", region_name.Ptr);
if ( possible_region->Content.contains(region_sig))
{
found = true;