Skip to content

Commit 71c3b0a

Browse files
committed
Convert array types into pointer types in array loading and variable auto typing
1 parent a47d7a2 commit 71c3b0a

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

include/Sand/Type.hpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,30 @@ class Type : public Name
102102
return new Type(base->name + "[" + std::to_string(size) + "]", llvm::ArrayType::get(ref, size), base);
103103
}
104104

105+
static Type *array_to_pointer(Type *type, const bool &recursive = true)
106+
{
107+
if (type->is_array())
108+
{
109+
auto base = type->base;
110+
111+
if (recursive)
112+
{
113+
base = array_to_pointer(type->base);
114+
}
115+
116+
auto ptr_type = pointer(base);
117+
118+
if (type->is_constant)
119+
{
120+
ptr_type = constant(ptr_type);
121+
}
122+
123+
return ptr_type;
124+
}
125+
126+
return type;
127+
}
128+
105129
static Type *constant(Type *origin)
106130
{
107131
auto type = Type::copy(origin);

include/Sand/Value.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class Value : public Name
130130
Value *load_array(llvm::IRBuilder<> &builder)
131131
{
132132
auto ref = builder.CreateConstInBoundsGEP2_64(this->get_ref(), 0, 0, "");
133-
return new Value(this->name + ".load", this->type, ref);
133+
return new Value(this->name + ".load", Type::array_to_pointer(this->type, false), ref);
134134
}
135135

136136
Value *load_reference(llvm::IRBuilder<> &builder)

src/grammar/Visitor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ class Visitor
688688

689689
if (type == nullptr)
690690
{
691-
type = rvalue->type;
691+
type = Type::array_to_pointer(rvalue->type);
692692

693693
if (type->is_function() && !type->is_pointer())
694694
{

0 commit comments

Comments
 (0)