https://gcc.gnu.org/g:2bb559cea908d08eb6bc9faae4567de0ebfdccd6

commit 2bb559cea908d08eb6bc9faae4567de0ebfdccd6
Author: Jakub Dupak <d...@jakubdupak.com>
Date:   Wed Oct 18 19:09:34 2023 +0200

    borrowck: Add initial structure for borrowchecking
    
    gcc/rust/ChangeLog:
    
            * Make-lang.in: Build borrowck.
            * checks/errors/borrowck/rust-borrow-checker.cc: New file.
            * checks/errors/borrowck/rust-borrow-checker.h: New file.
            * checks/errors/borrowck/rust-function-collector.h: New file.
    
    Signed-off-by: Jakub Dupak <d...@jakubdupak.com>

Diff:
---
 gcc/rust/Make-lang.in                              |   7 +
 .../checks/errors/borrowck/rust-borrow-checker.cc  |  43 +++++
 .../checks/errors/borrowck/rust-borrow-checker.h   |  39 ++++
 .../errors/borrowck/rust-function-collector.h      | 199 +++++++++++++++++++++
 4 files changed, 288 insertions(+)

diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in
index e6a2099f043c..f4b9eb5502e2 100644
--- a/gcc/rust/Make-lang.in
+++ b/gcc/rust/Make-lang.in
@@ -148,6 +148,7 @@ GRS_OBJS = \
     rust/rust-hir-type-check-stmt.o \
     rust/rust-hir-type-check-enumitem.o \
     rust/rust-hir-type-check-implitem.o \
+    rust/rust-borrow-checker.o \
     rust/rust-hir-dot-operator.o \
     rust/rust-hir-path-probe.o \
     rust/rust-type-util.o \
@@ -371,6 +372,7 @@ RUST_INCLUDES = -I $(srcdir)/rust \
        -I $(srcdir)/rust/checks/lints \
        -I $(srcdir)/rust/checks/errors \
        -I $(srcdir)/rust/checks/errors/privacy \
+       -I $(srcdir)/rust/checks/errors/borrowck \
        -I $(srcdir)/rust/util \
         -I $(srcdir)/rust/metadata \
                -I $(srcdir)/../libgrust
