From a3548a5bd3b415c47ac17aef2b4526b802b59413 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 3 Dec 2024 20:21:08 -0500 Subject: [PATCH] Added support for friend operator definitions --- gen_c_library/c_library.cpp | 46 +++++++++---------- .../components/memory.fixed_arena.hpp | 2 +- gen_c_library/components/misc.hpp | 10 ++-- project/components/code_serialization.cpp | 2 +- project/components/parser.cpp | 21 ++++++--- project/dependencies/strings.hpp | 14 +++--- project/helpers/push_ignores.inline.hpp | 1 + 7 files changed, 53 insertions(+), 43 deletions(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 6a2edb3..99363a0 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -128,12 +128,12 @@ int gen_main() header.print( debug ); CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" ); - CodeBody memory = def_body(ECode::Global_Body); + CodeBody memory = def_body(CT_Global_Body); for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry ) { switch (entry->Type) { - case ECode::Using: + case CT_Using: { log_fmt("REPLACE THIS MANUALLY: %S\n", entry->Name); CodeUsing using_ver = cast(CodeUsing, entry); @@ -142,7 +142,7 @@ int gen_main() memory.append(typedef_ver); } break; - case ECode::Function_Fwd: + case CT_Function_Fwd: { CodeFn fn = cast(CodeFn, entry); if ( fn->Name.is_equal(txt("free")) ) @@ -152,13 +152,13 @@ int gen_main() memory.append(entry); } break; - case ECode::Function: + case CT_Function: { CodeFn fn = cast(CodeFn, entry); - s32 constexpr_found = fn->Specs.remove( ESpecifier::Constexpr ); + s32 constexpr_found = fn->Specs.remove( Spec_Constexpr ); if (constexpr_found > -1) { log_fmt("Found constexpr: %S\n", entry.to_string()); - fn->Specs.append(ESpecifier::Inline); + fn->Specs.append(Spec_Inline); } if ( fn->Name.is_equal(txt("free")) ) { @@ -167,7 +167,7 @@ int gen_main() memory.append(entry); } break; - case ECode::Template: + case CT_Template: { CodeTemplate tmpl = cast(CodeTemplate, entry); if ( tmpl->Declaration->Name.contains(txt("swap"))) @@ -186,14 +186,14 @@ int gen_main() } } break; - case ECode::Class: - case ECode::Struct: + case CT_Class: + case CT_Struct: { CodeBody body = cast(CodeBody, entry->Body); CodeBody new_body = def_body( entry->Body->Type ); for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch (body_entry->Type) { - case ECode::Preprocess_If: + case CT_Preprocess_If: { ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), body_entry, body ); } @@ -208,7 +208,7 @@ int gen_main() memory.append(entry); } break; - case ECode::Preprocess_If: + case CT_Preprocess_If: { b32 found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory ); if (found) break; @@ -216,7 +216,7 @@ int gen_main() memory.append(entry); } break; - case ECode::Preprocess_IfDef: + case CT_Preprocess_IfDef: { b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory ); if (found) break; @@ -224,7 +224,7 @@ int gen_main() memory.append(entry); } break; - case ECode::Preprocess_Pragma: + case CT_Preprocess_Pragma: { b32 found = swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory); if (found) break; @@ -244,12 +244,12 @@ int gen_main() header.print( string_ops ); CodeBody printing_parsed = parse_file( project_dir "dependencies/printing.hpp" ); - CodeBody printing = def_body(ECode::Global_Body); + CodeBody printing = def_body(CT_Global_Body); for ( Code entry = printing_parsed.begin(); entry != printing_parsed.end(); ++ entry ) { switch (entry->Type) { - case ECode::Preprocess_IfDef: + case CT_Preprocess_IfDef: { b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed ); if (found) break; @@ -257,7 +257,7 @@ int gen_main() printing.append(entry); } break; - case ECode::Variable: + case CT_Variable: { if (contains(entry->Name, txt("Msg_Invalid_Value"))) { @@ -275,7 +275,7 @@ int gen_main() } header.print(dump_to_scratch_and_retireve(printing)); - CodeBody containers = def_body(ECode::Global_Body); + CodeBody containers = def_body(CT_Global_Body); { containers.append( def_pragma(code(region Containers))); @@ -291,24 +291,24 @@ int gen_main() header.print( hashing ); CodeBody parsed_strings = parse_file( project_dir "dependencies/strings.hpp" ); - CodeBody strings = def_body(ECode::Global_Body); + CodeBody strings = def_body(CT_Global_Body); for ( Code entry = parsed_strings.begin(); entry != parsed_strings.end(); ++ entry ) { switch (entry->Type) { - case ECode::Preprocess_If: + case CT_Preprocess_If: { ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), entry, parsed_strings); } break; - case ECode::Preprocess_IfDef: + case CT_Preprocess_IfDef: { ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_strings ); } break; - case ECode::Struct_Fwd: + case CT_Struct_Fwd: { if ( entry->Name.is_equal(txt("String")) ) { @@ -322,13 +322,13 @@ int gen_main() } break; - case ECode::Struct: + case CT_Struct: { CodeBody body = cast(CodeBody, entry->Body); CodeBody new_body = def_body( entry->Body->Type ); for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch (body_entry->Type) { - case ECode::Preprocess_If: + case CT_Preprocess_If: { b32 found = ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), body_entry, body ); if (found) break; diff --git a/gen_c_library/components/memory.fixed_arena.hpp b/gen_c_library/components/memory.fixed_arena.hpp index e75a6eb..3a73a0e 100644 --- a/gen_c_library/components/memory.fixed_arena.hpp +++ b/gen_c_library/components/memory.fixed_arena.hpp @@ -5,7 +5,7 @@ using namespace gen; CodeBody gen_fixed_arenas() { - CodeBody result = def_body(ECode::Global_Body); + CodeBody result = def_body(CT_Global_Body); result.append(def_pragma(txt("region FixedArena"))); char const* template_struct = stringize( diff --git a/gen_c_library/components/misc.hpp b/gen_c_library/components/misc.hpp index b383041..933e7ab 100644 --- a/gen_c_library/components/misc.hpp +++ b/gen_c_library/components/misc.hpp @@ -17,13 +17,13 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod s32 depth = 1; ++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ) switch (entry_iter->Type) { - case ECode::Preprocess_If: - case ECode::Preprocess_IfDef: - case ECode::Preprocess_IfNotDef: + case CT_Preprocess_If: + case CT_Preprocess_IfDef: + case CT_Preprocess_IfNotDef: depth ++; break; - case ECode::Preprocess_EndIf: + case CT_Preprocess_EndIf: { depth --; if (depth == 0) { @@ -56,7 +56,7 @@ bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_ ++ entry_iter; for(b32 continue_for = true; continue_for; ++entry_iter) switch (entry_iter->Type) { - case ECode::Preprocess_Pragma: + case CT_Preprocess_Pragma: { CodePragma possible_end_region = cast(CodePragma, entry_iter); if ( possible_end_region->Content.contains(endregion_sig) ) { diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 7cb9799..47436ed 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -449,7 +449,7 @@ void to_string(CodeFriend self, String* result ) { append_fmt( result, "friend %S", to_string(self->Declaration) ); - if ( self->Declaration->Type != CT_Function && (* result)[ length(* result) - 1 ] != ';' ) + if ( self->Declaration->Type != CT_Function && self->Declaration->Type != CT_Operator && (* result)[ length(* result) - 1 ] != ';' ) { append( result, ";" ); } diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 348886d..4e55d9f 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -905,11 +905,11 @@ CodeBody parse_class_struct_body( TokType which, Token name ) break; case Tok_Operator: - if ( currtok.Text[0] != '~' ) - { - log_failure( "Operator token found in global body but not destructor unary negation\n%s", to_string(Context) ); - return InvalidCode; - } + //if ( currtok.Text[0] != '~' ) + //{ + // log_failure( "Operator token found in global body but not destructor unary negation\n%s", to_string(Context) ); + // return InvalidCode; + //} member = parse_destructor(); // ~() @@ -3859,7 +3859,8 @@ CodeFriend parse_friend() eat( Tok_Decl_Friend ); // friend - CodeFn function = { nullptr }; + CodeFn function = { nullptr }; + CodeOperator op = { nullptr }; // Type declaration or return type CodeTypename type = parse_type(); @@ -3892,6 +3893,12 @@ CodeFriend parse_friend() // if ( params ) // function->Params = params; } + + // Operator declaration or definition + if ( currtok.Type == Tok_Decl_Operator ) + { + op = parse_operator_after_ret_type( ModuleFlag_None, NullCode, type ); + } CodeComment inline_cmt = NullCode; if ( function && function->Type == CT_Function_Fwd ) @@ -3912,6 +3919,8 @@ CodeFriend parse_friend() if ( function ) result->Declaration = function; + else if ( op ) + result->Declaration = op; else result->Declaration = type; diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index 5d74e81..ddfc7a9 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -25,13 +25,13 @@ struct StrC operator char const* () const { return Ptr; } char const& operator[]( ssize index ) const { return Ptr[index]; } -#if GEN_SUPPORT_CPP_MEMBER_FUNCTIONS - bool is_equal (StrC rhs) { return are_equal(* this, rhs); } - char* back () { return back(* this); } - bool contains (StrC substring) { return contains(* this, substring); } - String duplicate (AllocatorInfo allocator) { return duplicate(* this, allocator); } - b32 starts_with (StrC substring) { return starts_with(* this, substring); } - String visualize_whitespace() { return visualize_whitespace(* this); } +#if GEN_SUPPORT_CPP_MEMBER_FEATURES + bool is_equal (StrC rhs) const { return GEN_NS are_equal(* this, rhs); } + char const* back () const { return GEN_NS back(* this); } + bool contains (StrC substring) const { return GEN_NS contains(* this, substring); } + StrC duplicate (AllocatorInfo allocator) const { return GEN_NS duplicate(* this, allocator); } + b32 starts_with (StrC substring) const { return GEN_NS starts_with(* this, substring); } + StrC visualize_whitespace(AllocatorInfo allocator) const { return GEN_NS visualize_whitespace(* this, allocator); } #endif // GEN_SUPPORT_CPP_MEMBER_FUNCTIONS #endif // GEN_COMPILERC }; diff --git a/project/helpers/push_ignores.inline.hpp b/project/helpers/push_ignores.inline.hpp index a6b2df8..e31dd1b 100644 --- a/project/helpers/push_ignores.inline.hpp +++ b/project/helpers/push_ignores.inline.hpp @@ -7,6 +7,7 @@ # pragma clang diagnostic ignored "-Wunknown-pragmas" # pragma clang diagnostic ignored "-Wvarargs" # pragma clang diagnostic ignored "-Wunused-function" +# pragma clang diagnostic ignored "-Wbraced-scalar-init" #endif #ifdef __GNUC__