privacy: visibility: Add base for ModuleVisibility resolver

This commit is contained in:
Arthur Cohen 2022-04-22 10:00:03 +02:00
parent ca59275cc9
commit 77afec780a
6 changed files with 224 additions and 0 deletions

View File

@ -93,6 +93,7 @@ GRS_OBJS = \
rust/rust-privacy-check.o \
rust/rust-privacy-ctx.o \
rust/rust-reachability.o \
rust/rust-visibility-resolver.o \
rust/rust-tyty.o \
rust/rust-tyctx.o \
rust/rust-tyty-bounds.o \

View File

@ -19,6 +19,8 @@
#include "rust-privacy-check.h"
#include "rust-reachability.h"
#include "rust-hir-type-check.h"
#include "rust-hir-map.h"
#include "rust-visibility-resolver.h"
extern bool
saw_errors (void);
@ -29,10 +31,16 @@ void
Resolver::resolve (HIR::Crate &crate)
{
PrivacyContext ctx;
auto mappings = Analysis::Mappings::get ();
auto resolver = VisibilityResolver (*mappings);
resolver.go (crate);
auto ty_ctx = ::Rust::Resolver::TypeCheckContext::get ();
auto visitor = ReachabilityVisitor (ctx, *ty_ctx);
const auto &items = crate.items;
for (auto &item : items)
{
if (item->get_hir_kind () == HIR::Node::VIS_ITEM)

View File

@ -30,11 +30,14 @@ class ModuleVisibility
public:
enum Type
{
Unknown,
Private,
Public,
Restricted,
};
ModuleVisibility () : kind (Unknown), module_id (UNKNOWN_DEFID) {}
static ModuleVisibility create_restricted (DefId module_id)
{
return ModuleVisibility (Type::Restricted, module_id);

View File

@ -46,6 +46,8 @@ public:
: current_level (ReachLevel::Reachable), ctx (ctx), ty_ctx (ty_ctx)
{}
// FIXME: Add `go` method which takes an `HIR::Crate &` as argument
/**
* Visit all the predicates of all the generic types of a given item, marking
* them as reachable or not.

View File

@ -0,0 +1,121 @@
// 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/>.
#include "rust-visibility-resolver.h"
#include "rust-hir.h"
#include "rust-hir-item.h"
namespace Rust {
namespace Privacy {
VisibilityResolver::VisibilityResolver (Analysis::Mappings &mappings)
: mappings (mappings)
{
// FIXME: Insert a top module (crate) inside the module_stack
// FIXME: Insert the visibility of the crate in the mappings maybe?
}
void
VisibilityResolver::go (HIR::Crate &crate)
{
for (auto &item : crate.items)
{
if (item->get_hir_kind () == HIR::Node::VIS_ITEM)
{
auto vis_item = static_cast<HIR::VisItem *> (item.get ());
vis_item->accept_vis (*this);
}
}
}
bool
VisibilityResolver::resolve_visibility (const HIR::Visibility &visibility,
ModuleVisibility &to_resolve)
{
return false;
}
DefId
VisibilityResolver::peek_module ()
{
// We're always inserting a top module - the crate
// But we have to check otherwise `.back()` is UB
if (module_stack.empty ())
gcc_unreachable ();
return module_stack.back ();
}
void
VisibilityResolver::visit (HIR::Module &mod)
{}
void
VisibilityResolver::visit (HIR::ExternCrate &crate)
{}
void
VisibilityResolver::visit (HIR::UseDeclaration &use_decl)
{}
void
VisibilityResolver::visit (HIR::Function &func)
{}
void
VisibilityResolver::visit (HIR::TypeAlias &type_alias)
{}
void
VisibilityResolver::visit (HIR::StructStruct &struct_item)
{}
void
VisibilityResolver::visit (HIR::TupleStruct &tuple_struct)
{}
void
VisibilityResolver::visit (HIR::Enum &enum_item)
{}
void
VisibilityResolver::visit (HIR::Union &union_item)
{}
void
VisibilityResolver::visit (HIR::ConstantItem &const_item)
{}
void
VisibilityResolver::visit (HIR::StaticItem &static_item)
{}
void
VisibilityResolver::visit (HIR::Trait &trait)
{}
void
VisibilityResolver::visit (HIR::ImplBlock &impl)
{}
void
VisibilityResolver::visit (HIR::ExternBlock &block)
{}
} // namespace Privacy
} // namespace Rust

View File

@ -0,0 +1,89 @@
// 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_VISIBILITY_H
#define RUST_VISIBILITY_H
#include "rust-hir.h"
#include "rust-hir-expr.h"
#include "rust-hir-stmt.h"
#include "rust-hir-item.h"
#include "rust-hir-map.h"
#include "rust-hir-visitor.h"
namespace Rust {
namespace Privacy {
class VisibilityResolver : public HIR::HIRVisItemVisitor
{
public:
VisibilityResolver (Analysis::Mappings &mappings);
/**
* Perform visibility resolving on an entire crate
*/
void go (HIR::Crate &crate);
/**
* Resolve the visibility of an item to its ModuleVisibility. This function
* emits errors if necessary. The contents of the to_resolve parameter will be
* overwritten on success.
*
* @param visibility Visibility of the item to resolve
* @param to_resolve ModuleVisibility reference to fill on success.
*
* @return false on error, true if the resolving was successful.
*/
bool resolve_visibility (const HIR::Visibility &visibility,
ModuleVisibility &to_resolve);
/**
* Get the DefId of the parent module we are currently visiting.
*
* @return UNKNOWN_DEFID if the module stack is empty, a valid `DefId`
* otherwise
*/
DefId peek_module ();
virtual void visit (HIR::Module &mod);
virtual void visit (HIR::ExternCrate &crate);
virtual void visit (HIR::UseDeclaration &use_decl);
virtual void visit (HIR::Function &func);
virtual void visit (HIR::TypeAlias &type_alias);
virtual void visit (HIR::StructStruct &struct_item);
virtual void visit (HIR::TupleStruct &tuple_struct);
virtual void visit (HIR::Enum &enum_item);
virtual void visit (HIR::Union &union_item);
virtual void visit (HIR::ConstantItem &const_item);
virtual void visit (HIR::StaticItem &static_item);
virtual void visit (HIR::Trait &trait);
virtual void visit (HIR::ImplBlock &impl);
virtual void visit (HIR::ExternBlock &block);
private:
/* Mappings to insert visibilities into */
Analysis::Mappings &mappings;
/* Stack of modules visited by this visitor */
std::vector<DefId> module_stack;
};
} // namespace Privacy
} // namespace Rust
#endif // !RUST_VISIBILITY_H