Line data Source code
1 : // Copyright (C) 2020-2026 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 1225 : class ASTLoweringEnumItem : public ASTLoweringBase
34 : {
35 : using Rust::HIR::ASTLoweringBase::visit;
36 :
37 : public:
38 1225 : static HIR::EnumItem *translate (AST::EnumItem *item)
39 : {
40 2450 : ASTLoweringEnumItem resolver;
41 1225 : item->accept_vis (resolver);
42 :
43 1225 : rust_assert (resolver.translated != nullptr);
44 :
45 1225 : auto hirid = resolver.translated->get_mappings ().get_hirid ();
46 1225 : auto defid = resolver.translated->get_mappings ().get_defid ();
47 :
48 1225 : resolver.mappings.insert_defid_mapping (defid, resolver.translated);
49 1225 : resolver.mappings.insert_location (hirid,
50 1225 : resolver.translated->get_locus ());
51 :
52 1225 : return resolver.translated;
53 1225 : }
54 :
55 442 : void visit (AST::EnumItem &item) override
56 : {
57 442 : auto crate_num = mappings.get_current_crate ();
58 884 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
59 442 : mappings.get_next_hir_id (crate_num),
60 442 : mappings.get_next_localdef_id (crate_num));
61 :
62 442 : 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 442 : translated = new HIR::EnumItem (mapping, item.get_identifier (),
67 884 : item.get_outer_attrs (), item.get_locus ());
68 442 : }
69 :
70 419 : void visit (AST::EnumItemTuple &item) override
71 : {
72 419 : auto crate_num = mappings.get_current_crate ();
73 838 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
74 419 : mappings.get_next_hir_id (crate_num),
75 419 : mappings.get_next_localdef_id (crate_num));
76 :
77 419 : 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 419 : std::vector<HIR::TupleField> fields;
83 419 : fields.reserve (item.get_tuple_fields ().size ());
84 :
85 856 : for (auto &field : item.get_tuple_fields ())
86 : {
87 437 : HIR::Visibility vis = translate_visibility (field.get_visibility ());
88 437 : HIR::Type *type = ASTLoweringType::translate (field.get_field_type ());
89 :
90 437 : auto crate_num = mappings.get_current_crate ();
91 437 : Analysis::NodeMapping field_mapping (
92 437 : crate_num, field.get_node_id (), mappings.get_next_hir_id (crate_num),
93 437 : mappings.get_next_localdef_id (crate_num));
94 :
95 437 : fields.emplace_back (field_mapping, std::unique_ptr<HIR::Type> (type),
96 874 : vis, field.get_locus (), field.get_outer_attrs ());
97 437 : }
98 :
99 419 : translated
100 838 : = new HIR::EnumItemTuple (mapping, item.get_identifier (),
101 419 : std::move (fields), item.get_outer_attrs (),
102 838 : item.get_locus ());
103 419 : }
104 :
105 91 : void visit (AST::EnumItemStruct &item) override
106 : {
107 91 : auto crate_num = mappings.get_current_crate ();
108 182 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
109 91 : mappings.get_next_hir_id (crate_num),
110 91 : mappings.get_next_localdef_id (crate_num));
111 :
112 91 : 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 91 : std::vector<HIR::StructField> fields;
118 238 : for (auto &field : item.get_struct_fields ())
119 : {
120 149 : HIR::Visibility vis = translate_visibility (field.get_visibility ());
121 149 : HIR::Type *type = ASTLoweringType::translate (field.get_field_type ());
122 :
123 149 : auto crate_num = mappings.get_current_crate ();
124 149 : Analysis::NodeMapping field_mapping (
125 149 : crate_num, field.get_node_id (), mappings.get_next_hir_id (crate_num),
126 149 : mappings.get_next_localdef_id (crate_num));
127 :
128 149 : HIR::StructField translated_field (field_mapping,
129 298 : field.get_field_name (),
130 298 : std::unique_ptr<HIR::Type> (type),
131 : vis, field.get_locus (),
132 298 : field.get_outer_attrs ());
133 :
134 149 : if (struct_field_name_exists (fields, translated_field))
135 : break;
136 :
137 147 : fields.push_back (std::move (translated_field));
138 149 : }
139 :
140 91 : translated
141 182 : = new HIR::EnumItemStruct (mapping, item.get_identifier (),
142 91 : std::move (fields), item.get_outer_attrs (),
143 182 : item.get_locus ());
144 91 : }
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 1225 : ASTLoweringEnumItem () : translated (nullptr) {}
168 :
169 : HIR::EnumItem *translated;
170 : };
171 :
172 : } // namespace HIR
173 : } // namespace Rust
174 :
175 : #endif // RUST_AST_LOWER_ENUMITEM
|