From 502cc9c8dc1941877b35e45d742a3e36264671ca Mon Sep 17 00:00:00 2001 From: John Hodge Date: Thu, 9 Jun 2016 09:54:57 +0800 Subject: HIR - Save and use paths to #[lang] items --- src/ast/crate.hpp | 2 +- src/expand/lang_item.cpp | 22 ++++++++++++++-------- src/hir/from_ast.cpp | 6 ++++++ src/hir/hir.cpp | 31 +++++-------------------------- 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=\""<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; -- cgit v1.2.3