| 1 |
// Copyright (C) 2020-2025 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_location (hirid, |
| 50 |
resolver.translated->get_locus ()); |
| 51 |
|
| 52 |
return resolver.translated; |
| 53 |
} |
| 54 |
|
| 55 |
void visit (AST::EnumItem &item) override |
| 56 |
{ |
| 57 |
auto crate_num = mappings.get_current_crate (); |
| 58 |
Analysis::NodeMapping mapping (crate_num, item.get_node_id (), |
| 59 |
mappings.get_next_hir_id (crate_num), |
| 60 |
mappings.get_next_localdef_id (crate_num)); |
| 61 |
|
| 62 |
if (item.has_visibility ()) |
| 63 |
rust_error_at (item.get_locus (), |
| 64 |
"visibility qualifier %qs not allowed on enum item", |
| 65 |
item.get_visibility ().as_string ().c_str ()); |
| 66 |
translated = new HIR::EnumItem (mapping, item.get_identifier (), |
| 67 |
item.get_outer_attrs (), item.get_locus ()); |
| 68 |
} |
| 69 |
|
| 70 |
void visit (AST::EnumItemTuple &item) override |
| 71 |
{ |
| 72 |
auto crate_num = mappings.get_current_crate (); |
| 73 |
Analysis::NodeMapping mapping (crate_num, item.get_node_id (), |
| 74 |
mappings.get_next_hir_id (crate_num), |
| 75 |
mappings.get_next_localdef_id (crate_num)); |
| 76 |
|
| 77 |
if (item.has_visibility ()) |
| 78 |
rust_error_at (item.get_locus (), |
| 79 |
"visibility qualifier %qs not allowed on enum item", |
| 80 |
item.get_visibility ().as_string ().c_str ()); |
| 81 |
|
| 82 |
std::vector<HIR::TupleField> fields; |
| 83 |
for (auto &field : item.get_tuple_fields ()) |
| 84 |
{ |
| 85 |
HIR::Visibility vis = translate_visibility (field.get_visibility ()); |
| 86 |
HIR::Type *type = ASTLoweringType::translate (field.get_field_type ()); |
| 87 |
|
| 88 |
auto crate_num = mappings.get_current_crate (); |
| 89 |
Analysis::NodeMapping field_mapping ( |
| 90 |
crate_num, field.get_node_id (), mappings.get_next_hir_id (crate_num), |
| 91 |
mappings.get_next_localdef_id (crate_num)); |
| 92 |
|
| 93 |
HIR::TupleField translated_field (field_mapping, |
| 94 |
std::unique_ptr<HIR::Type> (type), |
| 95 |
vis, field.get_locus (), |
| 96 |
field.get_outer_attrs ()); |
| 97 |
fields.push_back (std::move (translated_field)); |
| 98 |
} |
| 99 |
|
| 100 |
translated |
| 101 |
= new HIR::EnumItemTuple (mapping, item.get_identifier (), |
| 102 |
std::move (fields), item.get_outer_attrs (), |
| 103 |
item.get_locus ()); |
| 104 |
} |
| 105 |
|
| 106 |
void visit (AST::EnumItemStruct &item) override |
| 107 |
{ |
| 108 |
auto crate_num = mappings.get_current_crate (); |
| 109 |
Analysis::NodeMapping mapping (crate_num, item.get_node_id (), |
| 110 |
mappings.get_next_hir_id (crate_num), |
| 111 |
mappings.get_next_localdef_id (crate_num)); |
| 112 |
|
| 113 |
if (item.has_visibility ()) |
| 114 |
rust_error_at (item.get_locus (), |
| 115 |
"visibility qualifier %qs not allowed on enum item", |
| 116 |
item.get_visibility ().as_string ().c_str ()); |
| 117 |
|
| 118 |
std::vector<HIR::StructField> fields; |
| 119 |
for (auto &field : item.get_struct_fields ()) |
| 120 |
{ |
| 121 |
HIR::Visibility vis = translate_visibility (field.get_visibility ()); |
| 122 |
HIR::Type *type = ASTLoweringType::translate (field.get_field_type ()); |
| 123 |
|
| 124 |
auto crate_num = mappings.get_current_crate (); |
| 125 |
Analysis::NodeMapping field_mapping ( |
| 126 |
crate_num, field.get_node_id (), mappings.get_next_hir_id (crate_num), |
| 127 |
mappings.get_next_localdef_id (crate_num)); |
| 128 |
|
| 129 |
HIR::StructField translated_field (field_mapping, |
| 130 |
field.get_field_name (), |
| 131 |
std::unique_ptr<HIR::Type> (type), |
| 132 |
vis, field.get_locus (), |
| 133 |
field.get_outer_attrs ()); |
| 134 |
|
| 135 |
if (struct_field_name_exists (fields, translated_field)) |
| 136 |
break; |
| 137 |
|
| 138 |
fields.push_back (std::move (translated_field)); |
| 139 |
} |
| 140 |
|
| 141 |
translated |
| 142 |
= new HIR::EnumItemStruct (mapping, item.get_identifier (), |
| 143 |
std::move (fields), item.get_outer_attrs (), |
| 144 |
item.get_locus ()); |
| 145 |
} |
| 146 |
|
| 147 |
void visit (AST::EnumItemDiscriminant &item) override |
| 148 |
{ |
| 149 |
auto crate_num = mappings.get_current_crate (); |
| 150 |
Analysis::NodeMapping mapping (crate_num, item.get_node_id (), |
| 151 |
mappings.get_next_hir_id (crate_num), |
| 152 |
mappings.get_next_localdef_id (crate_num)); |
| 153 |
|
| 154 |
if (item.has_visibility ()) |
| 155 |
rust_error_at (item.get_locus (), |
| 156 |
"visibility qualifier %qs not allowed on enum item", |
| 157 |
item.get_visibility ().as_string ().c_str ()); |
| 158 |
|
| 159 |
HIR::Expr *expr = ASTLoweringExpr::translate (item.get_expr ()); |
| 160 |
translated |
| 161 |
= new HIR::EnumItemDiscriminant (mapping, item.get_identifier (), |
| 162 |
std::unique_ptr<HIR::Expr> (expr), |
| 163 |
item.get_outer_attrs (), |
| 164 |
item.get_locus ()); |
| 165 |
} |
| 166 |
|
| 167 |
private: |
| 168 |
ASTLoweringEnumItem () : translated (nullptr) {} |
| 169 |
|
| 170 |
HIR::EnumItem *translated; |
| 171 |
}; |
| 172 |
|
| 173 |
} // namespace HIR |
| 174 |
} // namespace Rust |
| 175 |
|
| 176 |
#endif // RUST_AST_LOWER_ENUMITEM |