public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: gcc-rust@gcc.gnu.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH 1/3] Parse optional visibility for enum item
Date: Mon,  6 Sep 2021 00:48:09 +0200	[thread overview]
Message-ID: <20210905224811.69432-1-mark@klomp.org> (raw)

Syntactically enum items can have a visibility. The visibility has to
be removed (through a cfg attribute or macro) before they get lowered.
The semantic checking will be done when we implement lowering enum items.

Make the AST EnumItem class a VisItem. This simplifies things a little
for cloning items, handling outer attributes and will help when adding
EnumItem (sub)classes to AST visitors (so they can be handled as
Items). Also add a get_identifier method to Enum and EnumItem.
---
 gcc/rust/ast/rust-ast-full-test.cc |  6 ++--
 gcc/rust/ast/rust-item.h           | 58 +++++++++++++++---------------
 gcc/rust/parse/rust-parse-impl.h   | 12 ++++---
 3 files changed, 38 insertions(+), 38 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-full-test.cc b/gcc/rust/ast/rust-ast-full-test.cc
index 6241710226d..2477d7400a7 100644
--- a/gcc/rust/ast/rust-ast-full-test.cc
+++ b/gcc/rust/ast/rust-ast-full-test.cc
@@ -3311,10 +3311,8 @@ StructExprStructFields::as_string () const
 std::string
 EnumItem::as_string () const
 {
-  // outer attributes
-  std::string str = append_attributes (outer_attrs, OUTER);
-
-  str += "\n" + variant_name;
+  std::string str = VisItem::as_string ();
+  str += variant_name;
 
   return str;
 }
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 0578329f54b..881a888f092 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -2119,12 +2119,10 @@ protected:
 };
 
 /* An item used in an "enum" tagged union - not abstract: base represents a
- * name-only enum */
-class EnumItem
+ * name-only enum. EnumItems (variants) syntactically allow a Visibility
+ * annotation. */
+class EnumItem : public VisItem
 {
-  // bool has_attrs;
-  std::vector<Attribute> outer_attrs;
-
   Identifier variant_name;
 
   Location locus;
@@ -2132,19 +2130,16 @@ class EnumItem
 public:
   virtual ~EnumItem () {}
 
-  // Returns whether enum item has outer attributes.
-  bool has_outer_attrs () const { return !outer_attrs.empty (); }
-
-  EnumItem (Identifier variant_name, std::vector<Attribute> outer_attrs,
-	    Location locus)
-    : outer_attrs (std::move (outer_attrs)),
+  EnumItem (Identifier variant_name, Visibility vis,
+	    std::vector<Attribute> outer_attrs, Location locus)
+    : VisItem (std::move (vis), std::move (outer_attrs)),
       variant_name (std::move (variant_name)), locus (locus)
   {}
 
   // Unique pointer custom clone function
   std::unique_ptr<EnumItem> clone_enum_item () const
   {
-    return std::unique_ptr<EnumItem> (clone_enum_item_impl ());
+    return std::unique_ptr<EnumItem> (clone_item_impl ());
   }
 
   virtual std::string as_string () const;
@@ -2152,20 +2147,16 @@ public:
   // not pure virtual as not abstract
   virtual void accept_vis (ASTVisitor &vis);
 
+  Location get_locus () const { return locus; }
+
+  Identifier get_identifier () const { return variant_name; }
+
   // Based on idea that name is never empty.
   void mark_for_strip () { variant_name = ""; }
   bool is_marked_for_strip () const { return variant_name.empty (); }
 
-  // TODO: this mutable getter seems really dodgy. Think up better way.
-  std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
-  const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
-
 protected:
-  // Clone function implementation as (not pure) virtual method
-  virtual EnumItem *clone_enum_item_impl () const
-  {
-    return new EnumItem (*this);
-  }
+  EnumItem *clone_item_impl () const override { return new EnumItem (*this); }
 };
 
 // A tuple item used in an "enum" tagged union
@@ -2178,9 +2169,11 @@ public:
   // Returns whether tuple enum item has tuple fields.
   bool has_tuple_fields () const { return !tuple_fields.empty (); }
 
