From 46e816d7ce25f170d520af69964454c5ff614145 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Mon, 16 Dec 2024 20:53:49 -0500 Subject: [PATCH] Add restrict specifier support for C --- base/components/gen/especifier.hpp | 2 ++ base/components/gen/etoktype.hpp | 2 ++ base/components/parser.cpp | 19 +++++++++++-------- base/components/parser_case_macros.cpp | 7 +++++++ base/enums/ESpecifier.csv | 1 + base/enums/ETokType.csv | 1 + test/c_library/test_cuik.c | 17 ++++++++++++++--- 7 files changed, 38 insertions(+), 11 deletions(-) diff --git a/base/components/gen/especifier.hpp b/base/components/gen/especifier.hpp index 78e02a3..7259579 100644 --- a/base/components/gen/especifier.hpp +++ b/base/components/gen/especifier.hpp @@ -23,6 +23,7 @@ enum Specifier : u32 Spec_Ptr, Spec_Ref, Spec_Register, + Spec_Restrict, Spec_RValue, Spec_Static, Spec_Thread_Local, @@ -56,6 +57,7 @@ inline Str spec_to_str( Specifier type ) { "*", sizeof( "*" ) - 1 }, { "&", sizeof( "&" ) - 1 }, { "register", sizeof( "register" ) - 1 }, + { "restrict", sizeof( "restrict" ) - 1 }, { "&&", sizeof( "&&" ) - 1 }, { "static", sizeof( "static" ) - 1 }, { "thread_local", sizeof( "thread_local" ) - 1 }, diff --git a/base/components/gen/etoktype.hpp b/base/components/gen/etoktype.hpp index a0c7a12..a4a468b 100644 --- a/base/components/gen/etoktype.hpp +++ b/base/components/gen/etoktype.hpp @@ -83,6 +83,7 @@ enum TokType : u32 Tok_Spec_Mutable, Tok_Spec_NeverInline, Tok_Spec_Override, + Tok_Spec_Restrict, Tok_Spec_Static, Tok_Spec_ThreadLocal, Tok_Spec_Volatile, @@ -188,6 +189,7 @@ inline Str toktype_to_str( TokType type ) { "mutable", sizeof( "mutable" ) - 1 }, { "neverinline", sizeof( "neverinline" ) - 1 }, { "override", sizeof( "override" ) - 1 }, + { "restrict", sizeof( "restrict" ) - 1 }, { "static", sizeof( "static" ) - 1 }, { "thread_local", sizeof( "thread_local" ) - 1 }, { "volatile", sizeof( "volatile" ) - 1 }, diff --git a/base/components/parser.cpp b/base/components/parser.cpp index 73a859c..7118f5f 100644 --- a/base/components/parser.cpp +++ b/base/components/parser.cpp @@ -1217,9 +1217,9 @@ Code parse_complicated_definition( TokType which ) Token name = parse_identifier(nullptr); _ctx->parser.Scope->Name = name.Text; - Code result = parse_variable_after_name(ModuleFlag_None, NullCode, NullCode, type, name.Text); + CodeVar result = parse_variable_after_name(ModuleFlag_None, NullCode, NullCode, type, name.Text); parser_pop(& _ctx->parser); - return result; + return (Code) result; } else if ( tok.Type == Tok_Identifier && tokens.Arr[ idx - 3 ].Type == which ) { @@ -4719,13 +4719,16 @@ else if ( currtok.Type == Tok_DeclType ) { Specifier spec = str_to_specifier( currtok.Text ); - if ( spec != Spec_Const && spec != Spec_Ptr && spec != Spec_Ref && spec != Spec_RValue ) - { - log_failure( "Error, invalid specifier used in type definition: %S\n%SB", currtok.Text, parser_to_strbuilder(_ctx->parser) ); - parser_pop(& _ctx->parser); - return InvalidCode; - } + switch (spec ) { + GEN_PARSER_TYPENAME_ALLOWED_SUFFIX_SPECIFIER_CASES: + break; + default: { + log_failure( "Error, invalid specifier used in type definition: %S\n%SB", currtok.Text, parser_to_strbuilder(_ctx->parser) ); + parser_pop(& _ctx->parser); + return InvalidCode; + } + } specs_found[ NumSpecifiers ] = spec; NumSpecifiers++; eat( currtok.Type ); diff --git a/base/components/parser_case_macros.cpp b/base/components/parser_case_macros.cpp index a2b2963..0157cba 100644 --- a/base/components/parser_case_macros.cpp +++ b/base/components/parser_case_macros.cpp @@ -97,9 +97,16 @@ case Spec_Global: \ case Spec_Inline: \ case Spec_Local_Persist: \ case Spec_Mutable: \ +case Spec_Restrict: \ case Spec_Static: \ case Spec_Thread_Local: \ case Spec_Volatile +#define GEN_PARSER_TYPENAME_ALLOWED_SUFFIX_SPECIFIER_CASES \ +case Spec_Const: \ +case Spec_Ptr: \ +case Spec_Restrict: \ +case Spec_Ref: \ +case Spec_RValue diff --git a/base/enums/ESpecifier.csv b/base/enums/ESpecifier.csv index b539fbc..a396d37 100644 --- a/base/enums/ESpecifier.csv +++ b/base/enums/ESpecifier.csv @@ -14,6 +14,7 @@ NeverInline, neverinline Ptr, * Ref, & Register, register +Restrict, restrict RValue, && Static, static Thread_Local, thread_local diff --git a/base/enums/ETokType.csv b/base/enums/ETokType.csv index dc9bd4f..80dd875 100644 --- a/base/enums/ETokType.csv +++ b/base/enums/ETokType.csv @@ -72,6 +72,7 @@ Spec_LocalPersist, "local_persist" Spec_Mutable, "mutable" Spec_NeverInline, "neverinline" Spec_Override, "override" +Spec_Restrict, "restrict" Spec_Static, "static" Spec_ThreadLocal, "thread_local" Spec_Volatile, "volatile" diff --git a/test/c_library/test_cuik.c b/test/c_library/test_cuik.c index 3a965f5..81ccabf 100644 --- a/test/c_library/test_cuik.c +++ b/test/c_library/test_cuik.c @@ -20,13 +20,24 @@ int main() ((gen_Macro){ txt("FOR_SUCC"), MT_Statement, MF_Functional }) )); - gen_CodeBody h_passes = gen_parse_file("Cuik/tb/opt/passes.h"); + gen_CodeBody h_passes = gen_parse_file("Cuik/tb/opt/passes.h\n"); for (gen_Code code = gen_iterator(CodeBody, h_passes, code)) switch (code->Type) { - case CT_Struct: case CT_Function: + gen_log_fmt("%S:\t%S params: %S return type: %S\n" + , gen_codetype_to_str(code->Type) + , code->Name + , gen_strbuilder_to_str( gen_params_to_strbuilder( (gen_CodeParams) code->Params)) + , gen_strbuilder_to_str( gen_typename_to_strbuilder((gen_CodeTypename) code->ReturnType)) + ); + break; + case CT_Variable: + gen_log_fmt("%S:\t%S Type: %S\n", gen_codetype_to_str(code->Type), code->Name, code->ValueType); + break; + + case CT_Struct: case CT_Typedef: - gen_log_fmt("%S: %S", gen_codetype_to_str(code->Type), code->Name); + gen_log_fmt("%S:\t%S\n", gen_codetype_to_str(code->Type), code->Name); break; }