inst.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. namespace Carbon::Check {
  9. // Finish producing an instruction. Set its constant value, and register it in
  10. // any applicable instruction lists.
  11. static auto FinishInst(Context& context, SemIR::InstId inst_id,
  12. SemIR::Inst inst) -> void {
  13. GenericRegionStack::DependencyKind dep_kind =
  14. GenericRegionStack::DependencyKind::None;
  15. // If the instruction has a symbolic constant type, track that we need to
  16. // substitute into it.
  17. if (context.constant_values().DependsOnGenericParameter(
  18. context.types().GetConstantId(inst.type_id()))) {
  19. dep_kind |= GenericRegionStack::DependencyKind::SymbolicType;
  20. }
  21. // If the instruction has a constant value, compute it.
  22. auto const_id = TryEvalInst(context, inst_id, inst);
  23. context.constant_values().Set(inst_id, const_id);
  24. if (const_id.is_constant()) {
  25. CARBON_VLOG_TO(context.vlog_stream(), "Constant: {0} -> {1}\n", inst,
  26. context.constant_values().GetInstId(const_id));
  27. // If the constant value is symbolic, track that we need to substitute into
  28. // it.
  29. if (context.constant_values().DependsOnGenericParameter(const_id)) {
  30. dep_kind |= GenericRegionStack::DependencyKind::SymbolicConstant;
  31. }
  32. }
  33. // Keep track of dependent instructions.
  34. if (dep_kind != GenericRegionStack::DependencyKind::None) {
  35. // TODO: Also check for template-dependent instructions.
  36. context.generic_region_stack().AddDependentInst(
  37. {.inst_id = inst_id, .kind = dep_kind});
  38. }
  39. }
  40. auto AddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  41. -> SemIR::InstId {
  42. auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  43. context.inst_block_stack().AddInstId(inst_id);
  44. return inst_id;
  45. }
  46. auto AddInstInNoBlock(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  47. -> SemIR::InstId {
  48. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  49. CARBON_VLOG_TO(context.vlog_stream(), "AddInst: {0}\n", loc_id_and_inst.inst);
  50. FinishInst(context, inst_id, loc_id_and_inst.inst);
  51. return inst_id;
  52. }
  53. auto AddPatternInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  54. -> SemIR::InstId {
  55. auto inst_id = AddInstInNoBlock(context, loc_id_and_inst);
  56. context.pattern_block_stack().AddInstId(inst_id);
  57. return inst_id;
  58. }
  59. auto GetOrAddInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  60. -> SemIR::InstId {
  61. if (loc_id_and_inst.loc_id.is_implicit()) {
  62. auto const_id =
  63. TryEvalInst(context, SemIR::InstId::None, loc_id_and_inst.inst);
  64. if (const_id.has_value()) {
  65. CARBON_VLOG_TO(context.vlog_stream(), "GetOrAddInst: constant: {0}\n",
  66. loc_id_and_inst.inst);
  67. return context.constant_values().GetInstId(const_id);
  68. }
  69. }
  70. // TODO: For an implicit instruction, this reattempts evaluation.
  71. return AddInst(context, loc_id_and_inst);
  72. }
  73. auto AddPlaceholderInstInNoBlock(Context& context,
  74. SemIR::LocIdAndInst loc_id_and_inst)
  75. -> SemIR::InstId {
  76. auto inst_id = context.sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  77. CARBON_VLOG_TO(context.vlog_stream(), "AddPlaceholderInst: {0}\n",
  78. loc_id_and_inst.inst);
  79. context.constant_values().Set(inst_id, SemIR::ConstantId::None);
  80. return inst_id;
  81. }
  82. auto AddPlaceholderInst(Context& context, SemIR::LocIdAndInst loc_id_and_inst)
  83. -> SemIR::InstId {
  84. auto inst_id = AddPlaceholderInstInNoBlock(context, loc_id_and_inst);
  85. context.inst_block_stack().AddInstId(inst_id);
  86. return inst_id;
  87. }
  88. auto ReplaceLocIdAndInstBeforeConstantUse(Context& context,
  89. SemIR::InstId inst_id,
  90. SemIR::LocIdAndInst loc_id_and_inst)
  91. -> void {
  92. context.sem_ir().insts().SetLocIdAndInst(inst_id, loc_id_and_inst);
  93. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  94. loc_id_and_inst.inst);
  95. FinishInst(context, inst_id, loc_id_and_inst.inst);
  96. }
  97. auto ReplaceInstBeforeConstantUse(Context& context, SemIR::InstId inst_id,
  98. SemIR::Inst inst) -> void {
  99. context.sem_ir().insts().Set(inst_id, inst);
  100. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  101. inst);
  102. FinishInst(context, inst_id, inst);
  103. }
  104. auto ReplaceInstPreservingConstantValue(Context& context, SemIR::InstId inst_id,
  105. SemIR::Inst inst) -> void {
  106. auto old_const_id = context.constant_values().Get(inst_id);
  107. context.sem_ir().insts().Set(inst_id, inst);
  108. CARBON_VLOG_TO(context.vlog_stream(), "ReplaceInst: {0} -> {1}\n", inst_id,
  109. inst);
  110. auto new_const_id = TryEvalInst(context, inst_id, inst);
  111. CARBON_CHECK(old_const_id == new_const_id);
  112. }
  113. auto SetNamespaceNodeId(Context& context, SemIR::InstId inst_id,
  114. Parse::NodeId node_id) -> void {
  115. context.sem_ir().insts().SetLocId(inst_id, SemIR::LocId(node_id));
  116. }
  117. } // namespace Carbon::Check