Debian Codegraph @

gcc/rust/checks/errors/borrowck/rust-bir-dump.cc

raw
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-system.h"
20 #include "rust-bir-dump.h"
21 #include "rust-diagnostics.h"
22
23 namespace Rust {
24 namespace BIR {
25
26 constexpr auto indentation = " ";
27
28 std::string
29 get_tyty_name (TyTy::BaseType *tyty)
30 {
31 if (tyty)
32 return tyty->get_name ();
33 return "unknown";
34 }
35
36 template <typename T, typename FN>
37 void
38 print_comma_separated (std::ostream &stream, const std::vector<T> &collection,
39 FN printer)
40 {
41 if (collection.empty ())
42 return;
43 printer (collection[0]);
44 for (auto it = collection.begin () + 1; it != collection.end (); ++it)
45 {
46 stream << ", ";
47 printer (*it);
48 }
49 }
50
51 void
52 renumber_places (const Function &func, std::vector<PlaceId> &place_map)
53 {
54 // Renumbering places to avoid gaps in the place id space.
55 // This is needed to match MIR's shape.
56 PlaceId next_out_id = INVALID_PLACE;
57
58 for (PlaceId in_id = FIRST_VARIABLE_PLACE;
59 in_id.value < func.place_db.size (); ++in_id.value)
60 {
61 const Place &place = func.place_db[in_id];
62 if (place.kind == Place::VARIABLE || place.kind == Place::TEMPORARY)
63 {
64 place_map[in_id.value] = next_out_id;
65 ++next_out_id.value;
66 }
67
68 else
69 place_map[in_id.value] = INVALID_PLACE;
70 }
71 }
72
73 void
74 simplify_cfg (Function &func, IndexVec<BasicBlockId, BasicBlockId> &bb_fold_map)
75 {
76 // The BIR builder can generate many useless basic blocks, which contain only
77 // a goto.
78 // For actual borrow-checking, the folding has little value.
79
80 bool stabilized = false;
81 while (!stabilized)
82 {
83 stabilized = true;
84 // BB0 cannot be folded as it is an entry block.
85 for (BasicBlockId i = {1}; i.value < func.basic_blocks.size (); ++i.value)
86 {
87 const BasicBlock &bb = func.basic_blocks[bb_fold_map[i]];
88 if (bb.statements.empty () && bb.is_goto_terminated ())
89 {
90 auto dst = bb.successors.at (0);
91 if (bb_fold_map[dst] != dst)
92 {
93 rust_error_at (
94 UNKNOWN_LOCATION,
95 "BIR DUMP: Cannot fold CFG, because it contains an "
96 "infinite loop with no executable statements.");
97 rust_inform (UNKNOWN_LOCATION,
98 "Continuing with an unfolded CFG.");
99 // Reverting the fold map to the original state.
100 for (BasicBlockId i = ENTRY_BASIC_BLOCK;
101 i.value < bb_fold_map.size (); ++i.value)
102 {
103 bb_fold_map[i] = i;
104 }
105 stabilized = true;
106 break;
107 }
108 bb_fold_map[i] = dst;
109 stabilized = false;
110 }
111 }
112 }
113 }
114
115 void
116 Dump::go (bool enable_simplify_cfg)
117 {
118 // To avoid mutation of the BIR, we use indirection through bb_fold_map.
119 for (BasicBlockId i = ENTRY_BASIC_BLOCK; i.value < bb_fold_map.size ();
120 ++i.value)
121 {
122 bb_fold_map[i] = i;
123 }
124 for (PlaceId i = INVALID_PLACE; i.value < place_map.size (); ++i.value)
125 {
126 place_map[i] = i;
127 }
128
129 if (enable_simplify_cfg)
130 simplify_cfg (func, bb_fold_map);
131
132 // renumber_places (func, place_map);
133
134 stream << "fn " << name << "(";
135 print_comma_separated (stream, func.arguments, [this] (PlaceId place_id) {
136 stream << "_" << place_map[place_id].value << ": "
137 << get_tyty_name (func.place_db[place_id].tyty);
138 });
139 stream << ") -> " << get_tyty_name (func.place_db[RETURN_VALUE_PLACE].tyty);
140 stream << " {\n";
141
142 // Print locals declaration.
143 visit_scope (ROOT_SCOPE);
144
145 // Print BBs.
146 for (statement_bb = ENTRY_BASIC_BLOCK;
147 statement_bb.value < func.basic_blocks.size (); ++statement_bb.value)
148 {
149 if (bb_fold_map[statement_bb] != statement_bb)
150 continue; // This BB was folded.
151
152 if (func.basic_blocks[statement_bb].statements.empty ()
153 && func.basic_blocks[statement_bb].successors.empty ())
154 continue;
155
156 bb_terminated = false;
157
158 BasicBlock &bb = func.basic_blocks[statement_bb];
159 stream << "\n";
160 stream << indentation << "bb" << bb_fold_map[statement_bb].value
161 << ": {\n";
162 size_t i = 0;
163 for (auto &stmt : bb.statements)
164 {
165 stream << indentation << i++ << indentation;
166 visit (stmt);
167 stream << ";\n";
168 }
169 if (!bb_terminated)
170 stream << indentation << indentation << "goto -> bb"
171 << bb_fold_map[bb.successors.at (0)].value << ";\t\t" << i++
172 << "\n";
173
174 stream << indentation << "}\n";
175 }
176
177 stream << "}\n";
178 }
179 void
180 Dump::visit (const Statement &stmt)
181 {
182 statement_place = stmt.get_place ();
183 switch (stmt.get_kind ())
184 {
185 case Statement::Kind::ASSIGNMENT: {
186 visit_place (stmt.get_place ());
187 stream << " = ";
188 stmt.get_expr ().accept_vis (*this);
189 break;
190 }
191 case Statement::Kind::SWITCH:
192 stream << "switchInt(";
193 visit_move_place (stmt.get_place ());
194 stream << ") -> [";
195 print_comma_separated (stream, func.basic_blocks[statement_bb].successors,
196 [this] (BasicBlockId succ) {
197 stream << "bb" << bb_fold_map[succ].value;
198 });
199 stream << "]";
200 bb_terminated = true;
201 break;
202 case Statement::Kind::RETURN:
203 stream << "return";
204 bb_terminated = true;
205 break;
206 case Statement::Kind::GOTO:
207 stream
208 << "goto -> bb"
209 << bb_fold_map[func.basic_blocks[statement_bb].successors.at (0)].value;
210 bb_terminated = true;
211 break;
212 case Statement::Kind::STORAGE_DEAD:
213 stream << "StorageDead(";
214 visit_place (stmt.get_place ());
215 stream << ")";
216 break;
217 case Statement::Kind::STORAGE_LIVE:
218 stream << "StorageLive(";
219 visit_place (stmt.get_place ());
220 stream << ")";
221 break;
222 case Statement::Kind::USER_TYPE_ASCRIPTION:
223 visit_place (stmt.get_place ());
224 stream << " = ";
225 stream << "UserTypeAscription(";
226 stream << get_tyty_name (func.place_db[stmt.get_place ()].tyty);
227 stream << ")";
228 break;
229 case Statement::Kind::FAKE_READ:
230 stream << "FakeRead(";
231 visit_place (stmt.get_place ());
232 stream << ")";
233 break;
234 default:
235 rust_internal_error_at (UNKNOWN_LOCATION, "Unknown statement kind.");
236 }
237 statement_place = INVALID_PLACE;
238 }
239
240 void
241 Dump::visit_place (PlaceId place_id)
242 {
243 const Place &place = func.place_db[place_id];
244 switch (place.kind)
245 {
246 case Place::TEMPORARY:
247 case Place::VARIABLE:
248 stream << "_" << place_map[place_id].value;
249 break;
250 case Place::DEREF:
251 stream << "(";
252 stream << "*";
253 visit_place (place.path.parent);
254 stream << ")";
255 break;
256 case Place::FIELD:
257 stream << "(";
258 visit_place (place.path.parent);
259 stream << ".";
260 stream << place.variable_or_field_index;
261 stream << ": " << get_tyty_name (place.tyty) << ")";
262 break;
263 case Place::INDEX:
264 stream << "(";
265 visit_place (place.path.parent);
266 stream << "[]";
267 stream << ": " << get_tyty_name (place.tyty) << ")";
268 break;
269 case Place::CONSTANT:
270 stream << "const " << get_tyty_name (place.tyty);
271 break;
272 case Place::INVALID:
273 if (place_id == INVALID_PLACE)
274 stream << "_INVALID";
275 }
276 }
277
278 void
279 Dump::visit_move_place (PlaceId place_id)
280 {
281 const Place &place = func.place_db[place_id];
282 if (place.should_be_moved ())
283 stream << "move ";
284 visit_place (place_id);
285 }
286
287 void
288 Dump::visit (const BorrowExpr &expr)
289 {
290 stream << "&"
291 << "'?" << expr.get_origin () << " ";
292 if (func.place_db.get_loan (expr.get_loan_id ()).mutability
293 == Mutability::Mut)
294 stream << "mut ";
295 visit_place (expr.get_place ());
296 }
297
298 void
299 Dump::visit_lifetime (PlaceId place_id)
300 {}
301
302 void
303 Dump::visit (const InitializerExpr &expr)
304 {
305 stream << "{";
306 print_comma_separated (stream, expr.get_values (), [this] (PlaceId place_id) {
307 visit_move_place (place_id);
308 });
309 stream << "}";
310 }
311
312 void
313 Dump::visit (const CallExpr &expr)
314 {
315 stream << "Call(";
316 auto maybe_fn_type
317 = func.place_db[expr.get_callable ()].tyty->try_as<TyTy::FnType> ();
318 if (maybe_fn_type)
319 stream << maybe_fn_type->get_identifier ();
320 else
321 visit_move_place (expr.get_callable ());
322
323 stream << ")(";
324 print_comma_separated (stream, expr.get_arguments (),
325 [this] (PlaceId place_id) {
326 visit_move_place (place_id);
327 });
328 stream << ") -> [";
329 print_comma_separated (stream, func.basic_blocks[statement_bb].successors,
330 [this] (BasicBlockId succ) {
331 stream << "bb" << bb_fold_map[succ].value;
332 });
333 stream << "]";
334 bb_terminated = true;
335 }
336
337 void
338 Dump::visit (const Operator<1> &expr)
339 {
340 stream << "Operator(";
341 visit_move_place (expr.get_operand<0> ());
342 stream << ")";
343 }
344
345 void
346 Dump::visit (const Operator<2> &expr)
347 {
348 stream << "Operator(";
349 visit_move_place (expr.get_operand<0> ());
350 stream << ", ";
351 visit_move_place (expr.get_operand<1> ());
352 stream << ")";
353 }
354
355 void
356 Dump::visit (const Assignment &expr)
357 {
358 if (func.place_db[expr.get_rhs ()].is_rvalue ())
359 visit_move_place (expr.get_rhs ());
360 else
361 visit_place (expr.get_rhs ());
362 }
363
364 std::ostream &
365 Dump::indent (size_t depth)
366 {
367 for (size_t i = 0; i < depth; ++i)
368 stream << indentation;
369 return stream;
370 }
371
372 void
373 Dump::visit_scope (ScopeId id, size_t depth)
374 {
375 auto scope = func.place_db.get_scope (id);
376 if (scope.locals.empty () && scope.children.empty ())
377 return;
378
379 if (id.value > 1)
380 indent (depth) << "scope " << id.value - 1 << " {\n";
381
382 for (auto &local : scope.locals)
383 {
384 indent (depth + 1) << "let _";
385 stream << place_map[local].value << ": "
386 << get_tyty_name (func.place_db[local].tyty);
387 stream << ";\t";
388
389 stream << "[";
390 print_comma_separated (stream,
391 func.place_db[local].regions.get_regions (),
392 [this] (FreeRegion region_id) {
393 stream << "'?" << region_id.value;
394 });
395 stream << "]\n";
396 }
397 for (auto &child : scope.children)
398 visit_scope (child, (id.value >= 1) ? depth + 1 : depth);
399
400 if (id.value > 1)
401 indent (depth) << "}\n";
402 }
403
404 } // namespace BIR
405 } // namespace Rust