aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-04-13 10:41:12 +0000
committerGitHub <noreply@github.com>2022-04-13 10:41:12 +0000
commit497ee70b776574aa3559c7d49a5cc7cf359d320e (patch)
treec370014a4fa91bde13742e1f459accb3d72c69ae
parentc1639be8bb0bd19810c7dc3040ad224dd864f699 (diff)
parent03cb435c1905e03da44a6a0723f03c38e7f59b09 (diff)
downloadgcc-497ee70b776574aa3559c7d49a5cc7cf359d320e.zip
gcc-497ee70b776574aa3559c7d49a5cc7cf359d320e.tar.gz
gcc-497ee70b776574aa3559c7d49a5cc7cf359d320e.tar.bz2
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 <arthur.cohen@embecosm.com>
-rw-r--r--gcc/rust/ast/rust-ast-full-test.cc14
-rw-r--r--gcc/rust/ast/rust-item.h51
-rw-r--r--gcc/rust/hir/rust-ast-lower.cc15
-rw-r--r--gcc/rust/parse/rust-parse-impl.h2
4 files changed, 45 insertions, 37 deletions
diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc
index d580177..e6bad4b 100644
--- a/gcc/rust/ast/rust-ast-full-test.cc
+++ b/gcc/rust/ast/rust-ast-full-test.cc
@@ -306,17 +306,19 @@ SimplePath::as_string () const
std::string
Visibility::as_string () const
{
- switch (public_vis_type)
+ switch (vis_type)
{
- case NONE:
+ case PRIV:
+ return std::string ("");
+ case PUB:
return std::string ("pub");
- case CRATE:
+ case PUB_CRATE:
return std::string ("pub(crate)");
- case SELF:
+ case PUB_SELF:
return std::string ("pub(self)");
- case SUPER:
+ case PUB_SUPER:
return std::string ("pub(super)");
- case IN_PATH:
+ case PUB_IN_PATH:
return std::string ("pub(in ") + in_path.as_string () + std::string (")");
default:
gcc_unreachable ();
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index f08f2f0..1b925c3 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -609,41 +609,44 @@ protected:
struct Visibility
{
public:
- enum PublicVisType
- {
- NONE,
- CRATE,
- SELF,
- SUPER,
- IN_PATH
+ enum VisType
+ {
+ PRIV,
+ PUB,
+ PUB_CRATE,
+ PUB_SELF,
+ PUB_SUPER,
+ PUB_IN_PATH
};
private:
- // if vis is public, one of these
- PublicVisType public_vis_type;
- // Only assigned if public_vis_type is IN_PATH
+ VisType vis_type;
+ // Only assigned if vis_type is IN_PATH
SimplePath in_path;
// should this store location info?
public:
// Creates a Visibility - TODO make constructor protected or private?
- Visibility (PublicVisType public_vis_type, SimplePath in_path)
- : public_vis_type (public_vis_type), in_path (std::move (in_path))
+ Visibility (VisType vis_type, SimplePath in_path)
+ : vis_type (vis_type), in_path (std::move (in_path))
{}
- PublicVisType get_public_vis_type () const { return public_vis_type; }
+ VisType get_public_vis_type () const { return vis_type; }
// Returns whether visibility is in an error state.
bool is_error () const
{
- return public_vis_type == IN_PATH && in_path.is_empty ();
+ return vis_type == PUB_IN_PATH && in_path.is_empty ();
}
+ // Returns whether visibility is public or not.
+ bool is_public () const { return vis_type != PRIV && !is_error (); }
+
// Creates an error visibility.
static Visibility create_error ()
{
- return Visibility (IN_PATH, SimplePath::create_empty ());
+ return Visibility (PUB_IN_PATH, SimplePath::create_empty ());
}
// Unique pointer custom clone function
@@ -657,32 +660,38 @@ public:
// Creates a public visibility with no further features/arguments.
static Visibility create_public ()
{
- return Visibility (NONE, SimplePath::create_empty ());
+ return Visibility (PUB, SimplePath::create_empty ());
}
// Creates a public visibility with crate-relative paths or whatever.
static Visibility create_crate ()
{
- return Visibility (CRATE, SimplePath::create_empty ());
+ return Visibility (PUB_CRATE, SimplePath::create_empty ());
}
// Creates a public visibility with self-relative paths or whatever.
static Visibility create_self ()
{
- return Visibility (SELF, SimplePath::create_empty ());
+ return Visibility (PUB_SELF, SimplePath::create_empty ());
}
// Creates a public visibility with parent module-relative paths or
// whatever.
static Visibility create_super ()
{
- return Visibility (SUPER, SimplePath::create_empty ());
+ return Visibility (PUB_SUPER, SimplePath::create_empty ());
+ }
+
+ // Creates a private visibility
+ static Visibility create_private ()
+ {
+ return Visibility (PRIV, SimplePath::create_empty ());
}
// Creates a public visibility with a given path or whatever.
static Visibility create_in_path (SimplePath in_path)
{
- return Visibility (IN_PATH, std::move (in_path));
+ return Visibility (PUB_IN_PATH, std::move (in_path));
}
std::string as_string () const;
@@ -938,7 +947,7 @@ protected:
public:
/* Does the item have some kind of public visibility (non-default
* visibility)? */
- bool has_visibility () const { return !visibility.is_error (); }
+ bool has_visibility () const { return visibility.is_public (); }
std::string as_string () const override;
diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc
index 1da823a..4a344c9 100644
--- a/gcc/rust/hir/rust-ast-lower.cc
+++ b/gcc/rust/hir/rust-ast-lower.cc
@@ -36,24 +36,21 @@ translate_visibility (const AST::Visibility &vis)
if (vis.is_error ())
return Visibility::create_error ();
- // FIXME: ... And then use this?
- // if (vis.is_private ())
- // return Visibility::create_private ();
-
switch (vis.get_public_vis_type ())
{
- case AST::Visibility::NONE:
+ case AST::Visibility::PUB:
return Visibility (Visibility::VisType::PUBLIC);
- case AST::Visibility::SELF:
+ case AST::Visibility::PRIV:
+ case AST::Visibility::PUB_SELF:
return Visibility (Visibility::VisType::PRIVATE);
// Desugar pub(crate) into pub(in crate) and so on
- case AST::Visibility::CRATE:
+ case AST::Visibility::PUB_CRATE:
return Visibility (Visibility::PUBLIC,
AST::SimplePath::from_str ("crate"));
- case AST::Visibility::SUPER:
+ case AST::Visibility::PUB_SUPER:
return Visibility (Visibility::PUBLIC,
AST::SimplePath::from_str ("super"));
- case AST::Visibility::IN_PATH:
+ case AST::Visibility::PUB_IN_PATH:
return Visibility (Visibility::VisType::PUBLIC, vis.get_path ());
break;
}
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index e352836..c95afd6 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -2121,7 +2121,7 @@ Parser<ManagedTokenSource>::parse_visibility ()
// check for no visibility
if (lexer.peek_token ()->get_id () != PUB)
{
- return AST::Visibility::create_error ();
+ return AST::Visibility::create_private ();
}
lexer.skip_token ();