| 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 "optional.h" |
| 20 |
#include "rust-ast-lower-item.h" |
| 21 |
#include "rust-ast-lower-stmt.h" |
| 22 |
#include "rust-ast-lower-type.h" |
| 23 |
#include "rust-ast-lower-expr.h" |
| 24 |
#include "rust-ast-lower-pattern.h" |
| 25 |
|
| 26 |
namespace Rust { |
| 27 |
namespace HIR { |
| 28 |
|
| 29 |
HIR::Stmt * |
| 30 |
ASTLoweringStmt::translate (AST::Stmt *stmt, bool *terminated) |
| 31 |
{ |
| 32 |
ASTLoweringStmt resolver; |
| 33 |
stmt->accept_vis (resolver); |
| 34 |
|
| 35 |
if (!resolver.translated) |
| 36 |
return nullptr; |
| 37 |
|
| 38 |
*terminated = resolver.terminated; |
| 39 |
resolver.mappings.insert_location ( |
| 40 |
resolver.translated->get_mappings ().get_hirid (), |
| 41 |
resolver.translated->get_locus ()); |
| 42 |
resolver.mappings.insert_hir_stmt (resolver.translated); |
| 43 |
|
| 44 |
return resolver.translated; |
| 45 |
} |
| 46 |
|
| 47 |
void |
| 48 |
ASTLoweringStmt::visit (AST::ExprStmt &stmt) |
| 49 |
{ |
| 50 |
HIR::Expr *expr = ASTLoweringExpr::translate (stmt.get_expr (), &terminated); |
| 51 |
|
| 52 |
auto crate_num = mappings.get_current_crate (); |
| 53 |
Analysis::NodeMapping mapping (crate_num, stmt.get_node_id (), |
| 54 |
mappings.get_next_hir_id (crate_num), |
| 55 |
UNKNOWN_LOCAL_DEFID); |
| 56 |
translated |
| 57 |
= new HIR::ExprStmt (mapping, std::unique_ptr<HIR::Expr> (expr), |
| 58 |
stmt.get_locus (), !stmt.is_semicolon_followed ()); |
| 59 |
} |
| 60 |
|
| 61 |
void |
| 62 |
ASTLoweringStmt::visit (AST::ConstantItem &constant) |
| 63 |
{ |
| 64 |
translated = ASTLoweringItem::translate (constant); |
| 65 |
} |
| 66 |
|
| 67 |
void |
| 68 |
ASTLoweringStmt::visit (AST::LetStmt &stmt) |
| 69 |
{ |
| 70 |
HIR::Pattern *variables |
| 71 |
= ASTLoweringPattern::translate (stmt.get_pattern (), true); |
| 72 |
|
| 73 |
tl::optional<std::unique_ptr<Type>> type = tl::nullopt; |
| 74 |
|
| 75 |
if (stmt.has_type ()) |
| 76 |
type |
| 77 |
= std::unique_ptr<Type> (ASTLoweringType::translate (stmt.get_type ())); |
| 78 |
|
| 79 |
tl::optional<std::unique_ptr<HIR::Expr>> init_expr = tl::nullopt; |
| 80 |
tl::optional<std::unique_ptr<HIR::Expr>> else_expr = tl::nullopt; |
| 81 |
|
| 82 |
if (stmt.has_init_expr ()) |
| 83 |
init_expr = std::unique_ptr<HIR::Expr> ( |
| 84 |
ASTLoweringExpr::translate (stmt.get_init_expr ())); |
| 85 |
|
| 86 |
if (stmt.has_else_expr ()) |
| 87 |
else_expr = std::unique_ptr<HIR::Expr> ( |
| 88 |
ASTLoweringExpr::translate (stmt.get_else_expr ())); |
| 89 |
|
| 90 |
auto crate_num = mappings.get_current_crate (); |
| 91 |
Analysis::NodeMapping mapping (crate_num, stmt.get_node_id (), |
| 92 |
mappings.get_next_hir_id (crate_num), |
| 93 |
UNKNOWN_LOCAL_DEFID); |
| 94 |
translated |
| 95 |
= new HIR::LetStmt (mapping, std::unique_ptr<HIR::Pattern> (variables), |
| 96 |
std::move (init_expr), std::move (else_expr), |
| 97 |
std::move (type), stmt.get_outer_attrs (), |
| 98 |
stmt.get_locus ()); |
| 99 |
} |
| 100 |
|
| 101 |
void |
| 102 |
ASTLoweringStmt::visit (AST::TupleStruct &struct_decl) |
| 103 |
{ |
| 104 |
translated = ASTLoweringItem::translate (struct_decl); |
| 105 |
} |
| 106 |
|
| 107 |
void |
| 108 |
ASTLoweringStmt::visit (AST::StructStruct &struct_decl) |
| 109 |
{ |
| 110 |
translated = ASTLoweringItem::translate (struct_decl); |
| 111 |
} |
| 112 |
|
| 113 |
void |
| 114 |
ASTLoweringStmt::visit (AST::Union &union_decl) |
| 115 |
{ |
| 116 |
translated = ASTLoweringItem::translate (union_decl); |
| 117 |
} |
| 118 |
|
| 119 |
void |
| 120 |
ASTLoweringStmt::visit (AST::Enum &enum_decl) |
| 121 |
{ |
| 122 |
translated = ASTLoweringItem::translate (enum_decl); |
| 123 |
} |
| 124 |
|
| 125 |
void |
| 126 |
ASTLoweringStmt::visit (AST::EmptyStmt &empty) |
| 127 |
{ |
| 128 |
auto crate_num = mappings.get_current_crate (); |
| 129 |
Analysis::NodeMapping mapping (crate_num, empty.get_node_id (), |
| 130 |
mappings.get_next_hir_id (crate_num), |
| 131 |
mappings.get_next_localdef_id (crate_num)); |
| 132 |
|
| 133 |
translated = new HIR::EmptyStmt (mapping, empty.get_locus ()); |
| 134 |
} |
| 135 |
|
| 136 |
void |
| 137 |
ASTLoweringStmt::visit (AST::Function &function) |
| 138 |
{ |
| 139 |
translated = ASTLoweringItem::translate (function); |
| 140 |
} |
| 141 |
|
| 142 |
void |
| 143 |
ASTLoweringStmt::visit (AST::ExternBlock &extern_block) |
| 144 |
{ |
| 145 |
translated = lower_extern_block (extern_block); |
| 146 |
} |
| 147 |
|
| 148 |
void |
| 149 |
ASTLoweringStmt::visit (AST::MacroRulesDefinition &def) |
| 150 |
{ |
| 151 |
lower_macro_definition (def); |
| 152 |
} |
| 153 |
|
| 154 |
void |
| 155 |
ASTLoweringStmt::visit (AST::Trait &trait) |
| 156 |
{ |
| 157 |
translated = ASTLoweringItem::translate (trait); |
| 158 |
} |
| 159 |
|
| 160 |
void |
| 161 |
ASTLoweringStmt::visit (AST::InherentImpl &impl_block) |
| 162 |
{ |
| 163 |
translated = ASTLoweringItem::translate (impl_block); |
| 164 |
} |
| 165 |
|
| 166 |
void |
| 167 |
ASTLoweringStmt::visit (AST::TraitImpl &impl_block) |
| 168 |
{ |
| 169 |
translated = ASTLoweringItem::translate (impl_block); |
| 170 |
} |
| 171 |
|
| 172 |
void |
| 173 |
ASTLoweringStmt::visit (AST::StaticItem &var) |
| 174 |
{ |
| 175 |
translated = ASTLoweringItem::translate (var); |
| 176 |
} |
| 177 |
|
| 178 |
} // namespace HIR |
| 179 |
} // namespace Rust |