[Impl] StructInstance现在记录parent TypeInfo, StructType改用TypeInfo

This commit is contained in:
2025-12-29 20:21:12 +08:00
parent 92dcd19f39
commit 31c2ae4d84
10 changed files with 105 additions and 73 deletions

View File

@@ -76,4 +76,17 @@ namespace Fig
using NullClass = std::monostate;
using StringClass = FString;
}; // namespace ValueType
}; // namespace Fig
}; // namespace Fig
namespace std
{
template <>
struct hash<Fig::TypeInfo>
{
size_t operator()(const Fig::TypeInfo &t)
{
return std::hash<size_t>{}(t.getInstanceID());
}
};
};

View File

@@ -1,17 +1,18 @@
#pragma once
#include <Context/context_forward.hpp>
#include <Value/Type.hpp>
namespace Fig
{
struct StructInstance
{
size_t parentId;
TypeInfo parentType;
ContextPtr localContext;
// ===== Constructors =====
StructInstance(size_t _parentId, ContextPtr _localContext) :
parentId(_parentId), localContext(std::move(_localContext)) {}
StructInstance(TypeInfo _parentType, ContextPtr _localContext) :
parentType(_parentType), localContext(std::move(_localContext)) {}
StructInstance(const StructInstance &other) = default;
StructInstance(StructInstance &&) noexcept = default;
@@ -21,7 +22,7 @@ namespace Fig
// ===== Comparison =====
bool operator==(const StructInstance &other) const noexcept
{
return parentId == other.parentId && localContext == other.localContext;
return parentType == other.parentType && localContext == other.localContext;
}
bool operator!=(const StructInstance &other) const noexcept
{

View File

@@ -33,13 +33,13 @@ namespace Fig
struct StructType
{
std::size_t id;
TypeInfo type;
ContextPtr defContext; // 定义时的上下文
std::vector<Field> fields;
// ===== Constructors =====
StructType(ContextPtr _defContext, std::vector<Field> _fields) :
id(nextId()), defContext(std::move(_defContext)), fields(std::move(_fields)) {}
StructType(TypeInfo _type, ContextPtr _defContext, std::vector<Field> _fields) :
type(std::move(_type)), defContext(std::move(_defContext)), fields(std::move(_fields)) {}
StructType(const StructType &other) = default;
StructType(StructType &&) noexcept = default;
@@ -49,7 +49,7 @@ namespace Fig
// ===== Comparison =====
bool operator==(const StructType &other) const noexcept
{
return id == other.id;
return type == other.type;
}
bool operator!=(const StructType &other) const noexcept
{

View File

@@ -58,12 +58,12 @@ namespace Fig
return r;
};
const StructType &st = value->as<StructType>();
return std::hash<std::size_t>{}(st.id) + HashFields(st.fields);
return std::hash<TypeInfo>{}(st.type) + HashFields(st.fields);
}
if (type == ValueType::StructInstance)
{
const StructInstance &si = value->as<StructInstance>();
return std::hash<std::size_t>{}(si.parentId) + std::hash<uint64_t>{}(reinterpret_cast<uint64_t>(std::addressof(*si.localContext)));
return std::hash<TypeInfo>{}(si.parentType) + std::hash<uint64_t>{}(reinterpret_cast<uint64_t>(std::addressof(*si.localContext)));
}
assert(false);
}

View File

@@ -400,11 +400,11 @@ namespace Fig
static_cast<const void *>(&as<Function>())));
if (is<StructType>())
return FString(std::format("<StructType {} at {:p}>",
as<StructType>().id,
as<StructType>().type.toString().toBasicString(),
static_cast<const void *>(&as<StructType>())));
if (is<StructInstance>())
return FString(std::format("<StructInstance '{}' at {:p}>",
as<StructInstance>().parentId,
as<StructInstance>().parentType.toString().toBasicString(),
static_cast<const void *>(&as<StructInstance>())));
if (is<List>())
{