From b048bb766d91ce5965d395a8209a3acb2721dd31 Mon Sep 17 00:00:00 2001 From: Jon Meow <46229924+jonmeow@users.noreply.github.com> Date: Tue, 25 Jan 2022 11:06:16 -0800 Subject: [PATCH] Starting to think about toolchain semantic analysis -- create a trivial shell for it. (#1038) --- toolchain/semantics/BUILD | 26 ++++++++++++++++ toolchain/semantics/semantics.cpp | 15 +++++++++ toolchain/semantics/semantics.h | 25 +++++++++++++++ toolchain/semantics/semantics_test.cpp | 42 ++++++++++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 toolchain/semantics/BUILD create mode 100644 toolchain/semantics/semantics.cpp create mode 100644 toolchain/semantics/semantics.h create mode 100644 toolchain/semantics/semantics_test.cpp diff --git a/toolchain/semantics/BUILD b/toolchain/semantics/BUILD new file mode 100644 index 000000000000..acbdfbdfd61b --- /dev/null +++ b/toolchain/semantics/BUILD @@ -0,0 +1,26 @@ +# 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 + +package(default_visibility = ["//visibility:public"]) + +cc_library( + name = "semantics", + srcs = ["semantics.cpp"], + hdrs = ["semantics.h"], + deps = ["//toolchain/parser:parse_tree"], +) + +cc_test( + name = "semantics_test", + size = "small", + srcs = ["semantics_test.cpp"], + deps = [ + ":semantics", + "//toolchain/diagnostics:diagnostic_emitter", + "//toolchain/lexer:tokenized_buffer", + "//toolchain/parser:parse_tree", + "//toolchain/source:source_buffer", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/toolchain/semantics/semantics.cpp b/toolchain/semantics/semantics.cpp new file mode 100644 index 000000000000..31989108962c --- /dev/null +++ b/toolchain/semantics/semantics.cpp @@ -0,0 +1,15 @@ +// 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.h" + +namespace Carbon { + +Semantics Semantics::Analyze(const ParseTree& parse_tree, + DiagnosticConsumer& consumer) { + Semantics semantics; + return semantics; +} + +} // namespace Carbon diff --git a/toolchain/semantics/semantics.h b/toolchain/semantics/semantics.h new file mode 100644 index 000000000000..37639b6893b4 --- /dev/null +++ b/toolchain/semantics/semantics.h @@ -0,0 +1,25 @@ +// 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 TOOLCHAIN_SEMANTICS_SEMANTICS_H_ +#define TOOLCHAIN_SEMANTICS_SEMANTICS_H_ + +#include "toolchain/parser/parse_tree.h" + +namespace Carbon { + +// Provides semantic analysis on a ParseTree. +class Semantics { + public: + // Analyzes a parse tree and returns the constructed semantic information. + static auto Analyze(const ParseTree& parse_tree, DiagnosticConsumer& consumer) + -> Semantics; + + private: + Semantics() = default; +}; + +} // namespace Carbon + +#endif // TOOLCHAIN_SEMANTICS_SEMANTICS_H_ diff --git a/toolchain/semantics/semantics_test.cpp b/toolchain/semantics/semantics_test.cpp new file mode 100644 index 000000000000..ba50066d8f6b --- /dev/null +++ b/toolchain/semantics/semantics_test.cpp @@ -0,0 +1,42 @@ +// 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.h" + +#include +#include + +#include + +#include "toolchain/diagnostics/diagnostic_emitter.h" +#include "toolchain/lexer/tokenized_buffer.h" +#include "toolchain/parser/parse_tree.h" +#include "toolchain/source/source_buffer.h" + +namespace Carbon::Testing { +namespace { + +class ParseTreeTest : public ::testing::Test { + protected: + auto Analyze(llvm::Twine t) -> Semantics { + source_buffer.emplace(SourceBuffer::CreateFromText(t.str())); + tokenized_buffer = TokenizedBuffer::Lex(*source_buffer, consumer); + parse_tree = ParseTree::Parse(*tokenized_buffer, consumer); + return Semantics::Analyze(*parse_tree, consumer); + } + + std::optional source_buffer; + std::optional tokenized_buffer; + std::optional parse_tree; + DiagnosticConsumer& consumer = ConsoleDiagnosticConsumer(); +}; + +TEST_F(ParseTreeTest, Empty) { + // TODO: Validate the returned Semantics object. + Analyze(""); + ASSERT_FALSE(parse_tree->HasErrors()); +} + +} // namespace +} // namespace Carbon::Testing