(root)/
gcc-13.2.0/
gcc/
rust/
hir/
rust-ast-lower-enumitem.h
       1  // Copyright (C) 2020-2023 Free Software Foundation, Inc.
       2  
       3  // This file is part of GCC.
       4  
       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
       8  // version.
       9  
      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
      13  // for more details.
      14  
      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/>.
      18  
      19  #ifndef RUST_AST_LOWER_ENUMITEM
      20  #define RUST_AST_LOWER_ENUMITEM
      21  
      22  #include "rust-ast-lower.h"
      23  #include "rust-diagnostics.h"
      24  
      25  #include "rust-ast-lower-base.h"
      26  #include "rust-ast-lower-type.h"
      27  #include "rust-ast-lower-expr.h"
      28  #include "rust-hir-full-decls.h"
      29  
      30  namespace Rust {
      31  namespace HIR {
      32  
      33  class ASTLoweringEnumItem : public ASTLoweringBase
      34  {
      35    using Rust::HIR::ASTLoweringBase::visit;
      36  
      37  public:
      38    static HIR::EnumItem *translate (AST::EnumItem *item)
      39    {
      40      ASTLoweringEnumItem resolver;
      41      item->accept_vis (resolver);
      42  
      43      rust_assert (resolver.translated != nullptr);
      44  
      45      auto hirid = resolver.translated->get_mappings ().get_hirid ();
      46      auto defid = resolver.translated->get_mappings ().get_defid ();
      47  
      48      resolver.mappings->insert_defid_mapping (defid, resolver.translated);
      49      resolver.mappings->insert_hir_item (resolver.translated);
      50      resolver.mappings->insert_location (hirid,
      51  					resolver.translated->get_locus ());
      52  
      53      return resolver.translated;
      54    }
      55  
      56    void visit (AST::EnumItem &item) override
      57    {
      58      auto crate_num = mappings->get_current_crate ();
      59      Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
      60  				   mappings->get_next_hir_id (crate_num),
      61  				   mappings->get_next_localdef_id (crate_num));
      62  
      63      if (item.has_visibility ())
      64        rust_error_at (item.get_locus (),
      65  		     "visibility qualifier %qs not allowed on enum item",
      66  		     item.get_visibility ().as_string ().c_str ());
      67      translated = new HIR::EnumItem (mapping, item.get_identifier (),
      68  				    item.get_outer_attrs (), item.get_locus ());
      69    }
      70  
      71    void visit (AST::EnumItemTuple &item) override
      72    {
      73      auto crate_num = mappings->get_current_crate ();
      74      Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
      75  				   mappings->get_next_hir_id (crate_num),
      76  				   mappings->get_next_localdef_id (crate_num));
      77  
      78      if (item.has_visibility ())
      79        rust_error_at (item.get_locus (),
      80  		     "visibility qualifier %qs not allowed on enum item",
      81  		     item.get_visibility ().as_string ().c_str ());
      82  
      83      std::vector<HIR::TupleField> fields;
      84      for (auto &field : item.get_tuple_fields ())
      85        {
      86  	HIR::Visibility vis = translate_visibility (field.get_visibility ());
      87  	HIR::Type *type
      88  	  = ASTLoweringType::translate (field.get_field_type ().get ());
      89  
      90  	auto crate_num = mappings->get_current_crate ();
      91  	Analysis::NodeMapping field_mapping (
      92  	  crate_num, field.get_node_id (),
      93  	  mappings->get_next_hir_id (crate_num),
      94  	  mappings->get_next_localdef_id (crate_num));
      95  
      96  	HIR::TupleField translated_field (field_mapping,
      97  					  std::unique_ptr<HIR::Type> (type),
      98  					  vis, field.get_locus (),
      99  					  field.get_outer_attrs ());
     100  	fields.push_back (std::move (translated_field));
     101        }
     102  
     103      translated
     104        = new HIR::EnumItemTuple (mapping, item.get_identifier (),
     105  				std::move (fields), item.get_outer_attrs (),
     106  				item.get_locus ());
     107    }
     108  
     109    void visit (AST::EnumItemStruct &item) override
     110    {
     111      auto crate_num = mappings->get_current_crate ();
     112      Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
     113  				   mappings->get_next_hir_id (crate_num),
     114  				   mappings->get_next_localdef_id (crate_num));
     115  
     116      if (item.has_visibility ())
     117        rust_error_at (item.get_locus (),
     118  		     "visibility qualifier %qs not allowed on enum item",
     119  		     item.get_visibility ().as_string ().c_str ());
     120  
     121      std::vector<HIR::StructField> fields;
     122      for (auto &field : item.get_struct_fields ())
     123        {
     124  	HIR::Visibility vis = translate_visibility (field.get_visibility ());
     125  	HIR::Type *type
     126  	  = ASTLoweringType::translate (field.get_field_type ().get ());
     127  
     128  	auto crate_num = mappings->get_current_crate ();
     129  	Analysis::NodeMapping field_mapping (
     130  	  crate_num, field.get_node_id (),
     131  	  mappings->get_next_hir_id (crate_num),
     132  	  mappings->get_next_localdef_id (crate_num));
     133  
     134  	HIR::StructField translated_field (field_mapping,
     135  					   field.get_field_name (),
     136  					   std::unique_ptr<HIR::Type> (type),
     137  					   vis, field.get_locus (),
     138  					   field.get_outer_attrs ());
     139  
     140  	if (struct_field_name_exists (fields, translated_field))
     141  	  break;
     142  
     143  	fields.push_back (std::move (translated_field));
     144        }
     145  
     146      translated
     147        = new HIR::EnumItemStruct (mapping, item.get_identifier (),
     148  				 std::move (fields), item.get_outer_attrs (),
     149  				 item.get_locus ());
     150    }
     151  
     152    void visit (AST::EnumItemDiscriminant &item) override
     153    {
     154      auto crate_num = mappings->get_current_crate ();
     155      Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
     156  				   mappings->get_next_hir_id (crate_num),
     157  				   mappings->get_next_localdef_id (crate_num));
     158  
     159      if (item.has_visibility ())
     160        rust_error_at (item.get_locus (),
     161  		     "visibility qualifier %qs not allowed on enum item",
     162  		     item.get_visibility ().as_string ().c_str ());
     163  
     164      HIR::Expr *expr = ASTLoweringExpr::translate (item.get_expr ().get ());
     165      translated
     166        = new HIR::EnumItemDiscriminant (mapping, item.get_identifier (),
     167  				       std::unique_ptr<HIR::Expr> (expr),
     168  				       item.get_outer_attrs (),
     169  				       item.get_locus ());
     170    }
     171  
     172  private:
     173    ASTLoweringEnumItem () : translated (nullptr) {}
     174  
     175    HIR::EnumItem *translated;
     176  };
     177  
     178  } // namespace HIR
     179  } // namespace Rust
     180  
     181  #endif // RUST_AST_LOWER_ENUMITEM