handle_namespace.cpp 4.7 KB

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