2023-04-10 18:33:06 -07:00
|
|
|
// ReSharper disable CppClangTidyClangDiagnosticSwitchEnum
|
2023-07-08 11:11:41 -07:00
|
|
|
|
|
|
|
#ifdef gen_time
|
2023-07-16 09:08:57 -07:00
|
|
|
//! 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
|
|
|
|
# include "gen_dep.cpp"
|
2023-07-11 23:37:51 -07:00
|
|
|
#endif
|
2023-07-16 09:08:57 -07:00
|
|
|
|
|
|
|
#include "gen.hpp"
|
2023-07-11 23:37:51 -07:00
|
|
|
|
|
|
|
namespace gen
|
|
|
|
{
|
2023-04-03 00:55:28 -07:00
|
|
|
namespace StaticData
|
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
global Array< Pool > CodePools = { nullptr };
|
|
|
|
global Array< Arena > StringArenas = { nullptr };
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
global StringTable StringCache;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
// TODO : Need to implement String memory management for seriaization intermediates.
|
|
|
|
|
|
|
|
global Arena LexArena;
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
global AllocatorInfo Allocator_DataArrays = heap();
|
|
|
|
global AllocatorInfo Allocator_CodePool = heap();
|
2023-07-15 23:26:55 -07:00
|
|
|
global AllocatorInfo Allocator_Lexer = heap();
|
2023-07-09 09:35:48 -07:00
|
|
|
global AllocatorInfo Allocator_StringArena = heap();
|
|
|
|
global AllocatorInfo Allocator_StringTable = heap();
|
|
|
|
global AllocatorInfo Allocator_TypeTable = heap();
|
2023-04-03 00:55:28 -07:00
|
|
|
}
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
#pragma region Constants
|
2023-07-13 20:01:20 -07:00
|
|
|
global CodeType t_auto;
|
|
|
|
global CodeType t_void;
|
|
|
|
global CodeType t_int;
|
|
|
|
global CodeType t_bool;
|
|
|
|
global CodeType t_char;
|
|
|
|
global CodeType t_wchar_t;
|
|
|
|
global CodeType t_class;
|
|
|
|
global CodeType t_typename;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-06-30 21:23:40 -07:00
|
|
|
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
|
2023-07-13 20:01:20 -07:00
|
|
|
global CodeType t_b32;
|
2023-06-30 21:23:40 -07:00
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
global CodeType t_s8;
|
|
|
|
global CodeType t_s16;
|
|
|
|
global CodeType t_s32;
|
|
|
|
global CodeType t_s64;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
global CodeType t_u8;
|
|
|
|
global CodeType t_u16;
|
|
|
|
global CodeType t_u32;
|
|
|
|
global CodeType t_u64;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
global CodeType t_sw;
|
|
|
|
global CodeType t_uw;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
global CodeType t_f32;
|
|
|
|
global CodeType t_f64;
|
2023-04-22 19:24:55 -07:00
|
|
|
#endif
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
global Code access_public;
|
|
|
|
global Code access_protected;
|
|
|
|
global Code access_private;
|
|
|
|
|
|
|
|
global Code module_global_fragment;
|
|
|
|
global Code module_private_fragment;
|
|
|
|
|
|
|
|
global Code pragma_once;
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
global CodeSpecifier spec_const;
|
|
|
|
global CodeSpecifier spec_consteval;
|
|
|
|
global CodeSpecifier spec_constexpr;
|
|
|
|
global CodeSpecifier spec_constinit;
|
|
|
|
global CodeSpecifier spec_extern_linkage;
|
|
|
|
global CodeSpecifier spec_global;
|
|
|
|
global CodeSpecifier spec_inline;
|
|
|
|
global CodeSpecifier spec_internal_linkage;
|
|
|
|
global CodeSpecifier spec_local_persist;
|
|
|
|
global CodeSpecifier spec_mutable;
|
|
|
|
global CodeSpecifier spec_ptr;
|
|
|
|
global CodeSpecifier spec_ref;
|
|
|
|
global CodeSpecifier spec_register;
|
|
|
|
global CodeSpecifier spec_rvalue;
|
|
|
|
global CodeSpecifier spec_static_member;
|
|
|
|
global CodeSpecifier spec_thread_local;
|
|
|
|
global CodeSpecifier spec_volatile;
|
2023-04-09 21:38:47 -07:00
|
|
|
#pragma endregion Constants
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma region AST Body Case Macros
|
|
|
|
# define AST_BODY_CLASS_UNALLOWED_TYPES \
|
2023-07-14 21:03:14 -07:00
|
|
|
case PlatformAttributes: \
|
2023-04-22 19:24:55 -07:00
|
|
|
case Class_Body: \
|
|
|
|
case Enum_Body: \
|
2023-04-23 16:04:43 -07:00
|
|
|
case Extern_Linkage: \
|
2023-04-22 19:24:55 -07:00
|
|
|
case Function_Body: \
|
|
|
|
case Function_Fwd: \
|
|
|
|
case Global_Body: \
|
|
|
|
case Namespace: \
|
|
|
|
case Namespace_Body: \
|
|
|
|
case Operator: \
|
|
|
|
case Operator_Fwd: \
|
|
|
|
case Parameters: \
|
|
|
|
case Specifiers: \
|
|
|
|
case Struct_Body: \
|
|
|
|
case Typename:
|
|
|
|
|
|
|
|
# define AST_BODY_FUNCTION_UNALLOWED_TYPES \
|
|
|
|
case Access_Public: \
|
|
|
|
case Access_Protected: \
|
|
|
|
case Access_Private: \
|
2023-07-14 21:03:14 -07:00
|
|
|
case PlatformAttributes: \
|
2023-04-22 19:24:55 -07:00
|
|
|
case Class_Body: \
|
|
|
|
case Enum_Body: \
|
2023-04-23 16:04:43 -07:00
|
|
|
case Extern_Linkage: \
|
2023-04-22 19:24:55 -07:00
|
|
|
case Friend: \
|
|
|
|
case Function_Body: \
|
|
|
|
case Function_Fwd: \
|
|
|
|
case Global_Body: \
|
|
|
|
case Namespace: \
|
|
|
|
case Namespace_Body: \
|
|
|
|
case Operator: \
|
|
|
|
case Operator_Fwd: \
|
|
|
|
case Operator_Member: \
|
|
|
|
case Operator_Member_Fwd: \
|
|
|
|
case Parameters: \
|
|
|
|
case Specifiers: \
|
|
|
|
case Struct_Body: \
|
|
|
|
case Typename:
|
|
|
|
|
|
|
|
# define AST_BODY_GLOBAL_UNALLOWED_TYPES \
|
|
|
|
case Access_Public: \
|
|
|
|
case Access_Protected: \
|
|
|
|
case Access_Private: \
|
2023-07-14 21:03:14 -07:00
|
|
|
case PlatformAttributes: \
|
2023-04-22 19:24:55 -07:00
|
|
|
case Class_Body: \
|
|
|
|
case Enum_Body: \
|
|
|
|
case Execution: \
|
|
|
|
case Friend: \
|
|
|
|
case Function_Body: \
|
|
|
|
case Global_Body: \
|
|
|
|
case Namespace_Body: \
|
|
|
|
case Operator_Member: \
|
|
|
|
case Operator_Member_Fwd: \
|
|
|
|
case Parameters: \
|
|
|
|
case Specifiers: \
|
|
|
|
case Struct_Body: \
|
|
|
|
case Typename:
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
# define AST_BODY_EXPORT_UNALLOWED_TYPES AST_BODY_GLOBAL_UNALLOWED_TYPES
|
|
|
|
# define AST_BODY_NAMESPACE_UNALLOWED_TYPES AST_BODY_GLOBAL_UNALLOWED_TYPES
|
|
|
|
# define AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES AST_BODY_GLOBAL_UNALLOWED_TYPES
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-05-05 21:18:44 -07:00
|
|
|
# define AST_BODY_STRUCT_UNALLOWED_TYPES AST_BODY_CLASS_UNALLOWED_TYPES
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion AST Body Case Macros
|
|
|
|
|
|
|
|
#pragma region AST
|
2023-07-08 09:21:26 -07:00
|
|
|
Code Code::Global;
|
2023-04-10 18:33:06 -07:00
|
|
|
Code Code::Invalid;
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
AST* AST::duplicate()
|
|
|
|
{
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
AST*
|
|
|
|
result = make_code().ast;
|
|
|
|
#ifndef GEN_USE_RECURSIVE_AST_DUPLICATION
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
result->Parent = nullptr;
|
|
|
|
#else
|
|
|
|
switch ( Type )
|
|
|
|
{
|
|
|
|
case Invalid:
|
|
|
|
log_failure("Attempted to duplicate invalid code! - \n%s", Parent ? Parent->debug_str() : Name );
|
|
|
|
return nullptr
|
|
|
|
case Untyped:
|
|
|
|
case Comment:
|
|
|
|
case Execution:
|
|
|
|
case Access_Private:
|
|
|
|
case Access_Protected:
|
|
|
|
case Access_Public:
|
|
|
|
case PlatformAttributes:
|
|
|
|
case Preprocessor_Include:
|
|
|
|
case Module:
|
|
|
|
case Specifiers:
|
|
|
|
case Using_Namespace:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Extern_Linkage:
|
|
|
|
case Friend:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
if (Value)
|
|
|
|
result->Value = Value->duplicate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Class:
|
|
|
|
case Struct:
|
|
|
|
case Enum:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
if ( Attributes)
|
|
|
|
result->Attributes = Attributes->duplicate();
|
|
|
|
|
|
|
|
if ( ParentType )
|
|
|
|
result->ParentType = ParentType->duplicate();
|
|
|
|
|
|
|
|
result->Body = Body->duplicate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Enum_Fwd:
|
|
|
|
case Class_Fwd:
|
|
|
|
case Struct_Fwd:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
if ( Attributes)
|
|
|
|
result->Attributes = Attributes->duplicate();
|
|
|
|
|
|
|
|
if ( ParentType )
|
|
|
|
result->ParentType = ParentType->duplicate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Function:
|
|
|
|
case Operator:
|
|
|
|
case Operator_Member:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
2023-06-30 00:13:41 -07:00
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
if ( Attributes)
|
|
|
|
result->Attributes = Attributes->duplicate();
|
|
|
|
|
|
|
|
if ( Specs )
|
|
|
|
result->ParentType = ParentType->duplicate();
|
|
|
|
|
|
|
|
if ( ReturnType )
|
|
|
|
result->ReturnType = ReturnType->duplicate();
|
|
|
|
|
|
|
|
if ( Params )
|
|
|
|
result->Params = Params->duplicate();
|
|
|
|
|
|
|
|
result->Body = Body->duplicate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Function_Fwd:
|
|
|
|
case Operator_Fwd:
|
|
|
|
case Operator_Member_Fwd:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
if ( Attributes)
|
|
|
|
result->Attributes = Attributes->duplicate();
|
|
|
|
|
|
|
|
if ( Specs )
|
|
|
|
result->ParentType = ParentType->duplicate();
|
|
|
|
|
|
|
|
if ( ReturnType )
|
|
|
|
result->ReturnType = ReturnType->duplicate();
|
|
|
|
|
|
|
|
if ( Params )
|
|
|
|
result->Params = Params->duplicate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Namespace:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
result->Body = Body->duplicate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Operator_Cast:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
result->ValueType = ValueType->duplicate();
|
|
|
|
result->Body = Body->duplicate();
|
|
|
|
break;
|
|
|
|
case Operator_Cast_Fwd:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
result->ValueType = ValueType->duplicate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Parameters:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
result->NumEntries = 0;
|
|
|
|
result->Last = nullptr;
|
|
|
|
result->Next = nullptr;
|
|
|
|
|
|
|
|
if ( NumEntries - 1 > 0 )
|
|
|
|
{
|
|
|
|
CodeParam parent = result->cast<CodeParam>();
|
|
|
|
for ( CodeParam param : Next->cast<CodeParam>() )
|
|
|
|
{
|
|
|
|
parent.append( param );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-05-07 12:03:24 -07:00
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
case Template:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
result->Params = Params->duplicate();
|
|
|
|
result->Declaration = Declaration->duplicate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Typename:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
if (Attributes)
|
|
|
|
result->Attributes = Attributes->duplicate();
|
|
|
|
|
|
|
|
if ( Specs )
|
|
|
|
result->Specs = Specs->duplicate();
|
|
|
|
|
|
|
|
if ( ArrExpr )
|
|
|
|
result->ArrExpr = ArrExpr->duplicate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Typedef:
|
|
|
|
case Using:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
if (Attributes)
|
|
|
|
result->Attributes = Attributes->duplicate();
|
|
|
|
|
|
|
|
if ( UnderlyingType )
|
|
|
|
result->UnderlyingType = UnderlyingType->duplicate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Union:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
if ( Attributes)
|
|
|
|
result->Attributes = Attributes->duplicate();
|
|
|
|
|
|
|
|
result->Body = Body->duplicate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Variable:
|
|
|
|
mem_copy( result, this, sizeof( AST ) );
|
|
|
|
|
|
|
|
if (Attributes)
|
|
|
|
result->Attributes = Attributes->duplicate();
|
|
|
|
|
|
|
|
if ( Specs )
|
|
|
|
result->Specs = Specs->duplicate();
|
|
|
|
|
|
|
|
result->ValueType = UnderlyingType->duplicate();
|
|
|
|
|
|
|
|
if ( Value )
|
|
|
|
result->Value = Value->duplicate();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Class_Body:
|
|
|
|
case Enum_Body:
|
|
|
|
case Export_Body:
|
|
|
|
case Extern_Linkage_Body:
|
|
|
|
case Function_Body:
|
|
|
|
case Global_Body:
|
|
|
|
case Namespace_Body:
|
|
|
|
case Struct_Body:
|
|
|
|
case Union_Body:
|
|
|
|
CodeBody
|
|
|
|
body = cast<CodeBody>();
|
|
|
|
body->Name = Name;
|
|
|
|
body->Type = Type;
|
|
|
|
for ( Code entry : cast<CodeBody>() )
|
|
|
|
{
|
|
|
|
result->append( entry.ast );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
String AST::to_string()
|
2023-06-28 18:20:29 -07:00
|
|
|
{
|
2023-07-16 09:08:57 -07:00
|
|
|
# define ProcessModuleFlags() \
|
2023-05-05 21:18:44 -07:00
|
|
|
if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Export )) \
|
2023-07-16 09:08:57 -07:00
|
|
|
result.append( "export " ); \
|
2023-05-05 21:18:44 -07:00
|
|
|
\
|
|
|
|
if ( bitfield_is_equal( u32, ModuleFlags, ModuleFlag::Import )) \
|
2023-07-16 09:08:57 -07:00
|
|
|
result.append( "import " ); \
|
|
|
|
|
|
|
|
local_persist thread_local
|
|
|
|
char SerializationLevel = 0;
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-07-16 15:00:07 -07:00
|
|
|
#if defined(GEN_BENCHMARK) && defined(GEN_BENCHMARK_SERIALIZATION)
|
|
|
|
u64 time_start = time_rel_ms();
|
|
|
|
#endif
|
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
// TODO : Need to refactor so that intermeidate strings are freed conviently.
|
2023-07-09 09:35:48 -07:00
|
|
|
String result = String::make( Memory::GlobalAllocator, "" );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
|
|
|
switch ( Type )
|
|
|
|
{
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
case Invalid:
|
2023-07-15 23:26:55 -07:00
|
|
|
log_failure("Attempted to serialize invalid code! - %s", Parent ? Parent->debug_str() : Name );
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Untyped:
|
2023-06-30 21:23:40 -07:00
|
|
|
case Execution:
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( Content );
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
case Comment:
|
|
|
|
{
|
|
|
|
static char line[MaxCommentLineLength];
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
s32 left = Content.length();
|
2023-05-01 11:12:07 -07:00
|
|
|
s32 index = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
s32 length = 0;
|
|
|
|
while ( left && Content[index] != '\n' )
|
2023-06-28 11:43:21 -07:00
|
|
|
{
|
2023-05-01 11:12:07 -07:00
|
|
|
length++;
|
2023-06-28 11:43:21 -07:00
|
|
|
left--;
|
|
|
|
}
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
str_copy( line, Content, length );
|
2023-05-01 11:12:07 -07:00
|
|
|
line[length] = '\0';
|
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
result.append_fmt( "// %s", line );
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
2023-06-28 11:43:21 -07:00
|
|
|
while ( left--, left > 0 );
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
case Access_Private:
|
2023-05-01 11:12:07 -07:00
|
|
|
case Access_Protected:
|
|
|
|
case Access_Public:
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( Name );
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
case PlatformAttributes:
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( Content );
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
case Class:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes || ParentType )
|
2023-06-28 11:43:21 -07:00
|
|
|
{
|
|
|
|
result.append( "class " );
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes )
|
2023-07-15 23:26:55 -07:00
|
|
|
{
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-07-15 23:26:55 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( ParentType )
|
2023-06-28 11:43:21 -07:00
|
|
|
{
|
|
|
|
char const* access_level = to_str( ParentAccess );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
result.append_fmt( "%s : %s %s\n{\n%s\n};"
|
|
|
|
, Name
|
2023-06-28 11:43:21 -07:00
|
|
|
, access_level
|
2023-07-14 21:03:14 -07:00
|
|
|
, ParentType->to_string()
|
|
|
|
, Body->to_string()
|
2023-06-28 11:43:21 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt( "%s \n{\n%s\n};", Name, Body->to_string() );
|
2023-06-28 11:43:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt( "class %s\n{\n%s\n};", Name, Body->to_string() );
|
2023-06-28 11:43:21 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
case Class_Fwd:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "class %s %s;", Attributes->to_string(), Name );
|
2023-07-13 22:37:22 -07:00
|
|
|
|
|
|
|
else result.append_fmt( "class %s;", Name );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Enum:
|
2023-05-07 12:03:24 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes || UnderlyingType )
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-07-13 22:37:22 -07:00
|
|
|
result.append( "enum " );
|
2023-06-28 18:20:29 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-06-28 18:20:29 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( UnderlyingType )
|
2023-07-15 19:27:38 -07:00
|
|
|
result.append_fmt( "%s : %s\n{\n%s\n};"
|
2023-06-28 18:20:29 -07:00
|
|
|
, Name
|
2023-07-14 21:03:14 -07:00
|
|
|
, UnderlyingType->to_string()
|
2023-07-15 19:27:38 -07:00
|
|
|
, Body->to_string()
|
2023-06-28 18:20:29 -07:00
|
|
|
);
|
2023-07-13 22:37:22 -07:00
|
|
|
|
|
|
|
else result.append_fmt( "%s\n{\n%s\n};"
|
2023-04-22 21:43:31 -07:00
|
|
|
, Name
|
2023-07-14 21:03:14 -07:00
|
|
|
, Body->to_string()
|
2023-04-22 21:43:31 -07:00
|
|
|
);
|
|
|
|
}
|
2023-07-13 22:37:22 -07:00
|
|
|
else result.append_fmt( "enum %s\n{\n%s\n};"
|
|
|
|
, Name
|
2023-07-14 21:03:14 -07:00
|
|
|
, Body->to_string()
|
2023-05-05 21:18:44 -07:00
|
|
|
);
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
case Enum_Fwd:
|
2023-06-28 18:20:29 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-07-13 22:37:22 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt( "enum %s : %s;", Name, UnderlyingType->to_string() );
|
2023-06-28 18:20:29 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Enum_Class:
|
2023-05-07 12:03:24 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes || UnderlyingType )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-07-13 22:37:22 -07:00
|
|
|
result.append( "enum class " );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes )
|
2023-06-29 19:48:47 -07:00
|
|
|
{
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-06-29 19:48:47 -07:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( UnderlyingType )
|
2023-06-29 19:48:47 -07:00
|
|
|
{
|
2023-07-13 22:37:22 -07:00
|
|
|
result.append_fmt( "%s : %s\n{\n%s\n};"
|
2023-06-29 19:48:47 -07:00
|
|
|
, Name
|
2023-07-14 21:03:14 -07:00
|
|
|
, UnderlyingType->to_string()
|
|
|
|
, Body->to_string()
|
2023-06-29 19:48:47 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-15 19:27:38 -07:00
|
|
|
result.append_fmt( "%s\n{\n%s\n};"
|
2023-06-29 19:48:47 -07:00
|
|
|
, Name
|
2023-07-14 21:03:14 -07:00
|
|
|
, Body->to_string()
|
2023-06-29 19:48:47 -07:00
|
|
|
);
|
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-07-13 22:37:22 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
result.append_fmt( "enum class %s\n{\n%s\n};"
|
2023-07-14 21:03:14 -07:00
|
|
|
, Body->to_string()
|
2023-07-13 22:37:22 -07:00
|
|
|
);
|
|
|
|
}
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
case Enum_Class_Fwd:
|
2023-05-07 12:03:24 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
result.append( "enum class " );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt( "%s : %s;", Name, UnderlyingType->to_string() );
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
case Export_Body:
|
|
|
|
{
|
2023-07-08 09:21:26 -07:00
|
|
|
result.append_fmt( "export\n{\n" );
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
Code curr = cast<Code>();
|
|
|
|
s32 left = NumEntries;
|
|
|
|
while ( left-- )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append_fmt( "%s\n", curr.to_string() );
|
2023-07-14 21:03:14 -07:00
|
|
|
++curr;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-07-08 09:21:26 -07:00
|
|
|
result.append_fmt( "};" );
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
case Extern_Linkage:
|
2023-07-08 09:21:26 -07:00
|
|
|
result.append_fmt( "extern \"%s\"\n{\n%s\n}"
|
2023-05-05 21:18:44 -07:00
|
|
|
, Name
|
2023-07-14 21:03:14 -07:00
|
|
|
, Body->to_string()
|
2023-05-05 21:18:44 -07:00
|
|
|
);
|
2023-04-23 16:04:43 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
case Friend:
|
2023-07-15 19:27:38 -07:00
|
|
|
result.append_fmt( "friend %s", Declaration->to_string() );
|
2023-07-15 20:07:30 -07:00
|
|
|
|
|
|
|
if ( result[ result.length() -1 ] != ';' )
|
|
|
|
result.append( ";" );
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Function:
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-06-28 21:20:23 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Specs )
|
|
|
|
result.append_fmt( "%s\n", Specs->to_string() );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( ReturnType )
|
2023-07-15 19:27:38 -07:00
|
|
|
result.append_fmt( "%s %s(", ReturnType->to_string(), Name );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
else
|
|
|
|
result.append_fmt( "%s(", Name );
|
2023-06-28 21:20:23 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Params )
|
|
|
|
result.append_fmt( "%s", Params->to_string() );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
else
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append( "void" );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-07-08 09:21:26 -07:00
|
|
|
result.append_fmt( ")\n{\n%s\n}"
|
2023-07-14 21:03:14 -07:00
|
|
|
, Body->to_string()
|
2023-05-05 21:18:44 -07:00
|
|
|
);
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
case Function_Fwd:
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
ProcessModuleFlags();
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-06-28 11:43:21 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Specs )
|
|
|
|
result.append_fmt( "%s\n", Specs->to_string() );
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( ReturnType )
|
|
|
|
result.append_fmt( "%s %s(", ReturnType->to_string(), Name );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
else
|
|
|
|
result.append_fmt( "%s(", Name );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Params )
|
|
|
|
result.append_fmt( "%s", Params->to_string() );
|
2023-06-28 11:43:21 -07:00
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
else
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( "void" );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append( ");" );
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
case Module:
|
2023-06-28 21:20:23 -07:00
|
|
|
if (((u32(ModuleFlag::Export) & u32(ModuleFlags)) == u32(ModuleFlag::Export)))
|
|
|
|
result.append("export ");
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if (((u32(ModuleFlag::Import) & u32(ModuleFlags)) == u32(ModuleFlag::Import)))
|
|
|
|
result.append("import ");
|
|
|
|
|
|
|
|
result.append_fmt( "%s;", Name );
|
|
|
|
break;
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
case Namespace:
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
result.append_fmt( "namespace %s\n{\n%s}"
|
2023-05-05 21:18:44 -07:00
|
|
|
, Name
|
2023-07-14 21:03:14 -07:00
|
|
|
, Body->to_string()
|
2023-05-05 21:18:44 -07:00
|
|
|
);
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
case Operator:
|
|
|
|
case Operator_Member:
|
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "%s\n", Attributes->to_string() );
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( ReturnType )
|
|
|
|
result.append_fmt( "%s %s (", ReturnType->to_string(), Name );
|
2023-07-13 22:37:22 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Params )
|
|
|
|
result.append_fmt( "%s", Params->to_string() );
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
else
|
2023-07-13 22:37:22 -07:00
|
|
|
result.append( "void" );
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-07-08 09:21:26 -07:00
|
|
|
result.append_fmt( ")\n{\n%s\n}"
|
2023-07-14 21:03:14 -07:00
|
|
|
, Body->to_string()
|
2023-05-05 21:18:44 -07:00
|
|
|
);
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
case Operator_Fwd:
|
2023-04-22 21:43:31 -07:00
|
|
|
case Operator_Member_Fwd:
|
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Specs )
|
|
|
|
result.append_fmt( "%s", Specs->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt( "%s %s (", ReturnType->to_string(), Name );
|
2023-07-13 22:37:22 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Params )
|
|
|
|
result.append_fmt( "%s);", Params->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
|
|
|
|
else
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "void);" );
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
case Operator_Cast:
|
2023-07-13 22:37:22 -07:00
|
|
|
{
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt("operator %s(){\n%s\n}", ValueType->to_string(), Body->to_string() );
|
2023-07-13 22:37:22 -07:00
|
|
|
}
|
2023-07-10 19:14:41 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Operator_Cast_Fwd:
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt("operator %s();", ValueType->to_string() );
|
2023-07-10 19:14:41 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
case Parameters:
|
|
|
|
{
|
2023-07-10 22:09:50 -07:00
|
|
|
if ( Name )
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt( "%s %s", ValueType->to_string(), Name );
|
2023-07-13 22:37:22 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
else
|
|
|
|
result.append_fmt( "%s", ValueType->to_string() );
|
2023-07-10 22:09:50 -07:00
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
if ( Value )
|
|
|
|
result.append_fmt( "= %s", Value->to_string() );
|
|
|
|
|
2023-07-15 20:07:30 -07:00
|
|
|
if ( NumEntries - 1 > 0)
|
2023-07-15 17:56:27 -07:00
|
|
|
{
|
|
|
|
for ( CodeParam param : Next->cast<CodeParam>() )
|
2023-07-14 21:03:14 -07:00
|
|
|
{
|
2023-07-15 17:56:27 -07:00
|
|
|
result.append_fmt( ", %s", param.to_string() );
|
2023-07-14 21:03:14 -07:00
|
|
|
}
|
2023-07-15 17:56:27 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
case Preprocessor_Include:
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "#include \"%s\"", Name );
|
2023-05-01 11:12:07 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
case Specifiers:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
|
|
|
s32 idx = 0;
|
2023-07-14 21:03:14 -07:00
|
|
|
s32 left = NumEntries;
|
2023-06-30 00:13:41 -07:00
|
|
|
while ( left-- )
|
|
|
|
{
|
|
|
|
result.append_fmt( "%s ", (char const*)ESpecifier::to_str( ArrSpecs[idx]) );
|
|
|
|
idx++;
|
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Struct:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
if ( Name == nullptr)
|
|
|
|
{
|
|
|
|
result.append( "struct\n{\n%s\n};", Body->to_string() );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes || ParentType )
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-07-13 22:37:22 -07:00
|
|
|
result.append( "struct " );
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
if ( Attributes )
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
|
|
|
|
|
|
|
if ( ParentType )
|
2023-06-30 00:13:41 -07:00
|
|
|
{
|
2023-07-13 22:37:22 -07:00
|
|
|
char const* access_level = to_str( ParentAccess );
|
2023-06-30 00:13:41 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
result.append_fmt( "%s : %s %s\n{\n%s\n};"
|
|
|
|
, Name
|
|
|
|
, access_level
|
2023-07-14 21:03:14 -07:00
|
|
|
, ParentType->to_string()
|
|
|
|
, Body->to_string()
|
2023-07-13 22:37:22 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
2023-06-30 00:13:41 -07:00
|
|
|
{
|
2023-07-15 23:26:55 -07:00
|
|
|
if ( Name )
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt( "%s \n{\n%s\n};", Name, Body->to_string() );
|
2023-06-30 00:13:41 -07:00
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-07-13 22:37:22 -07:00
|
|
|
else
|
|
|
|
{
|
2023-07-14 21:03:14 -07:00
|
|
|
result.append_fmt( "struct %s\n{\n%s\n};", Name, Body->to_string() );
|
2023-07-13 22:37:22 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
case Struct_Fwd:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "struct %s %s;", Attributes->to_string(), Name );
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
else result.append_fmt( "struct %s;", Name );
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
case Template:
|
|
|
|
{
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append_fmt( "template< %s >\n%s", Params->to_string(), Declaration->to_string() );
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
case Typedef:
|
2023-05-07 12:03:24 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
result.append( "typedef ");
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( Attributes )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append_fmt( "%s %s", UnderlyingType->to_string(), Name );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( UnderlyingType->ArrExpr )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append_fmt( "[%s];", UnderlyingType->ArrExpr->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( ";" );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Typename:
|
2023-07-13 22:37:22 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( Attributes || Specs )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( Specs )
|
|
|
|
result.append_fmt( "%s %s", Name, Specs->to_string() );
|
2023-07-14 21:03:14 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
else
|
|
|
|
result.append_fmt( "%s", Name );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append_fmt( "%s", Name );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-07-14 21:03:14 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
case Union:
|
2023-05-07 12:03:24 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
result.append( "union " );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
if ( Name )
|
|
|
|
{
|
|
|
|
result.append_fmt( "%s\n{\n%s\n};"
|
|
|
|
, Name
|
2023-07-15 12:20:55 -07:00
|
|
|
, Body->to_string()
|
2023-07-09 22:15:25 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Anonymous union
|
|
|
|
result.append_fmt( "\n{\n%s\n};"
|
2023-07-15 12:20:55 -07:00
|
|
|
, Body->to_string()
|
2023-07-09 22:15:25 -07:00
|
|
|
);
|
|
|
|
}
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
2023-05-01 11:12:07 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-06 16:19:11 -07:00
|
|
|
case Using:
|
2023-05-07 12:03:24 -07:00
|
|
|
{
|
2023-05-05 21:18:44 -07:00
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "%s ", Attributes->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( UnderlyingType )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append_fmt( "using %s = %s", Name, UnderlyingType->to_string() );
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( UnderlyingType->ArrExpr )
|
|
|
|
result.append_fmt( "[%s]", UnderlyingType->ArrExpr->to_string() );
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append( ";" );
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
else
|
2023-06-28 21:20:23 -07:00
|
|
|
result.append_fmt( "using %s;", Name );
|
2023-05-07 12:03:24 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Using_Namespace:
|
2023-07-08 09:21:26 -07:00
|
|
|
result.append_fmt( "using namespace %s;", Name );
|
2023-04-22 21:43:31 -07:00
|
|
|
break;
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
case Variable:
|
|
|
|
{
|
|
|
|
ProcessModuleFlags();
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( Attributes || Specs )
|
2023-07-14 21:03:14 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( Attributes )
|
|
|
|
result.append_fmt( "%s ", Specs->to_string() );
|
2023-07-14 21:03:14 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( Specs )
|
|
|
|
result.append_fmt( "%s\n", Specs->to_string() );
|
2023-07-14 21:03:14 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append_fmt( "%s %s", ValueType->to_string(), Name );
|
2023-07-14 21:03:14 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( ValueType->ArrExpr )
|
|
|
|
result.append_fmt( "[%s]", ValueType->ArrExpr->to_string() );
|
2023-07-14 21:03:14 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( Value )
|
2023-07-15 19:27:38 -07:00
|
|
|
result.append_fmt( " = %s", Value->to_string() );
|
|
|
|
|
|
|
|
result.append( ";" );
|
2023-07-14 21:03:14 -07:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( UnderlyingType->ArrExpr )
|
|
|
|
result.append_fmt( "%s %s[%s];", UnderlyingType->to_string(), Name, UnderlyingType->ArrExpr->to_string() );
|
2023-07-14 21:03:14 -07:00
|
|
|
|
|
|
|
else
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append_fmt( "%s %s;", UnderlyingType->to_string(), Name );
|
2023-07-14 21:03:14 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
case Class_Body:
|
|
|
|
case Enum_Body:
|
2023-04-23 16:04:43 -07:00
|
|
|
case Extern_Linkage_Body:
|
2023-04-22 21:43:31 -07:00
|
|
|
case Function_Body:
|
|
|
|
case Global_Body:
|
|
|
|
case Namespace_Body:
|
|
|
|
case Struct_Body:
|
2023-05-01 11:12:07 -07:00
|
|
|
case Union_Body:
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
2023-07-14 21:03:14 -07:00
|
|
|
Code curr = Front->cast<Code>();
|
|
|
|
s32 left = NumEntries;
|
2023-04-22 21:43:31 -07:00
|
|
|
while ( left -- )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append_fmt( "%s\n", curr.to_string() );
|
2023-07-14 21:03:14 -07:00
|
|
|
++curr;
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-07-16 15:00:07 -07:00
|
|
|
#if defined(GEN_BENCHMARK) && defined(GEN_BENCHMARK_SERIALIZATION)
|
|
|
|
log_fmt("AST::to_string() time taken: %llu for: %s\n", time_rel_ms() - time_start, result );
|
|
|
|
#endif
|
2023-04-06 16:19:11 -07:00
|
|
|
return result;
|
2023-05-05 21:18:44 -07:00
|
|
|
#undef ProcessModuleFlags
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
2023-06-29 19:48:47 -07:00
|
|
|
|
|
|
|
bool AST::is_equal( AST* other )
|
|
|
|
{
|
|
|
|
if ( Type != other->Type )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
switch ( Type )
|
|
|
|
{
|
|
|
|
case ECode::Typedef:
|
|
|
|
case ECode::Typename:
|
|
|
|
{
|
|
|
|
if ( Name != other->Name )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( Name != other->Name )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-07-12 12:59:47 -07:00
|
|
|
|
|
|
|
bool AST::validate_body()
|
|
|
|
{
|
|
|
|
using namespace ECode;
|
2023-07-13 22:37:22 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
#define CheckEntries( Unallowed_Types ) \
|
|
|
|
do \
|
|
|
|
{ \
|
2023-07-15 12:20:55 -07:00
|
|
|
for ( Code entry : cast<CodeBody>() ) \
|
2023-07-14 21:03:14 -07:00
|
|
|
{ \
|
|
|
|
switch ( entry->Type ) \
|
|
|
|
{ \
|
|
|
|
Unallowed_Types \
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "AST::validate_body: Invalid entry in body %s", entry.debug_str() ); \
|
2023-07-14 21:03:14 -07:00
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} \
|
2023-07-12 12:59:47 -07:00
|
|
|
while (0);
|
|
|
|
|
|
|
|
switch ( Type )
|
|
|
|
{
|
|
|
|
case Class_Body:
|
|
|
|
CheckEntries( AST_BODY_CLASS_UNALLOWED_TYPES );
|
|
|
|
break;
|
|
|
|
case Enum_Body:
|
2023-07-15 12:20:55 -07:00
|
|
|
for ( Code entry : cast<CodeBody>() )
|
2023-07-12 12:59:47 -07:00
|
|
|
{
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( entry->Type != Untyped )
|
2023-07-12 12:59:47 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "AST::validate_body: Invalid entry in enum body (needs to be untyped or comment) %s", entry.debug_str() );
|
2023-07-12 12:59:47 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Export_Body:
|
2023-07-15 12:20:55 -07:00
|
|
|
CheckEntries( AST_BODY_CLASS_UNALLOWED_TYPES );
|
2023-07-12 12:59:47 -07:00
|
|
|
break;
|
|
|
|
case Extern_Linkage:
|
|
|
|
CheckEntries( AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES );
|
|
|
|
break;
|
|
|
|
case Function_Body:
|
|
|
|
CheckEntries( AST_BODY_FUNCTION_UNALLOWED_TYPES );
|
|
|
|
break;
|
|
|
|
case Global_Body:
|
2023-07-15 12:20:55 -07:00
|
|
|
CheckEntries( AST_BODY_GLOBAL_UNALLOWED_TYPES );
|
2023-07-12 12:59:47 -07:00
|
|
|
break;
|
|
|
|
case Namespace_Body:
|
|
|
|
CheckEntries( AST_BODY_NAMESPACE_UNALLOWED_TYPES );
|
|
|
|
break;
|
|
|
|
case Struct_Body:
|
|
|
|
CheckEntries( AST_BODY_STRUCT_UNALLOWED_TYPES );
|
|
|
|
break;
|
|
|
|
case Union_Body:
|
2023-07-15 12:20:55 -07:00
|
|
|
for ( Code entry : Body->cast<CodeBody>() )
|
2023-07-12 12:59:47 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( entry->Type != Untyped )
|
2023-07-12 12:59:47 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "AST::validate_body: Invalid entry in union body (needs to be untyped or comment) %s", entry.debug_str() );
|
2023-07-12 12:59:47 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
log_failure( "AST::validate_body: Invalid this AST does not have a body %s", debug_str() );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion AST
|
2023-04-06 16:19:11 -07:00
|
|
|
|
|
|
|
#pragma region Gen Interface
|
2023-04-03 00:55:28 -07:00
|
|
|
void init()
|
2023-04-01 22:07:44 -07:00
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
using namespace StaticData;
|
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
Memory::setup();
|
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
// Setup the arrays
|
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
CodePools = Array<Pool>::init_reserve( Allocator_DataArrays, InitSize_DataArrays );
|
|
|
|
|
|
|
|
if ( CodePools == nullptr )
|
2023-04-09 21:38:47 -07:00
|
|
|
fatal( "gen::init: Failed to initialize the CodePools array" );
|
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
StringArenas = Array<Arena>::init_reserve( Allocator_DataArrays, InitSize_DataArrays );
|
|
|
|
|
|
|
|
if ( StringArenas == nullptr )
|
2023-04-09 21:38:47 -07:00
|
|
|
fatal( "gen::init: Failed to initialize the StringArenas array" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the code pool and code entries arena.
|
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
Pool code_pool = Pool::init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) );
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
if ( code_pool.PhysicalStart == nullptr )
|
2023-04-09 21:38:47 -07:00
|
|
|
fatal( "gen::init: Failed to initialize the code pool" );
|
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
CodePools.append( code_pool );
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
#ifdef GEN_FEATURE_PARSING
|
|
|
|
LexArena = Arena::init_from_allocator( Allocator_Lexer, LexAllocator_Size );
|
|
|
|
#endif
|
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
Arena string_arena = Arena::init_from_allocator( Allocator_StringArena, SizePer_StringArena );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
if ( string_arena.PhysicalStart == nullptr )
|
2023-04-09 21:38:47 -07:00
|
|
|
fatal( "gen::init: Failed to initialize the string arena" );
|
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
StringArenas.append( string_arena );
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the hash tables
|
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
StringCache = StringTable::init( Allocator_StringTable );
|
|
|
|
|
|
|
|
if ( StringCache.Entries == nullptr )
|
|
|
|
fatal( "gen::init: Failed to initialize the StringCache");
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-07-08 09:21:26 -07:00
|
|
|
Code::Global = make_code();
|
2023-07-11 22:33:11 -07:00
|
|
|
Code::Global->Name = get_cached_string( txt_StrC("Global Code") );
|
2023-07-08 09:21:26 -07:00
|
|
|
Code::Global->Content = Code::Global->Name;
|
2023-06-30 08:54:27 -07:00
|
|
|
|
2023-07-08 09:21:26 -07:00
|
|
|
Code::Invalid = make_code();
|
|
|
|
Code::Invalid.set_global();
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-08 09:21:26 -07:00
|
|
|
# define def_constant_code_type( Type_ ) \
|
2023-07-08 20:29:18 -07:00
|
|
|
t_##Type_ = def_type( name(Type_) ); \
|
|
|
|
t_##Type_.set_global();
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-08 09:21:26 -07:00
|
|
|
def_constant_code_type( auto );
|
|
|
|
def_constant_code_type( void );
|
2023-04-10 18:33:06 -07:00
|
|
|
def_constant_code_type( int );
|
2023-04-03 23:04:19 -07:00
|
|
|
def_constant_code_type( bool );
|
|
|
|
def_constant_code_type( char );
|
|
|
|
def_constant_code_type( wchar_t );
|
2023-07-09 22:15:25 -07:00
|
|
|
def_constant_code_type( class );
|
|
|
|
def_constant_code_type( typename );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
#ifdef GEN_DEFINE_LIBRARY_CODE_CONSTANTS
|
2023-07-08 20:29:18 -07:00
|
|
|
t_b32 = def_type( name(b32) );
|
2023-06-30 21:23:40 -07:00
|
|
|
|
2023-04-03 23:04:19 -07:00
|
|
|
def_constant_code_type( s8 );
|
|
|
|
def_constant_code_type( s16 );
|
|
|
|
def_constant_code_type( s32 );
|
|
|
|
def_constant_code_type( s64 );
|
|
|
|
|
|
|
|
def_constant_code_type( u8 );
|
|
|
|
def_constant_code_type( u16 );
|
|
|
|
def_constant_code_type( u32 );
|
|
|
|
def_constant_code_type( u64 );
|
|
|
|
|
|
|
|
def_constant_code_type( sw );
|
|
|
|
def_constant_code_type( uw );
|
|
|
|
|
|
|
|
def_constant_code_type( f32 );
|
|
|
|
def_constant_code_type( f64 );
|
2023-04-22 19:24:55 -07:00
|
|
|
#endif
|
2023-05-09 18:54:54 -07:00
|
|
|
# undef def_constant_code_type
|
|
|
|
|
2023-07-08 09:21:26 -07:00
|
|
|
access_private = make_code();
|
|
|
|
access_private->Type = ECode::Access_Private;
|
2023-07-11 22:33:11 -07:00
|
|
|
access_private->Name = get_cached_string( txt_StrC("private:") );
|
2023-07-08 09:21:26 -07:00
|
|
|
access_private.set_global();
|
|
|
|
|
|
|
|
access_protected = make_code();
|
|
|
|
access_protected->Type = ECode::Access_Protected;
|
2023-07-11 22:33:11 -07:00
|
|
|
access_protected->Name = get_cached_string( txt_StrC("protected:") );
|
2023-07-08 09:21:26 -07:00
|
|
|
access_protected.set_global();
|
|
|
|
|
|
|
|
access_public = make_code();
|
|
|
|
access_public->Type = ECode::Access_Public;
|
2023-07-11 22:33:11 -07:00
|
|
|
access_public->Name = get_cached_string( txt_StrC("public:") );
|
2023-07-08 09:21:26 -07:00
|
|
|
access_public.set_global();
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
module_global_fragment = make_code();
|
|
|
|
module_global_fragment->Type = ECode::Untyped;
|
2023-07-11 22:33:11 -07:00
|
|
|
module_global_fragment->Name = get_cached_string( txt_StrC("module;") );
|
2023-06-28 21:20:23 -07:00
|
|
|
module_global_fragment->Content = module_global_fragment->Name;
|
2023-07-08 09:21:26 -07:00
|
|
|
module_global_fragment.set_global();
|
2023-06-28 21:20:23 -07:00
|
|
|
|
|
|
|
module_private_fragment = make_code();
|
|
|
|
module_private_fragment->Type = ECode::Untyped;
|
2023-07-11 22:33:11 -07:00
|
|
|
module_private_fragment->Name = get_cached_string( txt_StrC("module : private;") );
|
2023-06-28 21:20:23 -07:00
|
|
|
module_private_fragment->Content = module_private_fragment->Name;
|
2023-07-08 09:21:26 -07:00
|
|
|
module_private_fragment.set_global();
|
2023-06-28 21:20:23 -07:00
|
|
|
|
2023-06-28 22:37:42 -07:00
|
|
|
pragma_once = make_code();
|
|
|
|
pragma_once->Type = ECode::Untyped;
|
2023-07-11 22:33:11 -07:00
|
|
|
pragma_once->Name = get_cached_string( txt_StrC("#pragma once") );
|
2023-06-28 22:37:42 -07:00
|
|
|
pragma_once->Content = pragma_once->Name;
|
2023-07-08 09:21:26 -07:00
|
|
|
pragma_once.set_global();
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-08 20:29:18 -07:00
|
|
|
# pragma push_macro( "global" )
|
|
|
|
# pragma push_macro( "internal" )
|
|
|
|
# pragma push_macro( "local_persist" )
|
|
|
|
# define global global
|
|
|
|
# define internal internal
|
|
|
|
# define local_persist local_persist
|
|
|
|
|
2023-07-08 09:21:26 -07:00
|
|
|
# define def_constant_spec( Type_, ... ) \
|
2023-07-13 16:28:25 -07:00
|
|
|
spec_##Type_ = def_specifiers( num_args(__VA_ARGS__), __VA_ARGS__); \
|
2023-07-08 09:21:26 -07:00
|
|
|
spec_##Type_.set_global();
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-08 20:29:18 -07:00
|
|
|
def_constant_spec( const, ESpecifier::Const );
|
2023-05-09 18:54:54 -07:00
|
|
|
def_constant_spec( consteval, ESpecifier::Consteval );
|
|
|
|
def_constant_spec( constexpr, ESpecifier::Constexpr );
|
|
|
|
def_constant_spec( constinit, ESpecifier::Constinit );
|
|
|
|
def_constant_spec( extern_linkage, ESpecifier::External_Linkage );
|
2023-07-08 20:29:18 -07:00
|
|
|
def_constant_spec( global, ESpecifier::Global );
|
2023-05-09 18:54:54 -07:00
|
|
|
def_constant_spec( inline, ESpecifier::Inline );
|
|
|
|
def_constant_spec( internal_linkage, ESpecifier::Internal_Linkage );
|
2023-07-08 20:29:18 -07:00
|
|
|
def_constant_spec( local_persist, ESpecifier::Local_Persist );
|
2023-05-09 18:54:54 -07:00
|
|
|
def_constant_spec( mutable, ESpecifier::Mutable );
|
|
|
|
def_constant_spec( ptr, ESpecifier::Ptr );
|
|
|
|
def_constant_spec( ref, ESpecifier::Ref );
|
|
|
|
def_constant_spec( register, ESpecifier::Register );
|
|
|
|
def_constant_spec( rvalue, ESpecifier::RValue );
|
2023-07-15 23:26:55 -07:00
|
|
|
def_constant_spec( static_member, ESpecifier::Static );
|
2023-05-09 18:54:54 -07:00
|
|
|
def_constant_spec( thread_local, ESpecifier::Thread_Local );
|
|
|
|
def_constant_spec( volatile, ESpecifier::Volatile)
|
2023-07-08 09:21:26 -07:00
|
|
|
|
|
|
|
spec_local_persist = def_specifiers( 1, ESpecifier::Local_Persist );
|
|
|
|
spec_local_persist.set_global();
|
|
|
|
|
2023-07-08 20:29:18 -07:00
|
|
|
# pragma pop_macro( "global" )
|
|
|
|
# pragma pop_macro( "internal" )
|
|
|
|
# pragma pop_macro( "local_persist" )
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
# undef def_constant_spec
|
2023-04-01 22:07:44 -07:00
|
|
|
}
|
|
|
|
|
2023-05-08 17:54:24 -07:00
|
|
|
void deinit()
|
|
|
|
{
|
|
|
|
using namespace StaticData;
|
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
s32 index = 0;
|
2023-07-11 15:29:45 -07:00
|
|
|
s32 left = CodePools.num();
|
2023-05-08 17:54:24 -07:00
|
|
|
do
|
|
|
|
{
|
2023-05-09 18:54:54 -07:00
|
|
|
Pool* code_pool = & CodePools[index];
|
2023-07-11 15:29:45 -07:00
|
|
|
code_pool->free();
|
2023-05-09 18:54:54 -07:00
|
|
|
index++;
|
2023-05-08 17:54:24 -07:00
|
|
|
}
|
|
|
|
while ( left--, left );
|
|
|
|
|
2023-05-09 18:54:54 -07:00
|
|
|
index = 0;
|
2023-07-11 15:29:45 -07:00
|
|
|
left = StringArenas.num();
|
2023-05-08 17:54:24 -07:00
|
|
|
do
|
|
|
|
{
|
2023-05-09 18:54:54 -07:00
|
|
|
Arena* string_arena = & StringArenas[index];
|
2023-07-11 15:29:45 -07:00
|
|
|
string_arena->free();
|
2023-05-09 18:54:54 -07:00
|
|
|
index++;
|
2023-05-08 17:54:24 -07:00
|
|
|
}
|
|
|
|
while ( left--, left );
|
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
StringCache.destroy();
|
2023-05-08 17:54:24 -07:00
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
CodePools.free();
|
|
|
|
StringArenas.free();
|
2023-07-15 23:26:55 -07:00
|
|
|
|
|
|
|
#ifdef GEN_FEATURE_PARSING
|
|
|
|
LexArena.free();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
Memory::cleanup();
|
2023-05-08 17:54:24 -07:00
|
|
|
}
|
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
AllocatorInfo get_string_allocator( s32 str_length )
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
|
|
|
using namespace StaticData;
|
|
|
|
|
2023-07-16 15:00:07 -07:00
|
|
|
Arena* last = & StringArenas.back();
|
|
|
|
|
|
|
|
uw size_req = str_length + sizeof(String::Header) + sizeof(char*);
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-16 15:00:07 -07:00
|
|
|
if ( last->TotalUsed + size_req > last->TotalSize )
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
Arena new_arena = Arena::init_from_allocator( Allocator_StringArena, SizePer_StringArena );
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
if ( ! StringArenas.append( new_arena ) )
|
2023-04-09 21:38:47 -07:00
|
|
|
fatal( "gen::get_string_allocator: Failed to allocate a new string arena" );
|
2023-04-01 22:07:44 -07:00
|
|
|
|
2023-07-16 15:00:07 -07:00
|
|
|
last = & StringArenas.back();
|
2023-04-05 00:03:56 -07:00
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-07-16 15:00:07 -07:00
|
|
|
return * last;
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
|
|
|
|
2023-04-05 00:03:56 -07:00
|
|
|
// Will either make or retrive a code string.
|
2023-05-06 12:49:43 -07:00
|
|
|
StringCached get_cached_string( StrC str )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
using namespace StaticData;
|
|
|
|
|
2023-05-08 17:54:24 -07:00
|
|
|
s32 hash_length = str.Len > kilobytes(1) ? kilobytes(1) : str.Len;
|
2023-07-16 15:00:07 -07:00
|
|
|
u64 key = crc32( str.Ptr, hash_length );
|
2023-05-08 17:54:24 -07:00
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
StringCached* result = StringCache.get( key );
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-05-08 17:54:24 -07:00
|
|
|
if ( result )
|
|
|
|
return * result;
|
|
|
|
}
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-07-08 14:14:05 -07:00
|
|
|
String result = String::make( get_string_allocator( str.Len ), str );
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
StringCache.set( key, result );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-05-08 17:54:24 -07:00
|
|
|
return result;
|
2023-04-05 00:03:56 -07:00
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
/*
|
|
|
|
Used internally to retireve a Code object form the CodePool.
|
|
|
|
*/
|
|
|
|
Code make_code()
|
|
|
|
{
|
|
|
|
using namespace StaticData;
|
|
|
|
|
2023-07-16 15:00:07 -07:00
|
|
|
Pool* allocator = & CodePools.back();
|
|
|
|
if ( allocator->FreeList == nullptr )
|
2023-04-09 21:38:47 -07:00
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
Pool code_pool = Pool::init( Allocator_CodePool, CodePool_NumBlocks, sizeof(AST) );
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
if ( code_pool.PhysicalStart == nullptr )
|
2023-04-09 21:38:47 -07:00
|
|
|
fatal( "gen::make_code: Failed to allocate a new code pool - CodePool allcoator returned nullptr." );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
if ( ! CodePools.append( code_pool ) )
|
2023-04-09 21:38:47 -07:00
|
|
|
fatal( "gen::make_code: Failed to allocate a new code pool - CodePools failed to append new pool." );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-07-16 15:00:07 -07:00
|
|
|
allocator = & CodePools.back();
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-07-16 15:00:07 -07:00
|
|
|
Code result { rcast( AST*, alloc( * allocator, sizeof(AST) )) };
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Content = { nullptr };
|
|
|
|
result->Prev = { nullptr };
|
|
|
|
result->Next = { nullptr };
|
|
|
|
result->Parent = { nullptr };
|
|
|
|
result->Name = { nullptr };
|
|
|
|
result->Type = ECode::Invalid;
|
|
|
|
result->ModuleFlags = ModuleFlag::Invalid;
|
2023-07-14 21:03:14 -07:00
|
|
|
result->NumEntries = 0;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
return result;
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
enum class OpValidateResult : u32
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
Fail,
|
|
|
|
Global,
|
|
|
|
Member
|
|
|
|
};
|
2023-04-05 00:03:56 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
inline
|
2023-07-15 12:20:55 -07:00
|
|
|
OpValidateResult operator__validate( OperatorT op, CodeParam params_code, CodeType ret_type, CodeSpecifier specifier )
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
using namespace EOperator;
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( op == EOperator::Invalid )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: op cannot be invalid");
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma region Helper Macros
|
2023-07-15 12:20:55 -07:00
|
|
|
# define check_params() \
|
|
|
|
if ( ! params_code ) \
|
|
|
|
{ \
|
|
|
|
log_failure("gen::def_operator: params is null and operator%s requires it", to_str(op)); \
|
|
|
|
return OpValidateResult::Fail; \
|
|
|
|
} \
|
|
|
|
if ( params_code->Type != ECode::Parameters ) \
|
|
|
|
{ \
|
|
|
|
log_failure("gen::def_operator: params is not of Parameters type - %s", params_code.debug_str()); \
|
|
|
|
return OpValidateResult::Fail; \
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
# define check_param_eq_ret() \
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( ! is_member_symbol && params_code->ValueType != ret_type ) \
|
2023-04-11 19:18:02 -07:00
|
|
|
{ \
|
|
|
|
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), \
|
2023-07-15 12:20:55 -07:00
|
|
|
params_code.debug_str(), \
|
|
|
|
ret_type.debug_str() \
|
2023-04-11 19:18:02 -07:00
|
|
|
); \
|
|
|
|
return OpValidateResult::Fail; \
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion Helper Macros
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( ! ret_type )
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
log_failure("gen::def_operator: ret_type is null but is required by operator%s", to_str(op));
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( ret_type->Type != ECode::Typename )
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_operator: ret_type is not of typename type - %s", ret_type.debug_str());
|
2023-04-11 19:18:02 -07:00
|
|
|
return OpValidateResult::Fail;
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
bool is_member_symbol = false;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
switch ( op )
|
2023-04-06 16:19:11 -07:00
|
|
|
{
|
2023-07-13 16:28:25 -07:00
|
|
|
# define specs( ... ) num_args( __VA_ARGS__ ), __VA_ARGS__
|
2023-04-11 19:18:02 -07:00
|
|
|
case Assign:
|
|
|
|
check_params();
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( params_code->NumEntries > 1 )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: "
|
|
|
|
"operator%s does not support non-member definition (more than one parameter provided) - %s",
|
|
|
|
to_str(op),
|
2023-07-15 12:20:55 -07:00
|
|
|
params_code.debug_str()
|
2023-04-11 19:18:02 -07:00
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
is_member_symbol = true;
|
|
|
|
break;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
case Assign_Add:
|
|
|
|
case Assign_Subtract:
|
2023-07-10 19:14:41 -07:00
|
|
|
case Assign_Multiply:
|
|
|
|
case Assign_Divide:
|
|
|
|
case Assign_Modulo:
|
|
|
|
case Assign_BAnd:
|
|
|
|
case Assign_BOr:
|
2023-04-11 19:18:02 -07:00
|
|
|
case Assign_BXOr:
|
|
|
|
case Assign_LShift:
|
|
|
|
case Assign_RShift:
|
|
|
|
check_params();
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( params_code->NumEntries == 1 )
|
2023-04-11 19:18:02 -07:00
|
|
|
is_member_symbol = true;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
else
|
|
|
|
check_param_eq_ret();
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if (params_code->NumEntries > 2 )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s may not be defined with more than two parametes - param count; %d\n%s"
|
|
|
|
, to_str(op)
|
2023-07-15 12:20:55 -07:00
|
|
|
, params_code->NumEntries
|
|
|
|
, params_code.debug_str()
|
2023-04-11 19:18:02 -07:00
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
case Increment:
|
|
|
|
case Decrement:
|
|
|
|
// If its not set, it just means its a prefix member op.
|
|
|
|
if ( params_code )
|
|
|
|
{
|
|
|
|
if ( params_code->Type != ECode::Parameters )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s params code provided is not of Parameters type - %s"
|
|
|
|
, to_str(op)
|
2023-07-15 12:20:55 -07:00
|
|
|
, params_code.debug_str()
|
2023-04-11 19:18:02 -07:00
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
switch ( params_code->NumEntries )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
case 1:
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( params_code->ValueType.is_equal( t_int ) )
|
2023-04-11 19:18:02 -07:00
|
|
|
is_member_symbol = true;
|
2023-04-06 16:19:11 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
else
|
|
|
|
check_param_eq_ret();
|
|
|
|
break;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
case 2:
|
|
|
|
check_param_eq_ret();
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( ! params_code.get(1).is_equal( t_int ) )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: "
|
|
|
|
"operator%s requires second parameter of non-member definition to be int for post-decrement",
|
|
|
|
to_str(op)
|
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
default:
|
2023-07-13 20:01:20 -07:00
|
|
|
log_failure("gen::def_operator: operator%s recieved unexpected number of parameters recived %d instead of 0-2"
|
2023-04-11 19:18:02 -07:00
|
|
|
, to_str(op)
|
2023-07-15 12:20:55 -07:00
|
|
|
, params_code->NumEntries
|
2023-04-11 19:18:02 -07:00
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
case Unary_Plus:
|
|
|
|
case Unary_Minus:
|
|
|
|
case BNot:
|
|
|
|
if ( ! params_code )
|
|
|
|
is_member_symbol = true;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( params_code->Type != ECode::Parameters )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_operator: params is not of Parameters type - %s", params_code.debug_str());
|
2023-04-11 19:18:02 -07:00
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( params_code->ValueType.is_equal( ret_type ) )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
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"
|
2023-07-15 12:20:55 -07:00
|
|
|
, params_code.debug_str()
|
|
|
|
, ret_type.debug_str()
|
2023-04-11 19:18:02 -07:00
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( params_code->NumEntries > 1 )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s may not have more than one parameter - param count: %d"
|
|
|
|
, to_str(op)
|
2023-07-15 12:20:55 -07:00
|
|
|
, params_code->NumEntries
|
2023-04-11 19:18:02 -07:00
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Add:
|
|
|
|
case Subtract:
|
|
|
|
case Multiply:
|
|
|
|
case Divide:
|
|
|
|
case Modulo:
|
|
|
|
case BAnd:
|
|
|
|
case BOr:
|
|
|
|
case BXOr:
|
|
|
|
case LShift:
|
|
|
|
case RShift:
|
|
|
|
check_params();
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
switch ( params_code->NumEntries )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
is_member_symbol = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( ! params_code->ValueType.is_equal( ret_type ) )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
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"
|
2023-07-15 12:20:55 -07:00
|
|
|
, params_code.debug_str()
|
|
|
|
, ret_type.debug_str()
|
2023-04-11 19:18:02 -07:00
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
log_failure("gen::def_operator: operator%s recieved unexpected number of paramters recived %d instead of 0-2"
|
|
|
|
, to_str(op)
|
2023-07-15 12:20:55 -07:00
|
|
|
, params_code->NumEntries
|
2023-04-11 19:18:02 -07:00
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
case UnaryNot:
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( ! params_code )
|
|
|
|
is_member_symbol = true;
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( params_code->Type != ECode::Parameters )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_operator: params is not of Parameters type - %s", params_code.debug_str());
|
2023-04-11 19:18:02 -07:00
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( params_code->NumEntries != 1 )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s recieved unexpected number of paramters recived %d instead of 0-1"
|
|
|
|
, to_str(op)
|
2023-07-15 12:20:55 -07:00
|
|
|
, params_code->NumEntries
|
2023-04-11 19:18:02 -07:00
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( ! ret_type.is_equal( t_bool ))
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s return type must be of type bool - %s"
|
|
|
|
, to_str(op)
|
2023-07-15 12:20:55 -07:00
|
|
|
, ret_type.debug_str()
|
2023-04-11 19:18:02 -07:00
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LAnd:
|
|
|
|
case LOr:
|
2023-07-10 19:14:41 -07:00
|
|
|
case LEqual:
|
|
|
|
case LNot:
|
2023-04-11 19:18:02 -07:00
|
|
|
case Lesser:
|
|
|
|
case Greater:
|
|
|
|
case LesserEqual:
|
|
|
|
case GreaterEqual:
|
|
|
|
check_params();
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
switch ( params_code->NumEntries )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
is_member_symbol = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
log_failure("gen::def_operator: operator%s recieved unexpected number of paramters recived %d instead of 1-2"
|
|
|
|
, to_str(op)
|
2023-07-15 12:20:55 -07:00
|
|
|
, params_code->NumEntries
|
2023-04-11 19:18:02 -07:00
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Indirection:
|
|
|
|
case AddressOf:
|
|
|
|
case MemberOfPointer:
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( params_code && params_code->NumEntries > 1)
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: operator%s recieved unexpected number of paramters recived %d instead of 0-1"
|
|
|
|
, to_str(op)
|
2023-07-15 12:20:55 -07:00
|
|
|
, params_code->NumEntries
|
2023-04-11 19:18:02 -07:00
|
|
|
);
|
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
is_member_symbol = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PtrToMemOfPtr:
|
|
|
|
if ( params_code )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_operator: operator%s expects no paramters - %s", to_str(op), params_code.debug_str());
|
2023-04-11 19:18:02 -07:00
|
|
|
return OpValidateResult::Fail;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Subscript:
|
|
|
|
case FunctionCall:
|
|
|
|
case Comma:
|
|
|
|
check_params();
|
|
|
|
break;
|
2023-04-22 19:24:55 -07:00
|
|
|
# undef specs
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
return is_member_symbol ? OpValidateResult::Member : OpValidateResult::Global;
|
2023-04-22 19:24:55 -07:00
|
|
|
# undef check_params
|
|
|
|
# undef check_ret_type
|
|
|
|
# undef check_param_eq_ret
|
2023-04-06 16:19:11 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
void set_allocator_data_arrays( AllocatorInfo allocator )
|
2023-04-09 10:59:39 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
StaticData::Allocator_DataArrays = allocator;
|
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
void set_allocator_code_pool( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
StaticData::Allocator_CodePool = allocator;
|
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
void set_allocator_lexer( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
StaticData::Allocator_Lexer = allocator;
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
void set_allocator_string_arena( AllocatorInfo allocator )
|
|
|
|
{
|
|
|
|
StaticData::Allocator_StringArena = allocator;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
void set_allocator_string_table( AllocatorInfo allocator )
|
2023-04-09 21:38:47 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
StaticData::Allocator_StringArena = allocator;
|
|
|
|
}
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma region Helper Marcos
|
2023-04-11 19:18:02 -07:00
|
|
|
// This snippet is used in nearly all the functions.
|
2023-07-13 22:37:22 -07:00
|
|
|
# define name_check( Context_, Name_ ) \
|
|
|
|
{ \
|
|
|
|
if ( Name_.Len <= 0 ) \
|
|
|
|
{ \
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::" stringize(Context_) ": Invalid name length provided - %d", Name_.Len ); \
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if ( Name_.Ptr == nullptr ) \
|
|
|
|
{ \
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::" stringize(Context_) ": name is null" ); \
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid; \
|
|
|
|
} \
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
# define null_check( Context_, Code_ ) \
|
|
|
|
if ( ! Code_ ) \
|
|
|
|
{ \
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::" stringize(Context_) ": " stringize(Code_) " provided is null" ); \
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid; \
|
2023-04-19 12:40:23 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
# define null_or_invalid_check( Context_, Code_ ) \
|
|
|
|
{ \
|
|
|
|
if ( ! Code_ ) \
|
|
|
|
{ \
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::" stringize(Context_) ": " stringize(Code_) " provided is null" ); \
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if ( Code_->is_invalid() ) \
|
|
|
|
{ \
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure("gen::" stringize(Context_) ": " stringize(Code_) " provided is invalid" ); \
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid; \
|
|
|
|
} \
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
# define not_implemented( Context_ ) \
|
|
|
|
log_failure( "gen::%s: This function is not implemented" ); \
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion Helper Marcos
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma region Upfront Constructors
|
2023-04-11 19:18:02 -07:00
|
|
|
/*
|
|
|
|
The implementaiton of the upfront constructors involves bascially doing three things:
|
|
|
|
* Validate the arguments given to construct the intended type of AST is valid.
|
|
|
|
* Construct said AST type.
|
|
|
|
* Lock the AST (set to readonly) and return the valid object.
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
If any of the validation fails, it triggers a call to log_failure with as much info the give the user so that they can hopefully
|
|
|
|
identify the issue without having to debug too much (at least they can debug though...)
|
2023-04-09 10:59:39 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
The largest of the functions is related to operator overload definitions.
|
2023-07-08 15:49:49 -07:00
|
|
|
The library validates a good protion of their form and thus the argument processing for is quite a bit.
|
2023-04-11 19:18:02 -07:00
|
|
|
*/
|
2023-07-13 20:01:20 -07:00
|
|
|
CodeAttributes def_attributes( StrC content )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
if ( content.Len <= 0 || content.Ptr == nullptr )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-06-28 22:37:42 -07:00
|
|
|
log_failure( "gen::def_attributes: Invalid attributes provided" );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
2023-07-15 12:20:55 -07:00
|
|
|
result = make_code();
|
2023-07-14 21:03:14 -07:00
|
|
|
result->Type = ECode::PlatformAttributes;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( content );
|
2023-05-01 11:12:07 -07:00
|
|
|
result->Content = result->Name;
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
return (CodeAttributes) result;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
CodeComment def_comment( StrC content )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
if ( content.Len <= 0 || content.Ptr == nullptr )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-06-28 22:37:42 -07:00
|
|
|
log_failure( "gen::def_comment: Invalid comment provided:" );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
2023-07-15 12:20:55 -07:00
|
|
|
result = make_code();
|
2023-06-28 22:37:42 -07:00
|
|
|
result->Type = ECode::Comment;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( content );
|
2023-05-01 11:12:07 -07:00
|
|
|
result->Content = result->Name;
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
return (CodeComment) result;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
CodeClass def_class( StrC name
|
2023-07-15 12:20:55 -07:00
|
|
|
, Code body
|
|
|
|
, CodeType parent, AccessSpec parent_access
|
|
|
|
, CodeAttributes attributes
|
|
|
|
, ModuleFlag mflags )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
2023-04-03 00:55:28 -07:00
|
|
|
using namespace ECode;
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_class, name );
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( attributes && attributes->Type != PlatformAttributes )
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_class: attributes was not a 'PlatformAttributes' type: %s", attributes.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-03 23:04:19 -07:00
|
|
|
}
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-05-07 12:03:24 -07:00
|
|
|
if ( parent && (parent->Type != Class || parent->Type != Struct || parent->Type != Typename || parent->Type != Untyped) )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_class: parent provided is not type 'Class', 'Struct', 'Typeanme', or 'Untyped': %s", parent.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-03 00:55:28 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeClass
|
|
|
|
result = (CodeClass) make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-04-11 19:18:02 -07:00
|
|
|
|
|
|
|
if ( body )
|
2023-04-07 09:31:50 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
switch ( body->Type )
|
|
|
|
{
|
|
|
|
case Class_Body:
|
|
|
|
case Untyped:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_class: body must be either of Class_Body or Untyped type - %s", body.debug_str());
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
result->Type = Class;
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = body;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = Class_Fwd;
|
|
|
|
}
|
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Attributes = attributes;
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
if ( parent )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
|
|
|
result->ParentAccess = parent_access;
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ParentType = parent;
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-04-22 21:43:31 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
CodeEnum def_enum( StrC name
|
2023-07-15 13:13:44 -07:00
|
|
|
, Code body, CodeType type
|
|
|
|
, EnumT specifier, CodeAttributes attributes
|
2023-05-05 15:10:03 -07:00
|
|
|
, ModuleFlag mflags )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_enum, name );
|
2023-04-11 19:18:02 -07:00
|
|
|
|
|
|
|
if ( type && type->Type != Typename )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_enum: enum underlying type provided was not of type Typename: %s", type.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( attributes && attributes->Type != PlatformAttributes )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_enum: attributes was not a 'PlatformAttributes' type: %s", attributes.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-05-05 15:10:03 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeEnum
|
|
|
|
result = (CodeEnum) make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-07 09:31:50 -07:00
|
|
|
if ( body )
|
|
|
|
{
|
|
|
|
switch ( body->Type )
|
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
case Enum_Body:
|
2023-04-07 09:31:50 -07:00
|
|
|
case Untyped:
|
2023-04-11 19:18:02 -07:00
|
|
|
break;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
|
|
|
default:
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_enum: body must be of Enum_Body or Untyped type %s", body.debug_str());
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = specifier == EnumClass ?
|
|
|
|
Enum_Class : Enum;
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = body;
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
2023-06-28 18:20:29 -07:00
|
|
|
else
|
2023-04-07 09:31:50 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = specifier == EnumClass ?
|
|
|
|
Enum_Class_Fwd : Enum_Fwd;
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Attributes = attributes;
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
if ( type )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
result->UnderlyingType = type;
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
2023-05-05 21:18:44 -07:00
|
|
|
else if ( result->Type != Enum_Class_Fwd && result->Type != Enum_Fwd )
|
2023-04-22 21:43:31 -07:00
|
|
|
{
|
|
|
|
log_failure( "gen::def_enum: enum forward declaration must have an underlying type" );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-22 21:43:31 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 20:01:20 -07:00
|
|
|
CodeExec def_execution( StrC content )
|
2023-04-07 09:31:50 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
if ( content.Len <= 0 || content.Ptr == nullptr )
|
2023-04-07 09:31:50 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
log_failure( "gen::def_execution: Invalid execution provided" );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
Code
|
2023-07-15 12:20:55 -07:00
|
|
|
result = make_code();
|
2023-05-01 11:12:07 -07:00
|
|
|
result->Type = ECode::Execution;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( content );
|
2023-05-01 11:12:07 -07:00
|
|
|
result->Content = result->Name;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
return (CodeExec) result;
|
2023-04-23 16:04:43 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeExtern def_extern_link( StrC name, Code body )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_extern_linkage, name );
|
2023-04-23 16:04:43 -07:00
|
|
|
null_check( def_extern_linkage, body );
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if ( body->Type != Extern_Linkage_Body && body->Type != Untyped )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
log_failure("gen::def_extern_linkage: body is not of extern_linkage or untyped type %s", body->debug_str());
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-23 16:04:43 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeExtern
|
|
|
|
result = (CodeExtern)make_code();
|
|
|
|
result->Type = Extern_Linkage;
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
result->Body = body;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
return (CodeExtern) result;
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeFriend def_friend( Code declaration )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
using namespace ECode;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
null_check( def_friend, declaration );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
switch ( declaration->Type )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
case Class_Fwd:
|
|
|
|
case Function_Fwd:
|
|
|
|
case Operator_Fwd:
|
|
|
|
case Struct_Fwd:
|
2023-07-09 22:15:25 -07:00
|
|
|
case Class:
|
|
|
|
case Function:
|
|
|
|
case Operator:
|
|
|
|
case Struct:
|
2023-04-10 18:33:06 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
default:
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_friend: requires declartion to have class, function, operator, or struct - %s", declaration->debug_str());
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeFriend
|
|
|
|
result = (CodeFriend) make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Friend;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Declaration = declaration;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeFn def_function( StrC name
|
2023-07-15 12:20:55 -07:00
|
|
|
, CodeParam params , CodeType ret_type, Code body
|
|
|
|
, CodeSpecifier specifiers, CodeAttributes attributes
|
|
|
|
, ModuleFlag mflags )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
using namespace ECode;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_function, name );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( params && params->Type != Parameters )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_function: params was not a `Parameters` type: %s", params.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-06-28 11:43:21 -07:00
|
|
|
if ( ret_type && ret_type->Type != Typename )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_function: ret_type was not a Typename: %s", ret_type.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( specifiers && specifiers->Type != Specifiers )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_function: specifiers was not a `Specifiers` type: %s", specifiers.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-05-05 15:10:03 -07:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( attributes && attributes->Type != PlatformAttributes )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_function: attributes was not a `PlatformAttributes` type: %s", attributes.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeFn
|
|
|
|
result = (CodeFn) make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( body )
|
|
|
|
{
|
|
|
|
switch ( body->Type )
|
|
|
|
{
|
|
|
|
case Function_Body:
|
2023-06-30 21:23:40 -07:00
|
|
|
case Execution:
|
2023-04-11 19:18:02 -07:00
|
|
|
case Untyped:
|
|
|
|
break;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
default:
|
2023-04-10 18:33:06 -07:00
|
|
|
{
|
2023-06-30 21:23:40 -07:00
|
|
|
log_failure("gen::def_function: body must be either of Function_Body, Execution, or Untyped type. %s", body->debug_str());
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-10 18:33:06 -07:00
|
|
|
}
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Function;
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = body;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = Function_Fwd;
|
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Attributes = attributes;
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( specifiers )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Specs = specifiers;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( ret_type )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ReturnType = ret_type;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ReturnType = t_void;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( params )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Params = params;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeInclude def_include ( StrC path )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
if ( path.Len <= 0 || path.Ptr == nullptr )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
log_failure( "gen::def_include: Invalid path provided - %d" );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Type = ECode::Preprocessor_Include;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( path );
|
2023-05-01 11:12:07 -07:00
|
|
|
result->Content = result->Name;
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
return (CodeInclude) result;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeModule def_module( StrC name, ModuleFlag mflags )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_module, name );
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
Code
|
2023-07-15 12:20:55 -07:00
|
|
|
result = make_code();
|
2023-05-05 15:10:03 -07:00
|
|
|
result->Type = ECode::Module;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-06-28 21:20:23 -07:00
|
|
|
result->Content = result->Name;
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
return (CodeModule) result;
|
2023-05-05 15:10:03 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeNamespace def_namespace( StrC name, Code body, ModuleFlag mflags )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
using namespace ECode;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_namespace, name );
|
2023-04-11 19:18:02 -07:00
|
|
|
null_check( def_namespace, body );
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if ( body->Type != Namespace_Body && body->Type != Untyped )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_namespace: body is not of namespace or untyped type %s", body.debug_str());
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeNamespace
|
|
|
|
result = (CodeNamespace) make_code();
|
2023-05-05 15:10:03 -07:00
|
|
|
result->Type = Namespace;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = body;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeOperator def_operator( OperatorT op
|
2023-07-15 12:20:55 -07:00
|
|
|
, CodeParam params_code, CodeType ret_type, Code body
|
|
|
|
, CodeSpecifier specifiers, CodeAttributes attributes
|
|
|
|
, ModuleFlag mflags )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( attributes && attributes->Type != PlatformAttributes )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_operator: PlatformAttributes was provided but its not of attributes type: %s", attributes.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-05-05 15:10:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( specifiers && specifiers->Type != Specifiers )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_operator: Specifiers was provided but its not of specifiers type: %s", specifiers.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 22:24:43 -07:00
|
|
|
OpValidateResult check_result = operator__validate( op, params_code, ret_type, specifiers );
|
2023-04-11 19:18:02 -07:00
|
|
|
|
|
|
|
if ( check_result == OpValidateResult::Fail )
|
|
|
|
{
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-06-29 19:48:47 -07:00
|
|
|
char const* name = str_fmt_buf( "operator %s", to_str(op) );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeOperator
|
|
|
|
result = (CodeOperator) make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( { str_len(name), name } );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-04-09 21:38:47 -07:00
|
|
|
|
|
|
|
if ( body )
|
|
|
|
{
|
2023-06-30 21:23:40 -07:00
|
|
|
switch ( body->Type )
|
|
|
|
{
|
|
|
|
case Function_Body:
|
|
|
|
case Execution:
|
|
|
|
case Untyped:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
log_failure("gen::def_operator: body must be either of Function_Body, Execution, or Untyped type. %s", body->debug_str());
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-06-30 21:23:40 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = check_result == OpValidateResult::Global ?
|
|
|
|
Operator : Operator_Member;
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = body;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = check_result == OpValidateResult::Global ?
|
|
|
|
Operator_Fwd : Operator_Member_Fwd;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Attributes = attributes;
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
if ( specifiers )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Specs = specifiers;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ReturnType = ret_type;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-22 21:43:31 -07:00
|
|
|
if (params_code)
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Params = params_code;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeOpCast def_operator_cast( CodeType type, Code body )
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
using namespace ECode;
|
|
|
|
null_check( def_operator_cast, type );
|
|
|
|
|
|
|
|
if ( type->Type != Typename )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_operator_cast: type is not a typename - %s", type.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeOpCast result = (CodeOpCast) make_code();
|
2023-07-10 19:14:41 -07:00
|
|
|
|
|
|
|
if (body)
|
|
|
|
{
|
|
|
|
result->Type = Operator_Cast;
|
|
|
|
|
|
|
|
if ( body->Type != Function_Body && body->Type != Execution )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_operator_cast: body is not of function body or execution type - %s", body.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = body;
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = Operator_Cast_Fwd;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ValueType = type;
|
|
|
|
return result;
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam def_param( CodeType type, StrC name, Code value )
|
2023-04-07 09:31:50 -07:00
|
|
|
{
|
2023-04-09 21:38:47 -07:00
|
|
|
using namespace ECode;
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_param, name );
|
2023-04-09 21:38:47 -07:00
|
|
|
null_check( def_param, type );
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-09 21:38:47 -07:00
|
|
|
if ( type->Type != Typename )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_param: type is not a typename - %s", type.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
if ( value && value->Type != Untyped )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_param: value is not untyped - %s", value.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-04-23 16:04:43 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam
|
|
|
|
result = (CodeParam) make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Parameters;
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ValueType = type;
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
if ( value )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Value = value;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-15 17:56:27 -07:00
|
|
|
result->NumEntries++;
|
|
|
|
|
2023-07-15 19:27:38 -07:00
|
|
|
return result;
|
2023-04-07 09:31:50 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeSpecifier def_specifier( SpecifierT spec )
|
2023-06-30 00:13:41 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeSpecifier
|
|
|
|
result = (CodeSpecifier) make_code();
|
2023-06-30 00:13:41 -07:00
|
|
|
result->Type = ECode::Specifiers;
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( spec );
|
2023-06-30 00:13:41 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeStruct def_struct( StrC name
|
|
|
|
, Code body
|
|
|
|
, CodeType parent, AccessSpec parent_access
|
|
|
|
, CodeAttributes attributes
|
|
|
|
, ModuleFlag mflags )
|
2023-04-02 09:35:14 -07:00
|
|
|
{
|
2023-04-03 00:55:28 -07:00
|
|
|
using namespace ECode;
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( attributes && attributes->Type != PlatformAttributes )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_struct: attributes was not a `PlatformAttributes` type - %s", attributes.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-05-05 15:10:03 -07:00
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( parent && parent->Type != Typename )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_struct: parent was not a `Struct` type - %s", parent.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( body && body->Type != Struct_Body )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_struct: body was not a Struct_Body type - %s", body.debug_str() );
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeStruct
|
|
|
|
result = (CodeStruct) make_code();
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
if ( name )
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
if ( body )
|
2023-04-09 21:38:47 -07:00
|
|
|
{
|
|
|
|
result->Type = Struct;
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = body;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-10 18:33:06 -07:00
|
|
|
result->Type = Struct_Fwd;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Attributes = attributes;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( parent )
|
2023-05-05 21:18:44 -07:00
|
|
|
{
|
|
|
|
result->ParentAccess = parent_access;
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ParentType = parent;
|
2023-05-05 21:18:44 -07:00
|
|
|
}
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-15 19:27:38 -07:00
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeTemplate def_template( CodeParam params, Code declaration, ModuleFlag mflags )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
null_check( def_template, declaration );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
if ( params && params->Type != ECode::Parameters )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_template: params is not of parameters type - %s", params.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
switch (declaration->Type )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
|
|
|
case ECode::Class:
|
|
|
|
case ECode::Function:
|
|
|
|
case ECode::Struct:
|
|
|
|
case ECode::Variable:
|
|
|
|
case ECode::Using:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_template: declaration is not of class, function, struct, variable, or using type - %s", declaration.debug_str() );
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeTemplate
|
|
|
|
result = (CodeTemplate) make_code();
|
2023-07-09 22:15:25 -07:00
|
|
|
result->Type = ECode::Template;
|
|
|
|
result->ModuleFlags = mflags;
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Params = params;
|
|
|
|
result->Declaration = declaration;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-15 19:27:38 -07:00
|
|
|
return result;
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeType def_type( StrC name, Code arrayexpr, CodeSpecifier specifiers, CodeAttributes attributes )
|
2023-05-06 12:49:43 -07:00
|
|
|
{
|
|
|
|
name_check( def_type, name );
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( attributes && attributes->Type != ECode::PlatformAttributes )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_type: attributes is not of attributes type - %s", attributes.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
if ( specifiers && specifiers->Type != ECode::Specifiers )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_type: specifiers is not of specifiers type - %s", specifiers.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-05-06 12:49:43 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
if ( arrayexpr && arrayexpr->Type != ECode::Untyped )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_type: arrayexpr is not of untyped type - %s", arrayexpr->debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeType
|
|
|
|
result = (CodeType) make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
result->Type = ECode::Typename;
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
if ( attributes )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Attributes = attributes;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
if ( specifiers )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Specs = specifiers;
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
if ( arrayexpr )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ArrExpr = arrayexpr;
|
2023-05-06 12:49:43 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-05-06 12:49:43 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeTypedef def_typedef( StrC name, CodeType type, CodeAttributes attributes, ModuleFlag mflags )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_typedef, name );
|
2023-04-18 19:47:59 -07:00
|
|
|
null_check( def_typedef, type );
|
|
|
|
|
|
|
|
if ( type->Type != ECode::Typename )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_typedef: type was not a Typename - %s", type.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( attributes && attributes->Type != ECode::PlatformAttributes )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_typedef: attributes was not a PlatformAttributes - %s", attributes.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
|
2023-04-18 19:47:59 -07:00
|
|
|
// Registering the type.
|
2023-05-06 12:49:43 -07:00
|
|
|
Code registered_type = def_type( name );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
if ( ! registered_type )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_typedef: failed to register type" );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeTypedef
|
|
|
|
result = (CodeTypedef) make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->Type = ECode::Typedef;
|
|
|
|
result->ModuleFlags = mflags;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->UnderlyingType = type;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Attributes = attributes;
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeUnion def_union( StrC name, Code body, CodeAttributes attributes, ModuleFlag mflags )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
2023-06-28 21:20:23 -07:00
|
|
|
null_check( def_union, body );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if ( body->Type != ECode::Union_Body )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_union: body was not a Union_Body type - %s", body.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( attributes && attributes->Type != ECode::PlatformAttributes )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_union: attributes was not a PlatformAttributes type - %s", attributes.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeUnion
|
|
|
|
result = (CodeUnion) make_code();
|
2023-05-05 15:10:03 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-06-28 21:20:23 -07:00
|
|
|
result->Type = ECode::Union;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
if ( name.Ptr )
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = body;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Attributes = attributes;
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeUsing def_using( StrC name, CodeType type
|
|
|
|
, CodeAttributes attributes
|
|
|
|
, ModuleFlag mflags )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_using, name );
|
2023-04-09 21:38:47 -07:00
|
|
|
null_check( def_using, type );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
Code register_type = def_type( name );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
if ( ! register_type )
|
|
|
|
{
|
|
|
|
log_failure( "gen::def_using: failed to register type" );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( attributes && attributes->Type != ECode::PlatformAttributes )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_using: attributes was not a PlatformAttributes type - %s", attributes.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeUsing
|
|
|
|
result = (CodeUsing) make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 21:18:44 -07:00
|
|
|
result->ModuleFlags = mflags;
|
2023-06-28 21:20:23 -07:00
|
|
|
result->Type = ECode::Using;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
result->UnderlyingType = type;
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-05-05 21:18:44 -07:00
|
|
|
if ( attributes )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Attributes = attributes;
|
2023-05-05 21:18:44 -07:00
|
|
|
|
2023-07-15 19:27:38 -07:00
|
|
|
return result;
|
2023-04-02 09:35:14 -07:00
|
|
|
}
|
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
CodeUsing def_using_namespace( StrC name )
|
2023-06-28 21:20:23 -07:00
|
|
|
{
|
|
|
|
name_check( def_using_namespace, name );
|
|
|
|
|
|
|
|
Code
|
|
|
|
result = make_code();
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
result->Content = result->Name;
|
|
|
|
result->Type = ECode::Using_Namespace;
|
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
return (CodeUsing) result;
|
2023-06-28 21:20:23 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeVar def_variable( CodeType type, StrC name, Code value
|
|
|
|
, CodeSpecifier specifiers, CodeAttributes attributes
|
|
|
|
, ModuleFlag mflags )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
name_check( def_variable, name );
|
2023-05-01 11:12:07 -07:00
|
|
|
null_check( def_variable, type );
|
|
|
|
|
2023-07-14 21:03:14 -07:00
|
|
|
if ( attributes && attributes->Type != ECode::PlatformAttributes )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_variable: attributes was not a `PlatformAttributes` type - %s", attributes.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-05-05 15:10:03 -07:00
|
|
|
}
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
if ( specifiers && specifiers->Type != ECode::Specifiers )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_variable: specifiers was not a `Specifiers` type - %s", specifiers.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( type->Type != ECode::Typename )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_variable: type was not a Typename - %s", type.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( value && value->Type != ECode::Untyped )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure( "gen::def_variable: value was not a `Untyped` type - %s", value.debug_str() );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeVar
|
|
|
|
result = (CodeVar) make_code();
|
2023-05-06 12:49:43 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
result->Type = ECode::Variable;
|
|
|
|
result->ModuleFlags = mflags;
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ValueType = type;
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( attributes )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Attributes = attributes;
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
if ( specifiers )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Specs = specifiers;
|
2023-05-01 11:12:07 -07:00
|
|
|
|
|
|
|
if ( value )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Value = value;
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
/*
|
|
|
|
Body related functions typically follow the same implementation pattern.
|
2023-07-08 15:49:49 -07:00
|
|
|
Opted to use inline helper macros to get the implementaiton done.
|
2023-04-11 19:18:02 -07:00
|
|
|
|
|
|
|
The implementation pattern is as follows:
|
|
|
|
* Validate a valid parameter num was provided, or code array
|
|
|
|
def_body_start or def_body_code_array_start( <name of function >)
|
|
|
|
|
|
|
|
* Begin the code entry do-while loop, make sure each entry is valid processing its type in the switc
|
|
|
|
def_body_code_validation_start( <name of function> )
|
|
|
|
|
|
|
|
* Define the switch case statements between the macros.
|
|
|
|
|
|
|
|
* Add the code entry, finish the closing implemenation for the do-while loop.
|
|
|
|
def_body_code_validation_end( <name of function> )
|
|
|
|
|
|
|
|
* Lock the body AST and return it.
|
|
|
|
|
|
|
|
If a function's implementation deviates from the macros then its just writen it out.
|
|
|
|
*/
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma region Helper Macros for def_**_body functions
|
2023-07-15 12:20:55 -07:00
|
|
|
# define def_body_start( Name_ ) \
|
|
|
|
using namespace ECode; \
|
|
|
|
\
|
|
|
|
if ( num <= 0 ) \
|
|
|
|
{ \
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure("gen::" stringize(Name_) ": num cannot be zero or negative"); \
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid; \
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
# define def_body_code_array_start( Name_ ) \
|
|
|
|
using namespace ECode; \
|
|
|
|
\
|
|
|
|
if ( num <= 0 ) \
|
|
|
|
{ \
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure("gen::" stringize(Name_) ": num cannot be zero or negative"); \
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if ( codes == nullptr ) \
|
|
|
|
{ \
|
2023-07-12 12:59:47 -07:00
|
|
|
log_failure("gen::" stringize(Name_)" : Provided a null array of codes"); \
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid; \
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
# define def_body_code_validation_start( Name_ ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
Code_POD pod = va_arg(va, Code_POD); \
|
|
|
|
Code entry = pcast(Code, pod); \
|
|
|
|
\
|
|
|
|
if ( ! entry ) \
|
|
|
|
{ \
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure("gen::" stringize(Name_) ": Provided an null entry"); \
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
switch ( entry->Type ) \
|
|
|
|
{
|
|
|
|
|
|
|
|
# define def_body_code_array_validation_start( Name_ ) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
Code entry = *codes; codes++; \
|
|
|
|
\
|
|
|
|
if ( ! entry ) \
|
|
|
|
{ \
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure("gen::" stringize(Name_) ": Provided an null entry"); \
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
switch ( entry->Type ) \
|
|
|
|
{
|
|
|
|
|
|
|
|
# define def_body_code_validation_end( Name_ ) \
|
|
|
|
log_failure("gen::" stringize(Name_) ": Entry type is not allowed: %s", entry.debug_str() ); \
|
|
|
|
return CodeInvalid; \
|
|
|
|
\
|
|
|
|
default: \
|
|
|
|
break; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
result.append( entry ); \
|
|
|
|
} \
|
2023-04-11 19:18:02 -07:00
|
|
|
while ( num--, num > 0 )
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion Helper Macros for def_**_body functions
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_class_body( s32 num, ... )
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_start( def_class_body );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Class_Body;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_class_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_CLASS_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_class_body );
|
2023-04-07 21:29:09 -07:00
|
|
|
va_end(va);
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-05 23:21:23 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_class_body( s32 num, Code* codes )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_array_start( def_class_body );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-10 18:33:06 -07:00
|
|
|
result->Type = Function_Body;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_class_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_CLASS_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_class_body );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_enum_body( s32 num, ... )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_start( def_enum_body );
|
2023-04-09 21:38:47 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Enum_Body;
|
2023-04-09 21:38:47 -07:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
|
|
|
do
|
|
|
|
{
|
2023-05-07 12:46:28 -07:00
|
|
|
Code_POD pod = va_arg(va, Code_POD);
|
|
|
|
Code entry = pcast(Code, pod);
|
2023-04-09 21:38:47 -07:00
|
|
|
|
|
|
|
if ( ! entry )
|
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
log_failure("gen::def_enum_body: Provided a null entry");
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
|
2023-07-12 12:59:47 -07:00
|
|
|
if ( entry->Type != Untyped && entry->Type != Comment )
|
2023-04-09 21:38:47 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_enum_body: Entry type is not allowed - %s. Must be of untyped or comment type.", entry.debug_str() ); \
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( entry );
|
2023-04-09 21:38:47 -07:00
|
|
|
}
|
|
|
|
while ( num--, num > 0 );
|
|
|
|
va_end(va);
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return (CodeBody) result;
|
2023-04-10 18:33:06 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_enum_body( s32 num, Code* codes )
|
2023-04-10 18:33:06 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_array_start( def_enum_body );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-23 19:53:30 -07:00
|
|
|
result->Type = Enum_Body;
|
2023-04-10 18:33:06 -07:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Code entry = *codes;
|
|
|
|
|
|
|
|
if ( ! entry )
|
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
log_failure("gen::def_enum_body: Provided a null entry");
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-10 18:33:06 -07:00
|
|
|
}
|
|
|
|
|
2023-07-12 12:59:47 -07:00
|
|
|
if ( entry->Type != Untyped && entry->Type != Comment )
|
2023-04-10 18:33:06 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_enum_body: Entry type is not allowed: %s", entry.debug_str() ); \
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-10 18:33:06 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( entry );
|
2023-04-10 18:33:06 -07:00
|
|
|
}
|
2023-04-11 19:18:02 -07:00
|
|
|
while ( codes++, num--, num > 0 );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_export_body( s32 num, ... )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
|
|
|
def_body_start( def_export_body );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-05-01 11:12:07 -07:00
|
|
|
result->Type = Export_Body;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_export_body );
|
2023-05-01 11:12:07 -07:00
|
|
|
AST_BODY_EXPORT_UNALLOWED_TYPES
|
|
|
|
def_body_code_validation_end( def_export_body );
|
|
|
|
va_end(va);
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_export_body( s32 num, Code* codes )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
|
|
|
def_body_code_array_start( def_export_body );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-05-01 11:12:07 -07:00
|
|
|
result->Type = Export_Body;
|
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_export_body );
|
2023-05-01 11:12:07 -07:00
|
|
|
AST_BODY_EXPORT_UNALLOWED_TYPES
|
|
|
|
def_body_code_validation_end( def_export_body );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_extern_link_body( s32 num, ... )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
def_body_start( def_extern_linkage_body );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-23 16:04:43 -07:00
|
|
|
result->Type = Extern_Linkage_Body;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_extern_linkage_body );
|
2023-04-23 16:04:43 -07:00
|
|
|
AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES
|
|
|
|
def_body_code_validation_end( def_extern_linkage_body );
|
|
|
|
va_end(va);
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-23 16:04:43 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_extern_link_body( s32 num, Code* codes )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
def_body_code_array_start( def_extern_linkage_body );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-23 16:04:43 -07:00
|
|
|
result->Type = Extern_Linkage_Body;
|
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_extern_linkage_body );
|
2023-04-23 16:04:43 -07:00
|
|
|
AST_BODY_EXTERN_LINKAGE_UNALLOWED_TYPES
|
|
|
|
def_body_code_validation_end( def_extern_linkage_body );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-23 16:04:43 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_function_body( s32 num, ... )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_start( def_function_body );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Function_Body;
|
2023-04-07 21:29:09 -07:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_function_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_FUNCTION_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_function_body );
|
|
|
|
va_end(va);
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_function_body( s32 num, Code* codes )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
def_body_code_array_start( def_function_body );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Function_Body;
|
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_function_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_FUNCTION_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_function_body );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_global_body( s32 num, ... )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
def_body_start( def_global_body );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Global_Body;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_global_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_GLOBAL_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_global_body );
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_global_body( s32 num, Code* codes )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
def_body_code_array_start( def_global_body );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Global_Body;
|
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_global_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_GLOBAL_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_global_body );
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
return result;
|
|
|
|
}
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_namespace_body( s32 num, ... )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
def_body_start( def_namespace_body );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Namespace_Body;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_namespace_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_NAMESPACE_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_namespace_body );
|
2023-04-07 21:29:09 -07:00
|
|
|
va_end(va);
|
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_namespace_body( s32 num, Code* codes )
|
2023-04-10 18:33:06 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_array_start( def_namespace_body );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-10 18:33:06 -07:00
|
|
|
result->Type = Global_Body;
|
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_namespace_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_NAMESPACE_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_namespace_body );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam def_params( s32 num, ... )
|
2023-04-05 23:21:23 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_start( def_params );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
Code_POD pod = va_arg(va, Code_POD);
|
|
|
|
CodeParam param = pcast( CodeParam, pod );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-06-30 08:54:27 -07:00
|
|
|
null_check( def_params, param );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-06-30 08:54:27 -07:00
|
|
|
if ( param->Type != Parameters )
|
2023-04-05 23:21:23 -07:00
|
|
|
{
|
2023-06-30 08:54:27 -07:00
|
|
|
log_failure( "gen::def_params: param %d is not a Parameters", num - num + 1 );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-04-05 23:21:23 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam result = (CodeParam) param.duplicate();
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-06-30 08:54:27 -07:00
|
|
|
while ( -- num )
|
2023-04-05 23:21:23 -07:00
|
|
|
{
|
2023-06-30 08:54:27 -07:00
|
|
|
pod = va_arg(va, Code_POD);
|
2023-07-15 12:20:55 -07:00
|
|
|
param = pcast( CodeParam, pod );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-06-30 08:54:27 -07:00
|
|
|
if ( param->Type != Parameters )
|
2023-04-05 23:21:23 -07:00
|
|
|
{
|
2023-06-30 08:54:27 -07:00
|
|
|
log_failure( "gen::def_params: param %d is not a Parameters", num - num + 1 );
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-04-05 23:21:23 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( param );
|
2023-04-05 23:21:23 -07:00
|
|
|
}
|
|
|
|
va_end(va);
|
|
|
|
|
2023-07-15 19:27:38 -07:00
|
|
|
return result;
|
2023-04-03 23:04:19 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam def_params( s32 num, CodeParam* codes )
|
2023-04-10 18:33:06 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_array_start( def_params );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
# define check_current() \
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( current.ast == nullptr ) \
|
2023-04-10 18:33:06 -07:00
|
|
|
{ \
|
|
|
|
log_failure("gen::def_params: Provide a null code in codes array"); \
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid; \
|
2023-04-10 18:33:06 -07:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
if (current->Type != Parameters ) \
|
|
|
|
{ \
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_params: Code in coes array is not of paramter type - %s", current.debug_str() ); \
|
|
|
|
return CodeInvalid; \
|
2023-04-10 18:33:06 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam current = (CodeParam) codes->duplicate();
|
2023-04-10 18:33:06 -07:00
|
|
|
check_current();
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam
|
|
|
|
result = (CodeParam) make_code();
|
|
|
|
result->Name = current->Name;
|
|
|
|
result->Type = current->Type;
|
|
|
|
result->ValueType = current->ValueType;
|
2023-06-28 21:20:23 -07:00
|
|
|
|
2023-04-10 18:33:06 -07:00
|
|
|
while( codes++, current = * codes, num--, num > 0 )
|
|
|
|
{
|
|
|
|
check_current();
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( current );
|
2023-04-10 18:33:06 -07:00
|
|
|
}
|
2023-04-23 11:33:37 -07:00
|
|
|
# undef check_current
|
2023-04-10 18:33:06 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeSpecifier def_specifiers( s32 num, ... )
|
2023-04-01 19:21:46 -07:00
|
|
|
{
|
|
|
|
if ( num <= 0 )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
2023-04-10 18:33:06 -07:00
|
|
|
log_failure("gen::def_specifiers: num cannot be zero or less");
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-11 19:18:02 -07:00
|
|
|
if ( num > AST::ArrSpecs_Cap )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_specifiers: num of speciifers to define AST larger than AST specicifier capacity - %d", num);
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeSpecifier
|
|
|
|
result = (CodeSpecifier) make_code();
|
2023-04-09 21:38:47 -07:00
|
|
|
result->Type = ECode::Specifiers;
|
2023-04-07 09:31:50 -07:00
|
|
|
|
2023-04-01 19:21:46 -07:00
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
|
|
|
do
|
|
|
|
{
|
2023-04-03 23:04:19 -07:00
|
|
|
SpecifierT type = (SpecifierT)va_arg(va, int);
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( type );
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
2023-04-01 22:07:44 -07:00
|
|
|
while ( --num, num );
|
2023-04-01 19:21:46 -07:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeSpecifier def_specifiers( s32 num, SpecifierT* specs )
|
2023-07-08 11:11:41 -07:00
|
|
|
{
|
|
|
|
if ( num <= 0 )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_specifiers: num cannot be zero or less");
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-07-08 11:11:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( num > AST::ArrSpecs_Cap )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_specifiers: num of speciifers to define AST larger than AST specicifier capacity - %d", num);
|
2023-07-14 21:03:14 -07:00
|
|
|
return CodeInvalid;
|
2023-07-08 11:11:41 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeSpecifier
|
|
|
|
result = (CodeSpecifier) make_code();
|
2023-07-08 11:11:41 -07:00
|
|
|
result->Type = ECode::Specifiers;
|
|
|
|
|
|
|
|
s32 idx = 0;
|
|
|
|
do
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( specs[idx] );
|
2023-07-08 11:11:41 -07:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
while ( --num, num );
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_struct_body( s32 num, ... )
|
2023-04-02 09:35:14 -07:00
|
|
|
{
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_start( def_struct_body );
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Struct_Body;
|
2023-04-02 09:35:14 -07:00
|
|
|
|
2023-04-03 00:55:28 -07:00
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_validation_start( def_struct_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_STRUCT_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_struct_body );
|
|
|
|
va_end(va);
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-11 19:18:02 -07:00
|
|
|
}
|
2023-04-07 21:29:09 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_struct_body( s32 num, Code* codes )
|
2023-04-11 19:18:02 -07:00
|
|
|
{
|
|
|
|
def_body_code_array_start( def_struct_body );
|
2023-04-10 18:33:06 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-11 19:18:02 -07:00
|
|
|
result->Type = Struct_Body;
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-05-07 12:46:28 -07:00
|
|
|
def_body_code_array_validation_start( def_struct_body );
|
2023-04-22 19:24:55 -07:00
|
|
|
AST_BODY_STRUCT_UNALLOWED_TYPES
|
2023-04-11 19:18:02 -07:00
|
|
|
def_body_code_validation_end( def_struct_body );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return result;
|
2023-04-02 09:35:14 -07:00
|
|
|
}
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_union_body( s32 num, ... )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
|
|
|
def_body_start( def_union_body );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-23 19:53:30 -07:00
|
|
|
result->Type = Union_Body;
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, num);
|
|
|
|
do
|
|
|
|
{
|
2023-05-07 12:46:28 -07:00
|
|
|
Code_POD pod = va_arg(va, Code_POD);
|
|
|
|
Code entry = pcast( Code, pod );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
|
|
|
if ( ! entry )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_union_body: Provided a null entry");
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-06-28 21:20:23 -07:00
|
|
|
if ( entry->Type != Untyped && entry->Type != Comment )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_union_body: Entry type is not allowed - %s. Must be of untyped or comment type.", entry.debug_str() ); \
|
|
|
|
return CodeInvalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( entry );
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
while ( num--, num > 0 );
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody def_union_body( s32 num, CodeUnion* codes )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
|
|
|
def_body_code_array_start( def_union_body );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-04-23 19:53:30 -07:00
|
|
|
result->Type = Union_Body;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Code entry = *codes;
|
|
|
|
|
|
|
|
if ( ! entry )
|
|
|
|
{
|
|
|
|
log_failure("gen::def_union_body: Provided a null entry");
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-12 12:59:47 -07:00
|
|
|
if ( entry->Type != Untyped && entry->Type != Comment )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_union_body: Entry type is not allowed: %s", entry.debug_str() );
|
|
|
|
return CodeInvalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( entry );
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
while ( codes++, num--, num > 0 );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return (CodeBody) result;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
# undef name_check
|
|
|
|
# undef null_check
|
|
|
|
# undef null_or_invalid_check
|
2023-06-29 19:48:47 -07:00
|
|
|
#pragma endregion Upfront Constructors
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
#pragma region Parsing Constructors
|
|
|
|
#ifdef GEN_FEATURE_PARSING
|
2023-04-18 19:47:59 -07:00
|
|
|
/*
|
|
|
|
These constructors are the most implementation intensive other than the edtior or scanner.
|
|
|
|
*/
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
namespace Parser
|
|
|
|
{
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
/*
|
|
|
|
This is a simple lexer that focuses on tokenizing only tokens relevant to the library.
|
|
|
|
It will not be capable of lexing C++ code with unsupported features.
|
|
|
|
*/
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
# define Define_TokType \
|
2023-07-08 20:29:18 -07:00
|
|
|
Entry( Access_Private, "private" ) \
|
|
|
|
Entry( Access_Protected, "protected" ) \
|
|
|
|
Entry( Access_Public, "public" ) \
|
|
|
|
Entry( Access_MemberSymbol, "." ) \
|
|
|
|
Entry( Access_StaticSymbol, "::") \
|
|
|
|
Entry( Ampersand, "&" ) \
|
|
|
|
Entry( Ampersand_DBL, "&&" ) \
|
|
|
|
Entry( Assign_Classifer, ":" ) \
|
|
|
|
Entry( BraceCurly_Open, "{" ) \
|
|
|
|
Entry( BraceCurly_Close, "}" ) \
|
|
|
|
Entry( BraceSquare_Open, "[" ) \
|
|
|
|
Entry( BraceSquare_Close, "]" ) \
|
|
|
|
Entry( Capture_Start, "(" ) \
|
|
|
|
Entry( Capture_End, ")" ) \
|
|
|
|
Entry( Comment, "__comment__" ) \
|
|
|
|
Entry( Char, "__char__" ) \
|
|
|
|
Entry( Comma, "," ) \
|
|
|
|
Entry( Decl_Class, "class" ) \
|
|
|
|
Entry( Decl_Enum, "enum" ) \
|
|
|
|
Entry( Decl_Extern_Linkage, "extern" ) \
|
|
|
|
Entry( Decl_Friend, "friend" ) \
|
|
|
|
Entry( Decl_Module, "module" ) \
|
|
|
|
Entry( Decl_Namespace, "namespace" ) \
|
2023-07-09 09:35:48 -07:00
|
|
|
Entry( Decl_Operator, "operator" ) \
|
2023-07-08 20:29:18 -07:00
|
|
|
Entry( Decl_Struct, "struct" ) \
|
2023-07-10 22:09:50 -07:00
|
|
|
Entry( Decl_Template, "template" ) \
|
2023-07-08 20:29:18 -07:00
|
|
|
Entry( Decl_Typedef, "typedef" ) \
|
|
|
|
Entry( Decl_Using, "using" ) \
|
|
|
|
Entry( Decl_Union, "union" ) \
|
|
|
|
Entry( Identifier, "__SymID__" ) \
|
|
|
|
Entry( Module_Import, "import" ) \
|
|
|
|
Entry( Module_Export, "export" ) \
|
|
|
|
Entry( Number, "number" ) \
|
|
|
|
Entry( Operator, "operator" ) \
|
|
|
|
Entry( Spec_Alignas, "alignas" ) \
|
|
|
|
Entry( Spec_Const, "const" ) \
|
|
|
|
Entry( Spec_Consteval, "consteval" ) \
|
|
|
|
Entry( Spec_Constexpr, "constexpr" ) \
|
|
|
|
Entry( Spec_Constinit, "constinit" ) \
|
|
|
|
Entry( Spec_Extern, "extern" ) \
|
|
|
|
Entry( Spec_Global, "global" ) \
|
|
|
|
Entry( Spec_Inline, "inline" ) \
|
|
|
|
Entry( Spec_Internal_Linkage, "internal" ) \
|
|
|
|
Entry( Spec_LocalPersist, "local_persist" ) \
|
|
|
|
Entry( Spec_Mutable, "mutable" ) \
|
|
|
|
Entry( Spec_Static, "static" ) \
|
|
|
|
Entry( Spec_ThreadLocal, "thread_local" ) \
|
|
|
|
Entry( Spec_Volatile, "volatile") \
|
|
|
|
Entry( Star, "*" ) \
|
|
|
|
Entry( Statement_End, ";" ) \
|
|
|
|
Entry( String, "__String__" ) \
|
|
|
|
Entry( Type_Unsigned, "unsigned" ) \
|
|
|
|
Entry( Type_Signed, "signed" ) \
|
|
|
|
Entry( Type_Short, "short" ) \
|
2023-07-09 09:35:48 -07:00
|
|
|
Entry( Type_Long, "long" ) \
|
|
|
|
Entry( Type_char, "char" ) \
|
|
|
|
Entry( Type_int, "int" ) \
|
2023-07-09 10:07:30 -07:00
|
|
|
Entry( Type_double, "double" )
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
enum class TokType : u32
|
|
|
|
{
|
|
|
|
# define Entry( Name_, Str_ ) Name_,
|
|
|
|
Define_TokType
|
|
|
|
# undef Entry
|
2023-05-05 15:10:03 -07:00
|
|
|
Attr_Keyword,
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
Num,
|
|
|
|
Invalid
|
|
|
|
};
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
struct Token
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
char const* Text;
|
|
|
|
sptr Length;
|
|
|
|
TokType Type;
|
2023-04-23 11:33:37 -07:00
|
|
|
bool IsAssign;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
|
|
|
operator bool()
|
|
|
|
{
|
|
|
|
return Text && Length && Type != TokType::Invalid;
|
|
|
|
}
|
2023-05-06 13:15:49 -07:00
|
|
|
|
|
|
|
operator StrC()
|
|
|
|
{
|
|
|
|
return { Length, Text };
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
};
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal inline
|
2023-04-22 19:24:55 -07:00
|
|
|
TokType get_tok_type( char const* word, s32 length )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
local_persist
|
2023-07-09 10:07:30 -07:00
|
|
|
StrC lookup[(u32)TokType::Num] =
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
2023-07-09 10:07:30 -07:00
|
|
|
# define Entry( Name_, Str_ ) { sizeof(Str_), Str_ },
|
2023-04-22 19:24:55 -07:00
|
|
|
Define_TokType
|
|
|
|
# undef Entry
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-09 10:07:30 -07:00
|
|
|
{ sizeof(Attribute::Keyword), Attribute::Keyword },
|
2023-04-22 19:24:55 -07:00
|
|
|
};
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
for ( u32 index = 0; index < (u32)TokType::Num; index++ )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-07-09 10:07:30 -07:00
|
|
|
s32 lookup_len = lookup[index].Len - 1;
|
|
|
|
char const* lookup_str = lookup[index].Ptr;
|
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
if ( lookup_len != length )
|
|
|
|
continue;
|
|
|
|
|
2023-07-09 10:07:30 -07:00
|
|
|
if ( str_compare( word, lookup_str, lookup_len ) == 0 )
|
2023-04-22 19:24:55 -07:00
|
|
|
return scast(TokType, index);
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
return TokType::Invalid;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal inline
|
2023-04-23 11:33:37 -07:00
|
|
|
char const* str_tok_type( TokType type )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
local_persist
|
|
|
|
char const* lookup[(u32)TokType::Num] =
|
|
|
|
{
|
|
|
|
# define Entry( Name_, Str_ ) Str_,
|
|
|
|
Define_TokType
|
|
|
|
# undef Entry
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
Attribute::Keyword,
|
2023-04-22 19:24:55 -07:00
|
|
|
};
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
return lookup[(u32)type];
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
# undef Define_TokType
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal inline
|
2023-04-22 19:24:55 -07:00
|
|
|
bool tok_is_specifier( Token const& tok )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-07-08 11:11:41 -07:00
|
|
|
return (tok.Type <= TokType::Star && tok.Type >= TokType::Spec_Alignas)
|
2023-04-22 21:43:31 -07:00
|
|
|
|| tok.Type == TokType::Ampersand
|
|
|
|
|| tok.Type == TokType::Ampersand_DBL
|
|
|
|
;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal inline
|
2023-05-01 11:12:07 -07:00
|
|
|
bool tok_is_access_specifier( Token const& tok )
|
|
|
|
{
|
|
|
|
return tok.Type >= TokType::Access_Private && tok.Type <= TokType::Access_Public;
|
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal inline
|
2023-05-01 11:12:07 -07:00
|
|
|
AccessSpec tok_to_access_specifier( Token const& tok )
|
|
|
|
{
|
|
|
|
return scast(AccessSpec, tok.Type);
|
|
|
|
}
|
|
|
|
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
struct TokArray
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
Array<Token> Arr;
|
2023-05-01 11:12:07 -07:00
|
|
|
s32 Idx;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
bool __eat( TokType type, char const* context )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
if ( Arr.num() - Idx <= 0 )
|
2023-04-23 11:33:37 -07:00
|
|
|
{
|
|
|
|
log_failure( "gen::%s: No tokens left", context );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
2023-07-08 14:14:05 -07:00
|
|
|
if ( Arr[Idx].Type != type )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
2023-07-09 09:35:48 -07:00
|
|
|
String token_str = String::make( Memory::GlobalAllocator, { Arr[Idx].Length, Arr[Idx].Text } );
|
2023-04-23 11:33:37 -07:00
|
|
|
|
|
|
|
log_failure( "gen::%s: expected %s, got %s", context, str_tok_type(type), str_tok_type(Arr[Idx].Type) );
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
Idx++;
|
2023-07-08 11:11:41 -07:00
|
|
|
return true;
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
2023-07-08 14:14:05 -07:00
|
|
|
|
|
|
|
Token& current()
|
|
|
|
{
|
|
|
|
return Arr[Idx];
|
|
|
|
}
|
2023-07-08 16:13:52 -07:00
|
|
|
|
|
|
|
Token& previous()
|
|
|
|
{
|
|
|
|
return Arr[Idx - 1];
|
|
|
|
}
|
2023-07-10 19:14:41 -07:00
|
|
|
|
|
|
|
Token* next()
|
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
return Idx + 1 < Arr.num() ? &Arr[Idx + 1] : nullptr;
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
};
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray lex( StrC content )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
# define current ( * scanner )
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
# define move_forward() \
|
|
|
|
left--; \
|
|
|
|
scanner++
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
# define SkipWhitespace() \
|
|
|
|
while ( left && char_is_space( current ) ) \
|
|
|
|
{ \
|
|
|
|
move_forward(); \
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
# define SkipWhitespace_Checked( Context_, Msg_, ... ) \
|
|
|
|
while ( left && char_is_space( current ) ) \
|
|
|
|
{ \
|
|
|
|
move_forward(); \
|
|
|
|
} \
|
|
|
|
if ( left <= 0 ) \
|
|
|
|
{ \
|
|
|
|
log_failure( "gen::" txt(Context_) ": " Msg_, __VA_ARGS__ ); \
|
|
|
|
return { 0, nullptr }; \
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
local_persist thread_local
|
2023-07-11 15:29:45 -07:00
|
|
|
Array<Token> Tokens = { nullptr };
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
s32 left = content.Len;
|
2023-05-06 13:15:49 -07:00
|
|
|
char const* scanner = content.Ptr;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
char const* word = scanner;
|
|
|
|
s32 word_length = 0;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
SkipWhitespace();
|
|
|
|
if ( left <= 0 )
|
|
|
|
{
|
|
|
|
log_failure( "gen::lex: no tokens found (only whitespace provided)" );
|
2023-07-11 15:29:45 -07:00
|
|
|
return { { nullptr }, 0 };
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( Tokens )
|
2023-07-15 23:26:55 -07:00
|
|
|
{
|
|
|
|
Tokens.free();
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
Tokens = Array<Token>::init_reserve( StaticData::LexArena, content.Len / 6 );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
while (left )
|
|
|
|
{
|
2023-04-23 11:33:37 -07:00
|
|
|
Token token = { nullptr, 0, TokType::Invalid, false };
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
SkipWhitespace();
|
|
|
|
if ( left <= 0 )
|
|
|
|
break;
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
switch ( current )
|
|
|
|
{
|
|
|
|
case '.':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Access_MemberSymbol;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if (left)
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
case '&' :
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Ampersand;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == '&' ) // &&
|
|
|
|
{
|
|
|
|
token.Length = 2;
|
|
|
|
token.Type = TokType::Ampersand_DBL;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
case ':':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Assign_Classifer;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == ':' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Type = TokType::Access_StaticSymbol;
|
|
|
|
token.Length++;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '{':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::BraceCurly_Open;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '}':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::BraceCurly_Close;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '[':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::BraceSquare_Open;
|
|
|
|
if ( left )
|
|
|
|
{
|
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == ']' )
|
|
|
|
{
|
|
|
|
token.Length = 2;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case ']':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::BraceSquare_Close;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '(':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Capture_Start;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
case ')':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Capture_End;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
case '\'':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Char;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
move_forward();
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
while ( left && current != '\'' )
|
|
|
|
{
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Length++;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( left )
|
|
|
|
{
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case ',':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Comma;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '*':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Star;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case ';':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Statement_End;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '"':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::String;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
while ( left )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == '"' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
break;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == '\\' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
if ( left )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
continue;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
// All other operators we just label as an operator and move forward.
|
2023-04-23 11:33:37 -07:00
|
|
|
case '=':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
token.IsAssign = true;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
goto FoundToken;
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
case '+':
|
|
|
|
case '%':
|
|
|
|
case '^':
|
|
|
|
case '~':
|
|
|
|
case '!':
|
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
case '|':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
if ( current == '=' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
|
|
|
token.Length++;
|
2023-04-23 11:33:37 -07:00
|
|
|
token.IsAssign = true;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
2023-05-06 13:15:49 -07:00
|
|
|
else while ( left && current == *(scanner - 1) && token.Length < 3 )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
// Dash is unfortunatlly a bit more complicated...
|
|
|
|
case '-':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
if ( left )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == '>' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Length++;
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
if ( current == '*' )
|
|
|
|
{
|
|
|
|
token.Length++;
|
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( current == '=' )
|
|
|
|
{
|
2023-04-18 19:47:59 -07:00
|
|
|
token.Length++;
|
2023-04-23 11:33:37 -07:00
|
|
|
token.IsAssign = true;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
|
|
|
}
|
2023-05-06 13:15:49 -07:00
|
|
|
else while ( left && current == *(scanner - 1) && token.Length < 3 )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
if (left)
|
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
|
|
|
|
case '/':
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
|
|
|
token.Type = TokType::Operator;
|
|
|
|
|
|
|
|
if ( left )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( current == '/' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Type = TokType::Comment;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
move_forward();
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 0;
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
while ( left && current != '\n' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( current == '*' )
|
|
|
|
{
|
|
|
|
token.Type = TokType::Comment;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
move_forward();
|
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 0;
|
|
|
|
|
|
|
|
while ( left && ( current != '*' && *(scanner + 1) != '/' ) )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
move_forward();
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
|
|
|
}
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( char_is_alpha( current ) || current == '_' )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
token.Text = scanner;
|
|
|
|
token.Length = 1;
|
2023-04-18 19:47:59 -07:00
|
|
|
move_forward();
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
while ( left && ( char_is_alphanumeric(current) || current == '_' ) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
goto FoundToken;
|
|
|
|
}
|
2023-07-09 09:35:48 -07:00
|
|
|
else if ( char_is_digit(current) )
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
move_forward();
|
|
|
|
|
|
|
|
if (left
|
|
|
|
&& ( current == 'x' || current == 'X'
|
|
|
|
|| current == 'b' || current == 'B'
|
|
|
|
|| current == 'o' || current == 'O' )
|
|
|
|
)
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
while ( left && char_is_hex_digit(current) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto FoundToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && char_is_digit(current) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( left && current == '.' )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
|
|
|
|
while ( left && char_is_digit(current) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
token.Length++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
goto FoundToken;
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
else
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-07-09 09:35:48 -07:00
|
|
|
String context_str = String::fmt_buf( Memory::GlobalAllocator, "%s", scanner, min( 100, left ) );
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
log_failure( "Failed to lex token %s", context_str );
|
|
|
|
|
|
|
|
// Skip to next whitespace since we can't know if anything else is valid until then.
|
|
|
|
while ( left && ! char_is_space( current ) )
|
|
|
|
{
|
|
|
|
move_forward();
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
FoundToken:
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( token.Type != TokType::Invalid )
|
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
Tokens.append( token );
|
2023-04-22 19:24:55 -07:00
|
|
|
continue;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
TokType type = get_tok_type( token.Text, token.Length );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-08 14:14:05 -07:00
|
|
|
if ( type == TokType::Invalid)
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
// Its most likely an identifier...
|
2023-07-08 14:14:05 -07:00
|
|
|
type = TokType::Identifier;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
2023-07-08 14:14:05 -07:00
|
|
|
|
|
|
|
token.Type = type;
|
2023-07-11 15:29:45 -07:00
|
|
|
Tokens.append( token );
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
if ( Tokens.num() == 0 )
|
2023-04-23 11:33:37 -07:00
|
|
|
{
|
|
|
|
log_failure( "Failed to lex any tokens" );
|
2023-07-11 15:29:45 -07:00
|
|
|
return { { nullptr }, 0 };
|
2023-04-23 11:33:37 -07:00
|
|
|
}
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
return { Tokens, 0 };
|
2023-04-22 21:43:31 -07:00
|
|
|
# undef current
|
|
|
|
# undef move_forward
|
|
|
|
# undef SkipWhitespace
|
|
|
|
# undef SkipWhitespace_Checked
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
#pragma region Helper Macros
|
2023-07-13 22:37:22 -07:00
|
|
|
# define check_parse_args( func, def ) \
|
|
|
|
if ( def.Len <= 0 ) \
|
|
|
|
{ \
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::" stringize(func) ": length must greater than 0" ); \
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid; \
|
|
|
|
} \
|
|
|
|
if ( def.Ptr == nullptr ) \
|
|
|
|
{ \
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::" stringize(func) ": def was null" ); \
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid; \
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
# define nexttok toks.next()
|
2023-07-08 14:14:05 -07:00
|
|
|
# define currtok toks.current()
|
2023-07-08 16:13:52 -07:00
|
|
|
# define prevtok toks.previous()
|
2023-04-23 19:53:30 -07:00
|
|
|
# define eat( Type_ ) toks.__eat( Type_, context )
|
2023-07-11 15:29:45 -07:00
|
|
|
# define left ( toks.Arr.num() - toks.Idx )
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-07-08 14:14:05 -07:00
|
|
|
# define check( Type_ ) ( left && currtok.Type == Type_ )
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion Helper Macros
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal Code parse_function_body ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal Code parse_global_nspace ( Parser::TokArray& toks, char const* context );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
internal CodeClass parse_class ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeEnum parse_enum ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeBody parse_export_body ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeBody parse_extern_link_body ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeExtern parse_exten_link ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeFriend parse_friend ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeFn parse_function ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeNamespace parse_namespace ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeOpCast parse_operator_cast ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeStruct parse_struct ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeVar parse_variable ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeTemplate parse_template ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeType parse_type ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeTypedef parse_typedef ( Parser::TokArray& toks, char const* context );
|
|
|
|
internal CodeUnion parse_union ( Parser::TokArray& toks, char const* context );
|
2023-07-16 09:08:57 -07:00
|
|
|
internal CodeUsing parse_using ( Parser::TokArray& toks, char const* context );
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
internal inline
|
2023-05-01 11:12:07 -07:00
|
|
|
Code parse_array_decl( Parser::TokArray& toks, char const* context )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
if ( check( TokType::BraceSquare_Open ) )
|
|
|
|
{
|
|
|
|
eat( TokType::BraceSquare_Open );
|
|
|
|
|
|
|
|
if ( left == 0 )
|
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "%s: Error, unexpected end of typedef definition ( '[]' scope started )", stringize(parse_typedef) );
|
2023-04-23 16:04:43 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::BraceSquare_Close )
|
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "%s: Error, empty array expression in typedef definition", stringize(parse_typedef) );
|
2023-04-23 16:04:43 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
Token untyped_tok = currtok;
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceSquare_Close )
|
|
|
|
{
|
2023-07-11 15:29:45 -07:00
|
|
|
eat( currtok.Type );
|
2023-04-23 16:04:43 -07:00
|
|
|
}
|
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
untyped_tok.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)untyped_tok.Text;
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
Code array_expr = untyped_str( untyped_tok );
|
2023-04-23 16:04:43 -07:00
|
|
|
|
|
|
|
if ( left == 0 )
|
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "%s: Error, unexpected end of type definition, expected ]", stringize(parse_typedef) );
|
2023-04-23 16:04:43 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type != TokType::BraceSquare_Close )
|
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "%s: Error, expected ] in type definition, not %s", stringize(parse_typedef), str_tok_type( currtok.Type ) );
|
2023-04-23 16:04:43 -07:00
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::BraceSquare_Close );
|
|
|
|
return array_expr;
|
|
|
|
}
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
return { nullptr };
|
2023-04-23 16:04:43 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal inline
|
2023-05-01 11:12:07 -07:00
|
|
|
Parser::Token parse_identifier( Parser::TokArray& toks, char const* context )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
Token name = currtok;
|
|
|
|
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
while ( check( TokType::Access_StaticSymbol ) )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
eat( TokType::Access_StaticSymbol );
|
|
|
|
|
|
|
|
if ( left == 0 )
|
|
|
|
{
|
2023-05-01 11:12:07 -07:00
|
|
|
log_failure( "%s: Error, unexpected end of type definition, expected identifier", context );
|
2023-04-23 16:04:43 -07:00
|
|
|
return { nullptr, 0, TokType::Invalid };
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type != TokType::Identifier )
|
|
|
|
{
|
2023-05-01 11:12:07 -07:00
|
|
|
log_failure( "%s: Error, expected identifier in type definition, not %s", context, str_tok_type( currtok.Type ) );
|
2023-04-23 16:04:43 -07:00
|
|
|
return { nullptr, 0, TokType::Invalid };
|
|
|
|
}
|
|
|
|
|
|
|
|
name.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)name.Text;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam parse_params( Parser::TokArray& toks, char const* context, bool use_template_capture = false )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( ! use_template_capture )
|
|
|
|
eat( TokType::Capture_Start );
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( check ( TokType::Operator ) && currtok.Text[0] == '<' )
|
|
|
|
eat( TokType::Operator );
|
|
|
|
}
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( ! use_template_capture && check(TokType::Capture_End) )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
|
|
|
eat( TokType::Capture_End );
|
|
|
|
return { nullptr };
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeType type = { nullptr };
|
|
|
|
Code value = { nullptr };
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
type = parse_type( toks, context );
|
|
|
|
if ( type == Code::Invalid )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
Token name = { nullptr, 0, TokType::Invalid, false };
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
if ( check( TokType::Identifier ) )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
2023-07-10 22:09:50 -07:00
|
|
|
name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
if ( currtok.IsAssign )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
2023-07-10 22:09:50 -07:00
|
|
|
eat( TokType::Operator );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
Token value_tok = currtok;
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Statement_End )
|
|
|
|
{
|
|
|
|
log_failure( "gen::%s: Expected value after assignment operator", context );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-10 22:09:50 -07:00
|
|
|
}
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
while ( left && currtok.Type != TokType::Statement_End )
|
|
|
|
{
|
|
|
|
value_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)value_tok.Text;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
value = parse_type( toks, context );
|
|
|
|
}
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam
|
|
|
|
result = (CodeParam) make_code();
|
2023-04-23 16:04:43 -07:00
|
|
|
result->Type = Parameters;
|
2023-07-10 22:09:50 -07:00
|
|
|
|
|
|
|
if ( name.Length > 0 )
|
|
|
|
result->Name = get_cached_string( name );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ValueType = type;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
if ( value )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Value = value;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-15 20:07:30 -07:00
|
|
|
result->NumEntries++;
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
while ( left
|
|
|
|
&& use_template_capture ?
|
|
|
|
currtok.Type != TokType::Operator && currtok.Text[0] != '>'
|
|
|
|
: currtok.Type != TokType::Capture_End )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
2023-07-09 22:15:25 -07:00
|
|
|
eat( TokType::Comma );
|
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
Code type = { nullptr };
|
|
|
|
Code value = { nullptr };
|
|
|
|
|
|
|
|
type = parse_type( toks, context );
|
|
|
|
if ( type == Code::Invalid )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
name = { nullptr, 0, TokType::Invalid, false };
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
if ( check( TokType::Identifier ) )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
2023-07-10 22:09:50 -07:00
|
|
|
name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
if ( currtok.IsAssign )
|
2023-04-23 16:04:43 -07:00
|
|
|
{
|
2023-07-10 22:09:50 -07:00
|
|
|
eat( TokType::Operator );
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
Token value_tok = currtok;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
if ( currtok.Type == TokType::Statement_End )
|
|
|
|
{
|
|
|
|
log_failure( "gen::%s: Expected value after assignment operator", context );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-10 22:09:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::Statement_End )
|
|
|
|
{
|
|
|
|
value_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)value_tok.Text;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
value = parse_type( toks, context );
|
|
|
|
}
|
2023-04-23 16:04:43 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam
|
|
|
|
param = (CodeParam) make_code();
|
2023-04-23 16:04:43 -07:00
|
|
|
param->Type = Parameters;
|
2023-07-10 22:09:50 -07:00
|
|
|
|
|
|
|
if ( name.Length > 0 )
|
|
|
|
param->Name = get_cached_string( name );
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
param->ValueType = type;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
|
|
|
if ( value )
|
2023-07-15 12:20:55 -07:00
|
|
|
param->Value = value;
|
2023-04-23 16:04:43 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( param );
|
2023-04-23 16:04:43 -07:00
|
|
|
}
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( ! use_template_capture )
|
|
|
|
eat( TokType::Capture_End );
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( ! check( TokType::Operator) || currtok.Text[0] != '>' )
|
|
|
|
{
|
|
|
|
log_failure("gen::parse_template: expected '<' after 'template' keyword. %s", str_tok_type( currtok.Type ));
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
|
|
|
eat( TokType::Operator );
|
|
|
|
}
|
2023-04-23 16:04:43 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
# undef context
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// Function parsing is handled in multiple places because its initial signature is shared with variable parsing
|
|
|
|
internal inline
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeFn parse_function_after_name(
|
|
|
|
ModuleFlag mflags
|
|
|
|
, CodeAttributes attributes
|
|
|
|
, CodeSpecifier specifiers
|
|
|
|
, CodeType ret_type
|
|
|
|
, StrC name
|
2023-07-09 22:15:25 -07:00
|
|
|
, Parser::TokArray& toks
|
2023-07-15 12:20:55 -07:00
|
|
|
, char const* context
|
2023-07-09 22:15:25 -07:00
|
|
|
)
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam params = parse_params( toks, stringize(parse_function) );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody body = { nullptr };
|
2023-07-09 22:15:25 -07:00
|
|
|
if ( check( TokType::BraceCurly_Open ) )
|
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
body = parse_function_body( toks, stringize(parse_function) );
|
2023-07-09 22:15:25 -07:00
|
|
|
if ( body == Code::Invalid )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
}
|
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeFn
|
|
|
|
result = (CodeFn) make_code();
|
2023-07-09 22:15:25 -07:00
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
result->ModuleFlags = mflags;
|
|
|
|
|
|
|
|
if ( body )
|
|
|
|
{
|
|
|
|
switch ( body->Type )
|
|
|
|
{
|
|
|
|
case Function_Body:
|
|
|
|
case Untyped:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
log_failure("gen::def_function: body must be either of Function_Body or Untyped type. %s", body.debug_str());
|
|
|
|
return CodeInvalid;
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result->Type = Function;
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = body;
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = Function_Fwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( specifiers )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Specs = specifiers;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ReturnType = ret_type;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
if ( params )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Params = params;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal inline
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeOperator parse_operator_after_ret_type( ModuleFlag mflags
|
|
|
|
, CodeAttributes attributes
|
|
|
|
, CodeSpecifier specifiers
|
|
|
|
, CodeType ret_type
|
|
|
|
, Parser::TokArray& toks
|
|
|
|
, char const* context )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-10 19:14:41 -07:00
|
|
|
using namespace EOperator;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
// Parse Operator
|
|
|
|
eat( TokType::Decl_Operator );
|
|
|
|
|
|
|
|
if ( ! check( TokType::Operator ) )
|
|
|
|
{
|
|
|
|
log_failure( "gen::%s: Expected operator after 'operator' keyword", context );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
OperatorT op = Invalid;
|
2023-07-09 22:15:25 -07:00
|
|
|
switch ( currtok.Text[0] )
|
|
|
|
{
|
|
|
|
case '+':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_Add;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Add;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '-':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_Subtract;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Subtract;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '*':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_Multiply;
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Token& finder = prevtok;
|
|
|
|
while ( finder.Type != TokType::Decl_Operator )
|
|
|
|
{
|
|
|
|
if ( finder.Type == TokType::Identifier)
|
|
|
|
{
|
|
|
|
op = Indirection;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( op == Invalid)
|
|
|
|
op = Multiply;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '/':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_Divide;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Divide;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '%':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_Modulo;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Modulo;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '&':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_BAnd;
|
|
|
|
|
|
|
|
else if ( currtok.Text[1] == '&' )
|
|
|
|
op = LAnd;
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
if ( op == Invalid )
|
|
|
|
op = BAnd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '|':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_BOr;
|
|
|
|
|
|
|
|
else if ( currtok.Text[1] == '|' )
|
|
|
|
op = LOr;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = BOr;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '^':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = Assign_BXOr;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = BXOr;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '~':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
op = BNot;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '!':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = LNot;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = UnaryNot;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '=':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = LEqual;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Assign;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '<':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = LEqual;
|
|
|
|
|
|
|
|
else if ( currtok.Text[1] == '<' )
|
|
|
|
{
|
|
|
|
if ( currtok.Text[2] == '=' )
|
|
|
|
op = Assign_LShift;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = LShift;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
op = Lesser;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '>':
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == '=' )
|
|
|
|
op = GreaterEqual;
|
|
|
|
|
|
|
|
else if ( currtok.Text[1] == '>' )
|
|
|
|
{
|
|
|
|
if ( currtok.Text[2] == '=' )
|
|
|
|
op = Assign_RShift;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = RShift;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
op = Greater;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
case '(':
|
|
|
|
{
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( currtok.Text[1] == ')' )
|
|
|
|
op = FunctionCall;
|
|
|
|
|
|
|
|
else
|
|
|
|
op = Invalid;
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
2023-07-10 19:14:41 -07:00
|
|
|
break;
|
|
|
|
case '[':
|
|
|
|
{
|
|
|
|
if ( currtok.Text[1] == ']' )
|
|
|
|
op = Subscript;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
else
|
|
|
|
op = Invalid;
|
|
|
|
}
|
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( op == Invalid )
|
|
|
|
{
|
|
|
|
log_failure( "gen::%s: Invalid operator '%s'", context, currtok.Text );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// Parse Params
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam params = parse_params( toks, stringize(parse_operator) );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
// Parse Body
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody body = { nullptr };
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( check( TokType::BraceCurly_Open ) )
|
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
body = parse_function_body( toks, stringize(parse_function) );
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( body == Code::Invalid )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
}
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
// OpValidateResult check_result = operator__validate( op, params, ret_type, specifiers );
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeOperator result = def_operator( op, params, ret_type, body, specifiers, attributes, mflags );
|
2023-07-09 22:15:25 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variable parsing is handled in multiple places because its initial signature is shared with function parsing
|
|
|
|
internal inline
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeVar parse_variable_after_name(
|
|
|
|
ModuleFlag mflags
|
|
|
|
, CodeAttributes attributes
|
|
|
|
, CodeSpecifier specifiers
|
|
|
|
, CodeType type
|
|
|
|
, StrC name
|
2023-07-09 22:15:25 -07:00
|
|
|
, Parser::TokArray& toks
|
2023-07-15 12:20:55 -07:00
|
|
|
, char const* context )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
Code array_expr = parse_array_decl( toks, stringize(parse_variable) );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
Code expr = { nullptr };
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
if ( currtok.IsAssign )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
Token expr_tok = currtok;
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Statement_End )
|
|
|
|
{
|
|
|
|
log_failure( "gen::parse_variable: expected expression after assignment operator" );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::Statement_End )
|
|
|
|
{
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
expr_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)expr_tok.Text;
|
2023-07-13 22:37:22 -07:00
|
|
|
expr = untyped_str( expr_tok );
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeVar
|
|
|
|
result = (CodeVar) make_code();
|
2023-07-09 22:15:25 -07:00
|
|
|
result->Type = Variable;
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
result->ModuleFlags = mflags;
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ValueType = type;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if (array_expr )
|
|
|
|
type->ArrExpr = array_expr;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( attributes )
|
|
|
|
result->Attributes = attributes;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( specifiers )
|
|
|
|
result->Specs = specifiers;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
if ( expr )
|
|
|
|
result->Value = expr;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal inline
|
2023-05-01 11:12:07 -07:00
|
|
|
Code parse_variable_assignment( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
Code expr = Code::Invalid;
|
|
|
|
|
|
|
|
if ( currtok.IsAssign )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
Token expr_tok = currtok;
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Statement_End )
|
|
|
|
{
|
|
|
|
log_failure( "gen::parse_variable: expected expression after assignment operator" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::Statement_End )
|
|
|
|
{
|
|
|
|
expr_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)expr_tok.Text;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
expr = untyped_str( expr_tok );
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
internal inline
|
2023-07-15 12:20:55 -07:00
|
|
|
Code parse_operator_function_or_variable( bool expects_function, CodeAttributes attributes, CodeSpecifier specifiers, Parser::TokArray& toks, char const* context )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
Code result = Code::Invalid;
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeType type = parse_type( toks, stringize(parse_variable) );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
if ( type == Code::Invalid )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
if ( check( TokType::Operator) )
|
|
|
|
{
|
|
|
|
// Dealing with an operator overload
|
2023-07-11 22:33:11 -07:00
|
|
|
result = parse_operator_after_ret_type( ModuleFlag::None, attributes, specifiers, type, toks, stringize(parse_template) );
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StrC name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( check( TokType::Capture_Start) )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
|
|
|
// Dealing with a function
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
result = parse_function_after_name( ModuleFlag::None, attributes, specifiers, type, name, toks, stringize(parse_template) );
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( expects_function )
|
|
|
|
{
|
|
|
|
log_failure( "gen::parse_template: expected function declaration (consteval was used)" );
|
|
|
|
return Code::Invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dealing with a variable
|
2023-07-11 22:33:11 -07:00
|
|
|
result = parse_variable_after_name( ModuleFlag::None, attributes, specifiers, type, name, toks, stringize(parse_template) );
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody parse_class_struct_body( Parser::TokType which, Parser::TokArray& toks, char const* context )
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
using namespace Parser;
|
2023-04-23 19:53:30 -07:00
|
|
|
using namespace ECode;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
eat( TokType::BraceCurly_Open );
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-07-10 19:14:41 -07:00
|
|
|
|
|
|
|
if ( which == TokType::Decl_Class )
|
|
|
|
result->Type = Class_Body;
|
|
|
|
|
|
|
|
else
|
|
|
|
result->Type = Struct_Body;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
Code member = Code::Invalid;
|
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
CodeSpecifier specifiers = { nullptr };
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
bool expects_function = false;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
|
|
|
switch ( currtok.Type )
|
|
|
|
{
|
|
|
|
case TokType::Comment:
|
2023-05-06 13:15:49 -07:00
|
|
|
member = def_comment( currtok );
|
2023-05-01 11:12:07 -07:00
|
|
|
eat( TokType::Comment );
|
2023-04-23 19:53:30 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Access_Public:
|
2023-05-01 11:12:07 -07:00
|
|
|
member = access_public;
|
|
|
|
eat( TokType::Access_Public );
|
2023-07-11 00:10:20 -07:00
|
|
|
eat( TokType::Assign_Classifer );
|
2023-05-01 11:12:07 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
case TokType::Access_Protected:
|
2023-05-01 11:12:07 -07:00
|
|
|
member = access_protected;
|
|
|
|
eat( TokType::Access_Protected );
|
2023-07-11 00:10:20 -07:00
|
|
|
eat( TokType::Assign_Classifer );
|
2023-05-01 11:12:07 -07:00
|
|
|
break;
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
case TokType::Access_Private:
|
2023-05-01 11:12:07 -07:00
|
|
|
member = access_private;
|
|
|
|
eat( TokType::Access_Private );
|
2023-07-11 00:10:20 -07:00
|
|
|
eat( TokType::Assign_Classifer );
|
2023-04-23 19:53:30 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Class:
|
|
|
|
member = parse_class( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Enum:
|
|
|
|
member = parse_enum( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Friend:
|
|
|
|
member = parse_friend( toks, context );
|
|
|
|
break;
|
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
case TokType::Decl_Operator:
|
|
|
|
member = parse_operator_cast( toks, context );
|
|
|
|
break;
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
case TokType::Decl_Struct:
|
|
|
|
member = parse_struct( toks, context );
|
|
|
|
break;
|
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
case TokType::Decl_Template:
|
2023-07-09 22:15:25 -07:00
|
|
|
member = parse_template( toks, context );
|
|
|
|
break;
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
case TokType::Decl_Typedef:
|
|
|
|
member = parse_typedef( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Union:
|
|
|
|
member = parse_variable( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Decl_Using:
|
|
|
|
member = parse_using( toks, context );
|
|
|
|
break;
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
case TokType::BraceSquare_Open:
|
|
|
|
case TokType::Attr_Keyword:
|
|
|
|
{
|
|
|
|
// Standard attribute
|
|
|
|
if ( currtok.Type == TokType::BraceSquare_Open )
|
|
|
|
{
|
|
|
|
eat( TokType::BraceSquare_Open );
|
|
|
|
|
|
|
|
if ( currtok.Type != TokType::BraceSquare_Open )
|
|
|
|
{
|
|
|
|
log_failure( "%s: Error, expected attribute name", context );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceSquare_Close )
|
|
|
|
{
|
|
|
|
// TODO : Parse attributes
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::BraceSquare_Close );
|
|
|
|
eat( TokType::BraceSquare_Close );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Platform Specific attribute
|
|
|
|
eat( TokType::Attr_Keyword );
|
|
|
|
eat( TokType::Capture_Start );
|
|
|
|
|
|
|
|
// TODO : Parse attributes
|
|
|
|
|
|
|
|
eat( TokType::Capture_End );
|
|
|
|
}
|
|
|
|
//! Fallthrough intended
|
2023-04-23 19:53:30 -07:00
|
|
|
case TokType::Spec_Consteval:
|
|
|
|
case TokType::Spec_Constexpr:
|
|
|
|
case TokType::Spec_Constinit:
|
|
|
|
case TokType::Spec_Inline:
|
2023-07-09 22:15:25 -07:00
|
|
|
case TokType::Spec_Mutable:
|
2023-04-23 19:53:30 -07:00
|
|
|
case TokType::Spec_Static:
|
|
|
|
case TokType::Spec_Volatile:
|
2023-07-08 11:11:41 -07:00
|
|
|
{
|
2023-05-01 11:12:07 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
|
|
|
|
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
2023-05-01 11:12:07 -07:00
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::Constinit:
|
2023-07-09 22:15:25 -07:00
|
|
|
case ESpecifier::Inline:
|
2023-05-01 11:12:07 -07:00
|
|
|
case ESpecifier::Mutable:
|
2023-07-15 23:26:55 -07:00
|
|
|
case ESpecifier::Static:
|
2023-05-01 11:12:07 -07:00
|
|
|
case ESpecifier::Volatile:
|
|
|
|
break;
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
case ESpecifier::Consteval:
|
|
|
|
expects_function = true;
|
|
|
|
break;
|
|
|
|
|
2023-05-01 11:12:07 -07:00
|
|
|
default:
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::parse_class_struct_body: invalid specifier " "%s" " for variable", ESpecifier::to_str(spec) );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( num_specifiers )
|
|
|
|
{
|
|
|
|
specifiers = def_specifiers( num_specifiers, specs_found );
|
|
|
|
}
|
2023-07-08 11:11:41 -07:00
|
|
|
}
|
2023-07-09 22:15:25 -07:00
|
|
|
//! Fallthrough intentional
|
2023-05-01 11:12:07 -07:00
|
|
|
case TokType::Identifier:
|
|
|
|
case TokType::Spec_Const:
|
2023-04-23 19:53:30 -07:00
|
|
|
case TokType::Type_Unsigned:
|
|
|
|
case TokType::Type_Signed:
|
|
|
|
case TokType::Type_Short:
|
|
|
|
case TokType::Type_Long:
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-07-09 22:15:25 -07:00
|
|
|
member = parse_operator_function_or_variable( expects_function, attributes, specifiers, toks, context );
|
|
|
|
}
|
2023-07-10 22:09:50 -07:00
|
|
|
break;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
default:
|
|
|
|
Token untyped_tok = currtok;
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
while ( left && currtok.Type != TokType::BraceCurly_Close )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-07-09 22:15:25 -07:00
|
|
|
untyped_tok.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)untyped_tok.Text;
|
|
|
|
eat( currtok.Type );
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
member = untyped_str( untyped_tok );
|
2023-07-09 22:15:25 -07:00
|
|
|
break;
|
2023-07-08 20:56:37 -07:00
|
|
|
}
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-07-08 20:56:37 -07:00
|
|
|
if ( member == Code::Invalid )
|
|
|
|
{
|
|
|
|
log_failure( "gen::parse_variable: failed to parse member" );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( member );
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Close );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-10 19:14:41 -07:00
|
|
|
Code parse_class_struct( Parser::TokType which, Parser::TokArray& toks, char const* context )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( which != TokType::Decl_Class && which != TokType::Decl_Struct )
|
|
|
|
{
|
|
|
|
log_failure( "%s: Error, expected class or struct, not %s", context, str_tok_type( which ) );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
Token name { nullptr, 0, TokType::Invalid };
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeType parent = { nullptr };
|
|
|
|
CodeBody body = { nullptr };
|
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
ModuleFlag mflags = ModuleFlag::None;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeClass result = CodeInvalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
// TODO : Parse module specifiers
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
eat( which );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
// TODO : Parse attributes
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
if ( check( TokType::Identifier ) )
|
|
|
|
name = parse_identifier( toks, context );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
AccessSpec access = AccessSpec::Default;
|
2023-07-10 19:14:41 -07:00
|
|
|
Token parent_tok = { nullptr, 0, TokType::Invalid };
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( check( TokType::Assign_Classifer ) )
|
|
|
|
{
|
|
|
|
eat( TokType::Assign_Classifer );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( tok_is_access_specifier( currtok ) )
|
|
|
|
{
|
|
|
|
access = tok_to_access_specifier( currtok );
|
|
|
|
}
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
parent_tok = parse_identifier( toks, context );
|
|
|
|
}
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( check( TokType::BraceCurly_Open ) )
|
|
|
|
{
|
|
|
|
body = parse_class_struct_body( which, toks, context );
|
|
|
|
}
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
eat( TokType::Statement_End );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( parent_tok )
|
|
|
|
parent = def_type( parent_tok );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( which == TokType::Decl_Class )
|
|
|
|
result = def_class( name, body, parent, access
|
|
|
|
// TODO : Set these up later
|
|
|
|
, NoCode // Attributes
|
|
|
|
, ModuleFlag::None
|
|
|
|
);
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
else
|
2023-07-13 22:37:22 -07:00
|
|
|
result = def_struct( name, body, (CodeType)parent, access
|
2023-07-10 19:14:41 -07:00
|
|
|
// TODO : Set these up later
|
|
|
|
, NoCode // Attributes
|
|
|
|
, ModuleFlag::None
|
|
|
|
);
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
return result;
|
|
|
|
}
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
internal
|
|
|
|
Code parse_function_body( Parser::TokArray& toks, char const* context )
|
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
eat( TokType::BraceCurly_Open );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-07-10 19:14:41 -07:00
|
|
|
result->Type = Function_Body;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
Token start = currtok;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
s32 level = 0;
|
2023-07-10 22:09:50 -07:00
|
|
|
while ( left && ( currtok.Type != TokType::BraceCurly_Close || level > 0 ) )
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
if ( currtok.Type == TokType::BraceCurly_Open )
|
|
|
|
level++;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
else if ( currtok.Type == TokType::BraceCurly_Close && level > 0 )
|
|
|
|
level--;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
Token previous = prevtok;
|
|
|
|
|
|
|
|
s32 len = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)start.Text;
|
|
|
|
|
|
|
|
if ( len > 0 )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( def_execution( { len, start.Text } ) );
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
eat( TokType::BraceCurly_Close );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody parse_global_nspace( CodeT which, Parser::TokArray& toks, char const* context )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
using namespace Parser;
|
2023-07-09 09:35:48 -07:00
|
|
|
using namespace ECode;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
if ( which != Namespace_Body && which != Global_Body && which != Export_Body && which != Extern_Linkage_Body )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
if ( which != Global_Body )
|
|
|
|
eat( TokType::BraceCurly_Open );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody
|
|
|
|
result = (CodeBody) make_code();
|
2023-07-09 09:35:48 -07:00
|
|
|
result->Type = which;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
while ( left && currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
Code member = Code::Invalid;
|
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
CodeSpecifier specifiers = { nullptr };
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
bool expects_function = false;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
switch ( currtok.Type )
|
|
|
|
{
|
|
|
|
case TokType::Comment:
|
|
|
|
member = def_comment( currtok );
|
|
|
|
eat( TokType::Comment );
|
|
|
|
break;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
case TokType::Decl_Enum:
|
|
|
|
member = parse_enum( toks, context);
|
|
|
|
break;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
case TokType::Decl_Class:
|
|
|
|
member = parse_class( toks, context );
|
|
|
|
break;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
case TokType::Decl_Extern_Linkage:
|
|
|
|
if ( which == Extern_Linkage_Body )
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::parse_global_nspace: nested extern linkage" );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
member = parse_extern_link_body( toks, context );
|
|
|
|
break;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
case TokType::Decl_Namespace:
|
|
|
|
member = parse_namespace( toks, context );
|
|
|
|
break;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
case TokType::Decl_Struct:
|
|
|
|
member = parse_struct( toks, context );
|
|
|
|
break;
|
2023-04-18 21:56:43 -07:00
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
case TokType::Decl_Template:
|
2023-07-09 22:15:25 -07:00
|
|
|
member = parse_template( toks, context );
|
|
|
|
break;
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
case TokType::Decl_Typedef:
|
|
|
|
member = parse_typedef( toks, context );
|
|
|
|
break;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
case TokType::Decl_Union:
|
|
|
|
member = parse_union( toks, context );
|
|
|
|
break;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
case TokType::Decl_Using:
|
|
|
|
member = parse_using( toks, context );
|
|
|
|
break;
|
2023-04-18 21:56:43 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
case TokType::Module_Export:
|
|
|
|
if ( which == Export_Body )
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::parse_global_nspace: nested export declaration" );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
member = parse_export_body( toks, context );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TokType::Module_Import:
|
|
|
|
{
|
|
|
|
not_implemented();
|
|
|
|
}
|
|
|
|
//! Fallthrough intentional
|
|
|
|
case TokType::BraceSquare_Open:
|
|
|
|
case TokType::Attr_Keyword:
|
|
|
|
{
|
|
|
|
not_implemented();
|
|
|
|
|
|
|
|
// Standard attribute
|
|
|
|
if ( currtok.Type == TokType::BraceSquare_Open )
|
|
|
|
{
|
|
|
|
eat( TokType::BraceSquare_Open );
|
|
|
|
|
|
|
|
if ( currtok.Type != TokType::BraceSquare_Open )
|
|
|
|
{
|
|
|
|
log_failure( "%s: Error, expected attribute name", context );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ( left && currtok.Type != TokType::BraceSquare_Close )
|
|
|
|
{
|
|
|
|
// TODO : Parse attributes
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::BraceSquare_Close );
|
|
|
|
eat( TokType::BraceSquare_Close );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Platform Specific attribute
|
|
|
|
eat( TokType::Attr_Keyword );
|
|
|
|
eat( TokType::Capture_Start );
|
2023-07-08 20:29:18 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// TODO : Parse attributes
|
|
|
|
|
|
|
|
eat( TokType::Capture_End );
|
|
|
|
}
|
|
|
|
//! Fallthrough intentional
|
|
|
|
case TokType::Spec_Consteval:
|
|
|
|
case TokType::Spec_Constexpr:
|
|
|
|
case TokType::Spec_Constinit:
|
2023-07-08 20:29:18 -07:00
|
|
|
case TokType::Spec_Extern:
|
|
|
|
case TokType::Spec_Global:
|
|
|
|
case TokType::Spec_Inline:
|
|
|
|
case TokType::Spec_Internal_Linkage:
|
|
|
|
case TokType::Spec_Static:
|
|
|
|
{
|
2023-07-09 22:15:25 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
2023-07-08 20:29:18 -07:00
|
|
|
|
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
2023-07-09 22:15:25 -07:00
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::Constinit:
|
2023-07-08 20:29:18 -07:00
|
|
|
case ESpecifier::Inline:
|
2023-07-09 22:15:25 -07:00
|
|
|
case ESpecifier::Mutable:
|
2023-07-15 23:26:55 -07:00
|
|
|
case ESpecifier::Static:
|
2023-07-09 22:15:25 -07:00
|
|
|
case ESpecifier::Volatile:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ESpecifier::Consteval:
|
|
|
|
expects_function = true;
|
2023-07-08 20:29:18 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::parse_global_nspace: invalid specifier " "%s" " for variable", ESpecifier::to_str(spec) );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-08 20:29:18 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
2023-07-08 20:29:18 -07:00
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
if ( num_specifiers )
|
2023-07-08 20:29:18 -07:00
|
|
|
{
|
2023-07-09 22:15:25 -07:00
|
|
|
specifiers = def_specifiers( num_specifiers, specs_found );
|
2023-07-08 20:29:18 -07:00
|
|
|
}
|
|
|
|
}
|
2023-07-09 22:15:25 -07:00
|
|
|
//! Fallthrough intentional
|
2023-07-08 20:29:18 -07:00
|
|
|
case TokType::Identifier:
|
|
|
|
case TokType::Spec_Const:
|
|
|
|
case TokType::Type_Long:
|
|
|
|
case TokType::Type_Short:
|
|
|
|
case TokType::Type_Signed:
|
|
|
|
case TokType::Type_Unsigned:
|
|
|
|
{
|
2023-07-09 22:15:25 -07:00
|
|
|
member = parse_operator_function_or_variable( expects_function, attributes, specifiers, toks, context );
|
2023-07-08 20:29:18 -07:00
|
|
|
}
|
2023-07-08 20:56:37 -07:00
|
|
|
}
|
2023-07-08 20:29:18 -07:00
|
|
|
|
2023-07-08 20:56:37 -07:00
|
|
|
if ( member == Code::Invalid )
|
|
|
|
{
|
2023-07-09 09:35:48 -07:00
|
|
|
log_failure( "gen::%s: failed to parse extern linkage member", context );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-08 20:29:18 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result.append( member );
|
2023-07-08 20:29:18 -07:00
|
|
|
}
|
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
if ( which != Global_Body )
|
|
|
|
eat( TokType::BraceCurly_Close );
|
|
|
|
|
2023-07-08 20:29:18 -07:00
|
|
|
return result;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeClass parse_class( Parser::TokArray& toks, char const* context )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
return (CodeClass) parse_class_struct( Parser::TokType::Decl_Class, toks, context );
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeClass parse_class( StrC def )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
|
|
|
check_parse_args( parse_class, def );
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
return (CodeClass) parse_class_struct( TokType::Decl_Class, toks, stringize(parse_class) );
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeEnum parse_enum( Parser::TokArray& toks, char const* context )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
|
|
|
|
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
Code array_expr = { nullptr };
|
|
|
|
CodeType type = { nullptr };
|
|
|
|
Token body = { nullptr, 0, TokType::Invalid };
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
char entries_code[ kilobytes(128) ] { 0 };
|
|
|
|
s32 entries_length = 0;
|
|
|
|
|
|
|
|
bool is_enum_class = false;
|
|
|
|
|
|
|
|
eat( TokType::Decl_Enum );
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Decl_Class )
|
|
|
|
{
|
|
|
|
eat( TokType::Decl_Class);
|
|
|
|
is_enum_class = true;
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// TODO : Parse attributes
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
if ( currtok.Type != TokType::Identifier )
|
|
|
|
{
|
|
|
|
log_failure( "gen::parse_enum: expected identifier for enum name" );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Assign_Classifer )
|
|
|
|
{
|
|
|
|
eat( TokType::Assign_Classifer );
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
type = parse_type( toks, stringize(parse_enum) );
|
2023-07-09 09:35:48 -07:00
|
|
|
if ( type == Code::Invalid )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::BraceCurly_Open )
|
|
|
|
{
|
|
|
|
eat( TokType::BraceCurly_Open );
|
|
|
|
|
|
|
|
body = currtok;
|
|
|
|
|
|
|
|
while ( currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
|
|
|
eat( TokType::Identifier);
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Operator && currtok.Text[0] == '=' )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
while ( currtok.Type != TokType::Comma && currtok.Type != TokType::BraceCurly_Close )
|
|
|
|
{
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currtok.Type == TokType::Comma )
|
|
|
|
{
|
|
|
|
eat( TokType::Comma );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
body.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)body.Text;
|
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Close );
|
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
using namespace ECode;
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeEnum
|
|
|
|
result = (CodeEnum) make_code();
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
if ( body.Length )
|
|
|
|
{
|
|
|
|
// mem_copy( entries_code, body.Text, body.Length );
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
Code untyped_body = untyped_str( body );
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
result->Type = is_enum_class ? Enum_Class : Enum;
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = untyped_body;
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = is_enum_class ? Enum_Class_Fwd : Enum_Fwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
|
|
|
|
if ( type )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->UnderlyingType = type;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeEnum parse_enum( StrC def )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
|
|
|
check_parse_args( parse_enum, def );
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
return parse_enum( toks, stringize(parse_enum) );
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
internal inline
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody parse_export_body( Parser::TokArray& toks, char const* context )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
|
|
|
return parse_global_nspace( ECode::Export_Body, toks, context );
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody parse_export_body( StrC def )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
|
|
|
check_parse_args( parse_export_body, def );
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return parse_export_body( toks, stringize(parse_export_body) );
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
internal inline
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody parse_extern_link_body( Parser::TokArray& toks, char const* context )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
|
|
|
return parse_global_nspace( ECode::Extern_Linkage_Body, toks, context );
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
2023-07-16 09:08:57 -07:00
|
|
|
CodeExtern parse_extern_link( Parser::TokArray& toks, char const* context )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-07-08 20:29:18 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
eat( TokType::Decl_Extern_Linkage );
|
|
|
|
|
|
|
|
Token name = currtok;
|
|
|
|
eat( TokType::String );
|
|
|
|
|
|
|
|
name.Text += 1;
|
|
|
|
name.Length -= 1;
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeExtern
|
|
|
|
result = (CodeExtern) make_code();
|
2023-07-08 20:29:18 -07:00
|
|
|
result->Type = ECode::Extern_Linkage;
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
|
|
|
|
Code entry = parse_extern_link_body( toks, context );
|
|
|
|
if ( entry == Code::Invalid )
|
|
|
|
{
|
|
|
|
log_failure( "gen::parse_extern_link: failed to parse body" );
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = entry;
|
2023-07-08 20:29:18 -07:00
|
|
|
|
|
|
|
return result;
|
2023-05-01 11:12:07 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeExtern parse_extern_link( StrC def )
|
2023-05-01 11:12:07 -07:00
|
|
|
{
|
2023-07-08 20:29:18 -07:00
|
|
|
check_parse_args( parse_extern_link, def );
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-07-08 20:29:18 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
return parse_extern_link( toks, stringize(parse_extern_link) );
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeFriend parse_friend( Parser::TokArray& toks, char const* context )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-04-18 19:47:59 -07:00
|
|
|
eat( TokType::Decl_Friend );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeFn function = { nullptr };
|
2023-04-23 16:04:43 -07:00
|
|
|
|
|
|
|
// Type declaration or return type
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeType type = parse_type( toks, stringize(parse_friend) );
|
2023-04-23 16:04:43 -07:00
|
|
|
if ( type == Code::Invalid )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
// Funciton declaration
|
|
|
|
if ( currtok.Type == TokType::Identifier )
|
|
|
|
{
|
|
|
|
// Name
|
2023-07-11 22:33:11 -07:00
|
|
|
Token name = parse_identifier( toks, stringize(parse_friend) );
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
// Parameter list
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeParam params = parse_params( toks, stringize(parse_friend) );
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
function = make_code();
|
|
|
|
function->Type = Function_Fwd;
|
|
|
|
function->Name = get_cached_string( name );
|
|
|
|
function->ReturnType = type;
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
if ( params )
|
2023-07-15 12:20:55 -07:00
|
|
|
function->Params = params;
|
2023-04-23 16:04:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeFriend
|
|
|
|
result = (CodeFriend) make_code();
|
2023-04-23 16:04:43 -07:00
|
|
|
result->Type = Friend;
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
if ( function )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Declaration = function;
|
2023-04-19 12:40:23 -07:00
|
|
|
|
2023-04-23 16:04:43 -07:00
|
|
|
else
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Declaration = type;
|
2023-04-19 12:40:23 -07:00
|
|
|
|
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeFriend parse_friend( StrC def )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
check_parse_args( parse_friend, def );
|
2023-05-01 11:12:07 -07:00
|
|
|
using namespace Parser;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-04-23 19:53:30 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
return parse_friend( toks, stringize(parse_friend) );
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeFn parse_functon( Parser::TokArray& toks, char const* context )
|
2023-04-03 23:04:19 -07:00
|
|
|
{
|
2023-04-22 19:24:55 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
CodeSpecifier specifiers = { nullptr };
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// TODO : Parse module specifiers
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// TODO : Parse attributes
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
|
|
|
{
|
2023-05-06 14:23:17 -07:00
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-23 19:53:30 -07:00
|
|
|
switch ( spec )
|
|
|
|
{
|
2023-07-10 19:14:41 -07:00
|
|
|
case ESpecifier::Const:
|
|
|
|
case ESpecifier::Consteval:
|
2023-04-23 19:53:30 -07:00
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::External_Linkage:
|
2023-07-10 19:14:41 -07:00
|
|
|
case ESpecifier::Inline:
|
2023-07-15 23:26:55 -07:00
|
|
|
case ESpecifier::Static:
|
2023-04-23 19:53:30 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::parse_functon: invalid specifier " "%s" " for functon", ESpecifier::to_str(spec) );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( num_specifiers )
|
|
|
|
{
|
|
|
|
specifiers = def_specifiers( num_specifiers, specs_found );
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeType ret_type = parse_type( toks, stringize(parse_function) );
|
2023-04-23 19:53:30 -07:00
|
|
|
if ( ret_type == Code::Invalid )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
Token name = parse_identifier( toks, stringize(parse_function) );
|
2023-04-23 19:53:30 -07:00
|
|
|
if ( ! name )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeFn result = parse_function_after_name( ModuleFlag::None, attributes, specifiers, ret_type, name, toks, context );
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-04-02 09:35:14 -07:00
|
|
|
return result;
|
|
|
|
}
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeFn parse_function( StrC def )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
check_parse_args( parse_function, def );
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-04-23 19:53:30 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
return (CodeFn) parse_functon( toks, stringize(parse_function) );
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody parse_global_body( StrC def )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
2023-07-09 09:35:48 -07:00
|
|
|
check_parse_args( parse_global_body, def );
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return parse_global_nspace( ECode::Global_Body, toks, stringize(parse_global_body) );
|
2023-05-05 15:10:03 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeNamespace parse_namespace( Parser::TokArray& toks, char const* context )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-07-09 09:35:48 -07:00
|
|
|
eat( TokType::Decl_Namespace );
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
Token name = parse_identifier( toks, stringize(parse_namespace) );
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody body = parse_global_nspace( ECode::Namespace_Body, toks, stringize(parse_namespace) );
|
2023-07-09 09:35:48 -07:00
|
|
|
if ( body == Code::Invalid )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeNamespace
|
|
|
|
result = (CodeNamespace) make_code();
|
2023-07-09 09:35:48 -07:00
|
|
|
result->Type = ECode::Namespace;
|
|
|
|
result->Name = get_cached_string( name );
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = body;
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
return result;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeNamespace parse_namespace( StrC def )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-07-09 09:35:48 -07:00
|
|
|
check_parse_args( parse_namespace, def );
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
return parse_namespace( toks, stringize(parse_namespace) );
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeOperator parse_operator( Parser::TokArray& toks, char const* context )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
2023-07-10 19:14:41 -07:00
|
|
|
// TODO : Parse Module specifier
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
// TODO : Parse Attributes
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeAttributes attributes = { nullptr };
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeSpecifier specifiers = { nullptr };
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
|
|
|
|
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::Const:
|
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::Inline:
|
2023-07-15 23:26:55 -07:00
|
|
|
case ESpecifier::Static:
|
2023-07-10 19:14:41 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::parse_operator: invalid specifier " "%s" " for operator", ESpecifier::to_str(spec) );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( num_specifiers )
|
|
|
|
{
|
|
|
|
specifiers = def_specifiers( num_specifiers, specs_found );
|
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
// Parse Return Type
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeType ret_type = parse_type( toks, stringize(parse_operator) );
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeOperator result = parse_operator_after_ret_type( ModuleFlag::None, attributes, specifiers, ret_type, toks, context );
|
2023-07-09 22:15:25 -07:00
|
|
|
return result;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeOperator parse_operator( StrC def )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-07-09 09:35:48 -07:00
|
|
|
check_parse_args( parse_operator, def );
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
return (CodeOperator) parse_operator( toks, stringize(parse_operator) );
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeOpCast parse_operator_cast( Parser::TokArray& toks, char const* context )
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
eat( TokType::Decl_Operator );
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
Code type = parse_type( toks, stringize(parse_operator_cast) );
|
2023-07-10 19:14:41 -07:00
|
|
|
|
|
|
|
eat( TokType::Capture_Start );
|
|
|
|
eat( TokType::Capture_End );
|
|
|
|
|
|
|
|
Code body = { nullptr };
|
|
|
|
|
|
|
|
if ( check( TokType::BraceCurly_Open) )
|
|
|
|
{
|
|
|
|
eat( TokType::BraceCurly_Open );
|
|
|
|
|
|
|
|
Token body_str = currtok;
|
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
s32 level = 0;
|
|
|
|
while ( left && ( currtok.Type != TokType::BraceCurly_Close || level > 0 ) )
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
2023-07-10 22:09:50 -07:00
|
|
|
if ( currtok.Type == TokType::BraceCurly_Open )
|
|
|
|
level++;
|
|
|
|
|
|
|
|
else if ( currtok.Type == TokType::BraceCurly_Close && level > 0 )
|
|
|
|
level--;
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
body_str.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)body_str.Text;
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
body = untyped_str( body_str );
|
2023-07-10 22:09:50 -07:00
|
|
|
|
|
|
|
eat( TokType::BraceCurly_Close );
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeOpCast result = (CodeOpCast) make_code();
|
2023-07-10 19:14:41 -07:00
|
|
|
|
|
|
|
if (body)
|
|
|
|
{
|
|
|
|
result->Type = ECode::Operator_Cast;
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = body;
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = ECode::Operator_Cast_Fwd;
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->ValueType = type;
|
2023-07-10 22:09:50 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeOpCast parse_operator_cast( StrC def )
|
2023-07-10 19:14:41 -07:00
|
|
|
{
|
|
|
|
check_parse_args( parse_operator_cast, def );
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-07-10 19:14:41 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
return parse_operator_cast( toks, stringize(parse_operator_cast) );
|
2023-07-10 19:14:41 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
internal inline
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeStruct parse_struct( Parser::TokArray& toks, char const* context )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
2023-07-15 12:20:55 -07:00
|
|
|
return (CodeStruct) parse_class_struct( Parser::TokType::Decl_Struct, toks, stringize(parse_struct) );
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeStruct parse_struct( StrC def )
|
2023-04-02 09:35:14 -07:00
|
|
|
{
|
2023-07-08 15:49:49 -07:00
|
|
|
check_parse_args( parse_struct, def );
|
|
|
|
using namespace Parser;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
return (CodeStruct) parse_class_struct( TokType::Decl_Struct, toks, stringize(parse_struct) );
|
2023-07-08 15:49:49 -07:00
|
|
|
}
|
2023-04-03 23:04:19 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeTemplate parse_template( Parser::TokArray& toks, char const* context )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
2023-07-10 19:14:41 -07:00
|
|
|
# define UseTemplateCapture true
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
// TODO : Parse Module specifier
|
|
|
|
|
2023-07-10 22:09:50 -07:00
|
|
|
eat( TokType::Decl_Template );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
Code params = parse_params( toks, stringize(parse_template), UseTemplateCapture );
|
2023-07-09 22:15:25 -07:00
|
|
|
if ( params == Code::Invalid )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
Code definition = { nullptr };
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
while ( left )
|
|
|
|
{
|
|
|
|
if ( check( TokType::Decl_Class ) )
|
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
definition = parse_class( toks, stringize(parse_template) );
|
2023-07-09 22:15:25 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( check( TokType::Decl_Struct ) )
|
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
definition = parse_enum( toks, stringize(parse_template) );
|
2023-07-09 22:15:25 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( check( TokType::Decl_Using ))
|
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
definition = parse_using( toks, stringize(parse_template) );
|
2023-07-09 22:15:25 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Its either a function or a variable
|
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
CodeSpecifier specifiers = { nullptr };
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
// TODO : Parse attributes
|
|
|
|
|
|
|
|
bool expects_function = false;
|
|
|
|
|
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
|
|
|
|
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
|
|
|
case ESpecifier::Const:
|
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::Constinit:
|
|
|
|
case ESpecifier::External_Linkage:
|
|
|
|
case ESpecifier::Global:
|
|
|
|
case ESpecifier::Inline:
|
|
|
|
case ESpecifier::Local_Persist:
|
|
|
|
case ESpecifier::Mutable:
|
2023-07-15 23:26:55 -07:00
|
|
|
case ESpecifier::Static:
|
2023-07-09 22:15:25 -07:00
|
|
|
case ESpecifier::Thread_Local:
|
|
|
|
case ESpecifier::Volatile:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ESpecifier::Consteval:
|
|
|
|
expects_function = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::parse_template: invalid specifier %s for variable or function", ESpecifier::to_str( spec ) );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore const it will be handled by the type
|
|
|
|
if ( spec == ESpecifier::Const )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( num_specifiers )
|
|
|
|
{
|
|
|
|
specifiers = def_specifiers( num_specifiers, specs_found );
|
|
|
|
}
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
definition = parse_operator_function_or_variable( expects_function, attributes, specifiers, toks, stringize(parse_template) );
|
2023-07-10 19:14:41 -07:00
|
|
|
break;
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeTemplate
|
|
|
|
result = (CodeTemplate) make_code();
|
|
|
|
result->Type = ECode::Template;
|
|
|
|
result->Params = params;
|
|
|
|
result->Declaration = definition;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
return result;
|
|
|
|
# undef UseTemplateCapture
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeTemplate parse_template( StrC def )
|
2023-07-09 22:15:25 -07:00
|
|
|
{
|
|
|
|
check_parse_args( parse_template, def );
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
return parse_template( toks, stringize(parse_template) );
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeType parse_type( Parser::TokArray& toks, char const* context )
|
2023-07-08 15:49:49 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
2023-04-18 21:56:43 -07:00
|
|
|
|
2023-07-11 00:10:20 -07:00
|
|
|
Token context_tok = prevtok;
|
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
2023-04-03 00:55:28 -07:00
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
Token brute_sig = { currtok.Text, 0, TokType::Invalid };
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
2023-05-01 11:12:07 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
if ( spec != ESpecifier::Const )
|
2023-07-08 15:49:49 -07:00
|
|
|
{
|
|
|
|
log_failure( "gen::parse_type: Error, invalid specifier used in type definition: %s", currtok.Text );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-08 15:49:49 -07:00
|
|
|
}
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
if ( left == 0 )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-07-08 15:49:49 -07:00
|
|
|
log_failure( "%s: Error, unexpected end of type definition", context );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
if ( currtok.Type == TokType::Decl_Class
|
|
|
|
|| currtok.Type == TokType::Decl_Struct )
|
|
|
|
{
|
|
|
|
name = currtok;
|
|
|
|
eat( currtok.Type );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
name.Length = ( (sptr)currtok.Text + currtok.Length ) - (sptr)name.Text;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
}
|
|
|
|
else if ( currtok.Type >= TokType::Type_Unsigned )
|
|
|
|
{
|
|
|
|
name = currtok;
|
|
|
|
eat( currtok.Type );
|
2023-04-18 21:56:43 -07:00
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
while (currtok.Type >= TokType::Type_Unsigned)
|
|
|
|
{
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
name.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)name.Text;
|
2023-07-08 15:49:49 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
name = parse_identifier( toks, context );
|
|
|
|
if ( ! name )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-11 15:29:45 -07:00
|
|
|
|
|
|
|
// Problably dealing with a templated symbol
|
|
|
|
if ( currtok.Type == TokType::Operator && currtok.Text[0] == '<' && currtok.Length == 1 )
|
|
|
|
{
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
s32 level = 0;
|
|
|
|
while ( left && ( currtok.Text[0] != '>' || level > 0 ))
|
|
|
|
{
|
|
|
|
if ( currtok.Text[0] == '<' )
|
|
|
|
level++;
|
|
|
|
|
|
|
|
if ( currtok.Text[0] == '>' )
|
|
|
|
level--;
|
|
|
|
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat( TokType::Operator );
|
|
|
|
|
|
|
|
// Extend length of name to last token
|
|
|
|
name.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)name.Text;
|
|
|
|
}
|
2023-04-11 22:24:43 -07:00
|
|
|
}
|
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
|
|
|
{
|
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
if ( spec != ESpecifier::Const
|
|
|
|
&& spec != ESpecifier::Ptr
|
|
|
|
&& spec != ESpecifier::Ref
|
|
|
|
&& spec != ESpecifier::RValue )
|
|
|
|
{
|
|
|
|
log_failure( "%s: Error, invalid specifier used in type definition: %s", context, currtok.Text );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-07-08 15:49:49 -07:00
|
|
|
}
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( currtok.Type );
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-11 00:10:20 -07:00
|
|
|
// Not sure if its technically possible to cast ot a function pointer user defined operator cast...
|
|
|
|
// Supporting it is not worth the effort.
|
|
|
|
if ( check( TokType::Capture_Start ) && context_tok.Type != TokType::Decl_Operator )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
|
|
|
// Its a function type
|
|
|
|
eat( TokType::Capture_Start );
|
|
|
|
|
|
|
|
while ( check( TokType::Star ) || currtok.Type == TokType::Spec_Const )
|
|
|
|
{
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
2023-07-11 00:10:20 -07:00
|
|
|
// if its a using statement there will not be an ID.
|
|
|
|
if ( check( TokType::Identifier ) )
|
|
|
|
eat(TokType::Identifier);
|
2023-07-09 09:35:48 -07:00
|
|
|
|
|
|
|
eat( TokType::Capture_End );
|
|
|
|
|
|
|
|
// Parameters
|
|
|
|
|
|
|
|
eat( TokType::Capture_Start );
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// TODO : Change this to validate the parameters...
|
2023-07-09 09:35:48 -07:00
|
|
|
// Bruteforce lex the parameters, no validation.
|
2023-07-11 00:10:20 -07:00
|
|
|
s32 level = 0;
|
|
|
|
while ( ! check( TokType::Capture_End ) || level > 0 )
|
2023-07-09 09:35:48 -07:00
|
|
|
{
|
2023-07-11 00:10:20 -07:00
|
|
|
if ( check( TokType::Capture_Start ) )
|
|
|
|
level++;
|
|
|
|
|
|
|
|
if ( check( TokType::Capture_End ) )
|
|
|
|
level--;
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
eat(TokType::Capture_End);
|
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
brute_sig.Length = ( (sptr)prevtok.Text + prevtok.Length ) - (sptr)brute_sig.Text;
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
using namespace ECode;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeType
|
|
|
|
result = (CodeType) make_code();
|
2023-07-08 15:49:49 -07:00
|
|
|
result->Type = Typename;
|
2023-07-08 14:14:05 -07:00
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
if ( brute_sig.Length > 0 )
|
2023-07-08 15:49:49 -07:00
|
|
|
{
|
2023-07-11 00:10:20 -07:00
|
|
|
// Bruteforce all tokens together.
|
2023-07-11 15:29:45 -07:00
|
|
|
name = brute_sig;
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (num_specifiers)
|
|
|
|
{
|
|
|
|
Code specifiers = def_specifiers( num_specifiers, (SpecifierT*)specs_found );
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Specs = specifiers;
|
2023-07-09 09:35:48 -07:00
|
|
|
}
|
2023-07-08 15:49:49 -07:00
|
|
|
}
|
2023-07-08 14:14:05 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
|
2023-07-08 15:49:49 -07:00
|
|
|
return result;
|
2023-07-08 14:14:05 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeType parse_type( StrC def )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
check_parse_args( parse_type, def );
|
2023-05-01 11:12:07 -07:00
|
|
|
using namespace Parser;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
CodeType result = parse_type( toks, stringize(parse_type) );
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeTypedef parse_typedef( Parser::TokArray& toks, char const* context )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-04-23 11:33:37 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
Code array_expr = { nullptr };
|
|
|
|
CodeType type = { nullptr };
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Decl_Typedef );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
type = parse_type( toks, stringize(parse_typedef) );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-08 14:14:05 -07:00
|
|
|
if ( ! check( TokType::Identifier ) )
|
2023-04-18 21:56:43 -07:00
|
|
|
{
|
|
|
|
log_failure( "gen::parse_typedef: Error, expected identifier for typedef" );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-04-11 22:24:43 -07:00
|
|
|
}
|
2023-04-18 21:56:43 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
name = currtok;
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
array_expr = parse_array_decl( toks, stringize(parse_typedef) );
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Statement_End );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
|
|
|
using namespace ECode;
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeTypedef
|
|
|
|
result = (CodeTypedef) make_code();
|
2023-04-11 22:24:43 -07:00
|
|
|
result->Type = Typedef;
|
2023-05-06 14:23:17 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
result->UnderlyingType = type;
|
2023-04-11 22:24:43 -07:00
|
|
|
|
2023-07-08 14:14:05 -07:00
|
|
|
if ( array_expr && array_expr->Type != Invalid )
|
2023-07-15 12:20:55 -07:00
|
|
|
type->ArrExpr = array_expr;
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-04-11 22:24:43 -07:00
|
|
|
return result;
|
2023-04-07 21:29:09 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeTypedef parse_typedef( StrC def )
|
2023-04-07 21:29:09 -07:00
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
check_parse_args( parse_typedef, def );
|
2023-05-01 11:12:07 -07:00
|
|
|
using namespace Parser;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
return parse_typedef( toks, stringize(parse_typedef) );
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeUnion parse_union( Parser::TokArray& toks, char const* context )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// TODO : Parse module spec
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
eat( TokType::Decl_Union );
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
// TODO : Parse attributes
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeAttributes attributes = { nullptr };
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
StrC name = { 0, nullptr };
|
|
|
|
|
|
|
|
if ( check( TokType::Identifier ) )
|
|
|
|
{
|
|
|
|
name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeBody body = { nullptr };
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
eat( TokType::BraceCurly_Open );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
body = make_code();
|
|
|
|
body->Type = ECode::Union_Body;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
while ( ! check( TokType::BraceCurly_Close ) )
|
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
Code entry = parse_variable( toks, stringize(parse_union) );
|
2023-07-09 22:15:25 -07:00
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
if ( entry )
|
2023-07-15 12:20:55 -07:00
|
|
|
body.append( entry );
|
2023-07-09 22:15:25 -07:00
|
|
|
}
|
|
|
|
|
2023-07-10 19:14:41 -07:00
|
|
|
eat( TokType::BraceCurly_Close );
|
2023-07-09 22:15:25 -07:00
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeUnion
|
|
|
|
result = (CodeUnion) make_code();
|
2023-07-09 22:15:25 -07:00
|
|
|
result->Type = ECode::Union;
|
|
|
|
|
|
|
|
if ( name )
|
|
|
|
result->Name = get_cached_string( name );
|
|
|
|
|
|
|
|
if ( body )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Body = body;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
if ( attributes )
|
2023-07-15 12:20:55 -07:00
|
|
|
result->Attributes = attributes;
|
2023-07-09 22:15:25 -07:00
|
|
|
|
|
|
|
return result;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeUnion parse_union( StrC def )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
2023-07-09 09:35:48 -07:00
|
|
|
check_parse_args( parse_union, def );
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
TokArray toks = lex( def );
|
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
return parse_union( toks, stringize(parse_union) );
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-16 09:08:57 -07:00
|
|
|
CodeUsing parse_using( Parser::TokArray& toks, char const* context )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
Code array_expr = { nullptr };
|
|
|
|
CodeType type = { nullptr };
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
bool is_namespace = false;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// TODO : Parse module specs
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Decl_Using );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
if ( currtok.Type == TokType::Decl_Namespace )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-04-18 21:56:43 -07:00
|
|
|
is_namespace = true;
|
|
|
|
eat( TokType::Decl_Namespace );
|
2023-04-18 19:47:59 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
name = currtok;
|
2023-04-18 21:56:43 -07:00
|
|
|
eat( TokType::Identifier );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
if ( currtok.IsAssign )
|
2023-04-18 19:47:59 -07:00
|
|
|
{
|
2023-07-09 22:15:25 -07:00
|
|
|
// TODO : Parse Attributes (using type-alias)
|
2023-07-09 09:35:48 -07:00
|
|
|
|
2023-04-23 11:33:37 -07:00
|
|
|
eat( TokType::Operator );
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
type = parse_type( toks, stringize(parse_typedef) );
|
2023-04-23 11:33:37 -07:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
array_expr = parse_array_decl( toks, stringize(parse_typedef) );
|
2023-04-18 21:56:43 -07:00
|
|
|
|
|
|
|
eat( TokType::Statement_End );
|
|
|
|
|
|
|
|
using namespace ECode;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
CodeUsing
|
|
|
|
result = (CodeUsing) make_code();
|
2023-07-09 09:35:48 -07:00
|
|
|
result->Name = get_cached_string( name );
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-15 20:07:30 -07:00
|
|
|
if ( is_namespace)
|
|
|
|
{
|
|
|
|
result->Type = Using_Namespace;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result->Type = Using;
|
2023-04-18 19:47:59 -07:00
|
|
|
|
2023-07-15 20:07:30 -07:00
|
|
|
if ( type )
|
2023-07-16 09:08:57 -07:00
|
|
|
result->UnderlyingType = type;
|
2023-04-23 11:33:37 -07:00
|
|
|
|
2023-07-15 20:07:30 -07:00
|
|
|
if ( array_expr )
|
|
|
|
type->ArrExpr = array_expr;
|
|
|
|
}
|
2023-04-18 19:47:59 -07:00
|
|
|
return result;
|
2023-04-23 19:53:30 -07:00
|
|
|
}
|
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
CodeUsing parse_using( StrC def )
|
2023-04-23 19:53:30 -07:00
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
check_parse_args( parse_using, def );
|
2023-05-01 11:12:07 -07:00
|
|
|
using namespace Parser;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-05-06 13:15:49 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-04-23 19:53:30 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-04-23 19:53:30 -07:00
|
|
|
|
2023-07-16 09:08:57 -07:00
|
|
|
return parse_using( toks, stringize(parse_using) );
|
2023-04-02 09:35:14 -07:00
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-07-09 09:35:48 -07:00
|
|
|
internal
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeVar parse_variable( Parser::TokArray& toks, char const* context )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
|
|
|
using namespace Parser;
|
|
|
|
|
|
|
|
Token name = { nullptr, 0, TokType::Invalid };
|
|
|
|
|
|
|
|
SpecifierT specs_found[16] { ESpecifier::Num_Specifiers };
|
|
|
|
s32 num_specifiers = 0;
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeAttributes attributes = { nullptr };
|
|
|
|
CodeSpecifier specifiers = { nullptr };
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// TODO : Parse module specifiers
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// TODO : Parse attributes
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
while ( left && tok_is_specifier( currtok ) )
|
|
|
|
{
|
2023-05-06 13:15:49 -07:00
|
|
|
SpecifierT spec = ESpecifier::to_type( currtok );
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
switch ( spec )
|
|
|
|
{
|
2023-07-09 22:15:25 -07:00
|
|
|
case ESpecifier::Const:
|
2023-05-05 15:10:03 -07:00
|
|
|
case ESpecifier::Constexpr:
|
|
|
|
case ESpecifier::Constinit:
|
|
|
|
case ESpecifier::External_Linkage:
|
2023-07-09 22:15:25 -07:00
|
|
|
case ESpecifier::Global:
|
|
|
|
case ESpecifier::Inline:
|
2023-05-05 15:10:03 -07:00
|
|
|
case ESpecifier::Local_Persist:
|
|
|
|
case ESpecifier::Mutable:
|
2023-07-15 23:26:55 -07:00
|
|
|
case ESpecifier::Static:
|
2023-05-05 15:10:03 -07:00
|
|
|
case ESpecifier::Thread_Local:
|
|
|
|
case ESpecifier::Volatile:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2023-07-11 22:33:11 -07:00
|
|
|
log_failure( "gen::parse_variable: invalid specifier %s for variable", ESpecifier::to_str( spec ) );
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-05-05 15:10:03 -07:00
|
|
|
}
|
|
|
|
|
2023-07-09 22:15:25 -07:00
|
|
|
// Ignore const specifiers, they're handled by the type
|
|
|
|
if ( spec == ESpecifier::Const )
|
2023-05-05 15:10:03 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
specs_found[num_specifiers] = spec;
|
|
|
|
num_specifiers++;
|
|
|
|
eat( currtok.Type );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( num_specifiers )
|
|
|
|
{
|
|
|
|
specifiers = def_specifiers( num_specifiers, specs_found );
|
|
|
|
}
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeType type = parse_type( toks, stringize(parse_variable) );
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
if ( type == Code::Invalid )
|
2023-07-15 12:20:55 -07:00
|
|
|
return CodeInvalid;
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
name = currtok;
|
|
|
|
eat( TokType::Identifier );
|
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
CodeVar result = parse_variable_after_name( ModuleFlag::None, attributes, specifiers, type, name, toks, stringize(parse_variable) );
|
2023-05-05 15:10:03 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
CodeVar parse_variable( StrC def )
|
2023-05-05 15:10:03 -07:00
|
|
|
{
|
2023-05-06 12:49:43 -07:00
|
|
|
check_parse_args( parse_variable, def );
|
2023-05-05 15:10:03 -07:00
|
|
|
using namespace Parser;
|
|
|
|
|
2023-05-06 12:49:43 -07:00
|
|
|
TokArray toks = lex( def );
|
2023-05-05 15:10:03 -07:00
|
|
|
if ( toks.Arr == nullptr )
|
2023-07-13 22:37:22 -07:00
|
|
|
return CodeInvalid;
|
2023-05-05 15:10:03 -07:00
|
|
|
|
2023-07-15 12:20:55 -07:00
|
|
|
return parse_variable( toks, stringize(parse_variable) );
|
2023-05-05 15:10:03 -07:00
|
|
|
}
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
// Undef helper macros
|
|
|
|
# undef check_parse_args
|
|
|
|
# undef curr_tok
|
|
|
|
# undef eat
|
|
|
|
# undef left
|
2023-04-18 19:47:59 -07:00
|
|
|
// End GEN_FEATURE_PARSING
|
2023-07-08 20:29:18 -07:00
|
|
|
#endif
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion Parsing Constructors
|
|
|
|
|
|
|
|
#pragma region Untyped Constructors
|
2023-07-11 22:33:11 -07:00
|
|
|
sw token_fmt_va( char* buf, uw buf_size, s32 num_tokens, va_list va )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
char const* buf_begin = buf;
|
|
|
|
sw remaining = buf_size;
|
|
|
|
|
2023-07-15 23:26:55 -07:00
|
|
|
static Arena tok_map_arena;
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
HashTable<StrC> tok_map;
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
2023-07-15 23:26:55 -07:00
|
|
|
static char tok_map_mem[ TokenFmt_TokenMap_MemSize ];
|
|
|
|
|
|
|
|
tok_map_arena = Arena::init_from_memory( tok_map_mem, sizeof(tok_map_mem) );
|
|
|
|
|
|
|
|
tok_map = HashTable<StrC>::init( tok_map_arena );
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
s32 left = num_tokens - 1;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
while ( left-- )
|
|
|
|
{
|
|
|
|
char const* token = va_arg( va, char const* );
|
2023-07-11 22:33:11 -07:00
|
|
|
StrC value = va_arg( va, StrC );
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
u32 key = crc32( token, str_len(token) );
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
tok_map.set( key, value );
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
char const* fmt = va_arg( va, char const* );
|
|
|
|
char current = *fmt;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
while ( current )
|
|
|
|
{
|
|
|
|
sw len = 0;
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
while ( current && current != '<' && remaining )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
* buf = * fmt;
|
|
|
|
buf++;
|
|
|
|
fmt++;
|
2023-06-30 00:13:41 -07:00
|
|
|
remaining--;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
current = * fmt;
|
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( current == '<' )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
2023-06-30 00:13:41 -07:00
|
|
|
char const* scanner = fmt + 1;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
|
|
|
s32 tok_len = 0;
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
while ( *scanner != '>' )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
|
|
|
tok_len++;
|
|
|
|
scanner++;
|
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
char const* token = fmt + 1;
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
u32 key = crc32( token, tok_len );
|
2023-07-11 22:33:11 -07:00
|
|
|
StrC* value = tok_map.get( key );
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
if ( value )
|
2023-04-22 19:24:55 -07:00
|
|
|
{
|
2023-07-11 22:33:11 -07:00
|
|
|
sw left = value->Len;
|
|
|
|
char const* str = value->Ptr;
|
2023-06-30 00:13:41 -07:00
|
|
|
|
|
|
|
while ( left-- )
|
|
|
|
{
|
|
|
|
* buf = * str;
|
|
|
|
buf++;
|
|
|
|
str++;
|
2023-07-09 09:35:48 -07:00
|
|
|
remaining--;
|
2023-06-30 00:13:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
scanner++;
|
|
|
|
fmt = scanner;
|
|
|
|
current = * fmt;
|
|
|
|
continue;
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
|
|
|
|
2023-06-30 00:13:41 -07:00
|
|
|
* buf = * fmt;
|
|
|
|
buf++;
|
|
|
|
fmt++;
|
|
|
|
remaining--;
|
|
|
|
|
2023-04-22 19:24:55 -07:00
|
|
|
current = * fmt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-11 15:29:45 -07:00
|
|
|
tok_map.clear();
|
2023-07-15 23:26:55 -07:00
|
|
|
tok_map_arena.free();
|
2023-04-22 19:24:55 -07:00
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
sw result = buf_size - remaining;
|
2023-06-30 21:23:40 -07:00
|
|
|
|
|
|
|
return result;
|
2023-04-22 19:24:55 -07:00
|
|
|
}
|
2023-04-05 00:03:56 -07:00
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
Code untyped_str( StrC content )
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
2023-04-05 23:21:23 -07:00
|
|
|
Code
|
2023-04-05 00:03:56 -07:00
|
|
|
result = make_code();
|
2023-05-06 13:15:49 -07:00
|
|
|
result->Name = get_cached_string( content );
|
2023-04-05 00:03:56 -07:00
|
|
|
result->Type = ECode::Untyped;
|
|
|
|
result->Content = result->Name;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-13 22:37:22 -07:00
|
|
|
Code untyped_fmt( char const* fmt, ...)
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
2023-04-05 23:21:23 -07:00
|
|
|
local_persist thread_local
|
2023-07-11 23:37:51 -07:00
|
|
|
char buf[GEN_PRINTF_MAXLEN] = { 0 };
|
2023-04-05 00:03:56 -07:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, fmt);
|
2023-07-11 23:37:51 -07:00
|
|
|
sw length = str_fmt_va(buf, GEN_PRINTF_MAXLEN, fmt, va);
|
2023-04-05 00:03:56 -07:00
|
|
|
va_end(va);
|
2023-04-05 23:21:23 -07:00
|
|
|
|
|
|
|
Code
|
2023-04-05 00:03:56 -07:00
|
|
|
result = make_code();
|
2023-05-06 13:15:49 -07:00
|
|
|
result->Name = get_cached_string( { str_len(fmt, MaxNameLength), fmt } );
|
2023-04-05 00:03:56 -07:00
|
|
|
result->Type = ECode::Untyped;
|
2023-05-06 13:15:49 -07:00
|
|
|
result->Content = get_cached_string( { length, buf } );
|
2023-04-05 00:03:56 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-07-11 22:33:11 -07:00
|
|
|
Code untyped_token_fmt( s32 num_tokens, ... )
|
2023-04-05 00:03:56 -07:00
|
|
|
{
|
2023-04-05 23:21:23 -07:00
|
|
|
local_persist thread_local
|
2023-07-11 23:37:51 -07:00
|
|
|
char buf[GEN_PRINTF_MAXLEN] = { 0 };
|
2023-04-05 00:03:56 -07:00
|
|
|
|
|
|
|
va_list va;
|
2023-07-11 22:33:11 -07:00
|
|
|
va_start(va, num_tokens);
|
2023-07-11 23:37:51 -07:00
|
|
|
sw length = token_fmt_va(buf, GEN_PRINTF_MAXLEN, num_tokens, va);
|
2023-04-05 00:03:56 -07:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
Code
|
2023-04-09 21:38:47 -07:00
|
|
|
result = make_code();
|
2023-07-11 22:33:11 -07:00
|
|
|
result->Name = get_cached_string( { length, buf } );
|
2023-04-09 21:38:47 -07:00
|
|
|
result->Type = ECode::Untyped;
|
2023-07-11 22:33:11 -07:00
|
|
|
result->Content = result->Name;
|
2023-04-05 00:03:56 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2023-04-22 19:24:55 -07:00
|
|
|
#pragma endregion Untyped Constructors
|
2023-04-06 16:19:11 -07:00
|
|
|
#pragma endregion Gen Interface
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-07 21:29:09 -07:00
|
|
|
#pragma region Builder
|
2023-04-01 22:07:44 -07:00
|
|
|
void Builder::print( Code code )
|
|
|
|
{
|
2023-07-12 12:59:47 -07:00
|
|
|
Buffer.append_fmt( "%s\n", code->to_string() );
|
2023-06-28 11:43:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void Builder::print_fmt( char const* fmt, ... )
|
|
|
|
{
|
|
|
|
sw res;
|
2023-07-11 23:37:51 -07:00
|
|
|
char buf[ GEN_PRINTF_MAXLEN ] = { 0 };
|
2023-06-28 11:43:21 -07:00
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start( va, fmt );
|
|
|
|
res = str_fmt_va( buf, count_of( buf ) - 1, fmt, va ) - 1;
|
|
|
|
va_end( va );
|
|
|
|
|
|
|
|
Buffer.append( buf, res );
|
2023-04-01 22:07:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Builder::open( char const* path )
|
|
|
|
{
|
2023-04-09 10:59:39 -07:00
|
|
|
FileError error = file_open_mode( & File, EFileMode_WRITE, path );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-09 10:59:39 -07:00
|
|
|
if ( error != EFileError_NONE )
|
2023-04-01 22:07:44 -07:00
|
|
|
{
|
2023-04-09 10:59:39 -07:00
|
|
|
log_failure( "gen::File::open - Could not open file: %s", path);
|
2023-04-01 22:07:44 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-16 15:00:07 -07:00
|
|
|
Buffer = String::make_reserve( Memory::GlobalAllocator, Builder_StrBufferReserve );
|
2023-04-05 23:21:23 -07:00
|
|
|
|
2023-04-01 22:07:44 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Builder::write()
|
|
|
|
{
|
2023-06-28 11:43:21 -07:00
|
|
|
bool result = file_write( & File, Buffer, Buffer.length() );
|
2023-04-01 22:07:44 -07:00
|
|
|
|
|
|
|
if ( result == false )
|
2023-04-09 10:59:39 -07:00
|
|
|
log_failure("gen::File::write - Failed to write to file: %s", file_name( & File ) );
|
2023-04-01 19:21:46 -07:00
|
|
|
|
2023-04-01 22:07:44 -07:00
|
|
|
file_close( & File );
|
2023-07-16 15:00:07 -07:00
|
|
|
Buffer.free();
|
2023-04-01 22:07:44 -07:00
|
|
|
}
|
2023-04-07 21:29:09 -07:00
|
|
|
#pragma endregion Builder
|
|
|
|
|
|
|
|
#pragma region Editor
|
2023-04-18 19:47:59 -07:00
|
|
|
#ifdef GEN_FEATURE_EDITOR
|
|
|
|
#endif
|
2023-04-07 21:29:09 -07:00
|
|
|
#pragma endregion Editor
|
|
|
|
|
|
|
|
#pragma region Scanner
|
2023-04-18 19:47:59 -07:00
|
|
|
#ifdef GEN_FEATURE_SCANNER
|
|
|
|
#endif
|
2023-04-07 21:29:09 -07:00
|
|
|
#pragma endregion Scanner
|
2023-04-01 19:21:46 -07:00
|
|
|
}
|
2023-04-19 12:40:23 -07:00
|
|
|
// End: gen_time
|
2023-04-01 22:07:44 -07:00
|
|
|
#endif
|
2023-07-15 17:56:27 -07:00
|
|
|
|