bindings.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. namespace Carbon {
  9. void Bindings::Add(Nonnull<const GenericBinding*> binding,
  10. Nonnull<const Value*> value,
  11. std::optional<Nonnull<const Value*>> witness) {
  12. bool added_value = args_.insert({binding, value}).second;
  13. CARBON_CHECK(added_value) << "Add of already-existing binding";
  14. if (witness) {
  15. // TODO: Eventually we should check that we have a witness if and only if
  16. // the binding has an impl binding.
  17. auto impl_binding = binding->impl_binding();
  18. CARBON_CHECK(impl_binding) << "Given witness but have no impl binding";
  19. bool added_witness = witnesses_.insert({*impl_binding, *witness}).second;
  20. CARBON_CHECK(added_witness) << "Add of already-existing binding";
  21. }
  22. }
  23. auto Bindings::None() -> Nonnull<const Bindings*> {
  24. static Nonnull<const Bindings*> bindings = new Bindings;
  25. return bindings;
  26. }
  27. auto Bindings::SymbolicIdentity(
  28. Nonnull<Arena*> arena,
  29. llvm::ArrayRef<Nonnull<const GenericBinding*>> bindings)
  30. -> Nonnull<const Bindings*> {
  31. auto* result = arena->New<Bindings>();
  32. for (const auto* binding : bindings) {
  33. std::optional<Nonnull<const Value*>> witness;
  34. if (binding->impl_binding()) {
  35. witness = *binding->impl_binding().value()->symbolic_identity();
  36. }
  37. result->Add(binding, *binding->symbolic_identity(), witness);
  38. }
  39. return result;
  40. }
  41. } // namespace Carbon