From 0400aa1d99362fa2acb8df78b8127471a8fe130e Mon Sep 17 00:00:00 2001 From: Sofia Date: Tue, 14 Apr 2026 15:20:14 +0300 Subject: [PATCH] Make empty arrays into just pointers --- src/codegen.cpp | 5 +---- src/parsing.cpp | 13 ++++++++++--- src/types.cpp | 13 ++----------- src/types.h | 4 ++-- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index ac30932..a8a6190 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -293,9 +293,6 @@ namespace types { } llvm::Type* ArrayType::codegen(codegen::Builder& builder) { - if (this->m_size) { - return llvm::ArrayType::get(this->m_inner->codegen(builder), *this->m_size); - } - return llvm::PointerType::get(*builder.context, 0); + return llvm::ArrayType::get(this->m_inner->codegen(builder), this->m_size); } } \ No newline at end of file diff --git a/src/parsing.cpp b/src/parsing.cpp index 5b52066..f71e896 100644 --- a/src/parsing.cpp +++ b/src/parsing.cpp @@ -266,9 +266,16 @@ namespace parsing { auto array_postfix = parse_array_postfix(inner); while (array_postfix.ok()) { auto postfix = array_postfix.unwrap(); - ty = std::shared_ptr{ - new types::ArrayType(ty, postfix) - }; + if (postfix) { + ty = std::shared_ptr{ + new types::ArrayType(ty, *postfix) + }; + } + else { + ty = std::shared_ptr{ + new types::PointerType(ty) + }; + } array_postfix = parse_array_postfix(inner); } diff --git a/src/types.cpp b/src/types.cpp index b358b02..d1b056a 100644 --- a/src/types.cpp +++ b/src/types.cpp @@ -146,10 +146,7 @@ namespace types { std::string ArrayType::formatted() { std::stringstream out{ "" }; out << this->m_inner->formatted(); - if (this->m_size) - out << "[" << *this->m_size << "]"; - else - out << "[]"; + out << "[" << this->m_size << "]"; return out.str(); } @@ -159,13 +156,7 @@ namespace types { } uint32_t ArrayType::size() { - if (this->m_size) { - return (*this->m_size) * this->m_inner->size(); - } - else { - // Acts as a pointer type otherwise - return 64; - } + return this->m_size * this->m_inner->size(); } bool types_equal(std::shared_ptr type1, std::shared_ptr type2) { diff --git a/src/types.h b/src/types.h index beffead..24a820f 100644 --- a/src/types.h +++ b/src/types.h @@ -85,9 +85,9 @@ namespace types { class ArrayType : public Type { public: std::shared_ptr m_inner; - std::optional m_size; + uint32_t m_size; - ArrayType(std::shared_ptr inner, std::optional size) + ArrayType(std::shared_ptr inner, uint32_t size) : Type(TypeKind::Array), m_inner{ std::move(inner) }, m_size{ size } { } virtual ~ArrayType() override = default;