diff options
author | John Hodge <tpg@mutabah.net> | 2016-05-13 22:22:09 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-05-13 22:22:09 +0800 |
commit | 432892ea9ce604b6d12d6b087ad6328eb335f70c (patch) | |
tree | 754666f17175755194aa9aa701fb6050eb7ef92f | |
parent | 8e9e3a395f211dc871c3abc816b1cc892ed74312 (diff) | |
download | mrust-432892ea9ce604b6d12d6b087ad6328eb335f70c.tar.gz |
HIR - Continued expansion
-rw-r--r-- | src/ast/ast.hpp | 4 | ||||
-rw-r--r-- | src/hir/from_ast.cpp | 101 | ||||
-rw-r--r-- | src/hir/hir.hpp | 19 | ||||
-rw-r--r-- | src/hir/path.hpp | 3 |
4 files changed, 125 insertions, 2 deletions
diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index fc1a2f73..0076678c 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -620,10 +620,10 @@ public: const ::AST::Path& path() const { return m_my_path; }
ItemRef find_item(const ::std::string& needle, bool allow_leaves = true, bool ignore_private_wildcard = true) const;
- ::std::vector<Named<Item>>& items() { return m_items; }
+ ::std::vector<Named<Item>>& items() { return m_items; }
const ::std::vector<Named<Item>>& items() const { return m_items; }
- itemlist_use_t& imports() { return m_imports; }
+ itemlist_use_t& imports() { return m_imports; }
const itemlist_use_t& imports() const { return m_imports; }
::std::vector<Impl>& impls() { return m_impls; }
diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index 72654efd..7b481594 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -17,9 +17,110 @@ return ::HIR::CratePtr( ::HIR::Crate { mv$(rootmod), mv$(macros) } ); } + +::HIR::Expr LowerHIR_Expr(const ::AST::Expr& e) +{ + throw ::std::runtime_error("TODO: LowerHIR_Expr"); +} +::HIR::TypeRef LowerHIR_Type(const ::TypeRef& e) +{ + throw ::std::runtime_error("TODO: LowerHIR_Type"); +} + +::HIR::TypeAlias LowerHIR_TypeAlias(const ::AST::TypeAlias& ta) +{ + throw ::std::runtime_error("TODO: LowerHIR_TypeAlias"); +} + +::HIR::Struct LowerHIR_Struct(const ::AST::Struct& ta) +{ + ::HIR::Struct::Data data; + + throw ::std::runtime_error("TODO: LowerHIR_Struct"); + + return ::HIR::Struct { + LowerHIR_GenericParams(e.params()), + mv$(data) + }; +} + +::HIR::Enum LowerHIR_Enum(const ::AST::Enum& f) +{ + throw ::std::runtime_error("TODO: LowerHIR_Enum"); +} +::HIR::Trait LowerHIR_Trait(const ::AST::Trait& f) +{ + throw ::std::runtime_error("TODO: LowerHIR_Trait"); +} +::HIR::Function LowerHIR_Function(const ::AST::Function& f) +{ + throw ::std::runtime_error("TODO: LowerHIR_Function"); +} + +void _add_mod_ns_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::HIR::TypeItem ti) { + mod.m_mod_items.insert( ::std::make_pair( mv$(name), ::HIR::VisEnt< ::HIR::TypeItem> { is_pub, mv$(ti) } ) ); +} +void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::HIR::ValueItem ti) { + mod.m_value_items.insert( ::std::make_pair( mv$(name), ::HIR::VisEnt< ::HIR::ValueItem> { is_pub, mv$(ti) } ) ); +} + ::HIR::Module LowerHIR_Module(::AST::Module module, ::HIR::SimplePath path) { ::HIR::Module mod { }; + + for( const auto& item : module.items() ) + { + auto item_path = path + item.name; + TU_MATCH(::AST::Item, (item.data), (e), + (None, + ), + (Module, + _add_mod_ns_item( mod, item.name, item.is_pub, LowerHIR_Module( mv$(e), mv$(item_path)) ); + ), + (Crate, + // TODO: All 'extern crate' items should be normalised into a list in the crate root + ), + (Type, + _add_mod_ns_item( mod, item.name, item.is_pub, ::HIR::TypeItem::make_TypeAlias( LowerHIR_TypeAlias(mv$(e)) ) ); + ), + (Struct, + TU_IFLET( ::AST::StructData, e.m_data, Struct, e2, + ::HIR::TypeRef ty = ::HIR::TypeRef(mv$(item_path)); + if( e2.ents.size() == 0 ) + _add_mod_val_item( mod, item.name, item.is_pub, ::HIR::ValueItem::make_StructConstant({mv$(ty)}) ); + else + _add_mod_val_item( mod, item.name, item.is_pub, ::HIR::ValueItem::make_StructConstructor({mv$(ty)}) ); + ) + _add_mod_ns_item( mod, item.name, item.is_pub, LowerHIR_Struct(mv$(e)) ); + ), + (Enum, + _add_mod_ns_item( mod, item.name, item.is_pub, LowerHIR_Enum(mv$(e)) ); + ), + (Trait, + _add_mod_ns_item( mod, item.name, item.is_pub, LowerHIR_Trait(mv$(e)) ); + ), + (Function, + _add_mod_val_item(mod, item.name, item.is_pub, LowerHIR_Function(mv$(e))); + ), + (Static, + if( e.s_class() == ::AST::Static::CONST ) + _add_mod_val_item(mod, item.name, item.is_pub, ::HIR::ValueItem::make_Constant(::HIR::Constant { + ::HIR::GenericParams {}, + LowerHIR_Type( mv$(e.type()) ), + LowerHIR_Expr( mv$(e.value()) ) + })); + else { + _add_mod_val_item(mod, item.name, item.is_pub, ::HIR::ValueItem::make_Static(::HIR::Static { + (e.s_class() == ::AST::Static::MUT), + LowerHIR_Type( mv$(e.type()) ), + LowerHIR_Expr( mv$(e.value()) ) + })); + } + ) + ) + } + throw ::std::runtime_error("TODO: LowerHIR_Module"); } + diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp index 5743e24a..03fe0bde 100644 --- a/src/hir/hir.hpp +++ b/src/hir/hir.hpp @@ -21,6 +21,8 @@ namespace HIR { class Expr { }; +class Pattern { +}; class Crate; class Module; @@ -48,11 +50,27 @@ struct GenericParams // -------------------------------------------------------------------- struct Static { + //GenericParams m_params; + + bool m_is_mut; + TypeRef m_type; + Expr m_value; +}; +struct Constant +{ GenericParams m_params; + + TypeRef m_type; + Expr m_value; }; struct Function { GenericParams m_params; + + ::std::vector< ::std::pair< Pattern, TypeRef > > m_args; + TypeRef m_return; + + Expr m_code; }; // -------------------------------------------------------------------- @@ -124,6 +142,7 @@ TAGGED_UNION(TypeItem, Import, ); TAGGED_UNION(ValueItem, Import, (Import, ::HIR::SimplePath), + (Constant, Constant), (Static, Static), (StructConstant, struct { ::HIR::TypeRef ty; }), (Function, Function), diff --git a/src/hir/path.hpp b/src/hir/path.hpp index 561ddc83..cf17be17 100644 --- a/src/hir/path.hpp +++ b/src/hir/path.hpp @@ -23,6 +23,9 @@ struct SimplePath ::std::string m_crate_name; ::std::vector< ::std::string> m_components; + + + SimplePath operator+(const ::std::string& s) const; }; /// Generic path - Simple path with one lot of generic params class GenericPath |