pattern.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 "toolchain/sem_ir/pattern.h"
  5. namespace Carbon::SemIR {
  6. auto IsSelfPattern(const File& sem_ir, InstId pattern_id) -> bool {
  7. // Note that the public contract of GetPrettyNameFromPatternId does not
  8. // guarantee that this is correct; we're relying on knowledge of the
  9. // implementation details.
  10. return GetPrettyNameFromPatternId(sem_ir, pattern_id) == NameId::SelfValue;
  11. }
  12. auto GetPrettyNameFromPatternId(const File& sem_ir, InstId pattern_id)
  13. -> NameId {
  14. auto inst_id = pattern_id;
  15. auto inst = sem_ir.insts().Get(inst_id);
  16. if (auto var_pattern = inst.TryAs<VarPattern>()) {
  17. inst_id = var_pattern->subpattern_id;
  18. inst = sem_ir.insts().Get(inst_id);
  19. }
  20. if (auto addr_pattern = inst.TryAs<AddrPattern>()) {
  21. inst_id = addr_pattern->inner_id;
  22. inst = sem_ir.insts().Get(inst_id);
  23. }
  24. if (auto param_pattern_inst = inst.TryAs<AnyParamPattern>()) {
  25. inst_id = param_pattern_inst->subpattern_id;
  26. inst = sem_ir.insts().Get(inst_id);
  27. }
  28. if (inst.Is<ReturnSlotPattern>()) {
  29. return NameId::ReturnSlot;
  30. }
  31. if (auto binding_pattern = inst.TryAs<AnyBindingPattern>()) {
  32. if (binding_pattern->entity_name_id.has_value()) {
  33. return sem_ir.entity_names().Get(binding_pattern->entity_name_id).name_id;
  34. } else {
  35. return NameId::Underscore;
  36. }
  37. }
  38. return NameId::None;
  39. }
  40. } // namespace Carbon::SemIR