pattern.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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,
  44. SemIR::InstKind pattern_kind, bool is_template)
  45. -> BindingPatternInfo {
  46. SemIR::InstKind bind_name_kind;
  47. switch (pattern_kind) {
  48. case SemIR::InstKind::RefBindingPattern:
  49. bind_name_kind = SemIR::InstKind::RefBinding;
  50. break;
  51. case SemIR::InstKind::SymbolicBindingPattern:
  52. bind_name_kind = SemIR::InstKind::SymbolicBinding;
  53. break;
  54. case SemIR::InstKind::ValueBindingPattern:
  55. bind_name_kind = SemIR::InstKind::ValueBinding;
  56. break;
  57. default:
  58. CARBON_FATAL("pattern_kind is not a binding pattern kind");
  59. }
  60. bool is_generic = pattern_kind == SemIR::SymbolicBindingPattern::Kind;
  61. auto entity_name_id = context.entity_names().AddSymbolicBindingName(
  62. name_id, context.scope_stack().PeekNameScopeId(),
  63. is_generic ? context.scope_stack().AddCompileTimeBinding()
  64. : SemIR::CompileTimeBindIndex::None,
  65. is_template);
  66. auto bind_id = AddInstInNoBlock(
  67. context,
  68. SemIR::LocIdAndInst::UncheckedLoc(
  69. name_loc, SemIR::AnyBinding{.kind = bind_name_kind,
  70. .type_id = type_id,
  71. .entity_name_id = entity_name_id,
  72. .value_id = SemIR::InstId::None}));
  73. auto pattern_type_id = GetPatternType(context, type_id);
  74. auto binding_pattern_id = AddPatternInst(
  75. context, SemIR::LocIdAndInst::UncheckedLoc(
  76. name_loc,
  77. SemIR::AnyBindingPattern{.kind = pattern_kind,
  78. .type_id = pattern_type_id,
  79. .entity_name_id = entity_name_id}));
  80. if (is_generic) {
  81. context.scope_stack().PushCompileTimeBinding(bind_id);
  82. }
  83. bool inserted =
  84. context.bind_name_map()
  85. .Insert(binding_pattern_id, {.bind_name_id = bind_id,
  86. .type_expr_region_id = type_region_id})
  87. .is_inserted();
  88. CARBON_CHECK(inserted);
  89. return {.pattern_id = binding_pattern_id, .bind_id = bind_id};
  90. }
  91. // Returns a VarStorage inst for the given `var` pattern. If the pattern
  92. // is the body of a returned var, this reuses the return slot, and otherwise it
  93. // adds a new inst.
  94. static auto GetOrAddVarStorage(Context& context, SemIR::InstId var_pattern_id,
  95. bool is_returned_var) -> SemIR::InstId {
  96. if (is_returned_var) {
  97. auto& function = GetCurrentFunctionForReturn(context);
  98. auto return_info =
  99. SemIR::ReturnTypeInfo::ForFunction(context.sem_ir(), function);
  100. if (return_info.has_return_slot()) {
  101. return GetCurrentReturnSlot(context);
  102. }
  103. }
  104. auto pattern = context.insts().GetWithLocId(var_pattern_id);
  105. return AddInstWithCleanup(
  106. context, pattern.loc_id,
  107. SemIR::VarStorage{.type_id = ExtractScrutineeType(context.sem_ir(),
  108. pattern.inst.type_id()),
  109. .pattern_id = var_pattern_id});
  110. }
  111. auto AddPatternVarStorage(Context& context, SemIR::InstBlockId pattern_block_id,
  112. bool is_returned_var) -> void {
  113. // We need to emit the VarStorage insts early, because they may be output
  114. // arguments for the initializer. However, we can't emit them when we emit
  115. // the corresponding `VarPattern`s because they're part of the pattern match,
  116. // not part of the pattern.
  117. // TODO: Find a way to do this without walking the whole pattern block.
  118. for (auto inst_id : context.inst_blocks().Get(pattern_block_id)) {
  119. if (context.insts().Is<SemIR::VarPattern>(inst_id)) {
  120. context.var_storage_map().Insert(
  121. inst_id, GetOrAddVarStorage(context, inst_id, is_returned_var));
  122. }
  123. }
  124. }
  125. auto AddParamPattern(Context& context, SemIR::LocId loc_id,
  126. SemIR::NameId name_id,
  127. SemIR::ExprRegionId type_expr_region_id,
  128. SemIR::TypeId type_id, bool is_ref) -> SemIR::InstId {
  129. const auto& binding_pattern_kind = is_ref ? SemIR::RefBindingPattern::Kind
  130. : SemIR::ValueBindingPattern::Kind;
  131. SemIR::InstId pattern_id =
  132. AddBindingPattern(context, loc_id, name_id, type_id, type_expr_region_id,
  133. binding_pattern_kind,
  134. /*is_template=*/false)
  135. .pattern_id;
  136. const auto& param_pattern_kind =
  137. is_ref ? SemIR::RefParamPattern::Kind : SemIR::ValueParamPattern::Kind;
  138. pattern_id = AddPatternInst(
  139. context,
  140. SemIR::LocIdAndInst::UncheckedLoc(
  141. loc_id, SemIR::AnyParamPattern{
  142. .kind = param_pattern_kind,
  143. .type_id = context.insts().Get(pattern_id).type_id(),
  144. .subpattern_id = pattern_id,
  145. .index = SemIR::CallParamIndex::None}));
  146. return pattern_id;
  147. }
  148. } // namespace Carbon::Check