3 Commits

14 changed files with 213 additions and 81 deletions

52
LICENSE
View File

@@ -1,26 +1,44 @@
MIT License BSD 3-Clause License
Copyright (c) 2025 Edward R. Gonzalez Copyright (c) 2023, Edward R. Gonzalez
Permission is hereby granted, free of charge, to any person obtaining a copy Redistribution and use in source and binary forms, with or without
of this software and associated documentation files (the "Software"), to deal modification, are permitted provided that the following conditions are met:
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all 1. Redistributions of source code must retain the above copyright notice, this
copies or substantial portions of the Software. list of conditions and the following disclaimer.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2. Redistributions in binary form must reproduce the above copyright notice,
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, this list of conditions and the following disclaimer in the documentation
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE and/or other materials provided with the distribution.
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 3. Neither the name of the copyright holder nor the names of its
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE contributors may be used to endorse or promote products derived from
SOFTWARE. this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Source URL: https://github.com/Ed94/gencpp Source URL: https://github.com/Ed94/gencpp
Acknowledgements Acknowledgements
* The dependencies for gencpp source are derived from the zpl library: https://github.com/zpl-c/zpl * The dependencies for gencpp source are derived from the zpl library: https://github.com/zpl-c/zpl
Special thanks to:
* The Handmade Community.
* Casey Muratori, Ginger Bill (Bill Hall), Mr. 4th (Allen Webster), Ryan Fluery: Influnced conceptually how to handle staged metaprograming.
* Jonathan Blow: Jai's metaprogramming influenced the design of this library.
* My friends for putting up with discord spam on this library.

View File

@@ -38,13 +38,13 @@ void body_to_strbuilder_export( CodeBody body, StrBuilder* result )
GEN_ASSERT(result != nullptr); GEN_ASSERT(result != nullptr);
strbuilder_append_fmt( result, "export\n{\n" ); strbuilder_append_fmt( result, "export\n{\n" );
Code curr = cast(Code, body); Code curr = body->Front;
s32 left = body->NumEntries; s32 left = body->NumEntries;
while ( left-- ) while ( left-- )
{ {
code_to_strbuilder_ref(curr, result); code_to_strbuilder_ref(curr, result);
// strbuilder_append_fmt( result, "%SB", code_to_strbuilder(curr) ); // strbuilder_append_fmt( result, "%SB", code_to_strbuilder(curr) );
++curr; curr = curr->Next;
} }
strbuilder_append_fmt( result, "};\n" ); strbuilder_append_fmt( result, "};\n" );

View File

@@ -252,8 +252,8 @@ struct CodeSpecifiers
#if ! GEN_C_LIKE_CPP #if ! GEN_C_LIKE_CPP
Using_Code( CodeSpecifiers ); Using_Code( CodeSpecifiers );
bool append( Specifier spec ) { return specifiers_append(* this, spec); } bool append( Specifier spec ) { return specifiers_append(* this, spec); }
bool has( Specifier spec ) { return specifiers_has(* this, spec); } s32 has( Specifier spec ) { return specifiers_has(* this, spec); }
s32 index_of(Specifier spec) { return specifiers_index_of(* this, spec); } s32 index_of( Specifier spec ) { return specifiers_index_of(* this, spec); }
s32 remove( Specifier to_remove ) { return specifiers_remove(* this, to_remove); } s32 remove( Specifier to_remove ) { return specifiers_remove(* this, to_remove); }
StrBuilder to_strbuilder() { return specifiers_to_strbuilder(* this ); } StrBuilder to_strbuilder() { return specifiers_to_strbuilder(* this ); }
void to_strbuilder( StrBuilder& result ) { return specifiers_to_strbuilder_ref(* this, & result); } void to_strbuilder( StrBuilder& result ) { return specifiers_to_strbuilder_ref(* this, & result); }
@@ -1075,8 +1075,7 @@ forceinline StrBuilder to_strbuilder(CodeParams params ) {
forceinline void to_strbuilder(CodeParams params, StrBuilder& result ) { return params_to_strbuilder_ref(params, & result); } forceinline void to_strbuilder(CodeParams params, StrBuilder& result ) { return params_to_strbuilder_ref(params, & result); }
forceinline bool append (CodeSpecifiers specifiers, Specifier spec) { return specifiers_append(specifiers, spec); } forceinline bool append (CodeSpecifiers specifiers, Specifier spec) { return specifiers_append(specifiers, spec); }
forceinline bool has (CodeSpecifiers specifiers, Specifier spec) { return specifiers_has(specifiers, spec); } forceinline s32 has (CodeSpecifiers specifiers, Specifier spec) { return specifiers_has(specifiers, spec); }
forceinline s32 index_of (CodeSpecifiers specifiers, Specifier spec) { return specifiers_index_of(specifiers, spec); }
forceinline s32 remove (CodeSpecifiers specifiers, Specifier to_remove ) { return specifiers_remove(specifiers, to_remove); } forceinline s32 remove (CodeSpecifiers specifiers, Specifier to_remove ) { return specifiers_remove(specifiers, to_remove); }
forceinline StrBuilder to_strbuilder(CodeSpecifiers specifiers) { return specifiers_to_strbuilder(specifiers); } forceinline StrBuilder to_strbuilder(CodeSpecifiers specifiers) { return specifiers_to_strbuilder(specifiers); }
forceinline void to_strbuilder(CodeSpecifiers specifiers, StrBuilder& result) { return specifiers_to_strbuilder_ref(specifiers, & result); } forceinline void to_strbuilder(CodeSpecifiers specifiers, StrBuilder& result) { return specifiers_to_strbuilder_ref(specifiers, & result); }

View File

@@ -38,7 +38,7 @@ void body_to_strbuilder_ref( CodeBody body, StrBuilder* result )
{ {
code_to_strbuilder_ref(curr, result); code_to_strbuilder_ref(curr, result);
// strbuilder_append_fmt( result, "%SB", code_to_strbuilder(curr) ); // strbuilder_append_fmt( result, "%SB", code_to_strbuilder(curr) );
++curr; curr = curr->Next;
} }
} }

