From ce2be411d7398a7186c5ec76de2a232a413db0c2 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Wed, 29 Jan 2025 01:29:55 -0500 Subject: [PATCH] Add support for non Specifier enum SuffixSpecs in AST_Fn and parsing (Thanks Unreal) --- base/components/ast.hpp | 2 +- base/components/ast_types.hpp | 2 +- base/components/code_serialization.cpp | 11 ++++++++++- base/components/lexer.cpp | 3 +++ base/components/parser.cpp | 15 +++++++++++++++ base/components/parser_types.hpp | 2 ++ base/dependencies/macros.hpp | 2 ++ 7 files changed, 34 insertions(+), 3 deletions(-) diff --git a/base/components/ast.hpp b/base/components/ast.hpp index 359ad9c..ab107e9 100644 --- a/base/components/ast.hpp +++ b/base/components/ast.hpp @@ -400,7 +400,7 @@ struct AST }; union { Code NextVar; // Variable; Possible way to handle comma separated variables declarations. ( , NextVar->Specs NextVar->Name NextVar->ArrExpr = NextVar->Value ) - Code SuffixSpecs; // Only used with typenames, to store the function suffix if typename is function signature. ( May not be needed ) + Code SuffixSpecs; // Typename, Function (Thanks Unreal) Code PostNameMacro; // Only used with parameters for specifically UE_REQUIRES (Thanks Unreal) }; }; diff --git a/base/components/ast_types.hpp b/base/components/ast_types.hpp index f4511b5..0c33006 100644 --- a/base/components/ast_types.hpp +++ b/base/components/ast_types.hpp @@ -575,7 +575,7 @@ struct AST_Fn CodeTypename ReturnType; CodeParams Params; CodeBody Body; - char _PAD_PROPERTIES_ [ sizeof(AST*) ]; + Code SuffixSpecs; // Thanks Unreal }; }; StrCached Name; diff --git a/base/components/code_serialization.cpp b/base/components/code_serialization.cpp index ea78d1c..67c086c 100644 --- a/base/components/code_serialization.cpp +++ b/base/components/code_serialization.cpp @@ -589,6 +589,10 @@ void fn_to_strbuilder_def(CodeFn self, StrBuilder* result ) } } + // This is bodged in for now SOLEY for Unreal's PURE_VIRTUAL functional macro + if ( self->SuffixSpecs ) + strbuilder_append_fmt( result, " %SB", code_to_strbuilder(self->SuffixSpecs) ); + strbuilder_append_fmt( result, "\n{\n%SB\n}\n", body_to_strbuilder(self->Body) ); } @@ -646,7 +650,12 @@ void fn_to_strbuilder_fwd(CodeFn self, StrBuilder* result ) if ( self->Specs && specifiers_has(self->Specs, Spec_Pure ) >= 0 ) strbuilder_append_str( result, txt(" = 0;") ); - else if (self->Body) + + // This is bodged in for now SOLEY for Unreal's PURE_VIRTUAL functional macro (I kept it open ended for other jank) + if ( self->SuffixSpecs ) + strbuilder_append_fmt( result, " %SB", code_to_strbuilder(self->SuffixSpecs) ); + + if (self->Body) strbuilder_append_fmt( result, " = %SB;", body_to_strbuilder(self->Body) ); if ( self->InlineCmt ) diff --git a/base/components/lexer.cpp b/base/components/lexer.cpp index 5b5404e..ec6aa7c 100644 --- a/base/components/lexer.cpp +++ b/base/components/lexer.cpp @@ -550,6 +550,9 @@ void lex_found_token( LexContext* ctx ) if ( bitfield_is_set(MacroFlags, macro->Flags, MF_Allow_As_Attribute) ) { ctx->token.Flags |= TF_Attribute; } + if ( bitfield_is_set(MacroFlags, macro->Flags, MF_Allow_As_Specifier ) ) { + ctx->token.Flags |= TF_Specifier; + } } else { diff --git a/base/components/parser.cpp b/base/components/parser.cpp index a265c95..57e244a 100644 --- a/base/components/parser.cpp +++ b/base/components/parser.cpp @@ -1403,6 +1403,18 @@ CodeFn parse_function_after_name( } // ( ) + Code suffix_specs = NullCode; + + // For Unreal's PURE_VIRTUAL Support + if ( left ) + { + Macro* macro = lookup_macro( currtok.Text ); + if (macro && tok_is_specifier(currtok)) + { + suffix_specs = parse_simple_preprocess(Tok_Preprocess_Macro_Expr); + } + } + CodeBody body = NullCode; CodeComment inline_cmt = NullCode; if ( check( Tok_BraceCurly_Open ) ) @@ -1479,6 +1491,9 @@ CodeFn parse_function_after_name( if ( specifiers ) result->Specs = specifiers; + if ( suffix_specs ) + result->SuffixSpecs = suffix_specs; + result->ReturnType = ret_type; if ( params ) diff --git a/base/components/parser_types.hpp b/base/components/parser_types.hpp index 512e653..d3f43d2 100644 --- a/base/components/parser_types.hpp +++ b/base/components/parser_types.hpp @@ -186,6 +186,8 @@ enum EMacroFlags : u16 // (MUST BE OF MT_Statement TYPE) MF_Allow_As_Definition = bit(4), + MF_Allow_As_Specifier = bit(5), // Created for Unreal's PURE_VIRTUAL + MF_Null = 0, MF_UnderlyingType = GEN_U16_MAX, }; diff --git a/base/dependencies/macros.hpp b/base/dependencies/macros.hpp index 53e6c90..2c3c55a 100644 --- a/base/dependencies/macros.hpp +++ b/base/dependencies/macros.hpp @@ -5,6 +5,7 @@ #pragma region Macros +#ifndef GEN_API #if GEN_COMPILER_MSVC #ifdef GEN_DYN_LINK #ifdef GEN_DYN_EXPORT @@ -22,6 +23,7 @@ #define GEN_API // Empty for static builds #endif #endif +#endif // GEN_API #ifndef global #define global static // Global variables