1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #ifndef RUST_AST_LOWER_EXTERN_ITEM
20 #define RUST_AST_LOWER_EXTERN_ITEM
22 #include "rust-ast-lower-base.h"
23 #include "rust-ast-lower-type.h"
24 #include "rust-ast-lower.h"
29 class ASTLoweringExternItem
: public ASTLoweringBase
31 using Rust::HIR::ASTLoweringBase::visit
;
34 static HIR::ExternalItem
*translate (AST::ExternalItem
*item
,
37 ASTLoweringExternItem resolver
;
38 item
->accept_vis (resolver
);
40 rust_assert (resolver
.translated
!= nullptr);
41 resolver
.mappings
->insert_hir_extern_item (resolver
.translated
,
43 resolver
.mappings
->insert_location (
44 resolver
.translated
->get_mappings ().get_hirid (),
45 resolver
.translated
->get_locus ());
47 return resolver
.translated
;
50 void visit (AST::ExternalStaticItem
&item
) override
52 HIR::Visibility vis
= translate_visibility (item
.get_visibility ());
53 HIR::Type
*static_type
54 = ASTLoweringType::translate (item
.get_type ().get ());
56 auto crate_num
= mappings
->get_current_crate ();
57 Analysis::NodeMapping
mapping (crate_num
, item
.get_node_id (),
58 mappings
->get_next_hir_id (crate_num
),
59 mappings
->get_next_localdef_id (crate_num
));
61 translated
= new HIR::ExternalStaticItem (
62 mapping
, item
.get_identifier (), std::unique_ptr
<HIR::Type
> (static_type
),
63 item
.is_mut () ? Mutability::Mut
: Mutability::Imm
, std::move (vis
),
64 item
.get_outer_attrs (), item
.get_locus ());
67 void visit (AST::ExternalFunctionItem
&function
) override
69 std::vector
<std::unique_ptr
<HIR::WhereClauseItem
> > where_clause_items
;
70 HIR::WhereClause
where_clause (std::move (where_clause_items
));
71 HIR::Visibility vis
= translate_visibility (function
.get_visibility ());
73 std::vector
<std::unique_ptr
<HIR::GenericParam
> > generic_params
;
74 if (function
.has_generics ())
75 generic_params
= lower_generic_params (function
.get_generic_params ());
77 HIR::Type
*return_type
78 = function
.has_return_type ()
79 ? ASTLoweringType::translate (function
.get_return_type ().get ())
82 std::vector
<HIR::NamedFunctionParam
> function_params
;
83 for (auto ¶m
: function
.get_function_params ())
86 = ASTLoweringType::translate (param
.get_type ().get ());
87 Identifier param_name
= param
.get_name ();
89 auto crate_num
= mappings
->get_current_crate ();
90 Analysis::NodeMapping
mapping (crate_num
, param
.get_node_id (),
91 mappings
->get_next_hir_id (crate_num
),
92 mappings
->get_next_localdef_id (
95 function_params
.push_back (
96 HIR::NamedFunctionParam (mapping
, param_name
,
97 std::unique_ptr
<HIR::Type
> (param_type
)));
100 auto crate_num
= mappings
->get_current_crate ();
101 Analysis::NodeMapping
mapping (crate_num
, function
.get_node_id (),
102 mappings
->get_next_hir_id (crate_num
),
103 mappings
->get_next_localdef_id (crate_num
));
105 translated
= new HIR::ExternalFunctionItem (
106 mapping
, function
.get_identifier (), std::move (generic_params
),
107 std::unique_ptr
<HIR::Type
> (return_type
), std::move (where_clause
),
108 std::move (function_params
), function
.is_variadic (), std::move (vis
),
109 function
.get_outer_attrs (), function
.get_locus ());
113 ASTLoweringExternItem () : translated (nullptr) {}
115 HIR::ExternalItem
*translated
;
121 #endif // RUST_AST_LOWER_ITEM