From 163ad0a5112fc2ff18b0599822f7cadbac73ddda Mon Sep 17 00:00:00 2001 From: Ed_ Date: Fri, 29 Nov 2024 15:18:06 -0500 Subject: [PATCH 001/112] looking into removing "oop" features from base library I want to make member functions an optional addition the user can generate a derivative library with. The purpose is to simplify the implementation as to make generating a C-variant simpiler. I also want to use it as a study to see how much simpiler it makes the library without having it. --- project/components/interface.untyped.cpp | 4 +-- project/components/parser.cpp | 5 ++- project/dependencies/memory.hpp | 44 ++++++++++++++---------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/project/components/interface.untyped.cpp b/project/components/interface.untyped.cpp index 9eacf1b..b16e48c 100644 --- a/project/components/interface.untyped.cpp +++ b/project/components/interface.untyped.cpp @@ -16,8 +16,8 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) local_persist char tok_map_mem[ TokenFmt_TokenMap_MemSize ]; - tok_map_arena = Arena::init_from_memory( tok_map_mem, sizeof(tok_map_mem) ); - tok_map = HashTable::init( tok_map_arena ); + init_from_memory( tok_map_arena, tok_map_mem, sizeof(tok_map_mem) ); + tok_map = HashTable::init( tok_map_arena ); s32 left = num_tokens - 1; diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 796a182..9be9027 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -712,7 +712,10 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) local_persist char interface_arr_mem[ kilobytes(4) ] {0}; - Array interfaces = Array::init_reserve( Arena::init_from_memory(interface_arr_mem, kilobytes(4) ), 4 ); + Array interfaces; { + Arena arena; init_from_memory(arena, interface_arr_mem, kilobytes(4) ); + Array::init_reserve( arena, 4 ); + } // TODO(Ed) : Make an AST_DerivedType, we'll store any arbitary derived type into there as a linear linked list of them. if ( check( TokType::Assign_Classifer ) ) diff --git a/project/dependencies/memory.hpp b/project/dependencies/memory.hpp index 889749b..a327ba9 100644 --- a/project/dependencies/memory.hpp +++ b/project/dependencies/memory.hpp @@ -170,23 +170,20 @@ b32 gen_vm_purge( VirtualMemory vm ); //! Retrieve VM's page size and alignment. ssize gen_virtual_memory_page_size( ssize* alignment_out ); + +struct Arena; +void init_from_memory( Arena& arena, void* start, ssize size ); + struct Arena { static void* allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ); - static - Arena init_from_memory( void* start, ssize size ) - { - return - { - { nullptr, nullptr }, - start, - size, - 0, - 0 - }; - } + //forceinline static + //Arena init_from_memory( void* start, ssize size ) { + // Arena result; GEN_NS init_from_memory( result, start, size ); + // return result; + //} static Arena init_from_allocator( AllocatorInfo backing, ssize size ) @@ -249,16 +246,25 @@ struct Arena AllocatorInfo Backing; void* PhysicalStart; - ssize TotalSize; - ssize TotalUsed; - ssize TempCount; + ssize TotalSize; + ssize TotalUsed; + ssize TempCount; - operator AllocatorInfo() - { - return { allocator_proc, this }; - } + operator AllocatorInfo() { return { allocator_proc, this }; } }; +void init_from_memory( Arena& arena, void* start, ssize size ) +{ + arena = + { + { nullptr, nullptr }, + start, + size, + 0, + 0 + }; +} + // Just a wrapper around using an arena with memory associated with its scope instead of from an allocator. // Used for static segment or stack allocations. template< s32 Size > From 5958dd2055920f127561a8ec3dcf9eb539ba5d55 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 12:16:01 -0500 Subject: [PATCH 002/112] Did arena and fixedarena changes (for reducing usage of member procs) --- project/components/interface.untyped.cpp | 4 +- project/components/parser.cpp | 2 +- project/dependencies/basic_types.hpp | 1 + project/dependencies/memory.cpp | 2 +- project/dependencies/memory.hpp | 223 ++++++++++++++--------- 5 files changed, 143 insertions(+), 89 deletions(-) diff --git a/project/components/interface.untyped.cpp b/project/components/interface.untyped.cpp index b16e48c..bd73306 100644 --- a/project/components/interface.untyped.cpp +++ b/project/components/interface.untyped.cpp @@ -16,8 +16,8 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) local_persist char tok_map_mem[ TokenFmt_TokenMap_MemSize ]; - init_from_memory( tok_map_arena, tok_map_mem, sizeof(tok_map_mem) ); - tok_map = HashTable::init( tok_map_arena ); + tok_map_arena = init_from_memory( tok_map_mem, sizeof(tok_map_mem) ); + tok_map = HashTable::init( tok_map_arena ); s32 left = num_tokens - 1; diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 9be9027..e1fe6a2 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -713,7 +713,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) local_persist char interface_arr_mem[ kilobytes(4) ] {0}; Array interfaces; { - Arena arena; init_from_memory(arena, interface_arr_mem, kilobytes(4) ); + Arena arena = init_from_memory( interface_arr_mem, kilobytes(4) ); Array::init_reserve( arena, 4 ); } diff --git a/project/dependencies/basic_types.hpp b/project/dependencies/basic_types.hpp index 987f56f..9533827 100644 --- a/project/dependencies/basic_types.hpp +++ b/project/dependencies/basic_types.hpp @@ -1,5 +1,6 @@ #ifdef GEN_INTELLISENSE_DIRECTIVES # pragma once +# include "platform.hpp" # include "macros.hpp" #endif diff --git a/project/dependencies/memory.cpp b/project/dependencies/memory.cpp index 8f7f218..e51fb09 100644 --- a/project/dependencies/memory.cpp +++ b/project/dependencies/memory.cpp @@ -334,7 +334,7 @@ ssize virtual_memory_page_size( ssize* alignment_out ) #pragma endregion VirtualMemory -void* Arena::allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) +void* arena_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) { Arena* arena = rcast(Arena*, allocator_data); void* ptr = NULL; diff --git a/project/dependencies/memory.hpp b/project/dependencies/memory.hpp index a327ba9..2a704c7 100644 --- a/project/dependencies/memory.hpp +++ b/project/dependencies/memory.hpp @@ -170,127 +170,180 @@ b32 gen_vm_purge( VirtualMemory vm ); //! Retrieve VM's page size and alignment. ssize gen_virtual_memory_page_size( ssize* alignment_out ); - +#pragma region Arena struct Arena; -void init_from_memory( Arena& arena, void* start, ssize size ); -struct Arena -{ - static - void* allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ); +AllocatorInfo allocator_info( Arena& arena ); - //forceinline static - //Arena init_from_memory( void* start, ssize size ) { - // Arena result; GEN_NS init_from_memory( result, start, size ); - // return result; - //} +// Remove static keyword and rename allocator_proc +void* arena_allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags); - static - Arena init_from_allocator( AllocatorInfo backing, ssize size ) - { - Arena result = - { - backing, - alloc( backing, size), - size, - 0, - 0 - }; - return result; - } - - static - Arena init_sub( Arena& parent, ssize size ) - { - return init_from_allocator( parent.Backing, size ); - } - - ssize alignment_of( ssize alignment ) - { - ssize alignment_offset, result_pointer, mask; - GEN_ASSERT( is_power_of_two( alignment ) ); - - alignment_offset = 0; - result_pointer = (ssize) PhysicalStart + TotalUsed; - mask = alignment - 1; - - if ( result_pointer & mask ) - alignment_offset = alignment - ( result_pointer & mask ); - - return alignment_offset; - } +// Add these declarations after the Arena struct +Arena init_from_allocator(AllocatorInfo backing, ssize size); +Arena init_from_memory( void* start, ssize size ); +Arena init_sub(Arena& parent, ssize size); +ssize alignment_of(Arena& arena, ssize alignment); // This id is defined by Unreal for asserts #pragma push_macro("check") #undef check - void check() - { - GEN_ASSERT( TempCount == 0 ); - } +void check(Arena& arena); #pragma pop_macro("check") - void free() - { - if ( Backing.Proc ) - { - gen::free( Backing, PhysicalStart ); - PhysicalStart = nullptr; - } - } - - ssize size_remaining( ssize alignment ) - { - ssize result = TotalSize - ( TotalUsed + alignment_of( alignment ) ); - return result; - } +void free(Arena& arena); +ssize size_remaining(Arena& arena, ssize alignment); +struct Arena +{ AllocatorInfo Backing; void* PhysicalStart; ssize TotalSize; ssize TotalUsed; ssize TempCount; - operator AllocatorInfo() { return { allocator_proc, this }; } +#if 1 +#pragma region Member Mapping + forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); } + + forceinline static void* allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) { return GEN_NS arena_allocator_proc( allocator_data, type, size, alignment, old_memory, old_size, flags ); } + forceinline static Arena init_from_memory( void* start, ssize size ) { return GEN_NS init_from_memory( start, size ); } + forceinline static Arena init_from_allocator( AllocatorInfo backing, ssize size ) { return GEN_NS init_from_allocator( backing, size ); } + forceinline static Arena init_sub( Arena& parent, ssize size ) { return GEN_NS init_from_allocator( parent.Backing, size ); } + forceinline ssize alignment_of( ssize alignment ) { return GEN_NS alignment_of(* this, alignment); } + forceinline void free() { return GEN_NS free(* this); } + forceinline ssize size_remaining( ssize alignment ) { return GEN_NS size_remaining(* this, alignment); } + +// This id is defined by Unreal for asserts +#pragma push_macro("check") +#undef check + forceinline void check() { GEN_NS check(* this); } +#pragma pop_macro("check") + +#pragma endregion Member Mapping +#endif }; -void init_from_memory( Arena& arena, void* start, ssize size ) +inline +AllocatorInfo allocator_info( Arena& arena ) { + return { arena_allocator_proc, &arena }; +} + +inline +Arena init_from_memory( void* start, ssize size ) { - arena = - { + Arena arena = { { nullptr, nullptr }, start, size, 0, 0 }; + return arena; } +inline +Arena init_from_allocator(AllocatorInfo backing, ssize size) +{ + Arena result = + { + backing, + alloc(backing, size), + size, + 0, + 0 + }; + return result; +} + +inline +Arena init_sub(Arena& parent, ssize size) +{ + return init_from_allocator(parent.Backing, size); +} + +inline +ssize alignment_of(Arena& arena, ssize alignment) +{ + ssize alignment_offset, result_pointer, mask; + GEN_ASSERT(is_power_of_two(alignment)); + + alignment_offset = 0; + result_pointer = (ssize)arena.PhysicalStart + arena.TotalUsed; + mask = alignment - 1; + + if (result_pointer & mask) + alignment_offset = alignment - (result_pointer & mask); + + return alignment_offset; +} + +#pragma push_macro("check") +#undef check +inline +void check(Arena& arena) +{ + GEN_ASSERT(arena.TempCount == 0); +} +#pragma pop_macro("check") + +inline +void free(Arena& arena) +{ + if (arena.Backing.Proc) + { + gen::free(arena.Backing, arena.PhysicalStart); + arena.PhysicalStart = nullptr; + } +} + +inline +ssize size_remaining(Arena& arena, ssize alignment) +{ + ssize result = arena.TotalSize - (arena.TotalUsed + alignment_of(arena, alignment)); + return result; +} +#pragma endregion Arena + +#pragma region FixedArena +template +struct FixedArena; + +template AllocatorInfo allocator_info( FixedArena& fixed_arena ); +template FixedArena fixed_arena_init(); +template ssize size_remaining(FixedArena& fixed_arena, ssize alignment); + // Just a wrapper around using an arena with memory associated with its scope instead of from an allocator. // Used for static segment or stack allocations. template< s32 Size > struct FixedArena { - static - FixedArena init() - { - FixedArena result = { Arena::init_from_memory( result.memory, Size ), {0} }; - return result; - } + char memory[Size]; + Arena arena; - ssize size_remaining( ssize alignment ) - { - return arena.size_remaining( alignment ); - } +#if 1 +#pragma region Member Mapping + forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); } - operator AllocatorInfo() - { - return { Arena::allocator_proc, &arena }; - } - - Arena arena; - char memory[ Size ]; + forceinline static FixedArena init() { FixedArena result; GEN_NS fixed_arena_init(result); return result; } + forceinline ssize size_remaining(ssize alignment) { GEN_NS size_remaining(*this, alignment); } +#pragma endregion Member Mapping +#endif }; +template inline +AllocatorInfo allocator_info( FixedArena& fixed_arena ) { return { arena_allocator_proc, & fixed_arena.arena }; } + +template +void fixed_arena_init(FixedArena& result) { + zero_size(& result.memory[0], Size); + result.arena = init_from_memory(& result.memory[0], Size); +} + +template inline +ssize size_remaining(FixedArena& fixed_arena, ssize alignment) { + return size_remaining(fixed_arena.arena, alignment); +} + using Arena_1KB = FixedArena< kilobytes( 1 ) >; using Arena_4KB = FixedArena< kilobytes( 4 ) >; using Arena_8KB = FixedArena< kilobytes( 8 ) >; @@ -303,6 +356,7 @@ using Arena_512KB = FixedArena< kilobytes( 512 ) >; using Arena_1MB = FixedArena< megabytes( 1 ) >; using Arena_2MB = FixedArena< megabytes( 2 ) >; using Arena_4MB = FixedArena< megabytes( 4 ) >; +#pragma endregion FixedArena struct Pool { @@ -342,7 +396,6 @@ struct Pool } }; - inline b32 is_power_of_two( ssize x ) { if ( x <= 0 ) From 4137ebfbd8a07aeb16dfa7f56c901963e40189bb Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 12:27:54 -0500 Subject: [PATCH 003/112] pool done (see previous commits for context) --- .vscode/settings.json | 3 +- project/dependencies/memory.cpp | 16 ++++----- project/dependencies/memory.hpp | 63 +++++++++++++++++++-------------- 3 files changed, 47 insertions(+), 35 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 6e65a84..65611a9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -37,7 +37,8 @@ "propidl.h": "c", "android_native_app_glue.h": "c", "raylib.h": "c", - "*.m": "cpp" + "*.m": "cpp", + "atomic": "cpp" }, "C_Cpp.intelliSenseEngineFallback": "disabled", "mesonbuild.configureOnOpen": true, diff --git a/project/dependencies/memory.cpp b/project/dependencies/memory.cpp index e51fb09..f11cb47 100644 --- a/project/dependencies/memory.cpp +++ b/project/dependencies/memory.cpp @@ -384,7 +384,7 @@ void* arena_allocator_proc( void* allocator_data, AllocType type, ssize size, ss return ptr; } -void* Pool::allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) +void* pool_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) { Pool* pool = rcast( Pool*, allocator_data); void* ptr = NULL; @@ -457,7 +457,7 @@ void* Pool::allocator_proc( void* allocator_data, AllocType type, ssize size, ss return ptr; } -Pool Pool::init_align( AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align ) +Pool pool_init_align( AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align ) { Pool pool = {}; @@ -495,16 +495,16 @@ Pool Pool::init_align( AllocatorInfo backing, ssize num_blocks, ssize block_size return pool; } -void Pool::clear() +void clear(Pool& pool) { - ssize actual_block_size, block_index; + ssize actual_block_size, block_index; void* curr; uptr* end; - actual_block_size = BlockSize + BlockAlign; + actual_block_size = pool.BlockSize + pool.BlockAlign; - curr = PhysicalStart; - for ( block_index = 0; block_index < NumBlocks - 1; block_index++ ) + curr = pool.PhysicalStart; + for ( block_index = 0; block_index < pool.NumBlocks - 1; block_index++ ) { uptr* next = ( uptr* ) curr; *next = ( uptr ) curr + actual_block_size; @@ -514,7 +514,7 @@ void Pool::clear() end = ( uptr* ) curr; *end = ( uptr ) NULL; - FreeList = PhysicalStart; + pool.FreeList = pool.PhysicalStart; } #pragma endregion Memory diff --git a/project/dependencies/memory.hpp b/project/dependencies/memory.hpp index 2a704c7..7e2d857 100644 --- a/project/dependencies/memory.hpp +++ b/project/dependencies/memory.hpp @@ -358,30 +358,18 @@ using Arena_2MB = FixedArena< megabytes( 2 ) >; using Arena_4MB = FixedArena< megabytes( 4 ) >; #pragma endregion FixedArena +#pragma region Pool +struct Pool; + +AllocatorInfo allocator_info(Pool& pool); +void* pool_allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags); +Pool pool_init(AllocatorInfo backing, ssize num_blocks, ssize block_size); +Pool pool_init_align(AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align); +void clear(Pool& pool); +void free(Pool& pool); + struct Pool { - static - void* allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ); - - static - Pool init( AllocatorInfo backing, ssize num_blocks, ssize block_size ) - { - return init_align( backing, num_blocks, block_size, GEN_DEFAULT_MEMORY_ALIGNMENT ); - } - - static - Pool init_align( AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align ); - - void clear(); - - void free() - { - if ( Backing.Proc ) - { - gen::free( Backing, PhysicalStart ); - } - } - AllocatorInfo Backing; void* PhysicalStart; void* FreeList; @@ -390,12 +378,35 @@ struct Pool ssize TotalSize; ssize NumBlocks; - operator AllocatorInfo() - { - return { allocator_proc, this }; - } +#pragma region Member Mapping + forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); } + + forceinline static void* allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags) { return GEN_NS pool_allocator_proc(allocator_data, type, size, alignment, old_memory, old_size, flags); } + forceinline static Pool init(AllocatorInfo backing, ssize num_blocks, ssize block_size) { return GEN_NS pool_init(backing, num_blocks, block_size); } + forceinline static Pool init_align(AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align) { return GEN_NS pool_init_align(backing, num_blocks, block_size, block_align); } + forceinline void clear() { GEN_NS clear(* this); } + forceinline void free() { GEN_NS free(* this); } +#pragma endregion }; +inline +AllocatorInfo allocator_info(Pool& pool) { + return { pool_allocator_proc, &pool }; +} + +inline +Pool pool_init(AllocatorInfo backing, ssize num_blocks, ssize block_size) { + return pool_init_align(backing, num_blocks, block_size, GEN_DEFAULT_MEMORY_ALIGNMENT); +} + +inline +void free(Pool& pool) { + if(pool.Backing.Proc) { + GEN_NS free(pool.Backing, pool.PhysicalStart); + } +} +#pragma endregion Pool + inline b32 is_power_of_two( ssize x ) { if ( x <= 0 ) From 34eec66f357ef2fd549544ac6728e597253cd292 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 13:14:47 -0500 Subject: [PATCH 004/112] Array done --- project/components/parser.cpp | 2 +- project/dependencies/containers.hpp | 610 +++++++++++++++------------- project/dependencies/memory.hpp | 2 +- 3 files changed, 332 insertions(+), 282 deletions(-) diff --git a/project/components/parser.cpp b/project/components/parser.cpp index e1fe6a2..d44d748 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -133,7 +133,7 @@ internal void init() { Tokens = Array::init_reserve( LexArena - , ( LexAllocator_Size - sizeof( Array::Header ) ) / sizeof(Token) + , ( LexAllocator_Size - sizeof( ArrayHeader ) ) / sizeof(Token) ); defines_map_arena = Arena_256KB::init(); diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index 7dbb530..5ef762d 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -13,291 +13,341 @@ template struct RemoveConst { typede template using TRemoveConst = typename RemoveConst::Type; +#pragma region Array +struct ArrayHeader; +template struct Array; + +template Array array_init(AllocatorInfo allocator); +template Array array_init_reserve(AllocatorInfo allocator, ssize capacity); +template usize array_grow_formula(ssize value); +template bool append(Array& array, Array other); +template bool append(Array& array, Type value); +template bool append(Array& array, Type* items, usize item_num); +template bool append_at(Array& array, Type item, usize idx); +template bool append_at(Array& array, Type* items, usize item_num, usize idx); +template Type& back(Array& array); +template void clear(Array& array); +template bool fill(Array& array, usize begin, usize end, Type value); +template void free(Array& array); +template bool grow(Array& array, usize min_capacity); +template usize num(Array& array); +template void pop(Array& array); +template void remove_at(Array& array, usize idx); +template bool reserve(Array& array, usize new_capacity); +template bool resize(Array& array, usize num); +template bool set_capacity(Array& array, usize new_capacity); +template ArrayHeader* get_header(Array& array); + +struct ArrayHeader +{ + AllocatorInfo Allocator; + usize Capacity; + usize Num; +}; + template struct Array { - struct Header - { - AllocatorInfo Allocator; - usize Capacity; - usize Num; - }; - - static - Array init( AllocatorInfo allocator ) - { - return init_reserve( allocator, grow_formula(0) ); - } - - static - Array init_reserve( AllocatorInfo allocator, ssize capacity ) - { - Header* header = rcast( Header*, alloc( allocator, sizeof(Header) + sizeof(Type) * capacity )); - - if ( header == nullptr ) - return { nullptr }; - - header->Allocator = allocator; - header->Capacity = capacity; - header->Num = 0; - - return { rcast( Type*, header + 1) }; - } - - static - usize grow_formula( usize value ) - { - return 2 * value + 8; - } - - bool append( Array other ) - { - return append( other, other.num() ); - } - - bool append( Type value ) - { - Header* header = get_header(); - - if ( header->Num == header->Capacity ) - { - if ( ! grow( header->Capacity )) - return false; - - header = get_header(); - } - - Data[ header->Num ] = value; - header->Num++; - - return true; - } - - bool append( Type* items, usize item_num ) - { - Header* header = get_header(); - - if ( header->Num + item_num > header->Capacity ) - { - if ( ! grow( header->Capacity + item_num )) - return false; - - header = get_header(); - } - - mem_copy( Data + header->Num, items, item_num * sizeof(Type) ); - header->Num += item_num; - - return true; - } - - bool append_at( Type item, usize idx ) - { - Header* header = get_header(); - - if ( idx >= header->Num ) - idx = header->Num - 1; - - if ( idx < 0 ) - idx = 0; - - if ( header->Capacity < header->Num + 1 ) - { - if ( ! grow( header->Capacity + 1 )) - return false; - - header = get_header(); - } - - Type* target = Data + idx; - - mem_move( target + 1, target, (header->Num - idx) * sizeof(Type) ); - header->Num++; - - return true; - } - - bool append_at( Type* items, usize item_num, usize idx ) - { - Header* header = get_header(); - - if ( idx >= header->Num ) - { - return append( items, item_num ); - } - - if ( item_num > header->Capacity ) - { - if ( ! grow( header->Capacity + item_num ) ) - return false; - - header = get_header(); - } - - Type* target = Data + idx + item_num; - Type* src = Data + idx; - - mem_move( target, src, (header->Num - idx) * sizeof(Type) ); - mem_copy( src, items, item_num * sizeof(Type) ); - header->Num += item_num; - - return true; - } - - Type& back( void ) - { - Header& header = * get_header(); - return Data[ header.Num - 1 ]; - } - - void clear( void ) - { - Header& header = * get_header(); - header.Num = 0; - } - - bool fill( usize begin, usize end, Type value ) - { - Header& header = * get_header(); - - if ( begin < 0 || end > header.Num ) - return false; - - for ( ssize idx = ssize(begin); idx < ssize(end); idx++ ) - { - Data[ idx ] = value; - } - - return true; - } - - void free( void ) - { - Header& header = * get_header(); - gen::free( header.Allocator, &header ); - Data = nullptr; - } - - Header* get_header( void ) - { - using NonConstType = TRemoveConst< Type >; - return rcast( Header*, const_cast(Data) ) - 1 ; - } - - bool grow( usize min_capacity ) - { - Header& header = * get_header(); - usize new_capacity = grow_formula( header.Capacity ); - - if ( new_capacity < min_capacity ) - new_capacity = min_capacity; - - return set_capacity( new_capacity ); - } - - usize num( void ) - { - return get_header()->Num; - } - - void pop( void ) - { - Header& header = * get_header(); - - GEN_ASSERT( header.Num > 0 ); - header.Num--; - } - - void remove_at( usize idx ) - { - Header* header = get_header(); - GEN_ASSERT( idx < header->Num ); - - mem_move( header + idx, header + idx + 1, sizeof( Type ) * ( header->Num - idx - 1 ) ); - header->Num--; - } - - bool reserve( usize new_capacity ) - { - Header& header = * get_header(); - - if ( header.Capacity < new_capacity ) - return set_capacity( new_capacity ); - - return true; - } - - bool resize( usize num ) - { - Header* header = get_header(); - - if ( header->Capacity < num ) - { - if ( ! grow( num ) ) - return false; - - header = get_header(); - } - - header->Num = num; - return true; - } - - bool set_capacity( usize new_capacity ) - { - Header& header = * get_header(); - - if ( new_capacity == header.Capacity ) - return true; - - if ( new_capacity < header.Num ) - { - // Already have the memory, mine as well keep it. - header.Num = new_capacity; - return true; - } - - ssize size = sizeof( Header ) + sizeof( Type ) * new_capacity; - Header* new_header = rcast( Header*, alloc( header.Allocator, size ) ); - - if ( new_header == nullptr ) - return false; - - mem_move( new_header, &header, sizeof( Header ) + sizeof( Type ) * header.Num ); - - new_header->Capacity = new_capacity; - - gen::free( header.Allocator, &header ); - - Data = rcast( Type*, new_header + 1); - return true; - } - - Type* Data; - - operator Type*() - { - return Data; - } - - operator Type const*() const - { - return Data; - } - - // For-range based support - - Type* begin() - { - return Data; - } - - Type* end() - { - return Data + get_header()->Num; - } + Type* Data; + +#if 1 +#pragma region Member Mapping + forceinline static Array init(AllocatorInfo allocator) { return GEN_NS array_init(allocator); } + forceinline static Array init_reserve(AllocatorInfo allocator, ssize capacity) { return GEN_NS array_init_reserve(allocator, capacity); } + forceinline static usize grow_formula(ssize value) { return GEN_NS array_grow_formula(value); } + + forceinline bool append(Array other) { return GEN_NS append(*this, other); } + forceinline bool append(Type value) { return GEN_NS append(*this, value); } + forceinline bool append(Type* items, usize item_num) { return GEN_NS append(*this, items, item_num); } + forceinline bool append_at(Type item, usize idx) { return GEN_NS append_at(*this, item, idx); } + forceinline bool append_at(Type* items, usize item_num, usize idx) { return GEN_NS append_at(*this, items, item_num, idx); } + forceinline Type& back() { return GEN_NS back(*this); } + forceinline void clear() { GEN_NS clear(*this); } + forceinline bool fill(usize begin, usize end, Type value) { return GEN_NS fill(*this, begin, end, value); } + forceinline void free() { GEN_NS free(*this); } + forceinline ArrayHeader* get_header() { return GEN_NS get_header(*this); } + forceinline bool grow(usize min_capacity) { return GEN_NS grow(*this, min_capacity); } + forceinline usize num() { return GEN_NS num(*this); } + forceinline void pop() { GEN_NS pop(*this); } + forceinline void remove_at(usize idx) { GEN_NS remove_at(*this, idx); } + forceinline bool reserve(usize new_capacity) { return GEN_NS reserve(*this, new_capacity); } + forceinline bool resize(usize num) { return GEN_NS resize(*this, num); } + forceinline bool set_capacity(usize new_capacity) { return GEN_NS set_capacity(*this, new_capacity); } + + forceinline operator Type*() { return Data; } + forceinline operator Type const*() const { return Data; } + forceinline Type* begin() { return Data; } + forceinline Type* end() { return Data + get_header()->Num; } +#pragma endregion Member Mapping +#endif }; +template inline +Array array_init(AllocatorInfo allocator) +{ + return array_init_reserve(allocator, array_grow_formula(0)); +} + +template inline +Array array_init_reserve(AllocatorInfo allocator, ssize capacity) +{ + ArrayHeader* header = rcast(ArrayHeader*, alloc(allocator, sizeof(ArrayHeader) + sizeof(Type) * capacity)); + + if (header == nullptr) + return {nullptr}; + + header->Allocator = allocator; + header->Capacity = capacity; + header->Num = 0; + + return {rcast(Type*, header + 1)}; +} + +template inline +usize array_grow_formula(ssize value) +{ + return 2 * value + 8; +} + +template inline +bool append(Array& array, Array other) +{ + return append(array, other, num(other)); +} + +template inline +bool append(Array& array, Type value) +{ + ArrayHeader* header = get_header(array); + + if (header->Num == header->Capacity) + { + if (!grow(array, header->Capacity)) + return false; + + header = get_header(array); + } + + array.Data[header->Num] = value; + header->Num++; + + return true; +} + +template inline +bool append(Array& array, Type* items, usize item_num) +{ + ArrayHeader* header = get_header(array); + + if (header->Num + item_num > header->Capacity) + { + if (!grow(array, header->Capacity + item_num)) + return false; + + header = get_header(array); + } + + mem_copy(array.Data + header->Num, items, item_num * sizeof(Type)); + header->Num += item_num; + + return true; +} + +template inline +bool append_at(Array& array, Type item, usize idx) +{ + ArrayHeader* header = get_header(array); + + if (idx >= header->Num) + idx = header->Num - 1; + + if (idx < 0) + idx = 0; + + if (header->Capacity < header->Num + 1) + { + if (!grow(array, header->Capacity + 1)) + return false; + + header = get_header(array); + } + + Type* target = array.Data + idx; + + mem_move(target + 1, target, (header->Num - idx) * sizeof(Type)); + header->Num++; + + return true; +} + +template inline +bool append_at(Array& array, Type* items, usize item_num, usize idx) +{ + ArrayHeader* header = get_header(array); + + if (idx >= header->Num) + { + return append(array, items, item_num); + } + + if (item_num > header->Capacity) + { + if (!grow(array, header->Capacity + item_num)) + return false; + + header = get_header(array); + } + + Type* target = array.Data + idx + item_num; + Type* src = array.Data + idx; + + mem_move(target, src, (header->Num - idx) * sizeof(Type)); + mem_copy(src, items, item_num * sizeof(Type)); + header->Num += item_num; + + return true; +} + +template inline +Type& back(Array& array) +{ + ArrayHeader* header = get_header(array); + return array.Data[header->Num - 1]; +} + +template inline +void clear(Array& array) +{ + ArrayHeader* header = get_header(array); + header->Num = 0; +} + +template inline +bool fill(Array& array, usize begin, usize end, Type value) +{ + ArrayHeader* header = get_header(array); + + if (begin < 0 || end > header->Num) + return false; + + for (ssize idx = ssize(begin); idx < ssize(end); idx++) + { + array.Data[idx] = value; + } + + return true; +} + +template inline +void free(Array& array) +{ + ArrayHeader* header = get_header(array); + gen::free(header->Allocator, header); + array.Data = nullptr; +} + +template inline +ArrayHeader* get_header(Array& array) +{ + using NonConstType = TRemoveConst; + return rcast(ArrayHeader*, const_cast(array.Data)) - 1; +} + +template inline +bool grow(Array& array, usize min_capacity) +{ + ArrayHeader* header = get_header(array); + usize new_capacity = array_grow_formula(header->Capacity); + + if (new_capacity < min_capacity) + new_capacity = min_capacity; + + return set_capacity(array, new_capacity); +} + +template inline +usize num(Array& array) +{ + return get_header(array)->Num; +} + +template inline +void pop(Array& array) +{ + ArrayHeader* header = get_header(array); + GEN_ASSERT(header->Num > 0); + header->Num--; +} + +template inline +void remove_at(Array& array, usize idx) +{ + ArrayHeader* header = get_header(array); + GEN_ASSERT(idx < header->Num); + + mem_move(array.Data + idx, array.Data + idx + 1, sizeof(Type) * (header->Num - idx - 1)); + header->Num--; +} + +template inline +bool reserve(Array& array, usize new_capacity) +{ + ArrayHeader* header = get_header(array); + + if (header->Capacity < new_capacity) + return set_capacity(array, new_capacity); + + return true; +} + +template inline +bool resize(Array& array, usize num) +{ + ArrayHeader* header = get_header(array); + + if (header->Capacity < num) + { + if (!grow(array, num)) + return false; + + header = get_header(array); + } + + header->Num = num; + return true; +} + +template inline +bool set_capacity(Array& array, usize new_capacity) +{ + ArrayHeader* header = get_header(array); + + if (new_capacity == header->Capacity) + return true; + + if (new_capacity < header->Num) + { + header->Num = new_capacity; + return true; + } + + ssize size = sizeof(ArrayHeader) + sizeof(Type) * new_capacity; + ArrayHeader* new_header = rcast(ArrayHeader*, alloc(header->Allocator, size)); + + if (new_header == nullptr) + return false; + + mem_move(new_header, header, sizeof(ArrayHeader) + sizeof(Type) * header->Num); + + new_header->Capacity = new_capacity; + + gen::free(header->Allocator, header); + + array.Data = rcast(Type*, new_header + 1); + return true; +} +#pragma endregion Array + // TODO(Ed) : This thing needs ALOT of work. template diff --git a/project/dependencies/memory.hpp b/project/dependencies/memory.hpp index 7e2d857..bee56f3 100644 --- a/project/dependencies/memory.hpp +++ b/project/dependencies/memory.hpp @@ -333,7 +333,7 @@ struct FixedArena template inline AllocatorInfo allocator_info( FixedArena& fixed_arena ) { return { arena_allocator_proc, & fixed_arena.arena }; } -template +template inline void fixed_arena_init(FixedArena& result) { zero_size(& result.memory[0], Size); result.arena = init_from_memory(& result.memory[0], Size); From c6cb5835185a5d9ca71eb7fd63ef59eb778d8a4f Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 13:31:59 -0500 Subject: [PATCH 005/112] Hashtable done --- project/dependencies/containers.hpp | 836 ++++++++++++++-------------- 1 file changed, 427 insertions(+), 409 deletions(-) diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index 5ef762d..ecea1da 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -38,8 +38,7 @@ template bool resize(Array& array, usize num); template bool set_capacity(Array& array, usize new_capacity); template ArrayHeader* get_header(Array& array); -struct ArrayHeader -{ +struct ArrayHeader { AllocatorInfo Allocator; usize Capacity; usize Num; @@ -48,554 +47,573 @@ struct ArrayHeader template struct Array { - Type* Data; + Type* Data; #if 1 #pragma region Member Mapping - forceinline static Array init(AllocatorInfo allocator) { return GEN_NS array_init(allocator); } - forceinline static Array init_reserve(AllocatorInfo allocator, ssize capacity) { return GEN_NS array_init_reserve(allocator, capacity); } - forceinline static usize grow_formula(ssize value) { return GEN_NS array_grow_formula(value); } + forceinline static Array init(AllocatorInfo allocator) { return GEN_NS array_init(allocator); } + forceinline static Array init_reserve(AllocatorInfo allocator, ssize capacity) { return GEN_NS array_init_reserve(allocator, capacity); } + forceinline static usize grow_formula(ssize value) { return GEN_NS array_grow_formula(value); } - forceinline bool append(Array other) { return GEN_NS append(*this, other); } - forceinline bool append(Type value) { return GEN_NS append(*this, value); } - forceinline bool append(Type* items, usize item_num) { return GEN_NS append(*this, items, item_num); } - forceinline bool append_at(Type item, usize idx) { return GEN_NS append_at(*this, item, idx); } - forceinline bool append_at(Type* items, usize item_num, usize idx) { return GEN_NS append_at(*this, items, item_num, idx); } - forceinline Type& back() { return GEN_NS back(*this); } - forceinline void clear() { GEN_NS clear(*this); } - forceinline bool fill(usize begin, usize end, Type value) { return GEN_NS fill(*this, begin, end, value); } - forceinline void free() { GEN_NS free(*this); } - forceinline ArrayHeader* get_header() { return GEN_NS get_header(*this); } - forceinline bool grow(usize min_capacity) { return GEN_NS grow(*this, min_capacity); } - forceinline usize num() { return GEN_NS num(*this); } - forceinline void pop() { GEN_NS pop(*this); } - forceinline void remove_at(usize idx) { GEN_NS remove_at(*this, idx); } - forceinline bool reserve(usize new_capacity) { return GEN_NS reserve(*this, new_capacity); } - forceinline bool resize(usize num) { return GEN_NS resize(*this, num); } - forceinline bool set_capacity(usize new_capacity) { return GEN_NS set_capacity(*this, new_capacity); } + forceinline bool append(Array other) { return GEN_NS append(*this, other); } + forceinline bool append(Type value) { return GEN_NS append(*this, value); } + forceinline bool append(Type* items, usize item_num) { return GEN_NS append(*this, items, item_num); } + forceinline bool append_at(Type item, usize idx) { return GEN_NS append_at(*this, item, idx); } + forceinline bool append_at(Type* items, usize item_num, usize idx) { return GEN_NS append_at(*this, items, item_num, idx); } + forceinline Type& back() { return GEN_NS back(*this); } + forceinline void clear() { GEN_NS clear(*this); } + forceinline bool fill(usize begin, usize end, Type value) { return GEN_NS fill(*this, begin, end, value); } + forceinline void free() { GEN_NS free(*this); } + forceinline ArrayHeader* get_header() { return GEN_NS get_header(*this); } + forceinline bool grow(usize min_capacity) { return GEN_NS grow(*this, min_capacity); } + forceinline usize num() { return GEN_NS num(*this); } + forceinline void pop() { GEN_NS pop(*this); } + forceinline void remove_at(usize idx) { GEN_NS remove_at(*this, idx); } + forceinline bool reserve(usize new_capacity) { return GEN_NS reserve(*this, new_capacity); } + forceinline bool resize(usize num) { return GEN_NS resize(*this, num); } + forceinline bool set_capacity(usize new_capacity) { return GEN_NS set_capacity(*this, new_capacity); } - forceinline operator Type*() { return Data; } - forceinline operator Type const*() const { return Data; } - forceinline Type* begin() { return Data; } - forceinline Type* end() { return Data + get_header()->Num; } + forceinline operator Type*() { return Data; } + forceinline operator Type const*() const { return Data; } + forceinline Type* begin() { return Data; } + forceinline Type* end() { return Data + get_header()->Num; } #pragma endregion Member Mapping #endif }; template inline -Array array_init(AllocatorInfo allocator) -{ - return array_init_reserve(allocator, array_grow_formula(0)); +Array array_init(AllocatorInfo allocator) { + return array_init_reserve(allocator, array_grow_formula(0)); } template inline Array array_init_reserve(AllocatorInfo allocator, ssize capacity) { - ArrayHeader* header = rcast(ArrayHeader*, alloc(allocator, sizeof(ArrayHeader) + sizeof(Type) * capacity)); + ArrayHeader* header = rcast(ArrayHeader*, alloc(allocator, sizeof(ArrayHeader) + sizeof(Type) * capacity)); - if (header == nullptr) - return {nullptr}; + if (header == nullptr) + return {nullptr}; - header->Allocator = allocator; - header->Capacity = capacity; - header->Num = 0; + header->Allocator = allocator; + header->Capacity = capacity; + header->Num = 0; - return {rcast(Type*, header + 1)}; + return {rcast(Type*, header + 1)}; } template inline -usize array_grow_formula(ssize value) -{ - return 2 * value + 8; +usize array_grow_formula(ssize value) { + return 2 * value + 8; } template inline -bool append(Array& array, Array other) -{ - return append(array, other, num(other)); +bool append(Array& array, Array other) { + return append(array, other, num(other)); } template inline bool append(Array& array, Type value) { - ArrayHeader* header = get_header(array); + ArrayHeader* header = get_header(array); - if (header->Num == header->Capacity) - { - if (!grow(array, header->Capacity)) - return false; + if (header->Num == header->Capacity) + { + if (!grow(array, header->Capacity)) + return false; + header = get_header(array); + } - header = get_header(array); - } + array.Data[header->Num] = value; + header->Num++; - array.Data[header->Num] = value; - header->Num++; - - return true; + return true; } template inline bool append(Array& array, Type* items, usize item_num) { - ArrayHeader* header = get_header(array); + ArrayHeader* header = get_header(array); - if (header->Num + item_num > header->Capacity) - { - if (!grow(array, header->Capacity + item_num)) - return false; + if (header->Num + item_num > header->Capacity) + { + if (!grow(array, header->Capacity + item_num)) + return false; + header = get_header(array); + } - header = get_header(array); - } + mem_copy(array.Data + header->Num, items, item_num * sizeof(Type)); + header->Num += item_num; - mem_copy(array.Data + header->Num, items, item_num * sizeof(Type)); - header->Num += item_num; - - return true; + return true; } template inline bool append_at(Array& array, Type item, usize idx) { - ArrayHeader* header = get_header(array); + ArrayHeader* header = get_header(array); - if (idx >= header->Num) - idx = header->Num - 1; + if (idx >= header->Num) + idx = header->Num - 1; - if (idx < 0) - idx = 0; + if (idx < 0) + idx = 0; - if (header->Capacity < header->Num + 1) - { - if (!grow(array, header->Capacity + 1)) - return false; + if (header->Capacity < header->Num + 1) + { + if (!grow(array, header->Capacity + 1)) + return false; - header = get_header(array); - } + header = get_header(array); + } - Type* target = array.Data + idx; + Type* target = array.Data + idx; - mem_move(target + 1, target, (header->Num - idx) * sizeof(Type)); - header->Num++; + mem_move(target + 1, target, (header->Num - idx) * sizeof(Type)); + header->Num++; - return true; + return true; } template inline bool append_at(Array& array, Type* items, usize item_num, usize idx) { - ArrayHeader* header = get_header(array); + ArrayHeader* header = get_header(array); - if (idx >= header->Num) - { - return append(array, items, item_num); - } + if (idx >= header->Num) + { + return append(array, items, item_num); + } - if (item_num > header->Capacity) - { - if (!grow(array, header->Capacity + item_num)) - return false; + if (item_num > header->Capacity) + { + if (!grow(array, header->Capacity + item_num)) + return false; - header = get_header(array); - } + header = get_header(array); + } - Type* target = array.Data + idx + item_num; - Type* src = array.Data + idx; + Type* target = array.Data + idx + item_num; + Type* src = array.Data + idx; - mem_move(target, src, (header->Num - idx) * sizeof(Type)); - mem_copy(src, items, item_num * sizeof(Type)); - header->Num += item_num; + mem_move(target, src, (header->Num - idx) * sizeof(Type)); + mem_copy(src, items, item_num * sizeof(Type)); + header->Num += item_num; - return true; + return true; } template inline -Type& back(Array& array) -{ - ArrayHeader* header = get_header(array); - return array.Data[header->Num - 1]; +Type& back(Array& array) { + ArrayHeader* header = get_header(array); + return array.Data[header->Num - 1]; } template inline -void clear(Array& array) -{ - ArrayHeader* header = get_header(array); - header->Num = 0; +void clear(Array& array) { + ArrayHeader* header = get_header(array); + header->Num = 0; } template inline bool fill(Array& array, usize begin, usize end, Type value) { - ArrayHeader* header = get_header(array); + ArrayHeader* header = get_header(array); - if (begin < 0 || end > header->Num) - return false; + if (begin < 0 || end > header->Num) + return false; - for (ssize idx = ssize(begin); idx < ssize(end); idx++) - { - array.Data[idx] = value; - } + for (ssize idx = ssize(begin); idx < ssize(end); idx++) + { + array.Data[idx] = value; + } - return true; + return true; } template inline -void free(Array& array) -{ - ArrayHeader* header = get_header(array); - gen::free(header->Allocator, header); - array.Data = nullptr; +void free(Array& array) { + ArrayHeader* header = get_header(array); + gen::free(header->Allocator, header); + array.Data = nullptr; } template inline -ArrayHeader* get_header(Array& array) -{ - using NonConstType = TRemoveConst; - return rcast(ArrayHeader*, const_cast(array.Data)) - 1; +ArrayHeader* get_header(Array& array) { + using NonConstType = TRemoveConst; + return rcast(ArrayHeader*, const_cast(array.Data)) - 1; } template inline bool grow(Array& array, usize min_capacity) { - ArrayHeader* header = get_header(array); - usize new_capacity = array_grow_formula(header->Capacity); + ArrayHeader* header = get_header(array); + usize new_capacity = array_grow_formula(header->Capacity); - if (new_capacity < min_capacity) - new_capacity = min_capacity; + if (new_capacity < min_capacity) + new_capacity = min_capacity; - return set_capacity(array, new_capacity); + return set_capacity(array, new_capacity); } template inline -usize num(Array& array) -{ - return get_header(array)->Num; +usize num(Array& array) { + return get_header(array)->Num; } template inline -void pop(Array& array) -{ - ArrayHeader* header = get_header(array); - GEN_ASSERT(header->Num > 0); - header->Num--; +void pop(Array& array) { + ArrayHeader* header = get_header(array); + GEN_ASSERT(header->Num > 0); + header->Num--; } template inline void remove_at(Array& array, usize idx) { - ArrayHeader* header = get_header(array); - GEN_ASSERT(idx < header->Num); + ArrayHeader* header = get_header(array); + GEN_ASSERT(idx < header->Num); - mem_move(array.Data + idx, array.Data + idx + 1, sizeof(Type) * (header->Num - idx - 1)); - header->Num--; + mem_move(array.Data + idx, array.Data + idx + 1, sizeof(Type) * (header->Num - idx - 1)); + header->Num--; } template inline bool reserve(Array& array, usize new_capacity) { - ArrayHeader* header = get_header(array); + ArrayHeader* header = get_header(array); - if (header->Capacity < new_capacity) - return set_capacity(array, new_capacity); + if (header->Capacity < new_capacity) + return set_capacity(array, new_capacity); - return true; + return true; } template inline bool resize(Array& array, usize num) { - ArrayHeader* header = get_header(array); + ArrayHeader* header = get_header(array); - if (header->Capacity < num) - { - if (!grow(array, num)) - return false; + if (header->Capacity < num) { + if (!grow(array, num)) + return false; + header = get_header(array); + } - header = get_header(array); - } - - header->Num = num; - return true; + header->Num = num; + return true; } template inline bool set_capacity(Array& array, usize new_capacity) { - ArrayHeader* header = get_header(array); + ArrayHeader* header = get_header(array); - if (new_capacity == header->Capacity) - return true; + if (new_capacity == header->Capacity) + return true; - if (new_capacity < header->Num) - { - header->Num = new_capacity; - return true; - } + if (new_capacity < header->Num) + { + header->Num = new_capacity; + return true; + } - ssize size = sizeof(ArrayHeader) + sizeof(Type) * new_capacity; - ArrayHeader* new_header = rcast(ArrayHeader*, alloc(header->Allocator, size)); + ssize size = sizeof(ArrayHeader) + sizeof(Type) * new_capacity; + ArrayHeader* new_header = rcast(ArrayHeader*, alloc(header->Allocator, size)); - if (new_header == nullptr) - return false; + if (new_header == nullptr) + return false; - mem_move(new_header, header, sizeof(ArrayHeader) + sizeof(Type) * header->Num); + mem_move(new_header, header, sizeof(ArrayHeader) + sizeof(Type) * header->Num); - new_header->Capacity = new_capacity; + new_header->Capacity = new_capacity; - gen::free(header->Allocator, header); + GEN_NS free(header->Allocator, header); - array.Data = rcast(Type*, new_header + 1); - return true; + array.Data = rcast(Type*, new_header + 1); + return true; } #pragma endregion Array // TODO(Ed) : This thing needs ALOT of work. +#pragma region HashTable +template struct HashTable; + +struct HashTableFindResult { + ssize HashIndex; + ssize PrevIndex; + ssize EntryIndex; +}; + +template +struct HashTableEntry { + u64 Key; + ssize Next; + Type Value; +}; + +// Forward declarations for all lifted functions +template HashTable hashtable_init(AllocatorInfo allocator); +template HashTable hashtable_init_reserve(AllocatorInfo allocator, usize num); +template void clear(HashTable& table); +template void destroy(HashTable& table); +template Type* get(HashTable& table, u64 key); +template void grow(HashTable& table); +template void rehash(HashTable& table, ssize new_num); +template void rehash_fast(HashTable& table); +template void remove(HashTable& table, u64 key); +template void remove_entry(HashTable& table, ssize idx); +template void set(HashTable& table, u64 key, Type value); +template ssize slot(HashTable& table, u64 key); +template ssize add_entry(HashTable& table, u64 key); +template HashTableFindResult find(HashTable& table, u64 key); +template bool full(HashTable& table); +template void map(HashTable& table, void (*map_proc)(u64 key, Type value)); +template void map_mut(HashTable& table, void (*map_proc)(u64 key, Type* value)); + template struct HashTable { - struct FindResult - { - ssize HashIndex; - ssize PrevIndex; - ssize EntryIndex; - }; - - struct Entry - { - u64 Key; - ssize Next; - Type Value; - }; - static constexpr f32 CriticalLoadScale = 0.7f; - static - HashTable init( AllocatorInfo allocator ) - { - HashTable result = init_reserve(allocator, 8); - return result; - } + Array Hashes; + Array> Entries; - static - HashTable init_reserve( AllocatorInfo allocator, usize num ) - { - HashTable result = { { nullptr }, { nullptr } }; +#if 1 +#pragma region Member Mapping + forceinline static HashTable init(AllocatorInfo allocator) { return GEN_NS hashtable_init(allocator); } + forceinline static HashTable init_reserve(AllocatorInfo allocator, usize num) { return GEN_NS hashtable_init_reserve(allocator, num); } - result.Hashes = Array::init_reserve( allocator, num ); - result.Hashes.get_header()->Num = num; - result.Hashes.resize( num ); - result.Hashes.fill( 0, num, -1); - - result.Entries = Array::init_reserve( allocator, num ); - return result; - } - - void clear( void ) - { - Entries.clear(); - Hashes.fill( 0, Hashes.num(), -1); - } - - void destroy( void ) - { - if ( Hashes && Hashes.get_header()->Capacity ) - { - Hashes.free(); - Entries.free(); - } - } - - Type* get( u64 key ) - { - ssize idx = find( key ).EntryIndex; - if ( idx >= 0 ) - return & Entries[ idx ].Value; - - return nullptr; - } - - using MapProc = void (*)( u64 key, Type value ); - - void map( MapProc map_proc ) - { - GEN_ASSERT_NOT_NULL( map_proc ); - - for ( ssize idx = 0; idx < ssize(Entries.num()); ++idx ) - { - map_proc( Entries[ idx ].Key, Entries[ idx ].Value ); - } - } - - using MapMutProc = void (*)( u64 key, Type* value ); - - void map_mut( MapMutProc map_proc ) - { - GEN_ASSERT_NOT_NULL( map_proc ); - - for ( ssize idx = 0; idx < ssize(Entries.num()); ++idx ) - { - map_proc( Entries[ idx ].Key, & Entries[ idx ].Value ); - } - } - - void grow() - { - ssize new_num = Array::grow_formula( Entries.num() ); - rehash( new_num ); - } - - void rehash( ssize new_num ) - { - ssize last_added_index; - - HashTable new_ht = init_reserve( Hashes.get_header()->Allocator, new_num ); - for ( ssize idx = 0; idx < ssize(Entries.num()); ++idx ) - { - FindResult find_result; - - Entry& entry = Entries[ idx ]; - find_result = new_ht.find( entry.Key ); - last_added_index = new_ht.add_entry( entry.Key ); - - if ( find_result.PrevIndex < 0 ) - new_ht.Hashes[ find_result.HashIndex ] = last_added_index; - else - new_ht.Entries[ find_result.PrevIndex ].Next = last_added_index; - - new_ht.Entries[ last_added_index ].Next = find_result.EntryIndex; - new_ht.Entries[ last_added_index ].Value = entry.Value; - } - - destroy(); - *this = new_ht; - } - - void rehash_fast() - { - ssize idx; - - for ( idx = 0; idx < ssize(Entries.num()); idx++ ) - Entries[ idx ].Next = -1; - - for ( idx = 0; idx < ssize(Hashes.num()); idx++ ) - Hashes[ idx ] = -1; - - for ( idx = 0; idx < ssize(Entries.num()); idx++ ) - { - Entry* entry; - FindResult find_result; - - entry = & Entries[ idx ]; - find_result = find( entry->Key ); - - if ( find_result.PrevIndex < 0 ) - Hashes[ find_result.HashIndex ] = idx; - else - Entries[ find_result.PrevIndex ].Next = idx; - } - } - - void remove( u64 key ) - { - FindResult find_result = find( key); - - if ( find_result.EntryIndex >= 0 ) - { - Entries.remove_at( find_result.EntryIndex ); - rehash_fast(); - } - } - - void remove_entry( ssize idx ) - { - Entries.remove_at( idx ); - } - - void set( u64 key, Type value ) - { - ssize idx; - FindResult find_result; - - if ( full() ) - grow(); - - find_result = find( key ); - if ( find_result.EntryIndex >= 0 ) - { - idx = find_result.EntryIndex; - } - else - { - idx = add_entry( key ); - - if ( find_result.PrevIndex >= 0 ) - { - Entries[ find_result.PrevIndex ].Next = idx; - } - else - { - Hashes[ find_result.HashIndex ] = idx; - } - } - - Entries[ idx ].Value = value; - - if ( full() ) - grow(); - } - - ssize slot( u64 key ) - { - for ( ssize idx = 0; idx < ssize(Hashes.num()); ++idx ) - if ( Hashes[ idx ] == key ) - return idx; - - return -1; - } - - Array< ssize> Hashes; - Array< Entry> Entries; - -protected: - - ssize add_entry( u64 key ) - { - ssize idx; - Entry entry = { key, -1 }; - - idx = Entries.num(); - Entries.append( entry ); - return idx; - } - - FindResult find( u64 key ) - { - FindResult result = { -1, -1, -1 }; - - if ( Hashes.num() > 0 ) - { - result.HashIndex = key % Hashes.num(); - result.EntryIndex = Hashes[ result.HashIndex ]; - - while ( result.EntryIndex >= 0 ) - { - if ( Entries[ result.EntryIndex ].Key == key ) - break; - - result.PrevIndex = result.EntryIndex; - result.EntryIndex = Entries[ result.EntryIndex ].Next; - } - } - - return result; - } - - b32 full() - { - usize critical_load = usize( CriticalLoadScale * f32(Hashes.num()) ); - b32 result = Entries.num() > critical_load; - return result; - } + forceinline void clear() { GEN_NS clear(*this); } + forceinline void destroy() { GEN_NS destroy(*this); } + forceinline Type* get(u64 key) { return GEN_NS get(*this, key); } + forceinline void grow() { GEN_NS grow(*this); } + forceinline void rehash(ssize new_num) { GEN_NS rehash(*this, new_num); } + forceinline void rehash_fast() { GEN_NS rehash_fast(*this); } + forceinline void remove(u64 key) { GEN_NS remove(*this, key); } + forceinline void remove_entry(ssize idx) { GEN_NS remove_entry(*this, idx); } + forceinline void set(u64 key, Type value) { GEN_NS set(*this, key, value); } + forceinline ssize slot(u64 key) { return GEN_NS slot(*this, key); } + forceinline void map(void (*proc)(u64, Type)) { GEN_NS map(*this, proc); } + forceinline void map_mut(void (*proc)(u64, Type*)) { GEN_NS map_mut(*this, proc); } +#pragma endregion Member Mapping +#endif }; +template inline +HashTable hashtable_init(AllocatorInfo allocator) { + HashTable result = hashtable_init_reserve(allocator, 8); + return result; +} + +template inline +HashTable hashtable_init_reserve(AllocatorInfo allocator, usize num) +{ + HashTable result = { { nullptr }, { nullptr } }; + + result.Hashes = Array::init_reserve(allocator, num); + result.Hashes.get_header()->Num = num; + result.Hashes.resize(num); + result.Hashes.fill(0, num, -1); + + result.Entries = Array>::init_reserve(allocator, num); + return result; +} + +template inline +void clear(HashTable& table) { + table.Entries.clear(); + table.Hashes.fill(0, table.Hashes.num(), -1); +} + +template inline +void destroy(HashTable& table) { + if (table.Hashes && table.Hashes.get_header()->Capacity) { + table.Hashes.free(); + table.Entries.free(); + } +} + +template inline +Type* get(HashTable& table, u64 key) { + ssize idx = find(table, key).EntryIndex; + if (idx >= 0) + return &table.Entries[idx].Value; + + return nullptr; +} + +template inline +void map(HashTable& table, void (*map_proc)(u64 key, Type value)) { + GEN_ASSERT_NOT_NULL(map_proc); + + for (ssize idx = 0; idx < ssize(table.Entries.num()); ++idx) { + map_proc(table.Entries[idx].Key, table.Entries[idx].Value); + } +} + +template inline +void map_mut(HashTable& table, void (*map_proc)(u64 key, Type* value)) { + GEN_ASSERT_NOT_NULL(map_proc); + + for (ssize idx = 0; idx < ssize(table.Entries.num()); ++idx) { + map_proc(table.Entries[idx].Key, &table.Entries[idx].Value); + } +} + +template inline +void grow(HashTable& table) { + ssize new_num = Array>::grow_formula(table.Entries.num()); + rehash(table, new_num); +} + +template inline +void rehash(HashTable& table, ssize new_num) +{ + ssize last_added_index; + HashTable new_ht = hashtable_init_reserve(table.Hashes.get_header()->Allocator, new_num); + + for (ssize idx = 0; idx < ssize(table.Entries.num()); ++idx) + { + HashTableFindResult find_result; + HashTableEntry& entry = table.Entries[idx]; + + find_result = find(new_ht, entry.Key); + last_added_index = add_entry(new_ht, entry.Key); + + if (find_result.PrevIndex < 0) + new_ht.Hashes[find_result.HashIndex] = last_added_index; + else + new_ht.Entries[find_result.PrevIndex].Next = last_added_index; + + new_ht.Entries[last_added_index].Next = find_result.EntryIndex; + new_ht.Entries[last_added_index].Value = entry.Value; + } + + destroy(table); + table = new_ht; +} + +template inline +void rehash_fast(HashTable& table) +{ + ssize idx; + + for (idx = 0; idx < ssize(table.Entries.num()); idx++) + table.Entries[idx].Next = -1; + + for (idx = 0; idx < ssize(table.Hashes.num()); idx++) + table.Hashes[idx] = -1; + + for (idx = 0; idx < ssize(table.Entries.num()); idx++) + { + HashTableEntry* entry; + HashTableFindResult find_result; + + entry = &table.Entries[idx]; + find_result = find(table, entry->Key); + + if (find_result.PrevIndex < 0) + table.Hashes[find_result.HashIndex] = idx; + else + table.Entries[find_result.PrevIndex].Next = idx; + } +} + +template inline +void remove(HashTable& table, u64 key) { + HashTableFindResult find_result = find(table, key); + + if (find_result.EntryIndex >= 0) { + table.Entries.remove_at(find_result.EntryIndex); + rehash_fast(table); + } +} + +template inline +void remove_entry(HashTable& table, ssize idx) { + table.Entries.remove_at(idx); +} + +template inline +void set(HashTable& table, u64 key, Type value) +{ + ssize idx; + HashTableFindResult find_result; + + if (full(table)) + grow(table); + + find_result = find(table, key); + if (find_result.EntryIndex >= 0) { + idx = find_result.EntryIndex; + } + else + { + idx = add_entry(table, key); + + if (find_result.PrevIndex >= 0) { + table.Entries[find_result.PrevIndex].Next = idx; + } + else { + table.Hashes[find_result.HashIndex] = idx; + } + } + + table.Entries[idx].Value = value; + + if (full(table)) + grow(table); +} + +template inline +ssize slot(HashTable& table, u64 key) { + for (ssize idx = 0; idx < ssize(table.Hashes.num()); ++idx) + if (table.Hashes[idx] == key) + return idx; + + return -1; +} + +template inline +ssize add_entry(HashTable& table, u64 key) { + ssize idx; + HashTableEntry entry = { key, -1 }; + + idx = table.Entries.num(); + table.Entries.append(entry); + return idx; +} + +template inline +HashTableFindResult find(HashTable& table, u64 key) +{ + HashTableFindResult result = { -1, -1, -1 }; + + if (table.Hashes.num() > 0) + { + result.HashIndex = key % table.Hashes.num(); + result.EntryIndex = table.Hashes[result.HashIndex]; + + while (result.EntryIndex >= 0) + { + if (table.Entries[result.EntryIndex].Key == key) + break; + + result.PrevIndex = result.EntryIndex; + result.EntryIndex = table.Entries[result.EntryIndex].Next; + } + } + + return result; +} + +template inline +bool full(HashTable& table) { + usize critical_load = usize(HashTable::CriticalLoadScale * f32(table.Hashes.num())); + b32 result = table.Entries.num() > critical_load; + return result; +} +#pragma endregion HashTable + #pragma endregion Containers From 79eb5f1f76f3eab8781ebc015cffe57bb4adc9a1 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 14:13:30 -0500 Subject: [PATCH 006/112] strings done --- project/components/interface.cpp | 2 +- project/dependencies/containers.hpp | 28 +- project/dependencies/strings.cpp | 92 +-- project/dependencies/strings.hpp | 885 ++++++++++++++++------------ 4 files changed, 518 insertions(+), 489 deletions(-) diff --git a/project/components/interface.cpp b/project/components/interface.cpp index 134af0e..6a13b62 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -372,7 +372,7 @@ AllocatorInfo get_string_allocator( s32 str_length ) { Arena* last = & StringArenas.back(); - usize size_req = str_length + sizeof(String::Header) + sizeof(char*); + usize size_req = str_length + sizeof(StringHeader) + sizeof(char*); if ( last->TotalUsed + ssize(size_req) > last->TotalSize ) { diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index ecea1da..d185b24 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -376,26 +376,26 @@ struct HashTable { static constexpr f32 CriticalLoadScale = 0.7f; - Array Hashes; + Array Hashes; Array> Entries; #if 1 #pragma region Member Mapping - forceinline static HashTable init(AllocatorInfo allocator) { return GEN_NS hashtable_init(allocator); } + forceinline static HashTable init(AllocatorInfo allocator) { return GEN_NS hashtable_init(allocator); } forceinline static HashTable init_reserve(AllocatorInfo allocator, usize num) { return GEN_NS hashtable_init_reserve(allocator, num); } - forceinline void clear() { GEN_NS clear(*this); } - forceinline void destroy() { GEN_NS destroy(*this); } - forceinline Type* get(u64 key) { return GEN_NS get(*this, key); } - forceinline void grow() { GEN_NS grow(*this); } - forceinline void rehash(ssize new_num) { GEN_NS rehash(*this, new_num); } - forceinline void rehash_fast() { GEN_NS rehash_fast(*this); } - forceinline void remove(u64 key) { GEN_NS remove(*this, key); } - forceinline void remove_entry(ssize idx) { GEN_NS remove_entry(*this, idx); } - forceinline void set(u64 key, Type value) { GEN_NS set(*this, key, value); } - forceinline ssize slot(u64 key) { return GEN_NS slot(*this, key); } - forceinline void map(void (*proc)(u64, Type)) { GEN_NS map(*this, proc); } - forceinline void map_mut(void (*proc)(u64, Type*)) { GEN_NS map_mut(*this, proc); } + forceinline void clear() { GEN_NS clear(*this); } + forceinline void destroy() { GEN_NS destroy(*this); } + forceinline Type* get(u64 key) { return GEN_NS get(*this, key); } + forceinline void grow() { GEN_NS grow(*this); } + forceinline void rehash(ssize new_num) { GEN_NS rehash(*this, new_num); } + forceinline void rehash_fast() { GEN_NS rehash_fast(*this); } + forceinline void remove(u64 key) { GEN_NS remove(*this, key); } + forceinline void remove_entry(ssize idx) { GEN_NS remove_entry(*this, idx); } + forceinline void set(u64 key, Type value) { GEN_NS set(*this, key, value); } + forceinline ssize slot(u64 key) { return GEN_NS slot(*this, key); } + forceinline void map(void (*proc)(u64, Type)) { GEN_NS map(*this, proc); } + forceinline void map_mut(void (*proc)(u64, Type*)) { GEN_NS map_mut(*this, proc); } #pragma endregion Member Mapping #endif }; diff --git a/project/dependencies/strings.cpp b/project/dependencies/strings.cpp index a677122..209ee47 100644 --- a/project/dependencies/strings.cpp +++ b/project/dependencies/strings.cpp @@ -4,20 +4,9 @@ #endif #pragma region String - -String String::fmt( AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ... ) +String string_make_length( AllocatorInfo allocator, char const* str, ssize length ) { - va_list va; - va_start( va, fmt ); - str_fmt_va( buf, buf_size, fmt, va ); - va_end( va ); - - return make( allocator, buf ); -} - -String String::make_length( AllocatorInfo allocator, char const* str, ssize length ) -{ - constexpr ssize header_size = sizeof( Header ); + constexpr ssize header_size = sizeof( StringHeader ); s32 alloc_size = header_size + length + 1; void* allocation = alloc( allocator, alloc_size ); @@ -25,8 +14,8 @@ String String::make_length( AllocatorInfo allocator, char const* str, ssize leng if ( allocation == nullptr ) return { nullptr }; - Header& - header = * rcast(Header*, allocation); + StringHeader& + header = * rcast(StringHeader*, allocation); header = { allocator, length, length }; String result = { rcast( char*, allocation) + header_size }; @@ -41,9 +30,9 @@ String String::make_length( AllocatorInfo allocator, char const* str, ssize leng return result; } -String String::make_reserve( AllocatorInfo allocator, ssize capacity ) +String string_make_reserve( AllocatorInfo allocator, ssize capacity ) { - constexpr ssize header_size = sizeof( Header ); + constexpr ssize header_size = sizeof( StringHeader ); s32 alloc_size = header_size + capacity + 1; void* allocation = alloc( allocator, alloc_size ); @@ -53,8 +42,8 @@ String String::make_reserve( AllocatorInfo allocator, ssize capacity ) mem_set( allocation, 0, alloc_size ); - Header* - header = rcast(Header*, allocation); + StringHeader* + header = rcast(StringHeader*, allocation); header->Allocator = allocator; header->Capacity = capacity; header->Length = 0; @@ -62,69 +51,4 @@ String String::make_reserve( AllocatorInfo allocator, ssize capacity ) String result = { rcast(char*, allocation) + header_size }; return result; } - -String String::fmt_buf( AllocatorInfo allocator, char const* fmt, ... ) -{ - local_persist thread_local - char buf[ GEN_PRINTF_MAXLEN ] = { 0 }; - - va_list va; - va_start( va, fmt ); - str_fmt_va( buf, GEN_PRINTF_MAXLEN, fmt, va ); - va_end( va ); - - return make( allocator, buf ); -} - -bool String::append_fmt( char const* fmt, ... ) -{ - ssize res; - char buf[ GEN_PRINTF_MAXLEN ] = { 0 }; - - va_list va; - va_start( va, fmt ); - res = str_fmt_va( buf, count_of( buf ) - 1, fmt, va ) - 1; - va_end( va ); - - return append( buf, res ); -} - -bool String::make_space_for( char const* str, ssize add_len ) -{ - ssize available = avail_space(); - - // NOTE: Return if there is enough space left - if ( available >= add_len ) - { - return true; - } - else - { - ssize new_len, old_size, new_size; - - void* ptr; - void* new_ptr; - - AllocatorInfo allocator = get_header().Allocator; - Header* header = nullptr; - - new_len = grow_formula( length() + add_len ); - ptr = & get_header(); - old_size = size_of( Header ) + length() + 1; - new_size = size_of( Header ) + new_len + 1; - - new_ptr = resize( allocator, ptr, old_size, new_size ); - - if ( new_ptr == nullptr ) - return false; - - header = rcast( Header*, new_ptr); - header->Allocator = allocator; - header->Capacity = new_len; - - Data = rcast( char*, header + 1 ); - - return true; - } -} #pragma endregion String diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index 0db7d54..487ede5 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -19,8 +19,7 @@ struct StrC #define txt( text ) StrC { sizeof( text ) - 1, ( text ) } inline -StrC to_str( char const* str ) -{ +StrC to_str( char const* str ) { return { str_len( str ), str }; } @@ -28,417 +27,523 @@ StrC to_str( char const* str ) // This is directly based off the ZPL string api. // They used a header pattern // I kept it for simplicty of porting but its not necessary to keep it that way. +#pragma region String +struct String; +struct StringHeader; + +// Forward declarations for all file-scope functions +String string_make(AllocatorInfo allocator, char const* str); +String string_make(AllocatorInfo allocator, StrC str); +String string_make_reserve(AllocatorInfo allocator, ssize capacity); +String string_make_length(AllocatorInfo allocator, char const* str, ssize length); +String string_fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...); +String string_fmt_buf(AllocatorInfo allocator, char const* fmt, ...); +String string_join(AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue); +usize string_grow_formula(usize value); +bool are_equal(String lhs, String rhs); +bool are_equal(String lhs, StrC rhs); +bool make_space_for(String& str, char const* to_append, ssize add_len); +bool append(String& str, char c); +bool append(String& str, char const* str_to_append); +bool append(String& str, char const* str_to_append, ssize length); +bool append(String& str, StrC str_to_append); +bool append(String& str, const String other); +bool append_fmt(String& str, char const* fmt, ...); +ssize avail_space(String const& str); +char& back(String& str); +bool contains(String const& str, StrC substring); +bool contains(String const& str, String const& substring); +ssize capacity(String const& str); +void clear(String& str); +String duplicate(String const& str, AllocatorInfo allocator); +void free(String& str); +StringHeader& get_header(String& str); +ssize length(String const& str); +b32 starts_with(String const& str, StrC substring); +b32 starts_with(String const& str, String substring); +void skip_line(String& str); +void strip_space(String& str); +void trim(String& str, char const* cut_set); +void trim_space(String& str); +String visualize_whitespace(String const& str); + +struct StringHeader { + AllocatorInfo Allocator; + ssize Capacity; + ssize Length; +}; + struct String { - struct Header - { - AllocatorInfo Allocator; - ssize Capacity; - ssize Length; - }; + char* Data; + +#if 1 +#pragma region Member Mapping + forceinline static String make(AllocatorInfo allocator, char const* str) { return GEN_NS string_make(allocator, str); } + forceinline static String make(AllocatorInfo allocator, StrC str) { return GEN_NS string_make(allocator, str); } + forceinline static String make_reserve(AllocatorInfo allocator, ssize cap) { return GEN_NS string_make_reserve(allocator, cap); } + forceinline static String make_length(AllocatorInfo a, char const* s, ssize l) { return GEN_NS string_make_length(a, s, l); } + forceinline static String join(AllocatorInfo a, char const** p, ssize n, char const* g) { return GEN_NS string_join(a, p, n, g); } + forceinline static usize grow_formula(usize value) { return GEN_NS string_grow_formula(value); } + forceinline static bool are_equal(String lhs, String rhs) { return GEN_NS are_equal(lhs, rhs); } + forceinline static bool are_equal(String lhs, StrC rhs) { return GEN_NS are_equal(lhs, rhs); } static - usize grow_formula( usize value ) - { - // Using a very aggressive growth formula to reduce time mem_copying with recursive calls to append in this library. - return 4 * value + 8; + String fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) { + va_list va; + va_start(va, fmt); + str_fmt_va(buf, buf_size, fmt, va); + va_end(va); + return GEN_NS string_make(allocator, buf); } static - String make( AllocatorInfo allocator, char const* str ) - { - ssize length = str ? str_len( str ) : 0; - return make_length( allocator, str, length ); + String fmt_buf(AllocatorInfo allocator, char const* fmt, ...) { + local_persist thread_local + char buf[GEN_PRINTF_MAXLEN] = { 0 }; + va_list va; + va_start(va, fmt); + str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va); + va_end(va); + return GEN_NS string_make(allocator, buf); } - static - String make( AllocatorInfo allocator, StrC str ) - { - return make_length( allocator, str.Ptr, str.Len ); + forceinline bool make_space_for(char const* str, ssize add_len) { return GEN_NS make_space_for(*this, str, add_len); } + forceinline bool append(char c) { return GEN_NS append(*this, c); } + forceinline bool append(char const* str) { return GEN_NS append(*this, str); } + forceinline bool append(char const* str, ssize length) { return GEN_NS append(*this, str, length); } + forceinline bool append(StrC str) { return GEN_NS append(*this, str); } + forceinline bool append(const String other) { return GEN_NS append(*this, other); } + forceinline ssize avail_space() const { return GEN_NS avail_space(*this); } + forceinline char& back() { return GEN_NS back(*this); } + forceinline bool contains(StrC substring) const { return GEN_NS contains(*this, substring); } + forceinline bool contains(String const& substring) const { return GEN_NS contains(*this, substring); } + forceinline ssize capacity() const { return GEN_NS capacity(*this); } + forceinline void clear() { GEN_NS clear(*this); } + forceinline String duplicate(AllocatorInfo allocator) const { return GEN_NS duplicate(*this, allocator); } + forceinline void free() { GEN_NS free(*this); } + forceinline ssize length() const { return GEN_NS length(*this); } + forceinline b32 starts_with(StrC substring) const { return GEN_NS starts_with(*this, substring); } + forceinline b32 starts_with(String substring) const { return GEN_NS starts_with(*this, substring); } + forceinline void skip_line() { GEN_NS skip_line(*this); } + forceinline void strip_space() { GEN_NS strip_space(*this); } + forceinline void trim(char const* cut_set) { GEN_NS trim(*this, cut_set); } + forceinline void trim_space() { GEN_NS trim_space(*this); } + forceinline String visualize_whitespace() const { return GEN_NS visualize_whitespace(*this); } + forceinline StringHeader& get_header() { return GEN_NS get_header(*this); } + + bool append_fmt(char const* fmt, ...) { + ssize res; + char buf[GEN_PRINTF_MAXLEN] = { 0 }; + + va_list va; + va_start(va, fmt); + res = str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1; + va_end(va); + + return GEN_NS append(*this, buf, res); } - static - String make_reserve( AllocatorInfo allocator, ssize capacity ); + forceinline operator bool() { return Data != nullptr; } + forceinline operator char*() { return Data; } + forceinline operator char const*() const { return Data; } + forceinline operator StrC() const { return { length(), Data }; } - static - String make_length( AllocatorInfo allocator, char const* str, ssize length ); - - static - String fmt( AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ... ); - - static - String fmt_buf( AllocatorInfo allocator, char const* fmt, ... ); - - static - String join( AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue ) - { - String result = make( allocator, "" ); - - for ( ssize idx = 0; idx < num_parts; ++idx ) - { - result.append( parts[ idx ] ); - - if ( idx < num_parts - 1 ) - result.append( glue ); - } - - return result; - } - - static - bool are_equal( String lhs, String rhs ) - { - if ( lhs.length() != rhs.length() ) - return false; - - for ( ssize idx = 0; idx < lhs.length(); ++idx ) - if ( lhs[ idx ] != rhs[ idx ] ) - return false; - - return true; - } - - static - bool are_equal( String lhs, StrC rhs ) - { - if ( lhs.length() != (rhs.Len) ) - return false; - - for ( ssize idx = 0; idx < lhs.length(); ++idx ) - if ( lhs[idx] != rhs[idx] ) - return false; - - return true; - } - - bool make_space_for( char const* str, ssize add_len ); - - bool append( char c ) - { - return append( & c, 1 ); - } - - bool append( char const* str ) - { - return append( str, str_len( str ) ); - } - - bool append( char const* str, ssize length ) - { - if ( sptr(str) > 0 ) - { - ssize curr_len = this->length(); - - if ( ! make_space_for( str, length ) ) - return false; - - Header& header = get_header(); - - mem_copy( Data + curr_len, str, length ); - - Data[ curr_len + length ] = '\0'; - - header.Length = curr_len + length; - } - return str != nullptr; - } - - bool append( StrC str) - { - return append( str.Ptr, str.Len ); - } - - bool append( const String other ) - { - return append( other.Data, other.length() ); - } - - bool append_fmt( char const* fmt, ... ); - - ssize avail_space() const - { - Header const& - header = * rcast( Header const*, Data - sizeof( Header )); - - return header.Capacity - header.Length; - } - - char& back() - { - return Data[ length() - 1 ]; - } - - bool contains(StrC substring) const - { - Header const& header = * rcast( Header const*, Data - sizeof( Header )); - - if (substring.Len > header.Length) - return false; - - ssize main_len = header.Length; - ssize sub_len = substring.Len; - - for (ssize idx = 0; idx <= main_len - sub_len; ++idx) - { - if (str_compare(Data + idx, substring.Ptr, sub_len) == 0) - return true; - } - - return false; - } - - bool contains(String const& substring) const - { - Header const& header = * rcast( Header const*, Data - sizeof( Header )); - - if (substring.length() > header.Length) - return false; - - ssize main_len = header.Length; - ssize sub_len = substring.length(); - - for (ssize idx = 0; idx <= main_len - sub_len; ++idx) - { - if (str_compare(Data + idx, substring.Data, sub_len) == 0) - return true; - } - - return false; - } - - ssize capacity() const - { - Header const& - header = * rcast( Header const*, Data - sizeof( Header )); - - return header.Capacity; - } - - void clear() - { - get_header().Length = 0; - } - - String duplicate( AllocatorInfo allocator ) const - { - return make_length( allocator, Data, length() ); - } - - void free() - { - if ( ! Data ) - return; - - Header& header = get_header(); - - gen::free( header.Allocator, & header ); - } - - Header& get_header() - { - return *(Header*)(Data - sizeof(Header)); - } - - ssize length() const - { - Header const& - header = * rcast( Header const*, Data - sizeof( Header )); - - return header.Length; - } - - b32 starts_with( StrC substring ) const - { - if (substring.Len > length()) - return false; - - b32 result = str_compare(Data, substring.Ptr, substring.Len ) == 0; - return result; - } - - b32 starts_with( String substring ) const - { - if (substring.length() > length()) - return false; - - b32 result = str_compare(Data, substring, substring.length() - 1 ) == 0; - return result; - } - - void skip_line() - { - #define current (*scanner) - char* scanner = Data; - while ( current != '\r' && current != '\n' ) - { - ++ scanner; - } - - s32 new_length = scanner - Data; - - if ( current == '\r' ) - { - new_length += 1; - } - - mem_move( Data, scanner, new_length ); - - Header* header = & get_header(); - header->Length = new_length; - #undef current - } - - void strip_space() - { - char* write_pos = Data; - char* read_pos = Data; - - while ( * read_pos) - { - if ( ! char_is_space( *read_pos )) - { - *write_pos = *read_pos; - write_pos++; - } - read_pos++; - } - - write_pos[0] = '\0'; // Null-terminate the modified string - - // Update the length if needed - get_header().Length = write_pos - Data; - } - - void trim( char const* cut_set ) - { - ssize len = 0; - - char* start_pos = Data; - char* end_pos = Data + length() - 1; - - while ( start_pos <= end_pos && char_first_occurence( cut_set, *start_pos ) ) - start_pos++; - - while ( end_pos > start_pos && char_first_occurence( cut_set, *end_pos ) ) - end_pos--; - - len = scast( ssize, ( start_pos > end_pos ) ? 0 : ( ( end_pos - start_pos ) + 1 ) ); - - if ( Data != start_pos ) - mem_move( Data, start_pos, len ); - - Data[ len ] = '\0'; - - get_header().Length = len; - } - - void trim_space() - { - return trim( " \t\r\n\v\f" ); - } - - // Debug function that provides a copy of the string with whitespace characters visualized. - String visualize_whitespace() const - { - Header* header = (Header*)(Data - sizeof(Header)); - - String result = make_reserve(header->Allocator, length() * 2); // Assume worst case for space requirements. - - for ( char c : *this ) - { - switch ( c ) - { - case ' ': - result.append( txt("·") ); - break; - case '\t': - result.append( txt("→") ); - break; - case '\n': - result.append( txt("↵") ); - break; - case '\r': - result.append( txt("⏎") ); - break; - case '\v': - result.append( txt("⇕") ); - break; - case '\f': - result.append( txt("⌂") ); - break; - default: - result.append(c); - break; - } - } - - return result; - } - - // For-range support - - char* begin() const - { - return Data; - } - - char* end() const - { - Header const& - header = * rcast( Header const*, Data - sizeof( Header )); - - return Data + header.Length; - } - - operator bool() - { - return Data != nullptr; - } - - operator char* () - { - return Data; - } - - operator char const* () const - { - return Data; - } - - operator StrC() const - { - return { length(), Data }; - } - - // Used with cached strings - // Essentially makes the string a string view. - String const& operator = ( String const& other ) const - { - if ( this == & other ) + String const& operator=(String const& other) const { + if (this == &other) return *this; - String* - this_ = ccast(String*, this); + String* this_ = ccast(String*, this); this_->Data = other.Data; return *this; } - char& operator [] ( ssize index ) - { - return Data[ index ]; - } + forceinline char& operator[](ssize index) { return Data[index]; } + forceinline char const& operator[](ssize index) const { return Data[index]; } - char const& operator [] ( ssize index ) const - { - return Data[ index ]; - } - - char* Data; + forceinline char* begin() const { return Data; } + forceinline char* end() const { return Data + length(); } +#pragma endregion Member Mapping +#endif }; -struct String_POD +inline +usize string_grow_formula(usize value) { + // Using a very aggressive growth formula to reduce time mem_copying with recursive calls to append in this library. + return 4 * value + 8; +} + +inline +String string_make(AllocatorInfo allocator, char const* str) { + ssize length = str ? str_len(str) : 0; + return string_make_length(allocator, str, length); +} + +inline +String string_make(AllocatorInfo allocator, StrC str) { + return string_make_length(allocator, str.Ptr, str.Len); +} + +inline +String string_fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) { + va_list va; + va_start(va, fmt); + str_fmt_va(buf, buf_size, fmt, va); + va_end(va); + + return string_make(allocator, buf); +} + +inline +String string_fmt_buf(AllocatorInfo allocator, char const* fmt, ...) { + local_persist thread_local + char buf[GEN_PRINTF_MAXLEN] = { 0 }; + + va_list va; + va_start(va, fmt); + str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va); + va_end(va); + + return string_make(allocator, buf); +} + +inline +String string_join(AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue) +{ + String result = string_make(allocator, ""); + + for (ssize idx = 0; idx < num_parts; ++idx) + { + append(result, parts[idx]); + + if (idx < num_parts - 1) + append(result, glue); + } + + return result; +} + +inline +bool append(String& str, char c) { + return append(str, &c, 1); +} + +inline +bool append(String& str, char const* str_to_append) { + return append(str, str_to_append, str_len(str_to_append)); +} + +inline +bool append(String& str, char const* str_to_append, ssize append_length) +{ + if (sptr(str_to_append) > 0) + { + ssize curr_len = length(str); + + if (!make_space_for(str, str_to_append, append_length)) + return false; + + StringHeader& header = get_header(str); + + mem_copy(str.Data + curr_len, str_to_append, append_length); + + str.Data[curr_len + append_length] = '\0'; + + header.Length = curr_len + append_length; + } + return str_to_append != nullptr; +} + +inline +bool append(String& str, StrC str_to_append) { + return append(str, str_to_append.Ptr, str_to_append.Len); +} + +inline +bool append(String& str, const String other) { + return append(str, other.Data, length(other)); +} + +inline +bool are_equal(String lhs, String rhs) +{ + if (length(lhs) != length(rhs)) + return false; + + for (ssize idx = 0; idx < length(lhs); ++idx) + if (lhs[idx] != rhs[idx]) + return false; + + return true; +} + +inline +bool are_equal(String lhs, StrC rhs) +{ + if (length(lhs) != (rhs.Len)) + return false; + + for (ssize idx = 0; idx < length(lhs); ++idx) + if (lhs[idx] != rhs[idx]) + return false; + + return true; +} + +inline +ssize avail_space(String const& str) { + StringHeader const& header = *rcast(StringHeader const*, str.Data - sizeof(StringHeader)); + return header.Capacity - header.Length; +} + +inline +char& back(String& str) { + return str.Data[length(str) - 1]; +} + +inline +bool contains(String const& str, StrC substring) +{ + StringHeader const& header = *rcast(StringHeader const*, str.Data - sizeof(StringHeader)); + + if (substring.Len > header.Length) + return false; + + ssize main_len = header.Length; + ssize sub_len = substring.Len; + + for (ssize idx = 0; idx <= main_len - sub_len; ++idx) + { + if (str_compare(str.Data + idx, substring.Ptr, sub_len) == 0) + return true; + } + + return false; +} + +inline +bool contains(String const& str, String const& substring) +{ + StringHeader const& header = *rcast(StringHeader const*, str.Data - sizeof(StringHeader)); + + if (length(substring) > header.Length) + return false; + + ssize main_len = header.Length; + ssize sub_len = length(substring); + + for (ssize idx = 0; idx <= main_len - sub_len; ++idx) + { + if (str_compare(str.Data + idx, substring.Data, sub_len) == 0) + return true; + } + + return false; +} + +inline +ssize capacity(String const& str) { + StringHeader const& header = *rcast(StringHeader const*, str.Data - sizeof(StringHeader)); + return header.Capacity; +} + +inline +void clear(String& str) { + get_header(str).Length = 0; +} + +inline +String duplicate(String const& str, AllocatorInfo allocator) { + return string_make_length(allocator, str.Data, length(str)); +} + +inline +void free(String& str) { + if (!str.Data) + return; + + StringHeader& header = get_header(str); + GEN_NS free(header.Allocator, &header); +} + +inline +StringHeader& get_header(String& str) { + return *(StringHeader*)(str.Data - sizeof(StringHeader)); +} + +inline +ssize length(String const& str) +{ + StringHeader const& header = *rcast(StringHeader const*, str.Data - sizeof(StringHeader)); + return header.Length; +} + +inline +bool make_space_for(String& str, char const* to_append, ssize add_len) +{ + ssize available = avail_space(str); + + if (available >= add_len) { + return true; + } + else + { + ssize new_len, old_size, new_size; + void* ptr; + void* new_ptr; + + AllocatorInfo allocator = get_header(str).Allocator; + StringHeader* header = nullptr; + + new_len = string_grow_formula(length(str) + add_len); + ptr = &get_header(str); + old_size = size_of(StringHeader) + length(str) + 1; + new_size = size_of(StringHeader) + new_len + 1; + + new_ptr = resize(allocator, ptr, old_size, new_size); + + if (new_ptr == nullptr) + return false; + + header = rcast(StringHeader*, new_ptr); + header->Allocator = allocator; + header->Capacity = new_len; + + str.Data = rcast(char*, header + 1); + + return true; + } +} + +inline +b32 starts_with(String const& str, StrC substring) { + if (substring.Len > length(str)) + return false; + + b32 result = str_compare(str.Data, substring.Ptr, substring.Len) == 0; + return result; +} + +inline +b32 starts_with(String const& str, String substring) { + if (length(substring) > length(str)) + return false; + + b32 result = str_compare(str.Data, substring.Data, length(substring) - 1) == 0; + return result; +} + +inline +void skip_line(String& str) +{ +#define current (*scanner) + char* scanner = str.Data; + while (current != '\r' && current != '\n') { + ++scanner; + } + + s32 new_length = scanner - str.Data; + + if (current == '\r') { + new_length += 1; + } + + mem_move(str.Data, scanner, new_length); + + StringHeader* header = &get_header(str); + header->Length = new_length; +#undef current +} + +inline +void strip_space(String& str) +{ + char* write_pos = str.Data; + char* read_pos = str.Data; + + while (*read_pos) + { + if (!char_is_space(*read_pos)) + { + *write_pos = *read_pos; + write_pos++; + } + read_pos++; + } + + write_pos[0] = '\0'; // Null-terminate the modified string + + // Update the length if needed + get_header(str).Length = write_pos - str.Data; +} + +inline +void trim(String& str, char const* cut_set) +{ + ssize len = 0; + + char* start_pos = str.Data; + char* end_pos = str.Data + length(str) - 1; + + while (start_pos <= end_pos && char_first_occurence(cut_set, *start_pos)) + start_pos++; + + while (end_pos > start_pos && char_first_occurence(cut_set, *end_pos)) + end_pos--; + + len = scast(ssize, (start_pos > end_pos) ? 0 : ((end_pos - start_pos) + 1)); + + if (str.Data != start_pos) + mem_move(str.Data, start_pos, len); + + str.Data[len] = '\0'; + + get_header(str).Length = len; +} + +inline +void trim_space(String& str) { + trim(str, " \t\r\n\v\f"); +} + +inline +String visualize_whitespace(String const& str) +{ + StringHeader* header = (StringHeader*)(str.Data - sizeof(StringHeader)); + String result = string_make_reserve(header->Allocator, length(str) * 2); // Assume worst case for space requirements. + + for (char c : str) switch (c) + { + case ' ': + append(result, txt("·")); + break; + case '\t': + append(result, txt("→")); + break; + case '\n': + append(result, txt("↵")); + break; + case '\r': + append(result, txt("⏎")); + break; + case '\v': + append(result, txt("⇕")); + break; + case '\f': + append(result, txt("⌂")); + break; + default: + append(result, c); + break; + } + + return result; +} +#pragma endregion String + +struct String_POD { char* Data; }; static_assert( sizeof( String_POD ) == sizeof( String ), "String is not a POD" ); From 056a5863b81e117994b791731f8ac9c42d1eed37 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 14:34:28 -0500 Subject: [PATCH 007/112] for the future... --- project/auxillary/gen_template.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 project/auxillary/gen_template.hpp diff --git a/project/auxillary/gen_template.hpp b/project/auxillary/gen_template.hpp new file mode 100644 index 0000000..0f1d7b0 --- /dev/null +++ b/project/auxillary/gen_template.hpp @@ -0,0 +1,23 @@ +#ifdef GEN_INTELLISENSE_DIRECTIVES +# pragma once +# include "../gen.hpp" +#endif + +/* + Explicitly generates a resolved definition of a cpp template definition. + + TODO(Ed): Needs implementing for the C-library variant. + TODO(Ed): We need a non syntax subst implemtnation for Strings for this to work. It must subst keywords directly based on template parameter names. + + This is only meant to be used on relatively trivial templates, where the type or numeric is mostly a 'duck' type. + It cannot parse complex template parameters. + + The varadic args should correspond 1:1 with the type of objects the generator expects from the template's parameters.alignas. +*/ + +CodeOperator gen_operator_template( CodeTemplate template, ... ); +CodeFn gen_func_template( CodeTemplate template, ... ); +Code gen_class_struct_template( CodeTemplate template, ... ); + +Code gen_template( CodeTemplate template, ... ); +Code gen_template( StrC template, StrC instantiation ); From a67fdef20adb5c5846de1b2eb56a53003bb14579 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 16:50:53 -0500 Subject: [PATCH 008/112] dir restructuring just making it more organized (gen_ prefix for library generation meta-programs) --- gen_c_library/c_library.cpp | 21 +++++++++++++++++++ {singleheader => gen_singleheader}/Readme.md | 2 +- .../components/header_start.hpp | 0 .../singleheader.cpp | 0 .../Readme.md | 0 .../components/header_start.hpp | 0 .../components/src_start.cpp | 0 .../enums/AttributeTokens.csv | 0 .../unreal.cpp | 0 9 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 gen_c_library/c_library.cpp rename {singleheader => gen_singleheader}/Readme.md (50%) rename {singleheader => gen_singleheader}/components/header_start.hpp (100%) rename {singleheader => gen_singleheader}/singleheader.cpp (100%) rename {unreal_engine => gen_unreal_engine}/Readme.md (100%) rename {unreal_engine => gen_unreal_engine}/components/header_start.hpp (100%) rename {unreal_engine => gen_unreal_engine}/components/src_start.cpp (100%) rename {unreal_engine => gen_unreal_engine}/enums/AttributeTokens.csv (100%) rename {unreal_engine => gen_unreal_engine}/unreal.cpp (100%) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp new file mode 100644 index 0000000..5ced522 --- /dev/null +++ b/gen_c_library/c_library.cpp @@ -0,0 +1,21 @@ +#define GEN_DEFINE_LIBRARY_CODE_CONSTANTS +#define GEN_ENFORCE_STRONG_CODE_TYPES +#define GEN_EXPOSE_BACKEND +#include "gen.cpp" + +#include "helpers/push_ignores.inline.hpp" +#include "helpers/helper.hpp" + +GEN_NS_BEGIN +#include "dependencies/parsing.cpp" +GEN_NS_END + +#include "auxillary/builder.hpp" +#include "auxillary/builder.cpp" +#include "auxillary/scanner.hpp" + +#include // for system() + +using namespace gen; + + diff --git a/singleheader/Readme.md b/gen_singleheader/Readme.md similarity index 50% rename from singleheader/Readme.md rename to gen_singleheader/Readme.md index aa7f724..50eb682 100644 --- a/singleheader/Readme.md +++ b/gen_singleheader/Readme.md @@ -1,4 +1,4 @@ # Singleheader -Creates a single header file version of the library using `gen.singleheader.cpp`. +Creates a single header file version of the library using `singleheader.cpp`. Follows the same convention seen in the gb, stb, and zpl libraries. diff --git a/singleheader/components/header_start.hpp b/gen_singleheader/components/header_start.hpp similarity index 100% rename from singleheader/components/header_start.hpp rename to gen_singleheader/components/header_start.hpp diff --git a/singleheader/singleheader.cpp b/gen_singleheader/singleheader.cpp similarity index 100% rename from singleheader/singleheader.cpp rename to gen_singleheader/singleheader.cpp diff --git a/unreal_engine/Readme.md b/gen_unreal_engine/Readme.md similarity index 100% rename from unreal_engine/Readme.md rename to gen_unreal_engine/Readme.md diff --git a/unreal_engine/components/header_start.hpp b/gen_unreal_engine/components/header_start.hpp similarity index 100% rename from unreal_engine/components/header_start.hpp rename to gen_unreal_engine/components/header_start.hpp diff --git a/unreal_engine/components/src_start.cpp b/gen_unreal_engine/components/src_start.cpp similarity index 100% rename from unreal_engine/components/src_start.cpp rename to gen_unreal_engine/components/src_start.cpp diff --git a/unreal_engine/enums/AttributeTokens.csv b/gen_unreal_engine/enums/AttributeTokens.csv similarity index 100% rename from unreal_engine/enums/AttributeTokens.csv rename to gen_unreal_engine/enums/AttributeTokens.csv diff --git a/unreal_engine/unreal.cpp b/gen_unreal_engine/unreal.cpp similarity index 100% rename from unreal_engine/unreal.cpp rename to gen_unreal_engine/unreal.cpp From 5527a27f7bafde3e2e425a9e53b0882a55b000d4 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 16:54:03 -0500 Subject: [PATCH 009/112] prepare c_library meta-program a bit --- gen_c_library/c_library.cpp | 45 +++++++++++++++++++++++++++++++++++++ scripts/build.ci.ps1 | 12 ++++++++-- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 5ced522..adbe936 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -18,4 +18,49 @@ GEN_NS_END using namespace gen; +void format_file( char const* path ) +{ + String resolved_path = String::make(GlobalAllocator, to_str(path)); + String style_arg = String::make(GlobalAllocator, txt("-style=file:")); + style_arg.append("../scripts/.clang-format "); + + // Need to execute clang format on the generated file to get it to match the original. + #define clang_format "clang-format " + #define cf_format_inplace "-i " + #define cf_verbose "-verbose " + String command = String::make( GlobalAllocator, clang_format ); + command.append( cf_format_inplace ); + command.append( cf_verbose ); + command.append( style_arg ); + command.append( resolved_path ); + log_fmt("\tRunning clang-format on file:\n"); + system( command ); + log_fmt("\tclang-format finished reformatting.\n"); + #undef cf_cmd + #undef cf_format_inplace + #undef cf_style + #undef cf_verbse +} + +Code dump_to_scratch_and_retireve( Code code ) +{ + Builder ecode_file_temp = Builder::open("gen/scratch.hpp"); + ecode_file_temp.print(code); + ecode_file_temp.write(); + format_file("gen/scratch.hpp"); + Code result = scan_file( "gen/scratch.hpp" ); + remove("gen/scratch.hpp"); + return result; +} + +int gen_main() +{ +#define project_dir "../project/" + gen::init(); + + + gen::deinit(); + return 0; +#undef project_dir +} diff --git a/scripts/build.ci.ps1 b/scripts/build.ci.ps1 index 4f0f970..877d522 100644 --- a/scripts/build.ci.ps1 +++ b/scripts/build.ci.ps1 @@ -44,6 +44,7 @@ Push-Location $path_root $verbose = $false [bool] $bootstrap = $false [bool] $singleheader = $false +[bool] $c_library = $false [bool] $unreal = $false [bool] $test = $false @@ -59,6 +60,7 @@ if ( $args ) { $args | ForEach-Object { "debug" { $release = $false } "bootstrap" { $bootstrap = $true } "singleheader" { $singleheader = $true } + "c_library" { $c_library = $true } "unreal" { $unreal = $true } "test" { $test = $true } } @@ -103,8 +105,9 @@ write-host "Build Type: $(if ($release) {"Release"} else {"Debug"} )" $path_build = Join-Path $path_root build $path_project = Join-Path $path_root project $path_scripts = Join-Path $path_root scripts -$path_singleheader = Join-Path $path_root singleheader -$path_unreal = Join-Path $path_root unreal_engine +$path_c_library = join-Path $path_root gen_c_library +$path_singleheader = Join-Path $path_root gen_singleheader +$path_unreal = Join-Path $path_root gen_unreal_engine $path_test = Join-Path $path_root test if ( $bootstrap ) @@ -187,6 +190,11 @@ if ( $singleheader ) Pop-Location } +if ( $c_library ) +{ + +} + if ( $unreal ) { $path_build = join-path $path_unreal build From 06deb1e836a956f9457b61253b643e08c4c2d288 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 17:18:49 -0500 Subject: [PATCH 010/112] memory.hpp no longer uses memory mappings by default --- project/components/interface.cpp | 32 +++---- project/components/interface.untyped.cpp | 6 +- project/components/parser.cpp | 10 +-- project/dependencies/basic_types.hpp | 8 ++ project/dependencies/macros.hpp | 67 ++++++++++---- project/dependencies/memory.hpp | 109 ++++++++++++----------- project/dependencies/platform.hpp | 10 ++- project/helpers/helper.hpp | 20 +++-- project/helpers/member_proc_support.hpp | 10 +++ 9 files changed, 170 insertions(+), 102 deletions(-) create mode 100644 project/helpers/member_proc_support.hpp diff --git a/project/components/interface.cpp b/project/components/interface.cpp index 6a13b62..f4fe0ee 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -19,7 +19,7 @@ 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(), Global_BucketSize ); if ( bucket.PhysicalStart == nullptr ) GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets"); @@ -30,7 +30,7 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s last = & Global_AllocatorBuckets.back(); } - return alloc_align( * last, size, alignment ); + return alloc_align( allocator_info(* last), size, alignment ); } case EAllocation_FREE: { @@ -46,7 +46,7 @@ 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(), Global_BucketSize ); if ( bucket.PhysicalStart == nullptr ) GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets"); @@ -240,7 +240,7 @@ void init() if ( Global_AllocatorBuckets == nullptr ) GEN_FATAL( "Failed to reserve memory for Global_AllocatorBuckets"); - Arena bucket = Arena::init_from_allocator( heap(), Global_BucketSize ); + Arena bucket = arena_init_from_allocator( heap(), Global_BucketSize ); if ( bucket.PhysicalStart == nullptr ) GEN_FATAL( "Failed to create first bucket for Global_AllocatorBuckets"); @@ -264,16 +264,16 @@ void init() // Setup the code pool and code entries arena. { - Pool code_pool = Pool::init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) ); + Pool code_pool = pool_init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) ); if ( code_pool.PhysicalStart == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the code pool" ); CodePools.append( code_pool ); - LexArena = Arena::init_from_allocator( Allocator_Lexer, LexAllocator_Size ); + LexArena = arena_init_from_allocator( Allocator_Lexer, LexAllocator_Size ); - Arena string_arena = Arena::init_from_allocator( Allocator_StringArena, SizePer_StringArena ); + Arena string_arena = arena_init_from_allocator( Allocator_StringArena, SizePer_StringArena ); if ( string_arena.PhysicalStart == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the string arena" ); @@ -303,7 +303,7 @@ void deinit() do { Pool* code_pool = & CodePools[index]; - code_pool->free(); + free(* code_pool); index++; } while ( left--, left ); @@ -313,7 +313,7 @@ void deinit() do { Arena* string_arena = & StringArenas[index]; - string_arena->free(); + free(* string_arena); index++; } while ( left--, left ); @@ -323,7 +323,7 @@ void deinit() CodePools.free(); StringArenas.free(); - LexArena.free(); + free(LexArena); PreprocessorDefines.free(); @@ -332,7 +332,7 @@ void deinit() do { Arena* bucket = & Global_AllocatorBuckets[ index ]; - bucket->free(); + free(* bucket); index++; } while ( left--, left ); @@ -348,7 +348,7 @@ void reset() do { Pool* code_pool = & CodePools[index]; - code_pool->clear(); + clear(* code_pool); index++; } while ( left--, left ); @@ -376,7 +376,7 @@ AllocatorInfo get_string_allocator( s32 str_length ) if ( last->TotalUsed + ssize(size_req) > last->TotalSize ) { - Arena new_arena = Arena::init_from_allocator( Allocator_StringArena, SizePer_StringArena ); + Arena new_arena = arena_init_from_allocator( Allocator_StringArena, SizePer_StringArena ); if ( ! StringArenas.append( new_arena ) ) GEN_FATAL( "gen::get_string_allocator: Failed to allocate a new string arena" ); @@ -384,7 +384,7 @@ AllocatorInfo get_string_allocator( s32 str_length ) last = & StringArenas.back(); } - return * last; + return allocator_info(* last); } // Will either make or retrive a code string. @@ -411,7 +411,7 @@ Code make_code() Pool* allocator = & CodePools.back(); if ( allocator->FreeList == nullptr ) { - Pool code_pool = Pool::init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) ); + Pool code_pool = pool_init( Allocator_CodePool, 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." ); @@ -422,7 +422,7 @@ Code make_code() allocator = & CodePools.back(); } - Code result { rcast( AST*, alloc( * allocator, sizeof(AST) )) }; + Code result { rcast( AST*, alloc( allocator_info(* allocator), sizeof(AST) )) }; mem_set( result.ast, 0, sizeof(AST) ); // result->Type = ECode::Invalid; diff --git a/project/components/interface.untyped.cpp b/project/components/interface.untyped.cpp index bd73306..bdb0742 100644 --- a/project/components/interface.untyped.cpp +++ b/project/components/interface.untyped.cpp @@ -16,8 +16,8 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) local_persist char tok_map_mem[ TokenFmt_TokenMap_MemSize ]; - tok_map_arena = init_from_memory( tok_map_mem, sizeof(tok_map_mem) ); - tok_map = HashTable::init( tok_map_arena ); + tok_map_arena = arena_init_from_memory( tok_map_mem, sizeof(tok_map_mem) ); + tok_map = HashTable::init( allocator_info(tok_map_arena) ); s32 left = num_tokens - 1; @@ -95,7 +95,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) } tok_map.clear(); - tok_map_arena.free(); + free(tok_map_arena); ssize result = buf_size - remaining; diff --git a/project/components/parser.cpp b/project/components/parser.cpp index d44d748..94e5315 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -132,12 +132,12 @@ bool TokArray::__eat( TokType type ) internal void init() { - Tokens = Array::init_reserve( LexArena + Tokens = array_init_reserve( allocator_info(LexArena) , ( LexAllocator_Size - sizeof( ArrayHeader ) ) / sizeof(Token) ); - defines_map_arena = Arena_256KB::init(); - defines = HashTable::init_reserve( defines_map_arena, 256 ); + fixed_arena_init(defines_map_arena); + defines = HashTable::init_reserve( allocator_info(defines_map_arena), 256 ); } internal @@ -713,8 +713,8 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) local_persist char interface_arr_mem[ kilobytes(4) ] {0}; Array interfaces; { - Arena arena = init_from_memory( interface_arr_mem, kilobytes(4) ); - Array::init_reserve( arena, 4 ); + Arena arena = arena_init_from_memory( interface_arr_mem, kilobytes(4) ); + array_init_reserve( allocator_info(arena), 4 ); } // TODO(Ed) : Make an AST_DerivedType, we'll store any arbitary derived type into there as a linear linked list of them. diff --git a/project/dependencies/basic_types.hpp b/project/dependencies/basic_types.hpp index 9533827..5b4b0e3 100644 --- a/project/dependencies/basic_types.hpp +++ b/project/dependencies/basic_types.hpp @@ -126,10 +126,18 @@ typedef s32 b32; using mem_ptr = void*; using mem_ptr_const = void const*; +#if ! GEN_COMPILER_C template uptr to_uptr( Type* ptr ) { return (uptr)ptr; } template sptr to_sptr( Type* ptr ) { return (sptr)ptr; } template mem_ptr to_mem_ptr ( Type ptr ) { return (mem_ptr) ptr; } template mem_ptr_const to_mem_ptr_const( Type ptr ) { return (mem_ptr_const)ptr; } +#else +#define to_utpr( ptr ) ((uptr)(ptr)) +#define to_stpr( ptr ) ((sptr)(ptr)) + +#define to_mem_ptr( ptr) ((mem_ptr)ptr) +#define to_mem_ptr_const( ptr) ((mem_ptr)ptr) +#endif #pragma endregion Basic Types diff --git a/project/dependencies/macros.hpp b/project/dependencies/macros.hpp index 2896e7b..41ee2d6 100644 --- a/project/dependencies/macros.hpp +++ b/project/dependencies/macros.hpp @@ -23,17 +23,32 @@ #define bitfield_is_equal( Type, Field, Mask ) ( (Type(Mask) & Type(Field)) == Type(Mask) ) #endif -#ifndef ccast -#define ccast( type, value ) ( const_cast< type >( (value) ) ) -#endif -#ifndef pcast -#define pcast( type, value ) ( * reinterpret_cast< type* >( & ( value ) ) ) -#endif -#ifndef rcast -#define rcast( type, value ) reinterpret_cast< type >( value ) -#endif -#ifndef scast -#define scast( type, value ) static_cast< type >( value ) +#if ! GEN_C_COMPILER +# ifndef ccast +# define ccast( type, value ) ( const_cast< type >( (value) ) ) +# endif +# ifndef pcast +# define pcast( type, value ) ( * reinterpret_cast< type* >( & ( value ) ) ) +# endif +# ifndef rcast +# define rcast( type, value ) reinterpret_cast< type >( value ) +# endif +# ifndef scast +# define scast( type, value ) static_cast< type >( value ) +# endif +#else +# ifndef ccast +# define ccast( type, value ) ( (type)(value) ) +# endif +# ifndef pcast +# define pcast( type, value ) ( (type)(value) ) +# endif +# ifndef rcast +# define rcast( type, value ) ( (type)(value) ) +# endif +# ifndef scast +# define scast( type, value ) ( (type)(value) ) +# endif #endif #ifndef stringize @@ -123,20 +138,20 @@ #define min( a, b ) ( (a < b) ? (a) : (b) ) #endif -#if defined( _MSC_VER ) || defined( GEN_COMPILER_TINYC ) +#if GEN_COMPILER_MSVC || GEN_COMPILER_TINYC # define offset_of( Type, element ) ( ( GEN_NS( ssize ) ) & ( ( ( Type* )0 )->element ) ) #else # define offset_of( Type, element ) __builtin_offsetof( Type, element ) #endif #ifndef forceinline -# ifdef GEN_COMPILER_MSVC +# if GEN_COMPILER_MSVC # define forceinline __forceinline # define neverinline __declspec( noinline ) -# elif defined(GEN_COMPILER_GCC) +# elif GEN_COMPILER_GCC # define forceinline inline __attribute__((__always_inline__)) # define neverinline __attribute__( ( __noinline__ ) ) -# elif defined(GEN_COMPILER_CLANG) +# elif GEN_COMPILER_CLANG # if __has_attribute(__always_inline__) # define forceinline inline __attribute__((__always_inline__)) # define neverinline __attribute__( ( __noinline__ ) ) @@ -151,11 +166,11 @@ #endif #ifndef neverinline -# ifdef GEN_COMPILER_MSVC +# if GEN_COMPILER_MSVC # define neverinline __declspec( noinline ) -# elif defined(GEN_COMPILER_GCC) +# elif GEN_COMPILER_GCC # define neverinline __attribute__( ( __noinline__ ) ) -# elif defined(GEN_COMPILER_CLANG) +# elif GEN_COMPILER_CLANG # if __has_attribute(__always_inline__) # define neverinline __attribute__( ( __noinline__ ) ) # else @@ -166,4 +181,20 @@ # endif #endif +#if !defined(GEN_SUPPORT_CPP_MEMBER_FEATURES) && (!GEN_COMPILER_C || __STDC_VERSION__ < 202311L) +# define GEN_SUPPORT_CPP_MEMBER_FEATURES 0 +#endif + +#if !defined(typeof) && (!GEN_COMPILER_C || __STDC_VERSION__ < 202311L) +# if ! GEN_COMPILER_C +# define typeof +# elif defined(_MSC_VER) +# define typeof(x) __typeof(x) +# elif defined(__GNUC__) || defined(__clang__) +# define typeof(x) __typeof__(x) +# else +# error "Compiler not supported" +# endif +#endif + #pragma endregion Macros diff --git a/project/dependencies/memory.hpp b/project/dependencies/memory.hpp index bee56f3..d8c4253 100644 --- a/project/dependencies/memory.hpp +++ b/project/dependencies/memory.hpp @@ -14,13 +14,23 @@ #define GEN__HIGHS ( GEN__ONES * ( GEN_U8_MAX / 2 + 1 ) ) #define GEN__HAS_ZERO( x ) ( ( ( x ) - GEN__ONES ) & ~( x ) & GEN__HIGHS ) -template< class Type > -void swap( Type& a, Type& b ) -{ - Type tmp = a; - a = b; - b = tmp; -} +#if ! GEN_COMPILER_C + template< class Type > + void swap( Type& a, Type& b ) + { + Type tmp = a; + a = b; + b = tmp; + } +#else + #define swap( a, b ) \ + do { \ + typeof(a) \ + temp = (a); \ + (a) = (b); \ + (b) = temp; \ + } while(0) +#endif //! Checks if value is power of 2. b32 is_power_of_two( ssize x ); @@ -179,8 +189,8 @@ AllocatorInfo allocator_info( Arena& arena ); void* arena_allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags); // Add these declarations after the Arena struct -Arena init_from_allocator(AllocatorInfo backing, ssize size); -Arena init_from_memory( void* start, ssize size ); +Arena arena_init_from_allocator(AllocatorInfo backing, ssize size); +Arena arena_init_from_memory( void* start, ssize size ); Arena init_sub(Arena& parent, ssize size); ssize alignment_of(Arena& arena, ssize alignment); @@ -201,14 +211,14 @@ struct Arena ssize TotalUsed; ssize TempCount; -#if 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES #pragma region Member Mapping forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); } forceinline static void* allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) { return GEN_NS arena_allocator_proc( allocator_data, type, size, alignment, old_memory, old_size, flags ); } - forceinline static Arena init_from_memory( void* start, ssize size ) { return GEN_NS init_from_memory( start, size ); } - forceinline static Arena init_from_allocator( AllocatorInfo backing, ssize size ) { return GEN_NS init_from_allocator( backing, size ); } - forceinline static Arena init_sub( Arena& parent, ssize size ) { return GEN_NS init_from_allocator( parent.Backing, size ); } + forceinline static Arena init_from_memory( void* start, ssize size ) { return GEN_NS arena_init_from_memory( start, size ); } + forceinline static Arena init_from_allocator( AllocatorInfo backing, ssize size ) { return GEN_NS arena_init_from_allocator( backing, size ); } + forceinline static Arena init_sub( Arena& parent, ssize size ) { return GEN_NS arena_init_from_allocator( parent.Backing, size ); } forceinline ssize alignment_of( ssize alignment ) { return GEN_NS alignment_of(* this, alignment); } forceinline void free() { return GEN_NS free(* this); } forceinline ssize size_remaining( ssize alignment ) { return GEN_NS size_remaining(* this, alignment); } @@ -229,7 +239,7 @@ AllocatorInfo allocator_info( Arena& arena ) { } inline -Arena init_from_memory( void* start, ssize size ) +Arena arena_init_from_memory( void* start, ssize size ) { Arena arena = { { nullptr, nullptr }, @@ -242,39 +252,36 @@ Arena init_from_memory( void* start, ssize size ) } inline -Arena init_from_allocator(AllocatorInfo backing, ssize size) -{ - Arena result = - { - backing, - alloc(backing, size), - size, - 0, - 0 - }; - return result; +Arena arena_init_from_allocator(AllocatorInfo backing, ssize size) { + Arena result = { + backing, + alloc(backing, size), + size, + 0, + 0 + }; + return result; } inline -Arena init_sub(Arena& parent, ssize size) -{ - return init_from_allocator(parent.Backing, size); +Arena init_sub(Arena& parent, ssize size) { + return arena_init_from_allocator(parent.Backing, size); } inline ssize alignment_of(Arena& arena, ssize alignment) { - ssize alignment_offset, result_pointer, mask; - GEN_ASSERT(is_power_of_two(alignment)); + ssize alignment_offset, result_pointer, mask; + GEN_ASSERT(is_power_of_two(alignment)); - alignment_offset = 0; - result_pointer = (ssize)arena.PhysicalStart + arena.TotalUsed; - mask = alignment - 1; + alignment_offset = 0; + result_pointer = (ssize)arena.PhysicalStart + arena.TotalUsed; + mask = alignment - 1; - if (result_pointer & mask) - alignment_offset = alignment - (result_pointer & mask); + if (result_pointer & mask) + alignment_offset = alignment - (result_pointer & mask); - return alignment_offset; + return alignment_offset; } #pragma push_macro("check") @@ -289,18 +296,18 @@ void check(Arena& arena) inline void free(Arena& arena) { - if (arena.Backing.Proc) - { - gen::free(arena.Backing, arena.PhysicalStart); - arena.PhysicalStart = nullptr; - } + if (arena.Backing.Proc) + { + GEN_NS free(arena.Backing, arena.PhysicalStart); + arena.PhysicalStart = nullptr; + } } inline ssize size_remaining(Arena& arena, ssize alignment) { - ssize result = arena.TotalSize - (arena.TotalUsed + alignment_of(arena, alignment)); - return result; + ssize result = arena.TotalSize - (arena.TotalUsed + alignment_of(arena, alignment)); + return result; } #pragma endregion Arena @@ -317,15 +324,15 @@ template ssize size_remaining(FixedArena& fixed_arena template< s32 Size > struct FixedArena { - char memory[Size]; - Arena arena; + char memory[Size]; + Arena arena; -#if 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES #pragma region Member Mapping - forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); } + forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); } - forceinline static FixedArena init() { FixedArena result; GEN_NS fixed_arena_init(result); return result; } - forceinline ssize size_remaining(ssize alignment) { GEN_NS size_remaining(*this, alignment); } + forceinline static FixedArena init() { FixedArena result; GEN_NS fixed_arena_init(result); return result; } + forceinline ssize size_remaining(ssize alignment) { GEN_NS size_remaining(*this, alignment); } #pragma endregion Member Mapping #endif }; @@ -336,7 +343,7 @@ AllocatorInfo allocator_info( FixedArena& fixed_arena ) { return { arena_a template inline void fixed_arena_init(FixedArena& result) { zero_size(& result.memory[0], Size); - result.arena = init_from_memory(& result.memory[0], Size); + result.arena = arena_init_from_memory(& result.memory[0], Size); } template inline @@ -378,6 +385,7 @@ struct Pool ssize TotalSize; ssize NumBlocks; +#if GEN_SUPPORT_CPP_MEMBER_FEATURES #pragma region Member Mapping forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); } @@ -387,6 +395,7 @@ struct Pool forceinline void clear() { GEN_NS clear(* this); } forceinline void free() { GEN_NS free(* this); } #pragma endregion +#endif }; inline diff --git a/project/dependencies/platform.hpp b/project/dependencies/platform.hpp index be1bf8c..272dadb 100644 --- a/project/dependencies/platform.hpp +++ b/project/dependencies/platform.hpp @@ -101,6 +101,14 @@ # define GEN_GCC_VERSION_CHECK(major,minor,patch) (0) #endif +#ifndef GEN_COMPIELR_C +# if defined(__STDC_VERSION__) +# define GEN_COMPILER_C 1 +# else +# define GEN_COMPILER_C 0 +# endif +#endif + #pragma endregion Platform Detection #pragma region Mandatory Includes @@ -114,7 +122,7 @@ #pragma endregion Mandatory Includes -#ifdef GEN_DONT_USE_NAMESPACE +#if GEN_DONT_USE_NAMESPACE || GEN_COMPILER_C # define GEN_NS # define GEN_NS_BEGIN # define GEN_NS_END diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 790851e..60a3e0d 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -11,9 +11,9 @@ using namespace gen; CodeBody gen_ecode( char const* path ) { char scratch_mem[kilobytes(1)]; - Arena scratch = Arena::init_from_memory( scratch_mem, sizeof(scratch_mem) ); + Arena scratch = arena_init_from_memory( scratch_mem, sizeof(scratch_mem) ); - file_read_contents( scratch, zero_terminate, path ); + file_read_contents( allocator_info(scratch), zero_terminate, path ); CSV_Object csv_nodes; csv_parse( &csv_nodes, scratch_mem, GlobalAllocator, false ); @@ -57,9 +57,9 @@ CodeBody gen_ecode( char const* path ) CodeBody gen_eoperator( char const* path ) { char scratch_mem[kilobytes(4)]; - Arena scratch = Arena::init_from_memory( scratch_mem, sizeof(scratch_mem) ); + Arena scratch = arena_init_from_memory( scratch_mem, sizeof(scratch_mem) ); - file_read_contents( scratch, zero_terminate, path ); + file_read_contents( allocator_info(scratch), zero_terminate, path ); CSV_Object csv_nodes; csv_parse( &csv_nodes, scratch_mem, GlobalAllocator, false ); @@ -113,9 +113,9 @@ CodeBody gen_eoperator( char const* path ) CodeBody gen_especifier( char const* path ) { char scratch_mem[kilobytes(4)]; - Arena scratch = Arena::init_from_memory( scratch_mem, sizeof(scratch_mem) ); + Arena scratch = arena_init_from_memory( scratch_mem, sizeof(scratch_mem) ); - file_read_contents( scratch, zero_terminate, path ); + file_read_contents( allocator_info(scratch), zero_terminate, path ); CSV_Object csv_nodes; csv_parse( &csv_nodes, scratch_mem, GlobalAllocator, false ); @@ -218,14 +218,16 @@ CodeBody gen_especifier( char const* path ) CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) { char scratch_mem[kilobytes(16)]; - Arena scratch = Arena::init_from_memory( scratch_mem, sizeof(scratch_mem) ); + Arena scratch = arena_init_from_memory( scratch_mem, sizeof(scratch_mem) ); - FileContents enum_content = file_read_contents( scratch, zero_terminate, etok_path ); + AllocatorInfo scratch_info = allocator_info(scratch); + + FileContents enum_content = file_read_contents( scratch_info, zero_terminate, etok_path ); CSV_Object csv_enum_nodes; csv_parse( &csv_enum_nodes, rcast(char*, enum_content.data), GlobalAllocator, false ); - FileContents attrib_content = file_read_contents( scratch, zero_terminate, attr_path ); + FileContents attrib_content = file_read_contents( scratch_info, zero_terminate, attr_path ); CSV_Object csv_attr_nodes; csv_parse( &csv_attr_nodes, rcast(char*, attrib_content.data), GlobalAllocator, false ); diff --git a/project/helpers/member_proc_support.hpp b/project/helpers/member_proc_support.hpp new file mode 100644 index 0000000..02a3af2 --- /dev/null +++ b/project/helpers/member_proc_support.hpp @@ -0,0 +1,10 @@ +#pragma once + +#include "gen.hpp" + +GEN_NS_BEGIN +#include "dependencies/parsing.hpp" +GEN_NS_END + +using namespace gen; + From cc245cc263d6503ed88dbca2abc88481d994b858 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 17:22:06 -0500 Subject: [PATCH 011/112] new files --- scripts/c_library.refactor | 24 ++++++++++++++++++++++++ scripts/refactor_c_library.ps1 | 0 2 files changed, 24 insertions(+) create mode 100644 scripts/c_library.refactor create mode 100644 scripts/refactor_c_library.ps1 diff --git a/scripts/c_library.refactor b/scripts/c_library.refactor new file mode 100644 index 0000000..22c9961 --- /dev/null +++ b/scripts/c_library.refactor @@ -0,0 +1,24 @@ + __VERSION 1 + +// This is a example template to be used with the refactor program +// Use it to refactor the naming convention of this library to your own. +// Can be used as an aid to help use use your project's implementation if it fullfills the dependencies of this project. +// Example: Most likely have a memory and string library already, just rename the functions and make sure the args are the same. +// Program: https://github.com/Ed94/refactor + +// NOTE: Due to the current limitations of the program, not every symbol in the library can be renamed. +// This is due to the program not actually parsing C/C++. + +// not : Ignore +// include : #includes +// word : Alphanumeric or underscore +// namespace : Prefix search and replace (c-namspaces). +// regex : Unavailable in __VERSION 1. + +// Precedence (highest to lowest): +// word, namespace, regex + +// Gen Macro namespace +// namespace GEN_, new_namespace_ + +// TODO(Ed): This will be large as nearly all symbols will need to optionally support getting prefixed with gen_ or something else the user wants. diff --git a/scripts/refactor_c_library.ps1 b/scripts/refactor_c_library.ps1 new file mode 100644 index 0000000..e69de29 From 6d04165b96d6838fd2c49b49bd97bf3f3a0c75d6 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 18:54:19 -0500 Subject: [PATCH 012/112] Reduce cpp freatures usage of Array container. Almost ready to be inter-operable with C --- gen_c_library/c_library.cpp | 40 +++++++++++++ project/components/interface.cpp | 57 +++++++++--------- project/components/lexer.cpp | 40 ++++++------- project/components/parser.cpp | 26 ++++----- project/dependencies/containers.hpp | 90 ++++++++++++++++++----------- project/dependencies/filesystem.cpp | 14 ++--- project/dependencies/macros.hpp | 10 +++- project/dependencies/parsing.cpp | 46 +++++++-------- project/dependencies/platform.hpp | 2 + project/helpers/helper.hpp | 10 ++-- scripts/build.ci.ps1 | 33 +++++++++++ 11 files changed, 235 insertions(+), 133 deletions(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index adbe936..4d8ea1a 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -18,6 +18,32 @@ GEN_NS_END using namespace gen; +constexpr char const* generation_notice = +"// This file was generated automatially by gencpp's c_library.cpp" +"(See: https://github.com/Ed94/gencpp)\n\n"; + +constexpr StrC roll_own_dependencies_guard_start = txt(R"( +//! If its desired to roll your own dependencies, define GEN_ROLL_OWN_DEPENDENCIES before including this file. +// Dependencies are derived from the c-zpl library: https://github.com/zpl-c/zpl +#ifndef GEN_ROLL_OWN_DEPENDENCIES +)"); + +constexpr StrC roll_own_dependencies_guard_end = txt(R"( +// GEN_ROLL_OWN_DEPENDENCIES +#endif +)"); + +constexpr StrC implementation_guard_start = txt(R"( +#pragma region GENCPP IMPLEMENTATION GUARD +#if defined(GEN_IMPLEMENTATION) && ! defined(GEN_IMPLEMENTED) +# define GEN_IMPLEMENTED +)"); + +constexpr StrC implementation_guard_end = txt(R"( +#endif +#pragma endregion GENCPP IMPLEMENTATION GUARD +)"); + void format_file( char const* path ) { String resolved_path = String::make(GlobalAllocator, to_str(path)); @@ -59,6 +85,20 @@ int gen_main() #define project_dir "../project/" gen::init(); + Code push_ignores = scan_file( project_dir "helpers/push_ignores.inline.hpp" ); + Code pop_ignores = scan_file( project_dir "helpers/pop_ignores.inline.hpp" ); + Code single_header_start = scan_file( "components/header_start.hpp" ); + + Builder + header = Builder::open( "gen/gen.hpp" ); + header.print_fmt( generation_notice ); + header.print_fmt("#pragma once\n\n"); + header.print( push_ignores ); + + // Headers + { + + } gen::deinit(); return 0; diff --git a/project/components/interface.cpp b/project/components/interface.cpp index f4fe0ee..3d22496 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -11,7 +11,7 @@ internal void deinit(); internal void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) { - Arena* last = & Global_AllocatorBuckets.back(); + Arena* last = & back(Global_AllocatorBuckets); switch ( type ) { @@ -24,10 +24,10 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s if ( bucket.PhysicalStart == nullptr ) GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets"); - if ( ! Global_AllocatorBuckets.append( bucket ) ) + if ( ! append( Global_AllocatorBuckets, bucket ) ) GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets"); - last = & Global_AllocatorBuckets.back(); + last = & back(Global_AllocatorBuckets); } return alloc_align( allocator_info(* last), size, alignment ); @@ -51,10 +51,10 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s if ( bucket.PhysicalStart == nullptr ) GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets"); - if ( ! Global_AllocatorBuckets.append( bucket ) ) + if ( ! append( Global_AllocatorBuckets, bucket ) ) GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets"); - last = & Global_AllocatorBuckets.back(); + last = & back(Global_AllocatorBuckets); } void* result = alloc_align( last->Backing, size, alignment ); @@ -235,7 +235,7 @@ void init() { GlobalAllocator = AllocatorInfo { & Global_Allocator_Proc, nullptr }; - Global_AllocatorBuckets = Array::init_reserve( heap(), 128 ); + Global_AllocatorBuckets = array_init_reserve( heap(), 128 ); if ( Global_AllocatorBuckets == nullptr ) GEN_FATAL( "Failed to reserve memory for Global_AllocatorBuckets"); @@ -245,18 +245,17 @@ void init() if ( bucket.PhysicalStart == nullptr ) GEN_FATAL( "Failed to create first bucket for Global_AllocatorBuckets"); - Global_AllocatorBuckets.append( bucket ); - + append( Global_AllocatorBuckets, bucket ); } // Setup the arrays { - CodePools = Array::init_reserve( Allocator_DataArrays, InitSize_DataArrays ); + CodePools = array_init_reserve( Allocator_DataArrays, InitSize_DataArrays ); if ( CodePools == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the CodePools array" ); - StringArenas = Array::init_reserve( Allocator_DataArrays, InitSize_DataArrays ); + StringArenas = array_init_reserve( Allocator_DataArrays, InitSize_DataArrays ); if ( StringArenas == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the StringArenas array" ); @@ -269,7 +268,7 @@ void init() if ( code_pool.PhysicalStart == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the code pool" ); - CodePools.append( code_pool ); + append(CodePools, code_pool ); LexArena = arena_init_from_allocator( Allocator_Lexer, LexAllocator_Size ); @@ -278,7 +277,7 @@ void init() if ( string_arena.PhysicalStart == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the string arena" ); - StringArenas.append( string_arena ); + append(StringArenas, string_arena ); } // Setup the hash tables @@ -290,7 +289,7 @@ void init() } // Preprocessor Defines - PreprocessorDefines = Array::init_reserve( GlobalAllocator, kilobytes(1) ); + PreprocessorDefines = array_init_reserve( GlobalAllocator, kilobytes(1) ); define_constants(); parser::init(); @@ -299,7 +298,7 @@ void init() void deinit() { usize index = 0; - usize left = CodePools.num(); + usize left = num(CodePools); do { Pool* code_pool = & CodePools[index]; @@ -309,7 +308,7 @@ void deinit() while ( left--, left ); index = 0; - left = StringArenas.num(); + left = num(StringArenas); do { Arena* string_arena = & StringArenas[index]; @@ -320,15 +319,15 @@ void deinit() StringCache.destroy(); - CodePools.free(); - StringArenas.free(); + free(CodePools); + free(StringArenas); free(LexArena); - PreprocessorDefines.free(); + free(PreprocessorDefines); index = 0; - left = Global_AllocatorBuckets.num(); + left = num(Global_AllocatorBuckets); do { Arena* bucket = & Global_AllocatorBuckets[ index ]; @@ -337,14 +336,14 @@ void deinit() } while ( left--, left ); - Global_AllocatorBuckets.free(); + free(Global_AllocatorBuckets); parser::deinit(); } void reset() { s32 index = 0; - s32 left = CodePools.num(); + s32 left = num(CodePools); do { Pool* code_pool = & CodePools[index]; @@ -354,7 +353,7 @@ void reset() while ( left--, left ); index = 0; - left = StringArenas.num(); + left = num(StringArenas); do { Arena* string_arena = & StringArenas[index]; @@ -363,14 +362,14 @@ void reset() } while ( left--, left ); - StringCache.clear(); + clear(StringCache); define_constants(); } AllocatorInfo get_string_allocator( s32 str_length ) { - Arena* last = & StringArenas.back(); + Arena* last = & back(StringArenas); usize size_req = str_length + sizeof(StringHeader) + sizeof(char*); @@ -378,10 +377,10 @@ AllocatorInfo get_string_allocator( s32 str_length ) { Arena new_arena = arena_init_from_allocator( Allocator_StringArena, SizePer_StringArena ); - if ( ! StringArenas.append( new_arena ) ) + if ( ! append(StringArenas, new_arena ) ) GEN_FATAL( "gen::get_string_allocator: Failed to allocate a new string arena" ); - last = & StringArenas.back(); + last = & back(StringArenas); } return allocator_info(* last); @@ -408,7 +407,7 @@ StringCached get_cached_string( StrC str ) // Used internally to retireve a Code object form the CodePool. Code make_code() { - Pool* allocator = & CodePools.back(); + Pool* allocator = & back(CodePools); if ( allocator->FreeList == nullptr ) { Pool code_pool = pool_init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) ); @@ -416,10 +415,10 @@ Code make_code() if ( code_pool.PhysicalStart == nullptr ) GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePool allcoator returned nullptr." ); - if ( ! CodePools.append( code_pool ) ) + if ( ! append( CodePools, code_pool ) ) GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePools failed to append new pool." ); - allocator = & CodePools.back(); + allocator = & back(CodePools); } Code result { rcast( AST*, alloc( allocator_info(* allocator), sizeof(AST) )) }; diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index 4587544..12788a8 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -222,7 +222,7 @@ s32 lex_preprocessor_directive( , Token& token ) { char const* hash = scanner; - Tokens.append( { hash, 1, TokType::Preprocess_Hash, line, column, TF_Preprocess } ); + append(Tokens, { hash, 1, TokType::Preprocess_Hash, line, column, TF_Preprocess } ); move_forward(); SkipWhitespace(); @@ -298,14 +298,14 @@ s32 lex_preprocessor_directive( token.Length = token.Length + token.Text - hash; token.Text = hash; - Tokens.append( token ); + append(Tokens, token ); return Lex_Continue; // Skip found token, its all handled here. } if ( token.Type == TokType::Preprocess_Else || token.Type == TokType::Preprocess_EndIf ) { token.Flags |= TF_Preprocess_Cond; - Tokens.append( token ); + append(Tokens, token ); end_line(); return Lex_Continue; } @@ -314,7 +314,7 @@ s32 lex_preprocessor_directive( token.Flags |= TF_Preprocess_Cond; } - Tokens.append( token ); + append(Tokens, token ); SkipWhitespace(); @@ -338,7 +338,7 @@ s32 lex_preprocessor_directive( name.Length++; } - Tokens.append( name ); + append(Tokens, name ); u64 key = crc32( name.Text, name.Length ); defines.set( key, name ); @@ -384,7 +384,7 @@ s32 lex_preprocessor_directive( move_forward(); } - Tokens.append( preprocess_content ); + append(Tokens, preprocess_content ); return Lex_Continue; // Skip found token, its all handled here. } @@ -446,7 +446,7 @@ s32 lex_preprocessor_directive( preprocess_content.Length++; } - Tokens.append( preprocess_content ); + append(Tokens, preprocess_content ); return Lex_Continue; // Skip found token, its all handled here. } @@ -461,7 +461,7 @@ void lex_found_token( StrC& content { if ( token.Type != TokType::Invalid ) { - Tokens.append( token ); + append(Tokens, token ); return; } @@ -488,7 +488,7 @@ void lex_found_token( StrC& content } token.Type = type; - Tokens.append( token ); + append(Tokens, token ); return; } @@ -498,7 +498,7 @@ void lex_found_token( StrC& content { token.Type = type; token.Flags |= TF_Specifier; - Tokens.append( token ); + append(Tokens, token ); return; } @@ -506,7 +506,7 @@ void lex_found_token( StrC& content if ( type != TokType::Invalid ) { token.Type = type; - Tokens.append( token ); + append(Tokens, token ); return; } @@ -558,7 +558,7 @@ void lex_found_token( StrC& content token.Type = TokType::Identifier; } - Tokens.append( token ); + append(Tokens, token ); } @@ -582,7 +582,7 @@ TokArray lex( StrC content ) return { { nullptr }, 0 }; } - for ( StringCached entry : PreprocessorDefines ) + foreach( StringCached, entry, PreprocessorDefines ) { s32 length = 0; char const* scanner = entry.Data; @@ -600,7 +600,7 @@ TokArray lex( StrC content ) defines.set( key, entry ); } - Tokens.clear(); + clear(Tokens); while (left ) { @@ -630,7 +630,7 @@ TokArray lex( StrC content ) token.Type = TokType::NewLine; token.Length++; - Tokens.append( token ); + append(Tokens, token ); continue; } } @@ -1099,7 +1099,7 @@ TokArray lex( StrC content ) move_forward(); token.Length++; } - Tokens.append( token ); + append(Tokens, token ); continue; } else if ( current == '*' ) @@ -1135,7 +1135,7 @@ TokArray lex( StrC content ) move_forward(); token.Length++; } - Tokens.append( token ); + append(Tokens, token ); // end_line(); continue; } @@ -1228,9 +1228,9 @@ TokArray lex( StrC content ) } else { - s32 start = max( 0, Tokens.num() - 100 ); + s32 start = max( 0, num(Tokens) - 100 ); log_fmt("\n%d\n", start); - for ( s32 idx = start; idx < Tokens.num(); idx++ ) + for ( s32 idx = start; idx < num(Tokens); idx++ ) { log_fmt( "Token %d Type: %s : %.*s\n" , idx @@ -1253,7 +1253,7 @@ TokArray lex( StrC content ) lex_found_token( content, left, scanner, line, column, defines, token ); } - if ( Tokens.num() == 0 ) + if ( num(Tokens) == 0 ) { log_failure( "Failed to lex any tokens" ); return { { nullptr }, 0 }; diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 94e5315..2c19973 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -48,11 +48,11 @@ struct ParseContext String result = String::make_reserve( GlobalAllocator, kilobytes(4) ); Token scope_start = Scope->Start; - Token last_valid = Tokens.Idx >= Tokens.Arr.num() ? Tokens.Arr[Tokens.Arr.num() -1] : Tokens.current(); + Token last_valid = Tokens.Idx >= num(Tokens.Arr) ? Tokens.Arr[num(Tokens.Arr) -1] : Tokens.current(); sptr length = scope_start.Length; char const* current = scope_start.Text + length; - while ( current <= Tokens.Arr.back().Text && *current != '\n' && length < 74 ) + while ( current <= back(Tokens.Arr).Text && *current != '\n' && length < 74 ) { current++; length++; @@ -96,7 +96,7 @@ global ParseContext Context; bool TokArray::__eat( TokType type ) { - if ( Arr.num() - Idx <= 0 ) + if ( num(Arr) - Idx <= 0 ) { log_failure( "No tokens left.\n%s", Context.to_string() ); return false; @@ -167,7 +167,7 @@ if ( def.Ptr == nullptr ) \ # define prevtok Context.Tokens.previous() # define nexttok Context.Tokens.next() # define eat( Type_ ) Context.Tokens.__eat( Type_ ) -# define left ( Context.Tokens.Arr.num() - Context.Tokens.Idx ) +# define left ( num(Context.Tokens.Arr) - Context.Tokens.Idx ) #ifdef check #define CHECK_WAS_DEFINED @@ -745,7 +745,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) } Token interface_tok = parse_identifier(); - interfaces.append( def_type( interface_tok ) ); + append(interfaces, def_type( interface_tok ) ); // : , ... } } @@ -777,7 +777,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) if ( inline_cmt ) result->InlineCmt = inline_cmt; - interfaces.free(); + free(interfaces); return result; } @@ -1152,7 +1152,7 @@ Code parse_complicated_definition( TokType which ) s32 idx = tokens.Idx; s32 level = 0; - for ( ; idx < tokens.Arr.num(); idx++ ) + for ( ; idx < num(tokens.Arr); idx++ ) { if ( tokens[ idx ].Type == TokType::BraceCurly_Open ) level++; @@ -1837,7 +1837,7 @@ CodeBody parse_global_nspace( CodeT which ) bool found_operator_cast_outside_class_implmentation = false; s32 idx = Context.Tokens.Idx; - for ( ; idx < Context.Tokens.Arr.num(); idx++ ) + for ( ; idx < num(Context.Tokens.Arr); idx++ ) { Token tok = Context.Tokens[ idx ]; @@ -1909,14 +1909,14 @@ Code parse_global_nspace_constructor_destructor( CodeSpecifiers specifiers ) s32 idx = tokens.Idx; Token nav = tokens[ idx ]; - for ( ; idx < tokens.Arr.num(); idx++, nav = tokens[ idx ] ) + for ( ; idx < num(tokens.Arr); idx++, nav = tokens[ idx ] ) { if ( nav.Text[0] == '<' ) { // Skip templated expressions as they mey have expressions with the () operators s32 capture_level = 0; s32 template_level = 0; - for ( ; idx < tokens.Arr.num(); idx++, nav = tokens[idx] ) + for ( ; idx < num(tokens.Arr); idx++, nav = tokens[idx] ) { if (nav.Text[ 0 ] == '<') ++ template_level; @@ -2511,7 +2511,7 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes bool found_operator = false; s32 idx = Context.Tokens.Idx; - for ( ; idx < Context.Tokens.Arr.num(); idx++ ) + for ( ; idx < num(Context.Tokens.Arr); idx++ ) { Token tok = Context.Tokens[ idx ]; @@ -4348,7 +4348,7 @@ CodeTemplate parse_template() bool found_operator_cast_outside_class_implmentation = false; s32 idx = Context.Tokens.Idx; - for ( ; idx < Context.Tokens.Arr.num(); idx++ ) + for ( ; idx < num(Context.Tokens.Arr); idx++ ) { Token tok = Context.Tokens[ idx ]; @@ -4896,7 +4896,7 @@ CodeTypedef parse_typedef() s32 idx = tokens.Idx; s32 level = 0; - for ( ; idx < tokens.Arr.num(); idx ++ ) + for ( ; idx < num(tokens.Arr); idx ++ ) { if ( tokens[idx].Type == TokType::BraceCurly_Open ) level++; diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index d185b24..9c12af2 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -14,12 +14,26 @@ template using TRemoveConst = typename RemoveConst::Type; #pragma region Array +#if ! GEN_COMPILER_C +#define Array(Type) Array + +// #define array_init(Type, ...) array_init (__VA_ARGS__) +// #define array_init_reserve(Type, ...) array_init_reserve(__VA_ARGS__) +#endif + struct ArrayHeader; + +#if GEN_SUPPORT_CPP_MEMBER_FEATURES template struct Array; +#else +template +using Array = Type*; +#endif + +usize array_grow_formula(ssize value); template Array array_init(AllocatorInfo allocator); template Array array_init_reserve(AllocatorInfo allocator, ssize capacity); -template usize array_grow_formula(ssize value); template bool append(Array& array, Array other); template bool append(Array& array, Type value); template bool append(Array& array, Type* items, usize item_num); @@ -38,18 +52,22 @@ template bool resize(Array& array, usize num); template bool set_capacity(Array& array, usize new_capacity); template ArrayHeader* get_header(Array& array); +template forceinline Type* begin(Array& array) { return array; } +template forceinline Type* end(Array& array) { return array + get_header(array)->Num; } +template forceinline Type* next(Type* entry) { return entry + 1; } + struct ArrayHeader { AllocatorInfo Allocator; usize Capacity; usize Num; }; +#if GEN_SUPPORT_CPP_MEMBER_FEATURES template struct Array { Type* Data; -#if 1 #pragma region Member Mapping forceinline static Array init(AllocatorInfo allocator) { return GEN_NS array_init(allocator); } forceinline static Array init_reserve(AllocatorInfo allocator, ssize capacity) { return GEN_NS array_init_reserve(allocator, capacity); } @@ -78,12 +96,12 @@ struct Array forceinline Type* begin() { return Data; } forceinline Type* end() { return Data + get_header()->Num; } #pragma endregion Member Mapping -#endif }; +#endif template inline Array array_init(AllocatorInfo allocator) { - return array_init_reserve(allocator, array_grow_formula(0)); + return array_init_reserve(allocator, array_grow_formula(0)); } template inline @@ -101,7 +119,6 @@ Array array_init_reserve(AllocatorInfo allocator, ssize capacity) return {rcast(Type*, header + 1)}; } -template inline usize array_grow_formula(ssize value) { return 2 * value + 8; } @@ -123,7 +140,7 @@ bool append(Array& array, Type value) header = get_header(array); } - array.Data[header->Num] = value; + array[header->Num] = value; header->Num++; return true; @@ -166,7 +183,7 @@ bool append_at(Array& array, Type item, usize idx) header = get_header(array); } - Type* target = array.Data + idx; + Type* target = array + idx; mem_move(target + 1, target, (header->Num - idx) * sizeof(Type)); header->Num++; @@ -205,7 +222,7 @@ bool append_at(Array& array, Type* items, usize item_num, usize idx) template inline Type& back(Array& array) { ArrayHeader* header = get_header(array); - return array.Data[header->Num - 1]; + return array[header->Num - 1]; } template inline @@ -224,7 +241,7 @@ bool fill(Array& array, usize begin, usize end, Type value) for (ssize idx = ssize(begin); idx < ssize(end); idx++) { - array.Data[idx] = value; + array[idx] = value; } return true; @@ -234,20 +251,22 @@ template inline void free(Array& array) { ArrayHeader* header = get_header(array); gen::free(header->Allocator, header); - array.Data = nullptr; + Type*& Data = rcast(Type*&, array); + Data = nullptr; } template inline ArrayHeader* get_header(Array& array) { using NonConstType = TRemoveConst; - return rcast(ArrayHeader*, const_cast(array.Data)) - 1; + Type* Data = array; // This should do nothing in C but in C++ gets member Data struct. + return rcast(ArrayHeader*, const_cast(Data)) - 1; } template inline bool grow(Array& array, usize min_capacity) { ArrayHeader* header = get_header(array); - usize new_capacity = array_grow_formula(header->Capacity); + usize new_capacity = array_grow_formula(header->Capacity); if (new_capacity < min_capacity) new_capacity = min_capacity; @@ -273,7 +292,7 @@ void remove_at(Array& array, usize idx) ArrayHeader* header = get_header(array); GEN_ASSERT(idx < header->Num); - mem_move(array.Data + idx, array.Data + idx + 1, sizeof(Type) * (header->Num - idx - 1)); + mem_move(array + idx, array + idx + 1, sizeof(Type) * (header->Num - idx - 1)); header->Num--; } @@ -329,7 +348,8 @@ bool set_capacity(Array& array, usize new_capacity) GEN_NS free(header->Allocator, header); - array.Data = rcast(Type*, new_header + 1); + Type*& Data = rcast(Type*&, array); + Data = rcast(Type*, new_header + 1); return true; } #pragma endregion Array @@ -371,11 +391,11 @@ template bool full(HashTable& table); template void map(HashTable& table, void (*map_proc)(u64 key, Type value)); template void map_mut(HashTable& table, void (*map_proc)(u64 key, Type* value)); +static constexpr f32 HashTable_CriticalLoadScale = 0.7f; + template struct HashTable { - static constexpr f32 CriticalLoadScale = 0.7f; - Array Hashes; Array> Entries; @@ -411,26 +431,26 @@ HashTable hashtable_init_reserve(AllocatorInfo allocator, usize num) { HashTable result = { { nullptr }, { nullptr } }; - result.Hashes = Array::init_reserve(allocator, num); - result.Hashes.get_header()->Num = num; - result.Hashes.resize(num); - result.Hashes.fill(0, num, -1); + result.Hashes = array_init_reserve(allocator, num); + get_header(result.Hashes)->Num = num; + resize(result.Hashes, num); + fill(result.Hashes, 0, num, -1); - result.Entries = Array>::init_reserve(allocator, num); + result.Entries = array_init_reserve>(allocator, num); return result; } template inline void clear(HashTable& table) { - table.Entries.clear(); - table.Hashes.fill(0, table.Hashes.num(), -1); + clear(table.Entries); + fill(table.Hashes, 0, num(table.Hashes), -1); } template inline void destroy(HashTable& table) { - if (table.Hashes && table.Hashes.get_header()->Capacity) { - table.Hashes.free(); - table.Entries.free(); + if (table.Hashes && get_header(table.Hashes)->Capacity) { + free(table.Hashes); + free(table.Entries); } } @@ -463,7 +483,7 @@ void map_mut(HashTable& table, void (*map_proc)(u64 key, Type* value)) { template inline void grow(HashTable& table) { - ssize new_num = Array>::grow_formula(table.Entries.num()); + ssize new_num = array_grow_formula(num(table.Entries)); rehash(table, new_num); } @@ -471,9 +491,9 @@ template inline void rehash(HashTable& table, ssize new_num) { ssize last_added_index; - HashTable new_ht = hashtable_init_reserve(table.Hashes.get_header()->Allocator, new_num); + HashTable new_ht = hashtable_init_reserve(get_header(table.Hashes)->Allocator, new_num); - for (ssize idx = 0; idx < ssize(table.Entries.num()); ++idx) + for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) { HashTableFindResult find_result; HashTableEntry& entry = table.Entries[idx]; @@ -580,8 +600,8 @@ ssize add_entry(HashTable& table, u64 key) { ssize idx; HashTableEntry entry = { key, -1 }; - idx = table.Entries.num(); - table.Entries.append(entry); + idx = num(table.Entries); + append(table.Entries, entry); return idx; } @@ -590,9 +610,9 @@ HashTableFindResult find(HashTable& table, u64 key) { HashTableFindResult result = { -1, -1, -1 }; - if (table.Hashes.num() > 0) + if (num(table.Hashes) > 0) { - result.HashIndex = key % table.Hashes.num(); + result.HashIndex = key % num(table.Hashes); result.EntryIndex = table.Hashes[result.HashIndex]; while (result.EntryIndex >= 0) @@ -610,8 +630,8 @@ HashTableFindResult find(HashTable& table, u64 key) template inline bool full(HashTable& table) { - usize critical_load = usize(HashTable::CriticalLoadScale * f32(table.Hashes.num())); - b32 result = table.Entries.num() > critical_load; + usize critical_load = usize(HashTable_CriticalLoadScale * f32(num(table.Hashes))); + b32 result = num(table.Entries) > critical_load; return result; } #pragma endregion HashTable diff --git a/project/dependencies/filesystem.cpp b/project/dependencies/filesystem.cpp index c6c0444..931f9cd 100644 --- a/project/dependencies/filesystem.cpp +++ b/project/dependencies/filesystem.cpp @@ -505,7 +505,7 @@ b8 file_stream_new( FileInfo* file, AllocatorInfo allocator ) d->allocator = allocator; d->flags = EFileStream_CLONE_WRITABLE; d->cap = 0; - d->buf = Array::init( allocator ); + d->buf = array_init( allocator ); if ( ! d->buf ) return false; @@ -531,7 +531,7 @@ b8 file_stream_open( FileInfo* file, AllocatorInfo allocator, u8* buffer, ssize d->flags = flags; if ( d->flags & EFileStream_CLONE_WRITABLE ) { - Array arr = Array::init_reserve( allocator, size ); + Array arr = array_init_reserve( allocator, size ); d->buf = arr; if ( ! d->buf ) @@ -540,7 +540,7 @@ b8 file_stream_open( FileInfo* file, AllocatorInfo allocator, u8* buffer, ssize mem_copy( d->buf, buffer, size ); d->cap = size; - arr.get_header()->Num = size; + get_header(arr)->Num = size; } else { @@ -610,9 +610,9 @@ GEN_FILE_WRITE_AT_PROC( _memory_file_write ) { Array arr = { d->buf }; - if ( arr.get_header()->Capacity < usize(new_cap) ) + if ( get_header(arr)->Capacity < usize(new_cap) ) { - if ( ! arr.grow( ( s64 )( new_cap ) ) ) + if ( ! grow( arr, ( s64 )( new_cap ) ) ) return false; d->buf = arr; } @@ -626,7 +626,7 @@ GEN_FILE_WRITE_AT_PROC( _memory_file_write ) mem_copy( d->buf + offset + rwlen, pointer_add_const( buffer, rwlen ), extralen ); d->cap = new_cap; - arr.get_header()->Capacity = new_cap; + get_header(arr)->Capacity = new_cap; } else { @@ -647,7 +647,7 @@ GEN_FILE_CLOSE_PROC( _memory_file_close ) if ( d->flags & EFileStream_CLONE_WRITABLE ) { Array arr = { d->buf }; - arr.free(); + free(arr); } free( allocator, d ); diff --git a/project/dependencies/macros.hpp b/project/dependencies/macros.hpp index 41ee2d6..790f1b4 100644 --- a/project/dependencies/macros.hpp +++ b/project/dependencies/macros.hpp @@ -187,7 +187,7 @@ #if !defined(typeof) && (!GEN_COMPILER_C || __STDC_VERSION__ < 202311L) # if ! GEN_COMPILER_C -# define typeof +# define typeof decltype # elif defined(_MSC_VER) # define typeof(x) __typeof(x) # elif defined(__GNUC__) || defined(__clang__) @@ -197,4 +197,12 @@ # endif #endif +// This is intended to only really be used internally or with the C-library variant +// C++ users can just use the for-range directly. +#if GEN_COMPILER_C +# define foreach(Type, entry_id, iterable) for ( Type entry_id = begin(iterable); entry_id != end(iterable); entry_id = next(entry_id) ) +#else +# define foreach(Type, entry_id, iterable) for ( Type entry_id : iterable ) +#endif + #pragma endregion Macros diff --git a/project/dependencies/parsing.cpp b/project/dependencies/parsing.cpp index 293555e..c314218 100644 --- a/project/dependencies/parsing.cpp +++ b/project/dependencies/parsing.cpp @@ -23,7 +23,7 @@ u8 adt_make_branch( ADT_Node* node, AllocatorInfo backing, char const* name, b32 node->type = type; node->name = name; node->parent = parent; - node->nodes = Array::init( backing ); + node->nodes = array_init( backing ); if ( ! node->nodes ) return EADT_ERROR_OUT_OF_MEMORY; @@ -36,12 +36,12 @@ u8 adt_destroy_branch( ADT_Node* node ) GEN_ASSERT_NOT_NULL( node ); if ( ( node->type == EADT_TYPE_OBJECT || node->type == EADT_TYPE_ARRAY ) && node->nodes ) { - for ( ssize i = 0; i < scast(ssize, node->nodes.num()); ++i ) + for ( ssize i = 0; i < scast(ssize, num(node->nodes)); ++i ) { adt_destroy_branch( node->nodes + i ); } - node->nodes.free(); + free(node->nodes); } return 0; } @@ -66,7 +66,7 @@ ADT_Node* adt_find( ADT_Node* node, char const* name, b32 deep_search ) return NULL; } - for ( ssize i = 0; i < scast(ssize, node->nodes.num()); i++ ) + for ( ssize i = 0; i < scast(ssize, num(node->nodes)); i++ ) { if ( ! str_compare( node->nodes[ i ].name, name ) ) { @@ -76,7 +76,7 @@ ADT_Node* adt_find( ADT_Node* node, char const* name, b32 deep_search ) if ( deep_search ) { - for ( ssize i = 0; i < scast(ssize, node->nodes.num()); i++ ) + for ( ssize i = 0; i < scast(ssize, num(node->nodes)); i++ ) { ADT_Node* res = adt_find( node->nodes + i, name, deep_search ); @@ -132,7 +132,7 @@ internal ADT_Node* _adt_get_value( ADT_Node* node, char const* value ) internal ADT_Node* _adt_get_field( ADT_Node* node, char* name, char* value ) { - for ( ssize i = 0; i < scast(ssize, node->nodes.num()); i++ ) + for ( ssize i = 0; i < scast(ssize, num(node->nodes)); i++ ) { if ( ! str_compare( node->nodes[ i ].name, name ) ) { @@ -207,7 +207,7 @@ ADT_Node* adt_query( ADT_Node* node, char const* uri ) /* run a value comparison against any child that is an object node */ else if ( node->type == EADT_TYPE_ARRAY ) { - for ( ssize i = 0; i < scast(ssize, node->nodes.num()); i++ ) + for ( ssize i = 0; i < scast(ssize, num(node->nodes)); i++ ) { ADT_Node* child = &node->nodes[ i ]; if ( child->type != EADT_TYPE_OBJECT ) @@ -225,7 +225,7 @@ ADT_Node* adt_query( ADT_Node* node, char const* uri ) /* [value] */ else { - for ( ssize i = 0; i < scast(ssize, node->nodes.num()); i++ ) + for ( ssize i = 0; i < scast(ssize, num(node->nodes)); i++ ) { ADT_Node* child = &node->nodes[ i ]; if ( _adt_get_value( child, l_b2 ) ) @@ -257,7 +257,7 @@ ADT_Node* adt_query( ADT_Node* node, char const* uri ) else { ssize idx = ( ssize )str_to_i64( buf, NULL, 10 ); - if ( idx >= 0 && idx < scast(ssize, node->nodes.num()) ) + if ( idx >= 0 && idx < scast(ssize, num(node->nodes)) ) { found_node = &node->nodes[ idx ]; @@ -282,12 +282,12 @@ ADT_Node* adt_alloc_at( ADT_Node* parent, ssize index ) if ( ! parent->nodes ) return NULL; - if ( index < 0 || index > scast(ssize, parent->nodes.num()) ) + if ( index < 0 || index > scast(ssize, num(parent->nodes)) ) return NULL; ADT_Node o = { 0 }; o.parent = parent; - if ( ! parent->nodes.append_at( o, index ) ) + if ( ! append_at( parent->nodes, o, index ) ) return NULL; return parent->nodes + index; @@ -303,7 +303,7 @@ ADT_Node* adt_alloc( ADT_Node* parent ) if ( ! parent->nodes ) return NULL; - return adt_alloc_at( parent, parent->nodes.num() ); + return adt_alloc_at( parent, num(parent->nodes) ); } b8 adt_set_obj( ADT_Node* obj, char const* name, AllocatorInfo backing ) @@ -357,7 +357,7 @@ ADT_Node* adt_move_node( ADT_Node* node, ADT_Node* new_parent ) GEN_ASSERT_NOT_NULL( node ); GEN_ASSERT_NOT_NULL( new_parent ); GEN_ASSERT( new_parent->type == EADT_TYPE_ARRAY || new_parent->type == EADT_TYPE_OBJECT ); - return adt_move_node_at( node, new_parent, new_parent->nodes.num() ); + return adt_move_node_at( node, new_parent, num(new_parent->nodes) ); } void adt_swap_nodes( ADT_Node* node, ADT_Node* other_node ) @@ -381,7 +381,7 @@ void adt_remove_node( ADT_Node* node ) GEN_ASSERT_NOT_NULL( node->parent ); ADT_Node* parent = node->parent; ssize index = ( pointer_diff( parent->nodes, node ) / size_of( ADT_Node ) ); - parent->nodes.remove_at( index ); + remove_at( parent->nodes, index ); } ADT_Node* adt_append_obj( ADT_Node* parent, char const* name ) @@ -389,7 +389,7 @@ ADT_Node* adt_append_obj( ADT_Node* parent, char const* name ) ADT_Node* o = adt_alloc( parent ); if ( ! o ) return NULL; - if ( adt_set_obj( o, name, parent->nodes.get_header()->Allocator ) ) + if ( adt_set_obj( o, name, get_header(parent->nodes)->Allocator ) ) { adt_remove_node( o ); return NULL; @@ -402,7 +402,7 @@ ADT_Node* adt_append_arr( ADT_Node* parent, char const* name ) ADT_Node* o = adt_alloc( parent ); if ( ! o ) return NULL; - if ( adt_set_arr( o, name, parent->nodes.get_header()->Allocator ) ) + if ( adt_set_arr( o, name, get_header(parent->nodes)->Allocator ) ) { adt_remove_node( o ); return NULL; @@ -946,12 +946,12 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b } } - if ( columnIndex >= scast(ssize, root->nodes.num()) ) + if ( columnIndex >= scast(ssize, num(root->nodes)) ) { adt_append_arr( root, NULL ); } - root->nodes[ columnIndex ].nodes.append( rowItem ); + append(root->nodes[ columnIndex ].nodes, rowItem ); if ( delimiter == delim ) { @@ -979,7 +979,7 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b } while ( *currentChar ); - if ( root->nodes.num() == 0 ) + if (num( root->nodes) == 0 ) { GEN_CSV_ASSERT( "unexpected end of input. stream is empty." ); error = ECSV_Error__UNEXPECTED_END_OF_INPUT; @@ -989,12 +989,12 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b /* consider first row as a header. */ if ( has_header ) { - for ( ssize i = 0; i < scast(ssize, root->nodes.num()); i++ ) + for ( ssize i = 0; i < scast(ssize, num(root->nodes)); i++ ) { CSV_Object* col = root->nodes + i; CSV_Object* hdr = col->nodes; col->name = hdr->string; - col->nodes.remove_at( 0 ); + remove_at(col->nodes, 0 ); } } @@ -1057,11 +1057,11 @@ void csv_write_delimiter( FileInfo* file, CSV_Object* obj, char delimiter ) GEN_ASSERT_NOT_NULL( file ); GEN_ASSERT_NOT_NULL( obj ); GEN_ASSERT( obj->nodes ); - ssize cols = obj->nodes.num(); + ssize cols = num(obj->nodes); if ( cols == 0 ) return; - ssize rows = obj->nodes[ 0 ].nodes.num(); + ssize rows = num(obj->nodes[ 0 ].nodes); if ( rows == 0 ) return; diff --git a/project/dependencies/platform.hpp b/project/dependencies/platform.hpp index 272dadb..ca837af 100644 --- a/project/dependencies/platform.hpp +++ b/project/dependencies/platform.hpp @@ -1,3 +1,5 @@ +#define GEN_SUPPORT_CPP_MEMBER_FEATURES 1 + #ifdef GEN_INTELLISENSE_DIRECTIVES # pragma once #endif diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 60a3e0d..fde3a3d 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -70,7 +70,7 @@ CodeBody gen_eoperator( char const* path ) String enum_entries = String::make_reserve( GlobalAllocator, kilobytes(1) ); String to_str_entries = String::make_reserve( GlobalAllocator, kilobytes(1) ); - for (usize idx = 0; idx < enum_strs.num(); idx++) + for (usize idx = 0; idx < num(enum_strs); idx++) { char const* enum_str = enum_strs[idx].string; char const* entry_to_str = str_strs [idx].string; @@ -126,7 +126,7 @@ CodeBody gen_especifier( char const* path ) String enum_entries = String::make_reserve( GlobalAllocator, kilobytes(1) ); String to_str_entries = String::make_reserve( GlobalAllocator, kilobytes(1) ); - for (usize idx = 0; idx < enum_strs.num(); idx++) + for (usize idx = 0; idx < num(enum_strs); idx++) { char const* enum_str = enum_strs[idx].string; char const* entry_to_str = str_strs [idx].string; @@ -243,7 +243,7 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) String to_str_attributes = String::make_reserve( GlobalAllocator, kilobytes(4) ); String attribute_define_entries = String::make_reserve( GlobalAllocator, kilobytes(4) ); - for (usize idx = 0; idx < enum_strs.num(); idx++) + for (usize idx = 0; idx < num(enum_strs); idx++) { char const* enum_str = enum_strs[idx].string; char const* entry_to_str = enum_str_strs [idx].string; @@ -252,7 +252,7 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) to_str_entries.append_fmt( "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } - for ( usize idx = 0; idx < attribute_strs.num(); idx++ ) + for ( usize idx = 0; idx < num(attribute_strs); idx++ ) { char const* attribute_str = attribute_strs[idx].string; char const* entry_to_str = attribute_str_strs [idx].string; @@ -261,7 +261,7 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) to_str_attributes.append_fmt( "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); attribute_define_entries.append_fmt( "Entry( Attribute_%s, \"%s\" )", attribute_str, entry_to_str ); - if ( idx < attribute_strs.num() - 1 ) + if ( idx < num(attribute_strs) - 1 ) attribute_define_entries.append( " \\\n"); else attribute_define_entries.append( "\n"); diff --git a/scripts/build.ci.ps1 b/scripts/build.ci.ps1 index 877d522..73cd1b3 100644 --- a/scripts/build.ci.ps1 +++ b/scripts/build.ci.ps1 @@ -192,7 +192,40 @@ if ( $singleheader ) if ( $c_library ) { + $path_build = join-path $path_c_library build + $path_gen = join-path $path_c_library gen + if ( -not(Test-Path($path_build) )) { + New-Item -ItemType Directory -Path $path_build + } + if ( -not(Test-Path($path_gen) )) { + New-Item -ItemType Directory -Path $path_gen + } + + $includes = @( $path_project ) + $unit = join-path $path_c_library "c_library.cpp" + $executable = join-path $path_build "c_library.exe" + + $compiler_args = @() + $compiler_args += ( $flag_define + 'GEN_TIME' ) + + $linker_args = @( + $flag_link_win_subsystem_console + ) + + build-simple $path_build $includes $compiler_args $linker_args $unit $executable + + Push-Location $path_singleheader + if ( Test-Path( $executable ) ) { + write-host "`nRunning c_library generator" + $time_taken = Measure-Command { & $executable + | ForEach-Object { + write-host `t $_ -ForegroundColor Green + } + } + write-host "`nc_library generator completed in $($time_taken.TotalMilliseconds) ms" + } + Pop-Location } if ( $unreal ) From fbdb870986cb1ab2242eb70dd29c8aaa4feaddc3 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 23:38:27 -0500 Subject: [PATCH 013/112] Finished first pass reviewing memory.hpp for C lib generation --- .gitignore | 1 + .vscode/settings.json | 3 +- gen_c_library/c_library.cpp | 104 +++++++++++++++- gen_c_library/components/fixed_arena.hpp | 119 +++++++++++++++++++ gen_c_library/components/header_start.hpp | 14 +++ gen_c_library/components/misc.hpp | 61 ++++++++++ gen_singleheader/components/header_start.hpp | 9 -- project/components/code_types.hpp | 40 +++++++ project/components/header_end.hpp | 2 + project/components/parser.cpp | 4 +- project/dependencies/basic_types.hpp | 4 +- project/dependencies/containers.hpp | 8 +- project/dependencies/macros.hpp | 2 +- project/dependencies/memory.hpp | 5 +- scripts/build.ci.ps1 | 4 +- 15 files changed, 350 insertions(+), 30 deletions(-) create mode 100644 gen_c_library/components/fixed_arena.hpp create mode 100644 gen_c_library/components/header_start.hpp create mode 100644 gen_c_library/components/misc.hpp diff --git a/.gitignore b/.gitignore index 9ff6447..d638b75 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,4 @@ project/auxillary/vis_ast/dependencies/temp test/gen/original singleheader/gen/scratch.hpp test/gen/scratch.cpp +gen_c_library/gen diff --git a/.vscode/settings.json b/.vscode/settings.json index 65611a9..87d304b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -38,7 +38,8 @@ "android_native_app_glue.h": "c", "raylib.h": "c", "*.m": "cpp", - "atomic": "cpp" + "atomic": "cpp", + "gen.h": "c" }, "C_Cpp.intelliSenseEngineFallback": "disabled", "mesonbuild.configureOnOpen": true, diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 4d8ea1a..e29a620 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -1,7 +1,7 @@ #define GEN_DEFINE_LIBRARY_CODE_CONSTANTS #define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_EXPOSE_BACKEND -#include "gen.cpp" +#include "../project/gen.cpp" #include "helpers/push_ignores.inline.hpp" #include "helpers/helper.hpp" @@ -16,6 +16,9 @@ GEN_NS_END #include // for system() +#include "components/fixed_arena.hpp" +#include "components/misc.hpp" + using namespace gen; constexpr char const* generation_notice = @@ -80,26 +83,117 @@ Code dump_to_scratch_and_retireve( Code code ) return result; } +CodeBody parse_file( const char* path ) +{ + FileContents file = file_read_contents( GlobalAllocator, true, path ); + CodeBody code = parse_global_body( { file.size, (char const*)file.data } ); + return code; +} + int gen_main() { #define project_dir "../project/" gen::init(); - Code push_ignores = scan_file( project_dir "helpers/push_ignores.inline.hpp" ); - Code pop_ignores = scan_file( project_dir "helpers/pop_ignores.inline.hpp" ); - Code single_header_start = scan_file( "components/header_start.hpp" ); + Code push_ignores = scan_file( project_dir "helpers/push_ignores.inline.hpp" ); + Code pop_ignores = scan_file( project_dir "helpers/pop_ignores.inline.hpp" ); + Code c_library_header_start = scan_file( "components/header_start.hpp" ); Builder - header = Builder::open( "gen/gen.hpp" ); + header = Builder::open( "gen/gen.h" ); header.print_fmt( generation_notice ); header.print_fmt("#pragma once\n\n"); header.print( push_ignores ); // Headers { + header.print( c_library_header_start ); + Code platform = scan_file( project_dir "dependencies/platform.hpp" ); + Code macros = scan_file( project_dir "dependencies/macros.hpp" ); + Code basic_types = scan_file( project_dir "dependencies/basic_types.hpp" ); + Code debug = scan_file( project_dir "dependencies/debug.hpp" ); + + CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" ); + CodeBody memory = def_body(ECode::Struct_Body); + for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry ) + { + switch (entry->Type) + { + case ECode::Using: + { + log_fmt("REPLACE THIS MANUALLY: %S\n", entry->Name); + CodeUsing using_ver = entry.cast(); + CodeTypedef typedef_ver = def_typedef(using_ver->Name, using_ver->UnderlyingType); + + memory.append(typedef_ver); + } + break; + case ECode::Function: + { + CodeFn fn = entry.cast(); + s32 constexpr_found = fn->Specs.remove( ESpecifier::Constexpr ); + if (constexpr_found > -1) { + log_fmt("Found constexpr proc\n"); + fn->Specs.append(ESpecifier::Inline); + } + memory.append(entry); + } + break; + case ECode::Class: + case ECode::Struct: + { + CodeBody body = entry->Body->operator CodeBody(); + CodeBody new_body = def_body( entry->Body->Type ); + for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch + (body_entry->Type) { + case ECode::Preprocess_If: + { + ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), body_entry, body ); + } + break; + + default: + new_body.append(body_entry); + break; + } + + entry->Body = rcast(AST*, new_body.ast); + memory.append(entry); + } + break; + case ECode::Preprocess_If: + { + ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, memory ); + } + break; + case ECode::Preprocess_Pragma: + { + swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory); + } + break; + default: { + memory.append(entry); + } + break; + } + } + + header.print_fmt( roll_own_dependencies_guard_start ); + header.print( platform ); + header.print_fmt( "\nGEN_NS_BEGIN\n" ); + + header.print( macros ); + header.print( basic_types ); + header.print( debug ); + header.print( memory ); } + header.print( pop_ignores ); + header.write(); + + format_file( "gen/gen.h" ); + gen::deinit(); return 0; #undef project_dir diff --git a/gen_c_library/components/fixed_arena.hpp b/gen_c_library/components/fixed_arena.hpp new file mode 100644 index 0000000..8f0ff21 --- /dev/null +++ b/gen_c_library/components/fixed_arena.hpp @@ -0,0 +1,119 @@ +// #pragma once +// #include "../project/gen.hpp" + +// using namespace gen; + +CodeBody gen_fixed_arenas() +{ + CodeBody result = def_body(ECode::Global_Body); + + char const* template_struct = stringize( + struct FixedArena_ + { + char memory[]; + Arena arena; + }; + ); + + char const* template_interface = stringize( + inline + void fixed_arena_init_(FixedArena_* result) { + zero_size(& result.memory[0], ); + result.arena = arena_init_from_memory(& result.memory[0], ); + } + + inline + ssize fixed_arena_size_remaining_(FixedArena_* fixed_arena, ssize alignment) { + return size_remaining(fixed_arena.arena, alignment); + } + ); + + CodeStruct arena_struct_1kb = parse_struct( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_struct )); + CodeStruct arena_struct_4kb = parse_struct( token_fmt_impl( 3, "Name", txt("4KB"), "Size", txt("kilobytes(4)"), template_struct )); + CodeStruct arena_struct_8kb = parse_struct( token_fmt_impl( 3, "Name", txt("8KB"), "Size", txt("kilobytes(8)"), template_struct )); + CodeStruct arena_struct_16kb = parse_struct( token_fmt_impl( 3, "Name", txt("16KB"), "Size", txt("kilobytes(16)"), template_struct )); + CodeStruct arena_struct_32kb = parse_struct( token_fmt_impl( 3, "Name", txt("32KB"), "Size", txt("kilobytes(32)"), template_struct )); + CodeStruct arena_struct_64kb = parse_struct( token_fmt_impl( 3, "Name", txt("64KB"), "Size", txt("kilobytes(64)"), template_struct )); + CodeStruct arena_struct_128kb = parse_struct( token_fmt_impl( 3, "Name", txt("128KB"), "Size", txt("kilobytes(128)"), template_struct )); + CodeStruct arena_struct_256kb = parse_struct( token_fmt_impl( 3, "Name", txt("256KB"), "Size", txt("kilobytes(256)"), template_struct )); + CodeStruct arena_struct_512kb = parse_struct( token_fmt_impl( 3, "Name", txt("512KB"), "Size", txt("kilobytes(512)"), template_struct )); + CodeStruct arena_struct_1mb = parse_struct( token_fmt_impl( 3, "Name", txt("1MB"), "Size", txt("megabytes(1)"), template_struct )); + CodeStruct arena_struct_2mb = parse_struct( token_fmt_impl( 3, "Name", txt("2MB"), "Size", txt("megabytes(2)"), template_struct )); + CodeStruct arena_struct_4mb = parse_struct( token_fmt_impl( 3, "Name", txt("4MB"), "Size", txt("megabytes(4)"), template_struct )); + + + CodeBody arena_interface_1kb = parse_global_body( token_fmt_impl( 3, "Name", txt("1KB"), "Size", txt("kilobytes(1)"), template_interface )); + CodeBody arena_interface_4kb = parse_global_body( token_fmt_impl( 3, "Name", txt("4KB"), "Size", txt("kilobytes(4)"), template_interface )); + CodeBody arena_interface_8kb = parse_global_body( token_fmt_impl( 3, "Name", txt("8KB"), "Size", txt("kilobytes(8)"), template_interface )); + CodeBody arena_interface_16kb = parse_global_body( token_fmt_impl( 3, "Name", txt("16KB"), "Size", txt("kilobytes(16)"), template_interface )); + CodeBody arena_interface_32kb = parse_global_body( token_fmt_impl( 3, "Name", txt("32KB"), "Size", txt("kilobytes(32)"), template_interface )); + CodeBody arena_interface_64kb = parse_global_body( token_fmt_impl( 3, "Name", txt("64KB"), "Size", txt("kilobytes(64)"), template_interface )); + CodeBody arena_interface_128kb = parse_global_body( token_fmt_impl( 3, "Name", txt("128KB"), "Size", txt("kilobytes(128)"), template_interface )); + CodeBody arena_interface_256kb = parse_global_body( token_fmt_impl( 3, "Name", txt("256KB"), "Size", txt("kilobytes(256)"), template_interface )); + CodeBody arena_interface_512kb = parse_global_body( token_fmt_impl( 3, "Name", txt("512KB"), "Size", txt("kilobytes(512)"), template_interface )); + CodeBody arena_interface_1mb = parse_global_body( token_fmt_impl( 3, "Name", txt("1MB"), "Size", txt("megabytes(1)"), template_interface )); + CodeBody arena_interface_2mb = parse_global_body( token_fmt_impl( 3, "Name", txt("2MB"), "Size", txt("megabytes(2)"), template_interface )); + CodeBody arena_interface_4mb = parse_global_body( token_fmt_impl( 3, "Name", txt("4MB"), "Size", txt("megabytes(4)"), template_interface )); + + result.append(arena_struct_1kb); + result.append(arena_struct_4kb); + result.append(arena_struct_8kb); + result.append(arena_struct_16kb); + result.append(arena_struct_32kb); + result.append(arena_struct_128kb); + result.append(arena_struct_256kb); + result.append(arena_struct_512kb); + result.append(arena_struct_1mb); + result.append(arena_struct_2mb); + result.append(arena_struct_4mb); + + result.append(arena_interface_1kb); + result.append(arena_interface_4kb); + result.append(arena_interface_8kb); + result.append(arena_interface_16kb); + result.append(arena_interface_32kb); + result.append(arena_interface_128kb); + result.append(arena_interface_256kb); + result.append(arena_interface_512kb); + result.append(arena_interface_1mb); + result.append(arena_interface_2mb); + result.append(arena_interface_4mb); + + CodeDefine def = def_define(txt("fixed_arena_allocator_info(fixed_arena)"), code({ arena_allocator_proc, & fixed_arena.arena }) ); + result.append(def); + + result.append(parse_global_body(txt(R"( +#define fixed_arena_init(expr) _Generic((expr), \ + FixedArena_1KB* : fixed_arena_init_1KB, \ + FixedArena_4KB* : fixed_arena_init_4KB, \ + FixedArena_8KB* : fixed_arena_init_8KB, \ + FixedArena_16KB* : fixed_arena_init_16KB, \ + FixedArena_32KB* : fixed_arena_init_32KB, \ + FixedArena_64KB* : fixed_arena_init_64KB, \ + FixedArena_128KB* : fixed_arena_init_128KB, \ + FixedArena_256KB* : fixed_arena_init_256KB, \ + FixedArena_512KB* : fixed_arena_init_512KB, \ + FixedArena_1MB* : fixed_arena_init_1MB, \ + FixedArena_2MB* : fixed_arena_init_2MB, \ + FixedArena_4MB* : fixed_arena_init_4MB \ +)(expr) + +#define fixed_arena_size_remaining(expr, alignment) _Generic((expr), \ + FixedArena_1KB* : fixed_arena_size_remaining_1KB, \ + FixedArena_4KB* : fixed_arena_size_remaining_4KB, \ + FixedArena_8KB* : fixed_arena_size_remaining_8KB, \ + FixedArena_16KB* : fixed_arena_size_remaining_16KB, \ + FixedArena_32KB* : fixed_arena_size_remaining_32KB, \ + FixedArena_64KB* : fixed_arena_size_remaining_64KB, \ + FixedArena_128KB* : fixed_arena_size_remaining_128KB, \ + FixedArena_256KB* : fixed_arena_size_remaining_256KB, \ + FixedArena_512KB* : fixed_arena_size_remaining_512KB, \ + FixedArena_1MB* : fixed_arena_size_remaining_1MB, \ + FixedArena_2MB* : fixed_arena_size_remaining_2MB, \ + FixedArena_4MB* : fixed_arena_size_remaining_4MB \ +)(expr, alignment) +)" + ))); + + return result; +} diff --git a/gen_c_library/components/header_start.hpp b/gen_c_library/components/header_start.hpp new file mode 100644 index 0000000..cbfa228 --- /dev/null +++ b/gen_c_library/components/header_start.hpp @@ -0,0 +1,14 @@ +/* + gencpp: An attempt at "simple" staged metaprogramming for c/c++. + + See Readme.md for more information from the project repository. + + Public Address: + https://github.com/Ed94/gencpp + + This is a single header C-Library variant. + Define GEN_IMPLEMENTATION before including this file in a single compilation unit. +*/ +#if ! defined(GEN_DONT_ENFORCE_GEN_TIME_GUARD) && ! defined(GEN_TIME) +# error Gen.hpp : GEN_TIME not defined +#endif diff --git a/gen_c_library/components/misc.hpp b/gen_c_library/components/misc.hpp new file mode 100644 index 0000000..1fc20aa --- /dev/null +++ b/gen_c_library/components/misc.hpp @@ -0,0 +1,61 @@ +// #pragma once +// #include "../project/gen.hpp" + +// using namespace gen; + +using SwapContentProc = CodeBody(void); + +b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body ) +{ + CodePreprocessCond cond = entry_iter.cast(); + if ( cond->Content.contains(cond_sig) ) + { + s32 depth = 1; + ++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ++ entry_iter) switch + (entry_iter->Type) { + case ECode::Preprocess_If: + case ECode::Preprocess_IfDef: + case ECode::Preprocess_IfNotDef: + depth ++; + break; + + case ECode::Preprocess_EndIf: + { + depth --; + if (depth == 0) { + continue_for = false; + } + } + break; + } + } + + return entry_iter != body.end(); +} + +void swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body ) +{ + CodePragma possible_region = entry_iter.cast(); + + String region_sig = string_fmt_buf(GlobalAllocator, "region %s", region_name.Ptr); + String endregion_sig = string_fmt_buf(GlobalAllocator, "endregion %s", region_name.Ptr); + if ( possible_region->Content.contains(region_sig)) + { + body.append(possible_region); + body.append(swap_content()); + + ++ entry_iter; for(b32 continue_for = true; continue_for; ++entry_iter) switch + (entry_iter->Type) { + case ECode::Preprocess_Pragma: + { + CodePragma possible_end_region = entry_iter.cast(); + if ( possible_end_region->Content.contains(endregion_sig) ) { + body.append(possible_end_region); + continue_for = false; + } + } + break; + } + } + body.append(entry_iter); +} diff --git a/gen_singleheader/components/header_start.hpp b/gen_singleheader/components/header_start.hpp index fdee90c..8d69720 100644 --- a/gen_singleheader/components/header_start.hpp +++ b/gen_singleheader/components/header_start.hpp @@ -12,12 +12,3 @@ #if ! defined(GEN_DONT_ENFORCE_GEN_TIME_GUARD) && ! defined(GEN_TIME) # error Gen.hpp : GEN_TIME not defined #endif - -#ifdef GEN_DONT_USE_NAMESPACE -# define GEN_NS_BEGIN -# define GEN_NS_END -#else -# define GEN_NS_BEGIN namespace gen { -# define GEN_NS_END } -#endif - diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index 4b67d57..bc0f5f9 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -165,6 +165,46 @@ struct CodeSpecifiers return -1; } + s32 remove( SpecifierT to_remove ) + { + if ( ast == nullptr ) + { + log_failure("CodeSpecifiers: Attempted to append to a null specifiers AST!"); + return -1; + } + + if ( raw()->NumEntries == AST::ArrSpecs_Cap ) + { + log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST::ArrSpecs_Cap ); + return -1; + } + + s32 result = -1; + + s32 curr = 0; + s32 next = 0; + for(; next < raw()->NumEntries; ++ curr, ++ next) + { + SpecifierT spec = raw()->ArrSpecs[next]; + if (spec == to_remove) + { + result = next; + + next ++; + if (next >= raw()->NumEntries) + break; + + spec = raw()->ArrSpecs[next]; + } + + raw()->ArrSpecs[ curr ] = spec; + } + + if (result > -1) { + raw()->NumEntries --; + } + return result; + } void to_string( String& result ); AST* raw() { diff --git a/project/components/header_end.hpp b/project/components/header_end.hpp index 39cc1f2..27d03d9 100644 --- a/project/components/header_end.hpp +++ b/project/components/header_end.hpp @@ -133,6 +133,7 @@ extern CodeType t_typename; #pragma region Macros +#ifndef token_fmt # define gen_main main # define __ NoCode @@ -151,6 +152,7 @@ extern CodeType t_typename; // Takes a format string (char const*) and a list of tokens (StrC) and returns a StrC of the formatted string. # define token_fmt( ... ) GEN_NS token_fmt_impl( (num_args( __VA_ARGS__ ) + 1) / 2, __VA_ARGS__ ) +#endif #pragma endregion Macros diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 2c19973..3c00ee7 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -714,8 +714,8 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) char interface_arr_mem[ kilobytes(4) ] {0}; Array interfaces; { Arena arena = arena_init_from_memory( interface_arr_mem, kilobytes(4) ); - array_init_reserve( allocator_info(arena), 4 ); - } + interfaces = array_init_reserve( allocator_info(arena), 4 ); + } // TODO(Ed) : Make an AST_DerivedType, we'll store any arbitary derived type into there as a linear linked list of them. if ( check( TokType::Assign_Classifer ) ) diff --git a/project/dependencies/basic_types.hpp b/project/dependencies/basic_types.hpp index 5b4b0e3..37da1de 100644 --- a/project/dependencies/basic_types.hpp +++ b/project/dependencies/basic_types.hpp @@ -123,8 +123,8 @@ typedef s8 b8; typedef s16 b16; typedef s32 b32; -using mem_ptr = void*; -using mem_ptr_const = void const*; +typedef void* mem_ptr; +typedef void const* mem_ptr_const ; #if ! GEN_COMPILER_C template uptr to_uptr( Type* ptr ) { return (uptr)ptr; } diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index 9c12af2..e6ec2d8 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -52,9 +52,9 @@ template bool resize(Array& array, usize num); template bool set_capacity(Array& array, usize new_capacity); template ArrayHeader* get_header(Array& array); -template forceinline Type* begin(Array& array) { return array; } -template forceinline Type* end(Array& array) { return array + get_header(array)->Num; } -template forceinline Type* next(Type* entry) { return entry + 1; } +template forceinline Type* begin(Array& array) { return array; } +template forceinline Type* end(Array& array) { return array + get_header(array)->Num; } +template forceinline Type* next(Array& array, Type* entry) { return entry + 1; } struct ArrayHeader { AllocatorInfo Allocator; @@ -250,7 +250,7 @@ bool fill(Array& array, usize begin, usize end, Type value) template inline void free(Array& array) { ArrayHeader* header = get_header(array); - gen::free(header->Allocator, header); + GEN_NS free(header->Allocator, header); Type*& Data = rcast(Type*&, array); Data = nullptr; } diff --git a/project/dependencies/macros.hpp b/project/dependencies/macros.hpp index 790f1b4..cf0346d 100644 --- a/project/dependencies/macros.hpp +++ b/project/dependencies/macros.hpp @@ -200,7 +200,7 @@ // This is intended to only really be used internally or with the C-library variant // C++ users can just use the for-range directly. #if GEN_COMPILER_C -# define foreach(Type, entry_id, iterable) for ( Type entry_id = begin(iterable); entry_id != end(iterable); entry_id = next(entry_id) ) +# define foreach(Type, entry_id, iterable) for ( Type entry_id = begin(iterable); entry_id != end(iterable); entry_id = next(iterable, entry_id) ) #else # define foreach(Type, entry_id, iterable) for ( Type entry_id : iterable ) #endif diff --git a/project/dependencies/memory.hpp b/project/dependencies/memory.hpp index d8c4253..545e337 100644 --- a/project/dependencies/memory.hpp +++ b/project/dependencies/memory.hpp @@ -80,10 +80,7 @@ enum AllocType : u8 EAllocation_RESIZE, }; -using AllocatorProc = void* ( void* allocator_data, AllocType type - , ssize size, ssize alignment - , void* old_memory, ssize old_size - , u64 flags ); +typedef void*(AllocatorProc)( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ); struct AllocatorInfo { diff --git a/scripts/build.ci.ps1 b/scripts/build.ci.ps1 index 73cd1b3..67b2697 100644 --- a/scripts/build.ci.ps1 +++ b/scripts/build.ci.ps1 @@ -90,7 +90,7 @@ else { $optimize = $true } -if ( $bootstrap -eq $false -and $singleheader -eq $false -and $unreal -eq $false -and $test -eq $false ) { +if ( $bootstrap -eq $false -and $singleheader -eq $false -and $c_library -eq $false -and $unreal -eq $false -and $test -eq $false ) { throw "No build target specified. One must be specified, this script will not assume one" } @@ -215,7 +215,7 @@ if ( $c_library ) build-simple $path_build $includes $compiler_args $linker_args $unit $executable - Push-Location $path_singleheader + Push-Location $path_c_library if ( Test-Path( $executable ) ) { write-host "`nRunning c_library generator" $time_taken = Measure-Command { & $executable From 31a3609b28139ef42b353d907b82645679a10329 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sat, 30 Nov 2024 23:48:14 -0500 Subject: [PATCH 014/112] some fixes to c's fixed_arena gen --- gen_c_library/c_library.cpp | 2 +- .../{fixed_arena.hpp => memory.fixed_arena.hpp} | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) rename gen_c_library/components/{fixed_arena.hpp => memory.fixed_arena.hpp} (96%) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index e29a620..a0b3044 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -16,7 +16,7 @@ GEN_NS_END #include // for system() -#include "components/fixed_arena.hpp" +#include "components/memory.fixed_arena.hpp" #include "components/misc.hpp" using namespace gen; diff --git a/gen_c_library/components/fixed_arena.hpp b/gen_c_library/components/memory.fixed_arena.hpp similarity index 96% rename from gen_c_library/components/fixed_arena.hpp rename to gen_c_library/components/memory.fixed_arena.hpp index 8f0ff21..b5958cc 100644 --- a/gen_c_library/components/fixed_arena.hpp +++ b/gen_c_library/components/memory.fixed_arena.hpp @@ -1,7 +1,7 @@ -// #pragma once -// #include "../project/gen.hpp" +#pragma once +#include "../project/gen.hpp" -// using namespace gen; +using namespace gen; CodeBody gen_fixed_arenas() { @@ -18,13 +18,13 @@ CodeBody gen_fixed_arenas() char const* template_interface = stringize( inline void fixed_arena_init_(FixedArena_* result) { - zero_size(& result.memory[0], ); - result.arena = arena_init_from_memory(& result.memory[0], ); + zero_size(& result->memory[0], ); + result.arena = arena_init_from_memory(& result->memory[0], ); } inline ssize fixed_arena_size_remaining_(FixedArena_* fixed_arena, ssize alignment) { - return size_remaining(fixed_arena.arena, alignment); + return size_remaining(fixed_arena->arena, alignment); } ); From 0b4ccac8f9321e47dc7bfc645bde9684a3da97dc Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 01:39:21 -0500 Subject: [PATCH 015/112] Removed usage of hashtable member procs --- project/components/interface.cpp | 8 ++--- project/components/interface.untyped.cpp | 10 +++--- project/components/lexer.cpp | 8 ++--- project/components/parser.cpp | 2 +- project/dependencies/containers.hpp | 46 ++++++++++++++++++++++-- project/dependencies/parsing.cpp | 8 ++--- project/dependencies/platform.hpp | 2 +- project/dependencies/string_ops.hpp | 1 - 8 files changed, 63 insertions(+), 22 deletions(-) diff --git a/project/components/interface.cpp b/project/components/interface.cpp index 3d22496..5aadb92 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -282,7 +282,7 @@ void init() // Setup the hash tables { - StringCache = StringTable::init( Allocator_StringTable ); + StringCache = hashtable_init(Allocator_StringTable); if ( StringCache.Entries == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the StringCache"); @@ -317,7 +317,7 @@ void deinit() } while ( left--, left ); - StringCache.destroy(); + destroy(StringCache); free(CodePools); free(StringArenas); @@ -392,14 +392,14 @@ StringCached get_cached_string( StrC str ) s32 hash_length = str.Len > kilobytes(1) ? kilobytes(1) : str.Len; u64 key = crc32( str.Ptr, hash_length ); { - StringCached* result = StringCache.get( key ); + StringCached* result = get(StringCache, key ); if ( result ) return * result; } String result = String::make( get_string_allocator( str.Len ), str ); - StringCache.set( key, result ); + set(StringCache, key, result ); return result; } diff --git a/project/components/interface.untyped.cpp b/project/components/interface.untyped.cpp index bdb0742..217a5d2 100644 --- a/project/components/interface.untyped.cpp +++ b/project/components/interface.untyped.cpp @@ -6,7 +6,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) { char const* buf_begin = buf; - ssize remaining = buf_size; + ssize remaining = buf_size; local_persist Arena tok_map_arena; @@ -17,7 +17,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) char tok_map_mem[ TokenFmt_TokenMap_MemSize ]; tok_map_arena = arena_init_from_memory( tok_map_mem, sizeof(tok_map_mem) ); - tok_map = HashTable::init( allocator_info(tok_map_arena) ); + tok_map = hashtable_init( allocator_info(tok_map_arena) ); s32 left = num_tokens - 1; @@ -28,7 +28,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) u32 key = crc32( token, str_len(token) ); - tok_map.set( key, value ); + set(tok_map, key, value ); } } @@ -64,7 +64,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) char const* token = fmt + 1; u32 key = crc32( token, tok_len ); - StrC* value = tok_map.get( key ); + StrC* value = get(tok_map, key ); if ( value ) { @@ -94,7 +94,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) } } - tok_map.clear(); + clear(tok_map); free(tok_map_arena); ssize result = buf_size - remaining; diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index 12788a8..fe8cc94 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -341,7 +341,7 @@ s32 lex_preprocessor_directive( append(Tokens, name ); u64 key = crc32( name.Text, name.Length ); - defines.set( key, name ); + set(defines, key, name ); } Token preprocess_content = { scanner, 0, TokType::Preprocess_Content, line, column, TF_Preprocess }; @@ -516,7 +516,7 @@ void lex_found_token( StrC& content else key = crc32( token.Text, token.Length ); - StrC* define = defines.get( key ); + StrC* define = get(defines, key ); if ( define ) { token.Type = TokType::Preprocess_Macro; @@ -597,7 +597,7 @@ TokArray lex( StrC content ) } u64 key = crc32( entry.Data, length ); - defines.set( key, entry ); + set(defines, key, entry ); } clear(Tokens); @@ -1259,7 +1259,7 @@ TokArray lex( StrC content ) return { { nullptr }, 0 }; } - defines.clear(); + clear(defines); // defines_map_arena.free(); return { Tokens, 0 }; } diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 3c00ee7..6ed1d85 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -137,7 +137,7 @@ void init() ); fixed_arena_init(defines_map_arena); - defines = HashTable::init_reserve( allocator_info(defines_map_arena), 256 ); + defines = hashtable_init_reserve( allocator_info(defines_map_arena), 256 ); } internal diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index e6ec2d8..948c0d1 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -352,6 +352,28 @@ bool set_capacity(Array& array, usize new_capacity) Data = rcast(Type*, new_header + 1); return true; } + +#define array_init(Type, allocator) array_init(allocator) +#define array_init(Type, allocator) array_init(allocator) +#define array_init_reserve(Type, allocator, capacity) array_init_reserve(allocator, capacity) +#define array_append(Type, array, other) append(array, other) +#define array_append_value(Type, array, value) append(array, value) +#define array_append_items(Type, array, items, item_num) append(array, items, item_num) +#define array_append_at(Type, array, item, idx) append_at(array, item, idx) +#define array_append_items_at(Type, array, items, num, idx) append_at(array, items, num, idx) +#define array_back(Type, array) back(array) +#define array_clear(Type, array) clear(array) +#define array_fill(Type, array, begin, end, value) fill(array, begin, end, value) +#define array_free(Type, array) free(array) +#define array_grow(Type, array, min_capacity) grow(array, min_capacity) +#define array_num(Type, array) num(array) +#define array_pop(Type, array) pop(array) +#define array_remove_at(Type, array, idx) remove_at(array, idx) +#define array_reserve(Type, array, new_capacity) reserve(array, new_capacity) +#define array_resize(Type, array, num) resize(array, num) +#define array_set_capacity(Type, array, new_capacity) set_capacity(array, new_capacity) +#define array_get_header(array) get_header(array) + #pragma endregion Array // TODO(Ed) : This thing needs ALOT of work. @@ -372,7 +394,8 @@ struct HashTableEntry { Type Value; }; -// Forward declarations for all lifted functions +#define HashTableEntry(Type) HashTableEntry + template HashTable hashtable_init(AllocatorInfo allocator); template HashTable hashtable_init_reserve(AllocatorInfo allocator, usize num); template void clear(HashTable& table); @@ -399,7 +422,7 @@ struct HashTable Array Hashes; Array> Entries; -#if 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES #pragma region Member Mapping forceinline static HashTable init(AllocatorInfo allocator) { return GEN_NS hashtable_init(allocator); } forceinline static HashTable init_reserve(AllocatorInfo allocator, usize num) { return GEN_NS hashtable_init_reserve(allocator, num); } @@ -634,6 +657,25 @@ bool full(HashTable& table) { b32 result = num(table.Entries) > critical_load; return result; } + +#define hashtable_init(Type, allocator) hashtable_init(allocator) +#define hashtable_init_reserve(Type, allocator, num) hashtable_init_reserve(allocator, num) +#define hashtable_clear(Type, table) clear(table) +#define hashtable_destroy(Type, table) destroy(table) +#define hashtable_get(Type, table, key) get(table, key) +#define hashtable_grow(Type, table) grow(table) +#define hashtable_rehash(Type, table, new_num) rehash(table, new_num) +#define hashtable_rehash_fast(Type, table) rehash_fast(table) +#define hashtable_remove(Type, table, key) remove(table, key) +#define hashtable_remove_entry(Type, table, idx) remove_entry(table, idx) +#define hashtable_set(Type, table, key, value) set(table, key, value) +#define hashtable_slot(Type, table, key) slot(table, key) +#define hashtable_add_entry(Type, table, key) add_entry(table, key) +#define hashtable_find(Type, table, key) find(table, key) +#define hashtable_full(Type, table) full(table) +#define hashtable_map(Type, table, map_proc) map(table, map_proc) +#define hashtable_map_mut(Type, table, map_proc) map_mut(table, map_proc) + #pragma endregion HashTable #pragma endregion Containers diff --git a/project/dependencies/parsing.cpp b/project/dependencies/parsing.cpp index c314218..eee923e 100644 --- a/project/dependencies/parsing.cpp +++ b/project/dependencies/parsing.cpp @@ -447,7 +447,7 @@ char* adt_parse_number_strict( ADT_Node* node, char* base_str ) while ( *e ) ++e; - while ( *p && ( str_find( "eE.+-", *p ) || char_is_hex_digit( *p ) ) ) + while ( *p && ( char_first_occurence( "eE.+-", *p ) || char_is_hex_digit( *p ) ) ) { ++p; } @@ -476,7 +476,7 @@ char* adt_parse_number( ADT_Node* node, char* base_str ) u8 node_props = 0; /* skip false positives and special cases */ - if ( ! ! str_find( "eE", *p ) || ( ! ! str_find( ".+-", *p ) && ! char_is_hex_digit( *( p + 1 ) ) && *( p + 1 ) != '.' ) ) + if ( ! ! char_first_occurence( "eE", *p ) || ( ! ! char_first_occurence( ".+-", *p ) && ! char_is_hex_digit( *( p + 1 ) ) && *( p + 1 ) != '.' ) ) { return ++base_str; } @@ -552,7 +552,7 @@ char* adt_parse_number( ADT_Node* node, char* base_str ) char expbuf[ 6 ] = { 0 }; ssize expi = 0; - if ( *e && ! ! str_find( "eE", *e ) ) + if ( *e && ! ! char_first_occurence( "eE", *e ) ) { ++e; if ( *e == '+' || *e == '-' || char_is_digit( *e ) ) @@ -748,7 +748,7 @@ ADT_Error adt_print_string( FileInfo* file, ADT_Node* node, char const* escaped_ { p = str_skip_any( p, escaped_chars ); _adt_fprintf( file, "%.*s", pointer_diff( b, p ), b ); - if ( *p && ! ! str_find( escaped_chars, *p ) ) + if ( *p && ! ! char_first_occurence( escaped_chars, *p ) ) { _adt_fprintf( file, "%s%c", escape_symbol, *p ); p++; diff --git a/project/dependencies/platform.hpp b/project/dependencies/platform.hpp index ca837af..e1e358f 100644 --- a/project/dependencies/platform.hpp +++ b/project/dependencies/platform.hpp @@ -1,4 +1,4 @@ -#define GEN_SUPPORT_CPP_MEMBER_FEATURES 1 +#define GEN_SUPPORT_CPP_MEMBER_FEATURES 0 #ifdef GEN_INTELLISENSE_DIRECTIVES # pragma once diff --git a/project/dependencies/string_ops.hpp b/project/dependencies/string_ops.hpp index fe922ac..dc76b51 100644 --- a/project/dependencies/string_ops.hpp +++ b/project/dependencies/string_ops.hpp @@ -6,7 +6,6 @@ #pragma region String Ops const char* char_first_occurence( const char* str, char c ); -constexpr auto str_find = &char_first_occurence; b32 char_is_alpha( char c ); b32 char_is_alphanumeric( char c ); From a96d03eaed733e918dcbab00ec572738a8acad85 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 01:40:14 -0500 Subject: [PATCH 016/112] brought over the generators of array and hashtable for c-lib gen From the old genc repo. Still need to fully check that its code is up to date --- gen_c_library/components/containers.array.hpp | 295 +++++++++++++++ .../components/containers.hashtable.hpp | 354 ++++++++++++++++++ 2 files changed, 649 insertions(+) create mode 100644 gen_c_library/components/containers.array.hpp create mode 100644 gen_c_library/components/containers.hashtable.hpp diff --git a/gen_c_library/components/containers.array.hpp b/gen_c_library/components/containers.array.hpp new file mode 100644 index 0000000..45dc684 --- /dev/null +++ b/gen_c_library/components/containers.array.hpp @@ -0,0 +1,295 @@ +#pragma once + +#include "../project/gen.hpp" + +using namespace gen; + +CodeBody gen_array_base() +{ + CodeTypedef td_header = parse_typedef( code( typedef struct ArrayHeader ArrayHeader; )); + CodeStruct header = parse_struct( code( + struct ArrayHeader + { + AllocatorInfo Allocator; + usize Capacity; + usize Num; + }; + )); + + // Code grow_formula = untyped_str( txt( "#define gen_array_grow_formula( value ) ( 2 * value + 8 )\n" )); + Code get_header = untyped_str( txt( "#define array_get_header( Type, self ) ( (ArrayHeader*)( self ) - 1)\n" )); + + return def_global_body( args( td_header, header, get_header ) ); +}; + +CodeBody gen_array( StrC type, StrC array_name ) +{ + String array_type = String::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr ); + String fn = String::fmt_buf( GlobalAllocator, "%.*s", array_name.Len, array_name.Ptr ); + str_to_lower(fn.Data); + +#pragma push_macro( "GEN_ASSERT" ) +#undef GEN_ASSERT + CodeBody result = parse_global_body( token_fmt( "array_type", (StrC)array_type, "fn", (StrC)fn, "type", (StrC)type + , stringize( + typedef * ; + + _init ( AllocatorInfo allocator ); + _init_reserve ( AllocatorInfo allocator, usize capacity ); + bool _append ( * self, value ); + bool _append_items ( * self, * items, usize item_num ); + bool _append_at ( * self, item, usize idx ); + bool _append_items_at( * self, * items, usize item_num, usize idx ); + * _back ( self ); + void _clear ( self ); + bool _fill ( self, usize begin, usize end, value ); + void _free ( self ); + bool _grow ( * self, usize min_capacity ); + usize _num ( self ); + _pop ( self ); + bool _reserve ( * self, usize new_capacity ); + bool _resize ( * self, usize num ); + bool _set_capacity ( * self, usize new_capacity ); + + _init( AllocatorInfo allocator ) + { + return _init_reserve( allocator, array_grow_formula( 0 ) ); + } + + _init_reserve( AllocatorInfo allocator, usize capacity ) + { + ArrayHeader* header = cast(ArrayHeader*, alloc( allocator, sizeof(ArrayHeader) + sizeof() * capacity ) ); + + if ( header == NULL ) + return NULL; + + header->Allocator = allocator; + header->Capacity = capacity; + header->Num = 0; + + return cast( *, header + 1 ); + } + + bool _append( * self, value ) + { + ArrayHeader* header = get_header( * self ); + + if ( header->Num == header->Capacity ) + { + if ( ! _grow( self, header->Capacity)) + return false; + + header = get_header( * self ); + } + + (* self)[ header->Num ] = value; + header->Num++; + + return true; + } + + bool _append_items( * self, * items, usize item_num ) + { + ArrayHeader* header = get_header( * self ); + + if ( header->Num + item_num > header->Capacity ) + { + if ( ! _grow( self, header->Capacity + item_num )) + return false; + + header = get_header( * self ); + } + + mem_copy( (* self) + header->Num, items, sizeof() * item_num ); + header->Num += item_num; + + return true; + } + + bool _append_at( * self, item, usize idx ) + { + ArrayHeader* header = get_header( * self ); + + if ( idx >= header->Num ) + idx = header->Num - 1; + + if ( idx < 0 ) + idx = 0; + + if ( header->Capacity < header->Num + 1 ) + { + if ( ! _grow( self, header->Capacity + 1 ) ) + return false; + + header = get_header( * self ); + } + + target = (* self) + idx; + + mem_move( target + 1, target, (header->Num - idx) * sizeof() ); + header->Num++; + + return true; + } + + bool _append_items_at( * self, * items, usize item_num, usize idx ) + { + ArrayHeader* header = get_header( * self ); + + if ( idx >= header->Num ) + { + return _append_items( self, items, item_num ); + } + + if ( item_num > header->Capacity ) + { + if ( ! _grow( self, item_num + header->Capacity ) ) + return false; + + header = get_header( * self ); + } + + * target = (* self) + idx + item_num; + * src = (* self) + idx; + + mem_move( target, src, (header->Num - idx) * sizeof() ); + mem_copy( src, items, item_num * sizeof() ); + header->Num += item_num; + + return true; + } + + * _back( self ) + { + ArrayHeader* header = get_header( self ); + + if ( header->Num == 0 ) + return NULL; + + return self + header->Num - 1; + } + + void _clear( self ) + { + ArrayHeader* header = get_header( self ); + header->Num = 0; + } + + bool _fill( self, usize begin, usize end, value ) + { + ArrayHeader* header = get_header( self ); + + if ( begin < 0 || end >= header->Num ) + return false; + + for ( ssize idx = begin; idx < end; idx ++ ) + self[ idx ] = value; + + return true; + } + + void _free( self ) + { + ArrayHeader* header = get_header( self ); + free( header->Allocator, header ); + self = NULL; + } + + bool _grow( * self, usize min_capacity ) + { + ArrayHeader* header = get_header( *self ); + usize new_capacity = array_grow_formula( header->Capacity ); + + if ( new_capacity < min_capacity ) + new_capacity = min_capacity; + + return _set_capacity( self, new_capacity ); + } + + usize _num( self ) + { + return get_header(self)->Num; + } + + _pop( self ) + { + ArrayHeader* header = get_header( self ); + GEN_ASSERT( header->Num > 0 ); + + result = self[ header->Num - 1 ]; + header->Num--; + return result; + } + + void _remove_at( self, usize idx ) + { + ArrayHeader* header = get_header( self ); + GEN_ASSERT( idx < header->Num ); + + mem_move( self + idx, self + idx + 1, sizeof( ) * ( header->Num - idx - 1 ) ); + header->Num--; + } + + bool _reserve( * self, usize new_capacity ) + { + ArrayHeader* header = get_header( * self ); + + if ( header->Capacity < new_capacity ) + return _set_capacity( self, new_capacity ); + + return true; + } + + bool _resize( * self, usize num ) + { + ArrayHeader* header = get_header( * self ); + + if ( header->Capacity < num ) + { + if ( ! _grow( self, num ) ) + return false; + + header = get_header( * self ); + } + + header->Num = num; + return true; + } + + bool _set_capacity( * self, usize new_capacity ) + { + ArrayHeader* header = get_header( * self ); + + if ( new_capacity == header->Capacity ) + return true; + + if ( new_capacity < header->Num ) + header->Num = new_capacity; + + usize size = sizeof( ArrayHeader ) + sizeof( ) * new_capacity; + ArrayHeader* new_header = cast( ArrayHeader*, alloc( header->Allocator, size )); + + if ( new_header == NULL ) + return false; + + mem_move( new_header, header, sizeof( ArrayHeader ) + sizeof( ) * header->Num ); + free( header->Allocator, & header ); + + new_header->Capacity = new_capacity; + * self = cast( *, new_header + 1 ); + return true; + } + ))); +#pragma pop_macro( "GEN_ASSERT" ) + + return def_global_body( args( + def_pragma( to_str( str_fmt_buf( "region %S", array_type ))), + fmt_newline, + result, + fmt_newline, + def_pragma( to_str( str_fmt_buf( "endregion %S", array_type ))), + fmt_newline + )); +}; + +// CodeBody gen_ diff --git a/gen_c_library/components/containers.hashtable.hpp b/gen_c_library/components/containers.hashtable.hpp new file mode 100644 index 0000000..6c460a3 --- /dev/null +++ b/gen_c_library/components/containers.hashtable.hpp @@ -0,0 +1,354 @@ +#pragma once + +#include "../project/gen.hpp" +#include "containers.array.hpp" + +using namespace gen; + +CodeBody gen_hashtable_base() +{ + return parse_global_body( code( + typedef struct HT_FindResult HT_FindResult; + struct HT_FindResult + { + ssize HashIndex; + ssize PrevIndex; + ssize EntryIndex; + }; + )); +} + +CodeBody gen_hashtable( StrC type, StrC hashtable_name ) +{ + String + fn = String::make_reserve( GlobalAllocator, hashtable_name.Len + sizeof("gen") ); + fn.append_fmt( "%.*s", hashtable_name.Len, hashtable_name.Ptr ); + str_to_lower(fn.Data); + + String + tbl_type = String::make_reserve( GlobalAllocator, hashtable_name.Len + sizeof("gen") ); + tbl_type.append_fmt( "%.*s", hashtable_name.Len, hashtable_name.Ptr ); + + String name_lower = String::make( GlobalAllocator, hashtable_name ); + str_to_lower( name_lower.Data ); + + String hashtable_entry = String::fmt_buf( GlobalAllocator, "HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr ); + String entry_array_name = String::fmt_buf( GlobalAllocator, "Arr_HTE_%.*s", hashtable_name.Len, hashtable_name.Ptr ); + String entry_array_fn_ns = String::fmt_buf( GlobalAllocator, "arr_hte_%.*s", name_lower.length(), name_lower.Data ); + + CodeBody hashtable_types = parse_global_body( token_fmt( + "type", (StrC) type, + "tbl_name", (StrC) hashtable_name, + "tbl_type", (StrC) tbl_type, + stringize( + typedef struct HTE_ HTE_; + struct HTE_ + { + u64 Key; + ssize Next; + Value; + }; + + typedef void (* _MapProc) ( self, u64 key, value ); + typedef void (* _MapMutProc) ( self, u64 key, * value ); + ))); + + CodeBody entry_array = gen_array( hashtable_entry, entry_array_name ); + +#pragma push_macro( "GEN_ASSERT" ) +#pragma push_macro( "GEN_ASSERT_NOT_NULL" ) +#undef GEN_ASSERT +#undef GEN_ASSERT_NOT_NULL + CodeBody hashtable_def = parse_global_body( token_fmt( + "type", (StrC) type, + "tbl_name", (StrC) hashtable_name, + "tbl_type", (StrC) tbl_type, + "fn", (StrC) fn, + "entry_type", (StrC) hashtable_entry, + "array_entry", (StrC) entry_array_name, + "fn_array", (StrC) entry_array_fn_ns, + stringize( + typedef struct ; + struct + { + Array_ssize Hashes; + Entries; + }; + + _make ( AllocatorInfo allocator ); + _make_reserve( AllocatorInfo allocator, ssize num ); + void _clear ( self ); + void _destroy ( self ); + * _get ( self, u64 key ); + void _map ( self, _MapProc map_proc ); + void _map_mut ( self, _MapMutProc map_proc ); + void _grow ( * self ); + void _rehash ( * self, ssize new_num ); + void _rehash_fast ( self ); + void _remove ( self, u64 key ); + void _remove_entry( self, ssize idx ); + void _set ( * self, u64 key, value ); + ssize _slot ( self, u64 key ); + + ssize __add_entry( self, u64 key ); + HT_FindResult __find ( self, u64 key ); + b32 __full ( self ); + + _make( AllocatorInfo allocator ) + { + + result = { NULL, NULL }; + result.Hashes = array_ssize_make( allocator ); + result.Entries = _make( allocator ); + + return result; + } + + _make_reserve( AllocatorInfo allocator, ssize num ) + { + + result = { NULL, NULL }; + result.Hashes = array_ssize_make_reserve( allocator, num ); + result.Entries = _make_reserve( allocator, num ); + + return result; + } + + void _clear( self ) + { + for ( ssize idx = 0; idx < array_header( self.Hashes )->Num; idx++ ) + self.Hashes[idx] = -1; + + array_ssize_clear( self.Hashes ); + _clear( self.Entries ); + } + + void _destroy( self ) + { + if ( self.Hashes && self.Entries ) + { + array_ssize_free( self.Hashes ); + _free( self.Entries ); + } + } + + * _get( self, u64 key ) + { + ssize idx = __find( self, key ).EntryIndex; + if ( idx > 0 ) + return & self.Entries[idx].Value; + + return NULL; + } + + void _map( self, _MapProc map_proc ) + { + GEN_ASSERT_NOT_NULL( map_proc ); + + for ( ssize idx = 0; idx < array_header( self.Entries )->Num; idx++ ) + { + map_proc( self, self.Entries[idx].Key, self.Entries[idx].Value ); + } + } + + void _map_mut( self, _MapMutProc map_proc ) + { + GEN_ASSERT_NOT_NULL( map_proc ); + + for ( ssize idx = 0; idx < array_header( self.Entries )->Num; idx++ ) + { + map_proc( self, self.Entries[idx].Key, & self.Entries[idx].Value ); + } + } + + void _grow( * self ) + { + ssize new_num = array_grow_formula( array_header( self->Entries )->Num ); + _rehash( self, new_num ); + } + + void _rehash( * self, ssize new_num ) + { + ssize idx; + ssize last_added_index; + + ArrayHeader* old_hash_header = array_header( self->Hashes ); + ArrayHeader* old_entries_header = array_header( self->Entries ); + + new_tbl = _make_reserve( old_hash_header->Allocator, old_hash_header->Num ); + + ArrayHeader* new_hash_header = array_header( new_tbl.Hashes ); + + for ( idx = 0; idx < new_hash_header->Num; idx++ ) + new_tbl.Hashes[idx] = -1; + + for ( idx = 0; idx < old_entries_header->Num; idx++ ) + { + * entry; + HT_FindResult find_result; + + if ( new_hash_header->Num == 0 ) + _grow( & new_tbl ); + + entry = & self->Entries[ idx ]; + find_result = __find( new_tbl, entry->Key ); + last_added_index = __add_entry( new_tbl, entry->Key ); + + if ( find_result.PrevIndex < 0 ) + new_tbl.Hashes[ find_result.HashIndex ] = last_added_index; + else + new_tbl.Entries[ find_result.PrevIndex ].Next = last_added_index; + + new_tbl.Entries[ last_added_index ].Next = find_result.EntryIndex; + new_tbl.Entries[ last_added_index ].Value = entry->Value; + } + + _destroy( *self ); + * self = new_tbl; + } + + void _rehash_fast( self ) + { + ssize idx; + + for ( idx = 0; idx < array_header( self.Entries )->Num; idx++ ) + self.Entries[ idx ].Next = -1; + + for ( idx = 0; idx < array_header( self.Hashes )->Num; idx++ ) + self.Hashes[ idx ] = -1; + + for ( idx = 0; idx < array_header( self.Entries )->Num; idx++ ) + { + * entry; + HT_FindResult find_result; + + entry = & self.Entries[ idx ]; + find_result = __find( self, entry->Key ); + + if ( find_result.PrevIndex < 0 ) + self.Hashes[ find_result.HashIndex ] = idx; + else + self.Entries[ find_result.PrevIndex ].Next = idx; + } + } + + void _remove( self, u64 key ) + { + HT_FindResult find_result = __find( self, key ); + + if ( find_result.EntryIndex >= 0 ) + { + _remove_at( self.Entries, find_result.EntryIndex ); + _rehash_fast( self ); + } + } + + void _remove_entry( self, ssize idx ) + { + _remove_at( self.Entries, idx ); + } + + void _set( * self, u64 key, value ) + { + ssize idx; + HT_FindResult find_result; + + if ( array_header( self->Hashes )->Num == 0 ) + _grow( self ); + + find_result = __find( * self, key ); + + if ( find_result.EntryIndex >= 0 ) + { + idx = find_result.EntryIndex; + } + else + { + idx = __add_entry( * self, key ); + + if ( find_result.PrevIndex >= 0 ) + { + self->Entries[ find_result.PrevIndex ].Next = idx; + } + else + { + self->Hashes[ find_result.HashIndex ] = idx; + } + } + + self->Entries[ idx ].Value = value; + + if ( __full( * self ) ) + _grow( self ); + } + + ssize _slot( self, u64 key ) + { + for ( ssize idx = 0; idx < array_header( self.Hashes )->Num; ++idx ) + if ( self.Hashes[ idx ] == key ) + return idx; + + return -1; + } + + ssize __add_entry( self, u64 key ) + { + ssize idx; + entry = { key, -1 }; + + idx = array_header( self.Entries )->Num; + _append( & self.Entries, entry ); + return idx; + } + + HT_FindResult __find( self, u64 key ) + { + HT_FindResult result = { -1, -1, -1 }; + + ArrayHeader* hash_header = array_header( self.Hashes ); + + if ( hash_header->Num > 0 ) + { + result.HashIndex = key % hash_header->Num; + result.EntryIndex = self.Hashes[ result.HashIndex ]; + + while ( result.EntryIndex >= 0 ) + { + if ( self.Entries[ result.EntryIndex ].Key == key ) + break; + + result.PrevIndex = result.EntryIndex; + result.EntryIndex = self.Entries[ result.EntryIndex ].Next; + } + } + + return result; + } + + b32 __full( self ) + { + ArrayHeader* hash_header = array_header( self.Hashes ); + ArrayHeader* entries_header = array_header( self.Entries ); + + return 0.75f * hash_header->Num < entries_header->Num; + } + ))); +#pragma pop_macro( "GEN_ASSERT" ) +#pragma pop_macro( "GEN_ASSERT_NOT_NULL" ) + + char const* cmt_str = str_fmt_buf( "Name: %.*s Type: %.*s" + , tbl_type.length(), tbl_type.Data + , type.Len, type.Ptr ); + + return def_global_body(args( + def_pragma( to_str( str_fmt_buf( "region %S", tbl_type ))), + fmt_newline, + hashtable_types, + fmt_newline, + entry_array, + hashtable_def, + fmt_newline, + def_pragma( to_str( str_fmt_buf( "endregion %S", tbl_type ))), + fmt_newline + )); +} From c7b072266f160d8f7f5c528775334dfa26658103 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 01:40:31 -0500 Subject: [PATCH 017/112] progress on c_library.cpp --- gen_c_library/c_library.cpp | 75 +++++++++++++++++-- .../components/memory.fixed_arena.hpp | 3 + gen_c_library/components/misc.hpp | 17 +++-- 3 files changed, 82 insertions(+), 13 deletions(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index a0b3044..6073696 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -18,6 +18,8 @@ GEN_NS_END #include "components/memory.fixed_arena.hpp" #include "components/misc.hpp" +#include "components/containers.array.hpp" +#include "components/containers.hashtable.hpp" using namespace gen; @@ -114,6 +116,14 @@ int gen_main() Code basic_types = scan_file( project_dir "dependencies/basic_types.hpp" ); Code debug = scan_file( project_dir "dependencies/debug.hpp" ); + header.print_fmt( roll_own_dependencies_guard_start ); + header.print( platform ); + header.print_fmt( "\nGEN_NS_BEGIN\n" ); + + header.print( macros ); + header.print( basic_types ); + header.print( debug ); + CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" ); CodeBody memory = def_body(ECode::Struct_Body); for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry ) @@ -164,7 +174,12 @@ int gen_main() break; case ECode::Preprocess_If: { - ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, memory ); + ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory ); + } + break; + case ECode::Preprocess_IfDef: + { + ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory ); } break; case ECode::Preprocess_Pragma: @@ -179,14 +194,58 @@ int gen_main() } } - header.print_fmt( roll_own_dependencies_guard_start ); - header.print( platform ); - header.print_fmt( "\nGEN_NS_BEGIN\n" ); - - header.print( macros ); - header.print( basic_types ); - header.print( debug ); header.print( memory ); + + Code string_ops = scan_file( project_dir "dependencies/string_ops.hpp" ); + header.print( string_ops ); + + CodeBody printing_parsed = parse_file( project_dir "dependencies/printing.hpp" ); + CodeBody printing = def_body(ECode::Struct_Body); + for ( Code entry = printing_parsed.begin(); entry != printing_parsed.end(); ++ entry ) + { + switch (entry->Type) + { + case ECode::Preprocess_IfDef: + { + ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed ); + } + } + + if (entry->Type == ECode::Variable && + contains(entry->Name, txt("Msg_Invalid_Value"))) + { + CodeDefine define = def_define(entry->Name, entry->Value->Content); + printing.append(define); + continue; + } + printing.append(entry); + } + + header.print(printing); + + CodeBody parsed_containers = parse_file( project_dir "dependencies/containers.hpp" ); + CodeBody containers = def_body(ECode::Struct_Body); + for ( Code entry = parsed_containers.begin(); entry != parsed_containers.end(); ++ entry ) + { + switch ( entry->Type ) + { + case ECode::Preprocess_Pragma: + { + bool found = false; + + found = swap_pragma_region_implementation( txt("Array"), gen_array_base, entry, containers); + if (found) break; + + found = swap_pragma_region_implementation( txt("Hashtable"), gen_hashtable_base, entry, containers); + if (found) break; + + containers.append(entry); + } + break; + } + } + + header.print(containers); } header.print( pop_ignores ); diff --git a/gen_c_library/components/memory.fixed_arena.hpp b/gen_c_library/components/memory.fixed_arena.hpp index b5958cc..d136ec5 100644 --- a/gen_c_library/components/memory.fixed_arena.hpp +++ b/gen_c_library/components/memory.fixed_arena.hpp @@ -6,6 +6,7 @@ using namespace gen; CodeBody gen_fixed_arenas() { CodeBody result = def_body(ECode::Global_Body); + result.append(def_pragma(txt("region FixedArena"))); char const* template_struct = stringize( struct FixedArena_ @@ -115,5 +116,7 @@ CodeBody gen_fixed_arenas() )" ))); + result.append(def_pragma(txt("endregion FixedArena"))); + return result; } diff --git a/gen_c_library/components/misc.hpp b/gen_c_library/components/misc.hpp index 1fc20aa..7280231 100644 --- a/gen_c_library/components/misc.hpp +++ b/gen_c_library/components/misc.hpp @@ -11,7 +11,7 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod if ( cond->Content.contains(cond_sig) ) { s32 depth = 1; - ++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ++ entry_iter) switch + ++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ) switch (entry_iter->Type) { case ECode::Preprocess_If: case ECode::Preprocess_IfDef: @@ -24,24 +24,30 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod depth --; if (depth == 0) { continue_for = false; + break; } } break; + default: + ++ entry_iter; + break; } } return entry_iter != body.end(); } -void swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body ) +bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body ) { + bool found = false; CodePragma possible_region = entry_iter.cast(); String region_sig = string_fmt_buf(GlobalAllocator, "region %s", region_name.Ptr); String endregion_sig = string_fmt_buf(GlobalAllocator, "endregion %s", region_name.Ptr); if ( possible_region->Content.contains(region_sig)) { - body.append(possible_region); + found = true; + // body.append(possible_region); body.append(swap_content()); ++ entry_iter; for(b32 continue_for = true; continue_for; ++entry_iter) switch @@ -50,12 +56,13 @@ void swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_ { CodePragma possible_end_region = entry_iter.cast(); if ( possible_end_region->Content.contains(endregion_sig) ) { - body.append(possible_end_region); + // body.append(possible_end_region); continue_for = false; } } break; } + body.append(entry_iter); } - body.append(entry_iter); + return found; } From e5acac1d18af5148ea054a2d75ff2bd6dc89299c Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 03:06:30 -0500 Subject: [PATCH 018/112] String member definitions not longer used in the base project --- project/auxillary/builder.cpp | 12 +- project/auxillary/scanner.hpp | 8 +- project/bootstrap.cpp | 16 +- project/components/ast.cpp | 312 ++++++------- project/components/code_serialization.cpp | 544 +++++++++++----------- project/components/interface.cpp | 26 +- project/components/interface.upfront.cpp | 10 +- project/components/lexer.cpp | 16 +- project/components/parser.cpp | 70 +-- project/dependencies/parsing.cpp | 4 +- project/dependencies/platform.hpp | 2 - project/dependencies/printing.cpp | 2 +- project/dependencies/strings.hpp | 98 ++-- project/helpers/helper.hpp | 48 +- 14 files changed, 594 insertions(+), 574 deletions(-) diff --git a/project/auxillary/builder.cpp b/project/auxillary/builder.cpp index 900a03a..4632772 100644 --- a/project/auxillary/builder.cpp +++ b/project/auxillary/builder.cpp @@ -13,7 +13,7 @@ Builder Builder::open( char const* path ) return result; } - result.Buffer = String::make_reserve( GlobalAllocator, Builder_StrBufferReserve ); + result.Buffer = string_make_reserve( GlobalAllocator, Builder_StrBufferReserve ); // log_fmt("$Builder - Opened file: %s\n", result.File.filename ); return result; @@ -21,7 +21,7 @@ Builder Builder::open( char const* path ) void Builder::pad_lines( s32 num ) { - Buffer.append( "\n" ); + append( Buffer, "\n" ); } void Builder::print( Code code ) @@ -29,7 +29,7 @@ void Builder::print( Code code ) String str = code->to_string(); // const ssize len = str.length(); // log_fmt( "%s - print: %.*s\n", File.filename, len > 80 ? 80 : len, str.Data ); - Buffer.append( str ); + append( Buffer, str ); } void Builder::print_fmt( char const* fmt, ... ) @@ -43,17 +43,17 @@ void Builder::print_fmt( char const* fmt, ... ) va_end( va ); // log_fmt( "$%s - print_fmt: %.*s\n", File.filename, res > 80 ? 80 : res, buf ); - Buffer.append( buf, res ); + append( Buffer, buf, res ); } void Builder::write() { - b32 result = file_write( & File, Buffer, Buffer.length() ); + b32 result = file_write( & File, Buffer, length(Buffer) ); if ( result == false ) log_failure("gen::File::write - Failed to write to file: %s\n", file_name( & File ) ); log_fmt( "Generated: %s\n", File.filename ); file_close( & File ); - Buffer.free(); + free(Buffer); } diff --git a/project/auxillary/scanner.hpp b/project/auxillary/scanner.hpp index 97af098..6540a4d 100644 --- a/project/auxillary/scanner.hpp +++ b/project/auxillary/scanner.hpp @@ -23,9 +23,9 @@ Code scan_file( char const* path ) GEN_FATAL("scan_file: %s is empty", path ); } - String str = String::make_reserve( GlobalAllocator, fsize ); + String str = string_make_reserve( GlobalAllocator, fsize ); file_read( & file, str, fsize ); - str.get_header().Length = fsize; + get_header(str).Length = fsize; // Skip GEN_INTELLISENSE_DIRECTIVES preprocessor blocks // Its designed so that the directive should be the first thing in the file. @@ -97,12 +97,12 @@ Code scan_file( char const* path ) if ( (scanner + 2) >= ( str.Data + fsize ) ) { mem_move( str, scanner, left ); - str.get_header().Length = left; + get_header(str).Length = left; break; } mem_move( str, scanner, left ); - str.get_header().Length = left; + get_header(str).Length = left; break; } diff --git a/project/bootstrap.cpp b/project/bootstrap.cpp index e4f2aa7..3214183 100644 --- a/project/bootstrap.cpp +++ b/project/bootstrap.cpp @@ -24,20 +24,20 @@ constexpr char const* generation_notice = void format_file( char const* path ) { - String resolved_path = String::make(GlobalAllocator, to_str(path)); + String resolved_path = string_make(GlobalAllocator, to_str(path)); - String style_arg = String::make(GlobalAllocator, txt("-style=file:")); - style_arg.append("../scripts/.clang-format "); + String style_arg = string_make(GlobalAllocator, txt("-style=file:")); + append( style_arg, "../scripts/.clang-format "); // Need to execute clang format on the generated file to get it to match the original. #define clang_format "clang-format " #define cf_format_inplace "-i " #define cf_verbose "-verbose " - String command = String::make( GlobalAllocator, clang_format ); - command.append( cf_format_inplace ); - command.append( cf_verbose ); - command.append( style_arg ); - command.append( resolved_path ); + String command = string_make( GlobalAllocator, clang_format ); + append( command, cf_format_inplace ); + append( command, cf_verbose ); + append( command, style_arg ); + append( command, resolved_path ); log_fmt("\tRunning clang-format on file:\n"); system( command ); log_fmt("\tclang-format finished reformatting.\n"); diff --git a/project/components/ast.cpp b/project/components/ast.cpp index b19f7ef..d56eebf 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -9,16 +9,16 @@ Code Code::Invalid; // This serializes all the data-members in a "debug" format, where each member is printed with its associated value. char const* AST::debug_str() { - String result = String::make_reserve( GlobalAllocator, kilobytes(1) ); + String result = string_make_reserve( GlobalAllocator, kilobytes(1) ); if ( Parent ) - result.append_fmt( "\n\tParent : %S %S", Parent->type_str(), Name ? Name : "" ); + append_fmt( result, "\n\tParent : %S %S", Parent->type_str(), Name ? Name : "" ); else - result.append_fmt( "\n\tParent : %S", "Null" ); + append_fmt( result, "\n\tParent : %S", "Null" ); - result.append_fmt( "\n\tName : %S", Name ? Name : "Null" ); - result.append_fmt( "\n\tType : %S", type_str() ); - result.append_fmt( "\n\tModule Flags : %S", to_str( ModuleFlags ) ); + append_fmt( result, "\n\tName : %S", Name ? Name : "Null" ); + append_fmt( result, "\n\tType : %S", type_str() ); + append_fmt( result, "\n\tModule Flags : %S", to_str( ModuleFlags ) ); switch ( Type ) { @@ -30,9 +30,9 @@ char const* AST::debug_str() case Access_Protected: case Access_Public: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); break; case Untyped: @@ -48,74 +48,74 @@ char const* AST::debug_str() case Preprocess_IfDef: case Preprocess_IfNotDef: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tContent: %S", Content ); + append_fmt( result, "\n\tContent: %S", Content ); break; case Class: case Struct: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmd : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - result.append_fmt( "\n\tParentAccess: %s", ParentType ? to_str( ParentAccess ) : "No Parent" ); - result.append_fmt( "\n\tParentType : %s", ParentType ? ParentType->type_str() : "Null" ); - result.append_fmt( "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmd : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); + append_fmt( result, "\n\tParentAccess: %s", ParentType ? to_str( ParentAccess ) : "No Parent" ); + append_fmt( result, "\n\tParentType : %s", ParentType ? ParentType->type_str() : "Null" ); + append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); break; case Class_Fwd: case Struct_Fwd: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmd : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - result.append_fmt( "\n\tParentAccess: %s", ParentType ? to_str( ParentAccess ) : "No Parent" ); - result.append_fmt( "\n\tParentType : %s", ParentType ? ParentType->type_str() : "Null" ); + append_fmt( result, "\n\tInlineCmd : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); + append_fmt( result, "\n\tParentAccess: %s", ParentType ? to_str( ParentAccess ) : "No Parent" ); + append_fmt( result, "\n\tParentType : %s", ParentType ? ParentType->type_str() : "Null" ); break; case Constructor: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - result.append_fmt( "\n\tInitializerList: %S", InitializerList ? InitializerList->to_string() : "Null" ); - result.append_fmt( "\n\tParams : %S", Params ? Params->to_string() : "Null" ); - result.append_fmt( "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); + append_fmt( result, "\n\tInitializerList: %S", InitializerList ? InitializerList->to_string() : "Null" ); + append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); + append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); break; case Constructor_Fwd: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - result.append_fmt( "\n\tInitializerList: %S", InitializerList ? InitializerList->to_string() : "Null" ); - result.append_fmt( "\n\tParams : %S", Params ? Params->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); + append_fmt( result, "\n\tInitializerList: %S", InitializerList ? InitializerList->to_string() : "Null" ); + append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); break; case Destructor: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - result.append_fmt( "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); + append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); break; case Destructor_Fwd: @@ -124,208 +124,208 @@ char const* AST::debug_str() case Enum: case Enum_Class: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - result.append_fmt( "\n\tUnderlying Type : %S", UnderlyingType ? UnderlyingType->to_string() : "Null" ); - result.append_fmt( "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); + append_fmt( result, "\n\tUnderlying Type : %S", UnderlyingType ? UnderlyingType->to_string() : "Null" ); + append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); break; case Enum_Fwd: case Enum_Class_Fwd: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - result.append_fmt( "\n\tUnderlying Type : %S", UnderlyingType ? UnderlyingType->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); + append_fmt( result, "\n\tUnderlying Type : %S", UnderlyingType ? UnderlyingType->to_string() : "Null" ); break; case Extern_Linkage: case Namespace: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tBody: %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tBody: %S", Body ? Body->debug_str() : "Null" ); break; case Friend: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tDeclaration: %S", Declaration ? Declaration->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tDeclaration: %S", Declaration ? Declaration->to_string() : "Null" ); break; case Function: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); - result.append_fmt( "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - result.append_fmt( "\n\tReturnType: %S", ReturnType ? ReturnType->to_string() : "Null" ); - result.append_fmt( "\n\tParams : %S", Params ? Params->to_string() : "Null" ); - result.append_fmt( "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); + append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); + append_fmt( result, "\n\tReturnType: %S", ReturnType ? ReturnType->to_string() : "Null" ); + append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); + append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); break; case Function_Fwd: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); - result.append_fmt( "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - result.append_fmt( "\n\tReturnType: %S", ReturnType ? ReturnType->to_string() : "Null" ); - result.append_fmt( "\n\tParams : %S", Params ? Params->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); + append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); + append_fmt( result, "\n\tReturnType: %S", ReturnType ? ReturnType->to_string() : "Null" ); + append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); break; case Module: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); break; case Operator: case Operator_Member: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); - result.append_fmt( "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - result.append_fmt( "\n\tReturnType: %S", ReturnType ? ReturnType->to_string() : "Null" ); - result.append_fmt( "\n\tParams : %S", Params ? Params->to_string() : "Null" ); - result.append_fmt( "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); - result.append_fmt( "\n\tOp : %S", to_str( Op ) ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); + append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); + append_fmt( result, "\n\tReturnType: %S", ReturnType ? ReturnType->to_string() : "Null" ); + append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); + append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tOp : %S", to_str( Op ) ); break; case Operator_Fwd: case Operator_Member_Fwd: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); - result.append_fmt( "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - result.append_fmt( "\n\tReturnType: %S", ReturnType ? ReturnType->to_string() : "Null" ); - result.append_fmt( "\n\tParams : %S", Params ? Params->to_string() : "Null" ); - result.append_fmt( "\n\tOp : %S", to_str( Op ) ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); + append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); + append_fmt( result, "\n\tReturnType: %S", ReturnType ? ReturnType->to_string() : "Null" ); + append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); + append_fmt( result, "\n\tOp : %S", to_str( Op ) ); break; case Operator_Cast: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - result.append_fmt( "\n\tValueType : %S", ValueType ? ValueType->to_string() : "Null" ); - result.append_fmt( "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); + append_fmt( result, "\n\tValueType : %S", ValueType ? ValueType->to_string() : "Null" ); + append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); break; case Operator_Cast_Fwd: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - result.append_fmt( "\n\tValueType : %S", ValueType ? ValueType->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); + append_fmt( result, "\n\tValueType : %S", ValueType ? ValueType->to_string() : "Null" ); break; case Parameters: - result.append_fmt( "\n\tNumEntries: %d", NumEntries ); - result.append_fmt( "\n\tLast : %S", Last->Name ); - result.append_fmt( "\n\tNext : %S", Next->Name ); - result.append_fmt( "\n\tValueType : %S", ValueType ? ValueType->to_string() : "Null" ); - result.append_fmt( "\n\tValue : %S", Value ? Value->to_string() : "Null" ); + append_fmt( result, "\n\tNumEntries: %d", NumEntries ); + append_fmt( result, "\n\tLast : %S", Last->Name ); + append_fmt( result, "\n\tNext : %S", Next->Name ); + append_fmt( result, "\n\tValueType : %S", ValueType ? ValueType->to_string() : "Null" ); + append_fmt( result, "\n\tValue : %S", Value ? Value->to_string() : "Null" ); break; case Specifiers: { - result.append_fmt( "\n\tNumEntries: %d", NumEntries ); - result.append( "\n\tArrSpecs: " ); + append_fmt( result, "\n\tNumEntries: %d", NumEntries ); + GEN_NS append( result, "\n\tArrSpecs: " ); s32 idx = 0; s32 left = NumEntries; while ( left-- ) { StrC spec = ESpecifier::to_str( ArrSpecs[idx] ); - result.append_fmt( "%.*s, ", spec.Len, spec.Ptr ); + append_fmt( result, "%.*s, ", spec.Len, spec.Ptr ); idx++; } - result.append_fmt( "\n\tNextSpecs: %S", NextSpecs ? NextSpecs->debug_str() : "Null" ); + append_fmt( result, "\n\tNextSpecs: %S", NextSpecs ? NextSpecs->debug_str() : "Null" ); } break; case Template: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tParams : %S", Params ? Params->to_string() : "Null" ); - result.append_fmt( "\n\tDeclaration: %S", Declaration ? Declaration->to_string() : "Null" ); + append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); + append_fmt( result, "\n\tDeclaration: %S", Declaration ? Declaration->to_string() : "Null" ); break; case Typedef: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tUnderlyingType: %S", UnderlyingType ? UnderlyingType->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tUnderlyingType: %S", UnderlyingType ? UnderlyingType->to_string() : "Null" ); break; case Typename: - result.append_fmt( "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - result.append_fmt( "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - result.append_fmt( "\n\tReturnType : %S", ReturnType ? ReturnType->to_string() : "Null" ); - result.append_fmt( "\n\tParams : %S", Params ? Params->to_string() : "Null" ); - result.append_fmt( "\n\tArrExpr : %S", ArrExpr ? ArrExpr->to_string() : "Null" ); + append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); + append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); + append_fmt( result, "\n\tReturnType : %S", ReturnType ? ReturnType->to_string() : "Null" ); + append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); + append_fmt( result, "\n\tArrExpr : %S", ArrExpr ? ArrExpr->to_string() : "Null" ); break; case Union: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); - result.append_fmt( "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); + append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); break; case Using: if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - result.append_fmt( "\n\tUnderlyingType: %S", UnderlyingType ? UnderlyingType->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); + append_fmt( result, "\n\tUnderlyingType: %S", UnderlyingType ? UnderlyingType->to_string() : "Null" ); break; case Variable: @@ -333,25 +333,25 @@ char const* AST::debug_str() if ( Parent && Parent->Type == Variable ) { // Its a NextVar - result.append_fmt( "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - result.append_fmt( "\n\tValue : %S", Value ? Value->to_string() : "Null" ); - result.append_fmt( "\n\tBitfieldSize: %S", BitfieldSize ? BitfieldSize->to_string() : "Null" ); - result.append_fmt( "\n\tNextVar : %S", NextVar ? NextVar->debug_str() : "Null" ); + append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); + append_fmt( result, "\n\tValue : %S", Value ? Value->to_string() : "Null" ); + append_fmt( result, "\n\tBitfieldSize: %S", BitfieldSize ? BitfieldSize->to_string() : "Null" ); + append_fmt( result, "\n\tNextVar : %S", NextVar ? NextVar->debug_str() : "Null" ); break; } if ( Prev ) - result.append_fmt( "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); if ( Next ) - result.append_fmt( "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - result.append_fmt( "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - result.append_fmt( "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - result.append_fmt( "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - result.append_fmt( "\n\tValueType : %S", ValueType ? ValueType->to_string() : "Null" ); - result.append_fmt( "\n\tBitfieldSize: %S", BitfieldSize ? BitfieldSize->to_string() : "Null" ); - result.append_fmt( "\n\tValue : %S", Value ? Value->to_string() : "Null" ); - result.append_fmt( "\n\tNextVar : %S", NextVar ? NextVar->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); + append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); + append_fmt( result, "\n\tValueType : %S", ValueType ? ValueType->to_string() : "Null" ); + append_fmt( result, "\n\tBitfieldSize: %S", BitfieldSize ? BitfieldSize->to_string() : "Null" ); + append_fmt( result, "\n\tValue : %S", Value ? Value->to_string() : "Null" ); + append_fmt( result, "\n\tNextVar : %S", NextVar ? NextVar->debug_str() : "Null" ); break; } @@ -372,7 +372,7 @@ AST* AST::duplicate() String AST::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); to_string( result ); return result; } @@ -390,25 +390,25 @@ void AST::to_string( String& result ) #ifdef GEN_DONT_ALLOW_INVALID_CODE log_failure("Attempted to serialize invalid code! - %S", Parent ? Parent->debug_str() : Name ); #else - result.append_fmt( "Invalid Code!" ); + append_fmt( result, "Invalid Code!" ); #endif break; case NewLine: - result.append("\n"); + GEN_NS append( result,"\n"); break; case Untyped: case Execution: case Comment: case PlatformAttributes: - result.append( Content ); + GEN_NS append( result, Content ); break; case Access_Private: case Access_Protected: case Access_Public: - result.append( Name ); + GEN_NS append( result, Name ); break; case Class: @@ -659,8 +659,8 @@ bool AST::is_equal( AST* other ) "so it must be verified by eye for now\n" \ "AST Content:\n%S\n" \ "Other Content:\n%S\n" \ - , content.visualize_whitespace() \ - , other->content.visualize_whitespace() \ + , visualize_whitespace(content) \ + , visualize_whitespace(other->content) \ ); \ } diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 208e622..1597e38 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -15,18 +15,18 @@ String Code::to_string() String CodeAttributes::to_string() { - return ast->Content.duplicate( GlobalAllocator ); + return GEN_NS duplicate( ast->Content, GlobalAllocator ); } String CodeBody::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); switch ( ast->Type ) { using namespace ECode; case Untyped: case Execution: - result.append( raw()->Content ); + GEN_NS append( result, raw()->Content ); break; case Enum_Body: @@ -53,34 +53,34 @@ void CodeBody::to_string( String& result ) s32 left = ast->NumEntries; while ( left -- ) { - result.append_fmt( "%S", curr.to_string() ); + append_fmt( result, "%S", curr.to_string() ); ++curr; } } void CodeBody::to_string_export( String& result ) { - result.append_fmt( "export\n{\n" ); + append_fmt( result, "export\n{\n" ); Code curr = *this; s32 left = ast->NumEntries; while ( left-- ) { - result.append_fmt( "%S", curr.to_string() ); + append_fmt( result, "%S", curr.to_string() ); ++curr; } - result.append_fmt( "};\n" ); + append_fmt( result, "};\n" ); } String CodeComment::to_string() { - return ast->Content.duplicate( GlobalAllocator ); + return GEN_NS duplicate( ast->Content, GlobalAllocator ); } String CodeConstructor::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); switch (ast->Type) { using namespace ECode; @@ -98,53 +98,53 @@ void CodeConstructor::to_string_def( String& result ) { AST* ClassStructParent = ast->Parent->Parent; if (ClassStructParent) { - result.append( ClassStructParent->Name ); + append( result, ClassStructParent->Name ); } else { - result.append( ast->Name ); + append( result, ast->Name ); } if ( ast->Params ) - result.append_fmt( "( %S )", ast->Params.to_string() ); + append_fmt( result, "( %S )", ast->Params.to_string() ); else - result.append( "()" ); + append( result, "()" ); if ( ast->InitializerList ) - result.append_fmt( " : %S", ast->InitializerList.to_string() ); + append_fmt( result, " : %S", ast->InitializerList.to_string() ); if ( ast->InlineCmt ) - result.append_fmt( " // %S", ast->InlineCmt->Content ); + append_fmt( result, " // %S", ast->InlineCmt->Content ); - result.append_fmt( "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( result, "\n{\n%S\n}\n", ast->Body.to_string() ); } void CodeConstructor::to_string_fwd( String& result ) { AST* ClassStructParent = ast->Parent->Parent; if (ClassStructParent) { - result.append( ClassStructParent->Name ); + append( result, ClassStructParent->Name ); } else { - result.append( ast->Name ); + append( result, ast->Name ); } if ( ast->Params ) - result.append_fmt( "( %S )", ast->Params.to_string() ); + append_fmt( result, "( %S )", ast->Params.to_string() ); else - result.append_fmt("()"); + append_fmt( result, "()"); if (ast->Body) - result.append_fmt( " = %S", ast->Body.to_string() ); + append_fmt( result, " = %S", ast->Body.to_string() ); if ( ast->InlineCmt ) - result.append_fmt( "; // %S\n", ast->InlineCmt->Content ); + append_fmt( result, "; // %S\n", ast->InlineCmt->Content ); else - result.append( ";\n" ); + append( result, ";\n" ); } String CodeClass::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); switch ( ast->Type ) { using namespace ECode; @@ -161,80 +161,80 @@ String CodeClass::to_string() void CodeClass::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); - result.append( "class " ); + append( result, "class " ); if ( ast->Attributes ) { - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); } if ( ast->ParentType ) { char const* access_level = to_str( ast->ParentAccess ); - result.append_fmt( "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); + append_fmt( result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); CodeType interface = ast->ParentType->Next->cast< CodeType >(); if ( interface ) - result.append( "\n" ); + append( result, "\n" ); while ( interface ) { - result.append_fmt( ", %S", interface.to_string() ); + append_fmt( result, ", %S", interface.to_string() ); interface = interface->Next ? interface->Next->cast< CodeType >() : CodeType { nullptr }; } } else if ( ast->Name ) { - result.append( ast->Name ); + append( result, ast->Name ); } if ( ast->InlineCmt ) { - result.append_fmt( " // %S", ast->InlineCmt->Content ); + append_fmt( result, " // %S", ast->InlineCmt->Content ); } - result.append_fmt( "\n{\n%S\n}", ast->Body.to_string() ); + append_fmt( result, "\n{\n%S\n}", ast->Body.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - result.append(";\n"); + append( result, ";\n"); } void CodeClass::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); if ( ast->Attributes ) - result.append_fmt( "class %S %S", ast->Attributes.to_string(), ast->Name ); + append_fmt( result, "class %S %S", ast->Attributes.to_string(), ast->Name ); - else result.append_fmt( "class %S", ast->Name ); + else append_fmt( result, "class %S", ast->Name ); // Check if it can have an end-statement if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - result.append_fmt( "; // %S\n", ast->InlineCmt->Content ); + append_fmt( result, "; // %S\n", ast->InlineCmt->Content ); else - result.append(";\n"); + append( result,";\n"); } } String CodeDefine::to_string() { - return String::fmt_buf( GlobalAllocator, "#define %S %S\n", ast->Name, ast->Content ); + return string_fmt_buf( GlobalAllocator, "#define %S %S\n", ast->Name, ast->Content ); } void CodeDefine::to_string( String& result ) { - result.append_fmt( "#define %S %S\n", ast->Name, ast->Content ); + append_fmt( result, "#define %S %S\n", ast->Name, ast->Content ); } String CodeDestructor::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); switch ( ast->Type ) { using namespace ECode; @@ -252,19 +252,19 @@ void CodeDestructor::to_string_def( String& result ) { if ( ast->Name ) { - result.append_fmt( "%S()", ast->Name ); + append_fmt( result, "%S()", ast->Name ); } else if ( ast->Specs ) { if ( ast->Specs.has( ESpecifier::Virtual ) ) - result.append_fmt( "virtual ~%S()", ast->Parent->Name ); + append_fmt( result, "virtual ~%S()", ast->Parent->Name ); else - result.append_fmt( "~%S()", ast->Parent->Name ); + append_fmt( result, "~%S()", ast->Parent->Name ); } else - result.append_fmt( "~%S()", ast->Parent->Name ); + append_fmt( result, "~%S()", ast->Parent->Name ); - result.append_fmt( "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( result, "\n{\n%S\n}\n", ast->Body.to_string() ); } void CodeDestructor::to_string_fwd( String& result ) @@ -272,27 +272,27 @@ void CodeDestructor::to_string_fwd( String& result ) if ( ast->Specs ) { if ( ast->Specs.has( ESpecifier::Virtual ) ) - result.append_fmt( "virtual ~%S();\n", ast->Parent->Name ); + append_fmt( result, "virtual ~%S();\n", ast->Parent->Name ); else - result.append_fmt( "~%S()", ast->Parent->Name ); + append_fmt( result, "~%S()", ast->Parent->Name ); if ( ast->Specs.has( ESpecifier::Pure ) ) - result.append( " = 0;" ); + append( result, " = 0;" ); else if (ast->Body) - result.append_fmt( " = %S;", ast->Body.to_string() ); + append_fmt( result, " = %S;", ast->Body.to_string() ); } else - result.append_fmt( "~%S();", ast->Parent->Name ); + append_fmt( result, "~%S();", ast->Parent->Name ); if ( ast->InlineCmt ) - result.append_fmt( " %S", ast->InlineCmt->Content ); + append_fmt( result, " %S", ast->InlineCmt->Content ); else - result.append("\n"); + append( result, "\n"); } String CodeEnum::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); switch ( ast->Type ) { using namespace ECode; @@ -315,150 +315,150 @@ String CodeEnum::to_string() void CodeEnum::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); if ( ast->Attributes || ast->UnderlyingType ) { - result.append( "enum " ); + append( result, "enum " ); if ( ast->Attributes ) - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); if ( ast->UnderlyingType ) - result.append_fmt( "%S : %S\n{\n%S\n}" + append_fmt( result, "%S : %S\n{\n%S\n}" , ast->Name , ast->UnderlyingType.to_string() , ast->Body.to_string() ); - else result.append_fmt( "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); + else append_fmt( result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); } - else result.append_fmt( "enum %S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); + else append_fmt( result, "enum %S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - result.append(";\n"); + append( result, ";\n"); } void CodeEnum::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); if ( ast->Attributes ) - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); - result.append_fmt( "enum %S : %S", ast->Name, ast->UnderlyingType.to_string() ); + append_fmt( result, "enum %S : %S", ast->Name, ast->UnderlyingType.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - result.append_fmt("; %S", ast->InlineCmt->Content ); + append_fmt( result, "; %S", ast->InlineCmt->Content ); else - result.append(";\n"); + append( result, ";\n"); } } void CodeEnum::to_string_class_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); if ( ast->Attributes || ast->UnderlyingType ) { - result.append( "enum class " ); + append( result, "enum class " ); if ( ast->Attributes ) { - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); } if ( ast->UnderlyingType ) { - result.append_fmt( "%S : %S\n{\n%S\n}", ast->Name, ast->UnderlyingType.to_string(), ast->Body.to_string() ); + append_fmt( result, "%S : %S\n{\n%S\n}", ast->Name, ast->UnderlyingType.to_string(), ast->Body.to_string() ); } else { - result.append_fmt( "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); + append_fmt( result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); } } else { - result.append_fmt( "enum class %S\n{\n%S\n}", ast->Body.to_string() ); + append_fmt( result, "enum class %S\n{\n%S\n}", ast->Body.to_string() ); } if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - result.append(";\n"); + append( result, ";\n"); } void CodeEnum::to_string_class_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); - result.append( "enum class " ); + append( result, "enum class " ); if ( ast->Attributes ) - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); - result.append_fmt( "%S : %S", ast->Name, ast->UnderlyingType.to_string() ); + append_fmt( result, "%S : %S", ast->Name, ast->UnderlyingType.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - result.append_fmt("; %S", ast->InlineCmt->Content ); + append_fmt( result, "; %S", ast->InlineCmt->Content ); else - result.append(";\n"); + append( result, ";\n"); } } String CodeExec::to_string() { - return ast->Content.duplicate( GlobalAllocator ); + return GEN_NS duplicate( ast->Content, GlobalAllocator ); } void CodeExtern::to_string( String& result ) { if ( ast->Body ) - result.append_fmt( "extern \"%S\"\n{\n%S\n}\n", ast->Name, ast->Body.to_string() ); + append_fmt( result, "extern \"%S\"\n{\n%S\n}\n", ast->Name, ast->Body.to_string() ); else - result.append_fmt( "extern \"%S\"\n{}\n", ast->Name ); + append_fmt( result, "extern \"%S\"\n{}\n", ast->Name ); } String CodeInclude::to_string() { - return String::fmt_buf( GlobalAllocator, "#include %S\n", ast->Content ); + return string_fmt_buf( GlobalAllocator, "#include %S\n", ast->Content ); } void CodeInclude::to_string( String& result ) { - result.append_fmt( "#include %S\n", ast->Content ); + append_fmt( result, "#include %S\n", ast->Content ); } String CodeFriend::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); to_string( result ); return result; } void CodeFriend::to_string( String& result ) { - result.append_fmt( "friend %S", ast->Declaration->to_string() ); + append_fmt( result, "friend %S", ast->Declaration->to_string() ); - if ( ast->Declaration->Type != ECode::Function && result[ result.length() - 1 ] != ';' ) + if ( ast->Declaration->Type != ECode::Function && result[ length(result) - 1 ] != ';' ) { - result.append( ";" ); + append( result, ";" ); } if ( ast->InlineCmt ) - result.append_fmt(" %S", ast->InlineCmt->Content ); + append_fmt( result, " %S", ast->InlineCmt->Content ); else - result.append("\n"); + append( result, "\n"); } String CodeFn::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); switch ( ast->Type ) { using namespace ECode; @@ -475,10 +475,10 @@ String CodeFn::to_string() void CodeFn::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export" ); + append( result, "export" ); if ( ast->Attributes ) - result.append_fmt( " %S ", ast->Attributes.to_string() ); + append_fmt( result, " %S ", ast->Attributes.to_string() ); bool prefix_specs = false; if ( ast->Specs ) @@ -488,7 +488,7 @@ void CodeFn::to_string_def( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); prefix_specs = true; } @@ -496,19 +496,19 @@ void CodeFn::to_string_def( String& result ) } if ( ast->Attributes || prefix_specs ) - result.append( "\n" ); + append( result, "\n" ); if ( ast->ReturnType ) - result.append_fmt( "%S %S(", ast->ReturnType.to_string(), ast->Name ); + append_fmt( result, "%S %S(", ast->ReturnType.to_string(), ast->Name ); else - result.append_fmt( "%S(", ast->Name ); + append_fmt( result, "%S(", ast->Name ); if ( ast->Params ) - result.append_fmt( "%S)", ast->Params.to_string() ); + append_fmt( result, "%S)", ast->Params.to_string() ); else - result.append( ")" ); + append( result, ")" ); if ( ast->Specs ) { @@ -517,21 +517,21 @@ void CodeFn::to_string_def( String& result ) if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } - result.append_fmt( "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( result, "\n{\n%S\n}\n", ast->Body.to_string() ); } void CodeFn::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); if ( ast->Attributes ) - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); b32 prefix_specs = false; if ( ast->Specs ) @@ -541,7 +541,7 @@ void CodeFn::to_string_fwd( String& result ) if ( ! ESpecifier::is_trailing( spec ) || ! (spec != ESpecifier::Pure) ) { StrC spec_str = ESpecifier::to_str( spec ); - result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); prefix_specs = true; } @@ -550,20 +550,20 @@ void CodeFn::to_string_fwd( String& result ) if ( ast->Attributes || prefix_specs ) { - result.append("\n" ); + append( result, "\n" ); } if ( ast->ReturnType ) - result.append_fmt( "%S %S(", ast->ReturnType.to_string(), ast->Name ); + append_fmt( result, "%S %S(", ast->ReturnType.to_string(), ast->Name ); else - result.append_fmt( "%S(", ast->Name ); + append_fmt( result, "%S(", ast->Name ); if ( ast->Params ) - result.append_fmt( "%S)", ast->Params.to_string() ); + append_fmt( result, "%S)", ast->Params.to_string() ); else - result.append( ")" ); + append( result, ")" ); if ( ast->Specs ) { @@ -572,25 +572,25 @@ void CodeFn::to_string_fwd( String& result ) if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } if ( ast->Specs && ast->Specs.has( ESpecifier::Pure ) >= 0 ) - result.append( " = 0;" ); + append( result, " = 0;" ); else if (ast->Body) - result.append_fmt( " = %S;", ast->Body.to_string() ); + append_fmt( result, " = %S;", ast->Body.to_string() ); if ( ast->InlineCmt ) - result.append_fmt( "; %S", ast->InlineCmt->Content ); + append_fmt( result, "; %S", ast->InlineCmt->Content ); else - result.append( ";\n" ); + append( result, ";\n" ); } String CodeModule::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); to_string( result ); return result; } @@ -598,17 +598,17 @@ String CodeModule::to_string() void CodeModule::to_string( String& result ) { if (((u32(ModuleFlag::Export) & u32(ast->ModuleFlags)) == u32(ModuleFlag::Export))) - result.append("export "); + append( result, "export "); if (((u32(ModuleFlag::Import) & u32(ast->ModuleFlags)) == u32(ModuleFlag::Import))) - result.append("import "); + append( result, "import "); - result.append_fmt( "%S;\n", ast->Name ); + append_fmt( result, "%S;\n", ast->Name ); } String CodeNS::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); to_string( result ); return result; } @@ -616,14 +616,14 @@ String CodeNS::to_string() void CodeNS::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); - result.append_fmt( "namespace %S\n{\n%S\n}\n", ast->Name , ast->Body.to_string() ); + append_fmt( result, "namespace %S\n{\n%S\n}\n", ast->Name , ast->Body.to_string() ); } String CodeOperator::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); switch ( ast->Type ) { using namespace ECode; @@ -642,13 +642,13 @@ String CodeOperator::to_string() void CodeOperator::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); if ( ast->Attributes ) - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); if ( ast->Attributes ) - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); if ( ast->Specs ) { @@ -657,24 +657,24 @@ void CodeOperator::to_string_def( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } if ( ast->Attributes || ast->Specs ) { - result.append("\n" ); + append( result, "\n" ); } if ( ast->ReturnType ) - result.append_fmt( "%S %S (", ast->ReturnType.to_string(), ast->Name ); + append_fmt( result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); if ( ast->Params ) - result.append_fmt( "%S)", ast->Params.to_string() ); + append_fmt( result, "%S)", ast->Params.to_string() ); else - result.append( ")" ); + append( result, ")" ); if ( ast->Specs ) { @@ -683,12 +683,12 @@ void CodeOperator::to_string_def( String& result ) if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } - result.append_fmt( "\n{\n%S\n}\n" + append_fmt( result, "\n{\n%S\n}\n" , ast->Body.to_string() ); } @@ -696,10 +696,10 @@ void CodeOperator::to_string_def( String& result ) void CodeOperator::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); if ( ast->Attributes ) - result.append_fmt( "%S\n", ast->Attributes.to_string() ); + append_fmt( result, "%S\n", ast->Attributes.to_string() ); if ( ast->Specs ) { @@ -708,23 +708,23 @@ void CodeOperator::to_string_fwd( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } if ( ast->Attributes || ast->Specs ) { - result.append("\n" ); + append( result, "\n" ); } - result.append_fmt( "%S %S (", ast->ReturnType.to_string(), ast->Name ); + append_fmt( result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); if ( ast->Params ) - result.append_fmt( "%S)", ast->Params.to_string() ); + append_fmt( result, "%S)", ast->Params.to_string() ); else - result.append_fmt( ")" ); + append_fmt( result, ")" ); if ( ast->Specs ) { @@ -733,20 +733,20 @@ void CodeOperator::to_string_fwd( String& result ) if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } if ( ast->InlineCmt ) - result.append_fmt( "; %S", ast->InlineCmt->Content ); + append_fmt( result, "; %S", ast->InlineCmt->Content ); else - result.append( ";\n" ); + append( result, ";\n" ); } String CodeOpCast::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); switch ( ast->Type ) { using namespace ECode; @@ -769,32 +769,32 @@ void CodeOpCast::to_string_def( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - result.append_fmt( "%*s ", spec_str.Len, spec_str.Ptr ); + append_fmt( result, "%*s ", spec_str.Len, spec_str.Ptr ); } } - if ( ast->Name && ast->Name.length() ) - result.append_fmt( "%Soperator %S()", ast->Name, ast->ValueType.to_string() ); + if ( ast->Name && length(ast->Name) ) + append_fmt( result, "%Soperator %S()", ast->Name, ast->ValueType.to_string() ); else - result.append_fmt( "operator %S()", ast->ValueType.to_string() ); + append_fmt( result, "operator %S()", ast->ValueType.to_string() ); for ( SpecifierT spec : ast->Specs ) { if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - result.append_fmt( " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } - result.append_fmt( "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( result, "\n{\n%S\n}\n", ast->Body.to_string() ); return; } - if ( ast->Name && ast->Name.length() ) - result.append_fmt("%Soperator %S()\n{\n%S\n}\n", ast->Name, ast->ValueType.to_string(), ast->Body.to_string() ); + if ( ast->Name && length(ast->Name) ) + append_fmt( result, "%Soperator %S()\n{\n%S\n}\n", ast->Name, ast->ValueType.to_string(), ast->Body.to_string() ); else - result.append_fmt("operator %S()\n{\n%S\n}\n", ast->ValueType.to_string(), ast->Body.to_string() ); + append_fmt( result, "operator %S()\n{\n%S\n}\n", ast->ValueType.to_string(), ast->Body.to_string() ); } void CodeOpCast::to_string_fwd( String& result ) @@ -806,37 +806,37 @@ void CodeOpCast::to_string_fwd( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - result.append_fmt( "%*s ", spec_str.Len, spec_str.Ptr ); + append_fmt( result, "%*s ", spec_str.Len, spec_str.Ptr ); } } - result.append_fmt( "operator %S()", ast->ValueType.to_string() ); + append_fmt( result, "operator %S()", ast->ValueType.to_string() ); for ( SpecifierT spec : ast->Specs ) { if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - result.append_fmt( " %*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %*s", spec_str.Len, spec_str.Ptr ); } } if ( ast->InlineCmt ) - result.append_fmt( "; %S", ast->InlineCmt->Content ); + append_fmt( result, "; %S", ast->InlineCmt->Content ); else - result.append( ";\n" ); + append( result, ";\n" ); return; } if ( ast->InlineCmt ) - result.append_fmt("operator %S(); %S", ast->ValueType.to_string() ); + append_fmt( result, "operator %S(); %S", ast->ValueType.to_string() ); else - result.append_fmt("operator %S();\n", ast->ValueType.to_string() ); + append_fmt( result, "operator %S();\n", ast->ValueType.to_string() ); } String CodeParam::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); to_string( result ); return result; } @@ -846,41 +846,41 @@ void CodeParam::to_string( String& result ) if ( ast->Macro ) { // Related to parsing: ( , ... ) - result.append( ast->Macro.ast->Content ); + GEN_NS append( result, ast->Macro.ast->Content ); // Could also be: ( , ... ) } if ( ast->Name ) { if ( ast->ValueType.ast == nullptr ) - result.append_fmt( " %S", ast->Name ); + append_fmt( result, " %S", ast->Name ); else - result.append_fmt( " %S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( result, " %S %S", ast->ValueType.to_string(), ast->Name ); } else if ( ast->ValueType ) - result.append_fmt( " %S", ast->ValueType.to_string() ); + append_fmt( result, " %S", ast->ValueType.to_string() ); if ( ast->PostNameMacro ) { - result.append_fmt(" %S", ast->PostNameMacro.to_string() ); + append_fmt( result, " %S", ast->PostNameMacro.to_string() ); } if ( ast->Value ) - result.append_fmt( " = %S", ast->Value.to_string() ); + append_fmt( result, " = %S", ast->Value.to_string() ); if ( ast->NumEntries - 1 > 0 ) { for ( CodeParam param : ast->Next ) { - result.append_fmt( ", %S", param.to_string() ); + append_fmt( result, ", %S", param.to_string() ); } } } String CodePreprocessCond::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); switch ( ast->Type ) { using namespace ECode; @@ -908,49 +908,49 @@ String CodePreprocessCond::to_string() void CodePreprocessCond::to_string_if( String& result ) { - result.append_fmt( "#if %S\n", ast->Content ); + append_fmt( result, "#if %S\n", ast->Content ); } void CodePreprocessCond::to_string_ifdef( String& result ) { - result.append_fmt( "#ifdef %S\n", ast->Content ); + append_fmt( result, "#ifdef %S\n", ast->Content ); } void CodePreprocessCond::to_string_ifndef( String& result ) { - result.append_fmt( "#ifndef %S\n", ast->Content ); + append_fmt( result, "#ifndef %S\n", ast->Content ); } void CodePreprocessCond::to_string_elif( String& result ) { - result.append_fmt( "#elif %S\n", ast->Content ); + append_fmt( result, "#elif %S\n", ast->Content ); } void CodePreprocessCond::to_string_else( String& result ) { - result.append_fmt( "#else\n" ); + append_fmt( result, "#else\n" ); } void CodePreprocessCond::to_string_endif( String& result ) { - result.append_fmt( "#endif\n" ); + append_fmt( result, "#endif\n" ); } String CodePragma::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); to_string( result ); return result; } void CodePragma::to_string( String& result ) { - result.append_fmt( "#pragma %S\n", ast->Content ); + append_fmt( result, "#pragma %S\n", ast->Content ); } String CodeSpecifiers::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); to_string( result ); return result; } @@ -962,14 +962,14 @@ void CodeSpecifiers::to_string( String& result ) while ( left-- ) { StrC spec = ESpecifier::to_str( ast->ArrSpecs[idx] ); - result.append_fmt( "%.*s ", spec.Len, spec.Ptr ); + append_fmt( result, "%.*s ", spec.Len, spec.Ptr ); idx++; } } String CodeStruct::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); switch ( ast->Type ) { using namespace ECode; @@ -986,69 +986,69 @@ String CodeStruct::to_string() void CodeStruct::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); - result.append( "struct " ); + append( result, "struct " ); if ( ast->Attributes ) { - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); } if ( ast->ParentType ) { char const* access_level = to_str( ast->ParentAccess ); - result.append_fmt( "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); + append_fmt( result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); CodeType interface = ast->ParentType->Next->cast< CodeType >(); if ( interface ) - result.append( "\n" ); + append( result, "\n" ); while ( interface ) { - result.append_fmt( ", %S", interface.to_string() ); + append_fmt( result, ", %S", interface.to_string() ); interface = interface->Next ? interface->Next->cast< CodeType >() : CodeType { nullptr }; } } else if ( ast->Name ) { - result.append( ast->Name ); + append( result, ast->Name ); } if ( ast->InlineCmt ) { - result.append_fmt( " // %S", ast->InlineCmt->Content ); + append_fmt( result, " // %S", ast->InlineCmt->Content ); } - result.append_fmt( "\n{\n%S\n}", ast->Body.to_string() ); + append_fmt( result, "\n{\n%S\n}", ast->Body.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - result.append(";\n"); + append( result, ";\n"); } void CodeStruct::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); if ( ast->Attributes ) - result.append_fmt( "struct %S %S", ast->Attributes.to_string(), ast->Name ); + append_fmt( result, "struct %S %S", ast->Attributes.to_string(), ast->Name ); - else result.append_fmt( "struct %S", ast->Name ); + else append_fmt( result, "struct %S", ast->Name ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - result.append_fmt("; %S", ast->InlineCmt->Content ); + append_fmt( result, "; %S", ast->InlineCmt->Content ); else - result.append(";\n"); + append( result, ";\n"); } } String CodeTemplate::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); to_string( result ); return result; } @@ -1056,17 +1056,17 @@ String CodeTemplate::to_string() void CodeTemplate::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); if ( ast->Params ) - result.append_fmt( "template< %S >\n%S", ast->Params.to_string(), ast->Declaration.to_string() ); + append_fmt( result, "template< %S >\n%S", ast->Params.to_string(), ast->Declaration.to_string() ); else - result.append_fmt( "template<>\n%S", ast->Declaration.to_string() ); + append_fmt( result, "template<>\n%S", ast->Declaration.to_string() ); } String CodeTypedef::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); to_string( result ); return result; } @@ -1074,41 +1074,41 @@ String CodeTypedef::to_string() void CodeTypedef::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); - result.append( "typedef "); + append( result, "typedef "); // Determines if the typedef is a function typename if ( ast->UnderlyingType->ReturnType ) - result.append( ast->UnderlyingType.to_string() ); + append( result, ast->UnderlyingType.to_string() ); else - result.append_fmt( "%S %S", ast->UnderlyingType.to_string(), ast->Name ); + append_fmt( result, "%S %S", ast->UnderlyingType.to_string(), ast->Name ); if ( ast->UnderlyingType->Type == ECode::Typename && ast->UnderlyingType->ArrExpr ) { - result.append_fmt( "[ %S ];", ast->UnderlyingType->ArrExpr->to_string() ); + append_fmt( result, "[ %S ];", ast->UnderlyingType->ArrExpr->to_string() ); AST* next_arr_expr = ast->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) { - result.append_fmt( "[ %S ];", next_arr_expr->to_string() ); + append_fmt( result, "[ %S ];", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } else { - result.append( ";" ); + append( result, ";" ); } if ( ast->InlineCmt ) - result.append_fmt(" %S", ast->InlineCmt->Content); + append_fmt( result, " %S", ast->InlineCmt->Content); else - result.append("\n"); + append( result, "\n"); } String CodeType::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); to_string( result ); return result; } @@ -1119,13 +1119,13 @@ void CodeType::to_string( String& result ) if ( ast->ReturnType && ast->Params ) { if ( ast->Attributes ) - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); else { if ( ast->Specs ) - result.append_fmt( "%S ( %S ) ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); + append_fmt( result, "%S ( %S ) ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); else - result.append_fmt( "%S ( %S ) ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); + append_fmt( result, "%S ( %S ) ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); } break; @@ -1134,13 +1134,13 @@ void CodeType::to_string( String& result ) if ( ast->ReturnType && ast->Params ) { if ( ast->Attributes ) - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); else { if ( ast->Specs ) - result.append_fmt( "%S %S ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); + append_fmt( result, "%S %S ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); else - result.append_fmt( "%S %S ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); + append_fmt( result, "%S %S ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); } return; @@ -1148,20 +1148,20 @@ void CodeType::to_string( String& result ) #endif if ( ast->Attributes ) - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); if ( ast->Specs ) - result.append_fmt( "%S %S", ast->Name, ast->Specs.to_string() ); + append_fmt( result, "%S %S", ast->Name, ast->Specs.to_string() ); else - result.append_fmt( "%S", ast->Name ); + append_fmt( result, "%S", ast->Name ); if ( ast->IsParamPack ) - result.append("..."); + append( result, "..."); } String CodeUnion::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); to_string( result ); return result; } @@ -1169,16 +1169,16 @@ String CodeUnion::to_string() void CodeUnion::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); - result.append( "union " ); + append( result, "union " ); if ( ast->Attributes ) - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); if ( ast->Name ) { - result.append_fmt( "%S\n{\n%S\n}" + append_fmt( result, "%S\n{\n%S\n}" , ast->Name , ast->Body.to_string() ); @@ -1186,18 +1186,18 @@ void CodeUnion::to_string( String& result ) else { // Anonymous union - result.append_fmt( "\n{\n%S\n}" + append_fmt( result, "\n{\n%S\n}" , ast->Body.to_string() ); } if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - result.append(";\n"); + append( result, ";\n"); } String CodeUsing::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); switch ( ast->Type ) { using namespace ECode; @@ -1214,49 +1214,49 @@ String CodeUsing::to_string() void CodeUsing::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); if ( ast->Attributes ) - result.append_fmt( "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); if ( ast->UnderlyingType ) { - result.append_fmt( "using %S = %S", ast->Name, ast->UnderlyingType.to_string() ); + append_fmt( result, "using %S = %S", ast->Name, ast->UnderlyingType.to_string() ); if ( ast->UnderlyingType->ArrExpr ) { - result.append_fmt( "[ %S ]", ast->UnderlyingType->ArrExpr.to_string() ); + append_fmt( result, "[ %S ]", ast->UnderlyingType->ArrExpr.to_string() ); AST* next_arr_expr = ast->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) { - result.append_fmt( "[ %S ]", next_arr_expr->to_string() ); + append_fmt( result, "[ %S ]", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } - result.append( ";" ); + append( result, ";" ); } else - result.append_fmt( "using %S;", ast->Name ); + append_fmt( result, "using %S;", ast->Name ); if ( ast->InlineCmt ) - result.append_fmt(" %S\n", ast->InlineCmt->Content ); + append_fmt( result, " %S\n", ast->InlineCmt->Content ); else - result.append("\n"); + append( result, "\n"); } void CodeUsing::to_string_ns( String& result ) { if ( ast->InlineCmt ) - result.append_fmt( "using namespace $S; %S", ast->Name, ast->InlineCmt->Content ); + append_fmt( result, "using namespace $S; %S", ast->Name, ast->InlineCmt->Content ); else - result.append_fmt( "using namespace %s;\n", ast->Name ); + append_fmt( result, "using namespace %s;\n", ast->Name ); } String CodeVar::to_string() { - String result = String::make( GlobalAllocator, "" ); + String result = string_make( GlobalAllocator, "" ); to_string( result ); return result; } @@ -1268,18 +1268,18 @@ void CodeVar::to_string( String& result ) // Its a comma-separated variable ( a NextVar ) if ( ast->Specs ) - result.append_fmt( "%S ", ast->Specs.to_string() ); + append_fmt( result, "%S ", ast->Specs.to_string() ); - result.append( ast->Name ); + append( result, ast->Name ); if ( ast->ValueType->ArrExpr ) { - result.append_fmt( "[ %S ]", ast->ValueType->ArrExpr.to_string() ); + append_fmt( result, "[ %S ]", ast->ValueType->ArrExpr.to_string() ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - result.append_fmt( "[ %S ]", next_arr_expr->to_string() ); + append_fmt( result, "[ %S ]", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } @@ -1287,107 +1287,107 @@ void CodeVar::to_string( String& result ) if ( ast->Value ) { if ( ast->VarConstructorInit ) - result.append_fmt( "( %S ", ast->Value.to_string() ); + append_fmt( result, "( %S ", ast->Value.to_string() ); else - result.append_fmt( " = %S", ast->Value.to_string() ); + append_fmt( result, " = %S", ast->Value.to_string() ); } // Keep the chain going... if ( ast->NextVar ) - result.append_fmt( ", %S", ast->NextVar.to_string() ); + append_fmt( result, ", %S", ast->NextVar.to_string() ); if ( ast->VarConstructorInit ) - result.append( " )"); + append( result, " )"); return; } if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) - result.append( "export " ); + append( result, "export " ); if ( ast->Attributes || ast->Specs ) { if ( ast->Attributes ) - result.append_fmt( "%S ", ast->Specs.to_string() ); + append_fmt( result, "%S ", ast->Specs.to_string() ); if ( ast->Specs ) - result.append_fmt( "%S\n", ast->Specs.to_string() ); + append_fmt( result, "%S\n", ast->Specs.to_string() ); - result.append_fmt( "%S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( result, "%S %S", ast->ValueType.to_string(), ast->Name ); if ( ast->ValueType->ArrExpr ) { - result.append_fmt( "[ %S ]", ast->ValueType->ArrExpr.to_string() ); + append_fmt( result, "[ %S ]", ast->ValueType->ArrExpr.to_string() ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - result.append_fmt( "[ %S ]", next_arr_expr->to_string() ); + append_fmt( result, "[ %S ]", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } if ( ast->BitfieldSize ) - result.append_fmt( " : %S", ast->BitfieldSize.to_string() ); + append_fmt( result, " : %S", ast->BitfieldSize.to_string() ); if ( ast->Value ) { if ( ast->VarConstructorInit ) - result.append_fmt( "( %S ", ast->Value.to_string() ); + append_fmt( result, "( %S ", ast->Value.to_string() ); else - result.append_fmt( " = %S", ast->Value.to_string() ); + append_fmt( result, " = %S", ast->Value.to_string() ); } if ( ast->NextVar ) - result.append_fmt( ", %S", ast->NextVar.to_string() ); + append_fmt( result, ", %S", ast->NextVar.to_string() ); if ( ast->VarConstructorInit ) - result.append( " )"); + append( result, " )"); if ( ast->InlineCmt ) - result.append_fmt("; %S", ast->InlineCmt->Content); + append_fmt( result, "; %S", ast->InlineCmt->Content); else - result.append( ";\n" ); + append( result, ";\n" ); return; } if ( ast->BitfieldSize ) - result.append_fmt( "%S %S : %S", ast->ValueType.to_string(), ast->Name, ast->BitfieldSize.to_string() ); + append_fmt( result, "%S %S : %S", ast->ValueType.to_string(), ast->Name, ast->BitfieldSize.to_string() ); else if ( ast->ValueType->ArrExpr ) { - result.append_fmt( "%S %S[ %S ]", ast->ValueType.to_string(), ast->Name, ast->ValueType->ArrExpr.to_string() ); + append_fmt( result, "%S %S[ %S ]", ast->ValueType.to_string(), ast->Name, ast->ValueType->ArrExpr.to_string() ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - result.append_fmt( "[ %S ]", next_arr_expr->to_string() ); + append_fmt( result, "[ %S ]", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } else - result.append_fmt( "%S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( result, "%S %S", ast->ValueType.to_string(), ast->Name ); if ( ast->Value ) { if ( ast->VarConstructorInit ) - result.append_fmt( "( %S ", ast->Value.to_string() ); + append_fmt( result, "( %S ", ast->Value.to_string() ); else - result.append_fmt( " = %S", ast->Value.to_string() ); + append_fmt( result, " = %S", ast->Value.to_string() ); } if ( ast->NextVar ) - result.append_fmt( ", %S", ast->NextVar.to_string() ); + append_fmt( result, ", %S", ast->NextVar.to_string() ); if ( ast->VarConstructorInit ) - result.append( " )"); + append( result, " )"); - result.append( ";" ); + append( result, ";" ); if ( ast->InlineCmt ) - result.append_fmt(" %S", ast->InlineCmt->Content); + append_fmt( result, " %S", ast->InlineCmt->Content); else - result.append("\n"); + append( result, "\n"); } diff --git a/project/components/interface.cpp b/project/components/interface.cpp index 5aadb92..2d050ce 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -74,26 +74,26 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s internal void define_constants() { - Code::Global = make_code(); - Code::Global->Name = get_cached_string( txt("Global Code") ); - Code::Global->Content = Code::Global->Name; + Code::Global = make_code(); + scast(String, Code::Global->Name) = get_cached_string( txt("Global Code") ); + scast(String, Code::Global->Content) = Code::Global->Name; Code::Invalid = make_code(); Code::Invalid.set_global(); - t_empty = (CodeType) make_code(); - t_empty->Type = ECode::Typename; - t_empty->Name = get_cached_string( txt("") ); + t_empty = (CodeType) make_code(); + t_empty->Type = ECode::Typename; + scast(String, t_empty->Name) = get_cached_string( txt("") ); t_empty.set_global(); - access_private = make_code(); - access_private->Type = ECode::Access_Private; - access_private->Name = get_cached_string( txt("private:\n") ); + access_private = make_code(); + access_private->Type = ECode::Access_Private; + scast(String, access_private->Name) = get_cached_string( txt("private:\n") ); access_private.set_global(); - access_protected = make_code(); - access_protected->Type = ECode::Access_Protected; - access_protected->Name = get_cached_string( txt("protected:\n") ); + access_protected = make_code(); + access_protected->Type = ECode::Access_Protected; + scast(String, access_protected->Name) = get_cached_string( txt("protected:\n") ); access_protected.set_global(); access_public = make_code(); @@ -398,7 +398,7 @@ StringCached get_cached_string( StrC str ) return * result; } - String result = String::make( get_string_allocator( str.Len ), str ); + String result = string_make( get_string_allocator( str.Len ), str ); set(StringCache, key, result ); return result; diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index f0ca08f..104e1be 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -458,7 +458,7 @@ CodeComment def_comment( StrC content ) static char line[ MaxCommentLineLength ]; - String cmt_formatted = String::make_reserve( GlobalAllocator, kilobytes(1) ); + String cmt_formatted = string_make_reserve( GlobalAllocator, kilobytes(1) ); char const* end = content.Ptr + content.Len; char const* scanner = content.Ptr; s32 curr = 0; @@ -474,15 +474,15 @@ CodeComment def_comment( StrC content ) length++; str_copy( line, scanner, length ); - cmt_formatted.append_fmt( "//%.*s", length, line ); + append_fmt(cmt_formatted, "//%.*s", length, line ); mem_set( line, 0, MaxCommentLineLength ); scanner += length; } while ( scanner <= end ); - if ( cmt_formatted.back() != '\n' ) - cmt_formatted.append( "\n" ); + if ( back(cmt_formatted) != '\n' ) + append( cmt_formatted, "\n" ); Code result = make_code(); @@ -490,7 +490,7 @@ CodeComment def_comment( StrC content ) result->Name = get_cached_string( cmt_formatted ); result->Content = result->Name; - cmt_formatted.free(); + free(cmt_formatted); return (CodeComment) result; } diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index fe8cc94..f2fe4d3 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -89,11 +89,11 @@ struct Token String to_string() { - String result = String::make_reserve( GlobalAllocator, kilobytes(4) ); + String result = string_make_reserve( GlobalAllocator, kilobytes(4) ); StrC type_str = ETokType::to_str( Type ); - result.append_fmt( "Line: %d Column: %d, Type: %.*s Content: %.*s" + append_fmt( result, "Line: %d Column: %d, Type: %.*s Content: %.*s" , Line, Column , type_str.Len, type_str.Ptr , Length, Text @@ -352,7 +352,7 @@ s32 lex_preprocessor_directive( if ( current != '"' && current != '<' ) { - String directive_str = String::fmt_buf( GlobalAllocator, "%.*s", min( 80, left + preprocess_content.Length ), token.Text ); + String directive_str = string_fmt_buf( GlobalAllocator, "%.*s", min( 80, left + preprocess_content.Length ), token.Text ); log_failure( "gen::Parser::lex: Expected '\"' or '<' after #include, not '%c' (%d, %d)\n%s" , current @@ -419,8 +419,8 @@ s32 lex_preprocessor_directive( } else { - String directive_str = String::make_length( GlobalAllocator, token.Text, token.Length ); - String content_str = String::fmt_buf( GlobalAllocator, "%.*s", min( 400, left + preprocess_content.Length ), preprocess_content.Text ); + String directive_str = string_make_length( GlobalAllocator, token.Text, token.Length ); + String content_str = string_fmt_buf( GlobalAllocator, "%.*s", min( 400, left + preprocess_content.Length ), preprocess_content.Text ); log_failure( "gen::Parser::lex: Invalid escape sequence '\\%c' (%d, %d)" " in preprocessor directive '%s' (%d, %d)\n%s" @@ -586,7 +586,7 @@ TokArray lex( StrC content ) { s32 length = 0; char const* scanner = entry.Data; - while ( entry.length() > length && (char_is_alphanumeric( *scanner ) || *scanner == '_') ) + while ( GEN_NS length(entry) > length && (char_is_alphanumeric( *scanner ) || *scanner == '_') ) { scanner++; length ++; @@ -678,7 +678,7 @@ TokArray lex( StrC content ) } else { - String context_str = String::fmt_buf( GlobalAllocator, "%s", scanner, min( 100, left ) ); + String context_str = string_fmt_buf( GlobalAllocator, "%s", scanner, min( 100, left ) ); log_failure( "gen::lex: invalid varadic argument, expected '...' got '..%c' (%d, %d)\n%s", current, line, column, context_str ); } @@ -1239,7 +1239,7 @@ TokArray lex( StrC content ) ); } - String context_str = String::fmt_buf( GlobalAllocator, "%.*s", min( 100, left ), scanner ); + String context_str = string_fmt_buf( GlobalAllocator, "%.*s", min( 100, left ), scanner ); log_failure( "Failed to lex token '%c' (%d, %d)\n%s", current, line, column, context_str ); // Skip to next whitespace since we can't know if anything else is valid until then. diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 6ed1d85..b9f6a6b 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -45,7 +45,7 @@ struct ParseContext String to_string() { - String result = String::make_reserve( GlobalAllocator, kilobytes(4) ); + String result = string_make_reserve( GlobalAllocator, kilobytes(4) ); Token scope_start = Scope->Start; Token last_valid = Tokens.Idx >= num(Tokens.Arr) ? Tokens.Arr[num(Tokens.Arr) -1] : Tokens.current(); @@ -58,18 +58,18 @@ struct ParseContext length++; } - String line = String::make( GlobalAllocator, { length, scope_start.Text } ); - result.append_fmt("\tScope : %s\n", line ); - line.free(); + String line = string_make( GlobalAllocator, { length, scope_start.Text } ); + append_fmt( result, "\tScope : %s\n", line ); + free(line); sptr dist = (sptr)last_valid.Text - (sptr)scope_start.Text + 2; sptr length_from_err = dist; - String line_from_err = String::make( GlobalAllocator, { length_from_err, last_valid.Text } ); + String line_from_err = string_make( GlobalAllocator, { length_from_err, last_valid.Text } ); if ( length_from_err < 100 ) - result.append_fmt("\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); + append_fmt(result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); else - result.append_fmt("\t(%d, %d)\n", last_valid.Line, last_valid.Column ); + append_fmt(result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column ); StackNode* curr_scope = Scope; s32 level = 0; @@ -77,11 +77,11 @@ struct ParseContext { if ( curr_scope->Name ) { - result.append_fmt("\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text ); + append_fmt(result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text ); } else { - result.append_fmt("\t%d: %s\n", level, curr_scope->ProcName.Ptr ); + append_fmt(result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr ); } curr_scope = curr_scope->Prev; @@ -243,7 +243,7 @@ constexpr bool strip_formatting_dont_preserve_newlines = false; internal String strip_formatting( StrC raw_text, bool preserve_newlines = true ) { - String content = String::make_reserve( GlobalAllocator, raw_text.Len ); + String content = string_make_reserve( GlobalAllocator, raw_text.Len ); if ( raw_text.Len == 0 ) return content; @@ -290,7 +290,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if ( tokleft ) move_fwd(); - content.append( cut_ptr, cut_length ); + append( content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } @@ -312,7 +312,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if ( tokleft ) move_fwd(); - content.append( cut_ptr, cut_length ); + append( content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } @@ -326,7 +326,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) scanner += 2; tokleft -= 2; - content.append( cut_ptr, cut_length ); + append( content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } @@ -345,7 +345,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if (tokleft) move_fwd(); - content.append( cut_ptr, cut_length ); + append( content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } @@ -354,10 +354,10 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if (scanner[0] == '\t') { if (pos > last_cut) - content.append(cut_ptr, cut_length); + append( content, cut_ptr, cut_length); - if ( content.back() != ' ' ) - content.append(' '); + if ( back( content ) != ' ' ) + append( content, ' '); move_fwd(); last_cut = sptr(scanner) - sptr(raw_text.Ptr); @@ -373,17 +373,17 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) scanner += 2; tokleft -= 2; - content.append( cut_ptr, cut_length ); + append( content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } if ( pos > last_cut ) - content.append( cut_ptr, cut_length ); + append( content, cut_ptr, cut_length ); // Replace with a space - if ( content.back() != ' ' ) - content.append( ' ' ); + if ( back( content ) != ' ' ) + append( content, ' ' ); scanner += 2; tokleft -= 2; @@ -400,17 +400,17 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) move_fwd(); - content.append( cut_ptr, cut_length ); + append( content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } if ( pos > last_cut ) - content.append( cut_ptr, cut_length ); + append( content, cut_ptr, cut_length ); // Replace with a space - if ( content.back() != ' ' ) - content.append( ' ' ); + if ( back( content ) != ' ' ) + append( content, ' ' ); move_fwd(); @@ -421,7 +421,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) // Escaped newlines if ( scanner[0] == '\\' ) { - content.append( cut_ptr, cut_length ); + append( content, cut_ptr, cut_length ); s32 amount_to_skip = 1; if ( tokleft > 1 && scanner[1] == '\n' ) @@ -448,7 +448,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) // Consectuive spaces if ( tokleft > 1 && char_is_space( scanner[0] ) && char_is_space( scanner[ 1 ] ) ) { - content.append( cut_ptr, cut_length ); + append( content, cut_ptr, cut_length ); do { move_fwd(); @@ -458,8 +458,8 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); // Preserve only 1 space of formattting - if ( content.back() != ' ' ) - content.append( ' ' ); + if ( back( content ) != ' ' ) + append( content, ' ' ); continue; } @@ -469,7 +469,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if ( last_cut < raw_text.Len ) { - content.append( cut_ptr, raw_text.Len - last_cut ); + append( content, cut_ptr, raw_text.Len - last_cut ); } #undef cut_ptr @@ -1039,8 +1039,8 @@ CodeBody parse_class_struct_body( TokType which, Token name ) if ( attributes ) { - String fused = String::make_reserve( GlobalAllocator, attributes->Content.length() + more_attributes->Content.length() ); - fused.append_fmt( "%S %S", attributes->Content, more_attributes->Content ); + String fused = string_make_reserve( GlobalAllocator, length(attributes->Content) + length(more_attributes->Content) ); + append_fmt( fused, "%S %S", attributes->Content, more_attributes->Content ); attributes->Name = get_cached_string(fused); attributes->Content = attributes->Name; @@ -1484,8 +1484,8 @@ CodeFn parse_function_after_name( using namespace ECode; String - name_stripped = String::make( GlobalAllocator, name ); - name_stripped.strip_space(); + name_stripped = string_make( GlobalAllocator, name ); + strip_space(name_stripped); CodeFn result = (CodeFn) make_code(); @@ -4114,7 +4114,7 @@ CodeOpCast parse_operator_cast( CodeSpecifiers specifiers ) Code type = parse_type(); // :: ... operator - Context.Scope->Name = { type->Name.Data, type->Name.length() }; + Context.Scope->Name = { type->Name.Data, length(type->Name) }; eat( TokType::Capture_Start ); eat( TokType::Capture_End ); diff --git a/project/dependencies/parsing.cpp b/project/dependencies/parsing.cpp index eee923e..4ab0175 100644 --- a/project/dependencies/parsing.cpp +++ b/project/dependencies/parsing.cpp @@ -1099,10 +1099,10 @@ String csv_write_string_delimiter( AllocatorInfo a, CSV_Object* obj, char delimi FileInfo tmp; file_stream_new( &tmp, a ); csv_write_delimiter( &tmp, obj, delimiter ); - + ssize fsize; u8* buf = file_stream_buf( &tmp, &fsize ); - String output = String::make_length( a, ( char* )buf, fsize ); + String output = string_make_length( a, ( char* )buf, fsize ); file_close( &tmp ); return output; } diff --git a/project/dependencies/platform.hpp b/project/dependencies/platform.hpp index e1e358f..272dadb 100644 --- a/project/dependencies/platform.hpp +++ b/project/dependencies/platform.hpp @@ -1,5 +1,3 @@ -#define GEN_SUPPORT_CPP_MEMBER_FEATURES 0 - #ifdef GEN_INTELLISENSE_DIRECTIVES # pragma once #endif diff --git a/project/dependencies/printing.cpp b/project/dependencies/printing.cpp index 09abe2c..b47fddb 100644 --- a/project/dependencies/printing.cpp +++ b/project/dependencies/printing.cpp @@ -422,7 +422,7 @@ neverinline ssize str_fmt_va( char* text, ssize max_len, char const* fmt, va_lis { String gen_str = String { va_arg( va, char*) }; - info.precision = gen_str.length(); + info.precision = length(gen_str); len = _print_string( text, remaining, &info, gen_str ); } break; diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index 487ede5..07808cc 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -8,7 +8,7 @@ // Constant string with length. struct StrC { - ssize Len; + ssize Len; char const* Ptr; operator char const* () const { return Ptr; } @@ -28,10 +28,14 @@ StrC to_str( char const* str ) { // They used a header pattern // I kept it for simplicty of porting but its not necessary to keep it that way. #pragma region String -struct String; struct StringHeader; -// Forward declarations for all file-scope functions +#if GEN_COMPILER_C +typedef char* String; +#else +struct String; +#endif + String string_make(AllocatorInfo allocator, char const* str); String string_make(AllocatorInfo allocator, StrC str); String string_make_reserve(AllocatorInfo allocator, ssize capacity); @@ -73,11 +77,33 @@ struct StringHeader { ssize Length; }; +#if ! GEN_COMPILER_C struct String { char* Data; -#if 1 + forceinline operator bool() { return Data != nullptr; } + forceinline operator char*() { return Data; } + forceinline operator char const*() const { return Data; } + forceinline operator StrC() const { return { length(* this), Data }; } + + String const& operator=(String const& other) const { + if (this == &other) + return *this; + + String* this_ = ccast(String*, this); + this_->Data = other.Data; + + return *this; + } + + forceinline char& operator[](ssize index) { return Data[index]; } + forceinline char const& operator[](ssize index) const { return Data[index]; } + + forceinline char* begin() const { return Data; } + forceinline char* end() const { return Data + length(* this); } + +#if GEN_SUPPORT_CPP_MEMBER_FEATURES #pragma region Member Mapping forceinline static String make(AllocatorInfo allocator, char const* str) { return GEN_NS string_make(allocator, str); } forceinline static String make(AllocatorInfo allocator, StrC str) { return GEN_NS string_make(allocator, str); } @@ -143,30 +169,14 @@ struct String return GEN_NS append(*this, buf, res); } - - forceinline operator bool() { return Data != nullptr; } - forceinline operator char*() { return Data; } - forceinline operator char const*() const { return Data; } - forceinline operator StrC() const { return { length(), Data }; } - - String const& operator=(String const& other) const { - if (this == &other) - return *this; - - String* this_ = ccast(String*, this); - this_->Data = other.Data; - - return *this; - } - - forceinline char& operator[](ssize index) { return Data[index]; } - forceinline char const& operator[](ssize index) const { return Data[index]; } - - forceinline char* begin() const { return Data; } - forceinline char* end() const { return Data + length(); } #pragma endregion Member Mapping #endif }; +#endif + +inline char* begin(String& str) { return str; } +inline char* end(String& str) { return scast(char*, str) + length(str); } +inline char* next(String& str) { return scast(char*, str) + 1; } inline usize string_grow_formula(usize value) { @@ -247,9 +257,9 @@ bool append(String& str, char const* str_to_append, ssize append_length) StringHeader& header = get_header(str); - mem_copy(str.Data + curr_len, str_to_append, append_length); + mem_copy( scast(char*, str) + curr_len, str_to_append, append_length); - str.Data[curr_len + append_length] = '\0'; + str[curr_len + append_length] = '\0'; header.Length = curr_len + append_length; } @@ -263,7 +273,19 @@ bool append(String& str, StrC str_to_append) { inline bool append(String& str, const String other) { - return append(str, other.Data, length(other)); + return append(str, other, length(other)); +} + +bool append_fmt(String& str, char const* fmt, ...) { + ssize res; + char buf[GEN_PRINTF_MAXLEN] = { 0 }; + + va_list va; + va_start(va, fmt); + res = str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1; + va_end(va); + + return append(str, buf, res); } inline @@ -294,7 +316,7 @@ bool are_equal(String lhs, StrC rhs) inline ssize avail_space(String const& str) { - StringHeader const& header = *rcast(StringHeader const*, str.Data - sizeof(StringHeader)); + StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); return header.Capacity - header.Length; } @@ -306,7 +328,7 @@ char& back(String& str) { inline bool contains(String const& str, StrC substring) { - StringHeader const& header = *rcast(StringHeader const*, str.Data - sizeof(StringHeader)); + StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); if (substring.Len > header.Length) return false; @@ -326,7 +348,7 @@ bool contains(String const& str, StrC substring) inline bool contains(String const& str, String const& substring) { - StringHeader const& header = *rcast(StringHeader const*, str.Data - sizeof(StringHeader)); + StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); if (length(substring) > header.Length) return false; @@ -345,7 +367,7 @@ bool contains(String const& str, String const& substring) inline ssize capacity(String const& str) { - StringHeader const& header = *rcast(StringHeader const*, str.Data - sizeof(StringHeader)); + StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); return header.Capacity; } @@ -356,7 +378,7 @@ void clear(String& str) { inline String duplicate(String const& str, AllocatorInfo allocator) { - return string_make_length(allocator, str.Data, length(str)); + return string_make_length(allocator, str, length(str)); } inline @@ -376,7 +398,7 @@ StringHeader& get_header(String& str) { inline ssize length(String const& str) { - StringHeader const& header = *rcast(StringHeader const*, str.Data - sizeof(StringHeader)); + StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); return header.Length; } @@ -511,10 +533,10 @@ void trim_space(String& str) { inline String visualize_whitespace(String const& str) { - StringHeader* header = (StringHeader*)(str.Data - sizeof(StringHeader)); + StringHeader* header = (StringHeader*)(scast(char const*, str) - sizeof(StringHeader)); String result = string_make_reserve(header->Allocator, length(str) * 2); // Assume worst case for space requirements. - for (char c : str) switch (c) + for (auto c : str) switch (c) { case ' ': append(result, txt("·")); @@ -549,10 +571,10 @@ struct String_POD { static_assert( sizeof( String_POD ) == sizeof( String ), "String is not a POD" ); // Implements basic string interning. Data structure is based off the ZPL Hashtable. -using StringTable = HashTable; +typedef HashTable StringTable; // Represents strings cached with the string table. // Should never be modified, if changed string is desired, cache_string( str ) another. -using StringCached = String const; +typedef String const StringCached; #pragma endregion Strings diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index fde3a3d..07ef316 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -20,14 +20,14 @@ CodeBody gen_ecode( char const* path ) Array enum_strs = csv_nodes.nodes[0].nodes; - String enum_entries = String::make_reserve( GlobalAllocator, kilobytes(1) ); - String to_str_entries = String::make_reserve( GlobalAllocator, kilobytes(1) ); + String enum_entries = string_make_reserve( GlobalAllocator, kilobytes(1) ); + String to_str_entries = string_make_reserve( GlobalAllocator, kilobytes(1) ); for ( ADT_Node node : enum_strs ) { char const* code = node.string; - enum_entries.append_fmt( "%s,\n", code ); - to_str_entries.append_fmt( "{ sizeof(\"%s\"), \"%s\" },\n", code, code ); + append_fmt( enum_entries, "%s,\n", code ); + append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", code, code ); } CodeEnum enum_code = parse_enum(gen::token_fmt_impl((3 + 1) / 2, "entries", (StrC)enum_entries, "enum Type : u32 { NumTypes };")); @@ -67,16 +67,16 @@ CodeBody gen_eoperator( char const* path ) Array enum_strs = csv_nodes.nodes[0].nodes; Array str_strs = csv_nodes.nodes[1].nodes; - String enum_entries = String::make_reserve( GlobalAllocator, kilobytes(1) ); - String to_str_entries = String::make_reserve( GlobalAllocator, kilobytes(1) ); + String enum_entries = string_make_reserve( GlobalAllocator, kilobytes(1) ); + String to_str_entries = string_make_reserve( GlobalAllocator, kilobytes(1) ); for (usize idx = 0; idx < num(enum_strs); idx++) { char const* enum_str = enum_strs[idx].string; char const* entry_to_str = str_strs [idx].string; - enum_entries.append_fmt( "%s,\n", enum_str ); - to_str_entries.append_fmt( "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + append_fmt( enum_entries, "%s,\n", enum_str ); + append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } CodeEnum enum_code = parse_enum(token_fmt("entries", (StrC)enum_entries, stringize( @@ -123,16 +123,16 @@ CodeBody gen_especifier( char const* path ) Array enum_strs = csv_nodes.nodes[0].nodes; Array str_strs = csv_nodes.nodes[1].nodes; - String enum_entries = String::make_reserve( GlobalAllocator, kilobytes(1) ); - String to_str_entries = String::make_reserve( GlobalAllocator, kilobytes(1) ); + String enum_entries = string_make_reserve( GlobalAllocator, kilobytes(1) ); + String to_str_entries = string_make_reserve( GlobalAllocator, kilobytes(1) ); for (usize idx = 0; idx < num(enum_strs); idx++) { char const* enum_str = enum_strs[idx].string; char const* entry_to_str = str_strs [idx].string; - enum_entries.append_fmt( "%s,\n", enum_str ); - to_str_entries.append_fmt( "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + append_fmt( enum_entries, "%s,\n", enum_str ); + append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } CodeEnum enum_code = parse_enum(token_fmt("entries", (StrC)enum_entries, stringize( @@ -237,19 +237,19 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) Array attribute_strs = csv_attr_nodes.nodes[0].nodes; Array attribute_str_strs = csv_attr_nodes.nodes[1].nodes; - String enum_entries = String::make_reserve( GlobalAllocator, kilobytes(2) ); - String to_str_entries = String::make_reserve( GlobalAllocator, kilobytes(4) ); - String attribute_entries = String::make_reserve( GlobalAllocator, kilobytes(2) ); - String to_str_attributes = String::make_reserve( GlobalAllocator, kilobytes(4) ); - String attribute_define_entries = String::make_reserve( GlobalAllocator, kilobytes(4) ); + String enum_entries = string_make_reserve( GlobalAllocator, kilobytes(2) ); + String to_str_entries = string_make_reserve( GlobalAllocator, kilobytes(4) ); + String attribute_entries = string_make_reserve( GlobalAllocator, kilobytes(2) ); + String to_str_attributes = string_make_reserve( GlobalAllocator, kilobytes(4) ); + String attribute_define_entries = string_make_reserve( GlobalAllocator, kilobytes(4) ); for (usize idx = 0; idx < num(enum_strs); idx++) { char const* enum_str = enum_strs[idx].string; char const* entry_to_str = enum_str_strs [idx].string; - enum_entries.append_fmt( "%s,\n", enum_str ); - to_str_entries.append_fmt( "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + append_fmt( enum_entries, "%s,\n", enum_str ); + append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } for ( usize idx = 0; idx < num(attribute_strs); idx++ ) @@ -257,14 +257,14 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) char const* attribute_str = attribute_strs[idx].string; char const* entry_to_str = attribute_str_strs [idx].string; - attribute_entries.append_fmt( "Attribute_%s,\n", attribute_str ); - to_str_attributes.append_fmt( "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); - attribute_define_entries.append_fmt( "Entry( Attribute_%s, \"%s\" )", attribute_str, entry_to_str ); + append_fmt( attribute_entries, "Attribute_%s,\n", attribute_str ); + append_fmt( to_str_attributes, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + append_fmt( attribute_define_entries, "Entry( Attribute_%s, \"%s\" )", attribute_str, entry_to_str ); if ( idx < num(attribute_strs) - 1 ) - attribute_define_entries.append( " \\\n"); + append( attribute_define_entries, " \\\n"); else - attribute_define_entries.append( "\n"); + append( attribute_define_entries, "\n"); } #pragma push_macro("GEN_DEFINE_ATTRIBUTE_TOKENS") From ed0c0422ad91f708fa217c95f3353e082aa5f8ee Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 05:30:37 -0500 Subject: [PATCH 019/112] Looking into what the library's convention for enums will be. Most likely will just reduce them to C-enums with underlying type. Otherwise there has to be a mechanism to drop the defs down to them anyways, and eliminate the namespace wraps. --- .vscode/settings.json | 3 +- gen_c_library/c_library.cpp | 1 + project/bootstrap.cpp | 1 + project/components/ast.hpp | 3 +- project/components/ast_types.hpp | 4 +- project/components/code_serialization.cpp | 46 +++++++------ project/components/inlines.hpp | 4 +- project/components/interface.cpp | 22 +++--- project/components/interface.hpp | 32 ++++----- project/components/interface.upfront.cpp | 12 ++-- project/components/parser.cpp | 66 +++++++++++------- project/components/types.hpp | 82 ++++++++++++----------- project/dependencies/macros.hpp | 10 +++ project/dependencies/platform.hpp | 33 ++++++--- project/dependencies/strings.hpp | 4 +- 15 files changed, 195 insertions(+), 128 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 87d304b..3a32ceb 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -39,7 +39,8 @@ "raylib.h": "c", "*.m": "cpp", "atomic": "cpp", - "gen.h": "c" + "gen.h": "c", + "string_ops.hpp": "c" }, "C_Cpp.intelliSenseEngineFallback": "disabled", "mesonbuild.configureOnOpen": true, diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 6073696..ee154ea 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -1,6 +1,7 @@ #define GEN_DEFINE_LIBRARY_CODE_CONSTANTS #define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_EXPOSE_BACKEND +#define GEN_SUPPORT_CPP_MEMBER_FEATURES 1 #include "../project/gen.cpp" #include "helpers/push_ignores.inline.hpp" diff --git a/project/bootstrap.cpp b/project/bootstrap.cpp index 3214183..ffa783b 100644 --- a/project/bootstrap.cpp +++ b/project/bootstrap.cpp @@ -1,6 +1,7 @@ #define GEN_DEFINE_LIBRARY_CODE_CONSTANTS #define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_EXPOSE_BACKEND +#define GEN_SUPPORT_CPP_MEMBER_FEATURES 0 #include "gen.cpp" #include "helpers/push_ignores.inline.hpp" diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 17bbd75..026ed4b 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -376,7 +376,8 @@ struct AST OperatorT Op; AccessSpec ParentAccess; s32 NumEntries; - s32 VarConstructorInit; // Used by variables to know that initialization is using a constructor expression instead of an assignment expression. + s32 VarConstructorInit; // Used by variables to know that initialization is using a constructor expression instead of an assignment expression. + b32 EnumUnderlyingMacro; // Used by enums incase the user wants to wrap underlying type specification in a macro }; }; diff --git a/project/components/ast_types.hpp b/project/components/ast_types.hpp index 04817ca..e4e5295 100644 --- a/project/components/ast_types.hpp +++ b/project/components/ast_types.hpp @@ -174,7 +174,7 @@ struct AST_Enum CodeAttributes Attributes; char _PAD_SPEC_ [ sizeof(AST*) ]; CodeType UnderlyingType; - char _PAD_PARAMS_[ sizeof(AST*) ]; + Code UnderlyingTypeMacro; CodeBody Body; char _PAD_PROPERTIES_2_[ sizeof(AST*) ]; }; @@ -186,7 +186,7 @@ struct AST_Enum StringCached Name; CodeT Type; ModuleFlag ModuleFlags; - char _PAD_UNUSED_[ sizeof(u32) ]; + b32 EnumUnderlyingMacro; }; static_assert( sizeof(AST_Enum) == sizeof(AST), "ERROR: AST_Enum is not the same size as AST"); diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 1597e38..0a1df21 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -160,7 +160,7 @@ String CodeClass::to_string() void CodeClass::to_string_def( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); append( result, "class " ); @@ -204,7 +204,7 @@ void CodeClass::to_string_def( String& result ) void CodeClass::to_string_fwd( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); if ( ast->Attributes ) @@ -314,7 +314,7 @@ String CodeEnum::to_string() void CodeEnum::to_string_def( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); if ( ast->Attributes || ast->UnderlyingType ) @@ -330,6 +330,12 @@ void CodeEnum::to_string_def( String& result ) , ast->UnderlyingType.to_string() , ast->Body.to_string() ); + else if ( ast->UnderlyingTypeMacro ) + append_fmt( result, "%S : %S\n{\n%S\n}" + , ast->Name + , ast->UnderlyingTypeMacro.to_string() + , ast->Body.to_string() + ); else append_fmt( result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); } @@ -341,7 +347,7 @@ void CodeEnum::to_string_def( String& result ) void CodeEnum::to_string_fwd( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); if ( ast->Attributes ) @@ -360,7 +366,7 @@ void CodeEnum::to_string_fwd( String& result ) void CodeEnum::to_string_class_def( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); if ( ast->Attributes || ast->UnderlyingType ) @@ -392,7 +398,7 @@ void CodeEnum::to_string_class_def( String& result ) void CodeEnum::to_string_class_fwd( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); append( result, "enum class " ); @@ -474,7 +480,7 @@ String CodeFn::to_string() void CodeFn::to_string_def( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export" ); if ( ast->Attributes ) @@ -527,7 +533,7 @@ void CodeFn::to_string_def( String& result ) void CodeFn::to_string_fwd( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); if ( ast->Attributes ) @@ -597,10 +603,10 @@ String CodeModule::to_string() void CodeModule::to_string( String& result ) { - if (((u32(ModuleFlag::Export) & u32(ast->ModuleFlags)) == u32(ModuleFlag::Export))) + if (((u32(ModuleFlag_Export) & u32(ast->ModuleFlags)) == u32(ModuleFlag_Export))) append( result, "export "); - if (((u32(ModuleFlag::Import) & u32(ast->ModuleFlags)) == u32(ModuleFlag::Import))) + if (((u32(ModuleFlag_Import) & u32(ast->ModuleFlags)) == u32(ModuleFlag_Import))) append( result, "import "); append_fmt( result, "%S;\n", ast->Name ); @@ -615,7 +621,7 @@ String CodeNS::to_string() void CodeNS::to_string( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); append_fmt( result, "namespace %S\n{\n%S\n}\n", ast->Name , ast->Body.to_string() ); @@ -641,7 +647,7 @@ String CodeOperator::to_string() void CodeOperator::to_string_def( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); if ( ast->Attributes ) @@ -695,7 +701,7 @@ void CodeOperator::to_string_def( String& result ) void CodeOperator::to_string_fwd( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); if ( ast->Attributes ) @@ -985,7 +991,7 @@ String CodeStruct::to_string() void CodeStruct::to_string_def( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); append( result, "struct " ); @@ -1029,7 +1035,7 @@ void CodeStruct::to_string_def( String& result ) void CodeStruct::to_string_fwd( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); if ( ast->Attributes ) @@ -1055,7 +1061,7 @@ String CodeTemplate::to_string() void CodeTemplate::to_string( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); if ( ast->Params ) @@ -1073,7 +1079,7 @@ String CodeTypedef::to_string() void CodeTypedef::to_string( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); append( result, "typedef "); @@ -1168,7 +1174,7 @@ String CodeUnion::to_string() void CodeUnion::to_string( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); append( result, "union " ); @@ -1213,7 +1219,7 @@ String CodeUsing::to_string() void CodeUsing::to_string( String& result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); if ( ast->Attributes ) @@ -1302,7 +1308,7 @@ void CodeVar::to_string( String& result ) return; } - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag::Export )) + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( result, "export " ); if ( ast->Attributes || ast->Specs ) diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index 80068de..f8d2878 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -78,7 +78,7 @@ void CodeClass::add_interface( CodeType type ) if ( possible_slot.ast ) { // Were adding an interface to parent type, so we need to make sure the parent type is public. - ast->ParentAccess = AccessSpec::Public; + ast->ParentAccess = AccessSpec_Public; // If your planning on adding a proper parent, // then you'll need to move this over to ParentType->next and update ParentAccess accordingly. } @@ -151,7 +151,7 @@ void CodeStruct::add_interface( CodeType type ) if ( possible_slot.ast ) { // Were adding an interface to parent type, so we need to make sure the parent type is public. - ast->ParentAccess = AccessSpec::Public; + ast->ParentAccess = AccessSpec_Public; // If your planning on adding a proper parent, // then you'll need to move this over to ParentType->next and update ParentAccess accordingly. } diff --git a/project/components/interface.cpp b/project/components/interface.cpp index 2d050ce..c64f937 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -81,19 +81,19 @@ void define_constants() Code::Invalid = make_code(); Code::Invalid.set_global(); - t_empty = (CodeType) make_code(); - t_empty->Type = ECode::Typename; - scast(String, t_empty->Name) = get_cached_string( txt("") ); + t_empty = (CodeType) make_code(); + t_empty->Type = ECode::Typename; + t_empty->Name = get_cached_string( txt("") ); t_empty.set_global(); - access_private = make_code(); - access_private->Type = ECode::Access_Private; - scast(String, access_private->Name) = get_cached_string( txt("private:\n") ); + access_private = make_code(); + access_private->Type = ECode::Access_Private; + access_private->Name = get_cached_string( txt("private:\n") ); access_private.set_global(); - access_protected = make_code(); - access_protected->Type = ECode::Access_Protected; - scast(String, access_protected->Name) = get_cached_string( txt("protected:\n") ); + access_protected = make_code(); + access_protected->Type = ECode::Access_Protected; + access_protected->Name = get_cached_string( txt("protected:\n") ); access_protected.set_global(); access_public = make_code(); @@ -226,6 +226,10 @@ void define_constants() # pragma pop_macro("local_persist") # pragma pop_macro("neverinline") +# pragma push_macro("enum_underlying") + +# pragma pop_macro("enum_underlying") + # undef def_constant_spec } diff --git a/project/components/interface.hpp b/project/components/interface.hpp index b06267a..0982fd4 100644 --- a/project/components/interface.hpp +++ b/project/components/interface.hpp @@ -44,9 +44,9 @@ CodeComment def_comment ( StrC content ); CodeClass def_class( StrC name , Code body = NoCode - , CodeType parent = NoCode, AccessSpec access = AccessSpec::Default + , CodeType parent = NoCode, AccessSpec access = AccessSpec_Default , CodeAttributes attributes = NoCode - , ModuleFlag mflags = ModuleFlag::None + , ModuleFlag mflags = ModuleFlag_None , CodeType* interfaces = nullptr, s32 num_interfaces = 0 ); CodeConstructor def_constructor( CodeParam params = NoCode, Code initializer_list = NoCode, Code body = NoCode ); @@ -56,9 +56,9 @@ CodeDefine def_define( StrC name, StrC content ); CodeDestructor def_destructor( Code body = NoCode, CodeSpecifiers specifiers = NoCode ); CodeEnum def_enum( StrC name - , Code body = NoCode, CodeType type = NoCode - , EnumT specifier = EnumRegular, CodeAttributes attributes = NoCode - , ModuleFlag mflags = ModuleFlag::None ); + , Code body = NoCode, CodeType type = NoCode + , EnumT specifier = EnumDecl_Regular, CodeAttributes attributes = NoCode + , ModuleFlag mflags = ModuleFlag_None ); CodeExec def_execution ( StrC content ); CodeExtern def_extern_link( StrC name, Code body ); @@ -67,16 +67,16 @@ CodeFriend def_friend ( Code symbol ); CodeFn def_function( StrC name , CodeParam params = NoCode, CodeType ret_type = NoCode, Code body = NoCode , CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode - , ModuleFlag mflags = ModuleFlag::None ); + , ModuleFlag mflags = ModuleFlag_None ); CodeInclude def_include ( StrC content, bool foreign = false ); -CodeModule def_module ( StrC name, ModuleFlag mflags = ModuleFlag::None ); -CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags = ModuleFlag::None ); +CodeModule def_module ( StrC name, ModuleFlag mflags = ModuleFlag_None ); +CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags = ModuleFlag_None ); CodeOperator def_operator( OperatorT op, StrC nspace , CodeParam params = NoCode, CodeType ret_type = NoCode, Code body = NoCode , CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode - , ModuleFlag mflags = ModuleFlag::None ); + , ModuleFlag mflags = ModuleFlag_None ); CodeOpCast def_operator_cast( CodeType type, Code body = NoCode, CodeSpecifiers specs = NoCode ); @@ -89,27 +89,27 @@ CodeSpecifiers def_specifier( SpecifierT specifier ); CodeStruct def_struct( StrC name , Code body = NoCode - , CodeType parent = NoCode, AccessSpec access = AccessSpec::Default + , CodeType parent = NoCode, AccessSpec access = AccessSpec_Default , CodeAttributes attributes = NoCode - , ModuleFlag mflags = ModuleFlag::None + , ModuleFlag mflags = ModuleFlag_None , CodeType* interfaces = nullptr, s32 num_interfaces = 0 ); -CodeTemplate def_template( CodeParam params, Code definition, ModuleFlag mflags = ModuleFlag::None ); +CodeTemplate def_template( CodeParam params, Code definition, ModuleFlag mflags = ModuleFlag_None ); CodeType def_type ( StrC name, Code arrayexpr = NoCode, CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode ); -CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes = NoCode, ModuleFlag mflags = ModuleFlag::None ); +CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes = NoCode, ModuleFlag mflags = ModuleFlag_None ); -CodeUnion def_union( StrC name, Code body, CodeAttributes attributes = NoCode, ModuleFlag mflags = ModuleFlag::None ); +CodeUnion def_union( StrC name, Code body, CodeAttributes attributes = NoCode, ModuleFlag mflags = ModuleFlag_None ); CodeUsing def_using( StrC name, CodeType type = NoCode , CodeAttributes attributess = NoCode - , ModuleFlag mflags = ModuleFlag::None ); + , ModuleFlag mflags = ModuleFlag_None ); CodeUsing def_using_namespace( StrC name ); CodeVar def_variable( CodeType type, StrC name, Code value = NoCode , CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode - , ModuleFlag mflags = ModuleFlag::None ); + , ModuleFlag mflags = ModuleFlag_None ); // Constructs an empty body. Use AST::validate_body() to check if the body is was has valid entries. CodeBody def_body( CodeT type ); diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index 104e1be..1d0f559 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -719,14 +719,14 @@ CodeEnum def_enum( StrC name return CodeInvalid; } - result->Type = specifier == EnumClass ? + result->Type = specifier == EnumDecl_Class ? Enum_Class : Enum; result->Body = body; } else { - result->Type = specifier == EnumClass ? + result->Type = specifier == EnumDecl_Class ? Enum_Class_Fwd : Enum_Fwd; } @@ -1145,16 +1145,16 @@ CodePreprocessCond def_preprocess_cond( EPreprocessCond type, StrC expr ) switch (type) { - case EPreprocessCond::If: + case PreprocessCond_If: result->Type = Preprocess_If; break; - case EPreprocessCond::IfDef: + case PreprocessCond_IfDef: result->Type = Preprocess_IfDef; break; - case EPreprocessCond::IfNotDef: + case PreprocessCond_IfNotDef: result->Type = Preprocess_IfNotDef; break; - case EPreprocessCond::ElIf: + case PreprocessCond_ElIf: result->Type = Preprocess_ElIf; break; } diff --git a/project/components/parser.cpp b/project/components/parser.cpp index b9f6a6b..9b9e853 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -682,17 +682,17 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) Token name { nullptr, 0, TokType::Invalid }; - AccessSpec access = AccessSpec::Default; + AccessSpec access = AccessSpec_Default; CodeType parent = { nullptr }; CodeBody body = { nullptr }; CodeAttributes attributes = { nullptr }; - ModuleFlag mflags = ModuleFlag::None; + ModuleFlag mflags = ModuleFlag_None; CodeClass result = CodeInvalid; if ( check(TokType::Module_Export) ) { - mflags = ModuleFlag::Export; + mflags = ModuleFlag_Export; eat( TokType::Module_Export ); } // @@ -2534,7 +2534,7 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes if ( found_operator ) { // Dealing with an operator overload - result = parse_operator_after_ret_type( ModuleFlag::None, attributes, specifiers, type ); + result = parse_operator_after_ret_type( ModuleFlag_None, attributes, specifiers, type ); // operator ... } else @@ -2552,7 +2552,7 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes if ( detected_capture && ! detected_comma ) { // Dealing with a function - result = parse_function_after_name( ModuleFlag::None, attributes, specifiers, type, name ); + result = parse_function_after_name( ModuleFlag_None, attributes, specifiers, type, name ); // ( ... } else @@ -2565,7 +2565,7 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes } // Dealing with a variable - result = parse_variable_after_name( ModuleFlag::None, attributes, specifiers, type, name ); + result = parse_variable_after_name( ModuleFlag_None, attributes, specifiers, type, name ); // ... } } @@ -3319,7 +3319,7 @@ CodeVar parse_variable_declaration_list() eat( TokType::Identifier ); // , - CodeVar var = parse_variable_after_name( ModuleFlag::None, NoCode, specifiers, NoCode, name ); + CodeVar var = parse_variable_after_name( ModuleFlag_None, NoCode, specifiers, NoCode, name ); // , ... if ( ! result ) @@ -3589,6 +3589,8 @@ CodeEnum parse_enum( bool inplace_def ) } // enum + b32 use_macro_underlying = false; + Code underlying_macro = { nullptr }; if ( currtok.Type == TokType::Assign_Classifer ) { eat( TokType::Assign_Classifer ); @@ -3603,6 +3605,17 @@ CodeEnum parse_enum( bool inplace_def ) } // enum : } + else if ( currtok.Type == TokType::Preprocess_Define ) + { + // We'll support the enum_underlying macro + StrC sig = txt("enum_underlying"); + + if (currtok.Length >= sig.Len && str_compare(currtok.Text, sig.Ptr, sig.Len) == 0 ) + { + use_macro_underlying = true; + underlying_macro = parse_simple_preprocess( ETokType::Preprocess_Macro); + } + } CodeBody body = { nullptr }; @@ -3770,11 +3783,18 @@ CodeEnum parse_enum( bool inplace_def ) result->Attributes = attributes; if ( type ) - result->UnderlyingType = type; + { + result->EnumUnderlyingMacro = use_macro_underlying; + if ( use_macro_underlying ) + result->UnderlyingTypeMacro = underlying_macro; + else + result->UnderlyingType = type; + } if ( inline_cmt ) result->InlineCmt = inline_cmt; + Context.pop(); return result; } @@ -3860,7 +3880,7 @@ CodeFriend parse_friend() Context.Scope->Name = name; // friend - function = parse_function_after_name( ModuleFlag::None, NoCode, NoCode, type, name ); + function = parse_function_after_name( ModuleFlag_None, NoCode, NoCode, type, name ); // Parameter list // CodeParam params = parse_params(); @@ -3914,11 +3934,11 @@ CodeFn parse_function() CodeAttributes attributes = { nullptr }; CodeSpecifiers specifiers = { nullptr }; - ModuleFlag mflags = ModuleFlag::None; + ModuleFlag mflags = ModuleFlag_None; if ( check(TokType::Module_Export) ) { - mflags = ModuleFlag::Export; + mflags = ModuleFlag_Export; eat( TokType::Module_Export ); } // @@ -4024,14 +4044,14 @@ CodeOperator parse_operator() CodeAttributes attributes = { nullptr }; CodeSpecifiers specifiers = { nullptr }; - ModuleFlag mflags = ModuleFlag::None; + ModuleFlag mflags = ModuleFlag_None; SpecifierT specs_found[16] { ESpecifier::NumSpecifiers }; s32 NumSpecifiers = 0; if ( check(TokType::Module_Export) ) { - mflags = ModuleFlag::Export; + mflags = ModuleFlag_Export; eat( TokType::Module_Export ); } // @@ -4212,11 +4232,11 @@ CodeTemplate parse_template() push_scope(); - ModuleFlag mflags = ModuleFlag::None; + ModuleFlag mflags = ModuleFlag_None; if ( check( TokType::Module_Export ) ) { - mflags = ModuleFlag::Export; + mflags = ModuleFlag_Export; eat( TokType::Module_Export ); } // template @@ -4855,11 +4875,11 @@ CodeTypedef parse_typedef() Code array_expr = { nullptr }; Code type = { nullptr }; - ModuleFlag mflags = ModuleFlag::None; + ModuleFlag mflags = ModuleFlag_None; if ( check(TokType::Module_Export) ) { - mflags = ModuleFlag::Export; + mflags = ModuleFlag_Export; eat( TokType::Module_Export ); } // @@ -5050,11 +5070,11 @@ CodeUnion parse_union( bool inplace_def ) { push_scope(); - ModuleFlag mflags = ModuleFlag::None; + ModuleFlag mflags = ModuleFlag_None; if ( check(TokType::Module_Export) ) { - mflags = ModuleFlag::Export; + mflags = ModuleFlag_Export; eat( TokType::Module_Export ); } // @@ -5199,12 +5219,12 @@ CodeUsing parse_using() bool is_namespace = false; - ModuleFlag mflags = ModuleFlag::None; + ModuleFlag mflags = ModuleFlag_None; CodeAttributes attributes = { nullptr }; if ( check(TokType::Module_Export) ) { - mflags = ModuleFlag::Export; + mflags = ModuleFlag_Export; eat( TokType::Module_Export ); } // @@ -5293,13 +5313,13 @@ CodeVar parse_variable() SpecifierT specs_found[16] { ESpecifier::NumSpecifiers }; s32 NumSpecifiers = 0; - ModuleFlag mflags = ModuleFlag::None; + ModuleFlag mflags = ModuleFlag_None; CodeAttributes attributes = { nullptr }; CodeSpecifiers specifiers = { nullptr }; if ( check(TokType::Module_Export) ) { - mflags = ModuleFlag::Export; + mflags = ModuleFlag_Export; eat( TokType::Module_Export ); } // diff --git a/project/components/types.hpp b/project/components/types.hpp index 298cd62..35975ed 100644 --- a/project/components/types.hpp +++ b/project/components/types.hpp @@ -13,63 +13,71 @@ using LogFailType = ssize(*)(char const*, ...); #define log_failure GEN_FATAL #endif -enum class AccessSpec : u32 +enum AccessSpec enum_underlying(u32) { - Default, - Private, - Protected, - Public, + AccessSpec_Default, + AccessSpec_Private, + AccessSpec_Protected, + AccessSpec_Public, - Num_AccessSpec, - Invalid, + AccessSpec_Num_AccessSpec, + AccessSpec_Invalid, + + AccessSpec_SizeDef = GEN_U32_MAX, }; +static_assert( size_of(AccessSpec) == size_of(u32)); inline char const* to_str( AccessSpec type ) { local_persist - char const* lookup[ (u32)AccessSpec::Num_AccessSpec ] = { + char const* lookup[ (u32)AccessSpec_Num_AccessSpec ] = { "", "private", "protected", "public", }; - if ( type > AccessSpec::Public ) + if ( type > AccessSpec_Public ) return "Invalid"; return lookup[ (u32)type ]; } - -enum CodeFlag : u32 +enum CodeFlag enum_underlying(u32) { - None = 0, - FunctionType = bit(0), - ParamPack = bit(1), - Module_Export = bit(2), - Module_Import = bit(3), + CodeFlag_None = 0, + CodeFlag_FunctionType = bit(0), + CodeFlag_ParamPack = bit(1), + CodeFlag_Module_Export = bit(2), + CodeFlag_Module_Import = bit(3), + + CodeFlag_SizeDef = GEN_U32_MAX, }; +static_assert( size_of(CodeFlag) == size_of(u32)); // Used to indicate if enum definitoin is an enum class or regular enum. -enum class EnumT : u8 +enum EnumDecl enum_underlying(u8) { - Regular, - Class + EnumDecl_Regular, + EnumDecl_Class, + + EnumT_SizeDef = GEN_U8_MAX, }; +typedef u8 EnumT; -constexpr EnumT EnumClass = EnumT::Class; -constexpr EnumT EnumRegular = EnumT::Regular; - -enum class ModuleFlag : u32 +enum ModuleFlag enum_underlying(u32) { - None = 0, - Export = bit(0), - Import = bit(1), + ModuleFlag_None = 0, + ModuleFlag_Export = bit(0), + ModuleFlag_Import = bit(1), Num_ModuleFlags, - Invalid, + ModuleFlag_Invalid, + + ModuleFlag_SizeDef = GEN_U32_MAX, }; +static_assert( size_of(ModuleFlag) == size_of(u32)); inline StrC to_str( ModuleFlag flag ) @@ -81,7 +89,7 @@ StrC to_str( ModuleFlag flag ) { sizeof("import"), "import" }, }; - if ( flag > ModuleFlag::Import ) + if ( flag > ModuleFlag_Import ) return { sizeof("invalid"), "invalid" }; return lookup[ (u32)flag ]; @@ -93,15 +101,13 @@ ModuleFlag operator|( ModuleFlag A, ModuleFlag B) return (ModuleFlag)( (u32)A | (u32)B ); } -enum class EPreprocessCond : u32 +enum EPreprocessCond enum_underlying(u32) { - If, - IfDef, - IfNotDef, - ElIf -}; + PreprocessCond_If, + PreprocessCond_IfDef, + PreprocessCond_IfNotDef, + PreprocessCond_ElIf, -constexpr EPreprocessCond PreprocessCond_If = EPreprocessCond::If; -constexpr EPreprocessCond PreprocessCond_IfDef = EPreprocessCond::IfDef; -constexpr EPreprocessCond PreprocessCond_IfNotDef = EPreprocessCond::IfNotDef; -constexpr EPreprocessCond PreprocessCond_ElIf = EPreprocessCond::ElIf; + EPreprocessCond_SizeDef = GEN_U32_MAX, +}; +static_assert( size_of(EPreprocessCond) == size_of(u32)); diff --git a/project/dependencies/macros.hpp b/project/dependencies/macros.hpp index cf0346d..a42c141 100644 --- a/project/dependencies/macros.hpp +++ b/project/dependencies/macros.hpp @@ -205,4 +205,14 @@ # define foreach(Type, entry_id, iterable) for ( Type entry_id : iterable ) #endif +#if GENC_COMPILERC +# if __STDC_VERSION__ >= 202311L +# define enum_underlying(type) : type +# else +# define enum_underlying(type) +# endif +#else +# define enum_underlying(type) : type +#endif + #pragma endregion Macros diff --git a/project/dependencies/platform.hpp b/project/dependencies/platform.hpp index 272dadb..1e9a173 100644 --- a/project/dependencies/platform.hpp +++ b/project/dependencies/platform.hpp @@ -76,13 +76,18 @@ /* Platform compiler */ #if defined( _MSC_VER ) -# define GEN_COMPILER_MSVC 1 +# define GEN_COMPILER_CLANG 0 +# define GEN_COMPILER_MSVC 1 +# define GEN_COMPILER_GCC 0 #elif defined( __GNUC__ ) -# define GEN_COMPILER_GCC 1 +# define GEN_COMPILER_CLANG 0 +# define GEN_COMPILER_MSVC 0 +# define GEN_COMPILER_GCC 1 #elif defined( __clang__ ) # define GEN_COMPILER_CLANG 1 -#elif defined( __MINGW32__ ) -# define GEN_COMPILER_MINGW 1 +# define GEN_COMPILER_MSVC 0 +# define GEN_COMPILER_GCC 1 +#else # error Unknown compiler #endif @@ -122,11 +127,23 @@ #pragma endregion Mandatory Includes -#if GEN_DONT_USE_NAMESPACE || GEN_COMPILER_C -# define GEN_NS -# define GEN_NS_BEGIN -# define GEN_NS_END +#if GEN_DONT_USE_NAMESPACE +# if GEN_COMPILER_C +# define GEN_NS_ENUM_BEGIN +# define GEN_NS_ENUM_END +# define GEN_NS +# define GEN_NS_BEGIN +# define GEN_NS_END +# else +# define GEN_NS_ENUM_BEGIN namespace gen_internal_enums { +# define GEN_NS_ENUM_END } +# define GEN_NS :: +# define GEN_NS_BEGIN +# define GEN_NS_END +# endif #else +# define GEN_NS_ENUM_BEGIN namespace gen_internal_enums { +# define GEN_NS_ENUM_END } # define GEN_NS gen:: # define GEN_NS_BEGIN namespace gen { # define GEN_NS_END } diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index 07808cc..77e9f8f 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -85,7 +85,7 @@ struct String forceinline operator bool() { return Data != nullptr; } forceinline operator char*() { return Data; } forceinline operator char const*() const { return Data; } - forceinline operator StrC() const { return { length(* this), Data }; } + forceinline operator StrC() const { return { GEN_NS length(* this), Data }; } String const& operator=(String const& other) const { if (this == &other) @@ -101,7 +101,7 @@ struct String forceinline char const& operator[](ssize index) const { return Data[index]; } forceinline char* begin() const { return Data; } - forceinline char* end() const { return Data + length(* this); } + forceinline char* end() const { return Data + GEN_NS length(* this); } #if GEN_SUPPORT_CPP_MEMBER_FEATURES #pragma region Member Mapping From 31691b1466f757659ce6ee4444cbbd3a60e178c6 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 05:37:03 -0500 Subject: [PATCH 020/112] Fixed issue with HashTable region detection --- gen_c_library/c_library.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index ee154ea..7ee7d43 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -235,10 +235,14 @@ int gen_main() bool found = false; found = swap_pragma_region_implementation( txt("Array"), gen_array_base, entry, containers); - if (found) break; + if (found) { + break; + } - found = swap_pragma_region_implementation( txt("Hashtable"), gen_hashtable_base, entry, containers); - if (found) break; + found = swap_pragma_region_implementation( txt("HashTable"), gen_hashtable_base, entry, containers); + if (found) { + break; + } containers.append(entry); } From 8ef982003af5e50d20b5dbe7c0069cbe707feb4d Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 12:48:58 -0500 Subject: [PATCH 021/112] Added is_body to AST and Code types --- gen_c_library/c_library.cpp | 133 ++++++++++++------ gen_c_library/components/containers.array.hpp | 2 +- gen_c_library/components/misc.hpp | 6 +- project/components/parser.cpp | 1 + project/dependencies/containers.hpp | 7 +- project/dependencies/macros.hpp | 2 +- project/dependencies/memory.hpp | 24 +--- project/dependencies/platform.hpp | 2 +- project/dependencies/strings.hpp | 7 +- 9 files changed, 109 insertions(+), 75 deletions(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 7ee7d43..d22110c 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -90,6 +90,7 @@ CodeBody parse_file( const char* path ) { FileContents file = file_read_contents( GlobalAllocator, true, path ); CodeBody code = parse_global_body( { file.size, (char const*)file.data } ); + log_fmt("\nParsed: %s\n", path); return code; } @@ -126,7 +127,7 @@ int gen_main() header.print( debug ); CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" ); - CodeBody memory = def_body(ECode::Struct_Body); + CodeBody memory = def_body(ECode::Global_Body); for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry ) { switch (entry->Type) @@ -145,12 +146,32 @@ int gen_main() CodeFn fn = entry.cast(); s32 constexpr_found = fn->Specs.remove( ESpecifier::Constexpr ); if (constexpr_found > -1) { - log_fmt("Found constexpr proc\n"); + log_fmt("Found constexpr: %S\n", entry->to_string()); fn->Specs.append(ESpecifier::Inline); } memory.append(entry); } break; + case ECode::Template: + { + CodeTemplate tmpl = entry.cast(); + if ( tmpl->Declaration->Name.contains(txt("swap"))) + { + CodeBody macro_swap = parse_global_body( txt(R"( +#define swap( a, b ) \ + do \ + { \ + typeof( a ) temp = ( a ); \ + ( a ) = ( b ); \ + ( b ) = temp; \ + } while ( 0 ) +)" + )); + memory.append(macro_swap); + log_fmt( "\nmacro swap: %S\n", macro_swap.to_string() ); + } + } + break; case ECode::Class: case ECode::Struct: { @@ -175,17 +196,26 @@ int gen_main() break; case ECode::Preprocess_If: { - ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory ); + b32 found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory ); + if (found) break; + + memory.append(entry); } break; case ECode::Preprocess_IfDef: { - ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory ); + b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory ); + if (found) break; + + memory.append(entry); } break; case ECode::Preprocess_Pragma: { - swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory); + b32 found = swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory); + if (found) break; + + memory.append(entry); } break; default: { @@ -194,69 +224,84 @@ int gen_main() break; } } - - header.print( memory ); + header.print( dump_to_scratch_and_retireve(memory) ); Code string_ops = scan_file( project_dir "dependencies/string_ops.hpp" ); header.print( string_ops ); CodeBody printing_parsed = parse_file( project_dir "dependencies/printing.hpp" ); - CodeBody printing = def_body(ECode::Struct_Body); + CodeBody printing = def_body(ECode::Global_Body); for ( Code entry = printing_parsed.begin(); entry != printing_parsed.end(); ++ entry ) { switch (entry->Type) { case ECode::Preprocess_IfDef: { - ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed ); + b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed ); + if (found) break; + + printing.append(entry); } - } - - if (entry->Type == ECode::Variable && - contains(entry->Name, txt("Msg_Invalid_Value"))) - { - CodeDefine define = def_define(entry->Name, entry->Value->Content); - printing.append(define); - continue; - } - printing.append(entry); - } - - header.print(printing); - - CodeBody parsed_containers = parse_file( project_dir "dependencies/containers.hpp" ); - CodeBody containers = def_body(ECode::Struct_Body); - for ( Code entry = parsed_containers.begin(); entry != parsed_containers.end(); ++ entry ) - { - switch ( entry->Type ) - { - case ECode::Preprocess_Pragma: + break; + case ECode::Variable: { - bool found = false; - - found = swap_pragma_region_implementation( txt("Array"), gen_array_base, entry, containers); - if (found) { - break; + if (contains(entry->Name, txt("Msg_Invalid_Value"))) + { + CodeDefine define = def_define(entry->Name, entry->Value->Content); + printing.append(define); + continue; } - - found = swap_pragma_region_implementation( txt("HashTable"), gen_hashtable_base, entry, containers); - if (found) { - break; - } - - containers.append(entry); + printing.append(entry); } break; + default: + printing.append(entry); + break; } } + header.print(dump_to_scratch_and_retireve(printing)); - header.print(containers); + CodeBody containers = def_body(ECode::Global_Body); + { + containers.append( def_pragma(code(region Containers))); + + containers.append( gen_array_base() ); + containers.append( gen_hashtable_base() ); + + containers.append( def_pragma(code(endregion Containers))); + } + header.print(fmt_newline); + header.print(dump_to_scratch_and_retireve(containers)); + + Code hashing = scan_file( project_dir "dependencies/hashing.hpp" ); + header.print( hashing ); + + CodeBody parsed_strings = parse_file( project_dir "dependencies/strings.hpp" ); + CodeBody strings = def_body(ECode::Global_Body); + for ( Code entry = parsed_strings.begin(); entry != parsed_strings.end(); ++ entry ) + { + switch (entry->Type) + { + case ECode::Preprocess_IfDef: + { + ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_strings ); + } + break; + + default: + strings.append(entry); + break; + } + } + header.print(dump_to_scratch_and_retireve(strings)); + + header.print_fmt( roll_own_dependencies_guard_end ); } header.print( pop_ignores ); header.write(); - format_file( "gen/gen.h" ); + // format_file( "gen/gen.h" ); gen::deinit(); return 0; diff --git a/gen_c_library/components/containers.array.hpp b/gen_c_library/components/containers.array.hpp index 45dc684..c374258 100644 --- a/gen_c_library/components/containers.array.hpp +++ b/gen_c_library/components/containers.array.hpp @@ -19,7 +19,7 @@ CodeBody gen_array_base() // Code grow_formula = untyped_str( txt( "#define gen_array_grow_formula( value ) ( 2 * value + 8 )\n" )); Code get_header = untyped_str( txt( "#define array_get_header( Type, self ) ( (ArrayHeader*)( self ) - 1)\n" )); - return def_global_body( args( td_header, header, get_header ) ); + return def_global_body( args( fmt_newline, td_header, header, get_header, fmt_newline ) ); }; CodeBody gen_array( StrC type, StrC array_name ) diff --git a/gen_c_library/components/misc.hpp b/gen_c_library/components/misc.hpp index 7280231..109f42b 100644 --- a/gen_c_library/components/misc.hpp +++ b/gen_c_library/components/misc.hpp @@ -7,9 +7,13 @@ using SwapContentProc = CodeBody(void); b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body ) { + b32 found = false; CodePreprocessCond cond = entry_iter.cast(); if ( cond->Content.contains(cond_sig) ) { + log_fmt("Preprocess cond found: %S\n", cond->Content); + found = true; + s32 depth = 1; ++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ) switch (entry_iter->Type) { @@ -34,7 +38,7 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod } } - return entry_iter != body.end(); + return found; } bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body ) diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 9b9e853..7801ab0 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -4406,6 +4406,7 @@ CodeTemplate parse_template() result->Params = params; result->Declaration = definition; result->ModuleFlags = mflags; + // result->Name = definition->Name; Context.pop(); return result; diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index 948c0d1..66c6565 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -14,20 +14,17 @@ template using TRemoveConst = typename RemoveConst::Type; #pragma region Array -#if ! GEN_COMPILER_C #define Array(Type) Array // #define array_init(Type, ...) array_init (__VA_ARGS__) // #define array_init_reserve(Type, ...) array_init_reserve(__VA_ARGS__) -#endif struct ArrayHeader; #if GEN_SUPPORT_CPP_MEMBER_FEATURES -template struct Array; + template struct Array; #else -template -using Array = Type*; + template using Array = Type*; #endif usize array_grow_formula(ssize value); diff --git a/project/dependencies/macros.hpp b/project/dependencies/macros.hpp index a42c141..791ad1e 100644 --- a/project/dependencies/macros.hpp +++ b/project/dependencies/macros.hpp @@ -205,7 +205,7 @@ # define foreach(Type, entry_id, iterable) for ( Type entry_id : iterable ) #endif -#if GENC_COMPILERC +#if GEN_COMPILER_C # if __STDC_VERSION__ >= 202311L # define enum_underlying(type) : type # else diff --git a/project/dependencies/memory.hpp b/project/dependencies/memory.hpp index 545e337..aeae598 100644 --- a/project/dependencies/memory.hpp +++ b/project/dependencies/memory.hpp @@ -14,23 +14,13 @@ #define GEN__HIGHS ( GEN__ONES * ( GEN_U8_MAX / 2 + 1 ) ) #define GEN__HAS_ZERO( x ) ( ( ( x ) - GEN__ONES ) & ~( x ) & GEN__HIGHS ) -#if ! GEN_COMPILER_C - template< class Type > - void swap( Type& a, Type& b ) - { - Type tmp = a; - a = b; - b = tmp; - } -#else - #define swap( a, b ) \ - do { \ - typeof(a) \ - temp = (a); \ - (a) = (b); \ - (b) = temp; \ - } while(0) -#endif +template< class Type > +void swap( Type& a, Type& b ) +{ + Type tmp = a; + a = b; + b = tmp; +} //! Checks if value is power of 2. b32 is_power_of_two( ssize x ); diff --git a/project/dependencies/platform.hpp b/project/dependencies/platform.hpp index 1e9a173..583b260 100644 --- a/project/dependencies/platform.hpp +++ b/project/dependencies/platform.hpp @@ -106,7 +106,7 @@ # define GEN_GCC_VERSION_CHECK(major,minor,patch) (0) #endif -#ifndef GEN_COMPIELR_C +#ifndef GEN_COMPILER_C # if defined(__STDC_VERSION__) # define GEN_COMPILER_C 1 # else diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index 77e9f8f..946a610 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -11,8 +11,10 @@ struct StrC ssize Len; char const* Ptr; +#if ! GEN_COMPILER_C operator char const* () const { return Ptr; } char const& operator[]( ssize index ) const { return Ptr[index]; } +#endif }; #define cast_to_strc( str ) * rcast( StrC*, (str) - sizeof(ssize) ) @@ -29,12 +31,7 @@ StrC to_str( char const* str ) { // I kept it for simplicty of porting but its not necessary to keep it that way. #pragma region String struct StringHeader; - -#if GEN_COMPILER_C -typedef char* String; -#else struct String; -#endif String string_make(AllocatorInfo allocator, char const* str); String string_make(AllocatorInfo allocator, StrC str); From f61c1c560d29f6ec1a85c8a267e3a99c86a335bb Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 13:29:16 -0500 Subject: [PATCH 022/112] String::is_equal added --- gen_c_library/c_library.cpp | 51 ++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index d22110c..f192f67 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -168,7 +168,6 @@ int gen_main() )" )); memory.append(macro_swap); - log_fmt( "\nmacro swap: %S\n", macro_swap.to_string() ); } } break; @@ -282,12 +281,32 @@ int gen_main() { switch (entry->Type) { + case ECode::Preprocess_If: + { + ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), entry, parsed_strings); + } + break; + case ECode::Preprocess_IfDef: { ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_strings ); } break; + case ECode::Struct_Fwd: + { + if ( entry->Name.is_equal(txt("String")) ) + { + CodeTypedef c_def = parse_typedef(code( typedef Type* String; )); + strings.append(c_def); + strings.append(fmt_newline); + ++ entry; + continue; + } + strings.append(entry); + } + break; + default: strings.append(entry); break; @@ -295,7 +314,37 @@ int gen_main() } header.print(dump_to_scratch_and_retireve(strings)); + Code filesystem = scan_file( project_dir "dependencies/filesystem.hpp" ); + Code timing = scan_file( project_dir "dependencies/timing.hpp" ); + header.print( filesystem ); + header.print( timing ); + + header.print_fmt( "\nGEN_NS_END\n" ); header.print_fmt( roll_own_dependencies_guard_end ); + + Code types = scan_file( project_dir "components/types.hpp" ); + Code ast = scan_file( project_dir "components/ast.hpp" ); + Code ast_types = scan_file( project_dir "components/ast_types.hpp" ); + Code code_types = scan_file( project_dir "components/code_types.hpp" ); + Code interface = scan_file( project_dir "components/interface.hpp" ); + Code inlines = scan_file( project_dir "components/inlines.hpp" ); + Code header_end = scan_file( project_dir "components/header_end.hpp" ); + + CodeBody ecode = gen_ecode ( project_dir "enums/ECode.csv" ); + CodeBody eoperator = gen_eoperator ( project_dir "enums/EOperator.csv" ); + CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv" ); + CodeBody ast_inlines = gen_ast_inlines(); + + header.print_fmt("#pragma region Types\n"); + header.print( types ); + header.print( fmt_newline ); + header.print( dump_to_scratch_and_retireve( ecode )); + header.print( fmt_newline ); + header.print( dump_to_scratch_and_retireve( eoperator )); + header.print( fmt_newline ); + header.print( dump_to_scratch_and_retireve( especifier )); + header.print( fmt_newline ); + header.print_fmt("#pragma endregion Types\n\n"); } header.print( pop_ignores ); From 9e88cb8724807886f89ba7aeb408b9d38dafd1e8 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 13:29:33 -0500 Subject: [PATCH 023/112] String::is_equal added (bad last commit) --- project/components/ast.hpp | 2 + project/components/code_types.hpp | 4 + project/components/gen/ast_inlines.hpp | 261 +++++++++++++++++++++++++ project/components/inlines.hpp | 21 +- project/dependencies/strings.hpp | 12 +- project/helpers/helper.hpp | 9 + 6 files changed, 302 insertions(+), 7 deletions(-) diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 026ed4b..a2a1fd8 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -165,6 +165,7 @@ struct Code char const* debug_str(); \ Code duplicate(); \ bool is_equal( Code other ); \ + bool is_body(); \ bool is_valid(); \ void set_global(); \ String to_string(); \ @@ -259,6 +260,7 @@ struct AST Code& entry ( u32 idx ); bool has_entries(); bool is_equal ( AST* other ); + bool is_body(); char const* type_str(); bool validate_body(); diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index bc0f5f9..8bab98e 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -11,6 +11,10 @@ struct CodeBody void append( Code other ) { + if (other.is_body()) + { + append( other.cast() ); + } raw()->append( other.ast ); } void append( CodeBody body ) diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index d7dda4e..c449e0d 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -24,6 +24,15 @@ inline Code Code::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool Code::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool Code::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -91,6 +100,15 @@ inline Code CodeBody::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeBody::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeBody::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -158,6 +176,15 @@ inline Code CodeAttributes::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeAttributes::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeAttributes::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -245,6 +272,15 @@ inline Code CodeComment::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeComment::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeComment::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -332,6 +368,15 @@ inline Code CodeConstructor::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeConstructor::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeConstructor::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -419,6 +464,15 @@ inline Code CodeClass::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeClass::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeClass::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -486,6 +540,15 @@ inline Code CodeDefine::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeDefine::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeDefine::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -573,6 +636,15 @@ inline Code CodeDestructor::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeDestructor::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeDestructor::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -660,6 +732,15 @@ inline Code CodeEnum::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeEnum::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeEnum::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -747,6 +828,15 @@ inline Code CodeExec::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeExec::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeExec::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -834,6 +924,15 @@ inline Code CodeExtern::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeExtern::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeExtern::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -921,6 +1020,15 @@ inline Code CodeFriend::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeFriend::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeFriend::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1008,6 +1116,15 @@ inline Code CodeFn::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeFn::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeFn::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1095,6 +1212,15 @@ inline Code CodeInclude::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeInclude::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeInclude::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1182,6 +1308,15 @@ inline Code CodeModule::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeModule::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeModule::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1269,6 +1404,15 @@ inline Code CodeNS::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeNS::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeNS::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1356,6 +1500,15 @@ inline Code CodeOperator::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeOperator::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeOperator::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1443,6 +1596,15 @@ inline Code CodeOpCast::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeOpCast::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeOpCast::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1530,6 +1692,15 @@ inline Code CodeParam::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeParam::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeParam::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1597,6 +1768,15 @@ inline Code CodePragma::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodePragma::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodePragma::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1684,6 +1864,15 @@ inline Code CodePreprocessCond::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodePreprocessCond::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodePreprocessCond::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1771,6 +1960,15 @@ inline Code CodeSpecifiers::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeSpecifiers::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeSpecifiers::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1838,6 +2036,15 @@ inline Code CodeStruct::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeStruct::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeStruct::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1905,6 +2112,15 @@ inline Code CodeTemplate::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeTemplate::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeTemplate::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1992,6 +2208,15 @@ inline Code CodeType::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeType::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeType::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -2079,6 +2304,15 @@ inline Code CodeTypedef::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeTypedef::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeTypedef::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -2166,6 +2400,15 @@ inline Code CodeUnion::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeUnion::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeUnion::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -2253,6 +2496,15 @@ inline Code CodeUsing::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeUsing::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeUsing::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -2340,6 +2592,15 @@ inline Code CodeVar::duplicate() return { rcast( AST*, ast )->duplicate() }; } +inline bool CodeVar::is_body() +{ + if ( ast == nullptr ) + { + return rcast( AST*, ast )->is_body(); + } + return false; +} + inline bool CodeVar::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index f8d2878..34bdf5b 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -21,7 +21,7 @@ void AST::append( AST* other ) } AST* - Current = Back; + Current = Back; Current->Next = other; other->Prev = Current; Back = other; @@ -50,6 +50,25 @@ bool AST::has_entries() return NumEntries > 0; } +inline +bool AST::is_body() +{ + switch (Type) + { + case ECode::Enum_Body: + case ECode::Class_Body: + case ECode::Union_Body: + case ECode::Export_Body: + case ECode::Global_Body: + case ECode::Struct_Body: + case ECode::Function_Body: + case ECode::Namespace_Body: + case ECode::Extern_Linkage_Body: + return true; + } + return false; +} + inline char const* AST::type_str() { diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index 946a610..285e3e8 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -41,8 +41,8 @@ String string_fmt(AllocatorInfo allocator, char* buf, ssize buf_size, cha String string_fmt_buf(AllocatorInfo allocator, char const* fmt, ...); String string_join(AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue); usize string_grow_formula(usize value); -bool are_equal(String lhs, String rhs); -bool are_equal(String lhs, StrC rhs); +bool are_equal(String const& lhs, String const& rhs); +bool are_equal(String const& lhs, StrC rhs); bool make_space_for(String& str, char const* to_append, ssize add_len); bool append(String& str, char c); bool append(String& str, char const* str_to_append); @@ -108,8 +108,6 @@ struct String forceinline static String make_length(AllocatorInfo a, char const* s, ssize l) { return GEN_NS string_make_length(a, s, l); } forceinline static String join(AllocatorInfo a, char const** p, ssize n, char const* g) { return GEN_NS string_join(a, p, n, g); } forceinline static usize grow_formula(usize value) { return GEN_NS string_grow_formula(value); } - forceinline static bool are_equal(String lhs, String rhs) { return GEN_NS are_equal(lhs, rhs); } - forceinline static bool are_equal(String lhs, StrC rhs) { return GEN_NS are_equal(lhs, rhs); } static String fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...) { @@ -145,6 +143,8 @@ struct String forceinline void clear() { GEN_NS clear(*this); } forceinline String duplicate(AllocatorInfo allocator) const { return GEN_NS duplicate(*this, allocator); } forceinline void free() { GEN_NS free(*this); } + forceinline bool is_equal(String const& other) const { return GEN_NS are_equal(* this, other); } + forceinline bool is_equal(StrC other) const { return GEN_NS are_equal(* this, other); } forceinline ssize length() const { return GEN_NS length(*this); } forceinline b32 starts_with(StrC substring) const { return GEN_NS starts_with(*this, substring); } forceinline b32 starts_with(String substring) const { return GEN_NS starts_with(*this, substring); } @@ -286,7 +286,7 @@ bool append_fmt(String& str, char const* fmt, ...) { } inline -bool are_equal(String lhs, String rhs) +bool are_equal(String const& lhs, String const& rhs) { if (length(lhs) != length(rhs)) return false; @@ -299,7 +299,7 @@ bool are_equal(String lhs, String rhs) } inline -bool are_equal(String lhs, StrC rhs) +bool are_equal(String const& lhs, StrC rhs) { if (length(lhs) != (rhs.Len)) return false; diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 07ef316..e648ab6 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -368,6 +368,15 @@ CodeBody gen_ast_inlines() return { rcast(AST*, ast)->duplicate() }; } inline + bool ::is_body() + { + if ( ast == nullptr ) + { + return rcast(AST*, ast)->is_body(); + } + return false; + } + inline bool ::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) From 80cb3f4ecaa7387865af4ad1febcd6806d91e060 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 18:50:37 -0500 Subject: [PATCH 024/112] Significant progress reducing c++ feature usage in the library. --- project/bootstrap.cpp | 5 +- project/components/ast.cpp | 491 +++++++++++----------- project/components/ast.hpp | 57 ++- project/components/code_serialization.cpp | 13 +- project/components/code_types.hpp | 37 +- project/components/gen/ast_inlines.hpp | 58 +-- project/components/inlines.hpp | 49 ++- project/components/interface.cpp | 14 +- project/components/interface.untyped.cpp | 4 +- project/components/interface.upfront.cpp | 4 +- project/components/lexer.cpp | 6 +- project/components/parser.cpp | 129 +++--- project/components/types.hpp | 8 +- project/dependencies/macros.hpp | 25 +- project/dependencies/memory.hpp | 142 ++++--- project/dependencies/platform.hpp | 6 +- project/dependencies/strings.hpp | 12 +- project/helpers/helper.hpp | 13 +- scripts/build.ci.ps1 | 24 ++ 19 files changed, 611 insertions(+), 486 deletions(-) diff --git a/project/bootstrap.cpp b/project/bootstrap.cpp index ffa783b..24dd2bb 100644 --- a/project/bootstrap.cpp +++ b/project/bootstrap.cpp @@ -1,7 +1,8 @@ #define GEN_DEFINE_LIBRARY_CODE_CONSTANTS #define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_EXPOSE_BACKEND -#define GEN_SUPPORT_CPP_MEMBER_FEATURES 0 +#define GEN_SUPPORT_CPP_MEMBER_FEATURES 1 +#define GEN_SUPPORT_CPP_REFERENCES 0 #include "gen.cpp" #include "helpers/push_ignores.inline.hpp" @@ -63,6 +64,8 @@ int gen_main() { gen::init(); + // PreprocessorDefines.append("GEN_NS"); + Code push_ignores = scan_file( "helpers/push_ignores.inline.hpp" ); Code pop_ignores = scan_file( "helpers/pop_ignores.inline.hpp" ); diff --git a/project/components/ast.cpp b/project/components/ast.cpp index d56eebf..5f8fa43 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -7,20 +7,21 @@ Code Code::Global; Code Code::Invalid; // This serializes all the data-members in a "debug" format, where each member is printed with its associated value. -char const* AST::debug_str() +char const* debug_str(AST* self) { + GEN_ASSERT(self != nullptr); String result = string_make_reserve( GlobalAllocator, kilobytes(1) ); - if ( Parent ) - append_fmt( result, "\n\tParent : %S %S", Parent->type_str(), Name ? Name : "" ); + if ( self->Parent ) + append_fmt( result, "\n\tParent : %S %S", self->Parent->type_str(), self->Name ? self->Name : "" ); else append_fmt( result, "\n\tParent : %S", "Null" ); - append_fmt( result, "\n\tName : %S", Name ? Name : "Null" ); - append_fmt( result, "\n\tType : %S", type_str() ); - append_fmt( result, "\n\tModule Flags : %S", to_str( ModuleFlags ) ); + append_fmt( result, "\n\tName : %S", self->Name ? self->Name : "Null" ); + append_fmt( result, "\n\tType : %S", type_str(self) ); + append_fmt( result, "\n\tModule Flags : %S", to_str( self->ModuleFlags ) ); - switch ( Type ) + switch ( self->Type ) { using namespace ECode; @@ -29,10 +30,10 @@ char const* AST::debug_str() case Access_Private: case Access_Protected: case Access_Public: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); break; case Untyped: @@ -47,75 +48,75 @@ char const* AST::debug_str() case Preprocess_Else: case Preprocess_IfDef: case Preprocess_IfNotDef: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tContent: %S", Content ); + append_fmt( result, "\n\tContent: %S", self->Content ); break; case Class: case Struct: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmd : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - append_fmt( result, "\n\tParentAccess: %s", ParentType ? to_str( ParentAccess ) : "No Parent" ); - append_fmt( result, "\n\tParentType : %s", ParentType ? ParentType->type_str() : "Null" ); - append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes : %S", self->Attributes ? to_string(self->Attributes) : "Null" ); + append_fmt( result, "\n\tParentAccess: %s", self->ParentType ? to_str( self->ParentAccess ) : "No Parent" ); + append_fmt( result, "\n\tParentType : %s", self->ParentType ? type_str(self->ParentType) : "Null" ); + append_fmt( result, "\n\tBody : %S", self->Body ? debug_str(self->Body) : "Null" ); break; case Class_Fwd: case Struct_Fwd: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmd : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - append_fmt( result, "\n\tParentAccess: %s", ParentType ? to_str( ParentAccess ) : "No Parent" ); - append_fmt( result, "\n\tParentType : %s", ParentType ? ParentType->type_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes : %S", self->Attributes ? to_string(self->Attributes) : "Null" ); + append_fmt( result, "\n\tParentAccess: %s", self->ParentType ? to_str( self->ParentAccess ) : "No Parent" ); + append_fmt( result, "\n\tParentType : %s", self->ParentType ? type_str(self->ParentType) : "Null" ); break; case Constructor: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - append_fmt( result, "\n\tInitializerList: %S", InitializerList ? InitializerList->to_string() : "Null" ); - append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); - append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tSpecs : %S", self->Specs ? to_string(self->Specs) : "Null" ); + append_fmt( result, "\n\tInitializerList: %S", self->InitializerList ? to_string(self->InitializerList) : "Null" ); + append_fmt( result, "\n\tParams : %S", self->Params ? to_string(self->Params) : "Null" ); + append_fmt( result, "\n\tBody : %S", self->Body ? debug_str(self->Body) : "Null" ); break; case Constructor_Fwd: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - append_fmt( result, "\n\tInitializerList: %S", InitializerList ? InitializerList->to_string() : "Null" ); - append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tSpecs : %S", self->Specs ? to_string(self->Specs) : "Null" ); + append_fmt( result, "\n\tInitializerList: %S", self->InitializerList ? to_string(self->InitializerList) : "Null" ); + append_fmt( result, "\n\tParams : %S", self->Params ? to_string(self->Params) : "Null" ); break; case Destructor: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tSpecs : %S", self->Specs ? to_string(self->Specs) : "Null" ); + append_fmt( result, "\n\tBody : %S", self->Body ? debug_str(self->Body) : "Null" ); break; case Destructor_Fwd: @@ -123,248 +124,248 @@ char const* AST::debug_str() case Enum: case Enum_Class: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - append_fmt( result, "\n\tUnderlying Type : %S", UnderlyingType ? UnderlyingType->to_string() : "Null" ); - append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes : %S", self->Attributes ? to_string(self->Attributes) : "Null" ); + append_fmt( result, "\n\tUnderlying Type : %S", self->UnderlyingType ? to_string(self->UnderlyingType) : "Null" ); + append_fmt( result, "\n\tBody : %S", self->Body ? debug_str(self->Body) : "Null" ); break; case Enum_Fwd: case Enum_Class_Fwd: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - append_fmt( result, "\n\tUnderlying Type : %S", UnderlyingType ? UnderlyingType->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes : %S", self->Attributes ? to_string(self->Attributes) : "Null" ); + append_fmt( result, "\n\tUnderlying Type : %S", self->UnderlyingType ? to_string(self->UnderlyingType) : "Null" ); break; case Extern_Linkage: case Namespace: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tBody: %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tBody: %S", self->Body ? debug_str(self->Body) : "Null" ); break; case Friend: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tDeclaration: %S", Declaration ? Declaration->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tDeclaration: %S", self->Declaration ? to_string(self->Declaration) : "Null" ); break; case Function: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); - append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - append_fmt( result, "\n\tReturnType: %S", ReturnType ? ReturnType->to_string() : "Null" ); - append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); - append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes: %S", self->Attributes ? to_string(self->Attributes) : "Null" ); + append_fmt( result, "\n\tSpecs : %S", self->Specs ? to_string(self->Specs) : "Null" ); + append_fmt( result, "\n\tReturnType: %S", self->ReturnType ? to_string(self->ReturnType) : "Null" ); + append_fmt( result, "\n\tParams : %S", self->Params ? to_string(self->Params) : "Null" ); + append_fmt( result, "\n\tBody : %S", self->Body ? debug_str(self->Body) : "Null" ); break; case Function_Fwd: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); - append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - append_fmt( result, "\n\tReturnType: %S", ReturnType ? ReturnType->to_string() : "Null" ); - append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes: %S", self->Attributes ? to_string(self->Attributes) : "Null" ); + append_fmt( result, "\n\tSpecs : %S", self->Specs ? to_string(self->Specs) : "Null" ); + append_fmt( result, "\n\tReturnType: %S", self->ReturnType ? to_string(self->ReturnType) : "Null" ); + append_fmt( result, "\n\tParams : %S", self->Params ? to_string(self->Params) : "Null" ); break; case Module: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); break; case Operator: case Operator_Member: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); - append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - append_fmt( result, "\n\tReturnType: %S", ReturnType ? ReturnType->to_string() : "Null" ); - append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); - append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); - append_fmt( result, "\n\tOp : %S", to_str( Op ) ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes: %S", self->Attributes ? to_string(self->Attributes) : "Null" ); + append_fmt( result, "\n\tSpecs : %S", self->Specs ? to_string(self->Specs) : "Null" ); + append_fmt( result, "\n\tReturnType: %S", self->ReturnType ? to_string(self->ReturnType) : "Null" ); + append_fmt( result, "\n\tParams : %S", self->Params ? to_string(self->Params) : "Null" ); + append_fmt( result, "\n\tBody : %S", self->Body ? debug_str(self->Body) : "Null" ); + append_fmt( result, "\n\tOp : %S", to_str( self->Op ) ); break; case Operator_Fwd: case Operator_Member_Fwd: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); - append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - append_fmt( result, "\n\tReturnType: %S", ReturnType ? ReturnType->to_string() : "Null" ); - append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); - append_fmt( result, "\n\tOp : %S", to_str( Op ) ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes: %S", self->Attributes ? to_string(self->Attributes) : "Null" ); + append_fmt( result, "\n\tSpecs : %S", self->Specs ? to_string(self->Specs) : "Null" ); + append_fmt( result, "\n\tReturnType: %S", self->ReturnType ? to_string(self->ReturnType) : "Null" ); + append_fmt( result, "\n\tParams : %S", self->Params ? to_string(self->Params) : "Null" ); + append_fmt( result, "\n\tOp : %S", to_str( self->Op ) ); break; case Operator_Cast: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - append_fmt( result, "\n\tValueType : %S", ValueType ? ValueType->to_string() : "Null" ); - append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tSpecs : %S", self->Specs ? to_string(self->Specs) : "Null" ); + append_fmt( result, "\n\tValueType : %S", self->ValueType ? to_string(self->ValueType) : "Null" ); + append_fmt( result, "\n\tBody : %S", self->Body ? debug_str(self->Body) : "Null" ); break; case Operator_Cast_Fwd: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - append_fmt( result, "\n\tValueType : %S", ValueType ? ValueType->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tSpecs : %S", self->Specs ? to_string(self->Specs) : "Null" ); + append_fmt( result, "\n\tValueType : %S", self->ValueType ? to_string(self->ValueType) : "Null" ); break; case Parameters: - append_fmt( result, "\n\tNumEntries: %d", NumEntries ); - append_fmt( result, "\n\tLast : %S", Last->Name ); - append_fmt( result, "\n\tNext : %S", Next->Name ); - append_fmt( result, "\n\tValueType : %S", ValueType ? ValueType->to_string() : "Null" ); - append_fmt( result, "\n\tValue : %S", Value ? Value->to_string() : "Null" ); + append_fmt( result, "\n\tNumEntries: %d", self->NumEntries ); + append_fmt( result, "\n\tLast : %S", self->Last->Name ); + append_fmt( result, "\n\tNext : %S", self->Next->Name ); + append_fmt( result, "\n\tValueType : %S", self->ValueType ? to_string(self->ValueType) : "Null" ); + append_fmt( result, "\n\tValue : %S", self->Value ? to_string(self->Value) : "Null" ); break; case Specifiers: { - append_fmt( result, "\n\tNumEntries: %d", NumEntries ); + append_fmt( result, "\n\tNumEntries: %d", self->NumEntries ); GEN_NS append( result, "\n\tArrSpecs: " ); s32 idx = 0; - s32 left = NumEntries; + s32 left = self->NumEntries; while ( left-- ) { - StrC spec = ESpecifier::to_str( ArrSpecs[idx] ); + StrC spec = ESpecifier::to_str( self->ArrSpecs[idx] ); append_fmt( result, "%.*s, ", spec.Len, spec.Ptr ); idx++; } - append_fmt( result, "\n\tNextSpecs: %S", NextSpecs ? NextSpecs->debug_str() : "Null" ); + append_fmt( result, "\n\tNextSpecs: %S", self->NextSpecs ? debug_str(self->NextSpecs) : "Null" ); } break; case Template: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); - append_fmt( result, "\n\tDeclaration: %S", Declaration ? Declaration->to_string() : "Null" ); + append_fmt( result, "\n\tParams : %S", self->Params ? to_string(self->Params) : "Null" ); + append_fmt( result, "\n\tDeclaration: %S", self->Declaration ? to_string(self->Declaration) : "Null" ); break; case Typedef: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tUnderlyingType: %S", UnderlyingType ? UnderlyingType->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tUnderlyingType: %S", self->UnderlyingType ? to_string(self->UnderlyingType) : "Null" ); break; case Typename: - append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - append_fmt( result, "\n\tReturnType : %S", ReturnType ? ReturnType->to_string() : "Null" ); - append_fmt( result, "\n\tParams : %S", Params ? Params->to_string() : "Null" ); - append_fmt( result, "\n\tArrExpr : %S", ArrExpr ? ArrExpr->to_string() : "Null" ); + append_fmt( result, "\n\tAttributes : %S", self->Attributes ? to_string(self->Attributes) : "Null" ); + append_fmt( result, "\n\tSpecs : %S", self->Specs ? to_string(self->Specs) : "Null" ); + append_fmt( result, "\n\tReturnType : %S", self->ReturnType ? to_string(self->ReturnType) : "Null" ); + append_fmt( result, "\n\tParams : %S", self->Params ? to_string(self->Params) : "Null" ); + append_fmt( result, "\n\tArrExpr : %S", self->ArrExpr ? to_string(self->ArrExpr) : "Null" ); break; case Union: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tAttributes: %S", Attributes ? Attributes->to_string() : "Null" ); - append_fmt( result, "\n\tBody : %S", Body ? Body->debug_str() : "Null" ); + append_fmt( result, "\n\tAttributes: %S", self->Attributes ? to_string(self->Attributes) : "Null" ); + append_fmt( result, "\n\tBody : %S", self->Body ? debug_str(self->Body) : "Null" ); break; case Using: - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - append_fmt( result, "\n\tUnderlyingType: %S", UnderlyingType ? UnderlyingType->to_string() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes : %S", self->Attributes ? to_string(self->Attributes) : "Null" ); + append_fmt( result, "\n\tUnderlyingType: %S", self->UnderlyingType ? to_string(self->UnderlyingType) : "Null" ); break; case Variable: - if ( Parent && Parent->Type == Variable ) + if ( self->Parent && self->Parent->Type == Variable ) { // Its a NextVar - append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - append_fmt( result, "\n\tValue : %S", Value ? Value->to_string() : "Null" ); - append_fmt( result, "\n\tBitfieldSize: %S", BitfieldSize ? BitfieldSize->to_string() : "Null" ); - append_fmt( result, "\n\tNextVar : %S", NextVar ? NextVar->debug_str() : "Null" ); + append_fmt( result, "\n\tSpecs : %S", self->Specs ? to_string(self->Specs) : "Null" ); + append_fmt( result, "\n\tValue : %S", self->Value ? to_string(self->Value) : "Null" ); + append_fmt( result, "\n\tBitfieldSize: %S", self->BitfieldSize ? to_string(self->BitfieldSize) : "Null" ); + append_fmt( result, "\n\tNextVar : %S", self->NextVar ? debug_str(self->NextVar) : "Null" ); break; } - if ( Prev ) - append_fmt( result, "\n\tPrev: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); - if ( Next ) - append_fmt( result, "\n\tNext: %S %S", Prev->type_str(), Prev->Name ? Prev->Name : "Null" ); + if ( self->Prev ) + append_fmt( result, "\n\tPrev: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); + if ( self->Next ) + append_fmt( result, "\n\tNext: %S %S", type_str(self->Prev), self->Prev->Name ? self->Prev->Name : "Null" ); - append_fmt( result, "\n\tInlineCmt : %S", InlineCmt ? InlineCmt->Content : "Null" ); - append_fmt( result, "\n\tAttributes : %S", Attributes ? Attributes->to_string() : "Null" ); - append_fmt( result, "\n\tSpecs : %S", Specs ? Specs->to_string() : "Null" ); - append_fmt( result, "\n\tValueType : %S", ValueType ? ValueType->to_string() : "Null" ); - append_fmt( result, "\n\tBitfieldSize: %S", BitfieldSize ? BitfieldSize->to_string() : "Null" ); - append_fmt( result, "\n\tValue : %S", Value ? Value->to_string() : "Null" ); - append_fmt( result, "\n\tNextVar : %S", NextVar ? NextVar->debug_str() : "Null" ); + append_fmt( result, "\n\tInlineCmt : %S", self->InlineCmt ? self->InlineCmt->Content : "Null" ); + append_fmt( result, "\n\tAttributes : %S", self->Attributes ? to_string(self->Attributes) : "Null" ); + append_fmt( result, "\n\tSpecs : %S", self->Specs ? to_string(self->Specs) : "Null" ); + append_fmt( result, "\n\tValueType : %S", self->ValueType ? to_string(self->ValueType) : "Null" ); + append_fmt( result, "\n\tBitfieldSize: %S", self->BitfieldSize ? to_string(self->BitfieldSize) : "Null" ); + append_fmt( result, "\n\tValue : %S", self->Value ? to_string(self->Value) : "Null" ); + append_fmt( result, "\n\tNextVar : %S", self->NextVar ? debug_str(self->NextVar) : "Null" ); break; } return result; } -AST* AST::duplicate() +AST* duplicate(AST* self) { using namespace ECode; AST* result = make_code().ast; - mem_copy( result, this, sizeof( AST ) ); + mem_copy( result, self, sizeof( AST ) ); result->Parent = nullptr; return result; @@ -412,169 +413,169 @@ void AST::to_string( String& result ) break; case Class: - cast().to_string_def( result ); + code_cast().to_string_def( result ); break; case Class_Fwd: - cast().to_string_fwd( result ); + code_cast().to_string_fwd( result ); break; case Constructor: - cast().to_string_def( result ); + code_cast().to_string_def( result ); break; case Constructor_Fwd: - cast().to_string_fwd( result ); + code_cast().to_string_fwd( result ); break; case Destructor: - cast().to_string_def( result ); + code_cast().to_string_def( result ); break; case Destructor_Fwd: - cast().to_string_fwd( result ); + code_cast().to_string_fwd( result ); break; case Enum: - cast().to_string_def( result ); + code_cast().to_string_def( result ); break; case Enum_Fwd: - cast().to_string_fwd( result ); + code_cast().to_string_fwd( result ); break; case Enum_Class: - cast().to_string_class_def( result ); + code_cast().to_string_class_def( result ); break; case Enum_Class_Fwd: - cast().to_string_class_fwd( result ); + code_cast().to_string_class_fwd( result ); break; case Export_Body: - cast().to_string_export( result ); + code_cast().to_string_export( result ); break; case Extern_Linkage: - cast().to_string( result ); + code_cast().to_string( result ); break; case Friend: - cast().to_string( result ); + code_cast().to_string( result ); break; case Function: - cast().to_string_def( result ); + code_cast().to_string_def( result ); break; case Function_Fwd: - cast().to_string_fwd( result ); + code_cast().to_string_fwd( result ); break; case Module: - cast().to_string( result ); + code_cast().to_string( result ); break; case Namespace: - cast().to_string( result ); + code_cast().to_string( result ); break; case Operator: case Operator_Member: - cast().to_string_def( result ); + code_cast().to_string_def( result ); break; case Operator_Fwd: case Operator_Member_Fwd: - cast().to_string_fwd( result ); + code_cast().to_string_fwd( result ); break; case Operator_Cast: - cast().to_string_def( result ); + code_cast().to_string_def( result ); break; case Operator_Cast_Fwd: - cast().to_string_fwd( result ); + code_cast().to_string_fwd( result ); break; case Parameters: - cast().to_string( result ); + code_cast().to_string( result ); break; case Preprocess_Define: - cast().to_string( result ); + code_cast().to_string( result ); break; case Preprocess_If: - cast().to_string_if( result ); + code_cast().to_string_if( result ); break; case Preprocess_IfDef: - cast().to_string_ifdef( result ); + code_cast().to_string_ifdef( result ); break; case Preprocess_IfNotDef: - cast().to_string_ifndef( result ); + code_cast().to_string_ifndef( result ); break; case Preprocess_Include: - cast().to_string( result ); + code_cast().to_string( result ); break; case Preprocess_ElIf: - cast().to_string_elif( result ); + code_cast().to_string_elif( result ); break; case Preprocess_Else: - cast().to_string_else( result ); + code_cast().to_string_else( result ); break; case Preprocess_EndIf: - cast().to_string_endif( result ); + code_cast().to_string_endif( result ); break; case Preprocess_Pragma: - cast().to_string( result ); + code_cast().to_string( result ); break; case Specifiers: - cast().to_string( result ); + code_cast().to_string( result ); break; case Struct: - cast().to_string_def( result ); + code_cast().to_string_def( result ); break; case Struct_Fwd: - cast().to_string_fwd( result ); + code_cast().to_string_fwd( result ); break; case Template: - cast().to_string( result ); + code_cast().to_string( result ); break; case Typedef: - cast().to_string( result ); + code_cast().to_string( result ); break; case Typename: - cast().to_string( result ); + code_cast().to_string( result ); break; case Union: - cast().to_string( result ); + code_cast().to_string( result ); break; case Using: - cast().to_string( result ); + code_cast().to_string( result ); break; case Using_Namespace: - cast().to_string_ns( result ); + code_cast().to_string_ns( result ); break; case Variable: - cast().to_string( result ); + code_cast().to_string( result ); break; case Enum_Body: @@ -585,7 +586,7 @@ void AST::to_string( String& result ) case Namespace_Body: case Struct_Body: case Union_Body: - cast().to_string( result ); + code_cast().to_string( result ); break; } } @@ -1157,7 +1158,7 @@ bool AST::validate_body() #define CheckEntries( Unallowed_Types ) \ do \ { \ - for ( Code entry : cast() ) \ + for ( Code entry : code_cast() ) \ { \ switch ( entry->Type ) \ { \ @@ -1175,7 +1176,7 @@ bool AST::validate_body() CheckEntries( GEN_AST_BODY_CLASS_UNALLOWED_TYPES ); break; case Enum_Body: - for ( Code entry : cast() ) + for ( Code entry : code_cast() ) { if ( entry->Type != Untyped ) { @@ -1194,7 +1195,7 @@ bool AST::validate_body() CheckEntries( GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES ); break; case Global_Body: - for (Code entry : cast()) + for (Code entry : code_cast()) { switch (entry->Type) { @@ -1227,7 +1228,7 @@ bool AST::validate_body() CheckEntries( GEN_AST_BODY_STRUCT_UNALLOWED_TYPES ); break; case Union_Body: - for ( Code entry : Body->cast() ) + for ( Code entry : Body->code_cast() ) { if ( entry->Type != Untyped ) { diff --git a/project/components/ast.hpp b/project/components/ast.hpp index a2a1fd8..533e705 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -146,6 +146,11 @@ namespace parser struct Token; } +template< class Type> forceinline Type tmpl_cast( Code* self ) { return * rcast( Type*, self ); } +#if ! GEN_COMPILER_C && 0 +template< class Type> forceinline Type tmpl_cast( Code& self ) { return * rcast( Type*, & self ); } +#endif + /* AST* wrapper - Not constantly have to append the '*' as this is written often.. @@ -178,7 +183,7 @@ struct Code Using_Code( Code ); template< class Type > - forceinline Type cast() + forceinline Type code_cast() { return * rcast( Type*, this ); } @@ -248,33 +253,53 @@ static_assert( sizeof(Code) == sizeof(Code_POD), "ERROR: Code is not POD" ); // Desired width of the AST data structure. constexpr int const AST_POD_Size = 128; +void append ( AST* self, AST* other ); +char const* debug_str ( AST* self ); +AST* duplicate ( AST* self ); +Code* entry ( AST* self, u32 idx ); +bool has_entries( AST* self ); +bool is_body ( AST* self ); +String to_string ( AST* self ); +char const* type_str ( AST* self ); + +#if GEN_CPP_SUPPORT_REFERENCES +void append ( AST& self, AST& other ) { return append(& self, & other); } +bool is_body ( AST& self ) { return is_body(& self); } +char const* debug_str( AST& self ) { return debug_str( & self ); } +String to_string( AST& self ) { return to_string( & self ); } +char const* type_str ( AST& self ) { return type_str( & self ); } +#endif + /* Simple AST POD with functionality to seralize into C++ syntax. */ struct AST { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES # pragma region Member Functions - void append ( AST* other ); - char const* debug_str (); - AST* duplicate (); - Code& entry ( u32 idx ); + void append ( AST* other ) { GEN_NS append(this, other); } + char const* debug_str () { return GEN_NS debug_str(this); } + AST* duplicate () { return GEN_NS duplicate(this); } + Code* entry ( u32 idx ) { return GEN_NS entry(this, idx); } bool has_entries(); bool is_equal ( AST* other ); - bool is_body(); - char const* type_str(); + bool is_body() { return GEN_NS is_body(this); } + char const* type_str() { return GEN_NS type_str(this); } bool validate_body(); - String to_string(); - - neverinline - void to_string( String& result ); + String to_string(); //{ return GEN_NS to_string(this); } template< class Type > - forceinline Type cast() + forceinline Type code_cast() { return * this; } + neverinline + void to_string( String& result ); +# pragma endregion Member Functions +#endif + operator Code(); operator CodeBody(); operator CodeAttributes(); @@ -305,7 +330,6 @@ struct AST operator CodeUnion(); operator CodeUsing(); operator CodeVar(); -# pragma endregion Member Functions constexpr static int ArrSpecs_Cap = @@ -446,12 +470,9 @@ struct AST_POD }; }; -struct test { - SpecifierT ArrSpecs[AST::ArrSpecs_Cap]; // Specifiers - AST* NextSpecs; // Specifiers; If ArrSpecs is full, then NextSpecs is used. -}; -constexpr int pls = sizeof(test); +// TODO(Ed): Convert +String to_string ( AST* self ) { return self->to_string(); } // Its intended for the AST to have equivalent size to its POD. // All extra functionality within the AST namespace should just be syntatic sugar. diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 0a1df21..170feab 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -176,14 +176,14 @@ void CodeClass::to_string_def( String& result ) append_fmt( result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); - CodeType interface = ast->ParentType->Next->cast< CodeType >(); + CodeType interface = ast->ParentType->Next->code_cast< CodeType >(); if ( interface ) append( result, "\n" ); while ( interface ) { append_fmt( result, ", %S", interface.to_string() ); - interface = interface->Next ? interface->Next->cast< CodeType >() : CodeType { nullptr }; + interface = interface->Next ? interface->Next->code_cast< CodeType >() : CodeType { nullptr }; } } else if ( ast->Name ) @@ -353,7 +353,10 @@ void CodeEnum::to_string_fwd( String& result ) if ( ast->Attributes ) append_fmt( result, "%S ", ast->Attributes.to_string() ); - append_fmt( result, "enum %S : %S", ast->Name, ast->UnderlyingType.to_string() ); + if ( ast->UnderlyingType ) + append_fmt( result, "enum %S : %S", ast->Name, ast->UnderlyingType.to_string() ); + else + append_fmt( result, "enum %S", ast->Name ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { @@ -1007,14 +1010,14 @@ void CodeStruct::to_string_def( String& result ) append_fmt( result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); - CodeType interface = ast->ParentType->Next->cast< CodeType >(); + CodeType interface = ast->ParentType->Next->code_cast< CodeType >(); if ( interface ) append( result, "\n" ); while ( interface ) { append_fmt( result, ", %S", interface.to_string() ); - interface = interface->Next ? interface->Next->cast< CodeType >() : CodeType { nullptr }; + interface = interface->Next ? interface->Next->code_cast< CodeType >() : CodeType { nullptr }; } } else if ( ast->Name ) diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index 8bab98e..6d211d0 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -11,37 +11,30 @@ struct CodeBody void append( Code other ) { - if (other.is_body()) - { - append( other.cast() ); + GEN_ASSERT(other.ast != nullptr); + + if (other.is_body()) { + append( cast(CodeBody, & other) ); } - raw()->append( other.ast ); + + GEN_NS append( raw(), other.ast ); } void append( CodeBody body ) { - for ( Code entry : body ) - { + for ( Code entry : body ) { append( entry ); } } - bool has_entries() - { - return rcast( AST*, ast )->has_entries(); - } + bool has_entries() { return GEN_NS has_entries(rcast( AST*, ast )); } + AST* raw() { return rcast( AST*, ast ); } + void to_string( String& result ); void to_string_export( String& result ); - AST* raw() - { - return rcast( AST*, ast ); - } - AST_Body* operator->() - { - return ast; - } - operator Code() - { - return * rcast( Code*, this ); - } + + AST_Body* operator->() { return ast; } + + operator Code() { return * rcast( Code*, this ); } + #pragma region Iterator Code begin() { diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index c449e0d..ec26f08 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -11,7 +11,7 @@ inline char const* Code::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code Code::duplicate() @@ -87,7 +87,7 @@ inline char const* CodeBody::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeBody::duplicate() @@ -163,7 +163,7 @@ inline char const* CodeAttributes::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeAttributes::duplicate() @@ -259,7 +259,7 @@ inline char const* CodeComment::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeComment::duplicate() @@ -355,7 +355,7 @@ inline char const* CodeConstructor::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeConstructor::duplicate() @@ -451,7 +451,7 @@ inline char const* CodeClass::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeClass::duplicate() @@ -527,7 +527,7 @@ inline char const* CodeDefine::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeDefine::duplicate() @@ -623,7 +623,7 @@ inline char const* CodeDestructor::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeDestructor::duplicate() @@ -719,7 +719,7 @@ inline char const* CodeEnum::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeEnum::duplicate() @@ -815,7 +815,7 @@ inline char const* CodeExec::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeExec::duplicate() @@ -911,7 +911,7 @@ inline char const* CodeExtern::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeExtern::duplicate() @@ -1007,7 +1007,7 @@ inline char const* CodeFriend::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeFriend::duplicate() @@ -1103,7 +1103,7 @@ inline char const* CodeFn::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeFn::duplicate() @@ -1199,7 +1199,7 @@ inline char const* CodeInclude::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeInclude::duplicate() @@ -1295,7 +1295,7 @@ inline char const* CodeModule::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeModule::duplicate() @@ -1391,7 +1391,7 @@ inline char const* CodeNS::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeNS::duplicate() @@ -1487,7 +1487,7 @@ inline char const* CodeOperator::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeOperator::duplicate() @@ -1583,7 +1583,7 @@ inline char const* CodeOpCast::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeOpCast::duplicate() @@ -1679,7 +1679,7 @@ inline char const* CodeParam::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeParam::duplicate() @@ -1755,7 +1755,7 @@ inline char const* CodePragma::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodePragma::duplicate() @@ -1851,7 +1851,7 @@ inline char const* CodePreprocessCond::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodePreprocessCond::duplicate() @@ -1947,7 +1947,7 @@ inline char const* CodeSpecifiers::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeSpecifiers::duplicate() @@ -2023,7 +2023,7 @@ inline char const* CodeStruct::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeStruct::duplicate() @@ -2099,7 +2099,7 @@ inline char const* CodeTemplate::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeTemplate::duplicate() @@ -2195,7 +2195,7 @@ inline char const* CodeType::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeType::duplicate() @@ -2291,7 +2291,7 @@ inline char const* CodeTypedef::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeTypedef::duplicate() @@ -2387,7 +2387,7 @@ inline char const* CodeUnion::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeUnion::duplicate() @@ -2483,7 +2483,7 @@ inline char const* CodeUsing::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeUsing::duplicate() @@ -2579,7 +2579,7 @@ inline char const* CodeVar::debug_str() { if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast( AST*, ast )->debug_str(); + return GEN_NS debug_str( rcast( AST*, ast ) ); } inline Code CodeVar::duplicate() diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index 34bdf5b..8178ab2 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -4,56 +4,62 @@ #endif inline -void AST::append( AST* other ) +void append( AST* self, AST* other ) { + GEN_ASSERT(self != nullptr); + GEN_ASSERT(other != nullptr); + if ( other->Parent ) - other = other->duplicate(); + other = duplicate(other); - other->Parent = this; + other->Parent = self; - if ( Front == nullptr ) + if ( self->Front == nullptr ) { - Front = other; - Back = other; + self->Front = other; + self->Back = other; - NumEntries++; + self->NumEntries++; return; } AST* - Current = Back; + Current = self->Back; Current->Next = other; other->Prev = Current; - Back = other; - NumEntries++; + self->Back = other; + self->NumEntries++; } inline -Code& AST::entry( u32 idx ) +Code* entry( AST* self, u32 idx ) { - AST** current = & Front; + GEN_ASSERT(self != nullptr); + AST** current = & self->Front; while ( idx >= 0 && current != nullptr ) { if ( idx == 0 ) - return * rcast( Code*, current); + return rcast( Code*, current); current = & ( * current )->Next; idx--; } - return * rcast( Code*, current); + return rcast( Code*, current); } inline -bool AST::has_entries() +bool has_entries(AST* self) { - return NumEntries > 0; + GEN_ASSERT(self != nullptr); + return self->NumEntries > 0; } inline -bool AST::is_body() +bool is_body(AST* self) { - switch (Type) + GEN_ASSERT(self != nullptr); + switch (self->Type) { case ECode::Enum_Body: case ECode::Class_Body: @@ -70,9 +76,10 @@ bool AST::is_body() } inline -char const* AST::type_str() +char const* type_str(AST* self) { - return ECode::to_str( Type ); + GEN_ASSERT(self != nullptr); + return ECode::to_str( self->Type ); } inline @@ -117,7 +124,7 @@ void CodeParam::append( CodeParam other ) AST* entry = (AST*) other.ast; if ( entry->Parent ) - entry = entry->duplicate(); + entry = GEN_NS duplicate( entry ); entry->Parent = self; diff --git a/project/components/interface.cpp b/project/components/interface.cpp index c64f937..b62d537 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -30,7 +30,7 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s last = & back(Global_AllocatorBuckets); } - return alloc_align( allocator_info(* last), size, alignment ); + return alloc_align( allocator_info(last), size, alignment ); } case EAllocation_FREE: { @@ -306,7 +306,7 @@ void deinit() do { Pool* code_pool = & CodePools[index]; - free(* code_pool); + free(code_pool); index++; } while ( left--, left ); @@ -316,7 +316,7 @@ void deinit() do { Arena* string_arena = & StringArenas[index]; - free(* string_arena); + free(string_arena); index++; } while ( left--, left ); @@ -326,7 +326,7 @@ void deinit() free(CodePools); free(StringArenas); - free(LexArena); + free(& LexArena); free(PreprocessorDefines); @@ -335,7 +335,7 @@ void deinit() do { Arena* bucket = & Global_AllocatorBuckets[ index ]; - free(* bucket); + free(bucket); index++; } while ( left--, left ); @@ -387,7 +387,7 @@ AllocatorInfo get_string_allocator( s32 str_length ) last = & back(StringArenas); } - return allocator_info(* last); + return allocator_info(last); } // Will either make or retrive a code string. @@ -425,7 +425,7 @@ Code make_code() allocator = & back(CodePools); } - Code result { rcast( AST*, alloc( allocator_info(* allocator), sizeof(AST) )) }; + Code result { rcast( AST*, alloc( allocator_info(allocator), sizeof(AST) )) }; mem_set( result.ast, 0, sizeof(AST) ); // result->Type = ECode::Invalid; diff --git a/project/components/interface.untyped.cpp b/project/components/interface.untyped.cpp index 217a5d2..bafc313 100644 --- a/project/components/interface.untyped.cpp +++ b/project/components/interface.untyped.cpp @@ -17,7 +17,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) char tok_map_mem[ TokenFmt_TokenMap_MemSize ]; tok_map_arena = arena_init_from_memory( tok_map_mem, sizeof(tok_map_mem) ); - tok_map = hashtable_init( allocator_info(tok_map_arena) ); + tok_map = hashtable_init( allocator_info(& tok_map_arena) ); s32 left = num_tokens - 1; @@ -95,7 +95,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) } clear(tok_map); - free(tok_map_arena); + free(& tok_map_arena); ssize result = buf_size - remaining; diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index 1d0f559..c0e9c9b 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -1925,7 +1925,7 @@ CodeBody def_global_body( s32 num, ... ) switch (entry->Type) { case Global_Body: - result.append( entry.cast() ) ; + result.append( entry.code_cast() ) ; continue; GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES @@ -1966,7 +1966,7 @@ CodeBody def_global_body( s32 num, Code* codes ) switch (entry->Type) { case Global_Body: - result.append( entry.cast() ) ; + result.append( entry.code_cast() ) ; continue; GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index f2fe4d3..61e4283 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -579,7 +579,7 @@ TokArray lex( StrC content ) if ( left <= 0 ) { log_failure( "gen::lex: no tokens found (only whitespace provided)" ); - return { { nullptr }, 0 }; + return { {}, 0 }; } foreach( StringCached, entry, PreprocessorDefines ) @@ -652,7 +652,7 @@ TokArray lex( StrC content ) continue; case Lex_ReturnNull: - return { { nullptr }, 0 }; + return { {}, 0 }; } } case '.': @@ -1256,7 +1256,7 @@ TokArray lex( StrC content ) if ( num(Tokens) == 0 ) { log_failure( "Failed to lex any tokens" ); - return { { nullptr }, 0 }; + return { {}, 0 }; } clear(defines); diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 7801ab0..b04fd0a 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -132,12 +132,12 @@ bool TokArray::__eat( TokType type ) internal void init() { - Tokens = array_init_reserve( allocator_info(LexArena) + Tokens = array_init_reserve( allocator_info( & LexArena) , ( LexAllocator_Size - sizeof( ArrayHeader ) ) / sizeof(Token) ); - fixed_arena_init(defines_map_arena); - defines = hashtable_init_reserve( allocator_info(defines_map_arena), 256 ); + fixed_arena_init(& defines_map_arena); + defines = hashtable_init_reserve( allocator_info( & defines_map_arena), 256 ); } internal @@ -714,7 +714,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) char interface_arr_mem[ kilobytes(4) ] {0}; Array interfaces; { Arena arena = arena_init_from_memory( interface_arr_mem, kilobytes(4) ); - interfaces = array_init_reserve( allocator_info(arena), 4 ); + interfaces = array_init_reserve( allocator_info(& arena), 4 ); } // TODO(Ed) : Make an AST_DerivedType, we'll store any arbitary derived type into there as a linear linked list of them. @@ -4910,10 +4910,12 @@ CodeTypedef parse_typedef() || currtok.Type == TokType::Decl_Struct || currtok.Type == TokType::Decl_Union; + // This code is highly correlated with parse_complicated_definition if ( is_complicated ) { TokArray tokens = Context.Tokens; + TokType which = currtok.Type; s32 idx = tokens.Idx; s32 level = 0; @@ -4929,73 +4931,80 @@ CodeTypedef parse_typedef() break; } - if ( (idx - 2 ) == tokens.Idx ) + Token pre_foward_tok = currtok; + if ( (idx - 3 ) == tokens.Idx ) { + log_fmt("Identified forward typedef\n"); // Its a forward declaration only - type = parse_forward_or_definition( currtok.Type, from_typedef ); + type = parse_forward_or_definition( which, from_typedef ); // typedef } - - Token tok = tokens[ idx - 1 ]; - if ( tok.Type == TokType::Identifier ) + else { - tok = tokens[ idx - 2 ]; - - bool is_indirection = tok.Type == TokType::Ampersand - || tok.Type == TokType::Star; - - bool ok_to_parse = false; - - if ( tok.Type == TokType::BraceCurly_Close ) + Token tok = tokens.Arr[ idx - 1 ]; + if ( tok.Type == TokType::Identifier ) { - // Its an inplace definition - // typdef { ... } ; - ok_to_parse = true; - } - else if ( tok.Type == TokType::Identifier && tokens[ idx - 3 ].Type == TokType::Decl_Struct ) - { - // Its a variable with type ID using struct namespace. - // ; - ok_to_parse = true; - } - else if ( is_indirection ) - { - // Its a indirection type with type ID using struct namespace. - // * ; - ok_to_parse = true; - } + log_fmt("Found id\n"); + tok = tokens.Arr[ idx - 2 ]; - if ( ! ok_to_parse ) + bool is_indirection = tok.Type == TokType::Ampersand + || tok.Type == TokType::Star; + + bool ok_to_parse = false; + + Token temp_3 = tokens.Arr[ idx - 3 ]; + + if ( tok.Type == TokType::BraceCurly_Close ) + { + // Its an inplace definition + // typedef { ... } ; + ok_to_parse = true; + } + else if ( tok.Type == TokType::Identifier && tokens.Arr[ idx - 3 ].Type == which ) + { + // Its a variable with type ID using which namespace. + // typedef ; + ok_to_parse = true; + } + else if ( is_indirection ) + { + // Its a indirection type with type ID using struct namespace. + // typedef * ; + ok_to_parse = true; + } + + if ( ! ok_to_parse ) + { + log_failure( "Unsupported or bad member definition after struct declaration\n%s", Context.to_string() ); + Context.pop(); + return CodeInvalid; + } + + // TODO(Ed) : I'm not sure if I have to use parse_type here, I'd rather not as that would complicate parse_type. + // type = parse_type(); + type = parse_forward_or_definition( which, from_typedef ); + // typedef + } + else if ( tok.Type == TokType::BraceCurly_Close ) + { + // Its a definition + // { ... }; + type = parse_forward_or_definition( currtok.Type, from_typedef ); + // typedef + } + else if ( tok.Type == TokType::BraceSquare_Close) + { + // Its an array definition + // [ ... ]; + type = parse_type(); + // typedef + } + else { log_failure( "Unsupported or bad member definition after struct declaration\n%s", Context.to_string() ); Context.pop(); return CodeInvalid; } - - // TODO(Ed) : I'm not sure if I have to use parse_type here, I'd rather not as that would complicate parse_type. - // type = parse_type(); - type = parse_forward_or_definition( currtok.Type, from_typedef ); - // typedef - } - else if ( tok.Type == TokType::BraceCurly_Close ) - { - // Its a definition - // { ... }; - type = parse_forward_or_definition( currtok.Type, from_typedef ); - // typedef - } - else if ( tok.Type == TokType::BraceSquare_Close) - { - // Its an array definition - // [ ... ]; - type = parse_type(); - // typedef - } - else - { - log_failure( "Unsupported or bad member definition after struct declaration\n%s", Context.to_string() ); - Context.pop(); - return CodeInvalid; } } else @@ -5057,7 +5066,7 @@ CodeTypedef parse_typedef() // Type needs to be aware of its parent so that it can be serialized properly. if ( type->Type == Typename && array_expr && array_expr->Type != Invalid ) - type.cast()->ArrExpr = array_expr; + type.code_cast()->ArrExpr = array_expr; if ( inline_cmt ) result->InlineCmt = inline_cmt; diff --git a/project/components/types.hpp b/project/components/types.hpp index 35975ed..e7a9e81 100644 --- a/project/components/types.hpp +++ b/project/components/types.hpp @@ -25,7 +25,7 @@ enum AccessSpec enum_underlying(u32) AccessSpec_SizeDef = GEN_U32_MAX, }; -static_assert( size_of(AccessSpec) == size_of(u32)); +static_assert( size_of(AccessSpec) == size_of(u32), "AccessSpec not u32 size" ); inline char const* to_str( AccessSpec type ) @@ -54,7 +54,7 @@ enum CodeFlag enum_underlying(u32) CodeFlag_SizeDef = GEN_U32_MAX, }; -static_assert( size_of(CodeFlag) == size_of(u32)); +static_assert( size_of(CodeFlag) == size_of(u32), "CodeFlag not u32 size" ); // Used to indicate if enum definitoin is an enum class or regular enum. enum EnumDecl enum_underlying(u8) @@ -77,7 +77,7 @@ enum ModuleFlag enum_underlying(u32) ModuleFlag_SizeDef = GEN_U32_MAX, }; -static_assert( size_of(ModuleFlag) == size_of(u32)); +static_assert( size_of(ModuleFlag) == size_of(u32), "ModuleFlag not u32 size" ); inline StrC to_str( ModuleFlag flag ) @@ -110,4 +110,4 @@ enum EPreprocessCond enum_underlying(u32) EPreprocessCond_SizeDef = GEN_U32_MAX, }; -static_assert( size_of(EPreprocessCond) == size_of(u32)); +static_assert( size_of(EPreprocessCond) == size_of(u32), "EPreprocessCond not u32 size" ); diff --git a/project/dependencies/macros.hpp b/project/dependencies/macros.hpp index 791ad1e..91a1dd4 100644 --- a/project/dependencies/macros.hpp +++ b/project/dependencies/macros.hpp @@ -23,7 +23,11 @@ #define bitfield_is_equal( Type, Field, Mask ) ( (Type(Mask) & Type(Field)) == Type(Mask) ) #endif + #if ! GEN_C_COMPILER +# ifndef cast +# define cast( type, value ) (tmpl_cast( value )) +# endif # ifndef ccast # define ccast( type, value ) ( const_cast< type >( (value) ) ) # endif @@ -37,11 +41,14 @@ # define scast( type, value ) static_cast< type >( value ) # endif #else +# ifndef cast +# define cast( type, value ) ( (type)(value) ) +# endif # ifndef ccast # define ccast( type, value ) ( (type)(value) ) # endif # ifndef pcast -# define pcast( type, value ) ( (type)(value) ) +# define pcast( type, value ) ( * (type*)(value) ) # endif # ifndef rcast # define rcast( type, value ) ( (type)(value) ) @@ -181,8 +188,14 @@ # endif #endif -#if !defined(GEN_SUPPORT_CPP_MEMBER_FEATURES) && (!GEN_COMPILER_C || __STDC_VERSION__ < 202311L) -# define GEN_SUPPORT_CPP_MEMBER_FEATURES 0 +#if !defined(GEN_SUPPORT_CPP_REFERENCES) && (GEN_COMPILER_C || __STDC_VERSION__ < 202311L) +# undef GEN_SUPPORT_CPP_REFERENCES +# define GEN_SUPPORT_CPP_REFERENCES 0 +#endif + +#if !defined(GEN_SUPPORT_CPP_MEMBER_FEATURES) && (GEN_COMPILER_C || __STDC_VERSION__ < 202311L) +# undef GEN_SUPPORT_CPP_MEMBER_FEATURES +# define GEN_SUPPORT_CPP_MEMBER_FEATURES 0 #endif #if !defined(typeof) && (!GEN_COMPILER_C || __STDC_VERSION__ < 202311L) @@ -215,4 +228,10 @@ # define enum_underlying(type) : type #endif +#if GEN_COMPILER_C +# ifndef nullptr +# define nullptr NULL +# endif +#endif + #pragma endregion Macros diff --git a/project/dependencies/memory.hpp b/project/dependencies/memory.hpp index aeae598..0399248 100644 --- a/project/dependencies/memory.hpp +++ b/project/dependencies/memory.hpp @@ -62,21 +62,23 @@ void zero_size( void* ptr, ssize size ); //! Clears up an array. #define zero_array( a, count ) zero_size( ( a ), size_of( *( a ) ) * count ) -enum AllocType : u8 +enum AllocType_Def //enum_underlying(u8) { EAllocation_ALLOC, EAllocation_FREE, EAllocation_FREE_ALL, EAllocation_RESIZE, }; +typedef enum AllocType_Def AllocType; typedef void*(AllocatorProc)( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ); -struct AllocatorInfo +struct AllocatorInfo_Def { AllocatorProc* Proc; void* Data; }; +typedef struct AllocatorInfo_Def AllocatorInfo; enum AllocFlag { @@ -132,7 +134,7 @@ void* default_resize_align( AllocatorInfo a, void* ptr, ssize old_size, ssize ne void* heap_allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ); //! The heap allocator backed by operating system's memory manager. -constexpr AllocatorInfo heap( void ) { return { heap_allocator_proc, nullptr }; } +constexpr AllocatorInfo heap( void ) { AllocatorInfo allocator = { heap_allocator_proc, nullptr }; return allocator; } //! Helper to allocate memory using heap allocator. #define malloc( sz ) alloc( heap(), sz ) @@ -170,26 +172,26 @@ ssize gen_virtual_memory_page_size( ssize* alignment_out ); #pragma region Arena struct Arena; -AllocatorInfo allocator_info( Arena& arena ); +AllocatorInfo allocator_info( Arena* arena ); // Remove static keyword and rename allocator_proc void* arena_allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags); // Add these declarations after the Arena struct Arena arena_init_from_allocator(AllocatorInfo backing, ssize size); -Arena arena_init_from_memory( void* start, ssize size ); -Arena init_sub(Arena& parent, ssize size); -ssize alignment_of(Arena& arena, ssize alignment); +Arena arena_init_from_memory ( void* start, ssize size ); + +Arena init_sub (Arena* parent, ssize size); +ssize alignment_of (Arena* arena, ssize alignment); +void free (Arena* arena); +ssize size_remaining(Arena* arena, ssize alignment); // This id is defined by Unreal for asserts #pragma push_macro("check") #undef check -void check(Arena& arena); +void check(Arena* arena); #pragma pop_macro("check") -void free(Arena& arena); -ssize size_remaining(Arena& arena, ssize alignment); - struct Arena { AllocatorInfo Backing; @@ -200,29 +202,45 @@ struct Arena #if GEN_SUPPORT_CPP_MEMBER_FEATURES #pragma region Member Mapping - forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); } + forceinline operator AllocatorInfo() { return GEN_NS allocator_info(this); } forceinline static void* allocator_proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) { return GEN_NS arena_allocator_proc( allocator_data, type, size, alignment, old_memory, old_size, flags ); } forceinline static Arena init_from_memory( void* start, ssize size ) { return GEN_NS arena_init_from_memory( start, size ); } forceinline static Arena init_from_allocator( AllocatorInfo backing, ssize size ) { return GEN_NS arena_init_from_allocator( backing, size ); } forceinline static Arena init_sub( Arena& parent, ssize size ) { return GEN_NS arena_init_from_allocator( parent.Backing, size ); } - forceinline ssize alignment_of( ssize alignment ) { return GEN_NS alignment_of(* this, alignment); } - forceinline void free() { return GEN_NS free(* this); } - forceinline ssize size_remaining( ssize alignment ) { return GEN_NS size_remaining(* this, alignment); } + forceinline ssize alignment_of( ssize alignment ) { return GEN_NS alignment_of(this, alignment); } + forceinline void free() { return GEN_NS free(this); } + forceinline ssize size_remaining( ssize alignment ) { return GEN_NS size_remaining(this, alignment); } // This id is defined by Unreal for asserts #pragma push_macro("check") #undef check - forceinline void check() { GEN_NS check(* this); } + forceinline void check() { GEN_NS check(this); } #pragma pop_macro("check") #pragma endregion Member Mapping #endif }; +#if GEN_SUPPORT_CPP_REFERENCES +forceinline AllocatorInfo allocator_info(Arena& arena ) { return allocator_info(& arena); } +forceinline Arena init_sub (Arena& parent, ssize size) { return init_sub( & parent, size); } +forceinline ssize alignment_of (Arena& arena, ssize alignment) { return alignment_of( & arena, alignment); } +forceinline void free (Arena& arena) { return free(& arena); } +forceinline ssize size_remaining(Arena& arena, ssize alignment) { return size_remaining(& arena, alignment); } + +// This id is defined by Unreal for asserts +#pragma push_macro("check") +#undef check +forceinline void check(Arena& arena) { return check(& arena); }; +#pragma pop_macro("check") +#endif + + inline -AllocatorInfo allocator_info( Arena& arena ) { - return { arena_allocator_proc, &arena }; +AllocatorInfo allocator_info( Arena* arena ) { + GEN_ASSERT(arena != nullptr); + return { arena_allocator_proc, arena }; } inline @@ -251,18 +269,20 @@ Arena arena_init_from_allocator(AllocatorInfo backing, ssize size) { } inline -Arena init_sub(Arena& parent, ssize size) { - return arena_init_from_allocator(parent.Backing, size); +Arena init_sub(Arena* parent, ssize size) { + GEN_ASSERT(parent != nullptr); + return arena_init_from_allocator(parent->Backing, size); } inline -ssize alignment_of(Arena& arena, ssize alignment) +ssize alignment_of(Arena* arena, ssize alignment) { + GEN_ASSERT(arena != nullptr); ssize alignment_offset, result_pointer, mask; GEN_ASSERT(is_power_of_two(alignment)); alignment_offset = 0; - result_pointer = (ssize)arena.PhysicalStart + arena.TotalUsed; + result_pointer = (ssize)arena->PhysicalStart + arena->TotalUsed; mask = alignment - 1; if (result_pointer & mask) @@ -274,26 +294,29 @@ ssize alignment_of(Arena& arena, ssize alignment) #pragma push_macro("check") #undef check inline -void check(Arena& arena) +void check(Arena* arena) { - GEN_ASSERT(arena.TempCount == 0); + GEN_ASSERT(arena != nullptr ); + GEN_ASSERT(arena->TempCount == 0); } #pragma pop_macro("check") inline -void free(Arena& arena) +void free(Arena* arena) { - if (arena.Backing.Proc) + GEN_ASSERT(arena != nullptr); + if (arena->Backing.Proc) { - GEN_NS free(arena.Backing, arena.PhysicalStart); - arena.PhysicalStart = nullptr; + GEN_NS free(arena->Backing, arena->PhysicalStart); + arena->PhysicalStart = nullptr; } } inline -ssize size_remaining(Arena& arena, ssize alignment) +ssize size_remaining(Arena* arena, ssize alignment) { - ssize result = arena.TotalSize - (arena.TotalUsed + alignment_of(arena, alignment)); + GEN_ASSERT(arena != nullptr); + ssize result = arena->TotalSize - (arena->TotalUsed + alignment_of(arena, alignment)); return result; } #pragma endregion Arena @@ -302,9 +325,14 @@ ssize size_remaining(Arena& arena, ssize alignment) template struct FixedArena; -template AllocatorInfo allocator_info( FixedArena& fixed_arena ); template FixedArena fixed_arena_init(); -template ssize size_remaining(FixedArena& fixed_arena, ssize alignment); +template AllocatorInfo allocator_info(FixedArena* fixed_arena ); +template ssize size_remaining(FixedArena* fixed_arena, ssize alignment); + +#if GEN_SUPPORT_CPP_REFERENCES +template AllocatorInfo allocator_info( FixedArena& fixed_arena ) { return allocator_info(& fixed_arena); } +template ssize size_remaining(FixedArena& fixed_arena, ssize alignment) { return size_remaining( & fixed_arena, alignment); } +#endif // Just a wrapper around using an arena with memory associated with its scope instead of from an allocator. // Used for static segment or stack allocations. @@ -316,26 +344,29 @@ struct FixedArena #if GEN_SUPPORT_CPP_MEMBER_FEATURES #pragma region Member Mapping - forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); } + forceinline operator AllocatorInfo() { return GEN_NS allocator_info(this); } forceinline static FixedArena init() { FixedArena result; GEN_NS fixed_arena_init(result); return result; } - forceinline ssize size_remaining(ssize alignment) { GEN_NS size_remaining(*this, alignment); } + forceinline ssize size_remaining(ssize alignment) { GEN_NS size_remaining(this, alignment); } #pragma endregion Member Mapping #endif }; template inline -AllocatorInfo allocator_info( FixedArena& fixed_arena ) { return { arena_allocator_proc, & fixed_arena.arena }; } - -template inline -void fixed_arena_init(FixedArena& result) { - zero_size(& result.memory[0], Size); - result.arena = arena_init_from_memory(& result.memory[0], Size); +AllocatorInfo allocator_info( FixedArena* fixed_arena ) { + GEN_ASSERT(fixed_arena); + return { arena_allocator_proc, & fixed_arena->arena }; } template inline -ssize size_remaining(FixedArena& fixed_arena, ssize alignment) { - return size_remaining(fixed_arena.arena, alignment); +void fixed_arena_init(FixedArena* result) { + zero_size(& result->memory[0], Size); + result->arena = arena_init_from_memory(& result->memory[0], Size); +} + +template inline +ssize size_remaining(FixedArena* fixed_arena, ssize alignment) { + return size_remaining(fixed_arena->arena, alignment); } using Arena_1KB = FixedArena< kilobytes( 1 ) >; @@ -355,12 +386,19 @@ using Arena_4MB = FixedArena< megabytes( 4 ) >; #pragma region Pool struct Pool; -AllocatorInfo allocator_info(Pool& pool); -void* pool_allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags); +void* pool_allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags); + Pool pool_init(AllocatorInfo backing, ssize num_blocks, ssize block_size); Pool pool_init_align(AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align); +AllocatorInfo allocator_info(Pool* pool); +void clear(Pool* pool); +void free(Pool* pool); + +#if GEN_SUPPORT_CPP_REFERENCES +AllocatorInfo allocator_info(Pool& pool); void clear(Pool& pool); void free(Pool& pool); +#endif struct Pool { @@ -374,20 +412,20 @@ struct Pool #if GEN_SUPPORT_CPP_MEMBER_FEATURES #pragma region Member Mapping - forceinline operator AllocatorInfo() { return GEN_NS allocator_info(* this); } + forceinline operator AllocatorInfo() { return GEN_NS allocator_info(this); } forceinline static void* allocator_proc(void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags) { return GEN_NS pool_allocator_proc(allocator_data, type, size, alignment, old_memory, old_size, flags); } forceinline static Pool init(AllocatorInfo backing, ssize num_blocks, ssize block_size) { return GEN_NS pool_init(backing, num_blocks, block_size); } forceinline static Pool init_align(AllocatorInfo backing, ssize num_blocks, ssize block_size, ssize block_align) { return GEN_NS pool_init_align(backing, num_blocks, block_size, block_align); } - forceinline void clear() { GEN_NS clear(* this); } - forceinline void free() { GEN_NS free(* this); } + forceinline void clear() { GEN_NS clear( this); } + forceinline void free() { GEN_NS free( this); } #pragma endregion #endif }; inline -AllocatorInfo allocator_info(Pool& pool) { - return { pool_allocator_proc, &pool }; +AllocatorInfo allocator_info(Pool* pool) { + return { pool_allocator_proc, pool }; } inline @@ -396,9 +434,9 @@ Pool pool_init(AllocatorInfo backing, ssize num_blocks, ssize block_size) { } inline -void free(Pool& pool) { - if(pool.Backing.Proc) { - GEN_NS free(pool.Backing, pool.PhysicalStart); +void free(Pool* pool) { + if(pool->Backing.Proc) { + GEN_NS free(pool->Backing, pool->PhysicalStart); } } #pragma endregion Pool diff --git a/project/dependencies/platform.hpp b/project/dependencies/platform.hpp index 583b260..27a9a6b 100644 --- a/project/dependencies/platform.hpp +++ b/project/dependencies/platform.hpp @@ -125,9 +125,13 @@ # include # endif +#if GEN_COMPILER_C +#include +#endif + #pragma endregion Mandatory Includes -#if GEN_DONT_USE_NAMESPACE +#if GEN_DONT_USE_NAMESPACE || GEN_COMPILER_C # if GEN_COMPILER_C # define GEN_NS_ENUM_BEGIN # define GEN_NS_ENUM_END diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index 285e3e8..3058f87 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -305,7 +305,7 @@ bool are_equal(String const& lhs, StrC rhs) return false; for (ssize idx = 0; idx < length(lhs); ++idx) - if (lhs[idx] != rhs[idx]) + if (lhs[idx] != rhs.Ptr[idx]) return false; return true; @@ -319,7 +319,7 @@ ssize avail_space(String const& str) { inline char& back(String& str) { - return str.Data[length(str) - 1]; + return str[length(str) - 1]; } inline @@ -335,7 +335,7 @@ bool contains(String const& str, StrC substring) for (ssize idx = 0; idx <= main_len - sub_len; ++idx) { - if (str_compare(str.Data + idx, substring.Ptr, sub_len) == 0) + if (str_compare(str + idx, substring.Ptr, sub_len) == 0) return true; } @@ -355,7 +355,7 @@ bool contains(String const& str, String const& substring) for (ssize idx = 0; idx <= main_len - sub_len; ++idx) { - if (str_compare(str.Data + idx, substring.Data, sub_len) == 0) + if (str_compare(str + idx, substring, sub_len) == 0) return true; } @@ -380,7 +380,7 @@ String duplicate(String const& str, AllocatorInfo allocator) { inline void free(String& str) { - if (!str.Data) + if (! str) return; StringHeader& header = get_header(str); @@ -389,7 +389,7 @@ void free(String& str) { inline StringHeader& get_header(String& str) { - return *(StringHeader*)(str.Data - sizeof(StringHeader)); + return *(StringHeader*)(scast(char*, str) - sizeof(StringHeader)); } inline diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index e648ab6..975d08d 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -13,7 +13,7 @@ CodeBody gen_ecode( char const* path ) char scratch_mem[kilobytes(1)]; Arena scratch = arena_init_from_memory( scratch_mem, sizeof(scratch_mem) ); - file_read_contents( allocator_info(scratch), zero_terminate, path ); + file_read_contents( allocator_info( & scratch), zero_terminate, path ); CSV_Object csv_nodes; csv_parse( &csv_nodes, scratch_mem, GlobalAllocator, false ); @@ -59,7 +59,7 @@ CodeBody gen_eoperator( char const* path ) char scratch_mem[kilobytes(4)]; Arena scratch = arena_init_from_memory( scratch_mem, sizeof(scratch_mem) ); - file_read_contents( allocator_info(scratch), zero_terminate, path ); + file_read_contents( allocator_info(& scratch), zero_terminate, path ); CSV_Object csv_nodes; csv_parse( &csv_nodes, scratch_mem, GlobalAllocator, false ); @@ -115,7 +115,7 @@ CodeBody gen_especifier( char const* path ) char scratch_mem[kilobytes(4)]; Arena scratch = arena_init_from_memory( scratch_mem, sizeof(scratch_mem) ); - file_read_contents( allocator_info(scratch), zero_terminate, path ); + file_read_contents( allocator_info(& scratch), zero_terminate, path ); CSV_Object csv_nodes; csv_parse( &csv_nodes, scratch_mem, GlobalAllocator, false ); @@ -220,7 +220,7 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) char scratch_mem[kilobytes(16)]; Arena scratch = arena_init_from_memory( scratch_mem, sizeof(scratch_mem) ); - AllocatorInfo scratch_info = allocator_info(scratch); + AllocatorInfo scratch_info = allocator_info(& scratch); FileContents enum_content = file_read_contents( scratch_info, zero_terminate, etok_path ); @@ -342,8 +342,10 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) CodeBody gen_ast_inlines() { +#pragma push_macro("GEN_NS") #pragma push_macro("rcast") #pragma push_macro("log_failure") +#undef GEN_NS #undef rcast #undef log_failure char const* code_impl_tmpl = stringize( @@ -354,7 +356,7 @@ CodeBody gen_ast_inlines() if ( ast == nullptr ) return "Code::debug_str: AST is null!"; - return rcast(AST*, ast)->debug_str(); + return GEN_NS debug_str( rcast(AST*, ast) ); } inline Code ::duplicate() @@ -455,6 +457,7 @@ CodeBody gen_ast_inlines() } \n ); +#pragma pop_macro("GEN_NS") CodeBody impl_code = parse_global_body( token_fmt( "typename", StrC name(Code), code_impl_tmpl )); CodeBody impl_code_body = parse_global_body( token_fmt( "typename", StrC name(CodeBody), code_impl_tmpl )); diff --git a/scripts/build.ci.ps1 b/scripts/build.ci.ps1 index 67b2697..2a4e63a 100644 --- a/scripts/build.ci.ps1 +++ b/scripts/build.ci.ps1 @@ -226,6 +226,30 @@ if ( $c_library ) write-host "`nc_library generator completed in $($time_taken.TotalMilliseconds) ms" } Pop-Location + + $unit = join-path $path_c_library "gen.c" + $executable = join-path $path_build "gen_c_library_test.exe" + + if ($vendor -eq "clang") { + $compiler_args += "-x" + $compiler_args += "c" + } elseif ($vendor -eq "msvc") { + $compiler_args += "/TC" + } + + build-simple $path_build $includes $compiler_args $linker_args $unit $executable + + Push-Location $path_c_library + if ( Test-Path( $executable ) ) { + write-host "`nRunning c_library test" + $time_taken = Measure-Command { & $executable + | ForEach-Object { + write-host `t $_ -ForegroundColor Green + } + } + write-host "`nc_library generator completed in $($time_taken.TotalMilliseconds) ms" + } + Pop-Location } if ( $unreal ) From fec709cc7640eef46c43e0d31a60e7646b079ef3 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 21:59:43 -0500 Subject: [PATCH 025/112] Progresss --- docs/Readme.md | 2 + gen_c_library/c_library.cpp | 52 +++- .../components/memory.fixed_arena.hpp | 2 +- gen_c_library/components/misc.hpp | 6 +- gen_c_library/gen.c | 7 + project/Readme.md | 7 + project/components/interface.cpp | 34 +-- project/components/lexer.cpp | 30 +-- project/components/parser.cpp | 6 +- project/dependencies/containers.hpp | 231 +++++++++--------- project/dependencies/filesystem.cpp | 4 +- project/dependencies/parsing.cpp | 13 +- project/dependencies/parsing.hpp | 2 +- project/helpers/helper.hpp | 3 +- 14 files changed, 233 insertions(+), 166 deletions(-) create mode 100644 gen_c_library/gen.c diff --git a/docs/Readme.md b/docs/Readme.md index 7f2da37..4621d56 100644 --- a/docs/Readme.md +++ b/docs/Readme.md @@ -25,6 +25,8 @@ This library was written in a subset of C++ where the following are not used at * Exceptions Polymorphic & Member-functions are used as an ergonomic choice, along with a conserative use of operator overloads. +The base library itself does not use anything but C-like features to allow for generating a derviative compatiable with C (WIP). + There are only 4 template definitions in the entire library. (`Array`, `Hashtable`, `swap`, and `AST/Code::cast`) Two generic templated containers are used throughout the library: diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index f192f67..79baf1c 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -2,6 +2,7 @@ #define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_EXPOSE_BACKEND #define GEN_SUPPORT_CPP_MEMBER_FEATURES 1 +#define GEN_SUPPORT_CPP_REFERENCES 1 #include "../project/gen.cpp" #include "helpers/push_ignores.inline.hpp" @@ -135,26 +136,40 @@ int gen_main() case ECode::Using: { log_fmt("REPLACE THIS MANUALLY: %S\n", entry->Name); - CodeUsing using_ver = entry.cast(); + CodeUsing using_ver = entry.code_cast(); CodeTypedef typedef_ver = def_typedef(using_ver->Name, using_ver->UnderlyingType); memory.append(typedef_ver); } break; + case ECode::Function_Fwd: + { + CodeFn fn = entry.code_cast(); + if ( fn->Name.is_equal(txt("free")) ) + { + fn->Name = get_cached_string(txt("gen_free_ptr")); + } + memory.append(entry); + } + break; case ECode::Function: { - CodeFn fn = entry.cast(); + CodeFn fn = entry.code_cast(); s32 constexpr_found = fn->Specs.remove( ESpecifier::Constexpr ); if (constexpr_found > -1) { log_fmt("Found constexpr: %S\n", entry->to_string()); fn->Specs.append(ESpecifier::Inline); } + if ( fn->Name.is_equal(txt("free")) ) + { + fn->Name = get_cached_string(txt("gen_free_ptr")); + } memory.append(entry); } break; case ECode::Template: { - CodeTemplate tmpl = entry.cast(); + CodeTemplate tmpl = entry.code_cast(); if ( tmpl->Declaration->Name.contains(txt("swap"))) { CodeBody macro_swap = parse_global_body( txt(R"( @@ -297,7 +312,7 @@ int gen_main() { if ( entry->Name.is_equal(txt("String")) ) { - CodeTypedef c_def = parse_typedef(code( typedef Type* String; )); + CodeTypedef c_def = parse_typedef(code( typedef char* String; )); strings.append(c_def); strings.append(fmt_newline); ++ entry; @@ -307,6 +322,29 @@ int gen_main() } break; + case ECode::Struct: + { + CodeBody body = entry->Body->operator CodeBody(); + CodeBody new_body = def_body( entry->Body->Type ); + for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch + (body_entry->Type) { + case ECode::Preprocess_If: + { + b32 found = ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), body_entry, body ); + if (found) break; + + new_body.append(body_entry); + } + break; + default: + new_body.append(body_entry); + break; + } + entry->Body = rcast(AST*, new_body.ast); + strings.append(entry); + } + break; + default: strings.append(entry); break; @@ -316,8 +354,8 @@ int gen_main() Code filesystem = scan_file( project_dir "dependencies/filesystem.hpp" ); Code timing = scan_file( project_dir "dependencies/timing.hpp" ); - header.print( filesystem ); - header.print( timing ); + // header.print( filesystem ); + // header.print( timing ); header.print_fmt( "\nGEN_NS_END\n" ); header.print_fmt( roll_own_dependencies_guard_end ); @@ -335,6 +373,7 @@ int gen_main() CodeBody especifier = gen_especifier( project_dir "enums/ESpecifier.csv" ); CodeBody ast_inlines = gen_ast_inlines(); +#if 0 header.print_fmt("#pragma region Types\n"); header.print( types ); header.print( fmt_newline ); @@ -345,6 +384,7 @@ int gen_main() header.print( dump_to_scratch_and_retireve( especifier )); header.print( fmt_newline ); header.print_fmt("#pragma endregion Types\n\n"); + #endif } header.print( pop_ignores ); diff --git a/gen_c_library/components/memory.fixed_arena.hpp b/gen_c_library/components/memory.fixed_arena.hpp index d136ec5..e75a6eb 100644 --- a/gen_c_library/components/memory.fixed_arena.hpp +++ b/gen_c_library/components/memory.fixed_arena.hpp @@ -20,7 +20,7 @@ CodeBody gen_fixed_arenas() inline void fixed_arena_init_(FixedArena_* result) { zero_size(& result->memory[0], ); - result.arena = arena_init_from_memory(& result->memory[0], ); + result->arena = arena_init_from_memory(& result->memory[0], ); } inline diff --git a/gen_c_library/components/misc.hpp b/gen_c_library/components/misc.hpp index 109f42b..c323713 100644 --- a/gen_c_library/components/misc.hpp +++ b/gen_c_library/components/misc.hpp @@ -8,7 +8,7 @@ using SwapContentProc = CodeBody(void); b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body ) { b32 found = false; - CodePreprocessCond cond = entry_iter.cast(); + CodePreprocessCond cond = entry_iter.code_cast(); if ( cond->Content.contains(cond_sig) ) { log_fmt("Preprocess cond found: %S\n", cond->Content); @@ -44,7 +44,7 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body ) { bool found = false; - CodePragma possible_region = entry_iter.cast(); + CodePragma possible_region = entry_iter.code_cast(); String region_sig = string_fmt_buf(GlobalAllocator, "region %s", region_name.Ptr); String endregion_sig = string_fmt_buf(GlobalAllocator, "endregion %s", region_name.Ptr); @@ -58,7 +58,7 @@ bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_ (entry_iter->Type) { case ECode::Preprocess_Pragma: { - CodePragma possible_end_region = entry_iter.cast(); + CodePragma possible_end_region = entry_iter.code_cast(); if ( possible_end_region->Content.contains(endregion_sig) ) { // body.append(possible_end_region); continue_for = false; diff --git a/gen_c_library/gen.c b/gen_c_library/gen.c new file mode 100644 index 0000000..eb14df1 --- /dev/null +++ b/gen_c_library/gen.c @@ -0,0 +1,7 @@ +#define GEN_IMPLEMENTATION +#include "gen/gen.h" + +int main() +{ + // init(); +} diff --git a/project/Readme.md b/project/Readme.md index d1f2030..ecf7c8d 100644 --- a/project/Readme.md +++ b/project/Readme.md @@ -30,6 +30,13 @@ Feature Macros: * `GEN_ROLL_OWN_DEPENDENCIES` : Optional override so that user may define the dependencies themselves. * `GEN_DONT_ALLOW_INVALID_CODE` (Not implemented yet) : Will fail when an invalid code is constructed, parsed, or serialized. +By default the base library implementation strictly uses a C-like interface. This is to allow for the generation of a C-variant of the library using [gen_c_library](../gen_c_library/). However, the library was written in C++ and supports some of its features: + +* `GEN_SUPPORT_CPP_REFERENCES` : Will enable support for reference interface on some definitions +* `GEN_SUPPORT_CPP_MEMBER_FEATURES` : Will enable support for definitions to have their interface as members. + +*Note: A variant of the C++ library could be generated where those additonal support features are removed (see gen_c_library implementation for an idea of how)* + ## On multi-threading Currently unsupported. I want the library to be *stable* and *correct*, with the addition of exhausting all basic single-threaded optimizations before I consider multi-threading. diff --git a/project/components/interface.cpp b/project/components/interface.cpp index b62d537..9b3cbfb 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -11,7 +11,7 @@ internal void deinit(); internal void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, ssize alignment, void* old_memory, ssize old_size, u64 flags ) { - Arena* last = & back(Global_AllocatorBuckets); + Arena* last = back(& Global_AllocatorBuckets); switch ( type ) { @@ -24,10 +24,10 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s if ( bucket.PhysicalStart == nullptr ) GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets"); - if ( ! append( Global_AllocatorBuckets, bucket ) ) + if ( ! append( & Global_AllocatorBuckets, bucket ) ) GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets"); - last = & back(Global_AllocatorBuckets); + last = back(& Global_AllocatorBuckets); } return alloc_align( allocator_info(last), size, alignment ); @@ -51,10 +51,10 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s if ( bucket.PhysicalStart == nullptr ) GEN_FATAL( "Failed to create bucket for Global_AllocatorBuckets"); - if ( ! append( Global_AllocatorBuckets, bucket ) ) + if ( ! append( & Global_AllocatorBuckets, bucket ) ) GEN_FATAL( "Failed to append bucket to Global_AllocatorBuckets"); - last = & back(Global_AllocatorBuckets); + last = back(& Global_AllocatorBuckets); } void* result = alloc_align( last->Backing, size, alignment ); @@ -249,7 +249,7 @@ void init() if ( bucket.PhysicalStart == nullptr ) GEN_FATAL( "Failed to create first bucket for Global_AllocatorBuckets"); - append( Global_AllocatorBuckets, bucket ); + append( & Global_AllocatorBuckets, bucket ); } // Setup the arrays @@ -272,7 +272,7 @@ void init() if ( code_pool.PhysicalStart == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the code pool" ); - append(CodePools, code_pool ); + append( & CodePools, code_pool ); LexArena = arena_init_from_allocator( Allocator_Lexer, LexAllocator_Size ); @@ -281,7 +281,7 @@ void init() if ( string_arena.PhysicalStart == nullptr ) GEN_FATAL( "gen::init: Failed to initialize the string arena" ); - append(StringArenas, string_arena ); + append( & StringArenas, string_arena ); } // Setup the hash tables @@ -323,12 +323,12 @@ void deinit() destroy(StringCache); - free(CodePools); - free(StringArenas); + free( & CodePools); + free( & StringArenas); free(& LexArena); - free(PreprocessorDefines); + free(& PreprocessorDefines); index = 0; left = num(Global_AllocatorBuckets); @@ -373,7 +373,7 @@ void reset() AllocatorInfo get_string_allocator( s32 str_length ) { - Arena* last = & back(StringArenas); + Arena* last = back(& StringArenas); usize size_req = str_length + sizeof(StringHeader) + sizeof(char*); @@ -381,10 +381,10 @@ AllocatorInfo get_string_allocator( s32 str_length ) { Arena new_arena = arena_init_from_allocator( Allocator_StringArena, SizePer_StringArena ); - if ( ! append(StringArenas, new_arena ) ) + if ( ! append( & StringArenas, new_arena ) ) GEN_FATAL( "gen::get_string_allocator: Failed to allocate a new string arena" ); - last = & back(StringArenas); + last = back(& StringArenas); } return allocator_info(last); @@ -411,7 +411,7 @@ StringCached get_cached_string( StrC str ) // Used internally to retireve a Code object form the CodePool. Code make_code() { - Pool* allocator = & back(CodePools); + Pool* allocator = back( & CodePools); if ( allocator->FreeList == nullptr ) { Pool code_pool = pool_init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) ); @@ -419,10 +419,10 @@ Code make_code() if ( code_pool.PhysicalStart == nullptr ) GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePool allcoator returned nullptr." ); - if ( ! append( CodePools, code_pool ) ) + if ( ! append( & CodePools, code_pool ) ) GEN_FATAL( "gen::make_code: Failed to allocate a new code pool - CodePools failed to append new pool." ); - allocator = & back(CodePools); + allocator = back( & CodePools); } Code result { rcast( AST*, alloc( allocator_info(allocator), sizeof(AST) )) }; diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index 61e4283..8e4164f 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -222,7 +222,7 @@ s32 lex_preprocessor_directive( , Token& token ) { char const* hash = scanner; - append(Tokens, { hash, 1, TokType::Preprocess_Hash, line, column, TF_Preprocess } ); + append( & Tokens, { hash, 1, TokType::Preprocess_Hash, line, column, TF_Preprocess } ); move_forward(); SkipWhitespace(); @@ -298,14 +298,14 @@ s32 lex_preprocessor_directive( token.Length = token.Length + token.Text - hash; token.Text = hash; - append(Tokens, token ); + append( & Tokens, token ); return Lex_Continue; // Skip found token, its all handled here. } if ( token.Type == TokType::Preprocess_Else || token.Type == TokType::Preprocess_EndIf ) { token.Flags |= TF_Preprocess_Cond; - append(Tokens, token ); + append( & Tokens, token ); end_line(); return Lex_Continue; } @@ -314,7 +314,7 @@ s32 lex_preprocessor_directive( token.Flags |= TF_Preprocess_Cond; } - append(Tokens, token ); + append( & Tokens, token ); SkipWhitespace(); @@ -338,7 +338,7 @@ s32 lex_preprocessor_directive( name.Length++; } - append(Tokens, name ); + append( & Tokens, name ); u64 key = crc32( name.Text, name.Length ); set(defines, key, name ); @@ -384,7 +384,7 @@ s32 lex_preprocessor_directive( move_forward(); } - append(Tokens, preprocess_content ); + append( & Tokens, preprocess_content ); return Lex_Continue; // Skip found token, its all handled here. } @@ -446,7 +446,7 @@ s32 lex_preprocessor_directive( preprocess_content.Length++; } - append(Tokens, preprocess_content ); + append( & Tokens, preprocess_content ); return Lex_Continue; // Skip found token, its all handled here. } @@ -461,7 +461,7 @@ void lex_found_token( StrC& content { if ( token.Type != TokType::Invalid ) { - append(Tokens, token ); + append( & Tokens, token ); return; } @@ -488,7 +488,7 @@ void lex_found_token( StrC& content } token.Type = type; - append(Tokens, token ); + append( & Tokens, token ); return; } @@ -498,7 +498,7 @@ void lex_found_token( StrC& content { token.Type = type; token.Flags |= TF_Specifier; - append(Tokens, token ); + append( & Tokens, token ); return; } @@ -506,7 +506,7 @@ void lex_found_token( StrC& content if ( type != TokType::Invalid ) { token.Type = type; - append(Tokens, token ); + append( & Tokens, token ); return; } @@ -558,7 +558,7 @@ void lex_found_token( StrC& content token.Type = TokType::Identifier; } - append(Tokens, token ); + append( & Tokens, token ); } @@ -630,7 +630,7 @@ TokArray lex( StrC content ) token.Type = TokType::NewLine; token.Length++; - append(Tokens, token ); + append( & Tokens, token ); continue; } } @@ -1099,7 +1099,7 @@ TokArray lex( StrC content ) move_forward(); token.Length++; } - append(Tokens, token ); + append( & Tokens, token ); continue; } else if ( current == '*' ) @@ -1135,7 +1135,7 @@ TokArray lex( StrC content ) move_forward(); token.Length++; } - append(Tokens, token ); + append( & Tokens, token ); // end_line(); continue; } diff --git a/project/components/parser.cpp b/project/components/parser.cpp index b04fd0a..f184d70 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -52,7 +52,7 @@ struct ParseContext sptr length = scope_start.Length; char const* current = scope_start.Text + length; - while ( current <= back(Tokens.Arr).Text && *current != '\n' && length < 74 ) + while ( current <= back( & Tokens.Arr)->Text && *current != '\n' && length < 74 ) { current++; length++; @@ -745,7 +745,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) } Token interface_tok = parse_identifier(); - append(interfaces, def_type( interface_tok ) ); + append( & interfaces, def_type( interface_tok ) ); // : , ... } } @@ -777,7 +777,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) if ( inline_cmt ) result->InlineCmt = inline_cmt; - free(interfaces); + free(& interfaces); return result; } diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index 66c6565..f59fb2f 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -29,29 +29,29 @@ struct ArrayHeader; usize array_grow_formula(ssize value); -template Array array_init(AllocatorInfo allocator); -template Array array_init_reserve(AllocatorInfo allocator, ssize capacity); -template bool append(Array& array, Array other); -template bool append(Array& array, Type value); -template bool append(Array& array, Type* items, usize item_num); -template bool append_at(Array& array, Type item, usize idx); -template bool append_at(Array& array, Type* items, usize item_num, usize idx); -template Type& back(Array& array); -template void clear(Array& array); -template bool fill(Array& array, usize begin, usize end, Type value); -template void free(Array& array); -template bool grow(Array& array, usize min_capacity); -template usize num(Array& array); -template void pop(Array& array); -template void remove_at(Array& array, usize idx); -template bool reserve(Array& array, usize new_capacity); -template bool resize(Array& array, usize num); -template bool set_capacity(Array& array, usize new_capacity); -template ArrayHeader* get_header(Array& array); +template Array(Type) array_init (AllocatorInfo allocator); +template Array(Type) array_init_reserve(AllocatorInfo allocator, ssize capacity); +template bool append (Array(Type)* array, Array(Type) other); +template bool append (Array(Type)* array, Type value); +template bool append (Array(Type)* array, Type* items, usize item_num); +template bool append_at (Array(Type)* array, Type item, usize idx); +template bool append_at (Array(Type)* array, Type* items, usize item_num, usize idx); +template Type* back (Array(Type) array); +template void clear (Array(Type) array); +template bool fill (Array(Type) array, usize begin, usize end, Type value); +template void free (Array(Type)* array); +template bool grow (Array(Type)* array, usize min_capacity); +template usize num (Array(Type) array); +template void pop (Array(Type) array); +template void remove_at (Array(Type) array, usize idx); +template bool reserve (Array(Type)* array, usize new_capacity); +template bool resize (Array(Type)* array, usize num); +template bool set_capacity (Array(Type)* array, usize new_capacity); +template ArrayHeader* get_header (Array(Type) array); -template forceinline Type* begin(Array& array) { return array; } -template forceinline Type* end(Array& array) { return array + get_header(array)->Num; } -template forceinline Type* next(Array& array, Type* entry) { return entry + 1; } +// template forceinline Type* begin(Array array) { return array; } +// template forceinline Type* end(Array array) { return array + get_header(array)->Num; } +// template forceinline Type* next(Array array, Type* entry) { return entry + 1; } struct ArrayHeader { AllocatorInfo Allocator; @@ -70,32 +70,52 @@ struct Array forceinline static Array init_reserve(AllocatorInfo allocator, ssize capacity) { return GEN_NS array_init_reserve(allocator, capacity); } forceinline static usize grow_formula(ssize value) { return GEN_NS array_grow_formula(value); } - forceinline bool append(Array other) { return GEN_NS append(*this, other); } - forceinline bool append(Type value) { return GEN_NS append(*this, value); } - forceinline bool append(Type* items, usize item_num) { return GEN_NS append(*this, items, item_num); } - forceinline bool append_at(Type item, usize idx) { return GEN_NS append_at(*this, item, idx); } - forceinline bool append_at(Type* items, usize item_num, usize idx) { return GEN_NS append_at(*this, items, item_num, idx); } - forceinline Type& back() { return GEN_NS back(*this); } - forceinline void clear() { GEN_NS clear(*this); } - forceinline bool fill(usize begin, usize end, Type value) { return GEN_NS fill(*this, begin, end, value); } - forceinline void free() { GEN_NS free(*this); } - forceinline ArrayHeader* get_header() { return GEN_NS get_header(*this); } - forceinline bool grow(usize min_capacity) { return GEN_NS grow(*this, min_capacity); } + forceinline bool append(Array other) { return GEN_NS append(this, other); } + forceinline bool append(Type value) { return GEN_NS append(this, value); } + forceinline bool append(Type* items, usize item_num) { return GEN_NS append(this, items, item_num); } + forceinline bool append_at(Type item, usize idx) { return GEN_NS append_at(this, item, idx); } + forceinline bool append_at(Type* items, usize item_num, usize idx) { return GEN_NS append_at(this, items, item_num, idx); } + forceinline Type* back() { return GEN_NS back(* this); } + forceinline void clear() { GEN_NS clear(* this); } + forceinline bool fill(usize begin, usize end, Type value) { return GEN_NS fill(* this, begin, end, value); } + forceinline void free() { GEN_NS free(this); } + forceinline ArrayHeader* get_header() { return GEN_NS get_header(* this); } + forceinline bool grow(usize min_capacity) { return GEN_NS grow(this, min_capacity); } forceinline usize num() { return GEN_NS num(*this); } - forceinline void pop() { GEN_NS pop(*this); } - forceinline void remove_at(usize idx) { GEN_NS remove_at(*this, idx); } - forceinline bool reserve(usize new_capacity) { return GEN_NS reserve(*this, new_capacity); } - forceinline bool resize(usize num) { return GEN_NS resize(*this, num); } - forceinline bool set_capacity(usize new_capacity) { return GEN_NS set_capacity(*this, new_capacity); } + forceinline void pop() { GEN_NS pop(* this); } + forceinline void remove_at(usize idx) { GEN_NS remove_at(* this, idx); } + forceinline bool reserve(usize new_capacity) { return GEN_NS reserve(this, new_capacity); } + forceinline bool resize(usize num) { return GEN_NS resize(this, num); } + forceinline bool set_capacity(usize new_capacity) { return GEN_NS set_capacity(this, new_capacity); } +#pragma endregion Member Mapping forceinline operator Type*() { return Data; } forceinline operator Type const*() const { return Data; } forceinline Type* begin() { return Data; } forceinline Type* end() { return Data + get_header()->Num; } -#pragma endregion Member Mapping + + forceinline Type& operator[](ssize index) { return Data[index]; } + forceinline Type const& operator[](ssize index) const { return Data[index]; } }; #endif +#if GEN_SUPPORT_CPP_REFERENCES +template bool append(Array& array, Array other) { return GEN_NS append( & array, other ); } +template bool append(Array& array, Type value) { return GEN_NS append( & array, value ); } +template bool append(Array& array, Type* items, usize item_num) { return GEN_NS append( & array, items, item_num ); } +template bool append_at(Array& array, Type item, usize idx) { return GEN_NS append_at( & array, item, idx ); } +template bool append_at(Array& array, Type* items, usize item_num, usize idx) { return GEN_NS append_at( & array, items, item_num, idx ); } +template void free(Array& array) { return GEN_NS free( & array ); } +template bool grow(Array& array, usize min_capacity) { return GEN_NS grow( & array, min_capacity); } +template bool reserve(Array& array, usize new_capacity) { return GEN_NS reserve( & array, new_capacity); } +template bool resize(Array& array, usize num) { return GEN_NS resize( & array, num); } +template bool set_capacity(Array& array, usize new_capacity) { return GEN_NS set_capacity( & array, new_capacity); } + +template forceinline Type* begin(Array& array) { return array; } +template forceinline Type* end(Array& array) { return array + get_header(array)->Num; } +template forceinline Type* next(Array& array, Type* entry) { return entry + 1; } +#endif + template inline Array array_init(AllocatorInfo allocator) { return array_init_reserve(allocator, array_grow_formula(0)); @@ -121,30 +141,30 @@ usize array_grow_formula(ssize value) { } template inline -bool append(Array& array, Array other) { +bool append(Array* array, Array other) { return append(array, other, num(other)); } template inline -bool append(Array& array, Type value) +bool append(Array* array, Type value) { - ArrayHeader* header = get_header(array); + ArrayHeader* header = get_header(* array); if (header->Num == header->Capacity) { if (!grow(array, header->Capacity)) return false; - header = get_header(array); + header = get_header(* array); } - array[header->Num] = value; + (*array)[ header->Num] = value; header->Num++; return true; } template inline -bool append(Array& array, Type* items, usize item_num) +bool append(Array* array, Type* items, usize item_num) { ArrayHeader* header = get_header(array); @@ -162,34 +182,37 @@ bool append(Array& array, Type* items, usize item_num) } template inline -bool append_at(Array& array, Type item, usize idx) +bool append_at(Array* array, Type item, usize idx) { - ArrayHeader* header = get_header(array); + ArrayHeader* header = get_header(* array); - if (idx >= header->Num) - idx = header->Num - 1; + ssize slot = idx; + if (slot >= header->Num) + slot = header->Num - 1; - if (idx < 0) - idx = 0; + if (slot < 0) + slot = 0; if (header->Capacity < header->Num + 1) { - if (!grow(array, header->Capacity + 1)) + if ( ! grow(array, header->Capacity + 1)) return false; - header = get_header(array); + header = get_header(* array); } - Type* target = array + idx; + Type* target = array->Data + slot; - mem_move(target + 1, target, (header->Num - idx) * sizeof(Type)); + mem_move(target + 1, target, (header->Num - slot) * sizeof(Type)); header->Num++; + header = get_header(* array); + return true; } template inline -bool append_at(Array& array, Type* items, usize item_num, usize idx) +bool append_at(Array* array, Type* items, usize item_num, usize idx) { ArrayHeader* header = get_header(array); @@ -200,7 +223,7 @@ bool append_at(Array& array, Type* items, usize item_num, usize idx) if (item_num > header->Capacity) { - if (!grow(array, header->Capacity + item_num)) + if (! grow(array, header->Capacity + item_num)) return false; header = get_header(array); @@ -217,19 +240,25 @@ bool append_at(Array& array, Type* items, usize item_num, usize idx) } template inline -Type& back(Array& array) { - ArrayHeader* header = get_header(array); - return array[header->Num - 1]; +Type* back(Array* array) +{ + GEN_ASSERT(array != nullptr); + + ArrayHeader* header = get_header(* array); + if (header->Num <= 0) + return nullptr; + + return & (*array)[header->Num - 1]; } template inline -void clear(Array& array) { +void clear(Array array) { ArrayHeader* header = get_header(array); header->Num = 0; } template inline -bool fill(Array& array, usize begin, usize end, Type value) +bool fill(Array array, usize begin, usize end, Type value) { ArrayHeader* header = get_header(array); @@ -245,25 +274,25 @@ bool fill(Array& array, usize begin, usize end, Type value) } template inline -void free(Array& array) { - ArrayHeader* header = get_header(array); +void free(Array* array) { + GEN_ASSERT(array != nullptr); + ArrayHeader* header = get_header(* array); GEN_NS free(header->Allocator, header); - Type*& Data = rcast(Type*&, array); - Data = nullptr; + array->Data = nullptr; } -template inline -ArrayHeader* get_header(Array& array) { +template forceinline +ArrayHeader* get_header(Array array) { + Type* Data = array; + using NonConstType = TRemoveConst; - Type* Data = array; // This should do nothing in C but in C++ gets member Data struct. - return rcast(ArrayHeader*, const_cast(Data)) - 1; + return rcast(ArrayHeader*, const_cast(Data)) - 1; } - template inline -bool grow(Array& array, usize min_capacity) +bool grow(Array* array, usize min_capacity) { - ArrayHeader* header = get_header(array); - usize new_capacity = array_grow_formula(header->Capacity); + ArrayHeader* header = get_header(* array); + usize new_capacity = array_grow_formula(header->Capacity); if (new_capacity < min_capacity) new_capacity = min_capacity; @@ -272,19 +301,19 @@ bool grow(Array& array, usize min_capacity) } template inline -usize num(Array& array) { +usize num(Array array) { return get_header(array)->Num; } template inline -void pop(Array& array) { +void pop(Array array) { ArrayHeader* header = get_header(array); GEN_ASSERT(header->Num > 0); header->Num--; } template inline -void remove_at(Array& array, usize idx) +void remove_at(Array array, usize idx) { ArrayHeader* header = get_header(array); GEN_ASSERT(idx < header->Num); @@ -294,7 +323,7 @@ void remove_at(Array& array, usize idx) } template inline -bool reserve(Array& array, usize new_capacity) +bool reserve(Array* array, usize new_capacity) { ArrayHeader* header = get_header(array); @@ -305,14 +334,14 @@ bool reserve(Array& array, usize new_capacity) } template inline -bool resize(Array& array, usize num) +bool resize(Array* array, usize num) { - ArrayHeader* header = get_header(array); + ArrayHeader* header = get_header(* array); if (header->Capacity < num) { - if (!grow(array, num)) + if (! grow( array, num)) return false; - header = get_header(array); + header = get_header(* array); } header->Num = num; @@ -320,9 +349,9 @@ bool resize(Array& array, usize num) } template inline -bool set_capacity(Array& array, usize new_capacity) +bool set_capacity(Array* array, usize new_capacity) { - ArrayHeader* header = get_header(array); + ArrayHeader* header = get_header(* array); if (new_capacity == header->Capacity) return true; @@ -333,7 +362,7 @@ bool set_capacity(Array& array, usize new_capacity) return true; } - ssize size = sizeof(ArrayHeader) + sizeof(Type) * new_capacity; + ssize size = sizeof(ArrayHeader) + sizeof(Type) * new_capacity; ArrayHeader* new_header = rcast(ArrayHeader*, alloc(header->Allocator, size)); if (new_header == nullptr) @@ -345,32 +374,10 @@ bool set_capacity(Array& array, usize new_capacity) GEN_NS free(header->Allocator, header); - Type*& Data = rcast(Type*&, array); - Data = rcast(Type*, new_header + 1); + array->Data = rcast(Type*, new_header + 1); return true; } -#define array_init(Type, allocator) array_init(allocator) -#define array_init(Type, allocator) array_init(allocator) -#define array_init_reserve(Type, allocator, capacity) array_init_reserve(allocator, capacity) -#define array_append(Type, array, other) append(array, other) -#define array_append_value(Type, array, value) append(array, value) -#define array_append_items(Type, array, items, item_num) append(array, items, item_num) -#define array_append_at(Type, array, item, idx) append_at(array, item, idx) -#define array_append_items_at(Type, array, items, num, idx) append_at(array, items, num, idx) -#define array_back(Type, array) back(array) -#define array_clear(Type, array) clear(array) -#define array_fill(Type, array, begin, end, value) fill(array, begin, end, value) -#define array_free(Type, array) free(array) -#define array_grow(Type, array, min_capacity) grow(array, min_capacity) -#define array_num(Type, array) num(array) -#define array_pop(Type, array) pop(array) -#define array_remove_at(Type, array, idx) remove_at(array, idx) -#define array_reserve(Type, array, new_capacity) reserve(array, new_capacity) -#define array_resize(Type, array, num) resize(array, num) -#define array_set_capacity(Type, array, new_capacity) set_capacity(array, new_capacity) -#define array_get_header(array) get_header(array) - #pragma endregion Array // TODO(Ed) : This thing needs ALOT of work. @@ -453,7 +460,7 @@ HashTable hashtable_init_reserve(AllocatorInfo allocator, usize num) result.Hashes = array_init_reserve(allocator, num); get_header(result.Hashes)->Num = num; - resize(result.Hashes, num); + resize(& result.Hashes, num); fill(result.Hashes, 0, num, -1); result.Entries = array_init_reserve>(allocator, num); @@ -469,8 +476,8 @@ void clear(HashTable& table) { template inline void destroy(HashTable& table) { if (table.Hashes && get_header(table.Hashes)->Capacity) { - free(table.Hashes); - free(table.Entries); + free(& table.Hashes); + free(& table.Entries); } } @@ -621,7 +628,7 @@ ssize add_entry(HashTable& table, u64 key) { HashTableEntry entry = { key, -1 }; idx = num(table.Entries); - append(table.Entries, entry); + append( & table.Entries, entry); return idx; } diff --git a/project/dependencies/filesystem.cpp b/project/dependencies/filesystem.cpp index 931f9cd..683972b 100644 --- a/project/dependencies/filesystem.cpp +++ b/project/dependencies/filesystem.cpp @@ -612,7 +612,7 @@ GEN_FILE_WRITE_AT_PROC( _memory_file_write ) if ( get_header(arr)->Capacity < usize(new_cap) ) { - if ( ! grow( arr, ( s64 )( new_cap ) ) ) + if ( ! grow( & arr, ( s64 )( new_cap ) ) ) return false; d->buf = arr; } @@ -647,7 +647,7 @@ GEN_FILE_CLOSE_PROC( _memory_file_close ) if ( d->flags & EFileStream_CLONE_WRITABLE ) { Array arr = { d->buf }; - free(arr); + free(& arr); } free( allocator, d ); diff --git a/project/dependencies/parsing.cpp b/project/dependencies/parsing.cpp index 4ab0175..fbe44b1 100644 --- a/project/dependencies/parsing.cpp +++ b/project/dependencies/parsing.cpp @@ -41,7 +41,7 @@ u8 adt_destroy_branch( ADT_Node* node ) adt_destroy_branch( node->nodes + i ); } - free(node->nodes); + free(& node->nodes); } return 0; } @@ -287,10 +287,11 @@ ADT_Node* adt_alloc_at( ADT_Node* parent, ssize index ) ADT_Node o = { 0 }; o.parent = parent; - if ( ! append_at( parent->nodes, o, index ) ) + if ( ! append_at( & parent->nodes, o, index ) ) return NULL; - return parent->nodes + index; + ADT_Node* node = & parent->nodes[index]; + return node; } ADT_Node* adt_alloc( ADT_Node* parent ) @@ -402,7 +403,9 @@ ADT_Node* adt_append_arr( ADT_Node* parent, char const* name ) ADT_Node* o = adt_alloc( parent ); if ( ! o ) return NULL; - if ( adt_set_arr( o, name, get_header(parent->nodes)->Allocator ) ) + + ArrayHeader* node_header = get_header(parent->nodes); + if ( adt_set_arr( o, name, node_header->Allocator ) ) { adt_remove_node( o ); return NULL; @@ -951,7 +954,7 @@ u8 csv_parse_delimiter( CSV_Object* root, char* text, AllocatorInfo allocator, b adt_append_arr( root, NULL ); } - append(root->nodes[ columnIndex ].nodes, rowItem ); + append( & root->nodes[ columnIndex ].nodes, rowItem ); if ( delimiter == delim ) { diff --git a/project/dependencies/parsing.hpp b/project/dependencies/parsing.hpp index a0a7c37..b0a721d 100644 --- a/project/dependencies/parsing.hpp +++ b/project/dependencies/parsing.hpp @@ -83,7 +83,7 @@ struct ADT_Node union { char const* string; - Array nodes; ///< zpl_array + Array(ADT_Node) nodes; ///< zpl_array struct { diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 975d08d..5fa885c 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -23,9 +23,10 @@ CodeBody gen_ecode( char const* path ) String enum_entries = string_make_reserve( GlobalAllocator, kilobytes(1) ); String to_str_entries = string_make_reserve( GlobalAllocator, kilobytes(1) ); - for ( ADT_Node node : enum_strs ) + for ( ADT_Node& node : enum_strs ) { char const* code = node.string; + append_fmt( enum_entries, "%s,\n", code ); append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", code, code ); } From f9c21ebc049f53dd6d11a8e2680def5a5e9a0011 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Sun, 1 Dec 2024 23:35:58 -0500 Subject: [PATCH 026/112] progress --- project/auxillary/builder.cpp | 8 +- project/auxillary/scanner.hpp | 6 +- project/bootstrap.cpp | 10 +- project/components/ast.cpp | 13 +- project/components/code_serialization.cpp | 486 +++++++++++----------- project/components/interface.cpp | 4 +- project/components/interface.untyped.cpp | 3 +- project/components/interface.upfront.cpp | 8 +- project/components/lexer.cpp | 6 +- project/components/parser.cpp | 55 +-- project/dependencies/containers.hpp | 130 +++--- project/dependencies/strings.hpp | 293 +++++++------ project/helpers/helper.hpp | 26 +- 13 files changed, 540 insertions(+), 508 deletions(-) diff --git a/project/auxillary/builder.cpp b/project/auxillary/builder.cpp index 4632772..b283e96 100644 --- a/project/auxillary/builder.cpp +++ b/project/auxillary/builder.cpp @@ -21,7 +21,7 @@ Builder Builder::open( char const* path ) void Builder::pad_lines( s32 num ) { - append( Buffer, "\n" ); + append( & Buffer, "\n" ); } void Builder::print( Code code ) @@ -29,7 +29,7 @@ void Builder::print( Code code ) String str = code->to_string(); // const ssize len = str.length(); // log_fmt( "%s - print: %.*s\n", File.filename, len > 80 ? 80 : len, str.Data ); - append( Buffer, str ); + append( & Buffer, str ); } void Builder::print_fmt( char const* fmt, ... ) @@ -43,7 +43,7 @@ void Builder::print_fmt( char const* fmt, ... ) va_end( va ); // log_fmt( "$%s - print_fmt: %.*s\n", File.filename, res > 80 ? 80 : res, buf ); - append( Buffer, buf, res ); + append( & Buffer, buf, res ); } void Builder::write() @@ -55,5 +55,5 @@ void Builder::write() log_fmt( "Generated: %s\n", File.filename ); file_close( & File ); - free(Buffer); + free(& Buffer); } diff --git a/project/auxillary/scanner.hpp b/project/auxillary/scanner.hpp index 6540a4d..0cac91c 100644 --- a/project/auxillary/scanner.hpp +++ b/project/auxillary/scanner.hpp @@ -25,7 +25,7 @@ Code scan_file( char const* path ) String str = string_make_reserve( GlobalAllocator, fsize ); file_read( & file, str, fsize ); - get_header(str).Length = fsize; + get_header(str)->Length = fsize; // Skip GEN_INTELLISENSE_DIRECTIVES preprocessor blocks // Its designed so that the directive should be the first thing in the file. @@ -97,12 +97,12 @@ Code scan_file( char const* path ) if ( (scanner + 2) >= ( str.Data + fsize ) ) { mem_move( str, scanner, left ); - get_header(str).Length = left; + get_header(str)->Length = left; break; } mem_move( str, scanner, left ); - get_header(str).Length = left; + get_header(str)->Length = left; break; } diff --git a/project/bootstrap.cpp b/project/bootstrap.cpp index 24dd2bb..2a7332c 100644 --- a/project/bootstrap.cpp +++ b/project/bootstrap.cpp @@ -29,17 +29,17 @@ void format_file( char const* path ) String resolved_path = string_make(GlobalAllocator, to_str(path)); String style_arg = string_make(GlobalAllocator, txt("-style=file:")); - append( style_arg, "../scripts/.clang-format "); + append( & style_arg, "../scripts/.clang-format "); // Need to execute clang format on the generated file to get it to match the original. #define clang_format "clang-format " #define cf_format_inplace "-i " #define cf_verbose "-verbose " String command = string_make( GlobalAllocator, clang_format ); - append( command, cf_format_inplace ); - append( command, cf_verbose ); - append( command, style_arg ); - append( command, resolved_path ); + append( & command, cf_format_inplace ); + append( & command, cf_verbose ); + append( & command, style_arg ); + append( & command, resolved_path ); log_fmt("\tRunning clang-format on file:\n"); system( command ); log_fmt("\tclang-format finished reformatting.\n"); diff --git a/project/components/ast.cpp b/project/components/ast.cpp index 5f8fa43..792333f 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -10,7 +10,8 @@ Code Code::Invalid; char const* debug_str(AST* self) { GEN_ASSERT(self != nullptr); - String result = string_make_reserve( GlobalAllocator, kilobytes(1) ); + String result_stack = string_make_reserve( GlobalAllocator, kilobytes(1) ); + String* result = & result_stack; if ( self->Parent ) append_fmt( result, "\n\tParent : %S %S", self->Parent->type_str(), self->Name ? self->Name : "" ); @@ -356,7 +357,7 @@ char const* debug_str(AST* self) break; } - return result; + return * result; } AST* duplicate(AST* self) @@ -391,25 +392,25 @@ void AST::to_string( String& result ) #ifdef GEN_DONT_ALLOW_INVALID_CODE log_failure("Attempted to serialize invalid code! - %S", Parent ? Parent->debug_str() : Name ); #else - append_fmt( result, "Invalid Code!" ); + GEN_NS append_fmt( & result, "Invalid Code!" ); #endif break; case NewLine: - GEN_NS append( result,"\n"); + GEN_NS append( & result,"\n"); break; case Untyped: case Execution: case Comment: case PlatformAttributes: - GEN_NS append( result, Content ); + GEN_NS append( & result, Content ); break; case Access_Private: case Access_Protected: case Access_Public: - GEN_NS append( result, Name ); + GEN_NS append( & result, Name ); break; case Class: diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 170feab..d8c90c5 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -26,7 +26,7 @@ String CodeBody::to_string() using namespace ECode; case Untyped: case Execution: - GEN_NS append( result, raw()->Content ); + GEN_NS append( & result, raw()->Content ); break; case Enum_Body: @@ -53,24 +53,24 @@ void CodeBody::to_string( String& result ) s32 left = ast->NumEntries; while ( left -- ) { - append_fmt( result, "%S", curr.to_string() ); + append_fmt( & result, "%S", curr.to_string() ); ++curr; } } void CodeBody::to_string_export( String& result ) { - append_fmt( result, "export\n{\n" ); + append_fmt( & result, "export\n{\n" ); Code curr = *this; s32 left = ast->NumEntries; while ( left-- ) { - append_fmt( result, "%S", curr.to_string() ); + append_fmt( & result, "%S", curr.to_string() ); ++curr; } - append_fmt( result, "};\n" ); + append_fmt( & result, "};\n" ); } String CodeComment::to_string() @@ -98,48 +98,48 @@ void CodeConstructor::to_string_def( String& result ) { AST* ClassStructParent = ast->Parent->Parent; if (ClassStructParent) { - append( result, ClassStructParent->Name ); + append( & result, ClassStructParent->Name ); } else { - append( result, ast->Name ); + append( & result, ast->Name ); } if ( ast->Params ) - append_fmt( result, "( %S )", ast->Params.to_string() ); + append_fmt( & result, "( %S )", ast->Params.to_string() ); else - append( result, "()" ); + append( & result, "()" ); if ( ast->InitializerList ) - append_fmt( result, " : %S", ast->InitializerList.to_string() ); + append_fmt( & result, " : %S", ast->InitializerList.to_string() ); if ( ast->InlineCmt ) - append_fmt( result, " // %S", ast->InlineCmt->Content ); + append_fmt( & result, " // %S", ast->InlineCmt->Content ); - append_fmt( result, "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}\n", ast->Body.to_string() ); } void CodeConstructor::to_string_fwd( String& result ) { AST* ClassStructParent = ast->Parent->Parent; if (ClassStructParent) { - append( result, ClassStructParent->Name ); + append( & result, ClassStructParent->Name ); } else { - append( result, ast->Name ); + append( & result, ast->Name ); } if ( ast->Params ) - append_fmt( result, "( %S )", ast->Params.to_string() ); + append_fmt( & result, "( %S )", ast->Params.to_string() ); else - append_fmt( result, "()"); + append_fmt( & result, "()"); if (ast->Body) - append_fmt( result, " = %S", ast->Body.to_string() ); + append_fmt( & result, " = %S", ast->Body.to_string() ); if ( ast->InlineCmt ) - append_fmt( result, "; // %S\n", ast->InlineCmt->Content ); + append_fmt( & result, "; // %S\n", ast->InlineCmt->Content ); else - append( result, ";\n" ); + append( & result, ";\n" ); } String CodeClass::to_string() @@ -161,64 +161,64 @@ String CodeClass::to_string() void CodeClass::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); - append( result, "class " ); + append( & result, "class " ); if ( ast->Attributes ) { - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); } if ( ast->ParentType ) { char const* access_level = to_str( ast->ParentAccess ); - append_fmt( result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); + append_fmt( & result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); CodeType interface = ast->ParentType->Next->code_cast< CodeType >(); if ( interface ) - append( result, "\n" ); + append( & result, "\n" ); while ( interface ) { - append_fmt( result, ", %S", interface.to_string() ); + append_fmt( & result, ", %S", interface.to_string() ); interface = interface->Next ? interface->Next->code_cast< CodeType >() : CodeType { nullptr }; } } else if ( ast->Name ) { - append( result, ast->Name ); + append( & result, ast->Name ); } if ( ast->InlineCmt ) { - append_fmt( result, " // %S", ast->InlineCmt->Content ); + append_fmt( & result, " // %S", ast->InlineCmt->Content ); } - append_fmt( result, "\n{\n%S\n}", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}", ast->Body.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( result, ";\n"); + append( & result, ";\n"); } void CodeClass::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "class %S %S", ast->Attributes.to_string(), ast->Name ); + append_fmt( & result, "class %S %S", ast->Attributes.to_string(), ast->Name ); - else append_fmt( result, "class %S", ast->Name ); + else append_fmt( & result, "class %S", ast->Name ); // Check if it can have an end-statement if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - append_fmt( result, "; // %S\n", ast->InlineCmt->Content ); + append_fmt( & result, "; // %S\n", ast->InlineCmt->Content ); else - append( result,";\n"); + append( & result,";\n"); } } @@ -229,7 +229,7 @@ String CodeDefine::to_string() void CodeDefine::to_string( String& result ) { - append_fmt( result, "#define %S %S\n", ast->Name, ast->Content ); + append_fmt( & result, "#define %S %S\n", ast->Name, ast->Content ); } String CodeDestructor::to_string() @@ -252,19 +252,19 @@ void CodeDestructor::to_string_def( String& result ) { if ( ast->Name ) { - append_fmt( result, "%S()", ast->Name ); + append_fmt( & result, "%S()", ast->Name ); } else if ( ast->Specs ) { if ( ast->Specs.has( ESpecifier::Virtual ) ) - append_fmt( result, "virtual ~%S()", ast->Parent->Name ); + append_fmt( & result, "virtual ~%S()", ast->Parent->Name ); else - append_fmt( result, "~%S()", ast->Parent->Name ); + append_fmt( & result, "~%S()", ast->Parent->Name ); } else - append_fmt( result, "~%S()", ast->Parent->Name ); + append_fmt( & result, "~%S()", ast->Parent->Name ); - append_fmt( result, "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}\n", ast->Body.to_string() ); } void CodeDestructor::to_string_fwd( String& result ) @@ -272,22 +272,22 @@ void CodeDestructor::to_string_fwd( String& result ) if ( ast->Specs ) { if ( ast->Specs.has( ESpecifier::Virtual ) ) - append_fmt( result, "virtual ~%S();\n", ast->Parent->Name ); + append_fmt( & result, "virtual ~%S();\n", ast->Parent->Name ); else - append_fmt( result, "~%S()", ast->Parent->Name ); + append_fmt( & result, "~%S()", ast->Parent->Name ); if ( ast->Specs.has( ESpecifier::Pure ) ) - append( result, " = 0;" ); + append( & result, " = 0;" ); else if (ast->Body) - append_fmt( result, " = %S;", ast->Body.to_string() ); + append_fmt( & result, " = %S;", ast->Body.to_string() ); } else - append_fmt( result, "~%S();", ast->Parent->Name ); + append_fmt( & result, "~%S();", ast->Parent->Name ); if ( ast->InlineCmt ) - append_fmt( result, " %S", ast->InlineCmt->Content ); + append_fmt( & result, " %S", ast->InlineCmt->Content ); else - append( result, "\n"); + append( & result, "\n"); } String CodeEnum::to_string() @@ -315,108 +315,108 @@ String CodeEnum::to_string() void CodeEnum::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes || ast->UnderlyingType ) { - append( result, "enum " ); + append( & result, "enum " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->UnderlyingType ) - append_fmt( result, "%S : %S\n{\n%S\n}" + append_fmt( & result, "%S : %S\n{\n%S\n}" , ast->Name , ast->UnderlyingType.to_string() , ast->Body.to_string() ); else if ( ast->UnderlyingTypeMacro ) - append_fmt( result, "%S : %S\n{\n%S\n}" + append_fmt( & result, "%S : %S\n{\n%S\n}" , ast->Name , ast->UnderlyingTypeMacro.to_string() , ast->Body.to_string() ); - else append_fmt( result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); + else append_fmt( & result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); } - else append_fmt( result, "enum %S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); + else append_fmt( & result, "enum %S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( result, ";\n"); + append( & result, ";\n"); } void CodeEnum::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->UnderlyingType ) - append_fmt( result, "enum %S : %S", ast->Name, ast->UnderlyingType.to_string() ); + append_fmt( & result, "enum %S : %S", ast->Name, ast->UnderlyingType.to_string() ); else - append_fmt( result, "enum %S", ast->Name ); + append_fmt( & result, "enum %S", ast->Name ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content ); + append_fmt( & result, "; %S", ast->InlineCmt->Content ); else - append( result, ";\n"); + append( & result, ";\n"); } } void CodeEnum::to_string_class_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes || ast->UnderlyingType ) { - append( result, "enum class " ); + append( & result, "enum class " ); if ( ast->Attributes ) { - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); } if ( ast->UnderlyingType ) { - append_fmt( result, "%S : %S\n{\n%S\n}", ast->Name, ast->UnderlyingType.to_string(), ast->Body.to_string() ); + append_fmt( & result, "%S : %S\n{\n%S\n}", ast->Name, ast->UnderlyingType.to_string(), ast->Body.to_string() ); } else { - append_fmt( result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); + append_fmt( & result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); } } else { - append_fmt( result, "enum class %S\n{\n%S\n}", ast->Body.to_string() ); + append_fmt( & result, "enum class %S\n{\n%S\n}", ast->Body.to_string() ); } if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( result, ";\n"); + append( & result, ";\n"); } void CodeEnum::to_string_class_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); - append( result, "enum class " ); + append( & result, "enum class " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); - append_fmt( result, "%S : %S", ast->Name, ast->UnderlyingType.to_string() ); + append_fmt( & result, "%S : %S", ast->Name, ast->UnderlyingType.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content ); + append_fmt( & result, "; %S", ast->InlineCmt->Content ); else - append( result, ";\n"); + append( & result, ";\n"); } } @@ -428,9 +428,9 @@ String CodeExec::to_string() void CodeExtern::to_string( String& result ) { if ( ast->Body ) - append_fmt( result, "extern \"%S\"\n{\n%S\n}\n", ast->Name, ast->Body.to_string() ); + append_fmt( & result, "extern \"%S\"\n{\n%S\n}\n", ast->Name, ast->Body.to_string() ); else - append_fmt( result, "extern \"%S\"\n{}\n", ast->Name ); + append_fmt( & result, "extern \"%S\"\n{}\n", ast->Name ); } String CodeInclude::to_string() @@ -440,7 +440,7 @@ String CodeInclude::to_string() void CodeInclude::to_string( String& result ) { - append_fmt( result, "#include %S\n", ast->Content ); + append_fmt( & result, "#include %S\n", ast->Content ); } String CodeFriend::to_string() @@ -452,17 +452,17 @@ String CodeFriend::to_string() void CodeFriend::to_string( String& result ) { - append_fmt( result, "friend %S", ast->Declaration->to_string() ); + append_fmt( & result, "friend %S", ast->Declaration->to_string() ); if ( ast->Declaration->Type != ECode::Function && result[ length(result) - 1 ] != ';' ) { - append( result, ";" ); + append( & result, ";" ); } if ( ast->InlineCmt ) - append_fmt( result, " %S", ast->InlineCmt->Content ); + append_fmt( & result, " %S", ast->InlineCmt->Content ); else - append( result, "\n"); + append( & result, "\n"); } String CodeFn::to_string() @@ -484,10 +484,10 @@ String CodeFn::to_string() void CodeFn::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export" ); + append( & result, "export" ); if ( ast->Attributes ) - append_fmt( result, " %S ", ast->Attributes.to_string() ); + append_fmt( & result, " %S ", ast->Attributes.to_string() ); bool prefix_specs = false; if ( ast->Specs ) @@ -497,7 +497,7 @@ void CodeFn::to_string_def( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); prefix_specs = true; } @@ -505,19 +505,19 @@ void CodeFn::to_string_def( String& result ) } if ( ast->Attributes || prefix_specs ) - append( result, "\n" ); + append( & result, "\n" ); if ( ast->ReturnType ) - append_fmt( result, "%S %S(", ast->ReturnType.to_string(), ast->Name ); + append_fmt( & result, "%S %S(", ast->ReturnType.to_string(), ast->Name ); else - append_fmt( result, "%S(", ast->Name ); + append_fmt( & result, "%S(", ast->Name ); if ( ast->Params ) - append_fmt( result, "%S)", ast->Params.to_string() ); + append_fmt( & result, "%S)", ast->Params.to_string() ); else - append( result, ")" ); + append( & result, ")" ); if ( ast->Specs ) { @@ -526,21 +526,21 @@ void CodeFn::to_string_def( String& result ) if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } - append_fmt( result, "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}\n", ast->Body.to_string() ); } void CodeFn::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); b32 prefix_specs = false; if ( ast->Specs ) @@ -550,7 +550,7 @@ void CodeFn::to_string_fwd( String& result ) if ( ! ESpecifier::is_trailing( spec ) || ! (spec != ESpecifier::Pure) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); prefix_specs = true; } @@ -559,20 +559,20 @@ void CodeFn::to_string_fwd( String& result ) if ( ast->Attributes || prefix_specs ) { - append( result, "\n" ); + append( & result, "\n" ); } if ( ast->ReturnType ) - append_fmt( result, "%S %S(", ast->ReturnType.to_string(), ast->Name ); + append_fmt( & result, "%S %S(", ast->ReturnType.to_string(), ast->Name ); else - append_fmt( result, "%S(", ast->Name ); + append_fmt( & result, "%S(", ast->Name ); if ( ast->Params ) - append_fmt( result, "%S)", ast->Params.to_string() ); + append_fmt( & result, "%S)", ast->Params.to_string() ); else - append( result, ")" ); + append( & result, ")" ); if ( ast->Specs ) { @@ -581,20 +581,20 @@ void CodeFn::to_string_fwd( String& result ) if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } if ( ast->Specs && ast->Specs.has( ESpecifier::Pure ) >= 0 ) - append( result, " = 0;" ); + append( & result, " = 0;" ); else if (ast->Body) - append_fmt( result, " = %S;", ast->Body.to_string() ); + append_fmt( & result, " = %S;", ast->Body.to_string() ); if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content ); + append_fmt( & result, "; %S", ast->InlineCmt->Content ); else - append( result, ";\n" ); + append( & result, ";\n" ); } String CodeModule::to_string() @@ -607,12 +607,12 @@ String CodeModule::to_string() void CodeModule::to_string( String& result ) { if (((u32(ModuleFlag_Export) & u32(ast->ModuleFlags)) == u32(ModuleFlag_Export))) - append( result, "export "); + append( & result, "export "); if (((u32(ModuleFlag_Import) & u32(ast->ModuleFlags)) == u32(ModuleFlag_Import))) - append( result, "import "); + append( & result, "import "); - append_fmt( result, "%S;\n", ast->Name ); + append_fmt( & result, "%S;\n", ast->Name ); } String CodeNS::to_string() @@ -625,9 +625,9 @@ String CodeNS::to_string() void CodeNS::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); - append_fmt( result, "namespace %S\n{\n%S\n}\n", ast->Name , ast->Body.to_string() ); + append_fmt( & result, "namespace %S\n{\n%S\n}\n", ast->Name , ast->Body.to_string() ); } String CodeOperator::to_string() @@ -651,13 +651,13 @@ String CodeOperator::to_string() void CodeOperator::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->Specs ) { @@ -666,24 +666,24 @@ void CodeOperator::to_string_def( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } if ( ast->Attributes || ast->Specs ) { - append( result, "\n" ); + append( & result, "\n" ); } if ( ast->ReturnType ) - append_fmt( result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); + append_fmt( & result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); if ( ast->Params ) - append_fmt( result, "%S)", ast->Params.to_string() ); + append_fmt( & result, "%S)", ast->Params.to_string() ); else - append( result, ")" ); + append( & result, ")" ); if ( ast->Specs ) { @@ -692,12 +692,12 @@ void CodeOperator::to_string_def( String& result ) if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } - append_fmt( result, "\n{\n%S\n}\n" + append_fmt( & result, "\n{\n%S\n}\n" , ast->Body.to_string() ); } @@ -705,10 +705,10 @@ void CodeOperator::to_string_def( String& result ) void CodeOperator::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "%S\n", ast->Attributes.to_string() ); + append_fmt( & result, "%S\n", ast->Attributes.to_string() ); if ( ast->Specs ) { @@ -717,23 +717,23 @@ void CodeOperator::to_string_fwd( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } if ( ast->Attributes || ast->Specs ) { - append( result, "\n" ); + append( & result, "\n" ); } - append_fmt( result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); + append_fmt( & result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); if ( ast->Params ) - append_fmt( result, "%S)", ast->Params.to_string() ); + append_fmt( & result, "%S)", ast->Params.to_string() ); else - append_fmt( result, ")" ); + append_fmt( & result, ")" ); if ( ast->Specs ) { @@ -742,15 +742,15 @@ void CodeOperator::to_string_fwd( String& result ) if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content ); + append_fmt( & result, "; %S", ast->InlineCmt->Content ); else - append( result, ";\n" ); + append( & result, ";\n" ); } String CodeOpCast::to_string() @@ -778,32 +778,32 @@ void CodeOpCast::to_string_def( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, "%*s ", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, "%*s ", spec_str.Len, spec_str.Ptr ); } } if ( ast->Name && length(ast->Name) ) - append_fmt( result, "%Soperator %S()", ast->Name, ast->ValueType.to_string() ); + append_fmt( & result, "%Soperator %S()", ast->Name, ast->ValueType.to_string() ); else - append_fmt( result, "operator %S()", ast->ValueType.to_string() ); + append_fmt( & result, "operator %S()", ast->ValueType.to_string() ); for ( SpecifierT spec : ast->Specs ) { if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); } } - append_fmt( result, "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}\n", ast->Body.to_string() ); return; } if ( ast->Name && length(ast->Name) ) - append_fmt( result, "%Soperator %S()\n{\n%S\n}\n", ast->Name, ast->ValueType.to_string(), ast->Body.to_string() ); + append_fmt( & result, "%Soperator %S()\n{\n%S\n}\n", ast->Name, ast->ValueType.to_string(), ast->Body.to_string() ); else - append_fmt( result, "operator %S()\n{\n%S\n}\n", ast->ValueType.to_string(), ast->Body.to_string() ); + append_fmt( & result, "operator %S()\n{\n%S\n}\n", ast->ValueType.to_string(), ast->Body.to_string() ); } void CodeOpCast::to_string_fwd( String& result ) @@ -815,32 +815,32 @@ void CodeOpCast::to_string_fwd( String& result ) if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, "%*s ", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, "%*s ", spec_str.Len, spec_str.Ptr ); } } - append_fmt( result, "operator %S()", ast->ValueType.to_string() ); + append_fmt( & result, "operator %S()", ast->ValueType.to_string() ); for ( SpecifierT spec : ast->Specs ) { if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( result, " %*s", spec_str.Len, spec_str.Ptr ); + append_fmt( & result, " %*s", spec_str.Len, spec_str.Ptr ); } } if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content ); + append_fmt( & result, "; %S", ast->InlineCmt->Content ); else - append( result, ";\n" ); + append( & result, ";\n" ); return; } if ( ast->InlineCmt ) - append_fmt( result, "operator %S(); %S", ast->ValueType.to_string() ); + append_fmt( & result, "operator %S(); %S", ast->ValueType.to_string() ); else - append_fmt( result, "operator %S();\n", ast->ValueType.to_string() ); + append_fmt( & result, "operator %S();\n", ast->ValueType.to_string() ); } String CodeParam::to_string() @@ -855,34 +855,34 @@ void CodeParam::to_string( String& result ) if ( ast->Macro ) { // Related to parsing: ( , ... ) - GEN_NS append( result, ast->Macro.ast->Content ); + GEN_NS append( & result, ast->Macro.ast->Content ); // Could also be: ( , ... ) } if ( ast->Name ) { if ( ast->ValueType.ast == nullptr ) - append_fmt( result, " %S", ast->Name ); + append_fmt( & result, " %S", ast->Name ); else - append_fmt( result, " %S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( & result, " %S %S", ast->ValueType.to_string(), ast->Name ); } else if ( ast->ValueType ) - append_fmt( result, " %S", ast->ValueType.to_string() ); + append_fmt( & result, " %S", ast->ValueType.to_string() ); if ( ast->PostNameMacro ) { - append_fmt( result, " %S", ast->PostNameMacro.to_string() ); + append_fmt( & result, " %S", ast->PostNameMacro.to_string() ); } if ( ast->Value ) - append_fmt( result, " = %S", ast->Value.to_string() ); + append_fmt( & result, " = %S", ast->Value.to_string() ); if ( ast->NumEntries - 1 > 0 ) { for ( CodeParam param : ast->Next ) { - append_fmt( result, ", %S", param.to_string() ); + append_fmt( & result, ", %S", param.to_string() ); } } } @@ -917,32 +917,32 @@ String CodePreprocessCond::to_string() void CodePreprocessCond::to_string_if( String& result ) { - append_fmt( result, "#if %S\n", ast->Content ); + append_fmt( & result, "#if %S\n", ast->Content ); } void CodePreprocessCond::to_string_ifdef( String& result ) { - append_fmt( result, "#ifdef %S\n", ast->Content ); + append_fmt( & result, "#ifdef %S\n", ast->Content ); } void CodePreprocessCond::to_string_ifndef( String& result ) { - append_fmt( result, "#ifndef %S\n", ast->Content ); + append_fmt( & result, "#ifndef %S\n", ast->Content ); } void CodePreprocessCond::to_string_elif( String& result ) { - append_fmt( result, "#elif %S\n", ast->Content ); + append_fmt( & result, "#elif %S\n", ast->Content ); } void CodePreprocessCond::to_string_else( String& result ) { - append_fmt( result, "#else\n" ); + append_fmt( & result, "#else\n" ); } void CodePreprocessCond::to_string_endif( String& result ) { - append_fmt( result, "#endif\n" ); + append_fmt( & result, "#endif\n" ); } String CodePragma::to_string() @@ -954,7 +954,7 @@ String CodePragma::to_string() void CodePragma::to_string( String& result ) { - append_fmt( result, "#pragma %S\n", ast->Content ); + append_fmt( & result, "#pragma %S\n", ast->Content ); } String CodeSpecifiers::to_string() @@ -971,7 +971,7 @@ void CodeSpecifiers::to_string( String& result ) while ( left-- ) { StrC spec = ESpecifier::to_str( ast->ArrSpecs[idx] ); - append_fmt( result, "%.*s ", spec.Len, spec.Ptr ); + append_fmt( & result, "%.*s ", spec.Len, spec.Ptr ); idx++; } } @@ -995,63 +995,63 @@ String CodeStruct::to_string() void CodeStruct::to_string_def( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); - append( result, "struct " ); + append( & result, "struct " ); if ( ast->Attributes ) { - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); } if ( ast->ParentType ) { char const* access_level = to_str( ast->ParentAccess ); - append_fmt( result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); + append_fmt( & result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); CodeType interface = ast->ParentType->Next->code_cast< CodeType >(); if ( interface ) - append( result, "\n" ); + append( & result, "\n" ); while ( interface ) { - append_fmt( result, ", %S", interface.to_string() ); + append_fmt( & result, ", %S", interface.to_string() ); interface = interface->Next ? interface->Next->code_cast< CodeType >() : CodeType { nullptr }; } } else if ( ast->Name ) { - append( result, ast->Name ); + append( & result, ast->Name ); } if ( ast->InlineCmt ) { - append_fmt( result, " // %S", ast->InlineCmt->Content ); + append_fmt( & result, " // %S", ast->InlineCmt->Content ); } - append_fmt( result, "\n{\n%S\n}", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}", ast->Body.to_string() ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( result, ";\n"); + append( & result, ";\n"); } void CodeStruct::to_string_fwd( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "struct %S %S", ast->Attributes.to_string(), ast->Name ); + append_fmt( & result, "struct %S %S", ast->Attributes.to_string(), ast->Name ); - else append_fmt( result, "struct %S", ast->Name ); + else append_fmt( & result, "struct %S", ast->Name ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content ); + append_fmt( & result, "; %S", ast->InlineCmt->Content ); else - append( result, ";\n"); + append( & result, ";\n"); } } @@ -1065,12 +1065,12 @@ String CodeTemplate::to_string() void CodeTemplate::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Params ) - append_fmt( result, "template< %S >\n%S", ast->Params.to_string(), ast->Declaration.to_string() ); + append_fmt( & result, "template< %S >\n%S", ast->Params.to_string(), ast->Declaration.to_string() ); else - append_fmt( result, "template<>\n%S", ast->Declaration.to_string() ); + append_fmt( & result, "template<>\n%S", ast->Declaration.to_string() ); } String CodeTypedef::to_string() @@ -1083,36 +1083,36 @@ String CodeTypedef::to_string() void CodeTypedef::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); - append( result, "typedef "); + append( & result, "typedef "); // Determines if the typedef is a function typename if ( ast->UnderlyingType->ReturnType ) - append( result, ast->UnderlyingType.to_string() ); + append( & result, ast->UnderlyingType.to_string() ); else - append_fmt( result, "%S %S", ast->UnderlyingType.to_string(), ast->Name ); + append_fmt( & result, "%S %S", ast->UnderlyingType.to_string(), ast->Name ); if ( ast->UnderlyingType->Type == ECode::Typename && ast->UnderlyingType->ArrExpr ) { - append_fmt( result, "[ %S ];", ast->UnderlyingType->ArrExpr->to_string() ); + append_fmt( & result, "[ %S ];", ast->UnderlyingType->ArrExpr->to_string() ); AST* next_arr_expr = ast->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( result, "[ %S ];", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ];", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } else { - append( result, ";" ); + append( & result, ";" ); } if ( ast->InlineCmt ) - append_fmt( result, " %S", ast->InlineCmt->Content); + append_fmt( & result, " %S", ast->InlineCmt->Content); else - append( result, "\n"); + append( & result, "\n"); } String CodeType::to_string() @@ -1132,9 +1132,9 @@ void CodeType::to_string( String& result ) else { if ( ast->Specs ) - append_fmt( result, "%S ( %S ) ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); + append_fmt( & result, "%S ( %S ) ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); else - append_fmt( result, "%S ( %S ) ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); + append_fmt( & result, "%S ( %S ) ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); } break; @@ -1143,13 +1143,13 @@ void CodeType::to_string( String& result ) if ( ast->ReturnType && ast->Params ) { if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); else { if ( ast->Specs ) - append_fmt( result, "%S %S ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); + append_fmt( & result, "%S %S ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); else - append_fmt( result, "%S %S ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); + append_fmt( & result, "%S %S ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); } return; @@ -1157,15 +1157,15 @@ void CodeType::to_string( String& result ) #endif if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->Specs ) - append_fmt( result, "%S %S", ast->Name, ast->Specs.to_string() ); + append_fmt( & result, "%S %S", ast->Name, ast->Specs.to_string() ); else - append_fmt( result, "%S", ast->Name ); + append_fmt( & result, "%S", ast->Name ); if ( ast->IsParamPack ) - append( result, "..."); + append( & result, "..."); } String CodeUnion::to_string() @@ -1178,16 +1178,16 @@ String CodeUnion::to_string() void CodeUnion::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); - append( result, "union " ); + append( & result, "union " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->Name ) { - append_fmt( result, "%S\n{\n%S\n}" + append_fmt( & result, "%S\n{\n%S\n}" , ast->Name , ast->Body.to_string() ); @@ -1195,13 +1195,13 @@ void CodeUnion::to_string( String& result ) else { // Anonymous union - append_fmt( result, "\n{\n%S\n}" + append_fmt( & result, "\n{\n%S\n}" , ast->Body.to_string() ); } if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( result, ";\n"); + append( & result, ";\n"); } String CodeUsing::to_string() @@ -1223,44 +1223,44 @@ String CodeUsing::to_string() void CodeUsing::to_string( String& result ) { if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->UnderlyingType ) { - append_fmt( result, "using %S = %S", ast->Name, ast->UnderlyingType.to_string() ); + append_fmt( & result, "using %S = %S", ast->Name, ast->UnderlyingType.to_string() ); if ( ast->UnderlyingType->ArrExpr ) { - append_fmt( result, "[ %S ]", ast->UnderlyingType->ArrExpr.to_string() ); + append_fmt( & result, "[ %S ]", ast->UnderlyingType->ArrExpr.to_string() ); AST* next_arr_expr = ast->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( result, "[ %S ]", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ]", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } - append( result, ";" ); + append( & result, ";" ); } else - append_fmt( result, "using %S;", ast->Name ); + append_fmt( & result, "using %S;", ast->Name ); if ( ast->InlineCmt ) - append_fmt( result, " %S\n", ast->InlineCmt->Content ); + append_fmt( & result, " %S\n", ast->InlineCmt->Content ); else - append( result, "\n"); + append( & result, "\n"); } void CodeUsing::to_string_ns( String& result ) { if ( ast->InlineCmt ) - append_fmt( result, "using namespace $S; %S", ast->Name, ast->InlineCmt->Content ); + append_fmt( & result, "using namespace $S; %S", ast->Name, ast->InlineCmt->Content ); else - append_fmt( result, "using namespace %s;\n", ast->Name ); + append_fmt( & result, "using namespace %s;\n", ast->Name ); } String CodeVar::to_string() @@ -1277,18 +1277,18 @@ void CodeVar::to_string( String& result ) // Its a comma-separated variable ( a NextVar ) if ( ast->Specs ) - append_fmt( result, "%S ", ast->Specs.to_string() ); + append_fmt( & result, "%S ", ast->Specs.to_string() ); - append( result, ast->Name ); + append( & result, ast->Name ); if ( ast->ValueType->ArrExpr ) { - append_fmt( result, "[ %S ]", ast->ValueType->ArrExpr.to_string() ); + append_fmt( & result, "[ %S ]", ast->ValueType->ArrExpr.to_string() ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( result, "[ %S ]", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ]", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } @@ -1296,107 +1296,107 @@ void CodeVar::to_string( String& result ) if ( ast->Value ) { if ( ast->VarConstructorInit ) - append_fmt( result, "( %S ", ast->Value.to_string() ); + append_fmt( & result, "( %S ", ast->Value.to_string() ); else - append_fmt( result, " = %S", ast->Value.to_string() ); + append_fmt( & result, " = %S", ast->Value.to_string() ); } // Keep the chain going... if ( ast->NextVar ) - append_fmt( result, ", %S", ast->NextVar.to_string() ); + append_fmt( & result, ", %S", ast->NextVar.to_string() ); if ( ast->VarConstructorInit ) - append( result, " )"); + append( & result, " )"); return; } if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( result, "export " ); + append( & result, "export " ); if ( ast->Attributes || ast->Specs ) { if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Specs.to_string() ); + append_fmt( & result, "%S ", ast->Specs.to_string() ); if ( ast->Specs ) - append_fmt( result, "%S\n", ast->Specs.to_string() ); + append_fmt( & result, "%S\n", ast->Specs.to_string() ); - append_fmt( result, "%S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( & result, "%S %S", ast->ValueType.to_string(), ast->Name ); if ( ast->ValueType->ArrExpr ) { - append_fmt( result, "[ %S ]", ast->ValueType->ArrExpr.to_string() ); + append_fmt( & result, "[ %S ]", ast->ValueType->ArrExpr.to_string() ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( result, "[ %S ]", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ]", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } if ( ast->BitfieldSize ) - append_fmt( result, " : %S", ast->BitfieldSize.to_string() ); + append_fmt( & result, " : %S", ast->BitfieldSize.to_string() ); if ( ast->Value ) { if ( ast->VarConstructorInit ) - append_fmt( result, "( %S ", ast->Value.to_string() ); + append_fmt( & result, "( %S ", ast->Value.to_string() ); else - append_fmt( result, " = %S", ast->Value.to_string() ); + append_fmt( & result, " = %S", ast->Value.to_string() ); } if ( ast->NextVar ) - append_fmt( result, ", %S", ast->NextVar.to_string() ); + append_fmt( & result, ", %S", ast->NextVar.to_string() ); if ( ast->VarConstructorInit ) - append( result, " )"); + append( & result, " )"); if ( ast->InlineCmt ) - append_fmt( result, "; %S", ast->InlineCmt->Content); + append_fmt( & result, "; %S", ast->InlineCmt->Content); else - append( result, ";\n" ); + append( & result, ";\n" ); return; } if ( ast->BitfieldSize ) - append_fmt( result, "%S %S : %S", ast->ValueType.to_string(), ast->Name, ast->BitfieldSize.to_string() ); + append_fmt( & result, "%S %S : %S", ast->ValueType.to_string(), ast->Name, ast->BitfieldSize.to_string() ); else if ( ast->ValueType->ArrExpr ) { - append_fmt( result, "%S %S[ %S ]", ast->ValueType.to_string(), ast->Name, ast->ValueType->ArrExpr.to_string() ); + append_fmt( & result, "%S %S[ %S ]", ast->ValueType.to_string(), ast->Name, ast->ValueType->ArrExpr.to_string() ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( result, "[ %S ]", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ]", next_arr_expr->to_string() ); next_arr_expr = next_arr_expr->Next; } } else - append_fmt( result, "%S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( & result, "%S %S", ast->ValueType.to_string(), ast->Name ); if ( ast->Value ) { if ( ast->VarConstructorInit ) - append_fmt( result, "( %S ", ast->Value.to_string() ); + append_fmt( & result, "( %S ", ast->Value.to_string() ); else - append_fmt( result, " = %S", ast->Value.to_string() ); + append_fmt( & result, " = %S", ast->Value.to_string() ); } if ( ast->NextVar ) - append_fmt( result, ", %S", ast->NextVar.to_string() ); + append_fmt( & result, ", %S", ast->NextVar.to_string() ); if ( ast->VarConstructorInit ) - append( result, " )"); + append( & result, " )"); - append( result, ";" ); + append( & result, ";" ); if ( ast->InlineCmt ) - append_fmt( result, " %S", ast->InlineCmt->Content); + append_fmt( & result, " %S", ast->InlineCmt->Content); else - append( result, "\n"); + append( & result, "\n"); } diff --git a/project/components/interface.cpp b/project/components/interface.cpp index 9b3cbfb..b455259 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -321,7 +321,7 @@ void deinit() } while ( left--, left ); - destroy(StringCache); + destroy(& StringCache); free( & CodePools); free( & StringArenas); @@ -403,7 +403,7 @@ StringCached get_cached_string( StrC str ) } String result = string_make( get_string_allocator( str.Len ), str ); - set(StringCache, key, result ); + set(& StringCache, key, result ); return result; } diff --git a/project/components/interface.untyped.cpp b/project/components/interface.untyped.cpp index bafc313..4f49d5f 100644 --- a/project/components/interface.untyped.cpp +++ b/project/components/interface.untyped.cpp @@ -27,8 +27,7 @@ ssize token_fmt_va( char* buf, usize buf_size, s32 num_tokens, va_list va ) StrC value = va_arg( va, StrC ); u32 key = crc32( token, str_len(token) ); - - set(tok_map, key, value ); + set(& tok_map, key, value ); } } diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index c0e9c9b..fab42a1 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -474,15 +474,15 @@ CodeComment def_comment( StrC content ) length++; str_copy( line, scanner, length ); - append_fmt(cmt_formatted, "//%.*s", length, line ); + append_fmt(& cmt_formatted, "//%.*s", length, line ); mem_set( line, 0, MaxCommentLineLength ); scanner += length; } while ( scanner <= end ); - if ( back(cmt_formatted) != '\n' ) - append( cmt_formatted, "\n" ); + if ( * back(& cmt_formatted) != '\n' ) + append( & cmt_formatted, "\n" ); Code result = make_code(); @@ -490,7 +490,7 @@ CodeComment def_comment( StrC content ) result->Name = get_cached_string( cmt_formatted ); result->Content = result->Name; - free(cmt_formatted); + free(& cmt_formatted); return (CodeComment) result; } diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index 8e4164f..33f957e 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -93,7 +93,7 @@ struct Token StrC type_str = ETokType::to_str( Type ); - append_fmt( result, "Line: %d Column: %d, Type: %.*s Content: %.*s" + append_fmt( & result, "Line: %d Column: %d, Type: %.*s Content: %.*s" , Line, Column , type_str.Len, type_str.Ptr , Length, Text @@ -341,7 +341,7 @@ s32 lex_preprocessor_directive( append( & Tokens, name ); u64 key = crc32( name.Text, name.Length ); - set(defines, key, name ); + set(& defines, key, name ); } Token preprocess_content = { scanner, 0, TokType::Preprocess_Content, line, column, TF_Preprocess }; @@ -597,7 +597,7 @@ TokArray lex( StrC content ) } u64 key = crc32( entry.Data, length ); - set(defines, key, entry ); + set(& defines, key, entry ); } clear(Tokens); diff --git a/project/components/parser.cpp b/project/components/parser.cpp index f184d70..7055811 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -59,17 +59,17 @@ struct ParseContext } String line = string_make( GlobalAllocator, { length, scope_start.Text } ); - append_fmt( result, "\tScope : %s\n", line ); - free(line); + append_fmt( & result, "\tScope : %s\n", line ); + free(& line); sptr dist = (sptr)last_valid.Text - (sptr)scope_start.Text + 2; sptr length_from_err = dist; String line_from_err = string_make( GlobalAllocator, { length_from_err, last_valid.Text } ); if ( length_from_err < 100 ) - append_fmt(result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); + append_fmt(& result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); else - append_fmt(result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column ); + append_fmt(& result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column ); StackNode* curr_scope = Scope; s32 level = 0; @@ -77,11 +77,11 @@ struct ParseContext { if ( curr_scope->Name ) { - append_fmt(result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text ); + append_fmt(& result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text ); } else { - append_fmt(result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr ); + append_fmt(& result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr ); } curr_scope = curr_scope->Prev; @@ -290,7 +290,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if ( tokleft ) move_fwd(); - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } @@ -312,7 +312,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if ( tokleft ) move_fwd(); - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } @@ -326,7 +326,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) scanner += 2; tokleft -= 2; - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } @@ -345,7 +345,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if (tokleft) move_fwd(); - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } @@ -354,10 +354,10 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if (scanner[0] == '\t') { if (pos > last_cut) - append( content, cut_ptr, cut_length); + append( & content, cut_ptr, cut_length); - if ( back( content ) != ' ' ) - append( content, ' '); + if ( * back( & content ) != ' ' ) + append( & content, ' '); move_fwd(); last_cut = sptr(scanner) - sptr(raw_text.Ptr); @@ -373,17 +373,17 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) scanner += 2; tokleft -= 2; - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } if ( pos > last_cut ) - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); // Replace with a space - if ( back( content ) != ' ' ) - append( content, ' ' ); + if ( * back( & content ) != ' ' ) + append( & content, ' ' ); scanner += 2; tokleft -= 2; @@ -400,17 +400,17 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) move_fwd(); - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); continue; } if ( pos > last_cut ) - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); // Replace with a space - if ( back( content ) != ' ' ) - append( content, ' ' ); + if ( * back( & content ) != ' ' ) + append( & content, ' ' ); move_fwd(); @@ -421,7 +421,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) // Escaped newlines if ( scanner[0] == '\\' ) { - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); s32 amount_to_skip = 1; if ( tokleft > 1 && scanner[1] == '\n' ) @@ -448,7 +448,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) // Consectuive spaces if ( tokleft > 1 && char_is_space( scanner[0] ) && char_is_space( scanner[ 1 ] ) ) { - append( content, cut_ptr, cut_length ); + append( & content, cut_ptr, cut_length ); do { move_fwd(); @@ -458,8 +458,9 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) last_cut = sptr( scanner ) - sptr( raw_text.Ptr ); // Preserve only 1 space of formattting - if ( back( content ) != ' ' ) - append( content, ' ' ); + char* last = back(& content); + if ( last == nullptr || * last != ' ' ) + append( & content, ' ' ); continue; } @@ -469,7 +470,7 @@ String strip_formatting( StrC raw_text, bool preserve_newlines = true ) if ( last_cut < raw_text.Len ) { - append( content, cut_ptr, raw_text.Len - last_cut ); + append( & content, cut_ptr, raw_text.Len - last_cut ); } #undef cut_ptr @@ -1040,7 +1041,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) if ( attributes ) { String fused = string_make_reserve( GlobalAllocator, length(attributes->Content) + length(more_attributes->Content) ); - append_fmt( fused, "%S %S", attributes->Content, more_attributes->Content ); + append_fmt( & fused, "%S %S", attributes->Content, more_attributes->Content ); attributes->Name = get_cached_string(fused); attributes->Content = attributes->Name; diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index f59fb2f..a6d6248 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -49,10 +49,6 @@ template bool resize (Array(Type)* array, usize n template bool set_capacity (Array(Type)* array, usize new_capacity); template ArrayHeader* get_header (Array(Type) array); -// template forceinline Type* begin(Array array) { return array; } -// template forceinline Type* end(Array array) { return array + get_header(array)->Num; } -// template forceinline Type* next(Array array, Type* entry) { return entry + 1; } - struct ArrayHeader { AllocatorInfo Allocator; usize Capacity; @@ -114,6 +110,10 @@ template bool set_capacity(Array& array, usize new_cap template forceinline Type* begin(Array& array) { return array; } template forceinline Type* end(Array& array) { return array + get_header(array)->Num; } template forceinline Type* next(Array& array, Type* entry) { return entry + 1; } +#else +template forceinline Type* begin(Array array) { return array; } +template forceinline Type* end(Array array) { return array + get_header(array)->Num; } +template forceinline Type* next(Array array, Type* entry) { return entry + 1; } #endif template inline @@ -402,21 +402,21 @@ struct HashTableEntry { template HashTable hashtable_init(AllocatorInfo allocator); template HashTable hashtable_init_reserve(AllocatorInfo allocator, usize num); -template void clear(HashTable& table); -template void destroy(HashTable& table); -template Type* get(HashTable& table, u64 key); -template void grow(HashTable& table); -template void rehash(HashTable& table, ssize new_num); -template void rehash_fast(HashTable& table); -template void remove(HashTable& table, u64 key); -template void remove_entry(HashTable& table, ssize idx); -template void set(HashTable& table, u64 key, Type value); -template ssize slot(HashTable& table, u64 key); -template ssize add_entry(HashTable& table, u64 key); -template HashTableFindResult find(HashTable& table, u64 key); -template bool full(HashTable& table); -template void map(HashTable& table, void (*map_proc)(u64 key, Type value)); -template void map_mut(HashTable& table, void (*map_proc)(u64 key, Type* value)); +template void clear (HashTable table); +template void destroy (HashTable* table); +template Type* get (HashTable table, u64 key); +template void grow (HashTable* table); +template void rehash (HashTable* table, ssize new_num); +template void rehash_fast (HashTable table); +template void remove (HashTable table, u64 key); +template void remove_entry (HashTable table, ssize idx); +template void set (HashTable* table, u64 key, Type value); +template ssize slot (HashTable table, u64 key); +template ssize add_entry (HashTable* table, u64 key); +template HashTableFindResult find (HashTable table, u64 key); +template bool full (HashTable table); +template void map (HashTable table, void (*map_proc)(u64 key, Type value)); +template void map_mut (HashTable table, void (*map_proc)(u64 key, Type* value)); static constexpr f32 HashTable_CriticalLoadScale = 0.7f; @@ -447,6 +447,14 @@ struct HashTable #endif }; +#if GEN_SUPPORT_CPP_REFERENCES +template void destroy (HashTable& table) { destroy(& table); } +template void grow (HashTable& table) { grow(& table); } +template void rehash (HashTable& table, ssize new_num) { rehash(& table, new_num); } +template void set (HashTable& table, u64 key, Type value) { set(& table, key, value); } +template ssize add_entry(HashTable& table, u64 key) { add_entry(& table, key); } +#endif + template inline HashTable hashtable_init(AllocatorInfo allocator) { HashTable result = hashtable_init_reserve(allocator, 8); @@ -468,65 +476,65 @@ HashTable hashtable_init_reserve(AllocatorInfo allocator, usize num) } template inline -void clear(HashTable& table) { +void clear(HashTable table) { clear(table.Entries); fill(table.Hashes, 0, num(table.Hashes), -1); } template inline -void destroy(HashTable& table) { - if (table.Hashes && get_header(table.Hashes)->Capacity) { - free(& table.Hashes); - free(& table.Entries); +void destroy(HashTable* table) { + if (table->Hashes && get_header(table->Hashes)->Capacity) { + free(& table->Hashes); + free(& table->Entries); } } template inline -Type* get(HashTable& table, u64 key) { +Type* get(HashTable table, u64 key) { ssize idx = find(table, key).EntryIndex; if (idx >= 0) - return &table.Entries[idx].Value; + return & table.Entries[idx].Value; return nullptr; } template inline -void map(HashTable& table, void (*map_proc)(u64 key, Type value)) { +void map(HashTable table, void (*map_proc)(u64 key, Type value)) { GEN_ASSERT_NOT_NULL(map_proc); - for (ssize idx = 0; idx < ssize(table.Entries.num()); ++idx) { + for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) { map_proc(table.Entries[idx].Key, table.Entries[idx].Value); } } template inline -void map_mut(HashTable& table, void (*map_proc)(u64 key, Type* value)) { +void map_mut(HashTable table, void (*map_proc)(u64 key, Type* value)) { GEN_ASSERT_NOT_NULL(map_proc); - for (ssize idx = 0; idx < ssize(table.Entries.num()); ++idx) { - map_proc(table.Entries[idx].Key, &table.Entries[idx].Value); + for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) { + map_proc(table.Entries[idx].Key, & table.Entries[idx].Value); } } template inline -void grow(HashTable& table) { - ssize new_num = array_grow_formula(num(table.Entries)); +void grow(HashTable* table) { + ssize new_num = array_grow_formula(num(table->Entries)); rehash(table, new_num); } template inline -void rehash(HashTable& table, ssize new_num) +void rehash(HashTable* table, ssize new_num) { ssize last_added_index; - HashTable new_ht = hashtable_init_reserve(get_header(table.Hashes)->Allocator, new_num); + HashTable new_ht = hashtable_init_reserve(get_header(table->Hashes)->Allocator, new_num); - for (ssize idx = 0; idx < ssize(num(table.Entries)); ++idx) + for (ssize idx = 0; idx < ssize(num(table->Entries)); ++idx) { HashTableFindResult find_result; - HashTableEntry& entry = table.Entries[idx]; + HashTableEntry& entry = table->Entries[idx]; find_result = find(new_ht, entry.Key); - last_added_index = add_entry(new_ht, entry.Key); + last_added_index = add_entry(& new_ht, entry.Key); if (find_result.PrevIndex < 0) new_ht.Hashes[find_result.HashIndex] = last_added_index; @@ -538,21 +546,21 @@ void rehash(HashTable& table, ssize new_num) } destroy(table); - table = new_ht; + * table = new_ht; } template inline -void rehash_fast(HashTable& table) +void rehash_fast(HashTable table) { ssize idx; - for (idx = 0; idx < ssize(table.Entries.num()); idx++) + for (idx = 0; idx < ssize(num(table.Entries)); idx++) table.Entries[idx].Next = -1; - for (idx = 0; idx < ssize(table.Hashes.num()); idx++) + for (idx = 0; idx < ssize(num(table.Hashes)); idx++) table.Hashes[idx] = -1; - for (idx = 0; idx < ssize(table.Entries.num()); idx++) + for (idx = 0; idx < ssize(num(table.Entries)); idx++) { HashTableEntry* entry; HashTableFindResult find_result; @@ -568,30 +576,30 @@ void rehash_fast(HashTable& table) } template inline -void remove(HashTable& table, u64 key) { +void remove(HashTable table, u64 key) { HashTableFindResult find_result = find(table, key); if (find_result.EntryIndex >= 0) { - table.Entries.remove_at(find_result.EntryIndex); + remove_at(table.Entries, find_result.EntryIndex); rehash_fast(table); } } template inline -void remove_entry(HashTable& table, ssize idx) { - table.Entries.remove_at(idx); +void remove_entry(HashTable table, ssize idx) { + remove_at(table.Entries, idx); } template inline -void set(HashTable& table, u64 key, Type value) +void set(HashTable* table, u64 key, Type value) { ssize idx; HashTableFindResult find_result; - if (full(table)) + if (full(* table)) grow(table); - find_result = find(table, key); + find_result = find(* table, key); if (find_result.EntryIndex >= 0) { idx = find_result.EntryIndex; } @@ -600,22 +608,22 @@ void set(HashTable& table, u64 key, Type value) idx = add_entry(table, key); if (find_result.PrevIndex >= 0) { - table.Entries[find_result.PrevIndex].Next = idx; + table->Entries[find_result.PrevIndex].Next = idx; } else { - table.Hashes[find_result.HashIndex] = idx; + table->Hashes[find_result.HashIndex] = idx; } } - table.Entries[idx].Value = value; + table->Entries[idx].Value = value; - if (full(table)) + if (full(* table)) grow(table); } template inline -ssize slot(HashTable& table, u64 key) { - for (ssize idx = 0; idx < ssize(table.Hashes.num()); ++idx) +ssize slot(HashTable table, u64 key) { + for (ssize idx = 0; idx < ssize(num(table.Hashes)); ++idx) if (table.Hashes[idx] == key) return idx; @@ -623,17 +631,17 @@ ssize slot(HashTable& table, u64 key) { } template inline -ssize add_entry(HashTable& table, u64 key) { +ssize add_entry(HashTable* table, u64 key) { ssize idx; HashTableEntry entry = { key, -1 }; - idx = num(table.Entries); - append( & table.Entries, entry); + idx = num(table->Entries); + append( & table->Entries, entry); return idx; } template inline -HashTableFindResult find(HashTable& table, u64 key) +HashTableFindResult find(HashTable table, u64 key) { HashTableFindResult result = { -1, -1, -1 }; @@ -656,7 +664,7 @@ HashTableFindResult find(HashTable& table, u64 key) } template inline -bool full(HashTable& table) { +bool full(HashTable table) { usize critical_load = usize(HashTable_CriticalLoadScale * f32(num(table.Hashes))); b32 result = num(table.Entries) > critical_load; return result; diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index 3058f87..3c539ca 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -33,40 +33,41 @@ StrC to_str( char const* str ) { struct StringHeader; struct String; -String string_make(AllocatorInfo allocator, char const* str); -String string_make(AllocatorInfo allocator, StrC str); -String string_make_reserve(AllocatorInfo allocator, ssize capacity); -String string_make_length(AllocatorInfo allocator, char const* str, ssize length); -String string_fmt(AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...); -String string_fmt_buf(AllocatorInfo allocator, char const* fmt, ...); -String string_join(AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue); -usize string_grow_formula(usize value); -bool are_equal(String const& lhs, String const& rhs); -bool are_equal(String const& lhs, StrC rhs); -bool make_space_for(String& str, char const* to_append, ssize add_len); -bool append(String& str, char c); -bool append(String& str, char const* str_to_append); -bool append(String& str, char const* str_to_append, ssize length); -bool append(String& str, StrC str_to_append); -bool append(String& str, const String other); -bool append_fmt(String& str, char const* fmt, ...); -ssize avail_space(String const& str); -char& back(String& str); -bool contains(String const& str, StrC substring); -bool contains(String const& str, String const& substring); -ssize capacity(String const& str); -void clear(String& str); -String duplicate(String const& str, AllocatorInfo allocator); -void free(String& str); -StringHeader& get_header(String& str); -ssize length(String const& str); -b32 starts_with(String const& str, StrC substring); -b32 starts_with(String const& str, String substring); -void skip_line(String& str); -void strip_space(String& str); -void trim(String& str, char const* cut_set); -void trim_space(String& str); -String visualize_whitespace(String const& str); +usize string_grow_formula(usize value); + +String string_make (AllocatorInfo allocator, char const* str); +String string_make (AllocatorInfo allocator, StrC str); +String string_make_reserve (AllocatorInfo allocator, ssize capacity); +String string_make_length (AllocatorInfo allocator, char const* str, ssize length); +String string_fmt (AllocatorInfo allocator, char* buf, ssize buf_size, char const* fmt, ...); +String string_fmt_buf (AllocatorInfo allocator, char const* fmt, ...); +String string_join (AllocatorInfo allocator, char const** parts, ssize num_parts, char const* glue); +bool are_equal (String const lhs, String const rhs); +bool are_equal (String const lhs, StrC rhs); +bool make_space_for (String* str, char const* to_append, ssize add_len); +bool append (String* str, char c); +bool append (String* str, char const* str_to_append); +bool append (String* str, char const* str_to_append, ssize length); +bool append (String* str, StrC str_to_append); +bool append (String* str, String const other); +bool append_fmt (String* str, char const* fmt, ...); +ssize avail_space (String const str); +char* back (String str); +bool contains (String const str, StrC substring); +bool contains (String const str, String const substring); +ssize capacity (String const str); +void clear (String str); +String duplicate (String const str, AllocatorInfo allocator); +void free (String* str); +StringHeader* get_header (String str); +ssize length (String const str); +b32 starts_with (String const str, StrC substring); +b32 starts_with (String const str, String substring); +void skip_line (String str); +void strip_space (String str); +void trim (String str, char const* cut_set); +void trim_space (String str); +String visualize_whitespace(String const str); struct StringHeader { AllocatorInfo Allocator; @@ -129,31 +130,31 @@ struct String return GEN_NS string_make(allocator, buf); } - forceinline bool make_space_for(char const* str, ssize add_len) { return GEN_NS make_space_for(*this, str, add_len); } - forceinline bool append(char c) { return GEN_NS append(*this, c); } - forceinline bool append(char const* str) { return GEN_NS append(*this, str); } - forceinline bool append(char const* str, ssize length) { return GEN_NS append(*this, str, length); } - forceinline bool append(StrC str) { return GEN_NS append(*this, str); } - forceinline bool append(const String other) { return GEN_NS append(*this, other); } - forceinline ssize avail_space() const { return GEN_NS avail_space(*this); } - forceinline char& back() { return GEN_NS back(*this); } - forceinline bool contains(StrC substring) const { return GEN_NS contains(*this, substring); } - forceinline bool contains(String const& substring) const { return GEN_NS contains(*this, substring); } - forceinline ssize capacity() const { return GEN_NS capacity(*this); } - forceinline void clear() { GEN_NS clear(*this); } - forceinline String duplicate(AllocatorInfo allocator) const { return GEN_NS duplicate(*this, allocator); } - forceinline void free() { GEN_NS free(*this); } + forceinline bool make_space_for(char const* str, ssize add_len) { return GEN_NS make_space_for(this, str, add_len); } + forceinline bool append(char c) { return GEN_NS append(this, c); } + forceinline bool append(char const* str) { return GEN_NS append(this, str); } + forceinline bool append(char const* str, ssize length) { return GEN_NS append(this, str, length); } + forceinline bool append(StrC str) { return GEN_NS append(this, str); } + forceinline bool append(const String other) { return GEN_NS append(this, other); } + forceinline ssize avail_space() const { return GEN_NS avail_space(* this); } + forceinline char* back() { return GEN_NS back(* this); } + forceinline bool contains(StrC substring) const { return GEN_NS contains(* this, substring); } + forceinline bool contains(String const& substring) const { return GEN_NS contains(* this, substring); } + forceinline ssize capacity() const { return GEN_NS capacity(* this); } + forceinline void clear() { GEN_NS clear(* this); } + forceinline String duplicate(AllocatorInfo allocator) const { return GEN_NS duplicate(* this, allocator); } + forceinline void free() { GEN_NS free(this); } forceinline bool is_equal(String const& other) const { return GEN_NS are_equal(* this, other); } forceinline bool is_equal(StrC other) const { return GEN_NS are_equal(* this, other); } - forceinline ssize length() const { return GEN_NS length(*this); } - forceinline b32 starts_with(StrC substring) const { return GEN_NS starts_with(*this, substring); } - forceinline b32 starts_with(String substring) const { return GEN_NS starts_with(*this, substring); } - forceinline void skip_line() { GEN_NS skip_line(*this); } - forceinline void strip_space() { GEN_NS strip_space(*this); } - forceinline void trim(char const* cut_set) { GEN_NS trim(*this, cut_set); } - forceinline void trim_space() { GEN_NS trim_space(*this); } - forceinline String visualize_whitespace() const { return GEN_NS visualize_whitespace(*this); } - forceinline StringHeader& get_header() { return GEN_NS get_header(*this); } + forceinline ssize length() const { return GEN_NS length(* this); } + forceinline b32 starts_with(StrC substring) const { return GEN_NS starts_with(* this, substring); } + forceinline b32 starts_with(String substring) const { return GEN_NS starts_with(* this, substring); } + forceinline void skip_line() { GEN_NS skip_line(* this); } + forceinline void strip_space() { GEN_NS strip_space(* this); } + forceinline void trim(char const* cut_set) { GEN_NS trim(* this, cut_set); } + forceinline void trim_space() { GEN_NS trim_space(* this); } + forceinline String visualize_whitespace() const { return GEN_NS visualize_whitespace(* this); } + forceinline StringHeader& get_header() { return * GEN_NS get_header(* this); } bool append_fmt(char const* fmt, ...) { ssize res; @@ -164,13 +165,26 @@ struct String res = str_fmt_va(buf, count_of(buf) - 1, fmt, va) - 1; va_end(va); - return GEN_NS append(*this, buf, res); + return GEN_NS append(this, buf, res); } #pragma endregion Member Mapping #endif }; #endif +#if GEN_SUPPORT_CPP_REFERENCES +bool make_space_for(String& str, char const* to_append, ssize add_len); +bool append(String& str, char c); +bool append(String& str, char const* str_to_append); +bool append(String& str, char const* str_to_append, ssize length); +bool append(String& str, StrC str_to_append); +bool append(String& str, const String other); +bool append_fmt(String& str, char const* fmt, ...); +char& back(String& str); +void clear(String& str); +void free(String& str); +#endif + inline char* begin(String& str) { return str; } inline char* end(String& str) { return scast(char*, str) + length(str); } inline char* next(String& str) { return scast(char*, str) + 1; } @@ -223,57 +237,64 @@ String string_join(AllocatorInfo allocator, char const** parts, ssize num_parts, for (ssize idx = 0; idx < num_parts; ++idx) { - append(result, parts[idx]); + append(& result, parts[idx]); if (idx < num_parts - 1) - append(result, glue); + append(& result, glue); } return result; } inline -bool append(String& str, char c) { +bool append(String* str, char c) { + GEN_ASSERT(str != nullptr); return append(str, &c, 1); } inline -bool append(String& str, char const* str_to_append) { +bool append(String* str, char const* str_to_append) { + GEN_ASSERT(str != nullptr); return append(str, str_to_append, str_len(str_to_append)); } inline -bool append(String& str, char const* str_to_append, ssize append_length) +bool append(String* str, char const* str_to_append, ssize append_length) { + GEN_ASSERT(str != nullptr); if (sptr(str_to_append) > 0) { - ssize curr_len = length(str); + ssize curr_len = length(* str); - if (!make_space_for(str, str_to_append, append_length)) + if ( ! make_space_for(str, str_to_append, append_length)) return false; - StringHeader& header = get_header(str); + StringHeader* header = get_header(* str); - mem_copy( scast(char*, str) + curr_len, str_to_append, append_length); + char* Data = * str; + mem_copy( Data + curr_len, str_to_append, append_length); - str[curr_len + append_length] = '\0'; + Data[curr_len + append_length] = '\0'; - header.Length = curr_len + append_length; + header->Length = curr_len + append_length; } return str_to_append != nullptr; } inline -bool append(String& str, StrC str_to_append) { +bool append(String* str, StrC str_to_append) { + GEN_ASSERT(str != nullptr); return append(str, str_to_append.Ptr, str_to_append.Len); } inline -bool append(String& str, const String other) { +bool append(String* str, const String other) { + GEN_ASSERT(str != nullptr); return append(str, other, length(other)); } -bool append_fmt(String& str, char const* fmt, ...) { +bool append_fmt(String* str, char const* fmt, ...) { + GEN_ASSERT(str != nullptr); ssize res; char buf[GEN_PRINTF_MAXLEN] = { 0 }; @@ -286,7 +307,7 @@ bool append_fmt(String& str, char const* fmt, ...) { } inline -bool are_equal(String const& lhs, String const& rhs) +bool are_equal(String const lhs, String const rhs) { if (length(lhs) != length(rhs)) return false; @@ -299,7 +320,7 @@ bool are_equal(String const& lhs, String const& rhs) } inline -bool are_equal(String const& lhs, StrC rhs) +bool are_equal(String const lhs, StrC rhs) { if (length(lhs) != (rhs.Len)) return false; @@ -312,26 +333,26 @@ bool are_equal(String const& lhs, StrC rhs) } inline -ssize avail_space(String const& str) { - StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); - return header.Capacity - header.Length; +ssize avail_space(String const str) { + StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); + return header->Capacity - header->Length; } inline -char& back(String& str) { - return str[length(str) - 1]; +char* back(String* str) { + return & (*str)[length(* str) - 1]; } inline -bool contains(String const& str, StrC substring) +bool contains(String const str, StrC substring) { - StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); + StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); - if (substring.Len > header.Length) + if (substring.Len > header->Length) return false; - ssize main_len = header.Length; - ssize sub_len = substring.Len; + ssize main_len = header->Length; + ssize sub_len = substring.Len; for (ssize idx = 0; idx <= main_len - sub_len; ++idx) { @@ -343,15 +364,15 @@ bool contains(String const& str, StrC substring) } inline -bool contains(String const& str, String const& substring) +bool contains(String const str, String const substring) { - StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); + StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); - if (length(substring) > header.Length) + if (length(substring) > header->Length) return false; - ssize main_len = header.Length; - ssize sub_len = length(substring); + ssize main_len = header->Length; + ssize sub_len = length(substring); for (ssize idx = 0; idx <= main_len - sub_len; ++idx) { @@ -363,46 +384,47 @@ bool contains(String const& str, String const& substring) } inline -ssize capacity(String const& str) { - StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); - return header.Capacity; +ssize capacity(String const str) { + StringHeader const* header = rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); + return header->Capacity; } inline -void clear(String& str) { - get_header(str).Length = 0; +void clear(String str) { + get_header(str)->Length = 0; } inline -String duplicate(String const& str, AllocatorInfo allocator) { +String duplicate(String const str, AllocatorInfo allocator) { return string_make_length(allocator, str, length(str)); } inline -void free(String& str) { - if (! str) - return; +void free(String* str) { + GEN_ASSERT(str != nullptr); + if (! (* str)) + return; - StringHeader& header = get_header(str); - GEN_NS free(header.Allocator, &header); + StringHeader* header = get_header(* str); + GEN_NS free(header->Allocator, header); } inline -StringHeader& get_header(String& str) { - return *(StringHeader*)(scast(char*, str) - sizeof(StringHeader)); +StringHeader* get_header(String str) { + return (StringHeader*)(scast(char*, str) - sizeof(StringHeader)); } inline -ssize length(String const& str) +ssize length(String const str) { StringHeader const& header = *rcast(StringHeader const*, scast(char const*, str) - sizeof(StringHeader)); return header.Length; } inline -bool make_space_for(String& str, char const* to_append, ssize add_len) +bool make_space_for(String* str, char const* to_append, ssize add_len) { - ssize available = avail_space(str); + ssize available = avail_space(* str); if (available >= add_len) { return true; @@ -413,12 +435,12 @@ bool make_space_for(String& str, char const* to_append, ssize add_len) void* ptr; void* new_ptr; - AllocatorInfo allocator = get_header(str).Allocator; - StringHeader* header = nullptr; + AllocatorInfo allocator = get_header(* str)->Allocator; + StringHeader* header = nullptr; - new_len = string_grow_formula(length(str) + add_len); - ptr = &get_header(str); - old_size = size_of(StringHeader) + length(str) + 1; + new_len = string_grow_formula(length(* str) + add_len); + ptr = get_header(* str); + old_size = size_of(StringHeader) + length(* str) + 1; new_size = size_of(StringHeader) + new_len + 1; new_ptr = resize(allocator, ptr, old_size, new_size); @@ -428,16 +450,17 @@ bool make_space_for(String& str, char const* to_append, ssize add_len) header = rcast(StringHeader*, new_ptr); header->Allocator = allocator; - header->Capacity = new_len; + header->Capacity = new_len; - str.Data = rcast(char*, header + 1); + char** Data = rcast(char**, str); + * Data = rcast(char*, header + 1); return true; } } inline -b32 starts_with(String const& str, StrC substring) { +b32 starts_with(String const str, StrC substring) { if (substring.Len > length(str)) return false; @@ -446,7 +469,7 @@ b32 starts_with(String const& str, StrC substring) { } inline -b32 starts_with(String const& str, String substring) { +b32 starts_with(String const str, String substring) { if (length(substring) > length(str)) return false; @@ -455,7 +478,7 @@ b32 starts_with(String const& str, String substring) { } inline -void skip_line(String& str) +void skip_line(String str) { #define current (*scanner) char* scanner = str.Data; @@ -471,23 +494,23 @@ void skip_line(String& str) mem_move(str.Data, scanner, new_length); - StringHeader* header = &get_header(str); + StringHeader* header = get_header(str); header->Length = new_length; #undef current } inline -void strip_space(String& str) +void strip_space(String str) { - char* write_pos = str.Data; - char* read_pos = str.Data; + char* write_pos = str; + char* read_pos = str; - while (*read_pos) + while (* read_pos) { - if (!char_is_space(*read_pos)) + if (! char_is_space(* read_pos)) { - *write_pos = *read_pos; - write_pos++; + * write_pos = * read_pos; + write_pos++; } read_pos++; } @@ -495,11 +518,11 @@ void strip_space(String& str) write_pos[0] = '\0'; // Null-terminate the modified string // Update the length if needed - get_header(str).Length = write_pos - str.Data; + get_header(str)->Length = write_pos - str.Data; } inline -void trim(String& str, char const* cut_set) +void trim(String str, char const* cut_set) { ssize len = 0; @@ -519,16 +542,16 @@ void trim(String& str, char const* cut_set) str.Data[len] = '\0'; - get_header(str).Length = len; + get_header(str)->Length = len; } inline -void trim_space(String& str) { +void trim_space(String str) { trim(str, " \t\r\n\v\f"); } inline -String visualize_whitespace(String const& str) +String visualize_whitespace(String const str) { StringHeader* header = (StringHeader*)(scast(char const*, str) - sizeof(StringHeader)); String result = string_make_reserve(header->Allocator, length(str) * 2); // Assume worst case for space requirements. @@ -536,25 +559,25 @@ String visualize_whitespace(String const& str) for (auto c : str) switch (c) { case ' ': - append(result, txt("·")); + append(& result, txt("·")); break; case '\t': - append(result, txt("→")); + append(& result, txt("→")); break; case '\n': - append(result, txt("↵")); + append(& result, txt("↵")); break; case '\r': - append(result, txt("⏎")); + append(& result, txt("⏎")); break; case '\v': - append(result, txt("⇕")); + append(& result, txt("⇕")); break; case '\f': - append(result, txt("⌂")); + append(& result, txt("⌂")); break; default: - append(result, c); + append(& result, c); break; } diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 5fa885c..388c0e9 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -27,8 +27,8 @@ CodeBody gen_ecode( char const* path ) { char const* code = node.string; - append_fmt( enum_entries, "%s,\n", code ); - append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", code, code ); + append_fmt( & enum_entries, "%s,\n", code ); + append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", code, code ); } CodeEnum enum_code = parse_enum(gen::token_fmt_impl((3 + 1) / 2, "entries", (StrC)enum_entries, "enum Type : u32 { NumTypes };")); @@ -76,8 +76,8 @@ CodeBody gen_eoperator( char const* path ) char const* enum_str = enum_strs[idx].string; char const* entry_to_str = str_strs [idx].string; - append_fmt( enum_entries, "%s,\n", enum_str ); - append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + append_fmt( & enum_entries, "%s,\n", enum_str ); + append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } CodeEnum enum_code = parse_enum(token_fmt("entries", (StrC)enum_entries, stringize( @@ -132,8 +132,8 @@ CodeBody gen_especifier( char const* path ) char const* enum_str = enum_strs[idx].string; char const* entry_to_str = str_strs [idx].string; - append_fmt( enum_entries, "%s,\n", enum_str ); - append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + append_fmt( & enum_entries, "%s,\n", enum_str ); + append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } CodeEnum enum_code = parse_enum(token_fmt("entries", (StrC)enum_entries, stringize( @@ -249,8 +249,8 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) char const* enum_str = enum_strs[idx].string; char const* entry_to_str = enum_str_strs [idx].string; - append_fmt( enum_entries, "%s,\n", enum_str ); - append_fmt( to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + append_fmt( & enum_entries, "%s,\n", enum_str ); + append_fmt( & to_str_entries, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); } for ( usize idx = 0; idx < num(attribute_strs); idx++ ) @@ -258,14 +258,14 @@ CodeBody gen_etoktype( char const* etok_path, char const* attr_path ) char const* attribute_str = attribute_strs[idx].string; char const* entry_to_str = attribute_str_strs [idx].string; - append_fmt( attribute_entries, "Attribute_%s,\n", attribute_str ); - append_fmt( to_str_attributes, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); - append_fmt( attribute_define_entries, "Entry( Attribute_%s, \"%s\" )", attribute_str, entry_to_str ); + append_fmt( & attribute_entries, "Attribute_%s,\n", attribute_str ); + append_fmt( & to_str_attributes, "{ sizeof(\"%s\"), \"%s\" },\n", entry_to_str, entry_to_str); + append_fmt( & attribute_define_entries, "Entry( Attribute_%s, \"%s\" )", attribute_str, entry_to_str ); if ( idx < num(attribute_strs) - 1 ) - append( attribute_define_entries, " \\\n"); + append( & attribute_define_entries, " \\\n"); else - append( attribute_define_entries, "\n"); + append( & attribute_define_entries, "\n"); } #pragma push_macro("GEN_DEFINE_ATTRIBUTE_TOKENS") From 937235b776cbadeaa00c8fa35ece86246c1d20d8 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 00:03:38 -0500 Subject: [PATCH 027/112] progress (Code) --- project/components/ast.cpp | 4 +- project/components/ast.hpp | 29 +- project/components/gen/ast_inlines.hpp | 406 ++++------------------- project/components/inlines.hpp | 2 +- project/components/interface.cpp | 10 +- project/components/interface.parsing.cpp | 40 +-- project/components/interface.untyped.cpp | 12 +- project/components/interface.upfront.cpp | 212 ++++++------ project/components/parser.cpp | 168 +++++----- project/helpers/helper.hpp | 17 +- 10 files changed, 306 insertions(+), 594 deletions(-) diff --git a/project/components/ast.cpp b/project/components/ast.cpp index 792333f..acc572f 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -3,8 +3,8 @@ #include "static_data.cpp" #endif -Code Code::Global; -Code Code::Invalid; +global Code Code_Global; +global Code Code_Invalid; // This serializes all the data-members in a "debug" format, where each member is printed with its associated value. char const* debug_str(AST* self) diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 533e705..0f327db 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -151,6 +151,14 @@ template< class Type> forceinline Type tmpl_cast( Code* self ) { return * rcast( template< class Type> forceinline Type tmpl_cast( Code& self ) { return * rcast( Type*, & self ); } #endif +char const* debug_str (Code code); +Code duplicate (Code code); +bool is_equal (Code code, Code other); +bool is_body (Code code); +bool is_valid (Code code); +void set_global(Code code); +String to_string (Code code); + /* AST* wrapper - Not constantly have to append the '*' as this is written often.. @@ -158,13 +166,6 @@ template< class Type> forceinline Type tmpl_cast( Code& self ) { return * rcast( */ struct Code { -# pragma region Statics - // Used to identify ASTs that should always be duplicated. (Global constant ASTs) - static Code Global; - - // Used to identify invalid generated code. - static Code Invalid; -# pragma endregion Statics # define Using_Code( Typename ) \ char const* debug_str(); \ @@ -176,8 +177,8 @@ struct Code String to_string(); \ Typename& operator = ( AST* other ); \ Typename& operator = ( Code other ); \ - bool operator ==( Code other ); \ - bool operator !=( Code other ); \ + bool operator ==( Code other ) { return (AST*)ast == other.ast; } \ + bool operator !=( Code other ) { return (AST*)ast != other.ast; } \ operator bool(); Using_Code( Code ); @@ -243,6 +244,14 @@ struct Code #undef operator }; +#pragma region Statics +// Used to identify ASTs that should always be duplicated. (Global constant ASTs) +extern Code Code_Global; + +// Used to identify invalid generated code. +extern Code Code_Invalid; +#pragma endregion Statics + struct Code_POD { AST* ast; @@ -481,4 +490,4 @@ static_assert( sizeof(AST_POD) == AST_POD_Size, "ERROR: AST POD is not size o // Used when the its desired when omission is allowed in a definition. #define NoCode { nullptr } -#define CodeInvalid (* Code::Invalid.ast) // Uses an implicitly overloaded cast from the AST to the desired code type. +#define InvalidCode (* Code_Invalid.ast) // Uses an implicitly overloaded cast from the AST to the desired code type. diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index ec26f08..0df47a5 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -19,7 +19,7 @@ inline Code Code::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -54,7 +54,7 @@ inline void Code::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline Code& Code::operator=( Code other ) @@ -68,16 +68,6 @@ inline Code& Code::operator=( Code other ) return *this; } -inline bool Code::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool Code::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline Code::operator bool() { return ast != nullptr; @@ -95,7 +85,7 @@ inline Code CodeBody::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -130,7 +120,7 @@ inline void CodeBody::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeBody& CodeBody::operator=( Code other ) @@ -144,16 +134,6 @@ inline CodeBody& CodeBody::operator=( Code other ) return *this; } -inline bool CodeBody::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeBody::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeBody::operator bool() { return ast != nullptr; @@ -171,7 +151,7 @@ inline Code CodeAttributes::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -206,7 +186,7 @@ inline void CodeAttributes::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeAttributes& CodeAttributes::operator=( Code other ) @@ -220,16 +200,6 @@ inline CodeAttributes& CodeAttributes::operator=( Code other ) return *this; } -inline bool CodeAttributes::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeAttributes::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeAttributes::operator bool() { return ast != nullptr; @@ -267,7 +237,7 @@ inline Code CodeComment::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -302,7 +272,7 @@ inline void CodeComment::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeComment& CodeComment::operator=( Code other ) @@ -316,16 +286,6 @@ inline CodeComment& CodeComment::operator=( Code other ) return *this; } -inline bool CodeComment::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeComment::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeComment::operator bool() { return ast != nullptr; @@ -363,7 +323,7 @@ inline Code CodeConstructor::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -398,7 +358,7 @@ inline void CodeConstructor::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeConstructor& CodeConstructor::operator=( Code other ) @@ -412,16 +372,6 @@ inline CodeConstructor& CodeConstructor::operator=( Code other ) return *this; } -inline bool CodeConstructor::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeConstructor::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeConstructor::operator bool() { return ast != nullptr; @@ -459,7 +409,7 @@ inline Code CodeClass::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -494,7 +444,7 @@ inline void CodeClass::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeClass& CodeClass::operator=( Code other ) @@ -508,16 +458,6 @@ inline CodeClass& CodeClass::operator=( Code other ) return *this; } -inline bool CodeClass::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeClass::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeClass::operator bool() { return ast != nullptr; @@ -535,7 +475,7 @@ inline Code CodeDefine::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -570,7 +510,7 @@ inline void CodeDefine::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeDefine& CodeDefine::operator=( Code other ) @@ -584,16 +524,6 @@ inline CodeDefine& CodeDefine::operator=( Code other ) return *this; } -inline bool CodeDefine::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeDefine::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeDefine::operator bool() { return ast != nullptr; @@ -631,7 +561,7 @@ inline Code CodeDestructor::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -666,7 +596,7 @@ inline void CodeDestructor::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeDestructor& CodeDestructor::operator=( Code other ) @@ -680,16 +610,6 @@ inline CodeDestructor& CodeDestructor::operator=( Code other ) return *this; } -inline bool CodeDestructor::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeDestructor::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeDestructor::operator bool() { return ast != nullptr; @@ -727,7 +647,7 @@ inline Code CodeEnum::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -762,7 +682,7 @@ inline void CodeEnum::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeEnum& CodeEnum::operator=( Code other ) @@ -776,16 +696,6 @@ inline CodeEnum& CodeEnum::operator=( Code other ) return *this; } -inline bool CodeEnum::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeEnum::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeEnum::operator bool() { return ast != nullptr; @@ -823,7 +733,7 @@ inline Code CodeExec::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -858,7 +768,7 @@ inline void CodeExec::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeExec& CodeExec::operator=( Code other ) @@ -872,16 +782,6 @@ inline CodeExec& CodeExec::operator=( Code other ) return *this; } -inline bool CodeExec::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeExec::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeExec::operator bool() { return ast != nullptr; @@ -919,7 +819,7 @@ inline Code CodeExtern::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -954,7 +854,7 @@ inline void CodeExtern::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeExtern& CodeExtern::operator=( Code other ) @@ -968,16 +868,6 @@ inline CodeExtern& CodeExtern::operator=( Code other ) return *this; } -inline bool CodeExtern::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeExtern::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeExtern::operator bool() { return ast != nullptr; @@ -1015,7 +905,7 @@ inline Code CodeFriend::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -1050,7 +940,7 @@ inline void CodeFriend::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeFriend& CodeFriend::operator=( Code other ) @@ -1064,16 +954,6 @@ inline CodeFriend& CodeFriend::operator=( Code other ) return *this; } -inline bool CodeFriend::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeFriend::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeFriend::operator bool() { return ast != nullptr; @@ -1111,7 +991,7 @@ inline Code CodeFn::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -1146,7 +1026,7 @@ inline void CodeFn::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeFn& CodeFn::operator=( Code other ) @@ -1160,16 +1040,6 @@ inline CodeFn& CodeFn::operator=( Code other ) return *this; } -inline bool CodeFn::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeFn::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeFn::operator bool() { return ast != nullptr; @@ -1207,7 +1077,7 @@ inline Code CodeInclude::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -1242,7 +1112,7 @@ inline void CodeInclude::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeInclude& CodeInclude::operator=( Code other ) @@ -1256,16 +1126,6 @@ inline CodeInclude& CodeInclude::operator=( Code other ) return *this; } -inline bool CodeInclude::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeInclude::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeInclude::operator bool() { return ast != nullptr; @@ -1303,7 +1163,7 @@ inline Code CodeModule::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -1338,7 +1198,7 @@ inline void CodeModule::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeModule& CodeModule::operator=( Code other ) @@ -1352,16 +1212,6 @@ inline CodeModule& CodeModule::operator=( Code other ) return *this; } -inline bool CodeModule::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeModule::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeModule::operator bool() { return ast != nullptr; @@ -1399,7 +1249,7 @@ inline Code CodeNS::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -1434,7 +1284,7 @@ inline void CodeNS::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeNS& CodeNS::operator=( Code other ) @@ -1448,16 +1298,6 @@ inline CodeNS& CodeNS::operator=( Code other ) return *this; } -inline bool CodeNS::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeNS::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeNS::operator bool() { return ast != nullptr; @@ -1495,7 +1335,7 @@ inline Code CodeOperator::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -1530,7 +1370,7 @@ inline void CodeOperator::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeOperator& CodeOperator::operator=( Code other ) @@ -1544,16 +1384,6 @@ inline CodeOperator& CodeOperator::operator=( Code other ) return *this; } -inline bool CodeOperator::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeOperator::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeOperator::operator bool() { return ast != nullptr; @@ -1591,7 +1421,7 @@ inline Code CodeOpCast::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -1626,7 +1456,7 @@ inline void CodeOpCast::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeOpCast& CodeOpCast::operator=( Code other ) @@ -1640,16 +1470,6 @@ inline CodeOpCast& CodeOpCast::operator=( Code other ) return *this; } -inline bool CodeOpCast::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeOpCast::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeOpCast::operator bool() { return ast != nullptr; @@ -1687,7 +1507,7 @@ inline Code CodeParam::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -1722,7 +1542,7 @@ inline void CodeParam::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeParam& CodeParam::operator=( Code other ) @@ -1736,16 +1556,6 @@ inline CodeParam& CodeParam::operator=( Code other ) return *this; } -inline bool CodeParam::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeParam::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeParam::operator bool() { return ast != nullptr; @@ -1763,7 +1573,7 @@ inline Code CodePragma::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -1798,7 +1608,7 @@ inline void CodePragma::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodePragma& CodePragma::operator=( Code other ) @@ -1812,16 +1622,6 @@ inline CodePragma& CodePragma::operator=( Code other ) return *this; } -inline bool CodePragma::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodePragma::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodePragma::operator bool() { return ast != nullptr; @@ -1859,7 +1659,7 @@ inline Code CodePreprocessCond::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -1894,7 +1694,7 @@ inline void CodePreprocessCond::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodePreprocessCond& CodePreprocessCond::operator=( Code other ) @@ -1908,16 +1708,6 @@ inline CodePreprocessCond& CodePreprocessCond::operator=( Code other ) return *this; } -inline bool CodePreprocessCond::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodePreprocessCond::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodePreprocessCond::operator bool() { return ast != nullptr; @@ -1955,7 +1745,7 @@ inline Code CodeSpecifiers::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -1990,7 +1780,7 @@ inline void CodeSpecifiers::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeSpecifiers& CodeSpecifiers::operator=( Code other ) @@ -2004,16 +1794,6 @@ inline CodeSpecifiers& CodeSpecifiers::operator=( Code other ) return *this; } -inline bool CodeSpecifiers::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeSpecifiers::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeSpecifiers::operator bool() { return ast != nullptr; @@ -2031,7 +1811,7 @@ inline Code CodeStruct::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -2066,7 +1846,7 @@ inline void CodeStruct::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeStruct& CodeStruct::operator=( Code other ) @@ -2080,16 +1860,6 @@ inline CodeStruct& CodeStruct::operator=( Code other ) return *this; } -inline bool CodeStruct::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeStruct::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeStruct::operator bool() { return ast != nullptr; @@ -2107,7 +1877,7 @@ inline Code CodeTemplate::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -2142,7 +1912,7 @@ inline void CodeTemplate::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeTemplate& CodeTemplate::operator=( Code other ) @@ -2156,16 +1926,6 @@ inline CodeTemplate& CodeTemplate::operator=( Code other ) return *this; } -inline bool CodeTemplate::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeTemplate::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeTemplate::operator bool() { return ast != nullptr; @@ -2203,7 +1963,7 @@ inline Code CodeType::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -2238,7 +1998,7 @@ inline void CodeType::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeType& CodeType::operator=( Code other ) @@ -2252,16 +2012,6 @@ inline CodeType& CodeType::operator=( Code other ) return *this; } -inline bool CodeType::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeType::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeType::operator bool() { return ast != nullptr; @@ -2299,7 +2049,7 @@ inline Code CodeTypedef::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -2334,7 +2084,7 @@ inline void CodeTypedef::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeTypedef& CodeTypedef::operator=( Code other ) @@ -2348,16 +2098,6 @@ inline CodeTypedef& CodeTypedef::operator=( Code other ) return *this; } -inline bool CodeTypedef::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeTypedef::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeTypedef::operator bool() { return ast != nullptr; @@ -2395,7 +2135,7 @@ inline Code CodeUnion::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -2430,7 +2170,7 @@ inline void CodeUnion::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeUnion& CodeUnion::operator=( Code other ) @@ -2444,16 +2184,6 @@ inline CodeUnion& CodeUnion::operator=( Code other ) return *this; } -inline bool CodeUnion::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeUnion::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeUnion::operator bool() { return ast != nullptr; @@ -2491,7 +2221,7 @@ inline Code CodeUsing::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -2526,7 +2256,7 @@ inline void CodeUsing::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeUsing& CodeUsing::operator=( Code other ) @@ -2540,16 +2270,6 @@ inline CodeUsing& CodeUsing::operator=( Code other ) return *this; } -inline bool CodeUsing::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeUsing::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeUsing::operator bool() { return ast != nullptr; @@ -2587,7 +2307,7 @@ inline Code CodeVar::duplicate() if ( ast == nullptr ) { log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code::Invalid; + return Code_Invalid; } return { rcast( AST*, ast )->duplicate() }; } @@ -2622,7 +2342,7 @@ inline void CodeVar::set_global() log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); return; } - rcast( AST*, ast )->Parent = Code::Global.ast; + rcast( AST*, ast )->Parent = Code_Global.ast; } inline CodeVar& CodeVar::operator=( Code other ) @@ -2636,16 +2356,6 @@ inline CodeVar& CodeVar::operator=( Code other ) return *this; } -inline bool CodeVar::operator==( Code other ) -{ - return (AST*)ast == other.ast; -} - -inline bool CodeVar::operator!=( Code other ) -{ - return (AST*)ast != other.ast; -} - inline CodeVar::operator bool() { return ast != nullptr; diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index 8178ab2..613a8f8 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -209,7 +209,7 @@ CodeBody def_body( CodeT type ) default: log_failure( "def_body: Invalid type %s", (char const*)ECode::to_str(type) ); - return (CodeBody)Code::Invalid; + return (CodeBody)Code_Invalid; } Code diff --git a/project/components/interface.cpp b/project/components/interface.cpp index b455259..425b921 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -74,12 +74,12 @@ void* Global_Allocator_Proc( void* allocator_data, AllocType type, ssize size, s internal void define_constants() { - Code::Global = make_code(); - scast(String, Code::Global->Name) = get_cached_string( txt("Global Code") ); - scast(String, Code::Global->Content) = Code::Global->Name; + Code_Global = make_code(); + scast(String, Code_Global->Name) = get_cached_string( txt("Global Code") ); + scast(String, Code_Global->Content) = Code_Global->Name; - Code::Invalid = make_code(); - Code::Invalid.set_global(); + Code_Invalid = make_code(); + Code_Invalid.set_global(); t_empty = (CodeType) make_code(); t_empty->Type = ECode::Typename; diff --git a/project/components/interface.parsing.cpp b/project/components/interface.parsing.cpp index 726f9e4..d1161aa 100644 --- a/project/components/interface.parsing.cpp +++ b/project/components/interface.parsing.cpp @@ -15,7 +15,7 @@ CodeClass parse_class( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; push_scope(); @@ -31,7 +31,7 @@ CodeConstructor parse_constructor( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; // TODO(Ed): Constructors can have prefix attributes @@ -61,7 +61,7 @@ CodeConstructor parse_constructor( StrC def ) default : log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str( spec ), Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } // Every specifier after would be considered part of the type type signature @@ -91,7 +91,7 @@ CodeDestructor parse_destructor( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; // TODO(Ed): Destructors can have prefix attributes // TODO(Ed): Destructors can have virtual @@ -110,7 +110,7 @@ CodeEnum parse_enum( StrC def ) if ( toks.Arr == nullptr ) { Context.pop(); - return CodeInvalid; + return InvalidCode; } Context.Tokens = toks; @@ -124,7 +124,7 @@ CodeBody parse_export_body( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return parse_export_body(); @@ -137,7 +137,7 @@ CodeExtern parse_extern_link( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return parse_extern_link(); @@ -150,7 +150,7 @@ CodeFriend parse_friend( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return parse_friend(); @@ -163,7 +163,7 @@ CodeFn parse_function( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return (CodeFn) parse_function(); @@ -176,7 +176,7 @@ CodeBody parse_global_body( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; push_scope(); @@ -192,7 +192,7 @@ CodeNS parse_namespace( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return parse_namespace(); @@ -205,7 +205,7 @@ CodeOperator parse_operator( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return (CodeOperator) parse_operator(); @@ -218,7 +218,7 @@ CodeOpCast parse_operator_cast( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return parse_operator_cast(); @@ -231,7 +231,7 @@ CodeStruct parse_struct( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; push_scope(); @@ -247,7 +247,7 @@ CodeTemplate parse_template( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return parse_template(); @@ -260,7 +260,7 @@ CodeType parse_type( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return parse_type(); @@ -273,7 +273,7 @@ CodeTypedef parse_typedef( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return parse_typedef(); @@ -286,7 +286,7 @@ CodeUnion parse_union( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return parse_union(); @@ -299,7 +299,7 @@ CodeUsing parse_using( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return parse_using(); @@ -312,7 +312,7 @@ CodeVar parse_variable( StrC def ) TokArray toks = lex( def ); if ( toks.Arr == nullptr ) - return CodeInvalid; + return InvalidCode; Context.Tokens = toks; return parse_variable(); diff --git a/project/components/interface.untyped.cpp b/project/components/interface.untyped.cpp index 4f49d5f..f8beb4c 100644 --- a/project/components/interface.untyped.cpp +++ b/project/components/interface.untyped.cpp @@ -106,7 +106,7 @@ Code untyped_str( StrC content ) if ( content.Len == 0 ) { log_failure( "untyped_str: empty string" ); - return CodeInvalid; + return InvalidCode; } Code @@ -118,7 +118,7 @@ Code untyped_str( StrC content ) if ( result->Name == nullptr ) { log_failure( "untyped_str: could not cache string" ); - return CodeInvalid; + return InvalidCode; } return result; @@ -129,7 +129,7 @@ Code untyped_fmt( char const* fmt, ...) if ( fmt == nullptr ) { log_failure( "untyped_fmt: null format string" ); - return CodeInvalid; + return InvalidCode; } local_persist thread_local @@ -149,7 +149,7 @@ Code untyped_fmt( char const* fmt, ...) if ( result->Name == nullptr ) { log_failure( "untyped_fmt: could not cache string" ); - return CodeInvalid; + return InvalidCode; } return result; @@ -160,7 +160,7 @@ Code untyped_token_fmt( s32 num_tokens, ... ) if ( num_tokens == 0 ) { log_failure( "untyped_token_fmt: zero tokens" ); - return CodeInvalid; + return InvalidCode; } local_persist thread_local @@ -180,7 +180,7 @@ Code untyped_token_fmt( s32 num_tokens, ... ) if ( result->Name == nullptr ) { log_failure( "untyped_fmt: could not cache string" ); - return CodeInvalid; + return InvalidCode; } return result; diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index fab42a1..af72be6 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -381,13 +381,13 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy if ( Name_.Len <= 0 ) \ { \ log_failure( "gen::" stringize(Context_) ": Invalid name length provided - %d", Name_.Len ); \ - return CodeInvalid; \ + return InvalidCode; \ } \ \ if ( Name_.Ptr == nullptr ) \ { \ log_failure( "gen::" stringize(Context_) ": name is null" ); \ - return CodeInvalid; \ + return InvalidCode; \ } \ } @@ -395,7 +395,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy if ( ! Code_ ) \ { \ log_failure( "gen::" stringize(Context_) ": " stringize(Code_) " provided is null" ); \ - return CodeInvalid; \ + return InvalidCode; \ } #define null_or_invalid_check( Context_, Code_ ) \ @@ -403,19 +403,19 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy if ( ! Code_ ) \ { \ log_failure( "gen::" stringize(Context_) ": " stringize(Code_) " provided is null" ); \ - return CodeInvalid; \ + return InvalidCode; \ } \ \ if ( Code_->is_invalid() ) \ { \ log_failure("gen::" stringize(Context_) ": " stringize(Code_) " provided is invalid" ); \ - return CodeInvalid; \ + return InvalidCode; \ } \ } #define not_implemented( Context_ ) \ log_failure( "gen::%s: This function is not implemented" ); \ - return CodeInvalid; + return InvalidCode; #pragma endregion Helper Marcos @@ -436,7 +436,7 @@ CodeAttributes def_attributes( StrC content ) if ( content.Len <= 0 || content.Ptr == nullptr ) { log_failure( "gen::def_attributes: Invalid attributes provided" ); - return CodeInvalid; + return InvalidCode; } Code @@ -453,7 +453,7 @@ CodeComment def_comment( StrC content ) if ( content.Len <= 0 || content.Ptr == nullptr ) { log_failure( "gen::def_comment: Invalid comment provided:" ); - return CodeInvalid; + return InvalidCode; } static char line[ MaxCommentLineLength ]; @@ -502,7 +502,7 @@ CodeConstructor def_constructor( CodeParam params, Code initializer_list, Code b if ( params && params->Type != Parameters ) { log_failure("gen::def_constructor: params must be of Parameters type - %s", params.debug_str()); - return CodeInvalid; + return InvalidCode; } CodeConstructor @@ -528,7 +528,7 @@ CodeConstructor def_constructor( CodeParam params, Code initializer_list, Code b default: log_failure("gen::def_constructor: body must be either of Function_Body or Untyped type - %s", body.debug_str()); - return CodeInvalid; + return InvalidCode; } result->Type = Constructor; @@ -556,13 +556,13 @@ CodeClass def_class( StrC name if ( attributes && attributes->Type != PlatformAttributes ) { log_failure( "gen::def_class: attributes was not a 'PlatformAttributes' type: %s", attributes.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( parent && ( parent->Type != Class && parent->Type != Struct && parent->Type != Typename && parent->Type != Untyped ) ) { log_failure( "gen::def_class: parent provided is not type 'Class', 'Struct', 'Typeanme', or 'Untyped': %s", parent.debug_str() ); - return CodeInvalid; + return InvalidCode; } CodeClass @@ -580,7 +580,7 @@ CodeClass def_class( StrC name default: log_failure("gen::def_class: body must be either of Class_Body or Untyped type - %s", body.debug_str()); - return CodeInvalid; + return InvalidCode; } result->Type = Class; @@ -623,7 +623,7 @@ CodeDefine def_define( StrC name, StrC content ) if ( content.Len <= 0 || content.Ptr == nullptr ) { log_failure( "gen::def_define: Invalid value provided" ); - return CodeInvalid; + return InvalidCode; } #endif @@ -648,7 +648,7 @@ CodeDestructor def_destructor( Code body, CodeSpecifiers specifiers ) if ( specifiers && specifiers->Type != Specifiers ) { log_failure( "gen::def_destructor: specifiers was not a 'Specifiers' type: %s", specifiers.debug_str() ); - return CodeInvalid; + return InvalidCode; } CodeDestructor result = (CodeDestructor) make_code(); @@ -666,7 +666,7 @@ CodeDestructor def_destructor( Code body, CodeSpecifiers specifiers ) default: log_failure("gen::def_destructor: body must be either of Function_Body or Untyped type - %s", body.debug_str()); - return CodeInvalid; + return InvalidCode; } result->Type = Destructor; @@ -692,13 +692,13 @@ CodeEnum def_enum( StrC name if ( type && type->Type != Typename ) { log_failure( "gen::def_enum: enum underlying type provided was not of type Typename: %s", type.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( attributes && attributes->Type != PlatformAttributes ) { log_failure( "gen::def_enum: attributes was not a 'PlatformAttributes' type: %s", attributes.debug_str() ); - return CodeInvalid; + return InvalidCode; } CodeEnum @@ -716,7 +716,7 @@ CodeEnum def_enum( StrC name default: log_failure( "gen::def_enum: body must be of Enum_Body or Untyped type %s", body.debug_str()); - return CodeInvalid; + return InvalidCode; } result->Type = specifier == EnumDecl_Class ? @@ -740,7 +740,7 @@ CodeEnum def_enum( StrC name else if ( result->Type != Enum_Class_Fwd && result->Type != Enum_Fwd ) { log_failure( "gen::def_enum: enum forward declaration must have an underlying type" ); - return CodeInvalid; + return InvalidCode; } return result; @@ -751,7 +751,7 @@ CodeExec def_execution( StrC content ) if ( content.Len <= 0 || content.Ptr == nullptr ) { log_failure( "gen::def_execution: Invalid execution provided" ); - return CodeInvalid; + return InvalidCode; } Code @@ -773,7 +773,7 @@ CodeExtern def_extern_link( StrC name, Code body ) if ( body->Type != Extern_Linkage_Body && body->Type != Untyped ) { log_failure("gen::def_extern_linkage: body is not of extern_linkage or untyped type %s", body->debug_str()); - return CodeInvalid; + return InvalidCode; } CodeExtern @@ -805,7 +805,7 @@ CodeFriend def_friend( Code declaration ) default: log_failure("gen::def_friend: requires declartion to have class, function, operator, or struct - %s", declaration->debug_str()); - return CodeInvalid; + return InvalidCode; } CodeFriend @@ -829,25 +829,25 @@ CodeFn def_function( StrC name if ( params && params->Type != Parameters ) { log_failure( "gen::def_function: params was not a `Parameters` type: %s", params.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( ret_type && ret_type->Type != Typename ) { log_failure( "gen::def_function: ret_type was not a Typename: %s", ret_type.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( specifiers && specifiers->Type != Specifiers ) { log_failure( "gen::def_function: specifiers was not a `Specifiers` type: %s", specifiers.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( attributes && attributes->Type != PlatformAttributes ) { log_failure( "gen::def_function: attributes was not a `PlatformAttributes` type: %s", attributes.debug_str() ); - return CodeInvalid; + return InvalidCode; } CodeFn @@ -867,7 +867,7 @@ CodeFn def_function( StrC name default: { log_failure("gen::def_function: body must be either of Function_Body, Execution, or Untyped type. %s", body->debug_str()); - return CodeInvalid; + return InvalidCode; } } @@ -905,7 +905,7 @@ CodeInclude def_include( StrC path, bool foreign ) if ( path.Len <= 0 || path.Ptr == nullptr ) { log_failure( "gen::def_include: Invalid path provided - %d" ); - return CodeInvalid; + return InvalidCode; } StrC content = foreign ? @@ -945,7 +945,7 @@ CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags ) if ( body->Type != Namespace_Body && body->Type != Untyped ) { log_failure("gen::def_namespace: body is not of namespace or untyped type %s", body.debug_str()); - return CodeInvalid; + return InvalidCode; } CodeNS @@ -968,20 +968,20 @@ CodeOperator def_operator( OperatorT op, StrC nspace if ( attributes && attributes->Type != PlatformAttributes ) { log_failure( "gen::def_operator: PlatformAttributes was provided but its not of attributes type: %s", attributes.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( specifiers && specifiers->Type != Specifiers ) { log_failure( "gen::def_operator: Specifiers was provided but its not of specifiers type: %s", specifiers.debug_str() ); - return CodeInvalid; + return InvalidCode; } OpValidateResult check_result = operator__validate( op, params_code, ret_type, specifiers ); if ( check_result == OpValidateResult::Fail ) { - return CodeInvalid; + return InvalidCode; } char const* name = nullptr; @@ -1009,7 +1009,7 @@ CodeOperator def_operator( OperatorT op, StrC nspace default: { log_failure("gen::def_operator: body must be either of Function_Body, Execution, or Untyped type. %s", body->debug_str()); - return CodeInvalid; + return InvalidCode; } } @@ -1046,7 +1046,7 @@ CodeOpCast def_operator_cast( CodeType type, Code body, CodeSpecifiers const_spe if ( type->Type != Typename ) { log_failure( "gen::def_operator_cast: type is not a typename - %s", type.debug_str() ); - return CodeInvalid; + return InvalidCode; } CodeOpCast result = (CodeOpCast) make_code(); @@ -1058,7 +1058,7 @@ CodeOpCast def_operator_cast( CodeType type, Code body, CodeSpecifiers const_spe if ( body->Type != Function_Body && body->Type != Execution ) { log_failure( "gen::def_operator_cast: body is not of function body or execution type - %s", body.debug_str() ); - return CodeInvalid; + return InvalidCode; } result->Body = body; @@ -1087,13 +1087,13 @@ CodeParam def_param( CodeType type, StrC name, Code value ) if ( type->Type != Typename ) { log_failure( "gen::def_param: type is not a typename - %s", type.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( value && value->Type != Untyped ) { log_failure( "gen::def_param: value is not untyped - %s", value.debug_str() ); - return CodeInvalid; + return InvalidCode; } CodeParam @@ -1118,7 +1118,7 @@ CodePragma def_pragma( StrC directive ) if ( directive.Len <= 0 || directive.Ptr == nullptr ) { log_failure( "gen::def_comment: Invalid comment provided:" ); - return CodeInvalid; + return InvalidCode; } CodePragma @@ -1136,7 +1136,7 @@ CodePreprocessCond def_preprocess_cond( EPreprocessCond type, StrC expr ) if ( expr.Len <= 0 || expr.Ptr == nullptr ) { log_failure( "gen::def_comment: Invalid comment provided:" ); - return CodeInvalid; + return InvalidCode; } CodePreprocessCond @@ -1184,19 +1184,19 @@ CodeStruct def_struct( StrC name if ( attributes && attributes->Type != PlatformAttributes ) { log_failure( "gen::def_struct: attributes was not a `PlatformAttributes` type - %s", attributes.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( parent && parent->Type != Typename ) { log_failure( "gen::def_struct: parent was not a `Struct` type - %s", parent.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( body && body->Type != Struct_Body ) { log_failure( "gen::def_struct: body was not a Struct_Body type - %s", body.debug_str() ); - return CodeInvalid; + return InvalidCode; } CodeStruct @@ -1243,7 +1243,7 @@ CodeTemplate def_template( CodeParam params, Code declaration, ModuleFlag mflags if ( params && params->Type != ECode::Parameters ) { log_failure( "gen::def_template: params is not of parameters type - %s", params.debug_str() ); - return CodeInvalid; + return InvalidCode; } switch (declaration->Type ) @@ -1276,19 +1276,19 @@ CodeType def_type( StrC name, Code arrayexpr, CodeSpecifiers specifiers, CodeAtt if ( attributes && attributes->Type != ECode::PlatformAttributes ) { log_failure( "gen::def_type: attributes is not of attributes type - %s", attributes.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( specifiers && specifiers->Type != ECode::Specifiers ) { log_failure( "gen::def_type: specifiers is not of specifiers type - %s", specifiers.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( arrayexpr && arrayexpr->Type != ECode::Untyped ) { log_failure( "gen::def_type: arrayexpr is not of untyped type - %s", arrayexpr->debug_str() ); - return CodeInvalid; + return InvalidCode; } CodeType @@ -1330,13 +1330,13 @@ CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes, Module break; default: log_failure( "gen::def_typedef: type was not a Class, Enum, Function Forward, Struct, Typename, or Union - %s", type.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( attributes && attributes->Type != ECode::PlatformAttributes ) { log_failure( "gen::def_typedef: attributes was not a PlatformAttributes - %s", attributes.debug_str() ); - return CodeInvalid; + return InvalidCode; } // Registering the type. @@ -1345,7 +1345,7 @@ CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes, Module if ( ! registered_type ) { log_failure( "gen::def_typedef: failed to register type" ); - return CodeInvalid; + return InvalidCode; } CodeTypedef @@ -1360,7 +1360,7 @@ CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes, Module if (type->Type != Untyped) { log_failure( "gen::def_typedef: name was empty and type was not untyped (indicating its a function typedef) - %s", type.debug_str() ); - return CodeInvalid; + return InvalidCode; } result->Name = get_cached_string( type->Name ); @@ -1382,13 +1382,13 @@ CodeUnion def_union( StrC name, Code body, CodeAttributes attributes, ModuleFlag if ( body->Type != ECode::Union_Body ) { log_failure( "gen::def_union: body was not a Union_Body type - %s", body.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( attributes && attributes->Type != ECode::PlatformAttributes ) { log_failure( "gen::def_union: attributes was not a PlatformAttributes type - %s", attributes.debug_str() ); - return CodeInvalid; + return InvalidCode; } CodeUnion @@ -1419,13 +1419,13 @@ CodeUsing def_using( StrC name, CodeType type if ( ! register_type ) { log_failure( "gen::def_using: failed to register type" ); - return CodeInvalid; + return InvalidCode; } if ( attributes && attributes->Type != ECode::PlatformAttributes ) { log_failure( "gen::def_using: attributes was not a PlatformAttributes type - %s", attributes.debug_str() ); - return CodeInvalid; + return InvalidCode; } CodeUsing @@ -1465,25 +1465,25 @@ CodeVar def_variable( CodeType type, StrC name, Code value if ( attributes && attributes->Type != ECode::PlatformAttributes ) { log_failure( "gen::def_variable: attributes was not a `PlatformAttributes` type - %s", attributes.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( specifiers && specifiers->Type != ECode::Specifiers ) { log_failure( "gen::def_variable: specifiers was not a `Specifiers` type - %s", specifiers.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( type->Type != ECode::Typename ) { log_failure( "gen::def_variable: type was not a Typename - %s", type.debug_str() ); - return CodeInvalid; + return InvalidCode; } if ( value && value->Type != ECode::Untyped ) { log_failure( "gen::def_variable: value was not a `Untyped` type - %s", value.debug_str() ); - return CodeInvalid; + return InvalidCode; } CodeVar @@ -1513,7 +1513,7 @@ using namespace ECode; \ if ( num <= 0 ) \ { \ log_failure("gen::" stringize(Name_) ": num cannot be zero or negative"); \ - return CodeInvalid; \ + return InvalidCode; \ } #define def_body_code_array_start( Name_ ) \ @@ -1522,13 +1522,13 @@ using namespace ECode; \ if ( num <= 0 ) \ { \ log_failure("gen::" stringize(Name_) ": num cannot be zero or negative"); \ - return CodeInvalid; \ + return InvalidCode; \ } \ \ if ( codes == nullptr ) \ { \ log_failure("gen::" stringize(Name_)" : Provided a null array of codes"); \ - return CodeInvalid; \ + return InvalidCode; \ } #pragma endregion Helper Macros for def_**_body functions @@ -1552,14 +1552,14 @@ CodeBody def_class_body( s32 num, ... ) log_failure("gen::" "def_class_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) { GEN_AST_BODY_CLASS_UNALLOWED_TYPES log_failure("gen::" "def_class_body" ": Entry type is not allowed: %s", entry.debug_str()); - return CodeInvalid; + return InvalidCode; default: break; @@ -1589,14 +1589,14 @@ CodeBody def_class_body( s32 num, Code* codes ) if (!entry) { log_failure("gen::" "def_class_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) { GEN_AST_BODY_CLASS_UNALLOWED_TYPES log_failure("gen::" "def_class_body" ": Entry type is not allowed: %s", entry.debug_str()); - return CodeInvalid; + return InvalidCode; default: break; @@ -1627,13 +1627,13 @@ CodeBody def_enum_body( s32 num, ... ) if ( ! entry ) { log_failure("gen::def_enum_body: Provided a null entry"); - return CodeInvalid; + return InvalidCode; } if ( entry->Type != Untyped && entry->Type != Comment ) { log_failure("gen::def_enum_body: Entry type is not allowed - %s. Must be of untyped or comment type.", entry.debug_str() ); - return CodeInvalid; + return InvalidCode; } result.append( entry ); @@ -1659,13 +1659,13 @@ CodeBody def_enum_body( s32 num, Code* codes ) if ( ! entry ) { log_failure("gen::def_enum_body: Provided a null entry"); - return CodeInvalid; + return InvalidCode; } if ( entry->Type != Untyped && entry->Type != Comment ) { log_failure("gen::def_enum_body: Entry type is not allowed: %s", entry.debug_str() ); - return CodeInvalid; + return InvalidCode; } result.append( entry ); @@ -1693,14 +1693,14 @@ CodeBody def_export_body( s32 num, ... ) if (!entry) { log_failure("gen::" "def_export_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) { GEN_AST_BODY_EXPORT_UNALLOWED_TYPES log_failure("gen::" "def_export_body" ": Entry type is not allowed: %s", entry.debug_str()); - return CodeInvalid; + return InvalidCode; default: break; @@ -1730,14 +1730,14 @@ CodeBody def_export_body( s32 num, Code* codes ) if (!entry) { log_failure("gen::" "def_export_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) { GEN_AST_BODY_EXPORT_UNALLOWED_TYPES log_failure("gen::" "def_export_body" ": Entry type is not allowed: %s", entry.debug_str()); - return CodeInvalid; + return InvalidCode; default: break; @@ -1768,14 +1768,14 @@ CodeBody def_extern_link_body( s32 num, ... ) if (!entry) { log_failure("gen::" "def_extern_linkage_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) { GEN_AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES log_failure("gen::" "def_extern_linkage_body" ": Entry type is not allowed: %s", entry.debug_str()); - return CodeInvalid; + return InvalidCode; default: break; @@ -1805,14 +1805,14 @@ CodeBody def_extern_link_body( s32 num, Code* codes ) if (!entry) { log_failure("gen::" "def_extern_linkage_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) { GEN_AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES log_failure("gen::" "def_extern_linkage_body" ": Entry type is not allowed: %s", entry.debug_str()); - return CodeInvalid; + return InvalidCode; default: break; @@ -1844,7 +1844,7 @@ CodeBody def_function_body( s32 num, ... ) if (!entry) { log_failure("gen::" stringize(def_function_body) ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) @@ -1852,7 +1852,7 @@ CodeBody def_function_body( s32 num, ... ) GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES log_failure("gen::" stringize(def_function_body) ": Entry type is not allowed: %s", entry.debug_str()); - return CodeInvalid; + return InvalidCode; default: break; @@ -1882,14 +1882,14 @@ CodeBody def_function_body( s32 num, Code* codes ) if (!entry) { log_failure("gen::" "def_function_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) { GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES log_failure("gen::" "def_function_body" ": Entry type is not allowed: %s", entry.debug_str()); - return CodeInvalid; + return InvalidCode; default: break; @@ -1919,7 +1919,7 @@ CodeBody def_global_body( s32 num, ... ) if (!entry) { log_failure("gen::" "def_global_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) @@ -1930,7 +1930,7 @@ CodeBody def_global_body( s32 num, ... ) GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES log_failure("gen::" "def_global_body" ": Entry type is not allowed: %s", entry.debug_str()); - return (*Code::Invalid.ast); + return InvalidCode; default: break; @@ -1960,7 +1960,7 @@ CodeBody def_global_body( s32 num, Code* codes ) if (!entry) { log_failure("gen::" "def_global_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) @@ -1971,7 +1971,7 @@ CodeBody def_global_body( s32 num, Code* codes ) GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES log_failure("gen::" "def_global_body" ": Entry type is not allowed: %s", entry.debug_str()); - return CodeInvalid; + return InvalidCode; default: break; @@ -2002,14 +2002,14 @@ CodeBody def_namespace_body( s32 num, ... ) if (!entry) { log_failure("gen::" "def_namespace_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) { GEN_AST_BODY_NAMESPACE_UNALLOWED_TYPES log_failure("gen::" "def_namespace_body" ": Entry type is not allowed: %s", entry.debug_str()); - return CodeInvalid; + return InvalidCode; default: break; @@ -2039,14 +2039,14 @@ CodeBody def_namespace_body( s32 num, Code* codes ) if (!entry) { log_failure("gen::" "def_namespace_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) { GEN_AST_BODY_NAMESPACE_UNALLOWED_TYPES log_failure("gen::" "def_namespace_body" ": Entry type is not allowed: %s", entry.debug_str() ); - return CodeInvalid; + return InvalidCode; default: break; } @@ -2073,7 +2073,7 @@ CodeParam def_params( s32 num, ... ) if ( param->Type != Parameters ) { log_failure( "gen::def_params: param %d is not a Parameters", num - num + 1 ); - return CodeInvalid; + return InvalidCode; } CodeParam result = (CodeParam) param.duplicate(); @@ -2086,7 +2086,7 @@ CodeParam def_params( s32 num, ... ) if ( param->Type != Parameters ) { log_failure( "gen::def_params: param %d is not a Parameters", num - num + 1 ); - return CodeInvalid; + return InvalidCode; } result.append( param ); @@ -2104,13 +2104,13 @@ CodeParam def_params( s32 num, CodeParam* codes ) if ( current.ast == nullptr ) \ { \ log_failure("gen::def_params: Provide a null code in codes array"); \ - return CodeInvalid; \ + return InvalidCode; \ } \ \ if (current->Type != Parameters ) \ { \ log_failure("gen::def_params: Code in coes array is not of paramter type - %s", current.debug_str() ); \ - return CodeInvalid; \ + return InvalidCode; \ } CodeParam current = (CodeParam) codes->duplicate(); @@ -2137,13 +2137,13 @@ CodeSpecifiers def_specifiers( s32 num, ... ) if ( num <= 0 ) { log_failure("gen::def_specifiers: num cannot be zero or less"); - return CodeInvalid; + return InvalidCode; } if ( num > AST::ArrSpecs_Cap ) { log_failure("gen::def_specifiers: num of speciifers to define AST larger than AST specicifier capacity - %d", num); - return CodeInvalid; + return InvalidCode; } CodeSpecifiers @@ -2169,13 +2169,13 @@ CodeSpecifiers def_specifiers( s32 num, SpecifierT* specs ) if ( num <= 0 ) { log_failure("gen::def_specifiers: num cannot be zero or less"); - return CodeInvalid; + return InvalidCode; } if ( num > AST::ArrSpecs_Cap ) { log_failure("gen::def_specifiers: num of speciifers to define AST larger than AST specicifier capacity - %d", num); - return CodeInvalid; + return InvalidCode; } CodeSpecifiers @@ -2211,14 +2211,14 @@ CodeBody def_struct_body( s32 num, ... ) if (!entry) { log_failure("gen::" "def_struct_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) { GEN_AST_BODY_STRUCT_UNALLOWED_TYPES log_failure("gen::" "def_struct_body" ": Entry type is not allowed: %s", entry.debug_str()); - return CodeInvalid; + return InvalidCode; default: break; @@ -2248,14 +2248,14 @@ CodeBody def_struct_body( s32 num, Code* codes ) if (!entry) { log_failure("gen::" "def_struct_body" ": Provided an null entry"); - return CodeInvalid; + return InvalidCode; } switch (entry->Type) { GEN_AST_BODY_STRUCT_UNALLOWED_TYPES log_failure("gen::" "def_struct_body" ": Entry type is not allowed: %s", entry.debug_str() ); - return CodeInvalid; + return InvalidCode; default: break; @@ -2286,13 +2286,13 @@ CodeBody def_union_body( s32 num, ... ) if ( ! entry ) { log_failure("gen::def_union_body: Provided a null entry"); - return CodeInvalid; + return InvalidCode; } if ( entry->Type != Untyped && entry->Type != Comment ) { log_failure("gen::def_union_body: Entry type is not allowed - %s. Must be of untyped or comment type.", entry.debug_str() ); - return CodeInvalid; + return InvalidCode; } result.append( entry ); @@ -2318,13 +2318,13 @@ CodeBody def_union_body( s32 num, CodeUnion* codes ) if ( ! entry ) { log_failure("gen::def_union_body: Provided a null entry"); - return CodeInvalid; + return InvalidCode; } if ( entry->Type != Untyped && entry->Type != Comment ) { log_failure("gen::def_union_body: Entry type is not allowed: %s", entry.debug_str() ); - return CodeInvalid; + return InvalidCode; } result.append( entry ); diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 7055811..196a3d1 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -153,13 +153,13 @@ if ( def.Len <= 0 ) \ { \ log_failure( "gen::" stringize(__func__) ": length must greater than 0" ); \ parser::Context.pop(); \ - return CodeInvalid; \ + return InvalidCode; \ } \ if ( def.Ptr == nullptr ) \ { \ log_failure( "gen::" stringize(__func__) ": def was null" ); \ parser::Context.pop(); \ - return CodeInvalid; \ + return InvalidCode; \ } # define currtok_noskip Context.Tokens.current( dont_skip_formatting ) @@ -505,14 +505,14 @@ Code parse_array_decl() { log_failure( "Error, unexpected end of array declaration ( '[]' scope started )\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } if ( currtok.Type == TokType::BraceSquare_Close ) { log_failure( "Error, empty array expression in definition\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } Token untyped_tok = currtok; @@ -531,14 +531,14 @@ Code parse_array_decl() { log_failure( "Error, unexpected end of array declaration, expected ]\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } if ( currtok.Type != TokType::BraceSquare_Close ) { log_failure( "%s: Error, expected ] in array declaration, not %s\n%s", ETokType::to_str( currtok.Type ), Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } eat( TokType::BraceSquare_Close ); @@ -678,7 +678,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) if ( which != TokType::Decl_Class && which != TokType::Decl_Struct ) { log_failure( "Error, expected class or struct, not %s\n%s", ETokType::to_str( which ), Context.to_string() ); - return CodeInvalid; + return InvalidCode; } Token name { nullptr, 0, TokType::Invalid }; @@ -689,7 +689,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) CodeAttributes attributes = { nullptr }; ModuleFlag mflags = ModuleFlag_None; - CodeClass result = CodeInvalid; + CodeClass result = InvalidCode; if ( check(TokType::Module_Export) ) { @@ -802,7 +802,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) while ( left && currtok_noskip.Type != TokType::BraceCurly_Close ) { - Code member = Code::Invalid; + Code member = Code_Invalid; CodeAttributes attributes = { nullptr }; CodeSpecifiers specifiers = { nullptr }; @@ -901,7 +901,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) if ( currtok.Text[0] != '~' ) { log_failure( "Operator token found in global body but not destructor unary negation\n%s", Context.to_string() ); - return CodeInvalid; + return InvalidCode; } member = parse_destructor(); @@ -1015,7 +1015,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) default: log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str(spec), Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } // Every specifier after would be considered part of the type type signature @@ -1108,11 +1108,11 @@ CodeBody parse_class_struct_body( TokType which, Token name ) break; } - if ( member == Code::Invalid ) + if ( member == Code_Invalid ) { log_failure( "Failed to parse member\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } result.append( member ); @@ -1200,7 +1200,7 @@ Code parse_complicated_definition( TokType which ) log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } if ( tok.Type == TokType::Identifier ) { @@ -1245,7 +1245,7 @@ Code parse_complicated_definition( TokType which ) { log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } Code result = parse_operator_function_or_variable( false, { nullptr }, { nullptr } ); @@ -1264,7 +1264,7 @@ Code parse_complicated_definition( TokType which ) { log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } // Its a forward declaration of an enum class @@ -1294,7 +1294,7 @@ Code parse_complicated_definition( TokType which ) { log_failure( "Unsupported or bad member definition after %s declaration\n%S", to_str(which).Ptr, Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } } @@ -1313,7 +1313,7 @@ CodeDefine parse_define() { log_failure( "Error, expected identifier after #define\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } Context.Scope->Name = currtok; @@ -1325,7 +1325,7 @@ CodeDefine parse_define() { log_failure( "Error, expected content after #define %s\n%s", define->Name, Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } if ( currtok.Length == 0 ) @@ -1360,7 +1360,7 @@ Code parse_assignment_expression() { log_failure( "Expected expression after assignment operator\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } s32 level = 0; @@ -1387,7 +1387,7 @@ Code parse_assignment_expression() internal inline Code parse_forward_or_definition( TokType which, bool is_inplace ) { - Code result = CodeInvalid; + Code result = InvalidCode; switch ( which ) { @@ -1412,7 +1412,7 @@ Code parse_forward_or_definition( TokType which, bool is_inplace ) "(only supports class, enum, struct, union) \n%s" , Context.to_string() ); - return CodeInvalid; + return InvalidCode; } } @@ -1450,10 +1450,10 @@ CodeFn parse_function_after_name( if ( check( TokType::BraceCurly_Open ) ) { body = parse_function_body(); - if ( body == Code::Invalid ) + if ( body == Code_Invalid ) { Context.pop(); - return CodeInvalid; + return InvalidCode; } // ( ) { } } @@ -1505,7 +1505,7 @@ CodeFn parse_function_after_name( { log_failure("Body must be either of Function_Body or Untyped type, %s\n%s", body.debug_str(), Context.to_string()); Context.pop(); - return CodeInvalid; + return InvalidCode; } } @@ -1585,7 +1585,7 @@ CodeBody parse_global_nspace( CodeT which ) push_scope(); if ( which != Namespace_Body && which != Global_Body && which != Export_Body && which != Extern_Linkage_Body ) - return CodeInvalid; + return InvalidCode; if ( which != Global_Body ) eat( TokType::BraceCurly_Open ); @@ -1597,7 +1597,7 @@ CodeBody parse_global_nspace( CodeT which ) while ( left && currtok_noskip.Type != TokType::BraceCurly_Close ) { - Code member = Code::Invalid; + Code member = Code_Invalid; CodeAttributes attributes = { nullptr }; CodeSpecifiers specifiers = { nullptr }; @@ -1798,7 +1798,7 @@ CodeBody parse_global_nspace( CodeT which ) log_failure( "Invalid specifier %.*s for variable\n%s", spec_str.Len, spec_str, Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } if (ignore_spec) @@ -1870,11 +1870,11 @@ CodeBody parse_global_nspace( CodeT which ) } } - if ( member == Code::Invalid ) + if ( member == Code_Invalid ) { log_failure( "Failed to parse member\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } // log_fmt("Global Body Member: %s", member->debug_str()); @@ -2108,7 +2108,7 @@ CodeInclude parse_include() { log_failure( "Error, expected include string after #include\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } Context.Scope->Name = currtok; @@ -2157,7 +2157,7 @@ CodeOperator parse_operator_after_ret_type( { log_failure( "Expected operator after 'operator' keyword\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } Context.Scope->Name = currtok; @@ -2410,7 +2410,7 @@ CodeOperator parse_operator_after_ret_type( { log_failure( "Invalid operator '%s'\n%s", prevtok.Text, Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } } } @@ -2420,7 +2420,7 @@ CodeOperator parse_operator_after_ret_type( { log_failure( "Invalid operator '%s'\n%s", currtok.Text, Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } if ( ! was_new_or_delete) @@ -2454,10 +2454,10 @@ CodeOperator parse_operator_after_ret_type( if ( check( TokType::BraceCurly_Open ) ) { body = parse_function_body(); - if ( body == Code::Invalid ) + if ( body == Code_Invalid ) { Context.pop(); - return CodeInvalid; + return InvalidCode; } // operator ( ) { ... } } @@ -2487,7 +2487,7 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes { push_scope(); - Code result = CodeInvalid; + Code result = InvalidCode; #ifndef GEN_PARSER_DISABLE_MACRO_FUNCTION_SIGNATURES if ( currtok.Type == TokType::Preprocess_Macro ) @@ -2503,10 +2503,10 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes CodeType type = parse_type(); // - if ( type == CodeInvalid ) + if ( type == InvalidCode ) { Context.pop(); - return CodeInvalid; + return InvalidCode; } bool found_operator = false; @@ -2562,7 +2562,7 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes { log_failure( "Expected function declaration (consteval was used)\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } // Dealing with a variable @@ -2590,7 +2590,7 @@ CodePragma parse_pragma() { log_failure( "Error, expected content after #pragma\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } Context.Scope->Name = currtok; @@ -2667,10 +2667,10 @@ CodeParam parse_params( bool use_template_capture ) if ( currtok.Type != TokType::Comma ) { type = parse_type( use_template_capture ); - if ( type == Code::Invalid ) + if ( type == Code_Invalid ) { Context.pop(); - return CodeInvalid; + return InvalidCode; } // ( @@ -2704,7 +2704,7 @@ CodeParam parse_params( bool use_template_capture ) { log_failure( "Expected value after assignment operator\n%s.", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } s32 capture_level = 0; @@ -2777,10 +2777,10 @@ CodeParam parse_params( bool use_template_capture ) if ( currtok.Type != TokType::Comma ) { type = parse_type( use_template_capture ); - if ( type == Code::Invalid ) + if ( type == Code_Invalid ) { Context.pop(); - return CodeInvalid; + return InvalidCode; } // ( = , @@ -2816,7 +2816,7 @@ CodeParam parse_params( bool use_template_capture ) { log_failure( "Expected value after assignment operator\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } s32 capture_level = 0; @@ -2877,7 +2877,7 @@ CodeParam parse_params( bool use_template_capture ) { log_failure( "Expected '<' after 'template' keyword\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } eat( TokType::Operator ); // < = , = , .. > @@ -2897,7 +2897,7 @@ CodePreprocessCond parse_preprocess_cond() { log_failure( "Error, expected preprocess conditional\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } CodePreprocessCond @@ -2910,7 +2910,7 @@ CodePreprocessCond parse_preprocess_cond() { log_failure( "Error, expected content after #define\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } Context.Scope->Name = currtok; @@ -3168,7 +3168,7 @@ CodeVar parse_variable_after_name( { log_failure( "Expected expression after bitfield \n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } while ( left && currtok.Type != TokType::Statement_End ) @@ -3466,7 +3466,7 @@ CodeDestructor parse_destructor( CodeSpecifiers specifiers ) else { log_failure( "Expected destructor '~' token\n%s", Context.to_string() ); - return CodeInvalid; + return InvalidCode; } // ~ @@ -3503,7 +3503,7 @@ CodeDestructor parse_destructor( CodeSpecifiers specifiers ) else { log_failure( "Pure or default specifier expected due to '=' token\n%s", Context.to_string() ); - return CodeInvalid; + return InvalidCode; } pure_virtual = true; @@ -3598,11 +3598,11 @@ CodeEnum parse_enum( bool inplace_def ) // enum : type = parse_type(); - if ( type == Code::Invalid ) + if ( type == Code_Invalid ) { log_failure( "Failed to parse enum classifier\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } // enum : } @@ -3628,7 +3628,7 @@ CodeEnum parse_enum( bool inplace_def ) eat( TokType::BraceCurly_Open ); // enum : { - Code member = CodeInvalid; + Code member = InvalidCode; bool expects_entry = true; while ( left && currtok_noskip.Type != TokType::BraceCurly_Close ) @@ -3736,11 +3736,11 @@ CodeEnum parse_enum( bool inplace_def ) break; } - if ( member == Code::Invalid ) + if ( member == Code_Invalid ) { log_failure( "Failed to parse member\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } body.append( member ); @@ -3839,7 +3839,7 @@ CodeExtern parse_extern_link() result->Name = get_cached_string( name ); Code entry = parse_extern_link_body(); - if ( entry == Code::Invalid ) + if ( entry == Code_Invalid ) { log_failure( "Failed to parse body\n%s", Context.to_string() ); Context.pop(); @@ -3866,10 +3866,10 @@ CodeFriend parse_friend() // Type declaration or return type CodeType type = parse_type(); - if ( type == Code::Invalid ) + if ( type == Code_Invalid ) { Context.pop(); - return CodeInvalid; + return InvalidCode; } // friend @@ -3966,7 +3966,7 @@ CodeFn parse_function() default: log_failure( "Invalid specifier %s for functon\n%s", ESpecifier::to_str(spec), Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } if ( spec == ESpecifier::Const ) @@ -3984,10 +3984,10 @@ CodeFn parse_function() // CodeType ret_type = parse_type(); - if ( ret_type == Code::Invalid ) + if ( ret_type == Code_Invalid ) { Context.pop(); - return CodeInvalid; + return InvalidCode; } // @@ -3996,7 +3996,7 @@ CodeFn parse_function() if ( ! name ) { Context.pop(); - return CodeInvalid; + return InvalidCode; } // @@ -4020,10 +4020,10 @@ CodeNS parse_namespace() // namespace CodeBody body = parse_global_nspace( ECode::Namespace_Body ); - if ( body == Code::Invalid ) + if ( body == Code_Invalid ) { Context.pop(); - return CodeInvalid; + return InvalidCode; } // namespace { } @@ -4077,7 +4077,7 @@ CodeOperator parse_operator() default: log_failure( "Invalid specifier " "%s" " for operator\n%s", ESpecifier::to_str(spec), Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } if ( spec == ESpecifier::Const ) @@ -4246,10 +4246,10 @@ CodeTemplate parse_template() // template Code params = parse_params( UseTemplateCapture ); - if ( params == Code::Invalid ) + if ( params == Code_Invalid ) { Context.pop(); - return CodeInvalid; + return InvalidCode; } // template< > @@ -4328,7 +4328,7 @@ CodeTemplate parse_template() default : log_failure( "Invalid specifier %s for variable or function\n%s", ESpecifier::to_str( spec ), Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } // Ignore const it will be handled by the type @@ -4451,7 +4451,7 @@ CodeType parse_type( bool from_template, bool* typedef_is_function ) { log_failure( "Error, invalid specifier used in type definition: %s\n%s", currtok.Text, Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } specs_found[ NumSpecifiers ] = spec; @@ -4464,7 +4464,7 @@ CodeType parse_type( bool from_template, bool* typedef_is_function ) { log_failure( "Error, unexpected end of type definition\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } if ( from_template && currtok.Type == TokType::Decl_Class ) @@ -4548,7 +4548,7 @@ else if ( currtok.Type == TokType::DeclType ) { log_failure( "Error, failed to type signature\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } } } @@ -4562,7 +4562,7 @@ else if ( currtok.Type == TokType::DeclType ) { log_failure( "Error, failed to type signature\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } // // @@ -4577,7 +4577,7 @@ else if ( currtok.Type == TokType::DeclType ) { log_failure( "Error, invalid specifier used in type definition: %s\n%s", currtok.Text, Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } specs_found[ NumSpecifiers ] = spec; @@ -4710,7 +4710,7 @@ else if ( currtok.Type == TokType::DeclType ) { log_failure( "Error, invalid specifier used in type definition: %s\n%s", currtok.Text, Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } specs_found[ NumSpecifiers ] = spec; @@ -4780,7 +4780,7 @@ else if ( currtok.Type == TokType::DeclType ) { log_failure( "Error, invalid specifier used in type definition: %s\n%s", currtok.Text, Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } specs_found[ NumSpecifiers ] = spec; @@ -4978,7 +4978,7 @@ CodeTypedef parse_typedef() { log_failure( "Unsupported or bad member definition after struct declaration\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } // TODO(Ed) : I'm not sure if I have to use parse_type here, I'd rather not as that would complicate parse_type. @@ -5004,7 +5004,7 @@ CodeTypedef parse_typedef() { log_failure( "Unsupported or bad member definition after struct declaration\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } } } @@ -5025,7 +5025,7 @@ CodeTypedef parse_typedef() { log_failure( "Error, expected identifier for typedef\n%s", Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } array_expr = parse_array_decl(); @@ -5360,7 +5360,7 @@ CodeVar parse_variable() default: log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str( spec ), Context.to_string() ); Context.pop(); - return CodeInvalid; + return InvalidCode; } // Ignore const specifiers, they're handled by the type @@ -5381,8 +5381,8 @@ CodeVar parse_variable() CodeType type = parse_type(); // - if ( type == Code::Invalid ) - return CodeInvalid; + if ( type == Code_Invalid ) + return InvalidCode; Context.Scope->Name = parse_identifier(); // diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 388c0e9..0cc6673 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -346,9 +346,11 @@ CodeBody gen_ast_inlines() #pragma push_macro("GEN_NS") #pragma push_macro("rcast") #pragma push_macro("log_failure") +#pragma push_macro("CodeInvalid") #undef GEN_NS #undef rcast #undef log_failure +#undef CodeInvalid char const* code_impl_tmpl = stringize( \n inline @@ -365,7 +367,7 @@ CodeBody gen_ast_inlines() if ( ast == nullptr ) { log_failure("Code::duplicate: Cannot duplicate code, AST is null!"); - return Code::Invalid; + return Code_Invalid; } return { rcast(AST*, ast)->duplicate() }; @@ -404,7 +406,7 @@ CodeBody gen_ast_inlines() return; } - rcast(AST*, ast)->Parent = Code::Global.ast; + rcast(AST*, ast)->Parent = Code_Global.ast; } inline & ::operator =( Code other ) @@ -419,16 +421,6 @@ CodeBody gen_ast_inlines() return *this; } inline - bool ::operator ==( Code other ) - { - return (AST*) ast == other.ast; - } - inline - bool ::operator !=( Code other ) - { - return (AST*) ast != other.ast; - } - inline ::operator bool() { return ast != nullptr; @@ -459,6 +451,7 @@ CodeBody gen_ast_inlines() \n ); #pragma pop_macro("GEN_NS") +#pragma pop_macro("CodeInvalid") CodeBody impl_code = parse_global_body( token_fmt( "typename", StrC name(Code), code_impl_tmpl )); CodeBody impl_code_body = parse_global_body( token_fmt( "typename", StrC name(CodeBody), code_impl_tmpl )); From 37c33ffb3e62be8c078e0e24f5b19379f45e17fe Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 00:10:24 -0500 Subject: [PATCH 028/112] reduction on debug_str --- project/components/ast.hpp | 2 +- project/components/gen/ast_inlines.hpp | 203 ------------------------- project/components/inlines.hpp | 13 ++ project/helpers/helper.hpp | 8 - 4 files changed, 14 insertions(+), 212 deletions(-) diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 0f327db..dfd6d16 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -168,7 +168,7 @@ struct Code { # define Using_Code( Typename ) \ - char const* debug_str(); \ + char const* debug_str() { return GEN_NS debug_str(*this); } \ Code duplicate(); \ bool is_equal( Code other ); \ bool is_body(); \ diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index 0df47a5..5e14fde 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -7,13 +7,6 @@ #pragma region generated code inline implementation -inline char const* Code::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code Code::duplicate() { if ( ast == nullptr ) @@ -73,13 +66,6 @@ inline Code::operator bool() return ast != nullptr; } -inline char const* CodeBody::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeBody::duplicate() { if ( ast == nullptr ) @@ -139,13 +125,6 @@ inline CodeBody::operator bool() return ast != nullptr; } -inline char const* CodeAttributes::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeAttributes::duplicate() { if ( ast == nullptr ) @@ -225,13 +204,6 @@ inline AST_Attributes* CodeAttributes::operator->() return ast; } -inline char const* CodeComment::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeComment::duplicate() { if ( ast == nullptr ) @@ -311,13 +283,6 @@ inline AST_Comment* CodeComment::operator->() return ast; } -inline char const* CodeConstructor::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeConstructor::duplicate() { if ( ast == nullptr ) @@ -397,13 +362,6 @@ inline AST_Constructor* CodeConstructor::operator->() return ast; } -inline char const* CodeClass::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeClass::duplicate() { if ( ast == nullptr ) @@ -463,13 +421,6 @@ inline CodeClass::operator bool() return ast != nullptr; } -inline char const* CodeDefine::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeDefine::duplicate() { if ( ast == nullptr ) @@ -549,13 +500,6 @@ inline AST_Define* CodeDefine::operator->() return ast; } -inline char const* CodeDestructor::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeDestructor::duplicate() { if ( ast == nullptr ) @@ -635,13 +579,6 @@ inline AST_Destructor* CodeDestructor::operator->() return ast; } -inline char const* CodeEnum::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeEnum::duplicate() { if ( ast == nullptr ) @@ -721,13 +658,6 @@ inline AST_Enum* CodeEnum::operator->() return ast; } -inline char const* CodeExec::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeExec::duplicate() { if ( ast == nullptr ) @@ -807,13 +737,6 @@ inline AST_Exec* CodeExec::operator->() return ast; } -inline char const* CodeExtern::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeExtern::duplicate() { if ( ast == nullptr ) @@ -893,13 +816,6 @@ inline AST_Extern* CodeExtern::operator->() return ast; } -inline char const* CodeFriend::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeFriend::duplicate() { if ( ast == nullptr ) @@ -979,13 +895,6 @@ inline AST_Friend* CodeFriend::operator->() return ast; } -inline char const* CodeFn::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeFn::duplicate() { if ( ast == nullptr ) @@ -1065,13 +974,6 @@ inline AST_Fn* CodeFn::operator->() return ast; } -inline char const* CodeInclude::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeInclude::duplicate() { if ( ast == nullptr ) @@ -1151,13 +1053,6 @@ inline AST_Include* CodeInclude::operator->() return ast; } -inline char const* CodeModule::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeModule::duplicate() { if ( ast == nullptr ) @@ -1237,13 +1132,6 @@ inline AST_Module* CodeModule::operator->() return ast; } -inline char const* CodeNS::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeNS::duplicate() { if ( ast == nullptr ) @@ -1323,13 +1211,6 @@ inline AST_NS* CodeNS::operator->() return ast; } -inline char const* CodeOperator::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeOperator::duplicate() { if ( ast == nullptr ) @@ -1409,13 +1290,6 @@ inline AST_Operator* CodeOperator::operator->() return ast; } -inline char const* CodeOpCast::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeOpCast::duplicate() { if ( ast == nullptr ) @@ -1495,13 +1369,6 @@ inline AST_OpCast* CodeOpCast::operator->() return ast; } -inline char const* CodeParam::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeParam::duplicate() { if ( ast == nullptr ) @@ -1561,13 +1428,6 @@ inline CodeParam::operator bool() return ast != nullptr; } -inline char const* CodePragma::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodePragma::duplicate() { if ( ast == nullptr ) @@ -1647,13 +1507,6 @@ inline AST_Pragma* CodePragma::operator->() return ast; } -inline char const* CodePreprocessCond::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodePreprocessCond::duplicate() { if ( ast == nullptr ) @@ -1733,13 +1586,6 @@ inline AST_PreprocessCond* CodePreprocessCond::operator->() return ast; } -inline char const* CodeSpecifiers::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeSpecifiers::duplicate() { if ( ast == nullptr ) @@ -1799,13 +1645,6 @@ inline CodeSpecifiers::operator bool() return ast != nullptr; } -inline char const* CodeStruct::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeStruct::duplicate() { if ( ast == nullptr ) @@ -1865,13 +1704,6 @@ inline CodeStruct::operator bool() return ast != nullptr; } -inline char const* CodeTemplate::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeTemplate::duplicate() { if ( ast == nullptr ) @@ -1951,13 +1783,6 @@ inline AST_Template* CodeTemplate::operator->() return ast; } -inline char const* CodeType::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeType::duplicate() { if ( ast == nullptr ) @@ -2037,13 +1862,6 @@ inline AST_Type* CodeType::operator->() return ast; } -inline char const* CodeTypedef::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeTypedef::duplicate() { if ( ast == nullptr ) @@ -2123,13 +1941,6 @@ inline AST_Typedef* CodeTypedef::operator->() return ast; } -inline char const* CodeUnion::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeUnion::duplicate() { if ( ast == nullptr ) @@ -2209,13 +2020,6 @@ inline AST_Union* CodeUnion::operator->() return ast; } -inline char const* CodeUsing::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeUsing::duplicate() { if ( ast == nullptr ) @@ -2295,13 +2099,6 @@ inline AST_Using* CodeUsing::operator->() return ast; } -inline char const* CodeVar::debug_str() -{ - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - return GEN_NS debug_str( rcast( AST*, ast ) ); -} - inline Code CodeVar::duplicate() { if ( ast == nullptr ) diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index 613a8f8..6cbe321 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -88,6 +88,17 @@ AST::operator Code() return { this }; } +#pragma region Code + +inline +char const* debug_str( Code code ) +{ + if ( code.ast == nullptr ) + return "Code::debug_str: AST is null!"; + + return debug_str( code.ast ); +} + inline Code& Code::operator ++() { @@ -97,6 +108,8 @@ Code& Code::operator ++() return *this; } +#pragma endregion Code + inline void CodeClass::add_interface( CodeType type ) { diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 0cc6673..fa227bc 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -354,14 +354,6 @@ CodeBody gen_ast_inlines() char const* code_impl_tmpl = stringize( \n inline - char const* ::debug_str() - { - if ( ast == nullptr ) - return "Code::debug_str: AST is null!"; - - return GEN_NS debug_str( rcast(AST*, ast) ); - } - inline Code ::duplicate() { if ( ast == nullptr ) From 007bfa0cb0a0a7dfd4a1b69010a9a939cd269c24 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 00:16:11 -0500 Subject: [PATCH 029/112] Code::duplicate reduction --- project/components/ast.hpp | 7 +- project/components/gen/ast_inlines.hpp | 290 ------------------------- project/components/inlines.hpp | 12 + project/helpers/helper.hpp | 11 - 4 files changed, 15 insertions(+), 305 deletions(-) diff --git a/project/components/ast.hpp b/project/components/ast.hpp index dfd6d16..89bed56 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -166,10 +166,11 @@ String to_string (Code code); */ struct Code { + AST* ast; # define Using_Code( Typename ) \ - char const* debug_str() { return GEN_NS debug_str(*this); } \ - Code duplicate(); \ + char const* debug_str() { return GEN_NS debug_str(* this); } \ + Code duplicate() { return GEN_NS duplicate(* this); } \ bool is_equal( Code other ); \ bool is_body(); \ bool is_valid(); \ @@ -207,8 +208,6 @@ struct Code return *this; } - AST* ast; - #ifdef GEN_ENFORCE_STRONG_CODE_TYPES # define operator explicit operator #endif diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index 5e14fde..7206e47 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -7,16 +7,6 @@ #pragma region generated code inline implementation -inline Code Code::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool Code::is_body() { if ( ast == nullptr ) @@ -66,16 +56,6 @@ inline Code::operator bool() return ast != nullptr; } -inline Code CodeBody::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeBody::is_body() { if ( ast == nullptr ) @@ -125,16 +105,6 @@ inline CodeBody::operator bool() return ast != nullptr; } -inline Code CodeAttributes::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeAttributes::is_body() { if ( ast == nullptr ) @@ -204,16 +174,6 @@ inline AST_Attributes* CodeAttributes::operator->() return ast; } -inline Code CodeComment::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeComment::is_body() { if ( ast == nullptr ) @@ -283,16 +243,6 @@ inline AST_Comment* CodeComment::operator->() return ast; } -inline Code CodeConstructor::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeConstructor::is_body() { if ( ast == nullptr ) @@ -362,16 +312,6 @@ inline AST_Constructor* CodeConstructor::operator->() return ast; } -inline Code CodeClass::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeClass::is_body() { if ( ast == nullptr ) @@ -421,16 +361,6 @@ inline CodeClass::operator bool() return ast != nullptr; } -inline Code CodeDefine::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeDefine::is_body() { if ( ast == nullptr ) @@ -500,16 +430,6 @@ inline AST_Define* CodeDefine::operator->() return ast; } -inline Code CodeDestructor::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeDestructor::is_body() { if ( ast == nullptr ) @@ -579,16 +499,6 @@ inline AST_Destructor* CodeDestructor::operator->() return ast; } -inline Code CodeEnum::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeEnum::is_body() { if ( ast == nullptr ) @@ -658,16 +568,6 @@ inline AST_Enum* CodeEnum::operator->() return ast; } -inline Code CodeExec::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeExec::is_body() { if ( ast == nullptr ) @@ -737,16 +637,6 @@ inline AST_Exec* CodeExec::operator->() return ast; } -inline Code CodeExtern::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeExtern::is_body() { if ( ast == nullptr ) @@ -816,16 +706,6 @@ inline AST_Extern* CodeExtern::operator->() return ast; } -inline Code CodeFriend::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeFriend::is_body() { if ( ast == nullptr ) @@ -895,16 +775,6 @@ inline AST_Friend* CodeFriend::operator->() return ast; } -inline Code CodeFn::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeFn::is_body() { if ( ast == nullptr ) @@ -974,16 +844,6 @@ inline AST_Fn* CodeFn::operator->() return ast; } -inline Code CodeInclude::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeInclude::is_body() { if ( ast == nullptr ) @@ -1053,16 +913,6 @@ inline AST_Include* CodeInclude::operator->() return ast; } -inline Code CodeModule::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeModule::is_body() { if ( ast == nullptr ) @@ -1132,16 +982,6 @@ inline AST_Module* CodeModule::operator->() return ast; } -inline Code CodeNS::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeNS::is_body() { if ( ast == nullptr ) @@ -1211,16 +1051,6 @@ inline AST_NS* CodeNS::operator->() return ast; } -inline Code CodeOperator::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeOperator::is_body() { if ( ast == nullptr ) @@ -1290,16 +1120,6 @@ inline AST_Operator* CodeOperator::operator->() return ast; } -inline Code CodeOpCast::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeOpCast::is_body() { if ( ast == nullptr ) @@ -1369,16 +1189,6 @@ inline AST_OpCast* CodeOpCast::operator->() return ast; } -inline Code CodeParam::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeParam::is_body() { if ( ast == nullptr ) @@ -1428,16 +1238,6 @@ inline CodeParam::operator bool() return ast != nullptr; } -inline Code CodePragma::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodePragma::is_body() { if ( ast == nullptr ) @@ -1507,16 +1307,6 @@ inline AST_Pragma* CodePragma::operator->() return ast; } -inline Code CodePreprocessCond::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodePreprocessCond::is_body() { if ( ast == nullptr ) @@ -1586,16 +1376,6 @@ inline AST_PreprocessCond* CodePreprocessCond::operator->() return ast; } -inline Code CodeSpecifiers::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeSpecifiers::is_body() { if ( ast == nullptr ) @@ -1645,16 +1425,6 @@ inline CodeSpecifiers::operator bool() return ast != nullptr; } -inline Code CodeStruct::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeStruct::is_body() { if ( ast == nullptr ) @@ -1704,16 +1474,6 @@ inline CodeStruct::operator bool() return ast != nullptr; } -inline Code CodeTemplate::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeTemplate::is_body() { if ( ast == nullptr ) @@ -1783,16 +1543,6 @@ inline AST_Template* CodeTemplate::operator->() return ast; } -inline Code CodeType::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeType::is_body() { if ( ast == nullptr ) @@ -1862,16 +1612,6 @@ inline AST_Type* CodeType::operator->() return ast; } -inline Code CodeTypedef::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeTypedef::is_body() { if ( ast == nullptr ) @@ -1941,16 +1681,6 @@ inline AST_Typedef* CodeTypedef::operator->() return ast; } -inline Code CodeUnion::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeUnion::is_body() { if ( ast == nullptr ) @@ -2020,16 +1750,6 @@ inline AST_Union* CodeUnion::operator->() return ast; } -inline Code CodeUsing::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeUsing::is_body() { if ( ast == nullptr ) @@ -2099,16 +1819,6 @@ inline AST_Using* CodeUsing::operator->() return ast; } -inline Code CodeVar::duplicate() -{ - if ( ast == nullptr ) - { - log_failure( "Code::duplicate: Cannot duplicate code, AST is null!" ); - return Code_Invalid; - } - return { rcast( AST*, ast )->duplicate() }; -} - inline bool CodeVar::is_body() { if ( ast == nullptr ) diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index 6cbe321..dad89ad 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -99,6 +99,18 @@ char const* debug_str( Code code ) return debug_str( code.ast ); } +inline +Code duplicate( Code code ) +{ + if ( code.ast == nullptr ) + { + log_failure("Code::duplicate: Cannot duplicate code, AST is null!"); + return Code_Invalid; + } + + return { duplicate(code.ast) }; +} + inline Code& Code::operator ++() { diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index fa227bc..ce5a01f 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -354,17 +354,6 @@ CodeBody gen_ast_inlines() char const* code_impl_tmpl = stringize( \n inline - Code ::duplicate() - { - if ( ast == nullptr ) - { - log_failure("Code::duplicate: Cannot duplicate code, AST is null!"); - return Code_Invalid; - } - - return { rcast(AST*, ast)->duplicate() }; - } - inline bool ::is_body() { if ( ast == nullptr ) From 5cd69e1742fd394d3502c7fed13aa72515ffa5e2 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 00:18:54 -0500 Subject: [PATCH 030/112] Code::is_body reduction --- project/components/ast.hpp | 2 +- project/components/gen/ast_inlines.hpp | 261 ------------------------- project/components/inlines.hpp | 10 + project/helpers/helper.hpp | 9 - 4 files changed, 11 insertions(+), 271 deletions(-) diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 89bed56..2b14275 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -172,7 +172,7 @@ struct Code char const* debug_str() { return GEN_NS debug_str(* this); } \ Code duplicate() { return GEN_NS duplicate(* this); } \ bool is_equal( Code other ); \ - bool is_body(); \ + bool is_body() { return GEN_NS is_body(* this); } \ bool is_valid(); \ void set_global(); \ String to_string(); \ diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index 7206e47..bd41bdf 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -7,15 +7,6 @@ #pragma region generated code inline implementation -inline bool Code::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool Code::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -56,15 +47,6 @@ inline Code::operator bool() return ast != nullptr; } -inline bool CodeBody::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeBody::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -105,15 +87,6 @@ inline CodeBody::operator bool() return ast != nullptr; } -inline bool CodeAttributes::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeAttributes::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -174,15 +147,6 @@ inline AST_Attributes* CodeAttributes::operator->() return ast; } -inline bool CodeComment::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeComment::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -243,15 +207,6 @@ inline AST_Comment* CodeComment::operator->() return ast; } -inline bool CodeConstructor::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeConstructor::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -312,15 +267,6 @@ inline AST_Constructor* CodeConstructor::operator->() return ast; } -inline bool CodeClass::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeClass::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -361,15 +307,6 @@ inline CodeClass::operator bool() return ast != nullptr; } -inline bool CodeDefine::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeDefine::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -430,15 +367,6 @@ inline AST_Define* CodeDefine::operator->() return ast; } -inline bool CodeDestructor::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeDestructor::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -499,15 +427,6 @@ inline AST_Destructor* CodeDestructor::operator->() return ast; } -inline bool CodeEnum::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeEnum::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -568,15 +487,6 @@ inline AST_Enum* CodeEnum::operator->() return ast; } -inline bool CodeExec::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeExec::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -637,15 +547,6 @@ inline AST_Exec* CodeExec::operator->() return ast; } -inline bool CodeExtern::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeExtern::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -706,15 +607,6 @@ inline AST_Extern* CodeExtern::operator->() return ast; } -inline bool CodeFriend::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeFriend::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -775,15 +667,6 @@ inline AST_Friend* CodeFriend::operator->() return ast; } -inline bool CodeFn::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeFn::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -844,15 +727,6 @@ inline AST_Fn* CodeFn::operator->() return ast; } -inline bool CodeInclude::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeInclude::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -913,15 +787,6 @@ inline AST_Include* CodeInclude::operator->() return ast; } -inline bool CodeModule::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeModule::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -982,15 +847,6 @@ inline AST_Module* CodeModule::operator->() return ast; } -inline bool CodeNS::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeNS::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1051,15 +907,6 @@ inline AST_NS* CodeNS::operator->() return ast; } -inline bool CodeOperator::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeOperator::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1120,15 +967,6 @@ inline AST_Operator* CodeOperator::operator->() return ast; } -inline bool CodeOpCast::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeOpCast::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1189,15 +1027,6 @@ inline AST_OpCast* CodeOpCast::operator->() return ast; } -inline bool CodeParam::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeParam::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1238,15 +1067,6 @@ inline CodeParam::operator bool() return ast != nullptr; } -inline bool CodePragma::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodePragma::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1307,15 +1127,6 @@ inline AST_Pragma* CodePragma::operator->() return ast; } -inline bool CodePreprocessCond::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodePreprocessCond::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1376,15 +1187,6 @@ inline AST_PreprocessCond* CodePreprocessCond::operator->() return ast; } -inline bool CodeSpecifiers::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeSpecifiers::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1425,15 +1227,6 @@ inline CodeSpecifiers::operator bool() return ast != nullptr; } -inline bool CodeStruct::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeStruct::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1474,15 +1267,6 @@ inline CodeStruct::operator bool() return ast != nullptr; } -inline bool CodeTemplate::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeTemplate::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1543,15 +1327,6 @@ inline AST_Template* CodeTemplate::operator->() return ast; } -inline bool CodeType::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeType::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1612,15 +1387,6 @@ inline AST_Type* CodeType::operator->() return ast; } -inline bool CodeTypedef::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeTypedef::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1681,15 +1447,6 @@ inline AST_Typedef* CodeTypedef::operator->() return ast; } -inline bool CodeUnion::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeUnion::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1750,15 +1507,6 @@ inline AST_Union* CodeUnion::operator->() return ast; } -inline bool CodeUsing::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeUsing::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) @@ -1819,15 +1567,6 @@ inline AST_Using* CodeUsing::operator->() return ast; } -inline bool CodeVar::is_body() -{ - if ( ast == nullptr ) - { - return rcast( AST*, ast )->is_body(); - } - return false; -} - inline bool CodeVar::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index dad89ad..0713c14 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -111,6 +111,16 @@ Code duplicate( Code code ) return { duplicate(code.ast) }; } +inline +bool is_body(Code code) +{ + if ( code.ast == nullptr ) + { + return is_body(code.ast); + } + return false; +} + inline Code& Code::operator ++() { diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index ce5a01f..819c31f 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -354,15 +354,6 @@ CodeBody gen_ast_inlines() char const* code_impl_tmpl = stringize( \n inline - bool ::is_body() - { - if ( ast == nullptr ) - { - return rcast(AST*, ast)->is_body(); - } - return false; - } - inline bool ::is_equal( Code other ) { if ( ast == nullptr || other.ast == nullptr ) From 2b24511f7d12e3d664828c853072d08824693c7f Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 00:34:40 -0500 Subject: [PATCH 031/112] Code::is_equal reduction --- project/components/ast.cpp | 52 ++--- project/components/ast.hpp | 6 +- project/components/gen/ast_inlines.hpp | 261 ------------------------- project/components/inlines.hpp | 12 ++ project/helpers/helper.hpp | 11 -- 5 files changed, 42 insertions(+), 300 deletions(-) diff --git a/project/components/ast.cpp b/project/components/ast.cpp index acc572f..3d20a7b 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -592,7 +592,7 @@ void AST::to_string( String& result ) } } -bool AST::is_equal( AST* other ) +bool is_equal( AST* self, AST* other ) { /* AST values are either some u32 value, a cached string, or a pointer to another AST. @@ -603,31 +603,31 @@ bool AST::is_equal( AST* other ) */ if ( other == nullptr ) { - log_fmt( "AST::is_equal: other is null\nAST: %S", debug_str() ); + log_fmt( "AST::is_equal: other is null\nAST: %S", debug_str(self) ); return false; } - if ( Type != other->Type ) + if ( self->Type != other->Type ) { log_fmt("AST::is_equal: Type check failure with other\nAST: %S\nOther: %S" - , debug_str() + , debug_str(self) , other->debug_str() ); return false; } - switch ( Type ) + switch ( self->Type ) { using namespace ECode; #define check_member_val( val ) \ - if ( val != other->val ) \ + if ( self->val != other->val ) \ { \ log_fmt("\nAST::is_equal: Member - " #val " failed\n" \ "AST : %S\n" \ "Other: %S\n" \ - , debug_str() \ + , debug_str(self) \ , other->debug_str() \ ); \ \ @@ -635,12 +635,12 @@ bool AST::is_equal( AST* other ) } #define check_member_str( str ) \ - if ( str != other->str ) \ + if ( self->str != other->str ) \ { \ log_fmt("\nAST::is_equal: Member string - "#str " failed\n" \ "AST : %S\n" \ "Other: %S\n" \ - , debug_str() \ + , debug_str(self) \ , other->debug_str() \ ); \ \ @@ -648,12 +648,12 @@ bool AST::is_equal( AST* other ) } #define check_member_content( content ) \ - if ( content != other->content ) \ + if ( self->content != other->content ) \ { \ log_fmt("\nAST::is_equal: Member content - "#content " failed\n" \ "AST : %S\n" \ "Other: %S\n" \ - , debug_str() \ + , debug_str(self) \ , other->debug_str() \ ); \ \ @@ -661,13 +661,13 @@ bool AST::is_equal( AST* other ) "so it must be verified by eye for now\n" \ "AST Content:\n%S\n" \ "Other Content:\n%S\n" \ - , visualize_whitespace(content) \ + , visualize_whitespace(self->content) \ , visualize_whitespace(other->content) \ ); \ } #define check_member_ast( ast ) \ - if ( ast ) \ + if ( self->ast ) \ { \ if ( other->ast == nullptr ) \ { \ @@ -675,24 +675,24 @@ bool AST::is_equal( AST* other ) "AST : %s\n" \ "Other: %s\n" \ "For ast member: %s\n" \ - , debug_str() \ + , debug_str(self) \ , other->debug_str() \ - , ast->debug_str() \ + , self->ast->debug_str() \ ); \ \ return false; \ } \ \ - if ( ! ast->is_equal( other->ast ) ) \ + if ( ! self->ast->is_equal( other->ast ) ) \ { \ log_fmt( "\nAST::is_equal: Failed for " #ast"\n" \ "AST : %S\n" \ "Other: %S\n" \ "For ast member: %S\n" \ "other's ast member: %S\n" \ - , debug_str() \ + , debug_str(self) \ , other->debug_str() \ - , ast->debug_str() \ + , self->ast->debug_str() \ , other->ast->debug_str() \ ); \ \ @@ -907,9 +907,9 @@ bool AST::is_equal( AST* other ) case Parameters: { - if ( NumEntries > 1 ) + if ( self->NumEntries > 1 ) { - AST* curr = this; + AST* curr = self; AST* curr_other = other; while ( curr != nullptr ) { @@ -934,7 +934,7 @@ bool AST::is_equal( AST* other ) "Other: %S\n" "For ast member: %S\n" "other's ast member: %S\n" - , debug_str() + , debug_str(self) , other->debug_str() , curr->debug_str() , curr_other->debug_str() @@ -949,7 +949,7 @@ bool AST::is_equal( AST* other ) "Other: %S\n" "For ast member: %S\n" "other's ast member: %S\n" - , debug_str() + , debug_str(self) , other->debug_str() , curr->debug_str() , curr_other->debug_str() @@ -964,7 +964,7 @@ bool AST::is_equal( AST* other ) "Other: %S\n" "For ast member: %S\n" "other's ast member: %S\n" - , debug_str() + , debug_str(self) , other->debug_str() , curr->debug_str() , curr_other->debug_str() @@ -1020,7 +1020,7 @@ bool AST::is_equal( AST* other ) { check_member_val( NumEntries ); check_member_str( Name ); - for ( s32 idx = 0; idx < NumEntries; ++idx ) + for ( s32 idx = 0; idx < self->NumEntries; ++idx ) { check_member_val( ArrSpecs[ idx ] ); } @@ -1103,7 +1103,7 @@ bool AST::is_equal( AST* other ) check_member_ast( Front ); check_member_ast( Back ); - AST* curr = Front; + AST* curr = self->Front; AST* curr_other = other->Front; while ( curr != nullptr ) { @@ -1126,7 +1126,7 @@ bool AST::is_equal( AST* other ) "Other: %S\n" "For ast member: %S\n" "other's ast member: %S\n" - , debug_str() + , debug_str(self) , other->debug_str() , curr->debug_str() , curr_other->debug_str() diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 2b14275..6cec65d 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -171,7 +171,7 @@ struct Code # define Using_Code( Typename ) \ char const* debug_str() { return GEN_NS debug_str(* this); } \ Code duplicate() { return GEN_NS duplicate(* this); } \ - bool is_equal( Code other ); \ + bool is_equal( Code other ) { return GEN_NS is_equal(* this, other); } \ bool is_body() { return GEN_NS is_body(* this); } \ bool is_valid(); \ void set_global(); \ @@ -267,12 +267,14 @@ AST* duplicate ( AST* self ); Code* entry ( AST* self, u32 idx ); bool has_entries( AST* self ); bool is_body ( AST* self ); +bool is_equal ( AST* self, AST* other ); String to_string ( AST* self ); char const* type_str ( AST* self ); #if GEN_CPP_SUPPORT_REFERENCES void append ( AST& self, AST& other ) { return append(& self, & other); } bool is_body ( AST& self ) { return is_body(& self); } +bool is_equal ( AST& self, AST& other ) { return is_equal(& self, & other); } char const* debug_str( AST& self ) { return debug_str( & self ); } String to_string( AST& self ) { return to_string( & self ); } char const* type_str ( AST& self ) { return type_str( & self ); } @@ -290,7 +292,7 @@ struct AST AST* duplicate () { return GEN_NS duplicate(this); } Code* entry ( u32 idx ) { return GEN_NS entry(this, idx); } bool has_entries(); - bool is_equal ( AST* other ); + bool is_equal ( AST* other ) { return GEN_NS is_equal(this, other); } bool is_body() { return GEN_NS is_body(this); } char const* type_str() { return GEN_NS type_str(this); } bool validate_body(); diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index bd41bdf..b336234 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -7,15 +7,6 @@ #pragma region generated code inline implementation -inline bool Code::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool Code::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -47,15 +38,6 @@ inline Code::operator bool() return ast != nullptr; } -inline bool CodeBody::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeBody::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -87,15 +69,6 @@ inline CodeBody::operator bool() return ast != nullptr; } -inline bool CodeAttributes::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeAttributes::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -147,15 +120,6 @@ inline AST_Attributes* CodeAttributes::operator->() return ast; } -inline bool CodeComment::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeComment::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -207,15 +171,6 @@ inline AST_Comment* CodeComment::operator->() return ast; } -inline bool CodeConstructor::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeConstructor::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -267,15 +222,6 @@ inline AST_Constructor* CodeConstructor::operator->() return ast; } -inline bool CodeClass::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeClass::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -307,15 +253,6 @@ inline CodeClass::operator bool() return ast != nullptr; } -inline bool CodeDefine::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeDefine::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -367,15 +304,6 @@ inline AST_Define* CodeDefine::operator->() return ast; } -inline bool CodeDestructor::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeDestructor::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -427,15 +355,6 @@ inline AST_Destructor* CodeDestructor::operator->() return ast; } -inline bool CodeEnum::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeEnum::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -487,15 +406,6 @@ inline AST_Enum* CodeEnum::operator->() return ast; } -inline bool CodeExec::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeExec::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -547,15 +457,6 @@ inline AST_Exec* CodeExec::operator->() return ast; } -inline bool CodeExtern::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeExtern::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -607,15 +508,6 @@ inline AST_Extern* CodeExtern::operator->() return ast; } -inline bool CodeFriend::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeFriend::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -667,15 +559,6 @@ inline AST_Friend* CodeFriend::operator->() return ast; } -inline bool CodeFn::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeFn::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -727,15 +610,6 @@ inline AST_Fn* CodeFn::operator->() return ast; } -inline bool CodeInclude::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeInclude::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -787,15 +661,6 @@ inline AST_Include* CodeInclude::operator->() return ast; } -inline bool CodeModule::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeModule::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -847,15 +712,6 @@ inline AST_Module* CodeModule::operator->() return ast; } -inline bool CodeNS::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeNS::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -907,15 +763,6 @@ inline AST_NS* CodeNS::operator->() return ast; } -inline bool CodeOperator::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeOperator::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -967,15 +814,6 @@ inline AST_Operator* CodeOperator::operator->() return ast; } -inline bool CodeOpCast::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeOpCast::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1027,15 +865,6 @@ inline AST_OpCast* CodeOpCast::operator->() return ast; } -inline bool CodeParam::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeParam::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1067,15 +896,6 @@ inline CodeParam::operator bool() return ast != nullptr; } -inline bool CodePragma::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodePragma::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1127,15 +947,6 @@ inline AST_Pragma* CodePragma::operator->() return ast; } -inline bool CodePreprocessCond::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodePreprocessCond::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1187,15 +998,6 @@ inline AST_PreprocessCond* CodePreprocessCond::operator->() return ast; } -inline bool CodeSpecifiers::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeSpecifiers::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1227,15 +1029,6 @@ inline CodeSpecifiers::operator bool() return ast != nullptr; } -inline bool CodeStruct::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeStruct::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1267,15 +1060,6 @@ inline CodeStruct::operator bool() return ast != nullptr; } -inline bool CodeTemplate::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeTemplate::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1327,15 +1111,6 @@ inline AST_Template* CodeTemplate::operator->() return ast; } -inline bool CodeType::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeType::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1387,15 +1162,6 @@ inline AST_Type* CodeType::operator->() return ast; } -inline bool CodeTypedef::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeTypedef::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1447,15 +1213,6 @@ inline AST_Typedef* CodeTypedef::operator->() return ast; } -inline bool CodeUnion::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeUnion::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1507,15 +1264,6 @@ inline AST_Union* CodeUnion::operator->() return ast; } -inline bool CodeUsing::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeUsing::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; @@ -1567,15 +1315,6 @@ inline AST_Using* CodeUsing::operator->() return ast; } -inline bool CodeVar::is_equal( Code other ) -{ - if ( ast == nullptr || other.ast == nullptr ) - { - return ast == nullptr && other.ast == nullptr; - } - return rcast( AST*, ast )->is_equal( other.ast ); -} - inline bool CodeVar::is_valid() { return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index 0713c14..e80c567 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -121,6 +121,18 @@ bool is_body(Code code) return false; } +inline +bool is_equal( Code self, Code other ) +{ + if ( self.ast == nullptr || other.ast == nullptr ) + { + // Just check if they're both null. + // log_failure( "Code::is_equal: Cannot compare code, AST is null!" ); + return self.ast == nullptr && other.ast == nullptr; + } + return is_equal( self.ast, other.ast ); +} + inline Code& Code::operator ++() { diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 819c31f..fc91e95 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -354,17 +354,6 @@ CodeBody gen_ast_inlines() char const* code_impl_tmpl = stringize( \n inline - bool ::is_equal( Code other ) - { - if ( ast == nullptr || other.ast == nullptr ) - { - // Just check if they're both null. - // log_failure( "Code::is_equal: Cannot compare code, AST is null!" ); - return ast == nullptr && other.ast == nullptr; - } - return rcast(AST*, ast)->is_equal( other.ast ); - } - inline bool ::is_valid() { return (AST*) ast != nullptr && rcast( AST*, ast)->Type != CodeT::Invalid; From f9b5029e64a9c67566a8415fc1221a9f6c21b365 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 00:41:41 -0500 Subject: [PATCH 032/112] Code::is_valid rection --- project/components/ast.hpp | 2 +- project/components/gen/ast_inlines.hpp | 145 ------------------------- project/components/inlines.hpp | 6 + project/helpers/helper.hpp | 5 - 4 files changed, 7 insertions(+), 151 deletions(-) diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 6cec65d..16645a9 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -173,7 +173,7 @@ struct Code Code duplicate() { return GEN_NS duplicate(* this); } \ bool is_equal( Code other ) { return GEN_NS is_equal(* this, other); } \ bool is_body() { return GEN_NS is_body(* this); } \ - bool is_valid(); \ + bool is_valid() { return GEN_NS is_valid(* this); } \ void set_global(); \ String to_string(); \ Typename& operator = ( AST* other ); \ diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index b336234..c787fa4 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -7,11 +7,6 @@ #pragma region generated code inline implementation -inline bool Code::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void Code::set_global() { if ( ast == nullptr ) @@ -38,11 +33,6 @@ inline Code::operator bool() return ast != nullptr; } -inline bool CodeBody::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeBody::set_global() { if ( ast == nullptr ) @@ -69,11 +59,6 @@ inline CodeBody::operator bool() return ast != nullptr; } -inline bool CodeAttributes::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeAttributes::set_global() { if ( ast == nullptr ) @@ -120,11 +105,6 @@ inline AST_Attributes* CodeAttributes::operator->() return ast; } -inline bool CodeComment::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeComment::set_global() { if ( ast == nullptr ) @@ -171,11 +151,6 @@ inline AST_Comment* CodeComment::operator->() return ast; } -inline bool CodeConstructor::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeConstructor::set_global() { if ( ast == nullptr ) @@ -222,11 +197,6 @@ inline AST_Constructor* CodeConstructor::operator->() return ast; } -inline bool CodeClass::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeClass::set_global() { if ( ast == nullptr ) @@ -253,11 +223,6 @@ inline CodeClass::operator bool() return ast != nullptr; } -inline bool CodeDefine::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeDefine::set_global() { if ( ast == nullptr ) @@ -304,11 +269,6 @@ inline AST_Define* CodeDefine::operator->() return ast; } -inline bool CodeDestructor::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeDestructor::set_global() { if ( ast == nullptr ) @@ -355,11 +315,6 @@ inline AST_Destructor* CodeDestructor::operator->() return ast; } -inline bool CodeEnum::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeEnum::set_global() { if ( ast == nullptr ) @@ -406,11 +361,6 @@ inline AST_Enum* CodeEnum::operator->() return ast; } -inline bool CodeExec::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeExec::set_global() { if ( ast == nullptr ) @@ -457,11 +407,6 @@ inline AST_Exec* CodeExec::operator->() return ast; } -inline bool CodeExtern::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeExtern::set_global() { if ( ast == nullptr ) @@ -508,11 +453,6 @@ inline AST_Extern* CodeExtern::operator->() return ast; } -inline bool CodeFriend::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeFriend::set_global() { if ( ast == nullptr ) @@ -559,11 +499,6 @@ inline AST_Friend* CodeFriend::operator->() return ast; } -inline bool CodeFn::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeFn::set_global() { if ( ast == nullptr ) @@ -610,11 +545,6 @@ inline AST_Fn* CodeFn::operator->() return ast; } -inline bool CodeInclude::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeInclude::set_global() { if ( ast == nullptr ) @@ -661,11 +591,6 @@ inline AST_Include* CodeInclude::operator->() return ast; } -inline bool CodeModule::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeModule::set_global() { if ( ast == nullptr ) @@ -712,11 +637,6 @@ inline AST_Module* CodeModule::operator->() return ast; } -inline bool CodeNS::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeNS::set_global() { if ( ast == nullptr ) @@ -763,11 +683,6 @@ inline AST_NS* CodeNS::operator->() return ast; } -inline bool CodeOperator::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeOperator::set_global() { if ( ast == nullptr ) @@ -814,11 +729,6 @@ inline AST_Operator* CodeOperator::operator->() return ast; } -inline bool CodeOpCast::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeOpCast::set_global() { if ( ast == nullptr ) @@ -865,11 +775,6 @@ inline AST_OpCast* CodeOpCast::operator->() return ast; } -inline bool CodeParam::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeParam::set_global() { if ( ast == nullptr ) @@ -896,11 +801,6 @@ inline CodeParam::operator bool() return ast != nullptr; } -inline bool CodePragma::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodePragma::set_global() { if ( ast == nullptr ) @@ -947,11 +847,6 @@ inline AST_Pragma* CodePragma::operator->() return ast; } -inline bool CodePreprocessCond::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodePreprocessCond::set_global() { if ( ast == nullptr ) @@ -998,11 +893,6 @@ inline AST_PreprocessCond* CodePreprocessCond::operator->() return ast; } -inline bool CodeSpecifiers::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeSpecifiers::set_global() { if ( ast == nullptr ) @@ -1029,11 +919,6 @@ inline CodeSpecifiers::operator bool() return ast != nullptr; } -inline bool CodeStruct::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeStruct::set_global() { if ( ast == nullptr ) @@ -1060,11 +945,6 @@ inline CodeStruct::operator bool() return ast != nullptr; } -inline bool CodeTemplate::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeTemplate::set_global() { if ( ast == nullptr ) @@ -1111,11 +991,6 @@ inline AST_Template* CodeTemplate::operator->() return ast; } -inline bool CodeType::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeType::set_global() { if ( ast == nullptr ) @@ -1162,11 +1037,6 @@ inline AST_Type* CodeType::operator->() return ast; } -inline bool CodeTypedef::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeTypedef::set_global() { if ( ast == nullptr ) @@ -1213,11 +1083,6 @@ inline AST_Typedef* CodeTypedef::operator->() return ast; } -inline bool CodeUnion::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeUnion::set_global() { if ( ast == nullptr ) @@ -1264,11 +1129,6 @@ inline AST_Union* CodeUnion::operator->() return ast; } -inline bool CodeUsing::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeUsing::set_global() { if ( ast == nullptr ) @@ -1315,11 +1175,6 @@ inline AST_Using* CodeUsing::operator->() return ast; } -inline bool CodeVar::is_valid() -{ - return (AST*)ast != nullptr && rcast( AST*, ast )->Type != CodeT::Invalid; -} - inline void CodeVar::set_global() { if ( ast == nullptr ) diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index e80c567..48e6f77 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -133,6 +133,12 @@ bool is_equal( Code self, Code other ) return is_equal( self.ast, other.ast ); } +inline +bool is_valid(Code self) +{ + return self.ast != nullptr && self.ast->Type != CodeT::Invalid; +} + inline Code& Code::operator ++() { diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index fc91e95..03ca48c 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -354,11 +354,6 @@ CodeBody gen_ast_inlines() char const* code_impl_tmpl = stringize( \n inline - bool ::is_valid() - { - return (AST*) ast != nullptr && rcast( AST*, ast)->Type != CodeT::Invalid; - } - inline void ::set_global() { if ( ast == nullptr ) From c38b077c37d7dd921705d76e653c7e60c871ab8c Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 00:43:57 -0500 Subject: [PATCH 033/112] Code::set_global reduction --- project/components/ast.hpp | 2 +- project/components/gen/ast_inlines.hpp | 290 ------------------------- project/components/inlines.hpp | 12 + project/helpers/helper.hpp | 11 - 4 files changed, 13 insertions(+), 302 deletions(-) diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 16645a9..ee33940 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -174,7 +174,7 @@ struct Code bool is_equal( Code other ) { return GEN_NS is_equal(* this, other); } \ bool is_body() { return GEN_NS is_body(* this); } \ bool is_valid() { return GEN_NS is_valid(* this); } \ - void set_global(); \ + void set_global() { return GEN_NS set_global(* this); } \ String to_string(); \ Typename& operator = ( AST* other ); \ Typename& operator = ( Code other ); \ diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index c787fa4..3224148 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -7,16 +7,6 @@ #pragma region generated code inline implementation -inline void Code::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline Code& Code::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -33,16 +23,6 @@ inline Code::operator bool() return ast != nullptr; } -inline void CodeBody::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeBody& CodeBody::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -59,16 +39,6 @@ inline CodeBody::operator bool() return ast != nullptr; } -inline void CodeAttributes::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeAttributes& CodeAttributes::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -105,16 +75,6 @@ inline AST_Attributes* CodeAttributes::operator->() return ast; } -inline void CodeComment::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeComment& CodeComment::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -151,16 +111,6 @@ inline AST_Comment* CodeComment::operator->() return ast; } -inline void CodeConstructor::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeConstructor& CodeConstructor::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -197,16 +147,6 @@ inline AST_Constructor* CodeConstructor::operator->() return ast; } -inline void CodeClass::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeClass& CodeClass::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -223,16 +163,6 @@ inline CodeClass::operator bool() return ast != nullptr; } -inline void CodeDefine::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeDefine& CodeDefine::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -269,16 +199,6 @@ inline AST_Define* CodeDefine::operator->() return ast; } -inline void CodeDestructor::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeDestructor& CodeDestructor::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -315,16 +235,6 @@ inline AST_Destructor* CodeDestructor::operator->() return ast; } -inline void CodeEnum::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeEnum& CodeEnum::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -361,16 +271,6 @@ inline AST_Enum* CodeEnum::operator->() return ast; } -inline void CodeExec::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeExec& CodeExec::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -407,16 +307,6 @@ inline AST_Exec* CodeExec::operator->() return ast; } -inline void CodeExtern::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeExtern& CodeExtern::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -453,16 +343,6 @@ inline AST_Extern* CodeExtern::operator->() return ast; } -inline void CodeFriend::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeFriend& CodeFriend::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -499,16 +379,6 @@ inline AST_Friend* CodeFriend::operator->() return ast; } -inline void CodeFn::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeFn& CodeFn::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -545,16 +415,6 @@ inline AST_Fn* CodeFn::operator->() return ast; } -inline void CodeInclude::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeInclude& CodeInclude::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -591,16 +451,6 @@ inline AST_Include* CodeInclude::operator->() return ast; } -inline void CodeModule::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeModule& CodeModule::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -637,16 +487,6 @@ inline AST_Module* CodeModule::operator->() return ast; } -inline void CodeNS::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeNS& CodeNS::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -683,16 +523,6 @@ inline AST_NS* CodeNS::operator->() return ast; } -inline void CodeOperator::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeOperator& CodeOperator::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -729,16 +559,6 @@ inline AST_Operator* CodeOperator::operator->() return ast; } -inline void CodeOpCast::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeOpCast& CodeOpCast::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -775,16 +595,6 @@ inline AST_OpCast* CodeOpCast::operator->() return ast; } -inline void CodeParam::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeParam& CodeParam::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -801,16 +611,6 @@ inline CodeParam::operator bool() return ast != nullptr; } -inline void CodePragma::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodePragma& CodePragma::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -847,16 +647,6 @@ inline AST_Pragma* CodePragma::operator->() return ast; } -inline void CodePreprocessCond::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodePreprocessCond& CodePreprocessCond::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -893,16 +683,6 @@ inline AST_PreprocessCond* CodePreprocessCond::operator->() return ast; } -inline void CodeSpecifiers::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeSpecifiers& CodeSpecifiers::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -919,16 +699,6 @@ inline CodeSpecifiers::operator bool() return ast != nullptr; } -inline void CodeStruct::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeStruct& CodeStruct::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -945,16 +715,6 @@ inline CodeStruct::operator bool() return ast != nullptr; } -inline void CodeTemplate::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeTemplate& CodeTemplate::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -991,16 +751,6 @@ inline AST_Template* CodeTemplate::operator->() return ast; } -inline void CodeType::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeType& CodeType::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -1037,16 +787,6 @@ inline AST_Type* CodeType::operator->() return ast; } -inline void CodeTypedef::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeTypedef& CodeTypedef::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -1083,16 +823,6 @@ inline AST_Typedef* CodeTypedef::operator->() return ast; } -inline void CodeUnion::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeUnion& CodeUnion::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -1129,16 +859,6 @@ inline AST_Union* CodeUnion::operator->() return ast; } -inline void CodeUsing::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeUsing& CodeUsing::operator=( Code other ) { if ( other.ast && other->Parent ) @@ -1175,16 +895,6 @@ inline AST_Using* CodeUsing::operator->() return ast; } -inline void CodeVar::set_global() -{ - if ( ast == nullptr ) - { - log_failure( "Code::set_global: Cannot set code as global, AST is null!" ); - return; - } - rcast( AST*, ast )->Parent = Code_Global.ast; -} - inline CodeVar& CodeVar::operator=( Code other ) { if ( other.ast && other->Parent ) diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index 48e6f77..ce99931 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -139,6 +139,18 @@ bool is_valid(Code self) return self.ast != nullptr && self.ast->Type != CodeT::Invalid; } +inline +void set_global(Code self) +{ + if ( self.ast == nullptr ) + { + log_failure("Code::set_global: Cannot set code as global, AST is null!"); + return; + } + + self->Parent = Code_Global.ast; +} + inline Code& Code::operator ++() { diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 03ca48c..4d874be 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -354,17 +354,6 @@ CodeBody gen_ast_inlines() char const* code_impl_tmpl = stringize( \n inline - void ::set_global() - { - if ( ast == nullptr ) - { - log_failure("Code::set_global: Cannot set code as global, AST is null!"); - return; - } - - rcast(AST*, ast)->Parent = Code_Global.ast; - } - inline & ::operator =( Code other ) { if ( other.ast && other->Parent ) From 2dcc968c3948680feb072883f450184b401c567a Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 01:56:49 -0500 Subject: [PATCH 034/112] Preparing for reductions on code_types.hpp --- project/components/ast.hpp | 177 +++++++------ project/components/code_serialization.cpp | 6 +- project/components/code_types.hpp | 302 ++++++++++++++++------ 3 files changed, 319 insertions(+), 166 deletions(-) diff --git a/project/components/ast.hpp b/project/components/ast.hpp index ee33940..51bef49 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -73,73 +73,84 @@ struct AST_Union; struct AST_Using; struct AST_Var; -struct Code; -struct CodeBody; -// These are to offer ease of use and optionally strong type safety for the AST. -struct CodeAttributes; -// struct CodeBaseClass; -struct CodeComment; -struct CodeClass; -struct CodeConstructor; -struct CodeDefine; -struct CodeDestructor; -struct CodeEnum; -struct CodeExec; -struct CodeExtern; -struct CodeInclude; -struct CodeFriend; -struct CodeFn; -struct CodeModule; -struct CodeNS; -struct CodeOperator; -struct CodeOpCast; -struct CodeParam; -struct CodePreprocessCond; -struct CodePragma; -struct CodeSpecifiers; - -#if GEN_EXECUTION_EXPRESSION_SUPPORT -struct CodeExpr; -struct CodeExpr_Assign; -struct CodeExpr_Alignof; -struct CodeExpr_Binary; -struct CodeExpr_CStyleCast; -struct CodeExpr_FunctionalCast; -struct CodeExpr_CppCast; -struct CodeExpr_Element; -struct CodeExpr_ProcCall; -struct CodeExpr_Decltype; -struct CodeExpr_Comma; -struct CodeExpr_AMS; // Access Member Symbol -struct CodeExpr_Sizeof; -struct CodeExpr_Subscript; -struct CodeExpr_Ternary; -struct CodeExpr_UnaryPrefix; -struct CodeExpr_UnaryPostfix; - -struct CodeStmt; -struct CodeStmt_Break; -struct CodeStmt_Case; -struct CodeStmt_Continue; -struct CodeStmt_Decl; -struct CodeStmt_Do; -struct CodeStmt_Expr; -struct CodeStmt_Else; -struct CodeStmt_If; -struct CodeStmt_For; -struct CodeStmt_Goto; -struct CodeStmt_Label; -struct CodeStmt_Switch; -struct CodeStmt_While; +#if GEN_COMPILER_C +#define Define_Code(Type) typedef AST_##Type* Code##Type +#else +#define Define_Code(Type) struct Code##Type #endif -struct CodeStruct; -struct CodeTemplate; -struct CodeType; -struct CodeTypedef; -struct CodeUnion; -struct CodeUsing; -struct CodeVar; +#if GEN_COMPILER_C +typedef AST* code; +#else +struct Code; +#endif +Define_Code(Body); +// These are to offer ease of use and optionally strong type safety for the AST. +Define_Code(Attributes); +// struct CodeBaseClass; +Define_Code(Comment); +Define_Code(Class); +Define_Code(Constructor); +Define_Code(Define); +Define_Code(Destructor); +Define_Code(Enum); +Define_Code(Exec); +Define_Code(Extern); +Define_Code(Include); +Define_Code(Friend); +Define_Code(Fn); +Define_Code(Module); +Define_Code(NS); +Define_Code(Operator); +Define_Code(OpCast); +Define_Code(Param); +Define_Code(PreprocessCond); +Define_Code(Pragma); +Define_Code(Specifiers); + +#if GEN_EXECUTION_EXPRESSION_SUPPORT +Define_Code(Expr); +Define_Code(Expr_Assign); +Define_Code(Expr_Alignof); +Define_Code(Expr_Binary); +Define_Code(Expr_CStyleCast); +Define_Code(Expr_FunctionalCast); +Define_Code(Expr_CppCast); +Define_Code(Expr_Element); +Define_Code(Expr_ProcCall); +Define_Code(Expr_Decltype); +Define_Code(Expr_Comma); +Define_Code(Expr_AMS); // Access Member Symbol +Define_Code(Expr_Sizeof); +Define_Code(Expr_Subscript); +Define_Code(Expr_Ternary); +Define_Code(Expr_UnaryPrefix); +Define_Code(Expr_UnaryPostfix); + +Define_Code(Stmt); +Define_Code(Stmt_Break); +Define_Code(Stmt_Case); +Define_Code(Stmt_Continue); +Define_Code(Stmt_Decl); +Define_Code(Stmt_Do); +Define_Code(Stmt_Expr); +Define_Code(Stmt_Else); +Define_Code(Stmt_If); +Define_Code(Stmt_For); +Define_Code(Stmt_Goto); +Define_Code(Stmt_Label); +Define_Code(Stmt_Switch); +Define_Code(Stmt_While); +#endif + +Define_Code(Struct); +Define_Code(Template); +Define_Code(Type); +Define_Code(Typedef); +Define_Code(Union); +Define_Code(Using); +Define_Code(Var); +#undef Define_Code namespace parser { @@ -159,6 +170,7 @@ bool is_valid (Code code); void set_global(Code code); String to_string (Code code); +#if ! GEN_COMPILER_C /* AST* wrapper - Not constantly have to append the '*' as this is written often.. @@ -168,32 +180,32 @@ struct Code { AST* ast; -# define Using_Code( Typename ) \ - char const* debug_str() { return GEN_NS debug_str(* this); } \ - Code duplicate() { return GEN_NS duplicate(* this); } \ +# define Using_Code( Typename ) \ + char const* debug_str() { return GEN_NS debug_str(* this); } \ + Code duplicate() { return GEN_NS duplicate(* this); } \ bool is_equal( Code other ) { return GEN_NS is_equal(* this, other); } \ - bool is_body() { return GEN_NS is_body(* this); } \ - bool is_valid() { return GEN_NS is_valid(* this); } \ - void set_global() { return GEN_NS set_global(* this); } \ - String to_string(); \ - Typename& operator = ( AST* other ); \ - Typename& operator = ( Code other ); \ + bool is_body() { return GEN_NS is_body(* this); } \ + bool is_valid() { return GEN_NS is_valid(* this); } \ + void set_global() { return GEN_NS set_global(* this); } + +# define Using_CodeOps( Typename ) \ + Typename& operator = ( AST* other ); \ + Typename& operator = ( Code other ); \ bool operator ==( Code other ) { return (AST*)ast == other.ast; } \ bool operator !=( Code other ) { return (AST*)ast != other.ast; } \ operator bool(); +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( Code ); + String to_string() { return GEN_NS to_string(* this); } +#endif + + Using_CodeOps( Code ); template< class Type > - forceinline Type code_cast() - { - return * rcast( Type*, this ); - } + forceinline Type code_cast() { return * rcast( Type*, this ); } - AST* operator ->() - { - return ast; - } + AST* operator ->() { return ast; } Code& operator ++(); // TODO(Ed) : Remove this overload. @@ -242,6 +254,7 @@ struct Code operator CodeVar() const; #undef operator }; +#endif #pragma region Statics // Used to identify ASTs that should always be duplicated. (Global constant ASTs) diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index d8c90c5..c08144b 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -3,14 +3,14 @@ #include "ast.cpp" #endif -String Code::to_string() +String to_string(Code self) { - if ( ast == nullptr ) + if ( self.ast == nullptr ) { log_failure( "Code::to_string: Cannot convert code to string, AST is null!" ); return { nullptr }; } - return rcast( AST*, ast )->to_string(); + return rcast( AST*, self.ast )->to_string(); } String CodeAttributes::to_string() diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index 6d211d0..d823de8 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -4,9 +4,13 @@ #endif #pragma region Code Types +// These structs are not used at all by the C vairant. +#if ! GEN_COMPILER_C +// stati_assert( GEN_COMPILER_C, "This should not be compiled with the C-library" ); struct CodeBody { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeBody ); void append( Code other ) @@ -28,19 +32,20 @@ struct CodeBody bool has_entries() { return GEN_NS has_entries(rcast( AST*, ast )); } AST* raw() { return rcast( AST*, ast ); } - void to_string( String& result ); - void to_string_export( String& result ); - - AST_Body* operator->() { return ast; } + String to_string(); + void to_string( String& result ); + void to_string_export( String& result ); +#endif + Using_CodeOps( CodeBody ); operator Code() { return * rcast( Code*, this ); } + AST_Body* operator->() { return ast; } #pragma region Iterator Code begin() { if ( ast ) return { rcast( AST*, ast)->Front }; - return { nullptr }; } Code end() @@ -54,21 +59,20 @@ struct CodeBody struct CodeClass { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeClass ); void add_interface( CodeType interface ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); - AST* raw() - { - return rcast( AST*, ast ); - } - operator Code() - { - return * rcast( Code*, this ); - } + AST* raw() { return rcast( AST*, ast ); } +#endif + + Using_CodeOps( CodeClass ); + operator Code() { return * rcast( Code*, this ); } AST_Class* operator->() { if ( ast == nullptr ) @@ -83,13 +87,17 @@ struct CodeClass struct CodeParam { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeParam ); - void append( CodeParam other ); - + void append( CodeParam other ); CodeParam get( s32 idx ); - bool has_entries(); - void to_string( String& result ); + bool has_entries(); + String to_string(); + void to_string( String& result ); +#endif + + Using_CodeOps( CodeParam ); AST* raw() { return rcast( AST*, ast ); @@ -132,6 +140,7 @@ struct CodeParam struct CodeSpecifiers { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeSpecifiers ); bool append( SpecifierT spec ) @@ -202,7 +211,11 @@ struct CodeSpecifiers } return result; } - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + + Using_CodeOps(CodeSpecifiers); AST* raw() { return rcast( AST*, ast ); @@ -239,13 +252,17 @@ struct CodeSpecifiers struct CodeStruct { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeStruct ); void add_interface( CodeType interface ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_fwd( String& result ); + void to_string_def( String& result ); +#endif + Using_CodeOps( CodeStruct ); AST* raw() { return rcast( AST*, ast ); @@ -266,27 +283,47 @@ struct CodeStruct AST_Struct* ast; }; -#define Define_CodeType( Typename ) \ - struct Code##Typename \ - { \ - Using_Code( Code ## Typename ); \ - AST* raw(); \ - operator Code(); \ - AST_##Typename* operator->(); \ - AST_##Typename* ast; \ - } +struct CodeAttributes +{ +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 + Using_Code(CodeAttributes); + String to_string(); +#endif + + Using_CodeOps(CodeAttributes); + AST *raw(); + operator Code(); + AST_Attributes *operator->(); + AST_Attributes *ast; +}; -Define_CodeType( Attributes ); // Define_CodeType( BaseClass ); -Define_CodeType( Comment ); + +struct CodeComment +{ +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 + Using_Code(CodeComment); + String to_string(); +#endif + + Using_CodeOps(CodeComment); + AST *raw(); + operator Code(); + AST_Comment *operator->(); + AST_Comment *ast; +}; struct CodeConstructor { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeConstructor ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); +#endif + Using_CodeOps(CodeConstructor); AST* raw(); operator Code(); AST_Constructor* operator->(); @@ -295,10 +332,14 @@ struct CodeConstructor struct CodeDefine { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeDefine ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeDefine); AST* raw(); operator Code(); AST_Define* operator->(); @@ -307,11 +348,15 @@ struct CodeDefine struct CodeDestructor { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeDestructor ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); +#endif + Using_CodeOps(CodeDestructor); AST* raw(); operator Code(); AST_Destructor* operator->(); @@ -320,20 +365,36 @@ struct CodeDestructor struct CodeEnum { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeEnum ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); - void to_string_class_def( String& result ); - void to_string_class_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); + void to_string_class_def( String& result ); + void to_string_class_fwd( String& result ); +#endif + Using_CodeOps(CodeEnum); AST* raw(); operator Code(); AST_Enum* operator->(); AST_Enum* ast; }; -Define_CodeType( Exec ); +struct CodeExec +{ +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 + Using_Code(CodeExec); + String to_string(); +#endif + + Using_CodeOps(CodeExec); + AST *raw(); + operator Code(); + AST_Exec *operator->(); + AST_Exec *ast; +}; #if GEN_EXECUTION_EXPRESSION_SUPPORT struct CodeExpr @@ -543,10 +604,13 @@ struct CodeExpr_UnaryPostfix struct CodeExtern { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeExtern ); void to_string( String& result ); +#endif + Using_CodeOps(CodeExtern); AST* raw(); operator Code(); AST_Extern* operator->(); @@ -555,10 +619,14 @@ struct CodeExtern struct CodeInclude { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeInclude ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeInclude); AST* raw(); operator Code(); AST_Include* operator->(); @@ -567,10 +635,14 @@ struct CodeInclude struct CodeFriend { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeFriend ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeFriend); AST* raw(); operator Code(); AST_Friend* operator->(); @@ -579,11 +651,15 @@ struct CodeFriend struct CodeFn { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeFn ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); +#endif + Using_CodeOps(CodeFn); AST* raw(); operator Code(); AST_Fn* operator->(); @@ -592,10 +668,14 @@ struct CodeFn struct CodeModule { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeModule ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeModule); AST* raw(); operator Code(); AST_Module* operator->(); @@ -604,10 +684,14 @@ struct CodeModule struct CodeNS { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeNS ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeNS); AST* raw(); operator Code(); AST_NS* operator->(); @@ -616,11 +700,15 @@ struct CodeNS struct CodeOperator { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeOperator ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); +#endif + Using_CodeOps(CodeOperator); AST* raw(); operator Code(); AST_Operator* operator->(); @@ -629,11 +717,15 @@ struct CodeOperator struct CodeOpCast { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeOpCast ); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string(); + void to_string_def( String& result ); + void to_string_fwd( String& result ); +#endif + Using_CodeOps(CodeOpCast); AST* raw(); operator Code(); AST_OpCast* operator->(); @@ -642,10 +734,14 @@ struct CodeOpCast struct CodePragma { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodePragma ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps( CodePragma ); AST* raw(); operator Code(); AST_Pragma* operator->(); @@ -654,15 +750,19 @@ struct CodePragma struct CodePreprocessCond { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodePreprocessCond ); - void to_string_if( String& result ); - void to_string_ifdef( String& result ); - void to_string_ifndef( String& result ); - void to_string_elif( String& result ); - void to_string_else( String& result ); - void to_string_endif( String& result ); + String to_string(); + void to_string_if( String& result ); + void to_string_ifdef( String& result ); + void to_string_ifndef( String& result ); + void to_string_elif( String& result ); + void to_string_else( String& result ); + void to_string_endif( String& result ); +#endif + Using_CodeOps( CodePreprocessCond ); AST* raw(); operator Code(); AST_PreprocessCond* operator->(); @@ -674,7 +774,8 @@ struct CodeStmt { Using_Code( CodeStmt ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -686,7 +787,8 @@ struct CodeStmt_Break { Using_Code( CodeStmt_Break ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -698,7 +800,8 @@ struct CodeStmt_Case { Using_Code( CodeStmt_Case ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -710,7 +813,8 @@ struct CodeStmt_Continue { Using_Code( CodeStmt_Continue ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -722,7 +826,8 @@ struct CodeStmt_Decl { Using_Code( CodeStmt_Decl ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -734,7 +839,8 @@ struct CodeStmt_Do { Using_Code( CodeStmt_Do ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -746,7 +852,8 @@ struct CodeStmt_Expr { Using_Code( CodeStmt_Expr ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -758,7 +865,8 @@ struct CodeStmt_Else { Using_Code( CodeStmt_Else ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -770,7 +878,8 @@ struct CodeStmt_If { Using_Code( CodeStmt_If ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -782,7 +891,8 @@ struct CodeStmt_For { Using_Code( CodeStmt_For ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -794,7 +904,8 @@ struct CodeStmt_Goto { Using_Code( CodeStmt_Goto ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -806,7 +917,8 @@ struct CodeStmt_Label { Using_Code( CodeStmt_Label ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -818,7 +930,8 @@ struct CodeStmt_Switch { Using_Code( CodeStmt_Switch ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -830,7 +943,8 @@ struct CodeStmt_While { Using_Code( CodeStmt_While ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); AST* raw(); operator Code(); @@ -841,10 +955,14 @@ struct CodeStmt_While struct CodeTemplate { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeTemplate ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps( CodeTemplate ); AST* raw(); operator Code(); AST_Template* operator->(); @@ -853,10 +971,14 @@ struct CodeTemplate struct CodeType { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeType ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps( CodeType ); AST* raw(); operator Code(); AST_Type* operator->(); @@ -865,10 +987,14 @@ struct CodeType struct CodeTypedef { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeTypedef ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps( CodeTypedef ); AST* raw(); operator Code(); AST_Typedef* operator->(); @@ -877,10 +1003,14 @@ struct CodeTypedef struct CodeUnion { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeUnion ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeUnion); AST* raw(); operator Code(); AST_Union* operator->(); @@ -889,11 +1019,15 @@ struct CodeUnion struct CodeUsing { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeUsing ); - void to_string( String& result ); - void to_string_ns( String& result ); + String to_string(); + void to_string( String& result ); + void to_string_ns( String& result ); +#endif + Using_CodeOps(CodeUsing); AST* raw(); operator Code(); AST_Using* operator->(); @@ -902,10 +1036,14 @@ struct CodeUsing struct CodeVar { +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeVar ); - void to_string( String& result ); + String to_string(); + void to_string( String& result ); +#endif + Using_CodeOps(CodeVar); AST* raw(); operator Code(); AST_Var* operator->(); @@ -914,5 +1052,7 @@ struct CodeVar #undef Define_CodeType #undef Using_Code +#undef Using_CodeOps +#endif //if ! GEN_COMPILER_C #pragma endregion Code Types From 9b68791e387837af7c147b468c214516f9f25885 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 02:11:49 -0500 Subject: [PATCH 035/112] fixes for array when not using member features. --- gen_singleheader/singleheader.cpp | 2 ++ project/bootstrap.cpp | 2 +- project/components/ast.hpp | 2 +- project/dependencies/containers.hpp | 8 +++++--- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/gen_singleheader/singleheader.cpp b/gen_singleheader/singleheader.cpp index 5226d62..0e12511 100644 --- a/gen_singleheader/singleheader.cpp +++ b/gen_singleheader/singleheader.cpp @@ -1,6 +1,8 @@ #define GEN_DEFINE_LIBRARY_CODE_CONSTANTS #define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_EXPOSE_BACKEND +#define GEN_SUPPORT_CPP_MEMBER_FEATURES 1 +#define GEN_SUPPORT_CPP_REFERENCES 0 #include "gen.cpp" #include "helpers/push_ignores.inline.hpp" diff --git a/project/bootstrap.cpp b/project/bootstrap.cpp index 2a7332c..4d9b86d 100644 --- a/project/bootstrap.cpp +++ b/project/bootstrap.cpp @@ -1,7 +1,7 @@ #define GEN_DEFINE_LIBRARY_CODE_CONSTANTS #define GEN_ENFORCE_STRONG_CODE_TYPES #define GEN_EXPOSE_BACKEND -#define GEN_SUPPORT_CPP_MEMBER_FEATURES 1 +#define GEN_SUPPORT_CPP_MEMBER_FEATURES 0 #define GEN_SUPPORT_CPP_REFERENCES 0 #include "gen.cpp" diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 51bef49..2d1fe0a 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -298,7 +298,7 @@ char const* type_str ( AST& self ) { return type_str( & self ); } */ struct AST { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 # pragma region Member Functions void append ( AST* other ) { GEN_NS append(this, other); } char const* debug_str () { return GEN_NS debug_str(this); } diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index a6d6248..9205b95 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -201,7 +201,7 @@ bool append_at(Array* array, Type item, usize idx) header = get_header(* array); } - Type* target = array->Data + slot; + Type* target = &(*array)[slot]; mem_move(target + 1, target, (header->Num - slot) * sizeof(Type)); header->Num++; @@ -278,7 +278,8 @@ void free(Array* array) { GEN_ASSERT(array != nullptr); ArrayHeader* header = get_header(* array); GEN_NS free(header->Allocator, header); - array->Data = nullptr; + Type** Data = (Type**)array; + *Data = nullptr; } template forceinline @@ -374,7 +375,8 @@ bool set_capacity(Array* array, usize new_capacity) GEN_NS free(header->Allocator, header); - array->Data = rcast(Type*, new_header + 1); + Type** Data = (Type**)array; + * Data = rcast(Type*, new_header + 1); return true; } From 9321a04ebcfedb37921d157700692a683335e420 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 02:38:55 -0500 Subject: [PATCH 036/112] reduction of Code struct member function usage in base lib --- gen_c_library/c_library.cpp | 8 +-- gen_c_library/components/misc.hpp | 6 +- project/components/ast.cpp | 10 ++-- project/components/ast.hpp | 10 +--- project/components/code_serialization.cpp | 52 ++++++++-------- project/components/code_types.hpp | 4 +- project/components/interface.cpp | 28 ++++----- project/components/interface.upfront.cpp | 73 ++++++++++++----------- project/components/parser.cpp | 2 +- 9 files changed, 95 insertions(+), 98 deletions(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 79baf1c..0f9c3bd 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -136,7 +136,7 @@ int gen_main() case ECode::Using: { log_fmt("REPLACE THIS MANUALLY: %S\n", entry->Name); - CodeUsing using_ver = entry.code_cast(); + CodeUsing using_ver = cast(CodeUsing, entry); CodeTypedef typedef_ver = def_typedef(using_ver->Name, using_ver->UnderlyingType); memory.append(typedef_ver); @@ -144,7 +144,7 @@ int gen_main() break; case ECode::Function_Fwd: { - CodeFn fn = entry.code_cast(); + CodeFn fn = cast(CodeFn, entry); if ( fn->Name.is_equal(txt("free")) ) { fn->Name = get_cached_string(txt("gen_free_ptr")); @@ -154,7 +154,7 @@ int gen_main() break; case ECode::Function: { - CodeFn fn = entry.code_cast(); + CodeFn fn = cast(CodeFn, entry); s32 constexpr_found = fn->Specs.remove( ESpecifier::Constexpr ); if (constexpr_found > -1) { log_fmt("Found constexpr: %S\n", entry->to_string()); @@ -169,7 +169,7 @@ int gen_main() break; case ECode::Template: { - CodeTemplate tmpl = entry.code_cast(); + CodeTemplate tmpl = cast(CodeTemplate, entry); if ( tmpl->Declaration->Name.contains(txt("swap"))) { CodeBody macro_swap = parse_global_body( txt(R"( diff --git a/gen_c_library/components/misc.hpp b/gen_c_library/components/misc.hpp index c323713..b383041 100644 --- a/gen_c_library/components/misc.hpp +++ b/gen_c_library/components/misc.hpp @@ -8,7 +8,7 @@ using SwapContentProc = CodeBody(void); b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& body ) { b32 found = false; - CodePreprocessCond cond = entry_iter.code_cast(); + CodePreprocessCond cond = cast(CodePreprocessCond, entry_iter); if ( cond->Content.contains(cond_sig) ) { log_fmt("Preprocess cond found: %S\n", cond->Content); @@ -44,7 +44,7 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_content, Code& entry_iter, CodeBody& body ) { bool found = false; - CodePragma possible_region = entry_iter.code_cast(); + CodePragma possible_region = cast(CodePragma, entry_iter); String region_sig = string_fmt_buf(GlobalAllocator, "region %s", region_name.Ptr); String endregion_sig = string_fmt_buf(GlobalAllocator, "endregion %s", region_name.Ptr); @@ -58,7 +58,7 @@ bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_ (entry_iter->Type) { case ECode::Preprocess_Pragma: { - CodePragma possible_end_region = entry_iter.code_cast(); + CodePragma possible_end_region = cast(CodePragma, entry_iter); if ( possible_end_region->Content.contains(endregion_sig) ) { // body.append(possible_end_region); continue_for = false; diff --git a/project/components/ast.cpp b/project/components/ast.cpp index 3d20a7b..6948866 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -1159,12 +1159,12 @@ bool AST::validate_body() #define CheckEntries( Unallowed_Types ) \ do \ { \ - for ( Code entry : code_cast() ) \ + for ( Code entry : code_cast() ) \ { \ switch ( entry->Type ) \ { \ Unallowed_Types \ - log_failure( "AST::validate_body: Invalid entry in body %s", entry.debug_str() ); \ + log_failure( "AST::validate_body: Invalid entry in body %s", GEN_NS debug_str(entry) ); \ return false; \ } \ } \ @@ -1181,7 +1181,7 @@ bool AST::validate_body() { if ( entry->Type != Untyped ) { - log_failure( "AST::validate_body: Invalid entry in enum body (needs to be untyped or comment) %s", entry.debug_str() ); + log_failure( "AST::validate_body: Invalid entry in enum body (needs to be untyped or comment) %s", GEN_NS debug_str(entry) ); return false; } } @@ -1217,7 +1217,7 @@ bool AST::validate_body() case Specifiers: case Struct_Body: case Typename: - log_failure("AST::validate_body: Invalid entry in body %s", entry.debug_str()); + log_failure("AST::validate_body: Invalid entry in body %s", GEN_NS debug_str(entry)); return false; } } @@ -1233,7 +1233,7 @@ bool AST::validate_body() { if ( entry->Type != Untyped ) { - log_failure( "AST::validate_body: Invalid entry in union body (needs to be untyped or comment) %s", entry.debug_str() ); + log_failure( "AST::validate_body: Invalid entry in union body (needs to be untyped or comment) %s", GEN_NS debug_str(entry) ); return false; } } diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 2d1fe0a..9e8f8d7 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -157,9 +157,8 @@ namespace parser struct Token; } -template< class Type> forceinline Type tmpl_cast( Code* self ) { return * rcast( Type*, self ); } -#if ! GEN_COMPILER_C && 0 -template< class Type> forceinline Type tmpl_cast( Code& self ) { return * rcast( Type*, & self ); } +#if ! GEN_COMPILER_C +template< class Type> forceinline Type tmpl_cast( Code self ) { return * rcast( Type*, & self ); } #endif char const* debug_str (Code code); @@ -195,16 +194,13 @@ struct Code bool operator !=( Code other ) { return (AST*)ast != other.ast; } \ operator bool(); -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( Code ); String to_string() { return GEN_NS to_string(* this); } #endif Using_CodeOps( Code ); - template< class Type > - forceinline Type code_cast() { return * rcast( Type*, this ); } - AST* operator ->() { return ast; } Code& operator ++(); diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index c08144b..734fd24 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -53,7 +53,7 @@ void CodeBody::to_string( String& result ) s32 left = ast->NumEntries; while ( left -- ) { - append_fmt( & result, "%S", curr.to_string() ); + append_fmt( & result, "%S", GEN_NS to_string(curr) ); ++curr; } } @@ -66,7 +66,7 @@ void CodeBody::to_string_export( String& result ) s32 left = ast->NumEntries; while ( left-- ) { - append_fmt( & result, "%S", curr.to_string() ); + append_fmt( & result, "%S", GEN_NS to_string(curr) ); ++curr; } @@ -110,12 +110,12 @@ void CodeConstructor::to_string_def( String& result ) append( & result, "()" ); if ( ast->InitializerList ) - append_fmt( & result, " : %S", ast->InitializerList.to_string() ); + append_fmt( & result, " : %S", GEN_NS to_string(ast->InitializerList) ); if ( ast->InlineCmt ) append_fmt( & result, " // %S", ast->InlineCmt->Content ); - append_fmt( & result, "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}\n", GEN_NS to_string(ast->Body) ); } void CodeConstructor::to_string_fwd( String& result ) @@ -134,7 +134,7 @@ void CodeConstructor::to_string_fwd( String& result ) append_fmt( & result, "()"); if (ast->Body) - append_fmt( & result, " = %S", ast->Body.to_string() ); + append_fmt( & result, " = %S", GEN_NS to_string(ast->Body) ); if ( ast->InlineCmt ) append_fmt( & result, "; // %S\n", ast->InlineCmt->Content ); @@ -264,7 +264,7 @@ void CodeDestructor::to_string_def( String& result ) else append_fmt( & result, "~%S()", ast->Parent->Name ); - append_fmt( & result, "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}\n", GEN_NS to_string(ast->Body) ); } void CodeDestructor::to_string_fwd( String& result ) @@ -279,7 +279,7 @@ void CodeDestructor::to_string_fwd( String& result ) if ( ast->Specs.has( ESpecifier::Pure ) ) append( & result, " = 0;" ); else if (ast->Body) - append_fmt( & result, " = %S;", ast->Body.to_string() ); + append_fmt( & result, " = %S;", GEN_NS to_string(ast->Body) ); } else append_fmt( & result, "~%S();", ast->Parent->Name ); @@ -333,7 +333,7 @@ void CodeEnum::to_string_def( String& result ) else if ( ast->UnderlyingTypeMacro ) append_fmt( & result, "%S : %S\n{\n%S\n}" , ast->Name - , ast->UnderlyingTypeMacro.to_string() + , GEN_NS to_string(ast->UnderlyingTypeMacro) , ast->Body.to_string() ); @@ -872,11 +872,11 @@ void CodeParam::to_string( String& result ) if ( ast->PostNameMacro ) { - append_fmt( & result, " %S", ast->PostNameMacro.to_string() ); + append_fmt( & result, " %S", GEN_NS to_string(ast->PostNameMacro) ); } if ( ast->Value ) - append_fmt( & result, " = %S", ast->Value.to_string() ); + append_fmt( & result, " = %S", GEN_NS to_string(ast->Value) ); if ( ast->NumEntries - 1 > 0 ) { @@ -1068,9 +1068,9 @@ void CodeTemplate::to_string( String& result ) append( & result, "export " ); if ( ast->Params ) - append_fmt( & result, "template< %S >\n%S", ast->Params.to_string(), ast->Declaration.to_string() ); + append_fmt( & result, "template< %S >\n%S", GEN_NS to_string(ast->Params), GEN_NS to_string(ast->Declaration) ); else - append_fmt( & result, "template<>\n%S", ast->Declaration.to_string() ); + append_fmt( & result, "template<>\n%S", GEN_NS to_string(ast->Declaration) ); } String CodeTypedef::to_string() @@ -1089,9 +1089,9 @@ void CodeTypedef::to_string( String& result ) // Determines if the typedef is a function typename if ( ast->UnderlyingType->ReturnType ) - append( & result, ast->UnderlyingType.to_string() ); + append( & result, GEN_NS to_string(ast->UnderlyingType) ); else - append_fmt( & result, "%S %S", ast->UnderlyingType.to_string(), ast->Name ); + append_fmt( & result, "%S %S", GEN_NS to_string(ast->UnderlyingType), ast->Name ); if ( ast->UnderlyingType->Type == ECode::Typename && ast->UnderlyingType->ArrExpr ) { @@ -1234,7 +1234,7 @@ void CodeUsing::to_string( String& result ) if ( ast->UnderlyingType->ArrExpr ) { - append_fmt( & result, "[ %S ]", ast->UnderlyingType->ArrExpr.to_string() ); + append_fmt( & result, "[ %S ]", GEN_NS to_string(ast->UnderlyingType->ArrExpr) ); AST* next_arr_expr = ast->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) @@ -1283,7 +1283,7 @@ void CodeVar::to_string( String& result ) if ( ast->ValueType->ArrExpr ) { - append_fmt( & result, "[ %S ]", ast->ValueType->ArrExpr.to_string() ); + append_fmt( & result, "[ %S ]", GEN_NS to_string(ast->ValueType->ArrExpr) ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) @@ -1296,9 +1296,9 @@ void CodeVar::to_string( String& result ) if ( ast->Value ) { if ( ast->VarConstructorInit ) - append_fmt( & result, "( %S ", ast->Value.to_string() ); + append_fmt( & result, "( %S ", GEN_NS to_string(ast->Value) ); else - append_fmt( & result, " = %S", ast->Value.to_string() ); + append_fmt( & result, " = %S", GEN_NS to_string(ast->Value) ); } // Keep the chain going... @@ -1326,7 +1326,7 @@ void CodeVar::to_string( String& result ) if ( ast->ValueType->ArrExpr ) { - append_fmt( & result, "[ %S ]", ast->ValueType->ArrExpr.to_string() ); + append_fmt( & result, "[ %S ]", GEN_NS to_string(ast->ValueType->ArrExpr) ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) @@ -1337,14 +1337,14 @@ void CodeVar::to_string( String& result ) } if ( ast->BitfieldSize ) - append_fmt( & result, " : %S", ast->BitfieldSize.to_string() ); + append_fmt( & result, " : %S", GEN_NS to_string(ast->BitfieldSize) ); if ( ast->Value ) { if ( ast->VarConstructorInit ) - append_fmt( & result, "( %S ", ast->Value.to_string() ); + append_fmt( & result, "( %S ", GEN_NS to_string(ast->Value) ); else - append_fmt( & result, " = %S", ast->Value.to_string() ); + append_fmt( & result, " = %S", GEN_NS to_string(ast->Value) ); } if ( ast->NextVar ) @@ -1362,11 +1362,11 @@ void CodeVar::to_string( String& result ) } if ( ast->BitfieldSize ) - append_fmt( & result, "%S %S : %S", ast->ValueType.to_string(), ast->Name, ast->BitfieldSize.to_string() ); + append_fmt( & result, "%S %S : %S", ast->ValueType.to_string(), ast->Name, GEN_NS to_string(ast->BitfieldSize) ); else if ( ast->ValueType->ArrExpr ) { - append_fmt( & result, "%S %S[ %S ]", ast->ValueType.to_string(), ast->Name, ast->ValueType->ArrExpr.to_string() ); + append_fmt( & result, "%S %S[ %S ]", ast->ValueType.to_string(), ast->Name, GEN_NS to_string(ast->ValueType->ArrExpr) ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) @@ -1382,9 +1382,9 @@ void CodeVar::to_string( String& result ) if ( ast->Value ) { if ( ast->VarConstructorInit ) - append_fmt( & result, "( %S ", ast->Value.to_string() ); + append_fmt( & result, "( %S ", GEN_NS to_string(ast->Value) ); else - append_fmt( & result, " = %S", ast->Value.to_string() ); + append_fmt( & result, " = %S", GEN_NS to_string(ast->Value) ); } if ( ast->NextVar ) diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index d823de8..997ec07 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -17,8 +17,8 @@ struct CodeBody { GEN_ASSERT(other.ast != nullptr); - if (other.is_body()) { - append( cast(CodeBody, & other) ); + if (GEN_NS is_body(other)) { + append( cast(CodeBody, other) ); } GEN_NS append( raw(), other.ast ); diff --git a/project/components/interface.cpp b/project/components/interface.cpp index 425b921..f709dc4 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -79,69 +79,69 @@ void define_constants() scast(String, Code_Global->Content) = Code_Global->Name; Code_Invalid = make_code(); - Code_Invalid.set_global(); + set_global(Code_Invalid); t_empty = (CodeType) make_code(); t_empty->Type = ECode::Typename; t_empty->Name = get_cached_string( txt("") ); - t_empty.set_global(); + set_global(t_empty); access_private = make_code(); access_private->Type = ECode::Access_Private; access_private->Name = get_cached_string( txt("private:\n") ); - access_private.set_global(); + set_global(access_private); access_protected = make_code(); access_protected->Type = ECode::Access_Protected; access_protected->Name = get_cached_string( txt("protected:\n") ); - access_protected.set_global(); + set_global(access_protected); access_public = make_code(); access_public->Type = ECode::Access_Public; access_public->Name = get_cached_string( txt("public:\n") ); - access_public.set_global(); + set_global(access_public); attrib_api_export = def_attributes( code(GEN_API_Export_Code)); - attrib_api_export.set_global(); + set_global(attrib_api_export); attrib_api_import = def_attributes( code(GEN_API_Import_Code)); - attrib_api_import.set_global(); + set_global(attrib_api_import); module_global_fragment = make_code(); module_global_fragment->Type = ECode::Untyped; module_global_fragment->Name = get_cached_string( txt("module;") ); module_global_fragment->Content = module_global_fragment->Name; - module_global_fragment.set_global(); + set_global(module_global_fragment); module_private_fragment = make_code(); module_private_fragment->Type = ECode::Untyped; module_private_fragment->Name = get_cached_string( txt("module : private;") ); module_private_fragment->Content = module_private_fragment->Name; - module_private_fragment.set_global(); + set_global(module_private_fragment); fmt_newline = make_code(); fmt_newline->Type = ECode::NewLine; - fmt_newline.set_global(); + set_global(fmt_newline); pragma_once = (CodePragma) make_code(); pragma_once->Type = ECode::Preprocess_Pragma; pragma_once->Name = get_cached_string( txt("once") ); pragma_once->Content = pragma_once->Name; - pragma_once.set_global(); + set_global(pragma_once); param_varadic = (CodeType) make_code(); param_varadic->Type = ECode::Parameters; param_varadic->Name = get_cached_string( txt("...") ); param_varadic->ValueType = t_empty; - param_varadic.set_global(); + set_global(param_varadic); preprocess_else = (CodePreprocessCond) make_code(); preprocess_else->Type = ECode::Preprocess_Else; - preprocess_else.set_global(); + set_global(preprocess_else); preprocess_endif = (CodePreprocessCond) make_code(); preprocess_endif->Type = ECode::Preprocess_EndIf; - preprocess_endif.set_global(); + set_global(preprocess_endif); # define def_constant_code_type( Type_ ) \ t_##Type_ = def_type( name(Type_) ); \ diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index af72be6..9a19a6b 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -527,7 +527,7 @@ CodeConstructor def_constructor( CodeParam params, Code initializer_list, Code b break; default: - log_failure("gen::def_constructor: body must be either of Function_Body or Untyped type - %s", body.debug_str()); + log_failure("gen::def_constructor: body must be either of Function_Body or Untyped type - %s", debug_str(body)); return InvalidCode; } @@ -579,7 +579,7 @@ CodeClass def_class( StrC name break; default: - log_failure("gen::def_class: body must be either of Class_Body or Untyped type - %s", body.debug_str()); + log_failure("gen::def_class: body must be either of Class_Body or Untyped type - %s", debug_str(body)); return InvalidCode; } @@ -665,7 +665,7 @@ CodeDestructor def_destructor( Code body, CodeSpecifiers specifiers ) break; default: - log_failure("gen::def_destructor: body must be either of Function_Body or Untyped type - %s", body.debug_str()); + log_failure("gen::def_destructor: body must be either of Function_Body or Untyped type - %s", debug_str(body)); return InvalidCode; } @@ -715,7 +715,7 @@ CodeEnum def_enum( StrC name break; default: - log_failure( "gen::def_enum: body must be of Enum_Body or Untyped type %s", body.debug_str()); + log_failure( "gen::def_enum: body must be of Enum_Body or Untyped type %s", debug_str(body)); return InvalidCode; } @@ -944,7 +944,7 @@ CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags ) if ( body->Type != Namespace_Body && body->Type != Untyped ) { - log_failure("gen::def_namespace: body is not of namespace or untyped type %s", body.debug_str()); + log_failure("gen::def_namespace: body is not of namespace or untyped type %s", debug_str(body)); return InvalidCode; } @@ -1057,7 +1057,7 @@ CodeOpCast def_operator_cast( CodeType type, Code body, CodeSpecifiers const_spe if ( body->Type != Function_Body && body->Type != Execution ) { - log_failure( "gen::def_operator_cast: body is not of function body or execution type - %s", body.debug_str() ); + log_failure( "gen::def_operator_cast: body is not of function body or execution type - %s", debug_str(body) ); return InvalidCode; } @@ -1086,13 +1086,13 @@ CodeParam def_param( CodeType type, StrC name, Code value ) if ( type->Type != Typename ) { - log_failure( "gen::def_param: type is not a typename - %s", type.debug_str() ); + log_failure( "gen::def_param: type is not a typename - %s", debug_str(type) ); return InvalidCode; } if ( value && value->Type != Untyped ) { - log_failure( "gen::def_param: value is not untyped - %s", value.debug_str() ); + log_failure( "gen::def_param: value is not untyped - %s", debug_str(value) ); return InvalidCode; } @@ -1189,13 +1189,13 @@ CodeStruct def_struct( StrC name if ( parent && parent->Type != Typename ) { - log_failure( "gen::def_struct: parent was not a `Struct` type - %s", parent.debug_str() ); + log_failure( "gen::def_struct: parent was not a `Struct` type - %s", debug_str(parent) ); return InvalidCode; } if ( body && body->Type != Struct_Body ) { - log_failure( "gen::def_struct: body was not a Struct_Body type - %s", body.debug_str() ); + log_failure( "gen::def_struct: body was not a Struct_Body type - %s", debug_str(body) ); return InvalidCode; } @@ -1256,7 +1256,7 @@ CodeTemplate def_template( CodeParam params, Code declaration, ModuleFlag mflags break; default: - log_failure( "gen::def_template: declaration is not of class, function, struct, variable, or using type - %s", declaration.debug_str() ); + log_failure( "gen::def_template: declaration is not of class, function, struct, variable, or using type - %s", debug_str(declaration) ); } CodeTemplate @@ -1329,13 +1329,13 @@ CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes, Module case Typename: break; default: - log_failure( "gen::def_typedef: type was not a Class, Enum, Function Forward, Struct, Typename, or Union - %s", type.debug_str() ); + log_failure( "gen::def_typedef: type was not a Class, Enum, Function Forward, Struct, Typename, or Union - %s", debug_str(type) ); return InvalidCode; } if ( attributes && attributes->Type != ECode::PlatformAttributes ) { - log_failure( "gen::def_typedef: attributes was not a PlatformAttributes - %s", attributes.debug_str() ); + log_failure( "gen::def_typedef: attributes was not a PlatformAttributes - %s", debug_str(attributes) ); return InvalidCode; } @@ -1359,7 +1359,7 @@ CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes, Module { if (type->Type != Untyped) { - log_failure( "gen::def_typedef: name was empty and type was not untyped (indicating its a function typedef) - %s", type.debug_str() ); + log_failure( "gen::def_typedef: name was empty and type was not untyped (indicating its a function typedef) - %s", debug_str(type) ); return InvalidCode; } @@ -1381,7 +1381,7 @@ CodeUnion def_union( StrC name, Code body, CodeAttributes attributes, ModuleFlag if ( body->Type != ECode::Union_Body ) { - log_failure( "gen::def_union: body was not a Union_Body type - %s", body.debug_str() ); + log_failure( "gen::def_union: body was not a Union_Body type - %s", debug_str(body) ); return InvalidCode; } @@ -1482,7 +1482,7 @@ CodeVar def_variable( CodeType type, StrC name, Code value if ( value && value->Type != ECode::Untyped ) { - log_failure( "gen::def_variable: value was not a `Untyped` type - %s", value.debug_str() ); + log_failure( "gen::def_variable: value was not a `Untyped` type - %s", debug_str(value) ); return InvalidCode; } @@ -1558,7 +1558,7 @@ CodeBody def_class_body( s32 num, ... ) switch (entry->Type) { GEN_AST_BODY_CLASS_UNALLOWED_TYPES - log_failure("gen::" "def_class_body" ": Entry type is not allowed: %s", entry.debug_str()); + log_failure("gen::" "def_class_body" ": Entry type is not allowed: %s", debug_str(entry)); return InvalidCode; default: @@ -1595,7 +1595,7 @@ CodeBody def_class_body( s32 num, Code* codes ) switch (entry->Type) { GEN_AST_BODY_CLASS_UNALLOWED_TYPES - log_failure("gen::" "def_class_body" ": Entry type is not allowed: %s", entry.debug_str()); + log_failure("gen::" "def_class_body" ": Entry type is not allowed: %s", debug_str(entry)); return InvalidCode; default: @@ -1632,7 +1632,7 @@ CodeBody def_enum_body( s32 num, ... ) if ( entry->Type != Untyped && entry->Type != Comment ) { - log_failure("gen::def_enum_body: Entry type is not allowed - %s. Must be of untyped or comment type.", entry.debug_str() ); + log_failure("gen::def_enum_body: Entry type is not allowed - %s. Must be of untyped or comment type.", debug_str(entry) ); return InvalidCode; } @@ -1664,7 +1664,7 @@ CodeBody def_enum_body( s32 num, Code* codes ) if ( entry->Type != Untyped && entry->Type != Comment ) { - log_failure("gen::def_enum_body: Entry type is not allowed: %s", entry.debug_str() ); + log_failure("gen::def_enum_body: Entry type is not allowed: %s", debug_str(entry) ); return InvalidCode; } @@ -1699,7 +1699,7 @@ CodeBody def_export_body( s32 num, ... ) switch (entry->Type) { GEN_AST_BODY_EXPORT_UNALLOWED_TYPES - log_failure("gen::" "def_export_body" ": Entry type is not allowed: %s", entry.debug_str()); + log_failure("gen::" "def_export_body" ": Entry type is not allowed: %s", debug_str(entry)); return InvalidCode; default: @@ -1736,7 +1736,7 @@ CodeBody def_export_body( s32 num, Code* codes ) switch (entry->Type) { GEN_AST_BODY_EXPORT_UNALLOWED_TYPES - log_failure("gen::" "def_export_body" ": Entry type is not allowed: %s", entry.debug_str()); + log_failure("gen::" "def_export_body" ": Entry type is not allowed: %s", debug_str(entry)); return InvalidCode; default: @@ -1774,7 +1774,7 @@ CodeBody def_extern_link_body( s32 num, ... ) switch (entry->Type) { GEN_AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES - log_failure("gen::" "def_extern_linkage_body" ": Entry type is not allowed: %s", entry.debug_str()); + log_failure("gen::" "def_extern_linkage_body" ": Entry type is not allowed: %s", debug_str(entry)); return InvalidCode; default: @@ -1811,7 +1811,7 @@ CodeBody def_extern_link_body( s32 num, Code* codes ) switch (entry->Type) { GEN_AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES - log_failure("gen::" "def_extern_linkage_body" ": Entry type is not allowed: %s", entry.debug_str()); + log_failure("gen::" "def_extern_linkage_body" ": Entry type is not allowed: %s", debug_str(entry)); return InvalidCode; default: @@ -1851,7 +1851,7 @@ CodeBody def_function_body( s32 num, ... ) { GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES - log_failure("gen::" stringize(def_function_body) ": Entry type is not allowed: %s", entry.debug_str()); + log_failure("gen::" stringize(def_function_body) ": Entry type is not allowed: %s", debug_str(entry)); return InvalidCode; default: @@ -1888,7 +1888,7 @@ CodeBody def_function_body( s32 num, Code* codes ) switch (entry->Type) { GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES - log_failure("gen::" "def_function_body" ": Entry type is not allowed: %s", entry.debug_str()); + log_failure("gen::" "def_function_body" ": Entry type is not allowed: %s", debug_str(entry)); return InvalidCode; default: @@ -1925,11 +1925,12 @@ CodeBody def_global_body( s32 num, ... ) switch (entry->Type) { case Global_Body: - result.append( entry.code_cast() ) ; + // result.append( entry.code_cast() ) ; + result.append( cast(CodeBody, entry) ) ; continue; GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES - log_failure("gen::" "def_global_body" ": Entry type is not allowed: %s", entry.debug_str()); + log_failure("gen::" "def_global_body" ": Entry type is not allowed: %s", debug_str(entry)); return InvalidCode; default: @@ -1966,11 +1967,11 @@ CodeBody def_global_body( s32 num, Code* codes ) switch (entry->Type) { case Global_Body: - result.append( entry.code_cast() ) ; + result.append( cast(CodeBody, entry) ); continue; GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES - log_failure("gen::" "def_global_body" ": Entry type is not allowed: %s", entry.debug_str()); + log_failure("gen::" "def_global_body" ": Entry type is not allowed: %s", debug_str(entry)); return InvalidCode; default: @@ -2008,7 +2009,7 @@ CodeBody def_namespace_body( s32 num, ... ) switch (entry->Type) { GEN_AST_BODY_NAMESPACE_UNALLOWED_TYPES - log_failure("gen::" "def_namespace_body" ": Entry type is not allowed: %s", entry.debug_str()); + log_failure("gen::" "def_namespace_body" ": Entry type is not allowed: %s", debug_str(entry)); return InvalidCode; default: @@ -2045,7 +2046,7 @@ CodeBody def_namespace_body( s32 num, Code* codes ) switch (entry->Type) { GEN_AST_BODY_NAMESPACE_UNALLOWED_TYPES - log_failure("gen::" "def_namespace_body" ": Entry type is not allowed: %s", entry.debug_str() ); + log_failure("gen::" "def_namespace_body" ": Entry type is not allowed: %s", debug_str(entry) ); return InvalidCode; default: break; @@ -2217,7 +2218,7 @@ CodeBody def_struct_body( s32 num, ... ) switch (entry->Type) { GEN_AST_BODY_STRUCT_UNALLOWED_TYPES - log_failure("gen::" "def_struct_body" ": Entry type is not allowed: %s", entry.debug_str()); + log_failure("gen::" "def_struct_body" ": Entry type is not allowed: %s", debug_str(entry)); return InvalidCode; default: @@ -2254,7 +2255,7 @@ CodeBody def_struct_body( s32 num, Code* codes ) switch (entry->Type) { GEN_AST_BODY_STRUCT_UNALLOWED_TYPES - log_failure("gen::" "def_struct_body" ": Entry type is not allowed: %s", entry.debug_str() ); + log_failure("gen::" "def_struct_body" ": Entry type is not allowed: %s", debug_str(entry) ); return InvalidCode; default: @@ -2291,7 +2292,7 @@ CodeBody def_union_body( s32 num, ... ) if ( entry->Type != Untyped && entry->Type != Comment ) { - log_failure("gen::def_union_body: Entry type is not allowed - %s. Must be of untyped or comment type.", entry.debug_str() ); + log_failure("gen::def_union_body: Entry type is not allowed - %s. Must be of untyped or comment type.", debug_str(entry) ); return InvalidCode; } @@ -2323,7 +2324,7 @@ CodeBody def_union_body( s32 num, CodeUnion* codes ) if ( entry->Type != Untyped && entry->Type != Comment ) { - log_failure("gen::def_union_body: Entry type is not allowed: %s", entry.debug_str() ); + log_failure("gen::def_union_body: Entry type is not allowed: %s", debug_str(entry) ); return InvalidCode; } diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 196a3d1..c024bc2 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -5067,7 +5067,7 @@ CodeTypedef parse_typedef() // Type needs to be aware of its parent so that it can be serialized properly. if ( type->Type == Typename && array_expr && array_expr->Type != Invalid ) - type.code_cast()->ArrExpr = array_expr; + cast(CodeType, type)->ArrExpr = array_expr; if ( inline_cmt ) result->InlineCmt = inline_cmt; From 5b0079fb0c8e7f9004649381fd1edc3b3aa66734 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 03:18:52 -0500 Subject: [PATCH 037/112] ast interface uage reductions --- docs/AST_Types.md | 2 +- docs/Readme.md | 2 +- project/auxillary/builder.cpp | 2 +- project/components/ast.cpp | 171 +++++++++++----------- project/components/ast.hpp | 81 +++++----- project/components/ast_types.hpp | 122 +++++++-------- project/components/code_serialization.cpp | 24 +-- project/components/code_types.hpp | 8 +- project/components/gen/ast_inlines.hpp | 58 ++++---- project/components/interface.upfront.cpp | 18 +-- project/helpers/helper.hpp | 2 +- 11 files changed, 241 insertions(+), 249 deletions(-) diff --git a/docs/AST_Types.md b/docs/AST_Types.md index 3f96ceb..4fce55f 100644 --- a/docs/AST_Types.md +++ b/docs/AST_Types.md @@ -552,7 +552,7 @@ Serialization: Fields: ```cpp -SpecifierT ArrSpecs[ AST::ArrSpecs_Cap ]; +SpecifierT ArrSpecs[ AST_ArrSpecs_Cap ]; CodeSpecifiers NextSpecs; Code Prev; Code Next; diff --git a/docs/Readme.md b/docs/Readme.md index 4621d56..2ba612f 100644 --- a/docs/Readme.md +++ b/docs/Readme.md @@ -101,7 +101,7 @@ union { }; StringCached Content; // Attributes, Comment, Execution, Include struct { - SpecifierT ArrSpecs[AST::ArrSpecs_Cap]; // Specifiers + SpecifierT ArrSpecs[AST_ArrSpecs_Cap]; // Specifiers AST* NextSpecs; // Specifiers }; }; diff --git a/project/auxillary/builder.cpp b/project/auxillary/builder.cpp index b283e96..1960306 100644 --- a/project/auxillary/builder.cpp +++ b/project/auxillary/builder.cpp @@ -26,7 +26,7 @@ void Builder::pad_lines( s32 num ) void Builder::print( Code code ) { - String str = code->to_string(); + String str = to_string(code); // const ssize len = str.length(); // log_fmt( "%s - print: %.*s\n", File.filename, len > 80 ? 80 : len, str.Data ); append( & Buffer, str ); diff --git a/project/components/ast.cpp b/project/components/ast.cpp index 6948866..128c788 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -14,7 +14,7 @@ char const* debug_str(AST* self) String* result = & result_stack; if ( self->Parent ) - append_fmt( result, "\n\tParent : %S %S", self->Parent->type_str(), self->Name ? self->Name : "" ); + append_fmt( result, "\n\tParent : %S %S", type_str(self->Parent), self->Name ? self->Name : "" ); else append_fmt( result, "\n\tParent : %S", "Null" ); @@ -372,19 +372,20 @@ AST* duplicate(AST* self) return result; } -String AST::to_string() +String to_string(AST* self) { String result = string_make( GlobalAllocator, "" ); - to_string( result ); + GEN_NS to_string( self, & result ); return result; } -void AST::to_string( String& result ) +void to_string( AST* self, String* result ) { + GEN_ASSERT(self != nullptr); local_persist thread_local char SerializationLevel = 0; - switch ( Type ) + switch ( self->Type ) { using namespace ECode; @@ -392,191 +393,191 @@ void AST::to_string( String& result ) #ifdef GEN_DONT_ALLOW_INVALID_CODE log_failure("Attempted to serialize invalid code! - %S", Parent ? Parent->debug_str() : Name ); #else - GEN_NS append_fmt( & result, "Invalid Code!" ); + append_fmt( result, "Invalid Code!" ); #endif break; case NewLine: - GEN_NS append( & result,"\n"); + append( result,"\n"); break; case Untyped: case Execution: case Comment: case PlatformAttributes: - GEN_NS append( & result, Content ); + append( result, self->Content ); break; case Access_Private: case Access_Protected: case Access_Public: - GEN_NS append( & result, Name ); + append( result, self->Name ); break; case Class: - code_cast().to_string_def( result ); + cast(CodeClass, {self}).to_string_def( * result ); break; case Class_Fwd: - code_cast().to_string_fwd( result ); + cast(CodeClass, {self}).to_string_fwd( * result ); break; case Constructor: - code_cast().to_string_def( result ); + cast(CodeConstructor, {self}).to_string_def( * result ); break; case Constructor_Fwd: - code_cast().to_string_fwd( result ); + cast(CodeConstructor, {self}).to_string_fwd( * result ); break; case Destructor: - code_cast().to_string_def( result ); + cast(CodeDestructor, {self}).to_string_def( * result ); break; case Destructor_Fwd: - code_cast().to_string_fwd( result ); + cast(CodeDestructor, {self}).to_string_fwd( * result ); break; case Enum: - code_cast().to_string_def( result ); + cast(CodeEnum, {self}).to_string_def( * result ); break; case Enum_Fwd: - code_cast().to_string_fwd( result ); + cast(CodeEnum, {self}).to_string_fwd( * result ); break; case Enum_Class: - code_cast().to_string_class_def( result ); + cast(CodeEnum, {self}).to_string_class_def( * result ); break; case Enum_Class_Fwd: - code_cast().to_string_class_fwd( result ); + cast(CodeEnum, {self}).to_string_class_fwd( * result ); break; case Export_Body: - code_cast().to_string_export( result ); + cast(CodeBody, {self}).to_string_export( * result ); break; case Extern_Linkage: - code_cast().to_string( result ); + cast(CodeExtern, {self}).to_string( * result ); break; case Friend: - code_cast().to_string( result ); + cast(CodeFriend, {self}).to_string( * result ); break; case Function: - code_cast().to_string_def( result ); + cast(CodeFn, {self}).to_string_def( * result ); break; case Function_Fwd: - code_cast().to_string_fwd( result ); + cast(CodeFn, {self}).to_string_fwd( * result ); break; case Module: - code_cast().to_string( result ); + cast(CodeModule, {self}).to_string( * result ); break; case Namespace: - code_cast().to_string( result ); + cast(CodeNS, {self}).to_string( * result ); break; case Operator: case Operator_Member: - code_cast().to_string_def( result ); + cast(CodeOperator, {self}).to_string_def( * result ); break; case Operator_Fwd: case Operator_Member_Fwd: - code_cast().to_string_fwd( result ); + cast(CodeOperator, {self}).to_string_fwd( * result ); break; case Operator_Cast: - code_cast().to_string_def( result ); + cast(CodeOpCast, {self}).to_string_def( * result ); break; case Operator_Cast_Fwd: - code_cast().to_string_fwd( result ); + cast(CodeOpCast, {self}).to_string_fwd( * result ); break; case Parameters: - code_cast().to_string( result ); + cast(CodeParam, {self}).to_string( * result ); break; case Preprocess_Define: - code_cast().to_string( result ); + cast(CodeDefine, {self}).to_string( * result ); break; case Preprocess_If: - code_cast().to_string_if( result ); + cast(CodePreprocessCond, {self}).to_string_if( * result ); break; case Preprocess_IfDef: - code_cast().to_string_ifdef( result ); + cast(CodePreprocessCond, {self}).to_string_ifdef( * result ); break; case Preprocess_IfNotDef: - code_cast().to_string_ifndef( result ); + cast(CodePreprocessCond, {self}).to_string_ifndef( * result ); break; case Preprocess_Include: - code_cast().to_string( result ); + cast(CodeInclude, {self}).to_string( * result ); break; case Preprocess_ElIf: - code_cast().to_string_elif( result ); + cast(CodePreprocessCond, {self}).to_string_elif( * result ); break; case Preprocess_Else: - code_cast().to_string_else( result ); + cast(CodePreprocessCond, {self}).to_string_else( * result ); break; case Preprocess_EndIf: - code_cast().to_string_endif( result ); + cast(CodePreprocessCond, {self}).to_string_endif( * result ); break; case Preprocess_Pragma: - code_cast().to_string( result ); + cast(CodePragma, {self}).to_string( * result ); break; case Specifiers: - code_cast().to_string( result ); + cast(CodeSpecifiers, {self}).to_string( * result ); break; case Struct: - code_cast().to_string_def( result ); + cast(CodeStruct, {self}).to_string_def( * result ); break; case Struct_Fwd: - code_cast().to_string_fwd( result ); + cast(CodeStruct, {self}).to_string_fwd( * result ); break; case Template: - code_cast().to_string( result ); + cast(CodeTemplate, {self}).to_string( * result ); break; case Typedef: - code_cast().to_string( result ); + cast(CodeTypedef, {self}).to_string( * result ); break; case Typename: - code_cast().to_string( result ); + cast(CodeType, {self}).to_string( * result ); break; case Union: - code_cast().to_string( result ); + cast(CodeUnion, {self}).to_string( * result ); break; case Using: - code_cast().to_string( result ); + cast(CodeUsing, {self}).to_string( * result ); break; case Using_Namespace: - code_cast().to_string_ns( result ); + cast(CodeUsing, {self}).to_string_ns( * result ); break; case Variable: - code_cast().to_string( result ); + cast(CodeVar, {self}).to_string( * result ); break; case Enum_Body: @@ -587,7 +588,7 @@ void AST::to_string( String& result ) case Namespace_Body: case Struct_Body: case Union_Body: - code_cast().to_string( result ); + cast(CodeBody, {self}).to_string( * result ); break; } } @@ -611,7 +612,7 @@ bool is_equal( AST* self, AST* other ) { log_fmt("AST::is_equal: Type check failure with other\nAST: %S\nOther: %S" , debug_str(self) - , other->debug_str() + ,debug_str(other) ); return false; @@ -628,7 +629,7 @@ bool is_equal( AST* self, AST* other ) "AST : %S\n" \ "Other: %S\n" \ , debug_str(self) \ - , other->debug_str() \ + ,debug_str(other) \ ); \ \ return false; \ @@ -641,7 +642,7 @@ bool is_equal( AST* self, AST* other ) "AST : %S\n" \ "Other: %S\n" \ , debug_str(self) \ - , other->debug_str() \ + ,debug_str(other) \ ); \ \ return false; \ @@ -654,7 +655,7 @@ bool is_equal( AST* self, AST* other ) "AST : %S\n" \ "Other: %S\n" \ , debug_str(self) \ - , other->debug_str() \ + ,debug_str(other) \ ); \ \ log_fmt("Content cannot be trusted to be unique with this check " \ @@ -676,14 +677,14 @@ bool is_equal( AST* self, AST* other ) "Other: %s\n" \ "For ast member: %s\n" \ , debug_str(self) \ - , other->debug_str() \ - , self->ast->debug_str() \ + , debug_str(other) \ + , debug_str(self->ast) \ ); \ \ return false; \ } \ \ - if ( ! self->ast->is_equal( other->ast ) ) \ + if ( ! is_equal(self->ast, other->ast ) ) \ { \ log_fmt( "\nAST::is_equal: Failed for " #ast"\n" \ "AST : %S\n" \ @@ -691,9 +692,9 @@ bool is_equal( AST* self, AST* other ) "For ast member: %S\n" \ "other's ast member: %S\n" \ , debug_str(self) \ - , other->debug_str() \ - , self->ast->debug_str() \ - , other->ast->debug_str() \ + , debug_str(other) \ + , debug_str(self->ast) \ + , debug_str(other->ast) \ ); \ \ return false; \ @@ -921,7 +922,7 @@ bool is_equal( AST* self, AST* other ) "AST : %S\n" "Other: %S\n" "For ast member: %S\n" - , curr->debug_str() + , debug_str(curr) ); return false; @@ -935,14 +936,14 @@ bool is_equal( AST* self, AST* other ) "For ast member: %S\n" "other's ast member: %S\n" , debug_str(self) - , other->debug_str() - , curr->debug_str() - , curr_other->debug_str() + , debug_str(other) + , debug_str(curr) + , debug_str(curr_other) ); return false; } - if ( curr->ValueType && ! curr->ValueType->is_equal(curr_other->ValueType) ) + if ( curr->ValueType && ! is_equal(curr->ValueType, curr_other->ValueType) ) { log_fmt( "\nAST::is_equal: Failed for parameter value type check\n" "AST : %S\n" @@ -950,14 +951,14 @@ bool is_equal( AST* self, AST* other ) "For ast member: %S\n" "other's ast member: %S\n" , debug_str(self) - , other->debug_str() - , curr->debug_str() - , curr_other->debug_str() + , debug_str(other) + , debug_str(curr) + , debug_str(curr_other) ); return false; } - if ( curr->Value && ! curr->Value->is_equal(curr_other->Value) ) + if ( curr->Value && ! is_equal(curr->Value, curr_other->Value) ) { log_fmt( "\nAST::is_equal: Failed for parameter value check\n" "AST : %S\n" @@ -965,9 +966,9 @@ bool is_equal( AST* self, AST* other ) "For ast member: %S\n" "other's ast member: %S\n" , debug_str(self) - , other->debug_str() - , curr->debug_str() - , curr_other->debug_str() + , debug_str(other) + , debug_str(curr) + , debug_str(curr_other) ); return false; } @@ -1113,13 +1114,13 @@ bool is_equal( AST* self, AST* other ) "AST : %S\n" "Other: %S\n" "For ast member: %S\n" - , curr->debug_str() + , debug_str(curr) ); return false; } - if ( ! curr->is_equal( curr_other ) ) + if ( ! is_equal( curr, curr_other ) ) { log_fmt( "\nAST::is_equal: Failed for body\n" "AST : %S\n" @@ -1127,9 +1128,9 @@ bool is_equal( AST* self, AST* other ) "For ast member: %S\n" "other's ast member: %S\n" , debug_str(self) - , other->debug_str() - , curr->debug_str() - , curr_other->debug_str() + , debug_str(other) + , debug_str(curr) + , debug_str(curr_other) ); return false; @@ -1152,14 +1153,14 @@ bool is_equal( AST* self, AST* other ) return true; } -bool AST::validate_body() +bool validate_body(AST* self) { using namespace ECode; #define CheckEntries( Unallowed_Types ) \ do \ { \ - for ( Code entry : code_cast() ) \ + for ( Code entry : cast(CodeBody, {self}) ) \ { \ switch ( entry->Type ) \ { \ @@ -1171,13 +1172,13 @@ bool AST::validate_body() } \ while (0); - switch ( Type ) + switch ( self->Type ) { case Class_Body: CheckEntries( GEN_AST_BODY_CLASS_UNALLOWED_TYPES ); break; case Enum_Body: - for ( Code entry : code_cast() ) + for ( Code entry : cast(CodeBody, {self}) ) { if ( entry->Type != Untyped ) { @@ -1196,7 +1197,7 @@ bool AST::validate_body() CheckEntries( GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES ); break; case Global_Body: - for (Code entry : code_cast()) + for (Code entry : cast(CodeBody, {self})) { switch (entry->Type) { @@ -1229,7 +1230,7 @@ bool AST::validate_body() CheckEntries( GEN_AST_BODY_STRUCT_UNALLOWED_TYPES ); break; case Union_Body: - for ( Code entry : Body->code_cast() ) + for ( Code entry : cast(CodeBody, {self->Body}) ) { if ( entry->Type != Untyped ) { @@ -1240,7 +1241,7 @@ bool AST::validate_body() break; default: - log_failure( "AST::validate_body: Invalid this AST does not have a body %s", debug_str() ); + log_failure( "AST::validate_body: Invalid this AST does not have a body %s", debug_str(self) ); return false; } diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 9e8f8d7..4923199 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -270,15 +270,17 @@ static_assert( sizeof(Code) == sizeof(Code_POD), "ERROR: Code is not POD" ); // Desired width of the AST data structure. constexpr int const AST_POD_Size = 128; -void append ( AST* self, AST* other ); -char const* debug_str ( AST* self ); -AST* duplicate ( AST* self ); -Code* entry ( AST* self, u32 idx ); -bool has_entries( AST* self ); -bool is_body ( AST* self ); -bool is_equal ( AST* self, AST* other ); -String to_string ( AST* self ); -char const* type_str ( AST* self ); +void append ( AST* self, AST* other ); +char const* debug_str ( AST* self ); +AST* duplicate ( AST* self ); +Code* entry ( AST* self, u32 idx ); +bool has_entries ( AST* self ); +bool is_body ( AST* self ); +bool is_equal ( AST* self, AST* other ); +String to_string ( AST* self ); +void to_string ( AST* self, String* result ); +char const* type_str ( AST* self ); +bool validate_body( AST* self ); #if GEN_CPP_SUPPORT_REFERENCES void append ( AST& self, AST& other ) { return append(& self, & other); } @@ -289,33 +291,40 @@ String to_string( AST& self ) { return to_string( & self ); } char const* type_str ( AST& self ) { return type_str( & self ); } #endif +constexpr static +int AST_ArrSpecs_Cap = +( + AST_POD_Size + - sizeof(AST*) * 3 + - sizeof(parser::Token*) + - sizeof(AST*) + - sizeof(StringCached) + - sizeof(CodeT) + - sizeof(ModuleFlag) + - sizeof(int) +) +/ sizeof(int) - 1; // -1 for 4 extra bytes + /* Simple AST POD with functionality to seralize into C++ syntax. */ struct AST { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES # pragma region Member Functions - void append ( AST* other ) { GEN_NS append(this, other); } - char const* debug_str () { return GEN_NS debug_str(this); } - AST* duplicate () { return GEN_NS duplicate(this); } - Code* entry ( u32 idx ) { return GEN_NS entry(this, idx); } - bool has_entries(); + void append ( AST* other ) { GEN_NS append(this, other); } + char const* debug_str () { return GEN_NS debug_str(this); } + AST* duplicate () { return GEN_NS duplicate(this); } + Code* entry ( u32 idx ) { return GEN_NS entry(this, idx); } + bool has_entries() { return GEN_NS has_entries(this); } bool is_equal ( AST* other ) { return GEN_NS is_equal(this, other); } bool is_body() { return GEN_NS is_body(this); } char const* type_str() { return GEN_NS type_str(this); } - bool validate_body(); + bool validate_body() { return GEN_NS validate_body(this); } - String to_string(); //{ return GEN_NS to_string(this); } + String to_string() { return GEN_NS to_string(this); } + void to_string( String& result ) { return GEN_NS to_string(this, & result); } - template< class Type > - forceinline Type code_cast() - { - return * this; - } - - neverinline - void to_string( String& result ); # pragma endregion Member Functions #endif @@ -350,20 +359,6 @@ struct AST operator CodeUsing(); operator CodeVar(); - constexpr static - int ArrSpecs_Cap = - ( - AST_POD_Size - - sizeof(AST*) * 3 - - sizeof(parser::Token*) - - sizeof(AST*) - - sizeof(StringCached) - - sizeof(CodeT) - - sizeof(ModuleFlag) - - sizeof(int) - ) - / sizeof(int) - 1; // -1 for 4 extra bytes - union { struct { @@ -396,7 +391,7 @@ struct AST }; StringCached Content; // Attributes, Comment, Execution, Include struct { - SpecifierT ArrSpecs[ArrSpecs_Cap]; // Specifiers + SpecifierT ArrSpecs[AST_ArrSpecs_Cap]; // Specifiers AST* NextSpecs; // Specifiers; If ArrSpecs is full, then NextSpecs is used. }; }; @@ -460,7 +455,7 @@ struct AST_POD }; StringCached Content; // Attributes, Comment, Execution, Include struct { - SpecifierT ArrSpecs[AST::ArrSpecs_Cap]; // Specifiers + SpecifierT ArrSpecs[AST_ArrSpecs_Cap]; // Specifiers AST* NextSpecs; // Specifiers; If ArrSpecs is full, then NextSpecs is used. }; }; @@ -489,10 +484,6 @@ struct AST_POD }; }; - -// TODO(Ed): Convert -String to_string ( AST* self ) { return self->to_string(); } - // Its intended for the AST to have equivalent size to its POD. // All extra functionality within the AST namespace should just be syntatic sugar. static_assert( sizeof(AST) == sizeof(AST_POD), "ERROR: AST IS NOT POD" ); diff --git a/project/components/ast_types.hpp b/project/components/ast_types.hpp index e4e5295..d91db61 100644 --- a/project/components/ast_types.hpp +++ b/project/components/ast_types.hpp @@ -12,7 +12,7 @@ struct AST_Body { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; Code Front; Code Back; parser::Token* Tok; @@ -27,7 +27,7 @@ static_assert( sizeof(AST_Body) == sizeof(AST), "ERROR: AST_Body is not the same struct AST_Attributes { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; StringCached Content; }; Code Prev; @@ -44,7 +44,7 @@ static_assert( sizeof(AST_Attributes) == sizeof(AST), "ERROR: AST_Attributes is struct AST_BaseClass { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; Code Prev; Code Next; @@ -60,7 +60,7 @@ static_assert( sizeof(AST_BaseClass) == sizeof(AST), "ERROR: AST_BaseClass is no struct AST_Comment { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; StringCached Content; }; Code Prev; @@ -76,7 +76,7 @@ static_assert( sizeof(AST_Comment) == sizeof(AST), "ERROR: AST_Comment is not th struct AST_Class { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { CodeComment InlineCmt; // Only supported by forward declarations @@ -102,7 +102,7 @@ static_assert( sizeof(AST_Class) == sizeof(AST), "ERROR: AST_Class is not the sa struct AST_Constructor { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { CodeComment InlineCmt; // Only supported by forward declarations @@ -127,7 +127,7 @@ static_assert( sizeof(AST_Constructor) == sizeof(AST), "ERROR: AST_Constructor i struct AST_Define { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; StringCached Content; }; Code Prev; @@ -143,7 +143,7 @@ static_assert( sizeof(AST_Define) == sizeof(AST), "ERROR: AST_Define is not the struct AST_Destructor { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { CodeComment InlineCmt; @@ -167,7 +167,7 @@ static_assert( sizeof(AST_Destructor) == sizeof(AST), "ERROR: AST_Destructor is struct AST_Enum { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { CodeComment InlineCmt; @@ -193,7 +193,7 @@ static_assert( sizeof(AST_Enum) == sizeof(AST), "ERROR: AST_Enum is not the same struct AST_Exec { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; StringCached Content; }; Code Prev; @@ -210,7 +210,7 @@ static_assert( sizeof(AST_Exec) == sizeof(AST), "ERROR: AST_Exec is not the same struct AST_Expr { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -225,7 +225,7 @@ static_assert( sizeof(AST_Expr) == sizeof(AST), "ERROR: AST_Expr is not the same struct AST_Expr_Assign { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -240,7 +240,7 @@ static_assert( sizeof(AST_Expr_Assign) == sizeof(AST), "ERROR: AST_Expr_Assign i struct AST_Expr_Alignof { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -255,7 +255,7 @@ static_assert( sizeof(AST_Expr_Alignof) == sizeof(AST), "ERROR: AST_Expr_Alignof struct AST_Expr_Binary { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -270,7 +270,7 @@ static_assert( sizeof(AST_Expr_Binary) == sizeof(AST), "ERROR: AST_Expr_Binary i struct AST_Expr_CStyleCast { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -285,7 +285,7 @@ static_assert( sizeof(AST_Expr_CStyleCast) == sizeof(AST), "ERROR: AST_Expr_CSty struct AST_Expr_FunctionalCast { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -300,7 +300,7 @@ static_assert( sizeof(AST_Expr_FunctionalCast) == sizeof(AST), "ERROR: AST_Expr_ struct AST_Expr_CppCast { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -315,7 +315,7 @@ static_assert( sizeof(AST_Expr_CppCast) == sizeof(AST), "ERROR: AST_Expr_CppCast struct AST_Expr_ProcCall { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -330,7 +330,7 @@ static_assert( sizeof(AST_Expr_ProcCall) == sizeof(AST), "ERROR: AST_Expr_Identi struct AST_Expr_Decltype { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -345,7 +345,7 @@ static_assert( sizeof(AST_Expr_Decltype) == sizeof(AST), "ERROR: AST_Expr_Declty struct AST_Expr_Comma { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -360,7 +360,7 @@ static_assert( sizeof(AST_Expr_Comma) == sizeof(AST), "ERROR: AST_Expr_Comma is struct AST_Expr_AMS { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -375,7 +375,7 @@ static_assert( sizeof(AST_Expr_AMS) == sizeof(AST), "ERROR: AST_Expr_AMS is not struct AST_Expr_Sizeof { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -390,7 +390,7 @@ static_assert( sizeof(AST_Expr_Sizeof) == sizeof(AST), "ERROR: AST_Expr_Sizeof i struct AST_Expr_Subscript { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -405,7 +405,7 @@ static_assert( sizeof(AST_Expr_Subscript) == sizeof(AST), "ERROR: AST_Expr_Subsc struct AST_Expr_Ternary { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -420,7 +420,7 @@ static_assert( sizeof(AST_Expr_Ternary) == sizeof(AST), "ERROR: AST_Expr_Ternary struct AST_Expr_UnaryPrefix { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -435,7 +435,7 @@ static_assert( sizeof(AST_Expr_UnaryPrefix) == sizeof(AST), "ERROR: AST_Expr_Una struct AST_Expr_UnaryPostfix { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -450,7 +450,7 @@ static_assert( sizeof(AST_Expr_UnaryPostfix) == sizeof(AST), "ERROR: AST_Expr_Un struct AST_Expr_Element { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -466,7 +466,7 @@ static_assert( sizeof(AST_Expr_Element) == sizeof(AST), "ERROR: AST_Expr_Element struct AST_Extern { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { char _PAD_PROPERTIES_[ sizeof(AST*) * 5 ]; @@ -487,7 +487,7 @@ static_assert( sizeof(AST_Extern) == sizeof(AST), "ERROR: AST_Extern is not the struct AST_Include { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; StringCached Content; }; Code Prev; @@ -503,7 +503,7 @@ static_assert( sizeof(AST_Include) == sizeof(AST), "ERROR: AST_Include is not th struct AST_Friend { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { CodeComment InlineCmt; @@ -525,7 +525,7 @@ static_assert( sizeof(AST_Friend) == sizeof(AST), "ERROR: AST_Friend is not the struct AST_Fn { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { CodeComment InlineCmt; @@ -550,7 +550,7 @@ static_assert( sizeof(AST_Fn) == sizeof(AST), "ERROR: AST_Fn is not the same siz struct AST_Module { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; Code Prev; Code Next; parser::Token* Tok; @@ -565,7 +565,7 @@ static_assert( sizeof(AST_Module) == sizeof(AST), "ERROR: AST_Module is not the struct AST_NS { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { char _PAD_PROPERTIES_[ sizeof(AST*) * 5 ]; CodeBody Body; @@ -586,7 +586,7 @@ static_assert( sizeof(AST_NS) == sizeof(AST), "ERROR: AST_NS is not the same siz struct AST_Operator { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { CodeComment InlineCmt; @@ -612,7 +612,7 @@ static_assert( sizeof(AST_Operator) == sizeof(AST), "ERROR: AST_Operator is not struct AST_OpCast { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { CodeComment InlineCmt; @@ -637,7 +637,7 @@ static_assert( sizeof(AST_OpCast) == sizeof(AST), "ERROR: AST_OpCast is not the struct AST_Param { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { char _PAD_PROPERTIES_2_[ sizeof(AST*) * 3 ]; @@ -662,7 +662,7 @@ static_assert( sizeof(AST_Param) == sizeof(AST), "ERROR: AST_Param is not the sa struct AST_Pragma { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; StringCached Content; }; Code Prev; @@ -678,7 +678,7 @@ static_assert( sizeof(AST_Pragma) == sizeof(AST), "ERROR: AST_Pragma is not the struct AST_PreprocessCond { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; StringCached Content; }; Code Prev; @@ -693,7 +693,7 @@ static_assert( sizeof(AST_PreprocessCond) == sizeof(AST), "ERROR: AST_Preprocess struct AST_Specifiers { - SpecifierT ArrSpecs[ AST::ArrSpecs_Cap ]; + SpecifierT ArrSpecs[ AST_ArrSpecs_Cap ]; CodeSpecifiers NextSpecs; Code Prev; Code Next; @@ -710,7 +710,7 @@ static_assert( sizeof(AST_Specifiers) == sizeof(AST), "ERROR: AST_Specifier is n struct AST_Stmt { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -725,7 +725,7 @@ static_assert( sizeof(AST_Stmt) == sizeof(AST), "ERROR: AST_Stmt is not the same struct AST_Stmt_Break { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -740,7 +740,7 @@ static_assert( sizeof(AST_Stmt_Break) == sizeof(AST), "ERROR: AST_Stmt_Break is struct AST_Stmt_Case { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -755,7 +755,7 @@ static_assert( sizeof(AST_Stmt_Case) == sizeof(AST), "ERROR: AST_Stmt_Case is no struct AST_Stmt_Continue { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -770,7 +770,7 @@ static_assert( sizeof(AST_Stmt_Continue) == sizeof(AST), "ERROR: AST_Stmt_Contin struct AST_Stmt_Decl { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -785,7 +785,7 @@ static_assert( sizeof(AST_Stmt_Decl) == sizeof(AST), "ERROR: AST_Stmt_Decl is no struct AST_Stmt_Do { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -800,7 +800,7 @@ static_assert( sizeof(AST_Stmt_Do) == sizeof(AST), "ERROR: AST_Stmt_Do is not th struct AST_Stmt_Expr { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -815,7 +815,7 @@ static_assert( sizeof(AST_Stmt_Expr) == sizeof(AST), "ERROR: AST_Stmt_Expr is no struct AST_Stmt_Else { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -830,7 +830,7 @@ static_assert( sizeof(AST_Stmt_Else) == sizeof(AST), "ERROR: AST_Stmt_Else is no struct AST_Stmt_If { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -845,7 +845,7 @@ static_assert( sizeof(AST_Stmt_If) == sizeof(AST), "ERROR: AST_Stmt_If is not th struct AST_Stmt_For { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -860,7 +860,7 @@ static_assert( sizeof(AST_Stmt_For) == sizeof(AST), "ERROR: AST_Stmt_For is not struct AST_Stmt_Goto { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -875,7 +875,7 @@ static_assert( sizeof(AST_Stmt_Goto) == sizeof(AST), "ERROR: AST_Stmt_Goto is no struct AST_Stmt_Label { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -890,7 +890,7 @@ static_assert( sizeof(AST_Stmt_Label) == sizeof(AST), "ERROR: AST_Stmt_Label is struct AST_Stmt_Switch { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -905,7 +905,7 @@ static_assert( sizeof(AST_Stmt_Switch) == sizeof(AST), "ERROR: AST_Stmt_Switch i struct AST_Stmt_While { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; }; CodeExpr Prev; CodeExpr Next; @@ -921,7 +921,7 @@ static_assert( sizeof(AST_Stmt_While) == sizeof(AST), "ERROR: AST_Stmt_While is struct AST_Struct { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { CodeComment InlineCmt; @@ -947,7 +947,7 @@ static_assert( sizeof(AST_Struct) == sizeof(AST), "ERROR: AST_Struct is not the struct AST_Template { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { char _PAD_PROPERTIES_[ sizeof(AST*) * 4 ]; @@ -972,7 +972,7 @@ static_assert( sizeof(AST_Template) == sizeof(AST), "ERROR: AST_Template is not struct AST_Type { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { char _PAD_INLINE_CMT_[ sizeof(AST*) ]; @@ -1000,7 +1000,7 @@ static_assert( sizeof(AST_Type) == sizeof(AST), "ERROR: AST_Type is not the same struct AST_Type { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { char _PAD_INLINE_CMT_[ sizeof(AST*) ]; @@ -1026,7 +1026,7 @@ static_assert( sizeof(AST_Type) == sizeof(AST), "ERROR: AST_Type is not the same struct AST_Typedef { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { CodeComment InlineCmt; @@ -1049,7 +1049,7 @@ static_assert( sizeof(AST_Typedef) == sizeof(AST), "ERROR: AST_Typedef is not th struct AST_Union { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { char _PAD_INLINE_CMT_[ sizeof(AST*) ]; @@ -1073,7 +1073,7 @@ static_assert( sizeof(AST_Union) == sizeof(AST), "ERROR: AST_Union is not the sa struct AST_Using { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { CodeComment InlineCmt; @@ -1097,7 +1097,7 @@ static_assert( sizeof(AST_Using) == sizeof(AST), "ERROR: AST_Using is not the sa struct AST_Var { union { - char _PAD_[ sizeof(SpecifierT) * AST::ArrSpecs_Cap + sizeof(AST*) ]; + char _PAD_[ sizeof(SpecifierT) * AST_ArrSpecs_Cap + sizeof(AST*) ]; struct { CodeComment InlineCmt; diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 734fd24..08323d9 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -10,7 +10,7 @@ String to_string(Code self) log_failure( "Code::to_string: Cannot convert code to string, AST is null!" ); return { nullptr }; } - return rcast( AST*, self.ast )->to_string(); + return to_string( self.ast ); } String CodeAttributes::to_string() @@ -176,14 +176,14 @@ void CodeClass::to_string_def( String& result ) append_fmt( & result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); - CodeType interface = ast->ParentType->Next->code_cast< CodeType >(); + CodeType interface = cast(CodeType, ast->ParentType->Next); if ( interface ) append( & result, "\n" ); while ( interface ) { append_fmt( & result, ", %S", interface.to_string() ); - interface = interface->Next ? interface->Next->code_cast< CodeType >() : CodeType { nullptr }; + interface = interface->Next ? cast(CodeType, interface->Next) : CodeType { nullptr }; } } else if ( ast->Name ) @@ -452,7 +452,7 @@ String CodeFriend::to_string() void CodeFriend::to_string( String& result ) { - append_fmt( & result, "friend %S", ast->Declaration->to_string() ); + append_fmt( & result, "friend %S", GEN_NS to_string(ast->Declaration) ); if ( ast->Declaration->Type != ECode::Function && result[ length(result) - 1 ] != ';' ) { @@ -1010,14 +1010,14 @@ void CodeStruct::to_string_def( String& result ) append_fmt( & result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); - CodeType interface = ast->ParentType->Next->code_cast< CodeType >(); + CodeType interface = cast(CodeType, ast->ParentType->Next); if ( interface ) append( & result, "\n" ); while ( interface ) { append_fmt( & result, ", %S", interface.to_string() ); - interface = interface->Next ? interface->Next->code_cast< CodeType >() : CodeType { nullptr }; + interface = interface->Next ? cast( CodeType, interface->Next) : CodeType { nullptr }; } } else if ( ast->Name ) @@ -1095,12 +1095,12 @@ void CodeTypedef::to_string( String& result ) if ( ast->UnderlyingType->Type == ECode::Typename && ast->UnderlyingType->ArrExpr ) { - append_fmt( & result, "[ %S ];", ast->UnderlyingType->ArrExpr->to_string() ); + append_fmt( & result, "[ %S ];", GEN_NS to_string(ast->UnderlyingType->ArrExpr) ); AST* next_arr_expr = ast->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( & result, "[ %S ];", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ];", GEN_NS to_string(next_arr_expr) ); next_arr_expr = next_arr_expr->Next; } } @@ -1239,7 +1239,7 @@ void CodeUsing::to_string( String& result ) AST* next_arr_expr = ast->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( & result, "[ %S ]", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); next_arr_expr = next_arr_expr->Next; } } @@ -1288,7 +1288,7 @@ void CodeVar::to_string( String& result ) AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( & result, "[ %S ]", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); next_arr_expr = next_arr_expr->Next; } } @@ -1331,7 +1331,7 @@ void CodeVar::to_string( String& result ) AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( & result, "[ %S ]", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); next_arr_expr = next_arr_expr->Next; } } @@ -1371,7 +1371,7 @@ void CodeVar::to_string( String& result ) AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( & result, "[ %S ]", next_arr_expr->to_string() ); + append_fmt( & result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); next_arr_expr = next_arr_expr->Next; } } diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index 997ec07..a582b62 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -151,9 +151,9 @@ struct CodeSpecifiers return false; } - if ( raw()->NumEntries == AST::ArrSpecs_Cap ) + if ( raw()->NumEntries == AST_ArrSpecs_Cap ) { - log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST::ArrSpecs_Cap ); + log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST_ArrSpecs_Cap ); return false; } @@ -179,9 +179,9 @@ struct CodeSpecifiers return -1; } - if ( raw()->NumEntries == AST::ArrSpecs_Cap ) + if ( raw()->NumEntries == AST_ArrSpecs_Cap ) { - log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST::ArrSpecs_Cap ); + log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST_ArrSpecs_Cap ); return -1; } diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index 3224148..0f840a9 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -11,7 +11,7 @@ inline Code& Code::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -27,7 +27,7 @@ inline CodeBody& CodeBody::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -43,7 +43,7 @@ inline CodeAttributes& CodeAttributes::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -79,7 +79,7 @@ inline CodeComment& CodeComment::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -115,7 +115,7 @@ inline CodeConstructor& CodeConstructor::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -151,7 +151,7 @@ inline CodeClass& CodeClass::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -167,7 +167,7 @@ inline CodeDefine& CodeDefine::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -203,7 +203,7 @@ inline CodeDestructor& CodeDestructor::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -239,7 +239,7 @@ inline CodeEnum& CodeEnum::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -275,7 +275,7 @@ inline CodeExec& CodeExec::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -311,7 +311,7 @@ inline CodeExtern& CodeExtern::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -347,7 +347,7 @@ inline CodeFriend& CodeFriend::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -383,7 +383,7 @@ inline CodeFn& CodeFn::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -419,7 +419,7 @@ inline CodeInclude& CodeInclude::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -455,7 +455,7 @@ inline CodeModule& CodeModule::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -491,7 +491,7 @@ inline CodeNS& CodeNS::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -527,7 +527,7 @@ inline CodeOperator& CodeOperator::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -563,7 +563,7 @@ inline CodeOpCast& CodeOpCast::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -599,7 +599,7 @@ inline CodeParam& CodeParam::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -615,7 +615,7 @@ inline CodePragma& CodePragma::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -651,7 +651,7 @@ inline CodePreprocessCond& CodePreprocessCond::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -687,7 +687,7 @@ inline CodeSpecifiers& CodeSpecifiers::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -703,7 +703,7 @@ inline CodeStruct& CodeStruct::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -719,7 +719,7 @@ inline CodeTemplate& CodeTemplate::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -755,7 +755,7 @@ inline CodeType& CodeType::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -791,7 +791,7 @@ inline CodeTypedef& CodeTypedef::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -827,7 +827,7 @@ inline CodeUnion& CodeUnion::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -863,7 +863,7 @@ inline CodeUsing& CodeUsing::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); @@ -899,7 +899,7 @@ inline CodeVar& CodeVar::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), other.ast->duplicate() ); + ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); rcast( AST*, ast )->Parent = nullptr; } ast = rcast( decltype( ast ), other.ast ); diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index 9a19a6b..5f9ce5e 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -772,7 +772,7 @@ CodeExtern def_extern_link( StrC name, Code body ) if ( body->Type != Extern_Linkage_Body && body->Type != Untyped ) { - log_failure("gen::def_extern_linkage: body is not of extern_linkage or untyped type %s", body->debug_str()); + log_failure("gen::def_extern_linkage: body is not of extern_linkage or untyped type %s", debug_str(body)); return InvalidCode; } @@ -804,7 +804,7 @@ CodeFriend def_friend( Code declaration ) break; default: - log_failure("gen::def_friend: requires declartion to have class, function, operator, or struct - %s", declaration->debug_str()); + log_failure("gen::def_friend: requires declartion to have class, function, operator, or struct - %s", debug_str(declaration)); return InvalidCode; } @@ -866,7 +866,7 @@ CodeFn def_function( StrC name default: { - log_failure("gen::def_function: body must be either of Function_Body, Execution, or Untyped type. %s", body->debug_str()); + log_failure("gen::def_function: body must be either of Function_Body, Execution, or Untyped type. %s", debug_str(body)); return InvalidCode; } } @@ -1008,7 +1008,7 @@ CodeOperator def_operator( OperatorT op, StrC nspace default: { - log_failure("gen::def_operator: body must be either of Function_Body, Execution, or Untyped type. %s", body->debug_str()); + log_failure("gen::def_operator: body must be either of Function_Body, Execution, or Untyped type. %s", debug_str(body)); return InvalidCode; } } @@ -1275,19 +1275,19 @@ CodeType def_type( StrC name, Code arrayexpr, CodeSpecifiers specifiers, CodeAtt if ( attributes && attributes->Type != ECode::PlatformAttributes ) { - log_failure( "gen::def_type: attributes is not of attributes type - %s", attributes.debug_str() ); + log_failure( "gen::def_type: attributes is not of attributes type - %s", debug_str(attributes) ); return InvalidCode; } if ( specifiers && specifiers->Type != ECode::Specifiers ) { - log_failure( "gen::def_type: specifiers is not of specifiers type - %s", specifiers.debug_str() ); + log_failure( "gen::def_type: specifiers is not of specifiers type - %s", debug_str(specifiers) ); return InvalidCode; } if ( arrayexpr && arrayexpr->Type != ECode::Untyped ) { - log_failure( "gen::def_type: arrayexpr is not of untyped type - %s", arrayexpr->debug_str() ); + log_failure( "gen::def_type: arrayexpr is not of untyped type - %s", debug_str(arrayexpr) ); return InvalidCode; } @@ -2141,7 +2141,7 @@ CodeSpecifiers def_specifiers( s32 num, ... ) return InvalidCode; } - if ( num > AST::ArrSpecs_Cap ) + if ( num > AST_ArrSpecs_Cap ) { log_failure("gen::def_specifiers: num of speciifers to define AST larger than AST specicifier capacity - %d", num); return InvalidCode; @@ -2173,7 +2173,7 @@ CodeSpecifiers def_specifiers( s32 num, SpecifierT* specs ) return InvalidCode; } - if ( num > AST::ArrSpecs_Cap ) + if ( num > AST_ArrSpecs_Cap ) { log_failure("gen::def_specifiers: num of speciifers to define AST larger than AST specicifier capacity - %d", num); return InvalidCode; diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 4d874be..5baa9e8 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -358,7 +358,7 @@ CodeBody gen_ast_inlines() { if ( other.ast && other->Parent ) { - ast = rcast( decltype(ast), other.ast->duplicate() ); + ast = rcast( decltype(ast), GEN_NS duplicate(other.ast) ); rcast( AST*, ast)->Parent = nullptr; } From 16b8a3a164c6b6d83a4ec700a8cf2c627b5e69ad Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 04:12:09 -0500 Subject: [PATCH 038/112] began to remove usage of code specific types member procs --- project/components/ast.cpp | 8 +- project/components/code_serialization.cpp | 121 ++++++++++++---------- project/components/code_types.hpp | 48 ++++----- project/components/inlines.hpp | 28 ++++- project/components/interface.upfront.cpp | 42 ++++---- project/components/parser.cpp | 12 +-- project/helpers/helper.hpp | 46 ++++---- 7 files changed, 168 insertions(+), 137 deletions(-) diff --git a/project/components/ast.cpp b/project/components/ast.cpp index 128c788..4950e37 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -415,11 +415,11 @@ void to_string( AST* self, String* result ) break; case Class: - cast(CodeClass, {self}).to_string_def( * result ); + to_string_def(cast(CodeClass, {self}), result ); break; case Class_Fwd: - cast(CodeClass, {self}).to_string_fwd( * result ); + to_string_fwd(cast(CodeClass, {self}), result ); break; case Constructor: @@ -455,7 +455,7 @@ void to_string( AST* self, String* result ) break; case Export_Body: - cast(CodeBody, {self}).to_string_export( * result ); + to_string_export(cast(CodeBody, {self}), result ); break; case Extern_Linkage: @@ -588,7 +588,7 @@ void to_string( AST* self, String* result ) case Namespace_Body: case Struct_Body: case Union_Body: - cast(CodeBody, {self}).to_string( * result ); + to_string( cast(CodeBody, {self}), result ); break; } } diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 08323d9..3ccc61f 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -18,15 +18,16 @@ String CodeAttributes::to_string() return GEN_NS duplicate( ast->Content, GlobalAllocator ); } -String CodeBody::to_string() +String to_string(CodeBody body) { + GEN_ASSERT(body.ast != nullptr); String result = string_make( GlobalAllocator, "" ); - switch ( ast->Type ) + switch ( body.ast->Type ) { using namespace ECode; case Untyped: case Execution: - GEN_NS append( & result, raw()->Content ); + append( & result, rcast(AST*, body.ast)->Content ); break; case Enum_Body: @@ -37,40 +38,44 @@ String CodeBody::to_string() case Namespace_Body: case Struct_Body: case Union_Body: - to_string( result ); + to_string( body, & result ); break; case Export_Body: - to_string_export( result ); + to_string_export( body, & result ); break; } return result; } -void CodeBody::to_string( String& result ) +void to_string( CodeBody body, String* result ) { - Code curr = ast->Front; - s32 left = ast->NumEntries; + GEN_ASSERT(body.ast != nullptr); + GEN_ASSERT(result != nullptr); + Code curr = body.ast->Front; + s32 left = body.ast->NumEntries; while ( left -- ) { - append_fmt( & result, "%S", GEN_NS to_string(curr) ); + append_fmt( result, "%S", GEN_NS to_string(curr) ); ++curr; } } -void CodeBody::to_string_export( String& result ) +void to_string_export( CodeBody body, String* result ) { - append_fmt( & result, "export\n{\n" ); + GEN_ASSERT(body.ast != nullptr); + GEN_ASSERT(result != nullptr); + append_fmt( result, "export\n{\n" ); - Code curr = *this; - s32 left = ast->NumEntries; + Code curr = body; + s32 left = body.ast->NumEntries; while ( left-- ) { - append_fmt( & result, "%S", GEN_NS to_string(curr) ); + append_fmt( result, "%S", to_string(curr) ); ++curr; } - append_fmt( & result, "};\n" ); + append_fmt( result, "};\n" ); } String CodeComment::to_string() @@ -142,83 +147,89 @@ void CodeConstructor::to_string_fwd( String& result ) append( & result, ";\n" ); } -String CodeClass::to_string() +String to_string( CodeClass self ) { String result = string_make( GlobalAllocator, "" ); - switch ( ast->Type ) + switch ( self->Type ) { using namespace ECode; case Class: - to_string_def( result ); + to_string_def(self, & result ); break; case Class_Fwd: - to_string_fwd( result ); + to_string_fwd(self, & result ); break; } return result; } -void CodeClass::to_string_def( String& result ) +void to_string_def( CodeClass self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + GEN_ASSERT(self.ast != nullptr); + AST_Class* ast = self.ast; - append( & result, "class " ); + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); + + append( result, "class " ); if ( ast->Attributes ) { - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", ast->Attributes.to_string() ); } if ( ast->ParentType ) { char const* access_level = to_str( ast->ParentAccess ); - append_fmt( & result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); + append_fmt( result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); CodeType interface = cast(CodeType, ast->ParentType->Next); if ( interface ) - append( & result, "\n" ); + append( result, "\n" ); while ( interface ) { - append_fmt( & result, ", %S", interface.to_string() ); + append_fmt( result, ", %S", interface.to_string() ); interface = interface->Next ? cast(CodeType, interface->Next) : CodeType { nullptr }; } } else if ( ast->Name ) { - append( & result, ast->Name ); + append( result, ast->Name ); } if ( ast->InlineCmt ) { - append_fmt( & result, " // %S", ast->InlineCmt->Content ); + append_fmt( result, " // %S", ast->InlineCmt->Content ); } - append_fmt( & result, "\n{\n%S\n}", ast->Body.to_string() ); + append_fmt( result, "\n{\n%S\n}", GEN_NS to_string(ast->Body) ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( & result, ";\n"); + append( result, ";\n"); } -void CodeClass::to_string_fwd( String& result ) +void to_string_fwd( CodeClass self, String* result ) { + GEN_ASSERT(self.ast != nullptr); + AST_Class* ast = self.ast; + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + append( result, "export " ); if ( ast->Attributes ) - append_fmt( & result, "class %S %S", ast->Attributes.to_string(), ast->Name ); + append_fmt( result, "class %S %S", ast->Attributes.to_string(), ast->Name ); - else append_fmt( & result, "class %S", ast->Name ); + else append_fmt( result, "class %S", ast->Name ); // Check if it can have an end-statement if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - append_fmt( & result, "; // %S\n", ast->InlineCmt->Content ); + append_fmt( result, "; // %S\n", ast->InlineCmt->Content ); else - append( & result,";\n"); + append( result,";\n"); } } @@ -328,18 +339,18 @@ void CodeEnum::to_string_def( String& result ) append_fmt( & result, "%S : %S\n{\n%S\n}" , ast->Name , ast->UnderlyingType.to_string() - , ast->Body.to_string() + , GEN_NS to_string(ast->Body) ); else if ( ast->UnderlyingTypeMacro ) append_fmt( & result, "%S : %S\n{\n%S\n}" , ast->Name , GEN_NS to_string(ast->UnderlyingTypeMacro) - , ast->Body.to_string() + , GEN_NS to_string(ast->Body) ); - else append_fmt( & result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); + else append_fmt( & result, "%S\n{\n%S\n}", ast->Name, GEN_NS to_string(ast->Body) ); } - else append_fmt( & result, "enum %S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); + else append_fmt( & result, "enum %S\n{\n%S\n}", ast->Name, GEN_NS to_string(ast->Body) ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) append( & result, ";\n"); @@ -383,16 +394,16 @@ void CodeEnum::to_string_class_def( String& result ) if ( ast->UnderlyingType ) { - append_fmt( & result, "%S : %S\n{\n%S\n}", ast->Name, ast->UnderlyingType.to_string(), ast->Body.to_string() ); + append_fmt( & result, "%S : %S\n{\n%S\n}", ast->Name, ast->UnderlyingType.to_string(), GEN_NS to_string(ast->Body) ); } else { - append_fmt( & result, "%S\n{\n%S\n}", ast->Name, ast->Body.to_string() ); + append_fmt( & result, "%S\n{\n%S\n}", ast->Name, GEN_NS to_string(ast->Body) ); } } else { - append_fmt( & result, "enum class %S\n{\n%S\n}", ast->Body.to_string() ); + append_fmt( & result, "enum class %S\n{\n%S\n}", GEN_NS to_string(ast->Body) ); } if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) @@ -428,7 +439,7 @@ String CodeExec::to_string() void CodeExtern::to_string( String& result ) { if ( ast->Body ) - append_fmt( & result, "extern \"%S\"\n{\n%S\n}\n", ast->Name, ast->Body.to_string() ); + append_fmt( & result, "extern \"%S\"\n{\n%S\n}\n", ast->Name, GEN_NS to_string(ast->Body) ); else append_fmt( & result, "extern \"%S\"\n{}\n", ast->Name ); } @@ -531,7 +542,7 @@ void CodeFn::to_string_def( String& result ) } } - append_fmt( & result, "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}\n", GEN_NS to_string(ast->Body) ); } void CodeFn::to_string_fwd( String& result ) @@ -589,7 +600,7 @@ void CodeFn::to_string_fwd( String& result ) if ( ast->Specs && ast->Specs.has( ESpecifier::Pure ) >= 0 ) append( & result, " = 0;" ); else if (ast->Body) - append_fmt( & result, " = %S;", ast->Body.to_string() ); + append_fmt( & result, " = %S;", GEN_NS to_string(ast->Body) ); if ( ast->InlineCmt ) append_fmt( & result, "; %S", ast->InlineCmt->Content ); @@ -627,7 +638,7 @@ void CodeNS::to_string( String& result ) if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) append( & result, "export " ); - append_fmt( & result, "namespace %S\n{\n%S\n}\n", ast->Name , ast->Body.to_string() ); + append_fmt( & result, "namespace %S\n{\n%S\n}\n", ast->Name , GEN_NS to_string(ast->Body) ); } String CodeOperator::to_string() @@ -698,7 +709,7 @@ void CodeOperator::to_string_def( String& result ) } append_fmt( & result, "\n{\n%S\n}\n" - , ast->Body.to_string() + , GEN_NS to_string(ast->Body) ); } @@ -796,14 +807,14 @@ void CodeOpCast::to_string_def( String& result ) } } - append_fmt( & result, "\n{\n%S\n}\n", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}\n", GEN_NS to_string(ast->Body) ); return; } if ( ast->Name && length(ast->Name) ) - append_fmt( & result, "%Soperator %S()\n{\n%S\n}\n", ast->Name, ast->ValueType.to_string(), ast->Body.to_string() ); + append_fmt( & result, "%Soperator %S()\n{\n%S\n}\n", ast->Name, ast->ValueType.to_string(), GEN_NS to_string(ast->Body) ); else - append_fmt( & result, "operator %S()\n{\n%S\n}\n", ast->ValueType.to_string(), ast->Body.to_string() ); + append_fmt( & result, "operator %S()\n{\n%S\n}\n", ast->ValueType.to_string(), GEN_NS to_string(ast->Body) ); } void CodeOpCast::to_string_fwd( String& result ) @@ -1030,7 +1041,7 @@ void CodeStruct::to_string_def( String& result ) append_fmt( & result, " // %S", ast->InlineCmt->Content ); } - append_fmt( & result, "\n{\n%S\n}", ast->Body.to_string() ); + append_fmt( & result, "\n{\n%S\n}", GEN_NS to_string(ast->Body) ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) append( & result, ";\n"); @@ -1189,14 +1200,14 @@ void CodeUnion::to_string( String& result ) { append_fmt( & result, "%S\n{\n%S\n}" , ast->Name - , ast->Body.to_string() + , GEN_NS to_string(ast->Body) ); } else { // Anonymous union append_fmt( & result, "\n{\n%S\n}" - , ast->Body.to_string() + , GEN_NS to_string(ast->Body) ); } diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index a582b62..ff915c5 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -3,6 +3,17 @@ #include "ast.hpp" #endif +void append ( CodeBody body, Code other ); +void append ( CodeBody body, CodeBody other ); +String to_string ( CodeBody body ); +void to_string ( CodeBody body, String* result ); +void to_string_export ( CodeBody body, String* result ); + +void add_interface( CodeClass self, CodeType interface ); +String to_string ( CodeClass self ); +void to_string_def( CodeClass self, String* result ); +void to_string_fwd( CodeClass self, String* result ); + #pragma region Code Types // These structs are not used at all by the C vairant. #if ! GEN_COMPILER_C @@ -10,31 +21,16 @@ struct CodeBody { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeBody ); - void append( Code other ) - { - GEN_ASSERT(other.ast != nullptr); + void append( Code other ) { return GEN_NS append( *this, other ); } + void append( CodeBody body ) { return GEN_NS append(*this, body); } + bool has_entries() { return GEN_NS has_entries(rcast( AST*, ast )); } - if (GEN_NS is_body(other)) { - append( cast(CodeBody, other) ); - } - - GEN_NS append( raw(), other.ast ); - } - void append( CodeBody body ) - { - for ( Code entry : body ) { - append( entry ); - } - } - bool has_entries() { return GEN_NS has_entries(rcast( AST*, ast )); } - AST* raw() { return rcast( AST*, ast ); } - - String to_string(); - void to_string( String& result ); - void to_string_export( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result ); } + void to_string_export( String& result ) { return GEN_NS to_string_export(* this, & result); } #endif Using_CodeOps( CodeBody ); @@ -59,7 +55,7 @@ struct CodeBody struct CodeClass { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 0 Using_Code( CodeClass ); void add_interface( CodeType interface ); @@ -67,8 +63,6 @@ struct CodeClass String to_string(); void to_string_def( String& result ); void to_string_fwd( String& result ); - - AST* raw() { return rcast( AST*, ast ); } #endif Using_CodeOps( CodeClass ); @@ -1054,5 +1048,9 @@ struct CodeVar #undef Using_Code #undef Using_CodeOps +#if GEN_SUPPORT_CPP_REFERENCES +void to_string_export( CodeBody body, String& result ) { return to_string_export(body, & result); }; +#endif + #endif //if ! GEN_COMPILER_C #pragma endregion Code Types diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index ce99931..1258f2f 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -163,13 +163,35 @@ Code& Code::operator ++() #pragma endregion Code inline -void CodeClass::add_interface( CodeType type ) +void append( CodeBody self, Code other ) { - CodeType possible_slot = ast->ParentType; + GEN_ASSERT(other.ast != nullptr); + + if (is_body(other)) { + append( self, cast(CodeBody, other) ); + return; + } + + append( rcast(AST*, self.ast), other.ast ); +} + +inline +void append( CodeBody self, CodeBody body ) +{ + for ( Code entry : body ) { + append( self, entry ); + } +} + +inline +void add_interface( CodeClass self, CodeType type ) +{ + GEN_ASSERT(self.ast !=nullptr); + CodeType possible_slot = self->ParentType; if ( possible_slot.ast ) { // Were adding an interface to parent type, so we need to make sure the parent type is public. - ast->ParentAccess = AccessSpec_Public; + self->ParentAccess = AccessSpec_Public; // If your planning on adding a proper parent, // then you'll need to move this over to ParentType->next and update ParentAccess accordingly. } diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index 5f9ce5e..cc5b7a0 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -605,7 +605,7 @@ CodeClass def_class( StrC name { for (s32 idx = 0; idx < num_interfaces; idx++ ) { - result.add_interface( interfaces[idx] ); + add_interface(result, interfaces[idx] ); } } @@ -1565,7 +1565,7 @@ CodeBody def_class_body( s32 num, ... ) break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); va_end(va); @@ -1602,7 +1602,7 @@ CodeBody def_class_body( s32 num, Code* codes ) break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); @@ -1636,7 +1636,7 @@ CodeBody def_enum_body( s32 num, ... ) return InvalidCode; } - result.append( entry ); + append(result, entry ); } while ( num--, num > 0 ); va_end(va); @@ -1668,7 +1668,7 @@ CodeBody def_enum_body( s32 num, Code* codes ) return InvalidCode; } - result.append( entry ); + append(result, entry ); } while ( codes++, num--, num > 0 ); @@ -1706,7 +1706,7 @@ CodeBody def_export_body( s32 num, ... ) break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); va_end(va); @@ -1743,7 +1743,7 @@ CodeBody def_export_body( s32 num, Code* codes ) break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); @@ -1781,7 +1781,7 @@ CodeBody def_extern_link_body( s32 num, ... ) break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); va_end(va); @@ -1818,7 +1818,7 @@ CodeBody def_extern_link_body( s32 num, Code* codes ) break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); @@ -1858,7 +1858,7 @@ CodeBody def_function_body( s32 num, ... ) break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); va_end(va); @@ -1894,7 +1894,7 @@ CodeBody def_function_body( s32 num, Code* codes ) default: break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); @@ -1926,7 +1926,7 @@ CodeBody def_global_body( s32 num, ... ) { case Global_Body: // result.append( entry.code_cast() ) ; - result.append( cast(CodeBody, entry) ) ; + append( result, cast(CodeBody, entry) ); continue; GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES @@ -1937,7 +1937,7 @@ CodeBody def_global_body( s32 num, ... ) break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); va_end(va); @@ -1967,7 +1967,7 @@ CodeBody def_global_body( s32 num, Code* codes ) switch (entry->Type) { case Global_Body: - result.append( cast(CodeBody, entry) ); + append(result, cast(CodeBody, entry) ); continue; GEN_AST_BODY_GLOBAL_UNALLOWED_TYPES @@ -1978,7 +1978,7 @@ CodeBody def_global_body( s32 num, Code* codes ) break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); @@ -2016,7 +2016,7 @@ CodeBody def_namespace_body( s32 num, ... ) break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); va_end(va); @@ -2052,7 +2052,7 @@ CodeBody def_namespace_body( s32 num, Code* codes ) default: break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); @@ -2225,7 +2225,7 @@ CodeBody def_struct_body( s32 num, ... ) break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); va_end(va); @@ -2262,7 +2262,7 @@ CodeBody def_struct_body( s32 num, Code* codes ) break; } - result.append(entry); + append(result, entry); } while (num--, num > 0); @@ -2296,7 +2296,7 @@ CodeBody def_union_body( s32 num, ... ) return InvalidCode; } - result.append( entry ); + append(result, entry ); } while ( num--, num > 0 ); va_end(va); @@ -2328,7 +2328,7 @@ CodeBody def_union_body( s32 num, CodeUnion* codes ) return InvalidCode; } - result.append( entry ); + append(result, entry ); } while ( codes++, num--, num > 0 ); diff --git a/project/components/parser.cpp b/project/components/parser.cpp index c024bc2..2a98575 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -1115,7 +1115,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) return InvalidCode; } - result.append( member ); + append(result, member ); } eat( TokType::BraceCurly_Close ); @@ -1503,7 +1503,7 @@ CodeFn parse_function_after_name( default: { - log_failure("Body must be either of Function_Body or Untyped type, %s\n%s", body.debug_str(), Context.to_string()); + log_failure("Body must be either of Function_Body or Untyped type, %s\n%s", debug_str(body), Context.to_string()); Context.pop(); return InvalidCode; } @@ -1568,7 +1568,7 @@ Code parse_function_body() if ( len > 0 ) { - result.append( def_execution( { len, start.Text } ) ); + append( result, def_execution( { len, start.Text } ) ); } eat( TokType::BraceCurly_Close ); @@ -1878,7 +1878,7 @@ CodeBody parse_global_nspace( CodeT which ) } // log_fmt("Global Body Member: %s", member->debug_str()); - result.append( member ); + append(result, member ); } if ( which != Global_Body ) @@ -3743,7 +3743,7 @@ CodeEnum parse_enum( bool inplace_def ) return InvalidCode; } - body.append( member ); + append(body, member ); } eat( TokType::BraceCurly_Close ); @@ -5187,7 +5187,7 @@ CodeUnion parse_union( bool inplace_def ) } if ( member ) - body.append( member ); + append(body, member ); } // union { diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 5baa9e8..9e61baa 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -428,29 +428,29 @@ CodeBody gen_ast_inlines() CodeBody impl_code_using = parse_global_body( token_fmt( "typename", StrC name(CodeUsing), code_impl_tmpl )); CodeBody impl_code_var = parse_global_body( token_fmt( "typename", StrC name(CodeVar), code_impl_tmpl )); - impl_code_attr. append( parse_global_body( token_fmt( "typename", StrC name(Attributes), codetype_impl_tmpl ))); - impl_code_cmt. append( parse_global_body( token_fmt( "typename", StrC name(Comment), codetype_impl_tmpl ))); - impl_code_constr. append( parse_global_body( token_fmt( "typename", StrC name(Constructor), codetype_impl_tmpl ))); - impl_code_define. append( parse_global_body( token_fmt( "typename", StrC name(Define), codetype_impl_tmpl ))); - impl_code_destruct.append( parse_global_body( token_fmt( "typename", StrC name(Destructor), codetype_impl_tmpl ))); - impl_code_enum. append( parse_global_body( token_fmt( "typename", StrC name(Enum), codetype_impl_tmpl ))); - impl_code_exec. append( parse_global_body( token_fmt( "typename", StrC name(Exec), codetype_impl_tmpl ))); - impl_code_extern. append( parse_global_body( token_fmt( "typename", StrC name(Extern), codetype_impl_tmpl ))); - impl_code_include. append( parse_global_body( token_fmt( "typename", StrC name(Include), codetype_impl_tmpl ))); - impl_code_friend. append( parse_global_body( token_fmt( "typename", StrC name(Friend), codetype_impl_tmpl ))); - impl_code_fn. append( parse_global_body( token_fmt( "typename", StrC name(Fn), codetype_impl_tmpl ))); - impl_code_module. append( parse_global_body( token_fmt( "typename", StrC name(Module), codetype_impl_tmpl ))); - impl_code_ns. append( parse_global_body( token_fmt( "typename", StrC name(NS), codetype_impl_tmpl ))); - impl_code_op. append( parse_global_body( token_fmt( "typename", StrC name(Operator), codetype_impl_tmpl ))); - impl_code_opcast. append( parse_global_body( token_fmt( "typename", StrC name(OpCast), codetype_impl_tmpl ))); - impl_code_pragma . append( parse_global_body( token_fmt( "typename", StrC name(Pragma), codetype_impl_tmpl ))); - impl_code_precond. append( parse_global_body( token_fmt( "typename", StrC name(PreprocessCond), codetype_impl_tmpl ))); - impl_code_tmpl. append( parse_global_body( token_fmt( "typename", StrC name(Template), codetype_impl_tmpl ))); - impl_code_type. append( parse_global_body( token_fmt( "typename", StrC name(Type), codetype_impl_tmpl ))); - impl_code_typedef. append( parse_global_body( token_fmt( "typename", StrC name(Typedef), codetype_impl_tmpl ))); - impl_code_union. append( parse_global_body( token_fmt( "typename", StrC name(Union), codetype_impl_tmpl ))); - impl_code_using. append( parse_global_body( token_fmt( "typename", StrC name(Using), codetype_impl_tmpl ))); - impl_code_var. append( parse_global_body( token_fmt( "typename", StrC name(Var), codetype_impl_tmpl ))); + append(impl_code_attr, parse_global_body( token_fmt( "typename", StrC name(Attributes), codetype_impl_tmpl ))); + append(impl_code_cmt, parse_global_body( token_fmt( "typename", StrC name(Comment), codetype_impl_tmpl ))); + append(impl_code_constr, parse_global_body( token_fmt( "typename", StrC name(Constructor), codetype_impl_tmpl ))); + append(impl_code_define, parse_global_body( token_fmt( "typename", StrC name(Define), codetype_impl_tmpl ))); + append(impl_code_destruct, parse_global_body( token_fmt( "typename", StrC name(Destructor), codetype_impl_tmpl ))); + append(impl_code_enum, parse_global_body( token_fmt( "typename", StrC name(Enum), codetype_impl_tmpl ))); + append(impl_code_exec, parse_global_body( token_fmt( "typename", StrC name(Exec), codetype_impl_tmpl ))); + append(impl_code_extern, parse_global_body( token_fmt( "typename", StrC name(Extern), codetype_impl_tmpl ))); + append(impl_code_include, parse_global_body( token_fmt( "typename", StrC name(Include), codetype_impl_tmpl ))); + append(impl_code_friend, parse_global_body( token_fmt( "typename", StrC name(Friend), codetype_impl_tmpl ))); + append(impl_code_fn, parse_global_body( token_fmt( "typename", StrC name(Fn), codetype_impl_tmpl ))); + append(impl_code_module, parse_global_body( token_fmt( "typename", StrC name(Module), codetype_impl_tmpl ))); + append(impl_code_ns, parse_global_body( token_fmt( "typename", StrC name(NS), codetype_impl_tmpl ))); + append(impl_code_op, parse_global_body( token_fmt( "typename", StrC name(Operator), codetype_impl_tmpl ))); + append(impl_code_opcast, parse_global_body( token_fmt( "typename", StrC name(OpCast), codetype_impl_tmpl ))); + append(impl_code_pragma, parse_global_body( token_fmt( "typename", StrC name(Pragma), codetype_impl_tmpl ))); + append(impl_code_precond, parse_global_body( token_fmt( "typename", StrC name(PreprocessCond), codetype_impl_tmpl ))); + append(impl_code_tmpl, parse_global_body( token_fmt( "typename", StrC name(Template), codetype_impl_tmpl ))); + append(impl_code_type, parse_global_body( token_fmt( "typename", StrC name(Type), codetype_impl_tmpl ))); + append(impl_code_typedef, parse_global_body( token_fmt( "typename", StrC name(Typedef), codetype_impl_tmpl ))); + append(impl_code_union, parse_global_body( token_fmt( "typename", StrC name(Union), codetype_impl_tmpl ))); + append(impl_code_using, parse_global_body( token_fmt( "typename", StrC name(Using), codetype_impl_tmpl ))); + append(impl_code_var, parse_global_body( token_fmt( "typename", StrC name(Var), codetype_impl_tmpl ))); char const* cast_tmpl = stringize( inline AST::operator Code() From ea187923733b85c7fbda96528bca8b033d3ec454 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 10:58:24 -0500 Subject: [PATCH 039/112] Progress on member proc usage reduction (CodeParam, CodeSpecifiers) --- project/components/ast.cpp | 4 +- project/components/code_serialization.cpp | 67 ++++---- project/components/code_types.hpp | 184 ++++++---------------- project/components/inlines.hpp | 136 +++++++++++++--- project/components/interface.cpp | 4 +- project/components/interface.upfront.cpp | 58 +++---- project/components/parser.cpp | 20 +-- project/dependencies/strings.hpp | 6 +- 8 files changed, 249 insertions(+), 230 deletions(-) diff --git a/project/components/ast.cpp b/project/components/ast.cpp index 4950e37..9e713e6 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -501,7 +501,7 @@ void to_string( AST* self, String* result ) break; case Parameters: - cast(CodeParam, {self}).to_string( * result ); + to_string(cast(CodeParam, {self}), result ); break; case Preprocess_Define: @@ -541,7 +541,7 @@ void to_string( AST* self, String* result ) break; case Specifiers: - cast(CodeSpecifiers, {self}).to_string( * result ); + to_string(cast(CodeSpecifiers, {self}), result ); break; case Struct: diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 3ccc61f..9476c9a 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -110,7 +110,7 @@ void CodeConstructor::to_string_def( String& result ) } if ( ast->Params ) - append_fmt( & result, "( %S )", ast->Params.to_string() ); + append_fmt( & result, "( %S )", GEN_NS to_string(ast->Params) ); else append( & result, "()" ); @@ -134,7 +134,7 @@ void CodeConstructor::to_string_fwd( String& result ) } if ( ast->Params ) - append_fmt( & result, "( %S )", ast->Params.to_string() ); + append_fmt( & result, "( %S )", GEN_NS to_string(ast->Params) ); else append_fmt( & result, "()"); @@ -267,7 +267,7 @@ void CodeDestructor::to_string_def( String& result ) } else if ( ast->Specs ) { - if ( ast->Specs.has( ESpecifier::Virtual ) ) + if ( has(ast->Specs, ESpecifier::Virtual ) ) append_fmt( & result, "virtual ~%S()", ast->Parent->Name ); else append_fmt( & result, "~%S()", ast->Parent->Name ); @@ -282,12 +282,12 @@ void CodeDestructor::to_string_fwd( String& result ) { if ( ast->Specs ) { - if ( ast->Specs.has( ESpecifier::Virtual ) ) + if ( has(ast->Specs, ESpecifier::Virtual ) ) append_fmt( & result, "virtual ~%S();\n", ast->Parent->Name ); else append_fmt( & result, "~%S()", ast->Parent->Name ); - if ( ast->Specs.has( ESpecifier::Pure ) ) + if ( has(ast->Specs, ESpecifier::Pure ) ) append( & result, " = 0;" ); else if (ast->Body) append_fmt( & result, " = %S;", GEN_NS to_string(ast->Body) ); @@ -525,7 +525,7 @@ void CodeFn::to_string_def( String& result ) append_fmt( & result, "%S(", ast->Name ); if ( ast->Params ) - append_fmt( & result, "%S)", ast->Params.to_string() ); + append_fmt( & result, "%S)", GEN_NS to_string(ast->Params) ); else append( & result, ")" ); @@ -580,7 +580,7 @@ void CodeFn::to_string_fwd( String& result ) append_fmt( & result, "%S(", ast->Name ); if ( ast->Params ) - append_fmt( & result, "%S)", ast->Params.to_string() ); + append_fmt( & result, "%S)", GEN_NS to_string(ast->Params) ); else append( & result, ")" ); @@ -597,7 +597,7 @@ void CodeFn::to_string_fwd( String& result ) } } - if ( ast->Specs && ast->Specs.has( ESpecifier::Pure ) >= 0 ) + if ( ast->Specs && has(ast->Specs, ESpecifier::Pure ) >= 0 ) append( & result, " = 0;" ); else if (ast->Body) append_fmt( & result, " = %S;", GEN_NS to_string(ast->Body) ); @@ -691,7 +691,7 @@ void CodeOperator::to_string_def( String& result ) append_fmt( & result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); if ( ast->Params ) - append_fmt( & result, "%S)", ast->Params.to_string() ); + append_fmt( & result, "%S)", GEN_NS to_string(ast->Params) ); else append( & result, ")" ); @@ -741,7 +741,7 @@ void CodeOperator::to_string_fwd( String& result ) append_fmt( & result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); if ( ast->Params ) - append_fmt( & result, "%S)", ast->Params.to_string() ); + append_fmt( & result, "%S)", GEN_NS to_string(ast->Params) ); else append_fmt( & result, ")" ); @@ -854,46 +854,47 @@ void CodeOpCast::to_string_fwd( String& result ) append_fmt( & result, "operator %S();\n", ast->ValueType.to_string() ); } -String CodeParam::to_string() +String to_string(CodeParam self) { String result = string_make( GlobalAllocator, "" ); - to_string( result ); + to_string( self, & result ); return result; } -void CodeParam::to_string( String& result ) +void to_string( CodeParam self, String* result ) { + AST_Param* ast = self.ast; if ( ast->Macro ) { // Related to parsing: ( , ... ) - GEN_NS append( & result, ast->Macro.ast->Content ); + GEN_NS append( result, ast->Macro.ast->Content ); // Could also be: ( , ... ) } if ( ast->Name ) { if ( ast->ValueType.ast == nullptr ) - append_fmt( & result, " %S", ast->Name ); + append_fmt( result, " %S", ast->Name ); else - append_fmt( & result, " %S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( result, " %S %S", ast->ValueType.to_string(), ast->Name ); } else if ( ast->ValueType ) - append_fmt( & result, " %S", ast->ValueType.to_string() ); + append_fmt( result, " %S", ast->ValueType.to_string() ); if ( ast->PostNameMacro ) { - append_fmt( & result, " %S", GEN_NS to_string(ast->PostNameMacro) ); + append_fmt( result, " %S", GEN_NS to_string(ast->PostNameMacro) ); } if ( ast->Value ) - append_fmt( & result, " = %S", GEN_NS to_string(ast->Value) ); + append_fmt( result, " = %S", GEN_NS to_string(ast->Value) ); if ( ast->NumEntries - 1 > 0 ) { for ( CodeParam param : ast->Next ) { - append_fmt( & result, ", %S", param.to_string() ); + append_fmt( result, ", %S", to_string(param) ); } } } @@ -968,21 +969,23 @@ void CodePragma::to_string( String& result ) append_fmt( & result, "#pragma %S\n", ast->Content ); } -String CodeSpecifiers::to_string() +String to_string(CodeSpecifiers self) { String result = string_make( GlobalAllocator, "" ); - to_string( result ); + to_string( self, & result ); return result; } -void CodeSpecifiers::to_string( String& result ) +void to_string( CodeSpecifiers self, String* result ) { + GEN_ASSERT(self.ast != nullptr); + GEN_ASSERT(result != nullptr); s32 idx = 0; - s32 left = ast->NumEntries; + s32 left = self->NumEntries; while ( left-- ) { - StrC spec = ESpecifier::to_str( ast->ArrSpecs[idx] ); - append_fmt( & result, "%.*s ", spec.Len, spec.Ptr ); + StrC spec = ESpecifier::to_str( self->ArrSpecs[idx] ); + append_fmt( result, "%.*s ", spec.Len, spec.Ptr ); idx++; } } @@ -1158,9 +1161,9 @@ void CodeType::to_string( String& result ) else { if ( ast->Specs ) - append_fmt( & result, "%S %S ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); + append_fmt( & result, "%S %S ( %S ) %S", ast->ReturnType.to_string(), ast->Name, GEN_NS to_string(ast->Params), GEN_NS to_string(ast->Specs) ); else - append_fmt( & result, "%S %S ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); + append_fmt( & result, "%S %S ( %S )", ast->ReturnType.to_string(), ast->Name, GEN_NS to_string(ast->Params) ); } return; @@ -1171,7 +1174,7 @@ void CodeType::to_string( String& result ) append_fmt( & result, "%S ", ast->Attributes.to_string() ); if ( ast->Specs ) - append_fmt( & result, "%S %S", ast->Name, ast->Specs.to_string() ); + append_fmt( & result, "%S %S", ast->Name, GEN_NS to_string(ast->Specs) ); else append_fmt( & result, "%S", ast->Name ); @@ -1288,7 +1291,7 @@ void CodeVar::to_string( String& result ) // Its a comma-separated variable ( a NextVar ) if ( ast->Specs ) - append_fmt( & result, "%S ", ast->Specs.to_string() ); + append_fmt( & result, "%S ", GEN_NS to_string(ast->Specs) ); append( & result, ast->Name ); @@ -1328,10 +1331,10 @@ void CodeVar::to_string( String& result ) if ( ast->Attributes || ast->Specs ) { if ( ast->Attributes ) - append_fmt( & result, "%S ", ast->Specs.to_string() ); + append_fmt( & result, "%S ", GEN_NS to_string(ast->Specs) ); if ( ast->Specs ) - append_fmt( & result, "%S\n", ast->Specs.to_string() ); + append_fmt( & result, "%S\n", GEN_NS to_string(ast->Specs) ); append_fmt( & result, "%S %S", ast->ValueType.to_string(), ast->Name ); diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index ff915c5..ca4a80e 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -9,11 +9,32 @@ String to_string ( CodeBody body ); void to_string ( CodeBody body, String* result ); void to_string_export ( CodeBody body, String* result ); +Code begin( CodeBody body); +Code end ( CodeBody body ); + void add_interface( CodeClass self, CodeType interface ); String to_string ( CodeClass self ); void to_string_def( CodeClass self, String* result ); void to_string_fwd( CodeClass self, String* result ); +void append (CodeParam params, CodeParam param ); +CodeParam get (CodeParam params, s32 idx); +bool has_entries(CodeParam params ); +String to_string (CodeParam params ); +void to_string (CodeParam params, String* result ); + +CodeParam begin(CodeParam params); +CodeParam end (CodeParam params); + +bool append (CodeSpecifiers specifiers, SpecifierT spec); +s32 has (CodeSpecifiers specifiers, SpecifierT spec); +s32 remove (CodeSpecifiers specifiers, SpecifierT to_remove ); +String to_string(CodeSpecifiers specifiers); +void to_string(CodeSpecifiers specifiers, String* result); + +SpecifierT* begin( CodeSpecifiers specifiers ); +SpecifierT* end (CodeSpecifiers specifiers); + #pragma region Code Types // These structs are not used at all by the C vairant. #if ! GEN_COMPILER_C @@ -31,31 +52,21 @@ struct CodeBody String to_string() { return GEN_NS to_string(* this); } void to_string( String& result ) { return GEN_NS to_string(* this, & result ); } void to_string_export( String& result ) { return GEN_NS to_string_export(* this, & result); } + + Code begin() { return GEN_NS begin(* this); } + Code end() { return GEN_NS end(* this); } #endif Using_CodeOps( CodeBody ); operator Code() { return * rcast( Code*, this ); } AST_Body* operator->() { return ast; } -#pragma region Iterator - Code begin() - { - if ( ast ) - return { rcast( AST*, ast)->Front }; - return { nullptr }; - } - Code end() - { - return { rcast(AST*, ast)->Back->Next }; - } -#pragma endregion Iterator - AST_Body* ast; }; struct CodeClass { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 0 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeClass ); void add_interface( CodeType interface ); @@ -81,7 +92,7 @@ struct CodeClass struct CodeParam { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeParam ); void append( CodeParam other ); @@ -92,10 +103,6 @@ struct CodeParam #endif Using_CodeOps( CodeParam ); - AST* raw() - { - return rcast( AST*, ast ); - } AST_Param* operator->() { if ( ast == nullptr ) @@ -105,115 +112,27 @@ struct CodeParam } return ast; } - operator Code() - { - return { (AST*)ast }; - } -#pragma region Iterator - CodeParam begin() - { - if ( ast ) - return { ast }; - - return { nullptr }; - } - CodeParam end() - { - // return { (AST_Param*) rcast( AST*, ast)->Last }; - return { nullptr }; - } + operator Code() { return { (AST*)ast }; } + CodeParam operator*() { return * this; } CodeParam& operator++(); - CodeParam operator*() - { - return * this; - } -#pragma endregion Iterator AST_Param* ast; }; struct CodeSpecifiers { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeSpecifiers ); - bool append( SpecifierT spec ) - { - if ( ast == nullptr ) - { - log_failure("CodeSpecifiers: Attempted to append to a null specifiers AST!"); - return false; - } - - if ( raw()->NumEntries == AST_ArrSpecs_Cap ) - { - log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST_ArrSpecs_Cap ); - return false; - } - - raw()->ArrSpecs[ raw()->NumEntries ] = spec; - raw()->NumEntries++; - return true; - } - s32 has( SpecifierT spec ) - { - for ( s32 idx = 0; idx < raw()->NumEntries; idx++ ) - { - if ( raw()->ArrSpecs[ idx ] == spec ) - return idx; - } - - return -1; - } - s32 remove( SpecifierT to_remove ) - { - if ( ast == nullptr ) - { - log_failure("CodeSpecifiers: Attempted to append to a null specifiers AST!"); - return -1; - } - - if ( raw()->NumEntries == AST_ArrSpecs_Cap ) - { - log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST_ArrSpecs_Cap ); - return -1; - } - - s32 result = -1; - - s32 curr = 0; - s32 next = 0; - for(; next < raw()->NumEntries; ++ curr, ++ next) - { - SpecifierT spec = raw()->ArrSpecs[next]; - if (spec == to_remove) - { - result = next; - - next ++; - if (next >= raw()->NumEntries) - break; - - spec = raw()->ArrSpecs[next]; - } - - raw()->ArrSpecs[ curr ] = spec; - } - - if (result > -1) { - raw()->NumEntries --; - } - return result; - } - String to_string(); - void to_string( String& result ); + bool append( SpecifierT spec ) { return GEN_NS append(* this, spec); } + s32 has( SpecifierT spec ) { return GEN_NS has(* this, spec); } + s32 remove( SpecifierT to_remove ) { return GEN_NS remove(* this, to_remove); } + String to_string() { return GEN_NS to_string(* this ); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps(CodeSpecifiers); - AST* raw() - { - return rcast( AST*, ast ); - } + operator Code() { return { (AST*) ast }; } AST_Specifiers* operator->() { if ( ast == nullptr ) @@ -223,23 +142,6 @@ struct CodeSpecifiers } return ast; } - operator Code() - { - return { (AST*) ast }; - } -#pragma region Iterator - SpecifierT* begin() - { - if ( ast ) - return & raw()->ArrSpecs[0]; - - return nullptr; - } - SpecifierT* end() - { - return raw()->ArrSpecs + raw()->NumEntries; - } -#pragma endregion Iterator AST_Specifiers* ast; }; @@ -1053,4 +955,20 @@ void to_string_export( CodeBody body, String& result ) { return to_string_export #endif #endif //if ! GEN_COMPILER_C + +inline +CodeParam begin(CodeParam params) +{ + if ( params.ast ) + return { params.ast }; + + return { nullptr }; +} +inline +CodeParam end(CodeParam params) +{ + // return { (AST_Param*) rcast( AST*, ast)->Last }; + return { nullptr }; +} + #pragma endregion Code Types diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index 1258f2f..7f0e8eb 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -89,7 +89,6 @@ AST::operator Code() } #pragma region Code - inline char const* debug_str( Code code ) { @@ -98,7 +97,6 @@ char const* debug_str( Code code ) return debug_str( code.ast ); } - inline Code duplicate( Code code ) { @@ -110,7 +108,6 @@ Code duplicate( Code code ) return { duplicate(code.ast) }; } - inline bool is_body(Code code) { @@ -120,7 +117,6 @@ bool is_body(Code code) } return false; } - inline bool is_equal( Code self, Code other ) { @@ -132,13 +128,11 @@ bool is_equal( Code self, Code other ) } return is_equal( self.ast, other.ast ); } - inline bool is_valid(Code self) { return self.ast != nullptr && self.ast->Type != CodeT::Invalid; } - inline void set_global(Code self) { @@ -150,7 +144,6 @@ void set_global(Code self) self->Parent = Code_Global.ast; } - inline Code& Code::operator ++() { @@ -159,9 +152,9 @@ Code& Code::operator ++() return *this; } - #pragma endregion Code +#pragma region CodeBody inline void append( CodeBody self, Code other ) { @@ -174,15 +167,28 @@ void append( CodeBody self, Code other ) append( rcast(AST*, self.ast), other.ast ); } - inline void append( CodeBody self, CodeBody body ) { + GEN_ASSERT(self.ast != nullptr); + for ( Code entry : body ) { append( self, entry ); } } +inline +Code begin( CodeBody body) { + if ( body.ast ) + return { rcast( AST*, body.ast)->Front }; + return { nullptr }; +} +inline +Code end(CodeBody body ){ + return { rcast(AST*, body.ast)->Back->Next }; +} +#pragma endregion CodeBody +#pragma region CodeClass inline void add_interface( CodeClass self, CodeType type ) { @@ -203,11 +209,14 @@ void add_interface( CodeClass self, CodeType type ) possible_slot.ast = type.ast; } +#pragma endregion CodeClass +#pragma region CodeParam inline -void CodeParam::append( CodeParam other ) +void append( CodeParam appendee, CodeParam other ) { - AST* self = (AST*) ast; + GEN_ASSERT(appendee.ast != nullptr); + AST* self = cast(Code, appendee).ast; AST* entry = (AST*) other.ast; if ( entry->Parent ) @@ -227,36 +236,122 @@ void CodeParam::append( CodeParam other ) self->Last = entry; self->NumEntries++; } - inline -CodeParam CodeParam::get( s32 idx ) +CodeParam get(CodeParam self, s32 idx ) { - CodeParam param = *this; + GEN_ASSERT(self.ast != nullptr); + CodeParam param = * self; do { if ( ! ++ param ) return { nullptr }; - param = { (AST_Param*) param.raw()->Next }; + param = { (AST_Param*) cast(Code, param)->Next }; } while ( --idx ); return param; } - inline -bool CodeParam::has_entries() +bool has_entries(CodeParam self) { - return ast->NumEntries > 0; + GEN_ASSERT(self.ast != nullptr); + return self->NumEntries > 0; } - inline CodeParam& CodeParam::operator ++() { ast = ast->Next.ast; return * this; } +#pragma endregion CodeParam +#pragma region CodeSpecifiers +inline +bool append(CodeSpecifiers self, SpecifierT spec ) +{ + if ( self.ast == nullptr ) + { + log_failure("CodeSpecifiers: Attempted to append to a null specifiers AST!"); + return false; + } + if ( self->NumEntries == AST_ArrSpecs_Cap ) + { + log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST_ArrSpecs_Cap ); + return false; + } + + self->ArrSpecs[ self->NumEntries ] = spec; + self->NumEntries++; + return true; +} +inline +s32 has(CodeSpecifiers self, SpecifierT spec) +{ + GEN_ASSERT(self.ast != nullptr); + for ( s32 idx = 0; idx < self->NumEntries; idx++ ) { + if ( self->ArrSpecs[ idx ] == spec ) + return idx; + } + return -1; +} +inline +s32 remove( CodeSpecifiers self, SpecifierT to_remove ) +{ + AST_Specifiers* ast = self.ast; + if ( ast == nullptr ) + { + log_failure("CodeSpecifiers: Attempted to append to a null specifiers AST!"); + return -1; + } + if ( self->NumEntries == AST_ArrSpecs_Cap ) + { + log_failure("CodeSpecifiers: Attempted to append over %d specifiers to a specifiers AST!", AST_ArrSpecs_Cap ); + return -1; + } + + s32 result = -1; + + s32 curr = 0; + s32 next = 0; + for(; next < self->NumEntries; ++ curr, ++ next) + { + SpecifierT spec = self->ArrSpecs[next]; + if (spec == to_remove) + { + result = next; + + next ++; + if (next >= self->NumEntries) + break; + + spec = self->ArrSpecs[next]; + } + + self->ArrSpecs[ curr ] = spec; + } + + if (result > -1) { + self->NumEntries --; + } + return result; +} +inline +SpecifierT* begin(CodeSpecifiers self) +{ + if ( self.ast ) + return & self->ArrSpecs[0]; + + return nullptr; +} +inline +SpecifierT* end(CodeSpecifiers self) +{ + return self->ArrSpecs + self->NumEntries; +} +#pragma endregion CodeSpecifiers + +#pragma region CodeStruct inline void CodeStruct::add_interface( CodeType type ) { @@ -276,7 +371,9 @@ void CodeStruct::add_interface( CodeType type ) possible_slot.ast = type.ast; } +#pragma endregion Code +#pragma region Interface inline CodeBody def_body( CodeT type ) { @@ -319,3 +416,4 @@ StrC token_fmt_impl( ssize num, ... ) return { result, buf }; } +#pragma endregion Interface diff --git a/project/components/interface.cpp b/project/components/interface.cpp index f709dc4..63b9917 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -180,7 +180,7 @@ void define_constants() # define def_constant_spec( Type_, ... ) \ spec_##Type_ = def_specifiers( num_args(__VA_ARGS__), __VA_ARGS__); \ - spec_##Type_.set_global(); + set_global(spec_##Type_); # pragma push_macro("forceinline") # pragma push_macro("global") @@ -218,7 +218,7 @@ void define_constants() def_constant_spec( volatile, ESpecifier::Volatile) spec_local_persist = def_specifiers( 1, ESpecifier::Local_Persist ); - spec_local_persist.set_global(); + set_global(spec_local_persist); # pragma pop_macro("forceinline") # pragma pop_macro("global") diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index cc5b7a0..f50b365 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -31,7 +31,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy } \ if ( params_code->Type != ECode::Parameters ) \ { \ - log_failure("gen::def_operator: params is not of Parameters type - %s", params_code.debug_str()); \ + log_failure("gen::def_operator: params is not of Parameters type - %s", debug_str(params_code)); \ return OpValidateResult::Fail; \ } @@ -42,7 +42,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy "param types: %s\n" \ "return type: %s", \ to_str(op).Ptr, \ - params_code.debug_str(), \ + debug_str(params_code), \ ret_type.debug_str() \ ); \ return OpValidateResult::Fail; \ @@ -73,7 +73,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy log_failure("gen::def_operator: " "operator%s does not support non-member definition (more than one parameter provided) - %s", to_str(op), - params_code.debug_str() + debug_str(params_code) ); return OpValidateResult::Fail; } @@ -104,7 +104,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy log_failure("gen::def_operator: operator%s may not be defined with more than two parametes - param count; %d\n%s" , to_str(op) , params_code->NumEntries - , params_code.debug_str() + , debug_str(params_code) ); return OpValidateResult::Fail; } @@ -119,7 +119,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy { log_failure("gen::def_operator: operator%s params code provided is not of Parameters type - %s" , to_str(op) - , params_code.debug_str() + , debug_str(params_code) ); return OpValidateResult::Fail; } @@ -137,7 +137,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy case 2: check_param_eq_ret(); - if ( ! params_code.get(1).is_equal( t_int ) ) + if ( ! is_equal(get(params_code, 1), t_int ) ) { log_failure("gen::def_operator: " "operator%s requires second parameter of non-member definition to be int for post-decrement", @@ -166,7 +166,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy { if ( params_code->Type != ECode::Parameters ) { - log_failure("gen::def_operator: params is not of Parameters type - %s", params_code.debug_str()); + log_failure("gen::def_operator: params is not of Parameters type - %s", debug_str(params_code)); return OpValidateResult::Fail; } @@ -176,7 +176,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy "operator%s is non-member symbol yet first paramter does not equal return type\n" "param type: %s\n" "return type: %s\n" - , params_code.debug_str() + , debug_str(params_code) , ret_type.debug_str() ); return OpValidateResult::Fail; @@ -199,7 +199,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy #if 0 if ( ! ret_type.is_equal( t_bool) ) { - log_failure( "gen::def_operator: return type is not a boolean - %s", params_code.debug_str() ); + log_failure( "gen::def_operator: return type is not a boolean - %s", debug_str(params_code) ); return OpValidateResult::Fail; } #endif @@ -211,7 +211,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy { if ( params_code->Type != ECode::Parameters ) { - log_failure( "gen::def_operator: params is not of Parameters type - %s", params_code.debug_str() ); + log_failure( "gen::def_operator: params is not of Parameters type - %s", debug_str(params_code) ); return OpValidateResult::Fail; } @@ -253,7 +253,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy "operator%s is non-member symbol yet first paramter does not equal return type\n" "param type: %s\n" "return type: %s\n" - , params_code.debug_str() + , debug_str(params_code) , ret_type.debug_str() ); return OpValidateResult::Fail; @@ -277,7 +277,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy { if ( params_code->Type != ECode::Parameters ) { - log_failure("gen::def_operator: params is not of Parameters type - %s", params_code.debug_str()); + log_failure("gen::def_operator: params is not of Parameters type - %s", debug_str(params_code)); return OpValidateResult::Fail; } @@ -349,7 +349,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy case PtrToMemOfPtr: if ( params_code ) { - log_failure("gen::def_operator: operator%s expects no paramters - %s", to_str(op), params_code.debug_str()); + log_failure("gen::def_operator: operator%s expects no paramters - %s", to_str(op), debug_str(params_code)); return OpValidateResult::Fail; } break; @@ -501,7 +501,7 @@ CodeConstructor def_constructor( CodeParam params, Code initializer_list, Code b if ( params && params->Type != Parameters ) { - log_failure("gen::def_constructor: params must be of Parameters type - %s", params.debug_str()); + log_failure("gen::def_constructor: params must be of Parameters type - %s", debug_str(params)); return InvalidCode; } @@ -647,7 +647,7 @@ CodeDestructor def_destructor( Code body, CodeSpecifiers specifiers ) if ( specifiers && specifiers->Type != Specifiers ) { - log_failure( "gen::def_destructor: specifiers was not a 'Specifiers' type: %s", specifiers.debug_str() ); + log_failure( "gen::def_destructor: specifiers was not a 'Specifiers' type: %s", debug_str(specifiers) ); return InvalidCode; } @@ -828,19 +828,19 @@ CodeFn def_function( StrC name if ( params && params->Type != Parameters ) { - log_failure( "gen::def_function: params was not a `Parameters` type: %s", params.debug_str() ); + log_failure( "gen::def_function: params was not a `Parameters` type: %s", debug_str(params) ); return InvalidCode; } if ( ret_type && ret_type->Type != Typename ) { - log_failure( "gen::def_function: ret_type was not a Typename: %s", ret_type.debug_str() ); + log_failure( "gen::def_function: ret_type was not a Typename: %s", debug_str(ret_type) ); return InvalidCode; } if ( specifiers && specifiers->Type != Specifiers ) { - log_failure( "gen::def_function: specifiers was not a `Specifiers` type: %s", specifiers.debug_str() ); + log_failure( "gen::def_function: specifiers was not a `Specifiers` type: %s", debug_str(specifiers) ); return InvalidCode; } @@ -973,7 +973,7 @@ CodeOperator def_operator( OperatorT op, StrC nspace if ( specifiers && specifiers->Type != Specifiers ) { - log_failure( "gen::def_operator: Specifiers was provided but its not of specifiers type: %s", specifiers.debug_str() ); + log_failure( "gen::def_operator: Specifiers was provided but its not of specifiers type: %s", debug_str(specifiers) ); return InvalidCode; } @@ -1167,7 +1167,7 @@ CodeSpecifiers def_specifier( SpecifierT spec ) CodeSpecifiers result = (CodeSpecifiers) make_code(); result->Type = ECode::Specifiers; - result.append( spec ); + append(result, spec ); return result; } @@ -1242,7 +1242,7 @@ CodeTemplate def_template( CodeParam params, Code declaration, ModuleFlag mflags if ( params && params->Type != ECode::Parameters ) { - log_failure( "gen::def_template: params is not of parameters type - %s", params.debug_str() ); + log_failure( "gen::def_template: params is not of parameters type - %s", debug_str(params) ); return InvalidCode; } @@ -1470,7 +1470,7 @@ CodeVar def_variable( CodeType type, StrC name, Code value if ( specifiers && specifiers->Type != ECode::Specifiers ) { - log_failure( "gen::def_variable: specifiers was not a `Specifiers` type - %s", specifiers.debug_str() ); + log_failure( "gen::def_variable: specifiers was not a `Specifiers` type - %s", debug_str(specifiers) ); return InvalidCode; } @@ -2077,7 +2077,7 @@ CodeParam def_params( s32 num, ... ) return InvalidCode; } - CodeParam result = (CodeParam) param.duplicate(); + CodeParam result = (CodeParam) duplicate(param); while ( -- num ) { @@ -2090,7 +2090,7 @@ CodeParam def_params( s32 num, ... ) return InvalidCode; } - result.append( param ); + append(result, param ); } va_end(va); @@ -2110,11 +2110,11 @@ CodeParam def_params( s32 num, CodeParam* codes ) \ if (current->Type != Parameters ) \ { \ - log_failure("gen::def_params: Code in coes array is not of paramter type - %s", current.debug_str() ); \ + log_failure("gen::def_params: Code in coes array is not of paramter type - %s", debug_str(current) ); \ return InvalidCode; \ } - CodeParam current = (CodeParam) codes->duplicate(); + CodeParam current = (CodeParam)duplicate(* codes); check_current(); CodeParam @@ -2126,7 +2126,7 @@ CodeParam def_params( s32 num, CodeParam* codes ) while( codes++, current = * codes, num--, num > 0 ) { check_current(); - result.append( current ); + append(result, current ); } # undef check_current @@ -2157,7 +2157,7 @@ CodeSpecifiers def_specifiers( s32 num, ... ) { SpecifierT type = (SpecifierT)va_arg(va, int); - result.append( type ); + append(result, type ); } while ( --num, num ); va_end(va); @@ -2186,7 +2186,7 @@ CodeSpecifiers def_specifiers( s32 num, SpecifierT* specs ) s32 idx = 0; do { - result.append( specs[idx] ); + append(result, specs[idx] ); idx++; } while ( --num, num ); diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 2a98575..7f76377 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -1440,7 +1440,7 @@ CodeFn parse_function_after_name( continue; } - specifiers.append( ESpecifier::to_type(currtok) ); + append(specifiers, ESpecifier::to_type(currtok) ); eat( currtok.Type ); } // ( ) @@ -1460,7 +1460,7 @@ CodeFn parse_function_after_name( else if ( check(TokType::Operator) && currtok.Text[0] == '=' ) { eat(TokType::Operator); - specifiers.append( ESpecifier::Pure ); + append(specifiers, ESpecifier::Pure ); eat( TokType::Number); Token stmt_end = currtok; @@ -2443,7 +2443,7 @@ CodeOperator parse_operator_after_ret_type( continue; } - specifiers.append( ESpecifier::to_type(currtok) ); + append(specifiers, ESpecifier::to_type(currtok) ); eat( currtok.Type ); } // operator ( ) @@ -2760,7 +2760,7 @@ CodeParam parse_params( bool use_template_capture ) if ( check( TokType::Varadic_Argument ) ) { eat( TokType::Varadic_Argument ); - result.append( param_varadic ); + append(result, param_varadic ); continue; // ( = , ... } @@ -2864,7 +2864,7 @@ CodeParam parse_params( bool use_template_capture ) if ( value ) param->Value = value; - result.append( param ); + append(result, param ); } if ( ! use_template_capture ) @@ -3288,7 +3288,7 @@ CodeVar parse_variable_declaration_list() "(Parser will add and continue to specifiers, but will most likely fail to compile)\n%s" , Context.to_string() ); - specifiers.append( spec ); + append(specifiers, spec ); } break; @@ -3310,7 +3310,7 @@ CodeVar parse_variable_declaration_list() // eat(currtok.Type); if ( specifiers ) - specifiers.append( spec ); + append(specifiers, spec ); else specifiers = def_specifier( spec ); } @@ -3450,7 +3450,7 @@ CodeDestructor parse_destructor( CodeSpecifiers specifiers ) if ( check( TokType::Spec_Virtual ) ) { if ( specifiers ) - specifiers.append( ESpecifier::Virtual ); + append(specifiers, ESpecifier::Virtual ); else specifiers = def_specifier( ESpecifier::Virtual ); eat( TokType::Spec_Virtual ); @@ -3493,7 +3493,7 @@ CodeDestructor parse_destructor( CodeSpecifiers specifiers ) eat( TokType::Number ); // ~() = 0 - specifiers.append( ESpecifier::Pure ); + append(specifiers, ESpecifier::Pure ); } else if ( left && str_compare( next.Text, "default", sizeof("default") - 1 ) == 0) { @@ -4148,7 +4148,7 @@ CodeOpCast parse_operator_cast( CodeSpecifiers specifiers ) specifiers = def_specifier( ESpecifier::Const ); else - specifiers.append( ESpecifier::Const ); + append(specifiers, ESpecifier::Const ); eat( TokType::Spec_Const ); } diff --git a/project/dependencies/strings.hpp b/project/dependencies/strings.hpp index 3c539ca..7a51d4b 100644 --- a/project/dependencies/strings.hpp +++ b/project/dependencies/strings.hpp @@ -185,9 +185,9 @@ void clear(String& str); void free(String& str); #endif -inline char* begin(String& str) { return str; } -inline char* end(String& str) { return scast(char*, str) + length(str); } -inline char* next(String& str) { return scast(char*, str) + 1; } +inline char* begin(String str) { return str; } +inline char* end(String str) { return scast(char*, str) + length(str); } +inline char* next(String str) { return scast(char*, str) + 1; } inline usize string_grow_formula(usize value) { From 0bad61fda6a1dd29f6e8b434d6c3fccb6584eced Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 11:20:31 -0500 Subject: [PATCH 040/112] remove raw member def from code types, reduction on CodeAttributes --- project/components/ast.cpp | 4 +- project/components/code_serialization.cpp | 88 +++++++++-------- project/components/code_types.hpp | 45 +++------ project/components/gen/ast_inlines.hpp | 115 ---------------------- project/components/inlines.hpp | 6 +- project/components/interface.upfront.cpp | 18 ++-- project/helpers/helper.hpp | 5 - 7 files changed, 72 insertions(+), 209 deletions(-) diff --git a/project/components/ast.cpp b/project/components/ast.cpp index 9e713e6..95f74cb 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -545,11 +545,11 @@ void to_string( AST* self, String* result ) break; case Struct: - cast(CodeStruct, {self}).to_string_def( * result ); + to_string_def(cast(CodeStruct, {self}), result ); break; case Struct_Fwd: - cast(CodeStruct, {self}).to_string_fwd( * result ); + to_string_fwd(cast(CodeStruct, {self}), result ); break; case Template: diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 9476c9a..a47b6de 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -13,9 +13,8 @@ String to_string(Code self) return to_string( self.ast ); } -String CodeAttributes::to_string() -{ - return GEN_NS duplicate( ast->Content, GlobalAllocator ); +String to_string(CodeAttributes attributes) { + return GEN_NS duplicate( attributes->Content, GlobalAllocator ); } String to_string(CodeBody body) @@ -175,7 +174,7 @@ void to_string_def( CodeClass self, String* result ) if ( ast->Attributes ) { - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ", GEN_NS to_string(ast->Attributes) ); } if ( ast->ParentType ) @@ -219,7 +218,7 @@ void to_string_fwd( CodeClass self, String* result ) append( result, "export " ); if ( ast->Attributes ) - append_fmt( result, "class %S %S", ast->Attributes.to_string(), ast->Name ); + append_fmt( result, "class %S %S", to_string(ast->Attributes), ast->Name ); else append_fmt( result, "class %S", ast->Name ); @@ -333,7 +332,7 @@ void CodeEnum::to_string_def( String& result ) append( & result, "enum " ); if ( ast->Attributes ) - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ", GEN_NS to_string(ast->Attributes) ); if ( ast->UnderlyingType ) append_fmt( & result, "%S : %S\n{\n%S\n}" @@ -362,7 +361,7 @@ void CodeEnum::to_string_fwd( String& result ) append( & result, "export " ); if ( ast->Attributes ) - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); if ( ast->UnderlyingType ) append_fmt( & result, "enum %S : %S", ast->Name, ast->UnderlyingType.to_string() ); @@ -389,7 +388,7 @@ void CodeEnum::to_string_class_def( String& result ) if ( ast->Attributes ) { - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); } if ( ast->UnderlyingType ) @@ -418,7 +417,7 @@ void CodeEnum::to_string_class_fwd( String& result ) append( & result, "enum class " ); if ( ast->Attributes ) - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); append_fmt( & result, "%S : %S", ast->Name, ast->UnderlyingType.to_string() ); @@ -498,7 +497,7 @@ void CodeFn::to_string_def( String& result ) append( & result, "export" ); if ( ast->Attributes ) - append_fmt( & result, " %S ", ast->Attributes.to_string() ); + append_fmt( & result, " %S ",GEN_NS to_string(ast->Attributes) ); bool prefix_specs = false; if ( ast->Specs ) @@ -551,7 +550,7 @@ void CodeFn::to_string_fwd( String& result ) append( & result, "export " ); if ( ast->Attributes ) - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); b32 prefix_specs = false; if ( ast->Specs ) @@ -665,10 +664,10 @@ void CodeOperator::to_string_def( String& result ) append( & result, "export " ); if ( ast->Attributes ) - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); if ( ast->Attributes ) - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); if ( ast->Specs ) { @@ -719,7 +718,7 @@ void CodeOperator::to_string_fwd( String& result ) append( & result, "export " ); if ( ast->Attributes ) - append_fmt( & result, "%S\n", ast->Attributes.to_string() ); + append_fmt( & result, "%S\n",GEN_NS to_string(ast->Attributes) ); if ( ast->Specs ) { @@ -990,82 +989,89 @@ void to_string( CodeSpecifiers self, String* result ) } } -String CodeStruct::to_string() +String to_string(CodeStruct self) { + GEN_ASSERT(self.ast != nullptr); String result = string_make( GlobalAllocator, "" ); - switch ( ast->Type ) + switch ( self->Type ) { using namespace ECode; case Struct: - to_string_def( result ); + to_string_def( self, & result ); break; case Struct_Fwd: - to_string_fwd( result ); + to_string_fwd( self, & result ); break; } return result; } -void CodeStruct::to_string_def( String& result ) +void to_string_def( CodeStruct self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + GEN_ASSERT(self.ast != nullptr); + AST_Struct* ast = self.ast; - append( & result, "struct " ); + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); + + append( result, "struct " ); if ( ast->Attributes ) { - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ",GEN_NS to_string(ast->Attributes) ); } if ( ast->ParentType ) { char const* access_level = to_str( ast->ParentAccess ); - append_fmt( & result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); + append_fmt( result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); CodeType interface = cast(CodeType, ast->ParentType->Next); if ( interface ) - append( & result, "\n" ); + append( result, "\n" ); while ( interface ) { - append_fmt( & result, ", %S", interface.to_string() ); + append_fmt( result, ", %S", interface.to_string() ); interface = interface->Next ? cast( CodeType, interface->Next) : CodeType { nullptr }; } } else if ( ast->Name ) { - append( & result, ast->Name ); + append( result, ast->Name ); } if ( ast->InlineCmt ) { - append_fmt( & result, " // %S", ast->InlineCmt->Content ); + append_fmt( result, " // %S", ast->InlineCmt->Content ); } - append_fmt( & result, "\n{\n%S\n}", GEN_NS to_string(ast->Body) ); + append_fmt( result, "\n{\n%S\n}", GEN_NS to_string(ast->Body) ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( & result, ";\n"); + append( result, ";\n"); } -void CodeStruct::to_string_fwd( String& result ) +void to_string_fwd( CodeStruct self, String* result ) { + GEN_ASSERT(self.ast != nullptr); + AST_Struct* ast = self.ast; + if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + append( result, "export " ); if ( ast->Attributes ) - append_fmt( & result, "struct %S %S", ast->Attributes.to_string(), ast->Name ); + append_fmt( result, "struct %S %S",GEN_NS to_string(ast->Attributes), ast->Name ); - else append_fmt( & result, "struct %S", ast->Name ); + else append_fmt( result, "struct %S", ast->Name ); if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) { if ( ast->InlineCmt ) - append_fmt( & result, "; %S", ast->InlineCmt->Content ); + append_fmt( result, "; %S", ast->InlineCmt->Content ); else - append( & result, ";\n"); + append( result, ";\n"); } } @@ -1142,7 +1148,7 @@ void CodeType::to_string( String& result ) if ( ast->ReturnType && ast->Params ) { if ( ast->Attributes ) - append_fmt( result, "%S ", ast->Attributes.to_string() ); + append_fmt( result, "%S ",GEN_NS to_string(ast->Attributes) ); else { if ( ast->Specs ) @@ -1157,7 +1163,7 @@ void CodeType::to_string( String& result ) if ( ast->ReturnType && ast->Params ) { if ( ast->Attributes ) - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); else { if ( ast->Specs ) @@ -1171,7 +1177,7 @@ void CodeType::to_string( String& result ) #endif if ( ast->Attributes ) - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); if ( ast->Specs ) append_fmt( & result, "%S %S", ast->Name, GEN_NS to_string(ast->Specs) ); @@ -1197,7 +1203,7 @@ void CodeUnion::to_string( String& result ) append( & result, "union " ); if ( ast->Attributes ) - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); if ( ast->Name ) { @@ -1240,7 +1246,7 @@ void CodeUsing::to_string( String& result ) append( & result, "export " ); if ( ast->Attributes ) - append_fmt( & result, "%S ", ast->Attributes.to_string() ); + append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); if ( ast->UnderlyingType ) { diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index ca4a80e..5dd4ab3 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -32,9 +32,16 @@ s32 remove (CodeSpecifiers specifiers, SpecifierT to_remove ); String to_string(CodeSpecifiers specifiers); void to_string(CodeSpecifiers specifiers, String* result); -SpecifierT* begin( CodeSpecifiers specifiers ); +SpecifierT* begin(CodeSpecifiers specifiers ); SpecifierT* end (CodeSpecifiers specifiers); +void add_interface(CodeStruct self, CodeType interface); +String to_string (CodeStruct self); +void to_string_fwd(CodeStruct self, String* result); +void to_string_def(CodeStruct self, String* result); + +String to_string(CodeAttributes attributes); + #pragma region Code Types // These structs are not used at all by the C vairant. #if ! GEN_COMPILER_C @@ -148,7 +155,7 @@ struct CodeSpecifiers struct CodeStruct { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeStruct ); void add_interface( CodeType interface ); @@ -159,14 +166,7 @@ struct CodeStruct #endif Using_CodeOps( CodeStruct ); - AST* raw() - { - return rcast( AST*, ast ); - } - operator Code() - { - return * rcast( Code*, this ); - } + operator Code() { return * rcast( Code*, this ); } AST_Struct* operator->() { if ( ast == nullptr ) @@ -181,13 +181,12 @@ struct CodeStruct struct CodeAttributes { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code(CodeAttributes); String to_string(); #endif Using_CodeOps(CodeAttributes); - AST *raw(); operator Code(); AST_Attributes *operator->(); AST_Attributes *ast; @@ -203,7 +202,6 @@ struct CodeComment #endif Using_CodeOps(CodeComment); - AST *raw(); operator Code(); AST_Comment *operator->(); AST_Comment *ast; @@ -220,7 +218,6 @@ struct CodeConstructor #endif Using_CodeOps(CodeConstructor); - AST* raw(); operator Code(); AST_Constructor* operator->(); AST_Constructor* ast; @@ -236,7 +233,6 @@ struct CodeDefine #endif Using_CodeOps(CodeDefine); - AST* raw(); operator Code(); AST_Define* operator->(); AST_Define* ast; @@ -253,7 +249,6 @@ struct CodeDestructor #endif Using_CodeOps(CodeDestructor); - AST* raw(); operator Code(); AST_Destructor* operator->(); AST_Destructor* ast; @@ -272,7 +267,6 @@ struct CodeEnum #endif Using_CodeOps(CodeEnum); - AST* raw(); operator Code(); AST_Enum* operator->(); AST_Enum* ast; @@ -286,7 +280,6 @@ struct CodeExec #endif Using_CodeOps(CodeExec); - AST *raw(); operator Code(); AST_Exec *operator->(); AST_Exec *ast; @@ -507,7 +500,6 @@ struct CodeExtern #endif Using_CodeOps(CodeExtern); - AST* raw(); operator Code(); AST_Extern* operator->(); AST_Extern* ast; @@ -523,7 +515,6 @@ struct CodeInclude #endif Using_CodeOps(CodeInclude); - AST* raw(); operator Code(); AST_Include* operator->(); AST_Include* ast; @@ -539,7 +530,6 @@ struct CodeFriend #endif Using_CodeOps(CodeFriend); - AST* raw(); operator Code(); AST_Friend* operator->(); AST_Friend* ast; @@ -556,7 +546,6 @@ struct CodeFn #endif Using_CodeOps(CodeFn); - AST* raw(); operator Code(); AST_Fn* operator->(); AST_Fn* ast; @@ -572,7 +561,6 @@ struct CodeModule #endif Using_CodeOps(CodeModule); - AST* raw(); operator Code(); AST_Module* operator->(); AST_Module* ast; @@ -588,7 +576,6 @@ struct CodeNS #endif Using_CodeOps(CodeNS); - AST* raw(); operator Code(); AST_NS* operator->(); AST_NS* ast; @@ -605,7 +592,6 @@ struct CodeOperator #endif Using_CodeOps(CodeOperator); - AST* raw(); operator Code(); AST_Operator* operator->(); AST_Operator* ast; @@ -622,7 +608,6 @@ struct CodeOpCast #endif Using_CodeOps(CodeOpCast); - AST* raw(); operator Code(); AST_OpCast* operator->(); AST_OpCast* ast; @@ -638,7 +623,6 @@ struct CodePragma #endif Using_CodeOps( CodePragma ); - AST* raw(); operator Code(); AST_Pragma* operator->(); AST_Pragma* ast; @@ -659,7 +643,6 @@ struct CodePreprocessCond #endif Using_CodeOps( CodePreprocessCond ); - AST* raw(); operator Code(); AST_PreprocessCond* operator->(); AST_PreprocessCond* ast; @@ -859,7 +842,6 @@ struct CodeTemplate #endif Using_CodeOps( CodeTemplate ); - AST* raw(); operator Code(); AST_Template* operator->(); AST_Template* ast; @@ -875,7 +857,6 @@ struct CodeType #endif Using_CodeOps( CodeType ); - AST* raw(); operator Code(); AST_Type* operator->(); AST_Type* ast; @@ -891,7 +872,6 @@ struct CodeTypedef #endif Using_CodeOps( CodeTypedef ); - AST* raw(); operator Code(); AST_Typedef* operator->(); AST_Typedef* ast; @@ -907,7 +887,6 @@ struct CodeUnion #endif Using_CodeOps(CodeUnion); - AST* raw(); operator Code(); AST_Union* operator->(); AST_Union* ast; @@ -924,7 +903,6 @@ struct CodeUsing #endif Using_CodeOps(CodeUsing); - AST* raw(); operator Code(); AST_Using* operator->(); AST_Using* ast; @@ -940,7 +918,6 @@ struct CodeVar #endif Using_CodeOps(CodeVar); - AST* raw(); operator Code(); AST_Var* operator->(); AST_Var* ast; diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index 0f840a9..bc2d3fc 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -55,11 +55,6 @@ inline CodeAttributes::operator bool() return ast != nullptr; } -inline AST* CodeAttributes::raw() -{ - return rcast( AST*, ast ); -} - inline CodeAttributes::operator Code() { return *rcast( Code*, this ); @@ -91,11 +86,6 @@ inline CodeComment::operator bool() return ast != nullptr; } -inline AST* CodeComment::raw() -{ - return rcast( AST*, ast ); -} - inline CodeComment::operator Code() { return *rcast( Code*, this ); @@ -127,11 +117,6 @@ inline CodeConstructor::operator bool() return ast != nullptr; } -inline AST* CodeConstructor::raw() -{ - return rcast( AST*, ast ); -} - inline CodeConstructor::operator Code() { return *rcast( Code*, this ); @@ -179,11 +164,6 @@ inline CodeDefine::operator bool() return ast != nullptr; } -inline AST* CodeDefine::raw() -{ - return rcast( AST*, ast ); -} - inline CodeDefine::operator Code() { return *rcast( Code*, this ); @@ -215,11 +195,6 @@ inline CodeDestructor::operator bool() return ast != nullptr; } -inline AST* CodeDestructor::raw() -{ - return rcast( AST*, ast ); -} - inline CodeDestructor::operator Code() { return *rcast( Code*, this ); @@ -251,11 +226,6 @@ inline CodeEnum::operator bool() return ast != nullptr; } -inline AST* CodeEnum::raw() -{ - return rcast( AST*, ast ); -} - inline CodeEnum::operator Code() { return *rcast( Code*, this ); @@ -287,11 +257,6 @@ inline CodeExec::operator bool() return ast != nullptr; } -inline AST* CodeExec::raw() -{ - return rcast( AST*, ast ); -} - inline CodeExec::operator Code() { return *rcast( Code*, this ); @@ -323,11 +288,6 @@ inline CodeExtern::operator bool() return ast != nullptr; } -inline AST* CodeExtern::raw() -{ - return rcast( AST*, ast ); -} - inline CodeExtern::operator Code() { return *rcast( Code*, this ); @@ -359,11 +319,6 @@ inline CodeFriend::operator bool() return ast != nullptr; } -inline AST* CodeFriend::raw() -{ - return rcast( AST*, ast ); -} - inline CodeFriend::operator Code() { return *rcast( Code*, this ); @@ -395,11 +350,6 @@ inline CodeFn::operator bool() return ast != nullptr; } -inline AST* CodeFn::raw() -{ - return rcast( AST*, ast ); -} - inline CodeFn::operator Code() { return *rcast( Code*, this ); @@ -431,11 +381,6 @@ inline CodeInclude::operator bool() return ast != nullptr; } -inline AST* CodeInclude::raw() -{ - return rcast( AST*, ast ); -} - inline CodeInclude::operator Code() { return *rcast( Code*, this ); @@ -467,11 +412,6 @@ inline CodeModule::operator bool() return ast != nullptr; } -inline AST* CodeModule::raw() -{ - return rcast( AST*, ast ); -} - inline CodeModule::operator Code() { return *rcast( Code*, this ); @@ -503,11 +443,6 @@ inline CodeNS::operator bool() return ast != nullptr; } -inline AST* CodeNS::raw() -{ - return rcast( AST*, ast ); -} - inline CodeNS::operator Code() { return *rcast( Code*, this ); @@ -539,11 +474,6 @@ inline CodeOperator::operator bool() return ast != nullptr; } -inline AST* CodeOperator::raw() -{ - return rcast( AST*, ast ); -} - inline CodeOperator::operator Code() { return *rcast( Code*, this ); @@ -575,11 +505,6 @@ inline CodeOpCast::operator bool() return ast != nullptr; } -inline AST* CodeOpCast::raw() -{ - return rcast( AST*, ast ); -} - inline CodeOpCast::operator Code() { return *rcast( Code*, this ); @@ -627,11 +552,6 @@ inline CodePragma::operator bool() return ast != nullptr; } -inline AST* CodePragma::raw() -{ - return rcast( AST*, ast ); -} - inline CodePragma::operator Code() { return *rcast( Code*, this ); @@ -663,11 +583,6 @@ inline CodePreprocessCond::operator bool() return ast != nullptr; } -inline AST* CodePreprocessCond::raw() -{ - return rcast( AST*, ast ); -} - inline CodePreprocessCond::operator Code() { return *rcast( Code*, this ); @@ -731,11 +646,6 @@ inline CodeTemplate::operator bool() return ast != nullptr; } -inline AST* CodeTemplate::raw() -{ - return rcast( AST*, ast ); -} - inline CodeTemplate::operator Code() { return *rcast( Code*, this ); @@ -767,11 +677,6 @@ inline CodeType::operator bool() return ast != nullptr; } -inline AST* CodeType::raw() -{ - return rcast( AST*, ast ); -} - inline CodeType::operator Code() { return *rcast( Code*, this ); @@ -803,11 +708,6 @@ inline CodeTypedef::operator bool() return ast != nullptr; } -inline AST* CodeTypedef::raw() -{ - return rcast( AST*, ast ); -} - inline CodeTypedef::operator Code() { return *rcast( Code*, this ); @@ -839,11 +739,6 @@ inline CodeUnion::operator bool() return ast != nullptr; } -inline AST* CodeUnion::raw() -{ - return rcast( AST*, ast ); -} - inline CodeUnion::operator Code() { return *rcast( Code*, this ); @@ -875,11 +770,6 @@ inline CodeUsing::operator bool() return ast != nullptr; } -inline AST* CodeUsing::raw() -{ - return rcast( AST*, ast ); -} - inline CodeUsing::operator Code() { return *rcast( Code*, this ); @@ -911,11 +801,6 @@ inline CodeVar::operator bool() return ast != nullptr; } -inline AST* CodeVar::raw() -{ - return rcast( AST*, ast ); -} - inline CodeVar::operator Code() { return *rcast( Code*, this ); diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index 7f0e8eb..2884da4 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -353,13 +353,13 @@ SpecifierT* end(CodeSpecifiers self) #pragma region CodeStruct inline -void CodeStruct::add_interface( CodeType type ) +void add_interface(CodeStruct self, CodeType type ) { - CodeType possible_slot = ast->ParentType; + CodeType possible_slot = self->ParentType; if ( possible_slot.ast ) { // Were adding an interface to parent type, so we need to make sure the parent type is public. - ast->ParentAccess = AccessSpec_Public; + self->ParentAccess = AccessSpec_Public; // If your planning on adding a proper parent, // then you'll need to move this over to ParentType->next and update ParentAccess accordingly. } diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index f50b365..3d0c197 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -555,7 +555,7 @@ CodeClass def_class( StrC name if ( attributes && attributes->Type != PlatformAttributes ) { - log_failure( "gen::def_class: attributes was not a 'PlatformAttributes' type: %s", attributes.debug_str() ); + log_failure( "gen::def_class: attributes was not a 'PlatformAttributes' type: %s", debug_str(attributes) ); return InvalidCode; } @@ -697,7 +697,7 @@ CodeEnum def_enum( StrC name if ( attributes && attributes->Type != PlatformAttributes ) { - log_failure( "gen::def_enum: attributes was not a 'PlatformAttributes' type: %s", attributes.debug_str() ); + log_failure( "gen::def_enum: attributes was not a 'PlatformAttributes' type: %s", debug_str(attributes) ); return InvalidCode; } @@ -846,7 +846,7 @@ CodeFn def_function( StrC name if ( attributes && attributes->Type != PlatformAttributes ) { - log_failure( "gen::def_function: attributes was not a `PlatformAttributes` type: %s", attributes.debug_str() ); + log_failure( "gen::def_function: attributes was not a `PlatformAttributes` type: %s", debug_str(attributes) ); return InvalidCode; } @@ -967,7 +967,7 @@ CodeOperator def_operator( OperatorT op, StrC nspace if ( attributes && attributes->Type != PlatformAttributes ) { - log_failure( "gen::def_operator: PlatformAttributes was provided but its not of attributes type: %s", attributes.debug_str() ); + log_failure( "gen::def_operator: PlatformAttributes was provided but its not of attributes type: %s", debug_str(attributes) ); return InvalidCode; } @@ -1183,7 +1183,7 @@ CodeStruct def_struct( StrC name if ( attributes && attributes->Type != PlatformAttributes ) { - log_failure( "gen::def_struct: attributes was not a `PlatformAttributes` type - %s", attributes.debug_str() ); + log_failure( "gen::def_struct: attributes was not a `PlatformAttributes` type - %s", debug_str(attributes) ); return InvalidCode; } @@ -1229,7 +1229,7 @@ CodeStruct def_struct( StrC name { for (s32 idx = 0; idx < num_interfaces; idx++ ) { - result.add_interface( interfaces[idx] ); + add_interface(result, interfaces[idx] ); } } @@ -1387,7 +1387,7 @@ CodeUnion def_union( StrC name, Code body, CodeAttributes attributes, ModuleFlag if ( attributes && attributes->Type != ECode::PlatformAttributes ) { - log_failure( "gen::def_union: attributes was not a PlatformAttributes type - %s", attributes.debug_str() ); + log_failure( "gen::def_union: attributes was not a PlatformAttributes type - %s", debug_str(attributes) ); return InvalidCode; } @@ -1424,7 +1424,7 @@ CodeUsing def_using( StrC name, CodeType type if ( attributes && attributes->Type != ECode::PlatformAttributes ) { - log_failure( "gen::def_using: attributes was not a PlatformAttributes type - %s", attributes.debug_str() ); + log_failure( "gen::def_using: attributes was not a PlatformAttributes type - %s", debug_str(attributes) ); return InvalidCode; } @@ -1464,7 +1464,7 @@ CodeVar def_variable( CodeType type, StrC name, Code value if ( attributes && attributes->Type != ECode::PlatformAttributes ) { - log_failure( "gen::def_variable: attributes was not a `PlatformAttributes` type - %s", attributes.debug_str() ); + log_failure( "gen::def_variable: attributes was not a `PlatformAttributes` type - %s", debug_str(attributes) ); return InvalidCode; } diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 9e61baa..1a032d5 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -373,11 +373,6 @@ CodeBody gen_ast_inlines() ); char const* codetype_impl_tmpl = stringize( - inline - AST* Code::raw() - { - return rcast( AST*, ast ); - } inline Code::operator Code() { From 8f47f3b30fdf52ce04edbe9a8b66624b11723f27 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 16:59:13 -0500 Subject: [PATCH 041/112] Comment, Constructor, Destructor, Define, Enum, Exec, Extern, Include, Friend, Fn codes member proc usage reductions --- project/components/ast.cpp | 28 +- project/components/code_serialization.cpp | 384 +++++++++++----------- project/components/code_types.hpp | 113 ++++--- project/components/inlines.hpp | 14 + 4 files changed, 285 insertions(+), 254 deletions(-) diff --git a/project/components/ast.cpp b/project/components/ast.cpp index 95f74cb..3f262fc 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -423,35 +423,35 @@ void to_string( AST* self, String* result ) break; case Constructor: - cast(CodeConstructor, {self}).to_string_def( * result ); + to_string_def(cast(CodeConstructor, {self}), result ); break; case Constructor_Fwd: - cast(CodeConstructor, {self}).to_string_fwd( * result ); + to_string_fwd(cast(CodeConstructor, {self}), result ); break; case Destructor: - cast(CodeDestructor, {self}).to_string_def( * result ); + to_string_def(cast(CodeDestructor, {self}), result ); break; case Destructor_Fwd: - cast(CodeDestructor, {self}).to_string_fwd( * result ); + to_string_fwd(cast(CodeDestructor, {self}), result ); break; case Enum: - cast(CodeEnum, {self}).to_string_def( * result ); + to_string_def(cast(CodeEnum, {self}), result ); break; case Enum_Fwd: - cast(CodeEnum, {self}).to_string_fwd( * result ); + to_string_fwd(cast(CodeEnum, {self}), result ); break; case Enum_Class: - cast(CodeEnum, {self}).to_string_class_def( * result ); + to_string_class_def(cast(CodeEnum, {self}), result ); break; case Enum_Class_Fwd: - cast(CodeEnum, {self}).to_string_class_fwd( * result ); + to_string_class_fwd(cast(CodeEnum, {self}), result ); break; case Export_Body: @@ -459,19 +459,19 @@ void to_string( AST* self, String* result ) break; case Extern_Linkage: - cast(CodeExtern, {self}).to_string( * result ); + to_string(cast(CodeExtern, {self}), result ); break; case Friend: - cast(CodeFriend, {self}).to_string( * result ); + to_string(cast(CodeFriend, {self}), result ); break; case Function: - cast(CodeFn, {self}).to_string_def( * result ); + to_string_def(cast(CodeFn, {self}), result ); break; case Function_Fwd: - cast(CodeFn, {self}).to_string_fwd( * result ); + to_string_fwd(cast(CodeFn, {self}), result ); break; case Module: @@ -505,7 +505,7 @@ void to_string( AST* self, String* result ) break; case Preprocess_Define: - cast(CodeDefine, {self}).to_string( * result ); + to_string(cast(CodeDefine, {self}), result ); break; case Preprocess_If: @@ -521,7 +521,7 @@ void to_string( AST* self, String* result ) break; case Preprocess_Include: - cast(CodeInclude, {self}).to_string( * result ); + to_string(cast(CodeInclude, {self}), result ); break; case Preprocess_ElIf: diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index a47b6de..3304678 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -77,73 +77,73 @@ void to_string_export( CodeBody body, String* result ) append_fmt( result, "};\n" ); } -String CodeComment::to_string() +String to_string(CodeComment comment) { - return GEN_NS duplicate( ast->Content, GlobalAllocator ); + return GEN_NS duplicate( comment->Content, GlobalAllocator ); } -String CodeConstructor::to_string() +String to_string(CodeConstructor self) { String result = string_make( GlobalAllocator, "" ); - switch (ast->Type) + switch (self->Type) { using namespace ECode; case Constructor: - to_string_def( result ); + to_string_def( self, & result ); break; case Constructor_Fwd: - to_string_fwd( result ); + to_string_fwd( self, & result ); break; } return result; } -void CodeConstructor::to_string_def( String& result ) +void to_string_def(CodeConstructor self, String* result ) { - AST* ClassStructParent = ast->Parent->Parent; + AST* ClassStructParent = self->Parent->Parent; if (ClassStructParent) { - append( & result, ClassStructParent->Name ); + append( result, ClassStructParent->Name ); } else { - append( & result, ast->Name ); + append( result, self->Name ); } - if ( ast->Params ) - append_fmt( & result, "( %S )", GEN_NS to_string(ast->Params) ); + if ( self->Params ) + append_fmt( result, "( %S )", GEN_NS to_string(self->Params) ); else - append( & result, "()" ); + append( result, "()" ); - if ( ast->InitializerList ) - append_fmt( & result, " : %S", GEN_NS to_string(ast->InitializerList) ); + if ( self->InitializerList ) + append_fmt( result, " : %S", GEN_NS to_string(self->InitializerList) ); - if ( ast->InlineCmt ) - append_fmt( & result, " // %S", ast->InlineCmt->Content ); + if ( self->InlineCmt ) + append_fmt( result, " // %S", self->InlineCmt->Content ); - append_fmt( & result, "\n{\n%S\n}\n", GEN_NS to_string(ast->Body) ); + append_fmt( result, "\n{\n%S\n}\n", GEN_NS to_string(self->Body) ); } -void CodeConstructor::to_string_fwd( String& result ) +void to_string_fwd(CodeConstructor self, String* result ) { - AST* ClassStructParent = ast->Parent->Parent; + AST* ClassStructParent = self->Parent->Parent; if (ClassStructParent) { - append( & result, ClassStructParent->Name ); + append( result, ClassStructParent->Name ); } else { - append( & result, ast->Name ); + append( result, self->Name ); } - if ( ast->Params ) - append_fmt( & result, "( %S )", GEN_NS to_string(ast->Params) ); + if ( self->Params ) + append_fmt( result, "( %S )", GEN_NS to_string(self->Params) ); else - append_fmt( & result, "()"); + append_fmt( result, "()"); - if (ast->Body) - append_fmt( & result, " = %S", GEN_NS to_string(ast->Body) ); + if (self->Body) + append_fmt( result, " = %S", GEN_NS to_string(self->Body) ); - if ( ast->InlineCmt ) - append_fmt( & result, "; // %S\n", ast->InlineCmt->Content ); + if ( self->InlineCmt ) + append_fmt( result, "; // %S\n", self->InlineCmt->Content ); else - append( & result, ";\n" ); + append( result, ";\n" ); } String to_string( CodeClass self ) @@ -232,379 +232,379 @@ void to_string_fwd( CodeClass self, String* result ) } } -String CodeDefine::to_string() +String to_string(CodeDefine define) { - return string_fmt_buf( GlobalAllocator, "#define %S %S\n", ast->Name, ast->Content ); + return string_fmt_buf( GlobalAllocator, "#define %S %S\n", define->Name, define->Content ); } -void CodeDefine::to_string( String& result ) +void to_string(CodeDefine define, String* result ) { - append_fmt( & result, "#define %S %S\n", ast->Name, ast->Content ); + append_fmt( result, "#define %S %S\n", define->Name, define->Content ); } -String CodeDestructor::to_string() +String to_string(CodeDestructor self) { String result = string_make( GlobalAllocator, "" ); - switch ( ast->Type ) + switch ( self->Type ) { using namespace ECode; case Destructor: - to_string_def( result ); + to_string_def( self, & result ); break; case Destructor_Fwd: - to_string_fwd( result ); + to_string_fwd( self, & result ); break; } return result; } -void CodeDestructor::to_string_def( String& result ) +void to_string_def(CodeDestructor self, String* result ) { - if ( ast->Name ) + if ( self->Name ) { - append_fmt( & result, "%S()", ast->Name ); + append_fmt( result, "%S()", self->Name ); } - else if ( ast->Specs ) + else if ( self->Specs ) { - if ( has(ast->Specs, ESpecifier::Virtual ) ) - append_fmt( & result, "virtual ~%S()", ast->Parent->Name ); + if ( has(self->Specs, ESpecifier::Virtual ) ) + append_fmt( result, "virtual ~%S()", self->Parent->Name ); else - append_fmt( & result, "~%S()", ast->Parent->Name ); + append_fmt( result, "~%S()", self->Parent->Name ); } else - append_fmt( & result, "~%S()", ast->Parent->Name ); + append_fmt( result, "~%S()", self->Parent->Name ); - append_fmt( & result, "\n{\n%S\n}\n", GEN_NS to_string(ast->Body) ); + append_fmt( result, "\n{\n%S\n}\n", GEN_NS to_string(self->Body) ); } -void CodeDestructor::to_string_fwd( String& result ) +void to_string_fwd(CodeDestructor self, String* result ) { - if ( ast->Specs ) + if ( self->Specs ) { - if ( has(ast->Specs, ESpecifier::Virtual ) ) - append_fmt( & result, "virtual ~%S();\n", ast->Parent->Name ); + if ( has(self->Specs, ESpecifier::Virtual ) ) + append_fmt( result, "virtual ~%S();\n", self->Parent->Name ); else - append_fmt( & result, "~%S()", ast->Parent->Name ); + append_fmt( result, "~%S()", self->Parent->Name ); - if ( has(ast->Specs, ESpecifier::Pure ) ) - append( & result, " = 0;" ); - else if (ast->Body) - append_fmt( & result, " = %S;", GEN_NS to_string(ast->Body) ); + if ( has(self->Specs, ESpecifier::Pure ) ) + append( result, " = 0;" ); + else if (self->Body) + append_fmt( result, " = %S;", GEN_NS to_string(self->Body) ); } else - append_fmt( & result, "~%S();", ast->Parent->Name ); + append_fmt( result, "~%S();", self->Parent->Name ); - if ( ast->InlineCmt ) - append_fmt( & result, " %S", ast->InlineCmt->Content ); + if ( self->InlineCmt ) + append_fmt( result, " %S", self->InlineCmt->Content ); else - append( & result, "\n"); + append( result, "\n"); } -String CodeEnum::to_string() +String to_string(CodeEnum self) { String result = string_make( GlobalAllocator, "" ); - switch ( ast->Type ) + switch ( self->Type ) { using namespace ECode; case Enum: - to_string_def( result ); + to_string_def(self, & result ); break; case Enum_Fwd: - to_string_fwd( result ); + to_string_fwd(self, & result ); break; case Enum_Class: - to_string_class_def( result ); + to_string_class_def(self, & result ); break; case Enum_Class_Fwd: - to_string_class_fwd( result ); + to_string_class_fwd(self, & result ); break; } return result; } -void CodeEnum::to_string_def( String& result ) +void to_string_def(CodeEnum self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - if ( ast->Attributes || ast->UnderlyingType ) + if ( self->Attributes || self->UnderlyingType ) { - append( & result, "enum " ); + append( result, "enum " ); - if ( ast->Attributes ) - append_fmt( & result, "%S ", GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, "%S ", GEN_NS to_string(self->Attributes) ); - if ( ast->UnderlyingType ) - append_fmt( & result, "%S : %S\n{\n%S\n}" - , ast->Name - , ast->UnderlyingType.to_string() - , GEN_NS to_string(ast->Body) + if ( self->UnderlyingType ) + append_fmt( result, "%S : %S\n{\n%S\n}" + , self->Name + , self->UnderlyingType.to_string() + , GEN_NS to_string(self->Body) ); - else if ( ast->UnderlyingTypeMacro ) - append_fmt( & result, "%S : %S\n{\n%S\n}" - , ast->Name - , GEN_NS to_string(ast->UnderlyingTypeMacro) - , GEN_NS to_string(ast->Body) + else if ( self->UnderlyingTypeMacro ) + append_fmt( result, "%S : %S\n{\n%S\n}" + , self->Name + , GEN_NS to_string(self->UnderlyingTypeMacro) + , GEN_NS to_string(self->Body) ); - else append_fmt( & result, "%S\n{\n%S\n}", ast->Name, GEN_NS to_string(ast->Body) ); + else append_fmt( result, "%S\n{\n%S\n}", self->Name, GEN_NS to_string(self->Body) ); } - else append_fmt( & result, "enum %S\n{\n%S\n}", ast->Name, GEN_NS to_string(ast->Body) ); + else append_fmt( result, "enum %S\n{\n%S\n}", self->Name, GEN_NS to_string(self->Body) ); - if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( & result, ";\n"); + if ( self->Parent.ast == nullptr || ( self->Parent->Type != ECode::Typedef && self->Parent->Type != ECode::Variable ) ) + append( result, ";\n"); } -void CodeEnum::to_string_fwd( String& result ) +void to_string_fwd(CodeEnum self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - if ( ast->Attributes ) - append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); - if ( ast->UnderlyingType ) - append_fmt( & result, "enum %S : %S", ast->Name, ast->UnderlyingType.to_string() ); + if ( self->UnderlyingType ) + append_fmt( result, "enum %S : %S", self->Name, self->UnderlyingType.to_string() ); else - append_fmt( & result, "enum %S", ast->Name ); + append_fmt( result, "enum %S", self->Name ); - if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) + if ( self->Parent.ast == nullptr || ( self->Parent->Type != ECode::Typedef && self->Parent->Type != ECode::Variable ) ) { - if ( ast->InlineCmt ) - append_fmt( & result, "; %S", ast->InlineCmt->Content ); + if ( self->InlineCmt ) + append_fmt( result, "; %S", self->InlineCmt->Content ); else - append( & result, ";\n"); + append( result, ";\n"); } } -void CodeEnum::to_string_class_def( String& result ) +void to_string_class_def(CodeEnum self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - if ( ast->Attributes || ast->UnderlyingType ) + if ( self->Attributes || self->UnderlyingType ) { - append( & result, "enum class " ); + append( result, "enum class " ); - if ( ast->Attributes ) + if ( self->Attributes ) { - append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); + append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); } - if ( ast->UnderlyingType ) + if ( self->UnderlyingType ) { - append_fmt( & result, "%S : %S\n{\n%S\n}", ast->Name, ast->UnderlyingType.to_string(), GEN_NS to_string(ast->Body) ); + append_fmt( result, "%S : %S\n{\n%S\n}", self->Name, self->UnderlyingType.to_string(), GEN_NS to_string(self->Body) ); } else { - append_fmt( & result, "%S\n{\n%S\n}", ast->Name, GEN_NS to_string(ast->Body) ); + append_fmt( result, "%S\n{\n%S\n}", self->Name, GEN_NS to_string(self->Body) ); } } else { - append_fmt( & result, "enum class %S\n{\n%S\n}", GEN_NS to_string(ast->Body) ); + append_fmt( result, "enum class %S\n{\n%S\n}", GEN_NS to_string(self->Body) ); } - if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( & result, ";\n"); + if ( self->Parent.ast == nullptr || ( self->Parent->Type != ECode::Typedef && self->Parent->Type != ECode::Variable ) ) + append( result, ";\n"); } -void CodeEnum::to_string_class_fwd( String& result ) +void to_string_class_fwd(CodeEnum self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - append( & result, "enum class " ); + append( result, "enum class " ); - if ( ast->Attributes ) - append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); - append_fmt( & result, "%S : %S", ast->Name, ast->UnderlyingType.to_string() ); + append_fmt( result, "%S : %S", self->Name, self->UnderlyingType.to_string() ); - if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) + if ( self->Parent.ast == nullptr || ( self->Parent->Type != ECode::Typedef && self->Parent->Type != ECode::Variable ) ) { - if ( ast->InlineCmt ) - append_fmt( & result, "; %S", ast->InlineCmt->Content ); + if ( self->InlineCmt ) + append_fmt( result, "; %S", self->InlineCmt->Content ); else - append( & result, ";\n"); + append( result, ";\n"); } } -String CodeExec::to_string() +String to_string(CodeExec exec) { - return GEN_NS duplicate( ast->Content, GlobalAllocator ); + return GEN_NS duplicate( exec->Content, GlobalAllocator ); } -void CodeExtern::to_string( String& result ) +void to_string(CodeExtern self, String* result ) { - if ( ast->Body ) - append_fmt( & result, "extern \"%S\"\n{\n%S\n}\n", ast->Name, GEN_NS to_string(ast->Body) ); + if ( self->Body ) + append_fmt( result, "extern \"%S\"\n{\n%S\n}\n", self->Name, GEN_NS to_string(self->Body) ); else - append_fmt( & result, "extern \"%S\"\n{}\n", ast->Name ); + append_fmt( result, "extern \"%S\"\n{}\n", self->Name ); } -String CodeInclude::to_string() +String to_string(CodeInclude include) { - return string_fmt_buf( GlobalAllocator, "#include %S\n", ast->Content ); + return string_fmt_buf( GlobalAllocator, "#include %S\n", include->Content ); } -void CodeInclude::to_string( String& result ) +void to_string( CodeInclude include, String* result ) { - append_fmt( & result, "#include %S\n", ast->Content ); + append_fmt( result, "#include %S\n", include->Content ); } -String CodeFriend::to_string() +String to_string(CodeFriend self) { String result = string_make( GlobalAllocator, "" ); - to_string( result ); + to_string( self, & result ); return result; } -void CodeFriend::to_string( String& result ) +void to_string(CodeFriend self, String* result ) { - append_fmt( & result, "friend %S", GEN_NS to_string(ast->Declaration) ); + append_fmt( result, "friend %S", GEN_NS to_string(self->Declaration) ); - if ( ast->Declaration->Type != ECode::Function && result[ length(result) - 1 ] != ';' ) + if ( self->Declaration->Type != ECode::Function && (* result)[ length(* result) - 1 ] != ';' ) { - append( & result, ";" ); + append( result, ";" ); } - if ( ast->InlineCmt ) - append_fmt( & result, " %S", ast->InlineCmt->Content ); + if ( self->InlineCmt ) + append_fmt( result, " %S", self->InlineCmt->Content ); else - append( & result, "\n"); + append( result, "\n"); } -String CodeFn::to_string() +String to_string(CodeFn self) { String result = string_make( GlobalAllocator, "" ); - switch ( ast->Type ) + switch ( self->Type ) { using namespace ECode; case Function: - to_string_def( result ); + to_string_def(self, & result ); break; case Function_Fwd: - to_string_fwd( result ); + to_string_fwd(self, & result ); break; } return result; } -void CodeFn::to_string_def( String& result ) +void to_string_def(CodeFn self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export" ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export" ); - if ( ast->Attributes ) - append_fmt( & result, " %S ",GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, " %S ",GEN_NS to_string(self->Attributes) ); bool prefix_specs = false; - if ( ast->Specs ) + if ( self->Specs ) { - for ( SpecifierT spec : ast->Specs ) + for ( SpecifierT spec : self->Specs ) { if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); prefix_specs = true; } } } - if ( ast->Attributes || prefix_specs ) - append( & result, "\n" ); + if ( self->Attributes || prefix_specs ) + append( result, "\n" ); - if ( ast->ReturnType ) - append_fmt( & result, "%S %S(", ast->ReturnType.to_string(), ast->Name ); + if ( self->ReturnType ) + append_fmt( result, "%S %S(", self->ReturnType.to_string(), self->Name ); else - append_fmt( & result, "%S(", ast->Name ); + append_fmt( result, "%S(", self->Name ); - if ( ast->Params ) - append_fmt( & result, "%S)", GEN_NS to_string(ast->Params) ); + if ( self->Params ) + append_fmt( result, "%S)", GEN_NS to_string(self->Params) ); else - append( & result, ")" ); + append( result, ")" ); - if ( ast->Specs ) + if ( self->Specs ) { - for ( SpecifierT spec : ast->Specs ) + for ( SpecifierT spec : self->Specs ) { if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } - append_fmt( & result, "\n{\n%S\n}\n", GEN_NS to_string(ast->Body) ); + append_fmt( result, "\n{\n%S\n}\n", GEN_NS to_string(self->Body) ); } -void CodeFn::to_string_fwd( String& result ) +void to_string_fwd(CodeFn self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - if ( ast->Attributes ) - append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); b32 prefix_specs = false; - if ( ast->Specs ) + if ( self->Specs ) { - for ( SpecifierT spec : ast->Specs ) + for ( SpecifierT spec : self->Specs ) { if ( ! ESpecifier::is_trailing( spec ) || ! (spec != ESpecifier::Pure) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); prefix_specs = true; } } } - if ( ast->Attributes || prefix_specs ) + if ( self->Attributes || prefix_specs ) { - append( & result, "\n" ); + append( result, "\n" ); } - if ( ast->ReturnType ) - append_fmt( & result, "%S %S(", ast->ReturnType.to_string(), ast->Name ); + if ( self->ReturnType ) + append_fmt( result, "%S %S(", self->ReturnType.to_string(), self->Name ); else - append_fmt( & result, "%S(", ast->Name ); + append_fmt( result, "%S(", self->Name ); - if ( ast->Params ) - append_fmt( & result, "%S)", GEN_NS to_string(ast->Params) ); + if ( self->Params ) + append_fmt( result, "%S)", GEN_NS to_string(self->Params) ); else - append( & result, ")" ); + append( result, ")" ); - if ( ast->Specs ) + if ( self->Specs ) { - for ( SpecifierT spec : ast->Specs ) + for ( SpecifierT spec : self->Specs ) { if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } - if ( ast->Specs && has(ast->Specs, ESpecifier::Pure ) >= 0 ) - append( & result, " = 0;" ); - else if (ast->Body) - append_fmt( & result, " = %S;", GEN_NS to_string(ast->Body) ); + if ( self->Specs && has(self->Specs, ESpecifier::Pure ) >= 0 ) + append( result, " = 0;" ); + else if (self->Body) + append_fmt( result, " = %S;", GEN_NS to_string(self->Body) ); - if ( ast->InlineCmt ) - append_fmt( & result, "; %S", ast->InlineCmt->Content ); + if ( self->InlineCmt ) + append_fmt( result, "; %S", self->InlineCmt->Content ); else - append( & result, ";\n" ); + append( result, ";\n" ); } String CodeModule::to_string() diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index 5dd4ab3..302b667 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -41,6 +41,38 @@ void to_string_fwd(CodeStruct self, String* result); void to_string_def(CodeStruct self, String* result); String to_string(CodeAttributes attributes); +String to_string(CodeComment comment ); + +String to_string (CodeConstructor constructor); +void to_string_def(CodeConstructor constructor, String* result ); +void to_string_fwd(CodeConstructor constructor, String* result ); + +String to_string(CodeDefine define); +void to_string(CodeDefine define, String* result); + +String to_string (CodeDestructor destructor); +void to_string_def(CodeDestructor destructor, String* result ); +void to_string_fwd(CodeDestructor destructor, String* result ); + +String to_string (CodeEnum self); +void to_string_def (CodeEnum self, String* result ); +void to_string_fwd (CodeEnum self, String* result ); +void to_string_class_def(CodeEnum self, String* result ); +void to_string_class_fwd(CodeEnum self, String* result ); + +String to_string(CodeExec exec); + +void to_string(CodeExtern self, String* result); + +String to_string(CodeInclude include); +void to_string(CodeInclude include, String* result); + +String to_string(CodeFriend self); +void to_string(CodeFriend self, String* result); + +String to_string (CodeFn self); +void to_string_def(CodeFn self, String* result); +void to_string_fwd(CodeFn self, String* result); #pragma region Code Types // These structs are not used at all by the C vairant. @@ -196,9 +228,9 @@ struct CodeAttributes struct CodeComment { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code(CodeComment); - String to_string(); + String to_string() { return GEN_NS to_string(* this); } #endif Using_CodeOps(CodeComment); @@ -209,12 +241,12 @@ struct CodeComment struct CodeConstructor { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeConstructor ); - String to_string(); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string_def( String& result ) { return GEN_NS to_string_def(* this, & result); } + void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this, & result); } #endif Using_CodeOps(CodeConstructor); @@ -225,11 +257,11 @@ struct CodeConstructor struct CodeDefine { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeDefine ); - String to_string(); - void to_string( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps(CodeDefine); @@ -240,12 +272,12 @@ struct CodeDefine struct CodeDestructor { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeDestructor ); - String to_string(); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string_def( String& result ) { return GEN_NS to_string_def(* this, & result); } + void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this, & result); } #endif Using_CodeOps(CodeDestructor); @@ -256,14 +288,14 @@ struct CodeDestructor struct CodeEnum { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeEnum ); - String to_string(); - void to_string_def( String& result ); - void to_string_fwd( String& result ); - void to_string_class_def( String& result ); - void to_string_class_fwd( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string_def( String& result ) { return GEN_NS to_string_def(* this); } + void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this); } + void to_string_class_def( String& result ) { return GEN_NS to_string_class_def(* this); } + void to_string_class_fwd( String& result ) { return GEN_NS to_string_class_fwd(* this); } #endif Using_CodeOps(CodeEnum); @@ -274,9 +306,9 @@ struct CodeEnum struct CodeExec { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code(CodeExec); - String to_string(); + String to_string() { return GEN_NS to_string(* this); } #endif Using_CodeOps(CodeExec); @@ -493,10 +525,10 @@ struct CodeExpr_UnaryPostfix struct CodeExtern { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeExtern ); - void to_string( String& result ); + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps(CodeExtern); @@ -507,11 +539,11 @@ struct CodeExtern struct CodeInclude { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeInclude ); - String to_string(); - void to_string( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps(CodeInclude); @@ -522,11 +554,11 @@ struct CodeInclude struct CodeFriend { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeFriend ); - String to_string(); - void to_string( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps(CodeFriend); @@ -537,12 +569,12 @@ struct CodeFriend struct CodeFn { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeFn ); - String to_string(); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string_def( String& result ) { return GEN_NS to_string_def(* this); } + void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this); } #endif Using_CodeOps(CodeFn); @@ -933,19 +965,4 @@ void to_string_export( CodeBody body, String& result ) { return to_string_export #endif //if ! GEN_COMPILER_C -inline -CodeParam begin(CodeParam params) -{ - if ( params.ast ) - return { params.ast }; - - return { nullptr }; -} -inline -CodeParam end(CodeParam params) -{ - // return { (AST_Param*) rcast( AST*, ast)->Last }; - return { nullptr }; -} - #pragma endregion Code Types diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index 2884da4..17b7ac9 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -264,6 +264,20 @@ CodeParam& CodeParam::operator ++() ast = ast->Next.ast; return * this; } +inline +CodeParam begin(CodeParam params) +{ + if ( params.ast ) + return { params.ast }; + + return { nullptr }; +} +inline +CodeParam end(CodeParam params) +{ + // return { (AST_Param*) rcast( AST*, ast)->Last }; + return { nullptr }; +} #pragma endregion CodeParam #pragma region CodeSpecifiers From 05e65aa4643c1bf3a9c89cf21c9993f617fa176b Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 18:35:34 -0500 Subject: [PATCH 042/112] Did reductions on Module, NS, Operator, OpCast, Pragma, PreprocessCond, Template, and Type codes --- project/components/ast.cpp | 30 +- project/components/code_serialization.cpp | 324 +++++++++++----------- project/components/code_types.hpp | 101 ++++--- project/components/interface.cpp | 2 +- project/components/interface.upfront.cpp | 28 +- 5 files changed, 258 insertions(+), 227 deletions(-) diff --git a/project/components/ast.cpp b/project/components/ast.cpp index 3f262fc..b92b260 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -475,29 +475,29 @@ void to_string( AST* self, String* result ) break; case Module: - cast(CodeModule, {self}).to_string( * result ); + to_string(cast(CodeModule, {self}), result ); break; case Namespace: - cast(CodeNS, {self}).to_string( * result ); + to_string(cast(CodeNS, {self}), result ); break; case Operator: case Operator_Member: - cast(CodeOperator, {self}).to_string_def( * result ); + to_string_def(cast(CodeOperator, {self}), result ); break; case Operator_Fwd: case Operator_Member_Fwd: - cast(CodeOperator, {self}).to_string_fwd( * result ); + to_string_fwd(cast(CodeOperator, {self}), result ); break; case Operator_Cast: - cast(CodeOpCast, {self}).to_string_def( * result ); + to_string_def(cast(CodeOpCast, {self}), result ); break; case Operator_Cast_Fwd: - cast(CodeOpCast, {self}).to_string_fwd( * result ); + to_string_fwd(cast(CodeOpCast, {self}), result ); break; case Parameters: @@ -509,15 +509,15 @@ void to_string( AST* self, String* result ) break; case Preprocess_If: - cast(CodePreprocessCond, {self}).to_string_if( * result ); + to_string_if(cast(CodePreprocessCond, {self}), result ); break; case Preprocess_IfDef: - cast(CodePreprocessCond, {self}).to_string_ifdef( * result ); + to_string_ifdef(cast(CodePreprocessCond, {self}), result ); break; case Preprocess_IfNotDef: - cast(CodePreprocessCond, {self}).to_string_ifndef( * result ); + to_string_ifndef(cast(CodePreprocessCond, {self}), result ); break; case Preprocess_Include: @@ -525,19 +525,19 @@ void to_string( AST* self, String* result ) break; case Preprocess_ElIf: - cast(CodePreprocessCond, {self}).to_string_elif( * result ); + to_string_elif(cast(CodePreprocessCond, {self}), result ); break; case Preprocess_Else: - cast(CodePreprocessCond, {self}).to_string_else( * result ); + to_string_else(cast(CodePreprocessCond, {self}), result ); break; case Preprocess_EndIf: - cast(CodePreprocessCond, {self}).to_string_endif( * result ); + to_string_endif(cast(CodePreprocessCond, {self}), result ); break; case Preprocess_Pragma: - cast(CodePragma, {self}).to_string( * result ); + to_string(cast(CodePragma, {self}), result ); break; case Specifiers: @@ -553,7 +553,7 @@ void to_string( AST* self, String* result ) break; case Template: - cast(CodeTemplate, {self}).to_string( * result ); + to_string(cast(CodeTemplate, {self}), result ); break; case Typedef: @@ -561,7 +561,7 @@ void to_string( AST* self, String* result ) break; case Typename: - cast(CodeType, {self}).to_string( * result ); + to_string(cast(CodeType, {self}), result ); break; case Union: diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 3304678..71c1ccf 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -181,7 +181,7 @@ void to_string_def( CodeClass self, String* result ) { char const* access_level = to_str( ast->ParentAccess ); - append_fmt( result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); + append_fmt( result, "%S : %s %S", ast->Name, access_level, to_string(ast->ParentType) ); CodeType interface = cast(CodeType, ast->ParentType->Next); if ( interface ) @@ -189,7 +189,7 @@ void to_string_def( CodeClass self, String* result ) while ( interface ) { - append_fmt( result, ", %S", interface.to_string() ); + append_fmt( result, ", %S", to_string(interface) ); interface = interface->Next ? cast(CodeType, interface->Next) : CodeType { nullptr }; } } @@ -337,7 +337,7 @@ void to_string_def(CodeEnum self, String* result ) if ( self->UnderlyingType ) append_fmt( result, "%S : %S\n{\n%S\n}" , self->Name - , self->UnderlyingType.to_string() + , to_string(self->UnderlyingType) , GEN_NS to_string(self->Body) ); else if ( self->UnderlyingTypeMacro ) @@ -364,7 +364,7 @@ void to_string_fwd(CodeEnum self, String* result ) append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); if ( self->UnderlyingType ) - append_fmt( result, "enum %S : %S", self->Name, self->UnderlyingType.to_string() ); + append_fmt( result, "enum %S : %S", self->Name, to_string(self->UnderlyingType) ); else append_fmt( result, "enum %S", self->Name ); @@ -393,7 +393,7 @@ void to_string_class_def(CodeEnum self, String* result ) if ( self->UnderlyingType ) { - append_fmt( result, "%S : %S\n{\n%S\n}", self->Name, self->UnderlyingType.to_string(), GEN_NS to_string(self->Body) ); + append_fmt( result, "%S : %S\n{\n%S\n}", self->Name, to_string(self->UnderlyingType), GEN_NS to_string(self->Body) ); } else { @@ -419,7 +419,7 @@ void to_string_class_fwd(CodeEnum self, String* result ) if ( self->Attributes ) append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); - append_fmt( result, "%S : %S", self->Name, self->UnderlyingType.to_string() ); + append_fmt( result, "%S : %S", self->Name, to_string(self->UnderlyingType) ); if ( self->Parent.ast == nullptr || ( self->Parent->Type != ECode::Typedef && self->Parent->Type != ECode::Variable ) ) { @@ -518,7 +518,7 @@ void to_string_def(CodeFn self, String* result ) append( result, "\n" ); if ( self->ReturnType ) - append_fmt( result, "%S %S(", self->ReturnType.to_string(), self->Name ); + append_fmt( result, "%S %S(", to_string(self->ReturnType), self->Name ); else append_fmt( result, "%S(", self->Name ); @@ -573,7 +573,7 @@ void to_string_fwd(CodeFn self, String* result ) } if ( self->ReturnType ) - append_fmt( result, "%S %S(", self->ReturnType.to_string(), self->Name ); + append_fmt( result, "%S %S(", to_string(self->ReturnType), self->Name ); else append_fmt( result, "%S(", self->Name ); @@ -607,250 +607,250 @@ void to_string_fwd(CodeFn self, String* result ) append( result, ";\n" ); } -String CodeModule::to_string() +String to_string(CodeModule self) { String result = string_make( GlobalAllocator, "" ); - to_string( result ); + to_string( self, & result ); return result; } -void CodeModule::to_string( String& result ) +void to_string(CodeModule self, String* result ) { - if (((u32(ModuleFlag_Export) & u32(ast->ModuleFlags)) == u32(ModuleFlag_Export))) - append( & result, "export "); + if (((u32(ModuleFlag_Export) & u32(self->ModuleFlags)) == u32(ModuleFlag_Export))) + append( result, "export "); - if (((u32(ModuleFlag_Import) & u32(ast->ModuleFlags)) == u32(ModuleFlag_Import))) - append( & result, "import "); + if (((u32(ModuleFlag_Import) & u32(self->ModuleFlags)) == u32(ModuleFlag_Import))) + append( result, "import "); - append_fmt( & result, "%S;\n", ast->Name ); + append_fmt( result, "%S;\n", self->Name ); } -String CodeNS::to_string() +String to_string(CodeNS self) { String result = string_make( GlobalAllocator, "" ); - to_string( result ); + to_string( self, & result ); return result; } -void CodeNS::to_string( String& result ) +void to_string(CodeNS self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - append_fmt( & result, "namespace %S\n{\n%S\n}\n", ast->Name , GEN_NS to_string(ast->Body) ); + append_fmt( result, "namespace %S\n{\n%S\n}\n", self->Name, to_string(self->Body) ); } -String CodeOperator::to_string() +String to_string(CodeOperator self) { String result = string_make( GlobalAllocator, "" ); - switch ( ast->Type ) + switch ( self->Type ) { using namespace ECode; case Operator: case Operator_Member: - to_string_def( result ); + to_string_def( self, & result ); break; case Operator_Fwd: case Operator_Member_Fwd: - to_string_fwd( result ); + to_string_fwd( self, & result ); break; } return result; } -void CodeOperator::to_string_def( String& result ) +void to_string_def(CodeOperator self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - if ( ast->Attributes ) - append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); - if ( ast->Attributes ) - append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); - if ( ast->Specs ) + if ( self->Specs ) { - for ( SpecifierT spec : ast->Specs ) + for ( SpecifierT spec : self->Specs ) { if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } - if ( ast->Attributes || ast->Specs ) + if ( self->Attributes || self->Specs ) { - append( & result, "\n" ); + append( result, "\n" ); } - if ( ast->ReturnType ) - append_fmt( & result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); + if ( self->ReturnType ) + append_fmt( result, "%S %S (", GEN_NS to_string(self->ReturnType), self->Name ); - if ( ast->Params ) - append_fmt( & result, "%S)", GEN_NS to_string(ast->Params) ); + if ( self->Params ) + append_fmt( result, "%S)", GEN_NS to_string(self->Params) ); else - append( & result, ")" ); + append( result, ")" ); - if ( ast->Specs ) + if ( self->Specs ) { - for ( SpecifierT spec : ast->Specs ) + for ( SpecifierT spec : self->Specs ) { if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } - append_fmt( & result, "\n{\n%S\n}\n" - , GEN_NS to_string(ast->Body) + append_fmt( result, "\n{\n%S\n}\n" + , GEN_NS to_string(self->Body) ); } -void CodeOperator::to_string_fwd( String& result ) +void to_string_fwd(CodeOperator self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - if ( ast->Attributes ) - append_fmt( & result, "%S\n",GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, "%S\n",GEN_NS to_string(self->Attributes) ); - if ( ast->Specs ) + if ( self->Specs ) { - for ( SpecifierT spec : ast->Specs ) + for ( SpecifierT spec : self->Specs ) { if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } - if ( ast->Attributes || ast->Specs ) + if ( self->Attributes || self->Specs ) { - append( & result, "\n" ); + append( result, "\n" ); } - append_fmt( & result, "%S %S (", ast->ReturnType.to_string(), ast->Name ); + append_fmt( result, "%S %S (", to_string(self->ReturnType), self->Name ); - if ( ast->Params ) - append_fmt( & result, "%S)", GEN_NS to_string(ast->Params) ); + if ( self->Params ) + append_fmt( result, "%S)", GEN_NS to_string(self->Params) ); else - append_fmt( & result, ")" ); + append_fmt( result, ")" ); - if ( ast->Specs ) + if ( self->Specs ) { - for ( SpecifierT spec : ast->Specs ) + for ( SpecifierT spec : self->Specs ) { if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } } - if ( ast->InlineCmt ) - append_fmt( & result, "; %S", ast->InlineCmt->Content ); + if ( self->InlineCmt ) + append_fmt( result, "; %S", self->InlineCmt->Content ); else - append( & result, ";\n" ); + append( result, ";\n" ); } -String CodeOpCast::to_string() +String to_string(CodeOpCast self) { String result = string_make( GlobalAllocator, "" ); - switch ( ast->Type ) + switch ( self->Type ) { using namespace ECode; case Operator_Cast: - to_string_def( result ); + to_string_def(self, & result ); break; case Operator_Cast_Fwd: - to_string_fwd( result ); + to_string_fwd(self, & result ); break; } return result; } -void CodeOpCast::to_string_def( String& result ) +void to_string_def(CodeOpCast self, String* result ) { - if ( ast->Specs ) + if ( self->Specs ) { - for ( SpecifierT spec : ast->Specs ) + for ( SpecifierT spec : self->Specs ) { if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( & result, "%*s ", spec_str.Len, spec_str.Ptr ); + append_fmt( result, "%*s ", spec_str.Len, spec_str.Ptr ); } } - if ( ast->Name && length(ast->Name) ) - append_fmt( & result, "%Soperator %S()", ast->Name, ast->ValueType.to_string() ); + if ( self->Name && length(self->Name) ) + append_fmt( result, "%Soperator %S()", self->Name, to_string(self->ValueType) ); else - append_fmt( & result, "operator %S()", ast->ValueType.to_string() ); + append_fmt( result, "operator %S()", to_string(self->ValueType) ); - for ( SpecifierT spec : ast->Specs ) + for ( SpecifierT spec : self->Specs ) { if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( & result, " %.*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %.*s", spec_str.Len, spec_str.Ptr ); } } - append_fmt( & result, "\n{\n%S\n}\n", GEN_NS to_string(ast->Body) ); + append_fmt( result, "\n{\n%S\n}\n", GEN_NS to_string(self->Body) ); return; } - if ( ast->Name && length(ast->Name) ) - append_fmt( & result, "%Soperator %S()\n{\n%S\n}\n", ast->Name, ast->ValueType.to_string(), GEN_NS to_string(ast->Body) ); + if ( self->Name && length(self->Name) ) + append_fmt( result, "%Soperator %S()\n{\n%S\n}\n", self->Name, to_string(self->ValueType), GEN_NS to_string(self->Body) ); else - append_fmt( & result, "operator %S()\n{\n%S\n}\n", ast->ValueType.to_string(), GEN_NS to_string(ast->Body) ); + append_fmt( result, "operator %S()\n{\n%S\n}\n", to_string(self->ValueType), GEN_NS to_string(self->Body) ); } -void CodeOpCast::to_string_fwd( String& result ) +void to_string_fwd(CodeOpCast self, String* result ) { - if ( ast->Specs ) + if ( self->Specs ) { - for ( SpecifierT spec : ast->Specs ) + for ( SpecifierT spec : self->Specs ) { if ( ! ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( & result, "%*s ", spec_str.Len, spec_str.Ptr ); + append_fmt( result, "%*s ", spec_str.Len, spec_str.Ptr ); } } - append_fmt( & result, "operator %S()", ast->ValueType.to_string() ); + append_fmt( result, "operator %S()", to_string(self->ValueType) ); - for ( SpecifierT spec : ast->Specs ) + for ( SpecifierT spec : self->Specs ) { if ( ESpecifier::is_trailing( spec ) ) { StrC spec_str = ESpecifier::to_str( spec ); - append_fmt( & result, " %*s", spec_str.Len, spec_str.Ptr ); + append_fmt( result, " %*s", spec_str.Len, spec_str.Ptr ); } } - if ( ast->InlineCmt ) - append_fmt( & result, "; %S", ast->InlineCmt->Content ); + if ( self->InlineCmt ) + append_fmt( result, "; %S", self->InlineCmt->Content ); else - append( & result, ";\n" ); + append( result, ";\n" ); return; } - if ( ast->InlineCmt ) - append_fmt( & result, "operator %S(); %S", ast->ValueType.to_string() ); + if ( self->InlineCmt ) + append_fmt( result, "operator %S(); %S", to_string(self->ValueType) ); else - append_fmt( & result, "operator %S();\n", ast->ValueType.to_string() ); + append_fmt( result, "operator %S();\n", to_string(self->ValueType) ); } String to_string(CodeParam self) @@ -875,11 +875,11 @@ void to_string( CodeParam self, String* result ) if ( ast->ValueType.ast == nullptr ) append_fmt( result, " %S", ast->Name ); else - append_fmt( result, " %S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( result, " %S %S", to_string(ast->ValueType), ast->Name ); } else if ( ast->ValueType ) - append_fmt( result, " %S", ast->ValueType.to_string() ); + append_fmt( result, " %S", to_string(ast->ValueType) ); if ( ast->PostNameMacro ) { @@ -898,74 +898,74 @@ void to_string( CodeParam self, String* result ) } } -String CodePreprocessCond::to_string() +String to_string(CodePreprocessCond self) { String result = string_make( GlobalAllocator, "" ); - switch ( ast->Type ) + switch ( self->Type ) { using namespace ECode; case Preprocess_If: - to_string_if( result ); + to_string_if( self, & result ); break; case Preprocess_IfDef: - to_string_ifdef( result ); + to_string_ifdef( self, & result ); break; case Preprocess_IfNotDef: - to_string_ifndef( result ); + to_string_ifndef( self, & result ); break; case Preprocess_ElIf: - to_string_elif( result ); + to_string_elif( self, & result ); break; case Preprocess_Else: - to_string_else( result ); + to_string_else( self, & result ); break; case Preprocess_EndIf: - to_string_endif( result ); + to_string_endif( self, & result ); break; } return result; } -void CodePreprocessCond::to_string_if( String& result ) +void to_string_if(CodePreprocessCond cond, String* result ) { - append_fmt( & result, "#if %S\n", ast->Content ); + append_fmt( result, "#if %S\n", cond->Content ); } -void CodePreprocessCond::to_string_ifdef( String& result ) +void to_string_ifdef(CodePreprocessCond cond, String* result ) { - append_fmt( & result, "#ifdef %S\n", ast->Content ); + append_fmt( result, "#ifdef %S\n", cond->Content ); } -void CodePreprocessCond::to_string_ifndef( String& result ) +void to_string_ifndef(CodePreprocessCond cond, String* result ) { - append_fmt( & result, "#ifndef %S\n", ast->Content ); + append_fmt( result, "#ifndef %S\n", cond->Content ); } -void CodePreprocessCond::to_string_elif( String& result ) +void to_string_elif(CodePreprocessCond cond, String* result ) { - append_fmt( & result, "#elif %S\n", ast->Content ); + append_fmt( result, "#elif %S\n", cond->Content ); } -void CodePreprocessCond::to_string_else( String& result ) +void to_string_else(CodePreprocessCond cond, String* result ) { - append_fmt( & result, "#else\n" ); + append_fmt( result, "#else\n" ); } -void CodePreprocessCond::to_string_endif( String& result ) +void to_string_endif(CodePreprocessCond cond, String* result ) { - append_fmt( & result, "#endif\n" ); + append_fmt( result, "#endif\n" ); } -String CodePragma::to_string() +String to_string(CodePragma self) { String result = string_make( GlobalAllocator, "" ); - to_string( result ); + to_string( self, & result ); return result; } -void CodePragma::to_string( String& result ) +void to_string(CodePragma self, String* result ) { - append_fmt( & result, "#pragma %S\n", ast->Content ); + append_fmt( result, "#pragma %S\n", self->Content ); } String to_string(CodeSpecifiers self) @@ -1025,7 +1025,7 @@ void to_string_def( CodeStruct self, String* result ) { char const* access_level = to_str( ast->ParentAccess ); - append_fmt( result, "%S : %s %S", ast->Name, access_level, ast->ParentType.to_string() ); + append_fmt( result, "%S : %s %S", ast->Name, access_level, GEN_NS to_string(ast->ParentType) ); CodeType interface = cast(CodeType, ast->ParentType->Next); if ( interface ) @@ -1033,7 +1033,7 @@ void to_string_def( CodeStruct self, String* result ) while ( interface ) { - append_fmt( result, ", %S", interface.to_string() ); + append_fmt( result, ", %S", GEN_NS to_string(interface) ); interface = interface->Next ? cast( CodeType, interface->Next) : CodeType { nullptr }; } } @@ -1062,7 +1062,7 @@ void to_string_fwd( CodeStruct self, String* result ) append( result, "export " ); if ( ast->Attributes ) - append_fmt( result, "struct %S %S",GEN_NS to_string(ast->Attributes), ast->Name ); + append_fmt( result, "struct %S %S", GEN_NS to_string(ast->Attributes), ast->Name ); else append_fmt( result, "struct %S", ast->Name ); @@ -1075,22 +1075,22 @@ void to_string_fwd( CodeStruct self, String* result ) } } -String CodeTemplate::to_string() +String to_string(CodeTemplate self) { String result = string_make( GlobalAllocator, "" ); - to_string( result ); + to_string( self, & result ); return result; } -void CodeTemplate::to_string( String& result ) +void to_string(CodeTemplate self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - if ( ast->Params ) - append_fmt( & result, "template< %S >\n%S", GEN_NS to_string(ast->Params), GEN_NS to_string(ast->Declaration) ); + if ( self->Params ) + append_fmt( result, "template< %S >\n%S", GEN_NS to_string(self->Params), GEN_NS to_string(self->Declaration) ); else - append_fmt( & result, "template<>\n%S", GEN_NS to_string(ast->Declaration) ); + append_fmt( result, "template<>\n%S", GEN_NS to_string(self->Declaration) ); } String CodeTypedef::to_string() @@ -1135,57 +1135,57 @@ void CodeTypedef::to_string( String& result ) append( & result, "\n"); } -String CodeType::to_string() +String to_string(CodeType self) { String result = string_make( GlobalAllocator, "" ); - to_string( result ); + to_string( self, & result ); return result; } -void CodeType::to_string( String& result ) +void to_string(CodeType self, String* result ) { #if defined(GEN_USE_NEW_TYPENAME_PARSING) - if ( ast->ReturnType && ast->Params ) + if ( self->ReturnType && self->Params ) { - if ( ast->Attributes ) - append_fmt( result, "%S ",GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); else { - if ( ast->Specs ) - append_fmt( & result, "%S ( %S ) ( %S ) %S", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string(), ast->Specs.to_string() ); + if ( self->Specs ) + append_fmt( result, "%S ( %S ) ( %S ) %S", self->ReturnType.to_string(), self->Name, self->Params.to_string(), self->Specs.to_string() ); else - append_fmt( & result, "%S ( %S ) ( %S )", ast->ReturnType.to_string(), ast->Name, ast->Params.to_string() ); + append_fmt( result, "%S ( %S ) ( %S )", self->ReturnType.to_string(), self->Name, self->Params.to_string() ); } break; } #else - if ( ast->ReturnType && ast->Params ) + if ( self->ReturnType && self->Params ) { - if ( ast->Attributes ) - append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); else { - if ( ast->Specs ) - append_fmt( & result, "%S %S ( %S ) %S", ast->ReturnType.to_string(), ast->Name, GEN_NS to_string(ast->Params), GEN_NS to_string(ast->Specs) ); + if ( self->Specs ) + append_fmt( result, "%S %S ( %S ) %S", GEN_NS to_string(self->ReturnType), self->Name, GEN_NS to_string(self->Params), GEN_NS to_string(self->Specs) ); else - append_fmt( & result, "%S %S ( %S )", ast->ReturnType.to_string(), ast->Name, GEN_NS to_string(ast->Params) ); + append_fmt( result, "%S %S ( %S )", GEN_NS to_string(self->ReturnType), self->Name, GEN_NS to_string(self->Params) ); } return; } #endif - if ( ast->Attributes ) - append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); - if ( ast->Specs ) - append_fmt( & result, "%S %S", ast->Name, GEN_NS to_string(ast->Specs) ); + if ( self->Specs ) + append_fmt( result, "%S %S", self->Name, GEN_NS to_string(self->Specs) ); else - append_fmt( & result, "%S", ast->Name ); + append_fmt( result, "%S", self->Name ); - if ( ast->IsParamPack ) - append( & result, "..."); + if ( self->IsParamPack ) + append( result, "..."); } String CodeUnion::to_string() @@ -1250,7 +1250,7 @@ void CodeUsing::to_string( String& result ) if ( ast->UnderlyingType ) { - append_fmt( & result, "using %S = %S", ast->Name, ast->UnderlyingType.to_string() ); + append_fmt( & result, "using %S = %S", ast->Name, GEN_NS to_string(ast->UnderlyingType) ); if ( ast->UnderlyingType->ArrExpr ) { @@ -1342,7 +1342,7 @@ void CodeVar::to_string( String& result ) if ( ast->Specs ) append_fmt( & result, "%S\n", GEN_NS to_string(ast->Specs) ); - append_fmt( & result, "%S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( & result, "%S %S", GEN_NS to_string(ast->ValueType), ast->Name ); if ( ast->ValueType->ArrExpr ) { @@ -1382,11 +1382,11 @@ void CodeVar::to_string( String& result ) } if ( ast->BitfieldSize ) - append_fmt( & result, "%S %S : %S", ast->ValueType.to_string(), ast->Name, GEN_NS to_string(ast->BitfieldSize) ); + append_fmt( & result, "%S %S : %S", GEN_NS to_string(ast->ValueType), ast->Name, GEN_NS to_string(ast->BitfieldSize) ); else if ( ast->ValueType->ArrExpr ) { - append_fmt( & result, "%S %S[ %S ]", ast->ValueType.to_string(), ast->Name, GEN_NS to_string(ast->ValueType->ArrExpr) ); + append_fmt( & result, "%S %S[ %S ]", GEN_NS to_string(ast->ValueType), ast->Name, GEN_NS to_string(ast->ValueType->ArrExpr) ); AST* next_arr_expr = ast->ValueType->ArrExpr->Next; while ( next_arr_expr ) @@ -1397,7 +1397,7 @@ void CodeVar::to_string( String& result ) } else - append_fmt( & result, "%S %S", ast->ValueType.to_string(), ast->Name ); + append_fmt( & result, "%S %S", GEN_NS to_string(ast->ValueType), ast->Name ); if ( ast->Value ) { diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index 302b667..5659f13 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -74,6 +74,37 @@ String to_string (CodeFn self); void to_string_def(CodeFn self, String* result); void to_string_fwd(CodeFn self, String* result); +String to_string(CodeModule self); +void to_string(CodeModule self, String* result); + +String to_string(CodeNS self); +void to_string(CodeNS self, String* result); + +String to_string (CodeOperator self); +void to_string_fwd(CodeOperator self, String* result ); +void to_string_def(CodeOperator self, String* result ); + +String to_string (CodeOpCast op_cast ); +void to_string_def(CodeOpCast op_cast, String* result ); +void to_string_fwd(CodeOpCast op_cast, String* result ); + +String to_string(CodePragma self); +void to_string(CodePragma self, String* result); + +String to_string (CodePreprocessCond cond); +void to_string_if (CodePreprocessCond cond, String* result ); +void to_string_ifdef (CodePreprocessCond cond, String* result ); +void to_string_ifndef(CodePreprocessCond cond, String* result ); +void to_string_elif (CodePreprocessCond cond, String* result ); +void to_string_else (CodePreprocessCond cond, String* result ); +void to_string_endif (CodePreprocessCond cond, String* result ); + +String to_string(CodeTemplate self); +void to_string(CodeTemplate self, String* result); + +String to_string(CodeType self); +void to_string(CodeType self, String* result); + #pragma region Code Types // These structs are not used at all by the C vairant. #if ! GEN_COMPILER_C @@ -292,10 +323,10 @@ struct CodeEnum Using_Code( CodeEnum ); String to_string() { return GEN_NS to_string(* this); } - void to_string_def( String& result ) { return GEN_NS to_string_def(* this); } - void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this); } - void to_string_class_def( String& result ) { return GEN_NS to_string_class_def(* this); } - void to_string_class_fwd( String& result ) { return GEN_NS to_string_class_fwd(* this); } + void to_string_def( String& result ) { return GEN_NS to_string_def(* this, & result); } + void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this, & result); } + void to_string_class_def( String& result ) { return GEN_NS to_string_class_def(* this, & result); } + void to_string_class_fwd( String& result ) { return GEN_NS to_string_class_fwd(* this, & result); } #endif Using_CodeOps(CodeEnum); @@ -573,8 +604,8 @@ struct CodeFn Using_Code( CodeFn ); String to_string() { return GEN_NS to_string(* this); } - void to_string_def( String& result ) { return GEN_NS to_string_def(* this); } - void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this); } + void to_string_def( String& result ) { return GEN_NS to_string_def(* this, & result); } + void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this, & result); } #endif Using_CodeOps(CodeFn); @@ -585,11 +616,11 @@ struct CodeFn struct CodeModule { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeModule ); - String to_string(); - void to_string( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps(CodeModule); @@ -600,11 +631,11 @@ struct CodeModule struct CodeNS { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeNS ); - String to_string(); - void to_string( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps(CodeNS); @@ -615,12 +646,12 @@ struct CodeNS struct CodeOperator { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeOperator ); - String to_string(); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string_def( String& result ) { return GEN_NS to_string_def(* this, & result); } + void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this, & result); } #endif Using_CodeOps(CodeOperator); @@ -631,12 +662,12 @@ struct CodeOperator struct CodeOpCast { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeOpCast ); - String to_string(); - void to_string_def( String& result ); - void to_string_fwd( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string_def( String& result ) { return GEN_NS to_string_def(* this, & result); } + void to_string_fwd( String& result ) { return GEN_NS to_string_fwd(* this, & result); } #endif Using_CodeOps(CodeOpCast); @@ -650,8 +681,8 @@ struct CodePragma #if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodePragma ); - String to_string(); - void to_string( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps( CodePragma ); @@ -662,16 +693,16 @@ struct CodePragma struct CodePreprocessCond { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodePreprocessCond ); - String to_string(); - void to_string_if( String& result ); - void to_string_ifdef( String& result ); - void to_string_ifndef( String& result ); - void to_string_elif( String& result ); - void to_string_else( String& result ); - void to_string_endif( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string_if( String& result ) { return GEN_NS to_string_if(* this, & result); } + void to_string_ifdef( String& result ) { return GEN_NS to_string_ifdef(* this, & result); } + void to_string_ifndef( String& result ) { return GEN_NS to_string_ifndef(* this, & result); } + void to_string_elif( String& result ) { return GEN_NS to_string_elif(* this, & result); } + void to_string_else( String& result ) { return GEN_NS to_string_else(* this, & result); } + void to_string_endif( String& result ) { return GEN_NS to_string_endif(* this, & result); } #endif Using_CodeOps( CodePreprocessCond ); @@ -869,8 +900,8 @@ struct CodeTemplate #if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeTemplate ); - String to_string(); - void to_string( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps( CodeTemplate ); @@ -881,11 +912,11 @@ struct CodeTemplate struct CodeType { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeType ); - String to_string(); - void to_string( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps( CodeType ); diff --git a/project/components/interface.cpp b/project/components/interface.cpp index 63b9917..97049b6 100644 --- a/project/components/interface.cpp +++ b/project/components/interface.cpp @@ -145,7 +145,7 @@ void define_constants() # define def_constant_code_type( Type_ ) \ t_##Type_ = def_type( name(Type_) ); \ - t_##Type_.set_global(); + set_global(t_##Type_); def_constant_code_type( auto ); def_constant_code_type( void ); diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index 3d0c197..55a08c4 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -36,14 +36,14 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy } # define check_param_eq_ret() \ - if ( ! is_member_symbol && ! params_code->ValueType.is_equal( ret_type) ) \ + if ( ! is_member_symbol && ! is_equal(params_code->ValueType, ret_type) ) \ { \ log_failure("gen::def_operator: operator%s requires first parameter to equal return type\n" \ "param types: %s\n" \ "return type: %s", \ to_str(op).Ptr, \ debug_str(params_code), \ - ret_type.debug_str() \ + debug_str(ret_type) \ ); \ return OpValidateResult::Fail; \ } @@ -56,7 +56,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy if ( ret_type->Type != ECode::Typename ) { - log_failure("gen::def_operator: ret_type is not of typename type - %s", ret_type.debug_str()); + log_failure("gen::def_operator: ret_type is not of typename type - %s", debug_str(ret_type)); return OpValidateResult::Fail; } @@ -127,7 +127,7 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy switch ( params_code->NumEntries ) { case 1: - if ( params_code->ValueType.is_equal( t_int ) ) + if ( is_equal(params_code->ValueType, t_int ) ) is_member_symbol = true; else @@ -170,14 +170,14 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy return OpValidateResult::Fail; } - if ( params_code->ValueType.is_equal( ret_type ) ) + if ( is_equal(params_code->ValueType, ret_type ) ) { log_failure("gen::def_operator: " "operator%s is non-member symbol yet first paramter does not equal return type\n" "param type: %s\n" "return type: %s\n" , debug_str(params_code) - , ret_type.debug_str() + , debug_str(ret_type) ); return OpValidateResult::Fail; } @@ -247,14 +247,14 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy break; case 2: - if ( ! params_code->ValueType.is_equal( ret_type ) ) + if ( ! is_equal(params_code->ValueType, ret_type ) ) { log_failure("gen::def_operator: " "operator%s is non-member symbol yet first paramter does not equal return type\n" "param type: %s\n" "return type: %s\n" , debug_str(params_code) - , ret_type.debug_str() + , debug_str(ret_type) ); return OpValidateResult::Fail; } @@ -291,11 +291,11 @@ OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeTy } } - if ( ! ret_type.is_equal( t_bool )) + if ( ! is_equal(ret_type, t_bool )) { log_failure("gen::def_operator: operator%s return type must be of type bool - %s" , to_str(op) - , ret_type.debug_str() + , debug_str(ret_type) ); return OpValidateResult::Fail; } @@ -561,7 +561,7 @@ CodeClass def_class( StrC name if ( parent && ( parent->Type != Class && parent->Type != Struct && parent->Type != Typename && parent->Type != Untyped ) ) { - log_failure( "gen::def_class: parent provided is not type 'Class', 'Struct', 'Typeanme', or 'Untyped': %s", parent.debug_str() ); + log_failure( "gen::def_class: parent provided is not type 'Class', 'Struct', 'Typeanme', or 'Untyped': %s", debug_str(parent) ); return InvalidCode; } @@ -691,7 +691,7 @@ CodeEnum def_enum( StrC name if ( type && type->Type != Typename ) { - log_failure( "gen::def_enum: enum underlying type provided was not of type Typename: %s", type.debug_str() ); + log_failure( "gen::def_enum: enum underlying type provided was not of type Typename: %s", debug_str(type) ); return InvalidCode; } @@ -1045,7 +1045,7 @@ CodeOpCast def_operator_cast( CodeType type, Code body, CodeSpecifiers const_spe if ( type->Type != Typename ) { - log_failure( "gen::def_operator_cast: type is not a typename - %s", type.debug_str() ); + log_failure( "gen::def_operator_cast: type is not a typename - %s", debug_str(type) ); return InvalidCode; } @@ -1476,7 +1476,7 @@ CodeVar def_variable( CodeType type, StrC name, Code value if ( type->Type != ECode::Typename ) { - log_failure( "gen::def_variable: type was not a Typename - %s", type.debug_str() ); + log_failure( "gen::def_variable: type was not a Typename - %s", debug_str(type) ); return InvalidCode; } From defe42c15c570537eb9206f6c7197829f19e808f Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 18:58:07 -0500 Subject: [PATCH 043/112] member proc usage reductions on CodeTypes complete (Typedef, Union, Using, Var) proceeding to finalize the AST interface reductions... --- project/components/ast.cpp | 10 +- project/components/ast.hpp | 67 +----- project/components/code_serialization.cpp | 240 +++++++++++----------- project/components/code_types.hpp | 39 +++- 4 files changed, 154 insertions(+), 202 deletions(-) diff --git a/project/components/ast.cpp b/project/components/ast.cpp index b92b260..f08b5f5 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -557,7 +557,7 @@ void to_string( AST* self, String* result ) break; case Typedef: - cast(CodeTypedef, {self}).to_string( * result ); + to_string(cast(CodeTypedef, {self}), result ); break; case Typename: @@ -565,19 +565,19 @@ void to_string( AST* self, String* result ) break; case Union: - cast(CodeUnion, {self}).to_string( * result ); + to_string( cast(CodeUnion, {self}), result ); break; case Using: - cast(CodeUsing, {self}).to_string( * result ); + to_string(cast(CodeUsing, {self}), result ); break; case Using_Namespace: - cast(CodeUsing, {self}).to_string_ns( * result ); + to_string_ns(cast(CodeUsing, {self}), result ); break; case Variable: - cast(CodeVar, {self}).to_string( * result ); + to_string(cast(CodeVar, {self}), result ); break; case Enum_Body: diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 4923199..8770664 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -264,7 +264,6 @@ struct Code_POD { AST* ast; }; - static_assert( sizeof(Code) == sizeof(Code_POD), "ERROR: Code is not POD" ); // Desired width of the AST data structure. @@ -421,73 +420,9 @@ struct AST }; }; -struct AST_POD -{ - union { - struct - { - AST* InlineCmt; // Class, Constructor, Destructor, Enum, Friend, Functon, Operator, OpCast, Struct, Typedef, Using, Variable - AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable - AST* Specs; // Destructor, Function, Operator, Typename, Variable - union { - AST* InitializerList; // Constructor - AST* ParentType; // Class, Struct, ParentType->Next has a possible list of interfaces. - AST* ReturnType; // Function, Operator, Typename - AST* UnderlyingType; // Enum, Typedef - AST* ValueType; // Parameter, Variable - }; - union { - AST* Macro; // Parameter - AST* BitfieldSize; // Variable (Class/Struct Data Member) - AST* Params; // Constructor, Function, Operator, Template, Typename - }; - union { - AST* ArrExpr; // Typename - AST* Body; // Class, Constructr, Destructor, Enum, Friend, Function, Namespace, Struct, Union - AST* Declaration; // Friend, Template - AST* Value; // Parameter, Variable - }; - union { - AST* NextVar; // Variable; Possible way to handle comma separated variables declarations. ( , NextVar->Specs NextVar->Name NextVar->ArrExpr = NextVar->Value ) - AST* SuffixSpecs; // Only used with typenames, to store the function suffix if typename is function signature. ( May not be needed ) - AST* PostNameMacro; // Only used with parameters for specifically UE_REQUIRES (Thanks Unreal) - }; - }; - StringCached Content; // Attributes, Comment, Execution, Include - struct { - SpecifierT ArrSpecs[AST_ArrSpecs_Cap]; // Specifiers - AST* NextSpecs; // Specifiers; If ArrSpecs is full, then NextSpecs is used. - }; - }; - union { - AST* Prev; - AST* Front; - AST* Last; - }; - union { - AST* Next; - AST* Back; - }; - parser::Token* Token; // Reference to starting token, only avaialble if it was derived from parsing. - AST* Parent; - StringCached Name; - CodeT Type; - CodeFlag CodeFlags; - ModuleFlag ModuleFlags; - union { - b32 IsFunction; // Used by typedef to not serialize the name field. - b32 IsParamPack; // Used by typename to know if type should be considered a parameter pack. - OperatorT Op; - AccessSpec ParentAccess; - s32 NumEntries; - s32 VarConstructorInit; // Used by variables to know that initialization is using a constructor expression instead of an assignment expression. - }; -}; - // Its intended for the AST to have equivalent size to its POD. // All extra functionality within the AST namespace should just be syntatic sugar. -static_assert( sizeof(AST) == sizeof(AST_POD), "ERROR: AST IS NOT POD" ); -static_assert( sizeof(AST_POD) == AST_POD_Size, "ERROR: AST POD is not size of AST_POD_Size" ); +static_assert( sizeof(AST) == AST_POD_Size, "ERROR: AST POD is not size of AST_POD_Size" ); // Used when the its desired when omission is allowed in a definition. #define NoCode { nullptr } diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 71c1ccf..06f970b 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -1093,46 +1093,46 @@ void to_string(CodeTemplate self, String* result ) append_fmt( result, "template<>\n%S", GEN_NS to_string(self->Declaration) ); } -String CodeTypedef::to_string() +String to_string(CodeTypedef self) { String result = string_make( GlobalAllocator, "" ); - to_string( result ); + to_string( self, & result ); return result; } -void CodeTypedef::to_string( String& result ) +void to_string(CodeTypedef self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - append( & result, "typedef "); + append( result, "typedef "); // Determines if the typedef is a function typename - if ( ast->UnderlyingType->ReturnType ) - append( & result, GEN_NS to_string(ast->UnderlyingType) ); + if ( self->UnderlyingType->ReturnType ) + append( result, GEN_NS to_string(self->UnderlyingType) ); else - append_fmt( & result, "%S %S", GEN_NS to_string(ast->UnderlyingType), ast->Name ); + append_fmt( result, "%S %S", GEN_NS to_string(self->UnderlyingType), self->Name ); - if ( ast->UnderlyingType->Type == ECode::Typename && ast->UnderlyingType->ArrExpr ) + if ( self->UnderlyingType->Type == ECode::Typename && self->UnderlyingType->ArrExpr ) { - append_fmt( & result, "[ %S ];", GEN_NS to_string(ast->UnderlyingType->ArrExpr) ); + append_fmt( result, "[ %S ];", GEN_NS to_string(self->UnderlyingType->ArrExpr) ); - AST* next_arr_expr = ast->UnderlyingType->ArrExpr->Next; + AST* next_arr_expr = self->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( & result, "[ %S ];", GEN_NS to_string(next_arr_expr) ); + append_fmt( result, "[ %S ];", GEN_NS to_string(next_arr_expr) ); next_arr_expr = next_arr_expr->Next; } } else { - append( & result, ";" ); + append( result, ";" ); } - if ( ast->InlineCmt ) - append_fmt( & result, " %S", ast->InlineCmt->Content); + if ( self->InlineCmt ) + append_fmt( result, " %S", self->InlineCmt->Content); else - append( & result, "\n"); + append( result, "\n"); } String to_string(CodeType self) @@ -1188,235 +1188,235 @@ void to_string(CodeType self, String* result ) append( result, "..."); } -String CodeUnion::to_string() +String to_string(CodeUnion self) { String result = string_make( GlobalAllocator, "" ); - to_string( result ); + to_string( self, & result ); return result; } -void CodeUnion::to_string( String& result ) +void to_string(CodeUnion self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - append( & result, "union " ); + append( result, "union " ); - if ( ast->Attributes ) - append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); - if ( ast->Name ) + if ( self->Name ) { - append_fmt( & result, "%S\n{\n%S\n}" - , ast->Name - , GEN_NS to_string(ast->Body) + append_fmt( result, "%S\n{\n%S\n}" + , self->Name + , GEN_NS to_string(self->Body) ); } else { // Anonymous union - append_fmt( & result, "\n{\n%S\n}" - , GEN_NS to_string(ast->Body) + append_fmt( result, "\n{\n%S\n}" + , GEN_NS to_string(self->Body) ); } - if ( ast->Parent.ast == nullptr || ( ast->Parent->Type != ECode::Typedef && ast->Parent->Type != ECode::Variable ) ) - append( & result, ";\n"); + if ( self->Parent.ast == nullptr || ( self->Parent->Type != ECode::Typedef && self->Parent->Type != ECode::Variable ) ) + append( result, ";\n"); } -String CodeUsing::to_string() +String to_string(CodeUsing self) { String result = string_make( GlobalAllocator, "" ); - switch ( ast->Type ) + switch ( self->Type ) { using namespace ECode; case Using: - to_string( result ); + to_string( self, & result ); break; case Using_Namespace: - to_string_ns( result ); + to_string_ns( self, & result ); break; } return result; } -void CodeUsing::to_string( String& result ) +void to_string(CodeUsing self, String* result ) { - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - if ( ast->Attributes ) - append_fmt( & result, "%S ",GEN_NS to_string(ast->Attributes) ); + if ( self->Attributes ) + append_fmt( result, "%S ",GEN_NS to_string(self->Attributes) ); - if ( ast->UnderlyingType ) + if ( self->UnderlyingType ) { - append_fmt( & result, "using %S = %S", ast->Name, GEN_NS to_string(ast->UnderlyingType) ); + append_fmt( result, "using %S = %S", self->Name, GEN_NS to_string(self->UnderlyingType) ); - if ( ast->UnderlyingType->ArrExpr ) + if ( self->UnderlyingType->ArrExpr ) { - append_fmt( & result, "[ %S ]", GEN_NS to_string(ast->UnderlyingType->ArrExpr) ); + append_fmt( result, "[ %S ]", GEN_NS to_string(self->UnderlyingType->ArrExpr) ); - AST* next_arr_expr = ast->UnderlyingType->ArrExpr->Next; + AST* next_arr_expr = self->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( & result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); + append_fmt( result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); next_arr_expr = next_arr_expr->Next; } } - append( & result, ";" ); + append( result, ";" ); } else - append_fmt( & result, "using %S;", ast->Name ); + append_fmt( result, "using %S;", self->Name ); - if ( ast->InlineCmt ) - append_fmt( & result, " %S\n", ast->InlineCmt->Content ); + if ( self->InlineCmt ) + append_fmt( result, " %S\n", self->InlineCmt->Content ); else - append( & result, "\n"); + append( result, "\n"); } -void CodeUsing::to_string_ns( String& result ) +void to_string_ns(CodeUsing self, String* result ) { - if ( ast->InlineCmt ) - append_fmt( & result, "using namespace $S; %S", ast->Name, ast->InlineCmt->Content ); + if ( self->InlineCmt ) + append_fmt( result, "using namespace $S; %S", self->Name, self->InlineCmt->Content ); else - append_fmt( & result, "using namespace %s;\n", ast->Name ); + append_fmt( result, "using namespace %s;\n", self->Name ); } -String CodeVar::to_string() +String to_string(CodeVar self) { String result = string_make( GlobalAllocator, "" ); - to_string( result ); + to_string( self, & result ); return result; } -void CodeVar::to_string( String& result ) +void to_string(CodeVar self, String* result ) { - if ( ast->Parent && ast->Parent->Type == ECode::Variable ) + if ( self->Parent && self->Parent->Type == ECode::Variable ) { // Its a comma-separated variable ( a NextVar ) - if ( ast->Specs ) - append_fmt( & result, "%S ", GEN_NS to_string(ast->Specs) ); + if ( self->Specs ) + append_fmt( result, "%S ", GEN_NS to_string(self->Specs) ); - append( & result, ast->Name ); + append( result, self->Name ); - if ( ast->ValueType->ArrExpr ) + if ( self->ValueType->ArrExpr ) { - append_fmt( & result, "[ %S ]", GEN_NS to_string(ast->ValueType->ArrExpr) ); + append_fmt( result, "[ %S ]", GEN_NS to_string(self->ValueType->ArrExpr) ); - AST* next_arr_expr = ast->ValueType->ArrExpr->Next; + AST* next_arr_expr = self->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( & result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); + append_fmt( result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); next_arr_expr = next_arr_expr->Next; } } - if ( ast->Value ) + if ( self->Value ) { - if ( ast->VarConstructorInit ) - append_fmt( & result, "( %S ", GEN_NS to_string(ast->Value) ); + if ( self->VarConstructorInit ) + append_fmt( result, "( %S ", GEN_NS to_string(self->Value) ); else - append_fmt( & result, " = %S", GEN_NS to_string(ast->Value) ); + append_fmt( result, " = %S", GEN_NS to_string(self->Value) ); } // Keep the chain going... - if ( ast->NextVar ) - append_fmt( & result, ", %S", ast->NextVar.to_string() ); + if ( self->NextVar ) + append_fmt( result, ", %S", self->NextVar.to_string() ); - if ( ast->VarConstructorInit ) - append( & result, " )"); + if ( self->VarConstructorInit ) + append( result, " )"); return; } - if ( bitfield_is_equal( u32, ast->ModuleFlags, ModuleFlag_Export )) - append( & result, "export " ); + if ( bitfield_is_equal( u32, self->ModuleFlags, ModuleFlag_Export )) + append( result, "export " ); - if ( ast->Attributes || ast->Specs ) + if ( self->Attributes || self->Specs ) { - if ( ast->Attributes ) - append_fmt( & result, "%S ", GEN_NS to_string(ast->Specs) ); + if ( self->Attributes ) + append_fmt( result, "%S ", GEN_NS to_string(self->Specs) ); - if ( ast->Specs ) - append_fmt( & result, "%S\n", GEN_NS to_string(ast->Specs) ); + if ( self->Specs ) + append_fmt( result, "%S\n", GEN_NS to_string(self->Specs) ); - append_fmt( & result, "%S %S", GEN_NS to_string(ast->ValueType), ast->Name ); + append_fmt( result, "%S %S", GEN_NS to_string(self->ValueType), self->Name ); - if ( ast->ValueType->ArrExpr ) + if ( self->ValueType->ArrExpr ) { - append_fmt( & result, "[ %S ]", GEN_NS to_string(ast->ValueType->ArrExpr) ); + append_fmt( result, "[ %S ]", GEN_NS to_string(self->ValueType->ArrExpr) ); - AST* next_arr_expr = ast->ValueType->ArrExpr->Next; + AST* next_arr_expr = self->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( & result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); + append_fmt( result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); next_arr_expr = next_arr_expr->Next; } } - if ( ast->BitfieldSize ) - append_fmt( & result, " : %S", GEN_NS to_string(ast->BitfieldSize) ); + if ( self->BitfieldSize ) + append_fmt( result, " : %S", GEN_NS to_string(self->BitfieldSize) ); - if ( ast->Value ) + if ( self->Value ) { - if ( ast->VarConstructorInit ) - append_fmt( & result, "( %S ", GEN_NS to_string(ast->Value) ); + if ( self->VarConstructorInit ) + append_fmt( result, "( %S ", GEN_NS to_string(self->Value) ); else - append_fmt( & result, " = %S", GEN_NS to_string(ast->Value) ); + append_fmt( result, " = %S", GEN_NS to_string(self->Value) ); } - if ( ast->NextVar ) - append_fmt( & result, ", %S", ast->NextVar.to_string() ); + if ( self->NextVar ) + append_fmt( result, ", %S", self->NextVar.to_string() ); - if ( ast->VarConstructorInit ) - append( & result, " )"); + if ( self->VarConstructorInit ) + append( result, " )"); - if ( ast->InlineCmt ) - append_fmt( & result, "; %S", ast->InlineCmt->Content); + if ( self->InlineCmt ) + append_fmt( result, "; %S", self->InlineCmt->Content); else - append( & result, ";\n" ); + append( result, ";\n" ); return; } - if ( ast->BitfieldSize ) - append_fmt( & result, "%S %S : %S", GEN_NS to_string(ast->ValueType), ast->Name, GEN_NS to_string(ast->BitfieldSize) ); + if ( self->BitfieldSize ) + append_fmt( result, "%S %S : %S", GEN_NS to_string(self->ValueType), self->Name, GEN_NS to_string(self->BitfieldSize) ); - else if ( ast->ValueType->ArrExpr ) + else if ( self->ValueType->ArrExpr ) { - append_fmt( & result, "%S %S[ %S ]", GEN_NS to_string(ast->ValueType), ast->Name, GEN_NS to_string(ast->ValueType->ArrExpr) ); + append_fmt( result, "%S %S[ %S ]", GEN_NS to_string(self->ValueType), self->Name, GEN_NS to_string(self->ValueType->ArrExpr) ); - AST* next_arr_expr = ast->ValueType->ArrExpr->Next; + AST* next_arr_expr = self->ValueType->ArrExpr->Next; while ( next_arr_expr ) { - append_fmt( & result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); + append_fmt( result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); next_arr_expr = next_arr_expr->Next; } } else - append_fmt( & result, "%S %S", GEN_NS to_string(ast->ValueType), ast->Name ); + append_fmt( result, "%S %S", GEN_NS to_string(self->ValueType), self->Name ); - if ( ast->Value ) + if ( self->Value ) { - if ( ast->VarConstructorInit ) - append_fmt( & result, "( %S ", GEN_NS to_string(ast->Value) ); + if ( self->VarConstructorInit ) + append_fmt( result, "( %S ", GEN_NS to_string(self->Value) ); else - append_fmt( & result, " = %S", GEN_NS to_string(ast->Value) ); + append_fmt( result, " = %S", GEN_NS to_string(self->Value) ); } - if ( ast->NextVar ) - append_fmt( & result, ", %S", ast->NextVar.to_string() ); + if ( self->NextVar ) + append_fmt( result, ", %S", self->NextVar.to_string() ); - if ( ast->VarConstructorInit ) - append( & result, " )"); + if ( self->VarConstructorInit ) + append( result, " )"); - append( & result, ";" ); + append( result, ";" ); - if ( ast->InlineCmt ) - append_fmt( & result, " %S", ast->InlineCmt->Content); + if ( self->InlineCmt ) + append_fmt( result, " %S", self->InlineCmt->Content); else - append( & result, "\n"); + append( result, "\n"); } diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index 5659f13..deeb776 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -105,11 +105,26 @@ void to_string(CodeTemplate self, String* result); String to_string(CodeType self); void to_string(CodeType self, String* result); +String to_string(CodeTypedef self); +void to_string(CodeTypedef self, String* result); + +String to_string(CodeUnion self); +void to_string(CodeUnion self, String* result); + +String to_string (CodeUsing op_cast ); +void to_string (CodeUsing op_cast, String* result ); +void to_string_ns(CodeUsing op_cast, String* result ); + +String to_string(CodeVar self); +void to_string(CodeVar self, String* result); + #pragma region Code Types // These structs are not used at all by the C vairant. #if ! GEN_COMPILER_C // stati_assert( GEN_COMPILER_C, "This should not be compiled with the C-library" ); +#define Verify_POD(Type) static_assert(size_of(Code##Type) == size_of(AST_##Type), "ERROR: Code##Type is not a POD") + struct CodeBody { #if GEN_SUPPORT_CPP_MEMBER_FEATURES @@ -897,7 +912,7 @@ struct CodeStmt_While struct CodeTemplate { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeTemplate ); String to_string() { return GEN_NS to_string(* this); } @@ -927,11 +942,11 @@ struct CodeType struct CodeTypedef { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 +#if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( CodeTypedef ); - String to_string(); - void to_string( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps( CodeTypedef ); @@ -945,8 +960,8 @@ struct CodeUnion #if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeUnion ); - String to_string(); - void to_string( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps(CodeUnion); @@ -960,9 +975,9 @@ struct CodeUsing #if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeUsing ); - String to_string(); - void to_string( String& result ); - void to_string_ns( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } + void to_string_ns( String& result ) { return GEN_NS to_string_ns(* this, & result); } #endif Using_CodeOps(CodeUsing); @@ -976,8 +991,8 @@ struct CodeVar #if GEN_SUPPORT_CPP_MEMBER_FEATURES || 1 Using_Code( CodeVar ); - String to_string(); - void to_string( String& result ); + String to_string() { return GEN_NS to_string(* this); } + void to_string( String& result ) { return GEN_NS to_string(* this, & result); } #endif Using_CodeOps(CodeVar); @@ -994,6 +1009,8 @@ struct CodeVar void to_string_export( CodeBody body, String& result ) { return to_string_export(body, & result); }; #endif +#undef Verify_POD + #endif //if ! GEN_COMPILER_C #pragma endregion Code Types From 69a9abcd5972e1ac49323e7a279953b66fe9b5fa Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 20:20:30 -0500 Subject: [PATCH 044/112] Finished AST/Code member inferface usage elimination in base library. Now the lexer and parser need to be elimination... --- gen_c_library/c_library.cpp | 10 +- project/components/ast.cpp | 26 +-- project/components/ast.hpp | 171 ++++++--------- project/components/code_serialization.cpp | 24 +- project/components/code_types.hpp | 38 +++- project/components/gen/ast_inlines.hpp | 256 +++++----------------- project/components/header_end.hpp | 2 +- project/components/inlines.hpp | 120 +++------- project/components/interface.hpp | 46 ++-- project/components/parser.cpp | 58 ++--- project/helpers/helper.hpp | 12 +- scripts/gencpp.refactor | 2 +- test/_old/upfront/Sanity.Upfront.hpp | 4 +- 13 files changed, 277 insertions(+), 492 deletions(-) diff --git a/gen_c_library/c_library.cpp b/gen_c_library/c_library.cpp index 0f9c3bd..6a2edb3 100644 --- a/gen_c_library/c_library.cpp +++ b/gen_c_library/c_library.cpp @@ -157,7 +157,7 @@ int gen_main() CodeFn fn = cast(CodeFn, entry); s32 constexpr_found = fn->Specs.remove( ESpecifier::Constexpr ); if (constexpr_found > -1) { - log_fmt("Found constexpr: %S\n", entry->to_string()); + log_fmt("Found constexpr: %S\n", entry.to_string()); fn->Specs.append(ESpecifier::Inline); } if ( fn->Name.is_equal(txt("free")) ) @@ -189,7 +189,7 @@ int gen_main() case ECode::Class: case ECode::Struct: { - CodeBody body = entry->Body->operator CodeBody(); + CodeBody body = cast(CodeBody, entry->Body); CodeBody new_body = def_body( entry->Body->Type ); for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch (body_entry->Type) { @@ -204,7 +204,7 @@ int gen_main() break; } - entry->Body = rcast(AST*, new_body.ast); + entry->Body = new_body; memory.append(entry); } break; @@ -324,7 +324,7 @@ int gen_main() case ECode::Struct: { - CodeBody body = entry->Body->operator CodeBody(); + CodeBody body = cast(CodeBody, entry->Body); CodeBody new_body = def_body( entry->Body->Type ); for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch (body_entry->Type) { @@ -340,7 +340,7 @@ int gen_main() new_body.append(body_entry); break; } - entry->Body = rcast(AST*, new_body.ast); + entry->Body = new_body; strings.append(entry); } break; diff --git a/project/components/ast.cpp b/project/components/ast.cpp index f08b5f5..90bed56 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -7,7 +7,7 @@ global Code Code_Global; global Code Code_Invalid; // This serializes all the data-members in a "debug" format, where each member is printed with its associated value. -char const* debug_str(AST* self) +char const* debug_str(Code self) { GEN_ASSERT(self != nullptr); String result_stack = string_make_reserve( GlobalAllocator, kilobytes(1) ); @@ -360,26 +360,26 @@ char const* debug_str(AST* self) return * result; } -AST* duplicate(AST* self) +Code duplicate(Code self) { using namespace ECode; - AST* result = make_code().ast; + Code result = make_code(); - mem_copy( result, self, sizeof( AST ) ); + mem_copy( result.ast, self.ast, sizeof( AST ) ); - result->Parent = nullptr; + result->Parent = { nullptr }; return result; } -String to_string(AST* self) +String to_string(Code self) { String result = string_make( GlobalAllocator, "" ); GEN_NS to_string( self, & result ); return result; } -void to_string( AST* self, String* result ) +void to_string( Code self, String* result ) { GEN_ASSERT(self != nullptr); local_persist thread_local @@ -593,7 +593,7 @@ void to_string( AST* self, String* result ) } } -bool is_equal( AST* self, AST* other ) +bool is_equal( Code self, Code other ) { /* AST values are either some u32 value, a cached string, or a pointer to another AST. @@ -910,8 +910,8 @@ bool is_equal( AST* self, AST* other ) { if ( self->NumEntries > 1 ) { - AST* curr = self; - AST* curr_other = other; + Code curr = self; + Code curr_other = other; while ( curr != nullptr ) { if ( curr ) @@ -1104,8 +1104,8 @@ bool is_equal( AST* self, AST* other ) check_member_ast( Front ); check_member_ast( Back ); - AST* curr = self->Front; - AST* curr_other = other->Front; + Code curr = self->Front; + Code curr_other = other->Front; while ( curr != nullptr ) { if ( curr_other == nullptr ) @@ -1153,7 +1153,7 @@ bool is_equal( AST* self, AST* other ) return true; } -bool validate_body(AST* self) +bool validate_body(Code self) { using namespace ECode; diff --git a/project/components/ast.hpp b/project/components/ast.hpp index 8770664..dde624e 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -161,13 +161,21 @@ namespace parser template< class Type> forceinline Type tmpl_cast( Code self ) { return * rcast( Type*, & self ); } #endif -char const* debug_str (Code code); -Code duplicate (Code code); -bool is_equal (Code code, Code other); -bool is_body (Code code); -bool is_valid (Code code); -void set_global(Code code); -String to_string (Code code); +#pragma region Code Interface +void append (Code code, Code other ); +char const* debug_str (Code code); +Code duplicate (Code code); +Code* entry (Code code, u32 idx ); +bool has_entries (Code code); +bool is_body (Code code); +bool is_equal (Code code, Code other); +bool is_valid (Code code); +void set_global (Code code); +String to_string (Code self ); +void to_string (Code self, String* result ); +char const* type_str (Code self ); +bool validate_body(Code self ); +#pragma endregion Code Interface #if ! GEN_COMPILER_C /* @@ -188,7 +196,6 @@ struct Code void set_global() { return GEN_NS set_global(* this); } # define Using_CodeOps( Typename ) \ - Typename& operator = ( AST* other ); \ Typename& operator = ( Code other ); \ bool operator ==( Code other ) { return (AST*)ast == other.ast; } \ bool operator !=( Code other ) { return (AST*)ast != other.ast; } \ @@ -196,7 +203,13 @@ struct Code #if GEN_SUPPORT_CPP_MEMBER_FEATURES Using_Code( Code ); - String to_string() { return GEN_NS to_string(* this); } + void append(Code other) { return GEN_NS append(* this, other); } + Code* entry(u32 idx) { return GEN_NS entry(* this, idx); } + bool has_entries() { return GEN_NS has_entries(* this); } + String to_string() { return GEN_NS to_string(* this); } + void to_string(String& result) { return GEN_NS to_string(* this, & result); } + char const* type_str() { return GEN_NS type_str(* this); } + bool validate_body() { return GEN_NS validate_body(*this); } #endif Using_CodeOps( Code ); @@ -216,6 +229,11 @@ struct Code return *this; } + bool operator==(std::nullptr_t) const { return ast == nullptr; } + bool operator!=(std::nullptr_t) const { return ast != nullptr; } + friend bool operator==(std::nullptr_t, const Code code) { return code.ast == nullptr; } + friend bool operator!=(std::nullptr_t, const Code code) { return code.ast != nullptr; } + #ifdef GEN_ENFORCE_STRONG_CODE_TYPES # define operator explicit operator #endif @@ -269,27 +287,6 @@ static_assert( sizeof(Code) == sizeof(Code_POD), "ERROR: Code is not POD" ); // Desired width of the AST data structure. constexpr int const AST_POD_Size = 128; -void append ( AST* self, AST* other ); -char const* debug_str ( AST* self ); -AST* duplicate ( AST* self ); -Code* entry ( AST* self, u32 idx ); -bool has_entries ( AST* self ); -bool is_body ( AST* self ); -bool is_equal ( AST* self, AST* other ); -String to_string ( AST* self ); -void to_string ( AST* self, String* result ); -char const* type_str ( AST* self ); -bool validate_body( AST* self ); - -#if GEN_CPP_SUPPORT_REFERENCES -void append ( AST& self, AST& other ) { return append(& self, & other); } -bool is_body ( AST& self ) { return is_body(& self); } -bool is_equal ( AST& self, AST& other ) { return is_equal(& self, & other); } -char const* debug_str( AST& self ) { return debug_str( & self ); } -String to_string( AST& self ) { return to_string( & self ); } -char const* type_str ( AST& self ) { return type_str( & self ); } -#endif - constexpr static int AST_ArrSpecs_Cap = ( @@ -309,102 +306,53 @@ int AST_ArrSpecs_Cap = */ struct AST { -#if GEN_SUPPORT_CPP_MEMBER_FEATURES -# pragma region Member Functions - void append ( AST* other ) { GEN_NS append(this, other); } - char const* debug_str () { return GEN_NS debug_str(this); } - AST* duplicate () { return GEN_NS duplicate(this); } - Code* entry ( u32 idx ) { return GEN_NS entry(this, idx); } - bool has_entries() { return GEN_NS has_entries(this); } - bool is_equal ( AST* other ) { return GEN_NS is_equal(this, other); } - bool is_body() { return GEN_NS is_body(this); } - char const* type_str() { return GEN_NS type_str(this); } - bool validate_body() { return GEN_NS validate_body(this); } - - String to_string() { return GEN_NS to_string(this); } - void to_string( String& result ) { return GEN_NS to_string(this, & result); } - -# pragma endregion Member Functions -#endif - - operator Code(); - operator CodeBody(); - operator CodeAttributes(); - // operator CodeBaseClass(); - operator CodeComment(); - operator CodeConstructor(); - operator CodeDestructor(); - operator CodeClass(); - operator CodeDefine(); - operator CodeEnum(); - operator CodeExec(); - operator CodeExtern(); - operator CodeInclude(); - operator CodeFriend(); - operator CodeFn(); - operator CodeModule(); - operator CodeNS(); - operator CodeOperator(); - operator CodeOpCast(); - operator CodeParam(); - operator CodePragma(); - operator CodePreprocessCond(); - operator CodeSpecifiers(); - operator CodeStruct(); - operator CodeTemplate(); - operator CodeType(); - operator CodeTypedef(); - operator CodeUnion(); - operator CodeUsing(); - operator CodeVar(); - union { struct { - AST* InlineCmt; // Class, Constructor, Destructor, Enum, Friend, Functon, Operator, OpCast, Struct, Typedef, Using, Variable - AST* Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable - AST* Specs; // Destructor, Function, Operator, Typename, Variable + Code InlineCmt; // Class, Constructor, Destructor, Enum, Friend, Functon, Operator, OpCast, Struct, Typedef, Using, Variable + Code Attributes; // Class, Enum, Function, Struct, Typedef, Union, Using, Variable + Code Specs; // Destructor, Function, Operator, Typename, Variable union { - AST* InitializerList; // Constructor - AST* ParentType; // Class, Struct, ParentType->Next has a possible list of interfaces. - AST* ReturnType; // Function, Operator, Typename - AST* UnderlyingType; // Enum, Typedef - AST* ValueType; // Parameter, Variable + Code InitializerList; // Constructor + Code ParentType; // Class, Struct, ParentType->Next has a possible list of interfaces. + Code ReturnType; // Function, Operator, Typename + Code UnderlyingType; // Enum, Typedef + Code ValueType; // Parameter, Variable }; union { - AST* Macro; // Parameter - AST* BitfieldSize; // Variable (Class/Struct Data Member) - AST* Params; // Constructor, Function, Operator, Template, Typename + Code Macro; // Parameter + Code BitfieldSize; // Variable (Class/Struct Data Member) + Code Params; // Constructor, Function, Operator, Template, Typename }; union { - AST* ArrExpr; // Typename - AST* Body; // Class, Constructor, Destructor, Enum, Friend, Function, Namespace, Struct, Union - AST* Declaration; // Friend, Template - AST* Value; // Parameter, Variable + Code ArrExpr; // Typename + Code Body; // Class, Constructor, Destructor, Enum, Friend, Function, Namespace, Struct, Union + Code Declaration; // Friend, Template + Code Value; // Parameter, Variable }; union { - AST* NextVar; // Variable; Possible way to handle comma separated variables declarations. ( , NextVar->Specs NextVar->Name NextVar->ArrExpr = NextVar->Value ) - AST* SuffixSpecs; // Only used with typenames, to store the function suffix if typename is function signature. ( May not be needed ) - AST* PostNameMacro; // Only used with parameters for specifically UE_REQUIRES (Thanks Unreal) + Code NextVar; // Variable; Possible way to handle comma separated variables declarations. ( , NextVar->Specs NextVar->Name NextVar->ArrExpr = NextVar->Value ) + Code SuffixSpecs; // Only used with typenames, to store the function suffix if typename is function signature. ( May not be needed ) + Code PostNameMacro; // Only used with parameters for specifically UE_REQUIRES (Thanks Unreal) }; }; StringCached Content; // Attributes, Comment, Execution, Include struct { SpecifierT ArrSpecs[AST_ArrSpecs_Cap]; // Specifiers - AST* NextSpecs; // Specifiers; If ArrSpecs is full, then NextSpecs is used. + Code NextSpecs; // Specifiers; If ArrSpecs is full, then NextSpecs is used. }; }; union { - AST* Prev; - AST* Front; - AST* Last; + Code Prev; + Code Front; + Code Last; }; union { - AST* Next; - AST* Back; + Code Next; + Code Back; }; parser::Token* Token; // Reference to starting token, only avaialble if it was derived from parsing. - AST* Parent; + Code Parent; StringCached Name; CodeT Type; // CodeFlag CodeFlags; @@ -419,11 +367,16 @@ struct AST b32 EnumUnderlyingMacro; // Used by enums incase the user wants to wrap underlying type specification in a macro }; }; - -// Its intended for the AST to have equivalent size to its POD. -// All extra functionality within the AST namespace should just be syntatic sugar. static_assert( sizeof(AST) == AST_POD_Size, "ERROR: AST POD is not size of AST_POD_Size" ); +#if ! GEN_COMPILER_C +// Uses an implicitly overloaded cast from the AST to the desired code type. +// Necessary if the user wants GEN_ENFORCE_STRONG_CODE_TYPES +struct InvalidCode_ImplictCaster; +#define InvalidCode (InvalidCode_ImplictCaster{}) +#else +#define InvalidCode Code_Invalid +#endif + // Used when the its desired when omission is allowed in a definition. -#define NoCode { nullptr } -#define InvalidCode (* Code_Invalid.ast) // Uses an implicitly overloaded cast from the AST to the desired code type. +#define NullCode { nullptr } diff --git a/project/components/code_serialization.cpp b/project/components/code_serialization.cpp index 06f970b..28e3a6a 100644 --- a/project/components/code_serialization.cpp +++ b/project/components/code_serialization.cpp @@ -3,16 +3,6 @@ #include "ast.cpp" #endif -String to_string(Code self) -{ - if ( self.ast == nullptr ) - { - log_failure( "Code::to_string: Cannot convert code to string, AST is null!" ); - return { nullptr }; - } - return to_string( self.ast ); -} - String to_string(CodeAttributes attributes) { return GEN_NS duplicate( attributes->Content, GlobalAllocator ); } @@ -100,7 +90,7 @@ String to_string(CodeConstructor self) void to_string_def(CodeConstructor self, String* result ) { - AST* ClassStructParent = self->Parent->Parent; + Code ClassStructParent = self->Parent->Parent; if (ClassStructParent) { append( result, ClassStructParent->Name ); } @@ -124,7 +114,7 @@ void to_string_def(CodeConstructor self, String* result ) void to_string_fwd(CodeConstructor self, String* result ) { - AST* ClassStructParent = self->Parent->Parent; + Code ClassStructParent = self->Parent->Parent; if (ClassStructParent) { append( result, ClassStructParent->Name ); } @@ -1117,7 +1107,7 @@ void to_string(CodeTypedef self, String* result ) { append_fmt( result, "[ %S ];", GEN_NS to_string(self->UnderlyingType->ArrExpr) ); - AST* next_arr_expr = self->UnderlyingType->ArrExpr->Next; + Code next_arr_expr = self->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) { append_fmt( result, "[ %S ];", GEN_NS to_string(next_arr_expr) ); @@ -1256,7 +1246,7 @@ void to_string(CodeUsing self, String* result ) { append_fmt( result, "[ %S ]", GEN_NS to_string(self->UnderlyingType->ArrExpr) ); - AST* next_arr_expr = self->UnderlyingType->ArrExpr->Next; + Code next_arr_expr = self->UnderlyingType->ArrExpr->Next; while ( next_arr_expr ) { append_fmt( result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); @@ -1305,7 +1295,7 @@ void to_string(CodeVar self, String* result ) { append_fmt( result, "[ %S ]", GEN_NS to_string(self->ValueType->ArrExpr) ); - AST* next_arr_expr = self->ValueType->ArrExpr->Next; + Code next_arr_expr = self->ValueType->ArrExpr->Next; while ( next_arr_expr ) { append_fmt( result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); @@ -1348,7 +1338,7 @@ void to_string(CodeVar self, String* result ) { append_fmt( result, "[ %S ]", GEN_NS to_string(self->ValueType->ArrExpr) ); - AST* next_arr_expr = self->ValueType->ArrExpr->Next; + Code next_arr_expr = self->ValueType->ArrExpr->Next; while ( next_arr_expr ) { append_fmt( result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); @@ -1388,7 +1378,7 @@ void to_string(CodeVar self, String* result ) { append_fmt( result, "%S %S[ %S ]", GEN_NS to_string(self->ValueType), self->Name, GEN_NS to_string(self->ValueType->ArrExpr) ); - AST* next_arr_expr = self->ValueType->ArrExpr->Next; + Code next_arr_expr = self->ValueType->ArrExpr->Next; while ( next_arr_expr ) { append_fmt( result, "[ %S ]", GEN_NS to_string(next_arr_expr) ); diff --git a/project/components/code_types.hpp b/project/components/code_types.hpp index deeb776..58e63e6 100644 --- a/project/components/code_types.hpp +++ b/project/components/code_types.hpp @@ -3,6 +3,7 @@ #include "ast.hpp" #endif +#pragma region Code Type Interface void append ( CodeBody body, Code other ); void append ( CodeBody body, CodeBody other ); String to_string ( CodeBody body ); @@ -117,6 +118,7 @@ void to_string_ns(CodeUsing op_cast, String* result ); String to_string(CodeVar self); void to_string(CodeVar self, String* result); +#pragma endregion Code Type Interface #pragma region Code Types // These structs are not used at all by the C vairant. @@ -132,7 +134,7 @@ struct CodeBody void append( Code other ) { return GEN_NS append( *this, other ); } void append( CodeBody body ) { return GEN_NS append(*this, body); } - bool has_entries() { return GEN_NS has_entries(rcast( AST*, ast )); } + bool has_entries() { return GEN_NS has_entries(* this); } String to_string() { return GEN_NS to_string(* this); } void to_string( String& result ) { return GEN_NS to_string(* this, & result ); } @@ -1011,6 +1013,40 @@ void to_string_export( CodeBody body, String& result ) { return to_string_export #undef Verify_POD +struct InvalidCode_ImplictCaster +{ + // operator CodeBaseClass() const; + operator Code () const { return Code_Invalid; } + operator CodeBody () const { return cast(CodeBody, Code_Invalid); } + operator CodeAttributes () const { return cast(CodeAttributes, Code_Invalid); } + operator CodeComment () const { return cast(CodeComment, Code_Invalid); } + operator CodeClass () const { return cast(CodeClass, Code_Invalid); } + operator CodeConstructor () const { return cast(CodeConstructor, Code_Invalid); } + operator CodeDefine () const { return cast(CodeDefine, Code_Invalid); } + operator CodeDestructor () const { return cast(CodeDestructor, Code_Invalid); } + operator CodeExec () const { return cast(CodeExec, Code_Invalid); } + operator CodeEnum () const { return cast(CodeEnum, Code_Invalid); } + operator CodeExtern () const { return cast(CodeExtern, Code_Invalid); } + operator CodeInclude () const { return cast(CodeInclude, Code_Invalid); } + operator CodeFriend () const { return cast(CodeFriend, Code_Invalid); } + operator CodeFn () const { return cast(CodeFn, Code_Invalid); } + operator CodeModule () const { return cast(CodeModule, Code_Invalid); } + operator CodeNS () const { return cast(CodeNS, Code_Invalid); } + operator CodeOperator () const { return cast(CodeOperator, Code_Invalid); } + operator CodeOpCast () const { return cast(CodeOpCast, Code_Invalid); } + operator CodeParam () const { return cast(CodeParam, Code_Invalid); } + operator CodePragma () const { return cast(CodePragma, Code_Invalid); } + operator CodePreprocessCond() const { return cast(CodePreprocessCond, Code_Invalid); } + operator CodeSpecifiers () const { return cast(CodeSpecifiers, Code_Invalid); } + operator CodeStruct () const { return cast(CodeStruct, Code_Invalid); } + operator CodeTemplate () const { return cast(CodeTemplate, Code_Invalid); } + operator CodeType () const { return cast(CodeType, Code_Invalid); } + operator CodeTypedef () const { return cast(CodeTypedef, Code_Invalid); } + operator CodeUnion () const { return cast(CodeUnion, Code_Invalid); } + operator CodeUsing () const { return cast(CodeUsing, Code_Invalid); } + operator CodeVar () const { return cast(CodeVar, Code_Invalid); } +}; + #endif //if ! GEN_COMPILER_C #pragma endregion Code Types diff --git a/project/components/gen/ast_inlines.hpp b/project/components/gen/ast_inlines.hpp index bc2d3fc..c9c9519 100644 --- a/project/components/gen/ast_inlines.hpp +++ b/project/components/gen/ast_inlines.hpp @@ -11,8 +11,8 @@ inline Code& Code::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -27,8 +27,8 @@ inline CodeBody& CodeBody::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -43,8 +43,8 @@ inline CodeAttributes& CodeAttributes::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -74,8 +74,8 @@ inline CodeComment& CodeComment::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -105,8 +105,8 @@ inline CodeConstructor& CodeConstructor::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -136,8 +136,8 @@ inline CodeClass& CodeClass::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -152,8 +152,8 @@ inline CodeDefine& CodeDefine::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -183,8 +183,8 @@ inline CodeDestructor& CodeDestructor::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -214,8 +214,8 @@ inline CodeEnum& CodeEnum::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -245,8 +245,8 @@ inline CodeExec& CodeExec::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -276,8 +276,8 @@ inline CodeExtern& CodeExtern::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -307,8 +307,8 @@ inline CodeFriend& CodeFriend::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -338,8 +338,8 @@ inline CodeFn& CodeFn::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -369,8 +369,8 @@ inline CodeInclude& CodeInclude::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -400,8 +400,8 @@ inline CodeModule& CodeModule::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -431,8 +431,8 @@ inline CodeNS& CodeNS::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -462,8 +462,8 @@ inline CodeOperator& CodeOperator::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -493,8 +493,8 @@ inline CodeOpCast& CodeOpCast::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -524,8 +524,8 @@ inline CodeParam& CodeParam::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -540,8 +540,8 @@ inline CodePragma& CodePragma::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -571,8 +571,8 @@ inline CodePreprocessCond& CodePreprocessCond::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -602,8 +602,8 @@ inline CodeSpecifiers& CodeSpecifiers::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -618,8 +618,8 @@ inline CodeStruct& CodeStruct::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -634,8 +634,8 @@ inline CodeTemplate& CodeTemplate::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -665,8 +665,8 @@ inline CodeType& CodeType::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -696,8 +696,8 @@ inline CodeTypedef& CodeTypedef::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -727,8 +727,8 @@ inline CodeUnion& CodeUnion::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -758,8 +758,8 @@ inline CodeUsing& CodeUsing::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -789,8 +789,8 @@ inline CodeVar& CodeVar::operator=( Code other ) { if ( other.ast && other->Parent ) { - ast = rcast( decltype( ast ), GEN_NS duplicate( other.ast ) ); - rcast( AST*, ast )->Parent = nullptr; + ast = rcast( decltype( ast ), GEN_NS duplicate( other ).ast ); + ast->Parent = { nullptr }; } ast = rcast( decltype( ast ), other.ast ); return *this; @@ -820,281 +820,141 @@ inline AST_Var* CodeVar::operator->() #pragma region generated AST/Code cast implementation -inline AST::operator CodeBody() -{ - return { rcast( AST_Body*, this ) }; -} - inline Code::operator CodeBody() const { return { (AST_Body*)ast }; } -inline AST::operator CodeAttributes() -{ - return { rcast( AST_Attributes*, this ) }; -} - inline Code::operator CodeAttributes() const { return { (AST_Attributes*)ast }; } -inline AST::operator CodeComment() -{ - return { rcast( AST_Comment*, this ) }; -} - inline Code::operator CodeComment() const { return { (AST_Comment*)ast }; } -inline AST::operator CodeConstructor() -{ - return { rcast( AST_Constructor*, this ) }; -} - inline Code::operator CodeConstructor() const { return { (AST_Constructor*)ast }; } -inline AST::operator CodeClass() -{ - return { rcast( AST_Class*, this ) }; -} - inline Code::operator CodeClass() const { return { (AST_Class*)ast }; } -inline AST::operator CodeDefine() -{ - return { rcast( AST_Define*, this ) }; -} - inline Code::operator CodeDefine() const { return { (AST_Define*)ast }; } -inline AST::operator CodeDestructor() -{ - return { rcast( AST_Destructor*, this ) }; -} - inline Code::operator CodeDestructor() const { return { (AST_Destructor*)ast }; } -inline AST::operator CodeEnum() -{ - return { rcast( AST_Enum*, this ) }; -} - inline Code::operator CodeEnum() const { return { (AST_Enum*)ast }; } -inline AST::operator CodeExec() -{ - return { rcast( AST_Exec*, this ) }; -} - inline Code::operator CodeExec() const { return { (AST_Exec*)ast }; } -inline AST::operator CodeExtern() -{ - return { rcast( AST_Extern*, this ) }; -} - inline Code::operator CodeExtern() const { return { (AST_Extern*)ast }; } -inline AST::operator CodeFriend() -{ - return { rcast( AST_Friend*, this ) }; -} - inline Code::operator CodeFriend() const { return { (AST_Friend*)ast }; } -inline AST::operator CodeFn() -{ - return { rcast( AST_Fn*, this ) }; -} - inline Code::operator CodeFn() const { return { (AST_Fn*)ast }; } -inline AST::operator CodeInclude() -{ - return { rcast( AST_Include*, this ) }; -} - inline Code::operator CodeInclude() const { return { (AST_Include*)ast }; } -inline AST::operator CodeModule() -{ - return { rcast( AST_Module*, this ) }; -} - inline Code::operator CodeModule() const { return { (AST_Module*)ast }; } -inline AST::operator CodeNS() -{ - return { rcast( AST_NS*, this ) }; -} - inline Code::operator CodeNS() const { return { (AST_NS*)ast }; } -inline AST::operator CodeOperator() -{ - return { rcast( AST_Operator*, this ) }; -} - inline Code::operator CodeOperator() const { return { (AST_Operator*)ast }; } -inline AST::operator CodeOpCast() -{ - return { rcast( AST_OpCast*, this ) }; -} - inline Code::operator CodeOpCast() const { return { (AST_OpCast*)ast }; } -inline AST::operator CodeParam() -{ - return { rcast( AST_Param*, this ) }; -} - inline Code::operator CodeParam() const { return { (AST_Param*)ast }; } -inline AST::operator CodePragma() -{ - return { rcast( AST_Pragma*, this ) }; -} - inline Code::operator CodePragma() const { return { (AST_Pragma*)ast }; } -inline AST::operator CodePreprocessCond() -{ - return { rcast( AST_PreprocessCond*, this ) }; -} - inline Code::operator CodePreprocessCond() const { return { (AST_PreprocessCond*)ast }; } -inline AST::operator CodeSpecifiers() -{ - return { rcast( AST_Specifiers*, this ) }; -} - inline Code::operator CodeSpecifiers() const { return { (AST_Specifiers*)ast }; } -inline AST::operator CodeStruct() -{ - return { rcast( AST_Struct*, this ) }; -} - inline Code::operator CodeStruct() const { return { (AST_Struct*)ast }; } -inline AST::operator CodeTemplate() -{ - return { rcast( AST_Template*, this ) }; -} - inline Code::operator CodeTemplate() const { return { (AST_Template*)ast }; } -inline AST::operator CodeType() -{ - return { rcast( AST_Type*, this ) }; -} - inline Code::operator CodeType() const { return { (AST_Type*)ast }; } -inline AST::operator CodeTypedef() -{ - return { rcast( AST_Typedef*, this ) }; -} - inline Code::operator CodeTypedef() const { return { (AST_Typedef*)ast }; } -inline AST::operator CodeUnion() -{ - return { rcast( AST_Union*, this ) }; -} - inline Code::operator CodeUnion() const { return { (AST_Union*)ast }; } -inline AST::operator CodeUsing() -{ - return { rcast( AST_Using*, this ) }; -} - inline Code::operator CodeUsing() const { return { (AST_Using*)ast }; } -inline AST::operator CodeVar() -{ - return { rcast( AST_Var*, this ) }; -} - inline Code::operator CodeVar() const { return { (AST_Var*)ast }; diff --git a/project/components/header_end.hpp b/project/components/header_end.hpp index 27d03d9..193288c 100644 --- a/project/components/header_end.hpp +++ b/project/components/header_end.hpp @@ -136,7 +136,7 @@ extern CodeType t_typename; #ifndef token_fmt # define gen_main main -# define __ NoCode +# define __ NullCode // Convienence for defining any name used with the gen api. // Lets you provide the length and string literal to the functions without the need for the DSL. diff --git a/project/components/inlines.hpp b/project/components/inlines.hpp index 17b7ac9..060e4cf 100644 --- a/project/components/inlines.hpp +++ b/project/components/inlines.hpp @@ -3,11 +3,12 @@ #include "interface.hpp" #endif +#pragma region Code inline -void append( AST* self, AST* other ) +void append( Code self, Code other ) { - GEN_ASSERT(self != nullptr); - GEN_ASSERT(other != nullptr); + GEN_ASSERT(self.ast != nullptr); + GEN_ASSERT(other.ast != nullptr); if ( other->Parent ) other = duplicate(other); @@ -23,40 +24,15 @@ void append( AST* self, AST* other ) return; } - AST* + Code Current = self->Back; Current->Next = other; other->Prev = Current; self->Back = other; self->NumEntries++; } - inline -Code* entry( AST* self, u32 idx ) -{ - GEN_ASSERT(self != nullptr); - AST** current = & self->Front; - while ( idx >= 0 && current != nullptr ) - { - if ( idx == 0 ) - return rcast( Code*, current); - - current = & ( * current )->Next; - idx--; - } - - return rcast( Code*, current); -} - -inline -bool has_entries(AST* self) -{ - GEN_ASSERT(self != nullptr); - return self->NumEntries > 0; -} - -inline -bool is_body(AST* self) +bool is_body(Code self) { GEN_ASSERT(self != nullptr); switch (self->Type) @@ -74,59 +50,21 @@ bool is_body(AST* self) } return false; } - inline -char const* type_str(AST* self) +Code* entry( Code self, u32 idx ) { - GEN_ASSERT(self != nullptr); - return ECode::to_str( self->Type ); -} - -inline -AST::operator Code() -{ - return { this }; -} - -#pragma region Code -inline -char const* debug_str( Code code ) -{ - if ( code.ast == nullptr ) - return "Code::debug_str: AST is null!"; - - return debug_str( code.ast ); -} -inline -Code duplicate( Code code ) -{ - if ( code.ast == nullptr ) + GEN_ASSERT(self.ast != nullptr); + Code* current = & self->Front; + while ( idx >= 0 && current != nullptr ) { - log_failure("Code::duplicate: Cannot duplicate code, AST is null!"); - return Code_Invalid; + if ( idx == 0 ) + return rcast( Code*, current); + + current = & ( * current )->Next; + idx--; } - return { duplicate(code.ast) }; -} -inline -bool is_body(Code code) -{ - if ( code.ast == nullptr ) - { - return is_body(code.ast); - } - return false; -} -inline -bool is_equal( Code self, Code other ) -{ - if ( self.ast == nullptr || other.ast == nullptr ) - { - // Just check if they're both null. - // log_failure( "Code::is_equal: Cannot compare code, AST is null!" ); - return self.ast == nullptr && other.ast == nullptr; - } - return is_equal( self.ast, other.ast ); + return rcast( Code*, current); } inline bool is_valid(Code self) @@ -134,6 +72,12 @@ bool is_valid(Code self) return self.ast != nullptr && self.ast->Type != CodeT::Invalid; } inline +bool has_entries(AST* self) +{ + GEN_ASSERT(self != nullptr); + return self->NumEntries > 0; +} +inline void set_global(Code self) { if ( self.ast == nullptr ) @@ -142,15 +86,21 @@ void set_global(Code self) return; } - self->Parent = Code_Global.ast; + self->Parent.ast = Code_Global.ast; } inline Code& Code::operator ++() { if ( ast ) - ast = ast->Next; + ast = ast->Next.ast; - return *this; + return * this; +} +inline +char const* type_str(Code self) +{ + GEN_ASSERT(self != nullptr); + return ECode::to_str( self->Type ); } #pragma endregion Code @@ -165,7 +115,7 @@ void append( CodeBody self, Code other ) return; } - append( rcast(AST*, self.ast), other.ast ); + append( cast(Code, self), other ); } inline void append( CodeBody self, CodeBody body ) @@ -216,8 +166,8 @@ inline void append( CodeParam appendee, CodeParam other ) { GEN_ASSERT(appendee.ast != nullptr); - AST* self = cast(Code, appendee).ast; - AST* entry = (AST*) other.ast; + Code self = cast(Code, appendee); + Code entry = cast(Code, other); if ( entry->Parent ) entry = GEN_NS duplicate( entry ); @@ -246,7 +196,7 @@ CodeParam get(CodeParam self, s32 idx ) if ( ! ++ param ) return { nullptr }; - param = { (AST_Param*) cast(Code, param)->Next }; + param = cast(Code, param)->Next; } while ( --idx ); diff --git a/project/components/interface.hpp b/project/components/interface.hpp index 0982fd4..23bda92 100644 --- a/project/components/interface.hpp +++ b/project/components/interface.hpp @@ -43,21 +43,21 @@ CodeAttributes def_attributes( StrC content ); CodeComment def_comment ( StrC content ); CodeClass def_class( StrC name - , Code body = NoCode - , CodeType parent = NoCode, AccessSpec access = AccessSpec_Default - , CodeAttributes attributes = NoCode + , Code body = NullCode + , CodeType parent = NullCode, AccessSpec access = AccessSpec_Default + , CodeAttributes attributes = NullCode , ModuleFlag mflags = ModuleFlag_None , CodeType* interfaces = nullptr, s32 num_interfaces = 0 ); -CodeConstructor def_constructor( CodeParam params = NoCode, Code initializer_list = NoCode, Code body = NoCode ); +CodeConstructor def_constructor( CodeParam params = NullCode, Code initializer_list = NullCode, Code body = NullCode ); CodeDefine def_define( StrC name, StrC content ); -CodeDestructor def_destructor( Code body = NoCode, CodeSpecifiers specifiers = NoCode ); +CodeDestructor def_destructor( Code body = NullCode, CodeSpecifiers specifiers = NullCode ); CodeEnum def_enum( StrC name - , Code body = NoCode, CodeType type = NoCode - , EnumT specifier = EnumDecl_Regular, CodeAttributes attributes = NoCode + , Code body = NullCode, CodeType type = NullCode + , EnumT specifier = EnumDecl_Regular, CodeAttributes attributes = NullCode , ModuleFlag mflags = ModuleFlag_None ); CodeExec def_execution ( StrC content ); @@ -65,8 +65,8 @@ CodeExtern def_extern_link( StrC name, Code body ); CodeFriend def_friend ( Code symbol ); CodeFn def_function( StrC name - , CodeParam params = NoCode, CodeType ret_type = NoCode, Code body = NoCode - , CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode + , CodeParam params = NullCode, CodeType ret_type = NullCode, Code body = NullCode + , CodeSpecifiers specifiers = NullCode, CodeAttributes attributes = NullCode , ModuleFlag mflags = ModuleFlag_None ); CodeInclude def_include ( StrC content, bool foreign = false ); @@ -74,13 +74,13 @@ CodeModule def_module ( StrC name, ModuleFlag mflags = ModuleFla CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags = ModuleFlag_None ); CodeOperator def_operator( OperatorT op, StrC nspace - , CodeParam params = NoCode, CodeType ret_type = NoCode, Code body = NoCode - , CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode + , CodeParam params = NullCode, CodeType ret_type = NullCode, Code body = NullCode + , CodeSpecifiers specifiers = NullCode, CodeAttributes attributes = NullCode , ModuleFlag mflags = ModuleFlag_None ); -CodeOpCast def_operator_cast( CodeType type, Code body = NoCode, CodeSpecifiers specs = NoCode ); +CodeOpCast def_operator_cast( CodeType type, Code body = NullCode, CodeSpecifiers specs = NullCode ); -CodeParam def_param ( CodeType type, StrC name, Code value = NoCode ); +CodeParam def_param ( CodeType type, StrC name, Code value = NullCode ); CodePragma def_pragma( StrC directive ); CodePreprocessCond def_preprocess_cond( EPreprocessCond type, StrC content ); @@ -88,27 +88,27 @@ CodePreprocessCond def_preprocess_cond( EPreprocessCond type, StrC content ); CodeSpecifiers def_specifier( SpecifierT specifier ); CodeStruct def_struct( StrC name - , Code body = NoCode - , CodeType parent = NoCode, AccessSpec access = AccessSpec_Default - , CodeAttributes attributes = NoCode + , Code body = NullCode + , CodeType parent = NullCode, AccessSpec access = AccessSpec_Default + , CodeAttributes attributes = NullCode , ModuleFlag mflags = ModuleFlag_None , CodeType* interfaces = nullptr, s32 num_interfaces = 0 ); CodeTemplate def_template( CodeParam params, Code definition, ModuleFlag mflags = ModuleFlag_None ); -CodeType def_type ( StrC name, Code arrayexpr = NoCode, CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode ); -CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes = NoCode, ModuleFlag mflags = ModuleFlag_None ); +CodeType def_type ( StrC name, Code arrayexpr = NullCode, CodeSpecifiers specifiers = NullCode, CodeAttributes attributes = NullCode ); +CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes = NullCode, ModuleFlag mflags = ModuleFlag_None ); -CodeUnion def_union( StrC name, Code body, CodeAttributes attributes = NoCode, ModuleFlag mflags = ModuleFlag_None ); +CodeUnion def_union( StrC name, Code body, CodeAttributes attributes = NullCode, ModuleFlag mflags = ModuleFlag_None ); -CodeUsing def_using( StrC name, CodeType type = NoCode - , CodeAttributes attributess = NoCode +CodeUsing def_using( StrC name, CodeType type = NullCode + , CodeAttributes attributess = NullCode , ModuleFlag mflags = ModuleFlag_None ); CodeUsing def_using_namespace( StrC name ); -CodeVar def_variable( CodeType type, StrC name, Code value = NoCode - , CodeSpecifiers specifiers = NoCode, CodeAttributes attributes = NoCode +CodeVar def_variable( CodeType type, StrC name, Code value = NullCode + , CodeSpecifiers specifiers = NullCode, CodeAttributes attributes = NullCode , ModuleFlag mflags = ModuleFlag_None ); // Constructs an empty body. Use AST::validate_body() to check if the body is was has valid entries. diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 7f76377..baf78ed 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -214,7 +214,7 @@ internal CodeVar parse_variable_declaration_list (); internal CodeClass parse_class ( bool inplace_def = false ); internal CodeConstructor parse_constructor ( CodeSpecifiers specifiers ); -internal CodeDestructor parse_destructor ( CodeSpecifiers specifiers = NoCode ); +internal CodeDestructor parse_destructor ( CodeSpecifiers specifiers = NullCode ); internal CodeEnum parse_enum ( bool inplace_def = false ); internal CodeBody parse_export_body (); internal CodeBody parse_extern_link_body(); @@ -222,7 +222,7 @@ internal CodeExtern parse_extern_link (); internal CodeFriend parse_friend (); internal CodeFn parse_function (); internal CodeNS parse_namespace (); -internal CodeOpCast parse_operator_cast ( CodeSpecifiers specifiers = NoCode ); +internal CodeOpCast parse_operator_cast ( CodeSpecifiers specifiers = NullCode ); internal CodeStruct parse_struct ( bool inplace_def = false ); internal CodeVar parse_variable (); internal CodeTemplate parse_template (); @@ -550,7 +550,7 @@ Code parse_array_decl() Code adjacent_arr_expr = parse_array_decl(); // [ ][ ]... - array_expr->Next = adjacent_arr_expr.ast; + array_expr->Next.ast = adjacent_arr_expr.ast; } Context.pop(); @@ -757,7 +757,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) } // : , ... { } - CodeComment inline_cmt = NoCode; + CodeComment inline_cmt = NullCode; if ( ! inplace_def ) { Token stmt_end = currtok; @@ -1445,8 +1445,8 @@ CodeFn parse_function_after_name( } // ( ) - CodeBody body = NoCode; - CodeComment inline_cmt = NoCode; + CodeBody body = NullCode; + CodeComment inline_cmt = NullCode; if ( check( TokType::BraceCurly_Open ) ) { body = parse_function_body(); @@ -2450,7 +2450,7 @@ CodeOperator parse_operator_after_ret_type( // Parse Body CodeBody body = { nullptr }; - CodeComment inline_cmt = NoCode; + CodeComment inline_cmt = NullCode; if ( check( TokType::BraceCurly_Open ) ) { body = parse_function_body(); @@ -3181,9 +3181,9 @@ CodeVar parse_variable_after_name( // : } - CodeVar next_var = NoCode; + CodeVar next_var = NullCode; Token stmt_end = NullToken; - CodeComment inline_cmt = NoCode; + CodeComment inline_cmt = NullCode; if ( type ) { if ( currtok.Type == TokType::Comma ) @@ -3266,14 +3266,14 @@ CodeVar parse_variable_declaration_list() { push_scope(); - CodeVar result = NoCode; - CodeVar last_var = NoCode; + CodeVar result = NullCode; + CodeVar last_var = NullCode; while ( check( TokType::Comma ) ) { eat( TokType::Comma ); // , - CodeSpecifiers specifiers = NoCode; + CodeSpecifiers specifiers = NullCode; while ( left && currtok.is_specifier() ) { @@ -3320,7 +3320,7 @@ CodeVar parse_variable_declaration_list() eat( TokType::Identifier ); // , - CodeVar var = parse_variable_after_name( ModuleFlag_None, NoCode, specifiers, NoCode, name ); + CodeVar var = parse_variable_after_name( ModuleFlag_None, NullCode, specifiers, NullCode, name ); // , ... if ( ! result ) @@ -3358,9 +3358,9 @@ CodeConstructor parse_constructor( CodeSpecifiers specifiers ) CodeParam params = parse_params(); // ( ) - Code initializer_list = NoCode; - CodeBody body = NoCode; - CodeComment inline_cmt = NoCode; + Code initializer_list = NullCode; + CodeBody body = NullCode; + CodeComment inline_cmt = NullCode; // TODO(Ed) : Need to support postfix specifiers @@ -3472,7 +3472,7 @@ CodeDestructor parse_destructor( CodeSpecifiers specifiers ) Token identifier = parse_identifier(); CodeBody body = { nullptr }; - CodeComment inline_cmt = NoCode; + CodeComment inline_cmt = NullCode; // ~ eat( TokType::Capture_Start ); @@ -3750,7 +3750,7 @@ CodeEnum parse_enum( bool inplace_def ) // enum : { } } - CodeComment inline_cmt = NoCode; + CodeComment inline_cmt = NullCode; if ( ! inplace_def ) { @@ -3881,7 +3881,7 @@ CodeFriend parse_friend() Context.Scope->Name = name; // friend - function = parse_function_after_name( ModuleFlag_None, NoCode, NoCode, type, name ); + function = parse_function_after_name( ModuleFlag_None, NullCode, NullCode, type, name ); // Parameter list // CodeParam params = parse_params(); @@ -3896,7 +3896,7 @@ CodeFriend parse_friend() // function->Params = params; } - CodeComment inline_cmt = NoCode; + CodeComment inline_cmt = NullCode; if ( function && function->Type == ECode::Function_Fwd ) { Token stmt_end = currtok; @@ -4154,8 +4154,8 @@ CodeOpCast parse_operator_cast( CodeSpecifiers specifiers ) } // :: ... operator () - Code body = NoCode; - CodeComment inline_cmt = NoCode; + Code body = NullCode; + CodeComment inline_cmt = NullCode; if ( check( TokType::BraceCurly_Open) ) { @@ -4595,11 +4595,11 @@ else if ( currtok.Type == TokType::DeclType ) // // For function type signatures - CodeType return_type = NoCode; - CodeParam params = NoCode; + CodeType return_type = NullCode; + CodeParam params = NullCode; #ifdef GEN_USE_NEW_TYPENAME_PARSING - CodeParam params_nested = NoCode; + CodeParam params_nested = NullCode; #endif bool is_function_typename = false; @@ -5036,7 +5036,7 @@ CodeTypedef parse_typedef() eat( TokType::Statement_End ); // typedef ; - CodeComment inline_cmt = NoCode; + CodeComment inline_cmt = NullCode; if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) inline_cmt = parse_comment(); // typedef ; @@ -5061,8 +5061,8 @@ CodeTypedef parse_typedef() if ( type ) { - result->UnderlyingType = type; - result->UnderlyingType->Parent = rcast(AST*, result.ast); + result->UnderlyingType = type; + result->UnderlyingType->Parent.ast = rcast(AST*, result.ast); } // Type needs to be aware of its parent so that it can be serialized properly. @@ -5277,7 +5277,7 @@ CodeUsing parse_using() eat( TokType::Statement_End ); // using = ; - CodeComment inline_cmt = NoCode; + CodeComment inline_cmt = NullCode; if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) { inline_cmt = parse_comment(); diff --git a/project/helpers/helper.hpp b/project/helpers/helper.hpp index 1a032d5..cf9c524 100644 --- a/project/helpers/helper.hpp +++ b/project/helpers/helper.hpp @@ -358,12 +358,12 @@ CodeBody gen_ast_inlines() { if ( other.ast && other->Parent ) { - ast = rcast( decltype(ast), GEN_NS duplicate(other.ast) ); - rcast( AST*, ast)->Parent = nullptr; + ast = rcast( decltype(ast), GEN_NS duplicate(other).ast); + ast->Parent = { nullptr }; } - ast = rcast( decltype(ast), other.ast ); - return *this; + ast = rcast( decltype( ast ), other.ast ); + return * this; } inline ::operator bool() @@ -448,10 +448,6 @@ CodeBody gen_ast_inlines() append(impl_code_var, parse_global_body( token_fmt( "typename", StrC name(Var), codetype_impl_tmpl ))); char const* cast_tmpl = stringize( - inline AST::operator Code() - { - return { rcast( AST_*, this ) }; - } inline Code::operator Code() const { return { (AST_*) ast }; diff --git a/scripts/gencpp.refactor b/scripts/gencpp.refactor index aa94853..c97866a 100644 --- a/scripts/gencpp.refactor +++ b/scripts/gencpp.refactor @@ -253,7 +253,7 @@ // word log_failure, new_name -// word NoCode, new_name +// word NullCode, new_name // word CodeInvalid, new_name // ------------ gencpp common diff --git a/test/_old/upfront/Sanity.Upfront.hpp b/test/_old/upfront/Sanity.Upfront.hpp index d5d0ecc..b716342 100644 --- a/test/_old/upfront/Sanity.Upfront.hpp +++ b/test/_old/upfront/Sanity.Upfront.hpp @@ -50,7 +50,7 @@ u32 gen_sanity_upfront() // Enum { - CodeEnum fwd = def_enum( name(ETestEnum), NoCode, t_u8 ); + CodeEnum fwd = def_enum( name(ETestEnum), NullCode, t_u8 ); CodeEnum def; { Code body = untyped_str( code( @@ -62,7 +62,7 @@ u32 gen_sanity_upfront() def = def_enum( name(ETestEnum), body, t_u8 ); } - CodeEnum fwd_enum_class = def_enum( name(ETestEnumClass), NoCode, t_u8, EnumClass ); + CodeEnum fwd_enum_class = def_enum( name(ETestEnumClass), NullCode, t_u8, EnumClass ); gen_sanity_file.print(fwd); gen_sanity_file.print(def); From 2fe708e4bed46b302a81aee2d1c691b31c234f59 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 2 Dec 2024 22:25:39 -0500 Subject: [PATCH 045/112] Began to reduce cpp feature usage in lexer and parser --- project/components/ast.hpp | 9 +- project/components/interface.parsing.cpp | 4 +- project/components/lexer.cpp | 248 +++++++++---------- project/components/parser.cpp | 301 ++++++++++++----------- project/dependencies/containers.hpp | 2 + project/dependencies/platform.hpp | 6 + 6 files changed, 288 insertions(+), 282 deletions(-) diff --git a/project/components/ast.hpp b/project/components/ast.hpp index dde624e..a30e67f 100644 --- a/project/components/ast.hpp +++ b/project/components/ast.hpp @@ -152,10 +152,9 @@ Define_Code(Using); Define_Code(Var); #undef Define_Code -namespace parser -{ - struct Token; -} +GEN_NS_PARSER_BEGIN +struct Token; +GEN_NS_PARSER_END #if ! GEN_COMPILER_C template< class Type> forceinline Type tmpl_cast( Code self ) { return * rcast( Type*, & self ); } @@ -367,7 +366,7 @@ struct AST b32 EnumUnderlyingMacro; // Used by enums incase the user wants to wrap underlying type specification in a macro }; }; -static_assert( sizeof(AST) == AST_POD_Size, "ERROR: AST POD is not size of AST_POD_Size" ); +static_assert( sizeof(AST) == AST_POD_Size, "ERROR: AST POD is not size of AST_POD_Size" ); #if ! GEN_COMPILER_C // Uses an implicitly overloaded cast from the AST to the desired code type. diff --git a/project/components/interface.parsing.cpp b/project/components/interface.parsing.cpp index d1161aa..016645d 100644 --- a/project/components/interface.parsing.cpp +++ b/project/components/interface.parsing.cpp @@ -39,9 +39,9 @@ CodeConstructor parse_constructor( StrC def ) SpecifierT specs_found[ 16 ] { ESpecifier::NumSpecifiers }; s32 NumSpecifiers = 0; - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { - SpecifierT spec = ESpecifier::to_type( currtok ); + SpecifierT spec = ESpecifier::to_type( to_str(currtok) ); b32 ignore_spec = false; diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index 33f957e..0679b42 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -4,7 +4,7 @@ #include "gen/etoktype.cpp" #endif -namespace parser { +GEN_NS_PARSER_BEGIN enum TokFlags : u32 { @@ -31,137 +31,132 @@ struct Token s32 Line; s32 Column; u32 Flags; - - operator bool() - { - return Text && Length && Type != TokType::Invalid; - } - - operator StrC() - { - return { Length, Text }; - } - - bool is_access_operator() - { - return bitfield_is_equal( u32, Flags, TF_AccessOperator ); - } - - bool is_access_specifier() - { - return bitfield_is_equal( u32, Flags, TF_AccessSpecifier ); - } - - bool is_attribute() - { - return bitfield_is_equal( u32, Flags, TF_Attribute ); - } - - bool is_operator() - { - return bitfield_is_equal( u32, Flags, TF_Operator ); - } - - bool is_preprocessor() - { - return bitfield_is_equal( u32, Flags, TF_Preprocess ); - } - - bool is_preprocess_cond() - { - return bitfield_is_equal( u32, Flags, TF_Preprocess_Cond ); - } - - bool is_specifier() - { - return bitfield_is_equal( u32, Flags, TF_Specifier ); - } - - bool is_end_definition() - { - return bitfield_is_equal( u32, Flags, TF_EndDefinition ); - } - - AccessSpec to_access_specifier() - { - return scast(AccessSpec, Type); - } - - String to_string() - { - String result = string_make_reserve( GlobalAllocator, kilobytes(4) ); - - StrC type_str = ETokType::to_str( Type ); - - append_fmt( & result, "Line: %d Column: %d, Type: %.*s Content: %.*s" - , Line, Column - , type_str.Len, type_str.Ptr - , Length, Text - ); - - return result; - } }; constexpr Token NullToken { nullptr, 0, TokType::Invalid, false, 0, TF_Null }; +AccessSpec to_access_specifier(Token tok) +{ + return scast(AccessSpec, tok.Type); +} + +StrC to_str(Token tok) +{ + return { tok.Length, tok.Text }; +} + +bool is_valid( Token tok ) +{ + return tok.Text && tok.Length && tok.Type != TokType::Invalid; +} + +bool is_access_operator(Token tok) +{ + return bitfield_is_equal( u32, tok.Flags, TF_AccessOperator ); +} + +bool is_access_specifier(Token tok) +{ + return bitfield_is_equal( u32, tok.Flags, TF_AccessSpecifier ); +} + +bool is_attribute(Token tok) +{ + return bitfield_is_equal( u32, tok.Flags, TF_Attribute ); +} + +bool is_operator(Token tok) +{ + return bitfield_is_equal( u32, tok.Flags, TF_Operator ); +} + +bool is_preprocessor(Token tok) +{ + return bitfield_is_equal( u32, tok.Flags, TF_Preprocess ); +} + +bool is_preprocess_cond(Token tok) +{ + return bitfield_is_equal( u32, tok.Flags, TF_Preprocess_Cond ); +} + +bool is_specifier(Token tok) +{ + return bitfield_is_equal( u32, tok.Flags, TF_Specifier ); +} + +bool is_end_definition(Token tok) +{ + return bitfield_is_equal( u32, tok.Flags, TF_EndDefinition ); +} + +String to_string(Token tok) +{ + String result = string_make_reserve( GlobalAllocator, kilobytes(4) ); + + StrC type_str = ETokType::to_str( tok.Type ); + + append_fmt( & result, "Line: %d Column: %d, Type: %.*s Content: %.*s" + , tok.Line, tok.Column + , type_str.Len, type_str.Ptr + , tok.Length, tok.Text + ); + + return result; +} + struct TokArray { - Array Arr; + Array(Token) Arr; s32 Idx; - - bool __eat( TokType type ); - - Token& current( bool skip_formatting = true ) - { - if ( skip_formatting ) - { - while ( Arr[Idx].Type == TokType::NewLine || Arr[Idx].Type == TokType::Comment ) - Idx++; - } - - return Arr[Idx]; - } - - Token& previous( bool skip_formatting = false ) - { - s32 idx = this->Idx; - - if ( skip_formatting ) - { - while ( Arr[idx].Type == TokType::NewLine ) - idx--; - - return Arr[idx]; - } - - return Arr[idx - 1]; - } - - Token& next( bool skip_formatting = false ) - { - s32 idx = this->Idx; - - if ( skip_formatting ) - { - while ( Arr[idx].Type == TokType::NewLine ) - idx++; - - return Arr[idx + 1]; - } - - return Arr[idx + 1]; - } - - Token& operator []( s32 idx ) - { - return Arr[idx]; - } }; +bool __eat( TokType type ); + +Token* current(TokArray* self, bool skip_formatting ) +{ + if ( skip_formatting ) + { + while ( self->Arr[self->Idx].Type == TokType::NewLine || self->Arr[self->Idx].Type == TokType::Comment ) + self->Idx++; + } + + return & self->Arr[self->Idx]; +} + +Token* previous(TokArray self, bool skip_formatting) +{ + s32 idx = self.Idx; + + if ( skip_formatting ) + { + while ( self.Arr[idx].Type == TokType::NewLine ) + idx --; + + return & self.Arr[idx]; + } + + return & self.Arr[idx - 1]; +} + +Token* next(TokArray self, bool skip_formatting) +{ + s32 idx = self.Idx; + + if ( skip_formatting ) + { + while ( self.Arr[idx].Type == TokType::NewLine ) + idx++; + + return & self.Arr[idx + 1]; + } + + return & self.Arr[idx + 1]; +} + global Arena_256KB defines_map_arena; -global HashTable defines; -global Array Tokens; +global HashTable(StrC) defines; +global Array(Token) Tokens; #define current ( * scanner ) @@ -234,7 +229,7 @@ s32 lex_preprocessor_directive( token.Length++; } - token.Type = ETokType::to_type( token ); + token.Type = ETokType::to_type( to_str(token) ); bool is_preprocessor = token.Type >= TokType::Preprocess_Define && token.Type <= TokType::Preprocess_Pragma; if ( ! is_preprocessor ) @@ -341,7 +336,7 @@ s32 lex_preprocessor_directive( append( & Tokens, name ); u64 key = crc32( name.Text, name.Length ); - set(& defines, key, name ); + set(& defines, key, to_str(name) ); } Token preprocess_content = { scanner, 0, TokType::Preprocess_Content, line, column, TF_Preprocess }; @@ -465,7 +460,7 @@ void lex_found_token( StrC& content return; } - TokType type = ETokType::to_type( token ); + TokType type = ETokType::to_type( to_str(token) ); if (type <= TokType::Access_Public && type >= TokType::Access_Private ) { @@ -1267,5 +1262,4 @@ TokArray lex( StrC content ) #undef move_forward #undef SkipWhitespace -// namespace parser -} +GEN_NS_PARSER_END diff --git a/project/components/parser.cpp b/project/components/parser.cpp index baf78ed..66a0de5 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -5,11 +5,12 @@ #include "lexer.cpp" #endif -namespace parser { +GEN_NS_PARSER_BEGIN // TODO(Ed) : Rename ETokType::Capture_Start, ETokType::Capture_End to Open_Parenthesis adn Close_Parenthesis constexpr bool dont_skip_formatting = false; +constexpr bool skip_formatting = true; struct StackNode { @@ -48,7 +49,7 @@ struct ParseContext String result = string_make_reserve( GlobalAllocator, kilobytes(4) ); Token scope_start = Scope->Start; - Token last_valid = Tokens.Idx >= num(Tokens.Arr) ? Tokens.Arr[num(Tokens.Arr) -1] : Tokens.current(); + Token last_valid = Tokens.Idx >= num(Tokens.Arr) ? Tokens.Arr[num(Tokens.Arr) -1] : (* current(& Tokens, true)); sptr length = scope_start.Length; char const* current = scope_start.Text + length; @@ -75,7 +76,7 @@ struct ParseContext s32 level = 0; do { - if ( curr_scope->Name ) + if ( is_valid(curr_scope->Name) ) { append_fmt(& result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text ); } @@ -94,27 +95,30 @@ struct ParseContext global ParseContext Context; -bool TokArray::__eat( TokType type ) +bool __eat(TokArray* self, TokType type ) { - if ( num(Arr) - Idx <= 0 ) + if ( num(self->Arr) - self->Idx <= 0 ) { log_failure( "No tokens left.\n%s", Context.to_string() ); return false; } - if ( ( Arr[ Idx ].Type == TokType::NewLine && type != TokType::NewLine ) - || ( Arr[ Idx ].Type == TokType::Comment && type != TokType::Comment ) ) + Token at_idx = self->Arr[ self->Idx ]; + + if ( ( at_idx.Type == TokType::NewLine && type != TokType::NewLine ) + || ( at_idx.Type == TokType::Comment && type != TokType::Comment ) ) { - Idx++; + self->Idx ++; } - if ( Arr[Idx].Type != type ) + if ( at_idx.Type != type ) { + Token tok = * current( self, skip_formatting ); log_failure( "Parse Error, TokArray::eat, Expected: ' %s ' not ' %.*s ' (%d, %d)`\n%s" , ETokType::to_str(type).Ptr - , Arr[Idx].Length, Arr[Idx].Text - , current().Line - , current().Column + , at_idx.Length, at_idx.Text + , tok.Line + , tok.Column , Context.to_string() ); @@ -122,10 +126,10 @@ bool TokArray::__eat( TokType type ) } #if 0 && Build_Debug - log_fmt("Ate: %S\n", Arr[Idx].to_string() ); + log_fmt("Ate: %S\n", self->Arr[Idx].to_string() ); #endif - Idx++; + self->Idx ++; return true; } @@ -162,11 +166,11 @@ if ( def.Ptr == nullptr ) \ return InvalidCode; \ } -# define currtok_noskip Context.Tokens.current( dont_skip_formatting ) -# define currtok Context.Tokens.current() -# define prevtok Context.Tokens.previous() -# define nexttok Context.Tokens.next() -# define eat( Type_ ) Context.Tokens.__eat( Type_ ) +# define currtok_noskip (* current( & Context.Tokens, dont_skip_formatting )) +# define currtok (* current( & Context.Tokens, skip_formatting )) +# define prevtok (* previous( Context.Tokens, dont_skip_formatting)) +# define nexttok (* next( Context.Tokens, skip_formatting )) +# define eat( Type_ ) __eat( & Context.Tokens, Type_ ) # define left ( num(Context.Tokens.Arr) - Context.Tokens.Idx ) #ifdef check @@ -176,9 +180,9 @@ if ( def.Ptr == nullptr ) \ #endif # define check_noskip( Type_ ) ( left && currtok_noskip.Type == Type_ ) -# define check( Type_ ) ( left && currtok.Type == Type_ ) +# define check( Type_ ) ( left && currtok.Type == Type_ ) -# define push_scope() \ +# define push_scope() \ StackNode scope { nullptr, currtok_noskip, NullToken, txt( __func__ ) }; \ Context.push( & scope ) @@ -488,7 +492,7 @@ Code parse_array_decl() if ( check( TokType::Operator ) && currtok.Text[0] == '[' && currtok.Text[1] == ']' ) { - Code array_expr = untyped_str( currtok ); + Code array_expr = untyped_str( to_str(currtok) ); eat( TokType::Operator ); // [] @@ -524,7 +528,7 @@ Code parse_array_decl() untyped_tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)untyped_tok.Text; - Code array_expr = untyped_str( untyped_tok ); + Code array_expr = untyped_str( to_str(untyped_tok) ); // [ if ( left == 0 ) @@ -571,7 +575,7 @@ CodeAttributes parse_attributes() // There can be more than one attribute. If there is flatten them to a single string. // TODO(Ed): Support keeping an linked list of attributes similar to parameters - while ( left && currtok.is_attribute() ) + while ( left && is_attribute(currtok) ) { if ( check( TokType::Attribute_Open ) ) { @@ -625,7 +629,7 @@ CodeAttributes parse_attributes() len = ( ( sptr )prevtok.Text + prevtok.Length ) - ( sptr )start.Text; } - else if ( currtok.is_attribute() ) + else if ( is_attribute(currtok) ) { eat( currtok.Type ); // @@ -724,15 +728,15 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) eat( TokType::Assign_Classifer ); // : - if ( currtok.is_access_specifier() ) + if ( is_access_specifier(currtok) ) { - access = currtok.to_access_specifier(); + access = to_access_specifier(currtok); // : eat( currtok.Type ); } Token parent_tok = parse_identifier(); - parent = def_type( parent_tok ); + parent = def_type( to_str(parent_tok) ); // : while ( check(TokType::Comma) ) @@ -740,13 +744,13 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) eat( TokType::Comma ); // : , - if ( currtok.is_access_specifier() ) + if ( is_access_specifier(currtok) ) { eat(currtok.Type); } Token interface_tok = parse_identifier(); - append( & interfaces, def_type( interface_tok ) ); + append( & interfaces, def_type( to_str(interface_tok) ) ); // : , ... } } @@ -770,10 +774,10 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) } if ( which == TokType::Decl_Class ) - result = def_class( name, body, parent, access, attributes, mflags ); + result = def_class( to_str(name), body, parent, access, attributes, mflags ); else - result = def_struct( name, body, (CodeType)parent, access, attributes, mflags ); + result = def_struct( to_str(name), body, (CodeType)parent, access, attributes, mflags ); if ( inline_cmt ) result->InlineCmt = inline_cmt; @@ -818,7 +822,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) case TokType::Statement_End: { // TODO(Ed): Convert this to a general warning procedure - log_fmt("Dangling end statement found %S\n", currtok_noskip.to_string()); + log_fmt("Dangling end statement found %S\n", to_string(currtok_noskip)); eat( TokType::Statement_End ); continue; } @@ -984,9 +988,9 @@ CodeBody parse_class_struct_body( TokType which, Token name ) SpecifierT specs_found[16] { ESpecifier::NumSpecifiers }; s32 NumSpecifiers = 0; - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { - SpecifierT spec = ESpecifier::to_type( currtok ); + SpecifierT spec = ESpecifier::to_type( to_str(currtok) ); b32 ignore_spec = false; @@ -1033,7 +1037,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) } // - if ( currtok.is_attribute() ) + if ( is_attribute(currtok) ) { // Unfortuantely Unreal has code where there is attirbutes before specifiers CodeAttributes more_attributes = parse_attributes(); @@ -1103,7 +1107,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) eat( currtok.Type ); } - member = untyped_str( untyped_tok ); + member = untyped_str( to_str(untyped_tok) ); // Something unknown break; } @@ -1133,7 +1137,7 @@ CodeComment parse_comment() CodeComment result = (CodeComment) make_code(); result->Type = ECode::Comment; - result->Content = get_cached_string( currtok_noskip ); + result->Content = get_cached_string( to_str(currtok_noskip) ); result->Name = result->Content; // result->Token = currtok_noskip; eat( TokType::Comment ); @@ -1155,13 +1159,13 @@ Code parse_complicated_definition( TokType which ) s32 level = 0; for ( ; idx < num(tokens.Arr); idx++ ) { - if ( tokens[ idx ].Type == TokType::BraceCurly_Open ) + if ( tokens.Arr[ idx ].Type == TokType::BraceCurly_Open ) level++; - if ( tokens[ idx ].Type == TokType::BraceCurly_Close ) + if ( tokens.Arr[ idx ].Type == TokType::BraceCurly_Close ) level--; - if ( level == 0 && tokens[ idx ].Type == TokType::Statement_End ) + if ( level == 0 && tokens.Arr[ idx ].Type == TokType::Statement_End ) break; } @@ -1174,23 +1178,23 @@ Code parse_complicated_definition( TokType which ) return result; } - Token tok = tokens[ idx - 1 ]; - if ( tok.is_specifier() && is_trailing( ESpecifier::to_type(tok)) ) + Token tok = tokens.Arr[ idx - 1 ]; + if ( is_specifier(tok) && is_trailing( ESpecifier::to_type( to_str(tok))) ) { // (...) ...; s32 spec_idx = idx - 1; - Token spec = tokens[spec_idx]; - while ( spec.is_specifier() && is_trailing( ESpecifier::to_type(spec)) ) + Token spec = tokens.Arr[spec_idx]; + while ( is_specifier(spec) && is_trailing( ESpecifier::to_type( to_str(spec))) ) { -- spec_idx; - spec = tokens[spec_idx]; + spec = tokens.Arr[spec_idx]; } - if ( tokens[spec_idx].Type == TokType::Capture_End ) + if ( tokens.Arr[spec_idx].Type == TokType::Capture_End ) { // Forward declaration with trailing specifiers for a procedure - tok = tokens[spec_idx]; + tok = tokens.Arr[spec_idx]; Code result = parse_operator_function_or_variable( false, { nullptr }, { nullptr } ); // , or Name> ... @@ -1204,7 +1208,7 @@ Code parse_complicated_definition( TokType which ) } if ( tok.Type == TokType::Identifier ) { - tok = tokens[ idx - 2 ]; + tok = tokens.Arr[ idx - 2 ]; bool is_indirection = tok.Type == TokType::Ampersand || tok.Type == TokType::Star; bool ok_to_parse = false; @@ -1215,15 +1219,15 @@ Code parse_complicated_definition( TokType which ) ok_to_parse = true; is_inplace = true; } - else if ( tok.Type == TokType::Identifier && tokens[ idx - 3 ].Type == which ) + else if ( tok.Type == TokType::Identifier && tokens.Arr[ idx - 3 ].Type == which ) { // Its a variable with type ID using namespace. // ; ok_to_parse = true; } else if ( tok.Type == TokType::Assign_Classifer - && ( ( tokens[idx - 5].Type == which && tokens[idx - 4].Type == TokType::Decl_Class ) - || ( tokens[idx - 4].Type == which)) + && ( ( tokens.Arr[idx - 5].Type == which && tokens.Arr[idx - 4].Type == TokType::Decl_Class ) + || ( tokens.Arr[idx - 4].Type == which)) ) { // Its a forward declaration of an enum @@ -1255,11 +1259,11 @@ Code parse_complicated_definition( TokType which ) } else if ( tok.Type >= TokType::Type_Unsigned && tok.Type <= TokType::Type_MS_W64 ) { - tok = tokens[ idx - 2 ]; + tok = tokens.Arr[ idx - 2 ]; if ( tok.Type != TokType::Assign_Classifer - || ( ( tokens[idx - 5].Type != which && tokens[idx - 4].Type != TokType::Decl_Class ) - && ( tokens[idx - 4].Type != which)) + || ( ( tokens.Arr[idx - 5].Type != which && tokens.Arr[idx - 4].Type != TokType::Decl_Class ) + && ( tokens.Arr[idx - 4].Type != which)) ) { log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), Context.to_string() ); @@ -1317,7 +1321,7 @@ CodeDefine parse_define() } Context.Scope->Name = currtok; - define->Name = get_cached_string( currtok ); + define->Name = get_cached_string( to_str(currtok) ); eat( TokType::Identifier ); // #define @@ -1330,7 +1334,7 @@ CodeDefine parse_define() if ( currtok.Length == 0 ) { - define->Content = get_cached_string( currtok ); + define->Content = get_cached_string( to_str(currtok) ); eat( TokType::Preprocess_Content ); // #define @@ -1338,7 +1342,7 @@ CodeDefine parse_define() return define; } - define->Content = get_cached_string( strip_formatting( currtok, strip_formatting_dont_preserve_newlines ) ); + define->Content = get_cached_string( strip_formatting( to_str(currtok), strip_formatting_dont_preserve_newlines ) ); eat( TokType::Preprocess_Content ); // #define @@ -1379,7 +1383,7 @@ Code parse_assignment_expression() } expr_tok.Length = ( ( sptr )currtok.Text + currtok.Length ) - ( sptr )expr_tok.Text - 1; - expr = untyped_str( expr_tok ); + expr = untyped_str( to_str(expr_tok) ); // = return expr; } @@ -1431,16 +1435,16 @@ CodeFn parse_function_after_name( // ( ) // TODO(Ed), Review old comment : These have to be kept separate from the return type's specifiers. - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { if ( specifiers.ast == nullptr ) { - specifiers = def_specifier( ESpecifier::to_type(currtok) ); + specifiers = def_specifier( ESpecifier::to_type( to_str(currtok)) ); eat( currtok.Type ); continue; } - append(specifiers, ESpecifier::to_type(currtok) ); + append(specifiers, ESpecifier::to_type( to_str(currtok)) ); eat( currtok.Type ); } // ( ) @@ -1485,7 +1489,7 @@ CodeFn parse_function_after_name( using namespace ECode; String - name_stripped = string_make( GlobalAllocator, name ); + name_stripped = string_make( GlobalAllocator, to_str(name) ); strip_space(name_stripped); CodeFn @@ -1562,7 +1566,7 @@ Code parse_function_body() eat( currtok_noskip.Type ); } - Token previous = prevtok; + Token past = prevtok; s32 len = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)start.Text; @@ -1613,7 +1617,7 @@ CodeBody parse_global_nspace( CodeT which ) case TokType::Statement_End: { // TODO(Ed): Convert this to a general warning procedure - log_fmt("Dangling end statement found %S\n", currtok_noskip.to_string()); + log_fmt("Dangling end statement found %S\n", to_string(currtok_noskip)); eat( TokType::Statement_End ); continue; } @@ -1764,9 +1768,9 @@ CodeBody parse_global_nspace( CodeT which ) SpecifierT specs_found[16] { ESpecifier::NumSpecifiers }; s32 NumSpecifiers = 0; - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { - SpecifierT spec = ESpecifier::to_type( currtok ); + SpecifierT spec = ESpecifier::to_type( to_str(currtok) ); bool ignore_spec = false; @@ -1840,12 +1844,12 @@ CodeBody parse_global_nspace( CodeT which ) for ( ; idx < num(Context.Tokens.Arr); idx++ ) { - Token tok = Context.Tokens[ idx ]; + Token tok = Context.Tokens.Arr[ idx ]; if ( tok.Type == TokType::Identifier ) { idx++; - tok = Context.Tokens[ idx ]; + tok = Context.Tokens.Arr[ idx ]; if ( tok.Type == TokType::Access_StaticSymbol ) continue; @@ -1909,15 +1913,15 @@ Code parse_global_nspace_constructor_destructor( CodeSpecifiers specifiers ) TokArray tokens = Context.Tokens; s32 idx = tokens.Idx; - Token nav = tokens[ idx ]; - for ( ; idx < num(tokens.Arr); idx++, nav = tokens[ idx ] ) + Token nav = tokens.Arr[ idx ]; + for ( ; idx < num(tokens.Arr); idx++, nav = tokens.Arr[ idx ] ) { if ( nav.Text[0] == '<' ) { // Skip templated expressions as they mey have expressions with the () operators s32 capture_level = 0; s32 template_level = 0; - for ( ; idx < num(tokens.Arr); idx++, nav = tokens[idx] ) + for ( ; idx < num(tokens.Arr); idx++, nav = tokens.Arr[idx] ) { if (nav.Text[ 0 ] == '<') ++ template_level; @@ -1945,7 +1949,7 @@ Code parse_global_nspace_constructor_destructor( CodeSpecifiers specifiers ) } -- idx; - Token tok_right = tokens[idx]; + Token tok_right = tokens.Arr[idx]; Token tok_left = NullToken; if (tok_right.Type != TokType::Identifier) @@ -1955,7 +1959,7 @@ Code parse_global_nspace_constructor_destructor( CodeSpecifiers specifiers ) } -- idx; - tok_left = tokens[idx]; + tok_left = tokens.Arr[idx]; // ... bool possible_destructor = false; @@ -1963,14 +1967,14 @@ Code parse_global_nspace_constructor_destructor( CodeSpecifiers specifiers ) { possible_destructor = true; -- idx; - tok_left = tokens[idx]; + tok_left = tokens.Arr[idx]; } if ( tok_left.Type != TokType::Access_StaticSymbol ) return result; -- idx; - tok_left = tokens[idx]; + tok_left = tokens.Arr[idx]; // ... :: // We search toward the left until we find the next valid identifier @@ -1996,7 +2000,7 @@ Code parse_global_nspace_constructor_destructor( CodeSpecifiers specifiers ) break; -- idx; - tok_left = tokens[idx]; + tok_left = tokens.Arr[idx]; } bool is_same = str_compare( tok_right.Text, tok_left.Text, tok_right.Length ) == 0; @@ -2112,7 +2116,7 @@ CodeInclude parse_include() } Context.Scope->Name = currtok; - include->Content = get_cached_string( currtok ); + include->Content = get_cached_string( to_str(currtok) ); eat( TokType::String ); // #include or "Path" @@ -2364,10 +2368,10 @@ CodeOperator parse_operator_after_ret_type( s32 idx = Context.Tokens.Idx + 1; { - while ( Context.Tokens[ idx ].Type == TokType::NewLine ) + while ( Context.Tokens.Arr[ idx ].Type == TokType::NewLine ) idx++; } - Token next = Context.Tokens[idx]; + Token next = Context.Tokens.Arr[idx]; if ( currtok.Type == TokType::Operator && str_compare(currtok.Text, "[]", 2) == 0) { eat(ETokType::Operator); @@ -2388,10 +2392,10 @@ CodeOperator parse_operator_after_ret_type( s32 idx = Context.Tokens.Idx + 1; { - while ( Context.Tokens[ idx ].Type == TokType::NewLine ) + while ( Context.Tokens.Arr[ idx ].Type == TokType::NewLine ) idx++; } - Token next = Context.Tokens[idx]; + Token next = Context.Tokens.Arr[idx]; if ( currtok.Type == TokType::Operator && str_compare(currtok.Text, "[]", 2) == 0) { eat(ETokType::Operator); @@ -2434,16 +2438,16 @@ CodeOperator parse_operator_after_ret_type( if ( params.ast == nullptr && op == EOperator::Multiply ) op = MemberOfPointer; - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { if ( specifiers.ast == nullptr ) { - specifiers = def_specifier( ESpecifier::to_type(currtok) ); + specifiers = def_specifier( ESpecifier::to_type( to_str(currtok)) ); eat( currtok.Type ); continue; } - append(specifiers, ESpecifier::to_type(currtok) ); + append(specifiers, ESpecifier::to_type( to_str(currtok)) ); eat( currtok.Type ); } // operator ( ) @@ -2473,7 +2477,7 @@ CodeOperator parse_operator_after_ret_type( } // OpValidateResult check_result = operator__validate( op, params, ret_type, specifiers ); - CodeOperator result = def_operator( op, nspace, params, ret_type, body, specifiers, attributes, mflags ); + CodeOperator result = def_operator( op, to_str(nspace), params, ret_type, body, specifiers, attributes, mflags ); if ( inline_cmt ) result->InlineCmt = inline_cmt; @@ -2514,12 +2518,12 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes for ( ; idx < num(Context.Tokens.Arr); idx++ ) { - Token tok = Context.Tokens[ idx ]; + Token tok = Context.Tokens.Arr[ idx ]; if ( tok.Type == TokType::Identifier ) { idx++; - tok = Context.Tokens[ idx ]; + tok = Context.Tokens.Arr[ idx ]; if ( tok.Type == TokType::Access_StaticSymbol ) continue; @@ -2566,7 +2570,7 @@ Code parse_operator_function_or_variable( bool expects_function, CodeAttributes } // Dealing with a variable - result = parse_variable_after_name( ModuleFlag_None, attributes, specifiers, type, name ); + result = parse_variable_after_name( ModuleFlag_None, attributes, specifiers, type, to_str(name) ); // ... } } @@ -2595,7 +2599,7 @@ CodePragma parse_pragma() Context.Scope->Name = currtok; - pragma->Content = get_cached_string( currtok ); + pragma->Content = get_cached_string( to_str(currtok) ); eat( TokType::Preprocess_Content ); // #pragma @@ -2729,7 +2733,7 @@ CodeParam parse_params( bool use_template_capture ) eat( currtok.Type ); } - value = untyped_str( strip_formatting( value_tok, strip_formatting_dont_preserve_newlines ) ); + value = untyped_str( strip_formatting( to_str(value_tok), strip_formatting_dont_preserve_newlines ) ); // ( = } } @@ -2740,7 +2744,7 @@ CodeParam parse_params( bool use_template_capture ) result->Macro = macro; if ( name.Length > 0 ) - result->Name = get_cached_string( name ); + result->Name = get_cached_string( to_str(name) ); result->ValueType = type; @@ -2844,7 +2848,7 @@ CodeParam parse_params( bool use_template_capture ) eat( currtok.Type ); } - value = untyped_str( strip_formatting( value_tok, strip_formatting_dont_preserve_newlines ) ); + value = untyped_str( strip_formatting( to_str(value_tok), strip_formatting_dont_preserve_newlines ) ); // ( = , = } // ( = , = , .. @@ -2856,7 +2860,7 @@ CodeParam parse_params( bool use_template_capture ) param->Macro = macro; if ( name.Length > 0 ) - param->Name = get_cached_string( name ); + param->Name = get_cached_string( to_str(name) ); param->PostNameMacro = post_name_macro; param->ValueType = type; @@ -2893,7 +2897,7 @@ CodePreprocessCond parse_preprocess_cond() { push_scope(); - if ( ! currtok.is_preprocess_cond() ) + if ( ! is_preprocess_cond(currtok) ) { log_failure( "Error, expected preprocess conditional\n%s", Context.to_string() ); Context.pop(); @@ -2914,7 +2918,7 @@ CodePreprocessCond parse_preprocess_cond() } Context.Scope->Name = currtok; - cond->Content = get_cached_string( currtok ); + cond->Content = get_cached_string( to_str(currtok) ); eat( TokType::Preprocess_Content ); // # @@ -2993,7 +2997,7 @@ Code parse_simple_preprocess( TokType which ) char const* content = str_fmt_buf( "%.*s ", tok.Length, tok.Text ); - Code result = untyped_str( to_str( content ) ); + Code result = untyped_str( GEN_NS to_str(content) ); Context.Scope->Name = tok; Context.pop(); @@ -3126,7 +3130,7 @@ CodeVar parse_variable_after_name( eat( TokType::BraceCurly_Close ); expr_tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)expr_tok.Text; - expr = untyped_str( expr_tok ); + expr = untyped_str( to_str(expr_tok) ); // = { } } @@ -3152,7 +3156,7 @@ CodeVar parse_variable_after_name( } expr_token.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)expr_token.Text; - expr = untyped_str( expr_token ); + expr = untyped_str( to_str(expr_token) ); eat( TokType::Capture_End ); // ( ) } @@ -3177,7 +3181,7 @@ CodeVar parse_variable_after_name( } expr_tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)expr_tok.Text; - bitfield_expr = untyped_str( expr_tok ); + bitfield_expr = untyped_str( to_str(expr_tok) ); // : } @@ -3275,9 +3279,9 @@ CodeVar parse_variable_declaration_list() CodeSpecifiers specifiers = NullCode; - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { - SpecifierT spec = ESpecifier::to_type( currtok ); + SpecifierT spec = ESpecifier::to_type( to_str(currtok) ); switch ( spec ) { @@ -3316,7 +3320,7 @@ CodeVar parse_variable_declaration_list() } // , - StrC name = currtok; + StrC name = to_str(currtok); eat( TokType::Identifier ); // , @@ -3385,7 +3389,7 @@ CodeConstructor parse_constructor( CodeSpecifiers specifiers ) initializer_list_tok.Length = ( ( sptr )prevtok.Text + prevtok.Length ) - ( sptr )initializer_list_tok.Text; // ( ) : - initializer_list = untyped_str( initializer_list_tok ); + initializer_list = untyped_str( to_str(initializer_list_tok) ); // TODO(Ed): Constructors can have post-fix specifiers @@ -3414,7 +3418,7 @@ CodeConstructor parse_constructor( CodeSpecifiers specifiers ) CodeConstructor result = ( CodeConstructor )make_code(); - result->Name = get_cached_string(identifier); + result->Name = get_cached_string( to_str(identifier)); result->Specs = specifiers; @@ -3486,8 +3490,8 @@ CodeDestructor parse_destructor( CodeSpecifiers specifiers ) // ~() = bool skip_formatting = true; - Token next = Context.Tokens.next(skip_formatting); - if ( left && next.Text[ 0 ] == '0' ) + Token upcoming = nexttok; + if ( left && upcoming.Text[ 0 ] == '0' ) { eat( TokType::Operator ); eat( TokType::Number ); @@ -3495,7 +3499,7 @@ CodeDestructor parse_destructor( CodeSpecifiers specifiers ) append(specifiers, ESpecifier::Pure ); } - else if ( left && str_compare( next.Text, "default", sizeof("default") - 1 ) == 0) + else if ( left && str_compare( upcoming.Text, "default", sizeof("default") - 1 ) == 0) { body = parse_assignment_expression(); // ~< @@ -3525,10 +3529,10 @@ CodeDestructor parse_destructor( CodeSpecifiers specifiers ) CodeDestructor result = ( CodeDestructor )make_code(); - if ( prefix_identifier ) + if ( is_valid(prefix_identifier) ) { prefix_identifier.Length += 1 + identifier.Length; - result->Name = get_cached_string( prefix_identifier ); + result->Name = get_cached_string( to_str(prefix_identifier) ); } if ( specifiers ) @@ -3646,7 +3650,7 @@ CodeEnum parse_enum( bool inplace_def ) switch ( currtok_noskip.Type ) { case TokType::NewLine: - member = untyped_str( currtok_noskip ); + member = untyped_str( to_str(currtok_noskip) ); eat( TokType::NewLine ); break; @@ -3730,9 +3734,11 @@ CodeEnum parse_enum( bool inplace_def ) // = , // // } + + //Token prev = * previous(Context.Tokens, dont_skip_formatting); entry.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)entry.Text; - member = untyped_str( entry ); + member = untyped_str( to_str(entry) ); break; } @@ -3778,7 +3784,7 @@ CodeEnum parse_enum( bool inplace_def ) result->Type = is_enum_class ? Enum_Class_Fwd : Enum_Fwd; } - result->Name = get_cached_string( name ); + result->Name = get_cached_string( to_str(name) ); if ( attributes ) result->Attributes = attributes; @@ -3836,7 +3842,7 @@ CodeExtern parse_extern_link() CodeExtern result = (CodeExtern) make_code(); result->Type = ECode::Extern_Linkage; - result->Name = get_cached_string( name ); + result->Name = get_cached_string( to_str(name) ); Code entry = parse_extern_link_body(); if ( entry == Code_Invalid ) @@ -3947,9 +3953,9 @@ CodeFn parse_function() attributes = parse_attributes(); // - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { - SpecifierT spec = ESpecifier::to_type( currtok ); + SpecifierT spec = ESpecifier::to_type( to_str(currtok) ); switch ( spec ) { @@ -3993,7 +3999,7 @@ CodeFn parse_function() Token name = parse_identifier(); Context.Scope->Name = name; - if ( ! name ) + if ( ! is_valid(name) ) { Context.pop(); return InvalidCode; @@ -4030,7 +4036,7 @@ CodeNS parse_namespace() CodeNS result = (CodeNS) make_code(); result->Type = ECode::Namespace; - result->Name = get_cached_string( name ); + result->Name = get_cached_string( to_str(name) ); result->Body = body; @@ -4060,9 +4066,9 @@ CodeOperator parse_operator() attributes = parse_attributes(); // - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { - SpecifierT spec = ESpecifier::to_type( currtok ); + SpecifierT spec = ESpecifier::to_type( to_str(currtok) ); switch ( spec ) { @@ -4180,7 +4186,7 @@ CodeOpCast parse_operator_cast( CodeSpecifiers specifiers ) eat( TokType::BraceCurly_Close ); // :: ... operator () { } - body = untyped_str( body_str ); + body = untyped_str( to_str(body_str) ); } else { @@ -4195,8 +4201,8 @@ CodeOpCast parse_operator_cast( CodeSpecifiers specifiers ) CodeOpCast result = (CodeOpCast) make_code(); - if ( name ) - result->Name = get_cached_string( name ); + if ( is_valid(name) ) + result->Name = get_cached_string( to_str(name) ); if (body) { @@ -4301,9 +4307,9 @@ CodeTemplate parse_template() // Specifiers Parsing { - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { - SpecifierT spec = ESpecifier::to_type( currtok ); + SpecifierT spec = ESpecifier::to_type( to_str(currtok) ); switch ( spec ) { @@ -4371,12 +4377,12 @@ CodeTemplate parse_template() for ( ; idx < num(Context.Tokens.Arr); idx++ ) { - Token tok = Context.Tokens[ idx ]; + Token tok = Context.Tokens.Arr[ idx ]; if ( tok.Type == TokType::Identifier ) { idx++; - tok = Context.Tokens[ idx ]; + tok = Context.Tokens.Arr[ idx ]; if ( tok.Type == TokType::Access_StaticSymbol ) continue; @@ -4443,9 +4449,9 @@ CodeType parse_type( bool from_template, bool* typedef_is_function ) // // Prefix specifiers - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { - SpecifierT spec = ESpecifier::to_type( currtok ); + SpecifierT spec = ESpecifier::to_type( to_str(currtok) ); if ( spec != ESpecifier::Const ) { @@ -4544,7 +4550,7 @@ else if ( currtok.Type == TokType::DeclType ) { name = parse_identifier(); Context.Scope->Name = name; - if ( ! name ) + if ( ! is_valid(name) ) { log_failure( "Error, failed to type signature\n%s", Context.to_string() ); Context.pop(); @@ -4558,7 +4564,7 @@ else if ( currtok.Type == TokType::DeclType ) { name = parse_identifier(); Context.Scope->Name = name; - if ( ! name ) + if ( ! is_valid(name) ) { log_failure( "Error, failed to type signature\n%s", Context.to_string() ); Context.pop(); @@ -4569,9 +4575,9 @@ else if ( currtok.Type == TokType::DeclType ) } // Suffix specifiers for typename. - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { - SpecifierT spec = ESpecifier::to_type( currtok ); + SpecifierT spec = ESpecifier::to_type( to_str(currtok) ); if ( spec != ESpecifier::Const && spec != ESpecifier::Ptr && spec != ESpecifier::Ref && spec != ESpecifier::RValue ) { @@ -4657,7 +4663,7 @@ else if ( currtok.Type == TokType::DeclType ) // String // name_stripped = String::make( GlobalAllocator, name ); // name_stripped.strip_space(); - return_type->Name = get_cached_string( name ); + return_type->Name = get_cached_string( to_str(name) ); #ifdef GEN_USE_NEW_TYPENAME_PARSING if ( specifiers ) @@ -4769,9 +4775,9 @@ else if ( currtok.Type == TokType::DeclType ) // ( ) // Look for suffix specifiers for the function - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { - SpecifierT spec = ESpecifier::to_type( currtok ); + SpecifierT spec = ESpecifier::to_type( to_str(currtok) ); if ( spec != ESpecifier::Const // TODO : Add support for NoExcept, l-value, volatile, l-value, etc @@ -4814,7 +4820,7 @@ else if ( currtok.Type == TokType::DeclType ) // result->Token = Context.Scope->Start; // Need to wait until were using the new parsing method to do this. - String name_stripped = strip_formatting( name, strip_formatting_dont_preserve_newlines ); + String name_stripped = strip_formatting( to_str(name), strip_formatting_dont_preserve_newlines ); // name_stripped.strip_space(); @@ -4922,13 +4928,13 @@ CodeTypedef parse_typedef() s32 level = 0; for ( ; idx < num(tokens.Arr); idx ++ ) { - if ( tokens[idx].Type == TokType::BraceCurly_Open ) + if ( tokens.Arr[idx].Type == TokType::BraceCurly_Open ) level++; - if ( tokens[idx].Type == TokType::BraceCurly_Close ) + if ( tokens.Arr[idx].Type == TokType::BraceCurly_Close ) level--; - if ( level == 0 && tokens[idx].Type == TokType::Statement_End ) + if ( level == 0 && tokens.Arr[idx].Type == TokType::Statement_End ) break; } @@ -5055,7 +5061,7 @@ CodeTypedef parse_typedef() } else { - result->Name = get_cached_string( name ); + result->Name = get_cached_string( to_str(name) ); result->IsFunction = false; } @@ -5099,7 +5105,7 @@ CodeUnion parse_union( bool inplace_def ) StrC name = { 0, nullptr }; if ( check( TokType::Identifier ) ) { - name = currtok; + name = to_str(currtok); Context.Scope->Name = currtok; eat( TokType::Identifier ); } @@ -5288,7 +5294,7 @@ CodeUsing parse_using() CodeUsing result = (CodeUsing) make_code(); - result->Name = get_cached_string( name ); + result->Name = get_cached_string( to_str(name) ); result->ModuleFlags = mflags; if ( is_namespace) @@ -5338,9 +5344,9 @@ CodeVar parse_variable() attributes = parse_attributes(); // - while ( left && currtok.is_specifier() ) + while ( left && is_specifier(currtok) ) { - SpecifierT spec = ESpecifier::to_type( currtok ); + SpecifierT spec = ESpecifier::to_type( to_str(currtok) ); switch ( spec ) { @@ -5387,7 +5393,7 @@ CodeVar parse_variable() Context.Scope->Name = parse_identifier(); // - CodeVar result = parse_variable_after_name( mflags, attributes, specifiers, type, Context.Scope->Name ); + CodeVar result = parse_variable_after_name( mflags, attributes, specifiers, type, to_str(Context.Scope->Name) ); // Regular : = ; // Bitfield : : = ; @@ -5395,8 +5401,7 @@ CodeVar parse_variable() return result; } -// namespace parser -} +GEN_NS_PARSER_END #ifdef CHECK_WAS_DEFINED #pragma pop_macro("check") diff --git a/project/dependencies/containers.hpp b/project/dependencies/containers.hpp index 9205b95..dcafca4 100644 --- a/project/dependencies/containers.hpp +++ b/project/dependencies/containers.hpp @@ -385,6 +385,8 @@ bool set_capacity(Array* array, usize new_capacity) // TODO(Ed) : This thing needs ALOT of work. #pragma region HashTable +#define HashTable(Type) HashTable + template struct HashTable; struct HashTableFindResult { diff --git a/project/dependencies/platform.hpp b/project/dependencies/platform.hpp index 27a9a6b..85b54c3 100644 --- a/project/dependencies/platform.hpp +++ b/project/dependencies/platform.hpp @@ -133,12 +133,16 @@ #if GEN_DONT_USE_NAMESPACE || GEN_COMPILER_C # if GEN_COMPILER_C +# define GEN_NS_PARSER_BEGIN +# define GEN_NS_PARSER_END # define GEN_NS_ENUM_BEGIN # define GEN_NS_ENUM_END # define GEN_NS # define GEN_NS_BEGIN # define GEN_NS_END # else +# define GEN_NS_PARSER_BEGIN namespace parser { +# define GEN_NS_PARSER_END } # define GEN_NS_ENUM_BEGIN namespace gen_internal_enums { # define GEN_NS_ENUM_END } # define GEN_NS :: @@ -146,6 +150,8 @@ # define GEN_NS_END # endif #else +# define GEN_NS_PARSER_BEGIN namespace parser { +# define GEN_NS_PARSER_END } # define GEN_NS_ENUM_BEGIN namespace gen_internal_enums { # define GEN_NS_ENUM_END } # define GEN_NS gen:: From f28ae57f16800225bf0d80ebbfd952d068bfcc37 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 3 Dec 2024 00:45:30 -0500 Subject: [PATCH 046/112] setup upfront interface to have optional vars in structs (for C later) --- project/components/ast.cpp | 90 +++--- project/components/header_start.hpp | 12 - project/components/interface.hpp | 134 ++++++--- project/components/interface.upfront.cpp | 362 +++++++++++------------ project/components/parser.cpp | 10 +- project/dependencies/macros.hpp | 6 + 6 files changed, 327 insertions(+), 287 deletions(-) diff --git a/project/components/ast.cpp b/project/components/ast.cpp index 90bed56..3b2b4be 100644 --- a/project/components/ast.cpp +++ b/project/components/ast.cpp @@ -415,169 +415,169 @@ void to_string( Code self, String* result ) break; case Class: - to_string_def(cast(CodeClass, {self}), result ); + to_string_def(cast(CodeClass, self), result ); break; case Class_Fwd: - to_string_fwd(cast(CodeClass, {self}), result ); + to_string_fwd(cast(CodeClass, self), result ); break; case Constructor: - to_string_def(cast(CodeConstructor, {self}), result ); + to_string_def(cast(CodeConstructor, self), result ); break; case Constructor_Fwd: - to_string_fwd(cast(CodeConstructor, {self}), result ); + to_string_fwd(cast(CodeConstructor, self), result ); break; case Destructor: - to_string_def(cast(CodeDestructor, {self}), result ); + to_string_def(cast(CodeDestructor, self), result ); break; case Destructor_Fwd: - to_string_fwd(cast(CodeDestructor, {self}), result ); + to_string_fwd(cast(CodeDestructor, self), result ); break; case Enum: - to_string_def(cast(CodeEnum, {self}), result ); + to_string_def(cast(CodeEnum, self), result ); break; case Enum_Fwd: - to_string_fwd(cast(CodeEnum, {self}), result ); + to_string_fwd(cast(CodeEnum, self), result ); break; case Enum_Class: - to_string_class_def(cast(CodeEnum, {self}), result ); + to_string_class_def(cast(CodeEnum, self), result ); break; case Enum_Class_Fwd: - to_string_class_fwd(cast(CodeEnum, {self}), result ); + to_string_class_fwd(cast(CodeEnum, self), result ); break; case Export_Body: - to_string_export(cast(CodeBody, {self}), result ); + to_string_export(cast(CodeBody, self), result ); break; case Extern_Linkage: - to_string(cast(CodeExtern, {self}), result ); + to_string(cast(CodeExtern, self), result ); break; case Friend: - to_string(cast(CodeFriend, {self}), result ); + to_string(cast(CodeFriend, self), result ); break; case Function: - to_string_def(cast(CodeFn, {self}), result ); + to_string_def(cast(CodeFn, self), result ); break; case Function_Fwd: - to_string_fwd(cast(CodeFn, {self}), result ); + to_string_fwd(cast(CodeFn, self), result ); break; case Module: - to_string(cast(CodeModule, {self}), result ); + to_string(cast(CodeModule, self), result ); break; case Namespace: - to_string(cast(CodeNS, {self}), result ); + to_string(cast(CodeNS, self), result ); break; case Operator: case Operator_Member: - to_string_def(cast(CodeOperator, {self}), result ); + to_string_def(cast(CodeOperator, self), result ); break; case Operator_Fwd: case Operator_Member_Fwd: - to_string_fwd(cast(CodeOperator, {self}), result ); + to_string_fwd(cast(CodeOperator, self), result ); break; case Operator_Cast: - to_string_def(cast(CodeOpCast, {self}), result ); + to_string_def(cast(CodeOpCast, self), result ); break; case Operator_Cast_Fwd: - to_string_fwd(cast(CodeOpCast, {self}), result ); + to_string_fwd(cast(CodeOpCast, self), result ); break; case Parameters: - to_string(cast(CodeParam, {self}), result ); + to_string(cast(CodeParam, self), result ); break; case Preprocess_Define: - to_string(cast(CodeDefine, {self}), result ); + to_string(cast(CodeDefine, self), result ); break; case Preprocess_If: - to_string_if(cast(CodePreprocessCond, {self}), result ); + to_string_if(cast(CodePreprocessCond, self), result ); break; case Preprocess_IfDef: - to_string_ifdef(cast(CodePreprocessCond, {self}), result ); + to_string_ifdef(cast(CodePreprocessCond, self), result ); break; case Preprocess_IfNotDef: - to_string_ifndef(cast(CodePreprocessCond, {self}), result ); + to_string_ifndef(cast(CodePreprocessCond, self), result ); break; case Preprocess_Include: - to_string(cast(CodeInclude, {self}), result ); + to_string(cast(CodeInclude, self), result ); break; case Preprocess_ElIf: - to_string_elif(cast(CodePreprocessCond, {self}), result ); + to_string_elif(cast(CodePreprocessCond, self), result ); break; case Preprocess_Else: - to_string_else(cast(CodePreprocessCond, {self}), result ); + to_string_else(cast(CodePreprocessCond, self), result ); break; case Preprocess_EndIf: - to_string_endif(cast(CodePreprocessCond, {self}), result ); + to_string_endif(cast(CodePreprocessCond, self), result ); break; case Preprocess_Pragma: - to_string(cast(CodePragma, {self}), result ); + to_string(cast(CodePragma, self), result ); break; case Specifiers: - to_string(cast(CodeSpecifiers, {self}), result ); + to_string(cast(CodeSpecifiers, self), result ); break; case Struct: - to_string_def(cast(CodeStruct, {self}), result ); + to_string_def(cast(CodeStruct, self), result ); break; case Struct_Fwd: - to_string_fwd(cast(CodeStruct, {self}), result ); + to_string_fwd(cast(CodeStruct, self), result ); break; case Template: - to_string(cast(CodeTemplate, {self}), result ); + to_string(cast(CodeTemplate, self), result ); break; case Typedef: - to_string(cast(CodeTypedef, {self}), result ); + to_string(cast(CodeTypedef, self), result ); break; case Typename: - to_string(cast(CodeType, {self}), result ); + to_string(cast(CodeType, self), result ); break; case Union: - to_string( cast(CodeUnion, {self}), result ); + to_string( cast(CodeUnion, self), result ); break; case Using: - to_string(cast(CodeUsing, {self}), result ); + to_string(cast(CodeUsing, self), result ); break; case Using_Namespace: - to_string_ns(cast(CodeUsing, {self}), result ); + to_string_ns(cast(CodeUsing, self), result ); break; case Variable: - to_string(cast(CodeVar, {self}), result ); + to_string(cast(CodeVar, self), result ); break; case Enum_Body: @@ -588,7 +588,7 @@ void to_string( Code self, String* result ) case Namespace_Body: case Struct_Body: case Union_Body: - to_string( cast(CodeBody, {self}), result ); + to_string( cast(CodeBody, self), result ); break; } } @@ -1160,7 +1160,7 @@ bool validate_body(Code self) #define CheckEntries( Unallowed_Types ) \ do \ { \ - for ( Code entry : cast(CodeBody, {self}) ) \ + for ( Code entry : cast(CodeBody, self) ) \ { \ switch ( entry->Type ) \ { \ @@ -1178,7 +1178,7 @@ bool validate_body(Code self) CheckEntries( GEN_AST_BODY_CLASS_UNALLOWED_TYPES ); break; case Enum_Body: - for ( Code entry : cast(CodeBody, {self}) ) + for ( Code entry : cast(CodeBody, self) ) { if ( entry->Type != Untyped ) { @@ -1197,7 +1197,7 @@ bool validate_body(Code self) CheckEntries( GEN_AST_BODY_FUNCTION_UNALLOWED_TYPES ); break; case Global_Body: - for (Code entry : cast(CodeBody, {self})) + for (Code entry : cast(CodeBody, self)) { switch (entry->Type) { diff --git a/project/components/header_start.hpp b/project/components/header_start.hpp index 571f370..7963536 100644 --- a/project/components/header_start.hpp +++ b/project/components/header_start.hpp @@ -17,15 +17,3 @@ #ifndef GEN_ROLL_OWN_DEPENDENCIES # include "gen.dep.hpp" #endif - -#ifndef GEN_NS_BEGIN -# ifdef GEN_DONT_USE_NAMESPACE -# define GEN_NS -# define GEN_NS_BEGIN -# define GEN_NS_END -# else -# define GEN_NS gen:: -# define GEN_NS_BEGIN namespace gen { -# define GEN_NS_END } -# endif -#endif diff --git a/project/components/interface.hpp b/project/components/interface.hpp index 23bda92..d1c8c45 100644 --- a/project/components/interface.hpp +++ b/project/components/interface.hpp @@ -42,74 +42,126 @@ void set_allocator_type_table ( AllocatorInfo type_reg_allocator ); CodeAttributes def_attributes( StrC content ); CodeComment def_comment ( StrC content ); -CodeClass def_class( StrC name - , Code body = NullCode - , CodeType parent = NullCode, AccessSpec access = AccessSpec_Default - , CodeAttributes attributes = NullCode - , ModuleFlag mflags = ModuleFlag_None - , CodeType* interfaces = nullptr, s32 num_interfaces = 0 ); +struct Opts_def_struct { + Code body; + CodeType parent; + AccessSpec parent_access; + CodeAttributes attributes; + ModuleFlag mflags; + CodeType* interfaces; + s32 num_interfaces; +}; +CodeClass def_class( StrC name, Opts_def_struct otps GEN_PARAM_DEFAULT ); -CodeConstructor def_constructor( CodeParam params = NullCode, Code initializer_list = NullCode, Code body = NullCode ); +struct Opts_def_constructor { + CodeParam params; + Code initializer_list; + Code body; +}; +CodeConstructor def_constructor( Opts_def_constructor opts GEN_PARAM_DEFAULT ); CodeDefine def_define( StrC name, StrC content ); -CodeDestructor def_destructor( Code body = NullCode, CodeSpecifiers specifiers = NullCode ); +struct Opts_def_destructor { + Code body; + CodeSpecifiers specifiers; +}; +CodeDestructor def_destructor( Opts_def_destructor opts GEN_PARAM_DEFAULT ); -CodeEnum def_enum( StrC name - , Code body = NullCode, CodeType type = NullCode - , EnumT specifier = EnumDecl_Regular, CodeAttributes attributes = NullCode - , ModuleFlag mflags = ModuleFlag_None ); +struct Opts_def_enum { + Code body; + CodeType type; + EnumT specifier; + CodeAttributes attributes; + ModuleFlag mflags; +}; +CodeEnum def_enum( StrC name, Opts_def_enum opts GEN_PARAM_DEFAULT ); CodeExec def_execution ( StrC content ); CodeExtern def_extern_link( StrC name, Code body ); CodeFriend def_friend ( Code symbol ); -CodeFn def_function( StrC name - , CodeParam params = NullCode, CodeType ret_type = NullCode, Code body = NullCode - , CodeSpecifiers specifiers = NullCode, CodeAttributes attributes = NullCode - , ModuleFlag mflags = ModuleFlag_None ); +struct Opts_def_function { + CodeParam params; + CodeType ret_type; + Code body; + CodeSpecifiers specs; + CodeAttributes attrs; + ModuleFlag mflags; +}; +CodeFn def_function( StrC name, Opts_def_function opts GEN_PARAM_DEFAULT ); -CodeInclude def_include ( StrC content, bool foreign = false ); -CodeModule def_module ( StrC name, ModuleFlag mflags = ModuleFlag_None ); -CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags = ModuleFlag_None ); +struct Opts_def_include { b32 foreign; }; +struct Opts_def_module { ModuleFlag mflags; }; +struct Opts_def_namespace { ModuleFlag mflags; }; +CodeInclude def_include ( StrC content, Opts_def_include opts GEN_PARAM_DEFAULT ); +CodeModule def_module ( StrC name, Opts_def_module opts GEN_PARAM_DEFAULT ); +CodeNS def_namespace( StrC name, Code body, Opts_def_namespace opts GEN_PARAM_DEFAULT ); -CodeOperator def_operator( OperatorT op, StrC nspace - , CodeParam params = NullCode, CodeType ret_type = NullCode, Code body = NullCode - , CodeSpecifiers specifiers = NullCode, CodeAttributes attributes = NullCode - , ModuleFlag mflags = ModuleFlag_None ); +struct Opts_def_operator { + CodeParam params; + CodeType ret_type; + Code body; + CodeSpecifiers specifiers; + CodeAttributes attributes; + ModuleFlag mflags; +}; +CodeOperator def_operator( OperatorT op, StrC nspace, Opts_def_operator opts GEN_PARAM_DEFAULT ); -CodeOpCast def_operator_cast( CodeType type, Code body = NullCode, CodeSpecifiers specs = NullCode ); +struct Opts_def_operator_cast { + Code body; + CodeSpecifiers specs; +}; +CodeOpCast def_operator_cast( CodeType type, Opts_def_operator_cast opts GEN_PARAM_DEFAULT ); -CodeParam def_param ( CodeType type, StrC name, Code value = NullCode ); +struct Opts_def_param { Code value; }; +CodeParam def_param ( CodeType type, StrC name, Opts_def_param opts GEN_PARAM_DEFAULT ); CodePragma def_pragma( StrC directive ); CodePreprocessCond def_preprocess_cond( EPreprocessCond type, StrC content ); CodeSpecifiers def_specifier( SpecifierT specifier ); -CodeStruct def_struct( StrC name - , Code body = NullCode - , CodeType parent = NullCode, AccessSpec access = AccessSpec_Default - , CodeAttributes attributes = NullCode - , ModuleFlag mflags = ModuleFlag_None - , CodeType* interfaces = nullptr, s32 num_interfaces = 0 ); +CodeStruct def_struct( StrC name, Opts_def_struct opts GEN_PARAM_DEFAULT ); -CodeTemplate def_template( CodeParam params, Code definition, ModuleFlag mflags = ModuleFlag_None ); +struct Opts_def_template { ModuleFlag mflags; }; +CodeTemplate def_template( CodeParam params, Code definition, Opts_def_template opts GEN_PARAM_DEFAULT ); -CodeType def_type ( StrC name, Code arrayexpr = NullCode, CodeSpecifiers specifiers = NullCode, CodeAttributes attributes = NullCode ); -CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes = NullCode, ModuleFlag mflags = ModuleFlag_None ); +struct Opts_def_type { + Code arrayexpr; + CodeSpecifiers specifiers; + CodeAttributes attributes; +}; +CodeType def_type( StrC name, Opts_def_type opts GEN_PARAM_DEFAULT ); -CodeUnion def_union( StrC name, Code body, CodeAttributes attributes = NullCode, ModuleFlag mflags = ModuleFlag_None ); +struct Opts_def_typedef { + CodeAttributes attributes; + ModuleFlag mflags; +}; +CodeTypedef def_typedef( StrC name, Code type, Opts_def_typedef opts GEN_PARAM_DEFAULT ); -CodeUsing def_using( StrC name, CodeType type = NullCode - , CodeAttributes attributess = NullCode - , ModuleFlag mflags = ModuleFlag_None ); +struct Opts_def_union { + CodeAttributes attributes; + ModuleFlag mflags; +}; +CodeUnion def_union( StrC name, Code body, Opts_def_union opts GEN_PARAM_DEFAULT ); + +struct Opts_def_using { + CodeAttributes attributes; + ModuleFlag mflags; +}; +CodeUsing def_using( StrC name, Code type, Opts_def_using opts GEN_PARAM_DEFAULT ); CodeUsing def_using_namespace( StrC name ); -CodeVar def_variable( CodeType type, StrC name, Code value = NullCode - , CodeSpecifiers specifiers = NullCode, CodeAttributes attributes = NullCode - , ModuleFlag mflags = ModuleFlag_None ); +struct Opts_def_variable +{ + Code value; + CodeSpecifiers specifiers; + CodeAttributes attributes; + ModuleFlag mflags; +}; +CodeVar def_variable( CodeType type, StrC name, Opts_def_variable opts GEN_PARAM_DEFAULT ); // Constructs an empty body. Use AST::validate_body() to check if the body is was has valid entries. CodeBody def_body( CodeT type ); diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index 55a08c4..6aa0646 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -495,11 +495,13 @@ CodeComment def_comment( StrC content ) return (CodeComment) result; } -CodeConstructor def_constructor( CodeParam params, Code initializer_list, Code body ) +CodeConstructor def_constructor( Opts_def_constructor p ) { - using namespace ECode; + CodeParam params = p.params; + Code initializer_list = p.initializer_list; + Code body = p.body; - if ( params && params->Type != Parameters ) + if ( params && params->Type != ECode::Parameters ) { log_failure("gen::def_constructor: params must be of Parameters type - %s", debug_str(params)); return InvalidCode; @@ -522,8 +524,8 @@ CodeConstructor def_constructor( CodeParam params, Code initializer_list, Code b { switch ( body->Type ) { - case Function_Body: - case Untyped: + case ECode::Function_Body: + case ECode::Untyped: break; default: @@ -531,35 +533,36 @@ CodeConstructor def_constructor( CodeParam params, Code initializer_list, Code b return InvalidCode; } - result->Type = Constructor; + result->Type = ECode::Constructor; result->Body = body; } else { - result->Type = Constructor_Fwd; + result->Type = ECode::Constructor_Fwd; } return result; } -CodeClass def_class( StrC name - , Code body - , CodeType parent, AccessSpec parent_access - , CodeAttributes attributes - , ModuleFlag mflags - , CodeType* interfaces, s32 num_interfaces ) +CodeClass def_class( StrC name, Opts_def_struct p ) { - using namespace ECode; - name_check( def_class, name ); + + Code body = p.body; + CodeType parent = p.parent; + AccessSpec parent_access = p.parent_access; + CodeAttributes attributes = p.attributes; + ModuleFlag mflags = p.mflags; + CodeType* interfaces = p.interfaces; + s32 num_interfaces = p.num_interfaces; - if ( attributes && attributes->Type != PlatformAttributes ) + if ( attributes && attributes->Type != ECode::PlatformAttributes ) { log_failure( "gen::def_class: attributes was not a 'PlatformAttributes' type: %s", debug_str(attributes) ); return InvalidCode; } - if ( parent && ( parent->Type != Class && parent->Type != Struct && parent->Type != Typename && parent->Type != Untyped ) ) + if ( parent && ( parent->Type != ECode::Class && parent->Type != ECode::Struct && parent->Type != ECode::Typename && parent->Type != ECode::Untyped ) ) { log_failure( "gen::def_class: parent provided is not type 'Class', 'Struct', 'Typeanme', or 'Untyped': %s", debug_str(parent) ); return InvalidCode; @@ -574,8 +577,8 @@ CodeClass def_class( StrC name { switch ( body->Type ) { - case Class_Body: - case Untyped: + case ECode::Class_Body: + case ECode::Untyped: break; default: @@ -583,13 +586,13 @@ CodeClass def_class( StrC name return InvalidCode; } - result->Type = Class; + result->Type = ECode::Class; result->Body = body; result->Body->Parent = result; // TODO(Ed): Review this? } else { - result->Type = Class_Fwd; + result->Type = ECode::Class_Fwd; } if ( attributes ) @@ -614,8 +617,6 @@ CodeClass def_class( StrC name CodeDefine def_define( StrC name, StrC content ) { - using namespace ECode; - name_check( def_define, name ); // Defines can be empty definitions @@ -629,7 +630,7 @@ CodeDefine def_define( StrC name, StrC content ) CodeDefine result = (CodeDefine) make_code(); - result->Type = Preprocess_Define; + result->Type = ECode::Preprocess_Define; result->Name = get_cached_string( name ); if ( content.Len <= 0 || content.Ptr == nullptr ) { @@ -641,16 +642,17 @@ CodeDefine def_define( StrC name, StrC content ) return result; } -CodeDestructor def_destructor( Code body, CodeSpecifiers specifiers ) +CodeDestructor def_destructor( Opts_def_destructor p ) { - using namespace ECode; - - if ( specifiers && specifiers->Type != Specifiers ) + Code body = p.body; + CodeSpecifiers specifiers = p.specifiers; + + if ( specifiers && specifiers->Type != ECode::Specifiers ) { log_failure( "gen::def_destructor: specifiers was not a 'Specifiers' type: %s", debug_str(specifiers) ); return InvalidCode; } - + CodeDestructor result = (CodeDestructor) make_code(); if ( specifiers ) @@ -660,8 +662,8 @@ CodeDestructor def_destructor( Code body, CodeSpecifiers specifiers ) { switch ( body->Type ) { - case Function_Body: - case Untyped: + case ECode::Function_Body: + case ECode::Untyped: break; default: @@ -669,33 +671,34 @@ CodeDestructor def_destructor( Code body, CodeSpecifiers specifiers ) return InvalidCode; } - result->Type = Destructor; + result->Type = ECode::Destructor; result->Body = body; } else { - result->Type = Destructor_Fwd; + result->Type = ECode::Destructor_Fwd; } return result; } -CodeEnum def_enum( StrC name - , Code body, CodeType type - , EnumT specifier, CodeAttributes attributes - , ModuleFlag mflags ) +CodeEnum def_enum( StrC name, Opts_def_enum p ) { - using namespace ECode; + Code body = p.body; + CodeType type = p.type; + EnumT specifier = p.specifier; + CodeAttributes attributes = p.attributes; + ModuleFlag mflags = p.mflags; name_check( def_enum, name ); - if ( type && type->Type != Typename ) + if ( type && type->Type != ECode::Typename ) { log_failure( "gen::def_enum: enum underlying type provided was not of type Typename: %s", debug_str(type) ); return InvalidCode; } - if ( attributes && attributes->Type != PlatformAttributes ) + if ( attributes && attributes->Type != ECode::PlatformAttributes ) { log_failure( "gen::def_enum: attributes was not a 'PlatformAttributes' type: %s", debug_str(attributes) ); return InvalidCode; @@ -710,8 +713,8 @@ CodeEnum def_enum( StrC name { switch ( body->Type ) { - case Enum_Body: - case Untyped: + case ECode::Enum_Body: + case ECode::Untyped: break; default: @@ -720,14 +723,14 @@ CodeEnum def_enum( StrC name } result->Type = specifier == EnumDecl_Class ? - Enum_Class : Enum; + ECode::Enum_Class : ECode::Enum; result->Body = body; } else { result->Type = specifier == EnumDecl_Class ? - Enum_Class_Fwd : Enum_Fwd; + ECode::Enum_Class_Fwd : ECode::Enum_Fwd; } if ( attributes ) @@ -737,7 +740,7 @@ CodeEnum def_enum( StrC name { result->UnderlyingType = type; } - else if ( result->Type != Enum_Class_Fwd && result->Type != Enum_Fwd ) + else if ( result->Type != ECode::Enum_Class_Fwd && result->Type != ECode::Enum_Fwd ) { log_failure( "gen::def_enum: enum forward declaration must have an underlying type" ); return InvalidCode; @@ -765,12 +768,10 @@ CodeExec def_execution( StrC content ) CodeExtern def_extern_link( StrC name, Code body ) { - using namespace ECode; - name_check( def_extern_linkage, name ); null_check( def_extern_linkage, body ); - if ( body->Type != Extern_Linkage_Body && body->Type != Untyped ) + if ( body->Type != ECode::Extern_Linkage_Body && body->Type != ECode::Untyped ) { log_failure("gen::def_extern_linkage: body is not of extern_linkage or untyped type %s", debug_str(body)); return InvalidCode; @@ -778,7 +779,7 @@ CodeExtern def_extern_link( StrC name, Code body ) CodeExtern result = (CodeExtern)make_code(); - result->Type = Extern_Linkage; + result->Type = ECode::Extern_Linkage; result->Name = get_cached_string( name ); result->Body = body; @@ -787,20 +788,18 @@ CodeExtern def_extern_link( StrC name, Code body ) CodeFriend def_friend( Code declaration ) { - using namespace ECode; - null_check( def_friend, declaration ); switch ( declaration->Type ) { - case Class_Fwd: - case Function_Fwd: - case Operator_Fwd: - case Struct_Fwd: - case Class: - case Function: - case Operator: - case Struct: + case ECode::Class_Fwd: + case ECode::Function_Fwd: + case ECode::Operator_Fwd: + case ECode::Struct_Fwd: + case ECode::Class: + case ECode::Function: + case ECode::Operator: + case ECode::Struct: break; default: @@ -810,41 +809,43 @@ CodeFriend def_friend( Code declaration ) CodeFriend result = (CodeFriend) make_code(); - result->Type = Friend; + result->Type = ECode::Friend; result->Declaration = declaration; return result; } -CodeFn def_function( StrC name - , CodeParam params , CodeType ret_type, Code body - , CodeSpecifiers specifiers, CodeAttributes attributes - , ModuleFlag mflags ) +CodeFn def_function( StrC name, Opts_def_function p ) { - using namespace ECode; + CodeParam params = p.params; + CodeType ret_type = p.ret_type; + Code body = p.body; + CodeSpecifiers specifiers = p.specs; + CodeAttributes attributes = p.attrs; + ModuleFlag mflags = p.mflags; name_check( def_function, name ); - if ( params && params->Type != Parameters ) + if ( params && params->Type != ECode::Parameters ) { log_failure( "gen::def_function: params was not a `Parameters` type: %s", debug_str(params) ); return InvalidCode; } - if ( ret_type && ret_type->Type != Typename ) + if ( ret_type && ret_type->Type != ECode::Typename ) { log_failure( "gen::def_function: ret_type was not a Typename: %s", debug_str(ret_type) ); return InvalidCode; } - if ( specifiers && specifiers->Type != Specifiers ) + if ( specifiers && specifiers->Type != ECode::Specifiers ) { log_failure( "gen::def_function: specifiers was not a `Specifiers` type: %s", debug_str(specifiers) ); return InvalidCode; } - if ( attributes && attributes->Type != PlatformAttributes ) + if ( attributes && attributes->Type != ECode::PlatformAttributes ) { log_failure( "gen::def_function: attributes was not a `PlatformAttributes` type: %s", debug_str(attributes) ); return InvalidCode; @@ -859,9 +860,9 @@ CodeFn def_function( StrC name { switch ( body->Type ) { - case Function_Body: - case Execution: - case Untyped: + case ECode::Function_Body: + case ECode::Execution: + case ECode::Untyped: break; default: @@ -871,12 +872,12 @@ CodeFn def_function( StrC name } } - result->Type = Function; + result->Type = ECode::Function; result->Body = body; } else { - result->Type = Function_Fwd; + result->Type = ECode::Function_Fwd; } if ( attributes ) @@ -900,7 +901,7 @@ CodeFn def_function( StrC name return result; } -CodeInclude def_include( StrC path, bool foreign ) +CodeInclude def_include( StrC path, Opts_def_include p ) { if ( path.Len <= 0 || path.Ptr == nullptr ) { @@ -908,7 +909,7 @@ CodeInclude def_include( StrC path, bool foreign ) return InvalidCode; } - StrC content = foreign ? + StrC content = p.foreign ? to_str( str_fmt_buf( "<%.*s>", path.Len, path.Ptr )) : to_str( str_fmt_buf( "\"%.*s\"", path.Len, path.Ptr )); @@ -921,7 +922,7 @@ CodeInclude def_include( StrC path, bool foreign ) return (CodeInclude) result; } -CodeModule def_module( StrC name, ModuleFlag mflags ) +CodeModule def_module( StrC name, Opts_def_module p ) { name_check( def_module, name ); @@ -930,19 +931,17 @@ CodeModule def_module( StrC name, ModuleFlag mflags ) result->Type = ECode::Module; result->Name = get_cached_string( name ); result->Content = result->Name; - result->ModuleFlags = mflags; + result->ModuleFlags = p.mflags; return (CodeModule) result; } -CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags ) +CodeNS def_namespace( StrC name, Code body, Opts_def_namespace p ) { - using namespace ECode; - name_check( def_namespace, name ); - null_check( def_namespace, body ); - - if ( body->Type != Namespace_Body && body->Type != Untyped ) + null_check( def_namespace, body); + + if ( body && body->Type != ECode::Namespace_Body && body->Type != ECode::Untyped ) { log_failure("gen::def_namespace: body is not of namespace or untyped type %s", debug_str(body)); return InvalidCode; @@ -950,28 +949,29 @@ CodeNS def_namespace( StrC name, Code body, ModuleFlag mflags ) CodeNS result = (CodeNS) make_code(); - result->Type = Namespace; + result->Type = ECode::Namespace; result->Name = get_cached_string( name ); - result->ModuleFlags = mflags; + result->ModuleFlags = p.mflags; result->Body = body; - return result; } -CodeOperator def_operator( OperatorT op, StrC nspace - , CodeParam params_code, CodeType ret_type, Code body - , CodeSpecifiers specifiers, CodeAttributes attributes - , ModuleFlag mflags ) +CodeOperator def_operator( OperatorT op, StrC nspace, Opts_def_operator p ) { - using namespace ECode; + CodeParam params_code = p.params; + CodeType ret_type = p.ret_type; + Code body = p.body; + CodeSpecifiers specifiers = p.specifiers; + CodeAttributes attributes = p.attributes; + ModuleFlag mflags = p.mflags; - if ( attributes && attributes->Type != PlatformAttributes ) + if ( attributes && attributes->Type != ECode::PlatformAttributes ) { log_failure( "gen::def_operator: PlatformAttributes was provided but its not of attributes type: %s", debug_str(attributes) ); return InvalidCode; } - if ( specifiers && specifiers->Type != Specifiers ) + if ( specifiers && specifiers->Type != ECode::Specifiers ) { log_failure( "gen::def_operator: Specifiers was provided but its not of specifiers type: %s", debug_str(specifiers) ); return InvalidCode; @@ -1001,9 +1001,9 @@ CodeOperator def_operator( OperatorT op, StrC nspace { switch ( body->Type ) { - case Function_Body: - case Execution: - case Untyped: + case ECode::Function_Body: + case ECode::Execution: + case ECode::Untyped: break; default: @@ -1014,14 +1014,14 @@ CodeOperator def_operator( OperatorT op, StrC nspace } result->Type = check_result == OpValidateResult::Global ? - Operator : Operator_Member; + ECode::Operator : ECode::Operator_Member; result->Body = body; } else { result->Type = check_result == OpValidateResult::Global ? - Operator_Fwd : Operator_Member_Fwd; + ECode::Operator_Fwd : ECode::Operator_Member_Fwd; } if ( attributes ) @@ -1038,12 +1038,14 @@ CodeOperator def_operator( OperatorT op, StrC nspace return result; } -CodeOpCast def_operator_cast( CodeType type, Code body, CodeSpecifiers const_spec ) +CodeOpCast def_operator_cast( CodeType type, Opts_def_operator_cast p ) { - using namespace ECode; + Code body = p.body; + CodeSpecifiers const_spec = p.specs; + null_check( def_operator_cast, type ); - if ( type->Type != Typename ) + if ( type->Type != ECode::Typename ) { log_failure( "gen::def_operator_cast: type is not a typename - %s", debug_str(type) ); return InvalidCode; @@ -1053,9 +1055,9 @@ CodeOpCast def_operator_cast( CodeType type, Code body, CodeSpecifiers const_spe if (body) { - result->Type = Operator_Cast; + result->Type = ECode::Operator_Cast; - if ( body->Type != Function_Body && body->Type != Execution ) + if ( body->Type != ECode::Function_Body && body->Type != ECode::Execution ) { log_failure( "gen::def_operator_cast: body is not of function body or execution type - %s", debug_str(body) ); return InvalidCode; @@ -1065,7 +1067,7 @@ CodeOpCast def_operator_cast( CodeType type, Code body, CodeSpecifiers const_spe } else { - result->Type = Operator_Cast_Fwd; + result->Type = ECode::Operator_Cast_Fwd; } if ( const_spec ) @@ -1077,34 +1079,32 @@ CodeOpCast def_operator_cast( CodeType type, Code body, CodeSpecifiers const_spe return result; } -CodeParam def_param( CodeType type, StrC name, Code value ) +CodeParam def_param( CodeType type, StrC name, Opts_def_param p ) { - using namespace ECode; - name_check( def_param, name ); null_check( def_param, type ); - if ( type->Type != Typename ) + if ( type->Type != ECode::Typename ) { log_failure( "gen::def_param: type is not a typename - %s", debug_str(type) ); return InvalidCode; } - if ( value && value->Type != Untyped ) + if ( p.value && p.value->Type != ECode::Untyped ) { - log_failure( "gen::def_param: value is not untyped - %s", debug_str(value) ); + log_failure( "gen::def_param: value is not untyped - %s", debug_str(p.value) ); return InvalidCode; } CodeParam result = (CodeParam) make_code(); - result->Type = Parameters; + result->Type = ECode::Parameters; result->Name = get_cached_string( name ); result->ValueType = type; - if ( value ) - result->Value = value; + if ( p.value ) + result->Value = p.value; result->NumEntries++; @@ -1113,8 +1113,6 @@ CodeParam def_param( CodeType type, StrC name, Code value ) CodePragma def_pragma( StrC directive ) { - using namespace ECode; - if ( directive.Len <= 0 || directive.Ptr == nullptr ) { log_failure( "gen::def_comment: Invalid comment provided:" ); @@ -1123,7 +1121,7 @@ CodePragma def_pragma( StrC directive ) CodePragma result = (CodePragma) make_code(); - result->Type = Preprocess_Pragma; + result->Type = ECode::Preprocess_Pragma; result->Content = get_cached_string( directive ); return result; @@ -1131,8 +1129,6 @@ CodePragma def_pragma( StrC directive ) CodePreprocessCond def_preprocess_cond( EPreprocessCond type, StrC expr ) { - using namespace ECode; - if ( expr.Len <= 0 || expr.Ptr == nullptr ) { log_failure( "gen::def_comment: Invalid comment provided:" ); @@ -1146,16 +1142,16 @@ CodePreprocessCond def_preprocess_cond( EPreprocessCond type, StrC expr ) switch (type) { case PreprocessCond_If: - result->Type = Preprocess_If; + result->Type = ECode::Preprocess_If; break; case PreprocessCond_IfDef: - result->Type = Preprocess_IfDef; + result->Type = ECode::Preprocess_IfDef; break; case PreprocessCond_IfNotDef: - result->Type = Preprocess_IfNotDef; + result->Type = ECode::Preprocess_IfNotDef; break; case PreprocessCond_ElIf: - result->Type = Preprocess_ElIf; + result->Type = ECode::Preprocess_ElIf; break; } @@ -1172,28 +1168,29 @@ CodeSpecifiers def_specifier( SpecifierT spec ) return result; } -CodeStruct def_struct( StrC name - , Code body - , CodeType parent, AccessSpec parent_access - , CodeAttributes attributes - , ModuleFlag mflags - , CodeType* interfaces, s32 num_interfaces ) +CodeStruct def_struct( StrC name, Opts_def_struct p ) { - using namespace ECode; + Code body = p.body; + CodeType parent = p.parent; + AccessSpec parent_access = p.parent_access; + CodeAttributes attributes = p.attributes; + ModuleFlag mflags = p.mflags; + CodeType* interfaces = p.interfaces; + s32 num_interfaces = p.num_interfaces; - if ( attributes && attributes->Type != PlatformAttributes ) + if ( attributes && attributes->Type != ECode::PlatformAttributes ) { log_failure( "gen::def_struct: attributes was not a `PlatformAttributes` type - %s", debug_str(attributes) ); return InvalidCode; } - if ( parent && parent->Type != Typename ) + if ( parent && parent->Type != ECode::Typename ) { log_failure( "gen::def_struct: parent was not a `Struct` type - %s", debug_str(parent) ); return InvalidCode; } - if ( body && body->Type != Struct_Body ) + if ( body && body->Type != ECode::Struct_Body ) { log_failure( "gen::def_struct: body was not a Struct_Body type - %s", debug_str(body) ); return InvalidCode; @@ -1208,12 +1205,12 @@ CodeStruct def_struct( StrC name if ( body ) { - result->Type = Struct; + result->Type = ECode::Struct; result->Body = body; } else { - result->Type = Struct_Fwd; + result->Type = ECode::Struct_Fwd; } if ( attributes ) @@ -1236,7 +1233,7 @@ CodeStruct def_struct( StrC name return result; } -CodeTemplate def_template( CodeParam params, Code declaration, ModuleFlag mflags ) +CodeTemplate def_template( CodeParam params, Code declaration, Opts_def_template p ) { null_check( def_template, declaration ); @@ -1262,16 +1259,19 @@ CodeTemplate def_template( CodeParam params, Code declaration, ModuleFlag mflags CodeTemplate result = (CodeTemplate) make_code(); result->Type = ECode::Template; - result->ModuleFlags = mflags; + result->ModuleFlags = p.mflags; result->Params = params; result->Declaration = declaration; - return result; } -CodeType def_type( StrC name, Code arrayexpr, CodeSpecifiers specifiers, CodeAttributes attributes ) +CodeType def_type( StrC name, Opts_def_type p ) { name_check( def_type, name ); + + Code arrayexpr = p.arrayexpr; + CodeSpecifiers specifiers = p.specifiers; + CodeAttributes attributes = p.attributes; if ( attributes && attributes->Type != ECode::PlatformAttributes ) { @@ -1308,34 +1308,32 @@ CodeType def_type( StrC name, Code arrayexpr, CodeSpecifiers specifiers, CodeAtt return result; } -CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes, ModuleFlag mflags ) +CodeTypedef def_typedef( StrC name, Code type, Opts_def_typedef p ) { - using namespace ECode; - null_check( def_typedef, type ); switch ( type->Type ) { - case Class: - case Class_Fwd: - case Enum: - case Enum_Fwd: - case Enum_Class: - case Enum_Class_Fwd: - case Function_Fwd: - case Struct: - case Struct_Fwd: - case Union: - case Typename: + case ECode::Class: + case ECode::Class_Fwd: + case ECode::Enum: + case ECode::Enum_Fwd: + case ECode::Enum_Class: + case ECode::Enum_Class_Fwd: + case ECode::Function_Fwd: + case ECode::Struct: + case ECode::Struct_Fwd: + case ECode::Union: + case ECode::Typename: break; default: log_failure( "gen::def_typedef: type was not a Class, Enum, Function Forward, Struct, Typename, or Union - %s", debug_str(type) ); return InvalidCode; } - if ( attributes && attributes->Type != ECode::PlatformAttributes ) + if ( p.attributes && p.attributes->Type != ECode::PlatformAttributes ) { - log_failure( "gen::def_typedef: attributes was not a PlatformAttributes - %s", debug_str(attributes) ); + log_failure( "gen::def_typedef: attributes was not a PlatformAttributes - %s", debug_str(p.attributes) ); return InvalidCode; } @@ -1351,13 +1349,13 @@ CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes, Module CodeTypedef result = (CodeTypedef) make_code(); result->Type = ECode::Typedef; - result->ModuleFlags = mflags; + result->ModuleFlags = p.mflags; result->UnderlyingType = type; if ( name.Len <= 0 ) { - if (type->Type != Untyped) + if (type->Type != ECode::Untyped) { log_failure( "gen::def_typedef: name was empty and type was not untyped (indicating its a function typedef) - %s", debug_str(type) ); return InvalidCode; @@ -1375,7 +1373,7 @@ CodeTypedef def_typedef( StrC name, Code type, CodeAttributes attributes, Module return result; } -CodeUnion def_union( StrC name, Code body, CodeAttributes attributes, ModuleFlag mflags ) +CodeUnion def_union( StrC name, Code body, Opts_def_union p ) { null_check( def_union, body ); @@ -1385,15 +1383,15 @@ CodeUnion def_union( StrC name, Code body, CodeAttributes attributes, ModuleFlag return InvalidCode; } - if ( attributes && attributes->Type != ECode::PlatformAttributes ) + if ( p.attributes && p.attributes->Type != ECode::PlatformAttributes ) { - log_failure( "gen::def_union: attributes was not a PlatformAttributes type - %s", debug_str(attributes) ); + log_failure( "gen::def_union: attributes was not a PlatformAttributes type - %s", debug_str(p.attributes) ); return InvalidCode; } CodeUnion result = (CodeUnion) make_code(); - result->ModuleFlags = mflags; + result->ModuleFlags = p.mflags; result->Type = ECode::Union; if ( name.Ptr ) @@ -1401,15 +1399,13 @@ CodeUnion def_union( StrC name, Code body, CodeAttributes attributes, ModuleFlag result->Body = body; - if ( attributes ) - result->Attributes = attributes; + if ( p.attributes ) + result->Attributes = p.attributes; return result; } -CodeUsing def_using( StrC name, CodeType type - , CodeAttributes attributes - , ModuleFlag mflags ) +CodeUsing def_using( StrC name, Code type, Opts_def_using p ) { name_check( def_using, name ); null_check( def_using, type ); @@ -1422,22 +1418,22 @@ CodeUsing def_using( StrC name, CodeType type return InvalidCode; } - if ( attributes && attributes->Type != ECode::PlatformAttributes ) + if ( p.attributes && p.attributes->Type != ECode::PlatformAttributes ) { - log_failure( "gen::def_using: attributes was not a PlatformAttributes type - %s", debug_str(attributes) ); + log_failure( "gen::def_using: attributes was not a PlatformAttributes type - %s", debug_str(p.attributes) ); return InvalidCode; } CodeUsing result = (CodeUsing) make_code(); result->Name = get_cached_string( name ); - result->ModuleFlags = mflags; - result->Type = ECode::Using; + result->ModuleFlags = p.mflags; + result->Type = ECode::Using; result->UnderlyingType = type; - if ( attributes ) - result->Attributes = attributes; + if ( p.attributes ) + result->Attributes = p.attributes; return result; } @@ -1455,22 +1451,20 @@ CodeUsing def_using_namespace( StrC name ) return (CodeUsing) result; } -CodeVar def_variable( CodeType type, StrC name, Code value - , CodeSpecifiers specifiers, CodeAttributes attributes - , ModuleFlag mflags ) +CodeVar def_variable( CodeType type, StrC name, Code value, Opts_def_variable p ) { name_check( def_variable, name ); null_check( def_variable, type ); - if ( attributes && attributes->Type != ECode::PlatformAttributes ) + if ( p.attributes && p.attributes->Type != ECode::PlatformAttributes ) { - log_failure( "gen::def_variable: attributes was not a `PlatformAttributes` type - %s", debug_str(attributes) ); + log_failure( "gen::def_variable: attributes was not a `PlatformAttributes` type - %s", debug_str(p.attributes) ); return InvalidCode; } - if ( specifiers && specifiers->Type != ECode::Specifiers ) + if ( p.specifiers && p.specifiers->Type != ECode::Specifiers ) { - log_failure( "gen::def_variable: specifiers was not a `Specifiers` type - %s", debug_str(specifiers) ); + log_failure( "gen::def_variable: specifiers was not a `Specifiers` type - %s", debug_str(p.specifiers) ); return InvalidCode; } @@ -1490,15 +1484,15 @@ CodeVar def_variable( CodeType type, StrC name, Code value result = (CodeVar) make_code(); result->Name = get_cached_string( name ); result->Type = ECode::Variable; - result->ModuleFlags = mflags; + result->ModuleFlags = p.mflags; result->ValueType = type; - if ( attributes ) - result->Attributes = attributes; + if ( p.attributes ) + result->Attributes = p.attributes; - if ( specifiers ) - result->Specs = specifiers; + if ( p.specifiers ) + result->Specs = p.specifiers; if ( value ) result->Value = value; diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 66a0de5..7192643 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -716,7 +716,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) // local_persist - char interface_arr_mem[ kilobytes(4) ] {0}; + char interface_arr_mem[ kilobytes(4) ] {0}; Array interfaces; { Arena arena = arena_init_from_memory( interface_arr_mem, kilobytes(4) ); interfaces = array_init_reserve( allocator_info(& arena), 4 ); @@ -770,14 +770,14 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) if ( currtok_noskip.Type == TokType::Comment && currtok_noskip.Line == stmt_end.Line ) inline_cmt = parse_comment(); - // : , ... { }; + // : , ... { }; } if ( which == TokType::Decl_Class ) - result = def_class( to_str(name), body, parent, access, attributes, mflags ); + result = def_class( to_str(name), { body, parent, access, attributes, mflags } ); else - result = def_struct( to_str(name), body, (CodeType)parent, access, attributes, mflags ); + result = def_struct( to_str(name), { body, (CodeType)parent, access, attributes, mflags } ); if ( inline_cmt ) result->InlineCmt = inline_cmt; @@ -2477,7 +2477,7 @@ CodeOperator parse_operator_after_ret_type( } // OpValidateResult check_result = operator__validate( op, params, ret_type, specifiers ); - CodeOperator result = def_operator( op, to_str(nspace), params, ret_type, body, specifiers, attributes, mflags ); + CodeOperator result = def_operator( op, to_str(nspace), { params, ret_type, body, specifiers, attributes, mflags } ); if ( inline_cmt ) result->InlineCmt = inline_cmt; diff --git a/project/dependencies/macros.hpp b/project/dependencies/macros.hpp index 91a1dd4..f9da4d1 100644 --- a/project/dependencies/macros.hpp +++ b/project/dependencies/macros.hpp @@ -234,4 +234,10 @@ # endif #endif +#if ! defined(GEN_PARAM_DEFAULT) && ! GEN_COMPILER_C +# define GEN_PARAM_DEFAULT = {} +#else +# define GEN_PARAM_DEFAULT +#endif + #pragma endregion Macros From 63ebd0d0946a777f5450242e0cde72d18e62a3fd Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 3 Dec 2024 01:44:01 -0500 Subject: [PATCH 047/112] removed reference type usage in components/lexer.cpp, looking into resolving 'using namespace' usage --- project/components/interface.parsing.cpp | 11 +- project/components/lexer.cpp | 641 ++++++++++++----------- project/components/parser.cpp | 6 +- 3 files changed, 329 insertions(+), 329 deletions(-) diff --git a/project/components/interface.parsing.cpp b/project/components/interface.parsing.cpp index 016645d..4d18903 100644 --- a/project/components/interface.parsing.cpp +++ b/project/components/interface.parsing.cpp @@ -11,16 +11,15 @@ CodeClass parse_class( StrC def ) { check_parse_args( def ); - using namespace parser; - - TokArray toks = lex( def ); + + parser::TokArray toks = parser::lex( def ); if ( toks.Arr == nullptr ) return InvalidCode; - Context.Tokens = toks; + parser::Context.Tokens = toks; push_scope(); - CodeClass result = (CodeClass) parse_class_struct( TokType::Decl_Class ); - Context.pop(); + CodeClass result = (CodeClass) parser::parse_class_struct( parser::TokType::Decl_Class ); + parser::Context.pop(); return result; } diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index 0679b42..5d82209 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -158,46 +158,46 @@ global Arena_256KB defines_map_arena; global HashTable(StrC) defines; global Array(Token) Tokens; -#define current ( * scanner ) +#define current ( * ctx->scanner ) #define move_forward() \ { \ if ( current == '\n' ) \ { \ - line++; \ - column = 1; \ + ctx->line++; \ + ctx->column = 1; \ } \ else \ { \ - column++; \ + ctx->column++; \ } \ - left--; \ - scanner++; \ + ctx->left--; \ + ctx->scanner++; \ } -#define SkipWhitespace() \ - while ( left && char_is_space( current ) ) \ - { \ - move_forward(); \ +#define SkipWhitespace() \ + while ( ctx->left && char_is_space( current ) ) \ + { \ + move_forward(); \ } -#define end_line() \ - do \ - { \ - while ( left && current == ' ' ) \ - { \ - move_forward(); \ - } \ - if ( left && current == '\r' ) \ - { \ - move_forward(); \ - move_forward(); \ - } \ - else if ( left && current == '\n' ) \ - { \ - move_forward(); \ - } \ - } \ +#define end_line() \ + do \ + { \ + while ( ctx->left && current == ' ' ) \ + { \ + move_forward(); \ + } \ + if ( ctx->left && current == '\r' ) \ + { \ + move_forward(); \ + move_forward(); \ + } \ + else if ( ctx->left && current == '\n' ) \ + { \ + move_forward(); \ + } \ + } \ while (0) enum @@ -206,40 +206,44 @@ enum Lex_ReturnNull, }; -forceinline -s32 lex_preprocessor_directive( - StrC& content - , s32& left - , char const*& scanner - , s32& line - , s32& column - , HashTable& defines - , Token& token ) +struct LexContext { - char const* hash = scanner; - append( & Tokens, { hash, 1, TokType::Preprocess_Hash, line, column, TF_Preprocess } ); + StrC content; + s32 left; + char const* scanner; + s32 line; + s32 column; + HashTable(StrC) defines; + Token token; +}; + +forceinline +s32 lex_preprocessor_directive( LexContext* ctx ) +{ + char const* hash = ctx->scanner; + append( & Tokens, { hash, 1, TokType::Preprocess_Hash, ctx->line, ctx->column, TF_Preprocess } ); move_forward(); SkipWhitespace(); - token.Text = scanner; - while (left && ! char_is_space(current) ) + ctx->token.Text = ctx->scanner; + while (ctx->left && ! char_is_space(current) ) { move_forward(); - token.Length++; + ctx->token.Length++; } - token.Type = ETokType::to_type( to_str(token) ); + ctx->token.Type = ETokType::to_type( to_str(ctx->token) ); - bool is_preprocessor = token.Type >= TokType::Preprocess_Define && token.Type <= TokType::Preprocess_Pragma; + bool is_preprocessor = ctx->token.Type >= TokType::Preprocess_Define && ctx->token.Type <= TokType::Preprocess_Pragma; if ( ! is_preprocessor ) { - token.Type = TokType::Preprocess_Unsupported; + ctx->token.Type = TokType::Preprocess_Unsupported; // Its an unsupported directive, skip it s32 within_string = false; s32 within_char = false; - while ( left ) + while ( ctx->left ) { if ( current == '"' && ! within_char ) within_string ^= true; @@ -250,26 +254,26 @@ s32 lex_preprocessor_directive( if ( current == '\\' && ! within_string && ! within_char ) { move_forward(); - token.Length++; + ctx->token.Length++; if ( current == '\r' ) { move_forward(); - token.Length++; + ctx->token.Length++; } if ( current == '\n' ) { move_forward(); - token.Length++; + ctx->token.Length++; continue; } else { log_failure( "gen::Parser::lex: Invalid escape sequence '\\%c' (%d, %d)" " in preprocessor directive (%d, %d)\n%.100s" - , current, line, column - , token.Line, token.Column, token.Text ); + , current, ctx->line, ctx->column + , ctx->token.Line, ctx->token.Column, ctx->token.Text ); break; } } @@ -277,57 +281,57 @@ s32 lex_preprocessor_directive( if ( current == '\r' ) { move_forward(); - token.Length++; + ctx->token.Length++; } if ( current == '\n' ) { move_forward(); - token.Length++; + ctx->token.Length++; break; } move_forward(); - token.Length++; + ctx->token.Length++; } - token.Length = token.Length + token.Text - hash; - token.Text = hash; - append( & Tokens, token ); + ctx->token.Length = ctx->token.Length + ctx->token.Text - hash; + ctx->token.Text = hash; + append( & Tokens, ctx->token ); return Lex_Continue; // Skip found token, its all handled here. } - if ( token.Type == TokType::Preprocess_Else || token.Type == TokType::Preprocess_EndIf ) + if ( ctx->token.Type == TokType::Preprocess_Else || ctx->token.Type == TokType::Preprocess_EndIf ) { - token.Flags |= TF_Preprocess_Cond; - append( & Tokens, token ); + ctx->token.Flags |= TF_Preprocess_Cond; + append( & Tokens, ctx->token ); end_line(); return Lex_Continue; } - else if ( token.Type >= TokType::Preprocess_If && token.Type <= TokType::Preprocess_ElIf ) + else if ( ctx->token.Type >= TokType::Preprocess_If && ctx->token.Type <= TokType::Preprocess_ElIf ) { - token.Flags |= TF_Preprocess_Cond; + ctx->token.Flags |= TF_Preprocess_Cond; } - append( & Tokens, token ); + append( & Tokens, ctx->token ); SkipWhitespace(); - if ( token.Type == TokType::Preprocess_Define ) + if ( ctx->token.Type == TokType::Preprocess_Define ) { - Token name = { scanner, 0, TokType::Identifier, line, column, TF_Preprocess }; + Token name = { ctx->scanner, 0, TokType::Identifier, ctx->line, ctx->column, TF_Preprocess }; - name.Text = scanner; + name.Text = ctx->scanner; name.Length = 1; move_forward(); - while ( left && ( char_is_alphanumeric(current) || current == '_' ) ) + while ( ctx->left && ( char_is_alphanumeric(current) || current == '_' ) ) { move_forward(); name.Length++; } - if ( left && current == '(' ) + if ( ctx->left && current == '(' ) { move_forward(); name.Length++; @@ -336,18 +340,18 @@ s32 lex_preprocessor_directive( append( & Tokens, name ); u64 key = crc32( name.Text, name.Length ); - set(& defines, key, to_str(name) ); + set(& ctx->defines, key, to_str(name) ); } - Token preprocess_content = { scanner, 0, TokType::Preprocess_Content, line, column, TF_Preprocess }; + Token preprocess_content = { ctx->scanner, 0, TokType::Preprocess_Content, ctx->line, ctx->column, TF_Preprocess }; - if ( token.Type == TokType::Preprocess_Include ) + if ( ctx->token.Type == TokType::Preprocess_Include ) { preprocess_content.Type = TokType::String; if ( current != '"' && current != '<' ) { - String directive_str = string_fmt_buf( GlobalAllocator, "%.*s", min( 80, left + preprocess_content.Length ), token.Text ); + String directive_str = string_fmt_buf( GlobalAllocator, "%.*s", min( 80, ctx->left + preprocess_content.Length ), ctx->token.Text ); log_failure( "gen::Parser::lex: Expected '\"' or '<' after #include, not '%c' (%d, %d)\n%s" , current @@ -360,7 +364,7 @@ s32 lex_preprocessor_directive( move_forward(); preprocess_content.Length++; - while ( left && current != '"' && current != '>' ) + while ( ctx->left && current != '"' && current != '>' ) { move_forward(); preprocess_content.Length++; @@ -369,7 +373,7 @@ s32 lex_preprocessor_directive( move_forward(); preprocess_content.Length++; - if ( current == '\r' && scanner[1] == '\n' ) + if ( current == '\r' && ctx->scanner[1] == '\n' ) { move_forward(); move_forward(); @@ -387,7 +391,7 @@ s32 lex_preprocessor_directive( s32 within_char = false; // SkipWhitespace(); - while ( left ) + while ( ctx->left ) { if ( current == '"' && ! within_char ) within_string ^= true; @@ -414,12 +418,12 @@ s32 lex_preprocessor_directive( } else { - String directive_str = string_make_length( GlobalAllocator, token.Text, token.Length ); - String content_str = string_fmt_buf( GlobalAllocator, "%.*s", min( 400, left + preprocess_content.Length ), preprocess_content.Text ); + String directive_str = string_make_length( GlobalAllocator, ctx->token.Text, ctx->token.Length ); + String content_str = string_fmt_buf( GlobalAllocator, "%.*s", min( 400, ctx->left + preprocess_content.Length ), preprocess_content.Text ); log_failure( "gen::Parser::lex: Invalid escape sequence '\\%c' (%d, %d)" " in preprocessor directive '%s' (%d, %d)\n%s" - , current, line, column + , current, ctx->line, ctx->column , directive_str, preprocess_content.Line, preprocess_content.Column , content_str ); break; @@ -446,30 +450,24 @@ s32 lex_preprocessor_directive( } forceinline -void lex_found_token( StrC& content - , s32& left - , char const*& scanner - , s32& line - , s32& column - , HashTable& defines - , Token& token ) +void lex_found_token( LexContext* ctx ) { - if ( token.Type != TokType::Invalid ) + if ( ctx->token.Type != TokType::Invalid ) { - append( & Tokens, token ); + append( & Tokens, ctx->token ); return; } - TokType type = ETokType::to_type( to_str(token) ); + TokType type = ETokType::to_type( to_str(ctx->token) ); if (type <= TokType::Access_Public && type >= TokType::Access_Private ) { - token.Flags |= TF_AccessSpecifier; + ctx->token.Flags |= TF_AccessSpecifier; } if ( type > TokType::__Attributes_Start ) { - token.Flags |= TF_Attribute; + ctx->token.Flags |= TF_Attribute; } if ( type == ETokType::Decl_Extern_Linkage ) @@ -479,11 +477,11 @@ void lex_found_token( StrC& content if ( current != '"' ) { type = ETokType::Spec_Extern; - token.Flags |= TF_Specifier; + ctx->token.Flags |= TF_Specifier; } - token.Type = type; - append( & Tokens, token ); + ctx->token.Type = type; + append( & Tokens, ctx->token ); return; } @@ -491,39 +489,39 @@ void lex_found_token( StrC& content || type == TokType::Ampersand || type == TokType::Ampersand_DBL ) { - token.Type = type; - token.Flags |= TF_Specifier; - append( & Tokens, token ); + ctx->token.Type = type; + ctx->token.Flags |= TF_Specifier; + append( & Tokens, ctx->token ); return; } if ( type != TokType::Invalid ) { - token.Type = type; - append( & Tokens, token ); + ctx->token.Type = type; + append( & Tokens, ctx->token ); return; } u64 key = 0; if ( current == '(') - key = crc32( token.Text, token.Length + 1 ); + key = crc32( ctx->token.Text, ctx->token.Length + 1 ); else - key = crc32( token.Text, token.Length ); + key = crc32( ctx->token.Text, ctx->token.Length ); - StrC* define = get(defines, key ); + StrC* define = get(ctx->defines, key ); if ( define ) { - token.Type = TokType::Preprocess_Macro; + ctx->token.Type = TokType::Preprocess_Macro; // Want to ignore any arguments the define may have as they can be execution expressions. - if ( left && current == '(' ) + if ( ctx->left && current == '(' ) { move_forward(); - token.Length++; + ctx->token.Length++; s32 level = 0; - while ( left && (current != ')' || level > 0) ) + while ( ctx->left && (current != ')' || level > 0) ) { if ( current == '(' ) level++; @@ -532,14 +530,14 @@ void lex_found_token( StrC& content level--; move_forward(); - token.Length++; + ctx->token.Length++; } move_forward(); - token.Length++; + ctx->token.Length++; } - if ( current == '\r' && scanner[1] == '\n' ) + if ( current == '\r' && ctx->scanner[1] == '\n' ) { move_forward(); } @@ -550,10 +548,10 @@ void lex_found_token( StrC& content } else { - token.Type = TokType::Identifier; + ctx->token.Type = TokType::Identifier; } - append( & Tokens, token ); + append( & Tokens, ctx->token ); } @@ -561,17 +559,20 @@ neverinline // TokArray lex( Array tokens, StrC content ) TokArray lex( StrC content ) { - s32 left = content.Len; - char const* scanner = content.Ptr; + LexContext c; LexContext* ctx = & c; + c.content = content; + c.left = content.Len; + c.scanner = content.Ptr; + c.defines = defines; - char const* word = scanner; + char const* word = c.scanner; s32 word_length = 0; - s32 line = 1; - s32 column = 1; + c.line = 1; + c.column = 1; SkipWhitespace(); - if ( left <= 0 ) + if ( c.left <= 0 ) { log_failure( "gen::lex: no tokens found (only whitespace provided)" ); return { {}, 0 }; @@ -583,21 +584,21 @@ TokArray lex( StrC content ) char const* scanner = entry.Data; while ( GEN_NS length(entry) > length && (char_is_alphanumeric( *scanner ) || *scanner == '_') ) { - scanner++; + c.scanner++; length ++; } - if ( scanner[0] == '(' ) + if ( c.scanner[0] == '(' ) { length++; } u64 key = crc32( entry.Data, length ); - set(& defines, key, entry ); + set(& c.defines, key, (StrC)entry ); } clear(Tokens); - while (left ) + while (c.left ) { #if 0 if (Tokens.num()) @@ -606,41 +607,41 @@ TokArray lex( StrC content ) } #endif - Token token = { scanner, 0, TokType::Invalid, line, column, TF_Null }; + c.token = { c.scanner, 0, TokType::Invalid, c.line, c.column, TF_Null }; bool is_define = false; - if ( column == 1 ) + if ( c.column == 1 ) { if ( current == '\r') { move_forward(); - token.Length = 1; + c.token.Length = 1; } if ( current == '\n' ) { move_forward(); - token.Type = TokType::NewLine; - token.Length++; + c.token.Type = TokType::NewLine; + c.token.Length++; - append( & Tokens, token ); + append( & Tokens, c.token ); continue; } } - token.Length = 0; + c.token.Length = 0; SkipWhitespace(); - if ( left <= 0 ) + if ( c.left <= 0 ) break; switch ( current ) { case '#': { - s32 result = lex_preprocessor_directive( content, left, scanner, line, column, defines, token ); + s32 result = lex_preprocessor_directive( ctx ); switch ( result ) { case Lex_Continue: @@ -652,12 +653,12 @@ TokArray lex( StrC content ) } case '.': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Access_MemberSymbol; - token.Flags = TF_AccessOperator; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Access_MemberSymbol; + c.token.Flags = TF_AccessOperator; - if (left) { + if (c.left) { move_forward(); } @@ -666,16 +667,16 @@ TokArray lex( StrC content ) move_forward(); if( current == '.' ) { - token.Length = 3; - token.Type = TokType::Varadic_Argument; - token.Flags = TF_Null; + c.token.Length = 3; + c.token.Type = TokType::Varadic_Argument; + c.token.Flags = TF_Null; move_forward(); } else { - String context_str = string_fmt_buf( GlobalAllocator, "%s", scanner, min( 100, left ) ); + String context_str = string_fmt_buf( GlobalAllocator, "%s", c.scanner, min( 100, c.left ) ); - log_failure( "gen::lex: invalid varadic argument, expected '...' got '..%c' (%d, %d)\n%s", current, line, column, context_str ); + log_failure( "gen::lex: invalid varadic argument, expected '...' got '..%c' (%d, %d)\n%s", current, c.line, c.column, context_str ); } } @@ -683,21 +684,21 @@ TokArray lex( StrC content ) } case '&' : { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Ampersand; - token.Flags |= TF_Operator; - token.Flags |= TF_Specifier; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Ampersand; + c.token.Flags |= TF_Operator; + c.token.Flags |= TF_Specifier; - if (left) + if (c.left) move_forward(); if ( current == '&' ) // && { - token.Length = 2; - token.Type = TokType::Ampersand_DBL; + c.token.Length = 2; + c.token.Type = TokType::Ampersand_DBL; - if (left) + if (c.left) move_forward(); } @@ -705,41 +706,41 @@ TokArray lex( StrC content ) } case ':': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Assign_Classifer; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Assign_Classifer; // Can be either a classifier (ParentType, Bitfield width), or ternary else // token.Type = TokType::Colon; - if (left) + if (c.left) move_forward(); if ( current == ':' ) { move_forward(); - token.Type = TokType::Access_StaticSymbol; - token.Length++; + c.token.Type = TokType::Access_StaticSymbol; + c.token.Length++; } goto FoundToken; } case '{': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::BraceCurly_Open; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::BraceCurly_Open; - if (left) + if (c.left) move_forward(); goto FoundToken; } case '}': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::BraceCurly_Close; - token.Flags = TF_EndDefinition; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::BraceCurly_Close; + c.token.Flags = TF_EndDefinition; - if (left) + if (c.left) move_forward(); end_line(); @@ -747,17 +748,17 @@ TokArray lex( StrC content ) } case '[': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::BraceSquare_Open; - if ( left ) + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::BraceSquare_Open; + if ( c.left ) { move_forward(); if ( current == ']' ) { - token.Length = 2; - token.Type = TokType::Operator; + c.token.Length = 2; + c.token.Type = TokType::Operator; move_forward(); } } @@ -765,97 +766,97 @@ TokArray lex( StrC content ) } case ']': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::BraceSquare_Close; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::BraceSquare_Close; - if (left) + if (c.left) move_forward(); goto FoundToken; } case '(': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Capture_Start; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Capture_Start; - if (left) + if (c.left) move_forward(); goto FoundToken; } case ')': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Capture_End; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Capture_End; - if (left) + if (c.left) move_forward(); goto FoundToken; } case '\'': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Char; - token.Flags = TF_Literal; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Char; + c.token.Flags = TF_Literal; move_forward(); - if ( left && current == '\\' ) + if ( c.left && current == '\\' ) { move_forward(); - token.Length++; + c.token.Length++; if ( current == '\'' ) { move_forward(); - token.Length++; + c.token.Length++; } } - while ( left && current != '\'' ) + while ( c.left && current != '\'' ) { move_forward(); - token.Length++; + c.token.Length++; } - if ( left ) + if ( c.left ) { move_forward(); - token.Length++; + c.token.Length++; } goto FoundToken; } case ',': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Comma; - token.Flags = TF_Operator; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Comma; + c.token.Flags = TF_Operator; - if (left) + if (c.left) move_forward(); goto FoundToken; } case '*': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Star; - token.Flags |= TF_Specifier; - token.Flags |= TF_Operator; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Star; + c.token.Flags |= TF_Specifier; + c.token.Flags |= TF_Operator; - if (left) + if (c.left) move_forward(); if ( current == '=' ) { - token.Length++; - token.Flags |= TF_Assign; - // token.Type = TokType::Assign_Multiply; + c.token.Length++; + c.token.Flags |= TF_Assign; + // c.token.Type = TokType::Assign_Multiply; - if ( left ) + if ( c.left ) move_forward(); } @@ -863,12 +864,12 @@ TokArray lex( StrC content ) } case ';': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Statement_End; - token.Flags = TF_EndDefinition; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Statement_End; + c.token.Flags = TF_EndDefinition; - if (left) + if (c.left) move_forward(); end_line(); @@ -876,13 +877,13 @@ TokArray lex( StrC content ) } case '"': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::String; - token.Flags |= TF_Literal; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::String; + c.token.Flags |= TF_Literal; move_forward(); - while ( left ) + while ( c.left ) { if ( current == '"' ) { @@ -893,52 +894,52 @@ TokArray lex( StrC content ) if ( current == '\\' ) { move_forward(); - token.Length++; + c.token.Length++; - if ( left ) + if ( c.left ) { move_forward(); - token.Length++; + c.token.Length++; } continue; } move_forward(); - token.Length++; + c.token.Length++; } goto FoundToken; } case '?': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Operator; - // token.Type = TokType::Ternary; - token.Flags = TF_Operator; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Operator; + // c.token.Type = TokType::Ternary; + c.token.Flags = TF_Operator; - if (left) + if (c.left) move_forward(); goto FoundToken; } case '=': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Operator; - // token.Type = TokType::Assign; - token.Flags = TF_Operator; - token.Flags |= TF_Assign; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Operator; + // c.token.Type = TokType::Assign; + c.token.Flags = TF_Operator; + c.token.Flags |= TF_Assign; - if (left) + if (c.left) move_forward(); if ( current == '=' ) { - token.Length++; - token.Flags = TF_Operator; + c.token.Length++; + c.token.Flags = TF_Operator; - if (left) + if (c.left) move_forward(); } @@ -946,63 +947,63 @@ TokArray lex( StrC content ) } case '+': { - // token.Type = TokType::Add + // c.token.Type = TokType::Add } case '%': { - // token.Type = TokType::Modulo; + // c.token.Type = TokType::Modulo; } case '^': { - // token.Type = TokType::B_XOr; + // c.token.Type = TokType::B_XOr; } case '~': { - // token.Type = TokType::Unary_Not; + // c.token.Type = TokType::Unary_Not; } case '!': { - // token.Type = TokType::L_Not; + // c.token.Type = TokType::L_Not; } case '<': { - // token.Type = TokType::Lesser; + // c.token.Type = TokType::Lesser; } case '>': { - // token.Type = TokType::Greater; + // c.token.Type = TokType::Greater; } case '|': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Operator; - token.Flags = TF_Operator; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Operator; + c.token.Flags = TF_Operator; // token.Type = TokType::L_Or; - if (left) + if (c.left) move_forward(); if ( current == '=' ) { - token.Length++; - token.Flags |= TF_Assign; + c.token.Length++; + c.token.Flags |= TF_Assign; // token.Flags |= TokFlags::Assignment; // token.Type = TokType::Assign_L_Or; - if (left) + if (c.left) move_forward(); } - else while ( left && current == *(scanner - 1) && token.Length < 3 ) + else while ( c.left && current == *(c.scanner - 1) && c.token.Length < 3 ) { - token.Length++; + c.token.Length++; - if (left) + if (c.left) move_forward(); } goto FoundToken; @@ -1011,43 +1012,43 @@ TokArray lex( StrC content ) // Dash is unfortunatlly a bit more complicated... case '-': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Operator; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Operator; // token.Type = TokType::Subtract; - token.Flags = TF_Operator; - if ( left ) + c.token.Flags = TF_Operator; + if ( c.left ) { move_forward(); if ( current == '>' ) { - token.Length++; + c.token.Length++; // token.Type = TokType::Access_PointerToMemberSymbol; - token.Flags |= TF_AccessOperator; + c.token.Flags |= TF_AccessOperator; move_forward(); if ( current == '*' ) { // token.Type = TokType::Access_PointerToMemberOfPointerSymbol; - token.Length++; + c.token.Length++; move_forward(); } } else if ( current == '=' ) { - token.Length++; + c.token.Length++; // token.Type = TokType::Assign_Subtract; - token.Flags |= TF_Assign; + c.token.Flags |= TF_Assign; - if (left) + if (c.left) move_forward(); } - else while ( left && current == *(scanner - 1) && token.Length < 3 ) + else while ( c.left && current == *(c.scanner - 1) && c.token.Length < 3 ) { - token.Length++; + c.token.Length++; - if (left) + if (c.left) move_forward(); } } @@ -1055,82 +1056,82 @@ TokArray lex( StrC content ) } case '/': { - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Operator; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Operator; // token.Type = TokType::Divide; - token.Flags = TF_Operator; + c.token.Flags = TF_Operator; move_forward(); - if ( left ) + if ( c.left ) { if ( current == '=' ) { // token.Type = TokeType::Assign_Divide; move_forward(); - token.Length++; - token.Flags = TF_Assign; + c.token.Length++; + c.token.Flags = TF_Assign; } else if ( current == '/' ) { - token.Type = TokType::Comment; - token.Length = 2; - token.Flags = TF_Null; + c.token.Type = TokType::Comment; + c.token.Length = 2; + c.token.Flags = TF_Null; move_forward(); - while ( left && current != '\n' && current != '\r' ) + while ( c.left && current != '\n' && current != '\r' ) { move_forward(); - token.Length++; + c.token.Length++; } if ( current == '\r' ) { move_forward(); - token.Length++; + c.token.Length++; } if ( current == '\n' ) { move_forward(); - token.Length++; + c.token.Length++; } - append( & Tokens, token ); + append( & Tokens, c.token ); continue; } else if ( current == '*' ) { - token.Type = TokType::Comment; - token.Length = 2; - token.Flags = TF_Null; + c.token.Type = TokType::Comment; + c.token.Length = 2; + c.token.Flags = TF_Null; move_forward(); bool star = current == '*'; - bool slash = scanner[1] == '/'; + bool slash = c.scanner[1] == '/'; bool at_end = star && slash; - while ( left && ! at_end ) + while ( c.left && ! at_end ) { move_forward(); - token.Length++; + c.token.Length++; star = current == '*'; - slash = scanner[1] == '/'; + slash = c.scanner[1] == '/'; at_end = star && slash; } - token.Length += 2; + c.token.Length += 2; move_forward(); move_forward(); if ( current == '\r' ) { move_forward(); - token.Length++; + c.token.Length++; } if ( current == '\n' ) { move_forward(); - token.Length++; + c.token.Length++; } - append( & Tokens, token ); + append( & Tokens, c.token ); // end_line(); continue; } @@ -1141,14 +1142,14 @@ TokArray lex( StrC content ) if ( char_is_alpha( current ) || current == '_' ) { - token.Text = scanner; - token.Length = 1; + c.token.Text = c.scanner; + c.token.Length = 1; move_forward(); - while ( left && ( char_is_alphanumeric(current) || current == '_' ) ) + while ( c.left && ( char_is_alphanumeric(current) || current == '_' ) ) { move_forward(); - token.Length++; + c.token.Length++; } goto FoundToken; @@ -1157,49 +1158,49 @@ TokArray lex( StrC content ) { // This is a very brute force lex, no checks are done for validity of literal. - token.Text = scanner; - token.Length = 1; - token.Type = TokType::Number; - token.Flags = TF_Literal; + c.token.Text = c.scanner; + c.token.Length = 1; + c.token.Type = TokType::Number; + c.token.Flags = TF_Literal; move_forward(); - if (left + if (c.left && ( current == 'x' || current == 'X' || current == 'b' || current == 'B' || current == 'o' || current == 'O' ) ) { move_forward(); - token.Length++; + c.token.Length++; - while ( left && char_is_hex_digit(current) ) + while ( c.left && char_is_hex_digit(current) ) { move_forward(); - token.Length++; + c.token.Length++; } goto FoundToken; } - while ( left && char_is_digit(current) ) + while ( c.left && char_is_digit(current) ) { move_forward(); - token.Length++; + c.token.Length++; } - if ( left && current == '.' ) + if ( c.left && current == '.' ) { move_forward(); - token.Length++; + c.token.Length++; - while ( left && char_is_digit(current) ) + while ( c.left && char_is_digit(current) ) { move_forward(); - token.Length++; + c.token.Length++; } // Handle number literal suffixes in a botched way - if (left && ( + if (c.left && ( current == 'l' || current == 'L' || // long/long long current == 'u' || current == 'U' || // unsigned current == 'f' || current == 'F' || // float @@ -1208,13 +1209,13 @@ TokArray lex( StrC content ) { char prev = current; move_forward(); - token.Length++; + c.token.Length++; // Handle 'll'/'LL' as a special case when we just processed an 'l'/'L' - if (left && (prev == 'l' || prev == 'L') && (current == 'l' || current == 'L')) + if (c.left && (prev == 'l' || prev == 'L') && (current == 'l' || current == 'L')) { move_forward(); - token.Length++; + c.token.Length++; } } } @@ -1234,18 +1235,18 @@ TokArray lex( StrC content ) ); } - String context_str = string_fmt_buf( GlobalAllocator, "%.*s", min( 100, left ), scanner ); - log_failure( "Failed to lex token '%c' (%d, %d)\n%s", current, line, column, context_str ); + String context_str = string_fmt_buf( GlobalAllocator, "%.*s", min( 100, c.left ), c.scanner ); + log_failure( "Failed to lex token '%c' (%d, %d)\n%s", current, c.line, c.column, context_str ); // Skip to next whitespace since we can't know if anything else is valid until then. - while ( left && ! char_is_space( current ) ) + while ( c.left && ! char_is_space( current ) ) { move_forward(); } } FoundToken: - lex_found_token( content, left, scanner, line, column, defines, token ); + lex_found_token( ctx ); } if ( num(Tokens) == 0 ) diff --git a/project/components/parser.cpp b/project/components/parser.cpp index 7192643..a9c2a2a 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -166,7 +166,7 @@ if ( def.Ptr == nullptr ) \ return InvalidCode; \ } -# define currtok_noskip (* current( & Context.Tokens, dont_skip_formatting )) +# define currtok_noskip (* parser::current( & parser::Context.Tokens, parser::dont_skip_formatting )) # define currtok (* current( & Context.Tokens, skip_formatting )) # define prevtok (* previous( Context.Tokens, dont_skip_formatting)) # define nexttok (* next( Context.Tokens, skip_formatting )) @@ -183,8 +183,8 @@ if ( def.Ptr == nullptr ) \ # define check( Type_ ) ( left && currtok.Type == Type_ ) # define push_scope() \ - StackNode scope { nullptr, currtok_noskip, NullToken, txt( __func__ ) }; \ - Context.push( & scope ) + parser::StackNode scope { nullptr, currtok_noskip, parser::NullToken, txt( __func__ ) }; \ + parser::Context.push( & scope ) #pragma endregion Helper Macros From a7c9dad9fd6f14609c4c4a60840596faf20e9ea0 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 3 Dec 2024 09:31:27 -0500 Subject: [PATCH 048/112] cpp feature reduction usage in parser --- project/bootstrap.cpp | 7 +- project/components/gen/etoktype.cpp | 445 ++++++++++--------- project/components/interface.parsing.cpp | 55 +-- project/components/interface.upfront.cpp | 2 +- project/components/lexer.cpp | 2 +- project/components/parser.cpp | 519 ++++++++++++----------- project/dependencies/platform.hpp | 3 + project/helpers/helper.hpp | 11 +- 8 files changed, 531 insertions(+), 513 deletions(-) diff --git a/project/bootstrap.cpp b/project/bootstrap.cpp index 4d9b86d..12ee7bb 100644 --- a/project/bootstrap.cpp +++ b/project/bootstrap.cpp @@ -243,7 +243,12 @@ int gen_main() Code untyped = scan_file( "components/interface.untyped.cpp" ); CodeBody etoktype = gen_etoktype( "enums/ETokType.csv", "enums/AttributeTokens.csv" ); - CodeNS nspaced_etoktype = def_namespace( name(parser), def_namespace_body( args(etoktype)) ); + //CodeNS nspaced_etoktype = def_namespace( name(parser), def_namespace_body( args(etoktype)) ); + CodeBody nspaced_etoktype = def_global_body( args( + untyped_str(txt("GEN_NS_PARSER_BEGIN\n")), + etoktype, + untyped_str(txt("GEN_NS_PARSER_END\n")) + )); Builder src = Builder::open( "gen/gen.cpp" ); diff --git a/project/components/gen/etoktype.cpp b/project/components/gen/etoktype.cpp index 8a5c490..b1ce2ef 100644 --- a/project/components/gen/etoktype.cpp +++ b/project/components/gen/etoktype.cpp @@ -5,237 +5,236 @@ // This file was generated automatially by gencpp's bootstrap.cpp (See: https://github.com/Ed94/gencpp) -namespace parser +GEN_NS_PARSER_BEGIN + +namespace ETokType { - namespace ETokType - { #define GEN_DEFINE_ATTRIBUTE_TOKENS Entry( Attribute_API_Export, "GEN_API_Export_Code" ) Entry( Attribute_API_Import, "GEN_API_Import_Code" ) - enum Type : u32 - { - Invalid, - Access_Private, - Access_Protected, - Access_Public, - Access_MemberSymbol, - Access_StaticSymbol, - Ampersand, - Ampersand_DBL, - Assign_Classifer, - Attribute_Open, - Attribute_Close, - BraceCurly_Open, - BraceCurly_Close, - BraceSquare_Open, - BraceSquare_Close, - Capture_Start, - Capture_End, - Comment, - Comment_End, - Comment_Start, - Char, - Comma, - Decl_Class, - Decl_GNU_Attribute, - Decl_MSVC_Attribute, - Decl_Enum, - Decl_Extern_Linkage, - Decl_Friend, - Decl_Module, - Decl_Namespace, - Decl_Operator, - Decl_Struct, - Decl_Template, - Decl_Typedef, - Decl_Using, - Decl_Union, - Identifier, - Module_Import, - Module_Export, - NewLine, - Number, - Operator, - Preprocess_Hash, - Preprocess_Define, - Preprocess_If, - Preprocess_IfDef, - Preprocess_IfNotDef, - Preprocess_ElIf, - Preprocess_Else, - Preprocess_EndIf, - Preprocess_Include, - Preprocess_Pragma, - Preprocess_Content, - Preprocess_Macro, - Preprocess_Unsupported, - Spec_Alignas, - Spec_Const, - Spec_Consteval, - Spec_Constexpr, - Spec_Constinit, - Spec_Explicit, - Spec_Extern, - Spec_Final, - Spec_ForceInline, - Spec_Global, - Spec_Inline, - Spec_Internal_Linkage, - Spec_LocalPersist, - Spec_Mutable, - Spec_NeverInline, - Spec_Override, - Spec_Static, - Spec_ThreadLocal, - Spec_Volatile, - Spec_Virtual, - Star, - Statement_End, - StaticAssert, - String, - Type_Typename, - Type_Unsigned, - Type_Signed, - Type_Short, - Type_Long, - Type_bool, - Type_char, - Type_int, - Type_double, - Type_MS_int8, - Type_MS_int16, - Type_MS_int32, - Type_MS_int64, - Type_MS_W64, - Varadic_Argument, - __Attributes_Start, - Attribute_API_Export, - Attribute_API_Import, - NumTokens + enum Type : u32 + { + Invalid, + Access_Private, + Access_Protected, + Access_Public, + Access_MemberSymbol, + Access_StaticSymbol, + Ampersand, + Ampersand_DBL, + Assign_Classifer, + Attribute_Open, + Attribute_Close, + BraceCurly_Open, + BraceCurly_Close, + BraceSquare_Open, + BraceSquare_Close, + Capture_Start, + Capture_End, + Comment, + Comment_End, + Comment_Start, + Char, + Comma, + Decl_Class, + Decl_GNU_Attribute, + Decl_MSVC_Attribute, + Decl_Enum, + Decl_Extern_Linkage, + Decl_Friend, + Decl_Module, + Decl_Namespace, + Decl_Operator, + Decl_Struct, + Decl_Template, + Decl_Typedef, + Decl_Using, + Decl_Union, + Identifier, + Module_Import, + Module_Export, + NewLine, + Number, + Operator, + Preprocess_Hash, + Preprocess_Define, + Preprocess_If, + Preprocess_IfDef, + Preprocess_IfNotDef, + Preprocess_ElIf, + Preprocess_Else, + Preprocess_EndIf, + Preprocess_Include, + Preprocess_Pragma, + Preprocess_Content, + Preprocess_Macro, + Preprocess_Unsupported, + Spec_Alignas, + Spec_Const, + Spec_Consteval, + Spec_Constexpr, + Spec_Constinit, + Spec_Explicit, + Spec_Extern, + Spec_Final, + Spec_ForceInline, + Spec_Global, + Spec_Inline, + Spec_Internal_Linkage, + Spec_LocalPersist, + Spec_Mutable, + Spec_NeverInline, + Spec_Override, + Spec_Static, + Spec_ThreadLocal, + Spec_Volatile, + Spec_Virtual, + Star, + Statement_End, + StaticAssert, + String, + Type_Typename, + Type_Unsigned, + Type_Signed, + Type_Short, + Type_Long, + Type_bool, + Type_char, + Type_int, + Type_double, + Type_MS_int8, + Type_MS_int16, + Type_MS_int32, + Type_MS_int64, + Type_MS_W64, + Varadic_Argument, + __Attributes_Start, + Attribute_API_Export, + Attribute_API_Import, + NumTokens + }; + + inline StrC to_str( Type type ) + { + local_persist StrC lookup[] { + { sizeof( "__invalid__" ), "__invalid__" }, + { sizeof( "private" ), "private" }, + { sizeof( "protected" ), "protected" }, + { sizeof( "public" ), "public" }, + { sizeof( "." ), "." }, + { sizeof( "::" ), "::" }, + { sizeof( "&" ), "&" }, + { sizeof( "&&" ), "&&" }, + { sizeof( ":" ), ":" }, + { sizeof( "[[" ), "[[" }, + { sizeof( "]]" ), "]]" }, + { sizeof( "{" ), "{" }, + { sizeof( "}" ), "}" }, + { sizeof( "[" ), "[" }, + { sizeof( "]" ), "]" }, + { sizeof( "(" ), "(" }, + { sizeof( ")" ), ")" }, + { sizeof( "__comment__" ), "__comment__" }, + { sizeof( "__comment_end__" ), "__comment_end__" }, + { sizeof( "__comment_start__" ), "__comment_start__" }, + { sizeof( "__character__" ), "__character__" }, + { sizeof( "," ), "," }, + { sizeof( "class" ), "class" }, + { sizeof( "__attribute__" ), "__attribute__" }, + { sizeof( "__declspec" ), "__declspec" }, + { sizeof( "enum" ), "enum" }, + { sizeof( "extern" ), "extern" }, + { sizeof( "friend" ), "friend" }, + { sizeof( "module" ), "module" }, + { sizeof( "namespace" ), "namespace" }, + { sizeof( "operator" ), "operator" }, + { sizeof( "struct" ), "struct" }, + { sizeof( "template" ), "template" }, + { sizeof( "typedef" ), "typedef" }, + { sizeof( "using" ), "using" }, + { sizeof( "union" ), "union" }, + { sizeof( "__identifier__" ), "__identifier__" }, + { sizeof( "import" ), "import" }, + { sizeof( "export" ), "export" }, + { sizeof( "__new_line__" ), "__new_line__" }, + { sizeof( "__number__" ), "__number__" }, + { sizeof( "__operator__" ), "__operator__" }, + { sizeof( "#" ), "#" }, + { sizeof( "define" ), "define" }, + { sizeof( "if" ), "if" }, + { sizeof( "ifdef" ), "ifdef" }, + { sizeof( "ifndef" ), "ifndef" }, + { sizeof( "elif" ), "elif" }, + { sizeof( "else" ), "else" }, + { sizeof( "endif" ), "endif" }, + { sizeof( "include" ), "include" }, + { sizeof( "pragma" ), "pragma" }, + { sizeof( "__macro_content__" ), "__macro_content__" }, + { sizeof( "__macro__" ), "__macro__" }, + { sizeof( "__unsupported__" ), "__unsupported__" }, + { sizeof( "alignas" ), "alignas" }, + { sizeof( "const" ), "const" }, + { sizeof( "consteval" ), "consteval" }, + { sizeof( "constexpr" ), "constexpr" }, + { sizeof( "constinit" ), "constinit" }, + { sizeof( "explicit" ), "explicit" }, + { sizeof( "extern" ), "extern" }, + { sizeof( "final" ), "final" }, + { sizeof( "forceinline" ), "forceinline" }, + { sizeof( "global" ), "global" }, + { sizeof( "inline" ), "inline" }, + { sizeof( "internal" ), "internal" }, + { sizeof( "local_persist" ), "local_persist" }, + { sizeof( "mutable" ), "mutable" }, + { sizeof( "neverinline" ), "neverinline" }, + { sizeof( "override" ), "override" }, + { sizeof( "static" ), "static" }, + { sizeof( "thread_local" ), "thread_local" }, + { sizeof( "volatile" ), "volatile" }, + { sizeof( "virtual" ), "virtual" }, + { sizeof( "*" ), "*" }, + { sizeof( ";" ), ";" }, + { sizeof( "static_assert" ), "static_assert" }, + { sizeof( "__string__" ), "__string__" }, + { sizeof( "typename" ), "typename" }, + { sizeof( "unsigned" ), "unsigned" }, + { sizeof( "signed" ), "signed" }, + { sizeof( "short" ), "short" }, + { sizeof( "long" ), "long" }, + { sizeof( "bool" ), "bool" }, + { sizeof( "char" ), "char" }, + { sizeof( "int" ), "int" }, + { sizeof( "double" ), "double" }, + { sizeof( "__int8" ), "__int8" }, + { sizeof( "__int16" ), "__int16" }, + { sizeof( "__int32" ), "__int32" }, + { sizeof( "__int64" ), "__int64" }, + { sizeof( "_W64" ), "_W64" }, + { sizeof( "..." ), "..." }, + { sizeof( "__attrib_start__" ), "__attrib_start__" }, + { sizeof( "GEN_API_Export_Code" ), "GEN_API_Export_Code" }, + { sizeof( "GEN_API_Import_Code" ), "GEN_API_Import_Code" }, }; + return lookup[type]; + } - inline StrC to_str( Type type ) + inline Type to_type( StrC str ) + { + local_persist u32 keymap[NumTokens]; + do_once_start for ( u32 index = 0; index < NumTokens; index++ ) { - local_persist StrC lookup[] { - { sizeof( "__invalid__" ), "__invalid__" }, - { sizeof( "private" ), "private" }, - { sizeof( "protected" ), "protected" }, - { sizeof( "public" ), "public" }, - { sizeof( "." ), "." }, - { sizeof( "::" ), "::" }, - { sizeof( "&" ), "&" }, - { sizeof( "&&" ), "&&" }, - { sizeof( ":" ), ":" }, - { sizeof( "[[" ), "[[" }, - { sizeof( "]]" ), "]]" }, - { sizeof( "{" ), "{" }, - { sizeof( "}" ), "}" }, - { sizeof( "[" ), "[" }, - { sizeof( "]" ), "]" }, - { sizeof( "(" ), "(" }, - { sizeof( ")" ), ")" }, - { sizeof( "__comment__" ), "__comment__" }, - { sizeof( "__comment_end__" ), "__comment_end__" }, - { sizeof( "__comment_start__" ), "__comment_start__" }, - { sizeof( "__character__" ), "__character__" }, - { sizeof( "," ), "," }, - { sizeof( "class" ), "class" }, - { sizeof( "__attribute__" ), "__attribute__" }, - { sizeof( "__declspec" ), "__declspec" }, - { sizeof( "enum" ), "enum" }, - { sizeof( "extern" ), "extern" }, - { sizeof( "friend" ), "friend" }, - { sizeof( "module" ), "module" }, - { sizeof( "namespace" ), "namespace" }, - { sizeof( "operator" ), "operator" }, - { sizeof( "struct" ), "struct" }, - { sizeof( "template" ), "template" }, - { sizeof( "typedef" ), "typedef" }, - { sizeof( "using" ), "using" }, - { sizeof( "union" ), "union" }, - { sizeof( "__identifier__" ), "__identifier__" }, - { sizeof( "import" ), "import" }, - { sizeof( "export" ), "export" }, - { sizeof( "__new_line__" ), "__new_line__" }, - { sizeof( "__number__" ), "__number__" }, - { sizeof( "__operator__" ), "__operator__" }, - { sizeof( "#" ), "#" }, - { sizeof( "define" ), "define" }, - { sizeof( "if" ), "if" }, - { sizeof( "ifdef" ), "ifdef" }, - { sizeof( "ifndef" ), "ifndef" }, - { sizeof( "elif" ), "elif" }, - { sizeof( "else" ), "else" }, - { sizeof( "endif" ), "endif" }, - { sizeof( "include" ), "include" }, - { sizeof( "pragma" ), "pragma" }, - { sizeof( "__macro_content__" ), "__macro_content__" }, - { sizeof( "__macro__" ), "__macro__" }, - { sizeof( "__unsupported__" ), "__unsupported__" }, - { sizeof( "alignas" ), "alignas" }, - { sizeof( "const" ), "const" }, - { sizeof( "consteval" ), "consteval" }, - { sizeof( "constexpr" ), "constexpr" }, - { sizeof( "constinit" ), "constinit" }, - { sizeof( "explicit" ), "explicit" }, - { sizeof( "extern" ), "extern" }, - { sizeof( "final" ), "final" }, - { sizeof( "forceinline" ), "forceinline" }, - { sizeof( "global" ), "global" }, - { sizeof( "inline" ), "inline" }, - { sizeof( "internal" ), "internal" }, - { sizeof( "local_persist" ), "local_persist" }, - { sizeof( "mutable" ), "mutable" }, - { sizeof( "neverinline" ), "neverinline" }, - { sizeof( "override" ), "override" }, - { sizeof( "static" ), "static" }, - { sizeof( "thread_local" ), "thread_local" }, - { sizeof( "volatile" ), "volatile" }, - { sizeof( "virtual" ), "virtual" }, - { sizeof( "*" ), "*" }, - { sizeof( ";" ), ";" }, - { sizeof( "static_assert" ), "static_assert" }, - { sizeof( "__string__" ), "__string__" }, - { sizeof( "typename" ), "typename" }, - { sizeof( "unsigned" ), "unsigned" }, - { sizeof( "signed" ), "signed" }, - { sizeof( "short" ), "short" }, - { sizeof( "long" ), "long" }, - { sizeof( "bool" ), "bool" }, - { sizeof( "char" ), "char" }, - { sizeof( "int" ), "int" }, - { sizeof( "double" ), "double" }, - { sizeof( "__int8" ), "__int8" }, - { sizeof( "__int16" ), "__int16" }, - { sizeof( "__int32" ), "__int32" }, - { sizeof( "__int64" ), "__int64" }, - { sizeof( "_W64" ), "_W64" }, - { sizeof( "..." ), "..." }, - { sizeof( "__attrib_start__" ), "__attrib_start__" }, - { sizeof( "GEN_API_Export_Code" ), "GEN_API_Export_Code" }, - { sizeof( "GEN_API_Import_Code" ), "GEN_API_Import_Code" }, - }; - return lookup[type]; + StrC enum_str = to_str( (Type)index ); + keymap[index] = crc32( enum_str.Ptr, enum_str.Len - 1 ); } - - inline Type to_type( StrC str ) + do_once_end u32 hash = crc32( str.Ptr, str.Len ); + for ( u32 index = 0; index < NumTokens; index++ ) { - local_persist u32 keymap[NumTokens]; - do_once_start for ( u32 index = 0; index < NumTokens; index++ ) - { - StrC enum_str = to_str( (Type)index ); - keymap[index] = crc32( enum_str.Ptr, enum_str.Len - 1 ); - } - do_once_end u32 hash = crc32( str.Ptr, str.Len ); - for ( u32 index = 0; index < NumTokens; index++ ) - { - if ( keymap[index] == hash ) - return (Type)index; - } - return Invalid; + if ( keymap[index] == hash ) + return (Type)index; } + return Invalid; + } - } // namespace ETokType +} // namespace ETokType - using TokType = ETokType::Type; - -} // namespace parser +using TokType = ETokType::Type; +GEN_NS_PARSER_END diff --git a/project/components/interface.parsing.cpp b/project/components/interface.parsing.cpp index 4d18903..1abc9eb 100644 --- a/project/components/interface.parsing.cpp +++ b/project/components/interface.parsing.cpp @@ -10,23 +10,24 @@ CodeClass parse_class( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - parser::TokArray toks = parser::lex( def ); + TokArray toks = lex( def ); if ( toks.Arr == nullptr ) return InvalidCode; - parser::Context.Tokens = toks; + Context.Tokens = toks; push_scope(); - CodeClass result = (CodeClass) parser::parse_class_struct( parser::TokType::Decl_Class ); - parser::Context.pop(); + CodeClass result = (CodeClass) parse_class_struct( parser::TokType::Decl_Class ); + pop(& Context); return result; } CodeConstructor parse_constructor( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -58,8 +59,8 @@ CodeConstructor parse_constructor( StrC def ) break; default : - log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str( spec ), Context.to_string() ); - Context.pop(); + log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str( spec ), to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -85,8 +86,8 @@ CodeConstructor parse_constructor( StrC def ) CodeDestructor parse_destructor( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -102,13 +103,13 @@ CodeDestructor parse_destructor( StrC def ) CodeEnum parse_enum( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) { - Context.pop(); + pop(& Context); return InvalidCode; } @@ -118,8 +119,8 @@ CodeEnum parse_enum( StrC def ) CodeBody parse_export_body( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -131,8 +132,8 @@ CodeBody parse_export_body( StrC def ) CodeExtern parse_extern_link( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -144,8 +145,8 @@ CodeExtern parse_extern_link( StrC def ) CodeFriend parse_friend( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -157,8 +158,8 @@ CodeFriend parse_friend( StrC def ) CodeFn parse_function( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -170,8 +171,8 @@ CodeFn parse_function( StrC def ) CodeBody parse_global_body( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -180,14 +181,14 @@ CodeBody parse_global_body( StrC def ) Context.Tokens = toks; push_scope(); CodeBody result = parse_global_nspace( ECode::Global_Body ); - Context.pop(); + pop(& Context); return result; } CodeNS parse_namespace( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -199,8 +200,8 @@ CodeNS parse_namespace( StrC def ) CodeOperator parse_operator( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -212,8 +213,8 @@ CodeOperator parse_operator( StrC def ) CodeOpCast parse_operator_cast( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -225,8 +226,8 @@ CodeOpCast parse_operator_cast( StrC def ) CodeStruct parse_struct( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -235,14 +236,14 @@ CodeStruct parse_struct( StrC def ) Context.Tokens = toks; push_scope(); CodeStruct result = (CodeStruct) parse_class_struct( TokType::Decl_Struct ); - Context.pop(); + pop(& Context); return result; } CodeTemplate parse_template( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -254,8 +255,8 @@ CodeTemplate parse_template( StrC def ) CodeType parse_type( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -267,8 +268,8 @@ CodeType parse_type( StrC def ) CodeTypedef parse_typedef( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -280,8 +281,8 @@ CodeTypedef parse_typedef( StrC def ) CodeUnion parse_union( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -293,8 +294,8 @@ CodeUnion parse_union( StrC def ) CodeUsing parse_using( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) @@ -306,8 +307,8 @@ CodeUsing parse_using( StrC def ) CodeVar parse_variable( StrC def ) { + GEN_USING_NS_PARSER; check_parse_args( def ); - using namespace parser; TokArray toks = lex( def ); if ( toks.Arr == nullptr ) diff --git a/project/components/interface.upfront.cpp b/project/components/interface.upfront.cpp index 6aa0646..3eae336 100644 --- a/project/components/interface.upfront.cpp +++ b/project/components/interface.upfront.cpp @@ -499,7 +499,7 @@ CodeConstructor def_constructor( Opts_def_constructor p ) { CodeParam params = p.params; Code initializer_list = p.initializer_list; - Code body = p.body; + Code body = p.body; if ( params && params->Type != ECode::Parameters ) { diff --git a/project/components/lexer.cpp b/project/components/lexer.cpp index 5d82209..ddf47da 100644 --- a/project/components/lexer.cpp +++ b/project/components/lexer.cpp @@ -450,7 +450,7 @@ s32 lex_preprocessor_directive( LexContext* ctx ) } forceinline -void lex_found_token( LexContext* ctx ) +void lex_found_token( LexContext* ctx ) { if ( ctx->token.Type != TokType::Invalid ) { diff --git a/project/components/parser.cpp b/project/components/parser.cpp index a9c2a2a..88b4032 100644 --- a/project/components/parser.cpp +++ b/project/components/parser.cpp @@ -25,81 +25,81 @@ struct ParseContext { TokArray Tokens; StackNode* Scope; - - void push( StackNode* node ) - { - node->Prev = Scope; - Scope = node; - - #if 0 && Build_Debug - log_fmt("\tEntering Context: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); - #endif - } - - void pop() - { - #if 0 && Build_Debug - log_fmt("\tPopping Context: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); - #endif - Scope = Scope->Prev; - } - - String to_string() - { - String result = string_make_reserve( GlobalAllocator, kilobytes(4) ); - - Token scope_start = Scope->Start; - Token last_valid = Tokens.Idx >= num(Tokens.Arr) ? Tokens.Arr[num(Tokens.Arr) -1] : (* current(& Tokens, true)); - - sptr length = scope_start.Length; - char const* current = scope_start.Text + length; - while ( current <= back( & Tokens.Arr)->Text && *current != '\n' && length < 74 ) - { - current++; - length++; - } - - String line = string_make( GlobalAllocator, { length, scope_start.Text } ); - append_fmt( & result, "\tScope : %s\n", line ); - free(& line); - - sptr dist = (sptr)last_valid.Text - (sptr)scope_start.Text + 2; - sptr length_from_err = dist; - String line_from_err = string_make( GlobalAllocator, { length_from_err, last_valid.Text } ); - - if ( length_from_err < 100 ) - append_fmt(& result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); - else - append_fmt(& result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column ); - - StackNode* curr_scope = Scope; - s32 level = 0; - do - { - if ( is_valid(curr_scope->Name) ) - { - append_fmt(& result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text ); - } - else - { - append_fmt(& result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr ); - } - - curr_scope = curr_scope->Prev; - level++; - } - while ( curr_scope ); - return result; - } }; +void push( ParseContext* ctx, StackNode* node ) +{ + node->Prev = ctx->Scope; + ctx->Scope = node; + +#if 0 && Build_Debug + log_fmt("\tEntering Context: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); +#endif +} + +void pop(ParseContext* ctx) +{ +#if 0 && Build_Debug + log_fmt("\tPopping Context: %.*s\n", Scope->ProcName.Len, Scope->ProcName.Ptr ); +#endif + ctx->Scope = ctx->Scope->Prev; +} + +String to_string(ParseContext ctx) +{ + String result = string_make_reserve( GlobalAllocator, kilobytes(4) ); + + Token scope_start = ctx.Scope->Start; + Token last_valid = ctx.Tokens.Idx >= num(ctx.Tokens.Arr) ? ctx.Tokens.Arr[num(ctx.Tokens.Arr) -1] : (* current(& ctx.Tokens, true)); + + sptr length = scope_start.Length; + char const* current = scope_start.Text + length; + while ( current <= back( & ctx.Tokens.Arr)->Text && *current != '\n' && length < 74 ) + { + current++; + length++; + } + + String line = string_make( GlobalAllocator, { length, scope_start.Text } ); + append_fmt( & result, "\tScope : %s\n", line ); + free(& line); + + sptr dist = (sptr)last_valid.Text - (sptr)scope_start.Text + 2; + sptr length_from_err = dist; + String line_from_err = string_make( GlobalAllocator, { length_from_err, last_valid.Text } ); + + if ( length_from_err < 100 ) + append_fmt(& result, "\t(%d, %d):%*c\n", last_valid.Line, last_valid.Column, length_from_err, '^' ); + else + append_fmt(& result, "\t(%d, %d)\n", last_valid.Line, last_valid.Column ); + + StackNode* curr_scope = ctx.Scope; + s32 level = 0; + do + { + if ( is_valid(curr_scope->Name) ) + { + append_fmt(& result, "\t%d: %s, AST Name: %.*s\n", level, curr_scope->ProcName.Ptr, curr_scope->Name.Length, curr_scope->Name.Text ); + } + else + { + append_fmt(& result, "\t%d: %s\n", level, curr_scope->ProcName.Ptr ); + } + + curr_scope = curr_scope->Prev; + level++; + } + while ( curr_scope ); + return result; +} + global ParseContext Context; bool __eat(TokArray* self, TokType type ) { if ( num(self->Arr) - self->Idx <= 0 ) { - log_failure( "No tokens left.\n%s", Context.to_string() ); + log_failure( "No tokens left.\n%s", to_string(Context) ); return false; } @@ -119,7 +119,7 @@ bool __eat(TokArray* self, TokType type ) , at_idx.Length, at_idx.Text , tok.Line , tok.Column - , Context.to_string() + , to_string(Context) ); return false; @@ -152,22 +152,26 @@ void deinit() #pragma region Helper Macros -# define check_parse_args( def ) \ -if ( def.Len <= 0 ) \ -{ \ - log_failure( "gen::" stringize(__func__) ": length must greater than 0" ); \ - parser::Context.pop(); \ - return InvalidCode; \ -} \ -if ( def.Ptr == nullptr ) \ -{ \ - log_failure( "gen::" stringize(__func__) ": def was null" ); \ - parser::Context.pop(); \ - return InvalidCode; \ +#define check_parse_args( def ) _check_parse_args(def, stringize(_func_) ) +bool _check_parse_args( StrC def, char const* func_name ) +{ + if ( def.Len <= 0 ) + { + log_failure( str_fmt_buf("gen::%s: length must greater than 0", func_name) ); + pop(& Context); + return false; + } + if ( def.Ptr == nullptr ) + { + log_failure( str_fmt_buf("gen::%s: def was null", func_name) ); + pop(& Context); + return false; + } + return true; } -# define currtok_noskip (* parser::current( & parser::Context.Tokens, parser::dont_skip_formatting )) -# define currtok (* current( & Context.Tokens, skip_formatting )) +# define currtok_noskip (* current( & Context.Tokens, dont_skip_formatting )) +# define currtok (* current( & Context.Tokens, skip_formatting )) # define prevtok (* previous( Context.Tokens, dont_skip_formatting)) # define nexttok (* next( Context.Tokens, skip_formatting )) # define eat( Type_ ) __eat( & Context.Tokens, Type_ ) @@ -184,7 +188,7 @@ if ( def.Ptr == nullptr ) \ # define push_scope() \ parser::StackNode scope { nullptr, currtok_noskip, parser::NullToken, txt( __func__ ) }; \ - parser::Context.push( & scope ) + push( & parser::Context, & scope ) #pragma endregion Helper Macros @@ -496,7 +500,7 @@ Code parse_array_decl() eat( TokType::Operator ); // [] - Context.pop(); + pop(& Context); return array_expr; } @@ -507,15 +511,15 @@ Code parse_array_decl() if ( left == 0 ) { - log_failure( "Error, unexpected end of array declaration ( '[]' scope started )\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Error, unexpected end of array declaration ( '[]' scope started )\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } if ( currtok.Type == TokType::BraceSquare_Close ) { - log_failure( "Error, empty array expression in definition\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Error, empty array expression in definition\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -533,15 +537,15 @@ Code parse_array_decl() if ( left == 0 ) { - log_failure( "Error, unexpected end of array declaration, expected ]\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Error, unexpected end of array declaration, expected ]\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } if ( currtok.Type != TokType::BraceSquare_Close ) { - log_failure( "%s: Error, expected ] in array declaration, not %s\n%s", ETokType::to_str( currtok.Type ), Context.to_string() ); - Context.pop(); + log_failure( "%s: Error, expected ] in array declaration, not %s\n%s", ETokType::to_str( currtok.Type ), to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -557,11 +561,11 @@ Code parse_array_decl() array_expr->Next.ast = adjacent_arr_expr.ast; } - Context.pop(); + pop(& Context); return array_expr; } - Context.pop(); + pop(& Context); return { nullptr }; } @@ -659,7 +663,7 @@ CodeAttributes parse_attributes() if ( len > 0 ) { StrC attribute_txt = { len, start.Text }; - Context.pop(); + pop(& Context); String name_stripped = strip_formatting( attribute_txt, strip_formatting_dont_preserve_newlines ); @@ -672,7 +676,7 @@ CodeAttributes parse_attributes() return ( CodeAttributes )result; } - Context.pop(); + pop(& Context); return { nullptr }; } @@ -681,7 +685,7 @@ Code parse_class_struct( TokType which, bool inplace_def = false ) { if ( which != TokType::Decl_Class && which != TokType::Decl_Struct ) { - log_failure( "Error, expected class or struct, not %s\n%s", ETokType::to_str( which ), Context.to_string() ); + log_failure( "Error, expected class or struct, not %s\n%s", ETokType::to_str( which ), to_string(Context) ); return InvalidCode; } @@ -904,7 +908,7 @@ CodeBody parse_class_struct_body( TokType which, Token name ) case TokType::Operator: if ( currtok.Text[0] != '~' ) { - log_failure( "Operator token found in global body but not destructor unary negation\n%s", Context.to_string() ); + log_failure( "Operator token found in global body but not destructor unary negation\n%s", to_string(Context) ); return InvalidCode; } @@ -1017,8 +1021,8 @@ CodeBody parse_class_struct_body( TokType which, Token name ) break; default: - log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str(spec), Context.to_string() ); - Context.pop(); + log_failure( "Invalid specifier %s for variable\n%s", ESpecifier::to_str(spec), to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1114,8 +1118,8 @@ CodeBody parse_class_struct_body( TokType which, Token name ) if ( member == Code_Invalid ) { - log_failure( "Failed to parse member\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Failed to parse member\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1124,15 +1128,14 @@ CodeBody parse_class_struct_body( TokType which, Token name ) eat( TokType::BraceCurly_Close ); // { } - Context.pop(); + pop(& Context); return result; } internal CodeComment parse_comment() { - StackNode scope { nullptr, currtok_noskip, NullToken, txt( __func__ ) }; - Context.push( & scope ); + push_scope(); CodeComment result = (CodeComment) make_code(); @@ -1142,7 +1145,7 @@ CodeComment parse_comment() // result->Token = currtok_noskip; eat( TokType::Comment ); - Context.pop(); + pop(& Context); return result; } @@ -1174,7 +1177,7 @@ Code parse_complicated_definition( TokType which ) // Its a forward declaration only Code result = parse_forward_or_definition( which, is_inplace ); // ; - Context.pop(); + pop(& Context); return result; } @@ -1198,12 +1201,12 @@ Code parse_complicated_definition( TokType which ) Code result = parse_operator_function_or_variable( false, { nullptr }, { nullptr } ); // , or Name> ... - Context.pop(); + pop(& Context); return result; } - log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), Context.to_string() ); - Context.pop(); + log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), to_string(Context) ); + pop(& Context); return InvalidCode; } if ( tok.Type == TokType::Identifier ) @@ -1235,7 +1238,7 @@ Code parse_complicated_definition( TokType which ) // : ; ok_to_parse = true; Code result = parse_enum(); - Context.pop(); + pop(& Context); return result; } else if ( is_indirection ) @@ -1247,14 +1250,14 @@ Code parse_complicated_definition( TokType which ) if ( ! ok_to_parse ) { - log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), Context.to_string() ); - Context.pop(); + log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), to_string(Context) ); + pop(& Context); return InvalidCode; } Code result = parse_operator_function_or_variable( false, { nullptr }, { nullptr } ); // , or Name> ... - Context.pop(); + pop(& Context); return result; } else if ( tok.Type >= TokType::Type_Unsigned && tok.Type <= TokType::Type_MS_W64 ) @@ -1266,8 +1269,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", to_str(which), Context.to_string() ); - Context.pop(); + log_failure( "Unsupported or bad member definition after %s declaration\n%s", to_str(which), to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1275,7 +1278,7 @@ Code parse_complicated_definition( TokType which ) // : ; // : ; Code result = parse_enum(); - Context.pop(); + pop(& Context); return result; } else if ( tok.Type == TokType::BraceCurly_Close ) @@ -1283,7 +1286,7 @@ Code parse_complicated_definition( TokType which ) // Its a definition Code result = parse_forward_or_definition( which, is_inplace ); // { ... }; - Context.pop(); + pop(& Context); return result; } else if ( tok.Type == TokType::BraceSquare_Close ) @@ -1291,13 +1294,13 @@ Code parse_complicated_definition( TokType which ) // Its an array definition Code result = parse_operator_function_or_variable( false, { nullptr }, { nullptr } ); // [ ... ]; - Context.pop(); + pop(& Context); return result; } else { - log_failure( "Unsupported or bad member definition after %s declaration\n%S", to_str(which).Ptr, Context.to_string() ); - Context.pop(); + log_failure( "Unsupported or bad member definition after %s declaration\n%S", to_str(which).Ptr, to_string(Context) ); + pop(& Context); return InvalidCode; } } @@ -1315,8 +1318,8 @@ CodeDefine parse_define() if ( ! check( TokType::Identifier ) ) { - log_failure( "Error, expected identifier after #define\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Error, expected identifier after #define\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1327,8 +1330,8 @@ CodeDefine parse_define() if ( ! check( TokType::Preprocess_Content )) { - log_failure( "Error, expected content after #define %s\n%s", define->Name, Context.to_string() ); - Context.pop(); + log_failure( "Error, expected content after #define %s\n%s", define->Name, to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1338,7 +1341,7 @@ CodeDefine parse_define() eat( TokType::Preprocess_Content ); // #define - Context.pop(); + pop(& Context); return define; } @@ -1346,7 +1349,7 @@ CodeDefine parse_define() eat( TokType::Preprocess_Content ); // #define - Context.pop(); + pop(& Context); return define; } @@ -1362,8 +1365,8 @@ Code parse_assignment_expression() if ( currtok.Type == TokType::Statement_End && currtok.Type != TokType::Comma ) { - log_failure( "Expected expression after assignment operator\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Expected expression after assignment operator\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1414,7 +1417,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" - , Context.to_string() ); + , to_string(Context) ); return InvalidCode; } @@ -1456,7 +1459,7 @@ CodeFn parse_function_after_name( body = parse_function_body(); if ( body == Code_Invalid ) { - Context.pop(); + pop(& Context); return InvalidCode; } // ( ) { } @@ -1507,8 +1510,8 @@ CodeFn parse_function_after_name( default: { - log_failure("Body must be either of Function_Body or Untyped type, %s\n%s", debug_str(body), Context.to_string()); - Context.pop(); + log_failure("Body must be either of Function_Body or Untyped type, %s\n%s", debug_str(body), to_string(Context)); + pop(& Context); return InvalidCode; } } @@ -1535,7 +1538,7 @@ CodeFn parse_function_after_name( if ( inline_cmt ) result->InlineCmt = inline_cmt; - Context.pop(); + pop(& Context); return result; } @@ -1577,7 +1580,7 @@ Code parse_function_body() eat( TokType::BraceCurly_Close ); - Context.pop(); + pop(& Context); return result; } @@ -1643,7 +1646,7 @@ CodeBody parse_global_nspace( CodeT which ) case TokType::Decl_Extern_Linkage: if ( which == Extern_Linkage_Body ) - log_failure( "Nested extern linkage\n%s", Context.to_string() ); + log_failure( "Nested extern linkage\n%s", to_string(Context) ); member = parse_extern_link(); // extern "..." { ... } @@ -1731,7 +1734,7 @@ CodeBody parse_global_nspace( CodeT which ) case TokType::Module_Export: if ( which == Export_Body ) - log_failure( "Nested export declaration\n%s", Context.to_string() ); + log_failure( "Nested export declaration\n%s", to_string(Context) ); member = parse_export_body(); // export { ... } @@ -1800,8 +1803,8 @@ CodeBody parse_global_nspace( CodeT which ) default: StrC spec_str = ESpecifier::to_str(spec); - log_failure( "Invalid specifier %.*s for variable\n%s", spec_str.Len, spec_str, Context.to_string() ); - Context.pop(); + log_failure( "Invalid specifier %.*s for variable\n%s", spec_str.Len, spec_str, to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1876,8 +1879,8 @@ CodeBody parse_global_nspace( CodeT which ) if ( member == Code_Invalid ) { - log_failure( "Failed to parse member\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Failed to parse member\n%s", to_string(Context) ); + pop(& Context); return InvalidCode; } @@ -1889,7 +1892,7 @@ CodeBody parse_global_nspace( CodeT which ) eat( TokType::BraceCurly_Close ); // { } - Context.pop(); + pop(& Context); return result; } @@ -2044,8 +2047,8 @@ Token parse_identifier( bool* possible_member_function ) if ( left == 0 ) { - log_failure( "Error, unexpected end of static symbol identifier\n%s", Context.to_string() ); - Context.pop(); + log_failure( "Error, unexpected end of static symbol identifier\n%s", to_string(Context) ); + pop(& Context); return { nullptr, 0, TokType::Invalid }; } @@ -2055,12 +2058,12 @@ Token parse_identifier( bool* possible_member_function ) if (is_destructor) { name.Length = ( ( sptr )prevtok.Text + prevtok.Length ) - ( sptr )name.Text; - Context.pop(); + pop(& Context); return name; } - log_failure( "Error, had a ~ operator after %S but not a destructor\n%s", ETokType::to_str( prevtok.Type ), Context.to_string() ); - Context.pop(); + log_failure( "Error, had a ~ operator after %S but not a destructor\n%s", ETokType::to_str( prevtok.Type ), to_string(Context) ); + pop(& Context); return { nullptr, 0, TokType::Invalid }; } @@ -2071,16 +2074,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", Context.to_string() ); - Context.pop(); + log_failure( "Found a member function pointer identifier but the parsing context did not expect it\n%s", to_string(Context) ); + pop(& Context); return { nullptr, 0, TokType::Invalid }; } } if ( currtok.Type != TokType::Identifier ) { - log_failure( "Error, expected static symbol identifier, not %s\n%s", ETokType::to_str( currtok.Type ), Context.to_string() ); - Context.pop(); + log_failure( "Error, expected static symbol identifier, not %s\n%s", ETokType::to_str( currtok.Type ), to_string(Context) ); + pop(& Context); return { nullptr, 0, TokType::Invalid }; } @@ -2093,7 +2096,7 @@ Token parse_identifier( bool* possible_member_function ) } //