diff options
-rw-r--r-- | src/ast/ast.cpp | 41 | ||||
-rw-r--r-- | src/ast/ast.hpp | 18 | ||||
-rw-r--r-- | src/hir/from_ast.cpp | 44 |
3 files changed, 28 insertions, 75 deletions
diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index 70a61b32..23a3208d 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -304,51 +304,10 @@ Module::ItemRef Module::find_item(const ::std::string& needle, bool allow_leaves Function::Function(Span sp, GenericParams params, TypeRef ret_type, Arglist args):
m_span(sp),
- m_receiver(Receiver::Free),
m_params( move(params) ),
m_rettype( move(ret_type) ),
m_args( move(args) )
{
- if( m_args.size() > 0 && m_args.front().first.binding().m_name == "self" ) {
- const auto& ty = m_args.front().second;
- TU_MATCH_DEF( ::TypeData, (ty.m_data), (e),
- (
- // Whups!
- ERROR(sp, E0000, "Invalid receiver type - " << ty);
- ),
- (Path,
- TU_IFLET( ::AST::Path::Class, e.path.m_class, Relative, pe,
- if( pe.nodes.size() == 1 && pe.nodes.front().name() == "Box" )
- {
- if( pe.nodes.front().args().m_types.size() != 1 ) {
- ERROR(sp, E0000, "Box takes 1 argument - " << ty);
- }
- // HACK: Assumes that the param is Self or equivalent
- m_receiver = Receiver::Box;
- }
- )
-
- if( m_receiver == Receiver::Free ) {
- ERROR(sp, E0000, "Invalid receiver type - " << ty);
- }
- ),
- (Borrow,
- if(e.is_mut) {
- m_receiver = Receiver::BorrowUnique;
- }
- else {
- m_receiver = Receiver::BorrowShared;
- }
- ),
- // NOTE: This only applies for `self/&self/&mut self` syntax, NOT the `self: Self` syntax
- (Generic,
- if( e.index != 0xFFFF ) {
- ERROR(sp, E0000, "Invalid receiver type - " << ty);
- }
- m_receiver = Receiver::Value;
- )
- )
- }
}
void Trait::add_type(::std::string name, TypeRef type) {
diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index 1cd4d1ac..47b09ed4 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -147,29 +147,16 @@ class Function {
public:
typedef ::std::vector< ::std::pair<AST::Pattern,TypeRef> > Arglist;
-
- enum class Receiver {
- Free,
- Value,
- BorrowOwned,
- BorrowUnique,
- BorrowShared,
- //RawMut,
- //RawConst,
- Box,
- };
private:
Span m_span;
//::std::string m_lifetime;
- Receiver m_receiver;
GenericParams m_params;
Expr m_code;
TypeRef m_rettype;
Arglist m_args;
public:
- Function():
- m_receiver(Receiver::Free)
+ Function()
{}
Function(const Function&) = delete;
Function& operator=(const Function&) = delete;
@@ -187,9 +174,6 @@ public: TypeRef& rettype() { return m_rettype; }
const Arglist& args() const { return m_args; }
Arglist& args() { return m_args; }
-
- Receiver receiver() const { return m_receiver; }
-
};
class Trait
diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index b7d1f77e..67ee8e4b 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -8,7 +8,7 @@ #include "visitor.hpp" ::HIR::Module LowerHIR_Module(const ::AST::Module& module, ::HIR::ItemPath path, ::std::vector< ::HIR::SimplePath> traits = {}); -::HIR::Function LowerHIR_Function(::HIR::ItemPath path, const ::AST::Function& f); +::HIR::Function LowerHIR_Function(::HIR::ItemPath path, const ::AST::Function& f, const ::HIR::TypeRef& self_type); ::HIR::PathParams LowerHIR_PathParams(const Span& sp, const ::AST::PathParams& src_params, bool allow_assoc); ::HIR::TraitPath LowerHIR_TraitPath(const Span& sp, const ::AST::Path& path); @@ -867,7 +867,8 @@ namespace { }) ); ), (Function, - rv.m_values.insert( ::std::make_pair(item.name, ::HIR::TraitValueItem::make_Function( LowerHIR_Function(item_path, i) )) ); + ::HIR::TypeRef self_type {"Self", 0xFFFF}; + rv.m_values.insert( ::std::make_pair(item.name, ::HIR::TraitValueItem::make_Function( LowerHIR_Function(item_path, i, self_type) )) ); ), (Static, if( i.s_class() == ::AST::Static::CONST ) @@ -889,7 +890,7 @@ namespace { return rv; } -::HIR::Function LowerHIR_Function(::HIR::ItemPath p, const ::AST::Function& f) +::HIR::Function LowerHIR_Function(::HIR::ItemPath p, const ::AST::Function& f, const ::HIR::TypeRef& self_type) { static Span sp; @@ -900,18 +901,27 @@ namespace { auto receiver = ::HIR::Function::Receiver::Free; - switch( f.receiver() ) + if( args.size() > 0 && args.front().first.m_binding.m_name == "self" ) { - #define _(N) case ::AST::Function::Receiver::N: receiver = ::HIR::Function::Receiver::N; break; - _(Free) - _(Value) - _(BorrowOwned) - _(BorrowUnique) - _(BorrowShared) - case ::AST::Function::Receiver::Box: - TODO(sp, "Support `self: Box<Self>` receiver"); - break; - #undef _ + const auto& arg_self_ty = args.front().second; + if( arg_self_ty == self_type ) { + receiver = ::HIR::Function::Receiver::Value; + } + else TU_IFLET(::HIR::TypeRef::Data, arg_self_ty.m_data, Borrow, e, + if( *e.inner != self_type ) { + ERROR(sp, E0000, "Unknown receiver type - " << arg_self_ty); + } + switch(e.type) + { + case ::HIR::BorrowType::Owned: receiver = ::HIR::Function::Receiver::BorrowOwned; break; + case ::HIR::BorrowType::Unique: receiver = ::HIR::Function::Receiver::BorrowUnique; break; + case ::HIR::BorrowType::Shared: receiver = ::HIR::Function::Receiver::BorrowShared; break; + } + ) + else { + // TODO: Box - Compare with `boxed` lang item - May require moving to another pass + ERROR(sp, E0000, "Unknown receiver type - " << arg_self_ty); + } } // TODO: ABI and unsafety/constness @@ -993,7 +1003,7 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H _add_mod_ns_item( mod, item.name, item.is_pub, LowerHIR_Trait(item_path.get_simple_path(), e) ); ), (Function, - _add_mod_val_item(mod, item.name, item.is_pub, LowerHIR_Function(item_path, e)); + _add_mod_val_item(mod, item.name, item.is_pub, LowerHIR_Function(item_path, e, ::HIR::TypeRef{})); ), (Static, if( e.s_class() == ::AST::Static::CONST ) @@ -1068,7 +1078,7 @@ void LowerHIR_Module_Impls(const ::AST::Module& ast_mod, ::HIR::Crate& hir_crat ), (Function, DEBUG("- method " << item.name); - methods.insert( ::std::make_pair(item.name, ::HIR::TraitImpl::ImplEnt< ::HIR::Function> { item.is_specialisable, LowerHIR_Function(item_path, e) }) ); + methods.insert( ::std::make_pair(item.name, ::HIR::TraitImpl::ImplEnt< ::HIR::Function> { item.is_specialisable, LowerHIR_Function(item_path, e, type) }) ); ) ) } @@ -1117,7 +1127,7 @@ void LowerHIR_Module_Impls(const ::AST::Module& ast_mod, ::HIR::Crate& hir_crat ERROR(item.data->span, E0000, "Unexpected item type in inherent impl"); ), (Function, - methods.insert( ::std::make_pair(item.name, ::HIR::TypeImpl::VisImplEnt< ::HIR::Function> { item.is_pub, item.is_specialisable, LowerHIR_Function(item_path, e) } ) ); + methods.insert( ::std::make_pair(item.name, ::HIR::TypeImpl::VisImplEnt< ::HIR::Function> { item.is_pub, item.is_specialisable, LowerHIR_Function(item_path, e, type) } ) ); ) ) } |