bindings.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #include "explorer/ast/bindings.h"
  5. #include "common/error.h"
  6. #include "explorer/ast/impl_binding.h"
  7. #include "explorer/ast/pattern.h"
  8. #include "explorer/ast/value.h"
  9. namespace Carbon {
  10. Bindings::Bindings(CloneContext& context, const Bindings& other) {
  11. for (auto [binding, value] : other.args_) {
  12. args_.insert({context.Remap(binding), context.Clone(value)});
  13. }
  14. for (auto [binding, value] : other.witnesses_) {
  15. witnesses_.insert({context.Remap(binding), context.Clone(value)});
  16. }
  17. }
  18. void Bindings::Add(Nonnull<const GenericBinding*> binding,
  19. Nonnull<const Value*> value,
  20. std::optional<Nonnull<const Value*>> witness) {
  21. bool added_value = args_.insert({binding, value}).second;
  22. CARBON_CHECK(added_value) << "Add of already-existing binding";
  23. if (witness) {
  24. // TODO: Eventually we should check that we have a witness if and only if
  25. // the binding has an impl binding.
  26. auto impl_binding = binding->impl_binding();
  27. CARBON_CHECK(impl_binding) << "Given witness but have no impl binding";
  28. bool added_witness = witnesses_.insert({*impl_binding, *witness}).second;
  29. CARBON_CHECK(added_witness) << "Add of already-existing binding";
  30. }
  31. }
  32. auto Bindings::None() -> Nonnull<const Bindings*> {
  33. static Nonnull<const Bindings*> bindings = new Bindings;
  34. return bindings;
  35. }
  36. auto Bindings::SymbolicIdentity(
  37. Nonnull<Arena*> arena,
  38. llvm::ArrayRef<Nonnull<const GenericBinding*>> bindings)
  39. -> Nonnull<const Bindings*> {
  40. auto* result = arena->New<Bindings>();
  41. for (const auto* binding : bindings) {
  42. std::optional<Nonnull<const Value*>> witness;
  43. if (binding->impl_binding()) {
  44. witness = *binding->impl_binding().value()->symbolic_identity();
  45. }
  46. result->Add(binding, *binding->symbolic_identity(), witness);
  47. }
  48. return result;
  49. }
  50. } // namespace Carbon