Skip to content

Commit 2a264a3

Browse files
committed
hir: Cleanup Visibility struct
The HIR::Visibility struct was extremely similar to the AST::Visibility one. However, we do not need to keep as much information at the HIR level: Syntactic sugar such as pub(crate) can be kept as the desugared form, which is pub(in crate). Likewise, pub(self) can be desugared to pub(in self) which amounts to having a private item.
1 parent 6845803 commit 2a264a3

File tree

4 files changed

+24
-77
lines changed

4 files changed

+24
-77
lines changed

gcc/rust/backend/rust-compile-base.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ HIRCompileBase::setup_attributes_on_fndecl (
3737
{
3838
// if its the main fn or pub visibility mark its as DECL_PUBLIC
3939
// please see https://github.com/Rust-GCC/gccrs/pull/137
40-
bool is_pub
41-
= visibility.get_vis_type () != HIR::Visibility::PublicVisType::NONE;
40+
bool is_pub = visibility.get_vis_type () == HIR::Visibility::VisType::PUBLIC;
4241
if (is_main_entry_point || is_pub)
4342
{
4443
TREE_PUBLIC (fndecl) = 1;

gcc/rust/backend/rust-compile-implitem.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,8 @@ CompileTraitItem::visit (HIR::TraitItemFunc &func)
8484
&canonical_path);
8585
rust_assert (ok);
8686

87-
// FIXME
88-
HIR::Visibility vis (HIR::Visibility::PublicVisType::NONE,
89-
AST::SimplePath::create_empty ());
87+
// FIXME: Get from lowering the item's visibility instead
88+
auto vis = HIR::Visibility::create_public ();
9089
HIR::TraitFunctionDecl &function = func.get_decl ();
9190
tree fndecl
9291
= compile_function (ctx, function.get_function_name (),

gcc/rust/hir/tree/rust-hir-full-test.cc

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,12 @@ Crate::as_string () const
118118
std::string
119119
Visibility::as_string () const
120120
{
121-
switch (public_vis_type)
121+
switch (vis_type)
122122
{
123-
case NONE:
124-
return std::string ("pub");
125-
case CRATE:
126-
return std::string ("pub(crate)");
127-
case SELF:
128-
return std::string ("pub(self)");
129-
case SUPER:
130-
return std::string ("pub(super)");
131-
case IN_PATH:
132-
return std::string ("pub(in ") + in_path.as_string () + std::string (")");
123+
case PRIVATE:
124+
return std::string ("private");
125+
case PUBLIC:
126+
return std::string ("pub(in ") + path.as_string () + std::string (")");
133127
default:
134128
gcc_unreachable ();
135129
}

gcc/rust/hir/tree/rust-hir-item.h

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -553,91 +553,46 @@ struct FunctionParam
553553
struct Visibility
554554
{
555555
public:
556-
enum PublicVisType
556+
enum VisType
557557
{
558-
NONE,
559-
CRATE,
560-
SELF,
561-
SUPER,
562-
IN_PATH
558+
PRIVATE,
559+
PUBLIC,
560+
ERROR,
563561
};
564562

565563
private:
566-
// if vis is public, one of these
567-
PublicVisType public_vis_type;
568-
// Only assigned if public_vis_type is IN_PATH
569-
AST::SimplePath in_path;
564+
VisType vis_type;
565+
AST::SimplePath path;
570566

571567
// should this store location info?
572568

573569
public:
574570
// Creates a Visibility - TODO make constructor protected or private?
575-
Visibility (PublicVisType public_vis_type, AST::SimplePath in_path)
576-
: public_vis_type (public_vis_type), in_path (std::move (in_path))
571+
Visibility (VisType vis_type,
572+
AST::SimplePath path = AST::SimplePath::create_empty ())
573+
: vis_type (vis_type), path (std::move (path))
577574
{}
578575

579576
// Returns whether visibility is in an error state.
580-
bool is_error () const
581-
{
582-
return public_vis_type == IN_PATH && in_path.is_empty ();
583-
}
577+
bool is_error () const { return vis_type == ERROR; }
584578

585579
// Creates an error visibility.
586580
static Visibility create_error ()
587581
{
588-
return Visibility (IN_PATH, AST::SimplePath::create_empty ());
582+
return Visibility (ERROR, AST::SimplePath::create_empty ());
589583
}
590584

591-
// Unique pointer custom clone function
592-
/*std::unique_ptr<Visibility> clone_visibility() const {
593-
return std::unique_ptr<Visibility>(clone_visibility_impl());
594-
}*/
595-
596-
/* TODO: think of a way to only allow valid Visibility states - polymorphism
597-
* is one idea but may be too resource-intensive. */
598-
599-
// Creates a public visibility with no further features/arguments.
585+
// Creates a public visibility.
586+
// FIXME: Remove this function: We should not be calling it anymore and
587+
// instead we should be using `translate_visibility`
600588
static Visibility create_public ()
601589
{
602-
return Visibility (NONE, AST::SimplePath::create_empty ());
603-
}
604-
605-
// Creates a public visibility with crate-relative paths or whatever.
606-
static Visibility create_crate ()
607-
{
608-
return Visibility (CRATE, AST::SimplePath::create_empty ());
609-
}
610-
611-
// Creates a public visibility with self-relative paths or whatever.
612-
static Visibility create_self ()
613-
{
614-
return Visibility (SELF, AST::SimplePath::create_empty ());
615-
}
616-
617-
// Creates a public visibility with parent module-relative paths or
618-
// whatever.
619-
static Visibility create_super ()
620-
{
621-
return Visibility (SUPER, AST::SimplePath::create_empty ());
622-
}
623-
624-
// Creates a public visibility with a given path or whatever.
625-
static Visibility create_in_path (AST::SimplePath in_path)
626-
{
627-
return Visibility (IN_PATH, std::move (in_path));
590+
return Visibility (ERROR, AST::SimplePath::create_empty ());
628591
}
629592

630-
PublicVisType get_vis_type () const { return public_vis_type; }
593+
VisType get_vis_type () const { return vis_type; }
631594

632595
std::string as_string () const;
633-
634-
protected:
635-
// Clone function implementation - not currently virtual but may be if
636-
// polymorphism used
637-
/*virtual*/ Visibility *clone_visibility_impl () const
638-
{
639-
return new Visibility (*this);
640-
}
641596
};
642597

643598
// Item that supports visibility - abstract base class

0 commit comments

Comments
 (0)