From 4af0826c332c52af0796a2fe30074bbd6aba664b Mon Sep 17 00:00:00 2001 From: "John Hodge (sonata)" Date: Mon, 12 Jan 2015 19:30:26 +0800 Subject: Path resolution working in one function (types still to come, need AST rep) --- src/ast/ast.hpp | 3 ++- src/ast/path.cpp | 25 ++++++++++++++++++++++++- src/ast/path.hpp | 9 +++++++++ src/convert/resolve.cpp | 38 +++++++++++++++++++++----------------- 4 files changed, 56 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp index 3ec689f8..e7a0ba2d 100644 --- a/src/ast/ast.hpp +++ b/src/ast/ast.hpp @@ -270,7 +270,8 @@ public: const itemlist_mod_t& submods() const { return m_submods; } const itemlist_use_t& imports() const { return m_imports; } const itemlist_ext_t& extern_crates() const { return m_extern_crates; } - const itemlist_enum_t& enums() const { return m_enums; } + const itemlist_static_t& statics() const { return m_statics; } + const itemlist_enum_t& enums () const { return m_enums; } const itemlist_struct_t& structs() const { return m_structs; } SERIALISABLE_PROTOTYPES(); diff --git a/src/ast/path.cpp b/src/ast/path.cpp index 354da4da..a868be5d 100644 --- a/src/ast/path.cpp +++ b/src/ast/path.cpp @@ -142,11 +142,28 @@ void Path::resolve(const Crate& root_crate) return ; } else { - throw ParseError::Generic("Import of enum, too many extra nodes"); + throw ParseError::Generic("Binding path to enum, too many extra nodes"); } } } // - Constants / statics + { + auto& items = mod->statics(); + auto it = find_named(items, node.name()); + if( it != items.end() ) + { + DEBUG("Found static/const"); + if( is_last ) { + if( node.args().size() ) + throw ParseError::Generic("Unexpected generic params on static/const"); + bind_static(it->data); + return ; + } + else { + throw ParseError::Generic("Binding path to static, trailing nodes"); + } + } + } throw ParseError::Generic("Unable to find component '" + node.name() + "'"); } @@ -198,6 +215,12 @@ void Path::bind_struct(const Struct& ent, const ::std::vector& args) { throw ParseError::Todo("Path::resolve() bind to struct type"); } +void Path::bind_static(const Static& ent) +{ + m_binding_type = STATIC; + m_binding.static_ = &ent; +} + Path& Path::operator+=(const Path& other) { diff --git a/src/ast/path.hpp b/src/ast/path.hpp index 17325d10..f39e133d 100644 --- a/src/ast/path.hpp +++ b/src/ast/path.hpp @@ -19,6 +19,7 @@ class Crate; class Module; class Enum; class Struct; +class Static; class Function; @@ -56,6 +57,10 @@ public: MODULE, ENUM, ENUM_VAR, + STRUCT, + STRUCT_METHOD, + FUNCTION, + STATIC, }; private: enum Class { @@ -78,6 +83,9 @@ private: union { const Module* module_; const Enum* enum_; + const Struct* struct_; + const Static* static_; + const Function* func_; struct { const Enum* enum_; unsigned int idx; @@ -157,6 +165,7 @@ private: void bind_enum(const Enum& ent, const ::std::vector& args); void bind_enum_var(const Enum& ent, const ::std::string& name, const ::std::vector& args); void bind_struct(const Struct& ent, const ::std::vector& args); + void bind_static(const Static& ent); }; } // namespace AST diff --git a/src/convert/resolve.cpp b/src/convert/resolve.cpp index ee906150..53a1edaf 100644 --- a/src/convert/resolve.cpp +++ b/src/convert/resolve.cpp @@ -18,7 +18,7 @@ class CPathResolver // TODO: Maintain a stack of variable scopes public: - CPathResolver(const AST::Crate& crate, const AST::Module& mod); + CPathResolver(const AST::Crate& crate, const AST::Module& mod, AST::Path module_path); enum ResolvePathMode { MODE_EXPR, // Variables allowed @@ -85,9 +85,10 @@ public: } }; -CPathResolver::CPathResolver(const AST::Crate& crate, const AST::Module& mod): +CPathResolver::CPathResolver(const AST::Crate& crate, const AST::Module& mod, AST::Path module_path): m_crate(crate), - m_module(mod) + m_module(mod), + m_module_path( ::std::move(module_path) ) { } @@ -159,6 +160,14 @@ void CPathResolver::resolve_path(AST::Path& path, ResolvePathMode mode) const return ; } } + for( const auto& item : m_module.statics() ) + { + if( item.name == path[0].name() ) { + path = m_module_path + path; + path.resolve( m_crate ); + return ; + } + } for( const auto& import : m_module.imports() ) { const ::std::string& bind_name = import.name; @@ -244,28 +253,22 @@ void CPathResolver::handle_pattern(AST::Pattern& pat) void CPathResolver::handle_function(AST::Function& fcn) { CResolvePaths_NodeVisitor node_visitor(*this); - - for( auto& arg : fcn.args() ) - m_locals.push_back(arg.first); - - fcn.code().visit_nodes( node_visitor ); + DEBUG("Return type"); resolve_type(fcn.rettype()); - + DEBUG("Args"); for( auto& arg : fcn.args() ) resolve_type(arg.second); - + + DEBUG("Code"); + for( auto& arg : fcn.args() ) + m_locals.push_back(arg.first); + fcn.code().visit_nodes( node_visitor ); pop_scope(); if( m_locals.size() != 0 ) throw ParseError::BugCheck("m_locals.size() != 0"); } -void ResolvePaths_HandleFunction(const AST::Crate& crate, const AST::Module& mod, AST::Function& fcn) -{ - CPathResolver pr(crate, mod); - pr.handle_function(fcn); -} - void ResolvePaths_HandleModule_Use(const AST::Crate& crate, const AST::Path& modpath, AST::Module& mod) { DEBUG("modpath = " << modpath); @@ -340,7 +343,8 @@ void ResolvePaths_HandleModule(const AST::Crate& crate, const AST::Path& modpath { for( auto& fcn : mod.functions() ) { - ResolvePaths_HandleFunction(crate, mod, fcn.data); + CPathResolver pr(crate, mod, modpath); + pr.handle_function(fcn.data); } for( auto& submod : mod.submods() ) -- cgit v1.2.3