/*! @file src/Sema/Type.hpp @brief 类型系统定义:对齐 NaN-boxing 物理布局 */ #pragma once #include #include namespace Fig { enum class TypeTag : std::uint8_t { Int, Double, String, Bool, Null, Any, Function, Struct, Interface }; class BaseType; struct Type { BaseType *base = nullptr; bool isNullable = false; bool operator==(const Type &other) const { return base == other.base && isNullable == other.isNullable; } bool operator!=(const Type &other) const { return !(*this == other); } bool is(TypeTag tag) const; String toString() const; bool isAssignableTo(const Type &target) const; }; class BaseType { public: TypeTag tag; String name; BaseType(TypeTag t, String n) : tag(t), name(std::move(n)) {} virtual ~BaseType() = default; }; class FuncType : public BaseType { public: DynArray paramTypes; Type retType; FuncType(DynArray params, Type ret) : BaseType(TypeTag::Function, "Function"), paramTypes(std::move(params)), retType(ret) { } }; class StructType : public BaseType { public: struct Field { String name; Type type; bool isPublic; int index; }; DynArray fields; HashMap fieldMap; HashMap methods; StructType(String n) : BaseType(TypeTag::Struct, std::move(n)) {} void AddField(String name, Type type, bool isPublic) { size_t idx = fields.size(); fields.push_back({name, type, isPublic, (int) idx}); fieldMap[name] = idx; } }; class InterfaceType : public BaseType { public: struct MethodSig { String name; DynArray params; Type retType; }; HashMap methods; InterfaceType(String n) : BaseType(TypeTag::Interface, std::move(n)) {} }; class TypeContext { public: DynArray allTypes; BaseType *intType, *doubleType, *stringType, *boolType, *anyType, *nullType; TypeContext(); ~TypeContext(); Type GetBasic(TypeTag tag, bool nullable = false); Type CreateFuncType(DynArray params, Type ret); }; } // namespace Fig