| 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 |
#include "rust-compile-item.h" |
| 20 |
#include "rust-compile-implitem.h" |
| 21 |
#include "rust-compile-extern.h" |
| 22 |
#include "rust-substitution-mapper.h" |
| 23 |
#include "rust-type-util.h" |
| 24 |
#include "rust-immutable-name-resolution-context.h" |
| 25 |
|
| 26 |
namespace Rust { |
| 27 |
namespace Compile { |
| 28 |
|
| 29 |
void |
| 30 |
CompileItem::visit (HIR::StaticItem &var) |
| 31 |
{ |
| 32 |
// have we already compiled this? |
| 33 |
Bvariable *static_decl_ref = nullptr; |
| 34 |
if (ctx->lookup_var_decl (var.get_mappings ().get_hirid (), &static_decl_ref)) |
| 35 |
{ |
| 36 |
reference = Backend::var_expression (static_decl_ref, ref_locus); |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
HIR::Expr &const_value_expr = var.get_expr (); |
| 41 |
|
| 42 |
TyTy::BaseType *resolved_type = nullptr; |
| 43 |
TyTy::BaseType *expr_type = nullptr; |
| 44 |
bool ok = ctx->get_tyctx ()->lookup_type (var.get_mappings ().get_hirid (), |
| 45 |
&resolved_type); |
| 46 |
rust_assert (ok); |
| 47 |
ok = ctx->get_tyctx ()->lookup_type ( |
| 48 |
const_value_expr.get_mappings ().get_hirid (), &expr_type); |
| 49 |
rust_assert (ok); |
| 50 |
|
| 51 |
tree type = TyTyResolveCompile::compile (ctx, resolved_type); |
| 52 |
|
| 53 |
tl::optional<Resolver::CanonicalPath> canonical_path; |
| 54 |
|
| 55 |
if (flag_name_resolution_2_0) |
| 56 |
{ |
| 57 |
auto &nr_ctx |
| 58 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 59 |
|
| 60 |
canonical_path |
| 61 |
= nr_ctx.values.to_canonical_path (var.get_mappings ().get_nodeid ()); |
| 62 |
} |
| 63 |
else |
| 64 |
{ |
| 65 |
canonical_path = ctx->get_mappings ().lookup_canonical_path ( |
| 66 |
var.get_mappings ().get_nodeid ()); |
| 67 |
} |
| 68 |
|
| 69 |
rust_assert (canonical_path.has_value ()); |
| 70 |
|
| 71 |
ctx->push_const_context (); |
| 72 |
tree value |
| 73 |
= compile_constant_item (var.get_mappings ().get_hirid (), expr_type, |
| 74 |
resolved_type, *canonical_path, const_value_expr, |
| 75 |
var.get_locus (), const_value_expr.get_locus ()); |
| 76 |
ctx->pop_const_context (); |
| 77 |
|
| 78 |
std::string name = canonical_path->get (); |
| 79 |
std::string asm_name = ctx->mangle_item (resolved_type, *canonical_path); |
| 80 |
|
| 81 |
bool is_external = false; |
| 82 |
bool is_hidden = false; |
| 83 |
bool in_unique_section = true; |
| 84 |
|
| 85 |
Bvariable *static_global |
| 86 |
= Backend::global_variable (name, asm_name, type, is_external, is_hidden, |
| 87 |
in_unique_section, var.get_locus ()); |
| 88 |
|
| 89 |
tree init = value == error_mark_node ? error_mark_node : DECL_INITIAL (value); |
| 90 |
Backend::global_variable_set_init (static_global, init); |
| 91 |
|
| 92 |
ctx->insert_var_decl (var.get_mappings ().get_hirid (), static_global); |
| 93 |
ctx->push_var (static_global); |
| 94 |
|
| 95 |
reference = Backend::var_expression (static_global, ref_locus); |
| 96 |
} |
| 97 |
|
| 98 |
void |
| 99 |
CompileItem::visit (HIR::ConstantItem &constant) |
| 100 |
{ |
| 101 |
HIR::Expr &const_value_expr = constant.get_expr (); |
| 102 |
auto &mappings = constant.get_mappings (); |
| 103 |
|
| 104 |
if (ctx->lookup_const_decl (mappings.get_hirid (), &reference)) |
| 105 |
return; |
| 106 |
|
| 107 |
// resolve the type |
| 108 |
TyTy::BaseType *constant_type = nullptr; |
| 109 |
TyTy::BaseType *expr_type = nullptr; |
| 110 |
|
| 111 |
bool ok |
| 112 |
= ctx->get_tyctx ()->lookup_type (mappings.get_hirid (), &constant_type); |
| 113 |
rust_assert (ok); |
| 114 |
ok = ctx->get_tyctx ()->lookup_type ( |
| 115 |
const_value_expr.get_mappings ().get_hirid (), &expr_type); |
| 116 |
rust_assert (ok); |
| 117 |
|
| 118 |
// canonical path |
| 119 |
Resolver::CanonicalPath canonical_path |
| 120 |
= Resolver::CanonicalPath::create_empty (); |
| 121 |
|
| 122 |
if (flag_name_resolution_2_0) |
| 123 |
{ |
| 124 |
auto &nr_ctx |
| 125 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 126 |
|
| 127 |
canonical_path |
| 128 |
= nr_ctx.values.to_canonical_path (mappings.get_nodeid ()).value (); |
| 129 |
} |
| 130 |
else |
| 131 |
{ |
| 132 |
canonical_path = ctx->get_mappings () |
| 133 |
.lookup_canonical_path (mappings.get_nodeid ()) |
| 134 |
.value (); |
| 135 |
} |
| 136 |
|
| 137 |
ctx->push_const_context (); |
| 138 |
tree const_expr |
| 139 |
= compile_constant_item (mappings.get_hirid (), expr_type, constant_type, |
| 140 |
canonical_path, const_value_expr, |
| 141 |
constant.get_locus (), |
| 142 |
const_value_expr.get_locus ()); |
| 143 |
ctx->pop_const_context (); |
| 144 |
|
| 145 |
ctx->push_const (const_expr); |
| 146 |
ctx->insert_const_decl (mappings.get_hirid (), const_expr); |
| 147 |
reference = const_expr; |
| 148 |
} |
| 149 |
|
| 150 |
void |
| 151 |
CompileItem::visit (HIR::Function &function) |
| 152 |
{ |
| 153 |
TyTy::BaseType *fntype_tyty; |
| 154 |
if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (), |
| 155 |
&fntype_tyty)) |
| 156 |
{ |
| 157 |
rust_fatal_error (function.get_locus (), |
| 158 |
"failed to lookup function type"); |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
rust_assert (fntype_tyty->get_kind () == TyTy::TypeKind::FNDEF); |
| 163 |
TyTy::FnType *fntype = static_cast<TyTy::FnType *> (fntype_tyty); |
| 164 |
if (fntype->has_substitutions_defined ()) |
| 165 |
{ |
| 166 |
// we cant do anything for this only when it is used and a concrete type |
| 167 |
// is given |
| 168 |
if (concrete == nullptr) |
| 169 |
return; |
| 170 |
|
| 171 |
rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF); |
| 172 |
TyTy::FnType *concrete_fnty = static_cast<TyTy::FnType *> (concrete); |
| 173 |
bool is_trait_item_concrete |
| 174 |
= ctx->get_mappings () |
| 175 |
.lookup_trait_item_defid (concrete_fnty->get_id ()) |
| 176 |
.has_value (); |
| 177 |
if (!is_trait_item_concrete) |
| 178 |
{ |
| 179 |
rust_assert (concrete->get_kind () == TyTy::TypeKind::FNDEF); |
| 180 |
fntype = static_cast<TyTy::FnType *> (concrete); |
| 181 |
} |
| 182 |
else |
| 183 |
{ |
| 184 |
TyTy::BaseType *infer |
| 185 |
= Resolver::SubstMapper::InferSubst (fntype, function.get_locus ()); |
| 186 |
TyTy::BaseType *resolved |
| 187 |
= Resolver::unify_site (function.get_mappings ().get_hirid (), |
| 188 |
TyTy::TyWithLocation (infer), |
| 189 |
TyTy::TyWithLocation (concrete), |
| 190 |
function.get_locus ()); |
| 191 |
|
| 192 |
rust_assert (resolved->is<TyTy::FnType> ()); |
| 193 |
fntype = resolved->as<TyTy::FnType> (); |
| 194 |
} |
| 195 |
|
| 196 |
fntype->monomorphize (); |
| 197 |
} |
| 198 |
else |
| 199 |
{ |
| 200 |
// if this is part of a trait impl block which is not generic we need to |
| 201 |
// ensure associated types are setup |
| 202 |
HirId id = function.get_mappings ().get_hirid (); |
| 203 |
if (auto impl_item = ctx->get_mappings ().lookup_hir_implitem (id)) |
| 204 |
{ |
| 205 |
Resolver::AssociatedImplTrait *impl = nullptr; |
| 206 |
bool found = ctx->get_tyctx ()->lookup_associated_trait_impl ( |
| 207 |
impl_item->second, &impl); |
| 208 |
if (found) |
| 209 |
impl->setup_raw_associated_types (); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
Resolver::CanonicalPath canonical_path |
| 214 |
= Resolver::CanonicalPath::create_empty (); |
| 215 |
|
| 216 |
if (flag_name_resolution_2_0) |
| 217 |
{ |
| 218 |
auto &nr_ctx |
| 219 |
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver (); |
| 220 |
|
| 221 |
auto path = nr_ctx.values.to_canonical_path ( |
| 222 |
function.get_mappings ().get_nodeid ()); |
| 223 |
|
| 224 |
canonical_path = path.value (); |
| 225 |
} |
| 226 |
else |
| 227 |
{ |
| 228 |
auto path = ctx->get_mappings ().lookup_canonical_path ( |
| 229 |
function.get_mappings ().get_nodeid ()); |
| 230 |
|
| 231 |
canonical_path = *path; |
| 232 |
} |
| 233 |
|
| 234 |
const std::string asm_name = ctx->mangle_item (fntype, canonical_path); |
| 235 |
|
| 236 |
// items can be forward compiled which means we may not need to invoke this |
| 237 |
// code. We might also have already compiled this generic function as well. |
| 238 |
tree lookup = NULL_TREE; |
| 239 |
if (ctx->lookup_function_decl (fntype->get_ty_ref (), &lookup, |
| 240 |
fntype->get_id (), fntype, asm_name)) |
| 241 |
{ |
| 242 |
reference = address_expression (lookup, ref_locus); |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
if (fntype->has_substitutions_defined ()) |
| 247 |
{ |
| 248 |
// override the Hir Lookups for the substituions in this context |
| 249 |
fntype->override_context (); |
| 250 |
} |
| 251 |
|
| 252 |
if (function.get_qualifiers ().is_const ()) |
| 253 |
ctx->push_const_context (); |
| 254 |
|
| 255 |
auto lookup_root_item = ctx->get_mappings ().lookup_hir_item ( |
| 256 |
function.get_mappings ().get_hirid ()); |
| 257 |
bool is_root_item = lookup_root_item.has_value (); |
| 258 |
tree fndecl |
| 259 |
= compile_function (is_root_item, |
| 260 |
function.get_function_name ().as_string (), |
| 261 |
function.get_self_param (), |
| 262 |
function.get_function_params (), |
| 263 |
function.get_qualifiers (), function.get_visibility (), |
| 264 |
function.get_outer_attrs (), function.get_locus (), |
| 265 |
&function.get_definition (), canonical_path, fntype); |
| 266 |
reference = address_expression (fndecl, ref_locus); |
| 267 |
|
| 268 |
if (function.get_qualifiers ().is_const ()) |
| 269 |
ctx->pop_const_context (); |
| 270 |
} |
| 271 |
|
| 272 |
void |
| 273 |
CompileItem::visit (HIR::ImplBlock &impl_block) |
| 274 |
{ |
| 275 |
TyTy::BaseType *self_lookup = nullptr; |
| 276 |
if (!ctx->get_tyctx ()->lookup_type ( |
| 277 |
impl_block.get_type ().get_mappings ().get_hirid (), &self_lookup)) |
| 278 |
{ |
| 279 |
rust_error_at (impl_block.get_locus (), "failed to resolve type of impl"); |
| 280 |
return; |
| 281 |
} |
| 282 |
|
| 283 |
for (auto &impl_item : impl_block.get_impl_items ()) |
| 284 |
CompileInherentImplItem::Compile (impl_item.get (), ctx); |
| 285 |
} |
| 286 |
|
| 287 |
void |
| 288 |
CompileItem::visit (HIR::ExternBlock &extern_block) |
| 289 |
{ |
| 290 |
for (auto &item : extern_block.get_extern_items ()) |
| 291 |
{ |
| 292 |
CompileExternItem::compile (item.get (), ctx, concrete); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
void |
| 297 |
CompileItem::visit (HIR::Module &module) |
| 298 |
{ |
| 299 |
for (auto &item : module.get_items ()) |
| 300 |
CompileItem::compile (item.get (), ctx); |
| 301 |
} |
| 302 |
|
| 303 |
} // namespace Compile |
| 304 |
} // namespace Rust |