From 5d60a5828484573e81fe8af9598b99f0616c801b Mon Sep 17 00:00:00 2001 From: Ajay Brahmakshatriya Date: Mon, 22 Apr 2024 13:57:42 -0400 Subject: [PATCH] Added support for types to be const and volatile qualified --- include/blocks/var.h | 3 +++ include/builder/block_type_extractor.h | 36 ++++++++++++++++++++++++++ include/builder/builder_base.h | 3 +++ samples/outputs.var_names/sample30 | 14 +++++++--- samples/outputs/sample30 | 14 +++++++--- samples/sample30.cpp | 4 ++- src/blocks/c_code_generator.cpp | 8 ++++++ 7 files changed, 73 insertions(+), 9 deletions(-) diff --git a/include/blocks/var.h b/include/blocks/var.h index aef2483..957c6f4 100644 --- a/include/blocks/var.h +++ b/include/blocks/var.h @@ -9,6 +9,9 @@ class type : public block { public: typedef std::shared_ptr Ptr; + bool is_const = false; + bool is_volatile = false; + virtual void accept(block_visitor *a) override { a->visit(self()); } diff --git a/include/builder/block_type_extractor.h b/include/builder/block_type_extractor.h index 212018d..8af17b4 100644 --- a/include/builder/block_type_extractor.h +++ b/include/builder/block_type_extractor.h @@ -240,6 +240,42 @@ class type_extractor { } }; +template +class type_extractor { +public: + static block::type::Ptr extract_type(void) { + block::type::Ptr type = type_extractor::extract_type(); + type->is_const = true; + return type; + } + +}; + +template +class type_extractor { +public: + static block::type::Ptr extract_type(void) { + block::type::Ptr type = type_extractor::extract_type(); + type->is_volatile = true; + return type; + } + +}; + +template +class type_extractor { +public: + static block::type::Ptr extract_type(void) { + block::type::Ptr type = type_extractor::extract_type(); + type->is_volatile = true; + type->is_const = true; + return type; + } + +}; + + + // Type extractor for complete closure template class type_extractor> { diff --git a/include/builder/builder_base.h b/include/builder/builder_base.h index 325c95e..a5ad682 100644 --- a/include/builder/builder_base.h +++ b/include/builder/builder_base.h @@ -102,6 +102,9 @@ class builder { push_to_sequence(block_expr); } + builder(const unsigned long &a): builder((unsigned long long)a){} + builder(const long &a): builder((long long)a){} + builder(const double &a) { if (builder_precheck()) { builder_from_sequence(); diff --git a/samples/outputs.var_names/sample30 b/samples/outputs.var_names/sample30 index e212352..42b38d3 100644 --- a/samples/outputs.var_names/sample30 +++ b/samples/outputs.var_names/sample30 @@ -22,7 +22,7 @@ STMT_BLOCK DECL_STMT SCALAR_TYPE (UNSIGNED_LONG_INT) VAR (f_5) - NO_INITIALIZATION + INT_CONST (5) DECL_STMT SCALAR_TYPE (LONG_LONG_INT) VAR (g_6) @@ -63,9 +63,14 @@ STMT_BLOCK VAR_EXPR VAR (n_13) STRING_CONST ("new string") + DECL_STMT + POINTER_TYPE + SCALAR_TYPE (CHAR) + VAR (o_14) + STRING_CONST ("Hello world") DECL_STMT SCALAR_TYPE (INT) - VAR (x_14) + VAR (x_15) INT_CONST (0) { short int a_0; @@ -73,7 +78,7 @@ STMT_BLOCK int c_2; unsigned int d_3; long int e_4; - unsigned long int f_5; + unsigned long int f_5 = 5ll; long long int g_6; unsigned long long int h_7 = 4ll; char i_8; @@ -83,5 +88,6 @@ STMT_BLOCK void* m_12; char n_13[] = "Hello world"; n_13 = "new string"; - int x_14 = 0; + char const* const volatile o_14 = "Hello world"; + int x_15 = 0; } diff --git a/samples/outputs/sample30 b/samples/outputs/sample30 index c15f65e..9360948 100644 --- a/samples/outputs/sample30 +++ b/samples/outputs/sample30 @@ -22,7 +22,7 @@ STMT_BLOCK DECL_STMT SCALAR_TYPE (UNSIGNED_LONG_INT) VAR (var5) - NO_INITIALIZATION + INT_CONST (5) DECL_STMT SCALAR_TYPE (LONG_LONG_INT) VAR (var6) @@ -64,8 +64,13 @@ STMT_BLOCK VAR (var13) STRING_CONST ("new string") DECL_STMT - SCALAR_TYPE (INT) + POINTER_TYPE + SCALAR_TYPE (CHAR) VAR (var14) + STRING_CONST ("Hello world") + DECL_STMT + SCALAR_TYPE (INT) + VAR (var15) INT_CONST (0) { short int var0; @@ -73,7 +78,7 @@ STMT_BLOCK int var2; unsigned int var3; long int var4; - unsigned long int var5; + unsigned long int var5 = 5ll; long long int var6; unsigned long long int var7 = 4ll; char var8; @@ -83,5 +88,6 @@ STMT_BLOCK void* var12; char var13[] = "Hello world"; var13 = "new string"; - int var14 = 0; + char const* const volatile var14 = "Hello world"; + int var15 = 0; } diff --git a/samples/sample30.cpp b/samples/sample30.cpp index 1b456dc..9f83750 100644 --- a/samples/sample30.cpp +++ b/samples/sample30.cpp @@ -15,7 +15,7 @@ static void foo(void) { dyn_var c; dyn_var d; dyn_var e; - dyn_var f; + dyn_var f = (unsigned long)5; dyn_var g; dyn_var h = (unsigned long long)4; dyn_var i; @@ -26,6 +26,8 @@ static void foo(void) { dyn_var n = "Hello world"; n = "new string"; + dyn_var o = "Hello world"; + // bool test, fixes a bug // that causes false as an init value creates a variable // without context diff --git a/src/blocks/c_code_generator.cpp b/src/blocks/c_code_generator.cpp index cdab949..2d37647 100644 --- a/src/blocks/c_code_generator.cpp +++ b/src/blocks/c_code_generator.cpp @@ -221,6 +221,8 @@ void c_code_generator::visit(scalar_type::Ptr type) { default: assert(false && "Invalid scalar type"); } + if (type->is_const) oss << " const"; + if (type->is_volatile) oss << " volatile"; } void c_code_generator::visit(named_type::Ptr type) { oss << type->type_name; @@ -235,6 +237,8 @@ void c_code_generator::visit(named_type::Ptr type) { } oss << ">"; } + if (type->is_const) oss << " const"; + if (type->is_volatile) oss << " volatile"; } void c_code_generator::visit(pointer_type::Ptr type) { if (!isa(type->pointee_type) && !isa(type->pointee_type) && @@ -242,6 +246,8 @@ void c_code_generator::visit(pointer_type::Ptr type) { assert(false && "Printing pointers of complex type is not supported yet"); type->pointee_type->accept(this); oss << "*"; + if (type->is_const) oss << " const"; + if (type->is_volatile) oss << " volatile"; } void c_code_generator::visit(reference_type::Ptr type) { if (!isa(type->referenced_type) && !isa(type->referenced_type) && @@ -249,6 +255,8 @@ void c_code_generator::visit(reference_type::Ptr type) { assert(false && "Printing pointers of complex type is not supported yet"); type->referenced_type->accept(this); oss << "&"; + if (type->is_const) oss << " const"; + if (type->is_volatile) oss << " volatile"; } void c_code_generator::visit(array_type::Ptr type) { if (!isa(type->element_type) && !isa(type->element_type) &&