Skip to content

Commit e68a72b

Browse files
committed
fix(gil): deduce struct field pointer type
1 parent fc29a6e commit e68a72b

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

include/GIL/Instructions/Aggregates/StructFieldPtrInst.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define GLU_GIL_INSTRUCTIONS_STRUCT_FIELD_PTR_INST_HPP
33

44
#include "AggregateInst.hpp"
5+
#include "AST/Types/PointerTy.hpp"
56

67
namespace glu::gil {
78

@@ -32,6 +33,8 @@ class StructFieldPtrInst : public AggregateInst {
3233
, _member(member)
3334
, _ptr(pointerType)
3435
{
36+
auto *ptrType = llvm::cast<types::PointerTy>(pointerType);
37+
assert(ptrType->getPointee() == member.getType());
3538
}
3639

3740
/// @brief Gets the result type at the specified index.

include/GILGen/Context.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,10 @@ class Context {
411411
gil::StructFieldPtrInst *
412412
buildStructFieldPtr(gil::Value structPtr, gil::Member member)
413413
{
414-
// Create a pointer type to the field type
415-
auto *fieldPtrType = _functionDecl->getModule()
416-
->getContext()
414+
auto *fieldPtrType = getASTContext()
417415
->getTypesMemoryArena()
418-
.create<glu::types::PointerTy>(member.getType()
416+
.create<glu::types::PointerTy>(
417+
member.getType()
419418
);
420419
return insertInstruction(
421420
new gil::StructFieldPtrInst(structPtr, member, fieldPtrType)

test/GILGen/GILGenStmt.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "GILGen/GILGen.hpp"
2+
#include "Instructions.hpp"
23
#include "Parser.hpp"
34
#include "Scanner.hpp"
45
#include "Sema/Sema.hpp"
@@ -43,3 +44,43 @@ TEST(GILGenStmt, Empty)
4344
bb->getInstructions().front().getKind(), InstKind::ReturnInstKind
4445
);
4546
}
47+
48+
TEST(GILGenStmt, StructFieldPtrResultTypePointsToFieldType)
49+
{
50+
PREP_PARSER(R"(
51+
struct Inner {
52+
value: Int
53+
}
54+
55+
func copy(i: *Inner) -> Inner {
56+
var result: Inner;
57+
result.value = i.*.value;
58+
return result;
59+
}
60+
)");
61+
62+
ASSERT_EQ(module->getDecls().size(), 2u);
63+
auto *fn = llvm::cast<FunctionDecl>(module->getDecls()[1]);
64+
auto gilModule = std::make_unique<gil::Module>("test_module");
65+
GlobalContext globalCtx(gilModule.get());
66+
auto *f = generateFunction(gilModule.get(), fn, globalCtx);
67+
68+
auto *structFieldPtrInst = [&]() -> StructFieldPtrInst * {
69+
for (auto &bb : f->getBasicBlocks()) {
70+
for (auto &inst : bb.getInstructions()) {
71+
if (auto *fieldPtr = llvm::dyn_cast<StructFieldPtrInst>(&inst))
72+
return fieldPtr;
73+
}
74+
}
75+
return nullptr;
76+
}();
77+
78+
ASSERT_NE(structFieldPtrInst, nullptr);
79+
auto *resultType = llvm::dyn_cast<types::PointerTy>(
80+
structFieldPtrInst->getResultType()
81+
);
82+
ASSERT_NE(resultType, nullptr);
83+
EXPECT_EQ(
84+
resultType->getPointee(), structFieldPtrInst->getMember().getType()
85+
);
86+
}

0 commit comments

Comments
 (0)