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