Branch data Line data Source code
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 : 1217 : class ASTLoweringEnumItem : public ASTLoweringBase
34 : : {
35 : : using Rust::HIR::ASTLoweringBase::visit;
36 : :
37 : : public:
38 : 1217 : static HIR::EnumItem *translate (AST::EnumItem *item)
39 : : {
40 : 2434 : ASTLoweringEnumItem resolver;
41 : 1217 : item->accept_vis (resolver);
42 : :
43 : 1217 : rust_assert (resolver.translated != nullptr);
44 : :
45 : 1217 : auto hirid = resolver.translated->get_mappings ().get_hirid ();
46 : 1217 : auto defid = resolver.translated->get_mappings ().get_defid ();
47 : :
48 : 1217 : resolver.mappings.insert_defid_mapping (defid, resolver.translated);
49 : 1217 : resolver.mappings.insert_location (hirid,
50 : 1217 : resolver.translated->get_locus ());
51 : :
52 : 1217 : return resolver.translated;
53 : 1217 : }
54 : :
55 : 439 : void visit (AST::EnumItem &item) override
56 : : {
57 : 439 : auto crate_num = mappings.get_current_crate ();
58 : 878 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
59 : 439 : mappings.get_next_hir_id (crate_num),
60 : 439 : mappings.get_next_localdef_id (crate_num));
61 : :
62 : 439 : if (item.has_visibility ())
63 : 4 : rust_error_at (item.get_locus (),
64 : : "visibility qualifier %qs not allowed on enum item",
65 : 8 : item.get_visibility ().as_string ().c_str ());
66 : 439 : translated = new HIR::EnumItem (mapping, item.get_identifier (),
67 : 878 : item.get_outer_attrs (), item.get_locus ());
68 : 439 : }
69 : :
70 : 417 : void visit (AST::EnumItemTuple &item) override
71 : : {
72 : 417 : auto crate_num = mappings.get_current_crate ();
73 : 834 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
74 : 417 : mappings.get_next_hir_id (crate_num),
75 : 417 : mappings.get_next_localdef_id (crate_num));
76 : :
77 : 417 : if (item.has_visibility ())
78 : 3 : rust_error_at (item.get_locus (),
79 : : "visibility qualifier %qs not allowed on enum item",
80 : 6 : item.get_visibility ().as_string ().c_str ());
81 : :
82 : 417 : std::vector<HIR::TupleField> fields;
83 : 417 : fields.reserve (item.get_tuple_fields ().size ());
84 : :
85 : 853 : for (auto &field : item.get_tuple_fields ())
86 : : {
87 : 436 : HIR::Visibility vis = translate_visibility (field.get_visibility ());
88 : 436 : HIR::Type *type = ASTLoweringType::translate (field.get_field_type ());
89 : :
90 : 436 : auto crate_num = mappings.get_current_crate ();
91 : 436 : Analysis::NodeMapping field_mapping (
92 : 436 : crate_num, field.get_node_id (), mappings.get_next_hir_id (crate_num),
93 : 436 : mappings.get_next_localdef_id (crate_num));
94 : :
95 : 436 : fields.emplace_back (field_mapping, std::unique_ptr<HIR::Type> (type),
96 : 872 : vis, field.get_locus (), field.get_outer_attrs ());
97 : 436 : }
98 : :
99 : 417 : translated
100 : 834 : = new HIR::EnumItemTuple (mapping, item.get_identifier (),
101 : 417 : std::move (fields), item.get_outer_attrs (),
102 : 834 : item.get_locus ());
103 : 417 : }
104 : :
105 : 88 : void visit (AST::EnumItemStruct &item) override
106 : : {
107 : 88 : auto crate_num = mappings.get_current_crate ();
108 : 176 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
109 : 88 : mappings.get_next_hir_id (crate_num),
110 : 88 : mappings.get_next_localdef_id (crate_num));
111 : :
112 : 88 : if (item.has_visibility ())
113 : 1 : rust_error_at (item.get_locus (),
114 : : "visibility qualifier %qs not allowed on enum item",
115 : 2 : item.get_visibility ().as_string ().c_str ());
116 : :
117 : 88 : std::vector<HIR::StructField> fields;
118 : 233 : for (auto &field : item.get_struct_fields ())
119 : : {
120 : 147 : HIR::Visibility vis = translate_visibility (field.get_visibility ());
121 : 147 : HIR::Type *type = ASTLoweringType::translate (field.get_field_type ());
122 : :
123 : 147 : auto crate_num = mappings.get_current_crate ();
124 : 147 : Analysis::NodeMapping field_mapping (
125 : 147 : crate_num, field.get_node_id (), mappings.get_next_hir_id (crate_num),
126 : 147 : mappings.get_next_localdef_id (crate_num));
127 : :
128 : 147 : HIR::StructField translated_field (field_mapping,
129 : 294 : field.get_field_name (),
130 : 294 : std::unique_ptr<HIR::Type> (type),
131 : : vis, field.get_locus (),
132 : 294 : field.get_outer_attrs ());
133 : :
134 : 147 : if (struct_field_name_exists (fields, translated_field))
135 : : break;
136 : :
137 : 145 : fields.push_back (std::move (translated_field));
138 : 147 : }
139 : :
140 : 88 : translated
141 : 176 : = new HIR::EnumItemStruct (mapping, item.get_identifier (),
142 : 88 : std::move (fields), item.get_outer_attrs (),
143 : 176 : item.get_locus ());
144 : 88 : }
145 : :
146 : 273 : void visit (AST::EnumItemDiscriminant &item) override
147 : : {
148 : 273 : auto crate_num = mappings.get_current_crate ();
149 : 546 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
150 : 273 : mappings.get_next_hir_id (crate_num),
151 : 273 : mappings.get_next_localdef_id (crate_num));
152 : :
153 : 273 : if (item.has_visibility ())
154 : 2 : rust_error_at (item.get_locus (),
155 : : "visibility qualifier %qs not allowed on enum item",
156 : 4 : item.get_visibility ().as_string ().c_str ());
157 : :
158 : 273 : HIR::Expr *expr = ASTLoweringExpr::translate (item.get_expr ());
159 : 273 : translated
160 : 273 : = new HIR::EnumItemDiscriminant (mapping, item.get_identifier (),
161 : 546 : std::unique_ptr<HIR::Expr> (expr),
162 : 273 : item.get_outer_attrs (),
163 : 546 : item.get_locus ());
164 : 273 : }
165 : :
166 : : private:
167 : 1217 : ASTLoweringEnumItem () : translated (nullptr) {}
168 : :
169 : : HIR::EnumItem *translated;
170 : : };
171 : :
172 : : } // namespace HIR
173 : : } // namespace Rust
174 : :
175 : : #endif // RUST_AST_LOWER_ENUMITEM
|