pattern.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/check/pattern.h"
  5. #include "toolchain/check/control_flow.h"
  6. #include "toolchain/check/inst.h"
  7. #include "toolchain/check/return.h"
  8. #include "toolchain/check/type.h"
  9. namespace Carbon::Check {
  10. auto BeginSubpattern(Context& context) -> void {
  11. context.inst_block_stack().Push();
  12. context.region_stack().PushRegion(context.inst_block_stack().PeekOrAdd());
  13. }
  14. auto EndSubpatternAsExpr(Context& context, SemIR::InstId result_id)
  15. -> SemIR::ExprRegionId {
  16. if (context.region_stack().PeekRegion().size() > 1) {
  17. // End the exit block with a branch to a successor block, whose contents
  18. // will be determined later.
  19. AddInst(context,
  20. SemIR::LocIdAndInst::NoLoc<SemIR::Branch>(
  21. {.target_id = context.inst_blocks().AddPlaceholder()}));
  22. } else {
  23. // This single-block region will be inserted as a SpliceBlock, so we don't
  24. // need control flow out of it.
  25. }
  26. auto block_id = context.inst_block_stack().Pop();
  27. CARBON_CHECK(block_id == context.region_stack().PeekRegion().back());
  28. // TODO: Is it possible to validate that this region is genuinely
  29. // single-entry, single-exit?
  30. return context.sem_ir().expr_regions().Add(
  31. {.block_ids = context.region_stack().PopRegion(),
  32. .result_id = result_id});
  33. }
  34. auto EndSubpatternAsNonExpr(Context& context) -> void {
  35. auto block_id = context.inst_block_stack().Pop();
  36. CARBON_CHECK(block_id == context.region_stack().PeekRegion().back());
  37. CARBON_CHECK(context.region_stack().PeekRegion().size() == 1);
  38. CARBON_CHECK(context.inst_blocks().Get(block_id).empty());
  39. context.region_stack().PopAndDiscardRegion();
  40. }
  41. auto AddBindingPattern(Context& context, SemIR::LocId name_loc,
  42. SemIR::NameId name_id, SemIR::TypeId type_id,
  43. SemIR::ExprRegionId type_region_id, bool is_generic,
  44. bool is_template) -> BindingPatternInfo {
  45. auto entity_name_id = context.entity_names().AddSymbolicBindingName(
  46. name_id, context.scope_stack().PeekNameScopeId(),
  47. is_generic ? context.scope_stack().AddCompileTimeBinding()
  48. : SemIR::CompileTimeBindIndex::None,
  49. is_template);
  50. auto bind_id = SemIR::InstId::None;
  51. if (is_generic) {
  52. bind_id = AddInstInNoBlock<SemIR::BindSymbolicName>(
  53. context, name_loc,
  54. {.type_id = type_id,
  55. .entity_name_id = entity_name_id,
  56. .value_id = SemIR::InstId::None});
  57. } else {
  58. bind_id =
  59. AddInstInNoBlock<SemIR::BindName>(context, name_loc,
  60. {.type_id = type_id,
  61. .entity_name_id = entity_name_id,
  62. .value_id = SemIR::InstId::None});
  63. }
  64. auto pattern_type_id = GetPatternType(context, type_id);
  65. auto binding_pattern_id = SemIR::InstId::None;
  66. if (is_generic) {
  67. binding_pattern_id = AddPatternInst<SemIR::SymbolicBindingPattern>(
  68. context, name_loc,
  69. {.type_id = pattern_type_id, .entity_name_id = entity_name_id});
  70. } else {
  71. binding_pattern_id = AddPatternInst<SemIR::BindingPattern>(
  72. context, name_loc,
  73. {.type_id = pattern_type_id, .entity_name_id = entity_name_id});
  74. }
  75. if (is_generic) {
  76. context.scope_stack().PushCompileTimeBinding(bind_id);
  77. }
  78. bool inserted =
  79. context.bind_name_map()
  80. .Insert(binding_pattern_id, {.bind_name_id = bind_id,
  81. .type_expr_region_id = type_region_id})
  82. .is_inserted();
  83. CARBON_CHECK(inserted);
  84. return {.pattern_id = binding_pattern_id, .bind_id = bind_id};
  85. }
  86. // Returns a VarStorage inst for the given `var` pattern. If the pattern
  87. // is the body of a returned var, this reuses the return slot, and otherwise it
  88. // adds a new inst.
  89. static auto GetOrAddVarStorage(Context& context, SemIR::InstId var_pattern_id,
  90. bool is_returned_var) -> SemIR::InstId {
  91. if (is_returned_var) {
  92. auto& function = GetCurrentFunctionForReturn(context);
  93. auto return_info =
  94. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  95. if (return_info.has_return_slot()) {
  96. return GetCurrentReturnSlot(context);
  97. }
  98. }
  99. auto pattern = context.insts().GetWithLocId(var_pattern_id);
  100. return AddInstWithCleanup(
  101. context, pattern.loc_id,
  102. SemIR::VarStorage{.type_id = ExtractScrutineeType(context.sem_ir(),
  103. pattern.inst.type_id()),
  104. .pattern_id = var_pattern_id});
  105. }
  106. auto AddPatternVarStorage(Context& context, SemIR::InstBlockId pattern_block_id,
  107. bool is_returned_var) -> void {
  108. // We need to emit the VarStorage insts early, because they may be output
  109. // arguments for the initializer. However, we can't emit them when we emit
  110. // the corresponding `VarPattern`s because they're part of the pattern match,
  111. // not part of the pattern.
  112. // TODO: Find a way to do this without walking the whole pattern block.
  113. for (auto inst_id : context.inst_blocks().Get(pattern_block_id)) {
  114. if (context.insts().Is<SemIR::VarPattern>(inst_id)) {
  115. context.var_storage_map().Insert(
  116. inst_id, GetOrAddVarStorage(context, inst_id, is_returned_var));
  117. }
  118. }
  119. }
  120. auto AddSelfParamPattern(Context& context, SemIR::LocId loc_id,
  121. SemIR::ExprRegionId type_expr_region_id,
  122. SemIR::TypeId type_id) -> SemIR::InstId {
  123. SemIR::InstId pattern_id =
  124. AddBindingPattern(context, loc_id, SemIR::NameId::SelfValue, type_id,
  125. type_expr_region_id, /*is_generic=*/false,
  126. /*is_template=*/false)
  127. .pattern_id;
  128. pattern_id = AddPatternInst<SemIR::ValueParamPattern>(
  129. context, loc_id,
  130. {.type_id = context.insts().Get(pattern_id).type_id(),
  131. .subpattern_id = pattern_id,
  132. .index = SemIR::CallParamIndex::None});
  133. return pattern_id;
  134. }
  135. auto AddAddrSelfParamPattern(Context& context, SemIR::LocId loc_id,
  136. SemIR::ExprRegionId type_expr_region_id,
  137. SemIR::TypeInstId type_inst_id) -> SemIR::InstId {
  138. auto pattern_id = AddSelfParamPattern(context, loc_id, type_expr_region_id,
  139. GetPointerType(context, type_inst_id));
  140. return AddPatternInst<SemIR::AddrPattern>(
  141. context, loc_id,
  142. {.type_id = GetPatternType(context, SemIR::AutoType::TypeId),
  143. .inner_id = pattern_id});
  144. }
  145. } // namespace Carbon::Check