| 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-type.h" |
| 20 |
#include "rust-hir-map.h" |
| 21 |
#include "rust-hir-path.h" |
| 22 |
#include "rust-hir-type.h" |
| 23 |
#include "rust-path.h" |
| 24 |
#include "rust-pattern.h" |
| 25 |
|
| 26 |
namespace Rust { |
| 27 |
namespace HIR { |
| 28 |
|
| 29 |
HIR::TypePath * |
| 30 |
ASTLowerTypePath::translate (AST::TypePath &type) |
| 31 |
{ |
| 32 |
ASTLowerTypePath resolver; |
| 33 |
type.accept_vis (resolver); |
| 34 |
rust_assert (resolver.translated != nullptr); |
| 35 |
return resolver.translated; |
| 36 |
} |
| 37 |
|
| 38 |
void |
| 39 |
ASTLowerTypePath::visit (AST::TypePathSegmentFunction &segment) |
| 40 |
{ |
| 41 |
auto crate_num = mappings.get_current_crate (); |
| 42 |
auto hirid = mappings.get_next_hir_id (crate_num); |
| 43 |
Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid, |
| 44 |
UNKNOWN_LOCAL_DEFID); |
| 45 |
|
| 46 |
HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ()); |
| 47 |
|
| 48 |
AST::TypePathFunction &fn = segment.get_type_path_function (); |
| 49 |
std::vector<std::unique_ptr<HIR::Type>> inputs; |
| 50 |
for (auto ¶m : fn.get_params ()) |
| 51 |
{ |
| 52 |
HIR::Type *hir_type = ASTLoweringType::translate (*param); |
| 53 |
inputs.push_back (std::unique_ptr<HIR::Type> (hir_type)); |
| 54 |
} |
| 55 |
|
| 56 |
HIR::Type *result_type |
| 57 |
= fn.has_return_type () ? ASTLoweringType::translate (fn.get_return_type ()) |
| 58 |
: nullptr; |
| 59 |
|
| 60 |
HIR::TypePathFunction function_path (std::move (inputs), |
| 61 |
std::unique_ptr<HIR::Type> ( |
| 62 |
result_type)); |
| 63 |
|
| 64 |
translated_segment = new HIR::TypePathSegmentFunction ( |
| 65 |
mapping, std::move (ident), segment.get_separating_scope_resolution (), |
| 66 |
std::move (function_path), segment.get_locus ()); |
| 67 |
} |
| 68 |
|
| 69 |
void |
| 70 |
ASTLowerTypePath::visit (AST::TypePathSegment &segment) |
| 71 |
{ |
| 72 |
auto crate_num = mappings.get_current_crate (); |
| 73 |
auto hirid = mappings.get_next_hir_id (crate_num); |
| 74 |
Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid, |
| 75 |
UNKNOWN_LOCAL_DEFID); |
| 76 |
|
| 77 |
if (segment.is_lang_item ()) |
| 78 |
{ |
| 79 |
translated_segment = new HIR::TypePathSegment (std::move (mapping), |
| 80 |
segment.get_lang_item (), |
| 81 |
segment.get_locus ()); |
| 82 |
} |
| 83 |
else |
| 84 |
{ |
| 85 |
HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ()); |
| 86 |
translated_segment |
| 87 |
= new HIR::TypePathSegment (std::move (mapping), ident, |
| 88 |
segment.get_separating_scope_resolution (), |
| 89 |
segment.get_locus ()); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
void |
| 94 |
ASTLowerTypePath::visit (AST::TypePathSegmentGeneric &segment) |
| 95 |
{ |
| 96 |
std::vector<HIR::GenericArgsBinding> binding_args; // TODO |
| 97 |
|
| 98 |
auto generic_args = lower_generic_args (segment.get_generic_args ()); |
| 99 |
|
| 100 |
auto crate_num = mappings.get_current_crate (); |
| 101 |
auto hirid = mappings.get_next_hir_id (crate_num); |
| 102 |
Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid, |
| 103 |
UNKNOWN_LOCAL_DEFID); |
| 104 |
|
| 105 |
if (segment.is_lang_item ()) |
| 106 |
{ |
| 107 |
translated_segment |
| 108 |
= new HIR::TypePathSegmentGeneric (std::move (mapping), |
| 109 |
segment.get_lang_item (), |
| 110 |
generic_args, segment.get_locus ()); |
| 111 |
} |
| 112 |
else |
| 113 |
{ |
| 114 |
std::string segment_name = segment.get_ident_segment ().as_string (); |
| 115 |
bool has_separating_scope_resolution |
| 116 |
= segment.get_separating_scope_resolution (); |
| 117 |
|
| 118 |
translated_segment |
| 119 |
= new HIR::TypePathSegmentGeneric (std::move (mapping), segment_name, |
| 120 |
has_separating_scope_resolution, |
| 121 |
generic_args, segment.get_locus ()); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void |
| 126 |
ASTLowerTypePath::visit (AST::TypePath &path) |
| 127 |
{ |
| 128 |
std::vector<std::unique_ptr<HIR::TypePathSegment>> translated_segments; |
| 129 |
|
| 130 |
for (auto &seg : path.get_segments ()) |
| 131 |
{ |
| 132 |
translated_segment = nullptr; |
| 133 |
seg->accept_vis (*this); |
| 134 |
rust_assert (translated_segment != nullptr); |
| 135 |
|
| 136 |
translated_segments.push_back ( |
| 137 |
std::unique_ptr<HIR::TypePathSegment> (translated_segment)); |
| 138 |
} |
| 139 |
|
| 140 |
auto crate_num = mappings.get_current_crate (); |
| 141 |
auto hirid = mappings.get_next_hir_id (crate_num); |
| 142 |
Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid, |
| 143 |
mappings.get_next_localdef_id (crate_num)); |
| 144 |
|
| 145 |
translated |
| 146 |
= new HIR::TypePath (std::move (mapping), std::move (translated_segments), |
| 147 |
path.get_locus (), |
| 148 |
path.has_opening_scope_resolution_op ()); |
| 149 |
} |
| 150 |
|
| 151 |
HIR::QualifiedPathInType * |
| 152 |
ASTLowerQualifiedPathInType::translate (AST::QualifiedPathInType &type) |
| 153 |
{ |
| 154 |
ASTLowerQualifiedPathInType resolver; |
| 155 |
type.accept_vis (resolver); |
| 156 |
rust_assert (resolver.translated != nullptr); |
| 157 |
return resolver.translated; |
| 158 |
} |
| 159 |
|
| 160 |
void |
| 161 |
ASTLowerQualifiedPathInType::visit (AST::QualifiedPathInType &path) |
| 162 |
{ |
| 163 |
auto crate_num = mappings.get_current_crate (); |
| 164 |
auto hirid = mappings.get_next_hir_id (crate_num); |
| 165 |
Analysis::NodeMapping qual_mappings ( |
| 166 |
crate_num, path.get_qualified_path_type ().get_node_id (), hirid, |
| 167 |
UNKNOWN_LOCAL_DEFID); |
| 168 |
|
| 169 |
HIR::Type *qual_type |
| 170 |
= ASTLoweringType::translate (path.get_qualified_path_type ().get_type ()); |
| 171 |
|
| 172 |
HIR::TypePath *qual_trait = nullptr; |
| 173 |
if (!path.get_qualified_path_type ().is_error ()) |
| 174 |
{ |
| 175 |
AST::QualifiedPathType &qualifier = path.get_qualified_path_type (); |
| 176 |
if (qualifier.has_as_clause ()) |
| 177 |
qual_trait |
| 178 |
= ASTLowerTypePath::translate (qualifier.get_as_type_path ()); |
| 179 |
} |
| 180 |
|
| 181 |
HIR::QualifiedPathType qual_path_type ( |
| 182 |
qual_mappings, std::unique_ptr<HIR::Type> (qual_type), |
| 183 |
std::unique_ptr<HIR::TypePath> (qual_trait), |
| 184 |
path.get_qualified_path_type ().get_locus ()); |
| 185 |
|
| 186 |
translated_segment = nullptr; |
| 187 |
path.get_associated_segment ()->accept_vis (*this); |
| 188 |
rust_assert (translated_segment != nullptr); |
| 189 |
|
| 190 |
std::unique_ptr<HIR::TypePathSegment> associated_segment (translated_segment); |
| 191 |
|
| 192 |
std::vector<std::unique_ptr<HIR::TypePathSegment>> translated_segments; |
| 193 |
for (auto &seg : path.get_segments ()) |
| 194 |
{ |
| 195 |
translated_segment = nullptr; |
| 196 |
seg->accept_vis (*this); |
| 197 |
rust_assert (translated_segment != nullptr); |
| 198 |
|
| 199 |
translated_segments.push_back ( |
| 200 |
std::unique_ptr<HIR::TypePathSegment> (translated_segment)); |
| 201 |
} |
| 202 |
|
| 203 |
Analysis::NodeMapping mapping (crate_num, path.get_node_id (), hirid, |
| 204 |
mappings.get_next_localdef_id (crate_num)); |
| 205 |
translated = new HIR::QualifiedPathInType (std::move (mapping), |
| 206 |
std::move (qual_path_type), |
| 207 |
std::move (associated_segment), |
| 208 |
std::move (translated_segments), |
| 209 |
path.get_locus ()); |
| 210 |
} |
| 211 |
|
| 212 |
HIR::Type * |
| 213 |
ASTLoweringType::translate (AST::Type &type, bool default_to_static_lifetime) |
| 214 |
{ |
| 215 |
ASTLoweringType resolver (default_to_static_lifetime); |
| 216 |
type.accept_vis (resolver); |
| 217 |
|
| 218 |
rust_assert (resolver.translated != nullptr); |
| 219 |
resolver.mappings.insert_hir_type (resolver.translated); |
| 220 |
resolver.mappings.insert_location ( |
| 221 |
resolver.translated->get_mappings ().get_hirid (), |
| 222 |
resolver.translated->get_locus ()); |
| 223 |
|
| 224 |
return resolver.translated; |
| 225 |
} |
| 226 |
|
| 227 |
void |
| 228 |
ASTLoweringType::visit (AST::BareFunctionType &fntype) |
| 229 |
{ |
| 230 |
bool is_variadic = false; |
| 231 |
std::vector<HIR::LifetimeParam> lifetime_params; |
| 232 |
for (auto &lifetime_param : fntype.get_for_lifetimes ()) |
| 233 |
{ |
| 234 |
auto generic_param = ASTLowerGenericParam::translate (lifetime_param); |
| 235 |
lifetime_params.push_back ( |
| 236 |
*static_cast<HIR::LifetimeParam *> (generic_param)); |
| 237 |
} |
| 238 |
|
| 239 |
HIR::FunctionQualifiers qualifiers |
| 240 |
= lower_qualifiers (fntype.get_function_qualifiers ()); |
| 241 |
|
| 242 |
std::vector<HIR::MaybeNamedParam> named_params; |
| 243 |
for (auto ¶m : fntype.get_function_params ()) |
| 244 |
{ |
| 245 |
HIR::MaybeNamedParam::ParamKind kind; |
| 246 |
switch (param.get_param_kind ()) |
| 247 |
{ |
| 248 |
case AST::MaybeNamedParam::ParamKind::UNNAMED: |
| 249 |
kind = HIR::MaybeNamedParam::ParamKind::UNNAMED; |
| 250 |
break; |
| 251 |
case AST::MaybeNamedParam::ParamKind::IDENTIFIER: |
| 252 |
kind = HIR::MaybeNamedParam::ParamKind::IDENTIFIER; |
| 253 |
break; |
| 254 |
case AST::MaybeNamedParam::ParamKind::WILDCARD: |
| 255 |
kind = HIR::MaybeNamedParam::ParamKind::WILDCARD; |
| 256 |
break; |
| 257 |
default: |
| 258 |
rust_unreachable (); |
| 259 |
} |
| 260 |
|
| 261 |
HIR::Type *param_type |
| 262 |
= ASTLoweringType::translate (param.get_type (), |
| 263 |
default_to_static_lifetime); |
| 264 |
|
| 265 |
HIR::MaybeNamedParam p (param.get_name (), kind, |
| 266 |
std::unique_ptr<HIR::Type> (param_type), |
| 267 |
param.get_locus ()); |
| 268 |
named_params.push_back (std::move (p)); |
| 269 |
} |
| 270 |
|
| 271 |
HIR::Type *return_type = nullptr; |
| 272 |
if (fntype.has_return_type ()) |
| 273 |
{ |
| 274 |
return_type = ASTLoweringType::translate (fntype.get_return_type (), |
| 275 |
default_to_static_lifetime); |
| 276 |
} |
| 277 |
|
| 278 |
auto crate_num = mappings.get_current_crate (); |
| 279 |
Analysis::NodeMapping mapping (crate_num, fntype.get_node_id (), |
| 280 |
mappings.get_next_hir_id (crate_num), |
| 281 |
mappings.get_next_localdef_id (crate_num)); |
| 282 |
|
| 283 |
translated = new HIR::BareFunctionType ( |
| 284 |
std::move (mapping), std::move (lifetime_params), std::move (qualifiers), |
| 285 |
std::move (named_params), is_variadic, |
| 286 |
std::unique_ptr<HIR::Type> (return_type), fntype.get_locus ()); |
| 287 |
} |
| 288 |
|
| 289 |
void |
| 290 |
ASTLoweringType::visit (AST::TupleType &tuple) |
| 291 |
{ |
| 292 |
std::vector<std::unique_ptr<HIR::Type>> elems; |
| 293 |
for (auto &e : tuple.get_elems ()) |
| 294 |
{ |
| 295 |
HIR::Type *t |
| 296 |
= ASTLoweringType::translate (*e, default_to_static_lifetime); |
| 297 |
elems.push_back (std::unique_ptr<HIR::Type> (t)); |
| 298 |
} |
| 299 |
|
| 300 |
auto crate_num = mappings.get_current_crate (); |
| 301 |
Analysis::NodeMapping mapping (crate_num, tuple.get_node_id (), |
| 302 |
mappings.get_next_hir_id (crate_num), |
| 303 |
mappings.get_next_localdef_id (crate_num)); |
| 304 |
|
| 305 |
translated = new HIR::TupleType (std::move (mapping), std::move (elems), |
| 306 |
tuple.get_locus ()); |
| 307 |
} |
| 308 |
|
| 309 |
void |
| 310 |
ASTLoweringType::visit (AST::TypePath &path) |
| 311 |
{ |
| 312 |
translated = ASTLowerTypePath::translate (path); |
| 313 |
} |
| 314 |
|
| 315 |
void |
| 316 |
ASTLoweringType::visit (AST::QualifiedPathInType &path) |
| 317 |
{ |
| 318 |
translated = ASTLowerQualifiedPathInType::translate (path); |
| 319 |
} |
| 320 |
|
| 321 |
void |
| 322 |
ASTLoweringType::visit (AST::ArrayType &type) |
| 323 |
{ |
| 324 |
HIR::Type *translated_type |
| 325 |
= ASTLoweringType::translate (type.get_elem_type (), |
| 326 |
default_to_static_lifetime); |
| 327 |
HIR::Expr *array_size = ASTLoweringExpr::translate (type.get_size_expr ()); |
| 328 |
|
| 329 |
auto crate_num = mappings.get_current_crate (); |
| 330 |
Analysis::NodeMapping mapping (crate_num, type.get_node_id (), |
| 331 |
mappings.get_next_hir_id (crate_num), |
| 332 |
mappings.get_next_localdef_id (crate_num)); |
| 333 |
|
| 334 |
translated |
| 335 |
= new HIR::ArrayType (mapping, std::unique_ptr<HIR::Type> (translated_type), |
| 336 |
std::unique_ptr<HIR::Expr> (array_size), |
| 337 |
type.get_locus ()); |
| 338 |
} |
| 339 |
|
| 340 |
void |
| 341 |
ASTLoweringType::visit (AST::ReferenceType &type) |
| 342 |
{ |
| 343 |
HIR::Lifetime lifetime |
| 344 |
= lower_lifetime (type.get_lifetime (), default_to_static_lifetime); |
| 345 |
|
| 346 |
HIR::Type *base_type |
| 347 |
= ASTLoweringType::translate (type.get_base_type (), |
| 348 |
default_to_static_lifetime); |
| 349 |
|
| 350 |
auto crate_num = mappings.get_current_crate (); |
| 351 |
Analysis::NodeMapping mapping (crate_num, type.get_node_id (), |
| 352 |
mappings.get_next_hir_id (crate_num), |
| 353 |
mappings.get_next_localdef_id (crate_num)); |
| 354 |
|
| 355 |
translated = new HIR::ReferenceType (mapping, |
| 356 |
type.get_has_mut () ? Mutability::Mut |
| 357 |
: Mutability::Imm, |
| 358 |
std::unique_ptr<HIR::Type> (base_type), |
| 359 |
type.get_locus (), lifetime); |
| 360 |
} |
| 361 |
|
| 362 |
void |
| 363 |
ASTLoweringType::visit (AST::RawPointerType &type) |
| 364 |
{ |
| 365 |
HIR::Type *base_type |
| 366 |
= ASTLoweringType::translate (type.get_type_pointed_to (), |
| 367 |
default_to_static_lifetime); |
| 368 |
|
| 369 |
auto crate_num = mappings.get_current_crate (); |
| 370 |
Analysis::NodeMapping mapping (crate_num, type.get_node_id (), |
| 371 |
mappings.get_next_hir_id (crate_num), |
| 372 |
mappings.get_next_localdef_id (crate_num)); |
| 373 |
|
| 374 |
translated |
| 375 |
= new HIR::RawPointerType (mapping, |
| 376 |
type.get_pointer_type () |
| 377 |
== AST::RawPointerType::PointerType::MUT |
| 378 |
? Mutability::Mut |
| 379 |
: Mutability::Imm, |
| 380 |
std::unique_ptr<HIR::Type> (base_type), |
| 381 |
type.get_locus ()); |
| 382 |
} |
| 383 |
|
| 384 |
void |
| 385 |
ASTLoweringType::visit (AST::SliceType &type) |
| 386 |
{ |
| 387 |
HIR::Type *base_type |
| 388 |
= ASTLoweringType::translate (type.get_elem_type (), |
| 389 |
default_to_static_lifetime); |
| 390 |
|
| 391 |
auto crate_num = mappings.get_current_crate (); |
| 392 |
Analysis::NodeMapping mapping (crate_num, type.get_node_id (), |
| 393 |
mappings.get_next_hir_id (crate_num), |
| 394 |
mappings.get_next_localdef_id (crate_num)); |
| 395 |
|
| 396 |
translated |
| 397 |
= new HIR::SliceType (mapping, std::unique_ptr<HIR::Type> (base_type), |
| 398 |
type.get_locus ()); |
| 399 |
} |
| 400 |
|
| 401 |
void |
| 402 |
ASTLoweringType::visit (AST::InferredType &type) |
| 403 |
{ |
| 404 |
auto crate_num = mappings.get_current_crate (); |
| 405 |
Analysis::NodeMapping mapping (crate_num, type.get_node_id (), |
| 406 |
mappings.get_next_hir_id (crate_num), |
| 407 |
mappings.get_next_localdef_id (crate_num)); |
| 408 |
|
| 409 |
translated = new HIR::InferredType (mapping, type.get_locus ()); |
| 410 |
} |
| 411 |
|
| 412 |
void |
| 413 |
ASTLoweringType::visit (AST::NeverType &type) |
| 414 |
{ |
| 415 |
auto crate_num = mappings.get_current_crate (); |
| 416 |
Analysis::NodeMapping mapping (crate_num, type.get_node_id (), |
| 417 |
mappings.get_next_hir_id (crate_num), |
| 418 |
mappings.get_next_localdef_id (crate_num)); |
| 419 |
|
| 420 |
translated = new HIR::NeverType (mapping, type.get_locus ()); |
| 421 |
} |
| 422 |
|
| 423 |
void |
| 424 |
ASTLoweringType::visit (AST::TraitObjectTypeOneBound &type) |
| 425 |
{ |
| 426 |
std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds; |
| 427 |
HIR::TypeParamBound *translated_bound |
| 428 |
= ASTLoweringTypeBounds::translate (type.get_trait_bound ()); |
| 429 |
bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (translated_bound)); |
| 430 |
|
| 431 |
auto crate_num = mappings.get_current_crate (); |
| 432 |
Analysis::NodeMapping mapping (crate_num, type.get_node_id (), |
| 433 |
mappings.get_next_hir_id (crate_num), |
| 434 |
mappings.get_next_localdef_id (crate_num)); |
| 435 |
|
| 436 |
translated = new HIR::TraitObjectType (mapping, std::move (bounds), |
| 437 |
type.get_locus (), type.is_dyn ()); |
| 438 |
} |
| 439 |
|
| 440 |
void |
| 441 |
ASTLoweringType::visit (AST::TraitObjectType &type) |
| 442 |
{ |
| 443 |
std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds; |
| 444 |
|
| 445 |
for (auto &bound : type.get_type_param_bounds ()) |
| 446 |
{ |
| 447 |
HIR::TypeParamBound *translated_bound |
| 448 |
= ASTLoweringTypeBounds::translate (*bound); |
| 449 |
bounds.push_back ( |
| 450 |
std::unique_ptr<HIR::TypeParamBound> (translated_bound)); |
| 451 |
} |
| 452 |
|
| 453 |
auto crate_num = mappings.get_current_crate (); |
| 454 |
Analysis::NodeMapping mapping (crate_num, type.get_node_id (), |
| 455 |
mappings.get_next_hir_id (crate_num), |
| 456 |
mappings.get_next_localdef_id (crate_num)); |
| 457 |
|
| 458 |
translated = new HIR::TraitObjectType (mapping, std::move (bounds), |
| 459 |
type.get_locus (), type.is_dyn ()); |
| 460 |
} |
| 461 |
|
| 462 |
void |
| 463 |
ASTLoweringType::visit (AST::ParenthesisedType &type) |
| 464 |
{ |
| 465 |
auto *inner = ASTLoweringType::translate (*type.get_type_in_parens (), |
| 466 |
default_to_static_lifetime); |
| 467 |
|
| 468 |
auto crate_num = mappings.get_current_crate (); |
| 469 |
Analysis::NodeMapping mapping (crate_num, type.get_node_id (), |
| 470 |
mappings.get_next_hir_id (crate_num), |
| 471 |
mappings.get_next_localdef_id (crate_num)); |
| 472 |
|
| 473 |
// FIXME: Do we actually need to know if a type is parenthesized in the HIR? |
| 474 |
// or can we just use the type in parens? |
| 475 |
translated |
| 476 |
= new HIR::ParenthesisedType (mapping, std::unique_ptr<HIR::Type> (inner), |
| 477 |
type.get_locus ()); |
| 478 |
} |
| 479 |
|
| 480 |
void |
| 481 |
ASTLoweringType::visit (AST::ImplTraitType &type) |
| 482 |
{ |
| 483 |
std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds; |
| 484 |
for (auto &bound : type.get_type_param_bounds ()) |
| 485 |
{ |
| 486 |
auto b = ASTLoweringTypeBounds::translate (*bound.get ()); |
| 487 |
bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (b)); |
| 488 |
} |
| 489 |
|
| 490 |
auto crate_num = mappings.get_current_crate (); |
| 491 |
Analysis::NodeMapping mapping (crate_num, type.get_node_id (), |
| 492 |
mappings.get_next_hir_id (crate_num), |
| 493 |
mappings.get_next_localdef_id (crate_num)); |
| 494 |
|
| 495 |
translated |
| 496 |
= new HIR::ImplTraitType (mapping, std::move (bounds), type.get_locus ()); |
| 497 |
} |
| 498 |
|
| 499 |
void |
| 500 |
ASTLoweringType::visit (AST::ImplTraitTypeOneBound &type) |
| 501 |
{ |
| 502 |
std::vector<std::unique_ptr<HIR::TypeParamBound>> bounds; |
| 503 |
|
| 504 |
auto b = ASTLoweringTypeBounds::translate (type.get_trait_bound ()); |
| 505 |
bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (b)); |
| 506 |
|
| 507 |
auto crate_num = mappings.get_current_crate (); |
| 508 |
Analysis::NodeMapping mapping (crate_num, type.get_node_id (), |
| 509 |
mappings.get_next_hir_id (crate_num), |
| 510 |
mappings.get_next_localdef_id (crate_num)); |
| 511 |
|
| 512 |
translated |
| 513 |
= new HIR::ImplTraitType (mapping, std::move (bounds), type.get_locus ()); |
| 514 |
} |
| 515 |
|
| 516 |
HIR::GenericParam * |
| 517 |
ASTLowerGenericParam::translate (AST::GenericParam ¶m) |
| 518 |
{ |
| 519 |
ASTLowerGenericParam resolver; |
| 520 |
param.accept_vis (resolver); |
| 521 |
|
| 522 |
rust_assert (resolver.translated != nullptr); |
| 523 |
resolver.mappings.insert_location ( |
| 524 |
resolver.translated->get_mappings ().get_hirid (), param.get_locus ()); |
| 525 |
resolver.mappings.insert_hir_generic_param (resolver.translated); |
| 526 |
|
| 527 |
return resolver.translated; |
| 528 |
} |
| 529 |
|
| 530 |
void |
| 531 |
ASTLowerGenericParam::visit (AST::LifetimeParam ¶m) |
| 532 |
{ |
| 533 |
auto crate_num = mappings.get_current_crate (); |
| 534 |
AST::Lifetime lifetime = param.get_lifetime (); |
| 535 |
Analysis::NodeMapping mapping (crate_num, param.get_node_id (), |
| 536 |
mappings.get_next_hir_id (crate_num), |
| 537 |
mappings.get_next_localdef_id (crate_num)); |
| 538 |
|
| 539 |
HIR::Lifetime lt (mapping, lifetime.get_lifetime_type (), |
| 540 |
lifetime.get_lifetime_name (), lifetime.get_locus ()); |
| 541 |
|
| 542 |
translated = new HIR::LifetimeParam (mapping, lt, param.get_locus (), |
| 543 |
std::vector<Lifetime> (), |
| 544 |
param.get_outer_attrs ()); |
| 545 |
} |
| 546 |
|
| 547 |
void |
| 548 |
ASTLowerGenericParam::visit (AST::ConstGenericParam ¶m) |
| 549 |
{ |
| 550 |
auto crate_num = mappings.get_current_crate (); |
| 551 |
Analysis::NodeMapping mapping (crate_num, param.get_node_id (), |
| 552 |
mappings.get_next_hir_id (crate_num), |
| 553 |
mappings.get_next_localdef_id (crate_num)); |
| 554 |
|
| 555 |
auto type = ASTLoweringType::translate (param.get_type ()); |
| 556 |
|
| 557 |
HIR::Expr *default_expr = nullptr; |
| 558 |
if (param.has_default_value ()) |
| 559 |
default_expr = ASTLoweringExpr::translate ( |
| 560 |
param.get_default_value ().get_expression ()); |
| 561 |
|
| 562 |
translated = new HIR::ConstGenericParam (param.get_name ().as_string (), |
| 563 |
std::unique_ptr<Type> (type), |
| 564 |
std::unique_ptr<Expr> (default_expr), |
| 565 |
mapping, param.get_locus ()); |
| 566 |
} |
| 567 |
|
| 568 |
void |
| 569 |
ASTLowerGenericParam::visit (AST::TypeParam ¶m) |
| 570 |
{ |
| 571 |
std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds; |
| 572 |
if (param.has_type_param_bounds ()) |
| 573 |
{ |
| 574 |
for (auto &bound : param.get_type_param_bounds ()) |
| 575 |
{ |
| 576 |
HIR::TypeParamBound *lowered_bound = lower_bound (*bound); |
| 577 |
type_param_bounds.push_back ( |
| 578 |
std::unique_ptr<HIR::TypeParamBound> (lowered_bound)); |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
tl::optional<std::unique_ptr<HIR::Type>> type = tl::nullopt; |
| 583 |
if (param.has_type ()) |
| 584 |
type |
| 585 |
= tl::optional<std::unique_ptr<HIR::Type>> (std::unique_ptr<HIR::Type> ( |
| 586 |
ASTLoweringType::translate (param.get_type ()))); |
| 587 |
|
| 588 |
auto crate_num = mappings.get_current_crate (); |
| 589 |
Analysis::NodeMapping mapping (crate_num, param.get_node_id (), |
| 590 |
mappings.get_next_hir_id (crate_num), |
| 591 |
mappings.get_next_localdef_id (crate_num)); |
| 592 |
|
| 593 |
translated |
| 594 |
= new HIR::TypeParam (mapping, param.get_type_representation (), |
| 595 |
param.get_locus (), std::move (type_param_bounds), |
| 596 |
std::move (type), param.get_outer_attrs ()); |
| 597 |
} |
| 598 |
|
| 599 |
HIR::TypeParamBound * |
| 600 |
ASTLoweringTypeBounds::translate (AST::TypeParamBound &type) |
| 601 |
{ |
| 602 |
ASTLoweringTypeBounds resolver; |
| 603 |
type.accept_vis (resolver); |
| 604 |
|
| 605 |
rust_assert (resolver.translated != nullptr); |
| 606 |
resolver.mappings.insert_location ( |
| 607 |
resolver.translated->get_mappings ().get_hirid (), |
| 608 |
resolver.translated->get_locus ()); |
| 609 |
|
| 610 |
return resolver.translated; |
| 611 |
} |
| 612 |
|
| 613 |
void |
| 614 |
ASTLoweringTypeBounds::visit (AST::TraitBound &bound) |
| 615 |
{ |
| 616 |
std::vector<HIR::LifetimeParam> for_lifetimes; |
| 617 |
for (auto &lifetime_param : bound.get_for_lifetimes ()) |
| 618 |
{ |
| 619 |
auto generic_param = ASTLowerGenericParam::translate (lifetime_param); |
| 620 |
for_lifetimes.push_back ( |
| 621 |
*static_cast<HIR::LifetimeParam *> (generic_param)); |
| 622 |
} |
| 623 |
|
| 624 |
AST::TypePath &ast_trait_path = bound.get_type_path (); |
| 625 |
HIR::TypePath *trait_path = ASTLowerTypePath::translate (ast_trait_path); |
| 626 |
|
| 627 |
auto crate_num = mappings.get_current_crate (); |
| 628 |
Analysis::NodeMapping mapping (crate_num, bound.get_node_id (), |
| 629 |
mappings.get_next_hir_id (crate_num), |
| 630 |
UNKNOWN_LOCAL_DEFID); |
| 631 |
|
| 632 |
BoundPolarity polarity = bound.has_opening_question_mark () |
| 633 |
? BoundPolarity::AntiBound |
| 634 |
: BoundPolarity::RegularBound; |
| 635 |
translated |
| 636 |
= new HIR::TraitBound (mapping, *trait_path, bound.get_locus (), |
| 637 |
bound.is_in_parens (), polarity, for_lifetimes); |
| 638 |
} |
| 639 |
|
| 640 |
void |
| 641 |
ASTLoweringTypeBounds::visit (AST::Lifetime &bound) |
| 642 |
{ |
| 643 |
HIR::Lifetime lifetime = lower_lifetime (bound); |
| 644 |
translated = new HIR::Lifetime (lifetime); |
| 645 |
} |
| 646 |
|
| 647 |
HIR::WhereClauseItem * |
| 648 |
ASTLowerWhereClauseItem::translate (AST::WhereClauseItem &item) |
| 649 |
{ |
| 650 |
ASTLowerWhereClauseItem compiler; |
| 651 |
item.accept_vis (compiler); |
| 652 |
|
| 653 |
rust_assert (compiler.translated != nullptr); |
| 654 |
// FIXME |
| 655 |
// compiler.mappings.insert_location ( |
| 656 |
// compiler.translated->get_mappings ().get_hirid (), |
| 657 |
// compiler.translated->get_locus ()); |
| 658 |
|
| 659 |
return compiler.translated; |
| 660 |
} |
| 661 |
|
| 662 |
void |
| 663 |
ASTLowerWhereClauseItem::visit (AST::LifetimeWhereClauseItem &item) |
| 664 |
{ |
| 665 |
HIR::Lifetime l = lower_lifetime (item.get_lifetime ()); |
| 666 |
std::vector<HIR::Lifetime> lifetime_bounds; |
| 667 |
for (auto &lifetime_bound : item.get_lifetime_bounds ()) |
| 668 |
{ |
| 669 |
HIR::Lifetime ll = lower_lifetime (lifetime_bound); |
| 670 |
lifetime_bounds.push_back (std::move (ll)); |
| 671 |
} |
| 672 |
|
| 673 |
auto crate_num = mappings.get_current_crate (); |
| 674 |
Analysis::NodeMapping mapping (crate_num, item.get_node_id (), |
| 675 |
mappings.get_next_hir_id (crate_num), |
| 676 |
UNKNOWN_LOCAL_DEFID); |
| 677 |
|
| 678 |
translated = new HIR::LifetimeWhereClauseItem (mapping, std::move (l), |
| 679 |
std::move (lifetime_bounds), |
| 680 |
item.get_locus ()); |
| 681 |
} |
| 682 |
|
| 683 |
void |
| 684 |
ASTLowerWhereClauseItem::visit (AST::TypeBoundWhereClauseItem &item) |
| 685 |
{ |
| 686 |
// FIXME |
| 687 |
std::vector<HIR::LifetimeParam> for_lifetimes; |
| 688 |
|
| 689 |
for (auto &lifetime_param : item.get_for_lifetimes ()) |
| 690 |
{ |
| 691 |
auto generic_param = ASTLowerGenericParam::translate (lifetime_param); |
| 692 |
for_lifetimes.push_back ( |
| 693 |
*static_cast<HIR::LifetimeParam *> (generic_param)); |
| 694 |
} |
| 695 |
|
| 696 |
std::unique_ptr<HIR::Type> bound_type = std::unique_ptr<HIR::Type> ( |
| 697 |
ASTLoweringType::translate (item.get_type ())); |
| 698 |
|
| 699 |
std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds; |
| 700 |
for (auto &bound : item.get_type_param_bounds ()) |
| 701 |
{ |
| 702 |
HIR::TypeParamBound *b = ASTLoweringTypeBounds::translate (*bound); |
| 703 |
type_param_bounds.push_back (std::unique_ptr<HIR::TypeParamBound> (b)); |
| 704 |
} |
| 705 |
|
| 706 |
auto crate_num = mappings.get_current_crate (); |
| 707 |
Analysis::NodeMapping mapping (crate_num, item.get_node_id (), |
| 708 |
mappings.get_next_hir_id (crate_num), |
| 709 |
UNKNOWN_LOCAL_DEFID); |
| 710 |
|
| 711 |
translated |
| 712 |
= new HIR::TypeBoundWhereClauseItem (mapping, std::move (for_lifetimes), |
| 713 |
std::move (bound_type), |
| 714 |
std::move (type_param_bounds), |
| 715 |
item.get_locus ()); |
| 716 |
} |
| 717 |
|
| 718 |
} // namespace HIR |
| 719 |
} // namespace Rust |