generic_binding.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef EXECUTABLE_SEMANTICS_AST_GENERIC_BINDING_H_
  5. #define EXECUTABLE_SEMANTICS_AST_GENERIC_BINDING_H_
  6. #include <map>
  7. #include "common/check.h"
  8. #include "common/ostream.h"
  9. #include "executable_semantics/ast/ast_node.h"
  10. #include "executable_semantics/ast/value_category.h"
  11. namespace Carbon {
  12. class Value;
  13. class Expression;
  14. class ImplBinding;
  15. // TODO: expand the kinds of things that can be deduced parameters.
  16. // For now, only generic parameters are supported.
  17. class GenericBinding : public AstNode {
  18. public:
  19. using ImplementsCarbonValueNode = void;
  20. GenericBinding(SourceLocation source_loc, std::string name,
  21. Nonnull<Expression*> type)
  22. : AstNode(AstNodeKind::GenericBinding, source_loc),
  23. name_(std::move(name)),
  24. type_(type) {}
  25. void Print(llvm::raw_ostream& out) const override;
  26. static auto classof(const AstNode* node) -> bool {
  27. return InheritsFromGenericBinding(node->kind());
  28. }
  29. auto name() const -> const std::string& { return name_; }
  30. auto type() const -> const Expression& { return *type_; }
  31. auto type() -> Expression& { return *type_; }
  32. // The static type of the binding. Cannot be called before typechecking.
  33. auto static_type() const -> const Value& { return **static_type_; }
  34. // Sets the static type of the binding. Can only be called once, during
  35. // typechecking.
  36. void set_static_type(Nonnull<const Value*> type) {
  37. CHECK(!static_type_.has_value());
  38. static_type_ = type;
  39. }
  40. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  41. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  42. return constant_value_;
  43. }
  44. // Sets the value returned by constant_value(). Can only be called once,
  45. // during typechecking.
  46. void set_constant_value(Nonnull<const Value*> value) {
  47. CHECK(!constant_value_.has_value());
  48. constant_value_ = value;
  49. }
  50. // The impl binding associated with this type variable.
  51. auto impl_binding() const -> std::optional<Nonnull<const ImplBinding*>> {
  52. return impl_binding_;
  53. }
  54. // Set the impl binding.
  55. void set_impl_binding(Nonnull<const ImplBinding*> binding) {
  56. CHECK(!impl_binding_.has_value());
  57. impl_binding_ = binding;
  58. }
  59. private:
  60. std::string name_;
  61. Nonnull<Expression*> type_;
  62. std::optional<Nonnull<const Value*>> static_type_;
  63. std::optional<Nonnull<const Value*>> constant_value_;
  64. std::optional<Nonnull<const ImplBinding*>> impl_binding_;
  65. };
  66. using BindingMap =
  67. std::map<Nonnull<const GenericBinding*>, Nonnull<const Value*>>;
  68. // The run-time counterpart of a `GenericBinding`.
  69. //
  70. // Once a generic binding has been declared, it can be used
  71. // in two different ways: as a compile-time constant with a
  72. // symbolic value (such as a `VariableType`), or as a run-time
  73. // variable with a concrete value that is stored on the stack.
  74. // An `ImplBinding` is used in contexts where the second
  75. // interpretation is intended.
  76. class ImplBinding : public AstNode {
  77. public:
  78. using ImplementsCarbonValueNode = void;
  79. ImplBinding(SourceLocation source_loc,
  80. Nonnull<const GenericBinding*> type_var,
  81. Nonnull<const Value*> iface)
  82. : AstNode(AstNodeKind::ImplBinding, source_loc),
  83. type_var_(type_var),
  84. iface_(iface) {}
  85. static auto classof(const AstNode* node) -> bool {
  86. return InheritsFromImplBinding(node->kind());
  87. }
  88. void Print(llvm::raw_ostream& out) const override;
  89. // The binding for the type variable.
  90. auto type_var() const -> Nonnull<const GenericBinding*> { return type_var_; }
  91. // The interface being implemented.
  92. auto interface() const -> Nonnull<const Value*> { return iface_; }
  93. // Required for the the ValueNode interface
  94. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  95. return std::nullopt;
  96. }
  97. // The static type of the impl. Cannot be called before typechecking.
  98. auto static_type() const -> const Value& { return **static_type_; }
  99. // Sets the static type of the impl. Can only be called once, during
  100. // typechecking.
  101. void set_static_type(Nonnull<const Value*> type) {
  102. CHECK(!static_type_.has_value());
  103. static_type_ = type;
  104. }
  105. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  106. private:
  107. Nonnull<const GenericBinding*> type_var_;
  108. Nonnull<const Value*> iface_;
  109. std::optional<Nonnull<const Value*>> static_type_;
  110. };
  111. } // namespace Carbon
  112. #endif // EXECUTABLE_SEMANTICS_AST_GENERIC_BINDING_H_