handle_namespace.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include "toolchain/sem_ir/name_scope.h"
  12. #include "toolchain/sem_ir/typed_insts.h"
  13. namespace Carbon::Check {
  14. auto HandleParseNode(Context& context, Parse::NamespaceStartId /*node_id*/)
  15. -> bool {
  16. // Optional modifiers and the name follow.
  17. context.decl_introducer_state_stack().Push<Lex::TokenKind::Namespace>();
  18. context.decl_name_stack().PushScopeAndStartName();
  19. return true;
  20. }
  21. static auto IsNamespaceScope(Context& context, SemIR::NameScopeId name_scope_id)
  22. -> bool {
  23. auto [_, inst] = context.name_scopes().GetInstIfValid(name_scope_id);
  24. return inst && inst->Is<SemIR::Namespace>();
  25. }
  26. auto HandleParseNode(Context& context, Parse::NamespaceId node_id) -> bool {
  27. auto name_context = context.decl_name_stack().FinishName(
  28. PopNameComponentWithoutParams(context, Lex::TokenKind::Namespace));
  29. auto introducer =
  30. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Namespace>();
  31. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::None);
  32. auto namespace_inst = SemIR::Namespace{
  33. context.GetSingletonType(SemIR::NamespaceType::SingletonInstId),
  34. SemIR::NameScopeId::Invalid, SemIR::InstId::Invalid};
  35. auto namespace_id =
  36. context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, namespace_inst));
  37. auto [existing_inst_id, is_poisoned] =
  38. context.decl_name_stack().LookupOrAddName(name_context, namespace_id,
  39. SemIR::AccessKind::Public);
  40. if (is_poisoned) {
  41. context.DiagnosePoisonedName(namespace_id);
  42. } else if (existing_inst_id.is_valid()) {
  43. if (auto existing =
  44. context.insts().TryGetAs<SemIR::Namespace>(existing_inst_id)) {
  45. // If there's a name conflict with a namespace, "merge" by using the
  46. // previous declaration. Otherwise, diagnose the issue.
  47. // Point at the other namespace.
  48. namespace_inst.name_scope_id = existing->name_scope_id;
  49. if (context.name_scopes()
  50. .Get(existing->name_scope_id)
  51. .is_closed_import()) {
  52. // The existing name is a package name, so this is a name conflict.
  53. context.DiagnoseDuplicateName(namespace_id, existing_inst_id);
  54. // Treat this as a local namespace name from now on to avoid further
  55. // diagnostics.
  56. context.name_scopes()
  57. .Get(existing->name_scope_id)
  58. .set_is_closed_import(false);
  59. } else if (existing->import_id.is_valid() &&
  60. !context.insts().GetLocId(existing_inst_id).is_valid()) {
  61. // When the name conflict is an imported namespace, fill the location ID
  62. // so that future diagnostics point at this declaration.
  63. context.SetNamespaceNodeId(existing_inst_id, node_id);
  64. }
  65. } else {
  66. context.DiagnoseDuplicateName(namespace_id, existing_inst_id);
  67. }
  68. }
  69. // If we weren't able to merge namespaces, add a new name scope. Note this
  70. // occurs even for duplicates where we discard the namespace, because we want
  71. // to produce a valid constant.
  72. if (!namespace_inst.name_scope_id.is_valid()) {
  73. namespace_inst.name_scope_id = context.name_scopes().Add(
  74. namespace_id, name_context.name_id_for_new_inst(),
  75. name_context.parent_scope_id);
  76. if (!IsNamespaceScope(context, name_context.parent_scope_id)) {
  77. CARBON_DIAGNOSTIC(NamespaceDeclNotAtTopLevel, Error,
  78. "`namespace` declaration not at top level");
  79. context.emitter().Emit(node_id, NamespaceDeclNotAtTopLevel);
  80. }
  81. }
  82. context.ReplaceInstBeforeConstantUse(namespace_id, namespace_inst);
  83. context.decl_name_stack().PopScope();
  84. return true;
  85. }
  86. } // namespace Carbon::Check