action.cpp 6.8 KB

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