inst.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/inst.h"
  5. #include "common/vlog.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/eval.h"
  8. #include "toolchain/check/generic.h"
  9. #include "toolchain/check/import.h"
  10. #include "toolchain/sem_ir/constant.h"
  11. #include "toolchain/sem_ir/expr_info.h"
  12. #include "toolchain/sem_ir/ids.h"
  13. #include "toolchain/sem_ir/inst_kind.h"
  14. namespace Carbon::Check {
  15. // Finish producing an instruction. Set its constant value, and register it in
  16. // any applicable instruction lists.
  17. static auto FinishInst(Context& context, SemIR::InstId inst_id,
  18. SemIR::Inst inst) -> void {
  19. DependentInstKind dep_kind = DependentInstKind::None;
  20. // If the instruction has a symbolic constant type, track that we need to
  21. // substitute into it.
  22. if (context.constant_values().DependsOnGenericParameter(
  23. context.types().GetConstantId(inst.type_id()))) {
  24. dep_kind.Add(DependentInstKind::SymbolicType);
  25. }
  26. // If the instruction has a constant value, compute it.
  27. auto const_id = TryEvalInstUnsafe(context, inst_id, inst);
  28. context.constant_values().Set(inst_id, const_id);
  29. if (const_id.is_constant()) {
  30. CARBON_VLOG_TO(context.vlog_stream(), "Constant: {0} -> {1}\n", inst,
  31. context.constant_values().GetInstId(const_id));
  32. // If the constant value is symbolic, track that we need to substitute into
  33. // it.
  34. if (context.constant_values().DependsOnGenericParameter(const_id)) {
  35. dep_kind.Add(DependentInstKind::SymbolicConstant);
  36. }
  37. }
  38. // Template-dependent instructions are handled separately by
  39. // `AddDependentActionInst`.
  40. CARBON_CHECK(
  41. inst.kind().constant_kind() !=
  42. SemIR::InstConstantKind::ConstantInstAction &&
  43. inst.kind().constant_kind() != SemIR::InstConstantKind::InstAction,
  44. "Use AddDependentActionInst to add an action instruction");
  45. // Keep track of dependent instructions.
  46. if (!dep_kind.empty()) {
  47. AttachDependentInstToCurrentGeneric(context,
  48. {.inst_id = inst_id, .kind = dep_kind});
  49. }
  50. }
  51. auto AddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  52. -> SemIR::InstId {
  53. auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  54. if (SemIR::GetExprCategory(context.sem_ir(), inst_id) ==
  55. SemIR::ExprCategory::Pattern) {
  56. auto type_id = loc_id_and_inst.inst.type_id();
  57. CARBON_CHECK(type_id == SemIR::ErrorInst::TypeId ||
  58. context.types().Is<SemIR::PatternType>(type_id),
  59. "Unexpected kind for type {0}",
  60. context.types().GetAsInst(type_id));
  61. context.pattern_block_stack().AddInstId(inst_id);
  62. } else {
  63. context.inst_block_stack().AddInstId(inst_id);
  64. }
  65. return inst_id;
  66. }
  67. auto AddInstInNoBlock(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  68. -> SemIR::InstId {
  69. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  70. CARBON_VLOG_TO(context.vlog_stream(), "AddInst: {0}\n", loc_id_and_inst.inst);
  71. FinishInst(context, inst_id, loc_id_and_inst.inst);
  72. return inst_id;
  73. }
  74. auto AddDependentActionInst(Context& context,
  75. SemIR::LocIdAndInst loc_id_and_inst)
  76. -> SemIR::InstId {
  77. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  78. CARBON_VLOG_TO(context.vlog_stream(), "AddDependentActionInst: {0}\n",
  79. loc_id_and_inst.inst);
  80. // Set the constant value of this instruction to point back to itself.
  81. auto const_id = context.constant_values().AddSymbolicConstant(
  82. {.inst_id = inst_id,
  83. .generic_id = SemIR::GenericId::None,
  84. .index = SemIR::GenericInstIndex::None,
  85. .dependence = SemIR::ConstantDependence::Template});
  86. context.constant_values().Set(inst_id, const_id);
  87. // Register the instruction to be added to the eval block.
  88. AttachDependentInstToCurrentGeneric(
  89. context, {.inst_id = inst_id, .kind = DependentInstKind::Template});
  90. return inst_id;
  91. }
  92. auto GetOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  93. -> SemIR::InstId {
  94. CARBON_CHECK(!loc_id_and_inst.inst.kind().has_cleanup());
  95. auto handle_constant_id = [&](SemIR::ConstantId const_id) -> SemIR::InstId {
  96. CARBON_CHECK(const_id.has_value());
  97. // If we didn't produce a constant value for the instruction, we have to add
  98. // the instruction.
  99. if (!const_id.is_constant()) {
  100. return SemIR::InstId::None;
  101. }
  102. if (const_id.is_symbolic()) {
  103. // TODO: Only add this instruction to the eval block, and don't
  104. // re-evaluate it.
  105. return AddInst(context, loc_id_and_inst);
  106. }
  107. CARBON_VLOG_TO(context.vlog_stream(), "GetOrAddInst: constant: {0}\n",
  108. loc_id_and_inst.inst);
  109. return context.constant_values().GetInstId(const_id);
  110. };
  111. // If the instruction is from desugaring, produce its constant value instead
  112. // if possible.
  113. if (loc_id_and_inst.loc_id.is_desugared()) {
  114. switch (loc_id_and_inst.inst.kind().constant_needs_inst_id()) {
  115. case SemIR::InstConstantNeedsInstIdKind::No: {
  116. // Evaluation doesn't need an InstId. Just do it.
  117. auto const_id = TryEvalInstUnsafe(context, SemIR::InstId::None,
  118. loc_id_and_inst.inst);
  119. if (auto result_inst_id = handle_constant_id(const_id);
  120. result_inst_id.has_value()) {
  121. return result_inst_id;
  122. }
  123. break;
  124. }
  125. case SemIR::InstConstantNeedsInstIdKind::DuringEvaluation: {
  126. // Evaluation temporarily needs an InstId. Add one for now.
  127. auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  128. auto const_id = context.constant_values().Get(inst_id);
  129. if (auto result_inst_id = handle_constant_id(const_id);
  130. result_inst_id.has_value()) {
  131. // TODO: We didn't end up needing the `inst_id` instruction. Consider
  132. // removing it from `insts` if it's still the most recently added
  133. // instruction.
  134. CARBON_CHECK(result_inst_id != inst_id);
  135. return result_inst_id;
  136. }
  137. context.inst_block_stack().AddInstId(inst_id);
  138. return inst_id;
  139. }
  140. case SemIR::InstConstantNeedsInstIdKind::Permanent: {
  141. // Evaluation needs a permanent InstId. Add the instruction.
  142. break;
  143. }
  144. }
  145. }
  146. // TODO: For an implicit instruction, this reattempts evaluation.
  147. return AddInst(context, loc_id_and_inst);
  148. }
  149. auto EvalOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  150. -> SemIR::ConstantId {
  151. CARBON_CHECK(!loc_id_and_inst.inst.kind().has_cleanup());
  152. switch (loc_id_and_inst.inst.kind().constant_needs_inst_id()) {
  153. case SemIR::InstConstantNeedsInstIdKind::No: {
  154. // Evaluation doesn't need an InstId. Just do it.
  155. return TryEvalInstUnsafe(context, SemIR::InstId::None,
  156. loc_id_and_inst.inst);
  157. }
  158. case SemIR::InstConstantNeedsInstIdKind::DuringEvaluation: {
  159. // Evaluation temporarily needs an InstId. Add one for now. We add the
  160. // instruction outside of a block, and never call `FinishInst` for this
  161. // non-canonical instruction. This means it never gets attached to the
  162. // constant value, and is not added to any enclosing generic context's
  163. // eval block.
  164. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  165. CARBON_VLOG_TO(context.vlog_stream(), "AddInst: {0}\n",
  166. loc_id_and_inst.inst);
  167. // TODO: Consider removing `inst_id` from `insts` if it's still the most
  168. // recently added instruction.
  169. return TryEvalInstUnsafe(context, inst_id, loc_id_and_inst.inst);
  170. }
  171. case SemIR::InstConstantNeedsInstIdKind::Permanent: {
  172. // Evaluation needs a permanent InstId. Add the instruction.
  173. auto inst_id = AddInst(context, loc_id_and_inst);
  174. return context.constant_values().Get(inst_id);
  175. }
  176. }
  177. }
  178. auto AddPlaceholderInstInNoBlock(Context& context,
  179. SemIR::LocIdAndInst loc_id_and_inst)
  180. -> SemIR::InstId {
  181. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  182. CARBON_VLOG_TO(context.vlog_stream(), "AddPlaceholderInst: {0}\n",
  183. loc_id_and_inst.inst);
  184. context.constant_values().Set(inst_id, SemIR::ConstantId::None);
  185. return inst_id;
  186. }
  187. auto AddPlaceholderImportedInstInNoBlock(Context& context,
  188. SemIR::LocIdAndInst loc_id_and_inst)
  189. -> SemIR::InstId {
  190. auto inst_id = AddPlaceholderInstInNoBlock(context, loc_id_and_inst);
  191. context.imports().push_back(inst_id);
  192. return inst_id;
  193. }
  194. auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  195. -> SemIR::InstId {
  196. auto inst_id = AddPlaceholderInstInNoBlock(context, loc_id_and_inst);
  197. context.inst_block_stack().AddInstId(inst_id);
  198. return inst_id;
  199. }
  200. auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
  201. SemIR::InstId inst_id,
  202. SemIR::LocIdAndInst loc_id_and_inst)
  203. -> void {
  204. context.sem_ir().insts().SetLocIdAndInst(inst_id, loc_id_and_inst);
  205. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  206. loc_id_and_inst.inst);
  207. FinishInst(context, inst_id, loc_id_and_inst.inst);
  208. }
  209. auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
  210. SemIR::Inst inst) -> void {
  211. context.sem_ir().insts().Set(inst_id, inst);
  212. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  213. inst);
  214. FinishInst(context, inst_id, inst);
  215. }
  216. auto ReplaceInstPreservingConstantValue(Context& context, SemIR::InstId inst_id,
  217. SemIR::Inst inst) -> void {
  218. // Check that the type didn't change: a change of type will change the
  219. // constant value. Replace the type with the attached type.
  220. auto old_type_id = context.insts().GetAttachedType(inst_id);
  221. CARBON_CHECK(context.types().GetUnattachedType(old_type_id) == inst.type_id(),
  222. "Given wrong type for replacement instruction");
  223. inst.SetType(old_type_id);
  224. // Update the instruction.
  225. context.sem_ir().insts().Set(inst_id, inst);
  226. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  227. inst);
  228. // Check the constant value didn't change.
  229. auto old_const_id = context.constant_values().Get(inst_id);
  230. auto new_const_id = TryEvalInstUnsafe(context, inst_id, inst);
  231. CARBON_CHECK(old_const_id == new_const_id);
  232. }
  233. auto SetNamespaceNodeId(Context& context, SemIR::InstId inst_id,
  234. Parse::NodeId node_id) -> void {
  235. context.sem_ir().insts().SetLocId(inst_id, SemIR::LocId(node_id));
  236. }
  237. } // namespace Carbon::Check