Branch data Line data Source code
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-hir-type-check-stmt.h"
20 : : #include "rust-hir-type-check-type.h"
21 : : #include "rust-hir-type-check-expr.h"
22 : : #include "rust-hir-type-check-implitem.h"
23 : : #include "rust-hir-type-check-item.h"
24 : : #include "rust-hir-type-check-pattern.h"
25 : : #include "rust-type-util.h"
26 : :
27 : : namespace Rust {
28 : : namespace Resolver {
29 : :
30 : : TyTy::BaseType *
31 : 20862 : TypeCheckStmt::Resolve (HIR::Stmt &stmt)
32 : : {
33 : 20862 : TypeCheckStmt resolver;
34 : 20862 : stmt.accept_vis (resolver);
35 : 20862 : return resolver.infered;
36 : 20862 : }
37 : :
38 : : void
39 : 7671 : TypeCheckStmt::visit (HIR::ExprStmt &stmt)
40 : : {
41 : 7671 : infered = TypeCheckExpr::Resolve (stmt.get_expr ());
42 : 7671 : }
43 : :
44 : : void
45 : 54 : TypeCheckStmt::visit (HIR::EmptyStmt &stmt)
46 : : {
47 : 54 : infered = TyTy::TupleType::get_unit_type ();
48 : 54 : }
49 : :
50 : : void
51 : 18 : TypeCheckStmt::visit (HIR::ExternBlock &extern_block)
52 : : {
53 : 36 : for (auto &item : extern_block.get_extern_items ())
54 : : {
55 : 18 : TypeCheckTopLevelExternItem::Resolve (*item, extern_block);
56 : : }
57 : 18 : }
58 : :
59 : : void
60 : 67 : TypeCheckStmt::visit (HIR::ConstantItem &constant)
61 : : {
62 : 67 : TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ());
63 : 67 : TyTy::BaseType *expr_type = TypeCheckExpr::Resolve (constant.get_expr ());
64 : :
65 : 134 : infered = coercion_site (
66 : 67 : constant.get_mappings ().get_hirid (),
67 : 67 : TyTy::TyWithLocation (type, constant.get_type ().get_locus ()),
68 : 67 : TyTy::TyWithLocation (expr_type, constant.get_expr ().get_locus ()),
69 : : constant.get_locus ());
70 : 67 : context->insert_type (constant.get_mappings (), infered);
71 : 67 : }
72 : :
73 : : void
74 : 12696 : TypeCheckStmt::visit (HIR::LetStmt &stmt)
75 : : {
76 : 12696 : infered = TyTy::TupleType::get_unit_type ();
77 : :
78 : 12696 : auto &stmt_pattern = stmt.get_pattern ();
79 : 12696 : TyTy::BaseType *init_expr_ty = nullptr;
80 : 12696 : location_t init_expr_locus = UNKNOWN_LOCATION;
81 : 12696 : if (stmt.has_init_expr ())
82 : : {
83 : 11479 : init_expr_locus = stmt.get_init_expr ().get_locus ();
84 : 11479 : init_expr_ty = TypeCheckExpr::Resolve (stmt.get_init_expr ());
85 : 11479 : if (init_expr_ty->get_kind () == TyTy::TypeKind::ERROR)
86 : : return;
87 : :
88 : 11433 : init_expr_ty->append_reference (
89 : 11433 : stmt_pattern.get_mappings ().get_hirid ());
90 : : }
91 : :
92 : 12650 : TyTy::BaseType *specified_ty = nullptr;
93 : 12650 : location_t specified_ty_locus;
94 : 12650 : if (stmt.has_type ())
95 : : {
96 : 2263 : specified_ty = TypeCheckType::Resolve (stmt.get_type ());
97 : 2263 : specified_ty_locus = stmt.get_type ().get_locus ();
98 : : }
99 : :
100 : : // let x:i32 = 123;
101 : 12650 : if (specified_ty != nullptr && init_expr_ty != nullptr)
102 : : {
103 : 4084 : coercion_site (stmt.get_mappings ().get_hirid (),
104 : 2042 : TyTy::TyWithLocation (specified_ty, specified_ty_locus),
105 : 2042 : TyTy::TyWithLocation (init_expr_ty, init_expr_locus),
106 : : stmt.get_locus ());
107 : 2042 : TypeCheckPattern::Resolve (stmt_pattern, specified_ty);
108 : : }
109 : : else
110 : : {
111 : : // let x:i32;
112 : 10608 : if (specified_ty != nullptr)
113 : : {
114 : 221 : TypeCheckPattern::Resolve (stmt_pattern, specified_ty);
115 : : }
116 : : // let x = 123;
117 : 10387 : else if (init_expr_ty != nullptr)
118 : : {
119 : 9391 : TypeCheckPattern::Resolve (stmt_pattern, init_expr_ty);
120 : : }
121 : : // let x;
122 : : else
123 : : {
124 : 996 : auto infer
125 : 996 : = new TyTy::InferType (stmt_pattern.get_mappings ().get_hirid (),
126 : : TyTy::InferType::InferTypeKind::GENERAL,
127 : : TyTy::InferType::TypeHint::Default (),
128 : 996 : stmt.get_locus ());
129 : 996 : TypeCheckPattern::Resolve (stmt_pattern, infer);
130 : : }
131 : : }
132 : : }
133 : :
134 : : void
135 : 0 : TypeCheckStmt::visit (HIR::TypePath &path)
136 : : {
137 : 0 : infered = TypeCheckType::Resolve (path);
138 : 0 : }
139 : : void
140 : 0 : TypeCheckStmt::visit (HIR::QualifiedPathInType &path)
141 : : {
142 : 0 : infered = TypeCheckType::Resolve (path);
143 : 0 : }
144 : :
145 : : void
146 : 134 : TypeCheckStmt::visit (HIR::TupleStruct &struct_decl)
147 : : {
148 : 134 : infered = TypeCheckItem::Resolve (struct_decl);
149 : 134 : }
150 : :
151 : : void
152 : 12 : TypeCheckStmt::visit (HIR::Enum &enum_decl)
153 : : {
154 : 12 : infered = TypeCheckItem::Resolve (enum_decl);
155 : 12 : }
156 : :
157 : : void
158 : 101 : TypeCheckStmt::visit (HIR::StructStruct &struct_decl)
159 : : {
160 : 101 : infered = TypeCheckItem::Resolve (struct_decl);
161 : 101 : }
162 : :
163 : : void
164 : 0 : TypeCheckStmt::visit (HIR::Union &union_decl)
165 : : {
166 : 0 : infered = TypeCheckItem::Resolve (union_decl);
167 : 0 : }
168 : :
169 : : void
170 : 83 : TypeCheckStmt::visit (HIR::Function &function)
171 : : {
172 : 83 : infered = TypeCheckItem::Resolve (function);
173 : 83 : }
174 : :
175 : : void
176 : 0 : TypeCheckStmt::visit (HIR::Module &module)
177 : : {
178 : 0 : infered = TypeCheckItem::Resolve (module);
179 : 0 : }
180 : :
181 : : void
182 : 0 : TypeCheckStmt::visit (HIR::TypeAlias &type_alias)
183 : : {
184 : 0 : infered = TypeCheckItem::Resolve (type_alias);
185 : 0 : }
186 : :
187 : : void
188 : 2 : TypeCheckStmt::visit (HIR::StaticItem &static_item)
189 : : {
190 : 2 : infered = TypeCheckItem::Resolve (static_item);
191 : 2 : }
192 : :
193 : : void
194 : 12 : TypeCheckStmt::visit (HIR::Trait &trait)
195 : : {
196 : 12 : infered = TypeCheckItem::Resolve (trait);
197 : 12 : }
198 : :
199 : : void
200 : 12 : TypeCheckStmt::visit (HIR::ImplBlock &impl)
201 : : {
202 : 12 : infered = TypeCheckItem::Resolve (impl);
203 : 12 : }
204 : :
205 : : } // namespace Resolver
206 : : } // namespace Rust
|