bindings.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 : public Printable<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. // Add a value, and perhaps a witness, for a generic binding.
  55. void Add(Nonnull<const GenericBinding*> binding, Nonnull<const Value*> value,
  56. std::optional<Nonnull<const Value*>> witness);
  57. // Argument values corresponding to generic bindings.
  58. auto args() const -> const BindingMap& { return args_; }
  59. // Witnesses corresponding to impl bindings.
  60. auto witnesses() const -> const ImplWitnessMap& { return witnesses_; }
  61. // Determine whether this is an empty set of bindings.
  62. [[nodiscard]] auto empty() const -> bool {
  63. return args_.empty() && witnesses_.empty();
  64. }
  65. private:
  66. BindingMap args_;
  67. ImplWitnessMap witnesses_;
  68. };
  69. } // namespace Carbon
  70. #endif // CARBON_EXPLORER_AST_BINDINGS_H_