gen.h compiles with interface.upfront.cpp injected

This commit is contained in:
Edward R. Gonzalez 2024-12-09 14:55:02 -05:00
parent ed9f719a07
commit 6147912783
10 changed files with 385 additions and 85 deletions

View File

@ -538,8 +538,6 @@ do \
// Only has operator overload definitions that C doesn't need. // Only has operator overload definitions that C doesn't need.
// CodeBody ast_inlines = gen_ast_inlines(); // 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 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 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 ); CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv", helper_use_c_definition );
@ -595,6 +593,9 @@ do \
break; break;
} }
// Used to track which functions need generic selectors.
Array(CodeFn) code_c_interface = array_init_reserve<CodeFn>(GlobalAllocator, 16);
CodeBody parsed_ast = parse_file( project_dir "components/ast.hpp" ); CodeBody parsed_ast = parse_file( project_dir "components/ast.hpp" );
CodeBody ast = def_body(CT_Global_Body); CodeBody ast = def_body(CT_Global_Body);
for ( Code entry = parsed_ast.begin(); entry != parsed_ast.end(); ++ entry ) switch (entry->Type) for ( Code entry = parsed_ast.begin(); entry != parsed_ast.end(); ++ entry ) switch (entry->Type)
@ -617,6 +618,48 @@ do \
} }
break; 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__<name>
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: case CT_Struct_Fwd:
{ {
CodeStruct fwd = cast(CodeStruct, entry); CodeStruct fwd = cast(CodeStruct, entry);
@ -679,6 +722,37 @@ R"(#define AST_ArrSpecs_Cap \
break; 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 parsed_code_types = parse_file( project_dir "components/code_types.hpp" );
CodeBody code_types = def_body(CT_Global_Body); CodeBody code_types = def_body(CT_Global_Body);
for ( Code entry = parsed_code_types.begin(); entry != parsed_code_types.end(); ++ entry ) switch( entry->Type ) 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; 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_<interface_name>( code, ... ) _Generic( (code), \
<slots> of defintions that look like: <typeof(code)>: code__<interface_name>, \
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 <interface_name>( code, <params> ) _Generic( (code), \
)";
}
else {
tmpl_def_start =
R"(#define <interface_name>( 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( (<type>)code, <params> )");
}
else {
tmpl_def_end = txt("\t)\tGEN_RESOLVED_FUNCTION_CALL( (<type>)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: default:
code_types.append(entry); code_types.append(entry);
break; break;
@ -768,11 +920,56 @@ R"(#define AST_ArrSpecs_Cap \
{ {
CodeFn fn = cast(CodeFn, entry); CodeFn fn = cast(CodeFn, entry);
Code prev = entry->Prev; Code prev = entry->Prev;
if (prev && prev->Name.is_equal(entry->Name)) { 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); String postfix_arr = String::fmt_buf(GlobalAllocator, "%SC_arr", entry->Name);
entry->Name = get_cached_string(postfix_arr.to_strc()); entry->Name = get_cached_string(postfix_arr.to_strc());
postfix_arr.free(); postfix_arr.free();
} }
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 <def_name>( <params> ... ) <def__name>( <params> (<opts_type>) { __VA_ARGS__ } )\n";
}
else {
tmpl_fn_macro= "#define <def_name>( ... ) <def__name>( (<opts_type>) { __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); interface.append(fn);
} }
break; break;
@ -791,6 +988,32 @@ R"(#define AST_ArrSpecs_Cap \
break; 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__<name>
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; s32 idx = 0;
CodeBody parsed_header_end = parse_file( project_dir "components/header_end.hpp" ); CodeBody parsed_header_end = parse_file( project_dir "components/header_end.hpp" );
CodeBody header_end = def_body(CT_Global_Body); 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_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_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_code_serialization = scan_file( project_dir "components/code_serialization.cpp" );
Code src_interface = scan_file( project_dir "components/interface.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_lexer = scan_file( project_dir "components/lexer.cpp" );
Code src_parser = scan_file( project_dir "components/parser.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_parsing_interface = scan_file( project_dir "components/interface.parsing.cpp" );
Code src_untyped = scan_file( project_dir "components/interface.untyped.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__<name>
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 #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) // 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( format_code_to_untyped(interface) );
header.print_fmt("#pragma region Inlines\n"); 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("#pragma endregion Inlines\n");
header.print(fmt_newline); header.print(fmt_newline);
@ -991,7 +1282,7 @@ R"(#define AST_ArrSpecs_Cap \
header.print_fmt( "#pragma region Interface\n" ); header.print_fmt( "#pragma region Interface\n" );
header.print( src_interface ); header.print( src_interface );
// header.print( src_upfront ); header.print( format_code_to_untyped(src_upfront) );
// header.print_fmt( "\n#pragma region Parsing\n\n" ); // header.print_fmt( "\n#pragma region Parsing\n\n" );
// header.print( format_code_to_untyped(parser_nspace) ); // header.print( format_code_to_untyped(parser_nspace) );
// header.print( lexer ); // header.print( lexer );

View File

@ -101,7 +101,8 @@ CodeBody gen_fixed_arenas()
FixedArena_512KB* : fixed_arena_init_512KB, \ FixedArena_512KB* : fixed_arena_init_512KB, \
FixedArena_1MB* : fixed_arena_init_1MB, \ FixedArena_1MB* : fixed_arena_init_1MB, \
FixedArena_2MB* : fixed_arena_init_2MB, \ FixedArena_2MB* : fixed_arena_init_2MB, \
FixedArena_4MB* : fixed_arena_init_4MB \ FixedArena_4MB* : fixed_arena_init_4MB, \
default : gen_generic_selection_fail \
) GEN_RESOLVED_FUNCTION_CALL(& expr) ) GEN_RESOLVED_FUNCTION_CALL(& expr)
#define fixed_arena_size_remaining(expr, alignment) _Generic((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_512KB* : fixed_arena_size_remaining_512KB, \
FixedArena_1MB* : fixed_arena_size_remaining_1MB, \ FixedArena_1MB* : fixed_arena_size_remaining_1MB, \
FixedArena_2MB* : fixed_arena_size_remaining_2MB, \ 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) ) GEN_RESOLVED_FUNCTION_CALL(& expr, alignment)
)" )"
))); )));

