impl_binding.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 std::nullopt;
  47. }
  48. // The static type of the impl. Cannot be called before typechecking.
  49. auto static_type() const -> const Value& { return **static_type_; }
  50. // Sets the static type of the impl. Can only be called once, during
  51. // typechecking.
  52. void set_static_type(Nonnull<const Value*> type) {
  53. CARBON_CHECK(!static_type_.has_value());
  54. static_type_ = type;
  55. }
  56. auto value_category() const -> ValueCategory { return ValueCategory::Let; }
  57. private:
  58. Nonnull<const GenericBinding*> type_var_;
  59. Nonnull<const Value*> iface_;
  60. std::optional<Nonnull<const Value*>> static_type_;
  61. };
  62. } // namespace Carbon
  63. #endif // CARBON_EXPLORER_AST_IMPL_BINDING_H_