action.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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/action.h"
  5. #include "toolchain/base/kind_switch.h"
  6. #include "toolchain/check/generic_region_stack.h"
  7. #include "toolchain/check/inst.h"
  8. #include "toolchain/check/type.h"
  9. #include "toolchain/sem_ir/constant.h"
  10. #include "toolchain/sem_ir/id_kind.h"
  11. #include "toolchain/sem_ir/inst.h"
  12. #include "toolchain/sem_ir/typed_insts.h"
  13. namespace Carbon::Check {
  14. auto PerformAction(Context& context, SemIR::LocId loc_id,
  15. SemIR::RefineTypeAction action) -> SemIR::InstId {
  16. return AddInst<SemIR::AsCompatible>(
  17. context, loc_id,
  18. {.type_id =
  19. context.types().GetTypeIdForTypeInstId(action.inst_type_inst_id),
  20. .source_id = action.inst_id});
  21. }
  22. static auto OperandDependence(Context& context, SemIR::ConstantId const_id)
  23. -> SemIR::ConstantDependence {
  24. // A type operand makes the instruction dependent if it is a
  25. // template-dependent constant.
  26. if (!const_id.is_symbolic()) {
  27. return SemIR::ConstantDependence::None;
  28. }
  29. return context.constant_values().GetSymbolicConstant(const_id).dependence;
  30. }
  31. auto OperandDependence(Context& context, SemIR::TypeId type_id)
  32. -> SemIR::ConstantDependence {
  33. // A type operand makes the instruction dependent if it is a
  34. // template-dependent type.
  35. return OperandDependence(context, context.types().GetConstantId(type_id));
  36. }
  37. auto OperandDependence(Context& context, SemIR::InstId inst_id)
  38. -> SemIR::ConstantDependence {
  39. // An instruction operand makes the instruction dependent if its type or
  40. // constant value is dependent.
  41. return std::max(
  42. OperandDependence(context, context.insts().Get(inst_id).type_id()),
  43. OperandDependence(context, context.constant_values().Get(inst_id)));
  44. }
  45. auto OperandDependence(Context& context, SemIR::TypeInstId inst_id)
  46. -> SemIR::ConstantDependence {
  47. // An instruction operand makes the instruction dependent if its type or
  48. // constant value is dependent. TypeInstId has type `TypeType` which is
  49. // concrete, so we only need to look at the constant value.
  50. return OperandDependence(context, context.constant_values().Get(inst_id));
  51. }
  52. static auto OperandDependence(Context& context, SemIR::Inst::ArgAndKind arg)
  53. -> SemIR::ConstantDependence {
  54. CARBON_KIND_SWITCH(arg) {
  55. case CARBON_KIND(SemIR::InstId inst_id): {
  56. return OperandDependence(context, inst_id);
  57. }
  58. case CARBON_KIND(SemIR::MetaInstId inst_id): {
  59. return OperandDependence(context, inst_id);
  60. }
  61. case CARBON_KIND(SemIR::TypeInstId inst_id): {
  62. return OperandDependence(context, inst_id);
  63. }
  64. case SemIR::IdKind::None:
  65. case SemIR::IdKind::For<SemIR::AbsoluteInstId>:
  66. case SemIR::IdKind::For<SemIR::NameId>:
  67. return SemIR::ConstantDependence::None;
  68. default:
  69. // TODO: Properly handle different argument kinds.
  70. CARBON_FATAL("Unexpected argument kind for action");
  71. }
  72. }
  73. auto ActionIsPerformable(Context& context, SemIR::Inst action_inst) -> bool {
  74. if (auto refine_action = action_inst.TryAs<SemIR::RefineTypeAction>()) {
  75. // `RefineTypeAction` can be performed whenever the type is not template-
  76. // dependent, even if we don't know the instruction yet.
  77. return OperandDependence(context, refine_action->inst_type_inst_id) <
  78. SemIR::ConstantDependence::Template;
  79. }
  80. return OperandDependence(context, action_inst.type_id()) <
  81. SemIR::ConstantDependence::Template &&
  82. OperandDependence(context, action_inst.arg0_and_kind()) <
  83. SemIR::ConstantDependence::Template &&
  84. OperandDependence(context, action_inst.arg1_and_kind()) <
  85. SemIR::ConstantDependence::Template;
  86. }
  87. static auto AddDependentActionSpliceImpl(Context& context,
  88. SemIR::LocIdAndInst action,
  89. SemIR::TypeInstId result_type_inst_id)
  90. -> SemIR::InstId {
  91. auto inst_id = AddDependentActionInst(context, action);
  92. if (!result_type_inst_id.has_value()) {
  93. result_type_inst_id = AddDependentActionTypeInst(
  94. context, action.loc_id,
  95. SemIR::TypeOfInst{.type_id = SemIR::TypeType::TypeId,
  96. .inst_id = inst_id});
  97. }
  98. return AddInst(
  99. context, action.loc_id,
  100. SemIR::SpliceInst{.type_id = context.types().GetTypeIdForTypeInstId(
  101. result_type_inst_id),
  102. .inst_id = inst_id});
  103. }
  104. // Refine one operand of an action. Given an argument from a template, this
  105. // produces an argument that has the template-dependent parts replaced with
  106. // their concrete values, so that the action doesn't need to know which specific
  107. // it is operating on.
  108. static auto RefineOperand(Context& context, SemIR::LocId loc_id,
  109. SemIR::Inst::ArgAndKind arg) -> int32_t {
  110. if (auto inst_id = arg.TryAs<SemIR::MetaInstId>()) {
  111. auto inst = context.insts().Get(*inst_id);
  112. if (inst.Is<SemIR::SpliceInst>()) {
  113. // The argument will evaluate to the spliced instruction, which is already
  114. // refined.
  115. return arg.value();
  116. }
  117. // If the type of the action argument is dependent, refine to an instruction
  118. // with a concrete type.
  119. if (OperandDependence(context, inst.type_id()) ==
  120. SemIR::ConstantDependence::Template) {
  121. auto type_inst_id = context.types().GetTypeInstId(inst.type_id());
  122. inst_id = AddDependentActionSpliceImpl(
  123. context,
  124. SemIR::LocIdAndInst(
  125. loc_id,
  126. SemIR::RefineTypeAction{.type_id = GetSingletonType(
  127. context, SemIR::InstType::TypeInstId),
  128. .inst_id = *inst_id,
  129. .inst_type_inst_id = type_inst_id}),
  130. type_inst_id);
  131. }
  132. // TODO: Handle the case where the constant value of the instruction is
  133. // template-dependent.
  134. return inst_id->index;
  135. }
  136. return arg.value();
  137. }
  138. // Refine the operands of an action, ensuring that they will refer to concrete
  139. // instructions that don't have template-dependent types.
  140. static auto RefineOperands(Context& context, SemIR::LocId loc_id,
  141. SemIR::Inst action) -> SemIR::Inst {
  142. auto arg0 = RefineOperand(context, loc_id, action.arg0_and_kind());
  143. auto arg1 = RefineOperand(context, loc_id, action.arg1_and_kind());
  144. action.SetArgs(arg0, arg1);
  145. return action;
  146. }
  147. auto AddDependentActionSplice(Context& context, SemIR::LocIdAndInst action,
  148. SemIR::TypeInstId result_type_inst_id)
  149. -> SemIR::InstId {
  150. action.inst = RefineOperands(context, action.loc_id, action.inst);
  151. return AddDependentActionSpliceImpl(context, action, result_type_inst_id);
  152. }
  153. auto Internal::BeginPerformDelayedAction(Context& context) -> void {
  154. // Push an `InstBlock` to hold any instructions created by the action.
  155. // Note that we assume that actions don't need to create multiple blocks. If
  156. // this changes, we should push a region too.
  157. context.inst_block_stack().Push();
  158. }
  159. auto Internal::EndPerformDelayedAction(Context& context,
  160. SemIR::InstId result_id)
  161. -> SemIR::InstId {
  162. // If the only created instruction is the result, then we can use it directly.
  163. auto contents = context.inst_block_stack().PeekCurrentBlockContents();
  164. if (contents.size() == 1 && contents[0] == result_id) {
  165. context.inst_block_stack().PopAndDiscard();
  166. return result_id;
  167. }
  168. // Otherwise, create a splice_block to represent the sequence of instructions
  169. // created by the action.
  170. auto result = context.insts().GetWithLocId(result_id);
  171. return AddInstInNoBlock(
  172. context, result.loc_id,
  173. SemIR::SpliceBlock{.type_id = result.inst.type_id(),
  174. .block_id = context.inst_block_stack().Pop(),
  175. .result_id = result_id});
  176. }
  177. } // namespace Carbon::Check