summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-12-25 04:25:36 +0100
committerkdx <kikoodx@paranoici.org>2023-12-25 04:25:36 +0100
commit548254c577a995d6951c6d8cf3bc6e4d878a6ad5 (patch)
tree2f0f12e0208b707a0c77238a6941a621c75e3afe
parent99e24b2551261d6f3d94a4d8b94de01eab982063 (diff)
downloado7c-548254c577a995d6951c6d8cf3bc6e4d878a6ad5.tar.gz
prevent type duplication
-rw-r--r--inc/util/slice.h12
-rw-r--r--src/main.c19
2 files changed, 26 insertions, 5 deletions
diff --git a/inc/util/slice.h b/inc/util/slice.h
index ce1d3e4..0e018a6 100644
--- a/inc/util/slice.h
+++ b/inc/util/slice.h
@@ -7,7 +7,8 @@
} Slice##N; \
Slice##N Slice##N##_from_ptr(T* ptr, i32 len); \
Slice##N Slice##N##_sub(Slice##N s, i32 pos, i32 len); \
-Slice##N Slice##N##_next(Slice##N s);
+Slice##N Slice##N##_next(Slice##N s); \
+int Slice##N##_cmp(Slice##N s1, Slice##N s2);
#define slice_impl(N, T) \
Slice##N \
@@ -30,6 +31,15 @@ Slice##N \
Slice##N##_next(Slice##N s) \
{ \
return Slice##N##_sub(s, 1, -1); \
+} \
+int \
+Slice##N##_cmp(Slice##N s1, Slice##N s2) \
+{ \
+ if (s1.len < s2.len) \
+ return -1; \
+ if (s1.len > s2.len) \
+ return 1; \
+ return memcmp(s1.loc, s2.loc, s1.len * sizeof(s1.loc[0])); \
}
slice_decl(U8, u8);
diff --git a/src/main.c b/src/main.c
index b7e7760..dd6c1f8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -37,18 +37,29 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
auto types = VecType_new(0);
type_add_primaries(types);
for (auto node = nodes; node; node = node->next) {
+ bool is_type = true;
+ Type type;
switch (node->type) {
case NOD_ALIAS:
- VecType_push(types, type_new_alias(node));
+ type = type_new_alias(node);
break;
case NOD_STRUCT:
- VecType_push(types, type_new_struct(node));
+ type = type_new_struct(node);
break;
case NOD_UNION:
- VecType_push(types, type_new_union(node));
+ type = type_new_union(node);
+ break;
+ default:
+ is_type = false;
break;
- default: break;
}
+ if (!is_type)
+ continue;
+ for (size_t i = 0; i < types->size; i++)
+ if (SliceU8_cmp(type.name, types->p[i].name) == 0)
+ panic("duplicate declaration of type '%.*s'",
+ type.name.len, type.name.loc);
+ VecType_push(types, type);
}
vec_log(types, "%.*s", X->name.len, X->name.loc);