Debian Codegraph @

gcc/rust/typecheck/rust-hir-type-check-stmt.cc

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