1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #include "rust-compile-context.h"
20 #include "rust-compile-type.h"
26 : resolver (Resolver::Resolver::get ()),
27 tyctx (Resolver::TypeCheckContext::get ()),
28 mappings (Analysis::Mappings::get ()), mangler (Mangler ())
34 Context::setup_builtins ()
36 for (auto &builtin
: tyctx
->get_builtins ())
37 TyTyResolveCompile::compile (this, builtin
.get ());
41 Context::type_hasher (tree type
)
45 hstate
.add_int (TREE_CODE (type
));
49 hashval_t record_name_hash
50 = IDENTIFIER_HASH_VALUE (DECL_NAME (TYPE_NAME (type
)));
51 hstate
.add_object (record_name_hash
);
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
)));
58 switch (TREE_CODE (type
))
61 hstate
.add_object (TYPE_HASH (TYPE_METHOD_BASETYPE (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
)));
70 hstate
.add_object (TYPE_HASH (TYPE_OFFSET_BASETYPE (type
)));
74 if (TYPE_DOMAIN (type
))
75 hstate
.add_object (TYPE_HASH (TYPE_DOMAIN (type
)));
76 if (!AGGREGATE_TYPE_P (TREE_TYPE (type
)))
78 unsigned typeless
= TYPE_TYPELESS_STORAGE (type
);
79 hstate
.add_object (typeless
);
85 tree t
= TYPE_MAX_VALUE (type
);
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
));
94 case FIXED_POINT_TYPE
: {
95 unsigned prec
= TYPE_PRECISION (type
);
96 hstate
.add_object (prec
);
101 hstate
.add_poly_int (TYPE_VECTOR_SUBPARTS (type
));
106 case QUAL_UNION_TYPE
: {
107 for (tree t
= TYPE_FIELDS (type
); t
; t
= TREE_CHAIN (t
))
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
);
122 hashval_t type_hash
= type_hasher (TREE_TYPE (type
));
123 hstate
.add_object (type_hash
);
131 return hstate
.end ();
135 Context::push_closure_context (HirId id
)
137 auto it
= closure_bindings
.find (id
);
138 rust_assert (it
== closure_bindings
.end ());
140 closure_bindings
.insert ({id
, {}});
141 closure_scope_bindings
.push_back (id
);
145 Context::pop_closure_context ()
147 rust_assert (!closure_scope_bindings
.empty ());
149 HirId ref
= closure_scope_bindings
.back ();
150 closure_scope_bindings
.pop_back ();
151 closure_bindings
.erase (ref
);
155 Context::insert_closure_binding (HirId id
, tree expr
)
157 rust_assert (!closure_scope_bindings
.empty ());
159 HirId ref
= closure_scope_bindings
.back ();
160 closure_bindings
[ref
].insert ({id
, expr
});
164 Context::lookup_closure_binding (HirId id
, tree
*expr
)
166 if (closure_scope_bindings
.empty ())
169 HirId ref
= closure_scope_bindings
.back ();
170 auto it
= closure_bindings
.find (ref
);
171 rust_assert (it
!= closure_bindings
.end ());
173 auto iy
= it
->second
.find (id
);
174 if (iy
== it
->second
.end ())
181 } // namespace Compile