bindings.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_BINDINGS_H_
  5. #define CARBON_EXPLORER_AST_BINDINGS_H_
  6. #include <map>
  7. #include <utility>
  8. #include "explorer/ast/clone_context.h"
  9. #include "explorer/common/nonnull.h"
  10. #include "llvm/ADT/ArrayRef.h"
  11. namespace Carbon {
  12. class Arena;
  13. class ImplBinding;
  14. class GenericBinding;
  15. class Value;
  16. using BindingMap =
  17. std::map<Nonnull<const GenericBinding*>, Nonnull<const Value*>>;
  18. using ImplWitnessMap =
  19. std::map<Nonnull<const ImplBinding*>, Nonnull<const Value*>>;
  20. // A set of evaluated bindings in some context, such as a function or class.
  21. //
  22. // These are shared by a context and all unparameterized entities within that
  23. // context. For example, a class and the name of a method within that class
  24. // will have the same set of bindings.
  25. class Bindings {
  26. public:
  27. // Gets an empty set of bindings.
  28. static auto None() -> Nonnull<const Bindings*>;
  29. // Makes a set of symbolic identity bindings for the given collection of
  30. // generic bindings and their impl bindings.
  31. static auto SymbolicIdentity(
  32. Nonnull<Arena*> arena,
  33. llvm::ArrayRef<Nonnull<const GenericBinding*>> bindings)
  34. -> Nonnull<const Bindings*>;
  35. // Create an empty set of bindings.
  36. Bindings() = default;
  37. // Create an instantiated set of bindings for use during evaluation,
  38. // containing both arguments and witnesses.
  39. explicit Bindings(BindingMap args, ImplWitnessMap witnesses)
  40. : args_(std::move(args)), witnesses_(std::move(witnesses)) {}
  41. enum NoWitnessesTag { NoWitnesses };
  42. // Create a set of bindings for use during type-checking, containing only the
  43. // arguments but not the corresponding witnesses.
  44. explicit Bindings(BindingMap args, NoWitnessesTag /*unused*/)
  45. : args_(std::move(args)) {}
  46. explicit Bindings(CloneContext& context, const Bindings& other);
  47. template <typename F>
  48. auto Decompose(F f) const {
  49. return f(args_, witnesses_);
  50. }
  51. // Add a value, and perhaps a witness, for a generic binding.
  52. void Add(Nonnull<const GenericBinding*> binding, Nonnull<const Value*> value,
  53. std::optional<Nonnull<const Value*>> witness);
  54. // Argument values corresponding to generic bindings.
  55. auto args() const -> const BindingMap& { return args_; }
  56. // Witnesses corresponding to impl bindings.
  57. auto witnesses() const -> const ImplWitnessMap& { return witnesses_; }
  58. // Determine whether this is an empty set of bindings.
  59. [[nodiscard]] auto empty() const -> bool {
  60. return args_.empty() && witnesses_.empty();
  61. }
  62. private:
  63. BindingMap args_;
  64. ImplWitnessMap witnesses_;
  65. };
  66. } // namespace Carbon
  67. #endif // CARBON_EXPLORER_AST_BINDINGS_H_