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

55 lines
2.1 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_FACTORY_H_
#define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_FACTORY_H_
#include "toolchain/parser/parse_tree.h"
#include "toolchain/semantics/semantics_ir.h"
namespace Carbon {
// The main semantic analysis entry.
class SemanticsIRFactory {
public:
// Builds the SemanticsIR without doing any substantial semantic analysis.
static auto Build(const ParseTree& parse_tree) -> SemanticsIR;
private:
explicit SemanticsIRFactory(const ParseTree& parse_tree)
: semantics_(parse_tree) {}
void Build();
// Requires that a node have no children, to emphasize why the subtree isn't
// otherwise checked.
void RequireNodeEmpty(ParseTree::Node node);
// Each of these takes a parse tree node and does a transformation based on
// its type. These functions are per ParseNodeKind.
auto TransformCodeBlock(ParseTree::Node node) -> Semantics::StatementBlock;
auto TransformDeclaredName(ParseTree::Node node) -> Semantics::DeclaredName;
auto TransformExpression(ParseTree::Node node) -> Semantics::Expression;
auto TransformExpressionStatement(ParseTree::Node node)
-> Semantics::Statement;
auto TransformFunctionDeclaration(ParseTree::Node node)
-> std::tuple<llvm::StringRef, Semantics::Declaration>;
auto TransformInfixOperator(ParseTree::Node node) -> Semantics::InfixOperator;
auto TransformParameterList(ParseTree::Node node)
-> llvm::SmallVector<Semantics::PatternBinding, 0>;
auto TransformPatternBinding(ParseTree::Node node)
-> Semantics::PatternBinding;
auto TransformReturnType(ParseTree::Node node) -> Semantics::Expression;
auto TransformReturnStatement(ParseTree::Node node) -> Semantics::Statement;
// Convenience accessor.
auto parse_tree() -> const ParseTree& { return *semantics_.parse_tree_; }
SemanticsIR semantics_;
};
} // namespace Carbon
#endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_FACTORY_H_