View File

@@ -283,6 +283,15 @@ void init(Context* ctx)
ctx->InitSize_Fallback_Allocator_Bucket_Size = megabytes(8); ctx->InitSize_Fallback_Allocator_Bucket_Size = megabytes(8);
} }
if (ctx->InitSize_StrCacheTable == 0)
{
ctx->InitSize_StrCacheTable = kilobytes(8);
}
if (ctx->InitSize_MacrosTable == 0)
{
ctx->InitSize_MacrosTable = kilobytes(8);
}
// Override the current context (user has to put it back if unwanted). // Override the current context (user has to put it back if unwanted).
_ctx = ctx; _ctx = ctx;
@@ -311,11 +320,11 @@ void init(Context* ctx)
} }
// Setup the hash tables // Setup the hash tables
{ {
ctx->StrCache = hashtable_init(StrCached, ctx->Allocator_DyanmicContainers); ctx->StrCache = hashtable_init_reserve(StrCached, ctx->Allocator_DyanmicContainers, ctx->InitSize_StrCacheTable);
if ( ctx->StrCache.Entries == nullptr ) if ( ctx->StrCache.Entries == nullptr )
GEN_FATAL( "gen::init: Failed to initialize the StringCache"); GEN_FATAL( "gen::init: Failed to initialize the StringCache");
ctx->Macros = hashtable_init(Macro, ctx->Allocator_DyanmicContainers); ctx->Macros = hashtable_init_reserve(Macro, ctx->Allocator_DyanmicContainers, ctx->InitSize_MacrosTable);
if (ctx->Macros.Hashes == nullptr || ctx->Macros.Entries == nullptr) { if (ctx->Macros.Hashes == nullptr || ctx->Macros.Entries == nullptr) {
GEN_FATAL( "gen::init: Failed to initialize the PreprocessMacros table" ); GEN_FATAL( "gen::init: Failed to initialize the PreprocessMacros table" );
} }

View File

@@ -64,6 +64,9 @@ struct Context
u32 InitSize_LexerTokens; u32 InitSize_LexerTokens;
u32 SizePer_StringArena; u32 SizePer_StringArena;
u32 InitSize_StrCacheTable;
u32 InitSize_MacrosTable;
// TODO(Ed): Symbol Table // TODO(Ed): Symbol Table
// Keep track of all resolved symbols (naemspaced identifiers) // Keep track of all resolved symbols (naemspaced identifiers)

View File

@@ -473,8 +473,10 @@ CodeComment def_comment( Str content )
return (CodeComment) result; return (CodeComment) result;
} }
CodeConstructor def_constructor( Opts_def_constructor p ) CodeConstructor def_constructor( Opts_def_constructor opt )
{ {
Opts_def_constructor p = get_optional(opt);
if ( p.params && p.params->Type != CT_Parameters ) { if ( p.params && p.params->Type != CT_Parameters ) {
log_failure("gen::def_constructor: params must be of Parameters type - %s", code_debug_str((Code)p.params)); log_failure("gen::def_constructor: params must be of Parameters type - %s", code_debug_str((Code)p.params));
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
@@ -510,8 +512,10 @@ CodeConstructor def_constructor( Opts_def_constructor p )
return result; return result;
} }
CodeClass def_class( Str name, Opts_def_struct p ) CodeClass def_class( Str name, Opts_def_struct opt )
{ {
Opts_def_struct p = get_optional(opt);
if ( ! name_check( def_class, name ) ) { if ( ! name_check( def_class, name ) ) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -561,8 +565,10 @@ CodeClass def_class( Str name, Opts_def_struct p )
return result; return result;
} }
CodeDefine def_define( Str name, MacroType type, Opts_def_define p ) CodeDefine def_define( Str name, MacroType type, Opts_def_define opt )
{ {
Opts_def_define p = get_optional(opt);
if ( ! name_check( def_define, name ) ) { if ( ! name_check( def_define, name ) ) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -585,8 +591,10 @@ CodeDefine def_define( Str name, MacroType type, Opts_def_define p )
return result; return result;
} }
CodeDestructor def_destructor( Opts_def_destructor p ) CodeDestructor def_destructor( Opts_def_destructor opt )
{ {
Opts_def_destructor p = get_optional(opt);
if ( p.specifiers && p.specifiers->Type != CT_Specifiers ) { if ( p.specifiers && p.specifiers->Type != CT_Specifiers ) {
log_failure( "gen::def_destructor: specifiers was not a 'Specifiers' type: %s", code_debug_str(p.specifiers) ); log_failure( "gen::def_destructor: specifiers was not a 'Specifiers' type: %s", code_debug_str(p.specifiers) );
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
@@ -619,8 +627,10 @@ CodeDestructor def_destructor( Opts_def_destructor p )
return result; return result;
} }
CodeEnum def_enum( Str name, Opts_def_enum p ) CodeEnum def_enum( Str name, Opts_def_enum opt )
{ {
Opts_def_enum p = get_optional(opt);
if ( ! name_check( def_enum, name ) ) { if ( ! name_check( def_enum, name ) ) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -742,8 +752,10 @@ CodeFriend def_friend( Code declaration )
return result; return result;
} }
CodeFn def_function( Str name, Opts_def_function p ) CodeFn def_function( Str name, Opts_def_function opt )
{ {
Opts_def_function p = get_optional(opt);
if ( ! name_check( def_function, name )) { if ( ! name_check( def_function, name )) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -802,8 +814,10 @@ CodeFn def_function( Str name, Opts_def_function p )
return result; return result;
} }
CodeInclude def_include( Str path, Opts_def_include p ) CodeInclude def_include( Str path, Opts_def_include opt )
{ {
Opts_def_include p = get_optional(opt);
if ( path.Len <= 0 || path.Ptr == nullptr ) { if ( path.Len <= 0 || path.Ptr == nullptr ) {
log_failure( "gen::def_include: Invalid path provided - %d" ); log_failure( "gen::def_include: Invalid path provided - %d" );
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
@@ -821,8 +835,10 @@ CodeInclude def_include( Str path, Opts_def_include p )
return result; return result;
} }
CodeModule def_module( Str name, Opts_def_module p ) CodeModule def_module( Str name, Opts_def_module opt )
{ {
Opts_def_module p = get_optional(opt);
if ( ! name_check( def_module, name )) { if ( ! name_check( def_module, name )) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -835,8 +851,10 @@ CodeModule def_module( Str name, Opts_def_module p )
return result; return result;
} }
CodeNS def_namespace( Str name, CodeBody body, Opts_def_namespace p ) CodeNS def_namespace( Str name, CodeBody body, Opts_def_namespace opt )
{ {
Opts_def_namespace p = get_optional(opt);
if ( ! name_check( def_namespace, name )) { if ( ! name_check( def_namespace, name )) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -859,8 +877,10 @@ CodeNS def_namespace( Str name, CodeBody body, Opts_def_namespace p )
return result; return result;
} }
CodeOperator def_operator( Operator op, Str nspace, Opts_def_operator p ) CodeOperator def_operator( Operator op, Str nspace, Opts_def_operator opt )
{ {
Opts_def_operator p = get_optional(opt);
if ( p.attributes && p.attributes->Type != CT_PlatformAttributes ) { if ( p.attributes && p.attributes->Type != CT_PlatformAttributes ) {
log_failure( "gen::def_operator: PlatformAttributes was provided but its not of attributes type: %s", code_debug_str(p.attributes) ); log_failure( "gen::def_operator: PlatformAttributes was provided but its not of attributes type: %s", code_debug_str(p.attributes) );
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
@@ -926,8 +946,10 @@ CodeOperator def_operator( Operator op, Str nspace, Opts_def_operator p )
return result; return result;
} }
CodeOpCast def_operator_cast( CodeTypename type, Opts_def_operator_cast p ) CodeOpCast def_operator_cast( CodeTypename type, Opts_def_operator_cast opt )
{ {
Opts_def_operator_cast p = get_optional(opt);
if ( ! null_check( def_operator_cast, type )) { if ( ! null_check( def_operator_cast, type )) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -959,8 +981,10 @@ CodeOpCast def_operator_cast( CodeTypename type, Opts_def_operator_cast p )
return result; return result;
} }
CodeParams def_param( CodeTypename type, Str name, Opts_def_param p ) CodeParams def_param( CodeTypename type, Str name, Opts_def_param opt )
{ {
Opts_def_param p = get_optional(opt);
if ( ! name_check( def_param, name ) || ! null_check( def_param, type ) ) { if ( ! name_check( def_param, name ) || ! null_check( def_param, type ) ) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -1034,8 +1058,10 @@ CodeSpecifiers def_specifier( Specifier spec )
return result; return result;
} }
CodeStruct def_struct( Str name, Opts_def_struct p ) CodeStruct def_struct( Str name, Opts_def_struct opt )
{ {
Opts_def_struct p = get_optional(opt);
if ( p.attributes && p.attributes->Type != CT_PlatformAttributes ) { if ( p.attributes && p.attributes->Type != CT_PlatformAttributes ) {
log_failure( "gen::def_struct: attributes was not a `PlatformAttributes` type - %s", code_debug_str(cast(Code, p.attributes)) ); log_failure( "gen::def_struct: attributes was not a `PlatformAttributes` type - %s", code_debug_str(cast(Code, p.attributes)) );
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
@@ -1076,8 +1102,10 @@ CodeStruct def_struct( Str name, Opts_def_struct p )
return result; return result;
} }
CodeTemplate def_template( CodeParams params, Code declaration, Opts_def_template p ) CodeTemplate def_template( CodeParams params, Code declaration, Opts_def_template opt )
{ {
Opts_def_template p = get_optional(opt);
if ( ! null_check( def_template, declaration ) ) { if ( ! null_check( def_template, declaration ) ) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -1108,8 +1136,10 @@ CodeTemplate def_template( CodeParams params, Code declaration, Opts_def_templat
return result; return result;
} }
CodeTypename def_type( Str name, Opts_def_type p ) CodeTypename def_type( Str name, Opts_def_type opt )
{ {
Opts_def_type p = get_optional(opt);
if ( ! name_check( def_type, name )) { if ( ! name_check( def_type, name )) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -1143,8 +1173,10 @@ CodeTypename def_type( Str name, Opts_def_type p )
return result; return result;
} }
CodeTypedef def_typedef( Str name, Code type, Opts_def_typedef p ) CodeTypedef def_typedef( Str name, Code type, Opts_def_typedef opt )
{ {
Opts_def_typedef p = get_optional(opt);
if ( ! null_check( def_typedef, type ) ) { if ( ! null_check( def_typedef, type ) ) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -1206,8 +1238,10 @@ CodeTypedef def_typedef( Str name, Code type, Opts_def_typedef p )
return result; return result;
} }
CodeUnion def_union( Str name, CodeBody body, Opts_def_union p ) CodeUnion def_union( Str name, CodeBody body, Opts_def_union opt )
{ {
Opts_def_union p = get_optional(opt);
if ( ! null_check( def_union, body ) ) { if ( ! null_check( def_union, body ) ) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -1233,8 +1267,10 @@ CodeUnion def_union( Str name, CodeBody body, Opts_def_union p )
return result; return result;
} }
CodeUsing def_using( Str name, CodeTypename type, Opts_def_using p ) CodeUsing def_using( Str name, CodeTypename type, Opts_def_using opt )
{ {
Opts_def_using p = get_optional(opt);
if ( ! name_check( def_using, name ) || null_check( def_using, type ) ) { if ( ! name_check( def_using, name ) || null_check( def_using, type ) ) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;
@@ -1274,8 +1310,10 @@ CodeUsing def_using_namespace( Str name )
return result; return result;
} }
CodeVar def_variable( CodeTypename type, Str name, Opts_def_variable p ) CodeVar def_variable( CodeTypename type, Str name, Opts_def_variable opt )
{ {
Opts_def_variable p = get_optional(opt);
if ( ! name_check( def_variable, name ) || ! null_check( def_variable, type ) ) { if ( ! name_check( def_variable, name ) || ! null_check( def_variable, type ) ) {
GEN_DEBUG_TRAP(); GEN_DEBUG_TRAP();
return InvalidCode; return InvalidCode;

View File

@@ -1443,6 +1443,22 @@ CodeFn parse_function_after_name(
} }
// <Attributes> <Specifiers> <ReturnType> <Name> ( <Paraemters> ) <Specifiers> // <Attributes> <Specifiers> <ReturnType> <Name> ( <Paraemters> ) <Specifiers>
// Check for trailing specifiers...
CodeAttributes post_rt_attributes = parse_attributes();
if (post_rt_attributes)
{
if (attributes)
{
StrBuilder merged = strbuilder_fmt_buf(_ctx->Allocator_Temp, "%S %S", attributes->Content, post_rt_attributes->Content);
attributes->Content = cache_str(strbuilder_to_str(merged));
}
else
{
attributes = post_rt_attributes;
}
}
// <Attributes> <Specifiers> <ReturnType> <Name> ( <Paraemters> ) <Specifiers> <Attributes>
CodeBody body = NullCode; CodeBody body = NullCode;
CodeComment inline_cmt = NullCode; CodeComment inline_cmt = NullCode;
if ( check( Tok_BraceCurly_Open ) ) if ( check( Tok_BraceCurly_Open ) )
@@ -1481,17 +1497,19 @@ CodeFn parse_function_after_name(
if ( currtok_noskip.Type == Tok_Comment && currtok_noskip.Line == stmt_end.Line ) if ( currtok_noskip.Type == Tok_Comment && currtok_noskip.Line == stmt_end.Line )
inline_cmt = parse_comment(); inline_cmt = parse_comment();
// <Attributes> <Specifiers> <ReturnType> <Name> ( <Paraemters> ) <Specifiers>; <InlineCmt> // <Attributes> <Specifiers> <ReturnType> <Name> ( <Paraemters> ) <Specifiers> < = 0 or delete > ; <InlineCmt>
} }
else
if (body == nullptr)
{ {
Token stmt_end = currtok; Token stmt_end = currtok;
eat( Tok_Statement_End ); eat( Tok_Statement_End );
// <Attributes> <Specifiers> <ReturnType> <Name> ( <Paraemters> ) <Specifiers>; // <Attributes> <Specifiers> <ReturnType> <Name> ( <Paraemters> ) <Specifiers> <Attributes> < = 0 or delete > ;
if ( currtok_noskip.Type == Tok_Comment && currtok_noskip.Line == stmt_end.Line ) if ( currtok_noskip.Type == Tok_Comment && currtok_noskip.Line == stmt_end.Line )
inline_cmt = parse_comment(); inline_cmt = parse_comment();
// <Attributes> <Specifiers> <ReturnType> <Name> ( <Paraemters> ) <Specifiers>; <InlineCmt> // <Attributes> <Specifiers> <ReturnType> <Name> ( <Paraemters> ) <Specifiers> < = 0 or delete > ; <InlineCmt>
} }
StrBuilder StrBuilder
@@ -1766,7 +1784,7 @@ CodeBody parse_global_nspace( CodeType which )
case Tok_Preprocess_Macro_Expr: case Tok_Preprocess_Macro_Expr:
{ {
if (tok_is_attribute(currtok)) if ( ! tok_is_attribute(currtok))
{ {
log_failure("Unbounded macro expression residing in class/struct body\n%S", parser_to_strbuilder(_ctx->parser)); log_failure("Unbounded macro expression residing in class/struct body\n%S", parser_to_strbuilder(_ctx->parser));
return InvalidCode; return InvalidCode;
@@ -2049,11 +2067,20 @@ Token parse_identifier( bool* possible_member_function )
Macro* macro = lookup_macro(currtok.Text); Macro* macro = lookup_macro(currtok.Text);
b32 accept_as_identifier = macro && bitfield_is_set(MacroFlags, macro->Flags, MF_Allow_As_Identifier ); b32 accept_as_identifier = macro && bitfield_is_set(MacroFlags, macro->Flags, MF_Allow_As_Identifier );
b32 is_decarator = macro && bitfield_is_set(MacroFlags, macro->Flags, MF_Identifier_Decorator );
// Typename can be: '::' <name> // Typename can be: '::' <name>
// If that is the case first option will be Tok_Access_StaticSymbol below // If that is the case first option will be Tok_Access_StaticSymbol below
if (check(Tok_Identifier) || accept_as_identifier) if (check(Tok_Identifier) || accept_as_identifier)
eat( Tok_Identifier ); {
if (is_decarator) {
Code name_macro = parse_simple_preprocess(currtok.Type);
name.Text.Len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )name.Text.Ptr;
}
else {
eat(Tok_Identifier);
}
}
// <Name> // <Name>
parse_template_args( & name ); parse_template_args( & name );
@@ -2363,7 +2390,10 @@ CodeOperator parse_operator_after_ret_type(
case '(': case '(':
{ {
if ( currtok.Text.Ptr[1] == ')' ) if ( currtok.Text.Ptr[1] == ')' )
{
op = Op_FunctionCall; op = Op_FunctionCall;
eat(Tok_Paren_Open);
}
else else
op = Op_Invalid; op = Op_Invalid;
@@ -4714,21 +4744,37 @@ CodeTypename parser_parse_type( bool from_template, bool* typedef_is_function )
else if ( currtok.Type == Tok_Decl_Class || currtok.Type == Tok_Decl_Enum || currtok.Type == Tok_Decl_Struct else if ( currtok.Type == Tok_Decl_Class || currtok.Type == Tok_Decl_Enum || currtok.Type == Tok_Decl_Struct
|| currtok.Type == Tok_Decl_Union ) || currtok.Type == Tok_Decl_Union )
{ {
switch (currtok.Type) { Token next = nexttok;
case Tok_Decl_Class : tag = Tag_Class; break;
case Tok_Decl_Enum : tag = Tag_Enum; break; if (next.Type == Tok_Identifier)
case Tok_Decl_Struct : tag = Tag_Struct; break; {
case Tok_Decl_Union : tag = Tag_Union; break; switch (currtok.Type) {
default: case Tok_Decl_Class : tag = Tag_Class; break;
break; case Tok_Decl_Enum : tag = Tag_Enum; break;
case Tok_Decl_Struct : tag = Tag_Struct; break;
case Tok_Decl_Union : tag = Tag_Union; break;
default:
break;
}
eat( currtok.Type );
// <Attributes> <Specifiers> <class, enum, struct, union>
name = parse_identifier(nullptr);
// <Attributes> <Specifiers> <class, enum, struct, union> <Name>
} }
eat( currtok.Type ); else if (next.Type == Tok_BraceCurly_Open)
// <Attributes> <Specifiers> <class, enum, struct, union> {
name = currtok;
// We have an inplace definition, we need to consume that...
name = parse_identifier(nullptr); // TODO(Ed): we need to add a way for AST_CodeTypename to track an implace definition..
b32 const inplace = true;
Code indplace_def = cast(Code, parser_parse_struct(inplace));
// name.Length = ( ( sptr )currtok.Text + currtok.Length ) - ( sptr )name.Text; // For now we lose the structural information,
// eat( Tok_Identifier ); name.Text.Len = ( ( sptr )prevtok.Text.Ptr + prevtok.Text.Len ) - ( sptr )name.Text.Ptr;
// <Attributes> <Specifiers> <class, enum, struct, union> <inplace def>
}
_ctx->parser.Scope->Name = name.Text; _ctx->parser.Scope->Name = name.Text;
// <Attributes> <Specifiers> <class, enum, struct, union> <Name> // <Attributes> <Specifiers> <class, enum, struct, union> <Name>
} }

View File

@@ -168,26 +168,36 @@ Str macrotype_to_str( MacroType type )
enum EMacroFlags : u16 enum EMacroFlags : u16
{ {
MF_Functional = bit(0), // Macro has parameters (args expected to be passed) // Macro has parameters (args expected to be passed)
MF_Expects_Body = bit(1), // Expects to assign a braced scope to its body. MF_Functional = bit(0),
// Expects to assign a braced scope to its body.
MF_Expects_Body = bit(1),
// lex__eat wil treat this macro as an identifier if the parser attempts to consume it as one. // lex__eat wil treat this macro as an identifier if the parser attempts to consume it as one.
// ^^^ This is a kludge because we don't support push/pop macro pragmas rn. // This is a kludge because we don't support push/pop macro pragmas rn.
MF_Allow_As_Identifier = bit(2), MF_Allow_As_Identifier = bit(2),
// When parsing identifiers, it will allow the consumption of the macro parameters (as its expected to be a part of constructing the identifier)
// Example of a decarator macro from stb_sprintf.h:
// STBSP__PUBLICDEC int STB_SPRINTF_DECORATE(sprintf)(char* buf, char const *fmt, ...) STBSP__ATTRIBUTE_FORMAT(2,3);
// ^^ STB_SPRINTF_DECORATE is decorating sprintf
MF_Identifier_Decorator = bit(3),
// lex__eat wil treat this macro as an attribute if the parser attempts to consume it as one. // lex__eat wil treat this macro as an attribute if the parser attempts to consume it as one.
// ^^^ This a kludge because unreal has a macro that behaves as both a 'statement' and an attribute (UE_DEPRECATED, PRAGMA_ENABLE_DEPRECATION_WARNINGS, etc) // This a kludge because unreal has a macro that behaves as both a 'statement' and an attribute (UE_DEPRECATED, PRAGMA_ENABLE_DEPRECATION_WARNINGS, etc)
// TODO(Ed): We can keep the MF_Allow_As_Attribute flag for macros, however, we need to add the ability of AST_Attributes to chain themselves. // TODO(Ed): We can keep the MF_Allow_As_Attribute flag for macros, however, we need to add the ability of AST_Attributes to chain themselves.
// Its thats already a thing in the standard language anyway // Its thats already a thing in the standard language anyway
// & it would allow UE_DEPRECATED, (UE_PROPERTY / UE_FUNCTION) to chain themselves as attributes of a resolved member function/variable definition // & it would allow UE_DEPRECATED, (UE_PROPERTY / UE_FUNCTION) to chain themselves as attributes of a resolved member function/variable definition
MF_Allow_As_Attribute = bit(3), MF_Allow_As_Attribute = bit(4),
// When a macro is encountered after attributes and specifiers while parsing a function, or variable: // When a macro is encountered after attributes and specifiers while parsing a function, or variable:
// It will consume the macro and treat it as resolving the definition. (Yes this is for Unreal Engine) // It will consume the macro and treat it as resolving the definition.
// (MUST BE OF MT_Statement TYPE) // (MUST BE OF MT_Statement TYPE)
MF_Allow_As_Definition = bit(4), MF_Allow_As_Definition = bit(5),
MF_Allow_As_Specifier = bit(5), // Created for Unreal's PURE_VIRTUAL // Created for Unreal's PURE_VIRTUAL
MF_Allow_As_Specifier = bit(6),
MF_Null = 0, MF_Null = 0,
MF_UnderlyingType = GEN_U16_MAX, MF_UnderlyingType = GEN_U16_MAX,

View File

@@ -198,21 +198,16 @@
#ifndef forceinline #ifndef forceinline
# if GEN_COMPILER_MSVC # if GEN_COMPILER_MSVC
# define forceinline __forceinline # define forceinline __forceinline
# define neverinline __declspec( noinline )
# elif GEN_COMPILER_GCC # elif GEN_COMPILER_GCC
# define forceinline inline __attribute__((__always_inline__)) # define forceinline inline __attribute__((__always_inline__))
# define neverinline __attribute__( ( __noinline__ ) )
# elif GEN_COMPILER_CLANG # elif GEN_COMPILER_CLANG
# if __has_attribute(__always_inline__) # if __has_attribute(__always_inline__)
# define forceinline inline __attribute__((__always_inline__)) # define forceinline inline __attribute__((__always_inline__))
# define neverinline __attribute__( ( __noinline__ ) )
# else # else
# define forceinline # define forceinline
# define neverinline
# endif # endif
# else # else
# define forceinline # define forceinline
# define neverinline
# endif # endif
#endif #endif
@@ -319,4 +314,12 @@
# define GEN_OPITMIZE_MAPPINGS_END # define GEN_OPITMIZE_MAPPINGS_END
#endif #endif
#ifndef get_optional
# if GEN_COMPILER_C
# define get_optional(opt) opt ? *opt : (typeof(*opt)){0}
# else
# define get_optional(opt) opt
# endif
#endif
#pragma endregion Macros #pragma endregion Macros

View File

@@ -138,7 +138,7 @@ constexpr AllocatorInfo heap( void ) { AllocatorInfo allocator = { heap_allocato
#define malloc( sz ) alloc( heap(), sz ) #define malloc( sz ) alloc( heap(), sz )
//! Helper to free memory allocated by heap allocator. //! Helper to free memory allocated by heap allocator.
#define mfree( ptr ) free( heap(), ptr ) #define mfree( ptr ) allocator_free( heap(), ptr )
struct VirtualMemory struct VirtualMemory
{ {

View File

@@ -1159,6 +1159,8 @@ R"(#define <interface_name>( code ) _Generic( (code), \
Str actual_name = { fn->Name.Ptr + prefix.Len, fn->Name.Len - prefix.Len }; Str actual_name = { fn->Name.Ptr + prefix.Len, fn->Name.Len - prefix.Len };
Str new_name = StrBuilder::fmt_buf(_ctx->Allocator_Temp, "def__%S", actual_name ).to_str(); Str new_name = StrBuilder::fmt_buf(_ctx->Allocator_Temp, "def__%S", actual_name ).to_str();
opt_param->ValueType->Specs = def_specifier(Spec_Ptr);
// Resolve define's arguments // Resolve define's arguments
b32 has_args = fn->Params->NumEntries > 1; b32 has_args = fn->Params->NumEntries > 1;
StrBuilder params_str = StrBuilder::make_reserve(_ctx->Allocator_Temp, 32); StrBuilder params_str = StrBuilder::make_reserve(_ctx->Allocator_Temp, 32);
@@ -1172,10 +1174,10 @@ R"(#define <interface_name>( code ) _Generic( (code), \
} }
char const* tmpl_fn_macro = nullptr; char const* tmpl_fn_macro = nullptr;
if (params_str.length() > 0 ) { if (params_str.length() > 0 ) {
tmpl_fn_macro= "#define <def_name>( <params> ... ) <def__name>( <params> (<opts_type>) { __VA_ARGS__ } )\n"; tmpl_fn_macro= "#define <def_name>( <params> ... ) <def__name>( <params> & (<opts_type>) { __VA_ARGS__ } )\n";
} }
else { else {
tmpl_fn_macro= "#define <def_name>( ... ) <def__name>( (<opts_type>) { __VA_ARGS__ } )\n"; tmpl_fn_macro= "#define <def_name>( ... ) <def__name>( & (<opts_type>) { __VA_ARGS__ } )\n";
} }
Code fn_macro = untyped_str(token_fmt( Code fn_macro = untyped_str(token_fmt(
"def_name", fn->Name "def_name", fn->Name
@@ -1504,6 +1506,7 @@ R"(#define <interface_name>( code ) _Generic( (code), \
Str actual_name = { fn->Name.Ptr + prefix.Len, fn->Name.Len - prefix.Len }; Str actual_name = { fn->Name.Ptr + prefix.Len, fn->Name.Len - prefix.Len };
Str new_name = StrBuilder::fmt_buf(_ctx->Allocator_Temp, "def__%S", actual_name ).to_str(); Str new_name = StrBuilder::fmt_buf(_ctx->Allocator_Temp, "def__%S", actual_name ).to_str();
fn->Name = cache_str(new_name); fn->Name = cache_str(new_name);
opt_param->ValueType->Specs = def_specifier(Spec_Ptr);
} }
src_upfront.append(fn); src_upfront.append(fn);
} }

View File

@@ -53,6 +53,7 @@ word enum_underlying, gen_enum_underlying
word nullptr, gen_nullptr word nullptr, gen_nullptr
word struct_init, gen_struct_init word struct_init, gen_struct_init
word hash, gen_hash word hash, gen_hash
word txt, gen_txt
// Basic Types // Basic Types
@@ -410,6 +411,8 @@ namespace var_, gen_var_
word _ctx, gen__ctx word _ctx, gen__ctx
word get_context, gen_get_context
word init, gen_init word init, gen_init
word deinit, gen_deinit word deinit, gen_deinit
word reset, gen_reset word reset, gen_reset

View File

@@ -404,7 +404,7 @@ if ( $vendor -match "msvc" )
$flag_optimize_intrinsics = '/Oi' $flag_optimize_intrinsics = '/Oi'
$flag_optimized_debug_forceinline = '/d2Obforceinline' $flag_optimized_debug_forceinline = '/d2Obforceinline'
$flag_optimized_debug = '/Zo' $flag_optimized_debug = '/Zo'
$flag_ # $flag_
# $flag_out_name = '/OUT:' # $flag_out_name = '/OUT:'
$flag_path_interm = '/Fo' $flag_path_interm = '/Fo'
$flag_path_debug = '/Fd' $flag_path_debug = '/Fd'