From de881d73d2316254da3b9c8dc499bc51170e667d Mon Sep 17 00:00:00 2001 From: Sofia Date: Tue, 14 Apr 2026 19:26:21 +0300 Subject: [PATCH] Fix bug with string literals --- src/codegen.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index df4c8fa..706468b 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -37,7 +37,7 @@ namespace AST { return std::shared_ptr {stack_type}; } - codegen::StackValue StringLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope&) { + codegen::StackValue StringLiteralExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) { auto stack_type = new types::ArrayType{ std::make_unique(types::FundamentalTypeKind::Char), static_cast(this->m_value.size()) + 1 @@ -46,10 +46,18 @@ namespace AST { auto str = llvm::StringRef{ this->m_value.c_str() }; auto global_str = builder.builder->CreateGlobalString(str); - return codegen::StackValue{ - builder.builder->CreateLoad(stack_type->codegen(builder), global_str, "literal"), - std::unique_ptr{stack_type}, - }; + if (scope.is_lvalue) { + return codegen::StackValue{ + global_str, + std::unique_ptr{stack_type}, + }; + } + else { + return codegen::StackValue{ + builder.builder->CreateLoad(stack_type->codegen(builder), global_str, "literal"), + std::unique_ptr{stack_type}, + }; + } } std::shared_ptr ValueReferenceExpression::get_codegen_type(codegen::Scope& scope) { @@ -159,7 +167,9 @@ namespace AST { codegen::StackValue CastExpression::codegen(codegen::Builder& builder, codegen::Scope& scope) { auto expr_ty = this->m_expr->get_codegen_type(scope); - if (expr_ty->m_kind == types::TypeKind::Pointer && this->m_ty->m_kind == types::TypeKind::Pointer) { + if ((expr_ty->m_kind == types::TypeKind::Array + || expr_ty->m_kind == types::TypeKind::Pointer) + && this->m_ty->m_kind == types::TypeKind::Pointer) { std::cout << expr_ty->formatted() << std::endl; auto lvalued = scope.with_lvalue(); auto expr = this->m_expr->codegen(builder, lvalued);