diff options
| author | John Hodge (Mutabah) <acessdev@gmail.com> | 2016-12-25 17:19:27 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-12-25 17:19:27 +1100 |
| commit | 753a2f44cc4c00b952cddae2ceed066ebb18a470 (patch) | |
| tree | ed4bbff4be39c44c57164641f4ed32b5ad4675f0 /src/expand | |
| parent | d12a8a886caf2e0edf33c1af831b1df990d2c892 (diff) | |
| parent | 0c14c734fa32014fd24297ccdbed927016185ffd (diff) | |
| download | mrust-753a2f44cc4c00b952cddae2ceed066ebb18a470.tar.gz | |
Merge pull request #11 from ubsan/whitespace-fix
No more tears!
Diffstat (limited to 'src/expand')
| -rw-r--r-- | src/expand/cfg.cpp | 16 | ||||
| -rw-r--r-- | src/expand/concat.cpp | 8 | ||||
| -rw-r--r-- | src/expand/crate_tags.cpp | 4 | ||||
| -rw-r--r-- | src/expand/derive.cpp | 414 | ||||
| -rw-r--r-- | src/expand/env.cpp | 6 | ||||
| -rw-r--r-- | src/expand/format_args.cpp | 114 | ||||
| -rw-r--r-- | src/expand/include.cpp | 10 | ||||
| -rw-r--r-- | src/expand/lang_item.cpp | 28 | ||||
| -rw-r--r-- | src/expand/macro_rules.cpp | 16 | ||||
| -rw-r--r-- | src/expand/mod.cpp | 112 | ||||
| -rw-r--r-- | src/expand/rustc_diagnostics.cpp | 8 | ||||
| -rw-r--r-- | src/expand/std_prelude.cpp | 8 | ||||
| -rw-r--r-- | src/expand/stringify.cpp | 4 | ||||
| -rw-r--r-- | src/expand/test.cpp | 4 |
14 files changed, 376 insertions, 376 deletions
diff --git a/src/expand/cfg.cpp b/src/expand/cfg.cpp index e59da886..fcd31742 100644 --- a/src/expand/cfg.cpp +++ b/src/expand/cfg.cpp @@ -31,7 +31,7 @@ void Cfg_SetValueCb(::std::string name, ::std::function<bool(const ::std::string } bool check_cfg(Span sp, const ::AST::MetaItem& mi) { - + if( mi.has_sub_items() ) { // Must be `any`/`not`/`all` if( mi.name() == "any" || mi.name() == "cfg" ) { @@ -66,14 +66,14 @@ bool check_cfg(Span sp, const ::AST::MetaItem& mi) { DEBUG(""<<mi.name()<<": '"<<it->second<<"' == '"<<mi.string()<<"'"); return it->second == mi.string(); } - + auto it2 = g_cfg_value_fcns.find(mi.name()); if(it2 != g_cfg_value_fcns.end() ) { DEBUG(""<<mi.name()<<": ('"<<mi.string()<<"')?"); return it2->second( mi.string() ); } - + WARNING(sp, W0000, "Unknown cfg() param '" << mi.name() << "'"); return false; } @@ -93,11 +93,11 @@ class CCfgExpander: if( ident != "" ) { ERROR(sp, E0000, "cfg! doesn't take an identifier"); } - + auto lex = TTStream(tt); auto attrs = Parse_MetaItem(lex); DEBUG("cfg!() - " << attrs); - + if( check_cfg(sp, attrs) ) { return box$( TTStreamO(TokenTree({},TOK_RWORD_TRUE )) ); } @@ -112,8 +112,8 @@ class CCfgHandler: public ExpandDecorator { AttrStage stage() const override { return AttrStage::Pre; } - - + + void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate) const override { DEBUG("#[cfg] crate - " << mi); // Ignore, as #[cfg] on a crate is handled in expand/mod.cpp @@ -150,7 +150,7 @@ class CCfgHandler: impl.type() = ::TypeRef(sp); } } - + void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::StructItem& si) const override { DEBUG("#[cfg] struct item - " << mi); if( !check_cfg(sp, mi) ) { diff --git a/src/expand/concat.cpp b/src/expand/concat.cpp index 7475325a..a08e9168 100644 --- a/src/expand/concat.cpp +++ b/src/expand/concat.cpp @@ -19,18 +19,18 @@ class CConcatExpander: ::std::unique_ptr<TokenStream> expand(const Span& sp, const AST::Crate& crate, const ::std::string& ident, const TokenTree& tt, AST::Module& mod) override { Token tok; - + auto lex = TTStream(tt); if( ident != "" ) ERROR(sp, E0000, "format_args! doesn't take an ident"); - + ::std::string rv; do { if( LOOK_AHEAD(lex) == TOK_EOF ) { GET_TOK(tok, lex); break ; } - + auto v = Parse_Expr0(lex); DEBUG("concat - v=" << *v); Expand_BareExpr(crate, mod, v); @@ -63,7 +63,7 @@ class CConcatExpander: } while( GET_TOK(tok, lex) == TOK_COMMA ); if( tok.type() != TOK_EOF ) throw ParseError::Unexpected(lex, tok, {TOK_COMMA, TOK_EOF}); - + return box$( TTStreamO(TokenTree(Token(TOK_STRING, mv$(rv)))) ); } }; diff --git a/src/expand/crate_tags.cpp b/src/expand/crate_tags.cpp index df6b339b..ca5a98ce 100644 --- a/src/expand/crate_tags.cpp +++ b/src/expand/crate_tags.cpp @@ -13,7 +13,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 { if( crate.m_crate_type != AST::Crate::Type::Unknown ) { //ERROR(sp, E0000, "Multiple #![crate_type] attributes"); @@ -40,7 +40,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 { if( crate.m_crate_name != "" ) { ERROR(sp, E0000, "Multiple #![crate_name] attributes"); diff --git a/src/expand/derive.cpp b/src/expand/derive.cpp index f6021a7d..46457784 100644 --- a/src/expand/derive.cpp +++ b/src/expand/derive.cpp @@ -66,16 +66,16 @@ struct Deriver { virtual AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const = 0; virtual AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const = 0; - - + + AST::GenericParams get_params_with_bounds(const Span& sp, const AST::GenericParams& p, const AST::Path& trait_path, ::std::vector<TypeRef> additional_bounded_types) const { AST::GenericParams params = p.clone(); - + // TODO: Get bounds based on generic (or similar) types used within the type. // - How would this code (that runs before resolve) know what's a generic and what's a local type? // - Searches within the type for a Path that starts with that param. - + unsigned int i = 0; for(const auto& arg : params.ty_params()) { @@ -84,7 +84,7 @@ struct Deriver }) ); i ++; } - + // For each field type // - Locate used generic parameters in the type (and sub-types that directly use said parameter) for(auto& ty : additional_bounded_types) @@ -93,11 +93,11 @@ struct Deriver mv$(ty), {}, trait_path }) ); } - + return params; } - - + + ::std::vector<TypeRef> get_field_bounds(const AST::Struct& str) const { ::std::vector<TypeRef> ret; @@ -140,10 +140,10 @@ struct Deriver ) ) } - + return ret; } - + void add_field_bound_from_ty(const AST::GenericParams& params, ::std::vector<TypeRef>& out_list, const TypeRef& ty) const { struct H { @@ -264,14 +264,14 @@ class Deriver_Debug: //{ // throw CompileError::Todo("derive(Debug) - _try"); //} - + AST::Impl make_ret(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, ::std::vector<TypeRef> types_to_bound, AST::ExprNodeP node) const { const AST::Path debug_trait = AST::Path(core_name, { AST::PathNode("fmt", {}), AST::PathNode("Debug", {}) }); TypeRef f_type(TypeRef::TagReference(), sp, true, TypeRef(sp, AST::Path(core_name, {AST::PathNode("fmt",{}), AST::PathNode("Formatter", {})})) ); - + AST::Function fcn( sp, AST::GenericParams(), @@ -283,19 +283,19 @@ class Deriver_Debug: ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - + 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() ) ); rv.add_function(false, false, "fmt", mv$(fcn)); return mv$(rv); } - + public: AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const override { const ::std::string& name = type.path().nodes().back().name(); - + // Generate code for Debug AST::ExprNodeP node; TU_MATCH(AST::StructData, (str.m_data), (e), @@ -345,20 +345,20 @@ public: node = NEWNODE(CallMethod, mv$(node), AST::PathNode("finish",{}), {}); ) ) - + return this->make_ret(sp, core_name, p, type, this->get_field_bounds(str), mv$(node)); } AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const override { AST::Path base_path = type.m_data.as_Path().path; base_path.nodes().back() = base_path.nodes().back().name(); - + ::std::vector< AST::ExprNode_Match_Arm> arms; for(const auto& v : enm.variants()) { AST::ExprNodeP code; AST::Pattern pat_a; - + TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = NEWNODE(CallMethod, @@ -372,34 +372,34 @@ public: // TODO: Complete this. ::std::vector<AST::Pattern> pats_a; //::std::vector<AST::ExprNodeP> nodes; - + 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) ); //nodes.push_back( this->assert_is_eq(assert_method_path, NEWNODE(NamedValue, AST::Path(name_a))) ); } - + //code = NEWNODE(Block, mv$(nodes)); code = NEWNODE(CallMethod, NEWNODE(NamedValue, AST::Path("f")), AST::PathNode("write_str",{}), vec$( NEWNODE(String, v.m_name + "(...)") ) ); - + pat_a = AST::Pattern(AST::Pattern::TagNamedTuple(), base_path + v.m_name, mv$(pats_a)); ), (Struct, ::std::vector< ::std::pair<std::string, AST::Pattern> > pats_a; //::std::vector<AST::ExprNodeP> nodes; - + 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)) ); //nodes.push_back( this->assert_is_eq(assert_method_path, NEWNODE(NamedValue, AST::Path(name_a))) ); } - + //code = NEWNODE(Block, mv$(nodes) ); code = NEWNODE(CallMethod, NEWNODE(NamedValue, AST::Path("f")), @@ -409,10 +409,10 @@ public: pat_a = AST::Pattern(AST::Pattern::TagStruct(), 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)) ); - + arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), nullptr, @@ -423,7 +423,7 @@ public: NEWNODE(NamedValue, AST::Path("self")), mv$(arms) ); - + return this->make_ret(sp, core_name, p, type, this->get_field_bounds(enm), mv$(node)); } } g_derive_debug; @@ -434,7 +434,7 @@ class Deriver_PartialEq: AST::Impl make_ret(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, ::std::vector<TypeRef> types_to_bound, AST::ExprNodeP node) const { const AST::Path trait_path(core_name, { AST::PathNode("cmp", {}), AST::PathNode("PartialEq", {}) }); - + AST::Function fcn( sp, AST::GenericParams(), @@ -446,9 +446,9 @@ class Deriver_PartialEq: ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - + 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() ) ); rv.add_function(false, false, "eq", mv$(fcn)); return mv$(rv); @@ -465,7 +465,7 @@ public: AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const override { ::std::vector<AST::ExprNodeP> nodes; - + TU_MATCH(AST::StructData, (str.m_data), (e), (Struct, for( const auto& fld : e.ents ) @@ -488,22 +488,22 @@ public: ) ) nodes.push_back( NEWNODE(Bool, true) ); - + return this->make_ret(sp, core_name, p, type, this->get_field_bounds(str), NEWNODE(Block, mv$(nodes))); } - + AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const override { AST::Path base_path = type.m_data.as_Path().path; base_path.nodes().back().args() = ::AST::PathParams(); ::std::vector<AST::ExprNode_Match_Arm> arms; - + for(const auto& v : enm.variants()) { AST::ExprNodeP code; AST::Pattern pat_a; AST::Pattern pat_b; - + TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = NEWNODE(Bool, true); @@ -514,7 +514,7 @@ public: ::std::vector<AST::Pattern> pats_a; ::std::vector<AST::Pattern> pats_b; ::std::vector<AST::ExprNodeP> nodes; - + for( unsigned int idx = 0; idx < e.m_sub_types.size(); idx ++ ) { auto name_a = FMT("a" << idx); @@ -526,7 +526,7 @@ public: NEWNODE(NamedValue, AST::Path(name_b)) )); } - + 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)); @@ -536,7 +536,7 @@ public: ::std::vector< ::std::pair<std::string, AST::Pattern> > pats_a; ::std::vector< ::std::pair<std::string, AST::Pattern> > pats_b; ::std::vector<AST::ExprNodeP> nodes; - + for( const auto& fld : e.m_fields ) { auto name_a = FMT("a" << fld.m_name); @@ -548,14 +548,14 @@ public: NEWNODE(NamedValue, AST::Path(name_b)) )); } - + 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); code = NEWNODE(Block, mv$(nodes)); ) ) - + ::std::vector< AST::Pattern> pats; { ::std::vector< AST::Pattern> tuple_pats; @@ -563,14 +563,14 @@ public: tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_b)) ); pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), mv$(tuple_pats)) ); } - + arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), nullptr, mv$(code) )); } - + // Default arm { arms.push_back(AST::ExprNode_Match_Arm( @@ -593,7 +593,7 @@ public: class Deriver_PartialOrd: public Deriver { - + AST::Path get_path(const ::std::string core_name, ::std::string c1, ::std::string c2) const { return AST::Path(core_name, { AST::PathNode(c1, {}), AST::PathNode(c2, {}) }); @@ -602,15 +602,15 @@ class Deriver_PartialOrd: { return AST::Path(core_name, { AST::PathNode(c1, {}), AST::PathNode(c2, {}), AST::PathNode(c3, {}) }); } - + AST::Impl make_ret(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, ::std::vector<TypeRef> types_to_bound, AST::ExprNodeP node) const { const AST::Path trait_path(core_name, { AST::PathNode("cmp", {}), AST::PathNode("PartialOrd", {}) }); const AST::Path path_ordering(core_name, { AST::PathNode("cmp", {}), AST::PathNode("Ordering", {}) }); - + AST::Path path_option_ordering(core_name, { AST::PathNode("option", {}), AST::PathNode("Option", {}) }); path_option_ordering.nodes().back().args().m_types.push_back( TypeRef(sp, path_ordering) ); - + AST::Function fcn( sp, AST::GenericParams(), @@ -622,14 +622,14 @@ class Deriver_PartialOrd: ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - + 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() ) ); rv.add_function(false, false, "partial_cmp", mv$(fcn)); return mv$(rv); } - + AST::ExprNodeP make_compare_and_ret(Span sp, const ::std::string& core_name, AST::ExprNodeP v1, AST::ExprNodeP v2) const { return NEWNODE(Match, @@ -670,7 +670,7 @@ public: AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const override { ::std::vector<AST::ExprNodeP> nodes; - + TU_MATCH(AST::StructData, (str.m_data), (e), (Struct, for( const auto& fld : e.ents ) @@ -693,22 +693,22 @@ public: ) ) nodes.push_back( this->make_ret_equal(core_name) ); - + return this->make_ret(sp, core_name, p, type, this->get_field_bounds(str), NEWNODE(Block, mv$(nodes))); } - + AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const override { AST::Path base_path = type.m_data.as_Path().path; base_path.nodes().back().args() = ::AST::PathParams(); ::std::vector<AST::ExprNode_Match_Arm> arms; - + for(const auto& v : enm.variants()) { AST::ExprNodeP code; AST::Pattern pat_a; AST::Pattern pat_b; - + TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = this->make_ret_equal(core_name); @@ -719,20 +719,20 @@ public: ::std::vector<AST::Pattern> pats_a; ::std::vector<AST::Pattern> pats_b; ::std::vector<AST::ExprNodeP> nodes; - + for( unsigned int idx = 0; idx < e.m_sub_types.size(); idx ++ ) { 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) ); - + nodes.push_back(this->make_compare_and_ret( sp, core_name, NEWNODE(NamedValue, AST::Path(name_a)), NEWNODE(NamedValue, AST::Path(name_b)) )); } - + 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)); @@ -742,27 +742,27 @@ public: ::std::vector< ::std::pair<std::string, AST::Pattern> > pats_a; ::std::vector< ::std::pair<std::string, AST::Pattern> > pats_b; ::std::vector<AST::ExprNodeP> nodes; - + for( const auto& fld : e.m_fields ) { 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)) ); - + nodes.push_back(this->make_compare_and_ret( sp, core_name, NEWNODE(NamedValue, AST::Path(name_a)), NEWNODE(NamedValue, AST::Path(name_b)) )); } - + 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); code = NEWNODE(Block, mv$(nodes)); ) ) - + ::std::vector< AST::Pattern> pats; { ::std::vector< AST::Pattern> tuple_pats; @@ -770,25 +770,25 @@ public: tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_b)) ); pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), mv$(tuple_pats)) ); } - + arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), nullptr, mv$(code) )); } - + for(unsigned int a = 0; a < enm.variants().size(); a ++ ) { for(unsigned int b = 0; b < enm.variants().size(); b ++ ) { if( a == b ) continue ; - + struct H { static ::AST::Pattern get_pat_nc(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)); @@ -805,7 +805,7 @@ public: }; ::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]); - + ::std::vector< AST::Pattern> pats; { ::std::vector< AST::Pattern> tuple_pats; @@ -813,13 +813,13 @@ public: tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_b)) ); pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), mv$(tuple_pats)) ); } - + auto code = NEWNODE(CallPath, this->get_path(core_name, "option", "Option", "Some"), ::make_vec1( NEWNODE(NamedValue, this->get_path(core_name, "cmp", "Ordering", (a < b ? "Less" : "Greater"))) ) ); - + arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), nullptr, @@ -844,11 +844,11 @@ class Deriver_Eq: AST::Path get_trait_path(const ::std::string& core_name) const { return AST::Path(core_name, { AST::PathNode("cmp", {}), AST::PathNode("Eq", {}) }); } - + AST::Impl make_ret(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, ::std::vector<TypeRef> types_to_bound, AST::ExprNodeP node) const { const AST::Path trait_path = this->get_trait_path(core_name); - + AST::Function fcn( sp, AST::GenericParams(), @@ -859,9 +859,9 @@ class Deriver_Eq: ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - + 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() ) ); rv.add_function(false, false, "assert_receiver_is_total_eq", mv$(fcn)); return mv$(rv); @@ -875,13 +875,13 @@ class Deriver_Eq: AST::ExprNodeP field(const ::std::string& name) const { return NEWNODE(Field, NEWNODE(NamedValue, AST::Path("self")), name); } - + public: AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const override { const AST::Path assert_method_path = this->get_trait_path(core_name) + "assert_receiver_is_total_eq"; ::std::vector<AST::ExprNodeP> nodes; - + TU_MATCH(AST::StructData, (str.m_data), (e), (Struct, for( const auto& fld : e.ents ) @@ -896,10 +896,10 @@ public: } ) ) - + return this->make_ret(sp, core_name, p, type, this->get_field_bounds(str), NEWNODE(Block, mv$(nodes))); } - + AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const override { const AST::Path assert_method_path = this->get_trait_path(core_name) + "assert_receiver_is_total_eq"; @@ -907,12 +907,12 @@ public: AST::Path base_path = type.m_data.as_Path().path; base_path.nodes().back().args() = ::AST::PathParams(); ::std::vector<AST::ExprNode_Match_Arm> arms; - + for(const auto& v : enm.variants()) { AST::ExprNodeP code; AST::Pattern pat_a; - + TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = NEWNODE(Block); @@ -921,36 +921,36 @@ public: (Tuple, ::std::vector<AST::Pattern> pats_a; ::std::vector<AST::ExprNodeP> nodes; - + 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) ); 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)); code = NEWNODE(Block, mv$(nodes)); ), (Struct, ::std::vector< ::std::pair<std::string, AST::Pattern> > pats_a; ::std::vector<AST::ExprNodeP> nodes; - + 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)) ); 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); code = NEWNODE(Block, mv$(nodes)); ) ) - + ::std::vector< AST::Pattern> pats; pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); - + arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), nullptr, @@ -968,7 +968,7 @@ public: class Deriver_Ord: public Deriver { - + AST::Path get_path(const ::std::string core_name, ::std::string c1, ::std::string c2) const { return AST::Path(core_name, { AST::PathNode(c1, {}), AST::PathNode(c2, {}) }); @@ -977,12 +977,12 @@ class Deriver_Ord: { return AST::Path(core_name, { AST::PathNode(c1, {}), AST::PathNode(c2, {}), AST::PathNode(c3, {}) }); } - + AST::Impl make_ret(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, ::std::vector<TypeRef> types_to_bound, AST::ExprNodeP node) const { const AST::Path trait_path(core_name, { AST::PathNode("cmp", {}), AST::PathNode("Ord", {}) }); const AST::Path path_ordering(core_name, { AST::PathNode("cmp", {}), AST::PathNode("Ordering", {}) }); - + AST::Function fcn( sp, AST::GenericParams(), @@ -994,14 +994,14 @@ class Deriver_Ord: ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - + 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() ) ); rv.add_function(false, false, "cmp", mv$(fcn)); return mv$(rv); } - + AST::ExprNodeP make_compare_and_ret(Span sp, const ::std::string& core_name, AST::ExprNodeP v1, AST::ExprNodeP v2) const { return NEWNODE(Match, @@ -1034,7 +1034,7 @@ public: AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const override { ::std::vector<AST::ExprNodeP> nodes; - + TU_MATCH(AST::StructData, (str.m_data), (e), (Struct, for( const auto& fld : e.ents ) @@ -1057,22 +1057,22 @@ public: ) ) nodes.push_back( this->make_ret_equal(core_name) ); - + return this->make_ret(sp, core_name, p, type, this->get_field_bounds(str), NEWNODE(Block, mv$(nodes))); } - + AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const override { AST::Path base_path = type.m_data.as_Path().path; base_path.nodes().back().args() = ::AST::PathParams(); ::std::vector<AST::ExprNode_Match_Arm> arms; - + for(const auto& v : enm.variants()) { AST::ExprNodeP code; AST::Pattern pat_a; AST::Pattern pat_b; - + TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = this->make_ret_equal(core_name); @@ -1083,20 +1083,20 @@ public: ::std::vector<AST::Pattern> pats_a; ::std::vector<AST::Pattern> pats_b; ::std::vector<AST::ExprNodeP> nodes; - + for( unsigned int idx = 0; idx < e.m_sub_types.size(); idx ++ ) { 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) ); - + nodes.push_back(this->make_compare_and_ret( sp, core_name, NEWNODE(NamedValue, AST::Path(name_a)), NEWNODE(NamedValue, AST::Path(name_b)) )); } - + 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)); @@ -1106,27 +1106,27 @@ public: ::std::vector< ::std::pair<std::string, AST::Pattern> > pats_a; ::std::vector< ::std::pair<std::string, AST::Pattern> > pats_b; ::std::vector<AST::ExprNodeP> nodes; - + for( const auto& fld : e.m_fields ) { 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)) ); - + nodes.push_back(this->make_compare_and_ret( sp, core_name, NEWNODE(NamedValue, AST::Path(name_a)), NEWNODE(NamedValue, AST::Path(name_b)) )); } - + 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); code = NEWNODE(Block, mv$(nodes)); ) ) - + ::std::vector< AST::Pattern> pats; { ::std::vector< AST::Pattern> tuple_pats; @@ -1134,25 +1134,25 @@ public: tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_b)) ); pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), mv$(tuple_pats)) ); } - + arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), nullptr, mv$(code) )); } - + for(unsigned int a = 0; a < enm.variants().size(); a ++ ) { for(unsigned int b = 0; b < enm.variants().size(); b ++ ) { if( a == b ) continue ; - + struct H { static ::AST::Pattern get_pat_nc(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)); @@ -1169,7 +1169,7 @@ public: }; ::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]); - + ::std::vector< AST::Pattern> pats; { ::std::vector< AST::Pattern> tuple_pats; @@ -1177,9 +1177,9 @@ public: tuple_pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_b)) ); pats.push_back( AST::Pattern(AST::Pattern::TagTuple(), mv$(tuple_pats)) ); } - + auto code = NEWNODE(NamedValue, this->get_path(core_name, "cmp", "Ordering", (a < b ? "Less" : "Greater"))); - + arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), nullptr, @@ -1207,11 +1207,11 @@ class Deriver_Clone: AST::Path get_method_path(const ::std::string& core_name) const { return get_trait_path(core_name) + "clone"; } - + AST::Impl make_ret(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, ::std::vector<TypeRef> types_to_bound, AST::ExprNodeP node) const { const AST::Path trait_path = this->get_trait_path(core_name); - + AST::Function fcn( sp, AST::GenericParams(), @@ -1222,9 +1222,9 @@ class Deriver_Clone: ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - + 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() ) ); rv.add_function(false, false, "clone", mv$(fcn)); return mv$(rv); @@ -1244,13 +1244,13 @@ class Deriver_Clone: AST::ExprNodeP field(const ::std::string& name) const { return NEWNODE(Field, NEWNODE(NamedValue, AST::Path("self")), name); } - + public: AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const override { const AST::Path& ty_path = type.m_data.as_Path().path; ::std::vector<AST::ExprNodeP> nodes; - + TU_MATCH(AST::StructData, (str.m_data), (e), (Struct, ::std::vector< ::std::pair< ::std::string, AST::ExprNodeP> > vals; @@ -1276,21 +1276,21 @@ public: } ) ) - + return this->make_ret(sp, core_name, p, type, this->get_field_bounds(str), NEWNODE(Block, mv$(nodes))); } - + AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const override { AST::Path base_path = type.m_data.as_Path().path; base_path.nodes().back().args() = ::AST::PathParams(); ::std::vector<AST::ExprNode_Match_Arm> arms; - + for(const auto& v : enm.variants()) { AST::ExprNodeP code; AST::Pattern pat_a; - + TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = NEWNODE(NamedValue, base_path + v.m_name); @@ -1299,36 +1299,36 @@ public: (Tuple, ::std::vector<AST::Pattern> pats_a; ::std::vector<AST::ExprNodeP> nodes; - + 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) ); 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)); code = NEWNODE(CallPath, base_path + v.m_name, mv$(nodes)); ), (Struct, ::std::vector< ::std::pair<std::string, AST::Pattern> > pats_a; ::std::vector< ::std::pair<std::string, AST::ExprNodeP> > vals; - + 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)) ); vals.push_back( ::std::make_pair( 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); 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)) ); - + arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), nullptr, @@ -1349,23 +1349,23 @@ class Deriver_Copy: AST::Path get_trait_path(const ::std::string& core_name) const { return AST::Path(core_name, { AST::PathNode("marker", {}), AST::PathNode("Copy", {}) }); } - + AST::Impl make_ret(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, ::std::vector<TypeRef> types_to_bound, AST::ExprNodeP node) const { const AST::Path trait_path = this->get_trait_path(core_name); - + 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() ) ); return mv$(rv); } - + public: AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const override { return this->make_ret(sp, core_name, p, type, this->get_field_bounds(str), nullptr); } - + AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const override { return this->make_ret(sp, core_name, p, type, this->get_field_bounds(enm), nullptr); @@ -1381,11 +1381,11 @@ class Deriver_Default: AST::Path get_method_path(const ::std::string& core_name) const { return AST::Path(AST::Path::TagUfcs(), ::TypeRef(Span()), get_trait_path(core_name), { AST::PathNode("default", {}) } ); } - + AST::Impl make_ret(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, ::std::vector<TypeRef> types_to_bound, AST::ExprNodeP node) const { const AST::Path trait_path = this->get_trait_path(core_name); - + AST::Function fcn( sp, AST::GenericParams(), @@ -1394,9 +1394,9 @@ class Deriver_Default: {} ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - + 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() ) ); rv.add_function(false, false, "default", mv$(fcn)); return mv$(rv); @@ -1407,13 +1407,13 @@ class Deriver_Default: {} ); } - + public: AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const override { const AST::Path& ty_path = type.m_data.as_Path().path; ::std::vector<AST::ExprNodeP> nodes; - + TU_MATCH(AST::StructData, (str.m_data), (e), (Struct, ::std::vector< ::std::pair< ::std::string, AST::ExprNodeP> > vals; @@ -1439,10 +1439,10 @@ public: } ) ) - + return this->make_ret(sp, core_name, p, type, this->get_field_bounds(str), NEWNODE(Block, mv$(nodes))); } - + AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const override { ERROR(sp, E0000, "Default cannot be derived for enums"); @@ -1461,11 +1461,11 @@ class Deriver_Hash: AST::Path get_method_path(const ::std::string& core_name) const { return get_trait_path(core_name) + "hash"; } - + AST::Impl make_ret(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, ::std::vector<TypeRef> types_to_bound, AST::ExprNodeP node) const { const AST::Path trait_path = this->get_trait_path(core_name); - + AST::Function fcn( sp, AST::GenericParams(), @@ -1483,9 +1483,9 @@ class Deriver_Hash: this->get_trait_path_Hasher(core_name) }) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - + 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() ) ); rv.add_function(false, false, "hash", mv$(fcn)); return mv$(rv); @@ -1502,12 +1502,12 @@ class Deriver_Hash: AST::ExprNodeP field(const ::std::string& name) const { return NEWNODE(Field, NEWNODE(NamedValue, AST::Path("self")), name); } - + public: AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const override { ::std::vector<AST::ExprNodeP> nodes; - + TU_MATCH(AST::StructData, (str.m_data), (e), (Struct, for( const auto& fld : e.ents ) @@ -1522,24 +1522,24 @@ public: } ) ) - + return this->make_ret(sp, core_name, p, type, this->get_field_bounds(str), NEWNODE(Block, mv$(nodes))); } - + AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const override { AST::Path base_path = type.m_data.as_Path().path; base_path.nodes().back().args() = ::AST::PathParams(); ::std::vector<AST::ExprNode_Match_Arm> arms; - + for(unsigned int var_idx = 0; var_idx < enm.variants().size(); var_idx ++) { const auto& v = enm.variants()[var_idx]; AST::ExprNodeP code; AST::Pattern pat_a; - + auto var_idx_hash = this->hash_val_ref( core_name, NEWNODE(Integer, var_idx, CORETYPE_UINT) ); - + TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = mv$(var_idx_hash); @@ -1549,14 +1549,14 @@ public: ::std::vector<AST::Pattern> pats_a; ::std::vector<AST::ExprNodeP> nodes; nodes.push_back( mv$(var_idx_hash) ); - + 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) ); 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)); code = NEWNODE(Block, mv$(nodes)); ), @@ -1564,22 +1564,22 @@ public: ::std::vector< ::std::pair<std::string, AST::Pattern> > pats_a; ::std::vector< AST::ExprNodeP > nodes; nodes.push_back( mv$(var_idx_hash) ); - + 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)) ); 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); code = NEWNODE(Block, mv$(nodes)); ) ) - + ::std::vector< AST::Pattern> pats; pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); - + arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), nullptr, @@ -1607,15 +1607,15 @@ class Deriver_RustcEncodable: AST::Path get_method_path() const { return get_trait_path() + "encode"; } - + AST::Impl make_ret(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, ::std::vector<TypeRef> types_to_bound, AST::ExprNodeP node) const { const AST::Path trait_path = this->get_trait_path(); - + AST::Path result_path = AST::Path(core_name, { AST::PathNode("result", {}), AST::PathNode("Result", {}) }); result_path.nodes()[1].args().m_types.push_back( TypeRef(TypeRef::TagUnit(), sp) ); result_path.nodes()[1].args().m_types.push_back(TypeRef( sp, AST::Path(AST::Path::TagUfcs(), TypeRef(sp, "S", 0x100|0), this->get_trait_path_Encoder(), { AST::PathNode("Error",{}) }) )); - + AST::Function fcn( sp, AST::GenericParams(), @@ -1633,9 +1633,9 @@ class Deriver_RustcEncodable: this->get_trait_path_Encoder() }) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - + 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() ) ); rv.add_function(false, false, "encode", mv$(fcn)); return mv$(rv); @@ -1649,7 +1649,7 @@ class Deriver_RustcEncodable: AST::ExprNodeP field(const ::std::string& name) const { return NEWNODE(Field, NEWNODE(NamedValue, AST::Path("self")), name); } - + 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), @@ -1659,12 +1659,12 @@ class Deriver_RustcEncodable: AST::ExprNodeP get_val_ok(const ::std::string& core_name) const { return NEWNODE(CallPath, AST::Path(core_name, {AST::PathNode("result",{}), AST::PathNode("Result",{}), AST::PathNode("Ok",{})}), vec$( NEWNODE(Tuple, {})) ); } - + public: AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const override { const ::std::string& struct_name = type.m_data.as_Path().path.nodes().back().name(); - + ::std::vector<AST::ExprNodeP> nodes; TU_MATCH(AST::StructData, (str.m_data), (e), (Struct, @@ -1691,7 +1691,7 @@ public: nodes.push_back( this->get_val_ok(core_name) ); auto closure = this->enc_closure( sp, NEWNODE(Block, mv$(nodes)) ); - + ::AST::ExprNodeP node; TU_MATCH(AST::StructData, (str.m_data), (e), (Struct, @@ -1707,22 +1707,22 @@ public: ); ) ) - + return this->make_ret(sp, core_name, p, type, this->get_field_bounds(str), mv$(node)); } - + AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const override { AST::Path base_path = type.m_data.as_Path().path; base_path.nodes().back().args() = ::AST::PathParams(); ::std::vector<AST::ExprNode_Match_Arm> arms; - + for(unsigned int var_idx = 0; var_idx < enm.variants().size(); var_idx ++) { const auto& v = enm.variants()[var_idx]; AST::ExprNodeP code; AST::Pattern pat_a; - + TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = NEWNODE(CallPath, this->get_trait_path_Encoder() + "emit_enum_variant", @@ -1739,7 +1739,7 @@ public: (Tuple, ::std::vector<AST::Pattern> pats_a; ::std::vector<AST::ExprNodeP> nodes; - + for( unsigned int idx = 0; idx < e.m_sub_types.size(); idx ++ ) { auto name_a = FMT("a" << idx); @@ -1753,7 +1753,7 @@ public: ) ); } nodes.push_back( this->get_val_ok(core_name) ); - + code = NEWNODE(CallPath, this->get_trait_path_Encoder() + "emit_enum_variant", vec$( NEWNODE(NamedValue, AST::Path("s")), @@ -1768,13 +1768,13 @@ public: (Struct, ::std::vector< ::std::pair<std::string, AST::Pattern> > pats_a; ::std::vector< AST::ExprNodeP > nodes; - + unsigned int idx = 0; 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)) ); - + nodes.push_back( NEWNODE(CallPath, this->get_trait_path_Encoder() + "emit_enum_struct_variant_field", vec$( NEWNODE(NamedValue, AST::Path("s")), @@ -1786,7 +1786,7 @@ public: idx ++; } 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); code = NEWNODE(CallPath, this->get_trait_path_Encoder() + "emit_enum_struct_variant", vec$( @@ -1799,19 +1799,19 @@ public: ); ) ) - + ::std::vector< AST::Pattern> pats; pats.push_back( AST::Pattern(AST::Pattern::TagReference(), false, mv$(pat_a)) ); - + arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), nullptr, mv$(code) )); } - + auto node_match = NEWNODE(Match, NEWNODE(NamedValue, AST::Path("self")), mv$(arms)); - + const ::std::string& enum_name = type.m_data.as_Path().path.nodes().back().name(); auto node = NEWNODE(CallPath, this->get_trait_path_Encoder() + "emit_enum", vec$( NEWNODE(NamedValue, AST::Path("s")), NEWNODE(String, enum_name), this->enc_closure(sp, mv$(node_match)) ) @@ -1834,15 +1834,15 @@ class Deriver_RustcDecodable: AST::Path get_method_path() const { return get_trait_path() + "decode"; } - + AST::Impl make_ret(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, ::std::vector<TypeRef> types_to_bound, AST::ExprNodeP node) const { const AST::Path trait_path = this->get_trait_path(); - + AST::Path result_path = AST::Path(core_name, { AST::PathNode("result", {}), AST::PathNode("Result", {}) }); result_path.nodes()[1].args().m_types.push_back( TypeRef(sp, "Self", 0xFFFF) ); result_path.nodes()[1].args().m_types.push_back( TypeRef(sp, AST::Path(AST::Path::TagUfcs(), TypeRef(sp, "D", 0x100|0), this->get_trait_path_Decoder(), { AST::PathNode("Error",{}) })) ); - + AST::Function fcn( sp, AST::GenericParams(), @@ -1860,9 +1860,9 @@ class Deriver_RustcDecodable: this->get_trait_path_Decoder() }) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - + 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() ) ); rv.add_function(false, false, "decode", mv$(fcn)); return mv$(rv); @@ -1873,7 +1873,7 @@ class Deriver_RustcDecodable: AST::ExprNodeP field(const ::std::string& name) const { return NEWNODE(Field, NEWNODE(NamedValue, AST::Path("self")), name); } - + 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), @@ -1895,13 +1895,13 @@ class Deriver_RustcDecodable: AST::ExprNodeP get_val_ok_unit(const ::std::string& core_name) const { return get_val_ok(core_name, NEWNODE(Tuple, {})); } - + public: AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Struct& str) const override { AST::Path base_path = type.m_data.as_Path().path; const ::std::string& struct_name = type.m_data.as_Path().path.nodes().back().name(); - + AST::ExprNodeP node_v; TU_MATCH(AST::StructData, (str.m_data), (e), (Struct, @@ -1931,9 +1931,9 @@ public: ) auto closure = this->dec_closure( sp, this->get_val_ok(core_name, mv$(node_v)) ); - + auto args = vec$( NEWNODE(NamedValue, AST::Path("d")), NEWNODE(String, struct_name), AST::ExprNodeP(), mv$(closure) ); - + ::AST::ExprNodeP node; TU_MATCH(AST::StructData, (str.m_data), (e), (Struct, @@ -1947,32 +1947,32 @@ public: node = NEWNODE(CallPath, this->get_trait_path_Decoder() + "read_tuple_struct", mv$(args) ); ) ) - + return this->make_ret(sp, core_name, p, type, this->get_field_bounds(str), mv$(node)); } - + AST::Impl handle_item(Span sp, const ::std::string& core_name, const AST::GenericParams& p, const TypeRef& type, const AST::Enum& enm) const override { AST::Path base_path = type.m_data.as_Path().path; base_path.nodes().back().args() = ::AST::PathParams(); ::std::vector<AST::ExprNode_Match_Arm> arms; - + // 1. Variant names ::std::vector< AST::ExprNodeP> var_name_strs; - + // 2. Decoding arms for(unsigned int var_idx = 0; var_idx < enm.variants().size(); var_idx ++) { const auto& v = enm.variants()[var_idx]; AST::ExprNodeP code; - + TU_MATCH(::AST::EnumVariantData, (v.m_data), (e), (Value, code = NEWNODE(NamedValue, base_path + v.m_name); ), (Tuple, ::std::vector<AST::ExprNodeP> args; - + for( unsigned int idx = 0; idx < e.m_sub_types.size(); idx ++ ) { auto name_a = FMT("a" << idx); @@ -1988,12 +1988,12 @@ public: ), (Struct, ::std::vector< ::std::pair< ::std::string, AST::ExprNodeP > > vals; - + unsigned int idx = 0; for( const auto& fld : e.m_fields ) { auto name_a = FMT("a" << fld.m_name); - + vals.push_back(::std::make_pair(fld.m_name, NEWNODE(UniOp, ::AST::ExprNode_UniOp::QMARK, NEWNODE(CallPath, this->get_trait_path_Decoder() + "read_enum_struct_variant_field", vec$( NEWNODE(NamedValue, AST::Path("d")), @@ -2004,14 +2004,14 @@ public: ) ))); idx ++; } - + code = NEWNODE(StructLiteral, base_path + v.m_name, nullptr, mv$(vals) ); ) ) - + ::std::vector< AST::Pattern> pats; pats.push_back( AST::Pattern(AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({CORETYPE_UINT, var_idx})) ); - + arms.push_back(AST::ExprNode_Match_Arm( mv$(pats), nullptr, @@ -2019,7 +2019,7 @@ public: )); var_name_strs.push_back( NEWNODE(String, v.m_name) ); } - + // Default arm { arms.push_back(AST::ExprNode_Match_Arm( @@ -2028,7 +2028,7 @@ public: this->get_val_err_str(core_name, "enum value unknown") )); } - + auto node_match = NEWNODE(Match, NEWNODE(NamedValue, AST::Path("idx")), mv$(arms)); auto node_var_closure = NEWNODE(Closure, vec$( @@ -2040,7 +2040,7 @@ public: false ); const ::std::string& enum_name = type.m_data.as_Path().path.nodes().back().name(); - + auto node_rev = NEWNODE(CallPath, this->get_trait_path_Decoder() + "read_enum_variant", vec$( NEWNODE(NamedValue, AST::Path("d")), @@ -2048,7 +2048,7 @@ public: mv$( node_var_closure ) ) ); - + auto node = NEWNODE(CallPath, this->get_trait_path_Decoder() + "read_enum", vec$( NEWNODE(NamedValue, AST::Path("d")), NEWNODE(String, enum_name), this->dec_closure(sp, mv$(node_rev)) ) ); @@ -2096,17 +2096,17 @@ static void derive_item(const Span& sp, const AST::Crate& crate, AST::Module& mo //ERROR(sp, E0000, "#[derive()] requires a list of known traits to derive"); return ; } - + DEBUG("path = " << path); bool fail = false; - + const auto& params = item.params(); TypeRef type(sp, path); auto& types_args = type.path().nodes().back().args(); for( const auto& param : params.ty_params() ) { types_args.m_types.push_back( TypeRef(TypeRef::TagArg(), sp, param.name()) ); } - + ::std::vector< ::std::string> missing_handlers; for( const auto& trait : attr.items() ) { @@ -2118,10 +2118,10 @@ static void derive_item(const Span& sp, const AST::Crate& crate, AST::Module& mo fail = true; continue ; } - + mod.add_item(false, "", dp->handle_item(sp, (crate.m_load_std == ::AST::Crate::LOAD_NONE ? "" : "core"), params, type, item), {} ); } - + if( fail ) { ERROR(sp, E0000, "Failed to apply #[derive] - Missing handlers for " << missing_handlers); } diff --git a/src/expand/env.cpp b/src/expand/env.cpp index 8e087858..cad2fa7c 100644 --- a/src/expand/env.cpp +++ b/src/expand/env.cpp @@ -15,7 +15,7 @@ namespace { // Read a string out of the input stream ::std::string get_string(const Span& sp, const AST::Crate& crate, AST::Module& mod, const TokenTree& tt) { auto lex = TTStream(tt); - + auto n = Parse_ExprVal(lex); ASSERT_BUG(sp, n, "No expression returned"); if( lex.lookahead(0) != TOK_EOF ) { @@ -39,7 +39,7 @@ class CExpanderEnv: if( ident != "" ) ERROR(sp, E0000, "env! doesn't take an ident"); ::std::string varname = get_string(sp, crate, mod, tt); - + const char* var_val_cstr = getenv(varname.c_str()); if( !var_val_cstr ) { ERROR(sp, E0000, "Environment variable '" << varname << "' not defined"); @@ -56,7 +56,7 @@ class CExpanderOptionEnv: if( ident != "" ) ERROR(sp, E0000, "option_env! doesn't take an ident"); ::std::string varname = get_string(sp, crate, mod, tt); - + const char* var_val_cstr = getenv(varname.c_str()); if( !var_val_cstr ) { ::std::vector< TokenTree> rv; diff --git a/src/expand/format_args.cpp b/src/expand/format_args.cpp index 9f4eaa93..60c92952 100644 --- a/src/expand/format_args.cpp +++ b/src/expand/format_args.cpp @@ -16,7 +16,7 @@ #include <ast/expr.hpp> // for ExprNode_* namespace { - + /// Options for a formatting fragment struct FmtArgs { @@ -31,20 +31,20 @@ namespace { Plus, Minus, }; - + Align align = Align::Unspec; char align_char = ' '; - + Sign sign = Sign::Unspec; bool alternate = false; bool zero_pad = false; - + bool width_is_arg = false; unsigned int width = 0; - + bool prec_is_arg = false; unsigned int prec = 0; - + bool operator==(const FmtArgs& x) const { return ::std::memcmp(this, &x, sizeof(*this)) == 0; } bool operator!=(const FmtArgs& x) const { #define CMP(f) if(f != x.f) return true @@ -83,25 +83,25 @@ namespace { return os; } }; - + /// A single formatting fragment struct FmtFrag { /// Literal text preceding the fragment ::std::string leading_text; - + /// Argument index used unsigned int arg_index; - + /// Trait to use for formatting const char* trait_name; - + // TODO: Support case where this hasn't been edited (telling the formatter that it has nothing to apply) /// Options FmtArgs args; }; - + class string_view { const char* s; const char* e; @@ -109,16 +109,16 @@ namespace { string_view(const char* s, const char* e): s(s), e(e) {} - + friend ::std::ostream& operator<<(::std::ostream& os, const string_view& x) { for(const char* p = x.s; p != x.e; p++) os << *p; return os; } }; - + /// Parse a format string into a sequence of fragments. - /// + /// /// Returns a list of fragments, and the remaining free text after the last format sequence ::std::tuple< ::std::vector<FmtFrag>, ::std::string> parse_format_string( const Span& sp, @@ -129,10 +129,10 @@ namespace { { unsigned int n_named = named.size(); unsigned int next_free = 0; - + ::std::vector<FmtFrag> frags; ::std::string cur_literal; - + const char* s = format_string.c_str(); for( ; *s; s ++) { @@ -155,17 +155,17 @@ namespace { cur_literal += '{'; continue ; } - + // Debugging: A view of the formatting fragment const char* s2 = s; while(*s2 && *s2 != '}') s2 ++; auto fmt_frag_str = string_view { s, s2 }; - + unsigned int index = ~0u; const char* trait_name; FmtArgs args; - + // Formatting parameter if( *s != ':' && *s != '}' ) { // Parse either an integer or an identifer @@ -197,11 +197,11 @@ namespace { // - If index is ~0u at the end of this block, it's set to the next arg // - This allows {:.*} to format correctly (taking <prec> then <arg>) } - + // If next character is ':', parse extra information if( *s == ':' ) { s ++; // eat ':' - + // Alignment if( s[0] != '\0' && (s[1] == '<' || s[1] == '^' || s[1] == '>') ) { args.align_char = s[0]; @@ -222,7 +222,7 @@ namespace { else { //args.align = FmtArgs::Align::Unspec; } - + // Sign if( *s == '+' ) { args.sign = FmtArgs::Sign::Plus; @@ -235,7 +235,7 @@ namespace { else { args.sign = FmtArgs::Sign::Unspec; } - + if( *s == '#' ) { args.alternate = true; s ++; @@ -243,7 +243,7 @@ namespace { else { //args.alternate = false; } - + if( *s == '0' ) { args.zero_pad = true; s ++; @@ -251,7 +251,7 @@ namespace { else { //args.zero_pad = false; } - + // Padded width if( ::std::isdigit(*s) /*|| *s == '*'*/ ) { unsigned int val = 0; @@ -262,7 +262,7 @@ namespace { s ++; } args.width = val; - + if( *s == '$' ) { args.width_is_arg = true; s ++; @@ -287,7 +287,7 @@ namespace { ERROR(sp, E0000, "Named argument '"<<ident<<"' not found"); args.width = n_free + it->second; args.width_is_arg = true; - + s ++; } else { @@ -317,7 +317,7 @@ namespace { s ++; } args.prec = val; - + if( *s == '$' ) { args.prec_is_arg = true; s ++; @@ -330,13 +330,13 @@ namespace { // Wut? } } - + // Parse ident? // - Lazy way is to just handle a single char and ensure that it is just a single char if( s[0] != '}' && s[0] != '\0' && s[1] != '}' ) { TODO(sp, "Parse formatting fragment at \"" << fmt_frag_str << "\" (long type)"); } - + switch(s[0]) { case '\0': @@ -361,7 +361,7 @@ namespace { // Otherwise, it's just a trivial Display call trait_name = "Display"; } - + // Set index if unspecified if( index == ~0u ) { @@ -371,7 +371,7 @@ namespace { index = next_free + n_named; next_free ++; } - + frags.push_back( FmtFrag { mv$(cur_literal), index, trait_name, @@ -379,7 +379,7 @@ namespace { }); } } - + return ::std::make_tuple( mv$(frags), mv$(cur_literal) ); } } @@ -414,11 +414,11 @@ class CFormatArgsExpander: ::std::unique_ptr<TokenStream> expand(const Span& sp, const ::AST::Crate& crate, const ::std::string& ident, const TokenTree& tt, AST::Module& mod) override { Token tok; - + auto lex = TTStream(tt); if( ident != "" ) ERROR(sp, E0000, "format_args! doesn't take an ident"); - + auto n = Parse_ExprVal(lex); ASSERT_BUG(sp, n, "No expression returned"); Expand_BareExpr(crate, mod, n); @@ -429,11 +429,11 @@ class CFormatArgsExpander: } const auto& format_string_sp = format_string_np->span(); const auto& format_string = format_string_np->m_value; - + ::std::map< ::std::string, unsigned int> named_args_index; ::std::vector<TokenTree> named_args; ::std::vector<TokenTree> free_args; - + // - Parse the arguments while( GET_TOK(tok, lex) == TOK_COMMA ) { @@ -441,17 +441,17 @@ class CFormatArgsExpander: GET_TOK(tok, lex); break; } - + // - Named parameters if( lex.lookahead(0) == TOK_IDENT && lex.lookahead(1) == TOK_EQUAL ) { GET_CHECK_TOK(tok, lex, TOK_IDENT); auto name = mv$(tok.str()); - + GET_CHECK_TOK(tok, lex, TOK_EQUAL); - + auto expr_tt = TokenTree(Token( InterpolatedFragment(InterpolatedFragment::EXPR, Parse_Expr0(lex).release()) )); - + auto ins_rv = named_args_index.insert( ::std::make_pair(mv$(name), named_args.size()) ); if( ins_rv.second == false ) { ERROR(sp, E0000, "Duplicate definition of named argument `" << ins_rv.first->first << "`"); @@ -466,12 +466,12 @@ class CFormatArgsExpander: } } CHECK_TOK(tok, TOK_EOF); - + // - Parse the format string ::std::vector< FmtFrag> fragments; ::std::string tail; ::std::tie( fragments, tail ) = parse_format_string(format_string_sp, format_string, named_args_index, free_args.size()); - + bool is_simple = true; for(unsigned int i = 0; i < fragments.size(); i ++) { @@ -484,7 +484,7 @@ class CFormatArgsExpander: is_simple = false; } } - + ::std::vector<TokenTree> toks; // This should expand to a `match (a, b, c) { (ref _0, ref _1, ref _2) => ... }` to ensure that the values live long enough? // - Also avoids name collisions @@ -513,7 +513,7 @@ class CFormatArgsExpander: toks.push_back( TokenTree(TOK_PAREN_CLOSE) ); toks.push_back( TokenTree(TOK_FATARROW) ); toks.push_back( TokenTree(TOK_BRACE_OPEN) ); - + // Save fragments into a static // `static FRAGMENTS: [&'static str; N] = [...];` // - Contains N+1 entries, where N is the number of fragments @@ -521,7 +521,7 @@ class CFormatArgsExpander: toks.push_back( TokenTree(TOK_RWORD_STATIC) ); toks.push_back( Token(TOK_IDENT, "FRAGMENTS") ); toks.push_back( TokenTree(TOK_COLON) ); - + toks.push_back( TokenTree(TOK_SQUARE_OPEN) ); toks.push_back( Token(TOK_AMP) ); toks.push_back( Token(TOK_LIFETIME, "static") ); @@ -529,9 +529,9 @@ class CFormatArgsExpander: toks.push_back( Token(TOK_SEMICOLON) ); toks.push_back( Token(fragments.size() + 1, CORETYPE_UINT) ); toks.push_back( TokenTree(TOK_SQUARE_CLOSE) ); - + toks.push_back( Token(TOK_EQUAL) ); - + toks.push_back( TokenTree(TOK_SQUARE_OPEN) ); for(const auto& frag : fragments ) { toks.push_back( Token(TOK_STRING, frag.leading_text) ); @@ -539,10 +539,10 @@ class CFormatArgsExpander: } toks.push_back( Token(TOK_STRING, tail) ); toks.push_back( TokenTree(TOK_SQUARE_CLOSE) ); - + toks.push_back( Token(TOK_SEMICOLON) ); } - + if( is_simple ) { // ::fmt::Arguments::new_v1 @@ -553,7 +553,7 @@ class CFormatArgsExpander: toks.push_back( TokenTree(TOK_AMP) ); toks.push_back( Token(TOK_IDENT, "FRAGMENTS") ); toks.push_back( TokenTree(TOK_COMMA) ); - + toks.push_back( TokenTree(TOK_AMP) ); toks.push_back( TokenTree(TOK_SQUARE_OPEN) ); for(const auto& frag : fragments ) @@ -561,9 +561,9 @@ class CFormatArgsExpander: push_path(toks, crate, {"fmt", "ArgumentV1", "new"}); toks.push_back( Token(TOK_PAREN_OPEN) ); toks.push_back( Token(TOK_IDENT, FMT("a" << frag.arg_index)) ); - + toks.push_back( TokenTree(TOK_COMMA) ); - + push_path(toks, crate, {"fmt", frag.trait_name, "fmt"}); toks.push_back( TokenTree(TOK_PAREN_CLOSE) ); toks.push_back( TokenTree(TOK_COMMA) ); @@ -585,9 +585,9 @@ class CFormatArgsExpander: toks.push_back( TokenTree(TOK_AMP) ); toks.push_back( Token(TOK_IDENT, "FRAGMENTS") ); toks.push_back( TokenTree(TOK_COMMA) ); - + // 1. Generate a set of arguments+formatters - + // TODO: Fragments to format // - The format stored by mrustc doesn't quite work with how rustc (and fmt::rt::v1) works toks.push_back( TokenTree(TOK_AMP) ); @@ -599,10 +599,10 @@ class CFormatArgsExpander: // ) toks.push_back( TokenTree(TOK_PAREN_CLOSE) ); } // if(is_simple) else - + toks.push_back( TokenTree(TOK_BRACE_CLOSE) ); toks.push_back( TokenTree(TOK_BRACE_CLOSE) ); - + return box$( TTStreamO(TokenTree(Ident::Hygiene::new_scope(), mv$(toks))) ); } }; diff --git a/src/expand/include.cpp b/src/expand/include.cpp index 83a2aa03..6b1a5508 100644 --- a/src/expand/include.cpp +++ b/src/expand/include.cpp @@ -22,7 +22,7 @@ namespace { return base_path + path; } else { - + auto slash = base_path.find_last_of('/'); if( slash == ::std::string::npos ) { @@ -48,17 +48,17 @@ class CIncludeExpander: { if( ident != "" ) ERROR(sp, E0000, "include! doesn't take an ident"); - + Token tok; auto lex = TTStream(tt); - + // TODO: Parse+expand GET_CHECK_TOK(tok, lex, TOK_STRING); auto path = mv$(tok.str()); GET_CHECK_TOK(tok, lex, TOK_EOF); - + ::std::string file_path = get_path_relative_to(mod.m_file_info.path, mv$(path)); - + return box$( Lexer(file_path) ); } }; diff --git a/src/expand/lang_item.cpp b/src/expand/lang_item.cpp index 1e3564c2..83637bf6 100644 --- a/src/expand/lang_item.cpp +++ b/src/expand/lang_item.cpp @@ -37,10 +37,10 @@ void handle_lang_item(const Span& sp, AST::Crate& crate, const AST::Path& path, else if( name == "mul" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "div" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "rem" ) { DEBUG("Bind '"<<name<<"' to " << path); } - + else if( name == "neg" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "not" ) { DEBUG("Bind '"<<name<<"' to " << path); } - + else if( name == "bitand" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "bitor" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "bitxor" ) { DEBUG("Bind '"<<name<<"' to " << path); } @@ -57,7 +57,7 @@ void handle_lang_item(const Span& sp, AST::Crate& crate, const AST::Path& path, else if( name == "bitxor_assign" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "shl_assign" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "shr_assign" ) { DEBUG("Bind '"<<name<<"' to " << path); } - + else if( name == "index" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "deref" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "index_mut" ) { DEBUG("Bind '"<<name<<"' to " << path); } @@ -65,16 +65,16 @@ void handle_lang_item(const Span& sp, AST::Crate& crate, const AST::Path& path, else if( name == "fn" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "fn_mut" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "fn_once" ) { DEBUG("Bind '"<<name<<"' to " << path); } - + else if( name == "eq" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "ord" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "unsize" ) { DEBUG("Bind '"<<name<<"' to " << path); } else if( name == "coerce_unsized" ) { DEBUG("Bind '"<<name<<"' to " << path); } - + else if( name == "iterator" ) { /* mrustc just desugars? */ } - + else if( name == "debug_trait" ) { /* TODO: Poke derive() with this */ } - + // Structs else if( name == "non_zero" ) { } else if( name == "phantom_data" ) { } @@ -83,12 +83,12 @@ void handle_lang_item(const Span& sp, AST::Crate& crate, const AST::Path& path, else if( name == "range_from" ) { } else if( name == "range_to" ) { } else if( name == "unsafe_cell" ) { } - + // Functions else if( name == "panic" ) { } else if( name == "panic_bounds_check" ) { } else if( name == "panic_fmt" ) { - + } else if( name == "str_eq" ) { } // - builtin `box` support @@ -98,9 +98,9 @@ void handle_lang_item(const Span& sp, AST::Crate& crate, const AST::Path& path, else if( name == "owned_box" ) { } // - start else if( name == "start" ) { } - + else if( name == "eh_personality" ) { } - + else { ERROR(sp, E0000, "Unknown language item '" << name << "'"); } @@ -145,10 +145,10 @@ public: ) ) } - + void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, const AST::Module& mod, AST::ImplDef& impl) const override { const ::std::string& name = mi.string(); - + if( name == "i8" ) {} else if( name == "u8" ) {} else if( name == "i16" ) {} @@ -172,7 +172,7 @@ public: else { ERROR(sp, E0000, "Unknown lang item '" << name << "' on impl"); } - + // TODO: Somehow annotate these impls to allow them to provide inherents? // - mrustc is lazy and inefficient, so these don't matter :) } diff --git a/src/expand/macro_rules.cpp b/src/expand/macro_rules.cpp index 638b9ddd..338edd12 100644 --- a/src/expand/macro_rules.cpp +++ b/src/expand/macro_rules.cpp @@ -26,12 +26,12 @@ class CMacroRulesExpander: { if( ident == "" ) ERROR(sp, E0000, "macro_rules! requires an identifier" ); - + DEBUG("Parsing macro_rules! " << ident); TTStream lex(tt); auto mac = Parse_MacroRules(lex); mod.add_macro( false, ident, mv$(mac) ); - + return ::std::unique_ptr<TokenStream>( new TTStreamO(TokenTree()) ); } }; @@ -40,11 +40,11 @@ class CMacroUseHandler: 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 { TRACE_FUNCTION_F("path=" << path); - + TU_IFLET( ::AST::Item, i, None, e, // Just ignore ) @@ -107,14 +107,14 @@ class CMacroUseHandler: ERROR(sp, E0000, "Use of #[macro_use] on non-module/crate - " << i.tag_str()); } } - + }; class CMacroExportHandler: 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 { if( i.is_None() ) { @@ -125,7 +125,7 @@ class CMacroExportHandler: ERROR(sp, E0000, "#[macro_export] is only valid on macro_rules!"); } const auto& name = mac.input_ident(); - + // Tag the macro in the module for crate export auto it = ::std::find_if( mod.macros().begin(), mod.macros().end(), [&](const auto& x){ return x.name == name; } ); ASSERT_BUG(sp, it != mod.macros().end(), "Macro '" << name << "' not defined in this module"); @@ -150,7 +150,7 @@ class CMacroReexportHandler: const auto& crate_name = i.as_Crate().name; auto& ext_crate = *crate.m_extern_crates.at(crate_name).m_hir; - + if( mi.has_sub_items() ) { for( const auto& si : mi.items() ) diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index ab016b95..a2537218 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -82,7 +82,7 @@ void Expand_Attrs(::AST::MetaItems& attrs, AttrStage stage, ::AST::Crate& crate if( name == "" ) { return ::std::unique_ptr<TokenStream>(); } - + for( const auto& m : g_macros ) { if( name == m.first ) @@ -91,8 +91,8 @@ void Expand_Attrs(::AST::MetaItems& attrs, AttrStage stage, ::AST::Crate& crate return e; } } - - + + // Iterate up the module tree, using the first located macro for(const auto* ll = &modstack; ll; ll = ll->m_prev) { @@ -104,7 +104,7 @@ void Expand_Attrs(::AST::MetaItems& attrs, AttrStage stage, ::AST::Crate& crate { if( input_ident != "" ) ERROR(mi_span, E0000, "macro_rules! macros can't take an ident"); - + auto e = Macro_Invoke(name.c_str(), *mr.data, mv$(input_tt), mod); return e; } @@ -117,13 +117,13 @@ void Expand_Attrs(::AST::MetaItems& attrs, AttrStage stage, ::AST::Crate& crate { if( input_ident != "" ) ERROR(mi_span, E0000, "macro_rules! macros can't take an ident"); - + auto e = Macro_Invoke(name.c_str(), *mri.data, mv$(input_tt), mod); return e; } } } - + // Error - Unknown macro name ERROR(mi_span, E0000, "Unknown macro '" << name << "'"); } @@ -139,7 +139,7 @@ void Expand_Pattern(::AST::Crate& crate, LList<const AST::Module*> modstack, ::A ), (Macro, const auto span = e.inv->span(); - + auto tt = Expand_Macro(crate, modstack, mod, *e.inv); if( ! tt ) { ERROR(span, E0000, "Macro in pattern didn't expand to anything"); @@ -149,13 +149,13 @@ void Expand_Pattern(::AST::Crate& crate, LList<const AST::Module*> modstack, ::A if( LOOK_AHEAD(lex) != TOK_EOF ) { ERROR(span, E0000, "Trailing tokens in macro expansion"); } - + if( pat.binding().is_valid() ) { - if( newpat.binding().is_valid() ) + if( newpat.binding().is_valid() ) ERROR(span, E0000, "Macro expansion provided a binding, but one already present"); newpat.binding() = mv$(pat.binding()); } - + pat = mv$(newpat); ), (Any, @@ -218,7 +218,7 @@ void Expand_Type(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST: if( tt->lookahead(0) != TOK_EOF ) ERROR(e.inv.span(), E0000, "Extra tokens after parsed type"); ty = mv$(new_ty); - + Expand_Type(crate, modstack, mod, ty); ), (Primitive, @@ -263,19 +263,19 @@ struct CExpandExpr: ::AST::Crate& crate; LList<const AST::Module*> modstack; ::std::unique_ptr<::AST::ExprNode> replacement; - + AST::ExprNode_Block* current_block = nullptr; - + CExpandExpr(::AST::Crate& crate, LList<const AST::Module*> ms): crate(crate), modstack(ms) { } - + ::AST::Module& cur_mod() { return *const_cast< ::AST::Module*>(modstack.m_item); } - + void visit(::std::unique_ptr<AST::ExprNode>& cnode) { if(cnode.get()) Expand_Attrs(cnode->attrs(), AttrStage::Pre, [&](const auto& sp, const auto& d, const auto& a){ d.handle(sp, a, this->crate, cnode); }); @@ -292,7 +292,7 @@ struct CExpandExpr: cnode = mv$(this->replacement); } } - + if(cnode.get()) Expand_Attrs(cnode->attrs(), AttrStage::Post, [&](const auto& sp, const auto& d, const auto& a){ d.handle(sp, a, this->crate, cnode); }); assert( ! this->replacement ); @@ -320,14 +320,14 @@ struct CExpandExpr: } } } - + void visit(::AST::ExprNode_Macro& node) override { TRACE_FUNCTION_F("ExprNode_Macro - name = " << node.m_name); if( node.m_name == "" ) { return ; } - + auto& mod = this->cur_mod(); auto ttl = Expand_Macro( crate, modstack, mod, @@ -365,7 +365,7 @@ struct CExpandExpr: node.m_name = ""; } } - + void visit(::AST::ExprNode_Block& node) override { unsigned int mod_item_count = 0; // TODO: macro_rules! invocations within the expression list influence this. @@ -374,7 +374,7 @@ struct CExpandExpr: Expand_Mod(crate, modstack, node.m_local_mod->path(), *node.m_local_mod); mod_item_count = node.m_local_mod->items().size(); } - + auto saved = this->current_block; this->current_block = &node; this->visit_vector(node.m_nodes); @@ -444,7 +444,7 @@ struct CExpandExpr: nullptr, ::AST::ExprNodeP(new ::AST::ExprNode_Flow(::AST::ExprNode_Flow::BREAK, node.m_label, nullptr)) ) ); - + replacement.reset(new ::AST::ExprNode_Match( ::AST::ExprNodeP(new ::AST::ExprNode_CallPath( ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(node.span()), path_IntoIterator, { ::AST::PathNode("into_iter") } ), @@ -563,7 +563,7 @@ struct CExpandExpr: auto path_Err = ::AST::Path(core_crate, {::AST::PathNode("result"), ::AST::PathNode("Result"), ::AST::PathNode("Err")}); auto path_From = ::AST::Path(core_crate, {::AST::PathNode("convert"), ::AST::PathNode("From")}); path_From.nodes().back().args().m_types.push_back( ::TypeRef(node.span()) ); - + // Desugars into // ``` // match `m_value` { @@ -571,7 +571,7 @@ struct CExpandExpr: // Err(e) => return Err(From::from(e)), // } // ``` - + ::std::vector< ::AST::ExprNode_Match_Arm> arms; // `Ok(v) => v,` arms.push_back(::AST::ExprNode_Match_Arm( @@ -637,23 +637,23 @@ void Expand_Impl(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST: DEBUG("Deleted"); return ; } - + Expand_Type(crate, modstack, mod, impl.def().type()); //Expand_Type(crate, modstack, mod, impl.def().trait()); - + DEBUG("> Items"); for( unsigned int idx = 0; idx < impl.items().size(); idx ++ ) { auto& i = impl.items()[idx]; DEBUG(" - " << i.name << " :: " << i.data->attrs); - + // TODO: Make a path from the impl definition? Requires having the impl def resolved to be correct // - Does it? the namespace is essentially the same. There may be issues with wherever the path is used though //::AST::Path path = modpath + i.name; - + auto attrs = mv$(i.data->attrs); Expand_Attrs(attrs, AttrStage::Pre, crate, AST::Path(), mod, *i.data); - + TU_MATCH_DEF(AST::Item, (*i.data), (e), ( throw ::std::runtime_error("BUG: Unknown item type in impl block"); @@ -665,7 +665,7 @@ void Expand_Impl(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST: TRACE_FUNCTION_F("Macro invoke " << e.name()); // Move out of the module to avoid invalidation if a new macro invocation is added auto mi_owned = mv$(e); - + auto ttl = Expand_Macro(crate, modstack, mod, mi_owned); if( ttl.get() ) @@ -697,7 +697,7 @@ void Expand_Impl(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST: Expand_Type(crate, modstack, mod, e.type()); ) ) - + // Run post-expansion decorators and restore attributes { auto& i = impl.items()[idx]; @@ -717,7 +717,7 @@ void Expand_ImplDef(::AST::Crate& crate, LList<const AST::Module*> modstack, ::A DEBUG("Deleted"); return ; } - + Expand_Type(crate, modstack, mod, impl_def.type()); //Expand_Type(crate, modstack, mod, impl_def.trait()); @@ -727,10 +727,10 @@ void Expand_ImplDef(::AST::Crate& crate, LList<const AST::Module*> modstack, ::A void Expand_Mod(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST::Path modpath, ::AST::Module& mod, unsigned int first_item) { TRACE_FUNCTION_F("modpath = " << modpath); - + for( const auto& mi: mod.macro_imports_res() ) DEBUG("- Imports '" << mi.name << "'"); - + // Insert prelude if: Enabled for this module, present for the crate, and this module is not an anon if( crate.m_prelude_path != AST::Path() ) { @@ -738,20 +738,20 @@ void Expand_Mod(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST:: mod.add_alias(false, ::AST::UseStmt(Span(), crate.m_prelude_path), "", {}); } } - + DEBUG("Items"); for( unsigned int idx = first_item; idx < mod.items().size(); idx ++ ) { auto& i = mod.items()[idx]; - + DEBUG("- " << i.name << " (" << ::AST::Item::tag_to_str(i.data.tag()) << ") :: " << i.data.attrs); ::AST::Path path = modpath + i.name; - + auto attrs = mv$(i.data.attrs); Expand_Attrs(attrs, AttrStage::Pre, crate, path, mod, i.data); - + auto dat = mv$(i.data); - + TU_MATCH(::AST::Item, (dat), (e), (None, // Skip, nothing @@ -759,9 +759,9 @@ void Expand_Mod(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST:: (MacroInv, // Move out of the module to avoid invalidation if a new macro invocation is added auto mi_owned = mv$(e); - + TRACE_FUNCTION_F("Macro invoke " << mi_owned.name()); - + auto ttl = Expand_Macro(crate, modstack, mod, mi_owned); assert( mi_owned.name() != ""); @@ -805,7 +805,7 @@ void Expand_Mod(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST:: (Crate, // Can't recurse into an `extern crate` ), - + (Struct, TU_MATCH(AST::StructData, (e.m_data), (sd), (Struct, @@ -883,7 +883,7 @@ void Expand_Mod(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST:: { auto attrs = mv$(ti.data.attrs); Expand_Attrs(attrs, AttrStage::Pre, crate, AST::Path(), mod, ti.data); - + TU_MATCH_DEF(AST::Item, (ti.data), (e), ( throw ::std::runtime_error("BUG: Unknown item type in impl block"); @@ -905,7 +905,7 @@ void Expand_Mod(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST:: Expand_Type(crate, modstack, mod, e.type()); ) ) - + Expand_Attrs(attrs, AttrStage::Post, crate, AST::Path(), mod, ti.data); if( ti.data.attrs.m_items.size() == 0 ) ti.data.attrs = mv$(attrs); @@ -914,7 +914,7 @@ void Expand_Mod(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST:: (Type, Expand_Type(crate, modstack, mod, e.type()); ), - + (Function, for(auto& arg : e.args()) { Expand_Pattern(crate, modstack, mod, arg.first, false); @@ -929,9 +929,9 @@ void Expand_Mod(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST:: ) ) Expand_Attrs(attrs, AttrStage::Post, crate, path, mod, dat); - + { - + auto& i = mod.items()[idx]; if( i.data.tag() == ::AST::Item::TAGDEAD ) { i.data = mv$(dat); @@ -941,26 +941,26 @@ void Expand_Mod(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST:: i.data.attrs = mv$(attrs); } } - + // IGNORE m_anon_modules, handled as part of expressions - + //for( const auto& mi: mod.macro_imports_res() ) // DEBUG("- Imports '" << mi.name << "'"); } void Expand_Mod_IndexAnon(::AST::Crate& crate, ::AST::Module& mod) { TRACE_FUNCTION_F("mod=" << mod.path()); - + for(auto& i : mod.items()) { DEBUG("- " << i.data.tag_str() << " '" << i.name << "'"); TU_IFLET(::AST::Item, (i.data), Module, e, Expand_Mod_IndexAnon(crate, e); - + // TODO: Also ensure that all #[macro_export] macros end up in parent ) } - + for( auto& mp : mod.anon_mods() ) { if( mp.unique() ) { @@ -975,10 +975,10 @@ void Expand_Mod_IndexAnon(::AST::Crate& crate, ::AST::Module& mod) void Expand(::AST::Crate& crate) { auto modstack = LList<const ::AST::Module*>(nullptr, &crate.m_root_module); - + // 1. Crate attributes Expand_Attrs(crate.m_attrs, AttrStage::Pre, [&](const auto& sp, const auto& d, const auto& a){ d.handle(sp, a, crate); }); - + // Insert magic for libstd/libcore // NOTE: The actual crates are loaded in "LoadCrates" using magic in AST::Crate::load_externs switch( crate.m_load_std ) @@ -1002,7 +1002,7 @@ void Expand(::AST::Crate& crate) case ::AST::Crate::LOAD_NONE: break; } - + // 2. Module attributes for( auto& a : crate.m_attrs.m_items ) { @@ -1012,10 +1012,10 @@ void Expand(::AST::Crate& crate) } } } - + // 3. Module tree Expand_Mod(crate, modstack, ::AST::Path("",{}), crate.m_root_module); - + // Post-process Expand_Mod_IndexAnon(crate, crate.m_root_module); #if 0 diff --git a/src/expand/rustc_diagnostics.cpp b/src/expand/rustc_diagnostics.cpp index 917a5e4d..f2e436c4 100644 --- a/src/expand/rustc_diagnostics.cpp +++ b/src/expand/rustc_diagnostics.cpp @@ -34,16 +34,16 @@ class CExpanderBuildDiagnosticArray: if( ident != "" ) ERROR(sp, E0000, "__build_diagnostic_array! doesn't take an ident"); auto lex = TTStream(tt); - + Token tok; - + GET_CHECK_TOK(tok, lex, TOK_IDENT); //auto crate_name = mv$(tok.str()); GET_CHECK_TOK(tok, lex, TOK_COMMA); GET_CHECK_TOK(tok, lex, TOK_IDENT); auto item_name = mv$(tok.str()); GET_CHECK_TOK(tok, lex, TOK_EOF); - + ::std::vector<TokenTree> toks; toks.push_back( TOK_RWORD_STATIC ); toks.push_back( Token(TOK_IDENT, item_name) ); @@ -63,7 +63,7 @@ class CExpanderBuildDiagnosticArray: toks.push_back( TOK_SQUARE_OPEN ); toks.push_back( TOK_SQUARE_CLOSE ); toks.push_back( TOK_SEMICOLON ); - + return box$( TTStreamO(TokenTree( lex.getHygiene(), mv$(toks) )) ); } }; diff --git a/src/expand/std_prelude.cpp b/src/expand/std_prelude.cpp index 81eecf7f..16c4ce90 100644 --- a/src/expand/std_prelude.cpp +++ b/src/expand/std_prelude.cpp @@ -8,7 +8,7 @@ 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 { if( crate.m_load_std != AST::Crate::LOAD_STD ) { ERROR(sp, E0000, "Invalid use of #![no_std] with itself or #![no_core]"); @@ -21,7 +21,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 { if( crate.m_load_std != AST::Crate::LOAD_STD ) { ERROR(sp, E0000, "Invalid use of #![no_core] with itself or #![no_std]"); @@ -41,7 +41,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 { if( i.is_Module() ) { i.as_Module().m_insert_prelude = false; @@ -57,7 +57,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 { if( i.is_Use() ) { const auto& p = i.as_Use().path; diff --git a/src/expand/stringify.cpp b/src/expand/stringify.cpp index ce16847b..f43d896f 100644 --- a/src/expand/stringify.cpp +++ b/src/expand/stringify.cpp @@ -16,14 +16,14 @@ class CExpander: { Token tok; ::std::string rv; - + auto lex = TTStream(tt); while( GET_TOK(tok, lex) != TOK_EOF ) { rv += tok.to_str(); rv += " "; } - + return box$( TTStreamO(TokenTree(Token(TOK_STRING, mv$(rv)))) ); } }; diff --git a/src/expand/test.cpp b/src/expand/test.cpp index ba18fd24..fba6556f 100644 --- a/src/expand/test.cpp +++ b/src/expand/test.cpp @@ -12,12 +12,12 @@ class CTestHandler: public ExpandDecorator { 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 { if( ! i.is_Function() ) { ERROR(sp, E0000, "#[test] can only be put on functions - found on " << i.tag_str()); } - + // TODO: Proper #[test] support, for now just remove them i = AST::Item::make_None({}); } |
