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 : #include "rust-hir-expr.h"
20 : #include "rust-hir-type-check-type.h"
21 : #include "rust-hir-type-check-expr.h"
22 : #include "rust-hir-type-check-enumitem.h"
23 : #include "rust-rib.h"
24 : #include "rust-type-util.h"
25 : #include "rust-finalized-name-resolution-context.h"
26 :
27 : namespace Rust {
28 : namespace Resolver {
29 :
30 : TyTy::VariantDef *
31 1959 : TypeCheckEnumItem::Resolve (HIR::EnumItem &item, int64_t last_discriminant)
32 : {
33 1959 : TypeCheckEnumItem resolver (last_discriminant);
34 1959 : switch (item.get_enum_item_kind ())
35 : {
36 916 : case HIR::EnumItem::EnumItemKind::Named:
37 916 : resolver.visit (static_cast<HIR::EnumItem &> (item));
38 916 : break;
39 :
40 663 : case HIR::EnumItem::EnumItemKind::Tuple:
41 663 : resolver.visit (static_cast<HIR::EnumItemTuple &> (item));
42 663 : break;
43 :
44 89 : case HIR::EnumItem::EnumItemKind::Struct:
45 89 : resolver.visit (static_cast<HIR::EnumItemStruct &> (item));
46 89 : break;
47 :
48 291 : case HIR::EnumItem::EnumItemKind::Discriminant:
49 291 : resolver.visit (static_cast<HIR::EnumItemDiscriminant &> (item));
50 291 : break;
51 : }
52 1952 : return resolver.variant;
53 1952 : }
54 :
55 1959 : TypeCheckEnumItem::TypeCheckEnumItem (int64_t last_discriminant)
56 1959 : : TypeCheckBase (), variant (nullptr), last_discriminant (last_discriminant)
57 1959 : {}
58 :
59 : void
60 916 : TypeCheckEnumItem::visit (HIR::EnumItem &item)
61 : {
62 916 : if (last_discriminant == INT64_MAX)
63 0 : rust_error_at (item.get_locus (), "discriminant too big");
64 :
65 1832 : Analysis::NodeMapping mapping (item.get_mappings ().get_crate_num (),
66 916 : item.get_mappings ().get_nodeid (),
67 916 : mappings.get_next_hir_id (
68 916 : item.get_mappings ().get_crate_num ()),
69 916 : item.get_mappings ().get_local_defid ());
70 916 : auto discim_expr = std::make_unique<HIR::LiteralExpr> (
71 1832 : HIR::LiteralExpr (mapping, std::to_string (last_discriminant),
72 : HIR::Literal::LitType::INT,
73 2748 : PrimitiveCoreType::CORETYPE_I64, item.get_locus (), {}));
74 :
75 916 : TyTy::BaseType *isize = nullptr;
76 916 : bool ok = context->lookup_builtin ("isize", &isize);
77 916 : rust_assert (ok);
78 916 : context->insert_type (mapping, isize);
79 :
80 916 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
81 :
82 916 : CanonicalPath canonical_path
83 916 : = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid (),
84 916 : Resolver2_0::Namespace::Types);
85 :
86 916 : RustIdent ident{canonical_path, item.get_locus ()};
87 916 : variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
88 916 : item.get_mappings ().get_defid (),
89 916 : item.get_identifier ().as_string (), ident,
90 3664 : std::move (discim_expr));
91 916 : }
92 :
93 : void
94 291 : TypeCheckEnumItem::visit (HIR::EnumItemDiscriminant &item)
95 : {
96 291 : if (last_discriminant == INT64_MAX)
97 0 : rust_error_at (item.get_locus (), "discriminant too big");
98 :
99 291 : auto &discriminant = item.get_discriminant_expression ();
100 291 : auto capacity_type = TypeCheckExpr::Resolve (discriminant);
101 284 : if (capacity_type->get_kind () == TyTy::TypeKind::ERROR)
102 2 : return;
103 :
104 282 : TyTy::ISizeType *expected_ty
105 282 : = new TyTy::ISizeType (discriminant.get_mappings ().get_hirid ());
106 282 : context->insert_type (discriminant.get_mappings (), expected_ty);
107 :
108 282 : unify_site (item.get_mappings ().get_hirid (),
109 282 : TyTy::TyWithLocation (expected_ty),
110 282 : TyTy::TyWithLocation (capacity_type), item.get_locus ());
111 :
112 282 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
113 :
114 282 : CanonicalPath canonical_path
115 282 : = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid (),
116 282 : Resolver2_0::Namespace::Types);
117 :
118 282 : RustIdent ident{canonical_path, item.get_locus ()};
119 282 : variant
120 282 : = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
121 282 : item.get_mappings ().get_defid (),
122 282 : item.get_identifier ().as_string (), ident,
123 1128 : item.get_discriminant_expression ().clone_expr ());
124 282 : }
125 :
126 : void
127 663 : TypeCheckEnumItem::visit (HIR::EnumItemTuple &item)
128 : {
129 663 : if (last_discriminant == INT64_MAX)
130 0 : rust_error_at (item.get_locus (), "discriminant too big");
131 :
132 663 : std::vector<TyTy::StructFieldType *> fields;
133 663 : size_t idx = 0;
134 1412 : for (auto &field : item.get_tuple_fields ())
135 : {
136 749 : TyTy::BaseType *field_type
137 749 : = TypeCheckType::Resolve (field.get_field_type ());
138 749 : TyTy::StructFieldType *ty_field
139 1498 : = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
140 749 : std::to_string (idx), field_type,
141 749 : field.get_locus ());
142 749 : fields.push_back (ty_field);
143 749 : context->insert_type (field.get_mappings (), ty_field->get_field_type ());
144 749 : idx++;
145 : }
146 :
147 1326 : Analysis::NodeMapping mapping (item.get_mappings ().get_crate_num (),
148 663 : item.get_mappings ().get_nodeid (),
149 663 : mappings.get_next_hir_id (
150 663 : item.get_mappings ().get_crate_num ()),
151 663 : item.get_mappings ().get_local_defid ());
152 663 : auto discim_expr = std::make_unique<HIR::LiteralExpr> (
153 1326 : HIR::LiteralExpr (mapping, std::to_string (last_discriminant),
154 : HIR::Literal::LitType::INT,
155 1989 : PrimitiveCoreType::CORETYPE_I64, item.get_locus (), {}));
156 :
157 663 : TyTy::BaseType *isize = nullptr;
158 663 : bool ok = context->lookup_builtin ("isize", &isize);
159 663 : rust_assert (ok);
160 663 : context->insert_type (mapping, isize);
161 :
162 663 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
163 :
164 663 : CanonicalPath canonical_path
165 663 : = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid (),
166 663 : Resolver2_0::Namespace::Types);
167 :
168 663 : RustIdent ident{canonical_path, item.get_locus ()};
169 663 : variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
170 663 : item.get_mappings ().get_defid (),
171 663 : item.get_identifier ().as_string (), ident,
172 : TyTy::VariantDef::VariantType::TUPLE,
173 2652 : std::move (discim_expr), fields);
174 663 : }
175 :
176 : void
177 89 : TypeCheckEnumItem::visit (HIR::EnumItemStruct &item)
178 : {
179 89 : if (last_discriminant == INT64_MAX)
180 0 : rust_error_at (item.get_locus (), "discriminant too big");
181 :
182 89 : std::vector<TyTy::StructFieldType *> fields;
183 235 : for (auto &field : item.get_struct_fields ())
184 : {
185 146 : TyTy::BaseType *field_type
186 146 : = TypeCheckType::Resolve (field.get_field_type ());
187 146 : TyTy::StructFieldType *ty_field
188 146 : = new TyTy::StructFieldType (field.get_mappings ().get_hirid (),
189 146 : field.get_field_name ().as_string (),
190 292 : field_type, field.get_locus ());
191 146 : fields.push_back (ty_field);
192 146 : context->insert_type (field.get_mappings (), ty_field->get_field_type ());
193 : }
194 :
195 178 : Analysis::NodeMapping mapping (item.get_mappings ().get_crate_num (),
196 89 : item.get_mappings ().get_nodeid (),
197 89 : mappings.get_next_hir_id (
198 89 : item.get_mappings ().get_crate_num ()),
199 89 : item.get_mappings ().get_local_defid ());
200 89 : auto discrim_expr = std::make_unique<HIR::LiteralExpr> (
201 178 : HIR::LiteralExpr (mapping, std::to_string (last_discriminant),
202 : HIR::Literal::LitType::INT,
203 267 : PrimitiveCoreType::CORETYPE_I64, item.get_locus (), {}));
204 :
205 89 : TyTy::BaseType *isize = nullptr;
206 89 : bool ok = context->lookup_builtin ("isize", &isize);
207 89 : rust_assert (ok);
208 89 : context->insert_type (mapping, isize);
209 :
210 89 : auto &nr_ctx = Resolver2_0::FinalizedNameResolutionContext::get ();
211 :
212 89 : CanonicalPath canonical_path
213 89 : = nr_ctx.to_canonical_path (item.get_mappings ().get_nodeid (),
214 89 : Resolver2_0::Namespace::Types);
215 :
216 89 : RustIdent ident{canonical_path, item.get_locus ()};
217 89 : variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
218 89 : item.get_mappings ().get_defid (),
219 89 : item.get_identifier ().as_string (), ident,
220 : TyTy::VariantDef::VariantType::STRUCT,
221 356 : std::move (discrim_expr), fields);
222 89 : }
223 :
224 : } // namespace Resolver
225 : } // namespace Rust
|