inst.cpp 11 KB

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