diff options
author | John Hodge <tpg@ucc.asn.au> | 2019-10-06 12:23:25 +0800 |
---|---|---|
committer | John Hodge <tpg@ucc.asn.au> | 2019-10-06 12:23:25 +0800 |
commit | 848aa5c4dc438aedaaf5e1146e4788a1f0c43eff (patch) | |
tree | 5cd12eb35dd3aa34a442fae516477cb630c8db31 /src/ast | |
parent | 7f1410e86893d92f80f4c299943aaad5ee4bea73 (diff) | |
download | mrust-848aa5c4dc438aedaaf5e1146e4788a1f0c43eff.tar.gz |
AST - Clean up location of attributes and span on mod-level items
Diffstat (limited to 'src/ast')
-rw-r--r-- | src/ast/ast.cpp | 68 | ||||
-rw-r--r-- | src/ast/ast.hpp | 24 | ||||
-rw-r--r-- | src/ast/attrs.hpp | 1 | ||||
-rw-r--r-- | src/ast/crate.cpp | 18 | ||||
-rw-r--r-- | src/ast/dump.cpp | 16 | ||||
-rw-r--r-- | src/ast/item.hpp | 13 |
6 files changed, 71 insertions, 69 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index f1a68680..827ac2d1 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -107,18 +107,15 @@ Function Function::clone() const return rv; } -void Trait::add_type(RcString name, AttributeList attrs, TypeRef type) { - m_items.push_back( Named<Item>(mv$(name), Item::make_Type({TypeAlias(GenericParams(), mv$(type))}), true) ); - m_items.back().data.attrs = mv$(attrs); +void Trait::add_type(Span sp, RcString name, AttributeList attrs, TypeRef type) { + m_items.push_back( Named<Item>(sp, mv$(attrs), true, mv$(name), Item::make_Type({TypeAlias(GenericParams(), mv$(type))})) ); } -void Trait::add_function(RcString name, AttributeList attrs, Function fcn) { +void Trait::add_function(Span sp, RcString name, AttributeList attrs, Function fcn) { DEBUG("trait fn " << name); - m_items.push_back( Named<Item>(mv$(name), Item::make_Function({mv$(fcn)}), true) ); - m_items.back().data.attrs = mv$(attrs); + m_items.push_back( Named<Item>(sp, mv$(attrs), true, mv$(name), Item::make_Function({mv$(fcn)})) ); } -void Trait::add_static(RcString name, AttributeList attrs, Static v) { - m_items.push_back( Named<Item>(mv$(name), Item::make_Static({mv$(v)}), true) ); - m_items.back().data.attrs = mv$(attrs); +void Trait::add_static(Span sp, RcString name, AttributeList attrs, Static v) { + m_items.push_back( Named<Item>(sp, mv$(attrs), true, mv$(name), Item::make_Static({mv$(v)})) ); } void Trait::set_is_marker() { m_is_marker = true; @@ -143,7 +140,7 @@ Trait Trait::clone() const auto rv = Trait(m_params.clone(), m_supertraits); for(const auto& item : m_items) { - rv.m_items.push_back( Named<Item> { item.name, item.data.clone(), item.is_pub } ); + rv.m_items.push_back( Named<Item> { item.span, item.attrs.clone(), item.is_pub, item.name, item.data.clone() } ); } return rv; } @@ -208,21 +205,21 @@ Union Union::clone() const return os << "impl<" << impl.m_params << "> " << impl.m_trait.ent << " for " << impl.m_type << ""; } -void Impl::add_function(bool is_public, bool is_specialisable, RcString name, Function fcn) +void Impl::add_function(Span sp, AttributeList attrs, bool is_public, bool is_specialisable, RcString name, Function fcn) { DEBUG("impl fn " << name); - m_items.push_back( ImplItem { is_public, is_specialisable, mv$(name), box$( Item::make_Function(mv$(fcn)) ) } ); + m_items.push_back( ImplItem { sp, mv$(attrs), is_public, is_specialisable, mv$(name), box$( Item::make_Function(mv$(fcn)) ) } ); } -void Impl::add_type(bool is_public, bool is_specialisable, RcString name, TypeRef type) +void Impl::add_type(Span sp, AttributeList attrs, bool is_public, bool is_specialisable, RcString name, TypeRef type) { - m_items.push_back( ImplItem { is_public, is_specialisable, mv$(name), box$( Item::make_Type(TypeAlias(GenericParams(), mv$(type))) ) } ); + m_items.push_back( ImplItem { sp, mv$(attrs), is_public, is_specialisable, mv$(name), box$( Item::make_Type(TypeAlias(GenericParams(), mv$(type))) ) } ); } -void Impl::add_static(bool is_public, bool is_specialisable, RcString name, Static v) +void Impl::add_static(Span sp, AttributeList attrs, bool is_public, bool is_specialisable, RcString name, Static v) { - m_items.push_back( ImplItem { is_public, is_specialisable, mv$(name), box$( Item::make_Static(mv$(v)) ) } ); + m_items.push_back( ImplItem { sp, mv$(attrs), is_public, is_specialisable, mv$(name), box$( Item::make_Static(mv$(v)) ) } ); } void Impl::add_macro_invocation(MacroInvocation item) { - m_items.push_back( ImplItem { false, false, "", box$( Item::make_MacroInv(mv$(item)) ) } ); + m_items.push_back( ImplItem { item.span(), {}, false, false, "", box$( Item::make_MacroInv(mv$(item)) ) } ); } bool Impl::has_named_item(const RcString& name) const @@ -262,7 +259,7 @@ UseItem UseItem::clone() const void ExternBlock::add_item(Named<Item> named_item) { - ASSERT_BUG(named_item.data.span, named_item.data.is_Function() || named_item.data.is_Static() || named_item.data.is_Type(), "Incorrect item type for ExternBlock - " << named_item.data.tag_str()); + ASSERT_BUG(named_item.span, named_item.data.is_Function() || named_item.data.is_Static() || named_item.data.is_Type(), "Incorrect item type for ExternBlock - " << named_item.data.tag_str()); m_items.push_back( mv$(named_item) ); } ExternBlock ExternBlock::clone() const @@ -286,55 +283,54 @@ void Module::add_item( Named<Item> named_item ) { if( i.name == "" ) { } else { - DEBUG(m_my_path << "::" << i.name << " = " << i.data.tag_str() << ", attrs = " << i.data.attrs); + DEBUG(m_my_path << "::" << i.name << " = " << i.data.tag_str() << ", attrs = " << i.attrs); } } -void Module::add_item(bool is_pub, RcString name, Item it, AttributeList attrs) { - it.attrs = mv$(attrs); - add_item( Named<Item>( mv$(name), mv$(it), is_pub ) ); +void Module::add_item(Span sp, bool is_pub, RcString name, Item it, AttributeList attrs) { + add_item( Named<Item>( mv$(sp), mv$(attrs), is_pub, mv$(name), mv$(it) ) ); } -void Module::add_ext_crate(bool is_public, RcString ext_name, RcString imp_name, AttributeList attrs) { - this->add_item( is_public, imp_name, Item::make_Crate({mv$(ext_name)}), mv$(attrs) ); +void Module::add_ext_crate(Span sp, bool is_pub, RcString ext_name, RcString imp_name, AttributeList attrs) { + this->add_item( mv$(sp), is_pub, imp_name, Item::make_Crate({mv$(ext_name)}), mv$(attrs) ); } void Module::add_macro_invocation(MacroInvocation item) { - this->add_item( false, "", Item( mv$(item) ), ::AST::AttributeList {} ); + this->add_item( item.span(), false, "", Item( mv$(item) ), ::AST::AttributeList {} ); } void Module::add_macro(bool is_exported, RcString name, MacroRulesPtr macro) { - m_macros.push_back( Named<MacroRulesPtr>( mv$(name), mv$(macro), is_exported ) ); + m_macros.push_back( Named<MacroRulesPtr>( Span(), {}, /*is_pub=*/is_exported, mv$(name), mv$(macro) ) ); } void Module::add_macro_import(RcString name, const MacroRules& mr) { - m_macro_import_res.push_back( Named<const MacroRules*>( mv$(name), &mr, false ) ); + m_macro_import_res.push_back( Named<const MacroRules*>( Span(), /*attrs=*/{}, /*is_pub=*/false, mv$(name), &mr) ); } Item Item::clone() const { TU_MATCHA( (*this), (e), (None, - return AST::Item(e); + return Item(e); ), (MacroInv, - TODO(this->span, "Clone on Item::MacroInv"); + TODO(Span(), "Clone on Item::MacroInv"); ), (Macro, - TODO(this->span, "Clone on Item::Macro"); + TODO(Span(), "Clone on Item::Macro"); ), (Use, - return AST::Item(e.clone()); + return Item(e.clone()); ), (ExternBlock, - TODO(this->span, "Clone on Item::" << this->tag_str()); + TODO(Span(), "Clone on Item::" << this->tag_str()); ), (Impl, - TODO(this->span, "Clone on Item::Impl"); + TODO(Span(), "Clone on Item::" << this->tag_str()); ), (NegImpl, - TODO(this->span, "Clone on Item::NegImpl"); + TODO(Span(), "Clone on Item::" << this->tag_str()); ), (Module, - TODO(this->span, "Clone on Item::Module"); + TODO(Span(), "Clone on Item::" << this->tag_str()); ), (Crate, - return AST::Item(e); + return Item(e); ), (Type, return AST::Item(e.clone()); diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index a6c06e76..1f42a764 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -223,9 +223,9 @@ public: const NamedList<Item>& items() const { return m_items; } NamedList<Item>& items() { return m_items; } - void add_type(RcString name, AttributeList attrs, TypeRef type); - void add_function(RcString name, AttributeList attrs, Function fcn); - void add_static(RcString name, AttributeList attrs, Static v); + void add_type(Span sp, RcString name, AttributeList attrs, TypeRef type); + void add_function(Span sp, RcString name, AttributeList attrs, Function fcn); + void add_static(Span sp, RcString name, AttributeList attrs, Static v); void set_is_marker(); bool is_marker() const; @@ -424,6 +424,8 @@ class Impl { public: struct ImplItem { + Span sp; + AttributeList attrs; bool is_pub; // Ignored for trait impls bool is_specialisable; RcString name; @@ -446,9 +448,9 @@ public: {} Impl& operator=(Impl&&) = default; - void add_function(bool is_public, bool is_specialisable, RcString name, Function fcn); - void add_type(bool is_public, bool is_specialisable, RcString name, TypeRef type); - void add_static(bool is_public, bool is_specialisable, RcString name, Static v); + void add_function(Span sp, AttributeList attrs, bool is_public, bool is_specialisable, RcString name, Function fcn); + void add_type(Span sp, AttributeList attrs, bool is_public, bool is_specialisable, RcString name, TypeRef type); + void add_static(Span sp, AttributeList attrs, bool is_public, bool is_specialisable, RcString name, Static v); void add_macro_invocation( MacroInvocation inv ); const ImplDef& def() const { return m_def; } @@ -570,8 +572,8 @@ public: ::std::shared_ptr<AST::Module> add_anon(); void add_item(Named<Item> item); - void add_item(bool is_pub, RcString name, Item it, AttributeList attrs); - void add_ext_crate(bool is_public, RcString ext_name, RcString imp_name, AttributeList attrs); + void add_item(Span sp, bool is_pub, RcString name, Item it, AttributeList attrs); + void add_ext_crate(Span sp, bool is_pub, RcString ext_name, RcString imp_name, AttributeList attrs); void add_macro_invocation(MacroInvocation item); void add_macro(bool is_exported, RcString name, MacroRulesPtr macro); @@ -621,12 +623,8 @@ TAGGED_UNION_EX(Item, (), None, (Static, Static) ), - (, attrs(mv$(x.attrs))), (attrs = mv$(x.attrs);), + (), (), ( - public: - AttributeList attrs; - Span span; - Item clone() const; ) ); diff --git a/src/ast/attrs.hpp b/src/ast/attrs.hpp index 04328130..7a6ce864 100644 --- a/src/ast/attrs.hpp +++ b/src/ast/attrs.hpp @@ -8,6 +8,7 @@ #ifndef _AST_ATTRS_HPP_ #define _AST_ATTRS_HPP_ +#include <tagged_union.hpp> namespace AST { diff --git a/src/ast/crate.cpp b/src/ast/crate.cpp index 726a00c6..e788f6e0 100644 --- a/src/ast/crate.cpp +++ b/src/ast/crate.cpp @@ -32,12 +32,13 @@ namespace { fcn(mod); for( auto& sm : mod.items() ) { - TU_IFLET(::AST::Item, sm.data, Module, e, - if( check_item_cfg(sm.data.attrs) ) + if( auto* e = sm.data.opt_Module() ) + { + if( check_item_cfg(sm.attrs) ) { - iterate_module(e, fcn); + iterate_module(*e, fcn); } - ) + } } // TODO: What about if an anon mod has been #[cfg]-d out? // - For now, disable @@ -61,12 +62,13 @@ void Crate::load_externs() auto cb = [this](Module& mod) { for( /*const*/ auto& it : mod.items() ) { - TU_IFLET(AST::Item, it.data, Crate, c, - if( check_item_cfg(it.data.attrs) ) + if( auto* c = it.data.opt_Crate() ) + { + if( check_item_cfg(it.attrs) ) { - c.name = load_extern_crate( it.data.span, c.name.c_str() ); + c->name = load_extern_crate( it.span, c->name.c_str() ); } - ) + } } }; iterate_module(m_root_module, cb); diff --git a/src/ast/dump.cpp b/src/ast/dump.cpp index 9fb34099..4e880a76 100644 --- a/src/ast/dump.cpp +++ b/src/ast/dump.cpp @@ -653,7 +653,7 @@ void RustPrinter::handle_module(const AST::Module& mod) if( !item.data.is_Crate() ) continue ; const auto& e = item.data.as_Crate(); - print_attrs(item.data.attrs); + print_attrs(item.attrs); m_os << indent() << "extern crate \"" << e.name << "\" as " << item.name << ";\n"; } @@ -662,7 +662,7 @@ void RustPrinter::handle_module(const AST::Module& mod) if( !item.data.is_ExternBlock() ) continue ; const auto& e = item.data.as_ExternBlock(); - print_attrs(item.data.attrs); + print_attrs(item.attrs); m_os << indent() << "extern \"" << e.abi() << "\" {}\n"; } @@ -690,7 +690,7 @@ void RustPrinter::handle_module(const AST::Module& mod) m_os << "\n"; need_nl = false; } - print_attrs(item.data.attrs); + print_attrs(item.attrs); m_os << indent() << (item.is_pub ? "pub " : "") << "type " << item.name; print_params(e.params()); m_os << " = " << e.type(); @@ -705,7 +705,7 @@ void RustPrinter::handle_module(const AST::Module& mod) const auto& e = item.data.as_Struct(); m_os << "\n"; - print_attrs(item.data.attrs); + print_attrs(item.attrs); m_os << indent() << (item.is_pub ? "pub " : "") << "struct " << item.name; handle_struct(e); } @@ -716,7 +716,7 @@ void RustPrinter::handle_module(const AST::Module& mod) const auto& e = item.data.as_Enum(); m_os << "\n"; - print_attrs(item.data.attrs); + print_attrs(item.attrs); m_os << indent() << (item.is_pub ? "pub " : "") << "enum " << item.name; handle_enum(e); } @@ -727,7 +727,7 @@ void RustPrinter::handle_module(const AST::Module& mod) const auto& e = item.data.as_Trait(); m_os << "\n"; - print_attrs(item.data.attrs); + print_attrs(item.attrs); m_os << indent() << (item.is_pub ? "pub " : "") << "trait " << item.name; handle_trait(e); } @@ -741,7 +741,7 @@ void RustPrinter::handle_module(const AST::Module& mod) m_os << "\n"; need_nl = false; } - print_attrs(item.data.attrs); + print_attrs(item.attrs); m_os << indent() << (item.is_pub ? "pub " : ""); switch( e.s_class() ) { @@ -760,7 +760,7 @@ void RustPrinter::handle_module(const AST::Module& mod) const auto& e = item.data.as_Function(); m_os << "\n"; - print_attrs(item.data.attrs); + print_attrs(item.attrs); handle_function(item.is_pub, item.name, e); } diff --git a/src/ast/item.hpp b/src/ast/item.hpp index ee83a8db..a5b0b1a5 100644 --- a/src/ast/item.hpp +++ b/src/ast/item.hpp @@ -9,15 +9,18 @@ #include <string> #include <vector> +#include "attrs.hpp" namespace AST { template <typename T> struct Named { + Span span; + AttributeList attrs; + bool is_pub; RcString name; T data; - bool is_pub; Named(): is_pub(false) @@ -25,10 +28,12 @@ struct Named Named(Named&&) = default; Named(const Named&) = default; Named& operator=(Named&&) = default; - Named(RcString name, T data, bool is_pub): + Named(Span sp, AttributeList attrs, bool is_pub, RcString name, T data): + span(sp), + attrs( ::std::move(attrs) ), + is_pub( is_pub ), name( ::std::move(name) ), - data( ::std::move(data) ), - is_pub( is_pub ) + data( ::std::move(data) ) { } }; |