| 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_CONTEXT |
| 20 |
#define RUST_COMPILE_CONTEXT |
| 21 |
|
| 22 |
#include "rust-system.h" |
| 23 |
#include "rust-hir-map.h" |
| 24 |
#include "rust-name-resolver.h" |
| 25 |
#include "rust-hir-type-check.h" |
| 26 |
#include "rust-backend.h" |
| 27 |
#include "rust-hir-full.h" |
| 28 |
#include "rust-mangle.h" |
| 29 |
#include "rust-tree.h" |
| 30 |
#include "rust-immutable-name-resolution-context.h" |
| 31 |
|
| 32 |
namespace Rust { |
| 33 |
namespace Compile { |
| 34 |
|
| 35 |
struct fncontext |
| 36 |
{ |
| 37 |
tree fndecl; |
| 38 |
::Bvariable *ret_addr; |
| 39 |
TyTy::BaseType *retty; |
| 40 |
}; |
| 41 |
|
| 42 |
struct CustomDeriveInfo |
| 43 |
{ |
| 44 |
tree fndecl; |
| 45 |
std::string trait_name; |
| 46 |
std::vector<std::string> attributes; |
| 47 |
}; |
| 48 |
|
| 49 |
class Context |
| 50 |
{ |
| 51 |
public: |
| 52 |
Context (); |
| 53 |
|
| 54 |
void setup_builtins (); |
| 55 |
|
| 56 |
bool lookup_compiled_types (tree t, tree *type) |
| 57 |
{ |
| 58 |
hashval_t h = type_hasher (t); |
| 59 |
auto it = compiled_type_map.find (h); |
| 60 |
if (it == compiled_type_map.end ()) |
| 61 |
return false; |
| 62 |
|
| 63 |
*type = it->second; |
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
tree insert_compiled_type (tree type) |
| 68 |
{ |
| 69 |
hashval_t h = type_hasher (type); |
| 70 |
auto it = compiled_type_map.find (h); |
| 71 |
if (it != compiled_type_map.end ()) |
| 72 |
return it->second; |
| 73 |
|
| 74 |
compiled_type_map.insert ({h, type}); |
| 75 |
|
| 76 |
if (TYPE_NAME (type) != NULL) |
| 77 |
push_type (type); |
| 78 |
|
| 79 |
return type; |
| 80 |
} |
| 81 |
|
| 82 |
tree insert_main_variant (tree type) |
| 83 |
{ |
| 84 |
hashval_t h = type_hasher (type); |
| 85 |
auto it = main_variants.find (h); |
| 86 |
if (it != main_variants.end ()) |
| 87 |
return it->second; |
| 88 |
|
| 89 |
main_variants.insert ({h, type}); |
| 90 |
return type; |
| 91 |
} |
| 92 |
|
| 93 |
Resolver::Resolver *get_resolver () { return resolver; } |
| 94 |
Resolver::TypeCheckContext *get_tyctx () { return tyctx; } |
| 95 |
Analysis::Mappings &get_mappings () { return mappings; } |
| 96 |
|
| 97 |
void push_block (tree scope) |
| 98 |
{ |
| 99 |
scope_stack.push_back (scope); |
| 100 |
statements.push_back ({}); |
| 101 |
} |
| 102 |
|
| 103 |
tree pop_block () |
| 104 |
{ |
| 105 |
auto block = scope_stack.back (); |
| 106 |
scope_stack.pop_back (); |
| 107 |
|
| 108 |
auto stmts = statements.back (); |
| 109 |
statements.pop_back (); |
| 110 |
|
| 111 |
Backend::block_add_statements (block, stmts); |
| 112 |
|
| 113 |
return block; |
| 114 |
} |
| 115 |
|
| 116 |
tree peek_enclosing_scope () |
| 117 |
{ |
| 118 |
if (scope_stack.size () == 0) |
| 119 |
return nullptr; |
| 120 |
|
| 121 |
return scope_stack.back (); |
| 122 |
} |
| 123 |
|
| 124 |
void add_statement_to_enclosing_scope (tree stmt) |
| 125 |
{ |
| 126 |
statements.at (statements.size () - 2).push_back (stmt); |
| 127 |
} |
| 128 |
|
| 129 |
void add_statement (tree stmt) { statements.back ().push_back (stmt); } |
| 130 |
|
| 131 |
void insert_var_decl (HirId id, ::Bvariable *decl) |
| 132 |
{ |
| 133 |
compiled_var_decls[id] = decl; |
| 134 |
} |
| 135 |
|
| 136 |
bool lookup_var_decl (HirId id, ::Bvariable **decl) |
| 137 |
{ |
| 138 |
auto it = compiled_var_decls.find (id); |
| 139 |
if (it == compiled_var_decls.end ()) |
| 140 |
return false; |
| 141 |
|
| 142 |
*decl = it->second; |
| 143 |
return true; |
| 144 |
} |
| 145 |
|
| 146 |
void insert_function_decl (const TyTy::FnType *ref, tree fn) |
| 147 |
{ |
| 148 |
auto id = ref->get_ty_ref (); |
| 149 |
auto dId = ref->get_id (); |
| 150 |
|
| 151 |
rust_assert (compiled_fn_map.find (id) == compiled_fn_map.end ()); |
| 152 |
compiled_fn_map[id] = fn; |
| 153 |
|
| 154 |
auto it = mono_fns.find (dId); |
| 155 |
if (it == mono_fns.end ()) |
| 156 |
mono_fns[dId] = {}; |
| 157 |
|
| 158 |
mono_fns[dId].push_back ({ref, fn}); |
| 159 |
} |
| 160 |
|
| 161 |
void insert_closure_decl (const TyTy::ClosureType *ref, tree fn) |
| 162 |
{ |
| 163 |
auto dId = ref->get_def_id (); |
| 164 |
auto it = mono_closure_fns.find (dId); |
| 165 |
if (it == mono_closure_fns.end ()) |
| 166 |
mono_closure_fns[dId] = {}; |
| 167 |
|
| 168 |
mono_closure_fns[dId].push_back ({ref, fn}); |
| 169 |
} |
| 170 |
|
| 171 |
tree lookup_closure_decl (const TyTy::ClosureType *ref) |
| 172 |
{ |
| 173 |
auto dId = ref->get_def_id (); |
| 174 |
auto it = mono_closure_fns.find (dId); |
| 175 |
if (it == mono_closure_fns.end ()) |
| 176 |
return error_mark_node; |
| 177 |
|
| 178 |
for (auto &i : it->second) |
| 179 |
{ |
| 180 |
const TyTy::ClosureType *t = i.first; |
| 181 |
tree fn = i.second; |
| 182 |
|
| 183 |
if (ref->is_equal (*t)) |
| 184 |
return fn; |
| 185 |
} |
| 186 |
|
| 187 |
return error_mark_node; |
| 188 |
} |
| 189 |
|
| 190 |
bool lookup_function_decl (HirId id, tree *fn, DefId dId = UNKNOWN_DEFID, |
| 191 |
const TyTy::BaseType *ref = nullptr, |
| 192 |
const std::string &asm_name = std::string ()) |
| 193 |
{ |
| 194 |
// for for any monomorphized fns |
| 195 |
if (ref != nullptr) |
| 196 |
{ |
| 197 |
rust_assert (dId != UNKNOWN_DEFID); |
| 198 |
|
| 199 |
auto it = mono_fns.find (dId); |
| 200 |
if (it == mono_fns.end ()) |
| 201 |
return false; |
| 202 |
|
| 203 |
for (auto &e : mono_fns[dId]) |
| 204 |
{ |
| 205 |
const TyTy::BaseType *r = e.first; |
| 206 |
tree f = e.second; |
| 207 |
|
| 208 |
if (ref->is_equal (*r)) |
| 209 |
{ |
| 210 |
*fn = f; |
| 211 |
return true; |
| 212 |
} |
| 213 |
|
| 214 |
if (DECL_ASSEMBLER_NAME_SET_P (f) && !asm_name.empty ()) |
| 215 |
{ |
| 216 |
tree raw = DECL_ASSEMBLER_NAME_RAW (f); |
| 217 |
const char *rptr = IDENTIFIER_POINTER (raw); |
| 218 |
|
| 219 |
bool lengths_match_p |
| 220 |
= IDENTIFIER_LENGTH (raw) == asm_name.size (); |
| 221 |
if (lengths_match_p |
| 222 |
&& strncmp (rptr, asm_name.c_str (), |
| 223 |
IDENTIFIER_LENGTH (raw)) |
| 224 |
== 0) |
| 225 |
{ |
| 226 |
*fn = f; |
| 227 |
return true; |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
return false; |
| 232 |
} |
| 233 |
|
| 234 |
auto it = compiled_fn_map.find (id); |
| 235 |
if (it == compiled_fn_map.end ()) |
| 236 |
return false; |
| 237 |
|
| 238 |
*fn = it->second; |
| 239 |
return true; |
| 240 |
} |
| 241 |
|
| 242 |
void insert_const_decl (HirId id, tree expr) { compiled_consts[id] = expr; } |
| 243 |
|
| 244 |
bool lookup_const_decl (HirId id, tree *expr) |
| 245 |
{ |
| 246 |
auto it = compiled_consts.find (id); |
| 247 |
if (it == compiled_consts.end ()) |
| 248 |
return false; |
| 249 |
|
| 250 |
*expr = it->second; |
| 251 |
return true; |
| 252 |
} |
| 253 |
|
| 254 |
void insert_label_decl (HirId id, tree label) { compiled_labels[id] = label; } |
| 255 |
|
| 256 |
bool lookup_label_decl (HirId id, tree *label) |
| 257 |
{ |
| 258 |
auto it = compiled_labels.find (id); |
| 259 |
if (it == compiled_labels.end ()) |
| 260 |
return false; |
| 261 |
|
| 262 |
*label = it->second; |
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
void insert_pattern_binding (HirId id, tree binding) |
| 267 |
{ |
| 268 |
implicit_pattern_bindings[id] = binding; |
| 269 |
} |
| 270 |
|
| 271 |
bool lookup_pattern_binding (HirId id, tree *binding) |
| 272 |
{ |
| 273 |
auto it = implicit_pattern_bindings.find (id); |
| 274 |
if (it == implicit_pattern_bindings.end ()) |
| 275 |
return false; |
| 276 |
|
| 277 |
*binding = it->second; |
| 278 |
return true; |
| 279 |
} |
| 280 |
|
| 281 |
void push_fn (tree fn, ::Bvariable *ret_addr, TyTy::BaseType *retty) |
| 282 |
{ |
| 283 |
fn_stack.push_back (fncontext{fn, ret_addr, retty}); |
| 284 |
} |
| 285 |
void pop_fn () { fn_stack.pop_back (); } |
| 286 |
|
| 287 |
bool in_fn () { return fn_stack.size () != 0; } |
| 288 |
|
| 289 |
// Note: it is undefined behavior to call peek_fn () if fn_stack is empty. |
| 290 |
fncontext peek_fn () |
| 291 |
{ |
| 292 |
rust_assert (!fn_stack.empty ()); |
| 293 |
return fn_stack.back (); |
| 294 |
} |
| 295 |
|
| 296 |
void push_type (tree t) { type_decls.push_back (t); } |
| 297 |
void push_var (::Bvariable *v) { var_decls.push_back (v); } |
| 298 |
void push_const (tree c) { const_decls.push_back (c); } |
| 299 |
void push_function (tree f) { func_decls.push_back (f); } |
| 300 |
|
| 301 |
void write_to_backend () |
| 302 |
{ |
| 303 |
Backend::write_global_definitions (type_decls, const_decls, func_decls, |
| 304 |
var_decls); |
| 305 |
} |
| 306 |
|
| 307 |
bool function_completed (tree fn) |
| 308 |
{ |
| 309 |
for (auto it = func_decls.begin (); it != func_decls.end (); it++) |
| 310 |
{ |
| 311 |
tree i = (*it); |
| 312 |
if (i == fn) |
| 313 |
{ |
| 314 |
return true; |
| 315 |
} |
| 316 |
} |
| 317 |
return false; |
| 318 |
} |
| 319 |
|
| 320 |
void push_loop_context (Bvariable *var) { loop_value_stack.push_back (var); } |
| 321 |
|
| 322 |
Bvariable *peek_loop_context () { return loop_value_stack.back (); } |
| 323 |
|
| 324 |
Bvariable *pop_loop_context () |
| 325 |
{ |
| 326 |
auto back = loop_value_stack.back (); |
| 327 |
loop_value_stack.pop_back (); |
| 328 |
return back; |
| 329 |
} |
| 330 |
|
| 331 |
void push_loop_begin_label (tree label) |
| 332 |
{ |
| 333 |
loop_begin_labels.push_back (label); |
| 334 |
} |
| 335 |
|
| 336 |
tree peek_loop_begin_label () { return loop_begin_labels.back (); } |
| 337 |
|
| 338 |
tree pop_loop_begin_label () |
| 339 |
{ |
| 340 |
tree pop = loop_begin_labels.back (); |
| 341 |
loop_begin_labels.pop_back (); |
| 342 |
return pop; |
| 343 |
} |
| 344 |
|
| 345 |
void push_const_context (void) { const_context++; } |
| 346 |
void pop_const_context (void) |
| 347 |
{ |
| 348 |
if (const_context > 0) |
| 349 |
const_context--; |
| 350 |
} |
| 351 |
bool const_context_p (void) { return (const_context > 0); } |
| 352 |
|
| 353 |
std::string mangle_item (const TyTy::BaseType *ty, |
| 354 |
const Resolver::CanonicalPath &path) |
| 355 |
{ |
| 356 |
return mangler.mangle_item (this, ty, path); |
| 357 |
} |
| 358 |
|
| 359 |
void push_closure_context (HirId id); |
| 360 |
void pop_closure_context (); |
| 361 |
void insert_closure_binding (HirId id, tree expr); |
| 362 |
bool lookup_closure_binding (HirId id, tree *expr); |
| 363 |
|
| 364 |
std::vector<tree> &get_type_decls () { return type_decls; } |
| 365 |
std::vector<::Bvariable *> &get_var_decls () { return var_decls; } |
| 366 |
std::vector<tree> &get_const_decls () { return const_decls; } |
| 367 |
std::vector<tree> &get_func_decls () { return func_decls; } |
| 368 |
|
| 369 |
static hashval_t type_hasher (tree type); |
| 370 |
|
| 371 |
void collect_attribute_proc_macro (tree fndecl) |
| 372 |
{ |
| 373 |
attribute_macros.push_back (fndecl); |
| 374 |
} |
| 375 |
|
| 376 |
void collect_bang_proc_macro (tree fndecl) { bang_macros.push_back (fndecl); } |
| 377 |
|
| 378 |
void collect_derive_proc_macro (CustomDeriveInfo macro) |
| 379 |
{ |
| 380 |
custom_derive_macros.push_back (macro); |
| 381 |
} |
| 382 |
|
| 383 |
const std::vector<tree> &get_bang_proc_macros () const { return bang_macros; } |
| 384 |
const std::vector<tree> &get_attribute_proc_macros () const |
| 385 |
{ |
| 386 |
return attribute_macros; |
| 387 |
} |
| 388 |
const std::vector<CustomDeriveInfo> &get_derive_proc_macros () const |
| 389 |
{ |
| 390 |
return custom_derive_macros; |
| 391 |
} |
| 392 |
|
| 393 |
private: |
| 394 |
Resolver::Resolver *resolver; |
| 395 |
Resolver::TypeCheckContext *tyctx; |
| 396 |
Analysis::Mappings &mappings; |
| 397 |
Mangler mangler; |
| 398 |
|
| 399 |
// state |
| 400 |
std::vector<fncontext> fn_stack; |
| 401 |
std::map<HirId, ::Bvariable *> compiled_var_decls; |
| 402 |
std::map<hashval_t, tree> compiled_type_map; |
| 403 |
std::map<HirId, tree> compiled_fn_map; |
| 404 |
std::map<HirId, tree> compiled_consts; |
| 405 |
std::map<HirId, tree> compiled_labels; |
| 406 |
std::vector<::std::vector<tree>> statements; |
| 407 |
std::vector<tree> scope_stack; |
| 408 |
std::vector<::Bvariable *> loop_value_stack; |
| 409 |
std::vector<tree> loop_begin_labels; |
| 410 |
std::map<DefId, std::vector<std::pair<const TyTy::BaseType *, tree>>> |
| 411 |
mono_fns; |
| 412 |
std::map<DefId, std::vector<std::pair<const TyTy::ClosureType *, tree>>> |
| 413 |
mono_closure_fns; |
| 414 |
std::map<HirId, tree> implicit_pattern_bindings; |
| 415 |
std::map<hashval_t, tree> main_variants; |
| 416 |
|
| 417 |
std::vector<CustomDeriveInfo> custom_derive_macros; |
| 418 |
std::vector<tree> attribute_macros; |
| 419 |
std::vector<tree> bang_macros; |
| 420 |
|
| 421 |
// closure bindings |
| 422 |
std::vector<HirId> closure_scope_bindings; |
| 423 |
std::map<HirId, std::map<HirId, tree>> closure_bindings; |
| 424 |
|
| 425 |
// To GCC middle-end |
| 426 |
std::vector<tree> type_decls; |
| 427 |
std::vector<::Bvariable *> var_decls; |
| 428 |
std::vector<tree> const_decls; |
| 429 |
std::vector<tree> func_decls; |
| 430 |
|
| 431 |
// Nonzero iff we are currently compiling something inside a constant context. |
| 432 |
unsigned int const_context = 0; |
| 433 |
}; |
| 434 |
|
| 435 |
} // namespace Compile |
| 436 |
} // namespace Rust |
| 437 |
|
| 438 |
#endif // RUST_COMPILE_CONTEXT |