Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / gcc / rust / typecheck / rust-hir-type-check-stmt.cc
blob956249a76070a05a86424fa60b5b92929f55cf67
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
8 // version.
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.
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"
28 namespace Rust {
29 namespace Resolver {
31 TyTy::BaseType *
32 TypeCheckStmt::Resolve (HIR::Stmt *stmt)
34 TypeCheckStmt resolver;
35 stmt->accept_vis (resolver);
36 return resolver.infered;
39 void
40 TypeCheckStmt::visit (HIR::ExprStmtWithBlock &stmt)
42 infered = TypeCheckExpr::Resolve (stmt.get_expr ());
45 void
46 TypeCheckStmt::visit (HIR::ExprStmtWithoutBlock &stmt)
48 infered = TypeCheckExpr::Resolve (stmt.get_expr ());
51 void
52 TypeCheckStmt::visit (HIR::EmptyStmt &stmt)
54 infered = TyTy::TupleType::get_unit_type (stmt.get_mappings ().get_hirid ());
57 void
58 TypeCheckStmt::visit (HIR::ExternBlock &extern_block)
60 for (auto &item : extern_block.get_extern_items ())
62 TypeCheckTopLevelExternItem::Resolve (item.get (), extern_block);
66 void
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);
80 void
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)
93 return;
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 ();
107 // let x:i32 = 123;
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),
113 stmt.get_locus ());
114 TypeCheckPattern::Resolve (&stmt_pattern, specified_ty);
116 else
118 // let x:i32;
119 if (specified_ty != nullptr)
121 TypeCheckPattern::Resolve (&stmt_pattern, specified_ty);
123 // let x = 123;
124 else if (init_expr_ty != nullptr)
126 TypeCheckPattern::Resolve (&stmt_pattern, init_expr_ty);
128 // let x;
129 else
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 ()));
140 void
141 TypeCheckStmt::visit (HIR::TypePath &path)
143 infered = TypeCheckType::Resolve (&path);
145 void
146 TypeCheckStmt::visit (HIR::QualifiedPathInType &path)
148 infered = TypeCheckType::Resolve (&path);
151 void
152 TypeCheckStmt::visit (HIR::TupleStruct &struct_decl)
154 infered = TypeCheckItem::Resolve (struct_decl);
157 void
158 TypeCheckStmt::visit (HIR::Enum &enum_decl)
160 infered = TypeCheckItem::Resolve (enum_decl);
163 void
164 TypeCheckStmt::visit (HIR::StructStruct &struct_decl)
166 infered = TypeCheckItem::Resolve (struct_decl);
169 void
170 TypeCheckStmt::visit (HIR::Union &union_decl)
172 infered = TypeCheckItem::Resolve (union_decl);
175 void
176 TypeCheckStmt::visit (HIR::Function &function)
178 infered = TypeCheckItem::Resolve (function);
181 void
182 TypeCheckStmt::visit (HIR::Module &module)
184 infered = TypeCheckItem::Resolve (module);
187 void
188 TypeCheckStmt::visit (HIR::TypeAlias &type_alias)
190 infered = TypeCheckItem::Resolve (type_alias);
193 void
194 TypeCheckStmt::visit (HIR::StaticItem &static_item)
196 infered = TypeCheckItem::Resolve (static_item);
199 void
200 TypeCheckStmt::visit (HIR::Trait &trait)
202 infered = TypeCheckItem::Resolve (trait);
205 void
206 TypeCheckStmt::visit (HIR::ImplBlock &impl)
208 infered = TypeCheckItem::Resolve (impl);
211 } // namespace Resolver
212 } // namespace Rust