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