merge.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/class.h"
  7. #include "toolchain/check/function.h"
  8. #include "toolchain/check/import_ref.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. #include "toolchain/sem_ir/typed_insts.h"
  11. namespace Carbon::Check {
  12. CARBON_DIAGNOSTIC(RedeclPrevDecl, Note, "Previously declared here.");
  13. // Diagnoses a redeclaration which is redundant.
  14. static auto DiagnoseRedundant(Context& context, Lex::TokenKind decl_kind,
  15. SemIR::NameId name_id, SemIRLoc new_loc,
  16. SemIRLoc prev_loc) {
  17. CARBON_DIAGNOSTIC(RedeclRedundant, Error,
  18. "Redeclaration of `{0} {1}` is redundant.", Lex::TokenKind,
  19. SemIR::NameId);
  20. context.emitter()
  21. .Build(new_loc, RedeclRedundant, decl_kind, name_id)
  22. .Note(prev_loc, RedeclPrevDecl)
  23. .Emit();
  24. }
  25. // Diagnoses a redefinition.
  26. static auto DiagnoseRedef(Context& context, Lex::TokenKind decl_kind,
  27. SemIR::NameId name_id, SemIRLoc new_loc,
  28. SemIRLoc prev_loc) {
  29. CARBON_DIAGNOSTIC(RedeclRedef, Error, "Redefinition of `{0} {1}`.",
  30. Lex::TokenKind, SemIR::NameId);
  31. CARBON_DIAGNOSTIC(RedeclPrevDef, Note, "Previously defined here.");
  32. context.emitter()
  33. .Build(new_loc, RedeclRedef, decl_kind, name_id)
  34. .Note(prev_loc, RedeclPrevDef)
  35. .Emit();
  36. }
  37. // Diagnoses an `extern` versus non-`extern` mismatch.
  38. static auto DiagnoseExternMismatch(Context& context, Lex::TokenKind decl_kind,
  39. SemIR::NameId name_id, SemIRLoc new_loc,
  40. SemIRLoc prev_loc) {
  41. CARBON_DIAGNOSTIC(RedeclExternMismatch, Error,
  42. "Redeclarations of `{0} {1}` in the same library must "
  43. "match use of `extern`.",
  44. Lex::TokenKind, SemIR::NameId);
  45. context.emitter()
  46. .Build(new_loc, RedeclExternMismatch, decl_kind, name_id)
  47. .Note(prev_loc, RedeclPrevDecl)
  48. .Emit();
  49. }
  50. // Diagnoses when multiple non-`extern` declarations are found.
  51. static auto DiagnoseNonExtern(Context& context, Lex::TokenKind decl_kind,
  52. SemIR::NameId name_id, SemIRLoc new_loc,
  53. SemIRLoc prev_loc) {
  54. CARBON_DIAGNOSTIC(RedeclNonExtern, Error,
  55. "Only one library can declare `{0} {1}` without `extern`.",
  56. Lex::TokenKind, SemIR::NameId);
  57. context.emitter()
  58. .Build(new_loc, RedeclNonExtern, decl_kind, name_id)
  59. .Note(prev_loc, RedeclPrevDecl)
  60. .Emit();
  61. }
  62. // Checks to see if a structurally valid redeclaration is allowed in context.
  63. // These all still merge.
  64. auto CheckIsAllowedRedecl(Context& context, Lex::TokenKind decl_kind,
  65. SemIR::NameId name_id, RedeclInfo new_decl,
  66. RedeclInfo prev_decl,
  67. SemIR::ImportIRInstId prev_import_ir_inst_id)
  68. -> void {
  69. if (!prev_import_ir_inst_id.is_valid()) {
  70. // Check for disallowed redeclarations in the same file.
  71. if (!new_decl.is_definition) {
  72. DiagnoseRedundant(context, decl_kind, name_id, new_decl.loc,
  73. prev_decl.loc);
  74. return;
  75. }
  76. if (prev_decl.is_definition) {
  77. DiagnoseRedef(context, decl_kind, name_id, new_decl.loc, prev_decl.loc);
  78. return;
  79. }
  80. // `extern` definitions are prevented at creation; this is only
  81. // checking for a non-`extern` definition after an `extern` declaration.
  82. if (prev_decl.is_extern) {
  83. DiagnoseExternMismatch(context, decl_kind, name_id, new_decl.loc,
  84. prev_decl.loc);
  85. return;
  86. }
  87. return;
  88. }
  89. auto import_ir_id =
  90. context.import_ir_insts().Get(prev_import_ir_inst_id).ir_id;
  91. if (import_ir_id == SemIR::ImportIRId::ApiForImpl) {
  92. // Check for disallowed redeclarations in the same library. Note that a
  93. // forward declaration in the impl is allowed.
  94. if (prev_decl.is_definition) {
  95. if (new_decl.is_definition) {
  96. DiagnoseRedef(context, decl_kind, name_id, new_decl.loc, prev_decl.loc);
  97. } else {
  98. DiagnoseRedundant(context, decl_kind, name_id, new_decl.loc,
  99. prev_decl.loc);
  100. }
  101. return;
  102. }
  103. if (prev_decl.is_extern != new_decl.is_extern) {
  104. DiagnoseExternMismatch(context, decl_kind, name_id, new_decl.loc,
  105. prev_decl.loc);
  106. return;
  107. }
  108. return;
  109. }
  110. // Check for disallowed redeclarations cross-library.
  111. if (!new_decl.is_extern && !prev_decl.is_extern) {
  112. DiagnoseNonExtern(context, decl_kind, name_id, new_decl.loc, prev_decl.loc);
  113. return;
  114. }
  115. }
  116. auto ResolvePrevInstForMerge(Context& context, Parse::NodeId node_id,
  117. SemIR::InstId prev_inst_id) -> InstForMerge {
  118. InstForMerge result = {.inst = context.insts().Get(prev_inst_id),
  119. .import_ir_inst_id = SemIR::ImportIRInstId::Invalid,
  120. .is_extern = false};
  121. CARBON_KIND_SWITCH(result.inst) {
  122. case CARBON_KIND(SemIR::ExternDecl extern_decl): {
  123. result.is_extern = true;
  124. result.inst = context.insts().Get(extern_decl.decl_id);
  125. break;
  126. }
  127. case CARBON_KIND(SemIR::ImportRefUsed import_ref): {
  128. CARBON_DIAGNOSTIC(
  129. RedeclOfUsedImport, Error,
  130. "Redeclaration of imported entity that was previously used.");
  131. CARBON_DIAGNOSTIC(UsedImportLoc, Note, "Import used here.");
  132. context.emitter()
  133. .Build(node_id, RedeclOfUsedImport)
  134. .Note(import_ref.used_id, UsedImportLoc)
  135. .Emit();
  136. [[fallthrough]];
  137. }
  138. case SemIR::ImportRefLoaded::Kind: {
  139. // Follow the import ref.
  140. auto import_ref = result.inst.As<SemIR::AnyImportRef>();
  141. result.import_ir_inst_id = import_ref.import_ir_inst_id;
  142. result.inst = context.insts().Get(
  143. context.constant_values().Get(prev_inst_id).inst_id());
  144. if (auto extern_type = result.inst.TryAs<SemIR::ExternType>()) {
  145. result.inst =
  146. context.types().GetAsInst(extern_type->non_extern_type_id);
  147. }
  148. break;
  149. }
  150. default:
  151. break;
  152. }
  153. return result;
  154. }
  155. // Returns the instruction to consider when merging the given inst_id. Returns
  156. // nullopt if merging is infeasible and no diagnostic should be printed.
  157. static auto ResolveMergeableInst(Context& context, SemIR::InstId inst_id)
  158. -> std::optional<InstForMerge> {
  159. auto inst = context.insts().Get(inst_id);
  160. switch (inst.kind()) {
  161. case SemIR::ImportRefUnloaded::Kind:
  162. // Load before merging.
  163. LoadImportRef(context, inst_id, SemIR::LocId::Invalid);
  164. break;
  165. case SemIR::ImportRefLoaded::Kind:
  166. case SemIR::ImportRefUsed::Kind:
  167. // Already loaded.
  168. break;
  169. case SemIR::Namespace::Kind:
  170. // Return back the namespace directly.
  171. return {
  172. {.inst = inst, .import_ir_inst_id = SemIR::ImportIRInstId::Invalid}};
  173. default:
  174. CARBON_FATAL() << "Unexpected inst kind passed to ResolveMergeableInst: "
  175. << inst;
  176. }
  177. auto const_id = context.constant_values().Get(inst_id);
  178. // TODO: Function and type declarations are constant, but `var` declarations
  179. // are non-constant and should still merge.
  180. if (!const_id.is_constant()) {
  181. return std::nullopt;
  182. }
  183. InstForMerge result = {
  184. .inst = context.insts().Get(const_id.inst_id()),
  185. .import_ir_inst_id = inst.As<SemIR::AnyImportRef>().import_ir_inst_id,
  186. .is_extern = false};
  187. if (auto extern_type = result.inst.TryAs<SemIR::ExternType>()) {
  188. result.is_extern = true;
  189. result.inst = context.types().GetAsInst(extern_type->non_extern_type_id);
  190. }
  191. return result;
  192. }
  193. auto ReplacePrevInstForMerge(Context& context, SemIR::NameScopeId scope_id,
  194. SemIR::NameId name_id, SemIR::InstId new_inst_id)
  195. -> void {
  196. auto& names = context.name_scopes().Get(scope_id).names;
  197. auto it = names.find(name_id);
  198. if (it != names.end()) {
  199. it->second = new_inst_id;
  200. }
  201. }
  202. // TODO: On successful merges, this may need to "spoil" new_inst_id in order to
  203. // prevent it from being emitted in lowering.
  204. auto MergeImportRef(Context& context, SemIR::InstId new_inst_id,
  205. SemIR::InstId prev_inst_id) -> void {
  206. auto new_inst = ResolveMergeableInst(context, new_inst_id);
  207. auto prev_inst = ResolveMergeableInst(context, prev_inst_id);
  208. if (!new_inst || !prev_inst) {
  209. // TODO: Once `var` declarations get an associated instruction for handling,
  210. // it might be more appropriate to return without diagnosing here, to handle
  211. // invalid declarations.
  212. context.DiagnoseDuplicateName(new_inst_id, prev_inst_id);
  213. return;
  214. }
  215. if (new_inst->inst.kind() != prev_inst->inst.kind()) {
  216. context.DiagnoseDuplicateName(new_inst_id, prev_inst_id);
  217. return;
  218. }
  219. CARBON_KIND_SWITCH(new_inst->inst) {
  220. case CARBON_KIND(SemIR::FunctionDecl new_decl): {
  221. auto prev_decl = prev_inst->inst.TryAs<SemIR::FunctionDecl>();
  222. if (!prev_decl) {
  223. break;
  224. }
  225. auto new_fn = context.functions().Get(new_decl.function_id);
  226. MergeFunctionRedecl(context, new_inst_id, new_fn,
  227. /*new_is_import=*/true,
  228. /*new_is_definition=*/false, prev_decl->function_id,
  229. prev_inst->import_ir_inst_id);
  230. return;
  231. }
  232. case CARBON_KIND(SemIR::ClassType new_type): {
  233. auto prev_type = prev_inst->inst.TryAs<SemIR::ClassType>();
  234. if (!prev_type) {
  235. break;
  236. }
  237. auto new_class = context.classes().Get(new_type.class_id);
  238. MergeClassRedecl(context, new_inst_id, new_class,
  239. /*new_is_import=*/true, new_class.is_defined(),
  240. new_inst->is_extern, prev_type->class_id,
  241. prev_inst->is_extern, prev_inst->import_ir_inst_id);
  242. return;
  243. }
  244. default:
  245. context.TODO(new_inst_id, llvm::formatv("Merging {0} not yet supported.",
  246. new_inst->inst.kind()));
  247. return;
  248. }
  249. }
  250. } // namespace Carbon::Check