mirror of
https://github.com/Ed94/gencpp.git
synced 2025-06-30 19:01:02 -07:00
First compiling version of operator overloading for C! (on both msvc and clang using -std=c11 flag, using _Generic selection with some helper macros)
Extremely satsified with how unofuscated the generated code is for _Generic. Still fixing up the templated container code though in the c-codegen
This commit is contained in:
@ -39,10 +39,10 @@ usize array_grow_formula(ssize value);
|
||||
template<class Type> Array<Type> array_init (AllocatorInfo allocator);
|
||||
template<class Type> Array<Type> array_init_reserve (AllocatorInfo allocator, ssize capacity);
|
||||
template<class Type> bool array_append_array (Array<Type>* array, Array<Type> other);
|
||||
template<class Type> bool array_append_value (Array<Type>* array, Type value);
|
||||
template<class Type> bool array_append (Array<Type>* array, Type value);
|
||||
template<class Type> bool array_append_items (Array<Type>* array, Type* items, usize item_num);
|
||||
template<class Type> bool array_append_at (Array<Type>* array, Type item, usize idx);
|
||||
template<class Type> bool array_append_at_items(Array<Type>* array, Type* items, usize item_num, usize idx);
|
||||
template<class Type> bool array_append_items_at(Array<Type>* array, Type* items, usize item_num, usize idx);
|
||||
template<class Type> Type* array_back (Array<Type> array);
|
||||
template<class Type> void array_clear (Array<Type> array);
|
||||
template<class Type> bool array_fill (Array<Type> array, usize begin, usize end, Type value);
|
||||
@ -150,8 +150,8 @@ usize array_grow_formula(ssize value) {
|
||||
}
|
||||
|
||||
template<class Type> inline
|
||||
bool array_append(Array<Type>* array, Array<Type> other) {
|
||||
return append(array, other, num(other));
|
||||
bool array_append_array(Array<Type>* array, Array<Type> other) {
|
||||
return array_append_items(array, (Type*)other, num(other));
|
||||
}
|
||||
|
||||
template<class Type> inline
|
||||
@ -173,7 +173,7 @@ bool array_append(Array<Type>* array, Type value)
|
||||
}
|
||||
|
||||
template<class Type> inline
|
||||
bool array_append(Array<Type>* array, Type* items, usize item_num)
|
||||
bool array_append_items(Array<Type>* array, Type* items, usize item_num)
|
||||
{
|
||||
ArrayHeader* header = array_get_header(array);
|
||||
|
||||
@ -221,7 +221,7 @@ bool array_append_at(Array<Type>* array, Type item, usize idx)
|
||||
}
|
||||
|
||||
template<class Type> inline
|
||||
bool array_append_at(Array<Type>* array, Type* items, usize item_num, usize idx)
|
||||
bool array_append_items_at(Array<Type>* array, Type* items, usize item_num, usize idx)
|
||||
{
|
||||
ArrayHeader* header = get_header(array);
|
||||
|
||||
@ -396,9 +396,9 @@ bool array_set_capacity(Array<Type>* array, usize new_capacity)
|
||||
|
||||
#define array_init(type, allocator) array_init <type> (allocator )
|
||||
#define array_init_reserve(type, allocator, cap) array_init_reserve <type> (allocator, cap)
|
||||
#define array_append_array(array, other) array_append < get_array_underlying_type(array) > (& array, other )
|
||||
#define array_append_value(array, value) array_append < get_array_underlying_type(array) > (& array, value )
|
||||
#define array_append_items(array, items, item_num) array_append < get_array_underlying_type(array) > (& array, items, item_num )
|
||||
#define array_append_array(array, other) array_append_array < get_array_underlying_type(array) > (& array, other )
|
||||
#define array_append(array, value) array_append < get_array_underlying_type(array) > (& array, value )
|
||||
#define array_append_items(array, items, item_num) array_append_items < get_array_underlying_type(array) > (& array, items, item_num )
|
||||
#define array_append_at(array, item, idx ) array_append_at < get_array_underlying_type(array) > (& array, item, idx )
|
||||
#define array_append_at_items(array, items, item_num, idx) array_append_at_items< get_array_underlying_type(array) > (& items, item_num, idx )
|
||||
#define array_back(array) array_back < get_array_underlying_type(array) > (array )
|
||||
@ -680,7 +680,7 @@ ssize hashtable_add_entry(HashTable<Type>* table, u64 key) {
|
||||
HashTableEntry<Type> entry = { key, -1 };
|
||||
|
||||
idx = array_num(table->Entries);
|
||||
array_append( & table->Entries, entry);
|
||||
array_append( table->Entries, entry);
|
||||
return idx;
|
||||
}
|
||||
|
||||
|
@ -89,6 +89,10 @@
|
||||
#endif
|
||||
|
||||
#ifndef num_args_impl
|
||||
|
||||
// This is essentially an arg couneter version of GEN_SELECT_ARG macros
|
||||
// See section : _Generic function overloading for that usage (explains this heavier case)
|
||||
|
||||
#define num_args_impl( _0, \
|
||||
_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, \
|
||||
_11, _12, _13, _14, _15, _16, _17, _18, _19, _20, \
|
||||
@ -265,4 +269,98 @@
|
||||
#define struct_init(type, value) {value}
|
||||
#endif
|
||||
|
||||
// ------------------------ _Generic function overloading -----------------------------------------
|
||||
#if GEN_COMPILER_C
|
||||
// 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.
|
||||
|
||||
#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
|
||||
#define GEN_SELECT_ARG_1( _1, ... ) _1 // <-- Of all th args passed pick _1.
|
||||
#define GEN_SELECT_ARG_2( _1, _2, ... ) _2 // <-- Of all the args passed pick _2.
|
||||
#define GEN_SELECT_ARG_3( _1, _2, _3, ... ) _3 // etc.. (by induction until _8, which we don't support any more beyond)
|
||||
// #define GEN_SELECT_ARG_4( _1, _2, _3, _4, ... ) _4
|
||||
// #define GEN_SELECT_ARG_5( _1, _2, _3, _4, _5, ... ) _5
|
||||
// #define GEN_SELECT_ARG_6( _1, _2, _3, _4, _5, _6, ... ) _6
|
||||
// #define GEN_SELECT_ARG_7( _1, _2, _3, _4, _5, _6, _7, ... ) _7
|
||||
// #define GEN_SELECT_ARG_8( _1, _2, _3, _4, _5, _6, _7, _8, ... ) _8
|
||||
|
||||
#define GEN_GENERIC_SEL_ENTRY_TYPE GEN_SELECT_ARG_1 // Use the arg expansion macro to select arg 1 which should have the type.
|
||||
#define GEN_GENERIC_SEL_ENTRY_FUNCTION GEN_SELECT_ARG_2 // Use the arg expansion macro to select arg 2 which should have the function.
|
||||
#define GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER GEN_SELECT_ARG_3 // Use the arg expansion macro to select arg 3 which should have the comma delimiter ','.
|
||||
|
||||
#define GEN_RESOLVED_FUNCTION_CALL // Just used to indicate where the call "occurs"
|
||||
|
||||
// ----------------------------------------------------------------------------------------------------------------------------------
|
||||
// GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( macro ) includes a _Generic slot only if the specified macro is defined (as type, function_name).
|
||||
// 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) <comma_operator: ',' >
|
||||
// Where GEN_GENERIC_SEL_ENTRY_COMMA_DELIMITER is specifically looking for that <comma> ,
|
||||
#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.
|
||||
// |
|
||||
// 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
|
||||
// ----------------------------------------------------------------------------------------------------------------------------------
|
||||
#define GEN_FUNCTION_GENERIC_EXAMPLE( function_arguments ) _Generic( \
|
||||
(function_arguments), /* 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 ) \
|
||||
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( 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 )
|
||||
// ----------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
// Then each definiton of a function has an associated define:
|
||||
// #define <function_id_macro> GEN_GENERIC_FUNCTION_ARG_SIGNATURE( <function_id>, <arguments> )
|
||||
#define GEN_GENERIC_FUNCTION_ARG_SIGNATURE( name_of_function, type_delimiter ) type_delimiter name_of_function
|
||||
|
||||
// Then somehwere later on
|
||||
// <etc> <return_type> <function_id> ( <arguments> ) { <implementation> }
|
||||
|
||||
// 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; }
|
||||
|
||||
// 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; }
|
||||
|
||||
// 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.
|
||||
#define hash( 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_LAST( HASH__ARGS_SIG_8 ) \
|
||||
) GEN_RESOLVED_FUNCTION_CALL( function_arguments )
|
||||
|
||||
#endif // ENABLE_GENERIC_EXAMPLE
|
||||
|
||||
// END OF ------------------------ _Generic function overloading ----------------------------------------- END OF
|
||||
#endif
|
||||
|
||||
#pragma endregion Macros
|
||||
|
@ -955,7 +955,7 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b
|
||||
adt_append_arr( root, NULL );
|
||||
}
|
||||
|
||||
array_append( & root->nodes[ columnIndex ].nodes, rowItem );
|
||||
array_append( root->nodes[ columnIndex ].nodes, rowItem );
|
||||
|
||||
if ( delimiter == delim )
|
||||
{
|
||||
|
Reference in New Issue
Block a user