Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/GIL/Instructions/Aggregates/StructFieldPtrInst.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef GLU_GIL_INSTRUCTIONS_STRUCT_FIELD_PTR_INST_HPP
#define GLU_GIL_INSTRUCTIONS_STRUCT_FIELD_PTR_INST_HPP

#include "AST/Types/PointerTy.hpp"
#include "AggregateInst.hpp"

namespace glu::gil {
Expand Down Expand Up @@ -32,6 +33,8 @@ class StructFieldPtrInst : public AggregateInst {
, _member(member)
, _ptr(pointerType)
{
auto *ptrType = llvm::cast<types::PointerTy>(pointerType);
assert(ptrType->getPointee() == member.getType());
Comment on lines +36 to +37
}

/// @brief Gets the result type at the specified index.
Expand Down
7 changes: 3 additions & 4 deletions include/GILGen/Context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,11 +411,10 @@ class Context {
gil::StructFieldPtrInst *
buildStructFieldPtr(gil::Value structPtr, gil::Member member)
{
// Create a pointer type to the field type
auto *fieldPtrType = _functionDecl->getModule()
->getContext()
auto *fieldPtrType = getASTContext()
->getTypesMemoryArena()
.create<glu::types::PointerTy>(member.getType()
.create<glu::types::PointerTy>(
member.getType()
);
Comment on lines +414 to 418
return insertInstruction(
new gil::StructFieldPtrInst(structPtr, member, fieldPtrType)
Expand Down
40 changes: 40 additions & 0 deletions test/GILGen/GILGenStmt.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "GILGen/GILGen.hpp"
#include "Instructions.hpp"
#include "Parser.hpp"
#include "Scanner.hpp"
#include "Sema/Sema.hpp"
Expand Down Expand Up @@ -43,3 +44,42 @@ TEST(GILGenStmt, Empty)
bb->getInstructions().front().getKind(), InstKind::ReturnInstKind
);
}

TEST(GILGenStmt, StructFieldPtrResultTypePointsToFieldType)
{
PREP_PARSER(R"(
struct Inner {
value: Int
}

func copy(i: *Inner) -> Inner {
var result: Inner;
result.value = i.*.value;
return result;
}
)");

ASSERT_EQ(module->getDecls().size(), 2u);
auto *fn = llvm::cast<FunctionDecl>(module->getDecls()[1]);
auto gilModule = std::make_unique<gil::Module>("test_module");
GlobalContext globalCtx(gilModule.get());
auto *f = generateFunction(gilModule.get(), fn, globalCtx);

auto *structFieldPtrInst = [&]() -> StructFieldPtrInst * {
for (auto &bb : f->getBasicBlocks()) {
for (auto &inst : bb.getInstructions()) {
if (auto *fieldPtr = llvm::dyn_cast<StructFieldPtrInst>(&inst))
return fieldPtr;
}
}
return nullptr;
}();

ASSERT_NE(structFieldPtrInst, nullptr);
auto *resultType
= llvm::dyn_cast<types::PointerTy>(structFieldPtrInst->getResultType());
ASSERT_NE(resultType, nullptr);
EXPECT_EQ(
resultType->getPointee(), structFieldPtrInst->getMember().getType()
);
}
Loading