pattern.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "toolchain/sem_ir/ids.h"
  6. #include "toolchain/sem_ir/typed_insts.h"
  7. namespace Carbon::SemIR {
  8. // Returns the pattern instruction corresponding to the given ID, after
  9. // unwrapping any simple pattern operators such as `var`.
  10. static auto GetUnwrapped(const File& sem_ir, InstId pattern_id)
  11. -> std::pair<InstId, Inst> {
  12. auto inst_id = pattern_id;
  13. auto inst = sem_ir.insts().Get(inst_id);
  14. if (auto var_pattern = inst.TryAs<VarPattern>()) {
  15. inst_id = var_pattern->subpattern_id;
  16. inst = sem_ir.insts().Get(inst_id);
  17. }
  18. if (auto param_pattern_inst = inst.TryAs<AnyParamPattern>()) {
  19. inst_id = param_pattern_inst->subpattern_id;
  20. inst = sem_ir.insts().Get(inst_id);
  21. }
  22. return {inst_id, inst};
  23. }
  24. // Returns the name and entity name introduced by the given instruction if it is
  25. // a binding pattern, or otherwise `{None, None}`.
  26. static auto GetBoundEntityName(const File& sem_ir, Inst inst)
  27. -> std::pair<NameId, EntityNameId> {
  28. if (auto binding_pattern = inst.TryAs<AnyBindingPattern>()) {
  29. return {sem_ir.entity_names().Get(binding_pattern->entity_name_id).name_id,
  30. binding_pattern->entity_name_id};
  31. }
  32. return {NameId::None, EntityNameId::None};
  33. }
  34. auto IsSelfPattern(const File& sem_ir, InstId pattern_id) -> bool {
  35. auto [_, inst] = GetUnwrapped(sem_ir, pattern_id);
  36. auto [name_id, entity_name_id] = GetBoundEntityName(sem_ir, inst);
  37. return name_id == NameId::SelfValue;
  38. }
  39. auto GetFirstBindingNameFromPatternId(const File& sem_ir, InstId pattern_id)
  40. -> EntityNameId {
  41. llvm::SmallVector<InstId> work_list = {pattern_id};
  42. while (!work_list.empty()) {
  43. auto [_, inst] = GetUnwrapped(sem_ir, work_list.pop_back_val());
  44. if (auto tuple_patt = inst.TryAs<TuplePattern>()) {
  45. auto block = sem_ir.inst_blocks().Get(tuple_patt->elements_id);
  46. work_list.append(block.rbegin(), block.rend());
  47. continue;
  48. }
  49. // TODO: Look through struct patterns.
  50. auto [name_id, entity_name_id] = GetBoundEntityName(sem_ir, inst);
  51. CARBON_CHECK(entity_name_id.has_value(), "Unhandled pattern inst kind {0}",
  52. inst);
  53. // Skip unnamed bindings.
  54. if (name_id != NameId::Underscore) {
  55. return entity_name_id;
  56. }
  57. }
  58. return EntityNameId::None;
  59. }
  60. auto GetPrettyNameFromPatternId(const File& sem_ir, InstId pattern_id)
  61. -> NameId {
  62. auto [inst_id, inst] = GetUnwrapped(sem_ir, pattern_id);
  63. if (auto [name_id, entity_name_id] = GetBoundEntityName(sem_ir, inst);
  64. entity_name_id.has_value()) {
  65. return name_id;
  66. }
  67. if (inst.Is<ReturnSlotPattern>()) {
  68. return NameId::ReturnSlot;
  69. }
  70. return NameId::None;
  71. }
  72. } // namespace Carbon::SemIR