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