| 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_COMPILE_EXTERN_ITEM |
| 20 |
#define RUST_COMPILE_EXTERN_ITEM |
| 21 |
|
| 22 |
#include "rust-compile-base.h" |
| 23 |
#include "rust-compile-intrinsic.h" |
| 24 |
#include "rust-compile-type.h" |
| 25 |
#include "rust-diagnostics.h" |
| 26 |
#include "rust-hir-full-decls.h" |
| 27 |
|
| 28 |
namespace Rust { |
| 29 |
namespace Compile { |
| 30 |
|
| 31 |
class CompileExternItem : public HIRCompileBase, |
| 32 |
public HIR::HIRExternalItemVisitor |
| 33 |
{ |
| 34 |
public: |
| 35 |
static tree compile (HIR::ExternalItem *item, Context *ctx, |
| 36 |
TyTy::BaseType *concrete = nullptr, |
| 37 |
location_t ref_locus = UNDEF_LOCATION) |
| 38 |
{ |
| 39 |
CompileExternItem compiler (ctx, concrete, ref_locus); |
| 40 |
item->accept_vis (compiler); |
| 41 |
return compiler.reference; |
| 42 |
} |
| 43 |
|
| 44 |
void visit (HIR::ExternalStaticItem &item) override |
| 45 |
{ |
| 46 |
// check if its already been compiled |
| 47 |
Bvariable *lookup = Bvariable::error_variable (); |
| 48 |
if (ctx->lookup_var_decl (item.get_mappings ().get_hirid (), &lookup)) |
| 49 |
{ |
| 50 |
reference = Backend::var_expression (lookup, ref_locus); |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
TyTy::BaseType *resolved_type = nullptr; |
| 55 |
bool ok = ctx->get_tyctx ()->lookup_type (item.get_mappings ().get_hirid (), |
| 56 |
&resolved_type); |
| 57 |
rust_assert (ok); |
| 58 |
|
| 59 |
std::string name = item.get_item_name ().as_string (); |
| 60 |
// FIXME this is assuming C ABI |
| 61 |
std::string asm_name = name; |
| 62 |
|
| 63 |
tree type = TyTyResolveCompile::compile (ctx, resolved_type); |
| 64 |
bool is_external = true; |
| 65 |
bool is_hidden = false; |
| 66 |
bool in_unique_section = false; |
| 67 |
|
| 68 |
Bvariable *static_global |
| 69 |
= Backend::global_variable (name, asm_name, type, is_external, is_hidden, |
| 70 |
in_unique_section, item.get_locus ()); |
| 71 |
ctx->insert_var_decl (item.get_mappings ().get_hirid (), static_global); |
| 72 |
ctx->push_var (static_global); |
| 73 |
|
| 74 |
reference = Backend::var_expression (static_global, ref_locus); |
| 75 |
} |
| 76 |
|
| 77 |
void visit (HIR::ExternalFunctionItem &function) override |
| 78 |
{ |
| 79 |
TyTy::BaseType *fntype_tyty; |
| 80 |
if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (), |
| 81 |
&fntype_tyty)) |
| 82 |
{ |
| 83 |
rust_fatal_error (function.get_locus (), |
| 84 |
"failed to lookup function type"); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF); |
| 89 |
TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty); |
| 90 |
if (fntype->has_substitutions_defined ()) |
| 91 |
{ |
| 92 |
// we cant do anything for this only when it is used and a concrete type |
| 93 |
// is given |
| 94 |
if (concrete == nullptr) |
| 95 |
return; |
| 96 |
else |
| 97 |
{ |
| 98 |
rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF); |
| 99 |
fntype = static_cast<TyTy::FnType *> (concrete); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// items can be forward compiled which means we may not need to invoke this |
| 104 |
// code. We might also have already compiled this generic function as well. |
| 105 |
tree lookup = NULL_TREE; |
| 106 |
if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup, |
| 107 |
fntype->get_id (), fntype)) |
| 108 |
{ |
| 109 |
reference = address_expression (lookup, ref_locus); |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
if (fntype->has_substitutions_defined ()) |
| 114 |
// override the HIR lookups for the substitutions in this context |
| 115 |
fntype->override_context (); |
| 116 |
|
| 117 |
if (fntype->get_abi () == ABI::INTRINSIC) |
| 118 |
{ |
| 119 |
Intrinsics compile (ctx); |
| 120 |
tree fndecl = compile.compile (fntype); |
| 121 |
ctx->insert_function_decl (fntype, fndecl); |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype); |
| 126 |
std::string ir_symbol_name = function.get_item_name ().as_string (); |
| 127 |
std::string asm_name = function.get_item_name ().as_string (); |
| 128 |
if (fntype->get_abi () == ABI::RUST) |
| 129 |
{ |
| 130 |
// then we need to get the canonical path of it and mangle it |
| 131 |
auto canonical_path = ctx->get_mappings ().lookup_canonical_path ( |
| 132 |
function.get_mappings ().get_nodeid ()); |
| 133 |
|
| 134 |
ir_symbol_name = canonical_path->get () + fntype->subst_as_string (); |
| 135 |
asm_name = ctx->mangle_item (fntype, *canonical_path); |
| 136 |
} |
| 137 |
|
| 138 |
const unsigned int flags = Backend::function_is_declaration; |
| 139 |
tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name, asm_name, |
| 140 |
flags, function.get_locus ()); |
| 141 |
TREE_PUBLIC (fndecl) = 1; |
| 142 |
setup_abi_options (fndecl, fntype->get_abi ()); |
| 143 |
|
| 144 |
ctx->insert_function_decl (fntype, fndecl); |
| 145 |
|
| 146 |
reference = address_expression (fndecl, ref_locus); |
| 147 |
} |
| 148 |
|
| 149 |
void visit (HIR::ExternalTypeItem &type) override |
| 150 |
{ |
| 151 |
rust_sorry_at (type.get_locus (), "extern types are not supported yet"); |
| 152 |
} |
| 153 |
|
| 154 |
private: |
| 155 |
CompileExternItem (Context *ctx, TyTy::BaseType *concrete, |
| 156 |
location_t ref_locus) |
| 157 |
: HIRCompileBase (ctx), concrete (concrete), reference (error_mark_node), |
| 158 |
ref_locus (ref_locus) |
| 159 |
{} |
| 160 |
|
| 161 |
TyTy::BaseType *concrete; |
| 162 |
tree reference; |
| 163 |
location_t ref_locus; |
| 164 |
}; |
| 165 |
|
| 166 |
} // namespace Compile |
| 167 |
} // namespace Rust |
| 168 |
|
| 169 |
#endif // RUST_COMPILE_EXTERN_ITEM |