| 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 |
#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-type-util.h" |
| 24 |
#include "rust-immutable-name-resolution-context.h" |
| 25 |
|
| 26 |
// for flag_name_resolution_2_0 |
| 27 |
#include "options.h" |
| 28 |
|
| 29 |
namespace Rust { |
| 30 |
namespace Resolver { |
| 31 |
|
| 32 |
TyTy::VariantDef * |
| 33 |
TypeCheckEnumItem::Resolve (HIR::EnumItem &item, int64_t last_discriminant) |
| 34 |
{ |
| 35 |
TypeCheckEnumItem resolver (last_discriminant); |
| 36 |
switch (item.get_enum_item_kind ()) |
| 37 |
{ |
| 38 |
case HIR::EnumItem::EnumItemKind::Named: |
| 39 |
resolver.visit (static_cast<HIR::EnumItem &> (item)); |
| 40 |
break; |
| 41 |
|
| 42 |
case HIR::EnumItem::EnumItemKind::Tuple: |
| 43 |
resolver.visit (static_cast<HIR::EnumItemTuple &> (item)); |
| 44 |
break; |
| 45 |
|
| 46 |
case HIR::EnumItem::EnumItemKind::Struct: |
| 47 |
resolver.visit (static_cast<HIR::EnumItemStruct &> (item)); |
| 48 |
break; |
| 49 |
|
| 50 |
case HIR::EnumItem::EnumItemKind::Discriminant: |
| 51 |
resolver.visit (static_cast<HIR::EnumItemDiscriminant &> (item)); |
| 52 |
break; |
| 53 |
} |
| 54 |
return resolver.variant; |
| 55 |
} |
| 56 |
|
| 57 |
TypeCheckEnumItem::TypeCheckEnumItem (int64_t last_discriminant) |
| 58 |
: TypeCheckBase (), variant (nullptr), last_discriminant (last_discriminant) |
| 59 |
{} |
| 60 |
|
| 61 |
void |
| 62 |
TypeCheckEnumItem::visit (HIR::EnumItem &item) |
| 63 |
{ |
| 64 |
if (last_discriminant == INT64_MAX) |
| 65 |
rust_error_at (item.get_locus (), "discriminant too big"); |
| 66 |
|
| 67 |
Analysis::NodeMapping mapping (item.get_mappings ().get_crate_num (), |
| 68 |
item.get_mappings ().get_nodeid (), |
| 69 |
mappings.get_next_hir_id ( |
| 70 |
item.get_mappings ().get_crate_num ()), |
| 71 |
item.get_mappings ().get_local_defid ()); |
| 72 |
auto discim_expr = std::make_unique<HIR::LiteralExpr> ( |
| 73 |
HIR::LiteralExpr (mapping, std::to_string (last_discriminant), |
| 74 |
HIR::Literal::LitType::INT, |
| 75 |
PrimitiveCoreType::CORETYPE_I64, item.get_locus (), {})); |
| 76 |
|
| 77 |
TyTy::BaseType *isize = nullptr; |
| 78 |
bool ok = context->lookup_builtin ("isize", &isize); |
| 79 |
rust_assert (ok); |
| 80 |
context->insert_type (mapping, isize); |
| 81 |
|
| 82 |
tl::optional<CanonicalPath> canonical_path; |
| 83 |
|
| 84 |
if (flag_name_resolution_2_0) |
| 85 |
{ |
| 86 |
auto &nr_ctx |
| 87 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 88 |
|
| 89 |
canonical_path |
| 90 |
= nr_ctx.types.to_canonical_path (item.get_mappings ().get_nodeid ()); |
| 91 |
} |
| 92 |
else |
| 93 |
{ |
| 94 |
canonical_path |
| 95 |
= mappings.lookup_canonical_path (item.get_mappings ().get_nodeid ()); |
| 96 |
} |
| 97 |
|
| 98 |
rust_assert (canonical_path.has_value ()); |
| 99 |
|
| 100 |
RustIdent ident{*canonical_path, item.get_locus ()}; |
| 101 |
variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (), |
| 102 |
item.get_mappings ().get_defid (), |
| 103 |
item.get_identifier ().as_string (), ident, |
| 104 |
std::move (discim_expr)); |
| 105 |
} |
| 106 |
|
| 107 |
void |
| 108 |
TypeCheckEnumItem::visit (HIR::EnumItemDiscriminant &item) |
| 109 |
{ |
| 110 |
if (last_discriminant == INT64_MAX) |
| 111 |
rust_error_at (item.get_locus (), "discriminant too big"); |
| 112 |
|
| 113 |
auto &discriminant = item.get_discriminant_expression (); |
| 114 |
auto capacity_type = TypeCheckExpr::Resolve (discriminant); |
| 115 |
if (capacity_type->get_kind () == TyTy::TypeKind::ERROR) |
| 116 |
return; |
| 117 |
|
| 118 |
TyTy::ISizeType *expected_ty |
| 119 |
= new TyTy::ISizeType (discriminant.get_mappings ().get_hirid ()); |
| 120 |
context->insert_type (discriminant.get_mappings (), expected_ty); |
| 121 |
|
| 122 |
unify_site (item.get_mappings ().get_hirid (), |
| 123 |
TyTy::TyWithLocation (expected_ty), |
| 124 |
TyTy::TyWithLocation (capacity_type), item.get_locus ()); |
| 125 |
|
| 126 |
tl::optional<CanonicalPath> canonical_path; |
| 127 |
|
| 128 |
if (flag_name_resolution_2_0) |
| 129 |
{ |
| 130 |
auto &nr_ctx |
| 131 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 132 |
|
| 133 |
canonical_path |
| 134 |
= nr_ctx.types.to_canonical_path (item.get_mappings ().get_nodeid ()); |
| 135 |
} |
| 136 |
else |
| 137 |
{ |
| 138 |
canonical_path |
| 139 |
= mappings.lookup_canonical_path (item.get_mappings ().get_nodeid ()); |
| 140 |
} |
| 141 |
|
| 142 |
rust_assert (canonical_path.has_value ()); |
| 143 |
|
| 144 |
RustIdent ident{*canonical_path, item.get_locus ()}; |
| 145 |
variant |
| 146 |
= new TyTy::VariantDef (item.get_mappings ().get_hirid (), |
| 147 |
item.get_mappings ().get_defid (), |
| 148 |
item.get_identifier ().as_string (), ident, |
| 149 |
item.get_discriminant_expression ().clone_expr ()); |
| 150 |
} |
| 151 |
|
| 152 |
void |
| 153 |
TypeCheckEnumItem::visit (HIR::EnumItemTuple &item) |
| 154 |
{ |
| 155 |
if (last_discriminant == INT64_MAX) |
| 156 |
rust_error_at (item.get_locus (), "discriminant too big"); |
| 157 |
|
| 158 |
std::vector<TyTy::StructFieldType *> fields; |
| 159 |
size_t idx = 0; |
| 160 |
for (auto &field : item.get_tuple_fields ()) |
| 161 |
{ |
| 162 |
TyTy::BaseType *field_type |
| 163 |
= TypeCheckType::Resolve (field.get_field_type ()); |
| 164 |
TyTy::StructFieldType *ty_field |
| 165 |
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (), |
| 166 |
std::to_string (idx), field_type, |
| 167 |
field.get_locus ()); |
| 168 |
fields.push_back (ty_field); |
| 169 |
context->insert_type (field.get_mappings (), ty_field->get_field_type ()); |
| 170 |
idx++; |
| 171 |
} |
| 172 |
|
| 173 |
Analysis::NodeMapping mapping (item.get_mappings ().get_crate_num (), |
| 174 |
item.get_mappings ().get_nodeid (), |
| 175 |
mappings.get_next_hir_id ( |
| 176 |
item.get_mappings ().get_crate_num ()), |
| 177 |
item.get_mappings ().get_local_defid ()); |
| 178 |
auto discim_expr = std::make_unique<HIR::LiteralExpr> ( |
| 179 |
HIR::LiteralExpr (mapping, std::to_string (last_discriminant), |
| 180 |
HIR::Literal::LitType::INT, |
| 181 |
PrimitiveCoreType::CORETYPE_I64, item.get_locus (), {})); |
| 182 |
|
| 183 |
TyTy::BaseType *isize = nullptr; |
| 184 |
bool ok = context->lookup_builtin ("isize", &isize); |
| 185 |
rust_assert (ok); |
| 186 |
context->insert_type (mapping, isize); |
| 187 |
|
| 188 |
tl::optional<CanonicalPath> canonical_path; |
| 189 |
|
| 190 |
if (flag_name_resolution_2_0) |
| 191 |
{ |
| 192 |
auto &nr_ctx |
| 193 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 194 |
|
| 195 |
canonical_path |
| 196 |
= nr_ctx.types.to_canonical_path (item.get_mappings ().get_nodeid ()); |
| 197 |
} |
| 198 |
else |
| 199 |
{ |
| 200 |
canonical_path |
| 201 |
= mappings.lookup_canonical_path (item.get_mappings ().get_nodeid ()); |
| 202 |
} |
| 203 |
|
| 204 |
rust_assert (canonical_path.has_value ()); |
| 205 |
|
| 206 |
RustIdent ident{*canonical_path, item.get_locus ()}; |
| 207 |
variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (), |
| 208 |
item.get_mappings ().get_defid (), |
| 209 |
item.get_identifier ().as_string (), ident, |
| 210 |
TyTy::VariantDef::VariantType::TUPLE, |
| 211 |
std::move (discim_expr), fields); |
| 212 |
} |
| 213 |
|
| 214 |
void |
| 215 |
TypeCheckEnumItem::visit (HIR::EnumItemStruct &item) |
| 216 |
{ |
| 217 |
if (last_discriminant == INT64_MAX) |
| 218 |
rust_error_at (item.get_locus (), "discriminant too big"); |
| 219 |
|
| 220 |
std::vector<TyTy::StructFieldType *> fields; |
| 221 |
for (auto &field : item.get_struct_fields ()) |
| 222 |
{ |
| 223 |
TyTy::BaseType *field_type |
| 224 |
= TypeCheckType::Resolve (field.get_field_type ()); |
| 225 |
TyTy::StructFieldType *ty_field |
| 226 |
= new TyTy::StructFieldType (field.get_mappings ().get_hirid (), |
| 227 |
field.get_field_name ().as_string (), |
| 228 |
field_type, field.get_locus ()); |
| 229 |
fields.push_back (ty_field); |
| 230 |
context->insert_type (field.get_mappings (), ty_field->get_field_type ()); |
| 231 |
} |
| 232 |
|
| 233 |
Analysis::NodeMapping mapping (item.get_mappings ().get_crate_num (), |
| 234 |
item.get_mappings ().get_nodeid (), |
| 235 |
mappings.get_next_hir_id ( |
| 236 |
item.get_mappings ().get_crate_num ()), |
| 237 |
item.get_mappings ().get_local_defid ()); |
| 238 |
auto discrim_expr = std::make_unique<HIR::LiteralExpr> ( |
| 239 |
HIR::LiteralExpr (mapping, std::to_string (last_discriminant), |
| 240 |
HIR::Literal::LitType::INT, |
| 241 |
PrimitiveCoreType::CORETYPE_I64, item.get_locus (), {})); |
| 242 |
|
| 243 |
TyTy::BaseType *isize = nullptr; |
| 244 |
bool ok = context->lookup_builtin ("isize", &isize); |
| 245 |
rust_assert (ok); |
| 246 |
context->insert_type (mapping, isize); |
| 247 |
|
| 248 |
tl::optional<CanonicalPath> canonical_path; |
| 249 |
|
| 250 |
if (flag_name_resolution_2_0) |
| 251 |
{ |
| 252 |
auto &nr_ctx |
| 253 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 254 |
|
| 255 |
canonical_path |
| 256 |
= nr_ctx.types.to_canonical_path (item.get_mappings ().get_nodeid ()); |
| 257 |
} |
| 258 |
else |
| 259 |
{ |
| 260 |
canonical_path |
| 261 |
= mappings.lookup_canonical_path (item.get_mappings ().get_nodeid ()); |
| 262 |
} |
| 263 |
|
| 264 |
rust_assert (canonical_path.has_value ()); |
| 265 |
|
| 266 |
RustIdent ident{*canonical_path, item.get_locus ()}; |
| 267 |
variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (), |
| 268 |
item.get_mappings ().get_defid (), |
| 269 |
item.get_identifier ().as_string (), ident, |
| 270 |
TyTy::VariantDef::VariantType::STRUCT, |
| 271 |
std::move (discrim_expr), fields); |
| 272 |
} |
| 273 |
|
| 274 |
} // namespace Resolver |
| 275 |
} // namespace Rust |