diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index d10243d..0234433 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -538,8 +538,6 @@ do \ // Only has operator overload definitions that C doesn't need. // CodeBody ast_inlines = gen_ast_inlines(); - Code inlines = scan_file( project_dir "components/inlines.hpp" ); - CodeBody ecode = gen_ecode ( project_dir "enums/ECodeTypes.csv", helper_use_c_definition ); CodeBody eoperator = gen_eoperator ( project_dir "enums/EOperator.csv", helper_use_c_definition ); CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv", helper_use_c_definition ); @@ -595,6 +593,9 @@ do \ break; } + // Used to track which functions need generic selectors. + Array(CodeFn) code_c_interface = array_init_reserve(GlobalAllocator, 16); + CodeBody parsed_ast = parse_file( project_dir "components/ast.hpp" ); CodeBody ast = def_body(CT_Global_Body); for ( Code entry = parsed_ast.begin(); entry != parsed_ast.end(); ++ entry ) switch (entry->Type) @@ -617,6 +618,48 @@ do \ } break; + case CT_Preprocess_Pragma: + { + if ( ! entry->Content.contains(txt("region Code C-Interface"))) { + continue; + } + // Reached the #pragma region Code C-Interface + for (b32 continue_for = true; continue_for; ++ entry) switch(entry->Type) + { + default: + // Pass through everything but function forwards or the end region pragma + ast.append(entry); + break; + + case CT_Function_Fwd: + { + // Were going to wrap usage of these procedures into generic selectors in code_types.hpp section, + // so we're changing the namespace to code__ + CodeFn fn = cast(CodeFn, entry); + if (fn->Name.starts_with(txt("code_"))) + { + StrC old_prefix = txt("code_"); + StrC actual_name = { fn->Name.Len - old_prefix.Len, fn->Name.Ptr + old_prefix.Len }; + String new_name = String::fmt_buf(GlobalAllocator, "code__%SC", actual_name ); + + fn->Name = get_cached_string(new_name); + code_c_interface.append(fn); + } + ast.append(entry); + } + break; + + case CT_Preprocess_Pragma: + // Reached the end of the interface, go back to regular ast.hpp iteration. + ast.append(entry); + if ( entry->Content.contains(txt("endregion Code C-Interface"))) { + continue_for = false; + } + break; + } + } + break; + case CT_Struct_Fwd: { CodeStruct fwd = cast(CodeStruct, entry); @@ -679,6 +722,37 @@ R"(#define AST_ArrSpecs_Cap \ break; } + StrC code_typenames[] = { + txt("Code"), + txt("CodeBody"), + txt("CodeAttributes"), + txt("CodeComment"), + txt("CodeClass"), + txt("CodeConstructor"), + txt("CodeDefine"), + txt("CodeDestructor"), + txt("CodeEnum"), + txt("CodeExec"), + txt("CodeExtern"), + txt("CodeInclude"), + txt("CodeFriend"), + txt("CodeFn"), + txt("CodeModule"), + txt("CodeNS"), + txt("CodeOperator"), + txt("CodeOpCast"), + txt("CodePragma"), + txt("CodeParam"), + txt("CodePreprocessCond"), + txt("CodeSpecifiers"), + txt("CodeTemplate"), + txt("CodeTypename"), + txt("CodeTypedef"), + txt("CodeUnion"), + txt("CodeUsing"), + txt("CodeVar"), + }; + CodeBody parsed_code_types = parse_file( project_dir "components/code_types.hpp" ); CodeBody code_types = def_body(CT_Global_Body); for ( Code entry = parsed_code_types.begin(); entry != parsed_code_types.end(); ++ entry ) switch( entry->Type ) @@ -698,6 +772,84 @@ R"(#define AST_ArrSpecs_Cap \ } break; + case CT_Preprocess_Pragma: if ( entry->Content.is_equal(txt("region Code Type C-Interface")) ) + { + code_types.append(entry); + code_types.append(fmt_newline); + /* + This thing makes a: + #define code_( code, ... ) _Generic( (code), \ + of defintions that look like: : code__, \ + default: gen_generic_selection (Fail case) \ + ) GEN_RESOLVED_FUNCTION_CALL( code, ... ) \ + */ + String generic_selector = String::make_reserve(GlobalAllocator, kilobytes(2)); + for ( CodeFn fn : code_c_interface ) + { + generic_selector.clear(); + StrC private_prefix = txt("code__"); + StrC actual_name = { fn->Name.Len - private_prefix.Len, fn->Name.Ptr + private_prefix.Len }; + String interface_name = String::fmt_buf(GlobalAllocator, "code_%SC", actual_name ); + + // Resolve generic's arguments + b32 has_args = fn->Params->NumEntries > 1; + String params_str = String::make_reserve(GlobalAllocator, 32); + for (CodeParam param = fn->Params->Next; param != fn->Params.end(); ++ param) { + // We skip the first parameter as its always going to be the code for selection + if (param->Next == nullptr) { + params_str.append_fmt( "%SC", param->Name ); + continue; + } + params_str.append_fmt( "%SC, ", param->Name ); + } + + char const* tmpl_def_start = nullptr; + if (has_args) { + tmpl_def_start = +R"(#define ( code, ) _Generic( (code), \ +)"; + } + else { + tmpl_def_start = +R"(#define ( code ) _Generic( (code), \ +)"; + } + // Definition start + generic_selector.append( token_fmt( + "interface_name", interface_name.to_strc() + , "params", params_str.to_strc() // Only used if has_args + , tmpl_def_start + )); + + // Append slots + for(StrC type : code_typenames ) { + generic_selector.append_fmt("%SC : %SC,\\\n", type, fn->Name ); + } + generic_selector.append(txt("default: gen_generic_selection_fail \\\n")); + + char const* tmpl_def_end = nullptr; + if (has_args) { + tmpl_def_end = txt("\t)\tGEN_RESOLVED_FUNCTION_CALL( ()code, )"); + } + else { + tmpl_def_end = txt("\t)\tGEN_RESOLVED_FUNCTION_CALL( ()code )"); + } + // Definition end + generic_selector.append( token_fmt( + "params", params_str.to_strc() + , "type", fn->Params->ValueType->Name + , tmpl_def_end ) ); + + code_types.append( untyped_str(generic_selector) ); + code_types.append( fmt_newline); + code_types.append( fmt_newline); + } + } + else { + code_types.append(entry); // Ignore the pragma otherwise + } + break; + default: code_types.append(entry); break; @@ -768,12 +920,57 @@ R"(#define AST_ArrSpecs_Cap \ { CodeFn fn = cast(CodeFn, entry); Code prev = entry->Prev; + if (prev && prev->Name.is_equal(entry->Name)) { + // rename second definition so there isn't a symbol conflict String postfix_arr = String::fmt_buf(GlobalAllocator, "%SC_arr", entry->Name); entry->Name = get_cached_string(postfix_arr.to_strc()); postfix_arr.free(); } - interface.append(fn); + + b32 handled= false; + for ( CodeParam opt_param : fn->Params ) if (opt_param->ValueType->Name.starts_with(txt("Opts_"))) + { + // Convert the definition to use a default struct: https://vxtwitter.com/vkrajacic/status/1749816169736073295 + StrC prefix = txt("def_"); + StrC actual_name = { fn->Name.Len - prefix.Len, fn->Name.Ptr + prefix.Len }; + StrC new_name = String::fmt_buf(GlobalAllocator, "def__%SC", actual_name ).to_strc(); + + // Resolve define's arguments + b32 has_args = fn->Params->NumEntries > 1; + String params_str = String::make_reserve(GlobalAllocator, 32); + for (CodeParam other_param = fn->Params; other_param != opt_param; ++ other_param) { + if ( other_param == opt_param ) { + params_str.append_fmt( "%SC", other_param->Name ); + break; + } + // If there are arguments before the optional, prepare them here. + params_str.append_fmt( "%SC, ", other_param->Name ); + } + char const* tmpl_fn_macro = nullptr; + if (params_str.length() > 0 ) { + tmpl_fn_macro= "#define ( ... ) ( () { __VA_ARGS__ } )\n"; + } + else { + tmpl_fn_macro= "#define ( ... ) ( () { __VA_ARGS__ } )\n"; + } + Code fn_macro = untyped_str(token_fmt( + "def_name", fn->Name + , "def__name", new_name + , "params", params_str.to_strc() + , "opts_type", opt_param->ValueType->Name + , tmpl_fn_macro + )); + + fn->Name = get_cached_string(new_name); + interface.append(fn); + interface.append(fn_macro); + handled = true; + break; + } + + if (! handled) + interface.append(fn); } break; @@ -791,6 +988,32 @@ R"(#define AST_ArrSpecs_Cap \ break; } + CodeBody parsed_inlines = parse_file( project_dir "components/inlines.hpp" ); + CodeBody inlines = def_body(CT_Global_Body); + for ( Code entry = parsed_inlines.begin(); entry != parsed_inlines.end(); ++ entry ) switch( entry->Type ) + { + case CT_Function: + { + // Were going to wrap usage of these procedures into generic selectors in code_types.hpp section, + // so we're changing the namespace to code__ + CodeFn fn = cast(CodeFn, entry); + if (fn->Name.starts_with(txt("code_"))) + { + StrC old_prefix = txt("code_"); + StrC actual_name = { fn->Name.Len - old_prefix.Len, fn->Name.Ptr + old_prefix.Len }; + String new_name = String::fmt_buf(GlobalAllocator, "code__%SC", actual_name ); + + fn->Name = get_cached_string(new_name); + } + inlines.append(entry); + } + break; + + default: + inlines.append(entry); + break; + } + s32 idx = 0; CodeBody parsed_header_end = parse_file( project_dir "components/header_end.hpp" ); CodeBody header_end = def_body(CT_Global_Body); @@ -838,14 +1061,82 @@ R"(#define AST_ArrSpecs_Cap \ Code src_static_data = scan_file( project_dir "components/static_data.cpp" ); Code src_ast_case_macros = scan_file( project_dir "components/ast_case_macros.cpp" ); - Code src_ast = scan_file( project_dir "components/ast.cpp" ); Code src_code_serialization = scan_file( project_dir "components/code_serialization.cpp" ); Code src_interface = scan_file( project_dir "components/interface.cpp" ); - Code src_upfront = scan_file( project_dir "components/interface.upfront.cpp" ); Code src_lexer = scan_file( project_dir "components/lexer.cpp" ); Code src_parser = scan_file( project_dir "components/parser.cpp" ); Code src_parsing_interface = scan_file( project_dir "components/interface.parsing.cpp" ); Code src_untyped = scan_file( project_dir "components/interface.untyped.cpp" ); + + CodeBody parsed_src_ast = parse_file( project_dir "components/ast.cpp" ); + CodeBody src_ast = def_body(CT_Global_Body); + for ( Code entry = parsed_src_ast.begin(); entry != parsed_src_ast.end(); ++ entry ) switch( entry ->Type ) + { + case CT_Function: + { + // Were going to wrap usage of these procedures into generic selectors in code_types.hpp section, + // so we're changing the namespace to code__ + CodeFn fn = cast(CodeFn, entry); + if (fn->Name.starts_with(txt("code_"))) + { + StrC old_prefix = txt("code_"); + StrC actual_name = { fn->Name.Len - old_prefix.Len, fn->Name.Ptr + old_prefix.Len }; + String new_name = String::fmt_buf(GlobalAllocator, "code__%SC", actual_name ); + + fn->Name = get_cached_string(new_name); + } + src_ast.append(entry); + } + break; + + default: + src_ast.append(entry); + break; + } + + CodeBody parsed_src_upfront = parse_file( project_dir "components/interface.upfront.cpp" ); + CodeBody src_upfront = def_body(CT_Global_Body); + for ( Code entry = parsed_src_upfront.begin(); entry != parsed_src_upfront.end(); ++ entry ) switch( entry ->Type ) + { + case CT_Enum: { + convert_cpp_enum_to_c(cast(CodeEnum, entry), src_upfront); + } + break; + + case CT_Function: + { + CodeFn fn = cast(CodeFn, entry); + Code prev = entry->Prev; + + for ( CodeParam arr_param : fn->Params ) + if ( fn->Name.starts_with(txt("def_")) + && ( arr_param->ValueType->Name.starts_with(txt("Specifier")) + || arr_param->ValueType->Name.starts_with(txt("Code")) ) + ) + { + // rename second definition so there isn't a symbol conflict + String postfix_arr = String::fmt_buf(GlobalAllocator, "%SC_arr", fn->Name); + fn->Name = get_cached_string(postfix_arr.to_strc()); + postfix_arr.free(); + } + + for ( CodeParam opt_param : fn->Params ) if (opt_param->ValueType->Name.starts_with(txt("Opts_"))) + { + StrC prefix = txt("def_"); + StrC actual_name = { fn->Name.Len - prefix.Len, fn->Name.Ptr + prefix.Len }; + StrC new_name = String::fmt_buf(GlobalAllocator, "def__%SC", actual_name ).to_strc(); + + fn->Name = get_cached_string(new_name); + } + + src_upfront.append(fn); + } + break; + + default: + src_upfront.append(entry); + break; + } #pragma endregion Resolve Components // THERE SHOULD BE NO NEW GENERIC CONTAINER DEFINTIONS PAST THIS POINT (It will not have slots for the generic selection generated macros) @@ -930,7 +1221,7 @@ R"(#define AST_ArrSpecs_Cap \ header.print( format_code_to_untyped(interface) ); header.print_fmt("#pragma region Inlines\n"); - header.print( inlines ); + header.print( format_code_to_untyped(inlines) ); header.print_fmt("#pragma endregion Inlines\n"); header.print(fmt_newline); @@ -989,9 +1280,9 @@ R"(#define AST_ArrSpecs_Cap \ header.print( src_code_serialization ); header.print_fmt( "#pragma endregion AST\n\n" ); - header.print_fmt( "#pragma region Interface\n" ); - header.print( src_interface ); - // header.print( src_upfront ); + header.print_fmt( "#pragma region Interface\n" ); + header.print( src_interface ); + header.print( format_code_to_untyped(src_upfront) ); // header.print_fmt( "\n#pragma region Parsing\n\n" ); // header.print( format_code_to_untyped(parser_nspace) ); // header.print( lexer ); diff --git a/gen_c_library/components/memory.fixed_arena.hpp b/gen_c_library/components/memory.fixed_arena.hpp index 86f4647..4ae7cf7 100644 --- a/gen_c_library/components/memory.fixed_arena.hpp +++ b/gen_c_library/components/memory.fixed_arena.hpp @@ -89,19 +89,20 @@ CodeBody gen_fixed_arenas() result.append(fmt_newline); result.append(parse_global_body(txt(R"( -#define fixed_arena_init(expr) _Generic((expr), \ - FixedArena_1KB* : fixed_arena_init_1KB, \ - FixedArena_4KB* : fixed_arena_init_4KB, \ - FixedArena_8KB* : fixed_arena_init_8KB, \ - FixedArena_16KB* : fixed_arena_init_16KB, \ - FixedArena_32KB* : fixed_arena_init_32KB, \ - FixedArena_64KB* : fixed_arena_init_64KB, \ - FixedArena_128KB* : fixed_arena_init_128KB, \ - FixedArena_256KB* : fixed_arena_init_256KB, \ - FixedArena_512KB* : fixed_arena_init_512KB, \ - FixedArena_1MB* : fixed_arena_init_1MB, \ - FixedArena_2MB* : fixed_arena_init_2MB, \ - FixedArena_4MB* : fixed_arena_init_4MB \ +#define fixed_arena_init(expr) _Generic((expr), \ + FixedArena_1KB* : fixed_arena_init_1KB, \ + FixedArena_4KB* : fixed_arena_init_4KB, \ + FixedArena_8KB* : fixed_arena_init_8KB, \ + FixedArena_16KB* : fixed_arena_init_16KB, \ + FixedArena_32KB* : fixed_arena_init_32KB, \ + FixedArena_64KB* : fixed_arena_init_64KB, \ + FixedArena_128KB* : fixed_arena_init_128KB, \ + FixedArena_256KB* : fixed_arena_init_256KB, \ + FixedArena_512KB* : fixed_arena_init_512KB, \ + FixedArena_1MB* : fixed_arena_init_1MB, \ + FixedArena_2MB* : fixed_arena_init_2MB, \ + FixedArena_4MB* : fixed_arena_init_4MB, \ + default : gen_generic_selection_fail \ ) GEN_RESOLVED_FUNCTION_CALL(& expr) #define fixed_arena_size_remaining(expr, alignment) _Generic((expr), \ @@ -116,7 +117,8 @@ CodeBody gen_fixed_arenas() FixedArena_512KB* : fixed_arena_size_remaining_512KB, \ FixedArena_1MB* : fixed_arena_size_remaining_1MB, \ FixedArena_2MB* : fixed_arena_size_remaining_2MB, \ - FixedArena_4MB* : fixed_arena_size_remaining_4MB \ + FixedArena_4MB* : fixed_arena_size_remaining_4MB, \ + default : gen_generic_selection_fail \ ) GEN_RESOLVED_FUNCTION_CALL(& expr, alignment) )" ))); diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 545e4e0..f7fdd9c 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -402,7 +402,7 @@ static_assert( sizeof(AST) == AST_POD_Size, "ERROR: AST is not size of AST_POD_S struct InvalidCode_ImplictCaster; #define InvalidCode (InvalidCode_ImplictCaster{}) #else -#define InvalidCode { (void*)Code_Invalid } +#define InvalidCode (void*){ (void*)Code_Invalid } #endif #if GEN_COMPILER_CPP diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index 2485fb4..388d251 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -197,6 +197,9 @@ struct CodeParam forceinline bool has_entries(); forceinline String to_string(); forceinline void to_string( String& result ); + + forceinline CodeParam begin() { return begin_CodeParam(* this); } + forceinline CodeParam end() { return end_CodeParam(* this); } #endif Using_CodeOps( CodeParam ); forceinline operator Code() { return { (AST*)ast }; } diff --git a/project/components/interface.cpp b/project/components/interface.cpp index c3c0d5a..1927872 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -4,8 +4,8 @@ #endif GEN_NS_PARSER_BEGIN -internal void init(); -internal void deinit(); +internal void parser_init(); +internal void parser_deinit(); GEN_NS_PARSER_END internal @@ -148,9 +148,8 @@ void define_constants() # define def_constant_code_type( Type_ ) \ do \ { \ - Opts_def_type ops = {}; \ StrC name_str = name(Type_); \ - t_##Type_ = def_type( name_str, ops ); \ + t_##Type_ = def_type( name_str ); \ code_set_global( cast(Code, t_##Type_)); \ } while(0) @@ -323,7 +322,7 @@ void init() PreprocessorDefines = array_init_reserve(StringCached, GlobalAllocator, kilobytes(1) ); define_constants(); - GEN_NS_PARSER init(); + GEN_NS_PARSER parser_init(); } void deinit() @@ -368,7 +367,7 @@ void deinit() while ( left--, left ); array_free(Global_AllocatorBuckets); - GEN_NS_PARSER deinit(); + GEN_NS_PARSER parser_deinit(); } void reset() diff --git a/project/components/interface.hpp b/project/components/interface.hpp index 60cf6be..068dacb 100644 --- a/project/components/interface.hpp +++ b/project/components/interface.hpp @@ -16,7 +16,6 @@ */ // Initialize the library. -// This currently just initializes the CodePool. void init(); // Currently manually free's the arenas, code for checking for leaks. @@ -53,13 +52,13 @@ CodeAttributes def_attributes( StrC content ); CodeComment def_comment ( StrC content ); struct Opts_def_struct { - Code body; + CodeBody body; CodeTypename parent; AccessSpec parent_access; CodeAttributes attributes; - ModuleFlag mflags; CodeTypename* interfaces; s32 num_interfaces; + ModuleFlag mflags; }; CodeClass def_class( StrC name, Opts_def_struct otps GEN_PARAM_DEFAULT ); @@ -79,7 +78,7 @@ struct Opts_def_destructor { CodeDestructor def_destructor( Opts_def_destructor opts GEN_PARAM_DEFAULT ); struct Opts_def_enum { - Code body; + CodeBody body; CodeTypename type; EnumT specifier; CodeAttributes attributes; @@ -88,13 +87,13 @@ struct Opts_def_enum { CodeEnum def_enum( StrC name, Opts_def_enum opts GEN_PARAM_DEFAULT ); CodeExec def_execution ( StrC content ); -CodeExtern def_extern_link( StrC name, Code body ); +CodeExtern def_extern_link( StrC name, CodeBody body ); CodeFriend def_friend ( Code symbol ); struct Opts_def_function { CodeParam params; CodeTypename ret_type; - Code body; + CodeBody body; CodeSpecifiers specs; CodeAttributes attrs; ModuleFlag mflags; @@ -104,14 +103,14 @@ CodeFn def_function( StrC name, Opts_def_function opts GEN_PARAM_DEFAULT ); struct Opts_def_include { b32 foreign; }; struct Opts_def_module { ModuleFlag mflags; }; struct Opts_def_namespace { ModuleFlag mflags; }; -CodeInclude def_include ( StrC content, Opts_def_include opts GEN_PARAM_DEFAULT ); -CodeModule def_module ( StrC name, Opts_def_module opts GEN_PARAM_DEFAULT ); -CodeNS def_namespace( StrC name, Code body, Opts_def_namespace opts GEN_PARAM_DEFAULT ); +CodeInclude def_include ( StrC content, Opts_def_include opts GEN_PARAM_DEFAULT ); +CodeModule def_module ( StrC name, Opts_def_module opts GEN_PARAM_DEFAULT ); +CodeNS def_namespace( StrC name, CodeBody body, Opts_def_namespace opts GEN_PARAM_DEFAULT ); struct Opts_def_operator { CodeParam params; CodeTypename ret_type; - Code body; + CodeBody body; CodeSpecifiers specifiers; CodeAttributes attributes; ModuleFlag mflags; @@ -119,7 +118,7 @@ struct Opts_def_operator { CodeOperator def_operator( Operator op, StrC nspace, Opts_def_operator opts GEN_PARAM_DEFAULT ); struct Opts_def_operator_cast { - Code body; + CodeBody body; CodeSpecifiers specs; }; CodeOpCast def_operator_cast( CodeTypename type, Opts_def_operator_cast opts GEN_PARAM_DEFAULT ); @@ -155,13 +154,13 @@ struct Opts_def_union { CodeAttributes attributes; ModuleFlag mflags; }; -CodeUnion def_union( StrC name, Code body, Opts_def_union opts GEN_PARAM_DEFAULT ); +CodeUnion def_union( StrC name, CodeBody body, Opts_def_union opts GEN_PARAM_DEFAULT ); struct Opts_def_using { CodeAttributes attributes; ModuleFlag mflags; }; -CodeUsing def_using( StrC name, Code type, Opts_def_using opts GEN_PARAM_DEFAULT ); +CodeUsing def_using( StrC name, CodeTypename type, Opts_def_using opts GEN_PARAM_DEFAULT ); CodeUsing def_using_namespace( StrC name ); diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index 45214ab..835df36 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -482,10 +482,12 @@ CodeComment def_comment( StrC content ) if ( * string_back(cmt_formatted) != '\n' ) string_append_strc( & cmt_formatted, txt("\n") ); + StrC name = { string_length(cmt_formatted), cmt_formatted }; + Code result = make_code(); result->Type = CT_Comment; - result->Name = get_cached_string( { string_length(cmt_formatted), cmt_formatted } ); + result->Name = get_cached_string( name ); result->Content = result->Name; string_free(& cmt_formatted); @@ -546,7 +548,7 @@ CodeClass def_class( StrC name, Opts_def_struct p ) { name_check( def_class, name ); - Code body = p.body; + CodeBody body = p.body; CodeTypename parent = p.parent; AccessSpec parent_access = p.parent_access; CodeAttributes attributes = p.attributes; @@ -586,7 +588,7 @@ CodeClass def_class( StrC name, Opts_def_struct p ) result->Type = CT_Class; result->Body = body; - result->Body->Parent = result; // TODO(Ed): Review this? + result->Body->Parent = cast(Code, result); // TODO(Ed): Review this? } else { @@ -682,7 +684,7 @@ CodeDestructor def_destructor( Opts_def_destructor p ) CodeEnum def_enum( StrC name, Opts_def_enum p ) { - Code body = p.body; + CodeBody body = p.body; CodeTypename type = p.type; EnumT specifier = p.specifier; CodeAttributes attributes = p.attributes; @@ -764,7 +766,7 @@ CodeExec def_execution( StrC content ) return (CodeExec) result; } -CodeExtern def_extern_link( StrC name, Code body ) +CodeExtern def_extern_link( StrC name, CodeBody body ) { name_check( def_extern_linkage, name ); null_check( def_extern_linkage, body ); @@ -818,7 +820,7 @@ CodeFn def_function( StrC name, Opts_def_function p ) { CodeParam params = p.params; CodeTypename ret_type = p.ret_type; - Code body = p.body; + CodeBody body = p.body; CodeSpecifiers specifiers = p.specs; CodeAttributes attributes = p.attrs; ModuleFlag mflags = p.mflags; @@ -934,7 +936,7 @@ CodeModule def_module( StrC name, Opts_def_module p ) return (CodeModule) result; } -CodeNS def_namespace( StrC name, Code body, Opts_def_namespace p ) +CodeNS def_namespace( StrC name, CodeBody body, Opts_def_namespace p ) { name_check( def_namespace, name ); null_check( def_namespace, body); @@ -958,7 +960,7 @@ CodeOperator def_operator( Operator op, StrC nspace, Opts_def_operator p ) { CodeParam params_code = p.params; CodeTypename ret_type = p.ret_type; - Code body = p.body; + CodeBody body = p.body; CodeSpecifiers specifiers = p.specifiers; CodeAttributes attributes = p.attributes; ModuleFlag mflags = p.mflags; @@ -989,9 +991,12 @@ CodeOperator def_operator( Operator op, StrC nspace, Opts_def_operator p ) name = str_fmt_buf( "%.*soperator %.*s", nspace.Len, nspace.Ptr, op_str.Len, op_str.Ptr ); else name = str_fmt_buf( "operator %.*s", op_str.Len, op_str.Ptr ); + + StrC name_resolved = { str_len(name), name }; + CodeOperator result = (CodeOperator) make_code(); - result->Name = get_cached_string( { str_len(name), name } ); + result->Name = get_cached_string( name_resolved ); result->ModuleFlags = mflags; result->Op = op; @@ -1038,7 +1043,7 @@ CodeOperator def_operator( Operator op, StrC nspace, Opts_def_operator p ) CodeOpCast def_operator_cast( CodeTypename type, Opts_def_operator_cast p ) { - Code body = p.body; + CodeBody body = p.body; CodeSpecifiers const_spec = p.specs; null_check( def_operator_cast, type ); @@ -1168,7 +1173,7 @@ CodeSpecifiers def_specifier( Specifier spec ) CodeStruct def_struct( StrC name, Opts_def_struct p ) { - Code body = p.body; + CodeBody body = p.body; CodeTypename parent = p.parent; AccessSpec parent_access = p.parent_access; CodeAttributes attributes = p.attributes; @@ -1198,7 +1203,7 @@ CodeStruct def_struct( StrC name, Opts_def_struct p ) result = (CodeStruct) make_code(); result->ModuleFlags = mflags; - if ( name ) + if ( name.Len ) result->Name = get_cached_string( name ); if ( body ) @@ -1338,7 +1343,7 @@ CodeTypedef def_typedef( StrC name, Code type, Opts_def_typedef p ) } // Registering the type. - Code registered_type = def_type( name ); + CodeTypename registered_type = def_type( name ); if ( ! registered_type ) { @@ -1373,7 +1378,7 @@ CodeTypedef def_typedef( StrC name, Code type, Opts_def_typedef p ) return result; } -CodeUnion def_union( StrC name, Code body, Opts_def_union p ) +CodeUnion def_union( StrC name, CodeBody body, Opts_def_union p ) { null_check( def_union, body ); @@ -1405,12 +1410,12 @@ CodeUnion def_union( StrC name, Code body, Opts_def_union p ) return result; } -CodeUsing def_using( StrC name, Code type, Opts_def_using p ) +CodeUsing def_using( StrC name, CodeTypename type, Opts_def_using p ) { name_check( def_using, name ); null_check( def_using, type ); - Code register_type = def_type( name ); + CodeTypename register_type = def_type( name ); if ( ! register_type ) { @@ -1451,7 +1456,7 @@ CodeUsing def_using_namespace( StrC name ) return (CodeUsing) result; } -CodeVar def_variable( CodeTypename type, StrC name, Code value, Opts_def_variable p ) +CodeVar def_variable( CodeTypename type, StrC name, Opts_def_variable p ) { name_check( def_variable, name ); null_check( def_variable, type ); @@ -1474,9 +1479,9 @@ CodeVar def_variable( CodeTypename type, StrC name, Code value, Opts_def_variabl return InvalidCode; } - if ( value && value->Type != CT_Untyped ) + if ( p.value && p.value->Type != CT_Untyped ) { - log_failure( "gen::def_variable: value was not a `Untyped` type - %s", code_debug_str(value) ); + log_failure( "gen::def_variable: value was not a `Untyped` type - %s", code_debug_str(p.value) ); return InvalidCode; } @@ -1494,8 +1499,8 @@ CodeVar def_variable( CodeTypename type, StrC name, Code value, Opts_def_variabl if ( p.specifiers ) result->Specs = p.specifiers; - if ( value ) - result->Value = value; + if ( p.value ) + result->Value = p.value; return result; } @@ -1915,7 +1920,7 @@ CodeBody def_global_body( s32 num, ... ) { case CT_Global_Body: // result.body_append( entry.code_cast() ) ; - body_append( result, cast(CodeBody, entry) ); + body_append_body( result, cast(CodeBody, entry) ); continue; GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES @@ -1956,7 +1961,7 @@ CodeBody def_global_body( s32 num, Code* codes ) switch (entry->Type) { case CT_Global_Body: - body_append(result, cast(CodeBody, entry) ); + body_append_body(result, cast(CodeBody, entry) ); continue; GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES @@ -2090,21 +2095,21 @@ CodeParam def_params( s32 num, CodeParam* codes ) { def_body_code_array_start( def_params ); -# define check_current() \ - if ( current.ast == nullptr ) \ - { \ - log_failure("gen::def_params: Provide a null code in codes array"); \ - return InvalidCode; \ - } \ - \ - if (current->Type != CT_Parameters ) \ - { \ - log_failure("gen::def_params: Code in coes array is not of paramter type - %s", code_debug_str(current) ); \ - return InvalidCode; \ +# define check_current(current) \ + if ( current == nullptr ) \ + { \ + log_failure("gen::def_params: Provide a null code in codes array"); \ + return InvalidCode; \ + } \ + \ + if (current->Type != CT_Parameters ) \ + { \ + log_failure("gen::def_params: Code in coes array is not of paramter type - %s", code_debug_str(current) ); \ + return InvalidCode; \ } CodeParam current = (CodeParam)code_duplicate(* codes); - check_current(); + check_current(current); CodeParam result = (CodeParam) make_code(); @@ -2114,7 +2119,7 @@ CodeParam def_params( s32 num, CodeParam* codes ) while( codes++, current = * codes, num--, num > 0 ) { - check_current(); + check_current(current); params_append(result, current ); } # undef check_current @@ -2293,7 +2298,7 @@ CodeBody def_union_body( s32 num, ... ) return result; } -CodeBody def_union_body( s32 num, CodeUnion* codes ) +CodeBody def_union_body( s32 num, Code* codes ) { def_body_code_array_start( def_union_body ); diff --git a/project/components/parser.cpp b/project/components/parser.cpp index d678309..c5abaa3 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -134,7 +134,7 @@ bool __eat(TokArray* self, TokType type ) } internal -void init() +void parser_init() { Tokens = array_init_reserve(Token, arena_allocator_info( & LexArena) , ( LexAllocator_Size - sizeof( ArrayHeader ) ) / sizeof(Token) @@ -145,7 +145,7 @@ void init() } internal -void deinit() +void parser_deinit() { parser::Tokens = { nullptr }; } @@ -780,10 +780,10 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) } if ( which == Tok_Decl_Class ) - result = def_class( to_str(name), { body, parent, access, attributes, mflags } ); + result = def_class( to_str(name), { body, parent, access, attributes, nullptr, 0, mflags } ); else - result = def_struct( to_str(name), { body, (CodeTypename)parent, access, attributes, mflags } ); + result = def_struct( to_str(name), { body, (CodeTypename)parent, access, attributes, nullptr, 0, mflags } ); if ( inline_cmt ) result->InlineCmt = inline_cmt; diff --git a/project/dependencies/macros.hpp b/project/dependencies/macros.hpp index c51057e..2879b13 100644 --- a/project/dependencies/macros.hpp +++ b/project/dependencies/macros.hpp @@ -52,7 +52,7 @@ # define ccast( type, value ) ( (type)(value) ) # endif # ifndef pcast -# define pcast( type, value ) ( * (type*)(value) ) +# define pcast( type, value ) ( * (type*)(& value) ) # endif # ifndef rcast # define rcast( type, value ) ( (type)(value) ) diff --git a/project/helpers/push_ignores.inline.hpp b/project/helpers/push_ignores.inline.hpp index 5b8a932..8b77d07 100644 --- a/project/helpers/push_ignores.inline.hpp +++ b/project/helpers/push_ignores.inline.hpp @@ -9,6 +9,7 @@ # pragma clang diagnostic ignored "-Wunused-function" # pragma clang diagnostic ignored "-Wbraced-scalar-init" # pragma clang diagnostic ignored "-W#pragma-messages" +# pragma clang diagnostic ignored "-Wstatic-in-inline" #endif #ifdef __GNUC__