From 01fa38ba37ab139472f5bcf65ab1fb9f373ad222 Mon Sep 17 00:00:00 2001 From: Ajay Brahmakshatriya Date: Thu, 15 Feb 2024 12:35:40 -0500 Subject: [PATCH] Cleaned up dyn_var helper types, constructors and operators --- include/builder/builder.h | 1 - include/builder/builder_base.h | 1 - include/builder/builder_context.h | 3 +- include/builder/dyn_var.h | 131 +++++++------------------ include/builder/forward_declarations.h | 17 +--- include/builder/operator_overload.h | 2 +- include/builder/signature_extract.h | 1 + src/builder/builder.cpp | 8 -- 8 files changed, 39 insertions(+), 125 deletions(-) diff --git a/include/builder/builder.h b/include/builder/builder.h index 686b092..73ede01 100644 --- a/include/builder/builder.h +++ b/include/builder/builder.h @@ -5,7 +5,6 @@ namespace builder { -struct sentinel_member {}; } // namespace builder diff --git a/include/builder/builder_base.h b/include/builder/builder_base.h index e2809bd..325c95e 100644 --- a/include/builder/builder_base.h +++ b/include/builder/builder_base.h @@ -31,7 +31,6 @@ class builder { public: // All members here block::expr::Ptr block_expr; - static BT sentinel_builder; typedef builder super; diff --git a/include/builder/builder_context.h b/include/builder/builder_context.h index 840fbca..858101e 100644 --- a/include/builder/builder_context.h +++ b/include/builder/builder_context.h @@ -128,8 +128,7 @@ class builder_context { template T *assume_variable(std::string name) { - T *new_asm_variable = new T(dyn_var_sentinel_type()); - new_asm_variable->block_var->var_name = name; + T * new_asm_variable = new T(with_name(name)); assume_variables.push_back(new_asm_variable); return new_asm_variable; diff --git a/include/builder/dyn_var.h b/include/builder/dyn_var.h index 6b043a5..d3ff0f5 100644 --- a/include/builder/dyn_var.h +++ b/include/builder/dyn_var.h @@ -93,79 +93,35 @@ struct with_name { template class dyn_var_impl : public var { public: - typedef builder BT; - typedef dyn_var_impl my_type; - // These are required for overloads - typedef BT associated_BT; + typedef dyn_var_impl self_type; typedef T stored_type; + template - BT operator()(const types &...args) { - return ((BT) * this)(args...); + builder operator()(const types &...args) { + return ((builder) * this)(args...); } // These three need to be defined inside the class, cannot be defined globally - BT operator=(const var &a) { - return (BT) * this = a; - } - - BT operator[](const BT &a) { - return ((BT) * this)[a]; - } - BT operator*(void) { - return ((BT) * this)[0]; - } - BT operator=(const BT &a) { - return (BT) * this = a; - } - - BT operator=(const dyn_var_impl &a) { - return (BT) * this = a; - } - - template - BT operator=(const dyn_var_impl &a) { - return (BT) * this = a; - } - - BT operator=(const unsigned int &a) { - return operator=((BT)a); - } - BT operator=(const int &a) { - return operator=((BT)a); - } - BT operator=(const long long &a) { - return operator=((BT)a); - } - BT operator=(const unsigned long long &a) { - return operator=((BT)a); - } - - BT operator=(const double &a) { - return operator=((BT)a); - } - - BT operator=(const std::string &s) { - return operator=((BT)s); + builder operator[](const builder &a) { + return ((builder) * this)[a]; } - BT operator=(char *s) { - return operator=((BT)s); + builder operator*(void) { + return ((builder) * this)[0]; } - BT operator=(const char *s) { - return operator=((BT)s); + builder operator!() { + return !(builder) * this; } - - template - BT operator=(const static_var &a) { - return operator=((BT)a); + operator bool() { + return (bool)(builder) * this; } - BT operator!() { - return !(BT) * this; - } - operator bool() { - return (bool)(BT) * this; + // Unified operator= that offloads implementation to builder + template + builder operator=(const X& a) { + return ((builder)*this) = ((builder)a); } + static block::type::Ptr create_block_type(void) { return type_extractor::extract_type(); @@ -239,13 +195,6 @@ class dyn_var_impl : public var { void deferred_init(void) { create_dyn_var(false); } - dyn_var_impl(const dyn_var_sentinel_type &a, std::string name = "") { - create_dyn_var(true); - if (name != "") { - block_var->var_name = name; - var_name = name; - } - } // Constructor to initialize a dyn_var as member // This declaration does not produce a declaration dyn_var_impl(const as_member &a) { @@ -278,23 +227,8 @@ class dyn_var_impl : public var { block_decl_stmt = nullptr; encompassing_expr = a.encompassing_expr; } - // A very special move constructor that is used to create exact - // replicas of variables - dyn_var_impl(const dyn_var_consume &a) { - block_var = a.block_var; - var_name = block_var->var_name; - block_decl_stmt = nullptr; - } - - dyn_var_impl(const my_type &a) : my_type((BT)a) {} - template - dyn_var_impl(const dyn_var_impl &a) : my_type((BT)a) {} - - template - dyn_var_impl(const static_var &a) : my_type((TO)a) {} - - dyn_var_impl(const BT &a) { + dyn_var_impl(const builder &a) { builder_context::current_builder_context->remove_node_from_sequence(a.block_expr); create_dyn_var(); if (builder_context::current_builder_context->bool_vector.size() > 0) @@ -302,19 +236,22 @@ class dyn_var_impl : public var { block_decl_stmt->init_expr = a.block_expr; } - dyn_var_impl(const int &a) : my_type((BT)a) {} - dyn_var_impl(const unsigned int &a) : my_type((BT)a) {} - dyn_var_impl(const long long &a) : my_type((BT)a) {} - dyn_var_impl(const unsigned long long &a) : my_type((BT)a) {} - dyn_var_impl(const bool &a) : my_type((BT)a) {} - dyn_var_impl(const double &a) : my_type((BT)a) {} - dyn_var_impl(const float &a) : my_type((BT)a) {} - dyn_var_impl(const std::string &a) : my_type((BT)a) {} - dyn_var_impl(const char *s) : my_type((BT)(std::string)s) {} - dyn_var_impl(char *s) : my_type((BT)(std::string)s) {} + template + struct is_builder_constructible { + static const bool value = std::is_arithmetic::value + || std::is_base_of::value || std::is_base_of::value; + }; + + template + dyn_var_impl(const TO& a, typename std::enable_if::value>::type* _ = NULL) + : self_type((builder)a) {} + + dyn_var_impl(const std::string &a) : self_type((builder)a) {} + dyn_var_impl(const char *s) : self_type((builder)(std::string)s) {} + dyn_var_impl(char *s) : self_type((builder)(std::string)s) {} - dyn_var_impl(const std::initializer_list &_a) { - std::vector a(_a); + dyn_var_impl(const std::initializer_list &_a) { + std::vector a(_a); assert(builder_context::current_builder_context != nullptr); for (unsigned int i = 0; i < a.size(); i++) { @@ -431,7 +368,7 @@ class dyn_var template typename std::enable_if::value>::type create_return_stmt(const T &a) { - create_return_stmt((typename T::associated_BT)a); + create_return_stmt((builder)a); } } // namespace builder diff --git a/include/builder/forward_declarations.h b/include/builder/forward_declarations.h index ff9384a..06de387 100644 --- a/include/builder/forward_declarations.h +++ b/include/builder/forward_declarations.h @@ -10,8 +10,6 @@ class builder_root; // The builder base class class builder; -struct sentinel_member; - template using is_builder_type = typename std::is_same; @@ -49,18 +47,7 @@ struct defer_init { // No members }; -// This class does nothing -// Apart from just being used in the copy constructor to -// tell the constructor to no create without context -struct dyn_var_sentinel_type {}; - -// This class is used for creating exact replicas of a variable -// One possible use if when initializing args for func_decls -class dyn_var_consume { -public: - block::var::Ptr block_var = nullptr; - dyn_var_consume(const var &a); - dyn_var_consume(const dyn_var_consume &); -}; +struct with_name; + } // namespace builder #endif diff --git a/include/builder/operator_overload.h b/include/builder/operator_overload.h index 8e8c835..ede7a3b 100644 --- a/include/builder/operator_overload.h +++ b/include/builder/operator_overload.h @@ -19,7 +19,7 @@ struct return_type_helper::value>: }; template struct return_type_helper::value && is_dyn_var_type::value>::type> { - typedef typename T::associated_BT type; + typedef builder type; }; template diff --git a/include/builder/signature_extract.h b/include/builder/signature_extract.h index 96ea93a..8bb9fc1 100644 --- a/include/builder/signature_extract.h +++ b/include/builder/signature_extract.h @@ -15,6 +15,7 @@ struct peel_dyn::value>::type typedef typename T::stored_type type; }; + struct extract_signature_enable; template struct filter_var_type { diff --git a/src/builder/builder.cpp b/src/builder/builder.cpp index 61a83b6..4210c6b 100644 --- a/src/builder/builder.cpp +++ b/src/builder/builder.cpp @@ -14,14 +14,6 @@ std::vector extract_type_vector_dyn<>(void) { return empty_vector; } -dyn_var_consume::dyn_var_consume(const var &a) { - block_var = a.block_var; -} -dyn_var_consume::dyn_var_consume(const dyn_var_consume &a) { - block_var = a.block_var; -} - -builder builder::sentinel_builder; void create_return_stmt(const builder &a) { builder_context::current_builder_context->remove_node_from_sequence(a.block_expr);