bindings.h 2.9 KB

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