aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-08-29 22:20:36 +0900
committernsfisis <nsfisis@gmail.com>2025-08-29 22:48:12 +0900
commit34181f22cd6cec033a3136a7eb3c363c9e56d449 (patch)
tree5848fefd9c42e1c436bd1519a329ee61a7deaecf /src
parent7ee8c2d11443e2531a6c701e59117204f453796f (diff)
downloadducc-34181f22cd6cec033a3136a7eb3c363c9e56d449.tar.gz
ducc-34181f22cd6cec033a3136a7eb3c363c9e56d449.tar.zst
ducc-34181f22cd6cec033a3136a7eb3c363c9e56d449.zip
fix: dangling pointer
Diffstat (limited to 'src')
-rw-r--r--src/ast.c28
-rw-r--r--src/ast.h10
-rw-r--r--src/json.c5
-rw-r--r--src/parse.c12
4 files changed, 36 insertions, 19 deletions
diff --git a/src/ast.c b/src/ast.c
index 33faff1..ae11ed3 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -29,6 +29,10 @@ const char* type_kind_stringify(TypeKind k) {
unreachable();
}
+static AstNode* members_of(Type* ty) {
+ return ty->ref.defs->node_items[ty->ref.index].node_members;
+}
+
Type* type_new(TypeKind kind) {
Type* ty = calloc(1, sizeof(Type));
ty->kind = kind;
@@ -249,8 +253,8 @@ int type_sizeof_struct(Type* ty) {
int next_offset = 0;
int struct_align = 0;
- for (int i = 0; i < ty->def->node_members->node_len; ++i) {
- AstNode* member = ty->def->node_members->node_items + i;
+ for (int i = 0; i < members_of(ty)->node_len; ++i) {
+ AstNode* member = &members_of(ty)->node_items[i];
int size = type_sizeof(member->ty);
int align = type_alignof(member->ty);
@@ -267,8 +271,8 @@ int type_sizeof_union(Type* ty) {
int union_size = 0;
int union_align = 0;
- for (int i = 0; i < ty->def->node_members->node_len; ++i) {
- AstNode* member = ty->def->node_members->node_items + i;
+ for (int i = 0; i < members_of(ty)->node_len; ++i) {
+ AstNode* member = &members_of(ty)->node_items[i];
int size = type_sizeof(member->ty);
int align = type_alignof(member->ty);
@@ -286,8 +290,8 @@ int type_sizeof_union(Type* ty) {
int type_alignof_struct(Type* ty) {
int struct_align = 0;
- for (int i = 0; i < ty->def->node_members->node_len; ++i) {
- AstNode* member = ty->def->node_members->node_items + i;
+ for (int i = 0; i < members_of(ty)->node_len; ++i) {
+ AstNode* member = &members_of(ty)->node_items[i];
int align = type_alignof(member->ty);
if (struct_align < align) {
@@ -300,8 +304,8 @@ int type_alignof_struct(Type* ty) {
int type_alignof_union(Type* ty) {
int union_align = 0;
- for (int i = 0; i < ty->def->node_members->node_len; ++i) {
- AstNode* member = ty->def->node_members->node_items + i;
+ for (int i = 0; i < members_of(ty)->node_len; ++i) {
+ AstNode* member = &members_of(ty)->node_items[i];
int align = type_alignof(member->ty);
if (union_align < align) {
@@ -321,8 +325,8 @@ int type_offsetof(Type* ty, const char* name) {
int next_offset = 0;
- for (int i = 0; i < ty->def->node_members->node_len; ++i) {
- AstNode* member = ty->def->node_members->node_items + i;
+ for (int i = 0; i < members_of(ty)->node_len; ++i) {
+ AstNode* member = &members_of(ty)->node_items[i];
int size = type_sizeof(member->ty);
int align = type_alignof(member->ty);
@@ -341,8 +345,8 @@ Type* type_member_typeof(Type* ty, const char* name) {
fatal_error("type_member_typeof: type is neither a struct nor a union");
}
- for (int i = 0; i < ty->def->node_members->node_len; ++i) {
- AstNode* member = ty->def->node_members->node_items + i;
+ for (int i = 0; i < members_of(ty)->node_len; ++i) {
+ AstNode* member = &members_of(ty)->node_items[i];
if (strcmp(member->name, name) == 0) {
return member->ty;
}
diff --git a/src/ast.h b/src/ast.h
index 8940aee..108d436 100644
--- a/src/ast.h
+++ b/src/ast.h
@@ -22,13 +22,20 @@ typedef enum TypeKind TypeKind;
const char* type_kind_stringify(TypeKind k);
struct AstNode;
+typedef struct AstNode AstNode;
+
+struct TypeRef {
+ struct AstNode* defs;
+ size_t index;
+};
+typedef struct TypeRef TypeRef;
struct Type {
TypeKind kind;
// Check `base` instead of `kind` to test if the type is an array or a pointer.
struct Type* base;
int array_size;
- struct AstNode* def;
+ TypeRef ref;
BOOL is_static;
};
typedef struct Type Type;
@@ -131,7 +138,6 @@ struct AstNode {
int __i1;
int __i2;
};
-typedef struct AstNode AstNode;
struct Program {
AstNode* funcs;
diff --git a/src/json.c b/src/json.c
index 8abeea7..c255221 100644
--- a/src/json.c
+++ b/src/json.c
@@ -3,13 +3,16 @@
struct JsonBuilder {
StrBuilder buf;
- BOOL has_output_element[256];
+ // not supported by ducc for now
+ // BOOL has_output_element[256];
+ BOOL* has_output_element;
int depth;
};
JsonBuilder* jsonbuilder_new() {
JsonBuilder* b = calloc(1, sizeof(JsonBuilder));
strbuilder_init(&b->buf);
+ b->has_output_element = calloc(256, sizeof(BOOL));
return b;
}
diff --git a/src/parse.c b/src/parse.c
index b33c180..507a56e 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -412,7 +412,8 @@ static AstNode* parse_primary_expr(Parser* p) {
int n = enum_member_idx % 1000;
AstNode* e = ast_new_int(p->enums->node_items[enum_idx].node_members->node_items[n].node_int_value);
e->ty = type_new(TypeKind_enum);
- e->ty->def = &p->enums->node_items[enum_idx];
+ e->ty->ref.defs = p->enums;
+ e->ty->ref.index = enum_idx;
return e;
}
AstNode* e = ast_new(AstNodeKind_gvar);
@@ -558,7 +559,8 @@ static Type* parse_type(Parser* p) {
if (enum_idx == -1) {
fatal_error("parse_type: unknown enum, %s", name);
}
- ty->def = &p->enums->node_items[enum_idx];
+ ty->ref.defs = p->enums;
+ ty->ref.index = enum_idx;
} else if (t->kind == TokenKind_keyword_struct) {
ty->kind = TypeKind_struct;
const char* name = parse_ident(p);
@@ -566,7 +568,8 @@ static Type* parse_type(Parser* p) {
if (struct_idx == -1) {
fatal_error("parse_type: unknown struct, %s", name);
}
- ty->def = &p->structs->node_items[struct_idx];
+ ty->ref.defs = p->structs;
+ ty->ref.index = struct_idx;
} else if (t->kind == TokenKind_keyword_union) {
ty->kind = TypeKind_union;
const char* name = parse_ident(p);
@@ -574,7 +577,8 @@ static Type* parse_type(Parser* p) {
if (union_idx == -1) {
fatal_error("parse_type: unknown union, %s", name);
}
- ty->def = &p->unions->node_items[union_idx];
+ ty->ref.defs = p->unions;
+ ty->ref.index = union_idx;
} else {
unreachable();
}