diff options
Diffstat (limited to 'src/ast')
-rw-r--r-- | src/ast/ast.cpp | 44 | ||||
-rw-r--r-- | src/ast/ast.hpp | 69 | ||||
-rw-r--r-- | src/ast/attrs.hpp | 107 | ||||
-rw-r--r-- | src/ast/crate.cpp | 6 | ||||
-rw-r--r-- | src/ast/crate.hpp | 2 | ||||
-rw-r--r-- | src/ast/dump.cpp | 4 | ||||
-rw-r--r-- | src/ast/expr.hpp | 10 |
7 files changed, 132 insertions, 110 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index a4825a89..641bd633 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -14,8 +14,8 @@ namespace AST { namespace { - ::std::vector<MetaItem> clone_mivec(const ::std::vector<MetaItem>& v) { - ::std::vector<MetaItem> ri; + ::std::vector<Attribute> clone_mivec(const ::std::vector<Attribute>& v) { + ::std::vector<Attribute> ri; ri.reserve(v.size()); for(const auto& i : v) ri.push_back( i.clone() ); @@ -23,19 +23,16 @@ namespace { } } -MetaItems::~MetaItems() +AttributeList AttributeList::clone() const { -} -MetaItems MetaItems::clone() const -{ - return MetaItems( m_span, clone_mivec(m_items) ); + return AttributeList( clone_mivec(m_items) ); } -void MetaItems::push_back(MetaItem i) +void AttributeList::push_back(Attribute i) { m_items.push_back( ::std::move(i) ); } -const MetaItem* MetaItems::get(const char *name) const +const Attribute* AttributeList::get(const char *name) const { for( auto& i : m_items ) { if(i.name() == name) { @@ -46,23 +43,20 @@ const MetaItem* MetaItems::get(const char *name) const return 0; } -MetaItem::~MetaItem() +Attribute Attribute::clone() const { -} -MetaItem MetaItem::clone() const -{ - TU_MATCH(MetaItemData, (m_data), (e), + TU_MATCHA( (m_data), (e), (None, - return MetaItem(m_name); + return Attribute(m_span, m_name); ), (String, - return MetaItem(m_name, e.val); + return Attribute(m_span, m_name, e.val); ), (List, - return MetaItem(m_name, clone_mivec(e.sub_items)); + return Attribute(m_span, m_name, clone_mivec(e.sub_items)); ) ) - throw ::std::runtime_error("MetaItem::clone - Fell off end"); + throw ::std::runtime_error("Attribute::clone - Fell off end"); } StructItem StructItem::clone() const @@ -109,16 +103,16 @@ Function Function::clone() const return rv; } -void Trait::add_type(::std::string name, MetaItems attrs, TypeRef type) { +void Trait::add_type(::std::string 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_function(::std::string name, MetaItems attrs, Function fcn) { +void Trait::add_function(::std::string 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); } -void Trait::add_static(::std::string name, MetaItems attrs, Static v) { +void Trait::add_static(::std::string 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); } @@ -301,18 +295,18 @@ void Module::add_item( Named<Item> named_item ) { DEBUG(m_my_path << "::" << i.name << " = " << i.data.tag_str() << ", attrs = " << i.data.attrs); } } -void Module::add_item(bool is_pub, ::std::string name, Item it, MetaItems attrs) { +void Module::add_item(bool is_pub, ::std::string name, Item it, AttributeList attrs) { it.attrs = mv$(attrs); add_item( Named<Item>( mv$(name), mv$(it), is_pub ) ); } -void Module::add_ext_crate(bool is_public, ::std::string ext_name, ::std::string imp_name, MetaItems attrs) { +void Module::add_ext_crate(bool is_public, ::std::string ext_name, ::std::string imp_name, AttributeList attrs) { this->add_item( is_public, imp_name, Item::make_Crate({mv$(ext_name)}), mv$(attrs) ); } -void Module::add_alias(bool is_public, UseStmt us, ::std::string name, MetaItems attrs) { +void Module::add_alias(bool is_public, UseStmt us, ::std::string name, AttributeList attrs) { this->add_item( is_public, mv$(name), Item(mv$(us)), mv$(attrs) ); } void Module::add_macro_invocation(MacroInvocation item) { - this->add_item( false, "", Item( mv$(item) ), ::AST::MetaItems {} ); + this->add_item( false, "", Item( mv$(item) ), ::AST::AttributeList {} ); } void Module::add_macro(bool is_exported, ::std::string name, MacroRulesPtr macro) { m_macros.push_back( Named<MacroRulesPtr>( mv$(name), mv$(macro), is_exported ) ); diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index 6b729f8c..3d7c2dfb 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -51,14 +51,14 @@ enum eItemType struct StructItem { - ::AST::MetaItems m_attrs; + ::AST::AttributeList m_attrs; bool m_is_public; ::std::string m_name; TypeRef m_type; //StructItem() {} - StructItem(::AST::MetaItems attrs, bool is_pub, ::std::string name, TypeRef ty): + StructItem(::AST::AttributeList attrs, bool is_pub, ::std::string name, TypeRef ty): m_attrs( mv$(attrs) ), m_is_public(is_pub), m_name( mv$(name) ), @@ -75,13 +75,13 @@ struct StructItem struct TupleItem { - ::AST::MetaItems m_attrs; + ::AST::AttributeList m_attrs; bool m_is_public; TypeRef m_type; //TupleItem() {} - TupleItem(::AST::MetaItems attrs, bool is_pub, TypeRef ty): + TupleItem(::AST::AttributeList attrs, bool is_pub, TypeRef ty): m_attrs( mv$(attrs) ), m_is_public(is_pub), m_type( mv$(ty) ) @@ -129,9 +129,6 @@ private: TypeRef m_type; Expr m_value; public: - //Static(): - // m_class(CONST) - //{} Static(Class s_class, TypeRef type, Expr value): m_class(s_class), m_type( move(type) ), @@ -155,13 +152,11 @@ public: private: Span m_span; - //::std::string m_lifetime; GenericParams m_params; Expr m_code; TypeRef m_rettype; Arglist m_args; - // TODO: ABI, const, and unsafe ::std::string m_abi; bool m_is_const; bool m_is_unsafe; @@ -172,7 +167,6 @@ public: Function(Function&&) = default; Function& operator=(Function&&) = default; - //Function() {} Function(Span sp, GenericParams params, ::std::string abi, bool is_unsafe, bool is_const, bool is_variadic, TypeRef ret_type, Arglist args); void set_code(Expr code) { m_code = ::std::move(code); } @@ -200,15 +194,18 @@ class Trait ::std::vector< Spanned<AST::Path> > m_supertraits; bool m_is_marker; + bool m_is_unsafe; NamedList<Item> m_items; public: Trait(): - m_is_marker(false) + m_is_marker(false), + m_is_unsafe(false) {} Trait(GenericParams params, ::std::vector< Spanned<Path> > supertraits): m_params( mv$(params) ), m_supertraits( mv$(supertraits) ), - m_is_marker(false) + m_is_marker(false), + m_is_unsafe(false) { } @@ -220,12 +217,14 @@ public: const NamedList<Item>& items() const { return m_items; } NamedList<Item>& items() { return m_items; } - void add_type(::std::string name, MetaItems attrs, TypeRef type); - void add_function(::std::string name, MetaItems attrs, Function fcn); - void add_static(::std::string name, MetaItems attrs, Static v); + void add_type(::std::string name, AttributeList attrs, TypeRef type); + void add_function(::std::string name, AttributeList attrs, Function fcn); + void add_static(::std::string name, AttributeList attrs, Static v); void set_is_marker(); bool is_marker() const; + void set_is_unsafe() { m_is_unsafe = true; } + bool is_unsafe() const { return m_is_unsafe; } bool has_named_item(const ::std::string& name, bool& out_is_fcn) const; @@ -252,7 +251,7 @@ TAGGED_UNION_EX(EnumVariantData, (), Value, struct EnumVariant { - MetaItems m_attrs; + AttributeList m_attrs; ::std::string m_name; EnumVariantData m_data; @@ -260,21 +259,21 @@ struct EnumVariant { } - EnumVariant(MetaItems attrs, ::std::string name, Expr&& value): + EnumVariant(AttributeList attrs, ::std::string name, Expr&& value): m_attrs( mv$(attrs) ), m_name( mv$(name) ), m_data( EnumVariantData::make_Value({mv$(value)}) ) { } - EnumVariant(MetaItems attrs, ::std::string name, ::std::vector<TypeRef> sub_types): + EnumVariant(AttributeList attrs, ::std::string name, ::std::vector<TypeRef> sub_types): m_attrs( mv$(attrs) ), m_name( ::std::move(name) ), m_data( EnumVariantData::make_Tuple( {mv$(sub_types)} ) ) { } - EnumVariant(MetaItems attrs, ::std::string name, ::std::vector<StructItem> fields): + EnumVariant(AttributeList attrs, ::std::string name, ::std::vector<StructItem> fields): m_attrs( mv$(attrs) ), m_name( ::std::move(name) ), m_data( EnumVariantData::make_Struct( {mv$(fields)} ) ) @@ -380,27 +379,29 @@ public: class ImplDef { - Span m_span; - MetaItems m_attrs; + AttributeList m_attrs; + bool m_is_unsafe; GenericParams m_params; Spanned<Path> m_trait; TypeRef m_type; public: - //ImplDef() {} - ImplDef(ImplDef&&) /*noexcept*/ = default; - ImplDef(Span sp, MetaItems attrs, GenericParams params, Spanned<Path> trait_type, TypeRef impl_type): - m_span( mv$(sp) ), + ImplDef(AttributeList attrs, GenericParams params, Spanned<Path> trait_type, TypeRef impl_type): m_attrs( mv$(attrs) ), + m_is_unsafe( false ), m_params( mv$(params) ), m_trait( mv$(trait_type) ), m_type( mv$(impl_type) ) {} + + ImplDef(ImplDef&&) /*noexcept*/ = default; ImplDef& operator=(ImplDef&&) = default; + void set_is_unsafe() { m_is_unsafe = true; } + bool is_unsafe() const { return m_is_unsafe; } + // Accessors - const Span& span() const { return m_span; } - const MetaItems& attrs() const { return m_attrs; } - MetaItems& attrs() { return m_attrs; } + const AttributeList& attrs() const { return m_attrs; } + AttributeList& attrs() { return m_attrs; } const GenericParams& params() const { return m_params; } GenericParams& params() { return m_params; } @@ -426,7 +427,6 @@ public: private: ImplDef m_def; - Span m_span; ::std::vector< ImplItem > m_items; //NamedList<TypeRef> m_types; @@ -434,7 +434,6 @@ private: //NamedList<Static> m_statics; public: - //Impl() {} Impl(Impl&&) /*noexcept*/ = default; Impl(ImplDef def): m_def( mv$(def) ) @@ -464,8 +463,6 @@ struct UseStmt ::AST::Path path; ::AST::PathBinding alt_binding; - UseStmt() - {} UseStmt(Span sp, Path p): sp(sp), path(p) @@ -553,9 +550,9 @@ public: ::std::shared_ptr<AST::Module> add_anon(); void add_item(Named<Item> item); - void add_item(bool is_pub, ::std::string name, Item it, MetaItems attrs); - void add_ext_crate(bool is_public, ::std::string ext_name, ::std::string imp_name, MetaItems attrs); - void add_alias(bool is_public, UseStmt path, ::std::string name, MetaItems attrs); + void add_item(bool is_pub, ::std::string name, Item it, AttributeList attrs); + void add_ext_crate(bool is_public, ::std::string ext_name, ::std::string imp_name, AttributeList attrs); + void add_alias(bool is_public, UseStmt path, ::std::string name, AttributeList attrs); void add_macro_invocation(MacroInvocation item); void add_macro(bool is_exported, ::std::string name, MacroRulesPtr macro); @@ -609,7 +606,7 @@ TAGGED_UNION_EX(Item, (), None, (, attrs(mv$(x.attrs))), (attrs = mv$(x.attrs);), ( public: - MetaItems attrs; + AttributeList attrs; Span span; Item clone() const; diff --git a/src/ast/attrs.hpp b/src/ast/attrs.hpp index 28bfec96..4afbdbc1 100644 --- a/src/ast/attrs.hpp +++ b/src/ast/attrs.hpp @@ -1,3 +1,10 @@ +/* + * MRustC - Rust Compiler + * - By John Hodge (Mutabah/thePowersGang) + * + * ast/attrs.hpp + * - AST Attributes (#[foo] and #![foo]) + */ #ifndef _AST_ATTRS_HPP_ #define _AST_ATTRS_HPP_ @@ -5,93 +12,117 @@ namespace AST { // -class MetaItem; +class Attribute; +::std::ostream& operator<<(::std::ostream& os, const Attribute& x); -class MetaItems +/// A list of attributes on an item (searchable by the attribute name) +class AttributeList { public: - Span m_span; - ::std::vector<MetaItem> m_items; - - virtual ~MetaItems(); - MetaItems() {} - MetaItems(MetaItems&&) = default; - MetaItems& operator=(MetaItems&&) = default; - MetaItems(const MetaItems&) = delete; - MetaItems(Span sp, ::std::vector<MetaItem> items): - m_span( mv$(sp) ), + ::std::vector<Attribute> m_items; + + AttributeList() {} + AttributeList(::std::vector<Attribute> items): m_items( mv$(items) ) { } - void push_back(MetaItem i); + // Move present + AttributeList(AttributeList&&) = default; + AttributeList& operator=(AttributeList&&) = default; + // No copy + AttributeList(const AttributeList&) = delete; + AttributeList& operator=(const AttributeList&) = delete; + // Explicit clone + AttributeList clone() const; - MetaItems clone() const; + void push_back(Attribute i); - MetaItem* get(const char *name) { return const_cast<MetaItem*>( const_cast<const MetaItems*>(this)->get(name)); } - const MetaItem* get(const char *name) const; + const Attribute* get(const char *name) const; + Attribute* get(const char *name) { + return const_cast<Attribute*>( const_cast<const AttributeList*>(this)->get(name)); + } bool has(const char *name) const { return get(name) != 0; } - friend ::std::ostream& operator<<(::std::ostream& os, const MetaItems& x) { - return os << "[" << x.m_items << "]"; + friend ::std::ostream& operator<<(::std::ostream& os, const AttributeList& x) { + for(const auto& i : x.m_items) { + os << "#[" << i << "]"; + } + return os; } }; -TAGGED_UNION(MetaItemData, None, +TAGGED_UNION(AttributeData, None, (None, struct {}), (String, struct { ::std::string val; }), (List, struct { - ::std::vector<MetaItem> sub_items; + ::std::vector<Attribute> sub_items; }) ); -class MetaItem +// An attribute can has a name, and optional data: +// Data can be: +// - A parenthesised token tree +// > In 1.19 this was actually just sub-attributes +// - an associated (string) literal + +class Attribute { + Span m_span; ::std::string m_name; - MetaItemData m_data; + AttributeData m_data; + mutable bool m_is_used; public: - virtual ~MetaItem(); - MetaItem() {} - MetaItem(MetaItem&& ) = default; - MetaItem& operator=(MetaItem&& ) = default; - MetaItem(::std::string name): + Attribute(Span sp, ::std::string name): + m_span(::std::move(sp)), m_name(name), - m_data( MetaItemData::make_None({}) ) + m_data( AttributeData::make_None({}) ) { } - MetaItem(::std::string name, ::std::string str_val): + Attribute(Span sp, ::std::string name, ::std::string str_val): + m_span(::std::move(sp)), m_name(name), - m_data( MetaItemData::make_String({mv$(str_val)}) ) + m_data( AttributeData::make_String({mv$(str_val)}) ) { } - MetaItem(::std::string name, ::std::vector<MetaItem> items): + Attribute(Span sp, ::std::string name, ::std::vector<Attribute> items): + m_span(::std::move(sp)), m_name(name), - m_data( MetaItemData::make_List({mv$(items)}) ) + m_data( AttributeData::make_List({mv$(items)}) ) { } - MetaItem clone() const; + Attribute(const Attribute& ) = delete; + Attribute& operator=(const Attribute&& ) = delete; + Attribute(Attribute&& ) = default; + Attribute& operator=(Attribute&& ) = default; + Attribute clone() const; + + void mark_used() const { m_is_used = true; } + bool is_used() const { return m_is_used; } - void mark_used() {} + const Span& span() const { return m_span; } const ::std::string& name() const { return m_name; } + const AttributeData& data() const { return m_data; } + // Legacy accessors/checkers bool has_noarg() const { return m_data.is_None(); } bool has_string() const { return m_data.is_String(); } const ::std::string& string() const { return m_data.as_String().val; } bool has_sub_items() const { return m_data.is_List(); } - const ::std::vector<MetaItem>& items() const { return m_data.as_List().sub_items; } - ::std::vector<MetaItem>& items() { return m_data.as_List().sub_items; } + const ::std::vector<Attribute>& items() const { return m_data.as_List().sub_items; } + ::std::vector<Attribute>& items() { return m_data.as_List().sub_items; } - friend ::std::ostream& operator<<(::std::ostream& os, const MetaItem& x) { + friend ::std::ostream& operator<<(::std::ostream& os, const Attribute& x) { os << x.m_name; - TU_MATCH(MetaItemData, (x.m_data), (e), + TU_MATCHA( (x.m_data), (e), (None, ), (String, diff --git a/src/ast/crate.cpp b/src/ast/crate.cpp index 823f7d00..9e4ed2ad 100644 --- a/src/ast/crate.cpp +++ b/src/ast/crate.cpp @@ -12,10 +12,10 @@ ::std::map<::std::string, ::std::string> AST::g_crate_overrides; namespace { - bool check_item_cfg(const ::AST::MetaItems& attrs) + bool check_item_cfg(const ::AST::AttributeList& attrs) { for(const auto& at : attrs.m_items) { - if( at.name() == "cfg" && !check_cfg(attrs.m_span, at) ) { + if( at.name() == "cfg" && !check_cfg(at.span(), at) ) { return false; } } @@ -78,7 +78,7 @@ void Crate::load_externs() if( a.name() == "no_core" ) no_core = true; if( a.name() == "cfg_attr" && a.items().size() == 2 ) { - if( check_cfg(this->m_attrs.m_span, a.items().at(0)) ) + if( check_cfg(a.span(), a.items().at(0)) ) { const auto& a2 = a.items().at(1); if( a2.name() == "no_std" ) diff --git a/src/ast/crate.hpp b/src/ast/crate.hpp index d6628901..bf1758d7 100644 --- a/src/ast/crate.hpp +++ b/src/ast/crate.hpp @@ -38,7 +38,7 @@ public: class Crate { public: - ::AST::MetaItems m_attrs; + ::AST::AttributeList m_attrs; ::std::map< ::std::string, ::AST::Path> m_lang_items; public: diff --git a/src/ast/dump.cpp b/src/ast/dump.cpp index 7026f61d..b6336a02 100644 --- a/src/ast/dump.cpp +++ b/src/ast/dump.cpp @@ -560,7 +560,7 @@ private: m_os << ")"; } - void print_attrs(const AST::MetaItems& attrs); + void print_attrs(const AST::AttributeList& attrs); void print_params(const AST::GenericParams& params); void print_bounds(const AST::GenericParams& params); void print_pattern_tuple(const AST::Pattern::TuplePat& v, bool is_refutable); @@ -579,7 +579,7 @@ void Dump_Rust(const char *filename, const AST::Crate& crate) printer.handle_module(crate.root_module()); } -void RustPrinter::print_attrs(const AST::MetaItems& attrs) +void RustPrinter::print_attrs(const AST::AttributeList& attrs) { for(const auto& a : attrs.m_items) { diff --git a/src/ast/expr.hpp b/src/ast/expr.hpp index 2c9a1c35..7572b92f 100644 --- a/src/ast/expr.hpp +++ b/src/ast/expr.hpp @@ -25,7 +25,7 @@ class NodeVisitor; class ExprNode { - MetaItems m_attrs; + AttributeList m_attrs; Span m_span; public: virtual ~ExprNode() = 0; @@ -37,10 +37,10 @@ public: void set_span(Span s) { m_span = ::std::move(s); } const Span& span() const { return m_span; } - void set_attrs(MetaItems&& mi) { + void set_attrs(AttributeList&& mi) { m_attrs = mv$(mi); } - MetaItems& attrs() { return m_attrs; } + AttributeList& attrs() { return m_attrs; } static ::std::unique_ptr<ExprNode> from_deserialiser(Deserialiser& d); }; @@ -263,7 +263,7 @@ struct ExprNode_Loop: struct ExprNode_Match_Arm { - MetaItems m_attrs; + AttributeList m_attrs; ::std::vector<Pattern> m_patterns; unique_ptr<ExprNode> m_cond; @@ -418,7 +418,7 @@ struct ExprNode_StructLiteral: public ExprNode { struct Ent { - MetaItems attrs; + AttributeList attrs; ::std::string name; unique_ptr<ExprNode> value; }; |