From 3876c390cef37371236e43d2e6990c212e68c481 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Sat, 4 Apr 2015 13:00:39 +0800 Subject: Add structure to check for impls on type params --- src/ast/ast.cpp | 13 +++++++++++-- src/types.hpp | 21 +++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/ast/ast.cpp b/src/ast/ast.cpp index f70f2ec5..03dc76d2 100644 --- a/src/ast/ast.cpp +++ b/src/ast/ast.cpp @@ -348,12 +348,21 @@ bool Crate::check_impls_wildcard(const Path& trait, const TypeRef& type) // TODO: Handle more complex bounds like "[T]: Trait" if( type.is_type_param() ) { + assert(type.type_params_ptr()); // Obtain the relevant TypeParams structure + const TypeParams& tps = *type.type_params_ptr(); // - TODO: this structure should be pointed to by TypeRef // Search bounds for type: trait - // If found, success! + for( const auto& bound : tps.bounds() ) + { + if( bound.is_trait() && bound.test() == type && bound.bound() == trait ) { + // If found, success! + // TODO: What should be returned, kinda need to return a boolean + throw CompileError::Todo("find_impl - Return successful impl for generic"); + } + } // Else, failure - // TODO: What should be returned, kinda need to return a boolean + return ::rust::option(); } // TODO: Do a sort to allow a binary search diff --git a/src/types.hpp b/src/types.hpp index d963a058..a5052c9b 100644 --- a/src/types.hpp +++ b/src/types.hpp @@ -65,6 +65,10 @@ class TypeRef: AST::Path m_path; // local = argument ::std::vector m_inner_types; ::std::shared_ptr m_size_expr; //< Can be null (unsized array) + + /// A generic pointer, used for tagging with extra information + /// e.g. The source TypeParams for GENERIC + const void* m_tagged_ptr; public: TypeRef(): m_class(ANY) @@ -137,7 +141,8 @@ public: struct TagArg {}; TypeRef(TagArg, ::std::string name): m_class(GENERIC), - m_path({AST::PathNode(name, {})}) + m_path( ::std::move(name) ), + m_tagged_ptr(nullptr) {} TypeRef(::std::string name): TypeRef(TagArg(), ::std::move(name)) @@ -176,15 +181,23 @@ public: bool is_unit() const { return m_class == UNIT; } bool is_primitive() const { return m_class == PRIMITIVE; } + bool is_path() const { return m_class == PATH; } + const AST::Path& path() const { assert(is_path()); return m_path; } + AST::Path& path() { assert(is_path()); return m_path; } + bool is_type_param() const { return m_class == GENERIC; } + const ::std::string& type_param() const { assert(is_type_param()); return m_path[0].name(); } + void set_type_params_ptr(const AST::TypeParams& p) { assert(is_type_param()); m_tagged_ptr = &p; }; + const AST::TypeParams* type_params_ptr() const { + assert(is_type_param()); + return reinterpret_cast(m_tagged_ptr); + } + bool is_reference() const { return m_class == REFERENCE; } bool is_pointer() const { return m_class == POINTER; } bool is_tuple() const { return m_class == TUPLE; } - const ::std::string& type_param() const { assert(is_type_param()); return m_path[0].name(); } - AST::Path& path() { assert(is_path()); return m_path; } - const AST::Path& path() const { assert(is_path()); return m_path; } ::std::vector& sub_types() { return m_inner_types; } const ::std::vector& sub_types() const { return m_inner_types; } -- cgit v1.2.3