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

Fixed issue with custom_struct global constructors #85

Merged
merged 1 commit into from
Nov 18, 2024
Merged
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
Fixed issue with custom_struct global constructors
AjayBrahmakshatriya committed Nov 18, 2024
commit c13b7289c199f4efbbc56cebf4db095918592fe2
14 changes: 9 additions & 5 deletions include/builder/block_type_extractor.h
Original file line number Diff line number Diff line change
@@ -19,16 +19,20 @@ extern int type_naming_counter;

template <typename T, typename V=void>
struct type_namer {
static std::string obtained_name;
// Use a pointer to a string instead of
// a string because it is possible get_type_name is called
// from global constructors before obtained_name is initialized
static std::string *obtained_name;
static std::string get_type_name() {
if (obtained_name == "") {
obtained_name = "custom_struct" + std::to_string(type_naming_counter++);
if (obtained_name == nullptr) {
obtained_name = new std::string();
*obtained_name = "custom_struct" + std::to_string(type_naming_counter++);
}
return obtained_name;
return *obtained_name;
}
};
template <typename T, typename V>
std::string type_namer<T, V>::obtained_name = "";
std::string *type_namer<T, V>::obtained_name = nullptr;

template <typename T>
struct type_namer<T, typename check_valid_type<decltype(T::type_name)>::type> {
7 changes: 7 additions & 0 deletions samples/outputs.var_names/sample56
Original file line number Diff line number Diff line change
@@ -39,6 +39,12 @@ FUNC_DECL
VAR (a_0)
INT_CONST (1)
INT_CONST (1)
EXPR_STMT
ASSIGN_EXPR
MEMBER_ACCESS_EXPR (mem0)
VAR_EXPR
VAR (p)
INT_CONST (0)
struct custom_struct0 {
int mem0;
float mem1;
@@ -53,5 +59,6 @@ void bar (void) {
a_0.nested = b_1;
(a_0.nested).mem0 = a_0.mem0;
((a_0.nested).mem1 = (a_0.nested).mem1 + 1) - 1;
p.mem0 = 0;
}

7 changes: 7 additions & 0 deletions samples/outputs/sample56
Original file line number Diff line number Diff line change
@@ -39,6 +39,12 @@ FUNC_DECL
VAR (var0)
INT_CONST (1)
INT_CONST (1)
EXPR_STMT
ASSIGN_EXPR
MEMBER_ACCESS_EXPR (mem0)
VAR_EXPR
VAR (p)
INT_CONST (0)
struct custom_struct0 {
int mem0;
float mem1;
@@ -53,5 +59,6 @@ void bar (void) {
var0.nested = var1;
(var0.nested).mem0 = var0.mem0;
((var0.nested).mem1 = (var0.nested).mem1 + 1) - 1;
p.mem0 = 0;
}

4 changes: 4 additions & 0 deletions samples/sample56.cpp
Original file line number Diff line number Diff line change
@@ -21,6 +21,9 @@ struct my_type {
dyn_var<int> another;
};


dyn_var<struct_type> p = builder::as_global("p");

static void bar(void) {
dyn_var<my_type> a;
dyn_var<struct_type> b;
@@ -29,6 +32,7 @@ static void bar(void) {
a.nested.x = a.another;

a.nested.y++;
p.x = 0;
}

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