1 // Copyright (C) 2020-2022 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #include "rust-pub-restricted-visitor.h"
21 #include "rust-hir-item.h"
27 PubRestrictedVisitor::is_restriction_valid (NodeId item_id
,
28 const Location
&locus
)
30 ModuleVisibility visibility
;
32 // If there is no visibility in the mappings, then the item is private and
33 // does not contain any restriction
34 // FIXME: Is that correct?
35 if (!mappings
.lookup_visibility (item_id
, visibility
))
38 for (auto mod
= module_stack
.rbegin (); mod
!= module_stack
.rend (); mod
++)
39 if (*mod
== visibility
.get_module_id ())
42 rust_error_at (locus
, "restricted path is not an ancestor of the "
47 PubRestrictedVisitor::PubRestrictedVisitor (Analysis::Mappings
&mappings
)
52 PubRestrictedVisitor::go (HIR::Crate
&crate
)
54 // The `crate` module will always be present
55 module_stack
.emplace_back (crate
.get_mappings ().get_defid ());
57 // FIXME: When do we insert `super`? `self`?
58 // We need wrapper function for these
60 for (auto &item
: crate
.items
)
62 if (item
->get_hir_kind () == HIR::Node::VIS_ITEM
)
64 auto vis_item
= static_cast<HIR::VisItem
*> (item
.get ());
65 vis_item
->accept_vis (*this);
71 PubRestrictedVisitor::visit (HIR::Module
&mod
)
73 // FIXME: We need to update `super` and `self` here
74 module_stack
.push_back (mod
.get_mappings ().get_defid ());
76 is_restriction_valid (mod
.get_mappings ().get_nodeid (), mod
.get_locus ());
78 for (auto &item
: mod
.get_items ())
80 if (item
->get_hir_kind () == HIR::Node::VIS_ITEM
)
82 auto vis_item
= static_cast<HIR::VisItem
*> (item
.get ());
83 vis_item
->accept_vis (*this);
87 module_stack
.pop_back ();
91 PubRestrictedVisitor::visit (HIR::ExternCrate
&crate
)
93 is_restriction_valid (crate
.get_mappings ().get_nodeid (),
98 PubRestrictedVisitor::visit (HIR::UseDeclaration
&use_decl
)
100 is_restriction_valid (use_decl
.get_mappings ().get_nodeid (),
101 use_decl
.get_locus ());
105 PubRestrictedVisitor::visit (HIR::Function
&func
)
107 is_restriction_valid (func
.get_mappings ().get_nodeid (), func
.get_locus ());
111 PubRestrictedVisitor::visit (HIR::TypeAlias
&type_alias
)
113 is_restriction_valid (type_alias
.get_mappings ().get_nodeid (),
114 type_alias
.get_locus ());
118 PubRestrictedVisitor::visit (HIR::StructStruct
&struct_item
)
120 is_restriction_valid (struct_item
.get_mappings ().get_nodeid (),
121 struct_item
.get_locus ());
122 // FIXME: Check fields here as well
126 PubRestrictedVisitor::visit (HIR::TupleStruct
&tuple_struct
)
128 is_restriction_valid (tuple_struct
.get_mappings ().get_nodeid (),
129 tuple_struct
.get_locus ());
130 // FIXME: Check fields here as well
134 PubRestrictedVisitor::visit (HIR::Enum
&enum_item
)
136 is_restriction_valid (enum_item
.get_mappings ().get_nodeid (),
137 enum_item
.get_locus ());
141 PubRestrictedVisitor::visit (HIR::Union
&union_item
)
143 is_restriction_valid (union_item
.get_mappings ().get_nodeid (),
144 union_item
.get_locus ());
148 PubRestrictedVisitor::visit (HIR::ConstantItem
&const_item
)
150 is_restriction_valid (const_item
.get_mappings ().get_nodeid (),
151 const_item
.get_locus ());
155 PubRestrictedVisitor::visit (HIR::StaticItem
&static_item
)
157 is_restriction_valid (static_item
.get_mappings ().get_nodeid (),
158 static_item
.get_locus ());
162 PubRestrictedVisitor::visit (HIR::Trait
&trait
)
164 is_restriction_valid (trait
.get_mappings ().get_nodeid (),
169 PubRestrictedVisitor::visit (HIR::ImplBlock
&impl
)
171 is_restriction_valid (impl
.get_mappings ().get_nodeid (), impl
.get_locus ());
175 PubRestrictedVisitor::visit (HIR::ExternBlock
&block
)
177 is_restriction_valid (block
.get_mappings ().get_nodeid (),
181 } // namespace Privacy