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