diff options
author | John Hodge <tpg@mutabah.net> | 2016-06-09 09:54:57 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-06-09 09:54:57 +0800 |
commit | 502cc9c8dc1941877b35e45d742a3e36264671ca (patch) | |
tree | 5ff1f49577673035d7e4d9ff951884e6e979c3f1 /src | |
parent | 7685ecc25ad47d369ea250d36c71bae4cec2d9a1 (diff) | |
download | mrust-502cc9c8dc1941877b35e45d742a3e36264671ca.tar.gz |
HIR - Save and use paths to #[lang] items
Diffstat (limited to 'src')
-rw-r--r-- | src/ast/crate.hpp | 2 | ||||
-rw-r--r-- | src/expand/lang_item.cpp | 22 | ||||
-rw-r--r-- | src/hir/from_ast.cpp | 6 | ||||
-rw-r--r-- | src/hir/hir.cpp | 31 | ||||
-rw-r--r-- | src/hir/hir.hpp | 3 |
5 files changed, 29 insertions, 35 deletions
diff --git a/src/ast/crate.hpp b/src/ast/crate.hpp index 79a5d57f..24106bfa 100644 --- a/src/ast/crate.hpp +++ b/src/ast/crate.hpp @@ -19,7 +19,7 @@ public: ::AST::MetaItems m_attrs; - AST::Path m_lang_item_PhantomFn; + ::std::map< ::std::string, ::AST::Path> m_lang_items; public: Module m_root_module; ::std::map< ::std::string, ExternCrate> m_extern_crates; diff --git a/src/expand/lang_item.cpp b/src/expand/lang_item.cpp index c34642db..6d90370a 100644 --- a/src/expand/lang_item.cpp +++ b/src/expand/lang_item.cpp @@ -11,17 +11,18 @@ #include "../ast/crate.hpp" -void handle_lang_item(AST::Crate& crate, const AST::Path& path, const ::std::string& name, AST::eItemType type) +void handle_lang_item(const Span& sp, AST::Crate& crate, const AST::Path& path, const ::std::string& name, AST::eItemType type) { if(name == "phantom_fn") { - crate.m_lang_item_PhantomFn = AST::Path(path); - crate.m_lang_item_PhantomFn.nodes().back().args().m_types = { TypeRef("A"), TypeRef("B") }; + // - Just save path } else if( name == "send" ) { // Don't care, Send is fully library in mrustc + // - Needed for `static` } else if( name == "sync" ) { // Don't care, Sync is fully library in mrustc + // - Needed for `static` } else if( name == "sized" ) { DEBUG("Bind 'sized' to " << path); @@ -90,10 +91,13 @@ void handle_lang_item(AST::Crate& crate, const AST::Path& path, const ::std::str else if( name == "str_eq" ) { } else { - throw CompileError::Generic(FMT("Unknown lang item '" << name << "'")); + ERROR(sp, E0000, "Unknown language item '" << name << "'"); } - + auto rv = crate.m_lang_items.insert( ::std::make_pair( name, ::AST::Path(path) ) ); + if( !rv.second ) { + ERROR(sp, E0000, "Duplicate definition of language item '" << name << "'"); + } } class Decorator_LangItem: @@ -108,13 +112,13 @@ public: TODO(sp, "Unknown item type with #[lang=\""<<attr<<"\"] attached at " << path); ), (Function, - handle_lang_item(crate, path, attr.string(), AST::ITEM_FN); + handle_lang_item(sp, crate, path, attr.string(), AST::ITEM_FN); ), (Struct, - handle_lang_item(crate, path, attr.string(), AST::ITEM_STRUCT); + handle_lang_item(sp, crate, path, attr.string(), AST::ITEM_STRUCT); ), (Trait, - handle_lang_item(crate, path, attr.string(), AST::ITEM_TRAIT); + handle_lang_item(sp, crate, path, attr.string(), AST::ITEM_TRAIT); ) ) } @@ -137,6 +141,8 @@ public: else { ERROR(sp, E0000, "Unknown lang item '" << name << "' on impl"); } + + // TODO: Somehow annotate these impls to allow them to provide inherents? } }; diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index 3f297e89..6c873e1e 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -1028,6 +1028,12 @@ public: LowerHIR_Module_Impls(crate.m_root_module, rv); + auto sp = Span(); + for( const auto& lang_item_path : crate.m_lang_items ) + { + rv.m_lang_items.insert( ::std::make_pair(lang_item_path.first, LowerHIR_SimplePath(sp, lang_item_path.second)) ); + } + // Set all pointers in the HIR to the correct (now fixed) locations IndexVisitor(rv).visit_crate( rv ); diff --git a/src/hir/hir.cpp b/src/hir/hir.cpp index 1dce63f8..d9f85a53 100644 --- a/src/hir/hir.cpp +++ b/src/hir/hir.cpp @@ -153,33 +153,12 @@ bool ::HIR::MarkerImpl::matches_type(const ::HIR::TypeRef& type, ::HIR::t_cb_res const ::HIR::SimplePath& ::HIR::Crate::get_lang_item_path(const Span& sp, const char* name) const { - if( ::std::strcmp(name, "index") == 0 ) { - static ::HIR::SimplePath lang_index { "", {"ops", "Index"} }; - return lang_index; - } - else if( ::std::strcmp(name, "unsize") == 0 ) { - static ::HIR::SimplePath lang_unsize { "", {"marker", "Unsize"} }; - return lang_unsize; - } - else if( ::std::strcmp(name, "add") == 0 ) { - static ::HIR::SimplePath lang_path { "", {"ops", "Add"} }; - return lang_path; - } - else if( ::std::strcmp(name, "ord") == 0 ) { - static ::HIR::SimplePath lang_path { "", {"cmp", "PartialOrd"} }; - return lang_path; - } - else if( ::std::strcmp(name, "eq") == 0 ) { - static ::HIR::SimplePath lang_path { "", {"cmp", "PartialEq"} }; - return lang_path; - } - else if( ::std::strcmp(name, "fn_once") == 0 ) { - static ::HIR::SimplePath lang_path { "", {"ops", "FnOnce"} }; - return lang_path; - } - else { - ERROR(sp, E0000, "Unknown language item '" << name << "' encountered"); + // TODO: have map stored in crate populated by (or from) the #[lang] attribute handler + auto it = this->m_lang_items.find( name ); + if( it == this->m_lang_items.end() ) { + ERROR(sp, E0000, "Undefined language item '" << name << "' required"); } + return it->second; } const ::HIR::TypeItem& ::HIR::Crate::get_typeitem_by_path(const Span& sp, const ::HIR::SimplePath& path) const diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp index 3447aaa0..a938579a 100644 --- a/src/hir/hir.hpp +++ b/src/hir/hir.hpp @@ -263,6 +263,9 @@ public: /// Macros exported by this crate ::std::unordered_map< ::std::string, ::MacroRules > m_exported_macros; + /// Language items avaliable through this crate (includes ones from loaded externs) + ::std::unordered_map< ::std::string, ::HIR::SimplePath> m_lang_items; + const ::HIR::SimplePath& get_lang_item_path(const Span& sp, const char* name) const; const ::HIR::TypeItem& get_typeitem_by_path(const Span& sp, const ::HIR::SimplePath& path) const; |