2024-12-05 18:01:04 -08:00
|
|
|
#pragma once
|
2024-12-10 20:35:47 -08:00
|
|
|
#include "gen.hpp"
|
2024-12-04 08:01:53 -08:00
|
|
|
|
2024-12-05 18:01:04 -08:00
|
|
|
using namespace gen;
|
2024-12-04 08:01:53 -08:00
|
|
|
|
2024-12-07 14:17:02 -08:00
|
|
|
void convert_cpp_enum_to_c( CodeEnum to_convert, CodeBody to_append )
|
|
|
|
{
|
|
|
|
#pragma push_macro("enum_underlying")
|
|
|
|
#undef enum_underlying
|
2024-12-14 22:27:57 -08:00
|
|
|
StrCached type = to_convert->UnderlyingType ? to_convert->UnderlyingType.to_strbuilder().to_str() : to_convert->Name;
|
2024-12-14 11:02:16 -08:00
|
|
|
CodeTypedef tdef = parse_typedef(token_fmt("type", type, "name", to_convert->Name, stringize( typedef enum <type> <name>; )));
|
2024-12-07 14:17:02 -08:00
|
|
|
if (to_convert->UnderlyingType)
|
|
|
|
{
|
|
|
|
to_convert->UnderlyingTypeMacro = untyped_str(token_fmt("type", to_convert->UnderlyingType->Name, stringize(enum_underlying(<type>))));
|
|
|
|
to_convert->UnderlyingType = CodeTypename{nullptr};
|
|
|
|
}
|
|
|
|
to_append.append(to_convert);
|
|
|
|
to_append.append(tdef);
|
|
|
|
#pragma pop_macro("enum_underlying")
|
|
|
|
}
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
b32 ignore_preprocess_cond_block( Str cond_sig, Code& entry_iter, CodeBody& parsed_body, CodeBody& body )
|
2024-11-30 20:38:27 -08:00
|
|
|
{
|
2024-12-01 09:48:58 -08:00
|
|
|
b32 found = false;
|
2024-12-01 23:38:55 -08:00
|
|
|
CodePreprocessCond cond = cast(CodePreprocessCond, entry_iter);
|
2024-12-07 14:17:02 -08:00
|
|
|
if ( cond->Content.is_equal(cond_sig) )
|
2024-11-30 20:38:27 -08:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
//log_fmt("Preprocess cond found: %S\n", cond->Content);
|
2024-12-01 09:48:58 -08:00
|
|
|
found = true;
|
|
|
|
|
2024-11-30 20:38:27 -08:00
|
|
|
s32 depth = 1;
|
2024-12-04 08:01:53 -08:00
|
|
|
++ entry_iter;
|
2024-12-04 23:53:14 -08:00
|
|
|
for(b32 continue_for = true; continue_for && entry_iter != parsed_body.end(); ) switch
|
2024-11-30 20:38:27 -08:00
|
|
|
(entry_iter->Type) {
|
2024-12-03 17:21:08 -08:00
|
|
|
case CT_Preprocess_If:
|
|
|
|
case CT_Preprocess_IfDef:
|
|
|
|
case CT_Preprocess_IfNotDef:
|
2024-12-04 08:01:53 -08:00
|
|
|
++ depth;
|
|
|
|
++ entry_iter;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CT_Preprocess_Else:
|
|
|
|
++ entry_iter;
|
2024-12-04 23:53:14 -08:00
|
|
|
for(; continue_for && entry_iter != parsed_body.end(); ++ entry_iter)
|
2024-12-04 08:01:53 -08:00
|
|
|
{
|
|
|
|
if (entry_iter->Type == CT_Preprocess_EndIf)
|
|
|
|
{
|
|
|
|
continue_for = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
body.append(entry_iter);
|
|
|
|
}
|
2024-11-30 20:38:27 -08:00
|
|
|
break;
|
|
|
|
|
2024-12-03 17:21:08 -08:00
|
|
|
case CT_Preprocess_EndIf:
|
2024-11-30 20:38:27 -08:00
|
|
|
{
|
|
|
|
depth --;
|
|
|
|
if (depth == 0) {
|
|
|
|
continue_for = false;
|
2024-11-30 22:40:31 -08:00
|
|
|
break;
|
2024-11-30 20:38:27 -08:00
|
|
|
}
|
2024-12-04 08:01:53 -08:00
|
|
|
++ entry_iter;
|
2024-11-30 20:38:27 -08:00
|
|
|
}
|
|
|
|
break;
|
2024-11-30 22:40:31 -08:00
|
|
|
default:
|
|
|
|
++ entry_iter;
|
|
|
|
break;
|
2024-11-30 20:38:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-01 09:48:58 -08:00
|
|
|
return found;
|
2024-11-30 20:38:27 -08:00
|
|
|
}
|
|
|
|
|
2024-12-05 18:01:04 -08:00
|
|
|
constexpr bool GenericSel_One_Arg = true;
|
|
|
|
enum GenericSelectionOpts : u32 { GenericSel_Default, GenericSel_By_Ref, GenericSel_Direct_Type };
|
2024-12-12 09:55:15 -08:00
|
|
|
Code gen_generic_selection_function_macro( s32 num_slots, Str macro_name, GenericSelectionOpts opts = GenericSel_Default, bool one_arg = false )
|
2024-12-05 18:01:04 -08:00
|
|
|
{
|
|
|
|
/* 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
|
2024-12-13 19:09:43 -08:00
|
|
|
StrBuilder define_builder = StrBuilder::make_reserve(_ctx->Allocator_Temp, kilobytes(64));
|
2024-12-05 18:01:04 -08:00
|
|
|
define_builder.clear();
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
Str macro_begin;
|
2024-12-05 18:01:04 -08:00
|
|
|
if (opts == GenericSel_Direct_Type) {
|
2024-12-12 09:55:15 -08:00
|
|
|
macro_begin = token_fmt( "macro_name", (Str)macro_name,
|
2024-12-05 18:01:04 -08:00
|
|
|
R"(#define <macro_name>(selector_arg, ...) _Generic( (*(selector_arg*)NULL ), \
|
|
|
|
)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
2024-12-12 09:55:15 -08:00
|
|
|
macro_begin = token_fmt( "macro_name", (Str)macro_name,
|
2024-12-05 18:01:04 -08:00
|
|
|
R"(#define <macro_name>(selector_arg, ...) _Generic( (selector_arg), \
|
|
|
|
)"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
define_builder.append(macro_begin);
|
|
|
|
|
|
|
|
for ( s32 slot = 1; slot <= num_slots; ++ slot )
|
|
|
|
{
|
2024-12-13 19:09:43 -08:00
|
|
|
Str slot_str = StrBuilder::fmt_buf(_ctx->Allocator_Temp, "%d", slot).to_str();
|
2024-12-05 18:01:04 -08:00
|
|
|
define_builder.append( token_fmt( "macro_name", macro_name, "slot", slot_str,
|
2024-12-13 13:34:47 -08:00
|
|
|
R"(GEN_IF_MACRO_DEFINED_INCLUDE_THIS_SLOT( GENERIC_SLOT_<slot>__<macro_name> ) \
|
2024-12-05 18:01:04 -08:00
|
|
|
)"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2024-12-13 13:34:47 -08:00
|
|
|
define_builder.append( txt("default: gen_generic_selection_fail\\\n") );
|
2024-12-08 13:37:04 -08:00
|
|
|
|
2024-12-05 18:01:04 -08:00
|
|
|
if ( ! one_arg )
|
|
|
|
{
|
|
|
|
if (opts == GenericSel_By_Ref)
|
2024-12-13 13:34:47 -08:00
|
|
|
define_builder.append(txt(")\\\nGEN_RESOLVED_FUNCTION_CALL( & selector_arg, __VA_ARGS__ )"));
|
2024-12-05 18:01:04 -08:00
|
|
|
else if (opts == GenericSel_Direct_Type)
|
2024-12-13 13:34:47 -08:00
|
|
|
define_builder.append(txt(")\\\nGEN_RESOLVED_FUNCTION_CALL( __VA_ARGS__ )"));
|
2024-12-05 18:01:04 -08:00
|
|
|
else
|
2024-12-13 13:34:47 -08:00
|
|
|
define_builder.append(txt(")\\\nGEN_RESOLVED_FUNCTION_CALL( selector_arg, __VA_ARGS__ )"));
|
2024-12-05 18:01:04 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (opts == GenericSel_By_Ref)
|
2024-12-13 13:34:47 -08:00
|
|
|
define_builder.append(txt(")\\\nGEN_RESOLVED_FUNCTION_CALL( & selector_arg )"));
|
2024-12-05 18:01:04 -08:00
|
|
|
else if (opts == GenericSel_Direct_Type)
|
2024-12-13 13:34:47 -08:00
|
|
|
define_builder.append(txt(")\\\nGEN_RESOLVED_FUNCTION_CALL()"));
|
2024-12-05 18:01:04 -08:00
|
|
|
else
|
2024-12-13 13:34:47 -08:00
|
|
|
define_builder.append(txt(")\\\nGEN_RESOLVED_FUNCTION_CALL( selector_arg )"));
|
2024-12-05 18:01:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add gap for next definition
|
|
|
|
define_builder.append(txt("\n\n"));
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
Code macro = untyped_str(define_builder.to_str());
|
2024-12-05 18:01:04 -08:00
|
|
|
return macro;
|
|
|
|
}
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
CodeFn rename_function_to_unique_symbol(CodeFn fn, Str optional_prefix = txt(""))
|
2024-12-04 08:01:53 -08:00
|
|
|
{
|
|
|
|
// Get basic components for the name
|
2024-12-12 09:55:15 -08:00
|
|
|
Str old_name = fn->Name;
|
|
|
|
StrBuilder new_name;
|
2024-12-04 08:01:53 -08:00
|
|
|
|
|
|
|
// Add prefix if provided
|
|
|
|
if (optional_prefix.Len)
|
2024-12-13 19:09:43 -08:00
|
|
|
new_name = strbuilder_fmt_buf(_ctx->Allocator_Temp, "%S_%S_", optional_prefix, old_name);
|
2024-12-04 08:01:53 -08:00
|
|
|
else
|
2024-12-13 19:09:43 -08:00
|
|
|
new_name = strbuilder_fmt_buf(_ctx->Allocator_Temp, "%S_", old_name);
|
2024-12-04 08:01:53 -08:00
|
|
|
|
|
|
|
// Add return type to the signature
|
|
|
|
if (fn->ReturnType)
|
2024-12-12 09:55:15 -08:00
|
|
|
new_name.append_fmt("_%S", fn->ReturnType->Name);
|
2024-12-04 08:01:53 -08:00
|
|
|
|
|
|
|
// Add parameter types to create a unique signature
|
|
|
|
bool first_param = true;
|
2024-12-11 10:33:35 -08:00
|
|
|
for (CodeParams param = fn->Params; param.ast; param = param->Next)
|
2024-12-04 08:01:53 -08:00
|
|
|
{
|
|
|
|
if (param->ValueType)
|
|
|
|
{
|
|
|
|
// Add separator for readability
|
|
|
|
if (first_param)
|
|
|
|
{
|
|
|
|
new_name.append("_P_");
|
|
|
|
first_param = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
new_name.append("_");
|
|
|
|
|
|
|
|
// Add parameter type, handle any specifiers
|
|
|
|
if (param->ValueType->Specs && param->ValueType->Specs->NumEntries > 0)
|
|
|
|
{
|
|
|
|
// Add specifiers (const, volatile, etc)
|
|
|
|
for (Specifier spec : param->ValueType->Specs)
|
|
|
|
{
|
|
|
|
if (spec == Spec_Ptr) {
|
|
|
|
new_name.append("ptr_");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
new_name.append_fmt("%S_", spec_to_str(spec));
|
2024-12-04 08:01:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
new_name.append_fmt("%S", param->ValueType->Name);
|
2024-12-04 08:01:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle function specifiers if present
|
|
|
|
if (fn->Specs && fn->Specs->NumEntries > 0)
|
|
|
|
{
|
|
|
|
new_name.append("_S_");
|
2024-12-07 14:17:02 -08:00
|
|
|
for (Specifier* spec = begin(fn->Specs);
|
|
|
|
spec != end(fn->Specs);
|
2024-12-04 08:01:53 -08:00
|
|
|
++spec)
|
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
new_name.append_fmt("%S_", spec_to_str(*spec));
|
2024-12-04 08:01:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn->Name = new_name;
|
|
|
|
return fn;
|
|
|
|
}
|
|
|
|
|
2024-12-04 21:40:51 -08:00
|
|
|
using SwapContentProc = CodeBody(void);
|
2024-12-12 09:55:15 -08:00
|
|
|
bool swap_pragma_region_implementation( Str region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body )
|
2024-11-30 20:38:27 -08:00
|
|
|
{
|
2024-11-30 22:40:31 -08:00
|
|
|
bool found = false;
|
2024-12-01 23:38:55 -08:00
|
|
|
CodePragma possible_region = cast(CodePragma, entry_iter);
|
2024-11-30 20:38:27 -08:00
|
|
|
|
2024-12-13 19:09:43 -08:00
|
|
|
StrBuilder region_sig = strbuilder_fmt_buf(_ctx->Allocator_Temp, "region %s", region_name.Ptr);
|
|
|
|
StrBuilder endregion_sig = strbuilder_fmt_buf(_ctx->Allocator_Temp, "endregion %s", region_name.Ptr);
|
2024-11-30 20:38:27 -08:00
|
|
|
if ( possible_region->Content.contains(region_sig))
|
|
|
|
{
|
2024-11-30 22:40:31 -08:00
|
|
|
found = true;
|
|
|
|
// body.append(possible_region);
|
2024-11-30 20:38:27 -08:00
|
|
|
body.append(swap_content());
|
|
|
|
|
2024-12-04 08:01:53 -08:00
|
|
|
++ entry_iter;
|
|
|
|
for(b32 continue_for = true; continue_for; ++entry_iter) switch
|
2024-11-30 20:38:27 -08:00
|
|
|
(entry_iter->Type) {
|
2024-12-03 17:21:08 -08:00
|
|
|
case CT_Preprocess_Pragma:
|
2024-11-30 20:38:27 -08:00
|
|
|
{
|
2024-12-01 23:38:55 -08:00
|
|
|
CodePragma possible_end_region = cast(CodePragma, entry_iter);
|
2024-11-30 20:38:27 -08:00
|
|
|
if ( possible_end_region->Content.contains(endregion_sig) ) {
|
2024-11-30 22:40:31 -08:00
|
|
|
// body.append(possible_end_region);
|
2024-11-30 20:38:27 -08:00
|
|
|
continue_for = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2024-11-30 22:40:31 -08:00
|
|
|
body.append(entry_iter);
|
2024-11-30 20:38:27 -08:00
|
|
|
}
|
2024-11-30 22:40:31 -08:00
|
|
|
return found;
|
2024-11-30 20:38:27 -08:00
|
|
|
}
|