pattern.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. return sem_ir.entity_names().Get(binding_pattern->entity_name_id).name_id;
  33. }
  34. return NameId::None;
  35. }
  36. } // namespace Carbon::SemIR