Add formatted textual IR output (#3056)

Add a textual IR format to the toolchain.

The exact details of the format are somewhat arbitrary right now, and I
expect them to change as we refine the semantics IR model, but at the
moment they're somewhat directly following the current structure of the
IR.

Semantics tests currently test both the "raw" format, which shows the
details of the representation, and the textual format, which is somewhat
higher level. We may want to revisit that decision once the textual
format is a bit more stable, and test only the textual format in most of
these tests, but for now it seems prudent to keep both sets of tests.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
This commit is contained in:
Richard Smith
2023-08-10 19:41:39 +00:00
committed by GitHub
co-authored by Chandler Carruth
parent fc5a9541ce
commit 6cbf280a68
140 changed files with 3127 additions and 64 deletions
+13 -7
View File
@@ -5,6 +5,9 @@
#include "toolchain/semantics/semantics_ir.h"
#include "common/check.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/SaveAndRestore.h"
#include "toolchain/common/pretty_stack_trace_function.h"
#include "toolchain/parser/parse_tree_node_location_translator.h"
#include "toolchain/semantics/semantics_builtin_kind.h"
@@ -248,7 +251,8 @@ static auto GetTypePrecedence(SemanticsNodeKind kind) -> int {
}
}
auto SemanticsIR::StringifyType(SemanticsTypeId type_id) -> std::string {
auto SemanticsIR::StringifyType(SemanticsTypeId type_id,
bool in_type_context) const -> std::string {
std::string str;
llvm::raw_string_ostream out(str);
@@ -396,12 +400,14 @@ auto SemanticsIR::StringifyType(SemanticsTypeId type_id) -> std::string {
}
// For `{}` or any tuple type, we've printed a non-type expression, so add a
// conversion to type `type`.
auto outer_node = GetNode(outer_node_id);
if (outer_node.kind() == SemanticsNodeKind::TupleType ||
(outer_node.kind() == SemanticsNodeKind::StructType &&
GetNodeBlock(outer_node.GetAsStructType()).empty())) {
out << " as type";
// conversion to type `type` if it's not implied by the context.
if (!in_type_context) {
auto outer_node = GetNode(outer_node_id);
if (outer_node.kind() == SemanticsNodeKind::TupleType ||
(outer_node.kind() == SemanticsNodeKind::StructType &&
GetNodeBlock(outer_node.GetAsStructType()).empty())) {
out << " as type";
}
}
return str;