mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-16 13:18:11 +00:00
Trying to conform with #4009. Changes SemIR::LocIdAndInst construction to root out struct init cases with AddInst and related functions. I'm using templating of AddInst functions in order to avoid `AddInst(loc_id, InstName{...})` and instead have `AddInst<InstName>(loc_id, {...})` with I think similar readability results. There are a couple cases where inst construction is templated and so designated initializers couldn't be used, so this may be better for those in particular due to the extra type enforcement. This probably doesn't clean up every last case, but I was trying to get the bulk at once without bleeding over into less related changes. --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
61 lines
2.4 KiB
C++
61 lines
2.4 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/context.h"
|
|
#include "toolchain/check/decl_state.h"
|
|
#include "toolchain/check/modifiers.h"
|
|
#include "toolchain/check/name_component.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
#include "toolchain/sem_ir/inst.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto HandleNamespaceStart(Context& context, Parse::NamespaceStartId /*node_id*/)
|
|
-> bool {
|
|
// Optional modifiers and the name follow.
|
|
context.decl_state_stack().Push(DeclState::Namespace);
|
|
context.decl_name_stack().PushScopeAndStartName();
|
|
return true;
|
|
}
|
|
|
|
auto HandleNamespace(Context& context, Parse::NamespaceId node_id) -> bool {
|
|
auto name_context = context.decl_name_stack().FinishName(
|
|
PopNameComponentWithoutParams(context, Lex::TokenKind::Namespace));
|
|
LimitModifiersOnDecl(context, KeywordModifierSet::None,
|
|
Lex::TokenKind::Namespace);
|
|
auto namespace_inst = SemIR::Namespace{
|
|
context.GetBuiltinType(SemIR::BuiltinKind::NamespaceType),
|
|
SemIR::NameScopeId::Invalid, SemIR::InstId::Invalid};
|
|
auto namespace_id =
|
|
context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, namespace_inst));
|
|
namespace_inst.name_scope_id = context.name_scopes().Add(
|
|
namespace_id, name_context.name_id_for_new_inst(),
|
|
name_context.enclosing_scope_id_for_new_inst());
|
|
context.ReplaceInstBeforeConstantUse(namespace_id, namespace_inst);
|
|
|
|
auto existing_inst_id =
|
|
context.decl_name_stack().LookupOrAddName(name_context, namespace_id);
|
|
if (existing_inst_id.is_valid()) {
|
|
// If there's a name conflict with a namespace, "merge" by using the
|
|
// previous declaration. Otherwise, diagnose the issue.
|
|
if (auto existing =
|
|
context.insts().TryGetAs<SemIR::Namespace>(existing_inst_id)) {
|
|
// When the name conflict is an imported namespace, fill the location ID
|
|
// so that future diagnostics point at this declaration.
|
|
if (existing->import_id.is_valid() &&
|
|
!context.insts().GetLocId(existing_inst_id).is_valid()) {
|
|
context.SetNamespaceNodeId(existing_inst_id, node_id);
|
|
}
|
|
} else {
|
|
context.DiagnoseDuplicateName(namespace_id, existing_inst_id);
|
|
}
|
|
}
|
|
|
|
context.decl_name_stack().PopScope();
|
|
context.decl_state_stack().Pop(DeclState::Namespace);
|
|
return true;
|
|
}
|
|
|
|
} // namespace Carbon::Check
|