Skip to content

Commit 77afec7

Browse files
committed
privacy: visibility: Add base for ModuleVisibility resolver
1 parent ca59275 commit 77afec7

File tree

6 files changed

+224
-0
lines changed

6 files changed

+224
-0
lines changed

gcc/rust/Make-lang.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ GRS_OBJS = \
9393
rust/rust-privacy-check.o \
9494
rust/rust-privacy-ctx.o \
9595
rust/rust-reachability.o \
96+
rust/rust-visibility-resolver.o \
9697
rust/rust-tyty.o \
9798
rust/rust-tyctx.o \
9899
rust/rust-tyty-bounds.o \

gcc/rust/privacy/rust-privacy-check.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "rust-privacy-check.h"
2020
#include "rust-reachability.h"
2121
#include "rust-hir-type-check.h"
22+
#include "rust-hir-map.h"
23+
#include "rust-visibility-resolver.h"
2224

2325
extern bool
2426
saw_errors (void);
@@ -29,10 +31,16 @@ void
2931
Resolver::resolve (HIR::Crate &crate)
3032
{
3133
PrivacyContext ctx;
34+
auto mappings = Analysis::Mappings::get ();
35+
36+
auto resolver = VisibilityResolver (*mappings);
37+
resolver.go (crate);
38+
3239
auto ty_ctx = ::Rust::Resolver::TypeCheckContext::get ();
3340
auto visitor = ReachabilityVisitor (ctx, *ty_ctx);
3441

3542
const auto &items = crate.items;
43+
3644
for (auto &item : items)
3745
{
3846
if (item->get_hir_kind () == HIR::Node::VIS_ITEM)

gcc/rust/privacy/rust-privacy-common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ class ModuleVisibility
3030
public:
3131
enum Type
3232
{
33+
Unknown,
3334
Private,
3435
Public,
3536
Restricted,
3637
};
3738

39+
ModuleVisibility () : kind (Unknown), module_id (UNKNOWN_DEFID) {}
40+
3841
static ModuleVisibility create_restricted (DefId module_id)
3942
{
4043
return ModuleVisibility (Type::Restricted, module_id);

gcc/rust/privacy/rust-reachability.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class ReachabilityVisitor : public HIR::HIRVisItemVisitor
4646
: current_level (ReachLevel::Reachable), ctx (ctx), ty_ctx (ty_ctx)
4747
{}
4848

49+
// FIXME: Add `go` method which takes an `HIR::Crate &` as argument
50+
4951
/**
5052
* Visit all the predicates of all the generic types of a given item, marking
5153
* them as reachable or not.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright (C) 2020-2022 Free Software Foundation, Inc.
2+
3+
// This file is part of GCC.
4+
5+
// GCC is free software; you can redistribute it and/or modify it under
6+
// the terms of the GNU General Public License as published by the Free
7+
// Software Foundation; either version 3, or (at your option) any later
8+
// version.
9+
10+
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11+
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
// for more details.
14+
15+
// You should have received a copy of the GNU General Public License
16+
// along with GCC; see the file COPYING3. If not see
17+
// <http://www.gnu.org/licenses/>.
18+
19+
#include "rust-visibility-resolver.h"
20+
#include "rust-hir.h"
21+
#include "rust-hir-item.h"
22+
23+
namespace Rust {
24+
namespace Privacy {
25+
26+
VisibilityResolver::VisibilityResolver (Analysis::Mappings &mappings)
27+
: mappings (mappings)
28+
{
29+
// FIXME: Insert a top module (crate) inside the module_stack
30+
// FIXME: Insert the visibility of the crate in the mappings maybe?
31+
}
32+
33+
void
34+
VisibilityResolver::go (HIR::Crate &crate)
35+
{
36+
for (auto &item : crate.items)
37+
{
38+
if (item->get_hir_kind () == HIR::Node::VIS_ITEM)
39+
{
40+
auto vis_item = static_cast<HIR::VisItem *> (item.get ());
41+
vis_item->accept_vis (*this);
42+
}
43+
}
44+
}
45+
46+
bool
47+
VisibilityResolver::resolve_visibility (const HIR::Visibility &visibility,
48+
ModuleVisibility &to_resolve)
49+
{
50+
return false;
51+
}
52+
53+
DefId
54+
VisibilityResolver::peek_module ()
55+
{
56+
// We're always inserting a top module - the crate
57+
// But we have to check otherwise `.back()` is UB
58+
if (module_stack.empty ())
59+
gcc_unreachable ();
60+
61+
return module_stack.back ();
62+
}
63+
64+
void
65+
VisibilityResolver::visit (HIR::Module &mod)
66+
{}
67+
68+
void
69+
VisibilityResolver::visit (HIR::ExternCrate &crate)
70+
{}
71+
72+
void
73+
VisibilityResolver::visit (HIR::UseDeclaration &use_decl)
74+
{}
75+
76+
void
77+
VisibilityResolver::visit (HIR::Function &func)
78+
{}
79+
80+
void
81+
VisibilityResolver::visit (HIR::TypeAlias &type_alias)
82+
{}
83+
84+
void
85+
VisibilityResolver::visit (HIR::StructStruct &struct_item)
86+
{}
87+
88+
void
89+
VisibilityResolver::visit (HIR::TupleStruct &tuple_struct)
90+
{}
91+
92+
void
93+
VisibilityResolver::visit (HIR::Enum &enum_item)
94+
{}
95+
96+
void
97+
VisibilityResolver::visit (HIR::Union &union_item)
98+
{}
99+
100+
void
101+
VisibilityResolver::visit (HIR::ConstantItem &const_item)
102+
{}
103+
104+
void
105+
VisibilityResolver::visit (HIR::StaticItem &static_item)
106+
{}
107+
108+
void
109+
VisibilityResolver::visit (HIR::Trait &trait)
110+
{}
111+
112+
void
113+
VisibilityResolver::visit (HIR::ImplBlock &impl)
114+
{}
115+
116+
void
117+
VisibilityResolver::visit (HIR::ExternBlock &block)
118+
{}
119+
120+
} // namespace Privacy
121+
} // namespace Rust
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright (C) 2020-2022 Free Software Foundation, Inc.
2+
3+
// This file is part of GCC.
4+
5+
// GCC is free software; you can redistribute it and/or modify it under
6+
// the terms of the GNU General Public License as published by the Free
7+
// Software Foundation; either version 3, or (at your option) any later
8+
// version.
9+
10+
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11+
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
// for more details.
14+
15+
// You should have received a copy of the GNU General Public License
16+
// along with GCC; see the file COPYING3. If not see
17+
// <http://www.gnu.org/licenses/>.
18+
19+
#ifndef RUST_VISIBILITY_H
20+
#define RUST_VISIBILITY_H
21+
22+
#include "rust-hir.h"
23+
#include "rust-hir-expr.h"
24+
#include "rust-hir-stmt.h"
25+
#include "rust-hir-item.h"
26+
#include "rust-hir-map.h"
27+
#include "rust-hir-visitor.h"
28+
29+
namespace Rust {
30+
namespace Privacy {
31+
32+
class VisibilityResolver : public HIR::HIRVisItemVisitor
33+
{
34+
public:
35+
VisibilityResolver (Analysis::Mappings &mappings);
36+
37+
/**
38+
* Perform visibility resolving on an entire crate
39+
*/
40+
void go (HIR::Crate &crate);
41+
42+
/**
43+
* Resolve the visibility of an item to its ModuleVisibility. This function
44+
* emits errors if necessary. The contents of the to_resolve parameter will be
45+
* overwritten on success.
46+
*
47+
* @param visibility Visibility of the item to resolve
48+
* @param to_resolve ModuleVisibility reference to fill on success.
49+
*
50+
* @return false on error, true if the resolving was successful.
51+
*/
52+
bool resolve_visibility (const HIR::Visibility &visibility,
53+
ModuleVisibility &to_resolve);
54+
55+
/**
56+
* Get the DefId of the parent module we are currently visiting.
57+
*
58+
* @return UNKNOWN_DEFID if the module stack is empty, a valid `DefId`
59+
* otherwise
60+
*/
61+
DefId peek_module ();
62+
63+
virtual void visit (HIR::Module &mod);
64+
virtual void visit (HIR::ExternCrate &crate);
65+
virtual void visit (HIR::UseDeclaration &use_decl);
66+
virtual void visit (HIR::Function &func);
67+
virtual void visit (HIR::TypeAlias &type_alias);
68+
virtual void visit (HIR::StructStruct &struct_item);
69+
virtual void visit (HIR::TupleStruct &tuple_struct);
70+
virtual void visit (HIR::Enum &enum_item);
71+
virtual void visit (HIR::Union &union_item);
72+
virtual void visit (HIR::ConstantItem &const_item);
73+
virtual void visit (HIR::StaticItem &static_item);
74+
virtual void visit (HIR::Trait &trait);
75+
virtual void visit (HIR::ImplBlock &impl);
76+
virtual void visit (HIR::ExternBlock &block);
77+
78+
private:
79+
/* Mappings to insert visibilities into */
80+
Analysis::Mappings &mappings;
81+
82+
/* Stack of modules visited by this visitor */
83+
std::vector<DefId> module_stack;
84+
};
85+
86+
} // namespace Privacy
87+
} // namespace Rust
88+
89+
#endif // !RUST_VISIBILITY_H

0 commit comments

Comments
 (0)