]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/rust/backend/rust-compile-stmt.cc
1 // Copyright (C) 2020-2024 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-compile-pattern.h"
20 #include "rust-compile-stmt.h"
21 #include "rust-compile-expr.h"
26 CompileStmt::CompileStmt (Context
*ctx
)
27 : HIRCompileBase (ctx
), translated (nullptr)
31 CompileStmt::Compile (HIR::Stmt
*stmt
, Context
*ctx
)
33 CompileStmt
compiler (ctx
);
34 stmt
->accept_vis (compiler
);
35 return compiler
.translated
;
39 CompileStmt::visit (HIR::ExprStmtWithBlock
&stmt
)
41 translated
= CompileExpr::Compile (stmt
.get_expr (), ctx
);
45 CompileStmt::visit (HIR::ExprStmtWithoutBlock
&stmt
)
47 translated
= CompileExpr::Compile (stmt
.get_expr (), ctx
);
51 CompileStmt::visit (HIR::LetStmt
&stmt
)
54 if (!stmt
.has_init_expr ())
57 HIR::Pattern
&stmt_pattern
= *stmt
.get_pattern ();
58 HirId stmt_id
= stmt_pattern
.get_pattern_mappings ().get_hirid ();
60 TyTy::BaseType
*ty
= nullptr;
61 if (!ctx
->get_tyctx ()->lookup_type (stmt_id
, &ty
))
63 // FIXME this should be an assertion instead
64 rust_fatal_error (stmt
.get_locus (),
65 "failed to lookup variable declaration type");
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
75 TyTy::BaseType
*actual
= nullptr;
76 bool ok
= ctx
->get_tyctx ()->lookup_type (
77 stmt
.get_init_expr ()->get_mappings ().get_hirid (), &actual
);
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
);
86 CompilePatternLet::Compile (&stmt_pattern
, init
, ty
, rvalue_locus
, ctx
);
89 } // namespace Compile