From 2bf4e8bfeb83f135f9429256e301673c1a63ce09 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sun, 23 Oct 2016 15:26:53 +0800 Subject: AST - Reduce places where there's no span information provided --- src/ast/ast.cpp | 12 ++++---- src/ast/ast.hpp | 23 ++++++--------- src/ast/expr.hpp | 27 +---------------- src/ast/generics.hpp | 36 ++++++++++------------- src/ast/types.hpp | 24 ++++++--------- src/expand/cfg.cpp | 4 +-- src/expand/derive.cpp | 56 +++++++++++++++++------------------ src/expand/mod.cpp | 8 ++--- src/parse/expr.cpp | 6 ++-- src/parse/root.cpp | 76 ++++++++++++++++++++++++------------------------ src/resolve/absolute.cpp | 12 ++++---- 11 files changed, 121 insertions(+), 163 deletions(-) (limited to 'src') diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 08aad21b..144842db 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -77,7 +77,7 @@ TupleItem TupleItem::clone() const TypeAlias TypeAlias::clone() const { - return TypeAlias( m_params, m_type ); + return TypeAlias( m_params.clone(), m_type ); } Static Static::clone() const { @@ -101,7 +101,7 @@ Function Function::clone() const for(const auto& arg : m_args) new_args.push_back( ::std::make_pair( arg.first.clone(), arg.second ) ); - auto rv = Function( m_span, m_params, m_abi, m_is_unsafe, m_is_const, m_is_variadic, m_rettype, mv$(new_args) ); + auto rv = Function( m_span, m_params.clone(), m_abi, m_is_unsafe, m_is_const, m_is_variadic, m_rettype, mv$(new_args) ); if( m_code.is_valid() ) { rv.m_code = AST::Expr( m_code.node().clone() ); @@ -139,7 +139,7 @@ bool Trait::has_named_item(const ::std::string& name, bool& out_is_fcn) const Trait Trait::clone() const { - auto rv = Trait(m_params, m_supertraits); + auto rv = Trait(m_params.clone(), m_supertraits); for(const auto& item : m_items) { rv.m_items.push_back( Named { item.name, item.data.clone(), item.is_pub } ); @@ -167,7 +167,7 @@ Enum Enum::clone() const ) ) } - return Enum(m_params, mv$(new_variants)); + return Enum(m_params.clone(), mv$(new_variants)); } Struct Struct::clone() const { @@ -176,13 +176,13 @@ Struct Struct::clone() const decltype(e.ents) new_fields; for(const auto& f : e.ents) new_fields.push_back( f.clone() ); - return Struct(m_params, mv$(new_fields)); + return Struct(m_params.clone(), mv$(new_fields)); ), (Struct, decltype(e.ents) new_fields; for(const auto& f : e.ents) new_fields.push_back( f.clone() ); - return Struct(m_params, mv$(new_fields)); + return Struct(m_params.clone(), mv$(new_fields)); ) ) throw ""; diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index 700a7e40..e6b1ea1c 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -55,9 +55,7 @@ struct StructItem ::std::string m_name; TypeRef m_type; - StructItem() - { - } + //StructItem() {} StructItem(::AST::MetaItems attrs, bool is_pub, ::std::string name, TypeRef ty): m_attrs( mv$(attrs) ), @@ -80,9 +78,7 @@ struct TupleItem bool m_is_public; TypeRef m_type; - TupleItem() - { - } + //TupleItem() {} TupleItem(::AST::MetaItems attrs, bool is_pub, TypeRef ty): m_attrs( mv$(attrs) ), @@ -103,7 +99,7 @@ class TypeAlias GenericParams m_params; TypeRef m_type; public: - TypeAlias() {} + //TypeAlias() {} TypeAlias(GenericParams params, TypeRef type): m_params( move(params) ), m_type( move(type) ) @@ -132,9 +128,9 @@ private: TypeRef m_type; Expr m_value; public: - Static(): - m_class(CONST) - {} + //Static(): + // m_class(CONST) + //{} Static(Class s_class, TypeRef type, Expr value): m_class(s_class), m_type( move(type) ), @@ -170,13 +166,12 @@ private: bool m_is_unsafe; bool m_is_variadic; // extern only public: - Function() - {} Function(const Function&) = delete; Function& operator=(const Function&) = delete; 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); } @@ -369,7 +364,7 @@ class ImplDef Spanned m_trait; TypeRef m_type; public: - ImplDef() {} + //ImplDef() {} ImplDef(ImplDef&&) /*noexcept*/ = default; ImplDef(Span sp, MetaItems attrs, GenericParams params, Spanned trait_type, TypeRef impl_type): m_span( mv$(sp) ), @@ -416,7 +411,7 @@ private: //NamedList m_statics; public: - Impl() {} + //Impl() {} Impl(Impl&&) /*noexcept*/ = default; Impl(ImplDef def): m_def( mv$(def) ) diff --git a/src/ast/expr.hpp b/src/ast/expr.hpp index 3f2151dd..1d1ccdae 100644 --- a/src/ast/expr.hpp +++ b/src/ast/expr.hpp @@ -20,7 +20,7 @@ class NodeVisitor; class ExprNode { - TypeRef m_res_type; + TypeRef m_res_type = TypeRef(Span()); MetaItems m_attrs; Position m_pos; public: @@ -81,7 +81,6 @@ struct ExprNode_Macro: ::std::string m_ident; ::TokenTree m_tokens; - ExprNode_Macro() {} ExprNode_Macro(::std::string name, ::std::string ident, ::TokenTree&& tokens): m_name(name), m_ident(ident), @@ -103,7 +102,6 @@ struct ExprNode_Flow: ::std::string m_target; unique_ptr m_value; - ExprNode_Flow() {} ExprNode_Flow(Type type, ::std::string target, unique_ptr&& value): m_type(type), m_target( move(target) ), @@ -120,7 +118,6 @@ struct ExprNode_LetBinding: TypeRef m_type; unique_ptr m_value; - ExprNode_LetBinding() {} ExprNode_LetBinding(Pattern pat, TypeRef type, unique_ptr&& value): m_pat( move(pat) ), m_type( move(type) ), @@ -159,7 +156,6 @@ struct ExprNode_CallPath: Path m_path; ::std::vector> m_args; - ExprNode_CallPath() {} ExprNode_CallPath(Path&& path, ::std::vector>&& args): m_path( move(path) ), m_args( move(args) ) @@ -175,7 +171,6 @@ struct ExprNode_CallMethod: PathNode m_method; ::std::vector> m_args; - ExprNode_CallMethod() {} ExprNode_CallMethod(unique_ptr obj, PathNode method, ::std::vector> args): m_val( move(obj) ), m_method( move(method) ), @@ -192,7 +187,6 @@ struct ExprNode_CallObject: unique_ptr m_val; ::std::vector> m_args; - ExprNode_CallObject() {} ExprNode_CallObject(unique_ptr&& val, ::std::vector< unique_ptr >&& args): m_val( move(val) ), m_args( move(args) ) @@ -261,7 +255,6 @@ struct ExprNode_Match: unique_ptr m_val; ::std::vector m_arms; - ExprNode_Match() {} ExprNode_Match(unique_ptr val, ::std::vector arms): m_val( ::std::move(val) ), m_arms( ::std::move(arms) ) @@ -277,7 +270,6 @@ struct ExprNode_If: unique_ptr m_true; unique_ptr m_false; - ExprNode_If() {} ExprNode_If(unique_ptr&& cond, unique_ptr&& true_code, unique_ptr&& false_code): m_cond( ::std::move(cond) ), m_true( ::std::move(true_code) ), @@ -294,7 +286,6 @@ struct ExprNode_IfLet: unique_ptr m_true; unique_ptr m_false; - ExprNode_IfLet() {} ExprNode_IfLet(AST::Pattern pattern, unique_ptr&& cond, unique_ptr&& true_code, unique_ptr&& false_code): m_pattern( ::std::move(pattern) ), m_value( ::std::move(cond) ), @@ -311,7 +302,6 @@ struct ExprNode_Integer: enum eCoreType m_datatype; uint64_t m_value; - ExprNode_Integer() {} ExprNode_Integer(uint64_t value, enum eCoreType datatype): m_datatype(datatype), m_value(value) @@ -327,7 +317,6 @@ struct ExprNode_Float: enum eCoreType m_datatype; double m_value; - ExprNode_Float() {} ExprNode_Float(double value, enum eCoreType datatype): m_datatype(datatype), m_value(value) @@ -342,7 +331,6 @@ struct ExprNode_Bool: { bool m_value; - ExprNode_Bool() {} ExprNode_Bool(bool value): m_value(value) { @@ -356,7 +344,6 @@ struct ExprNode_String: { ::std::string m_value; - ExprNode_String() {} ExprNode_String(::std::string value): m_value( ::std::move(value) ) {} @@ -369,7 +356,6 @@ struct ExprNode_ByteString: { ::std::string m_value; - ExprNode_ByteString() {} ExprNode_ByteString(::std::string value): m_value( ::std::move(value) ) {} @@ -387,7 +373,6 @@ struct ExprNode_Closure: TypeRef m_return; unique_ptr m_code; - ExprNode_Closure() {} ExprNode_Closure(args_t args, TypeRef rv, unique_ptr code): m_args( ::std::move(args) ), m_return( ::std::move(rv) ), @@ -405,7 +390,6 @@ struct ExprNode_StructLiteral: unique_ptr m_base_value; t_values m_values; - ExprNode_StructLiteral() {} ExprNode_StructLiteral(Path path, unique_ptr&& base_value, t_values&& values ): m_path( move(path) ), m_base_value( move(base_value) ), @@ -421,7 +405,6 @@ struct ExprNode_Array: unique_ptr m_size; // if non-NULL, it's a sized array ::std::vector< unique_ptr > m_values; - ExprNode_Array() {} ExprNode_Array(::std::vector< unique_ptr > vals): m_values( ::std::move(vals) ) {} @@ -439,7 +422,6 @@ struct ExprNode_Tuple: { ::std::vector< unique_ptr > m_values; - ExprNode_Tuple() {} ExprNode_Tuple(::std::vector< unique_ptr > vals): m_values( ::std::move(vals) ) {} @@ -452,7 +434,6 @@ struct ExprNode_NamedValue: { Path m_path; - ExprNode_NamedValue() {} ExprNode_NamedValue(Path&& path): m_path( ::std::move(path) ) { @@ -466,7 +447,6 @@ struct ExprNode_Field: ::std::unique_ptr m_obj; ::std::string m_name; - ExprNode_Field() {} ExprNode_Field(::std::unique_ptr&& obj, ::std::string name): m_obj( ::std::move(obj) ), m_name( ::std::move(name) ) @@ -480,7 +460,6 @@ struct ExprNode_Index: ::std::unique_ptr m_obj; ::std::unique_ptr m_idx; - ExprNode_Index() {} ExprNode_Index(::std::unique_ptr obj, ::std::unique_ptr idx): m_obj( ::std::move(obj) ), m_idx( ::std::move(idx) ) @@ -495,7 +474,6 @@ struct ExprNode_Deref: { ::std::unique_ptr m_value; - ExprNode_Deref() {} ExprNode_Deref(::std::unique_ptr value): m_value( ::std::move(value) ) { @@ -511,7 +489,6 @@ struct ExprNode_Cast: unique_ptr m_value; TypeRef m_type; - ExprNode_Cast() {} ExprNode_Cast(unique_ptr&& value, TypeRef&& dst_type): m_value( move(value) ), m_type( move(dst_type) ) @@ -557,7 +534,6 @@ struct ExprNode_BinOp: ::std::unique_ptr m_left; ::std::unique_ptr m_right; - ExprNode_BinOp() {} ExprNode_BinOp(Type type, ::std::unique_ptr left, ::std::unique_ptr right): m_type(type), m_left( ::std::move(left) ), @@ -583,7 +559,6 @@ struct ExprNode_UniOp: enum Type m_type; ::std::unique_ptr m_value; - ExprNode_UniOp() {} ExprNode_UniOp(Type type, ::std::unique_ptr value): m_type(type), m_value( ::std::move(value) ) diff --git a/src/ast/generics.hpp b/src/ast/generics.hpp index 0209ccbb..f02de958 100644 --- a/src/ast/generics.hpp +++ b/src/ast/generics.hpp @@ -11,9 +11,10 @@ class TypeParam ::std::string m_name; TypeRef m_default; public: - TypeParam(): m_name("") {} + //TypeParam(): m_name("") {} TypeParam(::std::string name): - m_name( ::std::move(name) ) + m_name( ::std::move(name) ), + m_default( Span() ) {} void setDefault(TypeRef type) { assert(m_default.is_wildcard()); @@ -92,25 +93,18 @@ class GenericParams ::std::vector m_bounds; public: GenericParams() {} - GenericParams(GenericParams&& x) noexcept: - m_type_params( mv$(x.m_type_params) ), - m_lifetime_params( mv$(x.m_lifetime_params) ), - m_bounds( mv$(x.m_bounds) ) - {} - GenericParams& operator=(GenericParams&& x) { - m_type_params = mv$(x.m_type_params); - m_lifetime_params = mv$(x.m_lifetime_params); - m_bounds = mv$(x.m_bounds); - return *this; - } - GenericParams(const GenericParams& x): - m_type_params(x.m_type_params), - m_lifetime_params(x.m_lifetime_params), - m_bounds() - { - m_bounds.reserve( x.m_bounds.size() ); - for(auto& e: x.m_bounds) - m_bounds.push_back( e.clone() ); + GenericParams(GenericParams&& x) noexcept = default; + GenericParams& operator=(GenericParams&& x) = default; + GenericParams(const GenericParams& x) = delete; + + GenericParams clone() const { + GenericParams rv; + rv.m_type_params = m_type_params; // Copy-constructable + rv.m_lifetime_params = m_lifetime_params; + rv.m_bounds.reserve( m_bounds.size() ); + for(auto& e: m_bounds) + rv.m_bounds.push_back( e.clone() ); + return rv; } const ::std::vector& ty_params() const { return m_type_params; } diff --git a/src/ast/types.hpp b/src/ast/types.hpp index e0cede23..79934e16 100644 --- a/src/ast/types.hpp +++ b/src/ast/types.hpp @@ -96,6 +96,10 @@ TAGGED_UNION(TypeData, None, (TraitObject, struct { ::std::vector<::std::string> hrls; ::std::vector traits; + // }), + //(ImplTrait, struct { + // ::std::vector<::std::string> hrls; + // ::std::vector traits; }) ); @@ -125,7 +129,7 @@ public: return *this; } - TypeRef(Span sp = Span()): + TypeRef(Span sp): m_span( mv$(sp) ), m_data( TypeData::make_Any({}) ) {} @@ -195,11 +199,12 @@ public: {} struct TagArg {}; - TypeRef(TagArg, ::std::string name, unsigned int binding = ~0u): + TypeRef(TagArg, Span sp, ::std::string name, unsigned int binding = ~0u): + m_span( mv$(sp) ), m_data(TypeData::make_Generic({ name, binding })) {} - TypeRef(::std::string name, unsigned int binding = ~0u): - TypeRef(TagArg(), ::std::move(name), binding) + TypeRef(Span sp, ::std::string name, unsigned int binding = ~0u): + TypeRef(TagArg(), mv$(sp), mv$(name), binding) {} struct TagPath {}; @@ -242,12 +247,6 @@ public: return TypeRef(*this); } - //::option as_tuple() const { - // switch(m_data.tag()) - // { - // } - //} - const TypeRef& inner_type() const { TU_MATCH_DEF(TypeData, (m_data), (e), ( throw ::std::runtime_error("Called inner_type on non-wrapper"); ), @@ -264,11 +263,6 @@ public: (Array, return *e.inner; ) ) } - //::std::vector& sub_types() { return m_inner_types; } - //const ::std::vector& sub_types() const { return m_inner_types; } - - //void add_trait(TypeRef trait) { assert(is_wildcard()); m_inner_types.push_back( ::std::move(trait) ); } - //const ::std::vector& traits() const { assert(is_wildcard()); return m_inner_types; } Ordering ord(const TypeRef& x) const; bool operator==(const TypeRef& x) const { return ord(x) == OrdEqual; } diff --git a/src/expand/cfg.cpp b/src/expand/cfg.cpp index 897c952a..0a4617cf 100644 --- a/src/expand/cfg.cpp +++ b/src/expand/cfg.cpp @@ -142,7 +142,7 @@ class CCfgHandler: // Leave } else { - impl.type() = ::TypeRef(); + impl.type() = ::TypeRef(sp); } } @@ -155,7 +155,7 @@ class CCfgHandler: void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::TupleItem& i) const override { DEBUG("#[cfg] tuple item - " << mi); if( !check_cfg(sp, mi) ) { - i.m_type = ::TypeRef(); + i.m_type = ::TypeRef(sp); } } void handle(const Span& sp, const AST::MetaItem& mi, AST::Crate& crate, ::AST::EnumVariant& i) const override { diff --git a/src/expand/derive.cpp b/src/expand/derive.cpp index 173afa81..06e2921e 100644 --- a/src/expand/derive.cpp +++ b/src/expand/derive.cpp @@ -37,9 +37,9 @@ struct Deriver 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 AST::GenericParams& p, const AST::Path& trait_path, ::std::vector additional_bounded_types) const + AST::GenericParams get_params_with_bounds(const Span& sp, const AST::GenericParams& p, const AST::Path& trait_path, ::std::vector additional_bounded_types) const { - AST::GenericParams params = p; + 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? @@ -49,7 +49,7 @@ struct Deriver for(const auto& arg : params.ty_params()) { params.add_bound( ::AST::GenericBound::make_IsTrait({ - TypeRef(arg.name(), i), {}, trait_path + TypeRef(sp, arg.name(), i), {}, trait_path }) ); i ++; } @@ -247,13 +247,13 @@ class Deriver_Debug: ABI_RUST, false, false, false, ret_type, vec$( - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef("Self", 0xFFFF)) ), + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef(sp, "Self", 0xFFFF)) ), ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "f"), f_type ) ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - AST::GenericParams params = get_params_with_bounds(p, debug_trait, mv$(types_to_bound)); + 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 ) ); rv.add_function(false, false, "fmt", mv$(fcn)); @@ -424,13 +424,13 @@ 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, false, TypeRef("Self", 0xFFFF)) ), - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "v"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef("Self", 0xFFFF)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef(sp, "Self", 0xFFFF)) ), + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "v" ), TypeRef(TypeRef::TagReference(), sp, false, TypeRef(sp, "Self", 0xFFFF)) ) ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - AST::GenericParams params = get_params_with_bounds(p, trait_path, mv$(types_to_bound)); + 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 ) ); rv.add_function(false, false, "eq", mv$(fcn)); @@ -610,13 +610,13 @@ 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, false, TypeRef("Self", 0xFFFF)) ), - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "v"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef("Self", 0xFFFF)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef(sp, "Self", 0xFFFF)) ), + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "v" ), TypeRef(TypeRef::TagReference(), sp, false, TypeRef(sp, "Self", 0xFFFF)) ) ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - AST::GenericParams params = get_params_with_bounds(p, trait_path, mv$(types_to_bound)); + 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 ) ); rv.add_function(false, false, "partial_cmp", mv$(fcn)); @@ -865,12 +865,12 @@ 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, false, TypeRef("Self", 0xFFFF)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef(sp, "Self", 0xFFFF)) ) ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - AST::GenericParams params = get_params_with_bounds(p, trait_path, mv$(types_to_bound)); + 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 ) ); rv.add_function(false, false, "assert_receiver_is_total_eq", mv$(fcn)); @@ -1007,13 +1007,13 @@ 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, false, TypeRef("Self", 0xFFFF)) ), - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "v"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef("Self", 0xFFFF)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef(sp, "Self", 0xFFFF)) ), + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "v"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef(sp, "Self", 0xFFFF)) ) ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - AST::GenericParams params = get_params_with_bounds(p, trait_path, mv$(types_to_bound)); + 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 ) ); rv.add_function(false, false, "cmp", mv$(fcn)); @@ -1250,14 +1250,14 @@ class Deriver_Clone: sp, AST::GenericParams(), ABI_RUST, false, false, false, - TypeRef("Self", 0xFFFF), + TypeRef(sp, "Self", 0xFFFF), vec$( - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef("Self", 0xFFFF)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef(sp, "Self", 0xFFFF)) ) ) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - AST::GenericParams params = get_params_with_bounds(p, trait_path, mv$(types_to_bound)); + 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 ) ); rv.add_function(false, false, "clone", mv$(fcn)); @@ -1396,7 +1396,7 @@ class Deriver_Copy: { const AST::Path trait_path = this->get_trait_path(core_name); - AST::GenericParams params = get_params_with_bounds(p, trait_path, mv$(types_to_bound)); + 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 ) ); return mv$(rv); @@ -1421,7 +1421,7 @@ class Deriver_Default: return AST::Path(core_name, { AST::PathNode("default", {}), AST::PathNode("Default", {}) }); } AST::Path get_method_path(const ::std::string& core_name) const { - return AST::Path(AST::Path::TagUfcs(), ::TypeRef(), get_trait_path(core_name), { AST::PathNode("default", {}) } ); + 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 types_to_bound, AST::ExprNodeP node) const @@ -1432,12 +1432,12 @@ class Deriver_Default: sp, AST::GenericParams(), ABI_RUST, false, false, false, - TypeRef("Self", 0xFFFF), + TypeRef(sp, "Self", 0xFFFF), {} ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - AST::GenericParams params = get_params_with_bounds(p, trait_path, mv$(types_to_bound)); + 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 ) ); rv.add_function(false, false, "default", mv$(fcn)); @@ -1514,19 +1514,19 @@ 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, false, TypeRef("Self", 0xFFFF)) ), - ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "state"), TypeRef(TypeRef::TagReference(), sp, true, TypeRef("H", 0x100|0)) ) + ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), sp, false, TypeRef(sp, "Self", 0xFFFF)) ), + ::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_bound( AST::GenericBound::make_IsTrait({ - TypeRef("H", 0x100|0), + TypeRef(sp, "H", 0x100|0), {}, this->get_trait_path_Hasher(core_name) }) ); fcn.set_code( NEWNODE(Block, vec$(mv$(node))) ); - AST::GenericParams params = get_params_with_bounds(p, trait_path, mv$(types_to_bound)); + 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 ) ); rv.add_function(false, false, "hash", mv$(fcn)); @@ -1686,7 +1686,7 @@ static void derive_item(const Span& sp, const AST::Crate& crate, AST::Module& mo 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(), param.name()) ); + types_args.m_types.push_back( TypeRef(TypeRef::TagArg(), sp, param.name()) ); } ::std::vector< ::std::string> missing_handlers; diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp index 755defa7..ba8f6977 100644 --- a/src/expand/mod.cpp +++ b/src/expand/mod.cpp @@ -402,7 +402,7 @@ struct CExpandExpr: replacement.reset(new ::AST::ExprNode_Match( ::AST::ExprNodeP(new ::AST::ExprNode_CallPath( - ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(), path_IntoIterator, { ::AST::PathNode("into_iter") } ), + ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(node.span()), path_IntoIterator, { ::AST::PathNode("into_iter") } ), ::make_vec1( mv$(node.m_cond) ) )), ::make_vec1(::AST::ExprNode_Match_Arm( @@ -412,7 +412,7 @@ struct CExpandExpr: node.m_label, ::AST::ExprNodeP(new ::AST::ExprNode_Match( ::AST::ExprNodeP(new ::AST::ExprNode_CallPath( - ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(), path_Iterator, { ::AST::PathNode("next") } ), + ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(node.span()), path_Iterator, { ::AST::PathNode("next") } ), ::make_vec1( ::AST::ExprNodeP(new ::AST::ExprNode_UniOp( ::AST::ExprNode_UniOp::REFMUT, ::AST::ExprNodeP(new ::AST::ExprNode_NamedValue( ::AST::Path("it") )) @@ -513,7 +513,7 @@ struct CExpandExpr: auto path_Ok = ::AST::Path(core_crate, {::AST::PathNode("result"), ::AST::PathNode("Result"), ::AST::PathNode("Ok")}); 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() ); + path_From.nodes().back().args().m_types.push_back( ::TypeRef(node.span()) ); // Desugars into // ``` @@ -541,7 +541,7 @@ struct CExpandExpr: ::AST::Path(path_Err), ::make_vec1( ::AST::ExprNodeP(new ::AST::ExprNode_CallPath( - ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(), mv$(path_From), { ::AST::PathNode("from") }), + ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(node.span()), mv$(path_From), { ::AST::PathNode("from") }), ::make_vec1( ::AST::ExprNodeP( new ::AST::ExprNode_NamedValue( ::AST::Path(::AST::Path::TagLocal(), "e") ) ) ) )) ) diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index 5590b738..471e6c34 100644 --- a/src/parse/expr.cpp +++ b/src/parse/expr.cpp @@ -518,7 +518,7 @@ ExprNodeP Parse_Stmt_Let(TokenStream& lex) { Token tok; AST::Pattern pat = Parse_Pattern(lex, false); // irrefutable - TypeRef type; + TypeRef type { lex.getPosition() }; if( GET_TOK(tok, lex) == TOK_COLON ) { type = Parse_Type(lex); GET_TOK(tok, lex); @@ -997,7 +997,7 @@ ExprNodeP Parse_ExprVal_Closure(TokenStream& lex, bool is_move) // Irrefutable pattern AST::Pattern pat = Parse_Pattern(lex, false); - TypeRef type; + TypeRef type { lex.getPosition() }; if( GET_TOK(tok, lex) == TOK_COLON ) type = Parse_Type(lex); else @@ -1010,7 +1010,7 @@ ExprNodeP Parse_ExprVal_Closure(TokenStream& lex, bool is_move) } CHECK_TOK(tok, TOK_PIPE); - TypeRef rt; + auto rt = TypeRef(lex.getPosition()); if( GET_TOK(tok, lex) == TOK_THINARROW ) { if( GET_TOK(tok, lex) == TOK_EXCLAM ) { diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 4af5c147..488ef9d3 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -134,7 +134,8 @@ AST::GenericParams Parse_GenericParams(TokenStream& lex) ret.add_lft_param( param_name ); else ret.add_ty_param( AST::TypeParam( param_name ) ); - + auto param_ty = TypeRef(lex.getPosition(), param_name); + if( GET_TOK(tok, lex) == TOK_COLON ) { if( is_lifetime ) @@ -146,7 +147,7 @@ AST::GenericParams Parse_GenericParams(TokenStream& lex) } else { - Parse_TypeBound(lex, ret, TypeRef(TypeRef::TagArg(), param_name)); + Parse_TypeBound(lex, ret, param_ty); GET_TOK(tok, lex); } } @@ -278,14 +279,16 @@ AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, bool allow_ lifetime = tok.str(); GET_TOK(tok, lex); } + auto ty_sp = lex.end_span(ps); + if( tok.type() == TOK_RWORD_MUT ) { GET_CHECK_TOK(tok, lex, TOK_RWORD_SELF); - args.push_back( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), lex.end_span(ps), true, TypeRef("Self", 0xFFFF))) ); + args.push_back( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), ty_sp, true, TypeRef(ty_sp, "Self", 0xFFFF))) ); } else { - args.push_back( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), lex.end_span(ps), false, TypeRef("Self", 0xFFFF))) ); + args.push_back( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), TypeRef(TypeRef::TagReference(), ty_sp, false, TypeRef(ty_sp, "Self", 0xFFFF))) ); } DEBUG("TODO: UFCS / self lifetimes"); if( allow_self == false ) @@ -310,14 +313,13 @@ 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"); - TypeRef ty; + TypeRef ty = TypeRef( lex.getPosition(), "Self", 0xFFFF ); if( GET_TOK(tok, lex) == TOK_COLON ) { // Typed mut self ty = Parse_Type(lex); } else { PUTBACK(tok, lex); - ty = TypeRef("Self", 0xFFFF); } args.push_back( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), ty) ); GET_TOK(tok, lex); @@ -328,14 +330,13 @@ 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"); - TypeRef ty; + TypeRef ty = TypeRef( lex.getPosition(), "Self", 0xFFFF ); if( GET_TOK(tok, lex) == TOK_COLON ) { // Typed mut self ty = Parse_Type(lex); } else { PUTBACK(tok, lex); - ty = TypeRef("Self", 0xFFFF); } args.push_back( ::std::make_pair( AST::Pattern(AST::Pattern::TagBind(), "self"), ty) ); GET_TOK(tok, lex); @@ -658,14 +659,14 @@ AST::Trait Parse_TraitDef(TokenStream& lex, const AST::MetaItems& meta_items) if( GET_TOK(tok, lex) == TOK_COLON ) { // Bounded associated type - Parse_TypeBound(lex, atype_params, TypeRef("Self", 0xFFFF)); + Parse_TypeBound(lex, atype_params, TypeRef(lex.getPosition(), "Self", 0xFFFF)); GET_TOK(tok, lex); } if( tok.type() == TOK_RWORD_WHERE ) { throw ParseError::Todo(lex, "Where clause on associated type"); } - TypeRef default_type; + TypeRef default_type = TypeRef( lex.getPosition() ); if( tok.type() == TOK_EQUAL ) { default_type = Parse_Type(lex); GET_TOK(tok, lex); @@ -889,14 +890,14 @@ void Parse_Impl(TokenStream& lex, AST::Module& mod, AST::MetaItems attrs, bool i // 2. Either a trait name (with type params), or the type to impl Spanned trait_path; - TypeRef impl_type; - // - Handle negative impls, which must be a trait + + // - Handle negative impls specially, which must be a trait // "impl !Trait for Type {}" if( GET_TOK(tok, lex) == TOK_EXCLAM ) { trait_path = GET_SPANNED(::AST::Path, lex, Parse_Path(lex, PATH_GENERIC_TYPE)); GET_CHECK_TOK(tok, lex, TOK_RWORD_FOR); - impl_type = Parse_Type(lex, true); + auto impl_type = Parse_Type(lex, true); if( GET_TOK(tok, lex) == TOK_RWORD_WHERE ) { @@ -910,37 +911,36 @@ void Parse_Impl(TokenStream& lex, AST::Module& mod, AST::MetaItems attrs, bool i mod.add_neg_impl( AST::ImplDef(lex.end_span(ps), AST::MetaItems(), mv$(params), mv$(trait_path), mv$(impl_type) ) ); return ; } - else + + // - Don't care which at this stage + PUTBACK(tok, lex); + + auto impl_type = Parse_Type(lex, true); + + if( GET_TOK(tok, lex) == TOK_RWORD_FOR ) { - // - Don't care which at this stage - PUTBACK(tok, lex); - - impl_type = Parse_Type(lex, true); - - if( GET_TOK(tok, lex) == TOK_RWORD_FOR ) + // Trickery! All traits parse as valid types, so this works. + if( !impl_type.is_path() ) + throw ParseError::Generic(lex, "Trait was not a path"); + trait_path = Spanned< AST::Path> { + impl_type.span(), + mv$(impl_type.path()) + }; + // Implementing a trait for another type, get the target type + if( GET_TOK(tok, lex) == TOK_DOUBLE_DOT ) { - if( !impl_type.is_path() ) - throw ParseError::Generic(lex, "Trait was not a path"); - trait_path = Spanned< AST::Path> { - impl_type.span(), - mv$(impl_type.path()) - }; - // Implementing a trait for another type, get the target type - if( GET_TOK(tok, lex) == TOK_DOUBLE_DOT ) - { - // Default impl - impl_type = TypeRef(TypeRef::TagInvalid(), lex.getPosition()); - } - else - { - PUTBACK(tok, lex); - impl_type = Parse_Type(lex, true); - } + // Default impl + impl_type = TypeRef(TypeRef::TagInvalid(), lex.getPosition()); } - else { + else + { PUTBACK(tok, lex); + impl_type = Parse_Type(lex, true); } } + else { + PUTBACK(tok, lex); + } // Where clause if( GET_TOK(tok, lex) == TOK_RWORD_WHERE ) diff --git a/src/resolve/absolute.cpp b/src/resolve/absolute.cpp index f466a40f..4a4ac5cb 100644 --- a/src/resolve/absolute.cpp +++ b/src/resolve/absolute.cpp @@ -156,7 +156,7 @@ struct Context return e->clone(); } else { - return ::TypeRef("Self", 0xFFFF); + return ::TypeRef(Span(), "Self", 0xFFFF); } ) ) @@ -822,7 +822,7 @@ namespace { for(const auto& typ : e.m_params.m_types) { (void)typ; - trait_path.nodes().back().args().m_types.push_back( ::TypeRef() ); + trait_path.nodes().back().args().m_types.push_back( ::TypeRef(sp) ); } } trait_path.bind( ::AST::PathBinding::make_Trait({nullptr, &e}) ); @@ -848,7 +848,7 @@ namespace { new_path = ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(sp, mv$(trait_path))); } else { - new_path = ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(), mv$(trait_path)); + new_path = ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(sp), mv$(trait_path)); } for( unsigned int j = i+1; j < path_abs.nodes.size(); j ++ ) new_path.nodes().push_back( mv$(path_abs.nodes[j]) ); @@ -1093,14 +1093,14 @@ void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Contex for(const auto& typ : e.trait_->params().ty_params()) { (void)typ; - trait_path.nodes().back().args().m_types.push_back( ::TypeRef() ); + trait_path.nodes().back().args().m_types.push_back( ::TypeRef(sp) ); } } else { for(const auto& typ : e.hir->m_params.m_types) { (void)typ; - trait_path.nodes().back().args().m_types.push_back( ::TypeRef() ); + trait_path.nodes().back().args().m_types.push_back( ::TypeRef(sp) ); } } } @@ -1136,7 +1136,7 @@ void Resolve_Absolute_Path_BindAbsolute(Context& context, const Span& sp, Contex new_path = ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(sp, mv$(trait_path))); } else { - new_path = ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(), mv$(trait_path)); + new_path = ::AST::Path(::AST::Path::TagUfcs(), ::TypeRef(sp), mv$(trait_path)); } for( unsigned int j = i+1; j < path_abs.nodes.size(); j ++ ) new_path.nodes().push_back( mv$(path_abs.nodes[j]) ); -- cgit v1.2.3