xo-type: + DFunctionType

This commit is contained in:
Roland Conybeare 2026-03-07 11:43:14 +11:00
commit 429658c2e9
16 changed files with 485 additions and 3 deletions

View file

@ -32,10 +32,7 @@ stdenv.mkDerivation (finalattrs:
xo-cmake
];
propagatedBuildInputs = [
# xo-gc
# xo-reflect
xo-alloc2
# xo-stringtable2
# xo-printable2
xo-subsys
xo-facet

View file

@ -90,6 +90,26 @@ xo_add_genfacetimpl(
# ----------------------------------------------------------------
# note: manual target; generated code committed to git
xo_add_genfacetimpl(
TARGET xo-type-facetimpl-type-functiontype
FACET_PKG xo_type
FACET Type
REPR FunctionType
INPUT idl/IType_DFunctionType.json5
)
# note: manual target; generated code committed to git
xo_add_genfacetimpl(
TARGET xo-type-facetimpl-gcobject-functiontype
FACET_PKG xo_alloc2
FACET GCObject
REPR FunctionType
INPUT idl/IGCObject_DFunctionType.json5
)
# ----------------------------------------------------------------
xo_add_genfacet_all(xo-type-genfacet-all)
install(DIRECTORY idl/

View file

@ -1,6 +1,7 @@
@PACKAGE_INIT@
include(CMakeFindDependencyMacro)
find_dependency(xo_object2)
find_dependency(xo_alloc2)
find_dependency(xo_facet)
find_dependency(subsys)

View file

@ -0,0 +1,16 @@
{
mode: "implementation",
output_cpp_dir: "src/type",
output_hpp_dir: "include/xo/type",
output_impl_subdir: "function",
includes: [
],
local_types: [ ],
namespace1: "xo",
namespace2: "scm",
facet_idl: "idl/GCObject.json5",
brief: "provide AGCObject interface for DFunctionType",
using_doxygen: true,
repr: "DFunctionType",
doc: [ "implement AGCObject for DFunctionType" ],
}

View file

@ -0,0 +1,16 @@
{
mode: "implementation",
output_cpp_dir: "src/type",
output_hpp_dir: "include/xo/type",
output_impl_subdir: "function",
includes: [],
local_types: [],
namespace1: "xo",
namespace2: "scm",
facet_idl: "idl/Type.json5",
brief: "provide AType interface for DFunctionType",
using_doxygen: true,
repr: "DFunctionType",
doc: [ "implement AType for DFunctionType" ],
}

View file

@ -0,0 +1,92 @@
/** @file DFunctionType.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "Type.hpp"
#include "Metatype.hpp"
#include <xo/object2/Array.hpp>
#include <xo/alloc2/Collector.hpp>
#include <xo/alloc2/Allocator.hpp>
namespace xo {
namespace scm {
/** @brief A parameterized array type: array(T)
*
* Represents a fixed-size homogeneous collection.
**/
class DFunctionType {
public:
using ACollector = xo::mm::ACollector;
using AAllocator = xo::mm::AAllocator;
using AGCObject = xo::mm::AGCObject;
public:
/** @defgroup xo-scm-arraytype-ctors **/
///@{
/** create instance using memory from @p mm,
* for a function with return type @p ret_type and arguments @p args
**/
template <typename... Args>
requires (std::same_as<Args, obj<AType>> && ...)
explicit DFunctionType(obj<AAllocator> mm, obj<AType> ret_type, Args... args);
/** create instance using memory from @p mm,
* for a function with return type @p ret_type and arguments @p args
**/
template <typename... Args>
requires (std::same_as<Args, obj<AType>> && ...)
static DFunctionType * _make(obj<AAllocator> mm,
obj<AType> ret_type, Args... args);
///@}
/** @defgroup xo-scm-arraytype-type-facet **/
///@{
Metatype metatype() const noexcept { return Metatype::array(); }
bool is_equal_to(obj<AType> y) const noexcept;
bool is_subtype_of(const obj<AType> & y) const noexcept;
///@}
/** @defgroup xo-scm-arraytype-gcobject-facet **/
///@{
std::size_t shallow_size() const noexcept;
DFunctionType * shallow_copy(obj<AAllocator> mm) const noexcept;
std::size_t forward_children(obj<ACollector> gc) noexcept;
///@}
private:
/** @defgroup xo-scm-arraytype-member-vars **/
///@{
/** function return type **/
obj<AType> return_type_;
/** argument types **/
DArray * arg_types_ = nullptr;
///@}
};
template <typename... Args>
requires (std::same_as<Args, obj<AType>> && ...)
DFunctionType::DFunctionType(obj<AAllocator> mm, obj<AType> return_type, Args... args)
: return_type_{return_type},
arg_types_{DArray::array(mm, args...)}
{}
template <typename... Args>
requires (std::same_as<Args, obj<AType>> && ...)
DFunctionType *
DFunctionType::_make(obj<AAllocator> mm, obj<AType> ret_type, Args... args)
{
void * mem = mm.alloc_for<DFunctionType>();
return new (mem) DFunctionType(mm, ret_type, args...);
}
} /*namespace scm*/
} /*namespace xo*/
/* end DFunctionType.hpp */

View file

@ -0,0 +1,12 @@
/** @file FunctionType.hpp
*
* @author Roland Conybeare, Mar 2026
**/
#pragma once
#include "DFunctionType.hpp"
#include "function/IType_DFunctionType.hpp"
#include "function/IGCObject_DFunctionType.hpp"
/* end FunctionType.hpp */

View file

@ -47,6 +47,7 @@ namespace xo {
static Metatype list() { return Metatype(code::t_list); }
static Metatype array() { return Metatype(code::t_array); }
static Metatype function() { return Metatype(code::t_function); }
/** description string for this type category **/
const char * _descr() const noexcept;

View file

@ -0,0 +1,65 @@
/** @file IGCObject_DFunctionType.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IGCObject_DFunctionType.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IGCObject_DFunctionType.json5]
**/
#pragma once
#include "GCObject.hpp"
#include "DFunctionType.hpp"
namespace xo { namespace scm { class IGCObject_DFunctionType; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::mm::AGCObject,
xo::scm::DFunctionType>
{
using ImplType = xo::mm::IGCObject_Xfer
<xo::scm::DFunctionType,
xo::scm::IGCObject_DFunctionType>;
};
}
}
namespace xo {
namespace scm {
/** @class IGCObject_DFunctionType
**/
class IGCObject_DFunctionType {
public:
/** @defgroup scm-gcobject-dfunctiontype-type-traits **/
///@{
using size_type = xo::mm::AGCObject::size_type;
using AAllocator = xo::mm::AGCObject::AAllocator;
using ACollector = xo::mm::AGCObject::ACollector;
using Copaque = xo::mm::AGCObject::Copaque;
using Opaque = xo::mm::AGCObject::Opaque;
///@}
/** @defgroup scm-gcobject-dfunctiontype-methods **/
///@{
// const methods
/** memory consumption for this instance **/
static size_type shallow_size(const DFunctionType & self) noexcept;
/** copy instance using allocator **/
static Opaque shallow_copy(const DFunctionType & self, obj<AAllocator> mm) noexcept;
// non-const methods
/** during GC: forward immdiate children **/
static size_type forward_children(DFunctionType & self, obj<ACollector> gc) noexcept;
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -0,0 +1,63 @@
/** @file IType_DFunctionType.hpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IType_DFunctionType.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_repr.hpp.j2]
* 3. idl for facet methods
* [idl/IType_DFunctionType.json5]
**/
#pragma once
#include "Type.hpp"
#include "DFunctionType.hpp"
namespace xo { namespace scm { class IType_DFunctionType; } }
namespace xo {
namespace facet {
template <>
struct FacetImplementation<xo::scm::AType,
xo::scm::DFunctionType>
{
using ImplType = xo::scm::IType_Xfer
<xo::scm::DFunctionType,
xo::scm::IType_DFunctionType>;
};
}
}
namespace xo {
namespace scm {
/** @class IType_DFunctionType
**/
class IType_DFunctionType {
public:
/** @defgroup scm-type-dfunctiontype-type-traits **/
///@{
using obj_AType = xo::scm::AType::obj_AType;
using Copaque = xo::scm::AType::Copaque;
using Opaque = xo::scm::AType::Opaque;
///@}
/** @defgroup scm-type-dfunctiontype-methods **/
///@{
// const methods
/** category for this type **/
static Metatype metatype(const DFunctionType & self) noexcept;
/** true iff this type is equal to y **/
static bool is_equal_to(const DFunctionType & self, const obj_AType & y);
/** true iff this is a subtype of y **/
static bool is_subtype_of(const DFunctionType & self, const obj_AType & y);
// non-const methods
///@}
};
} /*namespace scm*/
} /*namespace xo*/
/* end */

View file

@ -10,13 +10,16 @@ set(SELF_SRCS
DAtomicType.cpp
DListType.cpp
DArrayType.cpp
DFunctionType.cpp
IType_Any.cpp
IType_DAtomicType.cpp
IType_DListType.cpp
IType_DArrayType.cpp
IType_DFunctionType.cpp
IGCObject_DAtomicType.cpp
IGCObject_DListType.cpp
IGCObject_DArrayType.cpp
IGCObject_DFunctionType.cpp
)
xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 ${SELF_SRCS})
@ -27,6 +30,7 @@ xo_add_shared_library4(${SELF_LIB} ${PROJECT_NAME}Targets ${PROJECT_VERSION} 1 $
# NOTE: dependency set here must be kept consistent with
# xo-type/cmake/xo_typeConfig.cmake.in
xo_dependency(${SELF_LIB} xo_object2)
xo_dependency(${SELF_LIB} xo_alloc2)
xo_dependency(${SELF_LIB} xo_facet)
xo_dependency(${SELF_LIB} subsys)

View file

@ -0,0 +1,109 @@
/** @file DFunctionType.cpp
*
* @author Roland Conybeare, Mar 2026
**/
#include "FunctionType.hpp"
#include <xo/facet/FacetRegistry.hpp>
namespace xo {
using xo::facet::FacetRegistry;
namespace scm {
// ----- type facet -----
bool
DFunctionType::is_equal_to(obj<AType> y_arg) const noexcept
{
Metatype y_mtype = y_arg.metatype();
if (y_mtype != Metatype::function())
return false;
auto y = obj<AType,DFunctionType>::from(y_arg);
assert(y);
if (arg_types_->size() != y->arg_types_->size())
return false;
if (obj<AType>(return_type_).is_equal_to(y->return_type_))
return false;
for (size_t i = 0, n = arg_types_->size(); i < n; ++i) {
auto argtype1
= FacetRegistry::instance().variant<AType,AGCObject>((*arg_types_)[i]);
auto argtype2
= FacetRegistry::instance().variant<AType,AGCObject>((*(y->arg_types_))[i]);
if (argtype1.is_equal_to(argtype2))
return false;
}
return true;
}
bool
DFunctionType::is_subtype_of(const obj<AType> & y_arg) const noexcept
{
Metatype y_mtype = y_arg.metatype();
if (y_mtype == Metatype::any())
return true;
if (y_mtype != Metatype::function())
return false;
auto y = obj<AType,DFunctionType>::from(y_arg);
assert(y);
if (arg_types_->size() != y->arg_types_->size())
return false;
if (!obj<AType>(return_type_).is_subtype_of(y->return_type_))
return false;
for (size_t i = 0, n = arg_types_->size(); i < n; ++i) {
auto x_argtype
= FacetRegistry::instance().variant<AType,AGCObject>((*arg_types_)[i]);
auto y_argtype
= FacetRegistry::instance().variant<AType,AGCObject>((*(y->arg_types_))[i]);
if (!y_argtype.is_subtype_of(x_argtype))
return false;
}
return true;
}
// ----- gcobject facet -----
std::size_t
DFunctionType::shallow_size() const noexcept
{
return sizeof(*this);
}
DFunctionType *
DFunctionType::shallow_copy(obj<AAllocator> mm) const noexcept
{
return mm.std_copy_for(this);
}
std::size_t
DFunctionType::forward_children(obj<ACollector> gc) noexcept
{
{
auto e = FacetRegistry::instance().variant<AGCObject,AType>(return_type_);
gc.forward_inplace(e.iface(), (void **)&(e.data_));
}
gc.forward_inplace(&arg_types_);
return this->shallow_size();
}
} /*namespace scm*/
} /*namespace xo*/
/* end DFunctionType.cpp */

View file

@ -0,0 +1,39 @@
/** @file IGCObject_DFunctionType.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IGCObject_DFunctionType.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IGCObject_DFunctionType.json5]
**/
#include "function/IGCObject_DFunctionType.hpp"
namespace xo {
namespace scm {
auto
IGCObject_DFunctionType::shallow_size(const DFunctionType & self) noexcept -> size_type
{
return self.shallow_size();
}
auto
IGCObject_DFunctionType::shallow_copy(const DFunctionType & self, obj<AAllocator> mm) noexcept -> Opaque
{
return self.shallow_copy(mm);
}
auto
IGCObject_DFunctionType::forward_children(DFunctionType & self, obj<ACollector> gc) noexcept -> size_type
{
return self.forward_children(gc);
}
} /*namespace scm*/
} /*namespace xo*/
/* end IGCObject_DFunctionType.cpp */

View file

@ -0,0 +1,40 @@
/** @file IType_DFunctionType.cpp
*
* Generated automagically from ingredients:
* 1. code generator:
* [xo-facet/codegen/genfacet]
* arguments:
* --input [idl/IType_DFunctionType.json5]
* 2. jinja2 template for abstract facet .hpp file:
* [iface_facet_any.hpp.j2]
* 3. idl for facet methods
* [idl/IType_DFunctionType.json5]
**/
#include "function/IType_DFunctionType.hpp"
namespace xo {
namespace scm {
auto
IType_DFunctionType::metatype(const DFunctionType & self) noexcept -> Metatype
{
return self.metatype();
}
auto
IType_DFunctionType::is_equal_to(const DFunctionType & self, const obj_AType & y) -> bool
{
return self.is_equal_to(y);
}
auto
IType_DFunctionType::is_subtype_of(const DFunctionType & self, const obj_AType & y) -> bool
{
return self.is_subtype_of(y);
}
} /*namespace scm*/
} /*namespace xo*/
/* end IType_DFunctionType.cpp */

View file

@ -8,6 +8,7 @@
#include <xo/type/AtomicType.hpp>
#include <xo/type/ListType.hpp>
#include <xo/type/ArrayType.hpp>
#include <xo/type/FunctionType.hpp>
#include <xo/facet/FacetRegistry.hpp>
#include <xo/indentlog/scope.hpp>
@ -32,9 +33,13 @@ namespace xo {
FacetRegistry::register_impl<AType, DArrayType>();
FacetRegistry::register_impl<AGCObject, DArrayType>();
FacetRegistry::register_impl<AType, DFunctionType>();
FacetRegistry::register_impl<AGCObject, DFunctionType>();
log && log(xtag("DAtomicType.tseq", typeseq::id<DAtomicType>()));
log && log(xtag("DListType.tseq", typeseq::id<DListType>()));
log && log(xtag("DArrayType.tseq", typeseq::id<DArrayType>()));
log && log(xtag("DFunctionType.tseq", typeseq::id<DFunctionType>()));
return true;
}

View file

@ -8,6 +8,7 @@
#include "AtomicType.hpp"
#include "ListType.hpp"
#include "ArrayType.hpp"
#include "FunctionType.hpp"
#include <xo/alloc2/Collector.hpp>
#include <xo/facet/FacetRegistry.hpp>
#include <xo/indentlog/scope.hpp>
@ -29,6 +30,7 @@ namespace xo {
ok &= gc.install_type(impl_for<AGCObject, DAtomicType>());
ok &= gc.install_type(impl_for<AGCObject, DListType>());
ok &= gc.install_type(impl_for<AGCObject, DArrayType>());
ok &= gc.install_type(impl_for<AGCObject, DFunctionType>());
return ok;
}