aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/resolve/rust-ast-resolve-pattern.h
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2020-12-10 20:02:03 +0000
committerPhilip Herron <herron.philip@googlemail.com>2020-12-17 17:23:46 +0000
commit4fb0ab7e635c65318aadf958e0e1303f3435c4e5 (patch)
treeb1472ad8c7c1d23686231a1bf0a697db4c2aef92 /gcc/rust/resolve/rust-ast-resolve-pattern.h
parent2cfc6276dadc54aea9f8ce2a8cfefaee456f380e (diff)
downloadgcc-4fb0ab7e635c65318aadf958e0e1303f3435c4e5.zip
gcc-4fb0ab7e635c65318aadf958e0e1303f3435c4e5.tar.gz
gcc-4fb0ab7e635c65318aadf958e0e1303f3435c4e5.tar.bz2
This sets up a name resolution framework trying to follow rust-dev guide
We can use the NodeId from the AST to generate apropriate mappings for all names and types. Ribs are the scopes for names being instansiated, and reference to defintion tables allows all names to be resolved to NodeId's. Later on NodeIds will map over to HIR ids allowing for type resolution.
Diffstat (limited to 'gcc/rust/resolve/rust-ast-resolve-pattern.h')
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-pattern.h97
1 files changed, 97 insertions, 0 deletions
diff --git a/gcc/rust/resolve/rust-ast-resolve-pattern.h b/gcc/rust/resolve/rust-ast-resolve-pattern.h
new file mode 100644
index 0000000..fc2da70
--- /dev/null
+++ b/gcc/rust/resolve/rust-ast-resolve-pattern.h
@@ -0,0 +1,97 @@
+// Copyright (C) 2020 Free Software Foundation, Inc.
+
+// This file is part of GCC.
+
+// GCC is free software; you can redistribute it and/or modify it under
+// the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3, or (at your option) any later
+// version.
+
+// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+// WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+// for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with GCC; see the file COPYING3. If not see
+// <http://www.gnu.org/licenses/>.
+
+#ifndef RUST_AST_RESOLVE_PATTERN_H
+#define RUST_AST_RESOLVE_PATTERN_H
+
+#include "rust-ast-resolve-base.h"
+#include "rust-ast-full.h"
+
+namespace Rust {
+namespace Resolver {
+
+class ResolvePattern : public ResolverBase
+{
+public:
+ static void go (AST::Pattern *pattern, NodeId parent)
+ {
+ ResolvePattern resolver (parent);
+
+ pattern->accept_vis (resolver);
+ if (resolver.resolved_node == UNKNOWN_NODEID)
+ {
+ rust_error_at (resolver.locus, "failed to resolve pattern %s",
+ pattern->as_string ().c_str ());
+ }
+ };
+
+ ~ResolvePattern () {}
+
+ void visit (AST::IdentifierPattern &pattern)
+ {
+ if (resolver->get_name_scope ().lookup (pattern.get_ident (),
+ &resolved_node))
+ {
+ resolver->insert_resolved_name (pattern.get_node_id (), resolved_node);
+ resolver->insert_new_definition (pattern.get_node_id (),
+ Definition{pattern.get_node_id (),
+ parent});
+ }
+ }
+
+private:
+ ResolvePattern (NodeId parent) : ResolverBase (parent) {}
+};
+
+class PatternDeclaration : public ResolverBase
+{
+public:
+ static void go (AST::Pattern *pattern, NodeId parent)
+ {
+ PatternDeclaration resolver (parent);
+
+ pattern->accept_vis (resolver);
+ if (resolver.resolved_node != UNKNOWN_NODEID)
+ {
+ // print both locations?!
+ rust_error_at (resolver.locus, "duplicate pattern %s",
+ pattern->as_string ().c_str ());
+ }
+ };
+
+ ~PatternDeclaration () {}
+
+ void visit (AST::IdentifierPattern &pattern)
+ {
+ // if we have a duplicate id this then allows for shadowing correctly
+ // as new refs to this decl will match back here so it is ok to overwrite
+ resolver->get_name_scope ().insert (pattern.get_ident (),
+ pattern.get_node_id ());
+ resolver->insert_new_definition (pattern.get_node_id (),
+ Definition{pattern.get_node_id (),
+ parent});
+ }
+
+private:
+ PatternDeclaration (NodeId parent) : ResolverBase (parent) {}
+};
+
+} // namespace Resolver
+} // namespace Rust
+
+#endif // RUST_AST_RESOLVE_PATTERN_H