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