Commit 7198c941 authored by Tom Niget's avatar Tom Niget

Update referencemodel

parent 23be3256
......@@ -23,7 +23,7 @@ struct object;
template <typename T, typename O>
struct instance;
template <typename T>
template <typename B, typename T>
struct classtype;
template <typename M>
......@@ -179,7 +179,7 @@ template <typename T>
concept classtype = !instance<T> && std::derived_from<
unwrap_all<T>,
referencemodel::classtype<
typename unwrap_all<T>::type
typename unwrap_all<T>::base, typename unwrap_all<T>::type
>
>;
......@@ -784,8 +784,7 @@ struct Ref {
Ref() = default;
Ref(T & t) : ptr(std::addressof(t)) {
}
Ref(T & t) : ptr(std::addressof(t)) {}
Ref(const Ref &) = default;
Ref(Ref &&) = default;
......@@ -809,8 +808,7 @@ struct Ref {
Ref(const Box<Pack<T>> & box) : ptr(box.ptr) {}
T * operator ->() const {
return ptr; }
T * operator ->() const { return ptr; }
private:
NonNull<T> ptr;
......@@ -940,11 +938,8 @@ struct object {
template <typename T, typename O>
friend struct instance;
template <typename T>
friend struct classtype;
template <typename B, typename T>
friend struct classbodytype;
friend struct classtype;
template <typename M>
friend struct moduletype;
......@@ -957,12 +952,10 @@ struct object {
template <typename T, typename O>
struct instance : T::template body<> {
struct instance : T {
using type = T;
using body = T::template body<>;
static constexpr std::string_view repr = meta::join<object::object_of_, body::repr>;
static constexpr std::string_view repr = meta::join<object::object_of_, T::repr>;
const O * operator -> () const { return static_cast<const O *>(this); }
O * operator -> () { return static_cast<O *>(this); }
......@@ -970,25 +963,39 @@ struct instance : T::template body<> {
};
template <typename T>
struct classtype {
template <typename B, typename T>
struct classtype : B {
using base = B;
using type = T;
auto * operator -> () const { return &(T::_body); }
auto * operator -> () { return &(T::_body); }
static constexpr std::string_view repr = meta::join<object::class_, T::name>;
const T * operator -> () const { return static_cast<const T *>(this); }
T * operator -> () { return static_cast<T *>(this); }
};
template <typename B, typename T>
struct classbodytype : B {
using base = B;
namespace meta {
static constexpr std::string_view repr = meta::join<object::class_, T::name>;
/* Class rebasing */
template <typename T>
struct rebase_s {
static_assert(always_false<T>, "Cannot rebase this type");
};
auto * operator -> () const { return static_cast<const T *>(this); }
auto * operator -> () { return static_cast<T *>(this); }
template <template <typename> typename Class, typename Base>
struct rebase_s<Class<Base>> {
template <typename Rebase>
using type = Class<Rebase>;
};
template <typename T, typename Rebase>
using rebase = typename rebase_s<std::remove_cvref_t<T>>::template type<Rebase>;
} // namespace meta
template <typename M>
struct moduletype : object {
......@@ -1012,13 +1019,6 @@ struct classmethod : function {};
struct staticmethod : function {};
template <meta::instance T, meta::method G>
auto bind(T &&, const G &);
template <meta::classtype T, meta::classmethod G>
auto bind(T &&, const G &);
template <meta::instance T, meta::classmethod G>
auto bind(T &&, const G &);
template <typename S, typename F>
struct boundmethod : object {
......@@ -1082,7 +1082,7 @@ struct boundmethod : object {
return self;
}
private://TODO
private:
template <typename T>
boundmethod(T && self, F func) : self(std::forward<T>(self)), func(func) {}
......
......@@ -198,5 +198,8 @@ def transpile(source, name: str, path=None):
# disp_scope(res.scope)
assert isinstance(res, ast.Module)
res.name = "__main__"
code = "\n".join(filter(None, map(str, FileVisitor(Scope(), name).visit(res))))
return code
......@@ -70,7 +70,7 @@ class NodeVisitor(UniversalVisitor):
# if node.is_reference:
# yield "PyObj<"
#yield "auto"
yield f"referencemodel::Rc<decltype(__main__::{node.name})::Obj>"
yield f"referencemodel::Rc<__main____oo<>::{node.name}__oo<>::Obj>"
# if node.is_reference:
# yield "::py_type>"
elif isinstance(node, TypeType):
......
......@@ -40,7 +40,7 @@ class BlockVisitor(NodeVisitor):
# declarations (functions, classes, modules, ...) and code.
# Also, for nitpickers, the C++ standard explicitly allows for omitting a `return` statement in the `main`.
# 0 is returned by default.
yield "typon::Root root()"
yield "typon::Root root() const"
def block():
yield from node.body
......
......@@ -23,7 +23,8 @@ class FileVisitor(BlockVisitor):
code = [line for stmt in node.body for line in visitor.visit(stmt)]
yield from visitor.includes
yield "namespace PROGRAMNS {"
yield "struct __main__ : referencemodel::moduletype<__main__> {"
yield "template <typename _Unused = void>"
yield f"struct {node.name}__oo : referencemodel::moduletype<{node.name}__oo<>> {{"
yield from code
# visitor = ModuleVisitor2(self.scope)
......@@ -41,8 +42,9 @@ class FileVisitor(BlockVisitor):
code = [line for stmt in node.body for line in visitor.visit(stmt)]
yield from code
yield "auto operator -> () const { return this; }"
yield "} __main__;"
yield "};"
yield f"constexpr {node.name}__oo<> {node.name};"
yield f"static_assert(sizeof {node.name} == 1);"
yield "}"
yield "#ifdef TYPON_EXTENSION"
yield f"PYBIND11_MODULE({self.module_name}, m) {{"
......
......@@ -141,21 +141,15 @@ class ModuleVisitor4(NodeVisitor):
yield from self.visit_ClassDef(inst)
return
yield f"struct {node.name} : referencemodel::classtype<{node.name}> {{"
yield f"template <typename _Base0 = referencemodel::object>"
yield f"struct body : referencemodel::classbodytype<_Base0, body<>> {{"
yield f"struct {node.name}__oo : referencemodel::classtype<_Base0, {node.name}__oo<>> {{"
yield f"static constexpr std::string_view name = \"{node.name}\";"
inner = ClassInnerVisitor2(node.inner_scope)
for stmt in node.body:
yield from inner.visit(stmt)
yield "};"
yield "static constexpr body _body{};"
yield "static_assert(sizeof _body == 1);"
yield f"struct Obj : referencemodel::instance<{node.name}, Obj> {{"
yield f"struct Obj : referencemodel::instance<{node.name}__oo<>, Obj> {{"
inner = ClassInnerVisitor4(node.inner_scope)
for stmt in node.body:
......@@ -169,12 +163,14 @@ class ModuleVisitor4(NodeVisitor):
yield "};"
yield "template <typename _Unused = void, typename... T>"
yield "template <typename... T>"
yield "auto operator() (T&&... args) const {"
yield "return referencemodel::rc(Obj{std::forward<T>(args)...});"
yield "}"
yield f"}} static constexpr {node.name} {{}};"
yield f"}};"
yield f"static constexpr {node.name}__oo<> {node.name} {{}};"
yield f"static_assert(sizeof {node.name} == 1);"
def visit_FunctionDef(self, node: ast.FunctionDef) -> Iterable[str]:
yield from ()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment