handle_namespace.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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/context.h"
  5. #include "toolchain/check/decl_introducer_state.h"
  6. #include "toolchain/check/handle.h"
  7. #include "toolchain/check/modifiers.h"
  8. #include "toolchain/check/name_component.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. #include "toolchain/sem_ir/inst.h"
  11. namespace Carbon::Check {
  12. auto HandleNamespaceStart(Context& context, Parse::NamespaceStartId /*node_id*/)
  13. -> bool {
  14. // Optional modifiers and the name follow.
  15. context.decl_introducer_state_stack().Push(DeclIntroducerState::Namespace);
  16. context.decl_name_stack().PushScopeAndStartName();
  17. return true;
  18. }
  19. auto HandleNamespace(Context& context, Parse::NamespaceId node_id) -> bool {
  20. auto name_context = context.decl_name_stack().FinishName(
  21. PopNameComponentWithoutParams(context, Lex::TokenKind::Namespace));
  22. auto introducer =
  23. context.decl_introducer_state_stack().Pop(DeclIntroducerState::Namespace);
  24. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::None);
  25. auto namespace_inst = SemIR::Namespace{
  26. context.GetBuiltinType(SemIR::BuiltinKind::NamespaceType),
  27. SemIR::NameScopeId::Invalid, SemIR::InstId::Invalid};
  28. auto namespace_id =
  29. context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, namespace_inst));
  30. namespace_inst.name_scope_id = context.name_scopes().Add(
  31. namespace_id, name_context.name_id_for_new_inst(),
  32. name_context.parent_scope_id_for_new_inst());
  33. context.ReplaceInstBeforeConstantUse(namespace_id, namespace_inst);
  34. auto existing_inst_id =
  35. context.decl_name_stack().LookupOrAddName(name_context, namespace_id);
  36. if (existing_inst_id.is_valid()) {
  37. // If there's a name conflict with a namespace, "merge" by using the
  38. // previous declaration. Otherwise, diagnose the issue.
  39. if (auto existing =
  40. context.insts().TryGetAs<SemIR::Namespace>(existing_inst_id)) {
  41. if (context.name_scopes().Get(existing->name_scope_id).is_closed_import) {
  42. // The existing name is a package name, so this is a name conflict.
  43. context.DiagnoseDuplicateName(namespace_id, existing_inst_id);
  44. // Treat this as a local namespace name from now on to avoid further
  45. // diagnostics.
  46. context.name_scopes().Get(existing->name_scope_id).is_closed_import =
  47. false;
  48. } else if (existing->import_id.is_valid() &&
  49. !context.insts().GetLocId(existing_inst_id).is_valid()) {
  50. // When the name conflict is an imported namespace, fill the location ID
  51. // so that future diagnostics point at this declaration.
  52. context.SetNamespaceNodeId(existing_inst_id, node_id);
  53. }
  54. } else {
  55. context.DiagnoseDuplicateName(namespace_id, existing_inst_id);
  56. }
  57. }
  58. context.decl_name_stack().PopScope();
  59. return true;
  60. }
  61. } // namespace Carbon::Check