// Part of the Carbon Language project, under the Apache License v2.0 with LLVM // Exceptions. See /LICENSE for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception #include "toolchain/check/merge.h" #include "toolchain/base/kind_switch.h" #include "toolchain/check/class.h" #include "toolchain/check/function.h" #include "toolchain/check/import_ref.h" #include "toolchain/sem_ir/ids.h" #include "toolchain/sem_ir/typed_insts.h" namespace Carbon::Check { CARBON_DIAGNOSTIC(RedeclPrevDecl, Note, "Previously declared here."); // Diagnoses a redeclaration which is redundant. static auto DiagnoseRedundant(Context& context, Lex::TokenKind decl_kind, SemIR::NameId name_id, SemIRLoc new_loc, SemIRLoc prev_loc) { CARBON_DIAGNOSTIC(RedeclRedundant, Error, "Redeclaration of `{0} {1}` is redundant.", Lex::TokenKind, SemIR::NameId); context.emitter() .Build(new_loc, RedeclRedundant, decl_kind, name_id) .Note(prev_loc, RedeclPrevDecl) .Emit(); } // Diagnoses a redefinition. static auto DiagnoseRedef(Context& context, Lex::TokenKind decl_kind, SemIR::NameId name_id, SemIRLoc new_loc, SemIRLoc prev_loc) { CARBON_DIAGNOSTIC(RedeclRedef, Error, "Redefinition of `{0} {1}`.", Lex::TokenKind, SemIR::NameId); CARBON_DIAGNOSTIC(RedeclPrevDef, Note, "Previously defined here."); context.emitter() .Build(new_loc, RedeclRedef, decl_kind, name_id) .Note(prev_loc, RedeclPrevDef) .Emit(); } // Diagnoses an `extern` versus non-`extern` mismatch. static auto DiagnoseExternMismatch(Context& context, Lex::TokenKind decl_kind, SemIR::NameId name_id, SemIRLoc new_loc, SemIRLoc prev_loc) { CARBON_DIAGNOSTIC(RedeclExternMismatch, Error, "Redeclarations of `{0} {1}` in the same library must " "match use of `extern`.", Lex::TokenKind, SemIR::NameId); context.emitter() .Build(new_loc, RedeclExternMismatch, decl_kind, name_id) .Note(prev_loc, RedeclPrevDecl) .Emit(); } // Diagnoses when multiple non-`extern` declarations are found. static auto DiagnoseNonExtern(Context& context, Lex::TokenKind decl_kind, SemIR::NameId name_id, SemIRLoc new_loc, SemIRLoc prev_loc) { CARBON_DIAGNOSTIC(RedeclNonExtern, Error, "Only one library can declare `{0} {1}` without `extern`.", Lex::TokenKind, SemIR::NameId); context.emitter() .Build(new_loc, RedeclNonExtern, decl_kind, name_id) .Note(prev_loc, RedeclPrevDecl) .Emit(); } // Checks to see if a structurally valid redeclaration is allowed in context. // These all still merge. auto CheckIsAllowedRedecl(Context& context, Lex::TokenKind decl_kind, SemIR::NameId name_id, RedeclInfo new_decl, RedeclInfo prev_decl, SemIR::ImportIRInstId prev_import_ir_inst_id) -> void { if (!prev_import_ir_inst_id.is_valid()) { // Check for disallowed redeclarations in the same file. if (!new_decl.is_definition) { DiagnoseRedundant(context, decl_kind, name_id, new_decl.loc, prev_decl.loc); return; } if (prev_decl.is_definition) { DiagnoseRedef(context, decl_kind, name_id, new_decl.loc, prev_decl.loc); return; } // `extern` definitions are prevented at creation; this is only // checking for a non-`extern` definition after an `extern` declaration. if (prev_decl.is_extern) { DiagnoseExternMismatch(context, decl_kind, name_id, new_decl.loc, prev_decl.loc); return; } return; } auto import_ir_id = context.import_ir_insts().Get(prev_import_ir_inst_id).ir_id; if (import_ir_id == SemIR::ImportIRId::ApiForImpl) { // Check for disallowed redeclarations in the same library. Note that a // forward declaration in the impl is allowed. if (prev_decl.is_definition) { if (new_decl.is_definition) { DiagnoseRedef(context, decl_kind, name_id, new_decl.loc, prev_decl.loc); } else { DiagnoseRedundant(context, decl_kind, name_id, new_decl.loc, prev_decl.loc); } return; } if (prev_decl.is_extern != new_decl.is_extern) { DiagnoseExternMismatch(context, decl_kind, name_id, new_decl.loc, prev_decl.loc); return; } return; } // Check for disallowed redeclarations cross-library. if (!new_decl.is_extern && !prev_decl.is_extern) { DiagnoseNonExtern(context, decl_kind, name_id, new_decl.loc, prev_decl.loc); return; } } auto ResolvePrevInstForMerge(Context& context, Parse::NodeId node_id, SemIR::InstId prev_inst_id) -> InstForMerge { InstForMerge result = {.inst = context.insts().Get(prev_inst_id), .import_ir_inst_id = SemIR::ImportIRInstId::Invalid}; CARBON_KIND_SWITCH(result.inst) { case CARBON_KIND(SemIR::ImportRefUsed import_ref): { CARBON_DIAGNOSTIC( RedeclOfUsedImport, Error, "Redeclaration of imported entity that was previously used."); CARBON_DIAGNOSTIC(UsedImportLoc, Note, "Import used here."); context.emitter() .Build(node_id, RedeclOfUsedImport) .Note(import_ref.used_id, UsedImportLoc) .Emit(); [[fallthrough]]; } case SemIR::ImportRefLoaded::Kind: { // Follow the import ref. auto import_ref = result.inst.As(); result.import_ir_inst_id = import_ref.import_ir_inst_id; result.inst = context.insts().Get( context.constant_values().Get(prev_inst_id).inst_id()); break; } default: break; } return result; } auto ReplacePrevInstForMerge(Context& context, SemIR::NameScopeId scope_id, SemIR::NameId name_id, SemIR::InstId new_inst_id) -> void { auto& names = context.name_scopes().Get(scope_id).names; auto it = names.find(name_id); if (it != names.end()) { it->second = new_inst_id; } } } // namespace Carbon::Check