impl_binding.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/expression_category.h"
  11. namespace Carbon {
  12. class Value;
  13. class Expression;
  14. class GenericBinding;
  15. // `ImplBinding` plays the role of the parameter for passing witness
  16. // tables to a generic. However, unlike regular parameters
  17. // (`BindingPattern`) there is no explicit syntax that corresponds to
  18. // an `ImplBinding`, so they are not created during parsing. Instances
  19. // of `ImplBinding` are created during type checking, when processing
  20. // a type parameter (a `GenericBinding`), or an `impls` requirement in
  21. // a `where` clause.
  22. class ImplBinding : public AstNode {
  23. public:
  24. using ImplementsCarbonValueNode = void;
  25. ImplBinding(SourceLocation source_loc,
  26. Nonnull<const GenericBinding*> type_var,
  27. std::optional<Nonnull<const Value*>> iface)
  28. : AstNode(AstNodeKind::ImplBinding, source_loc),
  29. type_var_(type_var),
  30. iface_(iface) {}
  31. explicit ImplBinding(CloneContext& context, const ImplBinding& other);
  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 expression_category() const -> ExpressionCategory {
  68. return ExpressionCategory::Value;
  69. }
  70. // Return the original impl binding.
  71. auto original() const -> Nonnull<const ImplBinding*> {
  72. if (original_.has_value()) {
  73. return *original_;
  74. } else {
  75. return this;
  76. }
  77. }
  78. // Set the original impl binding.
  79. void set_original(Nonnull<const ImplBinding*> orig) { original_ = orig; }
  80. private:
  81. Nonnull<const GenericBinding*> type_var_;
  82. std::optional<Nonnull<const Value*>> iface_;
  83. std::optional<Nonnull<const Value*>> symbolic_identity_;
  84. std::optional<Nonnull<const ImplBinding*>> original_;
  85. };
  86. } // namespace Carbon
  87. #endif // CARBON_EXPLORER_AST_IMPL_BINDING_H_