Files
carbon-lang/toolchain/semantics/semantics_ir.h
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

73 lines
2.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
#ifndef CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_
#define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_
#include "llvm/ADT/SmallVector.h"
#include "toolchain/parser/parse_tree.h"
#include "toolchain/semantics/meta_node_block.h"
#include "toolchain/semantics/nodes/expression_statement.h"
#include "toolchain/semantics/nodes/function.h"
#include "toolchain/semantics/nodes/infix_operator.h"
#include "toolchain/semantics/nodes/literal.h"
#include "toolchain/semantics/nodes/pattern_binding.h"
#include "toolchain/semantics/nodes/return.h"
namespace Carbon::Testing {
class SemanticsIRForTest;
} // namespace Carbon::Testing
namespace Carbon {
// Provides semantic analysis on a ParseTree.
class SemanticsIR {
public:
// File-level declarations.
auto root_block() const -> const Semantics::DeclarationBlock& {
return *root_block_;
}
// Debug printer for the parse tree.
void Print(llvm::raw_ostream& out, ParseTree::Node node) const;
// Debug printers for meta nodes.
void Print(llvm::raw_ostream& out, Semantics::Declaration decl) const;
void Print(llvm::raw_ostream& out, Semantics::Expression expr) const;
void Print(llvm::raw_ostream& out, Semantics::Statement stmt) const;
// Debug printers for other nodes.
void Print(llvm::raw_ostream& out, const Semantics::DeclaredName& name) const;
void Print(llvm::raw_ostream& out,
const Semantics::ExpressionStatement& expr) const;
void Print(llvm::raw_ostream& out, const Semantics::Function& function) const;
void Print(llvm::raw_ostream& out, const Semantics::InfixOperator& op) const;
void Print(llvm::raw_ostream& out, const Semantics::Literal& literal) const;
void Print(llvm::raw_ostream& out,
const Semantics::PatternBinding& binding) const;
void Print(llvm::raw_ostream& out, const Semantics::Return& ret) const;
void Print(llvm::raw_ostream& out,
const Semantics::StatementBlock& block) const;
private:
friend class SemanticsIRFactory;
friend class Testing::SemanticsIRForTest;
explicit SemanticsIR(const ParseTree& parse_tree)
: parse_tree_(&parse_tree) {}
Semantics::DeclarationStore declarations_;
Semantics::ExpressionStore expressions_;
Semantics::StatementStore statements_;
// The file-level block. Only assigned after initialization is complete.
llvm::Optional<Semantics::DeclarationBlock> root_block_;
const ParseTree* parse_tree_;
};
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_H_