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 : #include "rust-compile-drop.h"
25 : #include "rust-compile-drop-builder.h"
26 : #include "rust-bir-drop-analysis.h"
27 :
28 : namespace Rust {
29 : namespace Compile {
30 :
31 24516 : CompileStmt::CompileStmt (Context *ctx)
32 24516 : : HIRCompileBase (ctx), translated (nullptr)
33 24516 : {}
34 :
35 : tree
36 24516 : CompileStmt::Compile (HIR::Stmt *stmt, Context *ctx)
37 : {
38 24516 : CompileStmt compiler (ctx);
39 24516 : stmt->accept_vis (compiler);
40 24516 : return compiler.translated;
41 24516 : }
42 :
43 : void
44 11158 : CompileStmt::visit (HIR::ExprStmt &stmt)
45 : {
46 11158 : translated = CompileExpr::Compile (stmt.get_expr (), ctx);
47 11158 : }
48 :
49 : void
50 12920 : CompileStmt::visit (HIR::LetStmt &stmt)
51 : {
52 12920 : HIR::Pattern &stmt_pattern = stmt.get_pattern ();
53 12920 : HirId stmt_id = stmt_pattern.get_mappings ().get_hirid ();
54 :
55 12920 : TyTy::BaseType *ty = nullptr;
56 12920 : if (!ctx->get_tyctx ()->lookup_type (stmt_id, &ty))
57 : {
58 : // FIXME this should be an assertion instead
59 0 : rust_fatal_error (stmt.get_locus (),
60 : "failed to lookup variable declaration type");
61 1127 : return;
62 : }
63 :
64 12920 : rust_debug_loc (stmt.get_locus (), " -> LetStmt %s",
65 : ty->as_string ().c_str ());
66 :
67 : // setup var decl nodes
68 12920 : fncontext fnctx = ctx->peek_fn ();
69 12920 : tree fndecl = fnctx.fndecl;
70 12920 : tree translated_type = TyTyResolveCompile::compile (ctx, ty);
71 12920 : CompileVarDecl::compile (fndecl, translated_type, &stmt_pattern, ctx);
72 :
73 12920 : if (stmt_pattern.get_pattern_type () == HIR::Pattern::IDENTIFIER
74 12920 : && CompileDrop (ctx).type_has_drop_impl (ty))
75 55 : DropBuilder (*ctx).maybe_create_drop_flag (stmt_id, stmt.get_locus (),
76 : false);
77 :
78 : // nothing to do
79 12920 : if (!stmt.has_init_expr ())
80 : return;
81 :
82 11793 : tree init = CompileExpr::Compile (stmt.get_init_expr (), ctx);
83 : // FIXME use error_mark_node, check that CompileExpr returns error_mark_node
84 : // on failure and make this an assertion
85 11793 : if (init == nullptr)
86 : return;
87 :
88 11793 : TyTy::BaseType *actual = nullptr;
89 11793 : bool ok = ctx->get_tyctx ()->lookup_type (
90 11793 : stmt.get_init_expr ().get_mappings ().get_hirid (), &actual);
91 11793 : rust_assert (ok);
92 :
93 11793 : location_t lvalue_locus = stmt.get_pattern ().get_locus ();
94 11793 : location_t rvalue_locus = stmt.get_init_expr ().get_locus ();
95 11793 : TyTy::BaseType *expected = ty;
96 11793 : init = coercion_site (stmt.get_mappings ().get_hirid (), init, actual,
97 : expected, lvalue_locus, rvalue_locus);
98 :
99 11793 : CompilePatternLet::Compile (&stmt_pattern, init, ty, rvalue_locus, ctx);
100 :
101 11793 : HirId source = UNKNOWN_HIRID;
102 23586 : if (BIR::DropAnalysis::get ().lookup_move_source (
103 11793 : stmt.get_init_expr ().get_mappings ().get_hirid (), &source))
104 : {
105 5 : tree clear
106 5 : = DropBuilder (*ctx).drop_flag_assignment (source, false, rvalue_locus);
107 5 : if (clear != nullptr)
108 1 : ctx->add_statement (clear);
109 : }
110 : }
111 :
112 : } // namespace Compile
113 : } // namespace Rust
|