Files
carbon-lang/toolchain/semantics/semantics_ir.cpp
T
Jon Ross-Perkins 80570e1a5b Modify the driver to support using for semantics IR testing (#2222)
This unifies `dump-tokens` and `dump-parse-tree` so that we don't keep writing basically the same code repeatedly.

I'll need to modify the output of SemanticsIR::Print more, and may soon unify printing multiple IRs this way (particularly including the builtins SemanticsIR) but this is intended to offer a starting point.

I may eventually try to unify lit.cfg.py files, but I was thinking about whether that works in various contexts we may run in and eventually decided copying the driver/testdata/lit.cfg.py file would be the easiest solution.
2022-09-28 17:40:57 -07:00

45 lines
1.4 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/semantics/semantics_ir.h"
#include "common/check.h"
#include "llvm/Support/FormatVariadic.h"
#include "toolchain/lexer/tokenized_buffer.h"
namespace Carbon {
auto SemanticsIR::Print(llvm::raw_ostream& out) const -> void {
for (const auto& node_ref : root_block()) {
Print(out, node_ref);
out << "\n";
}
}
auto SemanticsIR::Print(llvm::raw_ostream& out,
Semantics::NodeRef node_ref) const -> void {
switch (node_ref.kind()) {
case Semantics::NodeKind::BinaryOperator:
nodes_.Get<Semantics::BinaryOperator>(node_ref).Print(out);
return;
case Semantics::NodeKind::Function:
nodes_.Get<Semantics::Function>(node_ref).Print(
out, [&](Semantics::NodeRef other) { Print(out, other); });
return;
case Semantics::NodeKind::IntegerLiteral:
nodes_.Get<Semantics::IntegerLiteral>(node_ref).Print(out);
return;
case Semantics::NodeKind::Return:
nodes_.Get<Semantics::Return>(node_ref).Print(out);
return;
case Semantics::NodeKind::SetName:
nodes_.Get<Semantics::SetName>(node_ref).Print(out);
return;
case Semantics::NodeKind::Invalid:
CARBON_FATAL() << "Invalid NodeRef kind";
}
}
} // namespace Carbon