1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
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"
31 TypeCheckStmt::Resolve (HIR::Stmt
*stmt
)
33 TypeCheckStmt resolver
;
34 stmt
->accept_vis (resolver
);
35 return resolver
.infered
;
39 TypeCheckStmt::visit (HIR::ExprStmt
&stmt
)
41 infered
= TypeCheckExpr::Resolve (stmt
.get_expr ().get ());
45 TypeCheckStmt::visit (HIR::EmptyStmt
&stmt
)
47 infered
= TyTy::TupleType::get_unit_type (stmt
.get_mappings ().get_hirid ());
51 TypeCheckStmt::visit (HIR::ExternBlock
&extern_block
)
53 for (auto &item
: extern_block
.get_extern_items ())
55 TypeCheckTopLevelExternItem::Resolve (item
.get (), extern_block
);
60 TypeCheckStmt::visit (HIR::ConstantItem
&constant
)
62 TyTy::BaseType
*type
= TypeCheckType::Resolve (constant
.get_type ().get ());
63 TyTy::BaseType
*expr_type
64 = TypeCheckExpr::Resolve (constant
.get_expr ().get ());
66 infered
= coercion_site (
67 constant
.get_mappings ().get_hirid (),
68 TyTy::TyWithLocation (type
, constant
.get_type ()->get_locus ()),
69 TyTy::TyWithLocation (expr_type
, constant
.get_expr ()->get_locus ()),
70 constant
.get_locus ());
71 context
->insert_type (constant
.get_mappings (), infered
);
75 TypeCheckStmt::visit (HIR::LetStmt
&stmt
)
77 infered
= TyTy::TupleType::get_unit_type (stmt
.get_mappings ().get_hirid ());
79 HIR::Pattern
&stmt_pattern
= *stmt
.get_pattern ();
80 TyTy::BaseType
*init_expr_ty
= nullptr;
81 location_t init_expr_locus
= UNKNOWN_LOCATION
;
82 if (stmt
.has_init_expr ())
84 init_expr_locus
= stmt
.get_init_expr ()->get_locus ();
85 init_expr_ty
= TypeCheckExpr::Resolve (stmt
.get_init_expr ().get ());
86 if (init_expr_ty
->get_kind () == TyTy::TypeKind::ERROR
)
89 init_expr_ty
->append_reference (
90 stmt_pattern
.get_mappings ().get_hirid ());
93 TyTy::BaseType
*specified_ty
= nullptr;
94 location_t specified_ty_locus
;
97 specified_ty
= TypeCheckType::Resolve (stmt
.get_type ().get ());
98 specified_ty_locus
= stmt
.get_type ()->get_locus ();
102 if (specified_ty
!= nullptr && init_expr_ty
!= nullptr)
104 coercion_site (stmt
.get_mappings ().get_hirid (),
105 TyTy::TyWithLocation (specified_ty
, specified_ty_locus
),
106 TyTy::TyWithLocation (init_expr_ty
, init_expr_locus
),
108 TypeCheckPattern::Resolve (&stmt_pattern
, specified_ty
);
113 if (specified_ty
!= nullptr)
115 TypeCheckPattern::Resolve (&stmt_pattern
, specified_ty
);
118 else if (init_expr_ty
!= nullptr)
120 TypeCheckPattern::Resolve (&stmt_pattern
, init_expr_ty
);
126 = new TyTy::InferType (stmt_pattern
.get_mappings ().get_hirid (),
127 TyTy::InferType::InferTypeKind::GENERAL
,
128 TyTy::InferType::TypeHint::Default (),
130 TypeCheckPattern::Resolve (&stmt_pattern
, infer
);
136 TypeCheckStmt::visit (HIR::TypePath
&path
)
138 infered
= TypeCheckType::Resolve (&path
);
141 TypeCheckStmt::visit (HIR::QualifiedPathInType
&path
)
143 infered
= TypeCheckType::Resolve (&path
);
147 TypeCheckStmt::visit (HIR::TupleStruct
&struct_decl
)
149 infered
= TypeCheckItem::Resolve (struct_decl
);
153 TypeCheckStmt::visit (HIR::Enum
&enum_decl
)
155 infered
= TypeCheckItem::Resolve (enum_decl
);
159 TypeCheckStmt::visit (HIR::StructStruct
&struct_decl
)
161 infered
= TypeCheckItem::Resolve (struct_decl
);
165 TypeCheckStmt::visit (HIR::Union
&union_decl
)
167 infered
= TypeCheckItem::Resolve (union_decl
);
171 TypeCheckStmt::visit (HIR::Function
&function
)
173 infered
= TypeCheckItem::Resolve (function
);
177 TypeCheckStmt::visit (HIR::Module
&module
)
179 infered
= TypeCheckItem::Resolve (module
);
183 TypeCheckStmt::visit (HIR::TypeAlias
&type_alias
)
185 infered
= TypeCheckItem::Resolve (type_alias
);
189 TypeCheckStmt::visit (HIR::StaticItem
&static_item
)
191 infered
= TypeCheckItem::Resolve (static_item
);
195 TypeCheckStmt::visit (HIR::Trait
&trait
)
197 infered
= TypeCheckItem::Resolve (trait
);
201 TypeCheckStmt::visit (HIR::ImplBlock
&impl
)
203 infered
= TypeCheckItem::Resolve (impl
);
206 } // namespace Resolver