]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/rust/backend/rust-compile-context.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / rust / backend / rust-compile-context.cc
CommitLineData
83ffe9cd 1// Copyright (C) 2020-2023 Free Software Foundation, Inc.
cfbda2f7
PH
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
22namespace Rust {
23namespace Compile {
24
25Context::Context (::Backend *backend)
26 : backend (backend), resolver (Resolver::Resolver::get ()),
27 tyctx (Resolver::TypeCheckContext::get ()),
28 mappings (Analysis::Mappings::get ()), mangler (Mangler ())
29{
30 setup_builtins ();
31}
32
33void
34Context::setup_builtins ()
35{
36 auto builtins = resolver->get_builtin_types ();
37 for (auto it = builtins.begin (); it != builtins.end (); it++)
38 {
39 HirId ref;
40 bool ok = tyctx->lookup_type_by_node_id ((*it)->get_node_id (), &ref);
41 rust_assert (ok);
42
43 TyTy::BaseType *lookup;
44 ok = tyctx->lookup_type (ref, &lookup);
45 rust_assert (ok);
46
47 TyTyResolveCompile::compile (this, lookup);
48 }
49}
50
51hashval_t
52Context::type_hasher (tree type)
53{
54 inchash::hash hstate;
55
56 hstate.add_int (TREE_CODE (type));
57
58 if (TYPE_NAME (type))
59 {
60 hashval_t record_name_hash
61 = IDENTIFIER_HASH_VALUE (DECL_NAME (TYPE_NAME (type)));
62 hstate.add_object (record_name_hash);
63 }
64
65 for (tree t = TYPE_ATTRIBUTES (type); t; t = TREE_CHAIN (t))
66 /* Just the identifier is adequate to distinguish. */
67 hstate.add_object (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (t)));
68
69 switch (TREE_CODE (type))
70 {
71 case METHOD_TYPE:
72 hstate.add_object (TYPE_HASH (TYPE_METHOD_BASETYPE (type)));
73 /* FALLTHROUGH. */
74 case FUNCTION_TYPE:
75 for (tree t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
76 if (TREE_VALUE (t) != error_mark_node)
77 hstate.add_object (TYPE_HASH (TREE_VALUE (t)));
78 break;
79
80 case OFFSET_TYPE:
81 hstate.add_object (TYPE_HASH (TYPE_OFFSET_BASETYPE (type)));
82 break;
83
84 case ARRAY_TYPE: {
85 if (TYPE_DOMAIN (type))
86 hstate.add_object (TYPE_HASH (TYPE_DOMAIN (type)));
87 if (!AGGREGATE_TYPE_P (TREE_TYPE (type)))
88 {
89 unsigned typeless = TYPE_TYPELESS_STORAGE (type);
90 hstate.add_object (typeless);
91 }
92 }
93 break;
94
95 case INTEGER_TYPE: {
96 tree t = TYPE_MAX_VALUE (type);
97 if (!t)
98 t = TYPE_MIN_VALUE (type);
99 for (int i = 0; i < TREE_INT_CST_NUNITS (t); i++)
100 hstate.add_object (TREE_INT_CST_ELT (t, i));
101 break;
102 }
103
104 case REAL_TYPE:
105 case FIXED_POINT_TYPE: {
106 unsigned prec = TYPE_PRECISION (type);
107 hstate.add_object (prec);
108 break;
109 }
110
111 case VECTOR_TYPE:
112 hstate.add_poly_int (TYPE_VECTOR_SUBPARTS (type));
113 break;
114
115 case RECORD_TYPE:
116 case UNION_TYPE:
117 case QUAL_UNION_TYPE: {
118 for (tree t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
119 {
120 hashval_t name_hash = IDENTIFIER_HASH_VALUE (DECL_NAME (t));
121 hashval_t type_hash = type_hasher (TREE_TYPE (t));
122 hstate.add_object (name_hash);
123 hstate.add_object (type_hash);
124 }
125 }
126 break;
127
128 case BOOLEAN_TYPE:
129 break;
130
131 case REFERENCE_TYPE:
132 case POINTER_TYPE: {
133 hashval_t type_hash = type_hasher (TREE_TYPE (type));
134 hstate.add_object (type_hash);
135 }
136 break;
137
138 default:
139 break;
140 }
141
142 return hstate.end ();
143}
144
145} // namespace Compile
146} // namespace Rust