action.cpp 7.4 KB

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