diff --git a/gen_c_library/components/containers.array.hpp b/gen_c_library/components/containers.array.hpp index 6cb5040..5d25bae 100644 --- a/gen_c_library/components/containers.array.hpp +++ b/gen_c_library/components/containers.array.hpp @@ -34,15 +34,19 @@ CodeBody gen_array( StrC type, StrC array_name ) #pragma push_macro( "GEN_ASSERT" ) #pragma push_macro( "rcast" ) #pragma push_macro( "cast" ) +#pragma push_macro( "typeof" ) +#pragma push_macro( "forceinline" ) #undef GEN_ASSERT #undef rcast #undef cast +#undef typeof +#undef forceinline CodeBody result = parse_global_body( token_fmt( "array_type", (StrC)array_type, "fn", (StrC)fn, "type", (StrC)type , stringize( typedef * ; - void _init ( * self, AllocatorInfo allocator ); - void _init_reserve ( * self, AllocatorInfo allocator, usize capacity ); + _init ( AllocatorInfo allocator ); + _init_reserve ( AllocatorInfo allocator, usize capacity ); bool _append_array ( * self, other ); bool _append ( * self, value ); bool _append_items ( * self, * items, usize item_num ); @@ -60,33 +64,40 @@ CodeBody gen_array( StrC type, StrC array_name ) bool _resize ( * self, usize num ); bool _set_capacity ( * self, usize new_capacity ); - void _init( * self, AllocatorInfo allocator ) + forceinline + _init( AllocatorInfo allocator ) { size_t initial_size = array_grow_formula(0); - array_init_reserve( * self, allocator, initial_size ); + return array_init_reserve( , allocator, initial_size ); } - void _init_reserve( * self, AllocatorInfo allocator, usize capacity ) + inline + _init_reserve( AllocatorInfo allocator, usize capacity ) { + GEN_ASSERT(capacity > 0); ArrayHeader* header = rcast(ArrayHeader*, alloc(allocator, sizeof(ArrayHeader) + sizeof() * capacity)); if (header == nullptr) - self = nullptr; + return nullptr; header->Allocator = allocator; header->Capacity = capacity; header->Num = 0; - self = rcast(*, header + 1); + return rcast(*, header + 1); } + forceinline bool _append_array( * self, other ) { return array_append_items( * self, ()other, _num(other)); } + inline bool _append( * self, value ) { + GEN_ASSERT( self != nullptr); + GEN_ASSERT(* self != nullptr); ArrayHeader* header = array_get_header( * self ); if ( header->Num == header->Capacity ) @@ -103,8 +114,13 @@ CodeBody gen_array( StrC type, StrC array_name ) return true; } + inline bool _append_items( * self, * items, usize item_num ) { + GEN_ASSERT( self != nullptr); + GEN_ASSERT(* self != nullptr); + GEN_ASSERT(items != nullptr); + GEN_ASSERT(item_num > 0); ArrayHeader* header = array_get_header( * self ); if ( header->Num + item_num > header->Capacity ) @@ -121,8 +137,11 @@ CodeBody gen_array( StrC type, StrC array_name ) return true; } + inline bool _append_at( * self, item, usize idx ) { + GEN_ASSERT( self != nullptr); + GEN_ASSERT(* self != nullptr); ArrayHeader* header = array_get_header( * self ); if ( idx >= header->Num ) @@ -147,8 +166,11 @@ CodeBody gen_array( StrC type, StrC array_name ) return true; } + inline bool _append_items_at( * self, * items, usize item_num, usize idx ) { + GEN_ASSERT( self != nullptr); + GEN_ASSERT(* self != nullptr); ArrayHeader* header = array_get_header( * self ); if ( idx >= header->Num ) @@ -174,8 +196,10 @@ CodeBody gen_array( StrC type, StrC array_name ) return true; } + inline * _back( self ) { + GEN_ASSERT(self != nullptr); ArrayHeader* header = array_get_header( self ); if ( header->Num == 0 ) @@ -184,14 +208,19 @@ CodeBody gen_array( StrC type, StrC array_name ) return self + header->Num - 1; } + inline void _clear( self ) { + GEN_ASSERT(self != nullptr); ArrayHeader* header = array_get_header( self ); header->Num = 0; } + inline bool _fill( self, usize begin, usize end, value ) { + GEN_ASSERT(self != nullptr); + GEN_ASSERT(begin <= end); ArrayHeader* header = array_get_header( self ); if ( begin < 0 || end >= header->Num ) @@ -203,17 +232,24 @@ CodeBody gen_array( StrC type, StrC array_name ) return true; } + inline void _free( * self ) { + GEN_ASSERT( self != nullptr); + GEN_ASSERT(* self != nullptr); ArrayHeader* header = array_get_header( * self ); allocator_free( header->Allocator, header ); self = NULL; } + inline bool _grow( * self, usize min_capacity ) { - ArrayHeader* header = array_get_header( *self ); - usize new_capacity = array_grow_formula( header->Capacity ); + GEN_ASSERT( self != nullptr); + GEN_ASSERT(* self != nullptr); + GEN_ASSERT( min_capacity > 0 ); + ArrayHeader* header = array_get_header( *self ); + usize new_capacity = array_grow_formula( header->Capacity ); if ( new_capacity < min_capacity ) new_capacity = min_capacity; @@ -221,13 +257,17 @@ CodeBody gen_array( StrC type, StrC array_name ) return array_set_capacity( self, new_capacity ); } + forceinline usize _num( self ) { + GEN_ASSERT( self != nullptr); return array_get_header(self)->Num; } + inline _pop( self ) { + GEN_ASSERT( self != nullptr); ArrayHeader* header = array_get_header( self ); GEN_ASSERT( header->Num > 0 ); @@ -236,8 +276,10 @@ CodeBody gen_array( StrC type, StrC array_name ) return result; } + forceinline void _remove_at( self, usize idx ) { + GEN_ASSERT( self != nullptr); ArrayHeader* header = array_get_header( self ); GEN_ASSERT( idx < header->Num ); @@ -245,8 +287,12 @@ CodeBody gen_array( StrC type, StrC array_name ) header->Num--; } + inline bool _reserve( * self, usize new_capacity ) { + GEN_ASSERT( self != nullptr); + GEN_ASSERT(* self != nullptr); + GEN_ASSERT(new_capacity > 0); ArrayHeader* header = array_get_header( * self ); if ( header->Capacity < new_capacity ) @@ -255,8 +301,12 @@ CodeBody gen_array( StrC type, StrC array_name ) return true; } + inline bool _resize( * self, usize num ) { + GEN_ASSERT( self != nullptr); + GEN_ASSERT(* self != nullptr); + GEN_ASSERT(num > 0); ArrayHeader* header = array_get_header( * self ); if ( header->Capacity < num ) @@ -271,26 +321,34 @@ CodeBody gen_array( StrC type, StrC array_name ) return true; } + inline bool _set_capacity( * self, usize new_capacity ) { + GEN_ASSERT( self != nullptr); + GEN_ASSERT(* self != nullptr); + GEN_ASSERT( new_capacity > 0 ); ArrayHeader* header = array_get_header( * self ); if ( new_capacity == header->Capacity ) return true; if ( new_capacity < header->Num ) + { header->Num = new_capacity; + return true; + } - usize size = sizeof( ArrayHeader ) + sizeof( ) * new_capacity; + usize size = sizeof( ArrayHeader ) + sizeof( ) * new_capacity; ArrayHeader* new_header = cast( ArrayHeader*, alloc( header->Allocator, size )); if ( new_header == NULL ) return false; mem_move( new_header, header, sizeof( ArrayHeader ) + sizeof( ) * header->Num ); + new_header->Capacity = new_capacity; + allocator_free( header->Allocator, & header ); - new_header->Capacity = new_capacity; * self = cast( *, new_header + 1 ); return true; } @@ -298,6 +356,8 @@ CodeBody gen_array( StrC type, StrC array_name ) #pragma pop_macro( "GEN_ASSERT" ) #pragma pop_macro( "rcast" ) #pragma pop_macro( "cast" ) +#pragma pop_macro( "typeof" ) +#pragma pop_macro( "forceinline" ) ++ ArrayDefinitionCounter; StrC slot_str = String::fmt_buf(GlobalAllocator, "%d", ArrayDefinitionCounter).to_strc(); @@ -335,67 +395,22 @@ R"(#define GENERIC_SLOT___array_init , (selector_arg, ...) _Generic( \ - (selector_arg), /* Select Via Expression*/ \ - /* Extendibility slots: */ \ -)" - ); - define_builder.append(macro_begin); - - for ( s32 slot = 1; slot <= ArrayDefinitionCounter; ++ slot ) - { - StrC slot_str = String::fmt_buf(GlobalAllocator, "%d", slot).to_strc(); - if (slot == ArrayDefinitionCounter) - { - define_builder.append( token_fmt( "macro_name", macro_name, "slot", slot_str, -R"( GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST( GENERIC_SLOT___ ) \ -)" - )); - continue; - } - - define_builder.append( token_fmt( "macro_name", macro_name, "slot", slot_str, -R"( GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT___ ) \ -)" - )); - } - - if (by_ref) - define_builder.append(txt(")\tGEN_RESOLVED_FUNCTION_CALL( & selector_arg, __VA_ARGS__ )")); - else - define_builder.append(txt(")\tGEN_RESOLVED_FUNCTION_CALL( selector_arg, __VA_ARGS__ )")); - - // Add gap for next definition - define_builder.append(txt("\n\n")); - - Code macro = untyped_str(define_builder.to_strc()); - return macro; -} - CodeBody gen_array_generic_selection_interface() { CodeBody interface_defines = def_body(CT_Global_Body); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_init"), array_by_ref) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_init_reserve"), array_by_ref) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_append"), array_by_ref) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_append_items"), array_by_ref) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_back")) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_clear")) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_fill")) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_free"), array_by_ref) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_grow")) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_num")) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("arary_pop")) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_remove_at")) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("arary_reserve"), array_by_ref) ); - interface_defines.append( gen_array_generic_selection_function_macro(txt("array_set_capacity")) ); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_init"), GenericSel_Direct_Type )); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_init_reserve"), GenericSel_Direct_Type )); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_append"), GenericSel_By_Ref )); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_append_items"), GenericSel_By_Ref )); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_back"), GenericSel_Default, GenericSel_One_Arg )); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_clear"), GenericSel_Default, GenericSel_One_Arg )); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_fill")) ); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_free"), GenericSel_By_Ref, GenericSel_One_Arg ) ); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_grow")) ); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_num")) ); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_pop"), GenericSel_Default, GenericSel_One_Arg )); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_remove_at")) ); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_reserve"), GenericSel_By_Ref) ); + interface_defines.append( gen_generic_selection_function_macro( ArrayDefinitionCounter, txt("array_set_capacity")) ); return interface_defines; } diff --git a/gen_c_library/components/containers.hashtable.hpp b/gen_c_library/components/containers.hashtable.hpp index 1704e81..6504144 100644 --- a/gen_c_library/components/containers.hashtable.hpp +++ b/gen_c_library/components/containers.hashtable.hpp @@ -93,16 +93,16 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) void _set ( * self, u64 key, value ); ssize _slot ( self, u64 key ); - ssize __add_entry( self, u64 key ); + ssize __add_entry( self, u64 key ); HT_FindResult __find ( self, u64 key ); b32 __full ( self ); _make( AllocatorInfo allocator ) { - result = { NULL, NULL }; - array_init(result.Hashes, allocator ); - array_init(result.Entries, allocator ); + result = { NULL, NULL }; + result.Hashes = array_init(Array_ssize, allocator ); + result.Entries = array_init(, allocator ); return result; } @@ -111,8 +111,8 @@ CodeBody gen_hashtable( StrC type, StrC hashtable_name ) { result = { NULL, NULL }; - array_init_reserve(result.Hashes, allocator, num ); - array_init_reserve(result.Entries, allocator, num ); + result.Hashes = array_init_reserve(Array_ssize, allocator, num ); + result.Entries = array_init_reserve(, allocator, num ); return result; } diff --git a/gen_c_library/components/misc.hpp b/gen_c_library/components/misc.hpp index ea6691d..61e80c5 100644 --- a/gen_c_library/components/misc.hpp +++ b/gen_c_library/components/misc.hpp @@ -1,9 +1,7 @@ -// #pragma once -// #include "../project/gen.hpp" - -// using namespace gen; - +#pragma once +#include "../project/gen.hpp" +using namespace gen; b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& parsed_body, CodeBody& body ) { @@ -57,6 +55,82 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& par return found; } +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 ) +{ +/* Implements: + #define GEN_FUNCTION_GENERIC_EXAMPLE( 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_LAST(FunctionID__ARGS_SIG_N ) \ + ) GEN_RESOLVED_FUNCTION_CALL( selector_arg ) +*/ + local_persist + String define_builder = String::make_reserve(GlobalAllocator, kilobytes(64)); + define_builder.clear(); + + StrC macro_begin; + if (opts == GenericSel_Direct_Type) { + macro_begin = token_fmt( "macro_name", (StrC)macro_name, +R"(#define (selector_arg, ...) _Generic( (*(selector_arg*)NULL ), \ +)" + ); + } + else { + macro_begin = token_fmt( "macro_name", (StrC)macro_name, +R"(#define (selector_arg, ...) _Generic( (selector_arg), \ +)" + ); + } + define_builder.append(macro_begin); + + for ( s32 slot = 1; slot <= num_slots; ++ slot ) + { + StrC slot_str = String::fmt_buf(GlobalAllocator, "%d", slot).to_strc(); + if (slot == num_slots) + { + define_builder.append( token_fmt( "macro_name", macro_name, "slot", slot_str, +R"( GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST( GENERIC_SLOT___ ) \ +)" + )); + continue; + } + + define_builder.append( token_fmt( "macro_name", macro_name, "slot", slot_str, +R"( GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT___ ) \ +)" + )); + } + + if ( ! one_arg ) + { + if (opts == GenericSel_By_Ref) + define_builder.append(txt("\t)\tGEN_RESOLVED_FUNCTION_CALL( & selector_arg, __VA_ARGS__ )")); + else if (opts == GenericSel_Direct_Type) + define_builder.append(txt("\t)\tGEN_RESOLVED_FUNCTION_CALL( __VA_ARGS__ )")); + else + define_builder.append(txt("\t)\tGEN_RESOLVED_FUNCTION_CALL( selector_arg, __VA_ARGS__ )")); + } + else + { + if (opts == GenericSel_By_Ref) + define_builder.append(txt("\t)\tGEN_RESOLVED_FUNCTION_CALL( & selector_arg )")); + else if (opts == GenericSel_Direct_Type) + define_builder.append(txt("\t)\tGEN_RESOLVED_FUNCTION_CALL()")); + else + define_builder.append(txt("\t)\tGEN_RESOLVED_FUNCTION_CALL( selector_arg )")); + } + + // Add gap for next definition + define_builder.append(txt("\n\n")); + + Code macro = untyped_str(define_builder.to_strc()); + return macro; +} + CodeFn rename_function_to_unique_symbol(CodeFn fn, StrC optional_prefix = txt("")) { // Get basic components for the name diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index 506e938..b97f4ef 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -133,6 +133,7 @@ Array array_init(AllocatorInfo allocator) { template inline Array array_init_reserve(AllocatorInfo allocator, ssize capacity) { + GEN_ASSERT(capacity > 0); ArrayHeader* header = rcast(ArrayHeader*, alloc(allocator, sizeof(ArrayHeader) + sizeof(Type) * capacity)); if (header == nullptr) @@ -157,6 +158,8 @@ bool array_append_array(Array* array, Array other) { template inline bool array_append(Array* array, Type value) { + GEN_ASSERT( array != nullptr); + GEN_ASSERT(* array != nullptr); ArrayHeader* header = array_get_header(* array); if (header->Num == header->Capacity) @@ -175,6 +178,10 @@ bool array_append(Array* array, Type value) template inline bool array_append_items(Array* array, Type* items, usize item_num) { + GEN_ASSERT( array != nullptr); + GEN_ASSERT(* array != nullptr); + GEN_ASSERT(items != nullptr); + GEN_ASSERT(item_num > 0); ArrayHeader* header = array_get_header(array); if (header->Num + item_num > header->Capacity) @@ -193,6 +200,8 @@ bool array_append_items(Array* array, Type* items, usize item_num) template inline bool array_append_at(Array* array, Type item, usize idx) { + GEN_ASSERT( array != nullptr); + GEN_ASSERT(* array != nullptr); ArrayHeader* header = array_get_header(* array); ssize slot = idx; @@ -215,19 +224,19 @@ bool array_append_at(Array* array, Type item, usize idx) mem_move(target + 1, target, (header->Num - slot) * sizeof(Type)); header->Num++; - header = array_get_header(* array); - return true; } template inline bool array_append_items_at(Array* array, Type* items, usize item_num, usize idx) { + GEN_ASSERT( array != nullptr); + GEN_ASSERT(* array != nullptr); ArrayHeader* header = get_header(array); if (idx >= header->Num) { - return append(array, items, item_num); + return array_append_items(array, items, item_num); } if (item_num > header->Capacity) @@ -262,6 +271,7 @@ Type* array_back(Array array) template inline void array_clear(Array array) { + GEN_ASSERT(array != nullptr); ArrayHeader* header = array_get_header(array); header->Num = 0; } @@ -269,6 +279,8 @@ void array_clear(Array array) { template inline bool array_fill(Array array, usize begin, usize end, Type value) { + GEN_ASSERT(array != nullptr); + GEN_ASSERT(begin <= end); ArrayHeader* header = array_get_header(array); if (begin < 0 || end > header->Num) @@ -282,9 +294,10 @@ bool array_fill(Array array, usize begin, usize end, Type value) return true; } -template inline +template forceinline void array_free(Array* array) { - GEN_ASSERT(array != nullptr); + GEN_ASSERT( array != nullptr); + GEN_ASSERT(* array != nullptr); ArrayHeader* header = array_get_header(* array); allocator_free(header->Allocator, header); Type** Data = (Type**)array; @@ -293,14 +306,18 @@ void array_free(Array* array) { template forceinline ArrayHeader* array_get_header(Array array) { + GEN_ASSERT(array != nullptr); Type* Data = array; using NonConstType = TRemoveConst; return rcast(ArrayHeader*, const_cast(Data)) - 1; } -template inline +template forceinline bool array_grow(Array* array, usize min_capacity) { + GEN_ASSERT( array != nullptr); + GEN_ASSERT(* array != nullptr); + GEN_ASSERT( min_capacity > 0 ); ArrayHeader* header = array_get_header(* array); usize new_capacity = array_grow_formula(header->Capacity); @@ -310,13 +327,15 @@ bool array_grow(Array* array, usize min_capacity) return array_set_capacity(array, new_capacity); } -template inline +template forceinline usize array_num(Array array) { + GEN_ASSERT(array != nullptr); return array_get_header(array)->Num; } -template inline +template forceinline void array_pop(Array array) { + GEN_ASSERT(array != nullptr); ArrayHeader* header = array_get_header(array); GEN_ASSERT(header->Num > 0); header->Num--; @@ -325,6 +344,7 @@ void array_pop(Array array) { template inline void array_remove_at(Array array, usize idx) { + GEN_ASSERT(array != nullptr); ArrayHeader* header = array_get_header(array); GEN_ASSERT(idx < header->Num); @@ -335,6 +355,9 @@ void array_remove_at(Array array, usize idx) template inline bool array_reserve(Array* array, usize new_capacity) { + GEN_ASSERT( array != nullptr); + GEN_ASSERT(* array != nullptr); + GEN_ASSERT(num > 0) ArrayHeader* header = array_get_header(array); if (header->Capacity < new_capacity) @@ -346,6 +369,8 @@ bool array_reserve(Array* array, usize new_capacity) template inline bool array_resize(Array* array, usize num) { + GEN_ASSERT( array != nullptr); + GEN_ASSERT(* array != nullptr); ArrayHeader* header = array_get_header(* array); if (header->Capacity < num) { @@ -361,10 +386,12 @@ bool array_resize(Array* array, usize num) template inline bool array_set_capacity(Array* array, usize new_capacity) { + GEN_ASSERT( array != nullptr); + GEN_ASSERT(* array != nullptr); ArrayHeader* header = array_get_header(* array); if (new_capacity == header->Capacity) - return true; + return true; if (new_capacity < header->Num) { diff --git a/project/dependencies/macros.hpp b/project/dependencies/macros.hpp index 7545062..5a30018 100644 --- a/project/dependencies/macros.hpp +++ b/project/dependencies/macros.hpp @@ -269,13 +269,16 @@ #define struct_init(type, value) {value} #endif -// ------------------------ _Generic function overloading ----------------------------------------- #if GEN_COMPILER_C +// ------------------------ _Generic function overloading ----------------------------------------- // 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 unopaque as possible. // Extensive effort was put in below to make this as easy as possible to understand what is going on with this mess of a preoprocessor. +// Where the signature would be defined using: +#define GEN_TYPE_TO_EXP(type) (type*)NULL + #define GEN_COMMA_OPERATOR , // The comma operator is used by preprocessor macros to delimit arguments, so we have to represent it via a macro to prevent parsing incorrectly. // Helper macros for argument selection @@ -299,19 +302,19 @@ // It takes advantage of the fact that if the macro is defined, then the expanded text will contain a comma. // Expands to ',' if it can find (type): (function) // Where GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER is specifically looking for that , -#define GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( slot_exp ) GEN_SELECT_ARG_3( slot_exp, GEN_SELECT_ARG_1( slot_exp, ): GEN_SELECT_ARG_2( slot_exp, ) GEN_COMMA_OPERATOR, , ) -// ^Selects the comma ^ is the type ^ is the function ^ comma delimiter to select ^ Insert a comma -// The slot won't exist if that comma is not found. +#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. | // | // This is the same as above but it does not insert a comma V no comma here. #define GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST( slot_exp ) GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER( slot_exp, GEN_GENERIC_SEL_ENTRY_TYPE( slot_exp, ): GEN_GENERIC_SEL_ENTRY_FUNCTION( slot_exp, ), , ) // Needed for the last slot as they don't allow trailing commas. // ---------------------------------------------------------------------------------------------------------------------------------- -// Below are generated on demand for a generated function +// Below are generated on demand for an overlaod depdendent on a type: // ---------------------------------------------------------------------------------------------------------------------------------- -#define GEN_FUNCTION_GENERIC_EXAMPLE( function_arguments ) _Generic( \ -(function_arguments), /* Select Via Expression*/ \ +#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 ) \ @@ -320,8 +323,8 @@ GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \ GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \ GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( FunctionID__ARGS_SIG_1 ) \ - GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST FunctionID__ARGS_SIG_1 ) \ -) GEN_RESOLVED_FUNCTION_CALL( function_arguments ) + GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST(FunctionID__ARGS_SIG_1 ) \ +) GEN_RESOLVED_FUNCTION_CALL( selector_arg ) // ---------------------------------------------------------------------------------------------------------------------------------- // Then each definiton of a function has an associated define: @@ -332,16 +335,14 @@ // ( ) { } // Concrete example: -#define ENABLE_GENERIC_EXAMPLE 0 -#if ENABLE_GENERIC_EXAMPLE // To add support for long: -#define HASH__ARGS_SIG_1 GEN_GENERIC_FUNCTION_ARG_SIGNATURE( hash__P_long, long long ) -size_t hash__P_long ( long val ) { return val * 2654435761ull; } +#define GEN_EXAMPLE_HASH__ARGS_SIG_1 GEN_GENERIC_FUNCTION_ARG_SIGNATURE( hash__P_long, long long ) +size_t gen_example_hash__P_long( long val ) { return val * 2654435761ull; } // To add support for long long: -#define HASH__ARGS_SIG_2 GEN_GENERIC_FUNCTION_ARG_SIGNATURE( hash__P_long_long, long long ) -size_t hash__P_long_long( long long val ) { return val * 2654435761ull; } +#define GEN_EXAMPLE_HASH__ARGS_SIG_2 GEN_GENERIC_FUNCTION_ARG_SIGNATURE( hash__P_long_long, 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, // or, "defined" for usage during the compilation pass that handles the _Generic instrinsic. @@ -358,8 +359,28 @@ size_t hash__P_long_long( long long val ) { return val * 2654435761ull; } GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT_LAST( HASH__ARGS_SIG_8 ) \ ) GEN_RESOLVED_FUNCTION_CALL( function_arguments ) -#endif // ENABLE_GENERIC_EXAMPLE +// Additional Variations: +// 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_LAST(FunctionID__ARGS_SIG_N ) \ +) 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_LAST(FunctionID__ARGS_SIG_N ) \ +) GEN_RESOLVED_FUNCTION_CALL() + +// typedef void* GEN_GenericExampleType; +// GEN_FUNCTION_GENERIC_EXAMPLE_DIRECT_TYPE( GEN_GenericExampleType ); // END OF ------------------------ _Generic function overloading ----------------------------------------- END OF #endif diff --git a/project/dependencies/platform.hpp b/project/dependencies/platform.hpp index 76f0a77..a3899bf 100644 --- a/project/dependencies/platform.hpp +++ b/project/dependencies/platform.hpp @@ -123,7 +123,7 @@ #endif #if GEN_COMPILER_C -#pragma message("Detected C") +#pragma message("GENCPP: Detected C") #endif #pragma endregion Platform Detection diff --git a/project/helpers/push_ignores.inline.hpp b/project/helpers/push_ignores.inline.hpp index e31dd1b..5b8a932 100644 --- a/project/helpers/push_ignores.inline.hpp +++ b/project/helpers/push_ignores.inline.hpp @@ -8,6 +8,7 @@ # pragma clang diagnostic ignored "-Wvarargs" # pragma clang diagnostic ignored "-Wunused-function" # pragma clang diagnostic ignored "-Wbraced-scalar-init" +# pragma clang diagnostic ignored "-W#pragma-messages" #endif #ifdef __GNUC__ diff --git a/scripts/build.ci.ps1 b/scripts/build.ci.ps1 index 8787dbd..0a1935d 100644 --- a/scripts/build.ci.ps1 +++ b/scripts/build.ci.ps1 @@ -137,7 +137,7 @@ if ( $bootstrap ) $unit = join-path $path_project "bootstrap.cpp" $executable = join-path $path_build "bootstrap.exe" - build-simple $path_build $includes $compiler_args $linker_args $unit $executable + $result = build-simple $path_build $includes $compiler_args $linker_args $unit $executable Push-Location $path_project if ( Test-Path( $executable ) ) { @@ -175,7 +175,7 @@ if ( $singleheader ) $flag_link_win_subsystem_console ) - build-simple $path_build $includes $compiler_args $linker_args $unit $executable + $result = build-simple $path_build $includes $compiler_args $linker_args $unit $executable Push-Location $path_singleheader if ( Test-Path( $executable ) ) { @@ -213,7 +213,7 @@ if ( $c_library ) $flag_link_win_subsystem_console ) - build-simple $path_build $includes $compiler_args $linker_args $unit $executable + $result = build-simple $path_build $includes $compiler_args $linker_args $unit $executable Push-Location $path_c_library if ( Test-Path( $executable ) ) { @@ -240,7 +240,7 @@ if ( $c_library ) $compiler_args += "/std:c11" } - build-simple $path_build $includes $compiler_args $linker_args $unit $executable + $result = build-simple $path_build $includes $compiler_args $linker_args $unit $executable Push-Location $path_c_library if ( Test-Path( $executable ) ) { @@ -278,7 +278,7 @@ if ( $unreal ) $flag_link_win_subsystem_console ) - build-simple $path_build $includes $compiler_args $linker_args $unit $executable + $result = build-simple $path_build $includes $compiler_args $linker_args $unit $executable Push-Location $path_unreal if ( Test-Path( $executable ) ) { @@ -340,7 +340,7 @@ if ( $test ) $flag_link_win_subsystem_console ) - build-simple $path_build $includes $compiler_args $linker_args $unit $executable + $result = build-simple $path_build $includes $compiler_args $linker_args $unit $executable Push-Location $path_test Write-Host $path_test