impl_binding.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. 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 interface being implemented.
  40. auto interface() const -> Nonnull<const Value*> { return iface_; }
  41. // Required for the the ValueNode interface
  42. auto constant_value() const -> std::optional<Nonnull<const Value*>> {
  43. return std::nullopt;
  44. }
  45. auto symbolic_identity() const -> std::optional<Nonnull<const Value*>> {
  46. return symbolic_identity_;
  47. }
  48. void set_symbolic_identity(Nonnull<const Value*> value) {
  49. CARBON_CHECK(!symbolic_identity_.has_value());
  50. symbolic_identity_ = value;
  51. }
  52. // The static type of the impl. Cannot be called before typechecking.
  53. auto static_type() const -> const Value& { return **static_type_; }
  54. // Sets the static type of the impl. Can only be called once, during
  55. // typechecking.
  56. void set_static_type(Nonnull<const Value*> type) {
  57. CARBON_CHECK(!static_type_.has_value());
  58. static_type_ = type;
  59. }
  60. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  61. // Return the original impl binding.
  62. auto original() const -> Nonnull<const ImplBinding*> {
  63. if (original_.has_value())
  64. return *original_;
  65. else
  66. return this;
  67. }
  68. // Set the original impl binding.
  69. void set_original(Nonnull<const ImplBinding*> orig) { original_ = orig; }
  70. private:
  71. Nonnull<const GenericBinding*> type_var_;
  72. Nonnull<const Value*> iface_;
  73. std::optional<Nonnull<const Value*>> symbolic_identity_;
  74. std::optional<Nonnull<const Value*>> static_type_;
  75. std::optional<Nonnull<const ImplBinding*>> original_;
  76. };
  77. } // namespace Carbon
  78. #endif // CARBON_EXPLORER_AST_IMPL_BINDING_H_