action.cpp 6.8 KB

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