Files
carbon-lang/toolchain/semantics/semantics_ir.cpp
T
Jon Ross-Perkins 4113ec8a67 Semantics (#1213)
Working on toolchain semantics:

- SemanticsIR is set up as a container for the semantic tree.
- SemanticsIRFactory builds the tree, with separate transformations for each ParseNodeKind.
- ParseSubtreeConsumer is a helper for transforming a ParseTree::Node's children, managing size/nodes to prevent errors.
- The nodes subdirectory contains SemanticIR nodes.
- MetaNode is used to represent nodes which have "sub-classes": Statements, Declarations, and Expressions.
- MetaNodeBlock is used to represent nodes which exist together in a block with name lookup: Statements and Declarations (not Expressions).

This is traversing children first in order to address the RPO format of ParseTree. This means that when lists are formed, they're reversed to be in code-order (`FixReverseOrdering`).

This is still very much incomplete -- the main intent at present is to demonstrate structure.
2022-06-15 12:40:07 -07:00

127 lines
3.6 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"
#include "toolchain/semantics/nodes/expression_statement.h"
namespace Carbon {
void SemanticsIR::Print(llvm::raw_ostream& out, ParseTree::Node node) const {
out << parse_tree_->GetNodeText(node);
}
void SemanticsIR::Print(llvm::raw_ostream& out,
Semantics::Declaration decl) const {
switch (decl.kind()) {
case Semantics::DeclarationKind::Function:
Print(out, declarations_.Get<Semantics::Function>(decl));
return;
case Semantics::DeclarationKind::Invalid:
CARBON_FATAL() << "Invalid declaration type";
}
}
void SemanticsIR::Print(llvm::raw_ostream& out,
Semantics::Expression expr) const {
switch (expr.kind()) {
case Semantics::ExpressionKind::InfixOperator:
Print(out, expressions_.Get<Semantics::InfixOperator>(expr));
return;
case Semantics::ExpressionKind::Literal:
Print(out, expressions_.Get<Semantics::Literal>(expr));
return;
case Semantics::ExpressionKind::Invalid:
CARBON_FATAL() << "Invalid expression type";
}
}
void SemanticsIR::Print(llvm::raw_ostream& out,
Semantics::Statement stmt) const {
switch (stmt.kind()) {
case Semantics::StatementKind::ExpressionStatement:
Print(out, statements_.Get<Semantics::ExpressionStatement>(stmt));
return;
case Semantics::StatementKind::Return:
Print(out, statements_.Get<Semantics::Return>(stmt));
return;
case Semantics::StatementKind::Invalid:
CARBON_FATAL() << "Invalid expression type";
}
out << ";";
}
void SemanticsIR::Print(llvm::raw_ostream& out,
const Semantics::DeclaredName& name) const {
Print(out, name.node());
}
void SemanticsIR::Print(llvm::raw_ostream& out,
const Semantics::ExpressionStatement& expr) const {
Print(out, expr.expression());
}
void SemanticsIR::Print(llvm::raw_ostream& out,
const Semantics::Function& function) const {
out << "fn ";
Print(out, function.name());
out << "(";
llvm::ListSeparator sep;
for (const auto& param : function.params()) {
out << sep;
Print(out, param);
}
out << ")";
if (function.return_expr()) {
out << " -> ";
Print(out, *function.return_expr());
}
Print(out, function.body());
}
void SemanticsIR::Print(llvm::raw_ostream& out,
const Semantics::InfixOperator& op) const {
Print(out, op.lhs());
out << " ";
Print(out, op.node());
out << " ";
Print(out, op.rhs());
}
void SemanticsIR::Print(llvm::raw_ostream& out,
const Semantics::Literal& literal) const {
Print(out, literal.node());
}
void SemanticsIR::Print(llvm::raw_ostream& out,
const Semantics::PatternBinding& binding) const {
Print(out, binding.name());
out << ": ";
Print(out, binding.type());
}
void SemanticsIR::Print(llvm::raw_ostream& out,
const Semantics::Return& ret) const {
out << "return";
if (ret.expression()) {
out << " ";
Print(out, *ret.expression());
}
}
void SemanticsIR::Print(llvm::raw_ostream& out,
const Semantics::StatementBlock& block) const {
out << " { ";
for (const auto& statement : block.nodes()) {
Print(out, statement);
out << "; ";
}
out << "}";
}
} // namespace Carbon