View File

@ -402,7 +402,7 @@ static_assert( sizeof(AST) == AST_POD_Size, "ERROR: AST is not size of AST_POD_S
struct InvalidCode_ImplictCaster; struct InvalidCode_ImplictCaster;
#define InvalidCode (InvalidCode_ImplictCaster{}) #define InvalidCode (InvalidCode_ImplictCaster{})
#else #else
#define InvalidCode { (void*)Code_Invalid } #define InvalidCode (void*){ (void*)Code_Invalid }
#endif #endif
#if GEN_COMPILER_CPP #if GEN_COMPILER_CPP

View File

@ -197,6 +197,9 @@ struct CodeParam
forceinline bool has_entries(); forceinline bool has_entries();
forceinline String to_string(); forceinline String to_string();
forceinline void to_string( String& result ); forceinline void to_string( String& result );
forceinline CodeParam begin() { return begin_CodeParam(* this); }
forceinline CodeParam end() { return end_CodeParam(* this); }
#endif #endif
Using_CodeOps( CodeParam ); Using_CodeOps( CodeParam );
forceinline operator Code() { return { (AST*)ast }; } forceinline operator Code() { return { (AST*)ast }; }

View File

@ -4,8 +4,8 @@
#endif #endif
GEN_NS_PARSER_BEGIN GEN_NS_PARSER_BEGIN
internal void init(); internal void parser_init();
internal void deinit(); internal void parser_deinit();
GEN_NS_PARSER_END GEN_NS_PARSER_END
internal internal
@ -148,9 +148,8 @@ void define_constants()
# define def_constant_code_type( Type_ ) \ # define def_constant_code_type( Type_ ) \
do \ do \
{ \ { \
Opts_def_type ops = {}; \
StrC name_str = name(Type_); \ 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_)); \ code_set_global( cast(Code, t_##Type_)); \
} while(0) } while(0)
@ -323,7 +322,7 @@ void init()
PreprocessorDefines = array_init_reserve(StringCached, GlobalAllocator, kilobytes(1) ); PreprocessorDefines = array_init_reserve(StringCached, GlobalAllocator, kilobytes(1) );
define_constants(); define_constants();
GEN_NS_PARSER init(); GEN_NS_PARSER parser_init();
} }
void deinit() void deinit()
@ -368,7 +367,7 @@ void deinit()
while ( left--, left ); while ( left--, left );
array_free(Global_AllocatorBuckets); array_free(Global_AllocatorBuckets);
GEN_NS_PARSER deinit(); GEN_NS_PARSER parser_deinit();
} }
void reset() void reset()

