impl_binding.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 CARBON_EXPLORER_AST_IMPL_BINDING_H_
  5. #define CARBON_EXPLORER_AST_IMPL_BINDING_H_
  6. #include <map>
  7. #include "common/check.h"
  8. #include "common/ostream.h"
  9. #include "explorer/ast/ast_node.h"
  10. #include "explorer/ast/pattern.h"
  11. #include "explorer/ast/value_category.h"
  12. namespace Carbon {
  13. class Value;
  14. class Expression;
  15. class ImplBinding;
  16. // `ImplBinding` plays the role of the parameter for passing witness
  17. // tables to a generic. However, unlike regular parameters
  18. // (`BindingPattern`) there is no explicit syntax that corresponds to
  19. // an `ImplBinding`, so they are not created during parsing. Instances
  20. // of `ImplBinding` are created during type checking, when processing
  21. // a type parameter (a `GenericBinding`), or an `is` requirement in
  22. // a `where` clause.
  23. class ImplBinding : public AstNode {
  24. public:
  25. using ImplementsCarbonValueNode = void;
  26. ImplBinding(SourceLocation source_loc,
  27. Nonnull<const GenericBinding*> type_var,
  28. std::optional<Nonnull<const Value*>> iface)
  29. : AstNode(AstNodeKind::ImplBinding, source_loc),
  30. type_var_(type_var),
  31. iface_(iface) {}
  32. static auto classof(const AstNode* node) -> bool {
  33. return InheritsFromImplBinding(node->kind());
  34. }
  35. void Print(llvm::raw_ostream& out) const override;
  36. void PrintID(llvm::raw_ostream& out) const override;
  37. // The binding for the type variable.
  38. auto type_var() const -> Nonnull<const GenericBinding*> { return type_var_; }
  39. // The constraint being implemented.
  40. // TODO: Rename this to `constraint`.
  41. auto interface() const -> Nonnull<const Value*> {
  42. CARBON_CHECK(iface_) << "interface has not been set yet";
  43. return *iface_;
  44. }
  45. // Set the interface being implemented, if not set by the constructor. Should
  46. // only be called by typechecking.
  47. void set_interface(Nonnull<const Value*> iface) {
  48. CARBON_CHECK(!iface_) << "interface set twice";
  49. iface_ = iface;
  50. }
  51. // Required for the ValueNode interface
  52. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  53. return std::nullopt;
  54. }
  55. auto symbolic_identity() const -> std::optional<Nonnull<const Value*>> {
  56. return symbolic_identity_;
  57. }
  58. void set_symbolic_identity(Nonnull<const Value*> value) {
  59. CARBON_CHECK(!symbolic_identity_.has_value());
  60. symbolic_identity_ = value;
  61. }
  62. // These functions exist only so that an `ImplBinding` can be used as a
  63. // `ValueNodeView` as a key in a `StaticScope`.
  64. auto static_type() const -> const Value& {
  65. CARBON_FATAL() << "an ImplBinding has no type";
  66. }
  67. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  68. // Return the original impl binding.
  69. auto original() const -> Nonnull<const ImplBinding*> {
  70. if (original_.has_value()) {
  71. return *original_;
  72. } else {
  73. return this;
  74. }
  75. }
  76. // Set the original impl binding.
  77. void set_original(Nonnull<const ImplBinding*> orig) { original_ = orig; }
  78. private:
  79. Nonnull<const GenericBinding*> type_var_;
  80. std::optional<Nonnull<const Value*>> iface_;
  81. std::optional<Nonnull<const Value*>> symbolic_identity_;
  82. std::optional<Nonnull<const ImplBinding*>> original_;
  83. };
  84. } // namespace Carbon
  85. #endif // CARBON_EXPLORER_AST_IMPL_BINDING_H_