handle_namespace.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 =
  36. SemIR::Namespace{GetSingletonType(context, SemIR::NamespaceType::InstId),
  37. SemIR::NameScopeId::None, SemIR::InstId::None};
  38. auto namespace_id = AddPlaceholderInst(context, node_id, namespace_inst);
  39. SemIR::ScopeLookupResult lookup_result =
  40. context.decl_name_stack().LookupOrAddName(name_context, namespace_id,
  41. SemIR::AccessKind::Public);
  42. if (lookup_result.is_poisoned()) {
  43. DiagnosePoisonedName(context, name_context.name_id_for_new_inst(),
  44. lookup_result.poisoning_loc_id(), name_context.loc_id);
  45. } else if (lookup_result.is_found()) {
  46. SemIR::InstId existing_inst_id = lookup_result.target_inst_id();
  47. if (auto existing =
  48. context.insts().TryGetAs<SemIR::Namespace>(existing_inst_id)) {
  49. // If there's a name conflict with a namespace, "merge" by using the
  50. // previous declaration. Otherwise, diagnose the issue.
  51. // Point at the other namespace.
  52. namespace_inst.name_scope_id = existing->name_scope_id;
  53. if (context.name_scopes()
  54. .Get(existing->name_scope_id)
  55. .is_closed_import()) {
  56. // The existing name is a package name, so this is a name conflict.
  57. DiagnoseDuplicateName(context, name_context.name_id,
  58. name_context.loc_id, existing_inst_id);
  59. // Treat this as a local namespace name from now on to avoid further
  60. // diagnostics.
  61. context.name_scopes()
  62. .Get(existing->name_scope_id)
  63. .set_is_closed_import(false);
  64. } else if (existing->import_id.has_value() &&
  65. !context.insts().GetLocId(existing_inst_id).has_value()) {
  66. // When the name conflict is an imported namespace, fill the location ID
  67. // so that future diagnostics point at this declaration.
  68. SetNamespaceNodeId(context, existing_inst_id, node_id);
  69. }
  70. } else {
  71. DiagnoseDuplicateName(context, name_context.name_id, name_context.loc_id,
  72. existing_inst_id);
  73. }
  74. }
  75. // If we weren't able to merge namespaces, add a new name scope. Note this
  76. // occurs even for duplicates where we discard the namespace, because we want
  77. // to produce a valid constant.
  78. if (!namespace_inst.name_scope_id.has_value()) {
  79. namespace_inst.name_scope_id = context.name_scopes().Add(
  80. namespace_id, name_context.name_id_for_new_inst(),
  81. name_context.parent_scope_id);
  82. if (!IsNamespaceScope(context, name_context.parent_scope_id)) {
  83. CARBON_DIAGNOSTIC(NamespaceDeclNotAtTopLevel, Error,
  84. "`namespace` declaration not at top level");
  85. context.emitter().Emit(node_id, NamespaceDeclNotAtTopLevel);
  86. }
  87. }
  88. ReplaceInstBeforeConstantUse(context, namespace_id, namespace_inst);
  89. context.decl_name_stack().PopScope();
  90. return true;
  91. }
  92. } // namespace Carbon::Check