Skip to content

Commit

Permalink
Merge pull request #92 from AjayBrahmakshatriya/master
Browse files Browse the repository at this point in the history
Fixed bug with dyn_var<T*> requiring T to be completely defined
  • Loading branch information
AjayBrahmakshatriya authored Mar 2, 2025
2 parents 7d1701b + 9f56a99 commit 14cd7fc
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
11 changes: 8 additions & 3 deletions include/builder/dyn_var.h
Original file line number Diff line number Diff line change
Expand Up @@ -440,12 +440,17 @@ class dyn_var<T *>
return (cast)(this->dyn_var_impl<T*>::operator*());
}
// Hack for creating a member that's live across return site
dyn_var<T> _p = as_member(this, "_p");

std::unique_ptr<dyn_var<T>> _p = nullptr;

dyn_var<T> *operator->() {
auto b = this->operator[](0);
b.encompassing_expr->template setMetadata<bool>("deref_is_star", true);
_p = (cast)b;
return _p.addr();
if (_p == nullptr) {
_p = std::unique_ptr<dyn_var<T>>(new dyn_var<T>(as_member(this, "_p")));
}
*_p = (cast)b;
return _p->addr();
}
};

Expand Down
5 changes: 5 additions & 0 deletions samples/outputs.var_names/sample63
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void bar (void) {
linked_list* x_0;
((x_0->next)->next)->next = 0;
}

5 changes: 5 additions & 0 deletions samples/outputs/sample63
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
void bar (void) {
linked_list* var0;
((var0->next)->next)->next = 0;
}

29 changes: 29 additions & 0 deletions samples/sample63.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Include the headers
#include "blocks/c_code_generator.h"
#include "builder/static_var.h"
#include "builder/dyn_var.h"
#include "blocks/rce.h"
#include <iostream>

// Include the BuildIt types
using builder::dyn_var;
using builder::static_var;

struct linked_list {
static constexpr const char* type_name = "linked_list";
dyn_var<linked_list*> next = builder::with_name("next");
};

static void bar(void) {
dyn_var<struct linked_list*> x;
x->next->next->next = 0;
}

int main(int argc, char* argv[]) {
builder::builder_context context;
auto ast = context.extract_function_ast(bar, "bar");
block::c_code_generator::generate_code(ast, std::cout, 0);
return 0;
}


0 comments on commit 14cd7fc

Please sign in to comment.