handle_namespace.cpp 4.2 KB

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