diff --git a/explorer/ast/BUILD b/explorer/ast/BUILD index 97d23e5e598c..1ce82ed6dfd8 100644 --- a/explorer/ast/BUILD +++ b/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", ], diff --git a/explorer/interpreter/address.h b/explorer/ast/address.h similarity index 93% rename from explorer/interpreter/address.h rename to explorer/ast/address.h index af99496ee5be..a7bb6e4a7f81 100644 --- a/explorer/interpreter/address.h +++ b/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 #include #include #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_ diff --git a/explorer/interpreter/element_path.h b/explorer/ast/element_path.h similarity index 95% rename from explorer/interpreter/element_path.h rename to explorer/ast/element_path.h index 26f58213b8db..25988c4aa7f5 100644 --- a/explorer/interpreter/element_path.h +++ b/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 #include @@ -106,4 +106,4 @@ class ElementPath { } // namespace Carbon -#endif // CARBON_EXPLORER_INTERPRETER_ELEMENT_PATH_H_ +#endif // CARBON_EXPLORER_AST_ELEMENT_PATH_H_ diff --git a/explorer/ast/element_test.cpp b/explorer/ast/element_test.cpp index 57f8eb47adcd..dc208c354556 100644 --- a/explorer/ast/element_test.cpp +++ b/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 { diff --git a/explorer/ast/expression.cpp b/explorer/ast/expression.cpp index 14c98866e2bb..7ad9c811ed1f 100644 --- a/explorer/ast/expression.cpp +++ b/explorer/ast/expression.cpp @@ -8,6 +8,7 @@ #include #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(*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; } diff --git a/explorer/interpreter/value.cpp b/explorer/ast/value.cpp similarity index 99% rename from explorer/interpreter/value.cpp rename to explorer/ast/value.cpp index b6a03abe4bd4..ac0e67938a46 100644 --- a/explorer/interpreter/value.cpp +++ b/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 #include @@ -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" diff --git a/explorer/interpreter/value.h b/explorer/ast/value.h similarity index 99% rename from explorer/interpreter/value.h rename to explorer/ast/value.h index 40d837ef5ca1..a709f96429a4 100644 --- a/explorer/interpreter/value.h +++ b/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 #include @@ -11,14 +11,13 @@ #include #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(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_ diff --git a/explorer/interpreter/value_kinds.def b/explorer/ast/value_kinds.def similarity index 100% rename from explorer/interpreter/value_kinds.def rename to explorer/ast/value_kinds.def diff --git a/explorer/interpreter/value_transform.h b/explorer/ast/value_transform.h similarity index 96% rename from explorer/interpreter/value_transform.h rename to explorer/ast/value_transform.h index 716cfa47fa62..0a2488fd63af 100644 --- a/explorer/interpreter/value_transform.h +++ b/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 { } // namespace Carbon -#endif // CARBON_EXPLORER_INTERPRETER_VALUE_TRANSFORM_H_ +#endif // CARBON_EXPLORER_AST_VALUE_TRANSFORM_H_ diff --git a/explorer/interpreter/BUILD b/explorer/interpreter/BUILD index cde9665273d7..277a22b99ec6 100644 --- a/explorer/interpreter/BUILD +++ b/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", diff --git a/explorer/interpreter/README.md b/explorer/interpreter/README.md index b389d049a39d..1292b458f667 100644 --- a/explorer/interpreter/README.md +++ b/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 diff --git a/explorer/interpreter/action.h b/explorer/interpreter/action.h index 21b10b70d957..f02961873bcd 100644 --- a/explorer/interpreter/action.h +++ b/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" diff --git a/explorer/interpreter/action_stack.h b/explorer/interpreter/action_stack.h index c118370ec999..579936959235 100644 --- a/explorer/interpreter/action_stack.h +++ b/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 { diff --git a/explorer/interpreter/builtins.h b/explorer/interpreter/builtins.h index 0cbdcb4ea575..c2fcdf72de4e 100644 --- a/explorer/interpreter/builtins.h +++ b/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 { diff --git a/explorer/interpreter/heap.cpp b/explorer/interpreter/heap.cpp index 3c9b01dfac57..7c8181b4dad8 100644 --- a/explorer/interpreter/heap.cpp +++ b/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" diff --git a/explorer/interpreter/heap.h b/explorer/interpreter/heap.h index ed0e5c3ddbe6..de512aad7b4a 100644 --- a/explorer/interpreter/heap.h +++ b/explorer/interpreter/heap.h @@ -8,11 +8,11 @@ #include #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 { diff --git a/explorer/interpreter/heap_allocation_interface.h b/explorer/interpreter/heap_allocation_interface.h index 60e54d1312f5..0a59bb93c154 100644 --- a/explorer/interpreter/heap_allocation_interface.h +++ b/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 { diff --git a/explorer/interpreter/impl_scope.cpp b/explorer/interpreter/impl_scope.cpp index 6a215d239ab2..3049c5cffefd 100644 --- a/explorer/interpreter/impl_scope.cpp +++ b/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" diff --git a/explorer/interpreter/impl_scope.h b/explorer/interpreter/impl_scope.h index f615e53a5c80..70b6485a1538 100644 --- a/explorer/interpreter/impl_scope.h +++ b/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 { diff --git a/explorer/interpreter/interpreter.cpp b/explorer/interpreter/interpreter.cpp index 26e49b9f636b..8047dbb5cbcc 100644 --- a/explorer/interpreter/interpreter.cpp +++ b/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" diff --git a/explorer/interpreter/interpreter.h b/explorer/interpreter/interpreter.h index ea3b51d763be..88d3d511a34f 100644 --- a/explorer/interpreter/interpreter.h +++ b/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 { diff --git a/explorer/interpreter/matching_impl_set.cpp b/explorer/interpreter/matching_impl_set.cpp index aa6e279a14d9..a84209600e8b 100644 --- a/explorer/interpreter/matching_impl_set.cpp +++ b/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 { diff --git a/explorer/interpreter/matching_impl_set.h b/explorer/interpreter/matching_impl_set.h index c96988383099..9bb4ac970d3b 100644 --- a/explorer/interpreter/matching_impl_set.h +++ b/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 { diff --git a/explorer/interpreter/pattern_analysis.h b/explorer/interpreter/pattern_analysis.h index 9b8bbb84e495..85d6c81ebf33 100644 --- a/explorer/interpreter/pattern_analysis.h +++ b/explorer/interpreter/pattern_analysis.h @@ -8,8 +8,8 @@ #include #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 { diff --git a/explorer/interpreter/stack_fragment.h b/explorer/interpreter/stack_fragment.h index 2c47fa051024..8804ade8e968 100644 --- a/explorer/interpreter/stack_fragment.h +++ b/explorer/interpreter/stack_fragment.h @@ -8,9 +8,9 @@ #include #include +#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 { diff --git a/explorer/interpreter/type_checker.cpp b/explorer/interpreter/type_checker.cpp index 6ece912f6b54..d5294912a0b2 100644 --- a/explorer/interpreter/type_checker.cpp +++ b/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" diff --git a/explorer/interpreter/type_checker.h b/explorer/interpreter/type_checker.h index b3f13d4c8728..5fd11159b9d2 100644 --- a/explorer/interpreter/type_checker.h +++ b/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 {