LCOV - code coverage report
Current view: top level - gcc/rust/hir - rust-ast-lower-type.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 98.2 % 435 427
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 35 35
Legend: Lines:     hit not hit

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

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.