| 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-ast-lower-item.h" |
| 20 |
#include "rust-diagnostics.h" |
| 21 |
#include "rust-ast-lower.h" |
| 22 |
#include "rust-ast-lower-base.h" |
| 23 |
#include "rust-ast-lower-enumitem.h" |
| 24 |
#include "rust-ast-lower-type.h" |
| 25 |
#include "rust-ast-lower-implitem.h" |
| 26 |
#include "rust-ast-lower-expr.h" |
| 27 |
#include "rust-ast-lower-pattern.h" |
| 28 |
#include "rust-ast-lower-block.h" |
| 29 |
#include "rust-item.h" |
| 30 |
|
| 31 |
namespace Rust { |
| 32 |
namespace HIR { |
| 33 |
|
| 34 |
HIR::Item * |
| 35 |
ASTLoweringItem::translate (AST::Item &item) |
| 36 |
{ |
| 37 |
ASTLoweringItem resolver; |
| 38 |
item.accept_vis (resolver); |
| 39 |
|
| 40 |
if (resolver.translated != nullptr) |
| 41 |
{ |
| 42 |
auto id = resolver.translated->get_mappings ().get_hirid (); |
| 43 |
auto defid = resolver.translated->get_mappings ().get_defid (); |
| 44 |
auto locus = resolver.translated->get_locus (); |
| 45 |
|
| 46 |
resolver.handle_outer_attributes (*resolver.translated); |
| 47 |
resolver.mappings.insert_ast_item (&item); |
| 48 |
resolver.mappings.insert_hir_item (resolver.translated); |
| 49 |
resolver.mappings.insert_location (id, locus); |
| 50 |
resolver.mappings.insert_defid_mapping (defid, resolver.translated); |
| 51 |
} |
| 52 |
|
| 53 |
return resolver.translated; |
| 54 |
} |
| 55 |
|
| 56 |
void |
| 57 |
ASTLoweringItem::visit (AST::Module &module) |
| 58 |
{ |
| 59 |
auto crate_num = mappings.get_current_crate (); |
| 60 |
Analysis::NodeMapping mapping (crate_num, module.get_node_id (), |
| 61 |
mappings.get_next_hir_id (crate_num), |
| 62 |
mappings.get_next_localdef_id (crate_num)); |
| 63 |
|
| 64 |
// should be lowered from module.get_vis() |
| 65 |
HIR::Visibility vis = translate_visibility (module.get_visibility ()); |
| 66 |
|
| 67 |
auto items = std::vector<std::unique_ptr<Item>> (); |
| 68 |
|
| 69 |
for (auto &item : module.get_items ()) |
| 70 |
{ |
| 71 |
auto transitem = translate (*item); |
| 72 |
// The item may be null if it doesn't need to live in the HIR - for |
| 73 |
// example, macro rules definitions |
| 74 |
if (transitem) |
| 75 |
items.push_back (std::unique_ptr<Item> (transitem)); |
| 76 |
} |
| 77 |
|
| 78 |
// should be lowered/copied from module.get_in/outer_attrs() |
| 79 |
AST::AttrVec inner_attrs = module.get_inner_attrs (); |
| 80 |
AST::AttrVec outer_attrs = module.get_outer_attrs (); |
| 81 |
|
| 82 |
translated |
| 83 |
= new HIR::Module (mapping, module.get_name (), module.get_locus (), |
| 84 |
std::move (items), std::move (vis), |
| 85 |
std::move (inner_attrs), std::move (outer_attrs)); |
| 86 |
mappings.insert_module (static_cast<Module *> (translated)); |
| 87 |
} |
| 88 |
|
| 89 |
void |
| 90 |
ASTLoweringItem::visit (AST::TypeAlias &alias) |
| 91 |
{ |
| 92 |
std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; |
| 93 |
for (auto &item : alias.get_where_clause ().get_items ()) |
| 94 |
{ |
| 95 |
HIR::WhereClauseItem *i |
| 96 |
= ASTLowerWhereClauseItem::translate (*item.get ()); |
| 97 |
where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); |
| 98 |
} |
| 99 |
|
| 100 |
HIR::WhereClause where_clause (std::move (where_clause_items)); |
| 101 |
HIR::Visibility vis = translate_visibility (alias.get_visibility ()); |
| 102 |
|
| 103 |
std::vector<std::unique_ptr<HIR::GenericParam>> generic_params; |
| 104 |
if (alias.has_generics ()) |
| 105 |
generic_params = lower_generic_params (alias.get_generic_params ()); |
| 106 |
|
| 107 |
HIR::Type *existing_type |
| 108 |
= ASTLoweringType::translate (alias.get_type_aliased ()); |
| 109 |
|
| 110 |
auto crate_num = mappings.get_current_crate (); |
| 111 |
Analysis::NodeMapping mapping (crate_num, alias.get_node_id (), |
| 112 |
mappings.get_next_hir_id (crate_num), |
| 113 |
mappings.get_next_localdef_id (crate_num)); |
| 114 |
|
| 115 |
translated |
| 116 |
= new HIR::TypeAlias (mapping, alias.get_new_type_name (), |
| 117 |
std::move (generic_params), std::move (where_clause), |
| 118 |
std::unique_ptr<HIR::Type> (existing_type), |
| 119 |
std::move (vis), alias.get_outer_attrs (), |
| 120 |
alias.get_locus ()); |
| 121 |
} |
| 122 |
|
| 123 |
void |
| 124 |
ASTLoweringItem::visit (AST::TupleStruct &struct_decl) |
| 125 |
{ |
| 126 |
std::vector<std::unique_ptr<HIR::GenericParam>> generic_params; |
| 127 |
if (struct_decl.has_generics ()) |
| 128 |
{ |
| 129 |
generic_params = lower_generic_params (struct_decl.get_generic_params ()); |
| 130 |
} |
| 131 |
|
| 132 |
std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; |
| 133 |
for (auto &item : struct_decl.get_where_clause ().get_items ()) |
| 134 |
{ |
| 135 |
HIR::WhereClauseItem *i |
| 136 |
= ASTLowerWhereClauseItem::translate (*item.get ()); |
| 137 |
where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); |
| 138 |
} |
| 139 |
|
| 140 |
HIR::WhereClause where_clause (std::move (where_clause_items)); |
| 141 |
HIR::Visibility vis = translate_visibility (struct_decl.get_visibility ()); |
| 142 |
|
| 143 |
std::vector<HIR::TupleField> fields; |
| 144 |
for (AST::TupleField &field : struct_decl.get_fields ()) |
| 145 |
{ |
| 146 |
if (field.get_field_type ().is_marked_for_strip ()) |
| 147 |
continue; |
| 148 |
|
| 149 |
// FIXME: How do we get the visibility from here? |
| 150 |
HIR::Visibility vis = translate_visibility (field.get_visibility ()); |
| 151 |
HIR::Type *type = ASTLoweringType::translate (field.get_field_type ()); |
| 152 |
|
| 153 |
auto crate_num = mappings.get_current_crate (); |
| 154 |
Analysis::NodeMapping mapping (crate_num, field.get_node_id (), |
| 155 |
mappings.get_next_hir_id (crate_num), |
| 156 |
mappings.get_next_localdef_id (crate_num)); |
| 157 |
|
| 158 |
HIR::TupleField translated_field (mapping, |
| 159 |
std::unique_ptr<HIR::Type> (type), vis, |
| 160 |
field.get_locus (), |
| 161 |
field.get_outer_attrs ()); |
| 162 |
fields.push_back (std::move (translated_field)); |
| 163 |
} |
| 164 |
|
| 165 |
auto crate_num = mappings.get_current_crate (); |
| 166 |
Analysis::NodeMapping mapping (crate_num, struct_decl.get_node_id (), |
| 167 |
mappings.get_next_hir_id (crate_num), |
| 168 |
mappings.get_next_localdef_id (crate_num)); |
| 169 |
|
| 170 |
translated = new HIR::TupleStruct (mapping, std::move (fields), |
| 171 |
struct_decl.get_identifier (), |
| 172 |
std::move (generic_params), |
| 173 |
std::move (where_clause), vis, |
| 174 |
struct_decl.get_outer_attrs (), |
| 175 |
struct_decl.get_locus ()); |
| 176 |
} |
| 177 |
|
| 178 |
void |
| 179 |
ASTLoweringItem::visit (AST::StructStruct &struct_decl) |
| 180 |
{ |
| 181 |
std::vector<std::unique_ptr<HIR::GenericParam>> generic_params; |
| 182 |
if (struct_decl.has_generics ()) |
| 183 |
{ |
| 184 |
generic_params = lower_generic_params (struct_decl.get_generic_params ()); |
| 185 |
} |
| 186 |
|
| 187 |
std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; |
| 188 |
for (auto &item : struct_decl.get_where_clause ().get_items ()) |
| 189 |
{ |
| 190 |
HIR::WhereClauseItem *i |
| 191 |
= ASTLowerWhereClauseItem::translate (*item.get ()); |
| 192 |
where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); |
| 193 |
} |
| 194 |
|
| 195 |
HIR::WhereClause where_clause (std::move (where_clause_items)); |
| 196 |
|
| 197 |
HIR::Visibility vis = translate_visibility (struct_decl.get_visibility ()); |
| 198 |
|
| 199 |
bool is_unit = struct_decl.is_unit_struct (); |
| 200 |
std::vector<HIR::StructField> fields; |
| 201 |
for (AST::StructField &field : struct_decl.get_fields ()) |
| 202 |
{ |
| 203 |
if (field.get_field_type ().is_marked_for_strip ()) |
| 204 |
continue; |
| 205 |
|
| 206 |
HIR::Visibility vis = translate_visibility (field.get_visibility ()); |
| 207 |
HIR::Type *type = ASTLoweringType::translate (field.get_field_type ()); |
| 208 |
|
| 209 |
auto crate_num = mappings.get_current_crate (); |
| 210 |
Analysis::NodeMapping mapping (crate_num, field.get_node_id (), |
| 211 |
mappings.get_next_hir_id (crate_num), |
| 212 |
mappings.get_next_localdef_id (crate_num)); |
| 213 |
|
| 214 |
HIR::StructField translated_field (mapping, field.get_field_name (), |
| 215 |
std::unique_ptr<HIR::Type> (type), vis, |
| 216 |
field.get_locus (), |
| 217 |
field.get_outer_attrs ()); |
| 218 |
|
| 219 |
if (struct_field_name_exists (fields, translated_field)) |
| 220 |
break; |
| 221 |
|
| 222 |
fields.push_back (std::move (translated_field)); |
| 223 |
} |
| 224 |
|
| 225 |
auto crate_num = mappings.get_current_crate (); |
| 226 |
Analysis::NodeMapping mapping (crate_num, struct_decl.get_node_id (), |
| 227 |
mappings.get_next_hir_id (crate_num), |
| 228 |
mappings.get_next_localdef_id (crate_num)); |
| 229 |
|
| 230 |
translated = new HIR::StructStruct (mapping, std::move (fields), |
| 231 |
struct_decl.get_identifier (), |
| 232 |
std::move (generic_params), |
| 233 |
std::move (where_clause), is_unit, vis, |
| 234 |
struct_decl.get_outer_attrs (), |
| 235 |
struct_decl.get_locus ()); |
| 236 |
} |
| 237 |
|
| 238 |
void |
| 239 |
ASTLoweringItem::visit (AST::Enum &enum_decl) |
| 240 |
{ |
| 241 |
std::vector<std::unique_ptr<HIR::GenericParam>> generic_params; |
| 242 |
if (enum_decl.has_generics ()) |
| 243 |
{ |
| 244 |
generic_params = lower_generic_params (enum_decl.get_generic_params ()); |
| 245 |
} |
| 246 |
|
| 247 |
std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; |
| 248 |
for (auto &item : enum_decl.get_where_clause ().get_items ()) |
| 249 |
{ |
| 250 |
HIR::WhereClauseItem *i |
| 251 |
= ASTLowerWhereClauseItem::translate (*item.get ()); |
| 252 |
where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); |
| 253 |
} |
| 254 |
|
| 255 |
HIR::WhereClause where_clause (std::move (where_clause_items)); |
| 256 |
HIR::Visibility vis = translate_visibility (enum_decl.get_visibility ()); |
| 257 |
|
| 258 |
// bool is_unit = enum_decl.is_zero_variant (); |
| 259 |
std::vector<std::unique_ptr<HIR::EnumItem>> items; |
| 260 |
for (auto &variant : enum_decl.get_variants ()) |
| 261 |
{ |
| 262 |
if (variant->is_marked_for_strip ()) |
| 263 |
continue; |
| 264 |
|
| 265 |
HIR::EnumItem *hir_item = ASTLoweringEnumItem::translate (variant.get ()); |
| 266 |
items.push_back (std::unique_ptr<HIR::EnumItem> (hir_item)); |
| 267 |
} |
| 268 |
|
| 269 |
auto crate_num = mappings.get_current_crate (); |
| 270 |
Analysis::NodeMapping mapping (crate_num, enum_decl.get_node_id (), |
| 271 |
mappings.get_next_hir_id (crate_num), |
| 272 |
mappings.get_next_localdef_id (crate_num)); |
| 273 |
|
| 274 |
HIR::Enum *hir_enum |
| 275 |
= new HIR::Enum (mapping, enum_decl.get_identifier (), vis, |
| 276 |
std::move (generic_params), std::move (where_clause), |
| 277 |
std::move (items), enum_decl.get_outer_attrs (), |
| 278 |
enum_decl.get_locus ()); |
| 279 |
translated = hir_enum; |
| 280 |
for (auto &variant : hir_enum->get_variants ()) |
| 281 |
{ |
| 282 |
mappings.insert_hir_enumitem (hir_enum, variant.get ()); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
void |
| 287 |
ASTLoweringItem::visit (AST::Union &union_decl) |
| 288 |
{ |
| 289 |
std::vector<std::unique_ptr<HIR::GenericParam>> generic_params; |
| 290 |
if (union_decl.has_generics ()) |
| 291 |
{ |
| 292 |
generic_params = lower_generic_params (union_decl.get_generic_params ()); |
| 293 |
} |
| 294 |
|
| 295 |
std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; |
| 296 |
for (auto &item : union_decl.get_where_clause ().get_items ()) |
| 297 |
{ |
| 298 |
HIR::WhereClauseItem *i |
| 299 |
= ASTLowerWhereClauseItem::translate (*item.get ()); |
| 300 |
where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); |
| 301 |
} |
| 302 |
HIR::WhereClause where_clause (std::move (where_clause_items)); |
| 303 |
HIR::Visibility vis = translate_visibility (union_decl.get_visibility ()); |
| 304 |
|
| 305 |
std::vector<HIR::StructField> variants; |
| 306 |
for (AST::StructField &variant : union_decl.get_variants ()) |
| 307 |
{ |
| 308 |
if (variant.get_field_type ().is_marked_for_strip ()) |
| 309 |
continue; |
| 310 |
|
| 311 |
// FIXME: Does visibility apply here? |
| 312 |
HIR::Visibility vis = translate_visibility (variant.get_visibility ()); |
| 313 |
HIR::Type *type = ASTLoweringType::translate (variant.get_field_type ()); |
| 314 |
|
| 315 |
auto crate_num = mappings.get_current_crate (); |
| 316 |
Analysis::NodeMapping mapping (crate_num, variant.get_node_id (), |
| 317 |
mappings.get_next_hir_id (crate_num), |
| 318 |
mappings.get_next_localdef_id (crate_num)); |
| 319 |
|
| 320 |
HIR::StructField translated_variant (mapping, variant.get_field_name (), |
| 321 |
std::unique_ptr<HIR::Type> (type), |
| 322 |
vis, variant.get_locus (), |
| 323 |
variant.get_outer_attrs ()); |
| 324 |
|
| 325 |
if (struct_field_name_exists (variants, translated_variant)) |
| 326 |
break; |
| 327 |
|
| 328 |
variants.push_back (std::move (translated_variant)); |
| 329 |
} |
| 330 |
|
| 331 |
auto crate_num = mappings.get_current_crate (); |
| 332 |
Analysis::NodeMapping mapping (crate_num, union_decl.get_node_id (), |
| 333 |
mappings.get_next_hir_id (crate_num), |
| 334 |
mappings.get_next_localdef_id (crate_num)); |
| 335 |
|
| 336 |
translated |
| 337 |
= new HIR::Union (mapping, union_decl.get_identifier (), vis, |
| 338 |
std::move (generic_params), std::move (where_clause), |
| 339 |
std::move (variants), union_decl.get_outer_attrs (), |
| 340 |
union_decl.get_locus ()); |
| 341 |
} |
| 342 |
|
| 343 |
void |
| 344 |
ASTLoweringItem::visit (AST::StaticItem &var) |
| 345 |
{ |
| 346 |
HIR::Visibility vis = translate_visibility (var.get_visibility ()); |
| 347 |
|
| 348 |
HIR::Type *type = ASTLoweringType::translate (var.get_type (), true); |
| 349 |
HIR::Expr *expr = ASTLoweringExpr::translate (var.get_expr ()); |
| 350 |
|
| 351 |
auto crate_num = mappings.get_current_crate (); |
| 352 |
Analysis::NodeMapping mapping (crate_num, var.get_node_id (), |
| 353 |
mappings.get_next_hir_id (crate_num), |
| 354 |
mappings.get_next_localdef_id (crate_num)); |
| 355 |
|
| 356 |
translated = new HIR::StaticItem (mapping, var.get_identifier (), |
| 357 |
var.is_mutable () ? Mutability::Mut |
| 358 |
: Mutability::Imm, |
| 359 |
std::unique_ptr<HIR::Type> (type), |
| 360 |
std::unique_ptr<HIR::Expr> (expr), vis, |
| 361 |
var.get_outer_attrs (), var.get_locus ()); |
| 362 |
} |
| 363 |
|
| 364 |
void |
| 365 |
ASTLoweringItem::visit (AST::ConstantItem &constant) |
| 366 |
{ |
| 367 |
HIR::Visibility vis = translate_visibility (constant.get_visibility ()); |
| 368 |
|
| 369 |
HIR::Type *type = ASTLoweringType::translate (constant.get_type (), true); |
| 370 |
HIR::Expr *expr = ASTLoweringExpr::translate (constant.get_expr ()); |
| 371 |
|
| 372 |
auto crate_num = mappings.get_current_crate (); |
| 373 |
Analysis::NodeMapping mapping (crate_num, constant.get_node_id (), |
| 374 |
mappings.get_next_hir_id (crate_num), |
| 375 |
mappings.get_next_localdef_id (crate_num)); |
| 376 |
|
| 377 |
translated = new HIR::ConstantItem (mapping, constant.get_identifier (), vis, |
| 378 |
std::unique_ptr<HIR::Type> (type), |
| 379 |
std::unique_ptr<HIR::Expr> (expr), |
| 380 |
constant.get_outer_attrs (), |
| 381 |
constant.get_locus ()); |
| 382 |
} |
| 383 |
|
| 384 |
void |
| 385 |
ASTLoweringItem::visit (AST::Function &function) |
| 386 |
{ |
| 387 |
if (function.is_marked_for_strip ()) |
| 388 |
return; |
| 389 |
|
| 390 |
std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; |
| 391 |
for (auto &item : function.get_where_clause ().get_items ()) |
| 392 |
{ |
| 393 |
HIR::WhereClauseItem *i |
| 394 |
= ASTLowerWhereClauseItem::translate (*item.get ()); |
| 395 |
where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); |
| 396 |
} |
| 397 |
|
| 398 |
HIR::WhereClause where_clause (std::move (where_clause_items)); |
| 399 |
HIR::FunctionQualifiers qualifiers |
| 400 |
= lower_qualifiers (function.get_qualifiers ()); |
| 401 |
HIR::Visibility vis = translate_visibility (function.get_visibility ()); |
| 402 |
|
| 403 |
// need |
| 404 |
std::vector<std::unique_ptr<HIR::GenericParam>> generic_params; |
| 405 |
if (function.has_generics ()) |
| 406 |
{ |
| 407 |
generic_params = lower_generic_params (function.get_generic_params ()); |
| 408 |
} |
| 409 |
Identifier function_name = function.get_function_name (); |
| 410 |
location_t locus = function.get_locus (); |
| 411 |
|
| 412 |
std::unique_ptr<HIR::Type> return_type |
| 413 |
= function.has_return_type () ? std::unique_ptr<HIR::Type> ( |
| 414 |
ASTLoweringType::translate (function.get_return_type ())) |
| 415 |
: nullptr; |
| 416 |
|
| 417 |
std::vector<HIR::FunctionParam> function_params; |
| 418 |
for (auto &p : function.get_function_params ()) |
| 419 |
{ |
| 420 |
if (p->is_variadic () || p->is_self ()) |
| 421 |
continue; |
| 422 |
auto param = static_cast<AST::FunctionParam &> (*p); |
| 423 |
|
| 424 |
auto translated_pattern = std::unique_ptr<HIR::Pattern> ( |
| 425 |
ASTLoweringPattern::translate (param.get_pattern ())); |
| 426 |
auto translated_type = std::unique_ptr<HIR::Type> ( |
| 427 |
ASTLoweringType::translate (param.get_type ())); |
| 428 |
|
| 429 |
auto crate_num = mappings.get_current_crate (); |
| 430 |
Analysis::NodeMapping mapping (crate_num, param.get_node_id (), |
| 431 |
mappings.get_next_hir_id (crate_num), |
| 432 |
UNKNOWN_LOCAL_DEFID); |
| 433 |
|
| 434 |
auto hir_param |
| 435 |
= HIR::FunctionParam (mapping, std::move (translated_pattern), |
| 436 |
std::move (translated_type), param.get_locus ()); |
| 437 |
function_params.push_back (std::move (hir_param)); |
| 438 |
} |
| 439 |
|
| 440 |
bool terminated = false; |
| 441 |
std::unique_ptr<HIR::BlockExpr> function_body |
| 442 |
= std::unique_ptr<HIR::BlockExpr> ( |
| 443 |
ASTLoweringBlock::translate (*function.get_definition ().value (), |
| 444 |
&terminated)); |
| 445 |
|
| 446 |
auto crate_num = mappings.get_current_crate (); |
| 447 |
Analysis::NodeMapping mapping (crate_num, function.get_node_id (), |
| 448 |
mappings.get_next_hir_id (crate_num), |
| 449 |
mappings.get_next_localdef_id (crate_num)); |
| 450 |
|
| 451 |
mappings.insert_location (function_body->get_mappings ().get_hirid (), |
| 452 |
function.get_locus ()); |
| 453 |
|
| 454 |
Defaultness defaultness |
| 455 |
= function.is_default () ? Defaultness::Default : Defaultness::Final; |
| 456 |
|
| 457 |
auto fn |
| 458 |
= new HIR::Function (mapping, std::move (function_name), |
| 459 |
std::move (qualifiers), std::move (generic_params), |
| 460 |
std::move (function_params), std::move (return_type), |
| 461 |
std::move (where_clause), std::move (function_body), |
| 462 |
std::move (vis), function.get_outer_attrs (), |
| 463 |
tl::nullopt, defaultness, locus); |
| 464 |
|
| 465 |
// add the mappings for the function params at the end |
| 466 |
for (auto ¶m : fn->get_function_params ()) |
| 467 |
{ |
| 468 |
mappings.insert_hir_param (¶m); |
| 469 |
mappings.insert_location (mapping.get_hirid (), param.get_locus ()); |
| 470 |
} |
| 471 |
|
| 472 |
translated = fn; |
| 473 |
} |
| 474 |
|
| 475 |
void |
| 476 |
ASTLoweringItem::visit (AST::InherentImpl &impl_block) |
| 477 |
{ |
| 478 |
std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; |
| 479 |
for (auto &item : impl_block.get_where_clause ().get_items ()) |
| 480 |
{ |
| 481 |
HIR::WhereClauseItem *i = ASTLowerWhereClauseItem::translate (*item); |
| 482 |
where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); |
| 483 |
} |
| 484 |
|
| 485 |
HIR::WhereClause where_clause (std::move (where_clause_items)); |
| 486 |
HIR::Visibility vis = translate_visibility (impl_block.get_visibility ()); |
| 487 |
|
| 488 |
std::vector<std::unique_ptr<HIR::GenericParam>> generic_params; |
| 489 |
if (impl_block.has_generics ()) |
| 490 |
{ |
| 491 |
generic_params = lower_generic_params (impl_block.get_generic_params ()); |
| 492 |
|
| 493 |
for (auto &generic_param : generic_params) |
| 494 |
{ |
| 495 |
switch (generic_param->get_kind ()) |
| 496 |
{ |
| 497 |
case HIR::GenericParam::GenericKind::TYPE: { |
| 498 |
const HIR::TypeParam &t |
| 499 |
= static_cast<const HIR::TypeParam &> (*generic_param); |
| 500 |
|
| 501 |
if (t.has_type ()) |
| 502 |
{ |
| 503 |
rich_location rich_locus (line_table, t.get_locus ()); |
| 504 |
rich_locus.add_fixit_replace ( |
| 505 |
t.get_locus (), |
| 506 |
"for more information, see issue #36887 " |
| 507 |
"<https://github.com/rust-lang/rust/issues/36887>"); |
| 508 |
rust_error_at (rich_locus, |
| 509 |
"defaults for type parameters are only " |
| 510 |
"allowed in %<struct%>, %<enum%>, %<type%>, " |
| 511 |
"or %<trait%> definitions"); |
| 512 |
} |
| 513 |
} |
| 514 |
break; |
| 515 |
|
| 516 |
default: |
| 517 |
break; |
| 518 |
} |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
HIR::Type *impl_type = ASTLoweringType::translate (impl_block.get_type ()); |
| 523 |
|
| 524 |
auto crate_num = mappings.get_current_crate (); |
| 525 |
Analysis::NodeMapping mapping (crate_num, impl_block.get_node_id (), |
| 526 |
mappings.get_next_hir_id (crate_num), |
| 527 |
mappings.get_next_localdef_id (crate_num)); |
| 528 |
|
| 529 |
std::vector<std::unique_ptr<HIR::ImplItem>> impl_items; |
| 530 |
std::vector<HirId> impl_item_ids; |
| 531 |
for (auto &impl_item : impl_block.get_impl_items ()) |
| 532 |
{ |
| 533 |
if (impl_item->is_marked_for_strip ()) |
| 534 |
continue; |
| 535 |
|
| 536 |
HIR::ImplItem *lowered |
| 537 |
= ASTLowerImplItem::translate (*impl_item, mapping.get_hirid ()); |
| 538 |
rust_assert (lowered != nullptr); |
| 539 |
impl_items.push_back (std::unique_ptr<HIR::ImplItem> (lowered)); |
| 540 |
impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ()); |
| 541 |
} |
| 542 |
|
| 543 |
BoundPolarity polarity = BoundPolarity::RegularBound; |
| 544 |
HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock ( |
| 545 |
mapping, std::move (impl_items), std::move (generic_params), |
| 546 |
std::unique_ptr<HIR::Type> (impl_type), nullptr, where_clause, polarity, |
| 547 |
vis, impl_block.get_inner_attrs (), impl_block.get_outer_attrs (), |
| 548 |
impl_block.get_locus (), false); |
| 549 |
translated = hir_impl_block; |
| 550 |
|
| 551 |
mappings.insert_hir_impl_block (hir_impl_block); |
| 552 |
for (auto &impl_item_id : impl_item_ids) |
| 553 |
{ |
| 554 |
mappings.insert_impl_item_mapping (impl_item_id, hir_impl_block); |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
void |
| 559 |
ASTLoweringItem::visit (AST::Trait &trait) |
| 560 |
{ |
| 561 |
std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; |
| 562 |
for (auto &item : trait.get_where_clause ().get_items ()) |
| 563 |
{ |
| 564 |
HIR::WhereClauseItem *i |
| 565 |
= ASTLowerWhereClauseItem::translate (*item.get ()); |
| 566 |
where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); |
| 567 |
} |
| 568 |
HIR::WhereClause where_clause (std::move (where_clause_items)); |
| 569 |
|
| 570 |
HIR::Visibility vis = translate_visibility (trait.get_visibility ()); |
| 571 |
|
| 572 |
std::vector<std::unique_ptr<HIR::GenericParam>> generic_params; |
| 573 |
if (trait.has_generics ()) |
| 574 |
{ |
| 575 |
generic_params = lower_generic_params (trait.get_generic_params ()); |
| 576 |
} |
| 577 |
|
| 578 |
// TODO: separate "Self" from normal generic parameters |
| 579 |
// in HIR as well as in AST? |
| 580 |
HIR::GenericParam *self_param |
| 581 |
= ASTLowerGenericParam::translate (trait.get_implicit_self ()); |
| 582 |
generic_params.emplace (generic_params.begin (), self_param); |
| 583 |
|
| 584 |
std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds; |
| 585 |
if (trait.has_type_param_bounds ()) |
| 586 |
{ |
| 587 |
for (auto &bound : trait.get_type_param_bounds ()) |
| 588 |
{ |
| 589 |
HIR::TypeParamBound *b = lower_bound (*bound); |
| 590 |
type_param_bounds.push_back ( |
| 591 |
std::unique_ptr<HIR::TypeParamBound> (b)); |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
std::vector<std::unique_ptr<HIR::TraitItem>> trait_items; |
| 596 |
std::vector<HirId> trait_item_ids; |
| 597 |
for (auto &item : trait.get_trait_items ()) |
| 598 |
{ |
| 599 |
if (item->is_marked_for_strip ()) |
| 600 |
continue; |
| 601 |
|
| 602 |
HIR::TraitItem *lowered = ASTLowerTraitItem::translate (*item); |
| 603 |
trait_items.push_back (std::unique_ptr<HIR::TraitItem> (lowered)); |
| 604 |
trait_item_ids.push_back (lowered->get_mappings ().get_hirid ()); |
| 605 |
} |
| 606 |
|
| 607 |
auto crate_num = mappings.get_current_crate (); |
| 608 |
Analysis::NodeMapping mapping (crate_num, trait.get_node_id (), |
| 609 |
mappings.get_next_hir_id (crate_num), |
| 610 |
mappings.get_next_localdef_id (crate_num)); |
| 611 |
|
| 612 |
auto trait_unsafety |
| 613 |
= trait.is_unsafe () ? Unsafety::Unsafe : Unsafety::Normal; |
| 614 |
|
| 615 |
HIR::Trait *hir_trait |
| 616 |
= new HIR::Trait (mapping, trait.get_identifier (), trait_unsafety, |
| 617 |
std::move (generic_params), std::move (type_param_bounds), |
| 618 |
where_clause, std::move (trait_items), vis, |
| 619 |
trait.get_outer_attrs (), trait.get_locus ()); |
| 620 |
|
| 621 |
if (trait.is_auto ()) |
| 622 |
mappings.insert_auto_trait (hir_trait); |
| 623 |
|
| 624 |
translated = hir_trait; |
| 625 |
|
| 626 |
for (auto trait_item_id : trait_item_ids) |
| 627 |
{ |
| 628 |
mappings.insert_trait_item_mapping (trait_item_id, hir_trait); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
void |
| 633 |
ASTLoweringItem::visit (AST::TraitImpl &impl_block) |
| 634 |
{ |
| 635 |
std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items; |
| 636 |
bool unsafe = impl_block.is_unsafe (); |
| 637 |
for (auto &item : impl_block.get_where_clause ().get_items ()) |
| 638 |
{ |
| 639 |
HIR::WhereClauseItem *i = ASTLowerWhereClauseItem::translate (*item); |
| 640 |
where_clause_items.push_back (std::unique_ptr<HIR::WhereClauseItem> (i)); |
| 641 |
} |
| 642 |
HIR::WhereClause where_clause (std::move (where_clause_items)); |
| 643 |
HIR::Visibility vis = translate_visibility (impl_block.get_visibility ()); |
| 644 |
|
| 645 |
std::vector<std::unique_ptr<HIR::GenericParam>> generic_params; |
| 646 |
if (impl_block.has_generics ()) |
| 647 |
{ |
| 648 |
generic_params = lower_generic_params (impl_block.get_generic_params ()); |
| 649 |
|
| 650 |
for (auto &generic_param : generic_params) |
| 651 |
{ |
| 652 |
switch (generic_param->get_kind ()) |
| 653 |
{ |
| 654 |
case HIR::GenericParam::GenericKind::TYPE: { |
| 655 |
const HIR::TypeParam &t |
| 656 |
= static_cast<const HIR::TypeParam &> (*generic_param); |
| 657 |
|
| 658 |
if (t.has_type ()) |
| 659 |
{ |
| 660 |
rich_location rich_locus (line_table, t.get_locus ()); |
| 661 |
rich_locus.add_fixit_replace ( |
| 662 |
t.get_locus (), "for more information, see issue #36887 " |
| 663 |
"<https://github.com/rust-lang/rust/" |
| 664 |
"issues/36887>"); |
| 665 |
rust_error_at (rich_locus, |
| 666 |
"defaults for type parameters are only " |
| 667 |
"allowed in %<struct%>, %<enum%>, %<type%>, " |
| 668 |
"or %<trait%> definitions"); |
| 669 |
} |
| 670 |
} |
| 671 |
break; |
| 672 |
|
| 673 |
default: |
| 674 |
break; |
| 675 |
} |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
HIR::Type *impl_type = ASTLoweringType::translate (impl_block.get_type ()); |
| 680 |
HIR::TypePath *trait_ref |
| 681 |
= ASTLowerTypePath::translate (impl_block.get_trait_path ()); |
| 682 |
|
| 683 |
auto crate_num = mappings.get_current_crate (); |
| 684 |
Analysis::NodeMapping mapping (crate_num, impl_block.get_node_id (), |
| 685 |
mappings.get_next_hir_id (crate_num), |
| 686 |
mappings.get_next_localdef_id (crate_num)); |
| 687 |
|
| 688 |
std::vector<std::unique_ptr<HIR::ImplItem>> impl_items; |
| 689 |
std::vector<HirId> impl_item_ids; |
| 690 |
for (auto &impl_item : impl_block.get_impl_items ()) |
| 691 |
{ |
| 692 |
if (impl_item->is_marked_for_strip ()) |
| 693 |
continue; |
| 694 |
|
| 695 |
HIR::ImplItem *lowered |
| 696 |
= ASTLowerImplItem::translate (*impl_item, mapping.get_hirid ()); |
| 697 |
rust_assert (lowered != nullptr); |
| 698 |
impl_items.push_back (std::unique_ptr<HIR::ImplItem> (lowered)); |
| 699 |
impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ()); |
| 700 |
} |
| 701 |
|
| 702 |
BoundPolarity polarity = impl_block.is_exclam () |
| 703 |
? BoundPolarity::NegativeBound |
| 704 |
: BoundPolarity::RegularBound; |
| 705 |
HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock ( |
| 706 |
mapping, std::move (impl_items), std::move (generic_params), |
| 707 |
std::unique_ptr<HIR::Type> (impl_type), |
| 708 |
std::unique_ptr<HIR::TypePath> (trait_ref), where_clause, polarity, vis, |
| 709 |
impl_block.get_inner_attrs (), impl_block.get_outer_attrs (), |
| 710 |
impl_block.get_locus (), unsafe); |
| 711 |
translated = hir_impl_block; |
| 712 |
|
| 713 |
mappings.insert_hir_impl_block (hir_impl_block); |
| 714 |
for (auto &impl_item_id : impl_item_ids) |
| 715 |
{ |
| 716 |
mappings.insert_impl_item_mapping (impl_item_id, hir_impl_block); |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
void |
| 721 |
ASTLoweringItem::visit (AST::ExternBlock &extern_block) |
| 722 |
{ |
| 723 |
translated = lower_extern_block (extern_block); |
| 724 |
} |
| 725 |
|
| 726 |
void |
| 727 |
ASTLoweringItem::visit (AST::MacroRulesDefinition &def) |
| 728 |
{ |
| 729 |
lower_macro_definition (def); |
| 730 |
} |
| 731 |
|
| 732 |
HIR::SimplePath |
| 733 |
ASTLoweringSimplePath::translate (const AST::SimplePath &path) |
| 734 |
{ |
| 735 |
ASTLoweringSimplePath resolver; |
| 736 |
|
| 737 |
return resolver.lower (path); |
| 738 |
} |
| 739 |
|
| 740 |
HIR::SimplePathSegment |
| 741 |
ASTLoweringSimplePath::lower (const AST::SimplePathSegment &segment) |
| 742 |
{ |
| 743 |
auto crate_num = mappings.get_current_crate (); |
| 744 |
auto node_id = segment.get_node_id (); |
| 745 |
|
| 746 |
auto mapping = Analysis::NodeMapping (crate_num, node_id, |
| 747 |
mappings.get_next_hir_id (crate_num), |
| 748 |
UNKNOWN_LOCAL_DEFID); |
| 749 |
|
| 750 |
auto hir_seg = HIR::SimplePathSegment (mapping); |
| 751 |
|
| 752 |
mappings.insert_node_to_hir (node_id, mapping.get_hirid ()); |
| 753 |
// mappings.insert_simple_path_segment (crate_num, node_id, &segment); |
| 754 |
|
| 755 |
return hir_seg; |
| 756 |
} |
| 757 |
|
| 758 |
HIR::SimplePath |
| 759 |
ASTLoweringSimplePath::lower (const AST::SimplePath &path) |
| 760 |
{ |
| 761 |
auto segments = std::vector<HIR::SimplePathSegment> (); |
| 762 |
for (auto &segment : path.get_segments ()) |
| 763 |
segments.emplace_back (lower (segment)); |
| 764 |
|
| 765 |
auto crate_num = mappings.get_current_crate (); |
| 766 |
auto node_id = path.get_node_id (); |
| 767 |
|
| 768 |
auto mapping = Analysis::NodeMapping (crate_num, node_id, |
| 769 |
mappings.get_next_hir_id (crate_num), |
| 770 |
UNKNOWN_LOCAL_DEFID); |
| 771 |
|
| 772 |
auto lowered |
| 773 |
= HIR::SimplePath (std::move (segments), mapping, path.get_locus ()); |
| 774 |
|
| 775 |
mappings.insert_node_to_hir (node_id, mapping.get_hirid ()); |
| 776 |
// mappings.insert_simple_path (crate_num, node_id, &path); |
| 777 |
|
| 778 |
return lowered; |
| 779 |
} |
| 780 |
|
| 781 |
} // namespace HIR |
| 782 |
} // namespace Rust |