action.cpp 7.5 KB

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