From aa4d3c5fc5f45891411eb72188c5383a23683495 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 20 May 2018 11:42:40 +0800 Subject: General TODO cleanup --- src/resolve/absolute.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/resolve/absolute.cpp') diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index aece0032..5e9cb51c 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -714,7 +714,7 @@ namespace { TU_MATCH(::HIR::ValueItem, (it->second->ent), (e), (Import, // Wait? is this even valid? - TODO(sp, "HIR Import item pointed to an import"); + BUG(sp, "HIR Import item pointed to an import"); ), (Constant, pb = ::AST::PathBinding::make_Static({nullptr, nullptr}); @@ -741,7 +741,7 @@ namespace { TU_MATCH(::HIR::TypeItem, (it->second->ent), (e), (Import, // Wait? is this even valid? - TODO(sp, "HIR Import item pointed to an import"); + BUG(sp, "HIR Import item pointed to an import"); ), (Module, pb = ::AST::PathBinding::make_Module({nullptr, &e}); @@ -787,7 +787,7 @@ namespace { path.bind( ::AST::PathBinding::make_Module({nullptr, &crate.m_hir->m_root_module}) ); return ; default: - TODO(sp, ""); + TODO(sp, "Looking up a non-namespace, but pointed to crate root"); } } @@ -1180,7 +1180,6 @@ void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Contex ), (Module, if( name_ref.is_import ) { - //TODO(sp, "Replace path component with new path - " << path << "[.."<() ); } -- cgit v1.2.3 From 1c50e757b45f64ead016d6cd2bf27585ba5dce04 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 20 May 2018 15:02:17 +0800 Subject: AST - Rename MetaItem and MetaItems to Attribute and AttributeList --- src/ast/ast.cpp | 44 +++++++-------- src/ast/ast.hpp | 69 +++++++++++------------ src/ast/attrs.hpp | 107 +++++++++++++++++++++++------------- src/ast/crate.cpp | 6 +- src/ast/crate.hpp | 2 +- src/ast/dump.cpp | 4 +- src/ast/expr.hpp | 10 ++-- src/expand/cfg.cpp | 20 +++---- src/expand/cfg.hpp | 2 +- src/expand/crate_tags.cpp | 10 ++-- src/expand/derive.cpp | 26 ++++----- src/expand/lang_item.cpp | 8 +-- src/expand/macro_rules.cpp | 6 +- src/expand/mod.cpp | 23 ++++---- src/expand/proc_macro.cpp | 6 +- src/expand/std_prelude.cpp | 10 ++-- src/expand/test.cpp | 6 +- src/hir/from_ast.cpp | 14 ++--- src/include/synext_decorator.hpp | 22 ++++---- src/macro_rules/eval.cpp | 2 +- src/parse/common.hpp | 10 ++-- src/parse/expr.cpp | 8 +-- src/parse/interpolated_fragment.cpp | 8 +-- src/parse/interpolated_fragment.hpp | 4 +- src/parse/root.cpp | 105 +++++++++++++++++++---------------- src/parse/token.cpp | 10 ++-- src/parse/token.hpp | 4 +- src/parse/tokenstream.hpp | 4 +- src/resolve/absolute.cpp | 4 +- 29 files changed, 295 insertions(+), 259 deletions(-) (limited to 'src/resolve/absolute.cpp') 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 clone_mivec(const ::std::vector& v) { - ::std::vector ri; + ::std::vector clone_mivec(const ::std::vector& v) { + ::std::vector 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(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(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(mv$(name), Item::make_Static({mv$(v)}), true) ); m_items.back().data.attrs = mv$(attrs); } @@ -301,18 +295,18 @@ void Module::add_item( Named 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( 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( 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 > m_supertraits; bool m_is_marker; + bool m_is_unsafe; NamedList m_items; public: Trait(): - m_is_marker(false) + m_is_marker(false), + m_is_unsafe(false) {} Trait(GenericParams params, ::std::vector< Spanned > 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& items() const { return m_items; } NamedList& 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 sub_types): + EnumVariant(AttributeList attrs, ::std::string name, ::std::vector 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 fields): + EnumVariant(AttributeList attrs, ::std::string name, ::std::vector 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 m_trait; TypeRef m_type; public: - //ImplDef() {} - ImplDef(ImplDef&&) /*noexcept*/ = default; - ImplDef(Span sp, MetaItems attrs, GenericParams params, Spanned trait_type, TypeRef impl_type): - m_span( mv$(sp) ), + ImplDef(AttributeList attrs, GenericParams params, Spanned 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 m_types; @@ -434,7 +434,6 @@ private: //NamedList 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 add_anon(); void add_item(Named 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 m_items; - - virtual ~MetaItems(); - MetaItems() {} - MetaItems(MetaItems&&) = default; - MetaItems& operator=(MetaItems&&) = default; - MetaItems(const MetaItems&) = delete; - MetaItems(Span sp, ::std::vector items): - m_span( mv$(sp) ), + ::std::vector m_items; + + AttributeList() {} + AttributeList(::std::vector 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( const_cast(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( const_cast(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 sub_items; + ::std::vector 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 items): + Attribute(Span sp, ::std::string name, ::std::vector 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& items() const { return m_data.as_List().sub_items; } - ::std::vector& items() { return m_data.as_List().sub_items; } + const ::std::vector& items() const { return m_data.as_List().sub_items; } + ::std::vector& 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 from_deserialiser(Deserialiser& d); }; @@ -263,7 +263,7 @@ struct ExprNode_Loop: struct ExprNode_Match_Arm { - MetaItems m_attrs; + AttributeList m_attrs; ::std::vector m_patterns; unique_ptr m_cond; @@ -418,7 +418,7 @@ struct ExprNode_StructLiteral: public ExprNode { struct Ent { - MetaItems attrs; + AttributeList attrs; ::std::string name; unique_ptr value; }; diff --git a/src/expand/cfg.cpp b/src/expand/cfg.cpp index a2fca312..773a38c4 100644 --- a/src/expand/cfg.cpp +++ b/src/expand/cfg.cpp @@ -30,7 +30,7 @@ void Cfg_SetValueCb(::std::string name, ::std::function& expr) const override { + void handle(const Span& sp, const AST::Attribute& mi, ::AST::Crate& crate, ::std::unique_ptr& expr) const override { DEBUG("#[cfg] expr - " << mi); if( check_cfg(sp, mi) ) { // Leave @@ -144,7 +144,7 @@ class CCfgHandler: expr.reset(); } } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, const AST::Module& mod, AST::ImplDef& impl) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, const AST::Module& mod, AST::ImplDef& impl) const override { DEBUG("#[cfg] impl - " << mi); if( check_cfg(sp, mi) ) { // Leave @@ -154,32 +154,32 @@ class CCfgHandler: } } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::StructItem& si) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::StructItem& si) const override { DEBUG("#[cfg] struct item - " << mi); if( !check_cfg(sp, mi) ) { si.m_name = ""; } } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::TupleItem& i) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::TupleItem& i) const override { DEBUG("#[cfg] tuple item - " << mi); if( !check_cfg(sp, mi) ) { i.m_type = ::TypeRef(sp); } } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::EnumVariant& i) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::EnumVariant& i) const override { DEBUG("#[cfg] enum variant - " << mi); if( !check_cfg(sp, mi) ) { i.m_name = ""; } } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::ExprNode_Match_Arm& i) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::ExprNode_Match_Arm& i) const override { DEBUG("#[cfg] match arm - " << mi); if( !check_cfg(sp, mi) ) { i.m_patterns.clear(); } } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::ExprNode_StructLiteral::Ent& i) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::ExprNode_StructLiteral::Ent& i) const override { DEBUG("#[cfg] struct lit - " << mi); if( !check_cfg(sp, mi) ) { i.value.reset(); diff --git a/src/expand/cfg.hpp b/src/expand/cfg.hpp index edf17851..7c7785bb 100644 --- a/src/expand/cfg.hpp +++ b/src/expand/cfg.hpp @@ -6,4 +6,4 @@ extern void Cfg_SetFlag(::std::string name); extern void Cfg_SetValue(::std::string name, ::std::string val); extern void Cfg_SetValueCb(::std::string name, ::std::function cb); -extern bool check_cfg(Span sp, const ::AST::MetaItem& mi); +extern bool check_cfg(const Span& sp, const ::AST::Attribute& mi); diff --git a/src/expand/crate_tags.cpp b/src/expand/crate_tags.cpp index e99f1aa2..0e7d8447 100644 --- a/src/expand/crate_tags.cpp +++ b/src/expand/crate_tags.cpp @@ -14,7 +14,7 @@ class Decorator_CrateType: public: AttrStage stage() const override { return AttrStage::Pre; } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate) const override { if( crate.m_crate_type != AST::Crate::Type::Unknown ) { //ERROR(sp, E0000, "Multiple #![crate_type] attributes"); return ; @@ -41,7 +41,7 @@ class Decorator_CrateName: public: AttrStage stage() const override { return AttrStage::Pre; } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate) const override { if( crate.m_crate_name != "" ) { ERROR(sp, E0000, "Multiple #![crate_name] attributes"); } @@ -58,7 +58,7 @@ class Decorator_Allocator: public: AttrStage stage() const override { return AttrStage::Pre; } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate) const override { // TODO: Check for an existing allocator crate crate.m_lang_items.insert(::std::make_pair( "mrustc-allocator", AST::Path("",{}) )); } @@ -69,7 +69,7 @@ class Decorator_PanicRuntime: public: AttrStage stage() const override { return AttrStage::Pre; } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate) const override { // TODO: Check for an existing panic_runtime crate crate.m_lang_items.insert(::std::make_pair( "mrustc-panic_runtime", AST::Path("",{}) )); } @@ -80,7 +80,7 @@ class Decorator_NeedsPanicRuntime: public: AttrStage stage() const override { return AttrStage::Pre; } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate) const override { crate.m_lang_items.insert(::std::make_pair( "mrustc-needs_panic_runtime", AST::Path("",{}) )); } }; diff --git a/src/expand/derive.cpp b/src/expand/derive.cpp index 3b25831c..077c4b70 100644 --- a/src/expand/derive.cpp +++ b/src/expand/derive.cpp @@ -303,7 +303,7 @@ class Deriver_Debug: AST::GenericParams params = get_params_with_bounds(sp, p, debug_trait, mv$(types_to_bound)); - AST::Impl rv( AST::ImplDef( sp, AST::MetaItems(), mv$(params), make_spanned(sp, debug_trait), type.clone() ) ); + AST::Impl rv( AST::ImplDef( AST::AttributeList(), mv$(params), make_spanned(sp, debug_trait), type.clone() ) ); rv.add_function(false, false, "fmt", mv$(fcn)); return mv$(rv); } @@ -488,7 +488,7 @@ class Deriver_PartialEq: AST::GenericParams params = get_params_with_bounds(sp, p, trait_path, mv$(types_to_bound)); - AST::Impl rv( AST::ImplDef( sp, AST::MetaItems(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); + AST::Impl rv( AST::ImplDef( AST::AttributeList(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); rv.add_function(false, false, "eq", mv$(fcn)); return mv$(rv); } @@ -667,7 +667,7 @@ class Deriver_PartialOrd: AST::GenericParams params = get_params_with_bounds(sp, p, trait_path, mv$(types_to_bound)); - AST::Impl rv( AST::ImplDef( sp, AST::MetaItems(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); + AST::Impl rv( AST::ImplDef( AST::AttributeList(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); rv.add_function(false, false, "partial_cmp", mv$(fcn)); return mv$(rv); } @@ -910,7 +910,7 @@ class Deriver_Eq: AST::GenericParams params = get_params_with_bounds(sp, p, trait_path, mv$(types_to_bound)); - AST::Impl rv( AST::ImplDef( sp, AST::MetaItems(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); + AST::Impl rv( AST::ImplDef( AST::AttributeList(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); rv.add_function(false, false, "assert_receiver_is_total_eq", mv$(fcn)); return mv$(rv); } @@ -1062,7 +1062,7 @@ class Deriver_Ord: AST::GenericParams params = get_params_with_bounds(sp, p, trait_path, mv$(types_to_bound)); - AST::Impl rv( AST::ImplDef( sp, AST::MetaItems(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); + AST::Impl rv( AST::ImplDef( AST::AttributeList(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); rv.add_function(false, false, "cmp", mv$(fcn)); return mv$(rv); } @@ -1294,7 +1294,7 @@ class Deriver_Clone: AST::GenericParams params = get_params_with_bounds(sp, p, trait_path, mv$(types_to_bound)); - AST::Impl rv( AST::ImplDef( sp, AST::MetaItems(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); + AST::Impl rv( AST::ImplDef( AST::AttributeList(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); rv.add_function(false, false, "clone", mv$(fcn)); return mv$(rv); } @@ -1439,7 +1439,7 @@ class Deriver_Copy: AST::GenericParams params = get_params_with_bounds(sp, p, trait_path, mv$(types_to_bound)); - AST::Impl rv( AST::ImplDef( sp, AST::MetaItems(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); + AST::Impl rv( AST::ImplDef( AST::AttributeList(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); return mv$(rv); } @@ -1486,7 +1486,7 @@ class Deriver_Default: AST::GenericParams params = get_params_with_bounds(sp, p, trait_path, mv$(types_to_bound)); - AST::Impl rv( AST::ImplDef( sp, AST::MetaItems(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); + AST::Impl rv( AST::ImplDef( AST::AttributeList(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); rv.add_function(false, false, "default", mv$(fcn)); return mv$(rv); } @@ -1573,7 +1573,7 @@ class Deriver_Hash: AST::GenericParams params = get_params_with_bounds(sp, p, trait_path, mv$(types_to_bound)); - AST::Impl rv( AST::ImplDef( sp, AST::MetaItems(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); + AST::Impl rv( AST::ImplDef( AST::AttributeList(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); rv.add_function(false, false, "hash", mv$(fcn)); return mv$(rv); } @@ -1727,7 +1727,7 @@ class Deriver_RustcEncodable: AST::GenericParams params = get_params_with_bounds(sp, p, trait_path, mv$(types_to_bound)); - AST::Impl rv( AST::ImplDef( sp, AST::MetaItems(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); + AST::Impl rv( AST::ImplDef( AST::AttributeList(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); rv.add_function(false, false, "encode", mv$(fcn)); return mv$(rv); } @@ -1961,7 +1961,7 @@ class Deriver_RustcDecodable: AST::GenericParams params = get_params_with_bounds(sp, p, trait_path, mv$(types_to_bound)); - AST::Impl rv( AST::ImplDef( sp, AST::MetaItems(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); + AST::Impl rv( AST::ImplDef( AST::AttributeList(), mv$(params), make_spanned(sp, trait_path), type.clone() ) ); rv.add_function(false, false, "decode", mv$(fcn)); return mv$(rv); } @@ -2185,7 +2185,7 @@ static const Deriver* find_impl(const ::std::string& trait_name) } template -static void derive_item(const Span& sp, const AST::Crate& crate, AST::Module& mod, const AST::MetaItem& attr, const AST::Path& path, const T& item) +static void derive_item(const Span& sp, const AST::Crate& crate, AST::Module& mod, const AST::Attribute& attr, const AST::Path& path, const T& item) { if( !attr.has_sub_items() ) { //ERROR(sp, E0000, "#[derive()] requires a list of known traits to derive"); @@ -2257,7 +2257,7 @@ class Decorator_Derive: { public: AttrStage stage() const override { return AttrStage::Post; } - void handle(const Span& sp, const AST::MetaItem& attr, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override + void handle(const Span& sp, const AST::Attribute& attr, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override { TU_MATCH_DEF(::AST::Item, (i), (e), ( diff --git a/src/expand/lang_item.cpp b/src/expand/lang_item.cpp index 53606bea..789ad88e 100644 --- a/src/expand/lang_item.cpp +++ b/src/expand/lang_item.cpp @@ -125,7 +125,7 @@ class Decorator_LangItem: { public: AttrStage stage() const override { return AttrStage::Post; } - void handle(const Span& sp, const AST::MetaItem& attr, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override + void handle(const Span& sp, const AST::Attribute& attr, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override { TU_MATCH_DEF(::AST::Item, (i), (e), ( @@ -154,7 +154,7 @@ public: ) } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, const AST::Module& mod, AST::ImplDef& impl) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, const AST::Module& mod, AST::ImplDef& impl) const override { const ::std::string& name = mi.string(); if( name == "i8" ) {} @@ -193,7 +193,7 @@ class Decorator_Main: { public: AttrStage stage() const override { return AttrStage::Post; } - void handle(const Span& sp, const AST::MetaItem& attr, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override + void handle(const Span& sp, const AST::Attribute& attr, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override { if( i.is_None() ) { // Ignore. @@ -217,7 +217,7 @@ class Decorator_Start: { public: AttrStage stage() const override { return AttrStage::Post; } - void handle(const Span& sp, const AST::MetaItem& attr, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override + void handle(const Span& sp, const AST::Attribute& attr, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override { TU_IFLET(::AST::Item, i, Function, e, auto rv = crate.m_lang_items.insert(::std::make_pair( ::std::string("mrustc-start"), ::AST::Path(path) )); diff --git a/src/expand/macro_rules.cpp b/src/expand/macro_rules.cpp index 5b783949..5aa15a46 100644 --- a/src/expand/macro_rules.cpp +++ b/src/expand/macro_rules.cpp @@ -41,7 +41,7 @@ class CMacroUseHandler: { AttrStage stage() const override { return AttrStage::Post; } - void handle(const Span& sp, const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override + void handle(const Span& sp, const AST::Attribute& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override { TRACE_FUNCTION_F("path=" << path); @@ -120,7 +120,7 @@ class CMacroExportHandler: { AttrStage stage() const override { return AttrStage::Post; } - void handle(const Span& sp, const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override + void handle(const Span& sp, const AST::Attribute& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override { if( i.is_None() ) { } @@ -147,7 +147,7 @@ class CMacroReexportHandler: public ExpandDecorator { AttrStage stage() const override { return AttrStage::Post; } - void handle(const Span& sp, const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override + void handle(const Span& sp, const AST::Attribute& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override { if( !i.is_Crate() ) { ERROR(sp, E0000, "Use of #[macro_reexport] on non-crate - " << i.tag_str()); diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index 571d21e2..aa7655e3 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -21,7 +21,7 @@ MacroDef* g_macros_list = nullptr; ::std::map< ::std::string, ::std::unique_ptr > g_decorators; ::std::map< ::std::string, ::std::unique_ptr > g_macros; -void Expand_Attrs(const ::AST::MetaItems& attrs, AttrStage stage, ::std::function f); +void Expand_Attrs(const ::AST::AttributeList& attrs, AttrStage stage, ::std::function f); void Expand_Mod(::AST::Crate& crate, LList modstack, ::AST::Path modpath, ::AST::Module& mod, unsigned int first_item = 0); void Expand_Expr(::AST::Crate& crate, LList modstack, AST::Expr& node); void Expand_Expr(::AST::Crate& crate, LList modstack, ::std::shared_ptr& node); @@ -43,12 +43,12 @@ void Register_Synext_Macro_Static(MacroDef* def) { } -void ExpandDecorator::unexpected(const Span& sp, const AST::MetaItem& mi, const char* loc_str) const +void ExpandDecorator::unexpected(const Span& sp, const AST::Attribute& mi, const char* loc_str) const { WARNING(sp, W0000, "Unexpected attribute " << mi.name() << " on " << loc_str); } -void Expand_Attr(const Span& sp, const ::AST::MetaItem& a, AttrStage stage, ::std::function f) +void Expand_Attr(const Span& sp, const ::AST::Attribute& a, AttrStage stage, ::std::function f) { for( auto& d : g_decorators ) { if( d.first == a.name() ) { @@ -59,29 +59,30 @@ void Expand_Attr(const Span& sp, const ::AST::MetaItem& a, AttrStage stage, ::s } } } -void Expand_Attrs(/*const */::AST::MetaItems& attrs, AttrStage stage, ::std::function f) +void Expand_Attrs(/*const */::AST::AttributeList& attrs, AttrStage stage, ::std::function f) { for( auto& a : attrs.m_items ) { if( a.name() == "cfg_attr" ) { - if( check_cfg(attrs.m_span, a.items().at(0)) ) { + if( check_cfg(a.span(), a.items().at(0)) ) { + // Wait? Why move? auto inner_attr = mv$(a.items().at(1)); - Expand_Attr(attrs.m_span, inner_attr, stage, f); + Expand_Attr(inner_attr.span(), inner_attr, stage, f); a = mv$(inner_attr); } else { } } else { - Expand_Attr(attrs.m_span, a, stage, f); + Expand_Attr(a.span(), a, stage, f); } } } -void Expand_Attrs(::AST::MetaItems& attrs, AttrStage stage, ::AST::Crate& crate, const ::AST::Path& path, ::AST::Module& mod, ::AST::Item& item) +void Expand_Attrs(::AST::AttributeList& attrs, AttrStage stage, ::AST::Crate& crate, const ::AST::Path& path, ::AST::Module& mod, ::AST::Item& item) { Expand_Attrs(attrs, stage, [&](const auto& sp, const auto& d, const auto& a){ if(!item.is_None()) d.handle(sp, a, crate, path, mod, item); }); } -void Expand_Attrs(::AST::MetaItems& attrs, AttrStage stage, ::AST::Crate& crate, ::AST::Module& mod, ::AST::ImplDef& impl) +void Expand_Attrs(::AST::AttributeList& attrs, AttrStage stage, ::AST::Crate& crate, ::AST::Module& mod, ::AST::ImplDef& impl) { Expand_Attrs(attrs, stage, [&](const auto& sp, const auto& d, const auto& a){ d.handle(sp, a, crate, mod, impl); }); } @@ -1293,7 +1294,7 @@ void Expand(::AST::Crate& crate) crate.m_extern_crates.at("std").with_all_macros([&](const auto& name, const auto& mac) { crate.m_root_module.add_macro_import( name, mac ); }); - crate.m_root_module.add_ext_crate(false, "std", "std", ::AST::MetaItems {}); + crate.m_root_module.add_ext_crate(false, "std", "std", ::AST::AttributeList {}); break; case ::AST::Crate::LOAD_CORE: if( crate.m_prelude_path == AST::Path() ) @@ -1301,7 +1302,7 @@ void Expand(::AST::Crate& crate) crate.m_extern_crates.at("core").with_all_macros([&](const auto& name, const auto& mac) { crate.m_root_module.add_macro_import( name, mac ); }); - crate.m_root_module.add_ext_crate(false, "core", "core", ::AST::MetaItems {}); + crate.m_root_module.add_ext_crate(false, "core", "core", ::AST::AttributeList {}); break; case ::AST::Crate::LOAD_NONE: break; diff --git a/src/expand/proc_macro.cpp b/src/expand/proc_macro.cpp index d34a91a5..cdd13a42 100644 --- a/src/expand/proc_macro.cpp +++ b/src/expand/proc_macro.cpp @@ -35,7 +35,7 @@ class Decorator_ProcMacroDerive: { public: AttrStage stage() const override { return AttrStage::Post; } - void handle(const Span& sp, const AST::MetaItem& attr, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override + void handle(const Span& sp, const AST::Attribute& attr, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override { if( i.is_None() ) return; @@ -609,7 +609,7 @@ namespace { TODO(sp, "ExprNode_UniOp"); } - void visit_attrs(const ::AST::MetaItems& attrs) + void visit_attrs(const ::AST::AttributeList& attrs) { for(const auto& a : attrs.m_items) { @@ -622,7 +622,7 @@ namespace { } } } - void visit_meta_item(const ::AST::MetaItem& i) + void visit_meta_item(const ::AST::Attribute& i) { m_pmi.send_ident(i.name().c_str()); if( i.has_noarg() ) { diff --git a/src/expand/std_prelude.cpp b/src/expand/std_prelude.cpp index e12a441c..6b81b71e 100644 --- a/src/expand/std_prelude.cpp +++ b/src/expand/std_prelude.cpp @@ -8,8 +8,8 @@ class Decorator_NoStd: { public: AttrStage stage() const override { return AttrStage::Pre; } - - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate) const override { + + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate) const override { if( crate.m_load_std != AST::Crate::LOAD_STD && crate.m_load_std != AST::Crate::LOAD_CORE ) { ERROR(sp, E0000, "Invalid use of #![no_std] with itself or #![no_core]"); } @@ -22,7 +22,7 @@ class Decorator_NoCore: public: AttrStage stage() const override { return AttrStage::Pre; } - void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate) const override { + void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate) const override { if( crate.m_load_std != AST::Crate::LOAD_STD && crate.m_load_std != AST::Crate::LOAD_NONE ) { ERROR(sp, E0000, "Invalid use of #![no_core] with itself or #![no_std]"); } @@ -42,7 +42,7 @@ class Decorator_NoPrelude: public: AttrStage stage() const override { return AttrStage::Pre; } - void handle(const Span& sp, const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { + void handle(const Span& sp, const AST::Attribute& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { if( i.is_Module() ) { i.as_Module().m_insert_prelude = false; } @@ -58,7 +58,7 @@ class Decorator_PreludeImport: public: AttrStage stage() const override { return AttrStage::Post; } - void handle(const Span& sp, const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { + void handle(const Span& sp, const AST::Attribute& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { if( i.is_Use() ) { const auto& p = i.as_Use().path; // TODO: Ensure that this statement is a glob (has a name of "") diff --git a/src/expand/test.cpp b/src/expand/test.cpp index 12bfbb7d..9497c692 100644 --- a/src/expand/test.cpp +++ b/src/expand/test.cpp @@ -14,7 +14,7 @@ class CTestHandler: { AttrStage stage() const override { return AttrStage::Post; } - void handle(const Span& sp, const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { + void handle(const Span& sp, const AST::Attribute& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { if( ! i.is_Function() ) { ERROR(sp, E0000, "#[test] can only be put on functions - found on " << i.tag_str()); } @@ -42,7 +42,7 @@ class CTestHandler_SP: { AttrStage stage() const override { return AttrStage::Pre; } - void handle(const Span& sp, const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { + void handle(const Span& sp, const AST::Attribute& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { if( ! i.is_Function() ) { ERROR(sp, E0000, "#[should_panic] can only be put on functions - found on " << i.tag_str()); } @@ -75,7 +75,7 @@ class CTestHandler_Ignore: { AttrStage stage() const override { return AttrStage::Pre; } - void handle(const Span& sp, const AST::MetaItem& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { + void handle(const Span& sp, const AST::Attribute& mi, ::AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const override { if( ! i.is_Function() ) { ERROR(sp, E0000, "#[should_panic] can only be put on functions - found on " << i.tag_str()); } diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index 062b4ff8..1dbfdad2 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -18,7 +18,7 @@ #include ::HIR::Module LowerHIR_Module(const ::AST::Module& module, ::HIR::ItemPath path, ::std::vector< ::HIR::SimplePath> traits = {}); -::HIR::Function LowerHIR_Function(::HIR::ItemPath path, const ::AST::MetaItems& attrs, const ::AST::Function& f, const ::HIR::TypeRef& self_type); +::HIR::Function LowerHIR_Function(::HIR::ItemPath path, const ::AST::AttributeList& attrs, const ::AST::Function& f, const ::HIR::TypeRef& self_type); ::HIR::PathParams LowerHIR_PathParams(const Span& sp, const ::AST::PathParams& src_params, bool allow_assoc); ::HIR::TraitPath LowerHIR_TraitPath(const Span& sp, const ::AST::Path& path); @@ -836,7 +836,7 @@ namespace { } } -::HIR::Struct LowerHIR_Struct(::HIR::ItemPath path, const ::AST::Struct& ent, const ::AST::MetaItems& attrs) +::HIR::Struct LowerHIR_Struct(::HIR::ItemPath path, const ::AST::Struct& ent, const ::AST::AttributeList& attrs) { TRACE_FUNCTION_F(path); ::HIR::Struct::Data data; @@ -879,7 +879,7 @@ namespace { is_packed = true; } else { - TODO(attrs.m_span, "Handle struct repr '" << repr_str << "'"); + TODO(a.span(), "Handle struct repr '" << repr_str << "'"); } } @@ -900,7 +900,7 @@ namespace { }; } -::HIR::Enum LowerHIR_Enum(::HIR::ItemPath path, const ::AST::Enum& ent, const ::AST::MetaItems& attrs, ::std::function push_struct) +::HIR::Enum LowerHIR_Enum(::HIR::ItemPath path, const ::AST::Enum& ent, const ::AST::AttributeList& attrs, ::std::function push_struct) { // 1. Figure out what sort of enum this is (value or data) @@ -1050,7 +1050,7 @@ namespace { mv$(data) }; } -::HIR::Union LowerHIR_Union(::HIR::ItemPath path, const ::AST::Union& f, const ::AST::MetaItems& attrs) +::HIR::Union LowerHIR_Union(::HIR::ItemPath path, const ::AST::Union& f, const ::AST::AttributeList& attrs) { auto repr = ::HIR::Union::Repr::Rust; @@ -1064,7 +1064,7 @@ namespace { repr = ::HIR::Union::Repr::C; } else { - ERROR(attrs.m_span, E0000, "Unknown union repr '" << repr_str << "'"); + ERROR(attr_repr->span(), E0000, "Unknown union repr '" << repr_str << "'"); } } @@ -1186,7 +1186,7 @@ namespace { return rv; } -::HIR::Function LowerHIR_Function(::HIR::ItemPath p, const ::AST::MetaItems& attrs, const ::AST::Function& f, const ::HIR::TypeRef& self_type) +::HIR::Function LowerHIR_Function(::HIR::ItemPath p, const ::AST::AttributeList& attrs, const ::AST::Function& f, const ::HIR::TypeRef& self_type) { static Span sp; diff --git a/src/include/synext_decorator.hpp b/src/include/synext_decorator.hpp index af19491d..049a28b0 100644 --- a/src/include/synext_decorator.hpp +++ b/src/include/synext_decorator.hpp @@ -13,7 +13,7 @@ class TypeRef; namespace AST { class Crate; - class MetaItem; + class Attribute; class Path; struct StructItem; @@ -38,26 +38,26 @@ enum class AttrStage class ExpandDecorator { - void unexpected(const Span& sp, const AST::MetaItem& mi, const char* loc_str) const; + void unexpected(const Span& sp, const AST::Attribute& mi, const char* loc_str) const; public: virtual AttrStage stage() const = 0; - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate) const { unexpected(sp, mi, "crate"); } - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const { unexpected(sp, mi, "item"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate) const { unexpected(sp, mi, "crate"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item&i) const { unexpected(sp, mi, "item"); } // NOTE: To delete, set the type to `_` - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, const AST::Module& mod, AST::ImplDef& impl) const { unexpected(sp, mi, "impl"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, const AST::Module& mod, AST::ImplDef& impl) const { unexpected(sp, mi, "impl"); } // NOTE: To delete, clear the name - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::StructItem& si) const { unexpected(sp, mi, "struct item"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::StructItem& si) const { unexpected(sp, mi, "struct item"); } // NOTE: To delete, make the type invalid - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::TupleItem& si) const { unexpected(sp, mi, "tuple item"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::TupleItem& si) const { unexpected(sp, mi, "tuple item"); } // NOTE: To delete, clear the name - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::EnumVariant& ev) const { unexpected(sp, mi, "enum variant"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::EnumVariant& ev) const { unexpected(sp, mi, "enum variant"); } - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::std::unique_ptr& expr) const { unexpected(sp, mi, "expression"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::std::unique_ptr& expr) const { unexpected(sp, mi, "expression"); } // NOTE: To delete, clear the patterns vector - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::ExprNode_Match_Arm& expr) const { unexpected(sp, mi, "match arm"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::ExprNode_Match_Arm& expr) const { unexpected(sp, mi, "match arm"); } // NOTE: To delete, clear the value - virtual void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::ExprNode_StructLiteral::Ent& expr) const { unexpected(sp, mi, "struct literal ent"); } + virtual void handle(const Span& sp, const AST::Attribute& mi, AST::Crate& crate, ::AST::ExprNode_StructLiteral::Ent& expr) const { unexpected(sp, mi, "struct literal ent"); } }; struct DecoratorDef; diff --git a/src/macro_rules/eval.cpp b/src/macro_rules/eval.cpp index 9372fa6e..b2f5e409 100644 --- a/src/macro_rules/eval.cpp +++ b/src/macro_rules/eval.cpp @@ -712,7 +712,7 @@ InterpolatedFragment Macro_HandlePatternCap(TokenStream& lex, MacroPatEnt::Type case MacroPatEnt::PAT_ITEM: { assert( lex.parse_state().module ); const auto& cur_mod = *lex.parse_state().module; - return InterpolatedFragment( Parse_Mod_Item_S(lex, cur_mod.m_file_info, cur_mod.path(), AST::MetaItems{}) ); + return InterpolatedFragment( Parse_Mod_Item_S(lex, cur_mod.m_file_info, cur_mod.path(), AST::AttributeList{}) ); } break; case MacroPatEnt::PAT_IDENT: // TODO: Any reserved word is also valid as an ident diff --git a/src/parse/common.hpp b/src/parse/common.hpp index ede08984..1ca86128 100644 --- a/src/parse/common.hpp +++ b/src/parse/common.hpp @@ -41,16 +41,16 @@ extern AST::PathParams Parse_Path_GenericList(TokenStream& lex); extern ::std::vector< ::std::string> Parse_HRB(TokenStream& lex); -extern AST::MetaItems Parse_ItemAttrs(TokenStream& lex); -extern void Parse_ParentAttrs(TokenStream& lex, AST::MetaItems& out); -extern AST::MetaItem Parse_MetaItem(TokenStream& lex); +extern AST::AttributeList Parse_ItemAttrs(TokenStream& lex); +extern void Parse_ParentAttrs(TokenStream& lex, AST::AttributeList& out); +extern AST::Attribute Parse_MetaItem(TokenStream& lex); extern ::AST::MacroInvocation Parse_MacroInvocation(ProtoSpan ps, ::std::string name, TokenStream& lex); extern TypeRef Parse_Type(TokenStream& lex, bool allow_trait_list = true); extern AST::Pattern Parse_Pattern(TokenStream& lex, bool is_refutable); extern void Parse_Impl_Item(TokenStream& lex, AST::Impl& impl); -extern void Parse_Mod_Item(TokenStream& lex, AST::Module& mod, AST::MetaItems meta_items); -extern ::AST::Named<::AST::Item> Parse_Mod_Item_S(TokenStream& lex, const AST::Module::FileInfo& mod_fileinfo, const ::AST::Path& mod_path, AST::MetaItems meta_items); +extern void Parse_Mod_Item(TokenStream& lex, AST::Module& mod, AST::AttributeList meta_items); +extern ::AST::Named<::AST::Item> Parse_Mod_Item_S(TokenStream& lex, const AST::Module::FileInfo& mod_fileinfo, const ::AST::Path& mod_path, AST::AttributeList meta_items); extern void Parse_ModRoot_Items(TokenStream& lex, AST::Module& mod); diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index 2ac13728..22bf9afc 100644 --- a/src/parse/expr.cpp +++ b/src/parse/expr.cpp @@ -66,7 +66,7 @@ ExprNodeP Parse_ExprBlockNode(TokenStream& lex, bool is_unsafe/*=false*/) DEBUG("tok = " << tok); // NOTE: Doc comments can appear within a function and apply to the function - ::AST::MetaItems node_attrs; + ::AST::AttributeList node_attrs; Parse_ParentAttrs(lex, node_attrs); (void)node_attrs; // TODO: Use these attributes if( LOOK_AHEAD(lex) == TOK_BRACE_CLOSE ) @@ -98,7 +98,7 @@ ExprNodeP Parse_ExprBlockLine_WithItems(TokenStream& lex, ::std::shared_ptr(m_ptr); break; case InterpolatedFragment::META: - delete reinterpret_cast(m_ptr); + delete reinterpret_cast(m_ptr); break; case InterpolatedFragment::ITEM: delete reinterpret_cast*>(m_ptr); @@ -47,9 +47,9 @@ InterpolatedFragment::InterpolatedFragment(InterpolatedFragment::Type type, AST: m_ptr( ptr ) { } -InterpolatedFragment::InterpolatedFragment(AST::MetaItem v): +InterpolatedFragment::InterpolatedFragment(AST::Attribute v): m_type( InterpolatedFragment::META ), - m_ptr( new AST::MetaItem(mv$(v)) ) + m_ptr( new AST::Attribute(mv$(v)) ) { } InterpolatedFragment::InterpolatedFragment(::AST::Named<::AST::Item> v): @@ -106,7 +106,7 @@ InterpolatedFragment::InterpolatedFragment(TypeRef v): break; case InterpolatedFragment::META: - os << "meta[" << *reinterpret_cast(x.m_ptr) << "]"; + os << "meta[" << *reinterpret_cast(x.m_ptr) << "]"; break; case InterpolatedFragment::ITEM: { const auto& named_item = *reinterpret_cast*>(x.m_ptr); diff --git a/src/parse/interpolated_fragment.hpp b/src/parse/interpolated_fragment.hpp index 1b18845a..857e77aa 100644 --- a/src/parse/interpolated_fragment.hpp +++ b/src/parse/interpolated_fragment.hpp @@ -10,7 +10,7 @@ namespace AST { class Pattern; class Path; class ExprNode; - class MetaItem; + class Attribute; template struct Named; class Item; }; @@ -44,7 +44,7 @@ public: InterpolatedFragment(::AST::Pattern); InterpolatedFragment(::AST::Path); InterpolatedFragment(::TypeRef); - InterpolatedFragment(::AST::MetaItem ); + InterpolatedFragment(::AST::Attribute ); InterpolatedFragment(::AST::Named ); ~InterpolatedFragment(); InterpolatedFragment(Type , ::AST::ExprNode*); diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 8fa72fb3..a57d6844 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -41,10 +41,10 @@ Spanned get_spanned(TokenStream& lex, ::std::function f) { return input; } -AST::MetaItems Parse_ItemAttrs(TokenStream& lex); -void Parse_ParentAttrs(TokenStream& lex, AST::MetaItems& out); -AST::MetaItem Parse_MetaItem(TokenStream& lex); -void Parse_ModRoot(TokenStream& lex, AST::Module& mod, AST::MetaItems& mod_attrs); +AST::AttributeList Parse_ItemAttrs(TokenStream& lex); +void Parse_ParentAttrs(TokenStream& lex, AST::AttributeList& out); +AST::Attribute Parse_MetaItem(TokenStream& lex); +void Parse_ModRoot(TokenStream& lex, AST::Module& mod, AST::AttributeList& mod_attrs); bool Parse_MacroInvocation_Opt(TokenStream& lex, AST::MacroInvocation& out_inv); //::AST::Path Parse_Publicity(TokenStream& lex) @@ -127,7 +127,7 @@ bool Parse_Publicity(TokenStream& lex, bool allow_restricted=true) ::std::vector< ::std::string> lifetimes; GET_CHECK_TOK(tok, lex, TOK_LT); do { - ::AST::MetaItems attrs = Parse_ItemAttrs(lex); + auto attrs = Parse_ItemAttrs(lex); (void)attrs; // TODO: Attributes on generic params switch(GET_TOK(tok, lex)) @@ -198,7 +198,7 @@ AST::GenericParams Parse_GenericParams(TokenStream& lex) } PUTBACK(tok, lex); - ::AST::MetaItems attrs = Parse_ItemAttrs(lex); + auto attrs = Parse_ItemAttrs(lex); (void)attrs; // TODO: Attributes on generic params GET_TOK(tok, lex); @@ -523,7 +523,7 @@ AST::TypeAlias Parse_TypeAlias(TokenStream& lex) return AST::TypeAlias( ::std::move(params), ::std::move(type) ); } -AST::Struct Parse_Struct(TokenStream& lex, const AST::MetaItems& meta_items) +AST::Struct Parse_Struct(TokenStream& lex, const AST::AttributeList& meta_items) { TRACE_FUNCTION; @@ -547,7 +547,7 @@ AST::Struct Parse_Struct(TokenStream& lex, const AST::MetaItems& meta_items) ::std::vector refs; while(lex.lookahead(0) != TOK_PAREN_CLOSE) { - AST::MetaItems item_attrs = Parse_ItemAttrs(lex); + auto item_attrs = Parse_ItemAttrs(lex); SET_ATTRS(lex, item_attrs); bool is_pub = Parse_Publicity(lex, /*allow_restricted=*/false); // HACK: Disable `pub(restricted)` syntax in tuple structs, due to ambiguity @@ -582,7 +582,7 @@ AST::Struct Parse_Struct(TokenStream& lex, const AST::MetaItems& meta_items) { PUTBACK(tok, lex); - AST::MetaItems item_attrs = Parse_ItemAttrs(lex); + auto item_attrs = Parse_ItemAttrs(lex); SET_ATTRS(lex, item_attrs); bool is_pub = Parse_Publicity(lex); @@ -607,7 +607,7 @@ AST::Struct Parse_Struct(TokenStream& lex, const AST::MetaItems& meta_items) } } -AST::Trait Parse_TraitDef(TokenStream& lex, const AST::MetaItems& meta_items) +AST::Trait Parse_TraitDef(TokenStream& lex, const AST::AttributeList& meta_items) { TRACE_FUNCTION; @@ -662,7 +662,7 @@ AST::Trait Parse_TraitDef(TokenStream& lex, const AST::MetaItems& meta_items) { PUTBACK(tok, lex); - AST::MetaItems item_attrs = Parse_ItemAttrs(lex); + auto item_attrs = Parse_ItemAttrs(lex); SET_ATTRS(lex, item_attrs); auto ps = lex.start_span(); @@ -795,7 +795,7 @@ AST::Trait Parse_TraitDef(TokenStream& lex, const AST::MetaItems& meta_items) return trait; } -AST::Enum Parse_EnumDef(TokenStream& lex, const AST::MetaItems& meta_items) +AST::Enum Parse_EnumDef(TokenStream& lex, const AST::AttributeList& meta_items) { TRACE_FUNCTION; @@ -823,7 +823,7 @@ AST::Enum Parse_EnumDef(TokenStream& lex, const AST::MetaItems& meta_items) auto sp = lex.start_span(); PUTBACK(tok, lex); - AST::MetaItems item_attrs = Parse_ItemAttrs(lex); + auto item_attrs = Parse_ItemAttrs(lex); SET_ATTRS(lex, item_attrs); GET_CHECK_TOK(tok, lex, TOK_IDENT); @@ -841,7 +841,7 @@ AST::Enum Parse_EnumDef(TokenStream& lex, const AST::MetaItems& meta_items) break; } - AST::MetaItems field_attrs = Parse_ItemAttrs(lex); + auto field_attrs = Parse_ItemAttrs(lex); (void)field_attrs; // TODO^ types.push_back( Parse_Type(lex) ); @@ -862,7 +862,7 @@ AST::Enum Parse_EnumDef(TokenStream& lex, const AST::MetaItems& meta_items) break; } - AST::MetaItems field_attrs = Parse_ItemAttrs(lex); + auto field_attrs = Parse_ItemAttrs(lex); GET_CHECK_TOK(tok, lex, TOK_IDENT); auto name = mv$(tok.str()); @@ -897,7 +897,7 @@ AST::Enum Parse_EnumDef(TokenStream& lex, const AST::MetaItems& meta_items) return AST::Enum( mv$(params), mv$(variants) ); } -::AST::Union Parse_Union(TokenStream& lex, AST::MetaItems& meta_items) +::AST::Union Parse_Union(TokenStream& lex, AST::AttributeList& meta_items) { Token tok; @@ -924,7 +924,7 @@ AST::Enum Parse_EnumDef(TokenStream& lex, const AST::MetaItems& meta_items) break ; } - AST::MetaItems item_attrs = Parse_ItemAttrs(lex); + auto item_attrs = Parse_ItemAttrs(lex); SET_ATTRS(lex, item_attrs); bool is_pub = Parse_Publicity(lex); @@ -943,9 +943,9 @@ AST::Enum Parse_EnumDef(TokenStream& lex, const AST::MetaItems& meta_items) return ::AST::Union( mv$(params), mv$(variants) ); } -AST::MetaItems Parse_ItemAttrs(TokenStream& lex) +AST::AttributeList Parse_ItemAttrs(TokenStream& lex) { - AST::MetaItems rv; + AST::AttributeList rv; Token tok; while( lex.lookahead(0) == TOK_HASH ) { @@ -956,7 +956,7 @@ AST::MetaItems Parse_ItemAttrs(TokenStream& lex) } return rv; } -void Parse_ParentAttrs(TokenStream& lex, AST::MetaItems& out) +void Parse_ParentAttrs(TokenStream& lex, AST::AttributeList& out) { Token tok; while( lex.lookahead(0) == TOK_HASH && lex.lookahead(1) == TOK_EXCLAM ) @@ -969,7 +969,7 @@ void Parse_ParentAttrs(TokenStream& lex, AST::MetaItems& out) } } /// Parse a meta-item declaration (either #![ or #[) -AST::MetaItem Parse_MetaItem(TokenStream& lex) +AST::Attribute Parse_MetaItem(TokenStream& lex) { TRACE_FUNCTION; Token tok; @@ -979,6 +979,7 @@ AST::MetaItem Parse_MetaItem(TokenStream& lex) return mv$(tok.frag_meta()); } + auto ps = lex.start_span(); CHECK_TOK(tok, TOK_IDENT); ::std::string name = mv$(tok.str()); switch(GET_TOK(tok, lex)) @@ -987,24 +988,26 @@ AST::MetaItem Parse_MetaItem(TokenStream& lex) switch(GET_TOK(tok, lex)) { case TOK_STRING: - return AST::MetaItem(name, tok.str()); + return AST::Attribute(lex.end_span(ps), name, tok.str()); case TOK_INTERPOLATED_EXPR: { auto n = tok.take_frag_node(); if( auto* v = dynamic_cast<::AST::ExprNode_String*>(&*n) ) { - return AST::MetaItem(name, mv$(v->m_value)); + return AST::Attribute(lex.end_span(ps), name, mv$(v->m_value)); } else { + // - Force an error. CHECK_TOK(tok, TOK_STRING); } break; } default: + // - Force an error. CHECK_TOK(tok, TOK_STRING); } throw ""; case TOK_PAREN_OPEN: { - ::std::vector items; + ::std::vector items; do { if(LOOK_AHEAD(lex) == TOK_PAREN_CLOSE) { GET_TOK(tok, lex); @@ -1013,14 +1016,14 @@ AST::MetaItem Parse_MetaItem(TokenStream& lex) items.push_back(Parse_MetaItem(lex)); } while(GET_TOK(tok, lex) == TOK_COMMA); CHECK_TOK(tok, TOK_PAREN_CLOSE); - return AST::MetaItem(name, mv$(items)); } + return AST::Attribute(lex.end_span(ps), name, mv$(items)); } default: PUTBACK(tok, lex); - return AST::MetaItem(name); + return AST::Attribute(lex.end_span(ps), name); } } -::AST::Item Parse_Impl(TokenStream& lex, AST::MetaItems attrs, bool is_unsafe=false) +::AST::Item Parse_Impl(TokenStream& lex, AST::AttributeList attrs, bool is_unsafe=false) { TRACE_FUNCTION; Token tok; @@ -1057,7 +1060,7 @@ AST::MetaItem Parse_MetaItem(TokenStream& lex) // negative impls can't have any content GET_CHECK_TOK(tok, lex, TOK_BRACE_CLOSE); - return ::AST::Item::make_NegImpl( AST::ImplDef(lex.end_span(ps), mv$(attrs), mv$(params), mv$(trait_path), mv$(impl_type) ) ); + return ::AST::Item::make_NegImpl(AST::ImplDef( mv$(attrs), mv$(params), mv$(trait_path), mv$(impl_type) )); } // - Don't care which at this stage @@ -1102,7 +1105,7 @@ AST::MetaItem Parse_MetaItem(TokenStream& lex) Parse_ParentAttrs(lex, attrs); - AST::Impl impl( AST::ImplDef( lex.end_span(ps), mv$(attrs), mv$(params), mv$(trait_path), mv$(impl_type) ) ); + auto impl = AST::Impl(AST::ImplDef( mv$(attrs), mv$(params), mv$(trait_path), mv$(impl_type) )); // A sequence of method implementations while( lex.lookahead(0) != TOK_BRACE_CLOSE ) @@ -1119,7 +1122,7 @@ void Parse_Impl_Item(TokenStream& lex, AST::Impl& impl) TRACE_FUNCTION; Token tok; - AST::MetaItems item_attrs = Parse_ItemAttrs(lex); + auto item_attrs = Parse_ItemAttrs(lex); SET_ATTRS(lex, item_attrs); { @@ -1216,7 +1219,7 @@ void Parse_Impl_Item(TokenStream& lex, AST::Impl& impl) impl.items().back().data->attrs = mv$(item_attrs); // Empty for functions } -AST::ExternBlock Parse_ExternBlock(TokenStream& lex, ::std::string abi, ::AST::MetaItems& block_attrs) +AST::ExternBlock Parse_ExternBlock(TokenStream& lex, ::std::string abi, ::AST::AttributeList& block_attrs) { TRACE_FUNCTION; Token tok; @@ -1228,7 +1231,7 @@ AST::ExternBlock Parse_ExternBlock(TokenStream& lex, ::std::string abi, ::AST::M while( GET_TOK(tok, lex) != TOK_BRACE_CLOSE ) { PUTBACK(tok, lex); - AST::MetaItems meta_items = Parse_ItemAttrs(lex); + auto meta_items = Parse_ItemAttrs(lex); SET_ATTRS(lex, meta_items); auto ps = lex.start_span(); @@ -1452,7 +1455,7 @@ bool Parse_MacroInvocation_Opt(TokenStream& lex, AST::MacroInvocation& out_inv) return true; } -::AST::Named<::AST::Item> Parse_Mod_Item_S(TokenStream& lex, const AST::Module::FileInfo& mod_fileinfo, const ::AST::Path& mod_path, AST::MetaItems meta_items) +::AST::Named<::AST::Item> Parse_Mod_Item_S(TokenStream& lex, const AST::Module::FileInfo& mod_fileinfo, const ::AST::Path& mod_path, AST::AttributeList meta_items) { TRACE_FUNCTION_F("mod_path="< { "", Parse_Impl(lex, mv$(meta_items), true), false }; + case TOK_RWORD_IMPL: { + auto impl = Parse_Impl(lex, mv$(meta_items), true); + if( impl.is_Impl() ) { + impl.as_Impl().def().set_is_unsafe(); + } + else if( impl.is_NegImpl() ) { + impl.as_NegImpl().set_is_unsafe(); + } + else { + BUG(lex.point_span(), "Parse_Impl returned a variant other than Impl or NegImpl"); + } + return ::AST::Named< ::AST::Item> { "", mv$(impl), false }; + } default: throw ParseError::Unexpected(lex, tok, {TOK_RWORD_FN, TOK_RWORD_TRAIT, TOK_RWORD_IMPL}); } @@ -1772,10 +1785,10 @@ bool Parse_MacroInvocation_Opt(TokenStream& lex, AST::MacroInvocation& out_inv) // Check #[cfg] and don't load if it fails struct H { - static bool check_item_cfg(const ::AST::MetaItems& attrs) + static 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; } } @@ -1853,7 +1866,7 @@ bool Parse_MacroInvocation_Opt(TokenStream& lex, AST::MacroInvocation& out_inv) return ::AST::Named< ::AST::Item> { mv$(item_name), mv$(item_data), is_public }; } -void Parse_Mod_Item(TokenStream& lex, AST::Module& mod, AST::MetaItems meta_items) +void Parse_Mod_Item(TokenStream& lex, AST::Module& mod, AST::AttributeList meta_items) { SET_MODULE(lex, mod); lex.parse_state().parent_attrs = &meta_items; @@ -1899,14 +1912,14 @@ void Parse_ModRoot_Items(TokenStream& lex, AST::Module& mod) } // Attributes on the following item - AST::MetaItems meta_items = Parse_ItemAttrs(lex); + auto meta_items = Parse_ItemAttrs(lex); DEBUG("meta_items = " << meta_items); Parse_Mod_Item(lex, mod, mv$(meta_items)); } } -void Parse_ModRoot(TokenStream& lex, AST::Module& mod, AST::MetaItems& mod_attrs) +void Parse_ModRoot(TokenStream& lex, AST::Module& mod, AST::AttributeList& mod_attrs) { TRACE_FUNCTION; diff --git a/src/parse/token.cpp b/src/parse/token.cpp index 768a96bc..6f3bafd9 100644 --- a/src/parse/token.cpp +++ b/src/parse/token.cpp @@ -33,7 +33,7 @@ Token::~Token() delete reinterpret_cast(m_data.as_Fragment()); break; case TOK_INTERPOLATED_META: - delete reinterpret_cast(m_data.as_Fragment()); + delete reinterpret_cast(m_data.as_Fragment()); break; default: break; @@ -92,7 +92,7 @@ Token::Token(const InterpolatedFragment& frag) break; case InterpolatedFragment::META: m_type = TOK_INTERPOLATED_META; - m_data = new AST::MetaItem( reinterpret_cast(frag.m_ptr)->clone() ); + m_data = new AST::Attribute( reinterpret_cast(frag.m_ptr)->clone() ); break; case InterpolatedFragment::ITEM: { m_type = TOK_INTERPOLATED_ITEM; @@ -197,11 +197,11 @@ Token Token::clone() const rv.m_data = reinterpret_cast(e)->clone().release(); break; case TOK_INTERPOLATED_META: - rv.m_data = new AST::MetaItem( reinterpret_cast(e)->clone() ); + rv.m_data = new AST::Attribute( reinterpret_cast(e)->clone() ); break; case TOK_INTERPOLATED_ITEM: TODO(m_pos, "clone interpolated item"); - //rv.m_data = new AST::Named( AST::Item( reinterpret_cast(e)->clone() ) ); + //rv.m_data = new AST::Named( AST::Item( reinterpret_cast(e)->clone() ) ); break; default: BUG(m_pos, "Fragment with invalid token type (" << *this << ")"); @@ -553,7 +553,7 @@ SERIALISE_TYPE(Token::, "Token", { os << ":" << *reinterpret_cast(tok.m_data.as_Fragment()); break; case TOK_INTERPOLATED_META: - os << ":" << *reinterpret_cast(tok.m_data.as_Fragment()); + os << ":" << *reinterpret_cast(tok.m_data.as_Fragment()); break; case TOK_INTERPOLATED_ITEM: { const auto& named_item = *reinterpret_cast*>(tok.m_data.as_Fragment()); diff --git a/src/parse/token.hpp b/src/parse/token.hpp index 0ef8f009..71b543fc 100644 --- a/src/parse/token.hpp +++ b/src/parse/token.hpp @@ -47,7 +47,7 @@ namespace AST { class Pattern; class Path; class ExprNode; - class MetaItem; + class Attribute; class Item; template @@ -115,7 +115,7 @@ public: TypeRef& frag_type() { assert(m_type == TOK_INTERPOLATED_TYPE); return *reinterpret_cast( m_data.as_Fragment() ); } AST::Path& frag_path() { assert(m_type == TOK_INTERPOLATED_PATH); return *reinterpret_cast( m_data.as_Fragment() ); } AST::Pattern& frag_pattern() { assert(m_type == TOK_INTERPOLATED_PATTERN); return *reinterpret_cast( m_data.as_Fragment() ); } - AST::MetaItem& frag_meta() { assert(m_type == TOK_INTERPOLATED_META); return *reinterpret_cast( m_data.as_Fragment() ); } + AST::Attribute& frag_meta() { assert(m_type == TOK_INTERPOLATED_META); return *reinterpret_cast( m_data.as_Fragment() ); } ::std::unique_ptr take_frag_node(); ::AST::Named take_frag_item(); diff --git a/src/parse/tokenstream.hpp b/src/parse/tokenstream.hpp index 766e52bc..a9d325c2 100644 --- a/src/parse/tokenstream.hpp +++ b/src/parse/tokenstream.hpp @@ -16,7 +16,7 @@ namespace AST { class Module; - class MetaItems; + class AttributeList; } /// State the parser needs to pass down via a second channel. @@ -28,7 +28,7 @@ struct ParseState bool no_expand_macros = false; ::AST::Module* module = nullptr; - ::AST::MetaItems* parent_attrs = nullptr; + ::AST::AttributeList* parent_attrs = nullptr; ::AST::Module& get_current_mod() { assert(this->module); diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index 5e9cb51c..2c17592c 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -2069,7 +2069,7 @@ void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod ) Resolve_Absolute_Path(item_context, def.trait().sp, Context::LookupMode::Type, def.trait().ent); if( e.items().size() != 0 ) { - ERROR(def.span(), E0000, "impl Trait for .. with methods"); + ERROR(i.data.span, E0000, "impl Trait for .. with methods"); } item_context.pop(def.params()); @@ -2102,7 +2102,7 @@ void Resolve_Absolute_Mod( Context item_context, ::AST::Module& mod ) Resolve_Absolute_Type(item_context, impl_def.type()); if( !impl_def.trait().ent.is_valid() ) - BUG(impl_def.span(), "Encountered negative impl with no trait"); + BUG(i.data.span, "Encountered negative impl with no trait"); Resolve_Absolute_Path(item_context, impl_def.trait().sp, Context::LookupMode::Type, impl_def.trait().ent); // No items -- cgit v1.2.3 From de9ecd7a2d70359b34e77ded57e5aa9284345ac5 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 20 May 2018 22:01:59 +0800 Subject: AST - Refactor lifetime/HRB handling --- src/ast/ast.cpp | 30 +++++++++++++----- src/ast/attrs.hpp | 21 +++++++++++-- src/ast/dump.cpp | 8 ++--- src/ast/generics.hpp | 74 ++++++++++++++++++++++++++++++++++----------- src/ast/path.hpp | 5 +-- src/ast/types.cpp | 33 ++++++++++++++++++-- src/ast/types.hpp | 77 +++++++++++++++++++++++++++++++++++++++++++---- src/expand/derive.cpp | 25 +++++++-------- src/expand/mod.cpp | 8 +++-- src/expand/proc_macro.cpp | 48 ++++++++++++++--------------- src/hir/from_ast.cpp | 57 +++++++++++++++++++++++------------ src/parse/common.hpp | 2 +- src/parse/paths.cpp | 4 +-- src/parse/root.cpp | 52 ++++++++++++++++++-------------- src/parse/tokenstream.cpp | 4 +++ src/parse/types.cpp | 77 ++++++++++++++++++++++++++++++----------------- src/resolve/absolute.cpp | 16 +++++----- 17 files changed, 376 insertions(+), 165 deletions(-) (limited to 'src/resolve/absolute.cpp') diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 641bd633..1a19a10f 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -379,10 +379,31 @@ Item Item::clone() const //os << ")"; return os; } +::std::ostream& operator<<(::std::ostream& os, const LifetimeParam& p) +{ + os << "'" << p.m_name; + return os; +} + +::std::ostream& operator<<(::std::ostream& os, const HigherRankedBounds& x) +{ + if( x.m_lifetimes.empty() ) { + return os; + } + os << "for<"; + for(const auto& l : x.m_lifetimes) + os << "'" << l << ","; + os << "> "; + return os; +} + ::std::ostream& operator<<(::std::ostream& os, const GenericBound& x) { TU_MATCH(GenericBound, (x), (ent), + (None, + os << "/*-*/"; + ), (Lifetime, os << "'" << ent.test << ": '" << ent.bound; ), @@ -390,14 +411,7 @@ Item Item::clone() const os << ent.type << ": '" << ent.bound; ), (IsTrait, - if( ! ent.hrls.empty() ) - { - os << "for<"; - for(const auto& l : ent.hrls) - os << "'" << l; - os << ">"; - } - os << ent.type << ": " << ent.trait; + os << ent.outer_hrbs << ent.type << ": " << ent.inner_hrbs << ent.trait; ), (MaybeTrait, os << ent.type << ": ?" << ent.trait; diff --git a/src/ast/attrs.hpp b/src/ast/attrs.hpp index 4afbdbc1..1926e96a 100644 --- a/src/ast/attrs.hpp +++ b/src/ast/attrs.hpp @@ -30,8 +30,8 @@ public: // Move present AttributeList(AttributeList&&) = default; AttributeList& operator=(AttributeList&&) = default; - // No copy - AttributeList(const AttributeList&) = delete; + // No copy assign, but explicit copy + explicit AttributeList(const AttributeList&) = default; AttributeList& operator=(const AttributeList&) = delete; // Explicit clone AttributeList clone() const; @@ -97,7 +97,22 @@ public: { } - Attribute(const Attribute& ) = delete; + explicit Attribute(const Attribute& x): + m_span(x.m_span), + m_name(x.m_name), + m_is_used(x.m_is_used) + { + TU_MATCHA( (x.m_data), (e), + (None, + ), + (String, + m_data = AttributeData::make_String({ e.val }); + ), + (List, + m_data = AttributeData::make_List({ ::std::vector(e.sub_items) }); + ) + ) + } Attribute& operator=(const Attribute&& ) = delete; Attribute(Attribute&& ) = default; Attribute& operator=(Attribute&& ) = default; diff --git a/src/ast/dump.cpp b/src/ast/dump.cpp index b6336a02..73b332c0 100644 --- a/src/ast/dump.cpp +++ b/src/ast/dump.cpp @@ -829,6 +829,9 @@ void RustPrinter::print_bounds(const AST::GenericParams& params) m_os << indent(); TU_MATCH(AST::GenericBound, (b), (ent), + (None, + m_os << "/*-*/"; + ), (Lifetime, m_os << "'" << ent.test << ": '" << ent.bound; ), @@ -836,10 +839,7 @@ void RustPrinter::print_bounds(const AST::GenericParams& params) m_os << ent.type << ": '" << ent.bound; ), (IsTrait, - if( ent.hrls.size() > 0 ) { - m_os << "for<'" << ::join(", '", ent.hrls) << "> "; - } - m_os << ent.type << ": " << ent.trait; + m_os << ent.outer_hrbs << ent.type << ": " << ent.inner_hrbs << ent.trait; ), (MaybeTrait, m_os << ent.type << ": ?" << ent.trait; diff --git a/src/ast/generics.hpp b/src/ast/generics.hpp index bc14202e..c222044c 100644 --- a/src/ast/generics.hpp +++ b/src/ast/generics.hpp @@ -14,49 +14,86 @@ namespace AST { class TypeParam { + ::AST::AttributeList m_attrs; + Span m_span; + // TODO: use an Ident? ::std::string m_name; ::TypeRef m_default; public: TypeParam(TypeParam&& x) = default; TypeParam& operator=(TypeParam&& x) = default; - TypeParam(const TypeParam& x): - m_name(x.m_name), - m_default(x.m_default.clone()) - {} - //TypeParam(): m_name("") {} - TypeParam(::std::string name): + explicit TypeParam(const TypeParam& x): + m_attrs( x.m_attrs ), + m_span( x.m_span ), + m_name( x.m_name ), + m_default( x.m_default.clone() ) + { + } + + TypeParam(Span sp, ::AST::AttributeList attrs, ::std::string name): + m_attrs( ::std::move(attrs) ), + m_span( ::std::move(sp) ), m_name( ::std::move(name) ), - m_default( Span() ) + m_default(m_span) {} + void setDefault(TypeRef type) { assert(m_default.is_wildcard()); m_default = ::std::move(type); } - const ::std::string& name() const { return m_name; } + const ::AST::AttributeList& attrs() const { return m_attrs; } + const Span& span() const { return m_span; } + const ::std::string& name() const { return m_name; } const TypeRef& get_default() const { return m_default; } TypeRef& get_default() { return m_default; } friend ::std::ostream& operator<<(::std::ostream& os, const TypeParam& tp); }; +class LifetimeParam +{ + ::AST::AttributeList m_attrs; + Span m_span; + Ident m_name; +public: + LifetimeParam(Span sp, ::AST::AttributeList attrs, Ident name): + m_attrs( ::std::move(attrs) ), + m_span( ::std::move(sp) ), + m_name( ::std::move(name) ) + { + } + LifetimeParam(LifetimeParam&&) = default; + LifetimeParam& operator=(LifetimeParam&&) = default; + explicit LifetimeParam(const LifetimeParam&) = default; + + const ::AST::AttributeList& attrs() const { return m_attrs; } + const Span& span() const { return m_span; } + const Ident& name() const { return m_name; } + + friend ::std::ostream& operator<<(::std::ostream& os, const LifetimeParam& p); +}; + +// HigherRankedBounds is defined in `types.hpp` -TAGGED_UNION_EX( GenericBound, (), Lifetime, +TAGGED_UNION_EX( GenericBound, (), None, ( + (None, struct{}), // Lifetime bound: 'test must be valid for 'bound (Lifetime, struct { - ::std::string test; - ::std::string bound; + LifetimeRef test; + LifetimeRef bound; }), // Type lifetime bound (TypeLifetime, struct { TypeRef type; - ::std::string bound; + LifetimeRef bound; }), // Standard trait bound: "Type: [for<'a>] Trait" (IsTrait, struct { + HigherRankedBounds outer_hrbs; TypeRef type; - ::std::vector< ::std::string> hrls; // Higher-ranked lifetimes + HigherRankedBounds inner_hrbs; AST::Path trait; }), // Removed trait bound: "Type: ?Trait" @@ -84,9 +121,10 @@ TAGGED_UNION_EX( GenericBound, (), Lifetime, GenericBound clone() const { TU_MATCH(GenericBound, ( (*this) ), (ent), + (None, return make_None({}); ), (Lifetime, return make_Lifetime({ent.test, ent.bound}); ), (TypeLifetime, return make_TypeLifetime({ent.type.clone(), ent.bound}); ), - (IsTrait, return make_IsTrait({ent.type.clone(), ent.hrls, ent.trait}); ), + (IsTrait, return make_IsTrait({ent.outer_hrbs, ent.type.clone(), ent.inner_hrbs, ent.trait}); ), (MaybeTrait, return make_MaybeTrait({ent.type.clone(), ent.trait}); ), (NotTrait, return make_NotTrait({ent.type.clone(), ent.trait}); ), (Equality, return make_Equality({ent.type.clone(), ent.replacement.clone()}); ) @@ -101,7 +139,7 @@ TAGGED_UNION_EX( GenericBound, (), Lifetime, class GenericParams { ::std::vector m_type_params; - ::std::vector< ::std::string > m_lifetime_params; + ::std::vector m_lifetime_params; ::std::vector m_bounds; public: GenericParams() {} @@ -112,7 +150,7 @@ public: GenericParams clone() const { GenericParams rv; rv.m_type_params = ::std::vector( m_type_params ); // Copy-constructable - rv.m_lifetime_params = m_lifetime_params; + rv.m_lifetime_params = ::std::vector(m_lifetime_params); rv.m_bounds.reserve( m_bounds.size() ); for(auto& e: m_bounds) rv.m_bounds.push_back( e.clone() ); @@ -121,12 +159,12 @@ public: const ::std::vector& ty_params() const { return m_type_params; } ::std::vector& ty_params() { return m_type_params; } - const ::std::vector< ::std::string>& lft_params() const { return m_lifetime_params; } + const ::std::vector& lft_params() const { return m_lifetime_params; } const ::std::vector& bounds() const { return m_bounds; } ::std::vector& bounds() { return m_bounds; } void add_ty_param(TypeParam param) { m_type_params.push_back( ::std::move(param) ); } - void add_lft_param(::std::string name) { m_lifetime_params.push_back( ::std::move(name) ); } + void add_lft_param(LifetimeParam lft) { m_lifetime_params.push_back( ::std::move(lft) ); } void add_bound(GenericBound bound) { m_bounds.push_back( ::std::move(bound) ); } diff --git a/src/ast/path.hpp b/src/ast/path.hpp index 2126e3b2..0470084b 100644 --- a/src/ast/path.hpp +++ b/src/ast/path.hpp @@ -28,6 +28,7 @@ class Static; namespace AST { +class LifetimeRef; class GenericParams; class Crate; class Module; @@ -109,14 +110,14 @@ extern ::std::ostream& operator<<(::std::ostream& os, const PathBinding& x); struct PathParams { - ::std::vector< ::std::string > m_lifetimes; + ::std::vector< LifetimeRef > m_lifetimes; ::std::vector< TypeRef > m_types; ::std::vector< ::std::pair< ::std::string, TypeRef> > m_assoc; PathParams(PathParams&& x) = default; PathParams(const PathParams& x); PathParams() {} - PathParams(::std::vector<::std::string> lfts, ::std::vector tys, ::std::vector<::std::pair<::std::string,TypeRef>> a): + PathParams(::std::vector lfts, ::std::vector tys, ::std::vector<::std::pair<::std::string,TypeRef>> a): m_lifetimes(mv$(lfts)), m_types(mv$(tys)), m_assoc(mv$(a)) diff --git a/src/ast/types.cpp b/src/ast/types.cpp index af99f3d7..8065a3ba 100644 --- a/src/ast/types.cpp +++ b/src/ast/types.cpp @@ -141,6 +141,15 @@ TypeRef TypeRef::clone() const throw ""; } +Ordering Type_TraitPath::ord(const Type_TraitPath& x) const +{ + Ordering rv; + + rv = ::ord( this->path, x.path ); + if(rv != OrdEqual) return rv; + + return rv; +} Ordering TypeRef::ord(const TypeRef& x) const { Ordering rv; @@ -277,7 +286,8 @@ void TypeRef::print(::std::ostream& os, bool is_debug/*=false*/) const for( const auto& it : ent.traits ) { if( &it != &ent.traits.front() ) os << "+"; - it.print_pretty(os, true, is_debug); + os << it.hrbs; + it.path.print_pretty(os, true, is_debug); } os << ")"; ) @@ -286,7 +296,8 @@ void TypeRef::print(::std::ostream& os, bool is_debug/*=false*/) const for( const auto& it : ent.traits ) { if( &it != &ent.traits.front() ) os << "+"; - it.print_pretty(os, true, is_debug); + os << it.hrbs; + it.path.print_pretty(os, true, is_debug); } os << ""; ) @@ -303,3 +314,21 @@ void TypeRef::print(::std::ostream& os, bool is_debug/*=false*/) const return os; } +namespace AST { + ::std::ostream& operator<<(::std::ostream& os, const LifetimeRef& x) { + if( x.m_binding == LifetimeRef::BINDING_STATIC ) { + os << "'static"; + } + else if( x.m_binding == LifetimeRef::BINDING_INFER ) { + os << "'_"; + } + else { + os << "'" << x.m_name; + if( x.m_binding != LifetimeRef::BINDING_UNBOUND ) { + os << "/*" << x.m_binding << "*/"; + } + } + return os; + } +} + diff --git a/src/ast/types.hpp b/src/ast/types.hpp index 820bcf27..49cae373 100644 --- a/src/ast/types.hpp +++ b/src/ast/types.hpp @@ -13,6 +13,63 @@ namespace AST { class ExprNode; class Expr; +class LifetimeParam; +} + +namespace AST { + + // Defined here for dependency reasons + class HigherRankedBounds + { + public: + ::std::vector m_lifetimes; + //::std::vector m_types; + //::std::vector m_bounds; + + bool empty() const { + return m_lifetimes.empty(); + } + + friend ::std::ostream& operator<<(::std::ostream& os, const HigherRankedBounds& x); + }; + + class LifetimeRef + { + static const uint16_t BINDING_STATIC = 0xFFFF; + static const uint16_t BINDING_UNBOUND = 0xFFFE; + static const uint16_t BINDING_INFER = 0xFFFD; + + Ident m_name; + uint16_t m_binding; + + LifetimeRef(Ident name, uint32_t binding): + m_name( ::std::move(name) ), + m_binding( binding ) + { + } + public: + LifetimeRef(): + LifetimeRef("", BINDING_INFER) + { + } + LifetimeRef(Ident name): + LifetimeRef(::std::move(name), BINDING_UNBOUND) + { + } + static LifetimeRef new_static() { + return LifetimeRef("static", BINDING_STATIC); + } + + void set_binding(uint16_t b) { assert(m_binding == BINDING_UNBOUND); m_binding = b; } + + const Ident& name() const { return m_name; } + Ordering ord(const LifetimeRef& x) const { return ::ord(m_name.name, x.m_name.name); } + bool operator==(const LifetimeRef& x) const { return ord(x) == OrdEqual; } + bool operator!=(const LifetimeRef& x) const { return ord(x) != OrdEqual; } + bool operator<(const LifetimeRef& x) const { return ord(x) == OrdLess; }; + + friend ::std::ostream& operator<<(::std::ostream& os, const LifetimeRef& x); + }; } class PrettyPrintType @@ -57,6 +114,14 @@ struct Type_Function Ordering ord(const Type_Function& x) const; }; +struct Type_TraitPath +{ + AST::HigherRankedBounds hrbs; + AST::Path path; + + Ordering ord(const Type_TraitPath& x) const; +}; + TAGGED_UNION(TypeData, None, (None, struct { }), (Any, struct { }), @@ -94,12 +159,12 @@ TAGGED_UNION(TypeData, None, AST::Path path; }), (TraitObject, struct { - ::std::vector<::std::string> hrls; - ::std::vector traits; + ::std::vector traits; + ::std::vector lifetimes; }), (ErasedType, struct { - ::std::vector<::std::string> hrls; - ::std::vector traits; + ::std::vector traits; + ::std::vector lifetimes; }) ); @@ -215,9 +280,9 @@ public: TypeRef(TagPath(), mv$(sp), mv$(path)) {} - TypeRef( Span sp, ::std::vector<::std::string> hrls, ::std::vector traits ): + TypeRef( Span sp, ::std::vector traits, ::std::vector lifetimes ): m_span(mv$(sp)), - m_data(TypeData::make_TraitObject({ mv$(hrls), ::std::move(traits) })) + m_data(TypeData::make_TraitObject({ ::std::move(traits), mv$(lifetimes) })) {} diff --git a/src/expand/derive.cpp b/src/expand/derive.cpp index 077c4b70..0e28b6da 100644 --- a/src/expand/derive.cpp +++ b/src/expand/derive.cpp @@ -86,7 +86,7 @@ struct Deriver for(const auto& arg : params.ty_params()) { params.add_bound( ::AST::GenericBound::make_IsTrait({ - TypeRef(sp, arg.name(), i), {}, trait_path + {}, TypeRef(sp, arg.name(), i), {}, trait_path }) ); i ++; } @@ -96,7 +96,7 @@ struct Deriver for(auto& ty : additional_bounded_types) { params.add_bound( ::AST::GenericBound::make_IsTrait({ - mv$(ty), {}, trait_path + {}, mv$(ty), {}, trait_path }) ); } @@ -1563,11 +1563,10 @@ class Deriver_Hash: ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "state"), TypeRef(TypeRef::TagReference(), sp, true, TypeRef(sp, "H", 0x100|0)) ) ) ); - fcn.params().add_ty_param( AST::TypeParam("H") ); + fcn.params().add_ty_param( AST::TypeParam(sp, {}, "H") ); fcn.params().add_bound( AST::GenericBound::make_IsTrait({ - TypeRef(sp, "H", 0x100|0), - {}, - this->get_trait_path_Hasher(core_name) + {}, TypeRef(sp, "H", 0x100|0), + {}, this->get_trait_path_Hasher(core_name) }) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); @@ -1717,11 +1716,10 @@ class Deriver_RustcEncodable: ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "s"), TypeRef(TypeRef::TagReference(), sp, true, TypeRef(sp, "S", 0x100|0)) ) ) ); - fcn.params().add_ty_param( AST::TypeParam("S") ); + fcn.params().add_ty_param( AST::TypeParam(sp, {}, "S") ); fcn.params().add_bound( AST::GenericBound::make_IsTrait({ - TypeRef(sp, "S", 0x100|0), - {}, - this->get_trait_path_Encoder() + {}, TypeRef(sp, "S", 0x100|0), + {}, this->get_trait_path_Encoder() }) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); @@ -1951,11 +1949,10 @@ class Deriver_RustcDecodable: ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "d"), TypeRef(TypeRef::TagReference(), sp, true, TypeRef(sp, "D", 0x100|0)) ) ) ); - fcn.params().add_ty_param( AST::TypeParam("D") ); + fcn.params().add_ty_param( AST::TypeParam(sp, {}, "D") ); fcn.params().add_bound( AST::GenericBound::make_IsTrait({ - TypeRef(sp, "D", 0x100|0), - {}, - this->get_trait_path_Decoder() + {}, TypeRef(sp, "D", 0x100|0), + {}, this->get_trait_path_Decoder() }) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index aa7655e3..4470b2bb 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -272,13 +272,15 @@ void Expand_Type(::AST::Crate& crate, LList modstack, ::AST: (TraitObject, for(auto& p : e.traits) { - Expand_Path(crate, modstack, mod, p); + // TODO: p.hrbs? Not needed until types are in those + Expand_Path(crate, modstack, mod, p.path); } ), (ErasedType, for(auto& p : e.traits) { - Expand_Path(crate, modstack, mod, p); + // TODO: p.hrbs? + Expand_Path(crate, modstack, mod, p.path); } ) ) @@ -859,6 +861,8 @@ void Expand_GenericParams(::AST::Crate& crate, LList modstac for(auto& bound : params.bounds()) { TU_MATCHA( (bound), (be), + (None, + ), (Lifetime, ), (TypeLifetime, diff --git a/src/expand/proc_macro.cpp b/src/expand/proc_macro.cpp index cdd13a42..28d9f613 100644 --- a/src/expand/proc_macro.cpp +++ b/src/expand/proc_macro.cpp @@ -345,45 +345,41 @@ namespace { ), (TraitObject, m_pmi.send_symbol("("); - if( te.hrls.size() > 0 ) - { - m_pmi.send_ident("for"); - m_pmi.send_symbol("<"); - for(const auto& v : te.hrls) - { - m_pmi.send_lifetime(v.c_str()); - m_pmi.send_symbol(","); - } - m_pmi.send_symbol(">"); - } for(const auto& t : te.traits) { - this->visit_path(t); + this->visit_hrbs(t.hrbs); + this->visit_path(t.path); m_pmi.send_symbol("+"); } + // TODO: Lifetimes m_pmi.send_symbol(")"); ), (ErasedType, m_pmi.send_ident("impl"); - if( te.hrls.size() > 0 ) - { - m_pmi.send_ident("for"); - m_pmi.send_symbol("<"); - for(const auto& v : te.hrls) - { - m_pmi.send_lifetime(v.c_str()); - m_pmi.send_symbol(","); - } - m_pmi.send_symbol(">"); - } for(const auto& t : te.traits) { - this->visit_path(t); + this->visit_hrbs(t.hrbs); + this->visit_path(t.path); m_pmi.send_symbol("+"); } + // TODO: Lifetimes ) ) } + void visit_hrbs(const AST::HigherRankedBounds& hrbs) + { + if( !hrbs.empty() ) + { + m_pmi.send_ident("for"); + m_pmi.send_symbol("<"); + for(const auto& v : hrbs.m_lifetimes) + { + m_pmi.send_lifetime(v.name().name.c_str()); + m_pmi.send_symbol(","); + } + m_pmi.send_symbol(">"); + } + } void visit_path(const AST::Path& path, bool is_expr=false) { @@ -445,7 +441,7 @@ namespace { m_pmi.send_symbol("<"); for(const auto& l : e.args().m_lifetimes) { - m_pmi.send_lifetime(l.c_str()); + m_pmi.send_lifetime(l.name().name.c_str()); m_pmi.send_symbol(","); } for(const auto& t : e.args().m_types) @@ -475,7 +471,7 @@ namespace { { if( !is_first ) m_pmi.send_symbol(","); - m_pmi.send_lifetime(p.c_str()); + m_pmi.send_lifetime(p.name().name.c_str()); is_first = false; } // Types diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index 1dbfdad2..6353a0ea 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -28,6 +28,11 @@ ::HIR::Crate* g_crate_ptr = nullptr; // -------------------------------------------------------------------- +::std::string LowerHIR_LifetimeRef(const ::AST::LifetimeRef& r) +{ + return r.name().name; +} + ::HIR::GenericParams LowerHIR_GenericParams(const ::AST::GenericParams& gp, bool* self_is_sized) { ::HIR::GenericParams rv; @@ -41,24 +46,26 @@ } if( gp.lft_params().size() > 0 ) { - for(const auto& lft_name : gp.lft_params()) - rv.m_lifetimes.push_back( lft_name ); + for(const auto& lft_def : gp.lft_params()) + rv.m_lifetimes.push_back( lft_def.name().name ); } if( gp.bounds().size() > 0 ) { for(const auto& bound : gp.bounds()) { TU_MATCH(::AST::GenericBound, (bound), (e), + (None, + ), (Lifetime, rv.m_bounds.push_back(::HIR::GenericBound::make_Lifetime({ - e.test, - e.bound + LowerHIR_LifetimeRef(e.test), + LowerHIR_LifetimeRef(e.bound) })); ), (TypeLifetime, rv.m_bounds.push_back(::HIR::GenericBound::make_TypeLifetime({ LowerHIR_Type(e.type), - e.bound + LowerHIR_LifetimeRef(e.bound) })); ), (IsTrait, @@ -66,8 +73,8 @@ // TODO: Check if this trait is `Sized` and ignore if it is - rv.m_bounds.push_back(::HIR::GenericBound::make_TraitBound({ mv$(type), LowerHIR_TraitPath(bound.span, e.trait) })); - rv.m_bounds.back().as_TraitBound().trait.m_hrls = e.hrls; + rv.m_bounds.push_back(::HIR::GenericBound::make_TraitBound({ /*LowerHIR_HigherRankedBounds(e.outer_hrbs),*/ mv$(type), LowerHIR_TraitPath(bound.span, e.trait) })); + //rv.m_bounds.back().as_TraitBound().trait.m_hrls = LowerHIR_HigherRankedBounds(e.inner_hrbs); ), (MaybeTrait, auto type = LowerHIR_Type(e.type); @@ -755,47 +762,59 @@ } ), (TraitObject, - //if( e.hrls.size() > 0 ) - // TODO(ty.span(), "TraitObjects with HRLS - " << ty); ::HIR::TypeRef::Data::Data_TraitObject v; // TODO: Lifetime for(const auto& t : e.traits) { - DEBUG("t = " << t); - const auto& tb = t.binding().as_Trait(); + DEBUG("t = " << t.path); + const auto& tb = t.path.binding().as_Trait(); assert( tb.trait_ || tb.hir ); - if( (tb.trait_ ? tb.trait_->is_marker() : tb.hir->m_is_marker) ) { + if( (tb.trait_ ? tb.trait_->is_marker() : tb.hir->m_is_marker) ) + { if( tb.hir ) { DEBUG(tb.hir->m_values.size()); } - v.m_markers.push_back( LowerHIR_GenericPath(ty.span(), t) ); + // TODO: If this has HRBs, what? + v.m_markers.push_back( LowerHIR_GenericPath(ty.span(), t.path) ); } else { // TraitPath -> GenericPath -> SimplePath if( v.m_trait.m_path.m_path.m_components.size() > 0 ) { ERROR(ty.span(), E0000, "Multiple data traits in trait object - " << ty); } - v.m_trait = LowerHIR_TraitPath(ty.span(), t); + // TODO: Handle HRBs + v.m_trait = LowerHIR_TraitPath(ty.span(), t.path); } } return ::HIR::TypeRef( ::HIR::TypeRef::Data::make_TraitObject( mv$(v) ) ); ), (ErasedType, - //if( e.hrls.size() > 0 ) - // TODO(ty.span(), "ErasedType with HRLS - " << ty); ASSERT_BUG(ty.span(), e.traits.size() > 0, "ErasedType with no traits"); ::std::vector< ::HIR::TraitPath> traits; for(const auto& t : e.traits) { - DEBUG("t = " << t); - traits.push_back( LowerHIR_TraitPath(ty.span(), t) ); + DEBUG("t = " << t.path); + // TODO: Pass the HRBs down + traits.push_back( LowerHIR_TraitPath(ty.span(), t.path) ); + } + ::HIR::LifetimeRef lft; + if( e.lifetimes.size() == 0 ) + { + } + else if( e.lifetimes.size() == 1 ) + { + // TODO: Convert the lifetime reference + } + else + { + TODO(ty.span(), "Handle multiple lifetime parameters - " << ty); } // Leave `m_origin` until the bind pass return ::HIR::TypeRef( ::HIR::TypeRef::Data::make_ErasedType(::HIR::TypeRef::Data::Data_ErasedType { ::HIR::Path(::HIR::SimplePath()), 0, mv$(traits), - ::HIR::LifetimeRef() // TODO: Lifetime ref + lft } ) ); ), (Function, diff --git a/src/parse/common.hpp b/src/parse/common.hpp index 1ca86128..49499c86 100644 --- a/src/parse/common.hpp +++ b/src/parse/common.hpp @@ -40,7 +40,7 @@ extern ::std::vector Parse_PathNodes(TokenStream& lex, eParsePath extern AST::PathParams Parse_Path_GenericList(TokenStream& lex); -extern ::std::vector< ::std::string> Parse_HRB(TokenStream& lex); +extern AST::HigherRankedBounds Parse_HRB(TokenStream& lex); extern AST::AttributeList Parse_ItemAttrs(TokenStream& lex); extern void Parse_ParentAttrs(TokenStream& lex, AST::AttributeList& out); extern AST::Attribute Parse_MetaItem(TokenStream& lex); diff --git a/src/parse/paths.cpp b/src/parse/paths.cpp index 59b6b939..d103840e 100644 --- a/src/parse/paths.cpp +++ b/src/parse/paths.cpp @@ -203,7 +203,7 @@ AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generi Token tok; ::std::vector types; - ::std::vector< ::std::string> lifetimes; + ::std::vector lifetimes; ::std::vector< ::std::pair< ::std::string, TypeRef > > assoc_bounds; do { @@ -214,7 +214,7 @@ AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generi switch(GET_TOK(tok, lex)) { case TOK_LIFETIME: - lifetimes.push_back( tok.str() ); + lifetimes.push_back(AST::LifetimeRef(/*lex.point_span(),*/ lex.get_ident(mv$(tok)) )); break; case TOK_IDENT: if( LOOK_AHEAD(lex) == TOK_EQUAL ) diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 35da3a76..2b027bf9 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -119,12 +119,12 @@ bool Parse_Publicity(TokenStream& lex, bool allow_restricted=true) } } -::std::vector< ::std::string> Parse_HRB(TokenStream& lex) +::AST::HigherRankedBounds Parse_HRB(TokenStream& lex) { TRACE_FUNCTION; Token tok; - ::std::vector< ::std::string> lifetimes; + ::AST::HigherRankedBounds rv; GET_CHECK_TOK(tok, lex, TOK_LT); do { auto attrs = Parse_ItemAttrs(lex); @@ -133,17 +133,25 @@ bool Parse_Publicity(TokenStream& lex, bool allow_restricted=true) switch(GET_TOK(tok, lex)) { case TOK_LIFETIME: - lifetimes.push_back(tok.str()); + rv.m_lifetimes.push_back(::AST::LifetimeParam(lex.point_span(), ::std::move(attrs), Ident(lex.getHygiene(), tok.str()))); break; default: throw ParseError::Unexpected(lex, tok, Token(TOK_LIFETIME)); } } while( GET_TOK(tok, lex) == TOK_COMMA ); CHECK_TOK(tok, TOK_GT); - return lifetimes; + return rv; +} + +namespace { + AST::LifetimeRef get_LifetimeRef(TokenStream& lex, Token tok) + { + CHECK_TOK(tok, TOK_LIFETIME); + return AST::LifetimeRef(/*lex.point_span(), */Ident(lex.getHygiene(), mv$(tok.str()))); + } } /// Parse type parameters in a definition -void Parse_TypeBound(TokenStream& lex, AST::GenericParams& ret, TypeRef checked_type, ::std::vector< ::std::string> lifetimes = {}) +void Parse_TypeBound(TokenStream& lex, AST::GenericParams& ret, TypeRef checked_type, AST::HigherRankedBounds outer_hrbs = {}) { TRACE_FUNCTION; Token tok; @@ -156,10 +164,9 @@ void Parse_TypeBound(TokenStream& lex, AST::GenericParams& ret, TypeRef checked_ // return; //} - ::std::vector< ::std::string> hrls; if(GET_TOK(tok, lex) == TOK_LIFETIME) { ret.add_bound(AST::GenericBound::make_TypeLifetime( { - checked_type.clone(), tok.str() + checked_type.clone(), get_LifetimeRef(lex, mv$(tok)) } )); } else if( tok.type() == TOK_QMARK ) { @@ -168,17 +175,17 @@ void Parse_TypeBound(TokenStream& lex, AST::GenericParams& ret, TypeRef checked_ } )); } else { + ::AST::HigherRankedBounds inner_hrls; if( tok.type() == TOK_RWORD_FOR ) { - hrls = Parse_HRB(lex); + inner_hrls = Parse_HRB(lex); } else { PUTBACK(tok, lex); } - (void)hrls; // TODO: HRLs ret.add_bound( AST::GenericBound::make_IsTrait({ - checked_type.clone(), mv$(lifetimes), Parse_Path(lex, PATH_GENERIC_TYPE) + mv$(outer_hrbs), checked_type.clone(), mv$(inner_hrls), Parse_Path(lex, PATH_GENERIC_TYPE) }) ); } } while( GET_TOK(tok, lex) == TOK_PLUS ); @@ -199,14 +206,12 @@ AST::GenericParams Parse_GenericParams(TokenStream& lex) PUTBACK(tok, lex); auto attrs = Parse_ItemAttrs(lex); - (void)attrs; // TODO: Attributes on generic params GET_TOK(tok, lex); if( tok.type() == TOK_IDENT ) { - // TODO: Hygine ::std::string param_name = mv$(tok.str()); - ret.add_ty_param( AST::TypeParam( param_name ) ); + ret.add_ty_param( AST::TypeParam( lex.point_span(), ::std::move(attrs), param_name ) ); auto param_ty = TypeRef(lex.point_span(), param_name); if( GET_TOK(tok, lex) == TOK_COLON ) @@ -223,14 +228,14 @@ AST::GenericParams Parse_GenericParams(TokenStream& lex) } else if( tok.type() == TOK_LIFETIME ) { - // TODO: Should lifetimes have hygine? - ::std::string param_name = tok.str(); - ret.add_lft_param( param_name ); + auto param_name = tok.str(); + auto ref = get_LifetimeRef(lex, mv$(tok)); + ret.add_lft_param(::AST::LifetimeParam(lex.point_span(), ::std::move(attrs), Ident(lex.getHygiene(), param_name) )); if( GET_TOK(tok, lex) == TOK_COLON ) { do { GET_CHECK_TOK(tok, lex, TOK_LIFETIME); - ret.add_bound(AST::GenericBound::make_Lifetime( {param_name, tok.str()} )); + ret.add_bound(AST::GenericBound::make_Lifetime({ AST::LifetimeRef(ref), get_LifetimeRef(lex, mv$(tok)) })); } while( GET_TOK(tok, lex) == TOK_PLUS ); } } @@ -258,11 +263,11 @@ void Parse_WhereClause(TokenStream& lex, AST::GenericParams& params) if( tok.type() == TOK_LIFETIME ) { - auto lhs = mv$(tok.str()); + auto lhs = get_LifetimeRef(lex, mv$(tok)); GET_CHECK_TOK(tok, lex, TOK_COLON); do { GET_CHECK_TOK(tok, lex, TOK_LIFETIME); - auto rhs = mv$(tok.str()); + auto rhs = get_LifetimeRef(lex, mv$(tok)); params.add_bound( AST::GenericBound::make_Lifetime({lhs, rhs}) ); } while( GET_TOK(tok, lex) == TOK_PLUS ); PUTBACK(tok, lex); @@ -270,11 +275,11 @@ void Parse_WhereClause(TokenStream& lex, AST::GenericParams& params) // Higher-ranked types/lifetimes else if( tok.type() == TOK_RWORD_FOR ) { - ::std::vector< ::std::string> lifetimes = Parse_HRB(lex); + auto hrbs = Parse_HRB(lex); TypeRef type = Parse_Type(lex); GET_CHECK_TOK(tok, lex, TOK_COLON); - Parse_TypeBound(lex,params, mv$(type), mv$(lifetimes)); + Parse_TypeBound(lex,params, mv$(type), mv$(hrbs)); } else { @@ -355,9 +360,9 @@ AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, bool allow_ if( lex.lookahead(ofs) == TOK_RWORD_SELF || (lex.lookahead(ofs) == TOK_RWORD_MUT && lex.lookahead(ofs+1) == TOK_RWORD_SELF) ) { auto ps = lex.start_span(); - ::std::string lifetime; + AST::LifetimeRef lifetime; if( GET_TOK(tok, lex) == TOK_LIFETIME ) { - lifetime = tok.str(); + lifetime = get_LifetimeRef(lex, mv$(tok)); GET_TOK(tok, lex); } auto ty_sp = lex.end_span(ps); @@ -622,6 +627,7 @@ AST::Trait Parse_TraitDef(TokenStream& lex, const AST::AttributeList& meta_items do { if( GET_TOK(tok, lex) == TOK_LIFETIME ) { // TODO: Need a better way of indiciating 'static than just an invalid path + // TODO: Ensure that it's 'static supertraits.push_back( make_spanned( Span(tok.get_pos()), AST::Path() ) ); } else if( tok.type() == TOK_BRACE_OPEN ) { diff --git a/src/parse/tokenstream.cpp b/src/parse/tokenstream.cpp index 625f12db..611df2ff 100644 --- a/src/parse/tokenstream.cpp +++ b/src/parse/tokenstream.cpp @@ -140,6 +140,10 @@ Ident TokenStream::get_ident(Token tok) const if(tok.type() == TOK_IDENT) { return Ident(getHygiene(), tok.str()); } + else if(tok.type() == TOK_LIFETIME) { + // TODO: Maybe only when it's explicitly asked for? + return Ident(getHygiene(), tok.str()); + } else if( tok.type() == TOK_INTERPOLATED_IDENT ) { TODO(getPosition(), "get_ident from TOK_INTERPOLATED_IDENT"); } diff --git a/src/parse/types.cpp b/src/parse/types.cpp index 26785cf7..b0b43426 100644 --- a/src/parse/types.cpp +++ b/src/parse/types.cpp @@ -13,8 +13,8 @@ // === PROTOTYPES === //TypeRef Parse_Type(TokenStream& lex, bool allow_trait_list); TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list); -TypeRef Parse_Type_Fn(TokenStream& lex, ::std::vector<::std::string> hrls = {}); -TypeRef Parse_Type_Path(TokenStream& lex, ::std::vector<::std::string> hrls, bool allow_trait_list); +TypeRef Parse_Type_Fn(TokenStream& lex, AST::HigherRankedBounds hrbs = {}); +TypeRef Parse_Type_Path(TokenStream& lex, AST::HigherRankedBounds hrbs, bool allow_trait_list); TypeRef Parse_Type_ErasedType(TokenStream& lex, bool allow_trait_list); // === CODE === @@ -196,7 +196,7 @@ TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list) throw ParseError::BugCheck("Reached end of Parse_Type"); } -TypeRef Parse_Type_Fn(TokenStream& lex, ::std::vector<::std::string> hrls) +TypeRef Parse_Type_Fn(TokenStream& lex, ::AST::HigherRankedBounds hrbs) { auto ps = lex.start_span(); // TODO: HRLs @@ -262,36 +262,52 @@ TypeRef Parse_Type_Fn(TokenStream& lex, ::std::vector<::std::string> hrls) return TypeRef(TypeRef::TagFunction(), lex.end_span(ps), is_unsafe, mv$(abi), mv$(args), is_variadic, mv$(ret_type)); } -TypeRef Parse_Type_Path(TokenStream& lex, ::std::vector<::std::string> hrls, bool allow_trait_list) +TypeRef Parse_Type_Path(TokenStream& lex, ::AST::HigherRankedBounds hrbs, bool allow_trait_list) { Token tok; auto ps = lex.start_span(); - if( ! allow_trait_list ) + if( hrbs.empty() && !allow_trait_list ) { return TypeRef(TypeRef::TagPath(), lex.end_span(ps), Parse_Path(lex, PATH_GENERIC_TYPE)); } else { - ::std::vector traits; - ::std::vector< ::std::string> lifetimes; - do { - if( LOOK_AHEAD(lex) == TOK_LIFETIME ) { - GET_TOK(tok, lex); - lifetimes.push_back( tok.str() ); + ::std::vector traits; + ::std::vector lifetimes; + + traits.push_back(Type_TraitPath { mv$(hrbs), Parse_Path(lex, PATH_GENERIC_TYPE) }); + + if( allow_trait_list ) + { + while( GET_TOK(tok, lex) == TOK_PLUS ) + { + if( LOOK_AHEAD(lex) == TOK_LIFETIME ) { + GET_TOK(tok, lex); + lifetimes.push_back(AST::LifetimeRef( /*lex.point_span(),*/ lex.get_ident(mv$(tok)) )); + } + else + { + if( lex.lookahead(0) == TOK_RWORD_FOR ) + { + hrbs = Parse_HRB(lex); + } + traits.push_back({ mv$(hrbs), Parse_Path(lex, PATH_GENERIC_TYPE) }); + } } - else - traits.push_back( Parse_Path(lex, PATH_GENERIC_TYPE) ); - } while( GET_TOK(tok, lex) == TOK_PLUS ); - PUTBACK(tok, lex); - if( hrls.size() > 0 || traits.size() > 1 || lifetimes.size() > 0 ) { - if( lifetimes.size() ) - DEBUG("TODO: Lifetime bounds on trait objects"); - return TypeRef(lex.end_span(ps), mv$(hrls), ::std::move(traits)); + PUTBACK(tok, lex); } - else { - return TypeRef(TypeRef::TagPath(), lex.end_span(ps), mv$(traits.at(0))); + + if( !traits[0].hrbs.empty() || traits.size() > 1 || lifetimes.size() > 0 ) + { + if( lifetimes.empty()) + lifetimes.push_back(AST::LifetimeRef()); + return TypeRef(lex.end_span(ps), mv$(traits), mv$(lifetimes)); + } + else + { + return TypeRef(TypeRef::TagPath(), lex.end_span(ps), mv$(traits.at(0).path)); } } } @@ -300,20 +316,25 @@ TypeRef Parse_Type_ErasedType(TokenStream& lex, bool allow_trait_list) Token tok; auto ps = lex.start_span(); - ::std::vector traits; - ::std::vector< ::std::string> lifetimes; + ::std::vector traits; + ::std::vector lifetimes; do { if( LOOK_AHEAD(lex) == TOK_LIFETIME ) { GET_TOK(tok, lex); - lifetimes.push_back( tok.str() ); + lifetimes.push_back(AST::LifetimeRef( /*lex.point_span(),*/ lex.get_ident(mv$(tok)) )); } else - traits.push_back( Parse_Path(lex, PATH_GENERIC_TYPE) ); + { + AST::HigherRankedBounds hrbs; + if( lex.lookahead(0) == TOK_RWORD_FOR ) + { + hrbs = Parse_HRB(lex); + } + traits.push_back({ mv$(hrbs), Parse_Path(lex, PATH_GENERIC_TYPE) }); + } } while( GET_TOK(tok, lex) == TOK_PLUS ); PUTBACK(tok, lex); - if( lifetimes.size() ) - DEBUG("TODO: Lifetime bounds on erased types"); - return TypeRef(lex.end_span(ps), TypeData::make_ErasedType({ {}, mv$(traits) })); + return TypeRef(lex.end_span(ps), TypeData::make_ErasedType({ mv$(traits), mv$(lifetimes) })); } diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index 2c17592c..d05e3bc6 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -1500,24 +1500,24 @@ void Resolve_Absolute_Type(Context& context, TypeRef& type) ) TU_IFLET(::AST::PathBinding, e.path.binding(), Trait, be, - auto ty = ::TypeRef( type.span(), {}, ::make_vec1(mv$(e.path)) ); + auto ty = ::TypeRef( type.span(), ::make_vec1(Type_TraitPath { {}, mv$(e.path)}), {} ); type = mv$(ty); return ; ) ), (TraitObject, - //context.push_lifetimes( e.hrls ); for(auto& trait : e.traits) { - Resolve_Absolute_Path(context, type.span(), Context::LookupMode::Type, trait); + //context.push_lifetimes( trait.hrbs.m_lifetimes ); + Resolve_Absolute_Path(context, type.span(), Context::LookupMode::Type, trait.path); + //context.pop_lifetimes(); } - //context.pop_lifetimes(); ), (ErasedType, - //context.push_lifetimes( e.hrls ); for(auto& trait : e.traits) { - Resolve_Absolute_Path(context, type.span(), Context::LookupMode::Type, trait); + //context.push_lifetimes( trait.hrbs.m_lifetimes ); + Resolve_Absolute_Path(context, type.span(), Context::LookupMode::Type, trait.path); + //context.pop_lifetimes(); } - //context.pop_lifetimes(); ) ) } @@ -1683,6 +1683,8 @@ void Resolve_Absolute_Generic(Context& context, ::AST::GenericParams& params) for( auto& bound : params.bounds() ) { TU_MATCH(::AST::GenericBound, (bound), (e), + (None, + ), (Lifetime, // TODO: Link lifetime names to params ), -- cgit v1.2.3 From 4f5053eb77644d69f75b3482a91752684d810bd9 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Fri, 25 May 2018 21:16:11 +0800 Subject: Resolve - Named lifetimes --- src/ast/types.hpp | 2 ++ src/resolve/absolute.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 79 insertions(+), 8 deletions(-) (limited to 'src/resolve/absolute.cpp') diff --git a/src/ast/types.hpp b/src/ast/types.hpp index 2490c5e8..1c06593c 100644 --- a/src/ast/types.hpp +++ b/src/ast/types.hpp @@ -61,6 +61,8 @@ namespace AST { } void set_binding(uint16_t b) { assert(m_binding == BINDING_UNBOUND); m_binding = b; } + bool is_unbound() const { return m_binding == BINDING_UNBOUND; } + bool is_infer() const { return m_binding == BINDING_INFER; } const Ident& name() const { return m_name; } Ordering ord(const LifetimeRef& x) const { return ::ord(m_name.name, x.m_name.name); } diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index d05e3bc6..ceb0e40a 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -20,6 +20,7 @@ namespace { Top, Method, + Hrb, } level; unsigned short index; @@ -38,6 +39,12 @@ namespace ::std::string name; Val value; }; + template + struct NamedI + { + const Ident& name; + Val value; + }; struct Context { @@ -55,7 +62,7 @@ namespace // Map of names to slots ::std::vector< Named< GenericSlot > > types; ::std::vector< Named< GenericSlot > > constants; - ::std::vector< Named< GenericSlot > > lifetimes; + ::std::vector< NamedI< GenericSlot > > lifetimes; }) ); @@ -74,6 +81,17 @@ namespace m_frozen_bind_set( false ) {} + void push(const ::AST::HigherRankedBounds& params) { + auto e = Ent::make_Generic({}); + auto& data = e.as_Generic(); + + for(size_t i = 0; i < params.m_lifetimes.size(); i ++) + { + data.lifetimes.push_back( NamedI { params.m_lifetimes[i].name(), GenericSlot { GenericSlot::Level::Hrb, static_cast(i) } } ); + } + + m_name_context.push_back(mv$(e)); + } void push(const ::AST::GenericParams& params, GenericSlot::Level level, bool has_self=false) { auto e = Ent::make_Generic({}); auto& data = e.as_Generic(); @@ -85,16 +103,24 @@ namespace } if( params.ty_params().size() > 0 ) { const auto& typs = params.ty_params(); - for(unsigned int i = 0; i < typs.size(); i ++ ) { + for(size_t i = 0; i < typs.size(); i ++ ) { data.types.push_back( Named { typs[i].name(), GenericSlot { level, static_cast(i) } } ); } } if( params.lft_params().size() > 0 ) { - //TODO(Span(), "resolve/absolute.cpp - Context::push(GenericParams) - Lifetime params - " << params); + const auto& lfts = params.lft_params(); + for(size_t i = 0; i < lfts.size(); i ++ ) { + data.lifetimes.push_back( NamedI { lfts[i].name(), GenericSlot { level, static_cast(i) } } ); + } } m_name_context.push_back(mv$(e)); } + void pop(const ::AST::HigherRankedBounds& ) { + if( !m_name_context.back().is_Generic() ) + BUG(Span(), "resolve/absolute.cpp - Context::pop(GenericParams) - Mismatched pop"); + m_name_context.pop_back(); + } void pop(const ::AST::GenericParams& , bool has_self=false) { if( !m_name_context.back().is_Generic() ) BUG(Span(), "resolve/absolute.cpp - Context::pop(GenericParams) - Mismatched pop"); @@ -501,6 +527,7 @@ namespace void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Context::LookupMode& mode, ::AST::Path& path); void Resolve_Absolute_Path(/*const*/ Context& context, const Span& sp, Context::LookupMode mode, ::AST::Path& path); +void Resolve_Absolute_Lifetime(Context& context, const Span& sp, AST::LifetimeRef& type); void Resolve_Absolute_Type(Context& context, TypeRef& type); void Resolve_Absolute_Expr(Context& context, ::AST::Expr& expr); void Resolve_Absolute_ExprNode(Context& context, ::AST::ExprNode& node); @@ -1432,6 +1459,37 @@ void Resolve_Absolute_Path(/*const*/ Context& context, const Span& sp, Context:: // - Helps with cases like PartialOrd, but hinders when the default is a hint (in expressions) } +void Resolve_Absolute_Lifetime(Context& context, const Span& sp, AST::LifetimeRef& lft) +{ + TRACE_FUNCTION_FR("lft = " << lft, "lft = " << lft); + if( lft.is_unbound() ) + { + if( lft.name() == "static" ) + { + lft = AST::LifetimeRef::new_static(); + return ; + } + + for(auto it = context.m_name_context.rbegin(); it != context.m_name_context.rend(); ++ it) + { + if( const auto* e = it->opt_Generic() ) + { + for(const auto& l : e->lifetimes) + { + // NOTE: Hygiene doesn't apply to lifetime params! + if( l.name.name == lft.name().name /*&& l.name.hygiene.is_visible(lft.name().hygiene)*/ ) + { + lft.set_binding( l.value.index | (static_cast(l.value.level) << 8) ); + return ; + } + } + } + } + + ERROR(sp, E0000, "Couldn't find lifetime " << lft); + } +} + void Resolve_Absolute_Type(Context& context, TypeRef& type) { TRACE_FUNCTION_FR("type = " << type, "type = " << type); @@ -1464,6 +1522,7 @@ void Resolve_Absolute_Type(Context& context, TypeRef& type) Resolve_Absolute_Type(context, t); ), (Borrow, + Resolve_Absolute_Lifetime(context, type.span(), e.lifetime); Resolve_Absolute_Type(context, *e.inner); ), (Pointer, @@ -1507,17 +1566,21 @@ void Resolve_Absolute_Type(Context& context, TypeRef& type) ), (TraitObject, for(auto& trait : e.traits) { - //context.push_lifetimes( trait.hrbs.m_lifetimes ); + context.push( trait.hrbs ); Resolve_Absolute_Path(context, type.span(), Context::LookupMode::Type, trait.path); - //context.pop_lifetimes(); + context.pop(trait.hrbs); } + for(auto& lft : e.lifetimes) + Resolve_Absolute_Lifetime(context, type.span(),lft); ), (ErasedType, for(auto& trait : e.traits) { - //context.push_lifetimes( trait.hrbs.m_lifetimes ); + context.push( trait.hrbs ); Resolve_Absolute_Path(context, type.span(), Context::LookupMode::Type, trait.path); - //context.pop_lifetimes(); + context.pop(trait.hrbs); } + for(auto& lft : e.lifetimes) + Resolve_Absolute_Lifetime(context, type.span(), lft); ) ) } @@ -1686,14 +1749,20 @@ void Resolve_Absolute_Generic(Context& context, ::AST::GenericParams& params) (None, ), (Lifetime, - // TODO: Link lifetime names to params + Resolve_Absolute_Lifetime(context, bound.span, e.test); + Resolve_Absolute_Lifetime(context, bound.span,e.bound); ), (TypeLifetime, Resolve_Absolute_Type(context, e.type); + Resolve_Absolute_Lifetime(context, bound.span, e.bound); ), (IsTrait, + context.push( e.outer_hrbs ); Resolve_Absolute_Type(context, e.type); + context.push( e.inner_hrbs ); Resolve_Absolute_Path(context, bound.span, Context::LookupMode::Type, e.trait); + context.pop( e.inner_hrbs ); + context.pop( e.outer_hrbs ); ), (MaybeTrait, Resolve_Absolute_Type(context, e.type); -- cgit v1.2.3 From af1ec8a893d33aa974186d06498d0fc81d22ae7b Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 27 May 2018 11:33:04 +0800 Subject: AST - Include HRBs on fn() types, fix minor parsing bug --- src/ast/types.hpp | 8 +++++--- src/parse/root.cpp | 4 +++- src/parse/types.cpp | 8 +++++--- src/resolve/absolute.cpp | 2 ++ 4 files changed, 15 insertions(+), 7 deletions(-) (limited to 'src/resolve/absolute.cpp') diff --git a/src/ast/types.hpp b/src/ast/types.hpp index 1c06593c..22465593 100644 --- a/src/ast/types.hpp +++ b/src/ast/types.hpp @@ -96,6 +96,7 @@ struct TypeArgRef struct Type_Function { + AST::HigherRankedBounds hrbs; bool is_unsafe; ::std::string m_abi; ::std::unique_ptr m_rettype; @@ -103,7 +104,8 @@ struct Type_Function bool is_variadic; Type_Function() {} - Type_Function(bool is_unsafe, ::std::string abi, ::std::unique_ptr ret, ::std::vector args, bool is_variadic): + Type_Function(AST::HigherRankedBounds hrbs, bool is_unsafe, ::std::string abi, ::std::unique_ptr ret, ::std::vector args, bool is_variadic): + hrbs(mv$(hrbs)), is_unsafe(is_unsafe), m_abi(mv$(abi)), m_rettype(mv$(ret)), @@ -239,9 +241,9 @@ public: m_data(TypeData::make_Tuple({::std::move(inner_types)})) {} struct TagFunction {}; - TypeRef(TagFunction, Span sp, bool is_unsafe, ::std::string abi, ::std::vector args, bool is_variadic, TypeRef ret): + TypeRef(TagFunction, Span sp, AST::HigherRankedBounds hrbs, bool is_unsafe, ::std::string abi, ::std::vector args, bool is_variadic, TypeRef ret): m_span(mv$(sp)), - m_data(TypeData::make_Function({ Type_Function( is_unsafe, abi, box$(ret), mv$(args), is_variadic ) })) + m_data(TypeData::make_Function({ Type_Function( mv$(hrbs), is_unsafe, abi, box$(ret), mv$(args), is_variadic ) })) {} struct TagReference {}; diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 0fcbe63a..0a8d2909 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -183,9 +183,11 @@ void Parse_TypeBound(TokenStream& lex, AST::GenericParams& ret, TypeRef checked_ else { PUTBACK(tok, lex); } + auto trait_path = Parse_Path(lex, PATH_GENERIC_TYPE); + auto this_outer_hrbs = (lex.lookahead(0) == TOK_PLUS ? AST::HigherRankedBounds(outer_hrbs) : mv$(outer_hrbs)); ret.add_bound( AST::GenericBound::make_IsTrait({ - mv$(outer_hrbs), checked_type.clone(), mv$(inner_hrls), Parse_Path(lex, PATH_GENERIC_TYPE) + mv$(this_outer_hrbs), checked_type.clone(), mv$(inner_hrls), mv$(trait_path) }) ); } } while( GET_TOK(tok, lex) == TOK_PLUS ); diff --git a/src/parse/types.cpp b/src/parse/types.cpp index 53c79d3b..a07e66f8 100644 --- a/src/parse/types.cpp +++ b/src/parse/types.cpp @@ -71,7 +71,6 @@ TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list) case TOK_RWORD_UNSAFE: case TOK_RWORD_EXTERN: case TOK_RWORD_FN: - // TODO: Handle HRLS in fn types return Parse_Type_Fn(lex, hrls); default: return Parse_Type_Path(lex, hrls, true); @@ -197,7 +196,6 @@ TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list) TypeRef Parse_Type_Fn(TokenStream& lex, ::AST::HigherRankedBounds hrbs) { auto ps = lex.start_span(); - // TODO: HRLs TRACE_FUNCTION; Token tok; @@ -206,11 +204,13 @@ TypeRef Parse_Type_Fn(TokenStream& lex, ::AST::HigherRankedBounds hrbs) GET_TOK(tok, lex); + // `unsafe` if( tok.type() == TOK_RWORD_UNSAFE ) { is_unsafe = true; GET_TOK(tok, lex); } + // `exern` if( tok.type() == TOK_RWORD_EXTERN ) { if( GET_TOK(tok, lex) == TOK_STRING ) { @@ -223,6 +223,7 @@ TypeRef Parse_Type_Fn(TokenStream& lex, ::AST::HigherRankedBounds hrbs) abi = "C"; } } + // `fn` CHECK_TOK(tok, TOK_RWORD_FN); ::std::vector args; @@ -248,6 +249,7 @@ TypeRef Parse_Type_Fn(TokenStream& lex, ::AST::HigherRankedBounds hrbs) } GET_CHECK_TOK(tok, lex, TOK_PAREN_CLOSE); + // `-> RetType` TypeRef ret_type = TypeRef(TypeRef::TagUnit(), Span(tok.get_pos())); if( GET_TOK(tok, lex) == TOK_THINARROW ) { @@ -257,7 +259,7 @@ TypeRef Parse_Type_Fn(TokenStream& lex, ::AST::HigherRankedBounds hrbs) PUTBACK(tok, lex); } - return TypeRef(TypeRef::TagFunction(), lex.end_span(ps), is_unsafe, mv$(abi), mv$(args), is_variadic, mv$(ret_type)); + return TypeRef(TypeRef::TagFunction(), lex.end_span(ps), mv$(hrbs), is_unsafe, mv$(abi), mv$(args), is_variadic, mv$(ret_type)); } TypeRef Parse_Type_Path(TokenStream& lex, ::AST::HigherRankedBounds hrbs, bool allow_trait_list) diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index ceb0e40a..f114fd78 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -1512,10 +1512,12 @@ void Resolve_Absolute_Type(Context& context, TypeRef& type) (Primitive, ), (Function, + context.push( e.info.hrbs ); Resolve_Absolute_Type(context, *e.info.m_rettype); for(auto& t : e.info.m_arg_types) { Resolve_Absolute_Type(context, t); } + context.pop( e.info.hrbs ); ), (Tuple, for(auto& t : e.inner_types) -- cgit v1.2.3 From 8120f62d373b8ef87357fbfe4b2fc210e955a2fb Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 27 May 2018 13:06:19 +0800 Subject: AST - Fix missing handling of `for<>` in a few places --- disabled_tests_run-pass.txt | 8 ++++++-- src/ast/ast.hpp | 8 ++++---- src/hir/from_ast.cpp | 4 ++-- src/parse/root.cpp | 35 +++++++++++++++++++++++++---------- src/resolve/absolute.cpp | 8 +++++--- 5 files changed, 42 insertions(+), 21 deletions(-) (limited to 'src/resolve/absolute.cpp') diff --git a/disabled_tests_run-pass.txt b/disabled_tests_run-pass.txt index 4bf3b2b7..767acc56 100644 --- a/disabled_tests_run-pass.txt +++ b/disabled_tests_run-pass.txt @@ -35,8 +35,6 @@ lex-bare-cr-nondoc-comment # Don't treat \r as a new line # PARSE align-struct # repr(align(2)) - Need to support integers in attributes catch-expr # MISSING: `do catch {}` syntax -issue-37733 # for<'a> in types -issue-39089 # for<'a> in optional traits loop-break-value # TODO: Handle loop labels in expression position. match-range # TODO: Exlusive ranges in patterns paths-in-macro-invocations # TODO: Handle path macros at root. @@ -154,6 +152,10 @@ placement-in-syntax # BUG: Can't find impl in optimise struct-path-associated-type # HUH? `T::A { ... }` struct literal with (bounded) associated const struct-path-self # TODO: Handle `Self` in patterns thread-local-extern-static # TODO: #[no_mangle] +issue-17170 # TODO: #[repr(simd)] +simd-size-align # ^ +simd-type # ^ +simd-upgraded # ^ # TYPECHECK associated-types-doubleendediterator-object # BUG BUG: Validation failed @@ -313,6 +315,8 @@ type-id-higher-rank # BUG: type_id returns the same value for `for<'a> fn(&'a T) type-id-higher-rank-2 # ^ (test failed) unsized3 # BUG: Incorrect dst type annotation for struct containing `str` utf8_idents # BUG: No escaping of utf8 in symbols, GCC doesn't like this +abi-sysv64-arg-passing # ERROR: Empty struct arguments to FFI aren't actually empty +extern-pass-empty # ^ # HIR MISC xcrate-associated-type-defaults # type_is_specialisable - Handle missing type in impl(0x17e3018) ::"xcrate_associated_type_defaults"::Foo for () {}, name = Out diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index 3d7c2dfb..5d0201d0 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -191,7 +191,7 @@ public: class Trait { GenericParams m_params; - ::std::vector< Spanned > m_supertraits; + ::std::vector< Spanned > m_supertraits; bool m_is_marker; bool m_is_unsafe; @@ -201,7 +201,7 @@ public: m_is_marker(false), m_is_unsafe(false) {} - Trait(GenericParams params, ::std::vector< Spanned > supertraits): + Trait(GenericParams params, ::std::vector< Spanned > supertraits): m_params( mv$(params) ), m_supertraits( mv$(supertraits) ), m_is_marker(false), @@ -211,8 +211,8 @@ public: const GenericParams& params() const { return m_params; } GenericParams& params() { return m_params; } - const ::std::vector >& supertraits() const { return m_supertraits; } - ::std::vector >& supertraits() { return m_supertraits; } + const ::std::vector >& supertraits() const { return m_supertraits; } + ::std::vector >& supertraits() { return m_supertraits; } const NamedList& items() const { return m_items; } NamedList& items() { return m_items; } diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index 6353a0ea..4def1a41 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -1108,8 +1108,8 @@ namespace { ::std::string lifetime; ::std::vector< ::HIR::TraitPath> supertraits; for(const auto& st : f.supertraits()) { - if( st.ent.is_valid() ) { - supertraits.push_back( LowerHIR_TraitPath(st.sp, st.ent) ); + if( st.ent.path.is_valid() ) { + supertraits.push_back( LowerHIR_TraitPath(st.sp, st.ent.path) ); } else { lifetime = "static"; diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 0a8d2909..4764c063 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -127,8 +127,12 @@ bool Parse_Publicity(TokenStream& lex, bool allow_restricted=true) ::AST::HigherRankedBounds rv; GET_CHECK_TOK(tok, lex, TOK_LT); do { + // Support empty lists and comma-terminated lists + if( lex.lookahead(0) == TOK_GT ) { + GET_TOK(tok, lex); + break; + } auto attrs = Parse_ItemAttrs(lex); - (void)attrs; // TODO: Attributes on generic params switch(GET_TOK(tok, lex)) { @@ -142,6 +146,18 @@ bool Parse_Publicity(TokenStream& lex, bool allow_restricted=true) CHECK_TOK(tok, TOK_GT); return rv; } +::AST::HigherRankedBounds Parse_HRB_Opt(TokenStream& lex) +{ + if( lex.lookahead(0) == TOK_RWORD_FOR ) + { + lex.getToken(); // Consume + return Parse_HRB(lex); + } + else + { + return ::AST::HigherRankedBounds(); + } +} namespace { AST::LifetimeRef get_LifetimeRef(TokenStream& lex, Token tok) @@ -170,6 +186,8 @@ void Parse_TypeBound(TokenStream& lex, AST::GenericParams& ret, TypeRef checked_ } )); } else if( tok.type() == TOK_QMARK ) { + auto hrbs = Parse_HRB_Opt(lex); + (void)hrbs; // The only valid ?Trait is Sized, which doesn't have any generics ret.add_bound(AST::GenericBound::make_MaybeTrait( { checked_type.clone(), Parse_Path(lex, PATH_GENERIC_TYPE) } )); @@ -623,26 +641,23 @@ AST::Trait Parse_TraitDef(TokenStream& lex, const AST::AttributeList& meta_items } // Trait bounds "trait Trait : 'lifetime + OtherTrait + OtherTrait2" - ::std::vector > supertraits; + ::std::vector > supertraits; if(tok.type() == TOK_COLON) { + // TODO: Just add these as `where Self: ` (would that break typecheck?) do { if( GET_TOK(tok, lex) == TOK_LIFETIME ) { // TODO: Need a better way of indiciating 'static than just an invalid path - // TODO: Ensure that it's 'static - supertraits.push_back( make_spanned( Span(tok.get_pos()), AST::Path() ) ); + ASSERT_BUG(lex.point_span(), tok.str() == "static", "TODO: Support lifetimes other than 'static in trait bounds"); + supertraits.push_back( make_spanned( Span(tok.get_pos()), Type_TraitPath{ {}, AST::Path() } ) ); } else if( tok.type() == TOK_BRACE_OPEN ) { break; } else { PUTBACK(tok, lex); - if( LOOK_AHEAD(lex) == TOK_RWORD_FOR ) - { - GET_TOK(tok, lex); - /*::std::vector< ::std::string> lifetimes =*/ Parse_HRB(lex); - } - supertraits.push_back( GET_SPANNED(::AST::Path, lex, Parse_Path(lex, PATH_GENERIC_TYPE)) ); + auto hrbs = Parse_HRB_Opt(lex); + supertraits.push_back( GET_SPANNED(Type_TraitPath, lex, (Type_TraitPath{ mv$(hrbs), Parse_Path(lex, PATH_GENERIC_TYPE) })) ); } } while( GET_TOK(tok, lex) == TOK_PLUS ); } diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index f114fd78..46a27c2d 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -2055,12 +2055,14 @@ void Resolve_Absolute_Trait(Context& item_context, ::AST::Trait& e) Resolve_Absolute_Generic(item_context, e.params()); for(auto& st : e.supertraits()) { - if( !st.ent.is_valid() ) { + if( !st.ent.path.is_valid() ) { DEBUG("- ST 'static"); } else { - DEBUG("- ST " << st.ent); - Resolve_Absolute_Path(item_context, st.sp, Context::LookupMode::Type, st.ent); + DEBUG("- ST " << st.ent.hrbs << st.ent.path); + item_context.push(st.ent.hrbs); + Resolve_Absolute_Path(item_context, st.sp, Context::LookupMode::Type, st.ent.path); + item_context.pop(st.ent.hrbs); } } -- cgit v1.2.3 From 5098d733af5daa569bfb57b2a9a3c521df9aeb06 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sat, 2 Jun 2018 11:19:44 +0800 Subject: Misc - Clean up/triage some tests --- disabled_tests_run-pass.txt | 6 ++---- src/resolve/absolute.cpp | 3 +++ 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'src/resolve/absolute.cpp') diff --git a/disabled_tests_run-pass.txt b/disabled_tests_run-pass.txt index 62266f00..63cb8fa2 100644 --- a/disabled_tests_run-pass.txt +++ b/disabled_tests_run-pass.txt @@ -92,8 +92,9 @@ static-function-pointer # ^ issue-17718 # ^ (const here, but same idea) issue-27890 # ^ empty-struct-braces # MISSING: Full support for braced initialisers for unit-like structs/variants +issue-22546 # ^ issue-29540 # Errors out on very deep method call list. -issue-34751 # Treats PhantomData as a refutable pattern +issue-34751 # Treats unit-like structs as refutable patterns mir_ascription_coercion # TODO: Either visit expected types, or make a type annotation its own node type type-ascription # ^ @@ -204,9 +205,6 @@ fat-ptr-cast # Array unsize missed issue-20797 # Trait unsize runthrough issue-26905 # Manual CoerceUnsized not working? -# None:: syntax somewhere (TODO) -issue-22546 - # MIR GEN: issue-18352 # append_from_lit - Match literal Borrow issue-11940 # ^ diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index 46a27c2d..f5e0d9e5 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -1839,7 +1839,10 @@ void Resolve_Absolute_Pattern(Context& context, bool allow_refutable, ::AST::Pa ), (Value, if( ! allow_refutable ) + { + // TODO: If this is a single value of a unit-like struct, accept BUG(pat.span(), "Resolve_Absolute_Pattern - Encountered refutable pattern where only irrefutable allowed - " << pat); + } Resolve_Absolute_PatternValue(context, pat.span(), e.start); Resolve_Absolute_PatternValue(context, pat.span(), e.end); ), -- cgit v1.2.3 From 092a639c36b8c64ba5401ffa6c4f1cd4065a0135 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sat, 2 Jun 2018 12:27:16 +0800 Subject: AST - Annotate all patterns with spans --- src/ast/pattern.hpp | 39 ++++---- src/expand/derive.cpp | 228 +++++++++++++++++++++++------------------------ src/expand/mod.cpp | 10 +-- src/parse/pattern.cpp | 80 +++++++++-------- src/parse/root.cpp | 10 ++- src/resolve/absolute.cpp | 6 +- 6 files changed, 194 insertions(+), 179 deletions(-) (limited to 'src/resolve/absolute.cpp') diff --git a/src/ast/pattern.hpp b/src/ast/pattern.hpp index 583ce351..a5e97c40 100644 --- a/src/ast/pattern.hpp +++ b/src/ast/pattern.hpp @@ -106,40 +106,45 @@ public: Pattern(Pattern&&) = default; Pattern& operator=(Pattern&&) = default; - Pattern(Data dat): - m_binding(), + Pattern(Span sp, Data dat): + m_span( mv$(sp) ), m_data( mv$(dat) ) {}; struct TagMaybeBind {}; - Pattern(TagMaybeBind, Ident name): - m_binding(), + Pattern(TagMaybeBind, Span sp, Ident name): + m_span( mv$(sp) ), m_data( Data::make_MaybeBind({ mv$(name) }) ) {} struct TagMacro {}; - Pattern(TagMacro, unique_ptr<::AST::MacroInvocation> inv): + Pattern(TagMacro, Span sp, unique_ptr<::AST::MacroInvocation> inv): + m_span( mv$(sp) ), m_data( Data::make_Macro({ mv$(inv) }) ) {} struct TagBind {}; - Pattern(TagBind, Ident name, PatternBinding::Type ty = PatternBinding::Type::MOVE, bool is_mut=false): + Pattern(TagBind, Span sp, Ident name, PatternBinding::Type ty = PatternBinding::Type::MOVE, bool is_mut=false): + m_span( mv$(sp) ), m_binding( PatternBinding(mv$(name), ty, is_mut) ) {} struct TagBox {}; - Pattern(TagBox, Pattern sub): + Pattern(TagBox, Span sp, Pattern sub): + m_span( mv$(sp) ), m_data( Data::make_Box({ unique_ptr(new Pattern(mv$(sub))) }) ) {} struct TagValue {}; - Pattern(TagValue, Value val, Value end = Value()): + Pattern(TagValue, Span sp, Value val, Value end = Value()): + m_span( mv$(sp) ), m_data( Data::make_Value({ ::std::move(val), ::std::move(end) }) ) {} struct TagReference {}; - Pattern(TagReference, bool is_mutable, Pattern sub_pattern): + Pattern(TagReference, Span sp, bool is_mutable, Pattern sub_pattern): + m_span( mv$(sp) ), m_data( Data::make_Ref( /*Data::Data_Ref */ { is_mutable, unique_ptr(new Pattern(::std::move(sub_pattern))) }) ) @@ -147,23 +152,28 @@ public: } struct TagTuple {}; - Pattern(TagTuple, ::std::vector pats): + Pattern(TagTuple, Span sp, ::std::vector pats): + m_span( mv$(sp) ), m_data( Data::make_Tuple( TuplePat { mv$(pats), false, {} } ) ) {} - Pattern(TagTuple, TuplePat pat): + Pattern(TagTuple, Span sp, TuplePat pat): + m_span( mv$(sp) ), m_data( Data::make_Tuple( mv$(pat) ) ) {} struct TagNamedTuple {}; - Pattern(TagNamedTuple, Path path, ::std::vector pats): + Pattern(TagNamedTuple, Span sp, Path path, ::std::vector pats): + m_span( mv$(sp) ), m_data( Data::make_StructTuple( { mv$(path), TuplePat { mv$(pats), false, {} } } ) ) {} - Pattern(TagNamedTuple, Path path, TuplePat pat = TuplePat { {}, false, {} }): + Pattern(TagNamedTuple, Span sp, Path path, TuplePat pat = TuplePat { {}, false, {} }): + m_span( mv$(sp) ), m_data( Data::make_StructTuple( { ::std::move(path), ::std::move(pat) } ) ) {} struct TagStruct {}; - Pattern(TagStruct, Path path, ::std::vector< ::std::pair< ::std::string,Pattern> > sub_patterns, bool is_exhaustive): + Pattern(TagStruct, Span sp, Path path, ::std::vector< ::std::pair< ::std::string,Pattern> > sub_patterns, bool is_exhaustive): + m_span( mv$(sp) ), m_data( Data::make_Struct( { ::std::move(path), ::std::move(sub_patterns), is_exhaustive } ) ) {} @@ -174,7 +184,6 @@ public: const Span& span() const { return m_span; } - void set_span(Span sp) { m_span = mv$(sp); } Pattern clone() const; diff --git a/src/expand/derive.cpp b/src/expand/derive.cpp index e60c228f..f7c8fabc 100644 --- a/src/expand/derive.cpp +++ b/src/expand/derive.cpp @@ -295,8 +295,8 @@ class Deriver_Debug: ABI_RUST, false, false, false, TypeRef(sp, AST::Path(core_name, {AST::PathNode("fmt",{}), AST::PathNode("Result",{})}) ), vec$( - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ), - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "f"), mv$(f_type) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ), + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "f"), mv$(f_type) ) ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); @@ -392,7 +392,7 @@ public: AST::PathNode("write_str",{}), vec$( NEWNODE(String, v.m_name) ) ); - pat_a = AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(base_path + v.m_name)); + pat_a = AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(base_path + v.m_name)); ), (Tuple, ::std::vector pats_a; @@ -407,7 +407,7 @@ public: for( unsigned int idx = 0; idx < e.m_sub_types.size(); idx ++ ) { auto name_a = FMT("a" << idx); - pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF) ); + pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF) ); node = NEWNODE(CallMethod, mv$(node), AST::PathNode("field",{}), @@ -418,7 +418,7 @@ public: } code = NEWNODE(CallMethod, mv$(node), AST::PathNode("finish",{}), {}); - pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), base_path + v.m_name, mv$(pats_a)); + pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), sp, base_path + v.m_name, mv$(pats_a)); ), (Struct, ::std::vector< ::std::pair > pats_a; @@ -433,7 +433,7 @@ public: for( const auto& fld : e.m_fields ) { auto name_a = FMT("a" << fld.m_name); - pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF)) ); + pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF)) ); node = NEWNODE(CallMethod, mv$(node), AST::PathNode("field",{}), @@ -445,12 +445,12 @@ public: } code = NEWNODE(CallMethod, mv$(node), AST::PathNode("finish",{}), {}); - pat_a = AST::Pattern(AST::Pattern::TagStruct(), base_path + v.m_name, mv$(pats_a), true); + pat_a = AST::Pattern(AST::Pattern::TagStruct(), sp, base_path + v.m_name, mv$(pats_a), true); ) ) ::std::vector< AST::Pattern> pats; - pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); + pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_a)) ); arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), @@ -480,8 +480,8 @@ class Deriver_PartialEq: ABI_RUST, false, false, false, TypeRef(sp, CORETYPE_BOOL), vec$( - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ), - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "v" ), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ), + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "v" ), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ) ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); @@ -550,8 +550,8 @@ public: TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = NEWNODE(Bool, true); - pat_a = AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(base_path + v.m_name)); - pat_b = AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(base_path + v.m_name)); + pat_a = AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(base_path + v.m_name)); + pat_b = AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(base_path + v.m_name)); ), (Tuple, ::std::vector pats_a; @@ -562,8 +562,8 @@ public: { auto name_a = FMT("a" << idx); auto name_b = FMT("b" << idx); - pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF) ); - pats_b.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), name_b, ::AST::PatternBinding::Type::REF) ); + pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF) ); + pats_b.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_b, ::AST::PatternBinding::Type::REF) ); nodes.push_back(this->compare_and_ret(sp, core_name, NEWNODE(NamedValue, AST::Path(name_a)), NEWNODE(NamedValue, AST::Path(name_b)) @@ -571,8 +571,8 @@ public: } nodes.push_back( NEWNODE(Bool, true) ); - pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), base_path + v.m_name, mv$(pats_a)); - pat_b = AST::Pattern(AST::Pattern::TagNamedTuple(), base_path + v.m_name, mv$(pats_b)); + pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), sp, base_path + v.m_name, mv$(pats_a)); + pat_b = AST::Pattern(AST::Pattern::TagNamedTuple(), sp, base_path + v.m_name, mv$(pats_b)); code = NEWNODE(Block, mv$(nodes)); ), (Struct, @@ -584,8 +584,8 @@ public: { auto name_a = FMT("a" << fld.m_name); auto name_b = FMT("b" << fld.m_name); - pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF)) ); - pats_b.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), name_b, ::AST::PatternBinding::Type::REF)) ); + pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF)) ); + pats_b.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_b, ::AST::PatternBinding::Type::REF)) ); nodes.push_back(this->compare_and_ret(sp, core_name, NEWNODE(NamedValue, AST::Path(name_a)), NEWNODE(NamedValue, AST::Path(name_b)) @@ -593,8 +593,8 @@ public: } nodes.push_back( NEWNODE(Bool, true) ); - pat_a = AST::Pattern(AST::Pattern::TagStruct(), base_path + v.m_name, mv$(pats_a), true); - pat_b = AST::Pattern(AST::Pattern::TagStruct(), base_path + v.m_name, mv$(pats_b), true); + pat_a = AST::Pattern(AST::Pattern::TagStruct(), sp, base_path + v.m_name, mv$(pats_a), true); + pat_b = AST::Pattern(AST::Pattern::TagStruct(), sp, base_path + v.m_name, mv$(pats_b), true); code = NEWNODE(Block, mv$(nodes)); ) ) @@ -602,9 +602,9 @@ public: ::std::vector< AST::Pattern> pats; { ::std::vector< AST::Pattern> tuple_pats; - tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); - tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_b)) ); - pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), mv$(tuple_pats)) ); + tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_a)) ); + tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_b)) ); + pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), sp, mv$(tuple_pats)) ); } arms.push_back(AST::ExprNode_Match_Arm( @@ -659,8 +659,8 @@ class Deriver_PartialOrd: ABI_RUST, false, false, false, TypeRef(sp, path_option_ordering), vec$( - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ), - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "v" ), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ), + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "v" ), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ) ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); @@ -683,19 +683,19 @@ class Deriver_PartialOrd: ), ::make_vec3( ::AST::ExprNode_Match_Arm( - ::make_vec1( AST::Pattern(AST::Pattern::TagValue(), this->get_path(core_name, "option", "Option", "None")) ), + ::make_vec1( AST::Pattern(AST::Pattern::TagValue(), sp, this->get_path(core_name, "option", "Option", "None")) ), nullptr, NEWNODE(Flow, AST::ExprNode_Flow::RETURN, "", NEWNODE(NamedValue, this->get_path(core_name, "option", "Option", "None"))) ), ::AST::ExprNode_Match_Arm( - ::make_vec1( AST::Pattern(AST::Pattern::TagNamedTuple(), this->get_path(core_name, "option", "Option", "Some"), - ::make_vec1( AST::Pattern(AST::Pattern::TagValue(), this->get_path(core_name, "cmp", "Ordering", "Equal")) ) + ::make_vec1( AST::Pattern(AST::Pattern::TagNamedTuple(), sp, this->get_path(core_name, "option", "Option", "Some"), + ::make_vec1( AST::Pattern(AST::Pattern::TagValue(), sp, this->get_path(core_name, "cmp", "Ordering", "Equal")) ) ) ), nullptr, NEWNODE(Tuple, ::std::vector()) ), ::AST::ExprNode_Match_Arm( - ::make_vec1( AST::Pattern(AST::Pattern::TagBind(), "res") ), + ::make_vec1( AST::Pattern(AST::Pattern::TagBind(), sp, "res") ), nullptr, NEWNODE(Flow, AST::ExprNode_Flow::RETURN, "", NEWNODE(NamedValue, AST::Path("res"))) ) @@ -758,8 +758,8 @@ public: TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = this->make_ret_equal(core_name); - pat_a = AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(base_path + v.m_name)); - pat_b = AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(base_path + v.m_name)); + pat_a = AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(base_path + v.m_name)); + pat_b = AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(base_path + v.m_name)); ), (Tuple, ::std::vector pats_a; @@ -770,8 +770,8 @@ public: { auto name_a = FMT("a" << idx); auto name_b = FMT("b" << idx); - pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF) ); - pats_b.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), name_b, ::AST::PatternBinding::Type::REF) ); + pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF) ); + pats_b.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_b, ::AST::PatternBinding::Type::REF) ); nodes.push_back(this->make_compare_and_ret( sp, core_name, NEWNODE(Deref, NEWNODE(NamedValue, AST::Path(name_a))), @@ -780,8 +780,8 @@ public: } nodes.push_back( this->make_ret_equal(core_name) ); - pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), base_path + v.m_name, mv$(pats_a)); - pat_b = AST::Pattern(AST::Pattern::TagNamedTuple(), base_path + v.m_name, mv$(pats_b)); + pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), sp, base_path + v.m_name, mv$(pats_a)); + pat_b = AST::Pattern(AST::Pattern::TagNamedTuple(), sp, base_path + v.m_name, mv$(pats_b)); code = NEWNODE(Block, mv$(nodes)); ), (Struct, @@ -793,8 +793,8 @@ public: { auto name_a = FMT("a" << fld.m_name); auto name_b = FMT("b" << fld.m_name); - pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF)) ); - pats_b.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), name_b, ::AST::PatternBinding::Type::REF)) ); + pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF)) ); + pats_b.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_b, ::AST::PatternBinding::Type::REF)) ); nodes.push_back(this->make_compare_and_ret( sp, core_name, NEWNODE(Deref, NEWNODE(NamedValue, AST::Path(name_a))), @@ -803,8 +803,8 @@ public: } nodes.push_back( this->make_ret_equal(core_name) ); - pat_a = AST::Pattern(AST::Pattern::TagStruct(), base_path + v.m_name, mv$(pats_a), true); - pat_b = AST::Pattern(AST::Pattern::TagStruct(), base_path + v.m_name, mv$(pats_b), true); + pat_a = AST::Pattern(AST::Pattern::TagStruct(), sp, base_path + v.m_name, mv$(pats_a), true); + pat_b = AST::Pattern(AST::Pattern::TagStruct(), sp, base_path + v.m_name, mv$(pats_b), true); code = NEWNODE(Block, mv$(nodes)); ) ) @@ -812,9 +812,9 @@ public: ::std::vector< AST::Pattern> pats; { ::std::vector< AST::Pattern> tuple_pats; - tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); - tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_b)) ); - pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), mv$(tuple_pats)) ); + tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_a)) ); + tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_b)) ); + pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), sp, mv$(tuple_pats)) ); } arms.push_back(AST::ExprNode_Match_Arm( @@ -834,32 +834,32 @@ public: continue ; struct H { - static ::AST::Pattern get_pat_nc(const AST::Path& base_path, const AST::EnumVariant& v) { + static ::AST::Pattern get_pat_nc(const Span& sp, const AST::Path& base_path, const AST::EnumVariant& v) { AST::Path var_path = base_path + v.m_name; TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, - return AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(var_path)); + return AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(var_path)); ), (Tuple, - return AST::Pattern(AST::Pattern::TagNamedTuple(), var_path, AST::Pattern::TuplePat { {}, true, {} }); + return AST::Pattern(AST::Pattern::TagNamedTuple(), sp, var_path, AST::Pattern::TuplePat { {}, true, {} }); ), (Struct, - return AST::Pattern(AST::Pattern::TagStruct(), var_path, {}, false); + return AST::Pattern(AST::Pattern::TagStruct(), sp, var_path, {}, false); ) ) throw ""; } }; - ::AST::Pattern pat_a = H::get_pat_nc(base_path, enm.variants()[a]); - ::AST::Pattern pat_b = H::get_pat_nc(base_path, enm.variants()[b]); + ::AST::Pattern pat_a = H::get_pat_nc(sp, base_path, enm.variants()[a]); + ::AST::Pattern pat_b = H::get_pat_nc(sp, base_path, enm.variants()[b]); ::std::vector< AST::Pattern> pats; { ::std::vector< AST::Pattern> tuple_pats; - tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); - tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_b)) ); - pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), mv$(tuple_pats)) ); + tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_a)) ); + tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_b)) ); + pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), sp, mv$(tuple_pats)) ); } auto code = NEWNODE(CallPath, this->get_path(core_name, "option", "Option", "Some"), @@ -903,7 +903,7 @@ class Deriver_Eq: ABI_RUST, false, false, false, TypeRef(TypeRef::TagUnit(), sp), vec$( - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ) ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); @@ -968,7 +968,7 @@ public: TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = NEWNODE(Block); - pat_a = AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(base_path + v.m_name)); + pat_a = AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(base_path + v.m_name)); ), (Tuple, ::std::vector pats_a; @@ -977,11 +977,11 @@ public: for( unsigned int idx = 0; idx < e.m_sub_types.size(); idx ++ ) { auto name_a = FMT("a" << idx); - pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF) ); + pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF) ); nodes.push_back( this->assert_is_eq(assert_method_path, NEWNODE(NamedValue, AST::Path(name_a))) ); } - pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), base_path + v.m_name, mv$(pats_a)); + pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), sp, base_path + v.m_name, mv$(pats_a)); code = NEWNODE(Block, mv$(nodes)); ), (Struct, @@ -991,17 +991,17 @@ public: for( const auto& fld : e.m_fields ) { auto name_a = FMT("a" << fld.m_name); - pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF)) ); + pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF)) ); nodes.push_back( this->assert_is_eq(assert_method_path, NEWNODE(NamedValue, AST::Path(name_a))) ); } - pat_a = AST::Pattern(AST::Pattern::TagStruct(), base_path + v.m_name, mv$(pats_a), true); + pat_a = AST::Pattern(AST::Pattern::TagStruct(), sp, base_path + v.m_name, mv$(pats_a), true); code = NEWNODE(Block, mv$(nodes)); ) ) ::std::vector< AST::Pattern> pats; - pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); + pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_a)) ); arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), @@ -1054,8 +1054,8 @@ class Deriver_Ord: ABI_RUST, false, false, false, TypeRef(sp, path_ordering), vec$( - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ), - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "v"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ), + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "v"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ) ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); @@ -1079,12 +1079,12 @@ class Deriver_Ord: ), ::make_vec2( ::AST::ExprNode_Match_Arm( - ::make_vec1( AST::Pattern(AST::Pattern::TagValue(), this->get_path(core_name, "cmp", "Ordering", "Equal")) ), + ::make_vec1( AST::Pattern(AST::Pattern::TagValue(), sp, this->get_path(core_name, "cmp", "Ordering", "Equal")) ), nullptr, NEWNODE(Tuple, ::std::vector()) ), ::AST::ExprNode_Match_Arm( - ::make_vec1( AST::Pattern(AST::Pattern::TagBind(), "res") ), + ::make_vec1( AST::Pattern(AST::Pattern::TagBind(), sp, "res") ), nullptr, NEWNODE(Flow, AST::ExprNode_Flow::RETURN, "", NEWNODE(NamedValue, AST::Path("res"))) ) @@ -1145,8 +1145,8 @@ public: TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = this->make_ret_equal(core_name); - pat_a = AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(base_path + v.m_name)); - pat_b = AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(base_path + v.m_name)); + pat_a = AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(base_path + v.m_name)); + pat_b = AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(base_path + v.m_name)); ), (Tuple, ::std::vector pats_a; @@ -1157,8 +1157,8 @@ public: { auto name_a = FMT("a" << idx); auto name_b = FMT("b" << idx); - pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF) ); - pats_b.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), name_b, ::AST::PatternBinding::Type::REF) ); + pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF) ); + pats_b.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_b, ::AST::PatternBinding::Type::REF) ); nodes.push_back(this->make_compare_and_ret( sp, core_name, NEWNODE(NamedValue, AST::Path(name_a)), @@ -1167,8 +1167,8 @@ public: } nodes.push_back( this->make_ret_equal(core_name) ); - pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), base_path + v.m_name, mv$(pats_a)); - pat_b = AST::Pattern(AST::Pattern::TagNamedTuple(), base_path + v.m_name, mv$(pats_b)); + pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), sp, base_path + v.m_name, mv$(pats_a)); + pat_b = AST::Pattern(AST::Pattern::TagNamedTuple(), sp, base_path + v.m_name, mv$(pats_b)); code = NEWNODE(Block, mv$(nodes)); ), (Struct, @@ -1180,8 +1180,8 @@ public: { auto name_a = FMT("a" << fld.m_name); auto name_b = FMT("b" << fld.m_name); - pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF)) ); - pats_b.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), name_b, ::AST::PatternBinding::Type::REF)) ); + pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF)) ); + pats_b.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_b, ::AST::PatternBinding::Type::REF)) ); nodes.push_back(this->make_compare_and_ret( sp, core_name, NEWNODE(NamedValue, AST::Path(name_a)), @@ -1190,8 +1190,8 @@ public: } nodes.push_back( this->make_ret_equal(core_name) ); - pat_a = AST::Pattern(AST::Pattern::TagStruct(), base_path + v.m_name, mv$(pats_a), true); - pat_b = AST::Pattern(AST::Pattern::TagStruct(), base_path + v.m_name, mv$(pats_b), true); + pat_a = AST::Pattern(AST::Pattern::TagStruct(), sp, base_path + v.m_name, mv$(pats_a), true); + pat_b = AST::Pattern(AST::Pattern::TagStruct(), sp, base_path + v.m_name, mv$(pats_b), true); code = NEWNODE(Block, mv$(nodes)); ) ) @@ -1199,9 +1199,9 @@ public: ::std::vector< AST::Pattern> pats; { ::std::vector< AST::Pattern> tuple_pats; - tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); - tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_b)) ); - pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), mv$(tuple_pats)) ); + tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_a)) ); + tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_b)) ); + pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), sp, mv$(tuple_pats)) ); } arms.push_back(AST::ExprNode_Match_Arm( @@ -1219,32 +1219,32 @@ public: continue ; struct H { - static ::AST::Pattern get_pat_nc(const AST::Path& base_path, const AST::EnumVariant& v) { + static ::AST::Pattern get_pat_nc(const Span& sp, const AST::Path& base_path, const AST::EnumVariant& v) { AST::Path var_path = base_path + v.m_name; TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, - return AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(var_path)); + return AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(var_path)); ), (Tuple, - return AST::Pattern(AST::Pattern::TagNamedTuple(), var_path, AST::Pattern::TuplePat { {}, true, {} }); + return AST::Pattern(AST::Pattern::TagNamedTuple(), sp, var_path, AST::Pattern::TuplePat { {}, true, {} }); ), (Struct, - return AST::Pattern(AST::Pattern::TagStruct(), var_path, {}, false); + return AST::Pattern(AST::Pattern::TagStruct(), sp, var_path, {}, false); ) ) throw ""; } }; - ::AST::Pattern pat_a = H::get_pat_nc(base_path, enm.variants()[a]); - ::AST::Pattern pat_b = H::get_pat_nc(base_path, enm.variants()[b]); + ::AST::Pattern pat_a = H::get_pat_nc(sp, base_path, enm.variants()[a]); + ::AST::Pattern pat_b = H::get_pat_nc(sp, base_path, enm.variants()[b]); ::std::vector< AST::Pattern> pats; { ::std::vector< AST::Pattern> tuple_pats; - tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); - tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_b)) ); - pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), mv$(tuple_pats)) ); + tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_a)) ); + tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_b)) ); + pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), sp, mv$(tuple_pats)) ); } auto code = NEWNODE(NamedValue, this->get_path(core_name, "cmp", "Ordering", (a < b ? "Less" : "Greater"))); @@ -1287,7 +1287,7 @@ class Deriver_Clone: ABI_RUST, false, false, false, TypeRef(sp, "Self", 0xFFFF), vec$( - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ) ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); @@ -1361,7 +1361,7 @@ public: TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = NEWNODE(NamedValue, base_path + v.m_name); - pat_a = AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(base_path + v.m_name)); + pat_a = AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(base_path + v.m_name)); ), (Tuple, ::std::vector pats_a; @@ -1370,11 +1370,11 @@ public: for( unsigned int idx = 0; idx < e.m_sub_types.size(); idx ++ ) { auto name_a = FMT("a" << idx); - pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF) ); + pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF) ); nodes.push_back( this->clone_val_direct(core_name, NEWNODE(NamedValue, AST::Path(name_a))) ); } - pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), base_path + v.m_name, mv$(pats_a)); + pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), sp, base_path + v.m_name, mv$(pats_a)); code = NEWNODE(CallPath, base_path + v.m_name, mv$(nodes)); ), (Struct, @@ -1384,17 +1384,17 @@ public: for( const auto& fld : e.m_fields ) { auto name_a = FMT("a" << fld.m_name); - pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF)) ); + pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF)) ); vals.push_back({ {}, fld.m_name, this->clone_val_direct(core_name, NEWNODE(NamedValue, AST::Path(name_a))) }); } - pat_a = AST::Pattern(AST::Pattern::TagStruct(), base_path + v.m_name, mv$(pats_a), true); + pat_a = AST::Pattern(AST::Pattern::TagStruct(), sp, base_path + v.m_name, mv$(pats_a), true); code = NEWNODE(StructLiteral, base_path + v.m_name, nullptr, mv$(vals)); ) ) ::std::vector< AST::Pattern> pats; - pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); + pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_a)) ); arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), @@ -1559,8 +1559,8 @@ class Deriver_Hash: ABI_RUST, false, false, false, TypeRef(TypeRef::TagUnit(), sp), vec$( - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ), - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "state"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), true, TypeRef(sp, "H", 0x100|0)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ), + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "state"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), true, TypeRef(sp, "H", 0x100|0)) ) ) ); fcn.params().add_ty_param( AST::TypeParam(sp, {}, "H") ); @@ -1633,7 +1633,7 @@ public: TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = mv$(var_idx_hash); - pat_a = AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(base_path + v.m_name)); + pat_a = AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(base_path + v.m_name)); ), (Tuple, ::std::vector pats_a; @@ -1643,11 +1643,11 @@ public: for( unsigned int idx = 0; idx < e.m_sub_types.size(); idx ++ ) { auto name_a = FMT("a" << idx); - pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF) ); + pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF) ); nodes.push_back( this->hash_val_direct(core_name, NEWNODE(NamedValue, AST::Path(name_a))) ); } - pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), base_path + v.m_name, mv$(pats_a)); + pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), sp, base_path + v.m_name, mv$(pats_a)); code = NEWNODE(Block, mv$(nodes)); ), (Struct, @@ -1658,17 +1658,17 @@ public: for( const auto& fld : e.m_fields ) { auto name_a = FMT("a" << fld.m_name); - pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF)) ); + pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF)) ); nodes.push_back( this->hash_val_direct(core_name, NEWNODE(NamedValue, AST::Path(name_a))) ); } - pat_a = AST::Pattern(AST::Pattern::TagStruct(), base_path + v.m_name, mv$(pats_a), true); + pat_a = AST::Pattern(AST::Pattern::TagStruct(), sp, base_path + v.m_name, mv$(pats_a), true); code = NEWNODE(Block, mv$(nodes)); ) ) ::std::vector< AST::Pattern> pats; - pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); + pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_a)) ); arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), @@ -1712,8 +1712,8 @@ class Deriver_RustcEncodable: ABI_RUST, false, false, false, TypeRef(sp, mv$(result_path)), vec$( - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ), - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "s"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), true, TypeRef(sp, "S", 0x100|0)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "self"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), false, TypeRef(sp, "Self", 0xFFFF)) ), + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "s"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), true, TypeRef(sp, "S", 0x100|0)) ) ) ); fcn.params().add_ty_param( AST::TypeParam(sp, {}, "S") ); @@ -1741,7 +1741,7 @@ class Deriver_RustcEncodable: AST::ExprNodeP enc_closure(Span sp, AST::ExprNodeP code) const { return NEWNODE(Closure, - vec$( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "s"), ::TypeRef(sp) ) ), ::TypeRef(sp), + vec$( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "s"), ::TypeRef(sp) ) ), ::TypeRef(sp), mv$(code), false ); } @@ -1830,7 +1830,7 @@ public: this->enc_closure(sp, this->get_val_ok(core_name)) ) ); - pat_a = AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(base_path + v.m_name)); + pat_a = AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Named(base_path + v.m_name)); ), (Tuple, ::std::vector pats_a; @@ -1839,7 +1839,7 @@ public: for( unsigned int idx = 0; idx < e.m_sub_types.size(); idx ++ ) { auto name_a = FMT("a" << idx); - pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), name_a, ::AST::PatternBinding::Type::REF) ); + pats_a.push_back( ::AST::Pattern(::AST::Pattern::TagBind(), sp, name_a, ::AST::PatternBinding::Type::REF) ); nodes.push_back( NEWNODE(CallPath, this->get_trait_path_Encoder() + "emit_enum_variant_arg", vec$( NEWNODE(NamedValue, AST::Path("s")), @@ -1859,7 +1859,7 @@ public: this->enc_closure(sp, NEWNODE(Block, mv$(nodes))) ) ); - pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), base_path + v.m_name, mv$(pats_a)); + pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), sp, base_path + v.m_name, mv$(pats_a)); ), (Struct, ::std::vector< ::std::pair > pats_a; @@ -1869,7 +1869,7 @@ public: for( const auto& fld : e.m_fields ) { auto name_a = Ident( FMT("a" << fld.m_name) ); - pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), Ident(name_a), ::AST::PatternBinding::Type::REF)) ); + pats_a.push_back( ::std::make_pair(fld.m_name, ::AST::Pattern(::AST::Pattern::TagBind(), sp, Ident(name_a), ::AST::PatternBinding::Type::REF)) ); nodes.push_back( NEWNODE(CallPath, this->get_trait_path_Encoder() + "emit_enum_struct_variant_field", vec$( @@ -1883,7 +1883,7 @@ public: } nodes.push_back( this->get_val_ok(core_name) ); - pat_a = AST::Pattern(AST::Pattern::TagStruct(), base_path + v.m_name, mv$(pats_a), true); + pat_a = AST::Pattern(AST::Pattern::TagStruct(), sp, base_path + v.m_name, mv$(pats_a), true); code = NEWNODE(CallPath, this->get_trait_path_Encoder() + "emit_enum_struct_variant", vec$( NEWNODE(NamedValue, AST::Path("s")), @@ -1897,7 +1897,7 @@ public: ) ::std::vector< AST::Pattern> pats; - pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); + pats.push_back( AST::Pattern(AST::Pattern::TagReference(), sp, false, mv$(pat_a)) ); arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), @@ -1945,8 +1945,8 @@ class Deriver_RustcDecodable: ABI_RUST, false, false, false, TypeRef(sp, result_path), vec$( - //::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, false, AST::LifetimeRef(), TypeRef(sp, "Self", 0xFFFF)) ), - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "d"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), true, TypeRef(sp, "D", 0x100|0)) ) + //::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "self"), TypeRef(TypeRef::TagReference(), sp, false, AST::LifetimeRef(), TypeRef(sp, "Self", 0xFFFF)) ), + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "d"), TypeRef(TypeRef::TagReference(), sp, AST::LifetimeRef(), true, TypeRef(sp, "D", 0x100|0)) ) ) ); fcn.params().add_ty_param( AST::TypeParam(sp, {}, "D") ); @@ -1971,7 +1971,7 @@ class Deriver_RustcDecodable: AST::ExprNodeP dec_closure(Span sp, AST::ExprNodeP code) const { return NEWNODE(Closure, - vec$( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "d"), ::TypeRef(sp) ) ), ::TypeRef(sp), + vec$( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "d"), ::TypeRef(sp) ) ), ::TypeRef(sp), mv$(code), false ); } @@ -2112,7 +2112,7 @@ public: ) ::std::vector< AST::Pattern> pats; - pats.push_back( AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({CORETYPE_UINT, var_idx})) ); + pats.push_back( AST::Pattern(AST::Pattern::TagValue(), sp, AST::Pattern::Value::make_Integer({CORETYPE_UINT, var_idx})) ); arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), @@ -2134,8 +2134,8 @@ public: auto node_match = NEWNODE(Match, NEWNODE(NamedValue, AST::Path("idx")), mv$(arms)); auto node_var_closure = NEWNODE(Closure, vec$( - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "d"), ::TypeRef(sp) ), - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "idx"), ::TypeRef(sp) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "d"), ::TypeRef(sp) ), + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "idx"), ::TypeRef(sp) ) ), ::TypeRef(sp), mv$(node_match), diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index 4470b2bb..8e052dfe 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -588,13 +588,13 @@ struct CExpandExpr: ::std::vector< ::AST::ExprNode_Match_Arm> arms; // - `Some(pattern ) => code` arms.push_back( ::AST::ExprNode_Match_Arm( - ::make_vec1( ::AST::Pattern(::AST::Pattern::TagNamedTuple(), path_Some, ::make_vec1( mv$(node.m_pattern) ) ) ), + ::make_vec1( ::AST::Pattern(::AST::Pattern::TagNamedTuple(), node.span(), path_Some, ::make_vec1( mv$(node.m_pattern) ) ) ), nullptr, mv$(node.m_code) ) ); // - `None => break label` arms.push_back( ::AST::ExprNode_Match_Arm( - ::make_vec1( ::AST::Pattern(::AST::Pattern::TagValue(), ::AST::Pattern::Value::make_Named(path_None)) ), + ::make_vec1( ::AST::Pattern(::AST::Pattern::TagValue(), node.span(), ::AST::Pattern::Value::make_Named(path_None)) ), nullptr, ::AST::ExprNodeP(new ::AST::ExprNode_Flow(::AST::ExprNode_Flow::BREAK, node.m_label, nullptr)) ) ); @@ -605,7 +605,7 @@ struct CExpandExpr: ::make_vec1( mv$(node.m_cond) ) )), ::make_vec1(::AST::ExprNode_Match_Arm( - ::make_vec1( ::AST::Pattern(::AST::Pattern::TagBind(), "it") ), + ::make_vec1( ::AST::Pattern(::AST::Pattern::TagBind(), node.span(), "it") ), nullptr, ::AST::ExprNodeP(new ::AST::ExprNode_Loop( node.m_label, @@ -799,13 +799,13 @@ struct CExpandExpr: ::std::vector< ::AST::ExprNode_Match_Arm> arms; // `Ok(v) => v,` arms.push_back(::AST::ExprNode_Match_Arm( - ::make_vec1( ::AST::Pattern(::AST::Pattern::TagNamedTuple(), path_Ok, ::make_vec1( ::AST::Pattern(::AST::Pattern::TagBind(), "v") )) ), + ::make_vec1( ::AST::Pattern(::AST::Pattern::TagNamedTuple(), node.span(), path_Ok, ::make_vec1( ::AST::Pattern(::AST::Pattern::TagBind(), node.span(), "v") )) ), nullptr, ::AST::ExprNodeP( new ::AST::ExprNode_NamedValue( ::AST::Path(::AST::Path::TagLocal(), "v") ) ) )); // `Err(e) => return Err(From::from(e)),` arms.push_back(::AST::ExprNode_Match_Arm( - ::make_vec1( ::AST::Pattern(::AST::Pattern::TagNamedTuple(), path_Err, ::make_vec1( ::AST::Pattern(::AST::Pattern::TagBind(), "e") )) ), + ::make_vec1( ::AST::Pattern(::AST::Pattern::TagNamedTuple(), node.span(), path_Err, ::make_vec1( ::AST::Pattern(::AST::Pattern::TagBind(), node.span(), "e") )) ), nullptr, ::AST::ExprNodeP(new ::AST::ExprNode_Flow( ::AST::ExprNode_Flow::RETURN, diff --git a/src/parse/pattern.cpp b/src/parse/pattern.cpp index 974ff5fb..f6d61728 100644 --- a/src/parse/pattern.cpp +++ b/src/parse/pattern.cpp @@ -18,9 +18,9 @@ using AST::ExprNode; ::AST::Pattern::TuplePat Parse_PatternTuple(TokenStream& lex, bool is_refutable); AST::Pattern Parse_PatternReal_Slice(TokenStream& lex, bool is_refutable); -AST::Pattern Parse_PatternReal_Path(TokenStream& lex, AST::Path path, bool is_refutable); +AST::Pattern Parse_PatternReal_Path(TokenStream& lex, ProtoSpan ps, AST::Path path, bool is_refutable); AST::Pattern Parse_PatternReal(TokenStream& lex, bool is_refutable); -AST::Pattern Parse_PatternStruct(TokenStream& lex, AST::Path path, bool is_refutable); +AST::Pattern Parse_PatternStruct(TokenStream& lex, ProtoSpan ps, AST::Path path, bool is_refutable); AST::Pattern Parse_PatternReal(TokenStream& lex, bool is_refutable); AST::Pattern Parse_PatternReal1(TokenStream& lex, bool is_refutable); @@ -46,7 +46,7 @@ AST::Pattern Parse_Pattern(TokenStream& lex, bool is_refutable) if( tok.type() == TOK_IDENT && lex.lookahead(0) == TOK_EXCLAM ) { lex.getToken(); - return AST::Pattern( AST::Pattern::TagMacro(), box$(Parse_MacroInvocation(ps, tok.str(), lex))); + return AST::Pattern( AST::Pattern::TagMacro(), lex.end_span(ps), box$(Parse_MacroInvocation(ps, tok.str(), lex))); } if( tok.type() == TOK_INTERPOLATED_PATTERN ) { @@ -92,7 +92,7 @@ AST::Pattern Parse_Pattern(TokenStream& lex, bool is_refutable) if( GET_TOK(tok, lex) != TOK_AT ) { PUTBACK(tok, lex); - return AST::Pattern(AST::Pattern::TagBind(), mv$(bind_name), bind_type, is_mut); + return AST::Pattern(AST::Pattern::TagBind(), lex.end_span(ps), mv$(bind_name), bind_type, is_mut); } binding = AST::PatternBinding( mv$(bind_name), bind_type, is_mut ); @@ -126,11 +126,11 @@ AST::Pattern Parse_Pattern(TokenStream& lex, bool is_refutable) if( is_refutable ) { assert(bind_type == ::AST::PatternBinding::Type::MOVE); assert(is_mut == false); - return AST::Pattern(AST::Pattern::TagMaybeBind(), mv$(name)); + return AST::Pattern(AST::Pattern::TagMaybeBind(), lex.end_span(ps), mv$(name)); } // Otherwise, it IS a binding else { - return AST::Pattern(AST::Pattern::TagBind(), mv$(name), bind_type, is_mut); + return AST::Pattern(AST::Pattern::TagBind(), lex.end_span(ps), mv$(name), bind_type, is_mut); } break;} } @@ -154,6 +154,7 @@ AST::Pattern Parse_PatternReal(TokenStream& lex, bool is_refutable) GET_TOK(tok, lex); return mv$(tok.frag_pattern()); } + auto ps = lex.start_span(); AST::Pattern ret = Parse_PatternReal1(lex, is_refutable); if( GET_TOK(tok, lex) == TOK_TRIPLE_DOT ) { @@ -166,6 +167,7 @@ AST::Pattern Parse_PatternReal(TokenStream& lex, bool is_refutable) throw ParseError::Generic(lex, "Using '...' with a non-value on right"); auto rightval = mv$( right_pat.data().as_Value().start ); ret_v.end = mv$(rightval); + // TODO: use `ps` here? return ret; } @@ -178,6 +180,7 @@ AST::Pattern Parse_PatternReal(TokenStream& lex, bool is_refutable) AST::Pattern Parse_PatternReal1(TokenStream& lex, bool is_refutable) { TRACE_FUNCTION; + auto ps = lex.start_span(); Token tok; AST::Path path; @@ -185,11 +188,11 @@ AST::Pattern Parse_PatternReal1(TokenStream& lex, bool is_refutable) switch( GET_TOK(tok, lex) ) { case TOK_UNDERSCORE: - return AST::Pattern( ); + return AST::Pattern( lex.end_span(ps), AST::Pattern::Data() ); //case TOK_DOUBLE_DOT: // return AST::Pattern( AST::Pattern::TagWildcard() ); case TOK_RWORD_BOX: - return AST::Pattern( AST::Pattern::TagBox(), Parse_Pattern(lex, is_refutable) ); + return AST::Pattern( AST::Pattern::TagBox(), lex.end_span(ps), Parse_Pattern(lex, is_refutable) ); case TOK_DOUBLE_AMP: lex.putback(TOK_AMP); case TOK_AMP: { @@ -200,7 +203,7 @@ AST::Pattern Parse_PatternReal1(TokenStream& lex, bool is_refutable) is_mut = true; else PUTBACK(tok, lex); - return AST::Pattern( AST::Pattern::TagReference(), is_mut, Parse_Pattern(lex, is_refutable) ); + return AST::Pattern( AST::Pattern::TagReference(), lex.end_span(ps), is_mut, Parse_Pattern(lex, is_refutable) ); } case TOK_RWORD_SELF: case TOK_RWORD_SUPER: @@ -209,53 +212,53 @@ AST::Pattern Parse_PatternReal1(TokenStream& lex, bool is_refutable) case TOK_DOUBLE_LT: case TOK_INTERPOLATED_PATH: PUTBACK(tok, lex); - return Parse_PatternReal_Path( lex, Parse_Path(lex, PATH_GENERIC_EXPR), is_refutable ); + return Parse_PatternReal_Path( lex, ps, Parse_Path(lex, PATH_GENERIC_EXPR), is_refutable ); case TOK_DOUBLE_COLON: // 2. Paths are enum/struct names - return Parse_PatternReal_Path( lex, Parse_Path(lex, true, PATH_GENERIC_EXPR), is_refutable ); + return Parse_PatternReal_Path( lex, ps, Parse_Path(lex, true, PATH_GENERIC_EXPR), is_refutable ); case TOK_DASH: if(GET_TOK(tok, lex) == TOK_INTEGER) { auto dt = tok.datatype(); // TODO: Ensure that the type is ANY or a signed integer - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({dt, -tok.intval()}) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Integer({dt, -tok.intval()}) ); } else if( tok.type() == TOK_FLOAT ) { - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Float({tok.datatype(), -tok.floatval()}) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Float({tok.datatype(), -tok.floatval()}) ); } else { throw ParseError::Unexpected(lex, tok, {TOK_INTEGER, TOK_FLOAT}); } case TOK_FLOAT: - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Float({tok.datatype(), tok.floatval()}) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Float({tok.datatype(), tok.floatval()}) ); case TOK_INTEGER: - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({tok.datatype(), tok.intval()}) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Integer({tok.datatype(), tok.intval()}) ); case TOK_RWORD_TRUE: - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({CORETYPE_BOOL, 1}) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Integer({CORETYPE_BOOL, 1}) ); case TOK_RWORD_FALSE: - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({CORETYPE_BOOL, 0}) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Integer({CORETYPE_BOOL, 0}) ); case TOK_STRING: - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_String( mv$(tok.str()) ) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_String( mv$(tok.str()) ) ); case TOK_BYTESTRING: - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_ByteString({ mv$(tok.str()) }) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_ByteString({ mv$(tok.str()) }) ); case TOK_INTERPOLATED_EXPR: { auto e = tok.take_frag_node(); if( auto* n = dynamic_cast(e.get()) ) { - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_String( mv$(n->m_value) ) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_String( mv$(n->m_value) ) ); } //else if( auto* n = dynamic_cast(e.get()) ) { - // return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_ByteString( mv$(n->m_value) ) ); + // return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_ByteString( mv$(n->m_value) ) ); //} else if( auto* n = dynamic_cast(e.get()) ) { - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({CORETYPE_BOOL, n->m_value}) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Integer({CORETYPE_BOOL, n->m_value}) ); } else if( auto* n = dynamic_cast(e.get()) ) { - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({n->m_datatype, n->m_value}) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Integer({n->m_datatype, n->m_value}) ); } else if( auto* n = dynamic_cast(e.get()) ) { - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Float({n->m_datatype, n->m_value}) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Float({n->m_datatype, n->m_value}) ); } else { TODO(lex.point_span(), "Convert :expr into a pattern value - " << *e); @@ -263,32 +266,32 @@ AST::Pattern Parse_PatternReal1(TokenStream& lex, bool is_refutable) } break; case TOK_PAREN_OPEN: - return AST::Pattern( AST::Pattern::TagTuple(), Parse_PatternTuple(lex, is_refutable) ); + return AST::Pattern( AST::Pattern::TagTuple(), lex.end_span(ps), Parse_PatternTuple(lex, is_refutable) ); case TOK_SQUARE_OPEN: return Parse_PatternReal_Slice(lex, is_refutable); default: throw ParseError::Unexpected(lex, tok); } } -AST::Pattern Parse_PatternReal_Path(TokenStream& lex, AST::Path path, bool is_refutable) +AST::Pattern Parse_PatternReal_Path(TokenStream& lex, ProtoSpan ps, AST::Path path, bool is_refutable) { Token tok; switch( GET_TOK(tok, lex) ) { case TOK_PAREN_OPEN: - return AST::Pattern( AST::Pattern::TagNamedTuple(), mv$(path), Parse_PatternTuple(lex, is_refutable) ); + return AST::Pattern( AST::Pattern::TagNamedTuple(), lex.end_span(ps), mv$(path), Parse_PatternTuple(lex, is_refutable) ); case TOK_BRACE_OPEN: - return Parse_PatternStruct(lex, mv$(path), is_refutable); + return Parse_PatternStruct(lex, ps, mv$(path), is_refutable); default: PUTBACK(tok, lex); - return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Named(mv$(path)) ); + return AST::Pattern( AST::Pattern::TagValue(), lex.end_span(ps), AST::Pattern::Value::make_Named(mv$(path)) ); } } AST::Pattern Parse_PatternReal_Slice(TokenStream& lex, bool is_refutable) { - auto sp = lex.start_span(); + auto ps = lex.start_span(); Token tok; ::std::vector< ::AST::Pattern> leading; @@ -320,7 +323,7 @@ AST::Pattern Parse_PatternReal_Slice(TokenStream& lex, bool is_refutable) if( has_binding ) { if(is_split) - ERROR(lex.end_span(sp), E0000, "Multiple instances of .. in a slice pattern"); + ERROR(lex.end_span(ps), E0000, "Multiple instances of .. in a slice pattern"); inner_binding = mv$(binding); is_split = true; @@ -343,13 +346,13 @@ AST::Pattern Parse_PatternReal_Slice(TokenStream& lex, bool is_refutable) if( is_split ) { - return ::AST::Pattern( ::AST::Pattern::Data::make_SplitSlice({ mv$(leading), mv$(inner_binding), mv$(trailing) }) ); + return ::AST::Pattern( lex.end_span(ps), ::AST::Pattern::Data::make_SplitSlice({ mv$(leading), mv$(inner_binding), mv$(trailing) }) ); } else { assert( !inner_binding.is_valid() ); assert( trailing.empty() ); - return ::AST::Pattern( ::AST::Pattern::Data::make_Slice({ mv$(leading) }) ); + return ::AST::Pattern( lex.end_span(ps), ::AST::Pattern::Data::make_Slice({ mv$(leading) }) ); } } @@ -398,7 +401,7 @@ AST::Pattern Parse_PatternReal_Slice(TokenStream& lex, bool is_refutable) return ::AST::Pattern::TuplePat { mv$(leading), true, mv$(trailing) }; } -AST::Pattern Parse_PatternStruct(TokenStream& lex, AST::Path path, bool is_refutable) +AST::Pattern Parse_PatternStruct(TokenStream& lex, ProtoSpan ps, AST::Path path, bool is_refutable) { TRACE_FUNCTION; Token tok; @@ -449,7 +452,7 @@ AST::Pattern Parse_PatternStruct(TokenStream& lex, AST::Path path, bool is_refut i ++; } - return AST::Pattern(AST::Pattern::TagNamedTuple(), mv$(path), AST::Pattern::TuplePat { mv$(leading), has_split, mv$(trailing) }); + return AST::Pattern(AST::Pattern::TagNamedTuple(), lex.end_span(ps), mv$(path), AST::Pattern::TuplePat { mv$(leading), has_split, mv$(trailing) }); } bool is_exhaustive = true; @@ -465,6 +468,7 @@ AST::Pattern Parse_PatternStruct(TokenStream& lex, AST::Path path, bool is_refut break; } + auto inner_ps = lex.start_span(); bool is_short_bind = false; bool is_box = false; auto bind_type = AST::PatternBinding::Type::MOVE; @@ -499,12 +503,12 @@ AST::Pattern Parse_PatternStruct(TokenStream& lex, AST::Path path, bool is_refut AST::Pattern pat; if( is_short_bind || tok.type() != TOK_COLON ) { PUTBACK(tok, lex); - pat = AST::Pattern(); + pat = AST::Pattern(lex.end_span(inner_ps), {}); field_name = field_ident.name; pat.set_bind(mv$(field_ident), bind_type, is_mut); if( is_box ) { - pat = AST::Pattern(AST::Pattern::TagBox(), mv$(pat)); + pat = AST::Pattern(AST::Pattern::TagBox(), lex.end_span(inner_ps), mv$(pat)); } } else { @@ -517,6 +521,6 @@ AST::Pattern Parse_PatternStruct(TokenStream& lex, AST::Path path, bool is_refut } while( GET_TOK(tok, lex) == TOK_COMMA ); CHECK_TOK(tok, TOK_BRACE_CLOSE); - return AST::Pattern(AST::Pattern::TagStruct(), ::std::move(path), ::std::move(subpats), is_exhaustive); + return AST::Pattern(AST::Pattern::TagStruct(), lex.end_span(ps), ::std::move(path), ::std::move(subpats), is_exhaustive); } diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 4764c063..68ad570b 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -385,7 +385,6 @@ AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, bool allow_ lifetime = get_LifetimeRef(lex, mv$(tok)); GET_TOK(tok, lex); } - auto ty_sp = lex.end_span(ps); bool is_mut = false; if( tok.type() == TOK_RWORD_MUT ) @@ -394,7 +393,8 @@ AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, bool allow_ GET_TOK(tok, lex); } CHECK_TOK(tok, TOK_RWORD_SELF); - args.push_back( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), ty_sp, ::std::move(lifetime), is_mut, TypeRef(ty_sp, "Self", 0xFFFF))) ); + auto sp = lex.end_span(ps); + args.push_back( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), sp, "self"), TypeRef(TypeRef::TagReference(), sp, ::std::move(lifetime), is_mut, TypeRef(sp, "Self", 0xFFFF))) ); if( allow_self == false ) throw ParseError::Generic(lex, "Self binding not expected"); @@ -413,6 +413,7 @@ AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, bool allow_ GET_TOK(tok, lex); if( allow_self == false ) throw ParseError::Generic(lex, "Self binding not expected"); + auto binding_sp = lex.end_span(ps); TypeRef ty = TypeRef( lex.point_span(), "Self", 0xFFFF ); if( GET_TOK(tok, lex) == TOK_COLON ) { // Typed mut self @@ -421,7 +422,7 @@ AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, bool allow_ else { PUTBACK(tok, lex); } - args.push_back( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), mv$(ty)) ); + args.push_back( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), binding_sp, "self"), mv$(ty)) ); GET_TOK(tok, lex); } } @@ -430,6 +431,7 @@ AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, bool allow_ // By-value method if( allow_self == false ) throw ParseError::Generic(lex, "Self binding not expected"); + auto binding_sp = lex.end_span(ps); TypeRef ty = TypeRef( lex.point_span(), "Self", 0xFFFF ); if( GET_TOK(tok, lex) == TOK_COLON ) { // Typed mut self @@ -438,7 +440,7 @@ AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, bool allow_ else { PUTBACK(tok, lex); } - args.push_back( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), mv$(ty)) ); + args.push_back( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), binding_sp, "self"), mv$(ty)) ); GET_TOK(tok, lex); } else diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index f5e0d9e5..c51cac72 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -1809,11 +1809,11 @@ void Resolve_Absolute_Pattern(Context& context, bool allow_refutable, ::AST::Pa auto p = context.lookup_opt( name.name, name.hygiene, Context::LookupMode::PatternValue ); if( p.is_valid() ) { Resolve_Absolute_Path(context, pat.span(), Context::LookupMode::PatternValue, p); - pat = ::AST::Pattern(::AST::Pattern::TagValue(), ::AST::Pattern::Value::make_Named(mv$(p))); + pat = ::AST::Pattern(::AST::Pattern::TagValue(), pat.span(), ::AST::Pattern::Value::make_Named(mv$(p))); DEBUG("MaybeBind resolved to " << pat); } else { - pat = ::AST::Pattern(::AST::Pattern::TagBind(), mv$(name)); + pat = ::AST::Pattern(::AST::Pattern::TagBind(), pat.span(), mv$(name)); pat.binding().m_slot = context.push_var( pat.span(), pat.binding().m_name ); DEBUG("- Binding #" << pat.binding().m_slot << " '" << pat.binding().m_name << "' (was MaybeBind)"); } @@ -1821,7 +1821,7 @@ void Resolve_Absolute_Pattern(Context& context, bool allow_refutable, ::AST::Pa else { auto name = mv$( e.name ); - pat = ::AST::Pattern(::AST::Pattern::TagBind(), mv$(name)); + pat = ::AST::Pattern(::AST::Pattern::TagBind(), pat.span(), mv$(name)); pat.binding().m_slot = context.push_var( pat.span(), pat.binding().m_name ); } ), -- cgit v1.2.3