handle_namespace.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // Push a pattern block to handle parameters of the namespace declaration.
  20. // TODO: Disallow these in parse, instead of check, so we don't have to do
  21. // this.
  22. context.pattern_block_stack().Push();
  23. return true;
  24. }
  25. static auto IsNamespaceScope(Context& context, SemIR::NameScopeId name_scope_id)
  26. -> bool {
  27. auto [_, inst] = context.name_scopes().GetInstIfValid(name_scope_id);
  28. return inst && inst->Is<SemIR::Namespace>();
  29. }
  30. auto HandleParseNode(Context& context, Parse::NamespaceId node_id) -> bool {
  31. auto name_context = context.decl_name_stack().FinishName(
  32. PopNameComponentWithoutParams(context, Lex::TokenKind::Namespace));
  33. auto introducer =
  34. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Namespace>();
  35. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::None);
  36. auto namespace_inst = SemIR::Namespace{
  37. context.GetSingletonType(SemIR::NamespaceType::SingletonInstId),
  38. SemIR::NameScopeId::Invalid, SemIR::InstId::Invalid};
  39. auto namespace_id =
  40. context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, namespace_inst));
  41. auto [existing_inst_id, is_poisoned] =
  42. context.decl_name_stack().LookupOrAddName(name_context, namespace_id,
  43. SemIR::AccessKind::Public);
  44. if (is_poisoned) {
  45. context.DiagnosePoisonedName(namespace_id);
  46. } else if (existing_inst_id.is_valid()) {
  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. context.DiagnoseDuplicateName(namespace_id, existing_inst_id);
  58. // Treat this as a local namespace name from now on to avoid further
  59. // diagnostics.
  60. context.name_scopes()
  61. .Get(existing->name_scope_id)
  62. .set_is_closed_import(false);
  63. } else if (existing->import_id.is_valid() &&
  64. !context.insts().GetLocId(existing_inst_id).is_valid()) {
  65. // When the name conflict is an imported namespace, fill the location ID
  66. // so that future diagnostics point at this declaration.
  67. context.SetNamespaceNodeId(existing_inst_id, node_id);
  68. }
  69. } else {
  70. context.DiagnoseDuplicateName(namespace_id, existing_inst_id);
  71. }
  72. }
  73. // If we weren't able to merge namespaces, add a new name scope. Note this
  74. // occurs even for duplicates where we discard the namespace, because we want
  75. // to produce a valid constant.
  76. if (!namespace_inst.name_scope_id.is_valid()) {
  77. namespace_inst.name_scope_id = context.name_scopes().Add(
  78. namespace_id, name_context.name_id_for_new_inst(),
  79. name_context.parent_scope_id);
  80. if (!IsNamespaceScope(context, name_context.parent_scope_id)) {
  81. CARBON_DIAGNOSTIC(NamespaceDeclNotAtTopLevel, Error,
  82. "`namespace` declaration not at top level");
  83. context.emitter().Emit(node_id, NamespaceDeclNotAtTopLevel);
  84. }
  85. }
  86. context.ReplaceInstBeforeConstantUse(namespace_id, namespace_inst);
  87. context.decl_name_stack().PopScope();
  88. return true;
  89. }
  90. } // namespace Carbon::Check