From 76ac3a0f935af926cc8f34ad1fca9ed35e377fa3 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Fri, 13 Dec 2024 19:16:52 -0500 Subject: [PATCH] Introduced the general context struct for gencpp --- base/Readme.md | 2 +- base/auxillary/builder.cpp | 2 +- base/auxillary/scanner.cpp | 4 +- base/base.cpp | 5 +- base/components/ast.cpp | 40 +- base/components/ast.hpp | 6 +- base/components/ast_types.hpp | 136 ++-- base/components/code_serialization.cpp | 54 +- base/components/header_end.hpp | 73 +-- base/components/interface.cpp | 281 +++++---- base/components/interface.hpp | 92 ++- base/components/interface.parsing.cpp | 50 +- base/components/interface.untyped.cpp | 31 +- base/components/interface.upfront.cpp | 65 +- base/components/lexer.cpp | 14 +- base/components/parser.cpp | 597 +++++++++--------- base/components/static_data.cpp | 27 +- base/dependencies/macros.hpp | 3 +- base/dependencies/strings.hpp | 4 +- base/helpers/base_codegen.hpp | 16 +- base/helpers/misc.hpp | 8 +- docs/AST_Types.md | 68 +- docs/Readme.md | 6 +- gen_c_library/c_library.cpp | 40 +- gen_c_library/c_library.refactor | 10 +- gen_c_library/components/containers.array.hpp | 10 +- .../components/containers.hashtable.hpp | 18 +- gen_c_library/components/misc.hpp | 12 +- gen_segmented/segmented.cpp | 7 +- gen_unreal_engine/unreal.cpp | 2 +- 30 files changed, 831 insertions(+), 852 deletions(-) diff --git a/base/Readme.md b/base/Readme.md index 1175e1e..b7c840e 100644 --- a/base/Readme.md +++ b/base/Readme.md @@ -150,7 +150,7 @@ The convention you'll see used throughout the upfront interface of the library i 3. Populate immediate fields (Name, Type, ModuleFlags, etc) 4. Populate sub-entires using `add_entry`. If using the default serialization function `to_strbuilder`, follow the order at which entires are expected to appear (there is a strong ordering expected). -Names or Content fields are interned strings and thus showed be cached using `get_cached_string` if its desired to preserve that behavior. +Names or Content fields are interned strings and thus showed be cached using `cache_str` if its desired to preserve that behavior. `def_operator` is the most sophisticated upfront constructor as it has multiple permutations of definitions that could be created that are not trivial to determine if valid. diff --git a/base/auxillary/builder.cpp b/base/auxillary/builder.cpp index d7797f2..606ab33 100644 --- a/base/auxillary/builder.cpp +++ b/base/auxillary/builder.cpp @@ -15,7 +15,7 @@ Builder builder_open( char const* path ) return result; } - result.Buffer = strbuilder_make_reserve( GlobalAllocator, Builder_StrBufferReserve ); + result.Buffer = strbuilder_make_reserve( _ctx->Allocator_Temp, _ctx->InitSize_BuilderBuffer ); // log_fmt("$Builder - Opened file: %s\n", result.File.filename ); return result; diff --git a/base/auxillary/scanner.cpp b/base/auxillary/scanner.cpp index 0ac23a3..94f5e48 100644 --- a/base/auxillary/scanner.cpp +++ b/base/auxillary/scanner.cpp @@ -20,7 +20,7 @@ Code scan_file( char const* path ) GEN_FATAL("scan_file: %s is empty", path ); } - StrBuilder str = strbuilder_make_reserve( GlobalAllocator, fsize ); + StrBuilder str = strbuilder_make_reserve( _ctx->Allocator_Temp, fsize ); file_read( & file, str, fsize ); strbuilder_get_header(str)->Length = fsize; @@ -117,7 +117,7 @@ Code scan_file( char const* path ) } CodeBody parse_file( const char* path ) { - FileContents file = file_read_contents( GlobalAllocator, true, path ); + FileContents file = file_read_contents( _ctx->Allocator_Temp, true, path ); Str content = { (char const*)file.data, file.size }; CodeBody code = parse_global_body( content ); log_fmt("\nParsed: %s\n", path); diff --git a/base/base.cpp b/base/base.cpp index 8701ad4..2a7b475 100644 --- a/base/base.cpp +++ b/base/base.cpp @@ -27,7 +27,8 @@ constexpr char const* generation_notice = int gen_main() { - gen::init(); + gen::Context ctx {}; + gen::init( & ctx); CodeBody gen_component_header = def_global_body( args( def_preprocess_cond( PreprocessCond_IfDef, txt("GEN_INTELLISENSE_DIRECTIVES") ), @@ -69,6 +70,6 @@ int gen_main() builder_print( & header_ast_inlines, format(ast_inlines) ); builder_write( & header_ast_inlines); - gen::deinit(); + gen::deinit(& ctx); return 0; } diff --git a/base/components/ast.cpp b/base/components/ast.cpp index 75bd779..f1efe5d 100644 --- a/base/components/ast.cpp +++ b/base/components/ast.cpp @@ -10,7 +10,7 @@ global Code Code_Invalid; Str code_debug_str(Code self) { GEN_ASSERT(self != nullptr); - StrBuilder result_stack = strbuilder_make_reserve( GlobalAllocator, kilobytes(1) ); + StrBuilder result_stack = strbuilder_make_reserve( _ctx->Allocator_Temp, kilobytes(1) ); StrBuilder* result = & result_stack; if ( self->Parent ) @@ -372,7 +372,7 @@ Code code_duplicate(Code self) StrBuilder code_to_strbuilder(Code self) { - StrBuilder result = strbuilder_make_str( GlobalAllocator, txt("") ); + StrBuilder result = strbuilder_make_str( _ctx->Allocator_Temp, txt("") ); code_to_strbuilder_ptr( self, & result ); return result; } @@ -612,7 +612,7 @@ bool code_is_equal( Code self, Code other ) { log_fmt("AST::is_equal: Type check failure with other\nAST: %S\nOther: %S" , code_debug_str(self) - ,code_debug_str(other) + , code_debug_str(other) ); return false; @@ -646,23 +646,23 @@ bool code_is_equal( Code self, Code other ) return false; \ } - #define check_member_content( content ) \ - if ( ! str_are_equal( self->content, other->content )) \ - { \ - log_fmt("\nAST::is_equal: Member content - "#content " failed\n" \ - "AST : %S\n" \ - "Other: %S\n" \ - , code_debug_str(self) \ - , code_debug_str(other) \ - ); \ - \ - log_fmt("Content cannot be trusted to be unique with this check " \ - "so it must be verified by eye for now\n" \ - "AST Content:\n%S\n" \ - "Other Content:\n%S\n" \ - , str_visualize_whitespace(self->content, GlobalAllocator) \ - , str_visualize_whitespace(other->content, GlobalAllocator) \ - ); \ + #define check_member_content( content ) \ + if ( ! str_are_equal( self->content, other->content )) \ + { \ + log_fmt("\nAST::is_equal: Member content - "#content " failed\n" \ + "AST : %S\n" \ + "Other: %S\n" \ + , code_debug_str(self) \ + , code_debug_str(other) \ + ); \ + \ + log_fmt("Content cannot be trusted to be unique with this check " \ + "so it must be verified by eye for now\n" \ + "AST Content:\n%S\n" \ + "Other Content:\n%S\n" \ + , str_visualize_whitespace(self->content, _ctx->Allocator_Temp) \ + , str_visualize_whitespace(other->content, _ctx->Allocator_Temp) \ + ); \ } #define check_member_ast( ast ) \ diff --git a/base/components/ast.hpp b/base/components/ast.hpp index 03f94bc..9f39db4 100644 --- a/base/components/ast.hpp +++ b/base/components/ast.hpp @@ -368,7 +368,7 @@ int AST_ArrSpecs_Cap = ( AST_POD_Size - sizeof(Code) - - sizeof(StringCached) + - sizeof(StrCached) - sizeof(Code) * 2 - sizeof(Token*) - sizeof(Code) @@ -414,13 +414,13 @@ struct AST Code PostNameMacro; // Only used with parameters for specifically UE_REQUIRES (Thanks Unreal) }; }; - StringCached Content; // Attributes, Comment, Execution, Include + StrCached Content; // Attributes, Comment, Execution, Include struct { Specifier ArrSpecs[AST_ArrSpecs_Cap]; // Specifiers Code NextSpecs; // Specifiers; If ArrSpecs is full, then NextSpecs is used. }; }; - StringCached Name; + StrCached Name; union { Code Prev; Code Front; diff --git a/base/components/ast_types.hpp b/base/components/ast_types.hpp index 6385d3a..f11e72e 100644 --- a/base/components/ast_types.hpp +++ b/base/components/ast_types.hpp @@ -31,7 +31,7 @@ struct AST_Body union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; Code Front; Code Back; Token* Tok; @@ -46,9 +46,9 @@ struct AST_Attributes { union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; - StringCached Content; + StrCached Content; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -64,7 +64,7 @@ struct AST_BaseClass union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -79,9 +79,9 @@ struct AST_Comment { union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; - StringCached Content; + StrCached Content; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -106,7 +106,7 @@ struct AST_Class char _PAD_PROPERTIES_2_[ sizeof(AST*) ]; }; }; - StringCached Name; + StrCached Name; CodeTypename Prev; CodeTypename Next; Token* Tok; @@ -132,7 +132,7 @@ struct AST_Constructor char _PAD_PROPERTIES_2_ [ sizeof(AST*) * 2 ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -146,9 +146,9 @@ struct AST_Define { union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; - StringCached Content; + StrCached Content; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -172,7 +172,7 @@ struct AST_Destructor char _PAD_PROPERTIES_3_ [ sizeof(AST*) ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -197,7 +197,7 @@ struct AST_Enum char _PAD_PROPERTIES_2_[ sizeof(AST*) ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -212,9 +212,9 @@ struct AST_Exec { union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; - StringCached Content; + StrCached Content; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -230,7 +230,7 @@ struct AST_Expr union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -245,7 +245,7 @@ struct AST_Expr_Assign union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -260,7 +260,7 @@ struct AST_Expr_Alignof union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -275,7 +275,7 @@ struct AST_Expr_Binary union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -290,7 +290,7 @@ struct AST_Expr_CStyleCast union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -305,7 +305,7 @@ struct AST_Expr_FunctionalCast union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -320,7 +320,7 @@ struct AST_Expr_CppCast union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -335,7 +335,7 @@ struct AST_Expr_ProcCall union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -350,7 +350,7 @@ struct AST_Expr_Decltype union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -365,7 +365,7 @@ struct AST_Expr_Comma union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -380,7 +380,7 @@ struct AST_Expr_AMS union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -395,7 +395,7 @@ struct AST_Expr_Sizeof union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -410,7 +410,7 @@ struct AST_Expr_Subscript union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -425,7 +425,7 @@ struct AST_Expr_Ternary union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -440,7 +440,7 @@ struct AST_Expr_UnaryPrefix union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -455,7 +455,7 @@ struct AST_Expr_UnaryPostfix union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -470,7 +470,7 @@ struct AST_Expr_Element union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -492,7 +492,7 @@ struct AST_Extern char _PAD_PROPERTIES_2_[ sizeof(AST*) ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -506,9 +506,9 @@ struct AST_Include { union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; - StringCached Content; + StrCached Content; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -530,7 +530,7 @@ struct AST_Friend char _PAD_PROPERTIES_2_[ sizeof(AST*) ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -555,7 +555,7 @@ struct AST_Fn char _PAD_PROPERTIES_ [ sizeof(AST*) ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -571,7 +571,7 @@ struct AST_Module union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -592,7 +592,7 @@ struct AST_NS char _PAD_PROPERTIES_2_[ sizeof(AST*) ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -618,7 +618,7 @@ struct AST_Operator char _PAD_PROPERTIES_ [ sizeof(AST*) ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -644,7 +644,7 @@ struct AST_OpCast char _PAD_PROPERTIES_3_[ sizeof(AST*) ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -668,7 +668,7 @@ struct AST_Params // char _PAD_PROPERTIES_3_[sizeof( AST* )]; }; }; - StringCached Name; + StrCached Name; CodeParams Last; CodeParams Next; Token* Tok; @@ -683,9 +683,9 @@ struct AST_Pragma { union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; - StringCached Content; + StrCached Content; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -699,9 +699,9 @@ struct AST_PreprocessCond { union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; - StringCached Content; + StrCached Content; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -714,7 +714,7 @@ static_assert( sizeof(AST_PreprocessCond) == sizeof(AST), "ERROR: AST_Preprocess struct AST_Specifiers { Specifier ArrSpecs[ AST_ArrSpecs_Cap ]; - StringCached Name; + StrCached Name; CodeSpecifiers NextSpecs; Code Prev; Code Next; @@ -732,7 +732,7 @@ struct AST_Stmt union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -747,7 +747,7 @@ struct AST_Stmt_Break union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -762,7 +762,7 @@ struct AST_Stmt_Case union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -777,7 +777,7 @@ struct AST_Stmt_Continue union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -792,7 +792,7 @@ struct AST_Stmt_Decl union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -807,7 +807,7 @@ struct AST_Stmt_Do union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -822,7 +822,7 @@ struct AST_Stmt_Expr union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -837,7 +837,7 @@ struct AST_Stmt_Else union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -852,7 +852,7 @@ struct AST_Stmt_If union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -867,7 +867,7 @@ struct AST_Stmt_For union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -882,7 +882,7 @@ struct AST_Stmt_Goto union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -897,7 +897,7 @@ struct AST_Stmt_Label union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -912,7 +912,7 @@ struct AST_Stmt_Switch union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -927,7 +927,7 @@ struct AST_Stmt_While union { char _PAD_[ sizeof(Specifier) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; - StringCached Name; + StrCached Name; CodeExpr Prev; CodeExpr Next; Token* Tok; @@ -953,7 +953,7 @@ struct AST_Struct char _PAD_PROPERTIES_2_[ sizeof(AST*) ]; }; }; - StringCached Name; + StrCached Name; CodeTypename Prev; CodeTypename Next; Token* Tok; @@ -976,7 +976,7 @@ struct AST_Template char _PAD_PROPERTIES_2_[ sizeof(AST*) ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -1005,7 +1005,7 @@ struct AST_Type // CodeSpecifiers SpecsFuncSuffix; // Only used for function signatures }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -1032,7 +1032,7 @@ struct AST_Typename CodeSpecifiers SpecsFuncSuffix; // Only used for function signatures }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -1058,7 +1058,7 @@ struct AST_Typedef char _PAD_PROPERTIES_2_[ sizeof(AST*) * 3 ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -1082,7 +1082,7 @@ struct AST_Union char _PAD_PROPERTIES_2_[ sizeof(AST*) ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -1106,7 +1106,7 @@ struct AST_Using char _PAD_PROPERTIES_[ sizeof(AST*) * 3 ]; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; @@ -1132,7 +1132,7 @@ struct AST_Var CodeVar NextVar; }; }; - StringCached Name; + StrCached Name; Code Prev; Code Next; Token* Tok; diff --git a/base/components/code_serialization.cpp b/base/components/code_serialization.cpp index 2e8794d..7848137 100644 --- a/base/components/code_serialization.cpp +++ b/base/components/code_serialization.cpp @@ -6,7 +6,7 @@ inline StrBuilder attributes_to_strbuilder(CodeAttributes attributes) { GEN_ASSERT(attributes); - char* raw = ccast(char*, str_duplicate( attributes->Content, GlobalAllocator ).Ptr); + char* raw = ccast(char*, str_duplicate( attributes->Content, _ctx->Allocator_Temp ).Ptr); StrBuilder result = { raw }; return result; } @@ -21,7 +21,7 @@ void attributes_to_strbuilder_ref(CodeAttributes attributes, StrBuilder* result) StrBuilder body_to_strbuilder(CodeBody body) { GEN_ASSERT(body); - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 ); switch ( body->Type ) { case CT_Untyped: @@ -82,7 +82,7 @@ void body_to_strbuilder_export( CodeBody body, StrBuilder* result ) inline StrBuilder comment_to_strbuilder(CodeComment comment) { GEN_ASSERT(comment); - char* raw = ccast(char*, str_duplicate( comment->Content, GlobalAllocator ).Ptr); + char* raw = ccast(char*, str_duplicate( comment->Content, _ctx->Allocator_Temp ).Ptr); StrBuilder result = { raw }; return result; } @@ -96,7 +96,7 @@ void comment_to_strbuilder_ref(CodeComment comment, StrBuilder* result) { StrBuilder constructor_to_strbuilder(CodeConstructor self) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 ); switch (self->Type) { case CT_Constructor: @@ -159,7 +159,7 @@ void constructor_to_strbuilder_fwd(CodeConstructor self, StrBuilder* result ) StrBuilder class_to_strbuilder( CodeClass self ) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 ); switch ( self->Type ) { case CT_Class: @@ -241,7 +241,7 @@ void class_to_strbuilder_fwd( CodeClass self, StrBuilder* result ) StrBuilder define_to_strbuilder(CodeDefine define) { - return strbuilder_fmt_buf( GlobalAllocator, "#define %S %S", define->Name, define->Content ); + return strbuilder_fmt_buf( _ctx->Allocator_Temp, "#define %S %S", define->Name, define->Content ); } void define_to_strbuilder_ref(CodeDefine define, StrBuilder* result ) @@ -251,7 +251,7 @@ void define_to_strbuilder_ref(CodeDefine define, StrBuilder* result ) StrBuilder destructor_to_strbuilder(CodeDestructor self) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 ); switch ( self->Type ) { case CT_Destructor: @@ -308,7 +308,7 @@ void destructor_to_strbuilder_fwd(CodeDestructor self, StrBuilder* result ) StrBuilder enum_to_strbuilder(CodeEnum self) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 ); switch ( self->Type ) { case CT_Enum: @@ -443,7 +443,7 @@ void enum_to_strbuilder_class_fwd(CodeEnum self, StrBuilder* result ) StrBuilder exec_to_strbuilder(CodeExec exec) { GEN_ASSERT(exec); - char* raw = ccast(char*, str_duplicate( exec->Content, GlobalAllocator ).Ptr); + char* raw = ccast(char*, str_duplicate( exec->Content, _ctx->Allocator_Temp ).Ptr); StrBuilder result = { raw }; return result; } @@ -458,7 +458,7 @@ void extern_to_strbuilder(CodeExtern self, StrBuilder* result ) StrBuilder include_to_strbuilder(CodeInclude include) { - return strbuilder_fmt_buf( GlobalAllocator, "#include %S\n", include->Content ); + return strbuilder_fmt_buf( _ctx->Allocator_Temp, "#include %S\n", include->Content ); } void include_to_strbuilder_ref( CodeInclude include, StrBuilder* result ) @@ -468,7 +468,7 @@ void include_to_strbuilder_ref( CodeInclude include, StrBuilder* result ) StrBuilder friend_to_strbuilder(CodeFriend self) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 256 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 256 ); friend_to_strbuilder_ref( self, & result ); return result; } @@ -490,7 +490,7 @@ void friend_to_strbuilder_ref(CodeFriend self, StrBuilder* result ) StrBuilder fn_to_strbuilder(CodeFn self) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 ); switch ( self->Type ) { case CT_Function: @@ -621,7 +621,7 @@ void fn_to_strbuilder_fwd(CodeFn self, StrBuilder* result ) StrBuilder module_to_strbuilder(CodeModule self) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 64 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 64 ); module_to_strbuilder_ref( self, & result ); return result; } @@ -639,7 +639,7 @@ void module_to_strbuilder_ref(CodeModule self, StrBuilder* result ) StrBuilder namespace_to_strbuilder(CodeNS self) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 ); namespace_to_strbuilder_ref( self, & result ); return result; } @@ -654,7 +654,7 @@ void namespace_to_strbuilder_ref(CodeNS self, StrBuilder* result ) StrBuilder code_op_to_strbuilder(CodeOperator self) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 ); switch ( self->Type ) { case CT_Operator: @@ -776,7 +776,7 @@ void code_op_to_strbuilder_fwd(CodeOperator self, StrBuilder* result ) StrBuilder opcast_to_strbuilder(CodeOpCast self) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 ); switch ( self->Type ) { case CT_Operator_Cast: @@ -867,7 +867,7 @@ StrBuilder params_to_strbuilder(CodeParams self) { GEN_ASSERT(self); GEN_ASSERT(self); - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 ); params_to_strbuilder_ref( self, & result ); return result; } @@ -914,7 +914,7 @@ void params_to_strbuilder_ref( CodeParams self, StrBuilder* result ) StrBuilder preprocess_to_strbuilder(CodePreprocessCond self) { GEN_ASSERT(self); - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 256 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 256 ); switch ( self->Type ) { case CT_Preprocess_If: @@ -978,7 +978,7 @@ void preprocess_to_strbuilder_endif(CodePreprocessCond cond, StrBuilder* result StrBuilder pragma_to_strbuilder(CodePragma self) { GEN_ASSERT(self); - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 256 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 256 ); pragma_to_strbuilder_ref( self, & result ); return result; } @@ -990,7 +990,7 @@ void pragma_to_strbuilder_ref(CodePragma self, StrBuilder* result ) StrBuilder specifiers_to_strbuilder(CodeSpecifiers self) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 64 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 64 ); specifiers_to_strbuilder_ref( self, & result ); return result; } @@ -1013,7 +1013,7 @@ StrBuilder struct_to_strbuilder(CodeStruct self) { GEN_ASSERT(self); GEN_ASSERT(self); - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 ); switch ( self->Type ) { case CT_Struct: @@ -1096,7 +1096,7 @@ void struct_to_strbuilder_fwd( CodeStruct self, StrBuilder* result ) StrBuilder template_to_strbuilder(CodeTemplate self) { GEN_ASSERT(self); - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 1024 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 1024 ); template_to_strbuilder_ref( self, & result ); return result; } @@ -1116,7 +1116,7 @@ void template_to_strbuilder_ref(CodeTemplate self, StrBuilder* result ) StrBuilder typedef_to_strbuilder(CodeTypedef self) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 ); typedef_to_strbuilder_ref( self, & result ); return result; } @@ -1158,7 +1158,7 @@ void typedef_to_strbuilder_ref(CodeTypedef self, StrBuilder* result ) StrBuilder typename_to_strbuilder(CodeTypename self) { - StrBuilder result = strbuilder_make_str( GlobalAllocator, txt("") ); + StrBuilder result = strbuilder_make_str( _ctx->Allocator_Temp, txt("") ); typename_to_strbuilder_ref( self, & result ); return result; } @@ -1221,7 +1221,7 @@ void typename_to_strbuilder_ref(CodeTypename self, StrBuilder* result ) StrBuilder union_to_strbuilder(CodeUnion self) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 512 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 512 ); switch ( self->Type ) { case CT_Union: @@ -1287,7 +1287,7 @@ void union_to_strbuilder_fwd(CodeUnion self, StrBuilder* result ) StrBuilder using_to_strbuilder(CodeUsing self) { GEN_ASSERT(self); - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 128 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 128 ); switch ( self->Type ) { case CT_Using: @@ -1352,7 +1352,7 @@ inline StrBuilder var_to_strbuilder(CodeVar self) { GEN_ASSERT(self); - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, 256 ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, 256 ); var_to_strbuilder_ref( self, & result ); return result; } diff --git a/base/components/header_end.hpp b/base/components/header_end.hpp index c20b910..703c1d9 100644 --- a/base/components/header_end.hpp +++ b/base/components/header_end.hpp @@ -6,53 +6,6 @@ #pragma region Constants -#ifndef GEN_GLOBAL_BUCKET_SIZE -# define GEN_GLOBAL_BUCKET_SIZE megabytes(8) -#endif -#ifndef GEN_CODEPOOL_NUM_BLOCKS -# define GEN_CODEPOOL_NUM_BLOCKS kilobytes(16) -#endif -#ifndef GEN_SIZE_PER_STRING_ARENA -# define GEN_SIZE_PER_STRING_ARENA megabytes(1) -#endif -#ifndef GEN_MAX_COMMENT_LINE_LENGTH -# define GEN_MAX_COMMENT_LINE_LENGTH 1024 -#endif -#ifndef GEN_MAX_NAME_LENGTH -# define GEN_MAX_NAME_LENGTH 128 -#endif -#ifndef GEN_MAX_UNTYPED_STR_LENGTH -# define GEN_MAX_UNTYPED_STR_LENGTH megabytes(1) -#endif -#ifndef TokenMap_FixedArena -# define TokenMap_FixedArena FixedArena_8KB -#endif -#ifndef GEN_LEX_ALLOCATOR_SIZE -# define GEN_LEX_ALLOCATOR_SIZE megabytes(4) -#endif -#ifndef GEN_BUILDER_STR_BUFFER_RESERVE -# define GEN_BUILDER_STR_BUFFER_RESERVE megabytes(2) -#endif - -// These constexprs are used for allocation behavior of data structures -// or string handling while constructing or serializing. -// Change them to suit your needs. - -constexpr s32 InitSize_DataArrays = 16; - -// NOTE: This limits the maximum size of an allocation -// If you are generating a string larger than this, increase the size of the bucket here. -constexpr usize Global_BucketSize = GEN_GLOBAL_BUCKET_SIZE; -constexpr s32 CodePool_NumBlocks = GEN_CODEPOOL_NUM_BLOCKS; -constexpr s32 SizePer_StringArena = GEN_SIZE_PER_STRING_ARENA; - -constexpr s32 MaxCommentLineLength = GEN_MAX_COMMENT_LINE_LENGTH; -constexpr s32 MaxNameLength = GEN_MAX_NAME_LENGTH; -constexpr s32 MaxUntypedStrLength = GEN_MAX_UNTYPED_STR_LENGTH; -// constexpr s32 TokenFmt_TokenMap_MemSize = GEN_TOKEN_FMT_TOKEN_MAP_MEM_SIZE; -constexpr s32 LexAllocator_Size = GEN_LEX_ALLOCATOR_SIZE; -constexpr s32 Builder_StrBufferReserve = GEN_BUILDER_STR_BUFFER_RESERVE; - extern Str enum_underlying_sig; extern Code access_public; @@ -111,6 +64,7 @@ extern CodeTypename t_typename; #ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS // Predefined typename codes. Are set to readonly and are setup during gen::init() + extern Context* _ctx; extern CodeTypename t_b32; @@ -132,28 +86,3 @@ extern CodeTypename t_typename; #endif #pragma endregion Constants - -// Used by the lexer to persistently treat all these identifiers as preprocessor defines. -// Populate with strings via gen::get_cached_string. -// Functional defines must have format: id( ;at minimum to indicate that the define is only valid with arguments. -extern Array(StringCached) PreprocessorDefines; - -#ifdef GEN_EXPOSE_BACKEND - // Global allocator used for data with process lifetime. - extern AllocatorInfo GlobalAllocator; - extern Array(Arena) Global_AllocatorBuckets; - - extern Array(Pool) CodePools; - extern Array(Arena) StringArenas; - - extern StringTable StringCache; - - extern Arena LexArena; - - extern AllocatorInfo Allocator_DataArrays; - extern AllocatorInfo Allocator_CodePool; - extern AllocatorInfo Allocator_Lexer; - extern AllocatorInfo Allocator_StringArena; - extern AllocatorInfo Allocator_StringTable; - extern AllocatorInfo Allocator_TypeTable; -#endif diff --git a/base/components/interface.cpp b/base/components/interface.cpp index 960e1c7..7d2ebf3 100644 --- a/base/components/interface.cpp +++ b/base/components/interface.cpp @@ -9,9 +9,11 @@ internal void parser_deinit(); GEN_NS_PARSER_END internal -void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) +void* fallback_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) { - Arena* last = array_back(Global_AllocatorBuckets); + GEN_ASSERT(_ctx); + GEN_ASSERT(_ctx->Fallback_AllocatorBuckets); + Arena* last = array_back(_ctx->Fallback_AllocatorBuckets); switch ( type ) { @@ -19,15 +21,15 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s { if ( ( last->TotalUsed + size ) > last->TotalSize ) { - Arena bucket = arena_init_from_allocator( heap(), Global_BucketSize ); + Arena bucket = arena_init_from_allocator( heap(), _ctx->InitSize_Fallback_Allocator_Bucket_Size ); if ( bucket.PhysicalStart == nullptr ) - GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets"); + GEN_FATAL( "Failed to create bucket for Fallback_AllocatorBuckets"); - if ( ! array_append( Global_AllocatorBuckets, bucket ) ) - GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets"); + if ( ! array_append( _ctx->Fallback_AllocatorBuckets, bucket ) ) + GEN_FATAL( "Failed to append bucket to Fallback_AllocatorBuckets"); - last = array_back(Global_AllocatorBuckets); + last = array_back(_ctx->Fallback_AllocatorBuckets); } return alloc_align( arena_allocator_info(last), size, alignment ); @@ -46,15 +48,15 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s { if ( last->TotalUsed + size > last->TotalSize ) { - Arena bucket = arena_init_from_allocator( heap(), Global_BucketSize ); + Arena bucket = arena_init_from_allocator( heap(), _ctx->InitSize_Fallback_Allocator_Bucket_Size ); if ( bucket.PhysicalStart == nullptr ) - GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets"); + GEN_FATAL( "Failed to create bucket for Fallback_AllocatorBuckets"); - if ( ! array_append( Global_AllocatorBuckets, bucket ) ) - GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets"); + if ( ! array_append( _ctx->Fallback_AllocatorBuckets, bucket ) ) + GEN_FATAL( "Failed to append bucket to Fallback_AllocatorBuckets"); - last = array_back(Global_AllocatorBuckets); + last = array_back( _ctx->Fallback_AllocatorBuckets); } void* result = alloc_align( last->Backing, size, alignment ); @@ -75,7 +77,7 @@ internal void define_constants() { Code_Global = make_code(); - Code_Global->Name = get_cached_string( txt("Global Code") ); + Code_Global->Name = cache_str( txt("Global Code") ); Code_Global->Content = Code_Global->Name; Code_Invalid = make_code(); @@ -83,22 +85,22 @@ void define_constants() t_empty = (CodeTypename) make_code(); t_empty->Type = CT_Typename; - t_empty->Name = get_cached_string( txt("") ); + t_empty->Name = cache_str( txt("") ); code_set_global(cast(Code, t_empty)); access_private = make_code(); access_private->Type = CT_Access_Private; - access_private->Name = get_cached_string( txt("private:\n") ); + access_private->Name = cache_str( txt("private:\n") ); code_set_global(cast(Code, access_private)); access_protected = make_code(); access_protected->Type = CT_Access_Protected; - access_protected->Name = get_cached_string( txt("protected:\n") ); + access_protected->Name = cache_str( txt("protected:\n") ); code_set_global(access_protected); access_public = make_code(); access_public->Type = CT_Access_Public; - access_public->Name = get_cached_string( txt("public:\n") ); + access_public->Name = cache_str( txt("public:\n") ); code_set_global(access_public); Str api_export_str = code(GEN_API_Export_Code); @@ -111,13 +113,13 @@ void define_constants() module_global_fragment = make_code(); module_global_fragment->Type = CT_Untyped; - module_global_fragment->Name = get_cached_string( txt("module;") ); + module_global_fragment->Name = cache_str( txt("module;") ); module_global_fragment->Content = module_global_fragment->Name; code_set_global(cast(Code, module_global_fragment)); module_private_fragment = make_code(); module_private_fragment->Type = CT_Untyped; - module_private_fragment->Name = get_cached_string( txt("module : private;") ); + module_private_fragment->Name = cache_str( txt("module : private;") ); module_private_fragment->Content = module_private_fragment->Name; code_set_global(cast(Code, module_private_fragment)); @@ -127,13 +129,13 @@ void define_constants() pragma_once = (CodePragma) make_code(); pragma_once->Type = CT_Preprocess_Pragma; - pragma_once->Name = get_cached_string( txt("once") ); + pragma_once->Name = cache_str( txt("once") ); pragma_once->Content = pragma_once->Name; code_set_global((Code)pragma_once); param_varadic = (CodeParams) make_code(); param_varadic->Type = CT_Parameters; - param_varadic->Name = get_cached_string( txt("...") ); + param_varadic->Name = cache_str( txt("...") ); param_varadic->ValueType = t_empty; code_set_global((Code)param_varadic); @@ -205,221 +207,249 @@ void define_constants() if (enum_underlying_sig.Len == 0) { enum_underlying_sig = txt("enum_underlying("); } - array_append(PreprocessorDefines, enum_underlying_sig); + array_append( _ctx->PreprocessorDefines, enum_underlying_sig); # undef def_constant_spec } -void init() +void init(Context* ctx) { - // Setup global allocator + AllocatorInfo fallback_allocator = { & fallback_allocator_proc, nullptr }; + + b32 using_fallback_allocator = false; + if (ctx->Allocator_DyanmicContainers.Proc == nullptr) { + ctx->Allocator_DyanmicContainers = fallback_allocator; + using_fallback_allocator = true; + } + if (ctx->Allocator_Pool.Proc == nullptr ) { + ctx->Allocator_Pool = fallback_allocator; + using_fallback_allocator = true; + } + if (ctx->Allocator_StrCache.Proc == nullptr) { + ctx->Allocator_StrCache = fallback_allocator; + using_fallback_allocator = true; + } + if (ctx->Allocator_Temp.Proc == nullptr) { + ctx->Allocator_Temp = fallback_allocator; + using_fallback_allocator = true; + } + // Setup fallback allocator + if (using_fallback_allocator) { - AllocatorInfo becasue_C = { & Global_Allocator_Proc, nullptr }; - GlobalAllocator = becasue_C; - - Global_AllocatorBuckets = array_init_reserve(Arena, heap(), 128 ); - - if ( Global_AllocatorBuckets == nullptr ) - GEN_FATAL( "Failed to reserve memory for Global_AllocatorBuckets"); - - Arena bucket = arena_init_from_allocator( heap(), Global_BucketSize ); + ctx->Fallback_AllocatorBuckets = array_init_reserve(Arena, heap(), 128 ); + if ( ctx->Fallback_AllocatorBuckets == nullptr ) + GEN_FATAL( "Failed to reserve memory for Fallback_AllocatorBuckets"); + Arena bucket = arena_init_from_allocator( heap(), ctx->InitSize_Fallback_Allocator_Bucket_Size ); if ( bucket.PhysicalStart == nullptr ) - GEN_FATAL( "Failed to create first bucket for Global_AllocatorBuckets"); + GEN_FATAL( "Failed to create first bucket for Fallback_AllocatorBuckets"); - array_append( Global_AllocatorBuckets, bucket ); + array_append( ctx->Fallback_AllocatorBuckets, bucket ); } - if (Allocator_DataArrays.Proc == nullptr) { - Allocator_DataArrays = GlobalAllocator; + if (ctx->Max_CommentLineLength == 0) { + ctx->Max_CommentLineLength = 1024; } - if (Allocator_CodePool.Proc == nullptr ) { - Allocator_CodePool = GlobalAllocator; + if (ctx->Max_StrCacheLength == 0) { + ctx->Max_StrCacheLength = kilobytes(512); } - if (Allocator_Lexer.Proc == nullptr) { - Allocator_Lexer = GlobalAllocator; + + if (ctx->InitSize_BuilderBuffer == 0) { + ctx->InitSize_BuilderBuffer = megabytes(2); } - if (Allocator_StringArena.Proc == nullptr) { - Allocator_StringArena = GlobalAllocator; + if (ctx->InitSize_CodePoolsArray == 0) { + ctx->InitSize_CodePoolsArray = 16; } - if (Allocator_StringTable.Proc == nullptr) { - Allocator_StringTable = GlobalAllocator; + if (ctx->InitSize_StringArenasArray == 0) { + ctx->InitSize_StringArenasArray = 16; } - if (Allocator_TypeTable.Proc == nullptr) { - Allocator_TypeTable = GlobalAllocator; + if (ctx->CodePool_NumBlocks == 0) { + ctx->CodePool_NumBlocks = kilobytes(16); } + if (ctx->InitSize_LexArena == 0 ) { + ctx->InitSize_LexArena = megabytes(4); + } + if (ctx->SizePer_StringArena == 0) { + ctx->SizePer_StringArena = megabytes(1); + } + + if (ctx->InitSize_Fallback_Allocator_Bucket_Size == 0) { + ctx->InitSize_Fallback_Allocator_Bucket_Size = megabytes(8); + } + + // Override the current context (user has to put it back if unwanted). + _ctx = ctx; + // Setup the arrays { - CodePools = array_init_reserve(Pool, Allocator_DataArrays, InitSize_DataArrays ); - - if ( CodePools == nullptr ) + ctx->CodePools = array_init_reserve(Pool, ctx->Allocator_DyanmicContainers, ctx->InitSize_CodePoolsArray ); + if ( ctx->CodePools == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the CodePools array" ); - StringArenas = array_init_reserve(Arena, Allocator_DataArrays, InitSize_DataArrays ); - - if ( StringArenas == nullptr ) + ctx->StringArenas = array_init_reserve(Arena, ctx->Allocator_DyanmicContainers, ctx->InitSize_StringArenasArray ); + if ( ctx->StringArenas == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the StringArenas array" ); } - // Setup the code pool and code entries arena. { - Pool code_pool = pool_init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) ); - + Pool code_pool = pool_init( ctx->Allocator_Pool, ctx->CodePool_NumBlocks, sizeof(AST) ); if ( code_pool.PhysicalStart == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the code pool" ); + array_append( ctx->CodePools, code_pool ); - array_append( CodePools, code_pool ); - - LexArena = arena_init_from_allocator( Allocator_Lexer, LexAllocator_Size ); - - Arena strbuilder_arena = arena_init_from_allocator( Allocator_StringArena, SizePer_StringArena ); + // TODO(Ed): This is going to be phased out most likely. + ctx->LexArena = arena_init_from_allocator( ctx->Allocator_DyanmicContainers, ctx->InitSize_LexArena ); + // TODO(Ed): Eventually the string arenas needs to be phased out for a dedicated string slab allocator + Arena strbuilder_arena = arena_init_from_allocator( ctx->Allocator_StrCache, ctx->SizePer_StringArena ); if ( strbuilder_arena.PhysicalStart == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the string arena" ); - - array_append( StringArenas, strbuilder_arena ); + array_append( ctx->StringArenas, strbuilder_arena ); } // Setup the hash tables { - StringCache = hashtable_init(StringCached, Allocator_StringTable); - - if ( StringCache.Entries == nullptr ) + ctx->StrCache = hashtable_init(StrCached, ctx->Allocator_DyanmicContainers); + if ( ctx->StrCache.Entries == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the StringCache"); } // Preprocessor Defines - PreprocessorDefines = array_init_reserve(StringCached, GlobalAllocator, kilobytes(1) ); + ctx->PreprocessorDefines = array_init_reserve(StrCached, ctx->Allocator_DyanmicContainers, kilobytes(1) ); define_constants(); GEN_NS_PARSER parser_init(); } -void deinit() +void deinit(Context* ctx) { usize index = 0; - usize left = array_num(CodePools); + usize left = array_num(ctx->CodePools); do { - Pool* code_pool = & CodePools[index]; + Pool* code_pool = & ctx->CodePools[index]; pool_free(code_pool); index++; } while ( left--, left ); index = 0; - left = array_num(StringArenas); + left = array_num(ctx->StringArenas); do { - Arena* strbuilder_arena = & StringArenas[index]; + Arena* strbuilder_arena = & ctx->StringArenas[index]; arena_free(strbuilder_arena); index++; } while ( left--, left ); - hashtable_destroy(StringCache); + hashtable_destroy(ctx->StrCache); - array_free( CodePools); - array_free( StringArenas); + array_free( ctx->CodePools); + array_free( ctx->StringArenas); - arena_free(& LexArena); + arena_free(& ctx->LexArena); - array_free(PreprocessorDefines); + array_free(ctx->PreprocessorDefines); index = 0; - left = array_num(Global_AllocatorBuckets); + left = array_num( ctx->Fallback_AllocatorBuckets); do { - Arena* bucket = & Global_AllocatorBuckets[ index ]; + Arena* bucket = & ctx->Fallback_AllocatorBuckets[ index ]; arena_free(bucket); index++; } while ( left--, left ); - array_free(Global_AllocatorBuckets); + array_free( ctx->Fallback_AllocatorBuckets); GEN_NS_PARSER parser_deinit(); + + if (_ctx == ctx) + _ctx = nullptr; } -void reset() +void reset(Context* ctx) { s32 index = 0; - s32 left = array_num(CodePools); + s32 left = array_num(ctx->CodePools); do { - Pool* code_pool = & CodePools[index]; + Pool* code_pool = & ctx->CodePools[index]; pool_clear(code_pool); index++; } while ( left--, left ); index = 0; - left = array_num(StringArenas); + left = array_num(ctx->StringArenas); do { - Arena* strbuilder_arena = & StringArenas[index]; + Arena* strbuilder_arena = & ctx->StringArenas[index]; strbuilder_arena->TotalUsed = 0;; index++; } while ( left--, left ); - hashtable_clear(StringCache); - + hashtable_clear(ctx->StrCache); define_constants(); } -AllocatorInfo get_strbuilder_allocator( s32 c_str_length ) +void set_context(Context* new_ctx) { + GEN_ASSERT(new_ctx); + _ctx = new_ctx; +} + +AllocatorInfo get_cached_str_allocator( s32 str_length ) { - Arena* last = array_back(StringArenas); - - usize size_req = c_str_length + sizeof(StrBuilderHeader) + sizeof(char*); - + Arena* last = array_back(_ctx->StringArenas); + usize size_req = str_length + sizeof(StrBuilderHeader) + sizeof(char*); if ( last->TotalUsed + scast(ssize, size_req) > last->TotalSize ) { - Arena new_arena = arena_init_from_allocator( Allocator_StringArena, SizePer_StringArena ); + Arena new_arena = arena_init_from_allocator( _ctx->Allocator_StrCache, _ctx->SizePer_StringArena ); + if ( ! array_append( _ctx->StringArenas, new_arena ) ) + GEN_FATAL( "gen::get_cached_str_allocator: Failed to allocate a new string arena" ); - if ( ! array_append( StringArenas, new_arena ) ) - GEN_FATAL( "gen::get_strbuilder_allocator: Failed to allocate a new string arena" ); - - last = array_back(StringArenas); + last = array_back( _ctx->StringArenas); } return arena_allocator_info(last); } // Will either make or retrive a code string. -StringCached get_cached_string( Str str ) +StrCached cache_str( Str str ) { - s32 hash_length = str.Len > kilobytes(1) ? kilobytes(1) : str.Len; - u64 key = crc32( str.Ptr, hash_length ); - { - StringCached* result = hashtable_get(StringCache, key ); - + if (str.Len > _ctx->Max_StrCacheLength) { + // Do not cache the string, just shove into the arena and and return it. + Str result = strbuilder_to_str( strbuilder_make_str( get_cached_str_allocator( str.Len ), str )); + return result; + } + u64 key = crc32( str.Ptr, str.Len ); { + StrCached* result = hashtable_get( _ctx->StrCache, key ); if ( result ) return * result; } - - Str result = strbuilder_to_str( strbuilder_make_str( get_strbuilder_allocator( str.Len ), str )); - hashtable_set(StringCache, key, result ); - + Str result = strbuilder_to_str( strbuilder_make_str( get_cached_str_allocator( str.Len ), str )); + hashtable_set( _ctx->StrCache, key, result ); return result; } // Used internally to retireve a Code object form the CodePool. Code make_code() { - Pool* allocator = array_back( CodePools); + Pool* allocator = array_back( _ctx->CodePools); if ( allocator->FreeList == nullptr ) { - Pool code_pool = pool_init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) ); + Pool code_pool = pool_init( _ctx->Allocator_Pool, _ctx->CodePool_NumBlocks, sizeof(AST) ); if ( code_pool.PhysicalStart == nullptr ) GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePool allcoator returned nullptr." ); - if ( ! array_append( CodePools, code_pool ) ) + if ( ! array_append( _ctx->CodePools, code_pool ) ) GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePools failed to append new pool." ); - allocator = array_back( CodePools); + allocator = array_back( _ctx->CodePools); } Code result = { rcast( AST*, alloc( pool_allocator_info(allocator), sizeof(AST) )) }; @@ -427,27 +457,10 @@ Code make_code() return result; } -void set_allocator_data_arrays( AllocatorInfo allocator ) -{ - Allocator_DataArrays = allocator; -} - -void set_allocator_code_pool( AllocatorInfo allocator ) -{ - Allocator_CodePool = allocator; -} - -void set_allocator_lexer( AllocatorInfo allocator ) -{ - Allocator_Lexer = allocator; -} - -void set_allocator_strbuilder_arena( AllocatorInfo allocator ) -{ - Allocator_StringArena = allocator; -} - -void set_allocator_strbuilder_table( AllocatorInfo allocator ) -{ - Allocator_StringArena = allocator; +void set_preprocess_define( Str id, b32 is_functional ) { + StrBuilder builder = strbuilder_make_str( _ctx->Allocator_Temp, id ); + if (is_functional) { + strbuilder_append_char( & builder, '(' ); + } + array_append( _ctx->PreprocessorDefines, cache_str(builder) ); } diff --git a/base/components/interface.hpp b/base/components/interface.hpp index 5f7b590..4aacd76 100644 --- a/base/components/interface.hpp +++ b/base/components/interface.hpp @@ -15,20 +15,97 @@ \▓▓▓▓▓▓ \▓▓▓▓▓▓▓\▓▓ \▓▓ \▓▓▓▓▓▓\▓▓ \▓▓ \▓▓▓▓ \▓▓▓▓▓▓▓\▓▓ \▓▓ \▓▓▓▓▓▓▓ \▓▓▓▓▓▓▓ \▓▓▓▓▓▓▓ */ +#if 0 +enum LogLevel : u32 +{ + Info, + Warning, + Panic, +}; + +struct LogEntry +{ + Str msg; + u32 line_num; + void* data; +}; + +typedef void LoggerCallback(LogEntry entry); +#endif + +// Note(Ed): This is subject to heavily change +// with upcoming changes to the library's fallback (default) allocations strategy; +// and major changes to lexer/parser context usage. +struct Context +{ +// User Configuration + +// Persistent Data Allocation + AllocatorInfo Allocator_DyanmicContainers; // By default will use a genral slab allocator (TODO(Ed): Currently does not) + AllocatorInfo Allocator_Pool; // By default will use the growing vmem reserve (TODO(Ed): Currently does not) + AllocatorInfo Allocator_StrCache; // By default will use a dedicated slab allocator (TODO(Ed): Currently does not) + +// Temporary Allocation + AllocatorInfo Allocator_Temp; + + // LoggerCallaback* log_callback; // TODO(Ed): Impl user logger callback as an option. + +// + u32 Max_CommentLineLength; // Used by def_comment + u32 Max_StrCacheLength; // Any cached string longer than this is always allocated again. + + u32 InitSize_BuilderBuffer; + u32 InitSize_CodePoolsArray; + u32 InitSize_StringArenasArray; + + u32 CodePool_NumBlocks; + + // TODO(Ed): Review these... (No longer needed if using the proper allocation strategy) + u32 InitSize_LexArena; + u32 SizePer_StringArena; + +// Parser + + // Used by the lexer to persistently treat all these identifiers as preprocessor defines. + // Populate with strings via gen::cache_str. + // Functional defines must have format: id( ;at minimum to indicate that the define is only valid with arguments. + Array(StrCached) PreprocessorDefines; + +// Backend + // The fallback allocator is utilized if any fo the three above allocators is not specified by the user. + u32 InitSize_Fallback_Allocator_Bucket_Size; + Array(Arena) Fallback_AllocatorBuckets; + + // Array(Token) LexerTokens; + + Array(Pool) CodePools; + Array(Arena) StringArenas; + + Arena LexArena; + + StringTable StrCache; +}; + // Initialize the library. -void init(); +void init(Context* ctx); // Currently manually free's the arenas, code for checking for leaks. // However on Windows at least, it doesn't need to occur as the OS will clean up after the process. -void deinit(); +void deinit(Context* ctx); // Clears the allocations, but doesn't return to the heap, the calls init() again. // Ease of use. -void reset(); +void reset(Context* ctx); + +void set_context(Context* ctx); + +// Alternative way to add a preprocess define entry for the lexer & parser to utilize +// if the user doesn't want to use def_define +void set_preprocess_define( Str id, b32 is_functional ); // Used internally to retrive or make string allocations. // Strings are stored in a series of string arenas of fixed size (SizePer_StringArena) -StringCached get_cached_string( Str str ); +StrCached cache_str( Str str ); /* This provides a fresh Code AST. @@ -39,13 +116,6 @@ Code make_code(); // Set these before calling gen's init() procedure. -void set_allocator_data_arrays ( AllocatorInfo data_array_allocator ); -void set_allocator_code_pool ( AllocatorInfo pool_allocator ); -void set_allocator_lexer ( AllocatorInfo lex_allocator ); -void set_allocator_strbuilder_arena( AllocatorInfo strbuilder_allocator ); -void set_allocator_strbuilder_table( AllocatorInfo strbuilder_allocator ); -void set_allocator_type_table ( AllocatorInfo type_reg_allocator ); - #pragma region Upfront CodeAttributes def_attributes( Str content ); diff --git a/base/components/interface.parsing.cpp b/base/components/interface.parsing.cpp index e8fc53e..76dd9eb 100644 --- a/base/components/interface.parsing.cpp +++ b/base/components/interface.parsing.cpp @@ -17,10 +17,10 @@ CodeClass parse_class( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; push_scope(); CodeClass result = (CodeClass) parse_class_struct( Tok_Decl_Class, parser_not_inplace_def ); - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } @@ -59,8 +59,8 @@ CodeConstructor parse_constructor( Str def ) break; default : - log_failure( "Invalid specifier %s for variable\n%S", spec_to_str( spec ), parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Invalid specifier %s for variable\n%S", spec_to_str( spec ), parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } @@ -79,7 +79,7 @@ CodeConstructor parse_constructor( Str def ) // ... } - Context.Tokens = toks; + parser_ctx.Tokens = toks; CodeConstructor result = parser_parse_constructor( specifiers ); return result; } @@ -96,7 +96,7 @@ CodeDestructor parse_destructor( Str def ) // TODO(Ed): Destructors can have prefix attributes // TODO(Ed): Destructors can have virtual - Context.Tokens = toks; + parser_ctx.Tokens = toks; CodeDestructor result = parser_parse_destructor(NullCode); return result; } @@ -109,11 +109,11 @@ CodeEnum parse_enum( Str def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) { - parser_pop(& Context); + parser_pop(& parser_ctx); return InvalidCode; } - Context.Tokens = toks; + parser_ctx.Tokens = toks; return parser_parse_enum( parser_not_inplace_def); } @@ -126,7 +126,7 @@ CodeBody parse_export_body( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return parser_parse_export_body(); } @@ -139,7 +139,7 @@ CodeExtern parse_extern_link( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return parser_parse_extern_link(); } @@ -152,7 +152,7 @@ CodeFriend parse_friend( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return parser_parse_friend(); } @@ -165,7 +165,7 @@ CodeFn parse_function( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return (CodeFn) parser_parse_function(); } @@ -178,10 +178,10 @@ CodeBody parse_global_body( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; push_scope(); CodeBody result = parse_global_nspace( CT_Global_Body ); - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } @@ -194,7 +194,7 @@ CodeNS parse_namespace( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return parser_parse_namespace(); } @@ -207,7 +207,7 @@ CodeOperator parse_operator( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return (CodeOperator) parser_parse_operator(); } @@ -220,7 +220,7 @@ CodeOpCast parse_operator_cast( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return parser_parse_operator_cast(NullCode); } @@ -233,10 +233,10 @@ CodeStruct parse_struct( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; push_scope(); CodeStruct result = (CodeStruct) parse_class_struct( Tok_Decl_Struct, parser_not_inplace_def ); - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } @@ -249,7 +249,7 @@ CodeTemplate parse_template( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return parser_parse_template(); } @@ -262,7 +262,7 @@ CodeTypename parse_type( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return parser_parse_type( parser_not_from_template, nullptr); } @@ -275,7 +275,7 @@ CodeTypedef parse_typedef( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return parser_parse_typedef(); } @@ -288,7 +288,7 @@ CodeUnion parse_union( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return parser_parse_union( parser_not_inplace_def); } @@ -301,7 +301,7 @@ CodeUsing parse_using( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return parser_parse_using(); } @@ -314,7 +314,7 @@ CodeVar parse_variable( Str def ) if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser_ctx.Tokens = toks; return parser_parse_variable(); } diff --git a/base/components/interface.untyped.cpp b/base/components/interface.untyped.cpp index e8eb297..e95abf5 100644 --- a/base/components/interface.untyped.cpp +++ b/base/components/interface.untyped.cpp @@ -8,21 +8,18 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) char const* buf_begin = buf; ssize remaining = buf_size; - local_persist - TokenMap_FixedArena tok_map_arena; - fixed_arena_init( & tok_map_arena); - - local_persist - StringTable tok_map; + local_persist StringTable tok_map; + do_once() { + tok_map = hashtable_init(Str, _ctx->Allocator_DyanmicContainers ); + } + // Populate token pairs { - tok_map = hashtable_init(Str, fixed_arena_allocator_info(& tok_map_arena) ); - s32 left = num_tokens - 1; while ( left-- ) { char const* token = va_arg( va, char const* ); - Str value = va_arg( va, Str ); + Str value = va_arg( va, Str ); u32 key = crc32( token, c_str_len(token) ); hashtable_set( tok_map, key, value ); @@ -90,12 +87,8 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) current = * fmt; } } - hashtable_clear(tok_map); - fixed_arena_free(& tok_map_arena); - ssize result = buf_size - remaining; - return result; } @@ -109,7 +102,7 @@ Code untyped_str( Str content ) Code result = make_code(); - result->Name = get_cached_string( content ); + result->Name = cache_str( content ); result->Type = CT_Untyped; result->Content = result->Name; @@ -137,15 +130,12 @@ Code untyped_fmt( char const* fmt, ...) va_start(va, fmt); ssize length = c_str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va); va_end(va); - - Str buf_str = { fmt, c_str_len_capped(fmt, MaxNameLength) }; - Str uncapped_str = { buf, length }; + Str content = { buf, length }; Code result = make_code(); - result->Name = get_cached_string( buf_str ); result->Type = CT_Untyped; - result->Content = get_cached_string( uncapped_str ); + result->Content = cache_str( content ); if ( result->Name.Len == 0 ) { @@ -176,9 +166,8 @@ Code untyped_token_fmt( s32 num_tokens, char const* fmt, ... ) Code result = make_code(); - result->Name = get_cached_string( buf_str ); result->Type = CT_Untyped; - result->Content = result->Name; + result->Content = cache_str( buf_str ); if ( result->Name.Len == 0 ) { diff --git a/base/components/interface.upfront.cpp b/base/components/interface.upfront.cpp index d7025f6..4d0c487 100644 --- a/base/components/interface.upfront.cpp +++ b/base/components/interface.upfront.cpp @@ -419,7 +419,7 @@ CodeAttributes def_attributes( Str content ) Code result = make_code(); result->Type = CT_PlatformAttributes; - result->Name = get_cached_string( content ); + result->Name = cache_str( content ); result->Content = result->Name; return (CodeAttributes) result; } @@ -433,9 +433,7 @@ CodeComment def_comment( Str content ) return InvalidCode; } - static char line[ MaxCommentLineLength ]; - - StrBuilder cmt_formatted = strbuilder_make_reserve( GlobalAllocator, kilobytes(1) ); + StrBuilder cmt_formatted = strbuilder_make_reserve( _ctx->Allocator_Temp, kilobytes(1) ); char const* end = content.Ptr + content.Len; char const* scanner = content.Ptr; s32 curr = 0; @@ -450,10 +448,7 @@ CodeComment def_comment( Str content ) } length++; - c_str_copy( line, scanner, length ); - strbuilder_append_fmt(& cmt_formatted, "//%.*s", length, line ); - mem_set( line, 0, MaxCommentLineLength ); - + strbuilder_append_fmt(& cmt_formatted, "//%.*s", length, scanner ); scanner += length; } while ( scanner <= end ); @@ -466,7 +461,7 @@ CodeComment def_comment( Str content ) Code result = make_code(); result->Type = CT_Comment; - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->Content = result->Name; strbuilder_free(& cmt_formatted); @@ -531,7 +526,7 @@ CodeClass def_class( Str name, Opts_def_struct p ) CodeClass result = (CodeClass) make_code(); - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->ModuleFlags = p.mflags; if ( p.body ) { @@ -574,12 +569,12 @@ CodeDefine def_define( Str name, Str content, Opts_def_define p ) CodeDefine result = (CodeDefine) make_code(); result->Type = CT_Preprocess_Define; - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); if ( content.Len <= 0 || content.Ptr == nullptr ) - result->Content = get_cached_string( txt("") ); + result->Content = cache_str( txt("") ); else - result->Content = get_cached_string( strbuilder_to_str(strbuilder_fmt_buf(GlobalAllocator, "%S\n", content)) ); + result->Content = cache_str( strbuilder_to_str(strbuilder_fmt_buf(_ctx->Allocator_Temp, "%S\n", content)) ); b32 append_preprocess_defines = ! p.dont_append_preprocess_defines; if ( append_preprocess_defines ) { @@ -590,7 +585,7 @@ CodeDefine def_define( Str name, Str content, Opts_def_define p ) break; } Str lex_id = { result->Name.Ptr, lex_id_len }; - array_append(PreprocessorDefines, lex_id ); + array_append(_ctx->PreprocessorDefines, cache_str(lex_id) ); } return result; } @@ -648,7 +643,7 @@ CodeEnum def_enum( Str name, Opts_def_enum p ) CodeEnum result = (CodeEnum) make_code(); - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->ModuleFlags = p.mflags; if ( p.body ) { @@ -700,7 +695,7 @@ CodeExec def_execution( Str content ) CodeExec result = (CodeExec) make_code(); result->Type = CT_Execution; - result->Content = get_cached_string( content ); + result->Content = cache_str( content ); return result; } @@ -718,7 +713,7 @@ CodeExtern def_extern_link( Str name, CodeBody body ) CodeExtern result = (CodeExtern)make_code(); result->Type = CT_Extern_Linkage; - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->Body = body; return result; } @@ -781,7 +776,7 @@ CodeFn def_function( Str name, Opts_def_function p ) CodeFn result = (CodeFn) make_code(); - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->ModuleFlags = p.mflags; if ( p.body ) { @@ -820,13 +815,13 @@ CodeInclude def_include( Str path, Opts_def_include p ) return InvalidCode; } StrBuilder content = p.foreign ? - strbuilder_fmt_buf( GlobalAllocator, "<%.*s>", path.Len, path.Ptr ) - : strbuilder_fmt_buf( GlobalAllocator, "\"%.*s\"", path.Len, path.Ptr ); + strbuilder_fmt_buf( _ctx->Allocator_Temp, "<%.*s>", path.Len, path.Ptr ) + : strbuilder_fmt_buf( _ctx->Allocator_Temp, "\"%.*s\"", path.Len, path.Ptr ); CodeInclude result = (CodeInclude) make_code(); result->Type = CT_Preprocess_Include; - result->Name = get_cached_string( strbuilder_to_str(content) ); + result->Name = cache_str( strbuilder_to_str(content) ); result->Content = result->Name; return result; } @@ -840,7 +835,7 @@ CodeModule def_module( Str name, Opts_def_module p ) CodeModule result = (CodeModule) make_code(); result->Type = CT_Module; - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->ModuleFlags = p.mflags; return result; } @@ -863,7 +858,7 @@ CodeNS def_namespace( Str name, CodeBody body, Opts_def_namespace p ) CodeNS result = (CodeNS) make_code(); result->Type = CT_Namespace; - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->ModuleFlags = p.mflags; result->Body = body; return result; @@ -899,7 +894,7 @@ CodeOperator def_operator( Operator op, Str nspace, Opts_def_operator p ) CodeOperator result = (CodeOperator) make_code(); - result->Name = get_cached_string( name_resolved ); + result->Name = cache_str( name_resolved ); result->ModuleFlags = p.mflags; result->Op = op; if ( p.body ) @@ -986,7 +981,7 @@ CodeParams def_param( CodeTypename type, Str name, Opts_def_param p ) CodeParams result = (CodeParams) make_code(); result->Type = CT_Parameters; - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->ValueType = type; result->Value = p.value; result->NumEntries++; @@ -1003,7 +998,7 @@ CodePragma def_pragma( Str directive ) CodePragma result = (CodePragma) make_code(); result->Type = CT_Preprocess_Pragma; - result->Content = get_cached_string( directive ); + result->Content = cache_str( directive ); return result; } @@ -1016,7 +1011,7 @@ CodePreprocessCond def_preprocess_cond( EPreprocessCond type, Str expr ) } CodePreprocessCond result = (CodePreprocessCond) make_code(); - result->Content = get_cached_string( expr ); + result->Content = cache_str( expr ); switch (type) { case PreprocessCond_If: @@ -1066,7 +1061,7 @@ CodeStruct def_struct( Str name, Opts_def_struct p ) result = (CodeStruct) make_code(); result->ModuleFlags = p.mflags; if ( name.Len ) - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); if ( p.body ) { result->Type = CT_Struct; @@ -1143,7 +1138,7 @@ CodeTypename def_type( Str name, Opts_def_type p ) } CodeTypename result = (CodeTypename) make_code(); - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->Type = CT_Typename; result->Attributes = p.attributes; result->Specs = p.specifiers; @@ -1204,12 +1199,12 @@ CodeTypedef def_typedef( Str name, Code type, Opts_def_typedef p ) GEN_DEBUG_TRAP(); return InvalidCode; } - result->Name = get_cached_string( type->Name ); + result->Name = cache_str( type->Name ); result->IsFunction = true; } else { - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->IsFunction = false; } return result; @@ -1238,7 +1233,7 @@ CodeUnion def_union( Str name, CodeBody body, Opts_def_union p ) result->Body = body; result->Attributes = p.attributes; if ( name.Ptr ) - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); return result; } @@ -1262,7 +1257,7 @@ CodeUsing def_using( Str name, CodeTypename type, Opts_def_using p ) } CodeUsing result = (CodeUsing) make_code(); - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->ModuleFlags = p.mflags; result->Type = CT_Using; result->UnderlyingType = type; @@ -1278,7 +1273,7 @@ CodeUsing def_using_namespace( Str name ) } CodeUsing result = (CodeUsing) make_code(); - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->Type = CT_Using_Namespace; return result; } @@ -1311,7 +1306,7 @@ CodeVar def_variable( CodeTypename type, Str name, Opts_def_variable p ) } CodeVar result = (CodeVar) make_code(); - result->Name = get_cached_string( name ); + result->Name = cache_str( name ); result->Type = CT_Variable; result->ModuleFlags = p.mflags; result->ValueType = type; diff --git a/base/components/lexer.cpp b/base/components/lexer.cpp index 4009ec0..c6df466 100644 --- a/base/components/lexer.cpp +++ b/base/components/lexer.cpp @@ -92,7 +92,7 @@ bool tok_is_end_definition(Token tok) { StrBuilder tok_to_strbuilder(Token tok) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, kilobytes(4) ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, kilobytes(4) ); Str type_str = toktype_to_str( tok.Type ); strbuilder_append_fmt( & result, "Line: %d Column: %d, Type: %.*s Content: %.*s" @@ -354,7 +354,7 @@ s32 lex_preprocessor_directive( LexContext* ctx ) if ( (* ctx->scanner) != '"' && (* ctx->scanner) != '<' ) { - StrBuilder directive_str = strbuilder_fmt_buf( GlobalAllocator, "%.*s", min( 80, ctx->left + preprocess_content.Text.Len ), ctx->token.Text.Ptr ); + StrBuilder directive_str = strbuilder_fmt_buf( _ctx->Allocator_Temp, "%.*s", min( 80, ctx->left + preprocess_content.Text.Len ), ctx->token.Text.Ptr ); log_failure( "gen::Parser::lex: Expected '\"' or '<' after #include, not '%c' (%d, %d)\n%s" , (* ctx->scanner) @@ -421,8 +421,8 @@ s32 lex_preprocessor_directive( LexContext* ctx ) } else { - StrBuilder directive_str = strbuilder_make_length( GlobalAllocator, ctx->token.Text.Ptr, ctx->token.Text.Len ); - StrBuilder content_str = strbuilder_fmt_buf( GlobalAllocator, "%.*s", min( 400, ctx->left + preprocess_content.Text.Len ), preprocess_content.Text.Ptr ); + StrBuilder directive_str = strbuilder_make_length( _ctx->Allocator_Temp, ctx->token.Text.Ptr, ctx->token.Text.Len ); + StrBuilder content_str = strbuilder_fmt_buf( _ctx->Allocator_Temp, "%.*s", min( 400, ctx->left + preprocess_content.Text.Len ), preprocess_content.Text.Ptr ); log_failure( "gen::Parser::lex: Invalid escape sequence '\\%c' (%d, %d)" " in preprocessor directive '%s' (%d, %d)\n%s" @@ -584,7 +584,7 @@ TokArray lex( Str content ) return null_array; } - for ( StringCached* entry = array_begin(PreprocessorDefines); entry != array_end(PreprocessorDefines); entry = array_next(PreprocessorDefines, entry)) + for ( StrCached* entry = array_begin(_ctx->PreprocessorDefines); entry != array_end(_ctx->PreprocessorDefines); entry = array_next(_ctx->PreprocessorDefines, entry)) { s32 length = 0; char const* entry_scanner = (*entry).Ptr; @@ -710,7 +710,7 @@ TokArray lex( Str content ) } else { - StrBuilder context_str = strbuilder_fmt_buf( GlobalAllocator, "%s", c.scanner, min( 100, c.left ) ); + StrBuilder context_str = strbuilder_fmt_buf( _ctx->Allocator_Temp, "%s", c.scanner, min( 100, c.left ) ); log_failure( "gen::lex: invalid varadic argument, expected '...' got '..%c' (%d, %d)\n%s", (* ctx->scanner), c.line, c.column, context_str ); } @@ -1271,7 +1271,7 @@ TokArray lex( Str content ) ); } - StrBuilder context_str = strbuilder_fmt_buf( GlobalAllocator, "%.*s", min( 100, c.left ), c.scanner ); + StrBuilder context_str = strbuilder_fmt_buf( _ctx->Allocator_Temp, "%.*s", min( 100, c.left ), c.scanner ); log_failure( "Failed to lex token '%c' (%d, %d)\n%s", (* ctx->scanner), c.line, c.column, context_str ); // Skip to next whitespace since we can't know if anything else is valid until then. diff --git a/base/components/parser.cpp b/base/components/parser.cpp index aad9116..0332d70 100644 --- a/base/components/parser.cpp +++ b/base/components/parser.cpp @@ -33,21 +33,21 @@ void parser_push( ParseContext* ctx, StackNode* node ) ctx->Scope = node; #if 0 && GEN_BUILD_DEBUG - log_fmt("\tEntering Context: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); + log_fmt("\tEntering parser_ctx: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); #endif } void parser_pop(ParseContext* ctx) { #if 0 && GEN_BUILD_DEBUG - log_fmt("\tPopping Context: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); + log_fmt("\tPopping parser_ctx: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); #endif ctx->Scope = ctx->Scope->Prev; } StrBuilder parser_to_strbuilder(ParseContext ctx) { - StrBuilder result = strbuilder_make_reserve( GlobalAllocator, kilobytes(4) ); + StrBuilder result = strbuilder_make_reserve( _ctx->Allocator_Temp, kilobytes(4) ); Token scope_start = ctx.Scope->Start; Token last_valid = ctx.Tokens.Idx >= array_num(ctx.Tokens.Arr) ? ctx.Tokens.Arr[array_num(ctx.Tokens.Arr) -1] : (* lex_current(& ctx.Tokens, true)); @@ -61,7 +61,7 @@ StrBuilder parser_to_strbuilder(ParseContext ctx) } Str scope_str = { scope_start.Text.Ptr, length }; - StrBuilder line = strbuilder_make_str( GlobalAllocator, scope_str ); + StrBuilder line = strbuilder_make_str( _ctx->Allocator_Temp, scope_str ); strbuilder_append_fmt( & result, "\tScope : %s\n", line ); strbuilder_free(& line); @@ -69,7 +69,7 @@ StrBuilder parser_to_strbuilder(ParseContext ctx) sptr length_from_err = dist; Str err_str = { last_valid.Text.Ptr, length_from_err }; - StrBuilder line_from_err = strbuilder_make_str( GlobalAllocator, err_str ); + StrBuilder line_from_err = strbuilder_make_str( _ctx->Allocator_Temp, err_str ); if ( length_from_err < 100 ) strbuilder_append_fmt(& result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); @@ -96,13 +96,13 @@ StrBuilder parser_to_strbuilder(ParseContext ctx) return result; } -global ParseContext Context; +global ParseContext parser_ctx; bool lex__eat(TokArray* self, TokType type ) { if ( array_num(self->Arr) - self->Idx <= 0 ) { - log_failure( "No tokens left.\n%s", parser_to_strbuilder(Context) ); + log_failure( "No tokens left.\n%s", parser_to_strbuilder(parser_ctx) ); return false; } @@ -122,7 +122,7 @@ bool lex__eat(TokArray* self, TokType type ) , at_idx.Text.Len, at_idx.Text.Ptr , tok.Line , tok.Column - , parser_to_strbuilder(Context) + , parser_to_strbuilder(parser_ctx) ); GEN_DEBUG_TRAP(); return false; @@ -139,8 +139,8 @@ bool lex__eat(TokArray* self, TokType type ) internal void parser_init() { - Lexer_Tokens = array_init_reserve(Token, arena_allocator_info( & LexArena) - , ( LexAllocator_Size - sizeof( ArrayHeader ) ) / sizeof(Token) + Lexer_Tokens = array_init_reserve(Token, arena_allocator_info( & _ctx->LexArena) + , ( _ctx->InitSize_LexArena - sizeof( ArrayHeader ) ) / sizeof(Token) ); fixed_arena_init(& Lexer_defines_map_arena); @@ -162,26 +162,26 @@ bool _check_parse_args( Str def, char const* func_name ) if ( def.Len <= 0 ) { log_failure( c_str_fmt_buf("gen::%s: length must greater than 0", func_name) ); - parser_pop(& Context); + parser_pop(& parser_ctx); return false; } if ( def.Ptr == nullptr ) { log_failure( c_str_fmt_buf("gen::%s: def was null", func_name) ); - parser_pop(& Context); + parser_pop(& parser_ctx); return false; } return true; } -# define currtok_noskip (* lex_current( & Context.Tokens, lex_dont_skip_formatting )) -# define currtok (* lex_current( & Context.Tokens, lex_skip_formatting )) -# define peektok (* lex_peek(Context.Tokens, lex_skip_formatting)) -# define prevtok (* lex_previous( Context.Tokens, lex_dont_skip_formatting)) -# define nexttok (* lex_next( Context.Tokens, lex_skip_formatting )) -# define nexttok_noskip (* lex_next( Context.Tokens, lex_dont_skip_formatting)) -# define eat( Type_ ) lex__eat( & Context.Tokens, Type_ ) -# define left ( array_num(Context.Tokens.Arr) - Context.Tokens.Idx ) +# define currtok_noskip (* lex_current( & parser_ctx.Tokens, lex_dont_skip_formatting )) +# define currtok (* lex_current( & parser_ctx.Tokens, lex_skip_formatting )) +# define peektok (* lex_peek(parser_ctx.Tokens, lex_skip_formatting)) +# define prevtok (* lex_previous( parser_ctx.Tokens, lex_dont_skip_formatting)) +# define nexttok (* lex_next( parser_ctx.Tokens, lex_skip_formatting )) +# define nexttok_noskip (* lex_next( parser_ctx.Tokens, lex_dont_skip_formatting)) +# define eat( Type_ ) lex__eat( & parser_ctx.Tokens, Type_ ) +# define left ( array_num(parser_ctx.Tokens.Arr) - parser_ctx.Tokens.Idx ) #if GEN_COMPILER_CPP # define def_assign( ... ) { __VA_ARGS__ } @@ -201,7 +201,7 @@ bool _check_parse_args( Str def, char const* func_name ) # define push_scope() \ GEN_NS_PARSER StackNode scope = { nullptr, currtok_noskip, GEN_NS_PARSER NullToken, txt( __func__ ) }; \ - parser_push( & GEN_NS_PARSER Context, & scope ) + parser_push( & GEN_NS_PARSER parser_ctx, & scope ) #pragma endregion Helper Macros @@ -270,7 +270,7 @@ constexpr bool parser_strip_formatting_dont_preserve_newlines = false; internal StrBuilder parser_strip_formatting( Str raw_text, bool preserve_newlines ) { - StrBuilder content = strbuilder_make_reserve( GlobalAllocator, raw_text.Len ); + StrBuilder content = strbuilder_make_reserve( _ctx->Allocator_Temp, raw_text.Len ); if ( raw_text.Len == 0 ) return content; @@ -519,7 +519,7 @@ Code parse_array_decl() eat( Tok_Operator ); // [] - parser_pop(& Context); + parser_pop(& parser_ctx); return array_expr; } @@ -530,15 +530,15 @@ Code parse_array_decl() if ( left == 0 ) { - log_failure( "Error, unexpected end of array declaration ( '[]' scope started )\n%s", parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Error, unexpected end of array declaration ( '[]' scope started )\n%s", parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } if ( currtok.Type == Tok_BraceSquare_Close ) { - log_failure( "Error, empty array expression in definition\n%s", parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Error, empty array expression in definition\n%s", parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } @@ -556,15 +556,15 @@ Code parse_array_decl() if ( left == 0 ) { - log_failure( "Error, unexpected end of array declaration, expected ]\n%s", parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Error, unexpected end of array declaration, expected ]\n%s", parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } if ( currtok.Type != Tok_BraceSquare_Close ) { - log_failure( "%s: Error, expected ] in array declaration, not %s\n%s", toktype_to_str( currtok.Type ), parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "%s: Error, expected ] in array declaration, not %s\n%s", toktype_to_str( currtok.Type ), parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } @@ -580,11 +580,11 @@ Code parse_array_decl() array_expr->Next = adjacent_arr_expr; } - parser_pop(& Context); + parser_pop(& parser_ctx); return array_expr; } - parser_pop(& Context); + parser_pop(& parser_ctx); return NullCode; } @@ -682,20 +682,20 @@ CodeAttributes parse_attributes() if ( len > 0 ) { Str attribute_txt = { start.Text.Ptr, len }; - parser_pop(& Context); + parser_pop(& parser_ctx); StrBuilder name_stripped = parser_strip_formatting( attribute_txt, parser_strip_formatting_dont_preserve_newlines ); Code result = make_code(); result->Type = CT_PlatformAttributes; - result->Name = get_cached_string( strbuilder_to_str(name_stripped) ); + result->Name = cache_str( strbuilder_to_str(name_stripped) ); result->Content = result->Name; // result->Token = return ( CodeAttributes )result; } - parser_pop(& Context); + parser_pop(& parser_ctx); return NullCode; } @@ -704,7 +704,7 @@ Code parse_class_struct( TokType which, bool inplace_def ) { if ( which != Tok_Decl_Class && which != Tok_Decl_Struct ) { - log_failure( "Error, expected class or struct, not %s\n%s", toktype_to_str( which ), parser_to_strbuilder(Context) ); + log_failure( "Error, expected class or struct, not %s\n%s", toktype_to_str( which ), parser_to_strbuilder(parser_ctx) ); return InvalidCode; } @@ -734,7 +734,7 @@ Code parse_class_struct( TokType which, bool inplace_def ) if ( check( Tok_Identifier ) ) { name = parse_identifier(nullptr); - Context.Scope->Name = name; + parser_ctx.Scope->Name = name; } // @@ -834,7 +834,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) bool expects_function = false; - // Context.Scope->Start = currtok_noskip; + // parser_ctx.Scope->Start = currtok_noskip; if ( currtok_noskip.Type == Tok_Preprocess_Hash ) eat( Tok_Preprocess_Hash ); @@ -928,7 +928,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) case Tok_Operator: //if ( currtok.Text[0] != '~' ) //{ - // log_failure( "Operator token found in global body but not destructor unary negation\n%s", to_strbuilder(Context) ); + // log_failure( "Operator token found in global body but not destructor unary negation\n%s", to_strbuilder(parser_ctx) ); // return InvalidCode; //} @@ -1042,8 +1042,8 @@ CodeBody parse_class_struct_body( TokType which, Token name ) break; default: - log_failure( "Invalid specifier %S for variable\n%S", spec_to_str(spec), strbuilder_to_str( parser_to_strbuilder(Context)) ); - parser_pop(& Context); + log_failure( "Invalid specifier %S for variable\n%S", spec_to_str(spec), strbuilder_to_str( parser_to_strbuilder(parser_ctx)) ); + parser_pop(& parser_ctx); return InvalidCode; } @@ -1069,11 +1069,11 @@ CodeBody parse_class_struct_body( TokType which, Token name ) if ( attributes ) { - StrBuilder fused = strbuilder_make_reserve( GlobalAllocator, attributes->Content.Len + more_attributes->Content.Len ); + StrBuilder fused = strbuilder_make_reserve( _ctx->Allocator_Temp, attributes->Content.Len + more_attributes->Content.Len ); strbuilder_append_fmt( & fused, "%SB %SB", attributes->Content, more_attributes->Content ); Str attrib_name = strbuilder_to_str(fused); - attributes->Name = get_cached_string( attrib_name ); + attributes->Name = cache_str( attrib_name ); attributes->Content = attributes->Name; // } @@ -1128,8 +1128,8 @@ CodeBody parse_class_struct_body( TokType which, Token name ) if ( member == Code_Invalid ) { - log_failure( "Failed to parse member\n%s", parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Failed to parse member\n%s", parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } continue; @@ -1162,8 +1162,8 @@ CodeBody parse_class_struct_body( TokType which, Token name ) if ( member == Code_Invalid ) { - log_failure( "Failed to parse member\n%s", parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Failed to parse member\n%s", parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } @@ -1172,7 +1172,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) eat( Tok_BraceCurly_Close ); // { } - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } @@ -1184,12 +1184,11 @@ CodeComment parse_comment() CodeComment result = (CodeComment) make_code(); result->Type = CT_Comment; - result->Content = get_cached_string( tok_to_str(currtok_noskip) ); - result->Name = result->Content; + result->Content = cache_str( tok_to_str(currtok_noskip) ); // result->Token = currtok_noskip; eat( Tok_Comment ); - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } @@ -1200,7 +1199,7 @@ Code parse_complicated_definition( TokType which ) bool is_inplace = false; - TokArray tokens = Context.Tokens; + TokArray tokens = parser_ctx.Tokens; s32 idx = tokens.Idx; s32 level = 0; @@ -1221,7 +1220,7 @@ Code parse_complicated_definition( TokType which ) // Its a forward declaration only Code result = parse_forward_or_definition( which, is_inplace ); // ; - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } @@ -1245,12 +1244,12 @@ Code parse_complicated_definition( TokType which ) Code result = parse_operator_function_or_variable( false, NullCode, NullCode ); // , or Name> ... - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } - log_failure( "Unsupported or bad member definition after %s declaration\n%s", toktype_to_str(which), parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Unsupported or bad member definition after %s declaration\n%s", toktype_to_str(which), parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } if ( tok.Type == Tok_Identifier ) @@ -1282,7 +1281,7 @@ Code parse_complicated_definition( TokType which ) // : ; ok_to_parse = true; Code result = cast(Code, parser_parse_enum( ! parser_inplace_def)); - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } else if ( is_indirection ) @@ -1294,14 +1293,14 @@ Code parse_complicated_definition( TokType which ) if ( ! ok_to_parse ) { - log_failure( "Unsupported or bad member definition after %s declaration\n%s", toktype_to_str(which), parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Unsupported or bad member definition after %s declaration\n%s", toktype_to_str(which), parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } Code result = parse_operator_function_or_variable( false, NullCode, NullCode ); // , or Name> ... - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } else if ( tok.Type >= Tok_Type_Unsigned && tok.Type <= Tok_Type_MS_W64 ) @@ -1313,8 +1312,8 @@ Code parse_complicated_definition( TokType which ) && ( tokens.Arr[idx - 4].Type != which)) ) { - log_failure( "Unsupported or bad member definition after %s declaration\n%s", toktype_to_str(which), parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Unsupported or bad member definition after %s declaration\n%s", toktype_to_str(which), parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } @@ -1322,7 +1321,7 @@ Code parse_complicated_definition( TokType which ) // : ; // : ; Code result = cast(Code, parser_parse_enum( ! parser_inplace_def)); - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } else if ( tok.Type == Tok_BraceCurly_Close ) @@ -1330,7 +1329,7 @@ Code parse_complicated_definition( TokType which ) // Its a definition Code result = parse_forward_or_definition( which, is_inplace ); // { ... }; - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } else if ( tok.Type == Tok_BraceSquare_Close ) @@ -1338,13 +1337,13 @@ Code parse_complicated_definition( TokType which ) // Its an array definition Code result = parse_operator_function_or_variable( false, NullCode, NullCode ); // [ ... ]; - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } else { - log_failure( "Unsupported or bad member definition after %s declaration\n%SB", toktype_to_str(which).Ptr, parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Unsupported or bad member definition after %s declaration\n%SB", toktype_to_str(which).Ptr, parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } } @@ -1362,38 +1361,38 @@ CodeDefine parse_define() if ( ! check( Tok_Identifier ) ) { - log_failure( "Error, expected identifier after #define\n%s", parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Error, expected identifier after #define\n%s", parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } - Context.Scope->Name = currtok; - define->Name = get_cached_string( tok_to_str(currtok) ); + parser_ctx.Scope->Name = currtok; + define->Name = cache_str( tok_to_str(currtok) ); eat( Tok_Identifier ); // #define if ( ! check( Tok_Preprocess_Content )) { - log_failure( "Error, expected content after #define %s\n%s", define->Name, parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Error, expected content after #define %s\n%s", define->Name, parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } if ( currtok.Text.Len == 0 ) { - define->Content = get_cached_string( tok_to_str(currtok) ); + define->Content = cache_str( tok_to_str(currtok) ); eat( Tok_Preprocess_Content ); // #define - parser_pop(& Context); + parser_pop(& parser_ctx); return define; } - define->Content = get_cached_string( strbuilder_to_str( parser_strip_formatting( tok_to_str(currtok), parser_strip_formatting_dont_preserve_newlines )) ); + define->Content = cache_str( strbuilder_to_str( parser_strip_formatting( tok_to_str(currtok), parser_strip_formatting_dont_preserve_newlines )) ); eat( Tok_Preprocess_Content ); // #define - parser_pop(& Context); + parser_pop(& parser_ctx); return define; } @@ -1409,8 +1408,8 @@ Code parse_assignment_expression() if ( currtok.Type == Tok_Statement_End && currtok.Type != Tok_Comma ) { - log_failure( "Expected expression after assignment operator\n%s", parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Expected expression after assignment operator\n%s", parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } @@ -1461,7 +1460,7 @@ Code parse_forward_or_definition( TokType which, bool is_inplace ) default: log_failure( "Error, wrong token type given to parse_complicated_definition " "(only supports class, enum, struct, union) \n%s" - , parser_to_strbuilder(Context) ); + , parser_to_strbuilder(parser_ctx) ); return InvalidCode; } @@ -1503,7 +1502,7 @@ CodeFn parse_function_after_name( body = cast(CodeBody, parse_function_body()); if ( cast(Code, body) == Code_Invalid ) { - parser_pop(& Context); + parser_pop(& parser_ctx); return InvalidCode; } // ( ) { } @@ -1534,12 +1533,12 @@ CodeFn parse_function_after_name( } StrBuilder - name_stripped = strbuilder_make_str( GlobalAllocator, tok_to_str(name) ); + name_stripped = strbuilder_make_str( _ctx->Allocator_Temp, tok_to_str(name) ); strip_space(name_stripped); CodeFn result = (CodeFn) make_code(); - result->Name = get_cached_string( strbuilder_to_str(name_stripped) ); + result->Name = cache_str( strbuilder_to_str(name_stripped) ); result->ModuleFlags = mflags; if ( body ) @@ -1552,8 +1551,8 @@ CodeFn parse_function_after_name( default: { - log_failure("Body must be either of Function_Body or Untyped type, %s\n%s", code_debug_str(body), parser_to_strbuilder(Context)); - parser_pop(& Context); + log_failure("Body must be either of Function_Body or Untyped type, %s\n%s", code_debug_str(body), parser_to_strbuilder(parser_ctx)); + parser_pop(& parser_ctx); return InvalidCode; } } @@ -1580,7 +1579,7 @@ CodeFn parse_function_after_name( if ( inline_cmt ) result->InlineCmt = inline_cmt; - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } @@ -1622,7 +1621,7 @@ Code parse_function_body() eat( Tok_BraceCurly_Close ); - parser_pop(& Context); + parser_pop(& parser_ctx); return cast(Code, result); } @@ -1650,7 +1649,7 @@ CodeBody parse_global_nspace( CodeType which ) bool expects_function = false; - // Context.Scope->Start = currtok_noskip; + // parser_ctx.Scope->Start = currtok_noskip; if ( currtok_noskip.Type == Tok_Preprocess_Hash ) eat( Tok_Preprocess_Hash ); @@ -1661,8 +1660,8 @@ CodeBody parse_global_nspace( CodeType which ) { case Tok_Comma: { - log_failure("Dangling comma found: %SB\nContext:\n%SB", tok_to_strbuilder(currtok), parser_to_strbuilder(Context)); - parser_pop( & Context); + log_failure("Dangling comma found: %SB\nContext:\n%SB", tok_to_strbuilder(currtok), parser_to_strbuilder(parser_ctx)); + parser_pop( & parser_ctx); return InvalidCode; } break; @@ -1695,7 +1694,7 @@ CodeBody parse_global_nspace( CodeType which ) case Tok_Decl_Extern_Linkage: if ( which == CT_Extern_Linkage_Body ) - log_failure( "Nested extern linkage\n%s", parser_to_strbuilder(Context) ); + log_failure( "Nested extern linkage\n%s", parser_to_strbuilder(parser_ctx) ); member = cast(Code, parser_parse_extern_link()); // extern "..." { ... } @@ -1788,7 +1787,7 @@ CodeBody parse_global_nspace( CodeType which ) case Tok_Module_Export: { if ( which == CT_Export_Body ) - log_failure( "Nested export declaration\n%s", parser_to_strbuilder(Context) ); + log_failure( "Nested export declaration\n%s", parser_to_strbuilder(parser_ctx) ); member = cast(Code, parser_parse_export_body()); // export { ... } @@ -1858,8 +1857,8 @@ CodeBody parse_global_nspace( CodeType which ) default: Str spec_str = spec_to_str(spec); - log_failure( "Invalid specifier %S for variable\n%S", spec_str, strbuilder_to_str( parser_to_strbuilder(Context)) ); - parser_pop(& Context); + log_failure( "Invalid specifier %S for variable\n%S", spec_str, strbuilder_to_str( parser_to_strbuilder(parser_ctx)) ); + parser_pop(& parser_ctx); return InvalidCode; } @@ -1900,16 +1899,16 @@ CodeBody parse_global_nspace( CodeType which ) } bool found_operator_cast_outside_class_implmentation = false; - s32 idx = Context.Tokens.Idx; + s32 idx = parser_ctx.Tokens.Idx; - for ( ; idx < array_num(Context.Tokens.Arr); idx++ ) + for ( ; idx < array_num(parser_ctx.Tokens.Arr); idx++ ) { - Token tok = Context.Tokens.Arr[ idx ]; + Token tok = parser_ctx.Tokens.Arr[ idx ]; if ( tok.Type == Tok_Identifier ) { idx++; - tok = Context.Tokens.Arr[ idx ]; + tok = parser_ctx.Tokens.Arr[ idx ]; if ( tok.Type == Tok_Access_StaticSymbol ) continue; @@ -1941,8 +1940,8 @@ CodeBody parse_global_nspace( CodeType which ) if ( member == Code_Invalid ) { - log_failure( "Failed to parse member\n%s", parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Failed to parse member\n%s", parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } goto Member_Resolved_To_Lone_Macro; @@ -1960,8 +1959,8 @@ CodeBody parse_global_nspace( CodeType which ) Member_Resolved_To_Lone_Macro: if ( member == Code_Invalid ) { - log_failure( "Failed to parse member\nToken: %SB\nContext:\n%SB", tok_to_strbuilder(currtok_noskip), parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Failed to parse member\nToken: %SB\nContext:\n%SB", tok_to_strbuilder(currtok_noskip), parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return InvalidCode; } @@ -1973,7 +1972,7 @@ CodeBody parse_global_nspace( CodeType which ) eat( Tok_BraceCurly_Close ); // { } - parser_pop(& Context); + parser_pop(& parser_ctx); return result; } @@ -1994,7 +1993,7 @@ Code parse_global_nspace_constructor_destructor( CodeSpecifiers specifiers ) TODO(Ed): We could fix this by attempting to parse a type, but we would have to have a way to have it soft fail and rollback. */ - TokArray tokens = Context.Tokens; + TokArray tokens = parser_ctx.Tokens; s32 idx = tokens.Idx; Token nav = tokens.Arr[ idx ]; @@ -2114,7 +2113,7 @@ Token parse_identifier( bool* possible_member_function ) push_scope(); Token name = currtok; - Context.Scope->Name = name; + parser_ctx.Scope->Name = name; eat( Tok_Identifier ); // @@ -2129,23 +2128,23 @@ Token parse_identifier( bool* possible_member_function ) Token invalid = { nullptr, 0, Tok_Invalid }; if ( left == 0 ) { - log_failure( "Error, unexpected end of static symbol identifier\n%s", parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Error, unexpected end of static symbol identifier\n%s", parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return invalid; } if ( currtok.Type == Tok_Operator && currtok.Text.Ptr[0] == '~' ) { - bool is_destructor = str_are_equal( Context.Scope->Prev->ProcName, txt("parser_parse_destructor")); + bool is_destructor = str_are_equal( parser_ctx.Scope->Prev->ProcName, txt("parser_parse_destructor")); if (is_destructor) { name.Text.Len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )name.Text.Ptr; - parser_pop(& Context); + parser_pop(& parser_ctx); return name; } - log_failure( "Error, had a ~ operator after %SB but not a destructor\n%s", toktype_to_str( prevtok.Type ), parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Error, had a ~ operator after %SB but not a destructor\n%s", toktype_to_str( prevtok.Type ), parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return invalid; } @@ -2156,16 +2155,16 @@ Token parse_identifier( bool* possible_member_function ) else { - log_failure( "Found a member function pointer identifier but the parsing context did not expect it\n%s", parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Found a member function pointer identifier but the parsing context did not expect it\n%s", parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return invalid; } } if ( currtok.Type != Tok_Identifier ) { - log_failure( "Error, expected static symbol identifier, not %s\n%s", toktype_to_str( currtok.Type ), parser_to_strbuilder(Context) ); - parser_pop(& Context); + log_failure( "Error, expected static symbol identifier, not %s\n%s", toktype_to_str( currtok.Type ), parser_to_strbuilder(parser_ctx) ); + parser_pop(& parser_ctx); return invalid; } @@ -2178,7 +2177,7 @@ Token parse_identifier( bool* possible_member_function ) } //