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 : #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 : #include "rust-hir-full-decls.h"
26 :
27 : namespace Rust {
28 : namespace HIR {
29 :
30 2214 : class ASTLoweringExternItem : public ASTLoweringBase
31 : {
32 : using Rust::HIR::ASTLoweringBase::visit;
33 :
34 : public:
35 2214 : static HIR::ExternalItem *translate (AST::ExternalItem *item,
36 : HirId parent_hirid)
37 : {
38 4428 : ASTLoweringExternItem resolver;
39 2214 : item->accept_vis (resolver);
40 :
41 2214 : rust_assert (resolver.translated != nullptr);
42 2214 : resolver.mappings.insert_hir_extern_item (resolver.translated,
43 : parent_hirid);
44 2214 : resolver.mappings.insert_location (
45 2214 : resolver.translated->get_mappings ().get_hirid (),
46 2214 : resolver.translated->get_locus ());
47 :
48 2214 : return resolver.translated;
49 2214 : }
50 :
51 1 : void visit (AST::ExternalStaticItem &item) override
52 : {
53 1 : HIR::Visibility vis = translate_visibility (item.get_visibility ());
54 1 : HIR::Type *static_type = ASTLoweringType::translate (item.get_type ());
55 :
56 1 : auto crate_num = mappings.get_current_crate ();
57 2 : 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 2213 : void visit (AST::Function &function) override
68 : {
69 2213 : std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
70 2213 : HIR::WhereClause where_clause (std::move (where_clause_items));
71 2213 : HIR::Visibility vis = translate_visibility (function.get_visibility ());
72 :
73 2213 : std::vector<std::unique_ptr<HIR::GenericParam> > generic_params;
74 2213 : if (function.has_generics ())
75 816 : generic_params = lower_generic_params (function.get_generic_params ());
76 :
77 2213 : HIR::Type *return_type
78 2213 : = function.has_return_type ()
79 2213 : ? ASTLoweringType::translate (function.get_return_type ())
80 : : nullptr;
81 :
82 2213 : bool is_variadic = function.is_variadic ();
83 2213 : auto begin = function.get_function_params ().begin ();
84 2213 : auto end = is_variadic ? function.get_function_params ().end () - 1
85 1385 : : function.get_function_params ().end ();
86 :
87 2213 : std::vector<HIR::NamedFunctionParam> function_params;
88 4903 : for (auto it = begin; it != end; it++)
89 : {
90 2690 : auto ¶m = static_cast<AST::FunctionParam &> (**it);
91 :
92 2690 : if (param.is_variadic () || param.is_self ())
93 0 : continue;
94 2690 : auto param_kind = param.get_pattern ().get_pattern_kind ();
95 :
96 2690 : rust_assert (param_kind == AST::Pattern::Kind::Identifier
97 : || param_kind == AST::Pattern::Kind::Wildcard);
98 2690 : auto ¶m_ident
99 2690 : = static_cast<AST::IdentifierPattern &> (param.get_pattern ());
100 2690 : Identifier param_name = param_kind == AST::Pattern::Kind::Identifier
101 2690 : ? param_ident.get_ident ()
102 2756 : : Identifier ("_", param.get_locus ());
103 :
104 2690 : HIR::Type *param_type = ASTLoweringType::translate (param.get_type ());
105 :
106 2690 : auto crate_num = mappings.get_current_crate ();
107 2690 : Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
108 2690 : mappings.get_next_hir_id (crate_num),
109 2690 : mappings.get_next_localdef_id (
110 2690 : crate_num));
111 :
112 2690 : function_params.emplace_back (mapping, param_name,
113 2690 : std::unique_ptr<HIR::Type> (param_type));
114 2690 : }
115 :
116 2213 : auto crate_num = mappings.get_current_crate ();
117 4426 : Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
118 2213 : mappings.get_next_hir_id (crate_num),
119 2213 : mappings.get_next_localdef_id (crate_num));
120 :
121 2213 : translated = new HIR::ExternalFunctionItem (
122 4426 : mapping, function.get_function_name (), std::move (generic_params),
123 4426 : std::unique_ptr<HIR::Type> (return_type), std::move (where_clause),
124 : std::move (function_params), is_variadic, std::move (vis),
125 6639 : function.get_outer_attrs (), function.get_locus ());
126 2213 : }
127 :
128 0 : void visit (AST::ExternalTypeItem &type) override
129 : {
130 0 : auto crate_num = mappings.get_current_crate ();
131 0 : Analysis::NodeMapping mapping (crate_num, type.get_node_id (),
132 0 : mappings.get_next_hir_id (crate_num),
133 0 : mappings.get_next_localdef_id (crate_num));
134 :
135 0 : HIR::Visibility vis = translate_visibility (type.get_visibility ());
136 :
137 0 : translated = new HIR::ExternalTypeItem (mapping, type.get_identifier (),
138 0 : vis, type.get_locus ());
139 0 : }
140 :
141 : private:
142 2214 : ASTLoweringExternItem () : translated (nullptr) {}
143 :
144 : HIR::ExternalItem *translated;
145 : };
146 :
147 : } // namespace HIR
148 : } // namespace Rust
149 :
150 : #endif // RUST_AST_LOWER_ITEM
|