aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/resolve/rust-ast-resolve-item.h
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-08-23 16:19:04 +0100
committerArthur Cohen <arthur.cohen@embecosm.com>2022-12-13 14:00:04 +0100
commit85a8fe00f805e7889b4e67a98ae1d435c042166b (patch)
treed034c272bdd63d7c63132e816e63f284c0076b82 /gcc/rust/resolve/rust-ast-resolve-item.h
parent1841081a8a306c1a220694a5ddb3a927cb4b2db3 (diff)
downloadgcc-85a8fe00f805e7889b4e67a98ae1d435c042166b.zip
gcc-85a8fe00f805e7889b4e67a98ae1d435c042166b.tar.gz
gcc-85a8fe00f805e7889b4e67a98ae1d435c042166b.tar.bz2
gccrs: Add name resolution pass to the Rust front-end
The name resolution is split into two phases, one toplevel pass which scans the whole "Crate" which iterates all items and nested items in modules to generate a context class full of CanonicalPath items. It also generates a hierarchy of parent->child and child->parent relationships using the AST NodeId for PathResolution in the second phase. The second phase drills into each item like functions and creates a stack of canonical paths for variables etc so that we can store information in a side table of usage variable 'a' resolves to NodeId '123' which refers to the NodeId of the "let a;" statement. gcc/rust/ * resolve/rust-ast-resolve-base.cc: New. * resolve/rust-ast-resolve-base.h: New. * resolve/rust-ast-resolve-expr.cc: New. * resolve/rust-ast-resolve-expr.h: New. * resolve/rust-ast-resolve-implitem.h: New. * resolve/rust-ast-resolve-item.cc: New. * resolve/rust-ast-resolve-item.h: New. * resolve/rust-ast-resolve-path.cc: New. * resolve/rust-ast-resolve-path.h: New. * resolve/rust-ast-resolve-pattern.cc: New. * resolve/rust-ast-resolve-pattern.h: New. * resolve/rust-ast-resolve-stmt.cc: New. * resolve/rust-ast-resolve-stmt.h: New. * resolve/rust-ast-resolve-struct-expr-field.cc: New. * resolve/rust-ast-resolve-struct-expr-field.h: New. * resolve/rust-ast-resolve-toplevel.h: New. * resolve/rust-ast-resolve-type.cc: New. * resolve/rust-ast-resolve-type.h: New. * resolve/rust-ast-resolve.cc: New. * resolve/rust-ast-resolve.h: New. * resolve/rust-ast-verify-assignee.h: New. * resolve/rust-name-resolver.cc: New. * resolve/rust-name-resolver.h: New.
Diffstat (limited to 'gcc/rust/resolve/rust-ast-resolve-item.h')
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-item.h149
1 files changed, 149 insertions, 0 deletions
diff --git a/gcc/rust/resolve/rust-ast-resolve-item.h b/gcc/rust/resolve/rust-ast-resolve-item.h
new file mode 100644
index 0000000..ce521f0
--- /dev/null
+++ b/gcc/rust/resolve/rust-ast-resolve-item.h
@@ -0,0 +1,149 @@
+// Copyright (C) 2020-2022 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_ITEM_H
+#define RUST_AST_RESOLVE_ITEM_H
+
+#include "rust-ast-full-decls.h"
+#include "rust-ast-resolve-base.h"
+#include "rust-ast-full.h"
+#include "rust-ast-resolve-toplevel.h"
+#include "rust-ast-resolve-type.h"
+#include "rust-ast-resolve-pattern.h"
+#include "rust-ast-resolve-stmt.h"
+#include "config.h"
+
+namespace Rust {
+namespace Resolver {
+
+class ResolveTraitItems : public ResolverBase
+{
+ using Rust::Resolver::ResolverBase::visit;
+
+public:
+ static void go (AST::TraitItem *item, const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix);
+
+ void visit (AST::TraitItemType &type) override;
+ void visit (AST::TraitItemFunc &func) override;
+ void visit (AST::TraitItemMethod &func) override;
+ void visit (AST::TraitItemConst &constant) override;
+
+private:
+ ResolveTraitItems (const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix);
+
+ const CanonicalPath &prefix;
+ const CanonicalPath &canonical_prefix;
+};
+
+class ResolveItem : public ResolverBase
+{
+public:
+ using Rust::Resolver::ResolverBase::visit;
+
+ static void go (AST::Item *item, const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix);
+
+ void visit (AST::TypeAlias &alias) override;
+ void visit (AST::Module &module) override;
+ void visit (AST::TupleStruct &struct_decl) override;
+ void visit (AST::Enum &enum_decl) override;
+ /* EnumItem doesn't need to be handled, no fields. */
+ void visit (AST::EnumItem &item) override;
+ void visit (AST::EnumItemTuple &item) override;
+ void visit (AST::EnumItemStruct &item) override;
+ void visit (AST::EnumItemDiscriminant &item) override;
+ void visit (AST::StructStruct &struct_decl) override;
+ void visit (AST::Union &union_decl) override;
+ void visit (AST::StaticItem &var) override;
+ void visit (AST::ConstantItem &constant) override;
+ void visit (AST::Function &function) override;
+ void visit (AST::InherentImpl &impl_block) override;
+ void visit (AST::Method &method) override;
+ void visit (AST::TraitImpl &impl_block) override;
+ void visit (AST::Trait &trait) override;
+ void visit (AST::ExternBlock &extern_block) override;
+ void visit (AST::UseDeclaration &) override;
+
+protected:
+ void resolve_impl_item (AST::TraitImplItem *item, const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix);
+ void resolve_impl_item (AST::InherentImplItem *item,
+ const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix);
+ void resolve_extern_item (AST::ExternalItem *item);
+
+ ResolveItem (const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix);
+
+ const CanonicalPath &prefix;
+ const CanonicalPath &canonical_prefix;
+};
+
+class ResolveImplItems : public ResolveItem
+{
+ using Rust::Resolver::ResolveItem::visit;
+
+public:
+ static void go (AST::InherentImplItem *item, const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix);
+ static void go (AST::TraitImplItem *item, const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix);
+
+ void visit (AST::TypeAlias &alias) override;
+
+private:
+ ResolveImplItems (const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix);
+};
+
+class ResolveExternItem : public ResolverBase
+{
+ using Rust::Resolver::ResolverBase::visit;
+
+public:
+ static void go (AST::ExternalItem *item, const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix);
+
+ void visit (AST::ExternalFunctionItem &function) override;
+ void visit (AST::ExternalStaticItem &item) override;
+
+private:
+ ResolveExternItem (const CanonicalPath &prefix,
+ const CanonicalPath &canonical_prefix)
+ : ResolverBase (), prefix (prefix), canonical_prefix (canonical_prefix)
+ {}
+
+ const CanonicalPath &prefix;
+ const CanonicalPath &canonical_prefix;
+};
+
+} // namespace Resolver
+} // namespace Rust
+
+#if CHECKING_P
+
+namespace selftest {
+extern void
+rust_simple_path_resolve_test (void);
+} // namespace selftest
+
+#endif // CHECKING_P
+
+#endif // RUST_AST_RESOLVE_ITEM_H