merge.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/merge.h"
  5. #include "toolchain/base/kind_switch.h"
  6. #include "toolchain/check/function.h"
  7. #include "toolchain/check/import_ref.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. #include "toolchain/sem_ir/typed_insts.h"
  10. namespace Carbon::Check {
  11. auto ResolvePrevInstForMerge(Context& context, Parse::NodeId node_id,
  12. SemIR::InstId prev_inst_id) -> InstForMerge {
  13. auto prev_inst = context.insts().Get(prev_inst_id);
  14. auto import_ref = prev_inst.TryAs<SemIR::AnyImportRef>();
  15. // If not imported, use the instruction directly.
  16. if (!import_ref) {
  17. return {.inst = prev_inst,
  18. .import_ir_inst_id = SemIR::ImportIRInstId::Invalid};
  19. }
  20. // If the import ref was previously used, print a diagnostic.
  21. if (auto import_ref_used = prev_inst.TryAs<SemIR::ImportRefUsed>()) {
  22. CARBON_DIAGNOSTIC(
  23. RedeclOfUsedImport, Error,
  24. "Redeclaration of imported entity that was previously used.");
  25. CARBON_DIAGNOSTIC(UsedImportLoc, Note, "Import used here.");
  26. context.emitter()
  27. .Build(node_id, RedeclOfUsedImport)
  28. .Note(import_ref_used->used_id, UsedImportLoc)
  29. .Emit();
  30. }
  31. // Follow the import ref.
  32. return {.inst = context.insts().Get(
  33. context.constant_values().Get(prev_inst_id).inst_id()),
  34. .import_ir_inst_id = import_ref->import_ir_inst_id};
  35. }
  36. // Returns the instruction to consider when merging the given inst_id. Returns
  37. // nullopt if merging is infeasible and no diagnostic should be printed.
  38. static auto ResolveMergeableInst(Context& context, SemIR::InstId inst_id)
  39. -> std::optional<InstForMerge> {
  40. auto inst = context.insts().Get(inst_id);
  41. switch (inst.kind()) {
  42. case SemIR::ImportRefUnloaded::Kind:
  43. // Load before merging.
  44. LoadImportRef(context, inst_id, SemIR::LocId::Invalid);
  45. break;
  46. case SemIR::ImportRefUsed::Kind:
  47. // Already loaded.
  48. break;
  49. case SemIR::Namespace::Kind:
  50. // Return back the namespace directly.
  51. return {
  52. {.inst = inst, .import_ir_inst_id = SemIR::ImportIRInstId::Invalid}};
  53. default:
  54. CARBON_FATAL() << "Unexpected inst kind passed to ResolveMergeableInst: "
  55. << inst;
  56. }
  57. auto const_id = context.constant_values().Get(inst_id);
  58. // TODO: Function and type declarations are constant, but `var` declarations
  59. // are non-constant and should still merge.
  60. if (!const_id.is_constant()) {
  61. return std::nullopt;
  62. }
  63. return {
  64. {.inst = context.insts().Get(const_id.inst_id()),
  65. .import_ir_inst_id = inst.As<SemIR::AnyImportRef>().import_ir_inst_id}};
  66. }
  67. auto ReplacePrevInstForMerge(Context& context, SemIR::NameScopeId scope_id,
  68. SemIR::NameId name_id, SemIR::InstId new_inst_id)
  69. -> void {
  70. auto& names = context.name_scopes().Get(scope_id).names;
  71. auto it = names.find(name_id);
  72. if (it != names.end()) {
  73. it->second = new_inst_id;
  74. }
  75. }
  76. auto MergeImportRef(Context& context, SemIR::InstId new_inst_id,
  77. SemIR::InstId prev_inst_id) -> void {
  78. auto new_inst = ResolveMergeableInst(context, new_inst_id);
  79. auto prev_inst = ResolveMergeableInst(context, prev_inst_id);
  80. if (!new_inst || !prev_inst) {
  81. // TODO: Once `var` declarations get an associated instruction for handling,
  82. // it might be more appropriate to return without diagnosing here, to handle
  83. // invalid declarations.
  84. context.DiagnoseDuplicateName(new_inst_id, prev_inst_id);
  85. return;
  86. }
  87. if (new_inst->inst.kind() != prev_inst->inst.kind()) {
  88. context.DiagnoseDuplicateName(new_inst_id, prev_inst_id);
  89. return;
  90. }
  91. CARBON_KIND_SWITCH(new_inst->inst) {
  92. case CARBON_KIND(SemIR::FunctionDecl new_decl): {
  93. auto prev_decl = prev_inst->inst.As<SemIR::FunctionDecl>();
  94. auto new_fn = context.functions().Get(new_decl.function_id);
  95. // TODO: May need to "spoil" the new function to prevent it from being
  96. // emitted, since it will already be added.
  97. MergeFunctionRedecl(context, context.insts().GetLocId(new_inst_id),
  98. new_fn,
  99. /*new_is_import=*/true,
  100. /*new_is_definition=*/false, prev_decl.function_id,
  101. prev_inst->import_ir_inst_id);
  102. return;
  103. }
  104. default:
  105. context.TODO(new_inst_id, "Merging not yet supported.");
  106. return;
  107. }
  108. }
  109. } // namespace Carbon::Check