-  EnumItemTuple (Identifier variant_name, std::vector<TupleField> tuple_fields,
+  EnumItemTuple (Identifier variant_name, Visibility vis,
+		 std::vector<TupleField> tuple_fields,
 		 std::vector<Attribute> outer_attrs, Location locus)
-    : EnumItem (std::move (variant_name), std::move (outer_attrs), locus),
+    : EnumItem (std::move (variant_name), std::move (vis),
+		std::move (outer_attrs), locus),
       tuple_fields (std::move (tuple_fields))
   {}
 
@@ -2197,7 +2190,7 @@ public:
 
 protected:
   // Clone function implementation as (not pure) virtual method
-  EnumItemTuple *clone_enum_item_impl () const override
+  EnumItemTuple *clone_item_impl () const override
   {
     return new EnumItemTuple (*this);
   }
@@ -2213,10 +2206,11 @@ public:
   // Returns whether struct enum item has struct fields.
   bool has_struct_fields () const { return !struct_fields.empty (); }
 
-  EnumItemStruct (Identifier variant_name,
+  EnumItemStruct (Identifier variant_name, Visibility vis,
 		  std::vector<StructField> struct_fields,
 		  std::vector<Attribute> outer_attrs, Location locus)
-    : EnumItem (std::move (variant_name), std::move (outer_attrs), locus),
+    : EnumItem (std::move (variant_name), std::move (vis),
+		std::move (outer_attrs), locus),
       struct_fields (std::move (struct_fields))
   {}
 
@@ -2233,7 +2227,7 @@ public:
 
 protected:
   // Clone function implementation as (not pure) virtual method
-  EnumItemStruct *clone_enum_item_impl () const override
+  EnumItemStruct *clone_item_impl () const override
   {
     return new EnumItemStruct (*this);
   }
@@ -2245,9 +2239,11 @@ class EnumItemDiscriminant : public EnumItem
   std::unique_ptr<Expr> expression;
 
 public:
-  EnumItemDiscriminant (Identifier variant_name, std::unique_ptr<Expr> expr,
+  EnumItemDiscriminant (Identifier variant_name, Visibility vis,
+			std::unique_ptr<Expr> expr,
 			std::vector<Attribute> outer_attrs, Location locus)
-    : EnumItem (std::move (variant_name), std::move (outer_attrs), locus),
+    : EnumItem (std::move (variant_name), std::move (vis),
+		std::move (outer_attrs), locus),
       expression (std::move (expr))
   {}
 
@@ -2284,7 +2280,7 @@ public:
 
 protected:
   // Clone function implementation as (not pure) virtual method
-  EnumItemDiscriminant *clone_enum_item_impl () const override
+  EnumItemDiscriminant *clone_item_impl () const override
   {
     return new EnumItemDiscriminant (*this);
   }
@@ -2374,6 +2370,8 @@ public:
 
   void accept_vis (ASTVisitor &vis) override;
 
+  Identifier get_identifier () const { return enum_name; }
+
   // Invalid if name is empty, so base stripping on that.
   void mark_for_strip () override { enum_name = ""; }
   bool is_marked_for_strip () const override { return enum_name.empty (); }
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index 1c0644d42ae..8cce9332350 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -4431,6 +4431,9 @@ Parser<ManagedTokenSource>::parse_enum_item ()
   // parse outer attributes if they exist
   AST::AttrVec outer_attrs = parse_outer_attributes ();
 
+  // parse visibility, which may or may not exist
+  AST::Visibility vis = parse_visibility ();
+
   // parse name for enum item, which is required
   const_TokenPtr item_name_tok = lexer.peek_token ();
   if (item_name_tok->get_id () != IDENTIFIER)
@@ -4463,7 +4466,7 @@ Parser<ManagedTokenSource>::parse_enum_item ()
 	  }
 
 	return std::unique_ptr<AST::EnumItemTuple> (new AST::EnumItemTuple (
-	  std::move (item_name), std::move (tuple_fields),
+	  std::move (item_name), std::move (vis), std::move (tuple_fields),
 	  std::move (outer_attrs), item_name_tok->get_locus ()));
       }
       case LEFT_CURLY: {
@@ -4480,7 +4483,7 @@ Parser<ManagedTokenSource>::parse_enum_item ()
 	  }
 
 	return std::unique_ptr<AST::EnumItemStruct> (new AST::EnumItemStruct (
-	  std::move (item_name), std::move (struct_fields),
+	  std::move (item_name), std::move (vis), std::move (struct_fields),
 	  std::move (outer_attrs), item_name_tok->get_locus ()));
       }
       case EQUAL: {
@@ -4490,7 +4493,7 @@ Parser<ManagedTokenSource>::parse_enum_item ()
 	std::unique_ptr<AST::Expr> discriminant_expr = parse_expr ();
 
 	return std::unique_ptr<AST::EnumItemDiscriminant> (
-	  new AST::EnumItemDiscriminant (std::move (item_name),
+	  new AST::EnumItemDiscriminant (std::move (item_name), std::move (vis),
 					 std::move (discriminant_expr),
 					 std::move (outer_attrs),
 					 item_name_tok->get_locus ()));
@@ -4498,7 +4501,8 @@ Parser<ManagedTokenSource>::parse_enum_item ()
     default:
       // regular enum with just an identifier
       return std::unique_ptr<AST::EnumItem> (
-	new AST::EnumItem (std::move (item_name), std::move (outer_attrs),
+	new AST::EnumItem (std::move (item_name), std::move (vis),
+			   std::move (outer_attrs),
 			   item_name_tok->get_locus ()));
     }
 }
-- 
2.32.0


             reply	other threads:[~2021-09-05 22:48 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-05 22:48 Mark Wielaard [this message]
2021-09-05 22:48 ` [PATCH 2/3] Resolve Enums and EnumItems Mark Wielaard
2021-09-05 22:48 ` [PATCH 3/3] Add EnumItem HIR lowering Mark Wielaard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210905224811.69432-1-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=gcc-rust@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).