Skip to content

Commit 497ee70

Browse files
Merge #1111
1111: Add AST Private Visibilities r=CohenArthur a=CohenArthur When parsing a visibility in `parse_visibility`, it is not an error to not have a pub token: It simply means we want to create a private visibility. If we had C++14 or another language, we could instead represent all visibilities as an optional<AST::Visibility> where the Visibility class would not need to change. But I think the best course of action for our case is to instead keep visibilities even when they are private and have a special case in the `VisKind` enumeration. This also enables HIR lowering of visibilities to be performed properly for private items Co-authored-by: Arthur Cohen <[email protected]>
2 parents c1639be + 03cb435 commit 497ee70

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

gcc/rust/ast/rust-ast-full-test.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,19 @@ SimplePath::as_string () const
306306
std::string
307307
Visibility::as_string () const
308308
{
309-
switch (public_vis_type)
309+
switch (vis_type)
310310
{
311-
case NONE:
311+
case PRIV:
312+
return std::string ("");
313+
case PUB:
312314
return std::string ("pub");
313-
case CRATE:
315+
case PUB_CRATE:
314316
return std::string ("pub(crate)");
315-
case SELF:
317+
case PUB_SELF:
316318
return std::string ("pub(self)");
317-
case SUPER:
319+
case PUB_SUPER:
318320
return std::string ("pub(super)");
319-
case IN_PATH:
321+
case PUB_IN_PATH:
320322
return std::string ("pub(in ") + in_path.as_string () + std::string (")");
321323
default:
322324
gcc_unreachable ();

gcc/rust/ast/rust-item.h

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -609,41 +609,44 @@ struct FunctionParam
609609
struct Visibility
610610
{
611611
public:
612-
enum PublicVisType
613-
{
614-
NONE,
615-
CRATE,
616-
SELF,
617-
SUPER,
618-
IN_PATH
612+
enum VisType
613+
{
614+
PRIV,
615+
PUB,
616+
PUB_CRATE,
617+
PUB_SELF,
618+
PUB_SUPER,
619+
PUB_IN_PATH
619620
};
620621

621622
private:
622-
// if vis is public, one of these
623-
PublicVisType public_vis_type;
624-
// Only assigned if public_vis_type is IN_PATH
623+
VisType vis_type;
624+
// Only assigned if vis_type is IN_PATH
625625
SimplePath in_path;
626626

627627
// should this store location info?
628628

629629
public:
630630
// Creates a Visibility - TODO make constructor protected or private?
631-
Visibility (PublicVisType public_vis_type, SimplePath in_path)
632-
: public_vis_type (public_vis_type), in_path (std::move (in_path))
631+
Visibility (VisType vis_type, SimplePath in_path)
632+
: vis_type (vis_type), in_path (std::move (in_path))
633633
{}
634634

635-
PublicVisType get_public_vis_type () const { return public_vis_type; }
635+
VisType get_public_vis_type () const { return vis_type; }
636636

637637
// Returns whether visibility is in an error state.
638638
bool is_error () const
639639
{
640-
return public_vis_type == IN_PATH && in_path.is_empty ();
640+
return vis_type == PUB_IN_PATH && in_path.is_empty ();
641641
}
642642

643+
// Returns whether visibility is public or not.
644+
bool is_public () const { return vis_type != PRIV && !is_error (); }
645+
643646
// Creates an error visibility.
644647
static Visibility create_error ()
645648
{
646-
return Visibility (IN_PATH, SimplePath::create_empty ());
649+
return Visibility (PUB_IN_PATH, SimplePath::create_empty ());
647650
}
648651

649652
// Unique pointer custom clone function
@@ -657,32 +660,38 @@ struct Visibility
657660
// Creates a public visibility with no further features/arguments.
658661
static Visibility create_public ()
659662
{
660-
return Visibility (NONE, SimplePath::create_empty ());
663+
return Visibility (PUB, SimplePath::create_empty ());
661664
}
662665

663666
// Creates a public visibility with crate-relative paths or whatever.
664667
static Visibility create_crate ()
665668
{
666-
return Visibility (CRATE, SimplePath::create_empty ());
669+
return Visibility (PUB_CRATE, SimplePath::create_empty ());
667670
}
668671

669672
// Creates a public visibility with self-relative paths or whatever.
670673
static Visibility create_self ()
671674
{
672-
return Visibility (SELF, SimplePath::create_empty ());
675+
return Visibility (PUB_SELF, SimplePath::create_empty ());
673676
}
674677

675678
// Creates a public visibility with parent module-relative paths or
676679
// whatever.
677680
static Visibility create_super ()
678681
{
679-
return Visibility (SUPER, SimplePath::create_empty ());
682+
return Visibility (PUB_SUPER, SimplePath::create_empty ());
683+
}
684+
685+
// Creates a private visibility
686+
static Visibility create_private ()
687+
{
688+
return Visibility (PRIV, SimplePath::create_empty ());
680689
}
681690

682691
// Creates a public visibility with a given path or whatever.
683692
static Visibility create_in_path (SimplePath in_path)
684693
{
685-
return Visibility (IN_PATH, std::move (in_path));
694+
return Visibility (PUB_IN_PATH, std::move (in_path));
686695
}
687696

688697
std::string as_string () const;
@@ -938,7 +947,7 @@ class VisItem : public Item
938947
public:
939948
/* Does the item have some kind of public visibility (non-default
940949
* visibility)? */
941-
bool has_visibility () const { return !visibility.is_error (); }
950+
bool has_visibility () const { return visibility.is_public (); }
942951

943952
std::string as_string () const override;
944953

gcc/rust/hir/rust-ast-lower.cc

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,21 @@ translate_visibility (const AST::Visibility &vis)
3636
if (vis.is_error ())
3737
return Visibility::create_error ();
3838

39-
// FIXME: ... And then use this?
40-
// if (vis.is_private ())
41-
// return Visibility::create_private ();
42-
4339
switch (vis.get_public_vis_type ())
4440
{
45-
case AST::Visibility::NONE:
41+
case AST::Visibility::PUB:
4642
return Visibility (Visibility::VisType::PUBLIC);
47-
case AST::Visibility::SELF:
43+
case AST::Visibility::PRIV:
44+
case AST::Visibility::PUB_SELF:
4845
return Visibility (Visibility::VisType::PRIVATE);
4946
// Desugar pub(crate) into pub(in crate) and so on
50-
case AST::Visibility::CRATE:
47+
case AST::Visibility::PUB_CRATE:
5148
return Visibility (Visibility::PUBLIC,
5249
AST::SimplePath::from_str ("crate"));
53-
case AST::Visibility::SUPER:
50+
case AST::Visibility::PUB_SUPER:
5451
return Visibility (Visibility::PUBLIC,
5552
AST::SimplePath::from_str ("super"));
56-
case AST::Visibility::IN_PATH:
53+
case AST::Visibility::PUB_IN_PATH:
5754
return Visibility (Visibility::VisType::PUBLIC, vis.get_path ());
5855
break;
5956
}

gcc/rust/parse/rust-parse-impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2121,7 +2121,7 @@ Parser<ManagedTokenSource>::parse_visibility ()
21212121
// check for no visibility
21222122
if (lexer.peek_token ()->get_id () != PUB)
21232123
{
2124-
return AST::Visibility::create_error ();
2124+
return AST::Visibility::create_private ();
21252125
}
21262126

21272127
lexer.skip_token ();

0 commit comments

Comments
 (0)