Skip to content

Commit

Permalink
Merge pull request #65 from AjayBrahmakshatriya/master
Browse files Browse the repository at this point in the history
Changed dyn_var specialization for pointer types to return the appropriate type
  • Loading branch information
AjayBrahmakshatriya authored Feb 7, 2024
2 parents f9e418d + 6288a8b commit ceb7759
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 40 deletions.
13 changes: 2 additions & 11 deletions include/builder/builder_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,7 @@ std::vector<block::expr::Ptr> extract_call_arguments(const arg_types &...args);
template <typename BT, typename... arg_types>
std::vector<block::expr::Ptr> extract_call_arguments_helper(const arg_types &...args);

class builder_root {
public:
virtual ~builder_root() = default;
};

class builder : builder_root {
class builder {

typedef builder BT;

Expand All @@ -46,7 +41,7 @@ class builder : builder_root {
// and set the block_expr immediately
builder() = default;
// Copy constructor from another builder
builder(const BT &other) : builder_root() {
builder(const BT &other) {
block_expr = other.block_expr;
}

Expand Down Expand Up @@ -302,10 +297,6 @@ class builder : builder_root {
push_to_sequence(block_expr);
}

// This is an overload for the virtual function inside member_base
virtual block::expr::Ptr get_parent() const {
return this->block_expr;
}
};

void annotate(std::string);
Expand Down
33 changes: 33 additions & 0 deletions include/builder/dyn_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,39 @@ class dyn_var : public dyn_var_impl<T>, public dyn_var_parent_selector<T, void>
}
};

// dyn var specialization for pointer types to return the appropriate types on [], * and ->

template <typename T>
class dyn_var<T *>
: public dyn_var_impl<T *> { // No need for parent selector, pointers types aren't custom types by themselves
public:
typedef dyn_var_impl<T *> super;
using super::super;
using super::operator=;

dyn_var() : dyn_var_impl<T *>() {}

dyn_var(const dyn_var<T *> &t) : dyn_var_impl<T *>((builder)t) {}

builder operator=(const dyn_var<T *> &t) {
return *this = (builder)t;
}

// Specialization for the [] operator to return the right type
dyn_var<T> operator[](const builder &bt) {
return (cast)this->dyn_var_impl<T *>::operator[](bt);
}
dyn_var<T> operator*() {
return this->operator[](0);
}
// Hack for creating a member that's live across return site
dyn_var<T> _p = as_member(this, "_p");
dyn_var<T> *operator->() {
_p = (cast)this->operator[](0);
return _p.addr();
}
};

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);
Expand Down
2 changes: 1 addition & 1 deletion include/builder/forward_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class builder;
struct sentinel_member;

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

template <typename T>
using if_builder = typename std::enable_if<is_builder_type<T>::value, T>::type;
Expand Down
35 changes: 35 additions & 0 deletions samples/outputs.var_names/sample46
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ FUNC_DECL
MEMBER_ACCESS_EXPR (member)
VAR_EXPR
VAR (g_0)
DECL_STMT
POINTER_TYPE
NAMED_TYPE (FooT)
VAR (i_5)
NO_INITIALIZATION
EXPR_STMT
ASSIGN_EXPR
MEMBER_ACCESS_EXPR (member)
SQ_BKT_EXPR
VAR_EXPR
VAR (i_5)
INT_CONST (0)
MEMBER_ACCESS_EXPR (my_member)
VAR_EXPR
VAR (l_4)
EXPR_STMT
ASSIGN_EXPR
MEMBER_ACCESS_EXPR (member)
SQ_BKT_EXPR
VAR_EXPR
VAR (i_5)
INT_CONST (0)
INT_CONST (0)
EXPR_STMT
ASSIGN_EXPR
MEMBER_ACCESS_EXPR (member)
SQ_BKT_EXPR
VAR_EXPR
VAR (i_5)
INT_CONST (3)
INT_CONST (0)
struct FooT {
int member;
};
Expand All @@ -74,5 +105,9 @@ void my_bar (void) {
f_3.second_member = g_0 + 1;
CarT<int, BarT> l_4;
l_4.my_member = g_0.member;
FooT* i_5;
i_5->member = l_4.my_member;
i_5->member = 0;
(i_5[3]).member = 0;
}

35 changes: 35 additions & 0 deletions samples/outputs/sample46
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,37 @@ FUNC_DECL
MEMBER_ACCESS_EXPR (member)
VAR_EXPR
VAR (var0)
DECL_STMT
POINTER_TYPE
NAMED_TYPE (FooT)
VAR (var5)
NO_INITIALIZATION
EXPR_STMT
ASSIGN_EXPR
MEMBER_ACCESS_EXPR (member)
SQ_BKT_EXPR
VAR_EXPR
VAR (var5)
INT_CONST (0)
MEMBER_ACCESS_EXPR (my_member)
VAR_EXPR
VAR (var4)
EXPR_STMT
ASSIGN_EXPR
MEMBER_ACCESS_EXPR (member)
SQ_BKT_EXPR
VAR_EXPR
VAR (var5)
INT_CONST (0)
INT_CONST (0)
EXPR_STMT
ASSIGN_EXPR
MEMBER_ACCESS_EXPR (member)
SQ_BKT_EXPR
VAR_EXPR
VAR (var5)
INT_CONST (3)
INT_CONST (0)
struct FooT {
int member;
};
Expand All @@ -74,5 +105,9 @@ void my_bar (void) {
var3.second_member = var0 + 1;
CarT<int, BarT> var4;
var4.my_member = var0.member;
FooT* var5;
var5->member = var4.my_member;
var5->member = 0;
(var5[3]).member = 0;
}

29 changes: 1 addition & 28 deletions samples/sample38.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,7 @@ class dyn_var<foo_t> : public dyn_var_impl<foo_t> {
dyn_var<int> member = as_member(this, "member");
};

// Create specialization for foo_t* so that we can overload the * operator

template <>
class dyn_var<foo_t *> : public dyn_var_impl<foo_t *> {
public:
typedef dyn_var_impl<foo_t *> super;
using super::super;
using super::operator=;
builder operator=(const dyn_var<foo_t *> &t) {
return (*this) = (builder)t;
}
dyn_var(const dyn_var &t) : dyn_var_impl((builder)t) {}
dyn_var() : dyn_var_impl<foo_t *>() {}

dyn_var<foo_t> operator*() {
// Rely on copy elision
return (cast)this->operator[](0);
}

// This is POC of how -> operator can
// be implemented. Requires the hack of creating a dummy
// member because -> needs to return a pointer.
dyn_var<foo_t> _p = as_member(this, "_p");
dyn_var<foo_t> *operator->() {
_p = (cast)this->operator[](0);
return _p.addr();
}
};
/* Specialization for foo_t* is not required because it comes built in with dyn_var now */

} // namespace builder

Expand Down
5 changes: 5 additions & 0 deletions samples/sample46.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ static void bar(void) {

dyn_var<CarT<int, BarT<float>>> l;
l.my_member = g.member;

dyn_var<FooT *> i;
i->member = l.my_member;
(*i).member = 0;
i[3].member = 0;
}

int main(int argc, char *argv[]) {
Expand Down

0 comments on commit ceb7759

Please sign in to comment.