Просмотр исходного кода

Move `Value`, `Address`, and `ElementPath` to ast/. (#2659)

These are used by the AST in lots of ways, and this resolves various layering issues.

This means that `AllocationId` also lives in ast/, but is managed by interpreter/. A better layering here would be desirable, but this seems good enough for the time being.
Richard Smith 3 лет назад
Родитель
Сommit
bfe5c36bfc

+ 8 - 1
explorer/ast/BUILD

@@ -43,18 +43,26 @@ cc_library(
         "expression.cpp",
         "pattern.cpp",
         "statement.cpp",
+        "value.cpp",
     ],
     hdrs = [
+        "address.h",
         "ast.h",
         "bindings.h",
         "declaration.h",
         "element.h",
+        "element_path.h",
         "expression.h",
         "impl_binding.h",
         "pattern.h",
         "return_term.h",
         "statement.h",
+        "value.h",
         "value_node.h",
+        "value_transform.h",
+    ],
+    textual_hdrs = [
+        "value_kinds.def",
     ],
     deps = [
         ":ast_node",
@@ -136,7 +144,6 @@ cc_test(
         ":paren_contents",
         "//common:gtest_main",
         "//explorer/common:arena",
-        "//explorer/interpreter:action_and_value",
         "@com_google_googletest//:gtest",
         "@llvm-project//llvm:Support",
     ],

+ 4 - 4
explorer/interpreter/address.h → explorer/ast/address.h

@@ -2,15 +2,15 @@
 // Exceptions. See /LICENSE for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-#ifndef CARBON_EXPLORER_INTERPRETER_ADDRESS_H_
-#define CARBON_EXPLORER_INTERPRETER_ADDRESS_H_
+#ifndef CARBON_EXPLORER_AST_ADDRESS_H_
+#define CARBON_EXPLORER_AST_ADDRESS_H_
 
 #include <cstdint>
 #include <string>
 #include <vector>
 
 #include "common/ostream.h"
-#include "explorer/interpreter/element_path.h"
+#include "explorer/ast/element_path.h"
 #include "llvm/Support/Compiler.h"
 
 namespace Carbon {
@@ -84,4 +84,4 @@ class Address {
 
 }  // namespace Carbon
 
-#endif  // CARBON_EXPLORER_INTERPRETER_ADDRESS_H_
+#endif  // CARBON_EXPLORER_AST_ADDRESS_H_

+ 3 - 3
explorer/interpreter/element_path.h → explorer/ast/element_path.h

@@ -2,8 +2,8 @@
 // Exceptions. See /LICENSE for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-#ifndef CARBON_EXPLORER_INTERPRETER_ELEMENT_PATH_H_
-#define CARBON_EXPLORER_INTERPRETER_ELEMENT_PATH_H_
+#ifndef CARBON_EXPLORER_AST_ELEMENT_PATH_H_
+#define CARBON_EXPLORER_AST_ELEMENT_PATH_H_
 
 #include <optional>
 #include <string>
@@ -106,4 +106,4 @@ class ElementPath {
 
 }  // namespace Carbon
 
-#endif  // CARBON_EXPLORER_INTERPRETER_ELEMENT_PATH_H_
+#endif  // CARBON_EXPLORER_AST_ELEMENT_PATH_H_

+ 1 - 1
explorer/ast/element_test.cpp

@@ -12,8 +12,8 @@
 #include "explorer/ast/bindings.h"
 #include "explorer/ast/declaration.h"
 #include "explorer/ast/expression.h"
+#include "explorer/ast/value.h"
 #include "explorer/common/arena.h"
-#include "explorer/interpreter/value.h"
 #include "llvm/Support/Casting.h"
 
 namespace Carbon::Testing {

+ 5 - 5
explorer/ast/expression.cpp

@@ -8,6 +8,7 @@
 #include <optional>
 
 #include "explorer/ast/pattern.h"
+#include "explorer/ast/value.h"
 #include "explorer/common/arena.h"
 #include "explorer/common/error_builders.h"
 #include "llvm/ADT/StringExtras.h"
@@ -340,9 +341,11 @@ void Expression::PrintID(llvm::raw_ostream& out) const {
     case ExpressionKind::ContinuationTypeLiteral:
       out << "Continuation";
       break;
+    case ExpressionKind::FunctionTypeLiteral:
+    case ExpressionKind::StructLiteral:
+    case ExpressionKind::ArrayTypeLiteral:
     case ExpressionKind::ValueLiteral:
-      // TODO: For layering reasons, we can't print out the value from here.
-      out << "ValueLiteral";
+      out << cast<ConstantValueLiteral>(*this).constant_value();
       break;
     case ExpressionKind::IndexExpression:
     case ExpressionKind::SimpleMemberAccessExpression:
@@ -352,14 +355,11 @@ void Expression::PrintID(llvm::raw_ostream& out) const {
     case ExpressionKind::WhereExpression:
     case ExpressionKind::BuiltinConvertExpression:
     case ExpressionKind::TupleLiteral:
-    case ExpressionKind::StructLiteral:
     case ExpressionKind::StructTypeLiteral:
     case ExpressionKind::CallExpression:
     case ExpressionKind::OperatorExpression:
     case ExpressionKind::IntrinsicExpression:
     case ExpressionKind::UnimplementedExpression:
-    case ExpressionKind::FunctionTypeLiteral:
-    case ExpressionKind::ArrayTypeLiteral:
       out << "...";
       break;
   }

+ 2 - 3
explorer/interpreter/value.cpp → explorer/ast/value.cpp

@@ -2,7 +2,7 @@
 // Exceptions. See /LICENSE for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-#include "explorer/interpreter/value.h"
+#include "explorer/ast/value.h"
 
 #include <algorithm>
 #include <optional>
@@ -12,10 +12,9 @@
 #include "common/error.h"
 #include "explorer/ast/declaration.h"
 #include "explorer/ast/element.h"
+#include "explorer/ast/element_path.h"
 #include "explorer/common/arena.h"
 #include "explorer/common/error_builders.h"
-#include "explorer/interpreter/action.h"
-#include "explorer/interpreter/element_path.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/Casting.h"

+ 7 - 8
explorer/interpreter/value.h → explorer/ast/value.h

@@ -2,8 +2,8 @@
 // Exceptions. See /LICENSE for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-#ifndef CARBON_EXPLORER_INTERPRETER_VALUE_H_
-#define CARBON_EXPLORER_INTERPRETER_VALUE_H_
+#ifndef CARBON_EXPLORER_AST_VALUE_H_
+#define CARBON_EXPLORER_AST_VALUE_H_
 
 #include <optional>
 #include <string>
@@ -11,14 +11,13 @@
 #include <vector>
 
 #include "common/ostream.h"
+#include "explorer/ast/address.h"
 #include "explorer/ast/bindings.h"
 #include "explorer/ast/declaration.h"
 #include "explorer/ast/element.h"
+#include "explorer/ast/element_path.h"
 #include "explorer/ast/statement.h"
 #include "explorer/common/nonnull.h"
-#include "explorer/interpreter/address.h"
-#include "explorer/interpreter/element_path.h"
-#include "explorer/interpreter/stack.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/Support/Compiler.h"
 
@@ -53,7 +52,7 @@ class Value {
  public:
   enum class Kind {
 #define CARBON_VALUE_KIND(kind) kind,
-#include "explorer/interpreter/value_kinds.def"
+#include "explorer/ast/value_kinds.def"
   };
 
   Value(const Value&) = delete;
@@ -1633,10 +1632,10 @@ auto Value::Visit(F f) const -> R {
 #define CARBON_VALUE_KIND(kind) \
   case Kind::kind:              \
     return f(static_cast<const kind*>(this));
-#include "explorer/interpreter/value_kinds.def"
+#include "explorer/ast/value_kinds.def"
   }
 }
 
 }  // namespace Carbon
 
-#endif  // CARBON_EXPLORER_INTERPRETER_VALUE_H_
+#endif  // CARBON_EXPLORER_AST_VALUE_H_

+ 0 - 0
explorer/interpreter/value_kinds.def → explorer/ast/value_kinds.def


+ 4 - 4
explorer/interpreter/value_transform.h → explorer/ast/value_transform.h

@@ -2,10 +2,10 @@
 // Exceptions. See /LICENSE for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 
-#ifndef CARBON_EXPLORER_INTERPRETER_VALUE_TRANSFORM_H_
-#define CARBON_EXPLORER_INTERPRETER_VALUE_TRANSFORM_H_
+#ifndef CARBON_EXPLORER_AST_VALUE_TRANSFORM_H_
+#define CARBON_EXPLORER_AST_VALUE_TRANSFORM_H_
 
-#include "explorer/interpreter/value.h"
+#include "explorer/ast/value.h"
 
 namespace Carbon {
 
@@ -192,4 +192,4 @@ class ValueTransform : public TransformBase<Derived> {
 
 }  // namespace Carbon
 
-#endif  // CARBON_EXPLORER_INTERPRETER_VALUE_TRANSFORM_H_
+#endif  // CARBON_EXPLORER_AST_VALUE_TRANSFORM_H_

+ 8 - 43
explorer/interpreter/BUILD

@@ -4,28 +4,16 @@
 
 package(default_visibility = ["//explorer:__pkg__"])
 
-# These currently have to be a single build rule because of a dependency cycle
-# in printing.
 cc_library(
-    name = "action_and_value",
+    name = "action",
     srcs = [
         "action.cpp",
-        "value.cpp",
     ],
     hdrs = [
         "action.h",
-        "value.h",
-        "value_transform.h",
     ],
-    textual_hdrs = [
-        "value_kinds.def",
-    ],
-    # Exposed to resolve `member_test` dependencies.
-    visibility = ["//explorer/ast:__pkg__"],
     deps = [
-        ":address",
         ":dictionary",
-        ":element_path",
         ":heap_allocation_interface",
         ":stack",
         "//common:check",
@@ -50,7 +38,7 @@ cc_library(
         "stack_fragment.h",
     ],
     deps = [
-        ":action_and_value",
+        ":action",
         ":stack",
         "//common:check",
         "//common:error",
@@ -60,16 +48,6 @@ cc_library(
     ],
 )
 
-cc_library(
-    name = "address",
-    hdrs = ["address.h"],
-    deps = [
-        ":element_path",
-        "//common:ostream",
-        "@llvm-project//llvm:Support",
-    ],
-)
-
 cc_library(
     name = "dictionary",
     hdrs = ["dictionary.h"],
@@ -99,26 +77,15 @@ cc_library(
     ],
 )
 
-cc_library(
-    name = "element_path",
-    hdrs = ["element_path.h"],
-    deps = [
-        "//common:ostream",
-        "//explorer/ast",
-        "//explorer/ast:static_scope",
-        "@llvm-project//llvm:Support",
-    ],
-)
-
 cc_library(
     name = "heap",
     srcs = ["heap.cpp"],
     hdrs = ["heap.h"],
     deps = [
-        ":action_and_value",
-        ":address",
+        ":action",
         ":heap_allocation_interface",
         "//common:ostream",
+        "//explorer/ast",
         "//explorer/common:error_builders",
         "//explorer/common:nonnull",
         "//explorer/common:source_location",
@@ -130,7 +97,7 @@ cc_library(
     name = "heap_allocation_interface",
     hdrs = ["heap_allocation_interface.h"],
     deps = [
-        ":address",
+        "//explorer/ast",
         "//explorer/common:arena",
         "//explorer/common:nonnull",
     ],
@@ -145,9 +112,8 @@ cc_library(
         "interpreter.h",
     ],
     deps = [
-        ":action_and_value",
+        ":action",
         ":action_stack",
-        ":address",
         ":heap",
         ":stack",
         ":trace_stream",
@@ -180,7 +146,6 @@ cc_library(
     srcs = ["resolve_names.cpp"],
     hdrs = ["resolve_names.h"],
     deps = [
-        ":action_and_value",
         "//common:check",
         "//explorer/ast",
         "//explorer/ast:static_scope",
@@ -204,7 +169,7 @@ cc_library(
         "pattern_analysis.h",
     ],
     deps = [
-        ":action_and_value",
+        ":action",
         "//common:error",
         "//explorer/ast",
         "//explorer/common:nonnull",
@@ -241,7 +206,7 @@ cc_library(
         "type_checker.h",
     ],
     deps = [
-        ":action_and_value",
+        ":action",
         ":dictionary",
         ":interpreter",
         ":pattern_analysis",

+ 9 - 9
explorer/interpreter/README.md

@@ -32,18 +32,18 @@ example by pushing a new `Action` onto it. When an `Action` is done executing,
 it can optionally produce a value as its result, which is made available to the
 `Action` below it on the stack.
 
-Carbon values are represented as [`Value`](value.h) objects, both at compile
-time and at run time. Note that in Carbon, a type is a kind of value, so types
-are represented as `Value`s. More subtly, `Value` can also represent information
-that isn't a true Carbon value, but needs to be propagated through channels that
-use `Value`. Most notably, certain kinds of `Value` are used to represent the
-result of "evaluating" a `Pattern`, which evaluates all the subexpressions
-nested within it, while preserving the structure of the non-expression parts for
-use in pattern matching.
+Carbon values are represented as [`Value`](../ast/value.h) objects, both at
+compile time and at run time. Note that in Carbon, a type is a kind of value, so
+types are represented as `Value`s. More subtly, `Value` can also represent
+information that isn't a true Carbon value, but needs to be propagated through
+channels that use `Value`. Most notably, certain kinds of `Value` are used to
+represent the result of "evaluating" a `Pattern`, which evaluates all the
+subexpressions nested within it, while preserving the structure of the
+non-expression parts for use in pattern matching.
 
 `Value`s are always immutable. The abstract machine's mutable memory is
 represented using the [`Heap`](heap.h) class, which is essentially a mapping of
-[`Address`es](address.h) to `Value`s.
+[`Address`es](../ast/address.h) to `Value`s.
 
 ### Example
 

+ 1 - 1
explorer/interpreter/action.h

@@ -14,10 +14,10 @@
 #include "explorer/ast/expression.h"
 #include "explorer/ast/pattern.h"
 #include "explorer/ast/statement.h"
+#include "explorer/ast/value.h"
 #include "explorer/interpreter/dictionary.h"
 #include "explorer/interpreter/heap_allocation_interface.h"
 #include "explorer/interpreter/stack.h"
-#include "explorer/interpreter/value.h"
 #include "llvm/ADT/MapVector.h"
 #include "llvm/Support/Compiler.h"
 

+ 1 - 1
explorer/interpreter/action_stack.h

@@ -11,9 +11,9 @@
 
 #include "common/ostream.h"
 #include "explorer/ast/statement.h"
+#include "explorer/ast/value.h"
 #include "explorer/interpreter/action.h"
 #include "explorer/interpreter/stack_fragment.h"
-#include "explorer/interpreter/value.h"
 
 namespace Carbon {
 

+ 1 - 1
explorer/interpreter/builtins.h

@@ -12,9 +12,9 @@
 #include "common/error.h"
 #include "explorer/ast/declaration.h"
 #include "explorer/ast/expression.h"
+#include "explorer/ast/value.h"
 #include "explorer/common/nonnull.h"
 #include "explorer/common/source_location.h"
-#include "explorer/interpreter/value.h"
 
 namespace Carbon {
 

+ 1 - 1
explorer/interpreter/heap.cpp

@@ -4,8 +4,8 @@
 
 #include "explorer/interpreter/heap.h"
 
+#include "explorer/ast/value.h"
 #include "explorer/common/error_builders.h"
-#include "explorer/interpreter/value.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/Error.h"
 

+ 2 - 2
explorer/interpreter/heap.h

@@ -8,11 +8,11 @@
 #include <vector>
 
 #include "common/ostream.h"
+#include "explorer/ast/address.h"
+#include "explorer/ast/value.h"
 #include "explorer/common/nonnull.h"
 #include "explorer/common/source_location.h"
-#include "explorer/interpreter/address.h"
 #include "explorer/interpreter/heap_allocation_interface.h"
-#include "explorer/interpreter/value.h"
 
 namespace Carbon {
 

+ 1 - 1
explorer/interpreter/heap_allocation_interface.h

@@ -5,9 +5,9 @@
 #ifndef CARBON_EXPLORER_INTERPRETER_HEAP_ALLOCATION_INTERFACE_H_
 #define CARBON_EXPLORER_INTERPRETER_HEAP_ALLOCATION_INTERFACE_H_
 
+#include "explorer/ast/address.h"
 #include "explorer/common/arena.h"
 #include "explorer/common/nonnull.h"
-#include "explorer/interpreter/address.h"
 
 namespace Carbon {
 

+ 1 - 1
explorer/interpreter/impl_scope.cpp

@@ -4,8 +4,8 @@
 
 #include "explorer/interpreter/impl_scope.h"
 
+#include "explorer/ast/value.h"
 #include "explorer/interpreter/type_checker.h"
-#include "explorer/interpreter/value.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/Casting.h"
 

+ 1 - 1
explorer/interpreter/impl_scope.h

@@ -6,7 +6,7 @@
 #define CARBON_EXPLORER_INTERPRETER_IMPL_SCOPE_H_
 
 #include "explorer/ast/declaration.h"
-#include "explorer/interpreter/value.h"
+#include "explorer/ast/value.h"
 
 namespace Carbon {
 

+ 1 - 1
explorer/interpreter/interpreter.cpp

@@ -20,6 +20,7 @@
 #include "explorer/ast/declaration.h"
 #include "explorer/ast/element.h"
 #include "explorer/ast/expression.h"
+#include "explorer/ast/value.h"
 #include "explorer/common/arena.h"
 #include "explorer/common/error_builders.h"
 #include "explorer/common/source_location.h"
@@ -27,7 +28,6 @@
 #include "explorer/interpreter/action_stack.h"
 #include "explorer/interpreter/stack.h"
 #include "explorer/interpreter/stack_fragment.h"
-#include "explorer/interpreter/value.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/Error.h"

+ 1 - 1
explorer/interpreter/interpreter.h

@@ -14,10 +14,10 @@
 #include "explorer/ast/declaration.h"
 #include "explorer/ast/expression.h"
 #include "explorer/ast/pattern.h"
+#include "explorer/ast/value.h"
 #include "explorer/interpreter/action.h"
 #include "explorer/interpreter/heap.h"
 #include "explorer/interpreter/trace_stream.h"
-#include "explorer/interpreter/value.h"
 #include "llvm/ADT/ArrayRef.h"
 
 namespace Carbon {

+ 1 - 1
explorer/interpreter/matching_impl_set.cpp

@@ -5,11 +5,11 @@
 #include "common/check.h"
 #include "common/error.h"
 #include "explorer/ast/declaration.h"
+#include "explorer/ast/value.h"
 #include "explorer/common/error_builders.h"
 #include "explorer/common/nonnull.h"
 #include "explorer/common/source_location.h"
 #include "explorer/interpreter/type_checker.h"
-#include "explorer/interpreter/value.h"
 
 namespace Carbon {
 

+ 1 - 1
explorer/interpreter/matching_impl_set.h

@@ -9,9 +9,9 @@
 
 #include "common/ostream.h"
 #include "explorer/ast/declaration.h"
+#include "explorer/ast/value.h"
 #include "explorer/common/nonnull.h"
 #include "explorer/interpreter/impl_scope.h"
-#include "explorer/interpreter/value.h"
 #include "llvm/ADT/DenseMap.h"
 
 namespace Carbon {

+ 1 - 1
explorer/interpreter/pattern_analysis.h

@@ -8,8 +8,8 @@
 #include <vector>
 
 #include "explorer/ast/pattern.h"
+#include "explorer/ast/value.h"
 #include "explorer/common/nonnull.h"
-#include "explorer/interpreter/value.h"
 #include "llvm/ADT/PointerUnion.h"
 
 namespace Carbon {

+ 1 - 1
explorer/interpreter/stack_fragment.h

@@ -8,9 +8,9 @@
 #include <memory>
 #include <vector>
 
+#include "explorer/ast/value.h"
 #include "explorer/interpreter/action.h"
 #include "explorer/interpreter/stack.h"
-#include "explorer/interpreter/value.h"
 #include "llvm/Support/raw_ostream.h"
 
 namespace Carbon {

+ 2 - 2
explorer/interpreter/type_checker.cpp

@@ -21,6 +21,8 @@
 #include "common/ostream.h"
 #include "explorer/ast/declaration.h"
 #include "explorer/ast/expression.h"
+#include "explorer/ast/value.h"
+#include "explorer/ast/value_transform.h"
 #include "explorer/common/arena.h"
 #include "explorer/common/error_builders.h"
 #include "explorer/common/nonnull.h"
@@ -28,8 +30,6 @@
 #include "explorer/interpreter/impl_scope.h"
 #include "explorer/interpreter/interpreter.h"
 #include "explorer/interpreter/pattern_analysis.h"
-#include "explorer/interpreter/value.h"
-#include "explorer/interpreter/value_transform.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/ScopeExit.h"

+ 1 - 1
explorer/interpreter/type_checker.h

@@ -16,6 +16,7 @@
 #include "explorer/ast/ast.h"
 #include "explorer/ast/expression.h"
 #include "explorer/ast/statement.h"
+#include "explorer/ast/value.h"
 #include "explorer/common/nonnull.h"
 #include "explorer/interpreter/builtins.h"
 #include "explorer/interpreter/dictionary.h"
@@ -23,7 +24,6 @@
 #include "explorer/interpreter/interpreter.h"
 #include "explorer/interpreter/matching_impl_set.h"
 #include "explorer/interpreter/trace_stream.h"
-#include "explorer/interpreter/value.h"
 
 namespace Carbon {