Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup dyn_var helper types, constructors and operators #66

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion include/builder/builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

namespace builder {

struct sentinel_member {};

} // namespace builder

Expand Down
1 change: 0 additions & 1 deletion include/builder/builder_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class builder {
public:
// All members here
block::expr::Ptr block_expr;
static BT sentinel_builder;

typedef builder super;

Expand Down
3 changes: 1 addition & 2 deletions include/builder/builder_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ class builder_context {

template <typename T>
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;
Expand Down
142 changes: 34 additions & 108 deletions include/builder/dyn_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,93 +79,38 @@ struct as_compound_expr {
};
using cast = as_compound_expr;

struct as_global {
std::string name;
as_global(const std::string &n) : name(n) {}
};
// With name is just like as_global but can be used locally
struct with_name {
std::string name;
bool with_decl;
with_name(const std::string &n, bool wd = false) : name(n), with_decl(wd) {}
};

template <typename T>
class dyn_var_impl : public var {
public:
typedef builder BT;
typedef dyn_var_impl<T> my_type;
// These are required for overloads
typedef BT associated_BT;
typedef dyn_var_impl<T> self_type;
typedef T stored_type;


template <typename... types>
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<T> &a) {
return (BT) * this = a;
}

template <typename TO>
BT operator=(const dyn_var_impl<TO> &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);
builder operator[](const builder &a) {
return ((builder) * this)[a];
}

BT operator=(const std::string &s) {
return operator=((BT)s);
}
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 <typename Ts>
BT operator=(const static_var<Ts> &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 <typename X>
builder operator=(const X& a) {
return ((builder)*this) = ((builder)a);
}


static block::type::Ptr create_block_type(void) {
return type_extractor<T>::extract_type();
Expand Down Expand Up @@ -239,13 +184,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) {
Expand Down Expand Up @@ -278,43 +216,31 @@ 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 <typename TO>
dyn_var_impl(const dyn_var_impl<TO> &a) : my_type((BT)a) {}

template <typename TO>
dyn_var_impl(const static_var<TO> &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)
return;
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 <typename TO>
struct is_builder_constructible {
static const bool value = std::is_arithmetic<TO>::value
|| std::is_base_of<static_var_base, TO>::value || std::is_base_of<var, TO>::value;
};

template <typename TO>
dyn_var_impl(const TO& a, typename std::enable_if<is_builder_constructible<TO>::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<BT> &_a) {
std::vector<BT> a(_a);
dyn_var_impl(const std::initializer_list<builder> &_a) {
std::vector<builder> a(_a);

assert(builder_context::current_builder_context != nullptr);
for (unsigned int i = 0; i < a.size(); i++) {
Expand Down Expand Up @@ -431,7 +357,7 @@ class dyn_var<T *>

template <typename T>
typename std::enable_if<std::is_base_of<var, T>::value>::type create_return_stmt(const T &a) {
create_return_stmt((typename T::associated_BT)a);
create_return_stmt((builder)a);
}

} // namespace builder
Expand Down
27 changes: 13 additions & 14 deletions include/builder/forward_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class builder_root;
// The builder base class
class builder;

struct sentinel_member;

template <typename T>
using is_builder_type = typename std::is_same<builder, T>;

Expand Down Expand Up @@ -49,18 +47,19 @@ 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 &);

// Constructor helpers for dyn_var
struct as_global {
std::string name;
as_global(const std::string &n) : name(n) {}
};
// With name is just like as_global but can be used locally
struct with_name {
std::string name;
bool with_decl;
with_name(const std::string &n, bool wd = false) : name(n), with_decl(wd) {}
};


} // namespace builder
#endif
2 changes: 1 addition & 1 deletion include/builder/operator_overload.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct return_type_helper<T, typename std::enable_if<is_builder_type<T>::value>:
};
template <typename T>
struct return_type_helper<T, typename std::enable_if<!is_builder_type<T>::value && is_dyn_var_type<T>::value>::type> {
typedef typename T::associated_BT type;
typedef builder type;
};

template <typename T1, typename T2, class Enable = void>
Expand Down
1 change: 1 addition & 0 deletions include/builder/signature_extract.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct peel_dyn<T, typename std::enable_if<std::is_base_of<var, T>::value>::type
typedef typename T::stored_type type;
};


struct extract_signature_enable;
template <typename T, class Enable = void>
struct filter_var_type {
Expand Down
8 changes: 0 additions & 8 deletions src/builder/builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ std::vector<block::type::Ptr> 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);
Expand Down
Loading