summaryrefslogtreecommitdiff
path: root/src/ast
diff options
context:
space:
mode:
Diffstat (limited to 'src/ast')
-rw-r--r--src/ast/ast.cpp36
-rw-r--r--src/ast/ast.hpp80
2 files changed, 85 insertions, 31 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp
index d1e8c9fd..48cbdb9d 100644
--- a/src/ast/ast.cpp
+++ b/src/ast/ast.cpp
@@ -10,7 +10,6 @@ namespace AST {
ExternCrate ExternCrate_std();
-
SERIALISE_TYPE(MetaItem::, "AST_MetaItem", {
s << m_name;
s << m_str_val;
@@ -42,12 +41,12 @@ SERIALISE_TYPE(MetaItem::, "AST_MetaItem", {
}
-Impl::Impl(TypeRef impl_type, TypeRef trait_type)
-{
-}
-void Impl::add_function(bool is_public, ::std::string name, Function fcn)
-{
-}
+SERIALISE_TYPE(Impl::, "AST_Impl", {
+ s << m_params;
+ s << m_trait;
+ s << m_type;
+ s << m_functions;
+})
Crate::Crate():
m_root_module(*this, ""),
@@ -144,9 +143,6 @@ void Module::add_ext_crate(::std::string ext_name, ::std::string int_name)
m_extern_crates.push_back( Item< ::std::string>( ::std::move(int_name), ::std::move(ext_name), false ) );
}
-void Module::add_impl(Impl impl)
-{
-}
void Module::iterate_functions(fcn_visitor_t *visitor, const Crate& crate)
{
for( auto fcn_item : this->m_functions )
@@ -200,10 +196,6 @@ SERIALISE_TYPE(Struct::, "AST_Struct", {
s << m_fields;
})
-TypeParam::TypeParam(bool is_lifetime, ::std::string name)
-{
-
-}
void TypeParam::addLifetimeBound(::std::string name)
{
@@ -212,6 +204,22 @@ void TypeParam::addTypeBound(TypeRef type)
{
}
+::std::ostream& operator<<(::std::ostream& os, const TypeParam& tp)
+{
+ os << "TypeParam(";
+ switch(tp.m_class)
+ {
+ case TypeParam::LIFETIME: os << "'"; break;
+ case TypeParam::TYPE: os << ""; break;
+ }
+ os << tp.m_name;
+ if( tp.m_trait_bounds.size() )
+ {
+ os << ": [" << tp.m_trait_bounds << "]";
+ }
+ os << ")";
+ return os;
+}
SERIALISE_TYPE(TypeParam::, "AST_TypeParam", {
// TODO: TypeParam
})
diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp
index 7a417e4f..f1289940 100644
--- a/src/ast/ast.hpp
+++ b/src/ast/ast.hpp
@@ -24,11 +24,30 @@ using ::std::move;
class TypeParam:
public Serialisable
{
+ enum Class {
+ LIFETIME,
+ TYPE,
+ //INTEGER,
+ };
+ Class m_class;
+ ::std::string m_name;
+ ::std::vector<TypeRef> m_trait_bounds;
public:
- TypeParam(bool is_lifetime, ::std::string name);
+ TypeParam(bool is_lifetime, ::std::string name):
+ m_class( is_lifetime ? LIFETIME : TYPE ),
+ m_name( ::std::move(name) )
+ {}
void addLifetimeBound(::std::string name);
void addTypeBound(TypeRef type);
+ void setDefault(TypeRef type);
+
+ const ::std::string& name() const { return m_name; }
+ bool is_type() const { return m_class == TYPE; }
+ //TypeRef& get_default() const { return m_
+ ::std::vector<TypeRef>& get_bounds() { assert(is_type()); return m_trait_bounds; }
+
+ friend ::std::ostream& operator<<(::std::ostream& os, const TypeParam& tp);
SERIALISABLE_PROTOTYPES();
};
@@ -158,21 +177,6 @@ public:
SERIALISABLE_PROTOTYPES();
};
-class Impl
-{
-public:
- Impl(TypeRef impl_type, TypeRef trait_type);
-
- void add_function(bool is_public, ::std::string name, Function fcn);
-};
-
-
-class Crate;
-class ExternCrate;
-class Module;
-
-typedef void fcn_visitor_t(const AST::Crate& crate, const AST::Module& mod, Function& fcn);
-
template <typename T>
struct Item:
public Serialisable
@@ -195,6 +199,44 @@ struct Item:
})
};
+class Impl:
+ public Serialisable
+{
+ TypeParams m_params;
+ TypeRef m_trait;
+ TypeRef m_type;
+
+ ::std::vector<Item<Function> > m_functions;
+public:
+ Impl(TypeParams params, TypeRef impl_type, TypeRef trait_type):
+ m_params( move(params) ),
+ m_trait( move(trait_type) ),
+ m_type( move(impl_type) )
+ {}
+
+ void add_function(bool is_public, ::std::string name, Function fcn) {
+ m_functions.push_back( Item<Function>( ::std::move(name), ::std::move(fcn), is_public ) );
+ }
+
+ const TypeParams& params() const { return m_params; }
+ const TypeRef& trait() const { return m_trait; }
+ const TypeRef& type() const { return m_type; }
+
+ TypeParams& params() { return m_params; }
+ TypeRef& trait() { return m_trait; }
+ TypeRef& type() { return m_type; }
+ ::std::vector<Item<Function> >& functions() { return m_functions; }
+
+ SERIALISABLE_PROTOTYPES();
+};
+
+
+class Crate;
+class ExternCrate;
+class Module;
+
+typedef void fcn_visitor_t(const AST::Crate& crate, const AST::Module& mod, Function& fcn);
+
/// Representation of a parsed (and being converted) function
class Module:
public Serialisable
@@ -218,6 +260,7 @@ class Module:
itemlist_static_t m_statics;
itemlist_enum_t m_enums;
itemlist_struct_t m_structs;
+ ::std::vector<Impl> m_impls;
public:
Module(Crate& crate, ::std::string name):
m_crate(crate),
@@ -246,7 +289,9 @@ public:
void add_submod(bool is_public, Module mod) {
m_submods.push_back( ::std::make_pair( move(mod), is_public ) );
}
- void add_impl(Impl impl);
+ void add_impl(Impl impl) {
+ m_impls.push_back( ::std::move(impl) );
+ }
void add_attr(MetaItem item) {
m_attrs.push_back(item);
@@ -264,6 +309,7 @@ public:
itemlist_mod_t& submods() { return m_submods; }
itemlist_use_t& imports() { return m_imports; }
itemlist_ext_t& extern_crates() { return m_extern_crates; }
+ ::std::vector<Impl>& impls() { return m_impls; }
const ::std::vector<MetaItem>& attrs() const { return m_attrs; }
const itemlist_fcn_t& functions() const { return m_functions; }