bindings.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 "explorer/common/nonnull.h"
  8. namespace Carbon {
  9. class ImplBinding;
  10. class GenericBinding;
  11. class Value;
  12. using BindingMap =
  13. std::map<Nonnull<const GenericBinding*>, Nonnull<const Value*>>;
  14. using ImplWitnessMap =
  15. std::map<Nonnull<const ImplBinding*>, Nonnull<const Value*>>;
  16. // A set of evaluated bindings in some context, such as a function or class.
  17. //
  18. // These are shared by a context and all unparameterized entities within that
  19. // context. For example, a class and the name of a method within that class
  20. // will have the same set of bindings.
  21. class Bindings {
  22. public:
  23. // Create an instantiated set of bindings for use during evaluation,
  24. // containing both arguments and witnesses.
  25. Bindings(BindingMap args, ImplWitnessMap witnesses)
  26. : args_(args), witnesses_(witnesses) {}
  27. enum NoWitnessesTag { NoWitnesses };
  28. // Create a set of bindings for use during type-checking, containing only the
  29. // arguments but not the corresponding witnesses.
  30. Bindings(BindingMap args, NoWitnessesTag) : args_(args), witnesses_() {}
  31. // Argument values corresponding to generic bindings.
  32. auto args() const -> const BindingMap& { return args_; }
  33. // Witnesses corresponding to impl bindings.
  34. auto witnesses() const -> const ImplWitnessMap& { return witnesses_; }
  35. // An empty set of bindings.
  36. static auto None() -> Nonnull<const Bindings*>;
  37. private:
  38. BindingMap args_;
  39. ImplWitnessMap witnesses_;
  40. };
  41. } // namespace Carbon
  42. #endif // CARBON_EXPLORER_AST_BINDINGS_H_