]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/rust/backend/rust-compile-stmt.cc
Update copyright years.
[thirdparty/gcc.git] / gcc / rust / backend / rust-compile-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-compile-pattern.h"
20 #include "rust-compile-stmt.h"
21 #include "rust-compile-expr.h"
22
23 namespace Rust {
24 namespace Compile {
25
26 CompileStmt::CompileStmt (Context *ctx)
27 : HIRCompileBase (ctx), translated (nullptr)
28 {}
29
30 tree
31 CompileStmt::Compile (HIR::Stmt *stmt, Context *ctx)
32 {
33 CompileStmt compiler (ctx);
34 stmt->accept_vis (compiler);
35 return compiler.translated;
36 }
37
38 void
39 CompileStmt::visit (HIR::ExprStmtWithBlock &stmt)
40 {
41 translated = CompileExpr::Compile (stmt.get_expr (), ctx);
42 }
43
44 void
45 CompileStmt::visit (HIR::ExprStmtWithoutBlock &stmt)
46 {
47 translated = CompileExpr::Compile (stmt.get_expr (), ctx);
48 }
49
50 void
51 CompileStmt::visit (HIR::LetStmt &stmt)
52 {
53 // nothing to do
54 if (!stmt.has_init_expr ())
55 return;
56
57 HIR::Pattern &stmt_pattern = *stmt.get_pattern ();
58 HirId stmt_id = stmt_pattern.get_pattern_mappings ().get_hirid ();
59
60 TyTy::BaseType *ty = nullptr;
61 if (!ctx->get_tyctx ()->lookup_type (stmt_id, &ty))
62 {
63 // FIXME this should be an assertion instead
64 rust_fatal_error (stmt.get_locus (),
65 "failed to lookup variable declaration type");
66 return;
67 }
68
69 tree init = CompileExpr::Compile (stmt.get_init_expr (), ctx);
70 // FIXME use error_mark_node, check that CompileExpr returns error_mark_node
71 // on failure and make this an assertion
72 if (init == nullptr)
73 return;
74
75 TyTy::BaseType *actual = nullptr;
76 bool ok = ctx->get_tyctx ()->lookup_type (
77 stmt.get_init_expr ()->get_mappings ().get_hirid (), &actual);
78 rust_assert (ok);
79
80 Location lvalue_locus = stmt.get_pattern ()->get_locus ();
81 Location rvalue_locus = stmt.get_init_expr ()->get_locus ();
82 TyTy::BaseType *expected = ty;
83 init = coercion_site (stmt.get_mappings ().get_hirid (), init, actual,
84 expected, lvalue_locus, rvalue_locus);
85
86 CompilePatternLet::Compile (&stmt_pattern, init, ty, rvalue_locus, ctx);
87 }
88
89 } // namespace Compile
90 } // namespace Rust