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