action.cpp 7.0 KB

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