@@ -448,6 +450,11 @@ rust/%.o: rust/checks/errors/privacy/%.cc
        $(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
        $(POSTCOMPILE)
 
+# build borrow checking pass files in rust folder
+rust/%.o: rust/checks/errors/borrowck/%.cc
+       $(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
+       $(POSTCOMPILE)
+
 # build rust/metadata files in rust folder
 rust/%.o: rust/metadata/%.cc
        $(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc 
b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
new file mode 100644
index 000000000000..a6086b8a6956
--- /dev/null
+++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
@@ -0,0 +1,43 @@
+// Copyright (C) 2020-2023 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/>.
+
+#include "rust-borrow-checker.h"
+#include "rust-function-collector.h"
+
+namespace Rust {
+namespace HIR {
+
+BorrowChecker::BorrowChecker () = default;
+
+void
+BorrowChecker::go (HIR::Crate &crate)
+{
+  FunctionCollector collector;
+  collector.go (crate);
+
+  for (auto func ATTRIBUTE_UNUSED : collector.get_functions ())
+    {
+    }
+
+  for (auto closure ATTRIBUTE_UNUSED : collector.get_closures ())
+    {
+    }
+}
+
+} // namespace HIR
+} // namespace Rust
\ No newline at end of file
diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.h 
b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.h
new file mode 100644
index 000000000000..7df5fe788a0b
--- /dev/null
+++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.h
@@ -0,0 +1,39 @@
+// Copyright (C) 2020-2023 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_HIR_BORROW_CHECK_H
+#define RUST_HIR_BORROW_CHECK_H
+
+#include "rust-hir.h"
+
+namespace Rust {
+namespace HIR {
+
+class BorrowChecker
+{
+public:
+  BorrowChecker ();
+
+  /** Perform borrow-checking using polonius on an entire crate */
+  void go (HIR::Crate &crate);
+};
+
+} // namespace HIR
+} // namespace Rust
+
+#endif // RUST_HIR_BORROW_CHECK_H
diff --git a/gcc/rust/checks/errors/borrowck/rust-function-collector.h 
b/gcc/rust/checks/errors/borrowck/rust-function-collector.h
new file mode 100644
index 000000000000..f8d36ffe2b42
--- /dev/null
+++ b/gcc/rust/checks/errors/borrowck/rust-function-collector.h
@@ -0,0 +1,199 @@
+// Copyright (C) 2020-2023 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_HIR_FUNCTION_COLLECTOR_H
+#define RUST_HIR_FUNCTION_COLLECTOR_H
+
+#include "rust-hir-item.h"
+#include "rust-hir-visitor.h"
+#include "rust-hir.h"
+
+#include <vector>
+
+namespace Rust {
+
+// Collects all HIR items eligible for borrow checking.
+class FunctionCollector : public HIR::HIRFullVisitor
+{
+  std::vector<HIR::Function *> functions;
+  std::vector<HIR::ClosureExpr *> closures;
+
+public:
+  void go (HIR::Crate &crate) { visit_all (crate.get_items ()); }
+
+  WARN_UNUSED_RESULT const std::vector<HIR::Function *> &get_functions () const
+  {
+    return functions;
+  }
+
+  WARN_UNUSED_RESULT const std::vector<HIR::ClosureExpr *> &
+  get_closures () const
+  {
+    return closures;
+  }
+
+protected:
+  template <typename T> void visit_all (std::vector<std::unique_ptr<T>> &items)
+  {
+    for (std::unique_ptr<T> &item : items)
+      {
+       item->accept_vis (*this);
+      }
+  }
+
+  void visit (HIR::Function &function) override
+  {
+    functions.push_back (&function);
+    function.get_definition ()->accept_vis (*this);
+  }
+
+  void visit (HIR::ClosureExpr &closure) override
+  {
+    closures.push_back (&closure);
+    closure.get_expr ()->accept_vis (*this);
+  }
+
+  // TODO: recurse for nested closures and functions.
+public:
+  void visit (HIR::Lifetime &lifetime) override {}
+  void visit (HIR::LifetimeParam &lifetime_param) override {}
+  void visit (HIR::PathInExpression &path) override {}
+  void visit (HIR::TypePathSegment &segment) override {}
+  void visit (HIR::TypePathSegmentGeneric &segment) override {}
+  void visit (HIR::TypePathSegmentFunction &segment) override {}
+  void visit (HIR::TypePath &path) override {}
+  void visit (HIR::QualifiedPathInExpression &path) override {}
+  void visit (HIR::QualifiedPathInType &path) override {}
+  void visit (HIR::LiteralExpr &expr) override {}
+  void visit (HIR::BorrowExpr &expr) override {}
+  void visit (HIR::DereferenceExpr &expr) override {}
+  void visit (HIR::ErrorPropagationExpr &expr) override {}
+  void visit (HIR::NegationExpr &expr) override {}
+  void visit (HIR::ArithmeticOrLogicalExpr &expr) override {}
+  void visit (HIR::ComparisonExpr &expr) override {}
+  void visit (HIR::LazyBooleanExpr &expr) override {}
+  void visit (HIR::TypeCastExpr &expr) override {}
+  void visit (HIR::AssignmentExpr &expr) override {}
+  void visit (HIR::CompoundAssignmentExpr &expr) override {}
+  void visit (HIR::GroupedExpr &expr) override {}
+  void visit (HIR::ArrayElemsValues &elems) override {}
+  void visit (HIR::ArrayElemsCopied &elems) override {}
+  void visit (HIR::ArrayExpr &expr) override {}
+  void visit (HIR::ArrayIndexExpr &expr) override {}
+  void visit (HIR::TupleExpr &expr) override {}
+  void visit (HIR::TupleIndexExpr &expr) override {}
+  void visit (HIR::StructExprStruct &expr) override {}
+  void visit (HIR::StructExprFieldIdentifier &field) override {}
+  void visit (HIR::StructExprFieldIdentifierValue &field) override {}
+  void visit (HIR::StructExprFieldIndexValue &field) override {}
+  void visit (HIR::StructExprStructFields &expr) override {}
+  void visit (HIR::StructExprStructBase &expr) override {}
+  void visit (HIR::CallExpr &expr) override {}
+  void visit (HIR::MethodCallExpr &expr) override {}
+  void visit (HIR::FieldAccessExpr &expr) override {}
+  void visit (HIR::BlockExpr &expr) override {}
+  void visit (HIR::ContinueExpr &expr) override {}
+  void visit (HIR::BreakExpr &expr) override {}
+  void visit (HIR::RangeFromToExpr &expr) override {}
+  void visit (HIR::RangeFromExpr &expr) override {}
+  void visit (HIR::RangeToExpr &expr) override {}
+  void visit (HIR::RangeFullExpr &expr) override {}
+  void visit (HIR::RangeFromToInclExpr &expr) override {}
+  void visit (HIR::RangeToInclExpr &expr) override {}
+  void visit (HIR::ReturnExpr &expr) override {}
+  void visit (HIR::UnsafeBlockExpr &expr) override {}
+  void visit (HIR::LoopExpr &expr) override {}
+  void visit (HIR::WhileLoopExpr &expr) override {}
+  void visit (HIR::WhileLetLoopExpr &expr) override {}
+  void visit (HIR::IfExpr &expr) override {}
+  void visit (HIR::IfExprConseqElse &expr) override {}
+  void visit (HIR::IfLetExpr &expr) override {}
+  void visit (HIR::IfLetExprConseqElse &expr) override {}
+  void visit (HIR::MatchExpr &expr) override {}
+  void visit (HIR::AwaitExpr &expr) override {}
+  void visit (HIR::AsyncBlockExpr &expr) override {}
+  void visit (HIR::TypeParam &param) override {}
+  void visit (HIR::ConstGenericParam &param) override {}
+  void visit (HIR::LifetimeWhereClauseItem &item) override {}
+  void visit (HIR::TypeBoundWhereClauseItem &item) override {}
+  void visit (HIR::Module &module) override {}
+  void visit (HIR::ExternCrate &crate) override {}
+  void visit (HIR::UseTreeGlob &use_tree) override {}
+  void visit (HIR::UseTreeList &use_tree) override {}
+  void visit (HIR::UseTreeRebind &use_tree) override {}
+  void visit (HIR::UseDeclaration &use_decl) override {}
+  void visit (HIR::TypeAlias &type_alias) override {}
+  void visit (HIR::StructStruct &struct_item) override {}
+  void visit (HIR::TupleStruct &tuple_struct) override {}
+  void visit (HIR::EnumItem &item) override {}
+  void visit (HIR::EnumItemTuple &item) override {}
+  void visit (HIR::EnumItemStruct &item) override {}
+  void visit (HIR::EnumItemDiscriminant &item) override {}
+  void visit (HIR::Enum &enum_item) override {}
+  void visit (HIR::Union &union_item) override {}
+  void visit (HIR::ConstantItem &const_item) override {}
+  void visit (HIR::StaticItem &static_item) override {}
+  void visit (HIR::TraitItemFunc &item) override {}
+  void visit (HIR::TraitItemConst &item) override {}
+  void visit (HIR::TraitItemType &item) override {}
+  void visit (HIR::Trait &trait) override {}
+  void visit (HIR::ImplBlock &impl) override {}
+  void visit (HIR::ExternalStaticItem &item) override {}
+  void visit (HIR::ExternalFunctionItem &item) override {}
+  void visit (HIR::ExternBlock &block) override {}
+  void visit (HIR::LiteralPattern &pattern) override {}
+  void visit (HIR::IdentifierPattern &pattern) override {}
+  void visit (HIR::WildcardPattern &pattern) override {}
+  void visit (HIR::RangePatternBoundLiteral &bound) override {}
+  void visit (HIR::RangePatternBoundPath &bound) override {}
+  void visit (HIR::RangePatternBoundQualPath &bound) override {}
+  void visit (HIR::RangePattern &pattern) override {}
+  void visit (HIR::ReferencePattern &pattern) override {}
+  void visit (HIR::StructPatternFieldTuplePat &field) override {}
+  void visit (HIR::StructPatternFieldIdentPat &field) override {}
+  void visit (HIR::StructPatternFieldIdent &field) override {}
+  void visit (HIR::StructPattern &pattern) override {}
+  void visit (HIR::TupleStructItemsNoRange &tuple_items) override {}
+  void visit (HIR::TupleStructItemsRange &tuple_items) override {}
+  void visit (HIR::TupleStructPattern &pattern) override {}
+  void visit (HIR::TuplePatternItemsMultiple &tuple_items) override {}
+  void visit (HIR::TuplePatternItemsRanged &tuple_items) override {}
+  void visit (HIR::TuplePattern &pattern) override {}
+  void visit (HIR::SlicePattern &pattern) override {}
+  void visit (HIR::AltPattern &pattern) override {}
+  void visit (HIR::EmptyStmt &stmt) override {}
+  void visit (HIR::LetStmt &stmt) override {}
+  void visit (HIR::ExprStmt &stmt) override {}
+  void visit (HIR::TraitBound &bound) override {}
+  void visit (HIR::ImplTraitType &type) override {}
+  void visit (HIR::TraitObjectType &type) override {}
+  void visit (HIR::ParenthesisedType &type) override {}
+  void visit (HIR::ImplTraitTypeOneBound &type) override {}
+  void visit (HIR::TupleType &type) override {}
+  void visit (HIR::NeverType &type) override {}
+  void visit (HIR::RawPointerType &type) override {}
+  void visit (HIR::ReferenceType &type) override {}
+  void visit (HIR::ArrayType &type) override {}
+  void visit (HIR::SliceType &type) override {}
+  void visit (HIR::InferredType &type) override {}
+  void visit (HIR::BareFunctionType &type) override {}
+};
+
+} // namespace Rust
+
+#endif // RUST_HIR_FUNCTION_COLLECTOR_H

Reply via email to