1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of GCC.
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
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
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/>.
19 #ifndef RUST_TYCHECK_DUMP
20 #define RUST_TYCHECK_DUMP
22 #include "rust-hir-type-check-base.h"
23 #include "rust-hir-full.h"
28 class TypeResolverDump
: private TypeCheckBase
, private HIR::HIRFullVisitorBase
30 using HIR::HIRFullVisitorBase::visit
;
33 static void go (HIR::Crate
&crate
, std::ofstream
&out
)
35 TypeResolverDump dumper
;
36 for (auto &item
: crate
.items
)
38 item
->accept_vis (dumper
);
45 void visit (HIR::StructStruct
&struct_decl
) override
47 dump
+= indent () + "struct " + type_string (struct_decl
.get_mappings ())
51 void visit (HIR::Union
&union_decl
) override
54 += indent () + "union " + type_string (union_decl
.get_mappings ()) + "\n";
57 void visit (HIR::TupleStruct
&struct_decl
) override
59 dump
+= indent () + "struct" + type_string (struct_decl
.get_mappings ())
63 void visit (HIR::ImplBlock
&impl_block
) override
65 dump
+= indent () + "impl "
66 + type_string (impl_block
.get_type ()->get_mappings ()) + " {\n";
69 for (auto &impl_item
: impl_block
.get_impl_items ())
71 impl_item
->accept_vis (*this);
76 dump
+= indent () + "}\n";
79 void visit (HIR::ConstantItem
&constant
) override
81 dump
+= indent () + "constant " + constant
.get_identifier () + ":"
82 + type_string (constant
.get_mappings ()) + " = ";
83 constant
.get_expr ()->accept_vis (*this);
87 void visit (HIR::Function
&function
) override
89 dump
+= indent () + "fn " + function
.get_function_name () + " "
90 + type_string (function
.get_mappings ()) + "\n";
91 dump
+= indent () + "{\n";
93 HIR::BlockExpr
*function_body
= function
.get_definition ().get ();
94 function_body
->accept_vis (*this);
96 dump
+= indent () + "}\n";
99 void visit (HIR::BlockExpr
&expr
) override
104 for (auto &s
: expr
.get_statements ())
107 s
->accept_vis (*this);
111 if (expr
.has_expr ())
114 expr
.expr
->accept_vis (*this);
122 void visit (HIR::UnsafeBlockExpr
&expr
) override
125 expr
.get_block_expr ()->accept_vis (*this);
128 void visit (HIR::LetStmt
&stmt
) override
130 dump
+= "let " + stmt
.get_pattern ()->as_string () + ":"
131 + type_string (stmt
.get_pattern ()->get_pattern_mappings ());
132 if (stmt
.has_init_expr ())
135 stmt
.get_init_expr ()->accept_vis (*this);
139 void visit (HIR::ExprStmtWithBlock
&stmt
) override
141 stmt
.get_expr ()->accept_vis (*this);
144 void visit (HIR::ExprStmtWithoutBlock
&stmt
) override
146 stmt
.get_expr ()->accept_vis (*this);
149 void visit (HIR::AssignmentExpr
&expr
) override
151 expr
.get_lhs ()->accept_vis (*this);
153 expr
.get_rhs ()->accept_vis (*this);
156 void visit (HIR::LiteralExpr
&expr
) override
158 dump
+= expr
.get_literal ().as_string () + ":"
159 + type_string (expr
.get_mappings ());
162 void visit (HIR::ArrayExpr
&expr
) override
164 dump
+= type_string (expr
.get_mappings ()) + ":[";
166 HIR::ArrayElems
*elements
= expr
.get_internal_elements ();
167 elements
->accept_vis (*this);
172 void visit (HIR::ArrayElemsValues
&elems
) override
174 for (auto &elem
: elems
.get_values ())
176 elem
->accept_vis (*this);
181 void visit (HIR::GroupedExpr
&expr
) override
183 HIR::Expr
*paren_expr
= expr
.get_expr_in_parens ().get ();
185 paren_expr
->accept_vis (*this);
189 void visit (HIR::PathInExpression
&expr
) override
191 dump
+= type_string (expr
.get_mappings ());
194 void visit (HIR::StructExprStructFields
&expr
) override
196 dump
+= "ctor: " + type_string (expr
.get_mappings ());
200 std::string
type_string (const Analysis::NodeMapping
&mappings
)
202 TyTy::BaseType
*lookup
= nullptr;
203 if (!context
->lookup_type (mappings
.get_hirid (), &lookup
))
206 std::string buf
= "[";
207 for (auto &ref
: lookup
->get_combined_refs ())
209 buf
+= std::to_string (ref
);
214 std::string repr
= lookup
->as_string ();
215 return "<" + repr
+ " HIRID: " + std::to_string (mappings
.get_hirid ())
216 + " RF:" + std::to_string (lookup
->get_ref ()) + " TF:"
217 + std::to_string (lookup
->get_ty_ref ()) + +" - " + buf
+ ">";
220 std::string
indent ()
223 for (size_t i
= 0; i
< indentation_level
; ++i
)
230 TypeResolverDump () : TypeCheckBase (), indentation_level (0) {}
233 size_t indentation_level
;
236 } // namespace Resolver
239 #endif // RUST_TYCHECK_DUMP