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