Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 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 : : #ifndef RUST_AST_LOWER_EXTERN_ITEM
20 : : #define RUST_AST_LOWER_EXTERN_ITEM
21 : :
22 : : #include "rust-ast-lower-base.h"
23 : : #include "rust-ast-lower-type.h"
24 : : #include "rust-ast-lower.h"
25 : :
26 : : namespace Rust {
27 : : namespace HIR {
28 : :
29 : 1592 : class ASTLoweringExternItem : public ASTLoweringBase
30 : : {
31 : : using Rust::HIR::ASTLoweringBase::visit;
32 : :
33 : : public:
34 : 1592 : static HIR::ExternalItem *translate (AST::ExternalItem *item,
35 : : HirId parent_hirid)
36 : : {
37 : 3184 : ASTLoweringExternItem resolver;
38 : 1592 : item->accept_vis (resolver);
39 : :
40 : 1592 : rust_assert (resolver.translated != nullptr);
41 : 1592 : resolver.mappings->insert_hir_extern_item (resolver.translated,
42 : : parent_hirid);
43 : 1592 : resolver.mappings->insert_location (
44 : 1592 : resolver.translated->get_mappings ().get_hirid (),
45 : 1592 : resolver.translated->get_locus ());
46 : :
47 : 1592 : return resolver.translated;
48 : 1592 : }
49 : :
50 : 1 : void visit (AST::ExternalStaticItem &item) override
51 : : {
52 : 1 : HIR::Visibility vis = translate_visibility (item.get_visibility ());
53 : 1 : HIR::Type *static_type
54 : 1 : = ASTLoweringType::translate (item.get_type ().get ());
55 : :
56 : 1 : auto crate_num = mappings->get_current_crate ();
57 : 1 : Analysis::NodeMapping mapping (crate_num, item.get_node_id (),
58 : 1 : mappings->get_next_hir_id (crate_num),
59 : 1 : mappings->get_next_localdef_id (crate_num));
60 : :
61 : 1 : translated = new HIR::ExternalStaticItem (
62 : 2 : mapping, item.get_identifier (), std::unique_ptr<HIR::Type> (static_type),
63 : 1 : item.is_mut () ? Mutability::Mut : Mutability::Imm, std::move (vis),
64 : 3 : item.get_outer_attrs (), item.get_locus ());
65 : 1 : }
66 : :
67 : 1591 : void visit (AST::ExternalFunctionItem &function) override
68 : : {
69 : 1591 : std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
70 : 1591 : HIR::WhereClause where_clause (std::move (where_clause_items));
71 : 1591 : HIR::Visibility vis = translate_visibility (function.get_visibility ());
72 : :
73 : 1591 : std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
74 : 1591 : if (function.has_generics ())
75 : 508 : generic_params = lower_generic_params (function.get_generic_params ());
76 : :
77 : 1591 : HIR::Type *return_type
78 : 1591 : = function.has_return_type ()
79 : 1591 : ? ASTLoweringType::translate (function.get_return_type ().get ())
80 : 1591 : : nullptr;
81 : :
82 : 1591 : bool is_variadic = function.is_variadic ();
83 : 1591 : auto begin = function.get_function_params ().begin ();
84 : 1591 : auto end = is_variadic ? function.get_function_params ().end () - 1
85 : : : function.get_function_params ().end ();
86 : :
87 : 1591 : std::vector<HIR::NamedFunctionParam> function_params;
88 : 3526 : for (auto it = begin; it != end; it++)
89 : : {
90 : 1935 : HIR::Type *param_type
91 : 1935 : = ASTLoweringType::translate (it->get_type ().get ());
92 : 1935 : Identifier param_name = it->get_name ();
93 : :
94 : 1935 : auto crate_num = mappings->get_current_crate ();
95 : 1935 : Analysis::NodeMapping mapping (crate_num, it->get_node_id (),
96 : 1935 : mappings->get_next_hir_id (crate_num),
97 : 1935 : mappings->get_next_localdef_id (
98 : 1935 : crate_num));
99 : :
100 : 3870 : function_params.push_back (
101 : 3870 : HIR::NamedFunctionParam (mapping, param_name,
102 : 3870 : std::unique_ptr<HIR::Type> (param_type)));
103 : 1935 : }
104 : :
105 : 1591 : auto crate_num = mappings->get_current_crate ();
106 : 1591 : Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
107 : 1591 : mappings->get_next_hir_id (crate_num),
108 : 1591 : mappings->get_next_localdef_id (crate_num));
109 : :
110 : 1591 : translated = new HIR::ExternalFunctionItem (
111 : 3182 : mapping, function.get_identifier (), std::move (generic_params),
112 : 3182 : std::unique_ptr<HIR::Type> (return_type), std::move (where_clause),
113 : : std::move (function_params), is_variadic, std::move (vis),
114 : 4773 : function.get_outer_attrs (), function.get_locus ());
115 : 1591 : }
116 : :
117 : : private:
118 : 1592 : ASTLoweringExternItem () : translated (nullptr) {}
119 : :
120 : : HIR::ExternalItem *translated;
121 : : };
122 : :
123 : : } // namespace HIR
124 : : } // namespace Rust
125 : :
126 : : #endif // RUST_AST_LOWER_ITEM
|