| 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-context.h" |
| 20 |
#include "rust-compile-type.h" |
| 21 |
|
| 22 |
namespace Rust { |
| 23 |
namespace Compile { |
| 24 |
|
| 25 |
Context::Context () |
| 26 |
: resolver (Resolver::Resolver::get ()), |
| 27 |
tyctx (Resolver::TypeCheckContext::get ()), |
| 28 |
mappings (Analysis::Mappings::get ()), mangler (Mangler ()) |
| 29 |
{ |
| 30 |
setup_builtins (); |
| 31 |
} |
| 32 |
|
| 33 |
void |
| 34 |
Context::setup_builtins () |
| 35 |
{ |
| 36 |
for (auto &builtin : tyctx->get_builtins ()) |
| 37 |
TyTyResolveCompile::compile (this, builtin.get ()); |
| 38 |
} |
| 39 |
|
| 40 |
hashval_t |
| 41 |
Context::type_hasher (tree type) |
| 42 |
{ |
| 43 |
inchash::hash hstate; |
| 44 |
|
| 45 |
hstate.add_int (TREE_CODE (type)); |
| 46 |
|
| 47 |
if (TYPE_NAME (type)) |
| 48 |
{ |
| 49 |
hashval_t record_name_hash |
| 50 |
= IDENTIFIER_HASH_VALUE (DECL_NAME (TYPE_NAME (type))); |
| 51 |
hstate.add_object (record_name_hash); |
| 52 |
} |
| 53 |
|
| 54 |
for (tree t = TYPE_ATTRIBUTES (type); t; t = TREE_CHAIN (t)) |
| 55 |
/* Just the identifier is adequate to distinguish. */ |
| 56 |
hstate.add_object (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (t))); |
| 57 |
|
| 58 |
switch (TREE_CODE (type)) |
| 59 |
{ |
| 60 |
case METHOD_TYPE: |
| 61 |
hstate.add_object (TYPE_HASH (TYPE_METHOD_BASETYPE (type))); |
| 62 |
/* FALLTHROUGH. */ |
| 63 |
case FUNCTION_TYPE: |
| 64 |
for (tree t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t)) |
| 65 |
if (TREE_VALUE (t) != error_mark_node) |
| 66 |
hstate.add_object (TYPE_HASH (TREE_VALUE (t))); |
| 67 |
break; |
| 68 |
|
| 69 |
case OFFSET_TYPE: |
| 70 |
hstate.add_object (TYPE_HASH (TYPE_OFFSET_BASETYPE (type))); |
| 71 |
break; |
| 72 |
|
| 73 |
case ARRAY_TYPE: { |
| 74 |
if (TYPE_DOMAIN (type)) |
| 75 |
hstate.add_object (TYPE_HASH (TYPE_DOMAIN (type))); |
| 76 |
if (!AGGREGATE_TYPE_P (TREE_TYPE (type))) |
| 77 |
{ |
| 78 |
unsigned typeless = TYPE_TYPELESS_STORAGE (type); |
| 79 |
hstate.add_object (typeless); |
| 80 |
} |
| 81 |
} |
| 82 |
break; |
| 83 |
|
| 84 |
case INTEGER_TYPE: { |
| 85 |
tree t = TYPE_MAX_VALUE (type); |
| 86 |
if (!t) |
| 87 |
t = TYPE_MIN_VALUE (type); |
| 88 |
for (int i = 0; i < TREE_INT_CST_NUNITS (t); i++) |
| 89 |
hstate.add_object (TREE_INT_CST_ELT (t, i)); |
| 90 |
break; |
| 91 |
} |
| 92 |
|
| 93 |
case REAL_TYPE: |
| 94 |
case FIXED_POINT_TYPE: { |
| 95 |
unsigned prec = TYPE_PRECISION (type); |
| 96 |
hstate.add_object (prec); |
| 97 |
break; |
| 98 |
} |
| 99 |
|
| 100 |
case VECTOR_TYPE: |
| 101 |
hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type)); |
| 102 |
break; |
| 103 |
|
| 104 |
case RECORD_TYPE: |
| 105 |
case UNION_TYPE: |
| 106 |
case QUAL_UNION_TYPE: { |
| 107 |
for (tree t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t)) |
| 108 |
{ |
| 109 |
hashval_t name_hash = IDENTIFIER_HASH_VALUE (DECL_NAME (t)); |
| 110 |
hashval_t type_hash = type_hasher (TREE_TYPE (t)); |
| 111 |
hstate.add_object (name_hash); |
| 112 |
hstate.add_object (type_hash); |
| 113 |
} |
| 114 |
} |
| 115 |
break; |
| 116 |
|
| 117 |
case BOOLEAN_TYPE: |
| 118 |
break; |
| 119 |
|
| 120 |
case REFERENCE_TYPE: |
| 121 |
case POINTER_TYPE: { |
| 122 |
hashval_t type_hash = type_hasher (TREE_TYPE (type)); |
| 123 |
hstate.add_object (type_hash); |
| 124 |
} |
| 125 |
break; |
| 126 |
|
| 127 |
default: |
| 128 |
break; |
| 129 |
} |
| 130 |
|
| 131 |
return hstate.end (); |
| 132 |
} |
| 133 |
|
| 134 |
void |
| 135 |
Context::push_closure_context (HirId id) |
| 136 |
{ |
| 137 |
auto it = closure_bindings.find (id); |
| 138 |
rust_assert (it == closure_bindings.end ()); |
| 139 |
|
| 140 |
closure_bindings.insert ({id, {}}); |
| 141 |
closure_scope_bindings.push_back (id); |
| 142 |
} |
| 143 |
|
| 144 |
void |
| 145 |
Context::pop_closure_context () |
| 146 |
{ |
| 147 |
rust_assert (!closure_scope_bindings.empty ()); |
| 148 |
|
| 149 |
HirId ref = closure_scope_bindings.back (); |
| 150 |
closure_scope_bindings.pop_back (); |
| 151 |
closure_bindings.erase (ref); |
| 152 |
} |
| 153 |
|
| 154 |
void |
| 155 |
Context::insert_closure_binding (HirId id, tree expr) |
| 156 |
{ |
| 157 |
rust_assert (!closure_scope_bindings.empty ()); |
| 158 |
|
| 159 |
HirId ref = closure_scope_bindings.back (); |
| 160 |
closure_bindings[ref].insert ({id, expr}); |
| 161 |
} |
| 162 |
|
| 163 |
bool |
| 164 |
Context::lookup_closure_binding (HirId id, tree *expr) |
| 165 |
{ |
| 166 |
if (closure_scope_bindings.empty ()) |
| 167 |
return false; |
| 168 |
|
| 169 |
HirId ref = closure_scope_bindings.back (); |
| 170 |
auto it = closure_bindings.find (ref); |
| 171 |
rust_assert (it != closure_bindings.end ()); |
| 172 |
|
| 173 |
auto iy = it->second.find (id); |
| 174 |
if (iy == it->second.end ()) |
| 175 |
return false; |
| 176 |
|
| 177 |
*expr = iy->second; |
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
} // namespace Compile |
| 182 |
} // namespace Rust |