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

Update referencemodel

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