mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-22 23:42:38 +00:00
This rewrites semantics towards a more pure instruction model, in pursuit of the simple instruction-style output. I think I can get this approach to type-check as it goes along, but obviously this change doesn't prove that yet. I'm separating it out because it's a large rewrite of the semantics structure, tossing out a lot of what was there before. But I think it does help towards several requests, like setting up a clear path for consolidating duplicate identifiers and making the node style more standardized. I expect to need to pass multiple args to function calls, that'd probably be storing vectors of args similar to how I'm showing identifiers and integer literals stored. This removes the semantics namespace because (a) it was getting annoying writing the `::` everywhere, and (b) I think the leaning with Carbon is to avoid namespaces (@chandlerc asked not to put SemanticsIR/SemanticsFactory in a namespace, which is the crux of the issue). But, it's still necessary to avoid name conflicts so I just prefix everything with "Semantics" (still a lot of typing, but no `::`).
84 lines
2.6 KiB
C++
84 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_FACTORY_H_
|
|
#define CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_FACTORY_H_
|
|
|
|
#include "common/check.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 TokenizedBuffer& tokens, const ParseTree& parse_tree)
|
|
-> SemanticsIR;
|
|
|
|
private:
|
|
struct TraversalStackEntry {
|
|
ParseTree::Node parse_node;
|
|
llvm::Optional<SemanticsNodeId> result_id;
|
|
};
|
|
|
|
explicit SemanticsIRFactory(const TokenizedBuffer& tokens,
|
|
const ParseTree& parse_tree)
|
|
: tokens_(&tokens), semantics_(parse_tree) {}
|
|
|
|
auto Build() -> void;
|
|
|
|
auto Push(ParseTree::Node parse_node) -> void {
|
|
node_stack_.push_back({parse_node, llvm::None});
|
|
}
|
|
|
|
auto Push(ParseTree::Node parse_node, SemanticsNode node) -> void {
|
|
auto node_id = semantics_.AddNode(node);
|
|
node_stack_.push_back({parse_node, node_id});
|
|
}
|
|
|
|
auto Pop(ParseNodeKind pop_parse_kind) -> void {
|
|
auto back = node_stack_.back();
|
|
auto parse_kind = parse_tree().node_kind(back.parse_node);
|
|
CARBON_CHECK(parse_kind == pop_parse_kind)
|
|
<< "Expected " << pop_parse_kind << ", found " << parse_kind;
|
|
CARBON_CHECK(!back.result_id) << "Expected no result ID on " << parse_kind;
|
|
node_stack_.pop_back();
|
|
}
|
|
|
|
auto PopWithResult() -> SemanticsNodeId {
|
|
auto back = node_stack_.back();
|
|
auto node_id = *back.result_id;
|
|
node_stack_.pop_back();
|
|
return node_id;
|
|
}
|
|
|
|
auto PopWithResult(ParseNodeKind pop_parse_kind) -> SemanticsNodeId {
|
|
auto back = node_stack_.back();
|
|
auto parse_kind = parse_tree().node_kind(back.parse_node);
|
|
auto node_id = *back.result_id;
|
|
CARBON_CHECK(parse_kind == pop_parse_kind)
|
|
<< "Expected " << pop_parse_kind << ", found " << parse_kind;
|
|
node_stack_.pop_back();
|
|
return node_id;
|
|
}
|
|
|
|
// Convenience accessor.
|
|
auto parse_tree() -> const ParseTree& { return *semantics_.parse_tree_; }
|
|
|
|
// Tokens for getting data on literals.
|
|
const TokenizedBuffer* tokens_;
|
|
|
|
// The SemanticsIR being constructed.
|
|
SemanticsIR semantics_;
|
|
|
|
// The stack during Build. Will contain file-level parse nodes on return.
|
|
llvm::SmallVector<TraversalStackEntry> node_stack_;
|
|
};
|
|
|
|
} // namespace Carbon
|
|
|
|
#endif // CARBON_TOOLCHAIN_SEMANTICS_SEMANTICS_IR_FACTORY_H_
|