Files
carbon-lang/toolchain/check/merge.cpp
T
Jon Ross-Perkins 92fa0ac1ac Remove ImportRefUsed (#3934)
This was to track use of a declaration after import, prior to a
redeclaration. Per [discussion on
Discord](https://discord.com/channels/655572317891461132/1217182321933815820/1236016521059237962),
we likely don't need this check due to the change in behavior of
`extern`.

Rather than potentially getting one of many `extern` decls and depending
on it by accident, it is now planned to be _required_ to be imported,
and the library doing a non-`extern` decl must _know_ it's importing the
`extern` decl. The stricter requirement on the library means it now
seems more reasonable to use the `extern` decl.

So kind of rolling back #3831, though keeping `ImportIRInstId` (at least
for now) and keeping `Loaded`/`Unloaded` terminology (seems a nicer
fit).
2024-05-03 23:33:42 +00:00

152 lines
5.7 KiB
C++

// 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, SemIR::InstId prev_inst_id)
-> InstForMerge {
InstForMerge result = {.inst = context.insts().Get(prev_inst_id),
.import_ir_inst_id = SemIR::ImportIRInstId::Invalid};
if (auto import_ref = result.inst.TryAs<SemIR::ImportRefLoaded>()) {
// Follow the import ref.
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());
}
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