Branch data Line data Source code
1 : : // Copyright (C) 2020-2025 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 : 21208 : CompileStmt::CompileStmt (Context *ctx)
29 : 21208 : : HIRCompileBase (ctx), translated (nullptr)
30 : 21208 : {}
31 : :
32 : : tree
33 : 21208 : CompileStmt::Compile (HIR::Stmt *stmt, Context *ctx)
34 : : {
35 : 21208 : CompileStmt compiler (ctx);
36 : 21208 : stmt->accept_vis (compiler);
37 : 21208 : return compiler.translated;
38 : 21208 : }
39 : :
40 : : void
41 : 8927 : CompileStmt::visit (HIR::ExprStmt &stmt)
42 : : {
43 : 8927 : translated = CompileExpr::Compile (stmt.get_expr (), ctx);
44 : 8927 : }
45 : :
46 : : void
47 : 11884 : CompileStmt::visit (HIR::LetStmt &stmt)
48 : : {
49 : 11884 : HIR::Pattern &stmt_pattern = stmt.get_pattern ();
50 : 11884 : HirId stmt_id = stmt_pattern.get_mappings ().get_hirid ();
51 : :
52 : 11884 : TyTy::BaseType *ty = nullptr;
53 : 11884 : 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 : 1114 : return;
59 : : }
60 : :
61 : 11884 : rust_debug_loc (stmt.get_locus (), " -> LetStmt %s",
62 : 11884 : ty->as_string ().c_str ());
63 : :
64 : : // setup var decl nodes
65 : 11884 : fncontext fnctx = ctx->peek_fn ();
66 : 11884 : tree fndecl = fnctx.fndecl;
67 : 11884 : tree translated_type = TyTyResolveCompile::compile (ctx, ty);
68 : 11884 : CompileVarDecl::compile (fndecl, translated_type, &stmt_pattern, ctx);
69 : :
70 : : // nothing to do
71 : 11884 : if (!stmt.has_init_expr ())
72 : : return;
73 : :
74 : 10770 : 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 : 10770 : if (init == nullptr)
78 : : return;
79 : :
80 : 10770 : TyTy::BaseType *actual = nullptr;
81 : 10770 : bool ok = ctx->get_tyctx ()->lookup_type (
82 : 10770 : stmt.get_init_expr ().get_mappings ().get_hirid (), &actual);
83 : 10770 : rust_assert (ok);
84 : :
85 : 10770 : location_t lvalue_locus = stmt.get_pattern ().get_locus ();
86 : 10770 : location_t rvalue_locus = stmt.get_init_expr ().get_locus ();
87 : 10770 : TyTy::BaseType *expected = ty;
88 : 10770 : init = coercion_site (stmt.get_mappings ().get_hirid (), init, actual,
89 : : expected, lvalue_locus, rvalue_locus);
90 : :
91 : 10770 : CompilePatternLet::Compile (&stmt_pattern, init, ty, rvalue_locus, ctx);
92 : : }
93 : :
94 : : } // namespace Compile
95 : : } // namespace Rust
|