Explorar o código

Starting to think about toolchain semantic analysis -- create a trivial shell for it. (#1038)

Jon Meow %!s(int64=4) %!d(string=hai) anos
pai
achega
b048bb766d

+ 26 - 0
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",
+    ],
+)

+ 15 - 0
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

+ 25 - 0
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_

+ 42 - 0
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 <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include <optional>
+
+#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<SourceBuffer> source_buffer;
+  std::optional<TokenizedBuffer> tokenized_buffer;
+  std::optional<ParseTree> 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