View File

@ -16,7 +16,6 @@
*/ */
// Initialize the library. // Initialize the library.
// This currently just initializes the CodePool.
void init(); void init();
// Currently manually free's the arenas, code for checking for leaks. // 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 ); CodeComment def_comment ( StrC content );
struct Opts_def_struct { struct Opts_def_struct {
Code body; CodeBody body;
CodeTypename parent; CodeTypename parent;
AccessSpec parent_access; AccessSpec parent_access;
CodeAttributes attributes; CodeAttributes attributes;
ModuleFlag mflags;
CodeTypename* interfaces; CodeTypename* interfaces;
s32 num_interfaces; s32 num_interfaces;
ModuleFlag mflags;
}; };
CodeClass def_class( StrC name, Opts_def_struct otps GEN_PARAM_DEFAULT ); 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 ); CodeDestructor def_destructor( Opts_def_destructor opts GEN_PARAM_DEFAULT );
struct Opts_def_enum { struct Opts_def_enum {
Code body; CodeBody body;
CodeTypename type; CodeTypename type;
EnumT specifier; EnumT specifier;
CodeAttributes attributes; CodeAttributes attributes;
@ -88,13 +87,13 @@ struct Opts_def_enum {
CodeEnum def_enum( StrC name, Opts_def_enum opts GEN_PARAM_DEFAULT ); CodeEnum def_enum( StrC name, Opts_def_enum opts GEN_PARAM_DEFAULT );
CodeExec def_execution ( StrC content ); 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 ); CodeFriend def_friend ( Code symbol );
struct Opts_def_function { struct Opts_def_function {
CodeParam params; CodeParam params;
CodeTypename ret_type; CodeTypename ret_type;
Code body; CodeBody body;
CodeSpecifiers specs; CodeSpecifiers specs;
CodeAttributes attrs; CodeAttributes attrs;
ModuleFlag mflags; ModuleFlag mflags;
@ -106,12 +105,12 @@ struct Opts_def_module { ModuleFlag mflags; };
struct Opts_def_namespace { ModuleFlag mflags; }; struct Opts_def_namespace { ModuleFlag mflags; };
CodeInclude def_include ( StrC content, Opts_def_include 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 ); 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 ); CodeNS def_namespace( StrC name, CodeBody body, Opts_def_namespace opts GEN_PARAM_DEFAULT );
struct Opts_def_operator { struct Opts_def_operator {
CodeParam params; CodeParam params;
CodeTypename ret_type; CodeTypename ret_type;
Code body; CodeBody body;
CodeSpecifiers specifiers; CodeSpecifiers specifiers;
CodeAttributes attributes; CodeAttributes attributes;
ModuleFlag mflags; ModuleFlag mflags;
@ -119,7 +118,7 @@ struct Opts_def_operator {
CodeOperator def_operator( Operator op, StrC nspace, Opts_def_operator opts GEN_PARAM_DEFAULT ); CodeOperator def_operator( Operator op, StrC nspace, Opts_def_operator opts GEN_PARAM_DEFAULT );
struct Opts_def_operator_cast { struct Opts_def_operator_cast {
Code body; CodeBody body;
CodeSpecifiers specs; CodeSpecifiers specs;
}; };
CodeOpCast def_operator_cast( CodeTypename type, Opts_def_operator_cast opts GEN_PARAM_DEFAULT ); CodeOpCast def_operator_cast( CodeTypename type, Opts_def_operator_cast opts GEN_PARAM_DEFAULT );
@ -155,13 +154,13 @@ struct Opts_def_union {
CodeAttributes attributes; CodeAttributes attributes;
ModuleFlag mflags; 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 { struct Opts_def_using {
CodeAttributes attributes; CodeAttributes attributes;
ModuleFlag mflags; 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 ); CodeUsing def_using_namespace( StrC name );

View File

@ -482,10 +482,12 @@ CodeComment def_comment( StrC content )
if ( * string_back(cmt_formatted) != '\n' ) if ( * string_back(cmt_formatted) != '\n' )
string_append_strc( & cmt_formatted, txt("\n") ); string_append_strc( & cmt_formatted, txt("\n") );
StrC name = { string_length(cmt_formatted), cmt_formatted };
Code Code
result = make_code(); result = make_code();
result->Type = CT_Comment; 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; result->Content = result->Name;
string_free(& cmt_formatted); string_free(& cmt_formatted);
@ -546,7 +548,7 @@ CodeClass def_class( StrC name, Opts_def_struct p )
{ {
name_check( def_class, name ); name_check( def_class, name );
Code body = p.body; CodeBody body = p.body;
CodeTypename parent = p.parent; CodeTypename parent = p.parent;
AccessSpec parent_access = p.parent_access; AccessSpec parent_access = p.parent_access;
CodeAttributes attributes = p.attributes; CodeAttributes attributes = p.attributes;
@ -586,7 +588,7 @@ CodeClass def_class( StrC name, Opts_def_struct p )
result->Type = CT_Class; result->Type = CT_Class;
result->Body = body; result->Body = body;
result->Body->Parent = result; // TODO(Ed): Review this? result->Body->Parent = cast(Code, result); // TODO(Ed): Review this?
} }
else else
{ {
@ -682,7 +684,7 @@ CodeDestructor def_destructor( Opts_def_destructor p )
CodeEnum def_enum( StrC name, Opts_def_enum p ) CodeEnum def_enum( StrC name, Opts_def_enum p )
{ {
Code body = p.body; CodeBody body = p.body;
CodeTypename type = p.type; CodeTypename type = p.type;
EnumT specifier = p.specifier; EnumT specifier = p.specifier;
CodeAttributes attributes = p.attributes; CodeAttributes attributes = p.attributes;
@ -764,7 +766,7 @@ CodeExec def_execution( StrC content )
return (CodeExec) result; 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 ); name_check( def_extern_linkage, name );
null_check( def_extern_linkage, body ); null_check( def_extern_linkage, body );
@ -818,7 +820,7 @@ CodeFn def_function( StrC name, Opts_def_function p )
{ {
CodeParam params = p.params; CodeParam params = p.params;
CodeTypename ret_type = p.ret_type; CodeTypename ret_type = p.ret_type;
Code body = p.body; CodeBody body = p.body;
CodeSpecifiers specifiers = p.specs; CodeSpecifiers specifiers = p.specs;
CodeAttributes attributes = p.attrs; CodeAttributes attributes = p.attrs;
ModuleFlag mflags = p.mflags; ModuleFlag mflags = p.mflags;
@ -934,7 +936,7 @@ CodeModule def_module( StrC name, Opts_def_module p )
return (CodeModule) result; 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 ); name_check( def_namespace, name );
null_check( def_namespace, body); 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; CodeParam params_code = p.params;
CodeTypename ret_type = p.ret_type; CodeTypename ret_type = p.ret_type;
Code body = p.body; CodeBody body = p.body;
CodeSpecifiers specifiers = p.specifiers; CodeSpecifiers specifiers = p.specifiers;
CodeAttributes attributes = p.attributes; CodeAttributes attributes = p.attributes;
ModuleFlag mflags = p.mflags; 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 ); name = str_fmt_buf( "%.*soperator %.*s", nspace.Len, nspace.Ptr, op_str.Len, op_str.Ptr );
else else
name = str_fmt_buf( "operator %.*s", op_str.Len, op_str.Ptr ); name = str_fmt_buf( "operator %.*s", op_str.Len, op_str.Ptr );
StrC name_resolved = { str_len(name), name };
CodeOperator CodeOperator
result = (CodeOperator) make_code(); result = (CodeOperator) make_code();
result->Name = get_cached_string( { str_len(name), name } ); result->Name = get_cached_string( name_resolved );
result->ModuleFlags = mflags; result->ModuleFlags = mflags;
result->Op = op; 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 ) CodeOpCast def_operator_cast( CodeTypename type, Opts_def_operator_cast p )
{ {
Code body = p.body; CodeBody body = p.body;
CodeSpecifiers const_spec = p.specs; CodeSpecifiers const_spec = p.specs;
null_check( def_operator_cast, type ); null_check( def_operator_cast, type );
@ -1168,7 +1173,7 @@ CodeSpecifiers def_specifier( Specifier spec )
CodeStruct def_struct( StrC name, Opts_def_struct p ) CodeStruct def_struct( StrC name, Opts_def_struct p )
{ {
Code body = p.body; CodeBody body = p.body;
CodeTypename parent = p.parent; CodeTypename parent = p.parent;
AccessSpec parent_access = p.parent_access; AccessSpec parent_access = p.parent_access;
CodeAttributes attributes = p.attributes; CodeAttributes attributes = p.attributes;
@ -1198,7 +1203,7 @@ CodeStruct def_struct( StrC name, Opts_def_struct p )
result = (CodeStruct) make_code(); result = (CodeStruct) make_code();
result->ModuleFlags = mflags; result->ModuleFlags = mflags;
if ( name ) if ( name.Len )
result->Name = get_cached_string( name ); result->Name = get_cached_string( name );
if ( body ) if ( body )
@ -1338,7 +1343,7 @@ CodeTypedef def_typedef( StrC name, Code type, Opts_def_typedef p )
} }
// Registering the type. // Registering the type.
Code registered_type = def_type( name ); CodeTypename registered_type = def_type( name );
if ( ! registered_type ) if ( ! registered_type )
{ {
@ -1373,7 +1378,7 @@ CodeTypedef def_typedef( StrC name, Code type, Opts_def_typedef p )
return result; 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 ); null_check( def_union, body );
@ -1405,12 +1410,12 @@ CodeUnion def_union( StrC name, Code body, Opts_def_union p )
return result; 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 ); name_check( def_using, name );
null_check( def_using, type ); null_check( def_using, type );
Code register_type = def_type( name ); CodeTypename register_type = def_type( name );
if ( ! register_type ) if ( ! register_type )
{ {
@ -1451,7 +1456,7 @@ CodeUsing def_using_namespace( StrC name )
return (CodeUsing) result; 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 ); name_check( def_variable, name );
null_check( def_variable, type ); null_check( def_variable, type );
@ -1474,9 +1479,9 @@ CodeVar def_variable( CodeTypename type, StrC name, Code value, Opts_def_variabl
return InvalidCode; 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; return InvalidCode;
} }
@ -1494,8 +1499,8 @@ CodeVar def_variable( CodeTypename type, StrC name, Code value, Opts_def_variabl
if ( p.specifiers ) if ( p.specifiers )
result->Specs = p.specifiers; result->Specs = p.specifiers;
if ( value ) if ( p.value )
result->Value = value; result->Value = p.value;
return result; return result;
} }
@ -1915,7 +1920,7 @@ CodeBody def_global_body( s32 num, ... )
{ {
case CT_Global_Body: case CT_Global_Body:
// result.body_append( entry.code_cast<CodeBody>() ) ; // result.body_append( entry.code_cast<CodeBody>() ) ;
body_append( result, cast(CodeBody, entry) ); body_append_body( result, cast(CodeBody, entry) );
continue; continue;
GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES
@ -1956,7 +1961,7 @@ CodeBody def_global_body( s32 num, Code* codes )
switch (entry->Type) switch (entry->Type)
{ {
case CT_Global_Body: case CT_Global_Body:
body_append(result, cast(CodeBody, entry) ); body_append_body(result, cast(CodeBody, entry) );
continue; continue;
GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES
@ -2090,8 +2095,8 @@ CodeParam def_params( s32 num, CodeParam* codes )
{ {
def_body_code_array_start( def_params ); def_body_code_array_start( def_params );
# define check_current() \ # define check_current(current) \
if ( current.ast == nullptr ) \ if ( current == nullptr ) \
{ \ { \
log_failure("gen::def_params: Provide a null code in codes array"); \ log_failure("gen::def_params: Provide a null code in codes array"); \
return InvalidCode; \ return InvalidCode; \
@ -2104,7 +2109,7 @@ CodeParam def_params( s32 num, CodeParam* codes )
} }
CodeParam current = (CodeParam)code_duplicate(* codes); CodeParam current = (CodeParam)code_duplicate(* codes);
check_current(); check_current(current);
CodeParam CodeParam
result = (CodeParam) make_code(); result = (CodeParam) make_code();
@ -2114,7 +2119,7 @@ CodeParam def_params( s32 num, CodeParam* codes )
while( codes++, current = * codes, num--, num > 0 ) while( codes++, current = * codes, num--, num > 0 )
{ {
check_current(); check_current(current);
params_append(result, current ); params_append(result, current );
} }
# undef check_current # undef check_current
@ -2293,7 +2298,7 @@ CodeBody def_union_body( s32 num, ... )
return result; 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 ); def_body_code_array_start( def_union_body );

View File

@ -134,7 +134,7 @@ bool __eat(TokArray* self, TokType type )
} }
internal internal
void init() void parser_init()
{ {
Tokens = array_init_reserve(Token, arena_allocator_info( & LexArena) Tokens = array_init_reserve(Token, arena_allocator_info( & LexArena)
, ( LexAllocator_Size - sizeof( ArrayHeader ) ) / sizeof(Token) , ( LexAllocator_Size - sizeof( ArrayHeader ) ) / sizeof(Token)
@ -145,7 +145,7 @@ void init()
} }
internal internal
void deinit() void parser_deinit()
{ {
parser::Tokens = { nullptr }; parser::Tokens = { nullptr };
} }
@ -780,10 +780,10 @@ Code parse_class_struct( TokType which, bool inplace_def = false )
} }
if ( which == Tok_Decl_Class ) 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 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 ) if ( inline_cmt )
result->InlineCmt = inline_cmt; result->InlineCmt = inline_cmt;

View File

@ -52,7 +52,7 @@
# define ccast( type, value ) ( (type)(value) ) # define ccast( type, value ) ( (type)(value) )
# endif # endif
# ifndef pcast # ifndef pcast
# define pcast( type, value ) ( * (type*)(value) ) # define pcast( type, value ) ( * (type*)(& value) )
# endif # endif
# ifndef rcast # ifndef rcast
# define rcast( type, value ) ( (type)(value) ) # define rcast( type, value ) ( (type)(value) )

View File

@ -9,6 +9,7 @@
# pragma clang diagnostic ignored "-Wunused-function" # pragma clang diagnostic ignored "-Wunused-function"
# pragma clang diagnostic ignored "-Wbraced-scalar-init" # pragma clang diagnostic ignored "-Wbraced-scalar-init"
# pragma clang diagnostic ignored "-W#pragma-messages" # pragma clang diagnostic ignored "-W#pragma-messages"
# pragma clang diagnostic ignored "-Wstatic-in-inline"
#endif #endif
#ifdef __GNUC__ #ifdef __GNUC__