Skip to content

Commit c980a84

Browse files
Merge pull request #85 from AjayBrahmakshatriya/master
Fixed issue with custom_struct global constructors
2 parents 2b8eed0 + c13b728 commit c980a84

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

include/builder/block_type_extractor.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,20 @@ extern int type_naming_counter;
1919

2020
template <typename T, typename V=void>
2121
struct type_namer {
22-
static std::string obtained_name;
22+
// Use a pointer to a string instead of
23+
// a string because it is possible get_type_name is called
24+
// from global constructors before obtained_name is initialized
25+
static std::string *obtained_name;
2326
static std::string get_type_name() {
24-
if (obtained_name == "") {
25-
obtained_name = "custom_struct" + std::to_string(type_naming_counter++);
27+
if (obtained_name == nullptr) {
28+
obtained_name = new std::string();
29+
*obtained_name = "custom_struct" + std::to_string(type_naming_counter++);
2630
}
27-
return obtained_name;
31+
return *obtained_name;
2832
}
2933
};
3034
template <typename T, typename V>
31-
std::string type_namer<T, V>::obtained_name = "";
35+
std::string *type_namer<T, V>::obtained_name = nullptr;
3236

3337
template <typename T>
3438
struct type_namer<T, typename check_valid_type<decltype(T::type_name)>::type> {

samples/outputs.var_names/sample56

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ FUNC_DECL
3939
VAR (a_0)
4040
INT_CONST (1)
4141
INT_CONST (1)
42+
EXPR_STMT
43+
ASSIGN_EXPR
44+
MEMBER_ACCESS_EXPR (mem0)
45+
VAR_EXPR
46+
VAR (p)
47+
INT_CONST (0)
4248
struct custom_struct0 {
4349
int mem0;
4450
float mem1;
@@ -53,5 +59,6 @@ void bar (void) {
5359
a_0.nested = b_1;
5460
(a_0.nested).mem0 = a_0.mem0;
5561
((a_0.nested).mem1 = (a_0.nested).mem1 + 1) - 1;
62+
p.mem0 = 0;
5663
}
5764

samples/outputs/sample56

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ FUNC_DECL
3939
VAR (var0)
4040
INT_CONST (1)
4141
INT_CONST (1)
42+
EXPR_STMT
43+
ASSIGN_EXPR
44+
MEMBER_ACCESS_EXPR (mem0)
45+
VAR_EXPR
46+
VAR (p)
47+
INT_CONST (0)
4248
struct custom_struct0 {
4349
int mem0;
4450
float mem1;
@@ -53,5 +59,6 @@ void bar (void) {
5359
var0.nested = var1;
5460
(var0.nested).mem0 = var0.mem0;
5561
((var0.nested).mem1 = (var0.nested).mem1 + 1) - 1;
62+
p.mem0 = 0;
5663
}
5764

samples/sample56.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ struct my_type {
2121
dyn_var<int> another;
2222
};
2323

24+
25+
dyn_var<struct_type> p = builder::as_global("p");
26+
2427
static void bar(void) {
2528
dyn_var<my_type> a;
2629
dyn_var<struct_type> b;
@@ -29,6 +32,7 @@ static void bar(void) {
2932
a.nested.x = a.another;
3033

3134
a.nested.y++;
35+
p.x = 0;
3236
}
3337

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

0 commit comments

Comments
 (0)