bindings.h 2.6 KB

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