Line data Source code
1 : // Copyright (C) 2020-2026 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 : #include "rust-compile-type.h"
23 : #include "rust-compile-var-decl.h"
24 :
25 : namespace Rust {
26 : namespace Compile {
27 :
28 21664 : CompileStmt::CompileStmt (Context *ctx)
29 21664 : : HIRCompileBase (ctx), translated (nullptr)
30 21664 : {}
31 :
32 : tree
33 21664 : CompileStmt::Compile (HIR::Stmt *stmt, Context *ctx)
34 : {
35 21664 : CompileStmt compiler (ctx);
36 21664 : stmt->accept_vis (compiler);
37 21664 : return compiler.translated;
38 21664 : }
39 :
40 : void
41 9114 : CompileStmt::visit (HIR::ExprStmt &stmt)
42 : {
43 9114 : translated = CompileExpr::Compile (stmt.get_expr (), ctx);
44 9114 : }
45 :
46 : void
47 12133 : CompileStmt::visit (HIR::LetStmt &stmt)
48 : {
49 12133 : HIR::Pattern &stmt_pattern = stmt.get_pattern ();
50 12133 : HirId stmt_id = stmt_pattern.get_mappings ().get_hirid ();
51 :
52 12133 : TyTy::BaseType *ty = nullptr;
53 12133 : if (!ctx->get_tyctx ()->lookup_type (stmt_id, &ty))
54 : {
55 : // FIXME this should be an assertion instead
56 0 : rust_fatal_error (stmt.get_locus (),
57 : "failed to lookup variable declaration type");
58 1116 : return;
59 : }
60 :
61 12133 : rust_debug_loc (stmt.get_locus (), " -> LetStmt %s",
62 12133 : ty->as_string ().c_str ());
63 :
64 : // setup var decl nodes
65 12133 : fncontext fnctx = ctx->peek_fn ();
66 12133 : tree fndecl = fnctx.fndecl;
67 12133 : tree translated_type = TyTyResolveCompile::compile (ctx, ty);
68 12133 : CompileVarDecl::compile (fndecl, translated_type, &stmt_pattern, ctx);
69 :
70 : // nothing to do
71 12133 : if (!stmt.has_init_expr ())
72 : return;
73 :
74 11017 : tree init = CompileExpr::Compile (stmt.get_init_expr (), ctx);
75 : // FIXME use error_mark_node, check that CompileExpr returns error_mark_node
76 : // on failure and make this an assertion
77 11017 : if (init == nullptr)
78 : return;
79 :
80 11017 : TyTy::BaseType *actual = nullptr;
81 11017 : bool ok = ctx->get_tyctx ()->lookup_type (
82 11017 : stmt.get_init_expr ().get_mappings ().get_hirid (), &actual);
83 11017 : rust_assert (ok);
84 :
85 11017 : location_t lvalue_locus = stmt.get_pattern ().get_locus ();
86 11017 : location_t rvalue_locus = stmt.get_init_expr ().get_locus ();
87 11017 : TyTy::BaseType *expected = ty;
88 11017 : init = coercion_site (stmt.get_mappings ().get_hirid (), init, actual,
89 : expected, lvalue_locus, rvalue_locus);
90 :
91 11017 : CompilePatternLet::Compile (&stmt_pattern, init, ty, rvalue_locus, ctx);
92 : }
93 :
94 : } // namespace Compile
95 : } // namespace Rust
|