mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 06:51:05 +01:00
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.
This commit is contained in:
@@ -7,22 +7,120 @@
|
||||
#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::Block::Add(llvm::StringRef name, Node named_entity) {
|
||||
ordering_.push_back(named_entity);
|
||||
name_lookup_.insert({name, named_entity});
|
||||
void SemanticsIR::Print(llvm::raw_ostream& out, ParseTree::Node node) const {
|
||||
out << parse_tree_->GetNodeText(node);
|
||||
}
|
||||
|
||||
auto SemanticsIR::AddFunction(Block& block, ParseTree::Node decl_node,
|
||||
ParseTree::Node name_node)
|
||||
-> Semantics::Function& {
|
||||
int32_t index = functions_.size();
|
||||
functions_.push_back(Semantics::Function(decl_node, name_node));
|
||||
block.Add(parse_tree_->GetNodeText(name_node),
|
||||
Node(Node::Kind::Function, index));
|
||||
return functions_[index];
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user