1 // Copyright (C) 2020-2023 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-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"
32 TypeCheckStmt::Resolve (HIR::Stmt
*stmt
)
34 TypeCheckStmt resolver
;
35 stmt
->accept_vis (resolver
);
36 return resolver
.infered
;
40 TypeCheckStmt::visit (HIR::ExprStmtWithBlock
&stmt
)
42 infered
= TypeCheckExpr::Resolve (stmt
.get_expr ());
46 TypeCheckStmt::visit (HIR::ExprStmtWithoutBlock
&stmt
)
48 infered
= TypeCheckExpr::Resolve (stmt
.get_expr ());
52 TypeCheckStmt::visit (HIR::EmptyStmt
&stmt
)
54 infered
= TyTy::TupleType::get_unit_type (stmt
.get_mappings ().get_hirid ());
58 TypeCheckStmt::visit (HIR::ExternBlock
&extern_block
)
60 for (auto &item
: extern_block
.get_extern_items ())
62 TypeCheckTopLevelExternItem::Resolve (item
.get (), extern_block
);
67 TypeCheckStmt::visit (HIR::ConstantItem
&constant
)
69 TyTy::BaseType
*type
= TypeCheckType::Resolve (constant
.get_type ());
70 TyTy::BaseType
*expr_type
= TypeCheckExpr::Resolve (constant
.get_expr ());
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
);
81 TypeCheckStmt::visit (HIR::LetStmt
&stmt
)
83 infered
= TyTy::TupleType::get_unit_type (stmt
.get_mappings ().get_hirid ());
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 ())
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
)
95 init_expr_ty
->append_reference (
96 stmt_pattern
.get_pattern_mappings ().get_hirid ());
99 TyTy::BaseType
*specified_ty
= nullptr;
100 Location specified_ty_locus
;
101 if (stmt
.has_type ())
103 specified_ty
= TypeCheckType::Resolve (stmt
.get_type ());
104 specified_ty_locus
= stmt
.get_type ()->get_locus ();
108 if (specified_ty
!= nullptr && init_expr_ty
!= nullptr)
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
),
114 TypeCheckPattern::Resolve (&stmt_pattern
, specified_ty
);
119 if (specified_ty
!= nullptr)
121 TypeCheckPattern::Resolve (&stmt_pattern
, specified_ty
);
124 else if (init_expr_ty
!= nullptr)
126 TypeCheckPattern::Resolve (&stmt_pattern
, init_expr_ty
);
131 TypeCheckPattern::Resolve (
133 new TyTy::InferType (
134 stmt_pattern
.get_pattern_mappings ().get_hirid (),
135 TyTy::InferType::InferTypeKind::GENERAL
, stmt
.get_locus ()));
141 TypeCheckStmt::visit (HIR::TypePath
&path
)
143 infered
= TypeCheckType::Resolve (&path
);
146 TypeCheckStmt::visit (HIR::QualifiedPathInType
&path
)
148 infered
= TypeCheckType::Resolve (&path
);
152 TypeCheckStmt::visit (HIR::TupleStruct
&struct_decl
)
154 infered
= TypeCheckItem::Resolve (struct_decl
);
158 TypeCheckStmt::visit (HIR::Enum
&enum_decl
)
160 infered
= TypeCheckItem::Resolve (enum_decl
);
164 TypeCheckStmt::visit (HIR::StructStruct
&struct_decl
)
166 infered
= TypeCheckItem::Resolve (struct_decl
);
170 TypeCheckStmt::visit (HIR::Union
&union_decl
)
172 infered
= TypeCheckItem::Resolve (union_decl
);
176 TypeCheckStmt::visit (HIR::Function
&function
)
178 infered
= TypeCheckItem::Resolve (function
);
182 TypeCheckStmt::visit (HIR::Module
&module
)
184 infered
= TypeCheckItem::Resolve (module
);
188 TypeCheckStmt::visit (HIR::TypeAlias
&type_alias
)
190 infered
= TypeCheckItem::Resolve (type_alias
);
194 TypeCheckStmt::visit (HIR::StaticItem
&static_item
)
196 infered
= TypeCheckItem::Resolve (static_item
);
200 TypeCheckStmt::visit (HIR::Trait
&trait
)
202 infered
= TypeCheckItem::Resolve (trait
);
206 TypeCheckStmt::visit (HIR::ImplBlock
&impl
)
208 infered
= TypeCheckItem::Resolve (impl
);
211 } // namespace Resolver