| 1 |
// Copyright (C) 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-derive-hash.h" |
| 20 |
#include "rust-ast.h" |
| 21 |
#include "rust-expr.h" |
| 22 |
#include "rust-item.h" |
| 23 |
#include "rust-path.h" |
| 24 |
#include "rust-pattern.h" |
| 25 |
#include "rust-stmt.h" |
| 26 |
#include "rust-system.h" |
| 27 |
|
| 28 |
namespace Rust { |
| 29 |
namespace AST { |
| 30 |
|
| 31 |
DeriveHash::DeriveHash (location_t loc) : DeriveVisitor (loc) {} |
| 32 |
|
| 33 |
std::unique_ptr<AST::Item> |
| 34 |
DeriveHash::go (Item &item) |
| 35 |
{ |
| 36 |
item.accept_vis (*this); |
| 37 |
|
| 38 |
return std::move (expanded); |
| 39 |
} |
| 40 |
|
| 41 |
std::unique_ptr<Expr> |
| 42 |
DeriveHash::hash_call (std::unique_ptr<Expr> &&value) |
| 43 |
{ |
| 44 |
auto hash |
| 45 |
= builder.path_in_expression ({"core", "hash", "Hash", "hash"}, true); |
| 46 |
|
| 47 |
return builder.call (ptrify (hash), |
| 48 |
vec (std::move (value), |
| 49 |
builder.identifier (DeriveHash::state))); |
| 50 |
} |
| 51 |
|
| 52 |
std::unique_ptr<AssociatedItem> |
| 53 |
DeriveHash::hash_fn (std::unique_ptr<BlockExpr> &&block) |
| 54 |
{ |
| 55 |
auto hash_calls = std::vector<std::unique_ptr<Stmt>> (); |
| 56 |
|
| 57 |
auto state_type = std::unique_ptr<TypeNoBounds> ( |
| 58 |
new TypePath (builder.type_path (DeriveHash::state_type))); |
| 59 |
auto state_param = |
| 60 |
|
| 61 |
builder.function_param (builder.identifier_pattern (DeriveHash::state), |
| 62 |
builder.reference_type (std::move (state_type), |
| 63 |
true)); |
| 64 |
|
| 65 |
auto params = vec (builder.self_ref_param (), std::move (state_param)); |
| 66 |
auto bounds = vec ( |
| 67 |
builder.trait_bound (builder.type_path ({"core", "hash", "Hasher"}, true))); |
| 68 |
auto generics = vec ( |
| 69 |
builder.generic_type_param (DeriveHash::state_type, std::move (bounds))); |
| 70 |
|
| 71 |
return builder.function ("hash", std::move (params), nullptr, |
| 72 |
std::move (block), std::move (generics)); |
| 73 |
} |
| 74 |
|
| 75 |
std::unique_ptr<Item> |
| 76 |
DeriveHash::hash_impl ( |
| 77 |
std::unique_ptr<AssociatedItem> &&hash_fn, std::string name, |
| 78 |
const std::vector<std::unique_ptr<GenericParam>> &type_generics) |
| 79 |
{ |
| 80 |
auto hash_path = builder.type_path ({"core", "hash", "Hash"}, true); |
| 81 |
|
| 82 |
auto trait_items = vec (std::move (hash_fn)); |
| 83 |
|
| 84 |
auto generics = setup_impl_generics (name, type_generics, |
| 85 |
builder.trait_bound (hash_path)); |
| 86 |
|
| 87 |
return builder.trait_impl (hash_path, std::move (generics.self_type), |
| 88 |
std::move (trait_items), |
| 89 |
std::move (generics.impl)); |
| 90 |
} |
| 91 |
|
| 92 |
void |
| 93 |
DeriveHash::visit_struct (StructStruct &item) |
| 94 |
{ |
| 95 |
auto hash_calls = std::vector<std::unique_ptr<Stmt>> (); |
| 96 |
|
| 97 |
for (auto &field : item.get_fields ()) |
| 98 |
{ |
| 99 |
auto value = builder.ref ( |
| 100 |
builder.field_access (builder.identifier ("self"), |
| 101 |
field.get_field_name ().as_string ())); |
| 102 |
|
| 103 |
auto stmt = builder.statementify (hash_call (std::move (value))); |
| 104 |
|
| 105 |
hash_calls.emplace_back (std::move (stmt)); |
| 106 |
} |
| 107 |
|
| 108 |
auto block = builder.block (std::move (hash_calls)); |
| 109 |
|
| 110 |
expanded = hash_impl (hash_fn (std::move (block)), |
| 111 |
item.get_identifier ().as_string (), |
| 112 |
item.get_generic_params ()); |
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
DeriveHash::visit_tuple (TupleStruct &item) |
| 117 |
{ |
| 118 |
auto hash_calls = std::vector<std::unique_ptr<Stmt>> (); |
| 119 |
|
| 120 |
for (size_t idx = 0; idx < item.get_fields ().size (); idx++) |
| 121 |
{ |
| 122 |
auto value = builder.ref (builder.tuple_idx ("self", idx)); |
| 123 |
|
| 124 |
auto stmt = builder.statementify (hash_call (std::move (value))); |
| 125 |
|
| 126 |
hash_calls.emplace_back (std::move (stmt)); |
| 127 |
} |
| 128 |
|
| 129 |
auto block = builder.block (std::move (hash_calls)); |
| 130 |
|
| 131 |
expanded = hash_impl (hash_fn (std::move (block)), |
| 132 |
item.get_identifier ().as_string (), |
| 133 |
item.get_generic_params ()); |
| 134 |
} |
| 135 |
|
| 136 |
MatchCase |
| 137 |
DeriveHash::match_enum_tuple (PathInExpression variant_path, |
| 138 |
const EnumItemTuple &variant) |
| 139 |
{ |
| 140 |
auto self_patterns = std::vector<std::unique_ptr<Pattern>> (); |
| 141 |
auto hash_calls = std::vector<std::unique_ptr<Stmt>> (); |
| 142 |
|
| 143 |
for (size_t i = 0; i < variant.get_tuple_fields ().size (); i++) |
| 144 |
{ |
| 145 |
auto pattern = "__self_" + std::to_string (i); |
| 146 |
|
| 147 |
auto call = hash_call (builder.ref (builder.identifier (pattern))); |
| 148 |
|
| 149 |
self_patterns.emplace_back (builder.identifier_pattern (pattern)); |
| 150 |
hash_calls.emplace_back (builder.statementify (std::move (call))); |
| 151 |
} |
| 152 |
|
| 153 |
auto patterns_elts = std::unique_ptr<TupleStructItems> ( |
| 154 |
new TupleStructItemsNoRange (std::move (self_patterns))); |
| 155 |
auto pattern = std::unique_ptr<Pattern> ( |
| 156 |
new ReferencePattern (std::unique_ptr<Pattern> (new TupleStructPattern ( |
| 157 |
variant_path, std::move (patterns_elts))), |
| 158 |
false, false, loc)); |
| 159 |
|
| 160 |
auto block = builder.block (std::move (hash_calls)); |
| 161 |
|
| 162 |
return builder.match_case (std::move (pattern), std::move (block)); |
| 163 |
} |
| 164 |
|
| 165 |
MatchCase |
| 166 |
DeriveHash::match_enum_struct (PathInExpression variant_path, |
| 167 |
const EnumItemStruct &variant) |
| 168 |
{ |
| 169 |
auto field_patterns = std::vector<std::unique_ptr<StructPatternField>> (); |
| 170 |
auto hash_calls = std::vector<std::unique_ptr<Stmt>> (); |
| 171 |
|
| 172 |
for (const auto &field : variant.get_struct_fields ()) |
| 173 |
{ |
| 174 |
auto call = hash_call (builder.ref ( |
| 175 |
builder.identifier (field.get_field_name ().as_string ()))); |
| 176 |
|
| 177 |
field_patterns.emplace_back ( |
| 178 |
std::unique_ptr<StructPatternField> (new StructPatternFieldIdent ( |
| 179 |
field.get_field_name (), false /* is_ref? true? */, false, {}, loc))); |
| 180 |
|
| 181 |
hash_calls.emplace_back (builder.statementify (std::move (call))); |
| 182 |
} |
| 183 |
|
| 184 |
auto pattern_elts = StructPatternElements (std::move (field_patterns)); |
| 185 |
auto pattern = std::unique_ptr<Pattern> ( |
| 186 |
new ReferencePattern (std::unique_ptr<Pattern> (new StructPattern ( |
| 187 |
variant_path, loc, pattern_elts)), |
| 188 |
false, false, loc)); |
| 189 |
|
| 190 |
auto block = builder.block (std::move (hash_calls)); |
| 191 |
return builder.match_case (std::move (pattern), std::move (block)); |
| 192 |
} |
| 193 |
|
| 194 |
void |
| 195 |
DeriveHash::visit_enum (Enum &item) |
| 196 |
{ |
| 197 |
// Enums are a bit different: We start by hashing the discriminant value of |
| 198 |
// the enum instance, and then hash all of the data contained in each of the |
| 199 |
// enum's variants. For data-less variants, we don't have any data to hash, so |
| 200 |
// hashing the discriminant value is enough. To access the rest of the |
| 201 |
// variants' data, we create a match and destructure each internal field and |
| 202 |
// hash it. |
| 203 |
// |
| 204 |
// So for example with the following enum: |
| 205 |
// |
| 206 |
// ```rust |
| 207 |
// enum Foo { |
| 208 |
// A, |
| 209 |
// B(i32), |
| 210 |
// C { a: i32 }, |
| 211 |
// } |
| 212 |
// ``` |
| 213 |
// |
| 214 |
// we create the following implementation: |
| 215 |
// |
| 216 |
// ```rust |
| 217 |
// fn hash<H: Hasher>(&self, state: &mut H) { |
| 218 |
// let discriminant = intrinsics::discriminant_value(&self); |
| 219 |
// Hash::hash(&discriminant, state); |
| 220 |
// |
| 221 |
// match self { |
| 222 |
// B(self_0) => { Hash::hash(self_0, state); }, |
| 223 |
// C { a } => { Hash::hash(a, state); }, |
| 224 |
// _ => {}, |
| 225 |
// } |
| 226 |
// } |
| 227 |
// ``` |
| 228 |
// |
| 229 |
// Note the extra wildcard pattern to satisfy the exhaust checker. |
| 230 |
|
| 231 |
auto cases = std::vector<MatchCase> (); |
| 232 |
auto type_name = item.get_identifier ().as_string (); |
| 233 |
|
| 234 |
auto intrinsic = ptrify ( |
| 235 |
builder.path_in_expression ({"core", "intrinsics", "discriminant_value"}, |
| 236 |
true)); |
| 237 |
|
| 238 |
auto let_discr |
| 239 |
= builder.let (builder.identifier_pattern (DeriveHash::discr), nullptr, |
| 240 |
builder.call (std::move (intrinsic), |
| 241 |
builder.identifier ("self"))); |
| 242 |
|
| 243 |
auto discr_hash = builder.statementify ( |
| 244 |
hash_call (builder.ref (builder.identifier (DeriveHash::discr)))); |
| 245 |
|
| 246 |
for (auto &variant : item.get_variants ()) |
| 247 |
{ |
| 248 |
auto variant_path |
| 249 |
= builder.variant_path (type_name, |
| 250 |
variant->get_identifier ().as_string ()); |
| 251 |
|
| 252 |
switch (variant->get_enum_item_kind ()) |
| 253 |
{ |
| 254 |
case EnumItem::Kind::Identifier: |
| 255 |
case EnumItem::Kind::Discriminant: |
| 256 |
// nothing to do in these cases, as we just need to hash the |
| 257 |
// discriminant value |
| 258 |
continue; |
| 259 |
case EnumItem::Kind::Tuple: |
| 260 |
cases.emplace_back ( |
| 261 |
match_enum_tuple (variant_path, |
| 262 |
static_cast<EnumItemTuple &> (*variant))); |
| 263 |
break; |
| 264 |
case EnumItem::Kind::Struct: |
| 265 |
cases.emplace_back ( |
| 266 |
match_enum_struct (variant_path, |
| 267 |
static_cast<EnumItemStruct &> (*variant))); |
| 268 |
break; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
// The extra empty wildcard case |
| 273 |
cases.emplace_back ( |
| 274 |
builder.match_case (builder.wildcard (), builder.block ())); |
| 275 |
|
| 276 |
auto match = builder.match (builder.identifier ("self"), std::move (cases)); |
| 277 |
|
| 278 |
auto block |
| 279 |
= builder.block (vec (std::move (let_discr), std::move (discr_hash)), |
| 280 |
std::move (match)); |
| 281 |
|
| 282 |
expanded = hash_impl (hash_fn (std::move (block)), type_name, |
| 283 |
item.get_generic_params ()); |
| 284 |
} |
| 285 |
|
| 286 |
void |
| 287 |
DeriveHash::visit_union (Union &item) |
| 288 |
{ |
| 289 |
rust_error_at (item.get_locus (), "derive(Hash) cannot be used on unions"); |
| 290 |
} |
| 291 |
|
| 292 |
} // namespace AST |
| 293 |
} // namespace Rust |