mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-29 10:35:06 +01:00
For parameters (and in the future, arguments too; generally comma-separated lists) track two node blocks: 1. param_ir: The complete IR. 2. param_refs: Nodes within the IR that are the "root" parameter. param_refs should allow quick counting of the # of parameters, and more efficient comparison of call args with function parameters. param_ir should be necessary to generate the actual signature. In order to construct this, this refactors the node_block_stack into its own class, which is reused in params_stack. These carry references to the underlying SmallVector for lazy modification in order to avoid a dependency cycle with SemanticsIR (also see notes on empty node blocks below). When finalized, the block pair is pushed onto finished_params_stack. That's because node_stack only has space for one thing, and this is two things -- so I'm essentially choosing a trade-off of adding another stack in order to avoid consuming more space in the expectation that most parse nodes have 0 or 1 things to return, and 2 will be very rare. As factored, this currently consolidates most empty node blocks into a single canonical empty node block. This is because I think empty blocks, i.e. `()`, will be very common. In order to achieve this, SemanticsNodeBlockStack does lazy creation. An alternative approach would have been to use 1 node block per parameter. We decided against this in order to reduce the number of vectors being created.
80 lines
3.0 KiB
C++
80 lines
3.0 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 <gmock/gmock.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <forward_list>
|
|
|
|
#include "gmock/gmock.h"
|
|
#include "gtest/gtest.h"
|
|
#include "toolchain/common/yaml_test_helpers.h"
|
|
#include "toolchain/diagnostics/diagnostic_emitter.h"
|
|
#include "toolchain/lexer/tokenized_buffer.h"
|
|
|
|
namespace Carbon::Testing {
|
|
namespace {
|
|
|
|
using ::testing::_;
|
|
using ::testing::AllOf;
|
|
using ::testing::Contains;
|
|
using ::testing::Each;
|
|
using ::testing::ElementsAre;
|
|
using ::testing::IsEmpty;
|
|
using ::testing::MatchesRegex;
|
|
using ::testing::Pair;
|
|
|
|
TEST(SemanticsIRTest, YAML) {
|
|
DiagnosticConsumer& consumer = ConsoleDiagnosticConsumer();
|
|
llvm::Expected<SourceBuffer> source =
|
|
SourceBuffer::CreateFromText("var x: i32 = 0;");
|
|
TokenizedBuffer tokens = TokenizedBuffer::Lex(*source, consumer);
|
|
ParseTree parse_tree =
|
|
ParseTree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
|
|
SemanticsIR builtin_ir = SemanticsIR::MakeBuiltinIR();
|
|
SemanticsIR semantics_ir = SemanticsIR::MakeFromParseTree(
|
|
builtin_ir, tokens, parse_tree, consumer, /*vlog_stream=*/nullptr);
|
|
|
|
std::string print_output;
|
|
llvm::raw_string_ostream print_stream(print_output);
|
|
semantics_ir.Print(print_stream);
|
|
print_stream.flush();
|
|
|
|
// Matches the ID of a node. The numbers may change because of builtin
|
|
// cross-references, so this code is only doing loose structural checks.
|
|
auto node_id = Yaml::Scalar(MatchesRegex("node\\d+"));
|
|
|
|
EXPECT_THAT(
|
|
Yaml::Value::FromText(print_output),
|
|
ElementsAre(Yaml::Mapping(ElementsAre(
|
|
Pair("cross_reference_irs_size", "1"),
|
|
Pair("callables", Yaml::Sequence(IsEmpty())),
|
|
Pair("integer_literals", Yaml::Sequence(ElementsAre("0"))),
|
|
Pair("strings", Yaml::Sequence(ElementsAre("x"))),
|
|
Pair("nodes",
|
|
Yaml::Sequence(AllOf(
|
|
// kind is required, other parts are optional.
|
|
Each(Yaml::Mapping(Contains(Pair("kind", _)))),
|
|
// A 0-arg node.
|
|
Contains(Yaml::Mapping(ElementsAre(
|
|
Pair("kind", "VarStorage"), Pair("type", node_id)))),
|
|
// A 1-arg node.
|
|
Contains(Yaml::Mapping(ElementsAre(
|
|
Pair("kind", "IntegerLiteral"), Pair("arg0", "int0"),
|
|
Pair("type", node_id)))),
|
|
// A 2-arg node.
|
|
Contains(Yaml::Mapping(ElementsAre(
|
|
Pair("kind", "BindName"), Pair("arg0", "str0"),
|
|
Pair("arg1", node_id), Pair("type", node_id))))))),
|
|
// This production has only one node block.
|
|
Pair("node_blocks",
|
|
Yaml::Sequence(ElementsAre(Yaml::Sequence(IsEmpty()),
|
|
Yaml::Sequence(Each(node_id)))))))));
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Testing
|