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