impl_binding.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/pattern.h"
  11. #include "executable_semantics/ast/value_category.h"
  12. namespace Carbon {
  13. class Value;
  14. class Expression;
  15. class ImplBinding;
  16. // The run-time counterpart of a `GenericBinding`.
  17. //
  18. // Once a generic binding has been declared, it can be used
  19. // in two different ways: as a compile-time constant with a
  20. // symbolic value (such as a `VariableType`), or as a run-time
  21. // variable with a concrete value that is stored on the stack.
  22. // An `ImplBinding` is used in contexts where the second
  23. // interpretation is intended.
  24. class ImplBinding : public AstNode {
  25. public:
  26. using ImplementsCarbonValueNode = void;
  27. ImplBinding(SourceLocation source_loc,
  28. Nonnull<const GenericBinding*> type_var,
  29. Nonnull<const Value*> iface)
  30. : AstNode(AstNodeKind::ImplBinding, source_loc),
  31. type_var_(type_var),
  32. iface_(iface) {}
  33. static auto classof(const AstNode* node) -> bool {
  34. return InheritsFromImplBinding(node->kind());
  35. }
  36. void Print(llvm::raw_ostream& out) const override;
  37. void PrintID(llvm::raw_ostream& out) const override;
  38. // The binding for the type variable.
  39. auto type_var() const -> Nonnull<const GenericBinding*> { return type_var_; }
  40. // The interface being implemented.
  41. auto interface() const -> Nonnull<const Value*> { return iface_; }
  42. // Required for the the ValueNode interface
  43. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  44. return std::nullopt;
  45. }
  46. auto symbolic_identity() const -> std::optional<Nonnull<const Value*>> {
  47. return std::nullopt;
  48. }
  49. // The static type of the impl. Cannot be called before typechecking.
  50. auto static_type() const -> const Value& { return **static_type_; }
  51. // Sets the static type of the impl. Can only be called once, during
  52. // typechecking.
  53. void set_static_type(Nonnull<const Value*> type) {
  54. CHECK(!static_type_.has_value());
  55. static_type_ = type;
  56. }
  57. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  58. private:
  59. Nonnull<const GenericBinding*> type_var_;
  60. Nonnull<const Value*> iface_;
  61. std::optional<Nonnull<const Value*>> static_type_;
  62. };
  63. } // namespace Carbon
  64. #endif // EXECUTABLE_SEMANTICS_AST_GENERIC_BINDING_H_