handle_namespace.cpp 4.4 KB

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