handle_namespace.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. namespace Carbon::Check {
  13. auto HandleParseNode(Context& context, Parse::NamespaceStartId /*node_id*/)
  14. -> bool {
  15. // Optional modifiers and the name follow.
  16. context.decl_introducer_state_stack().Push<Lex::TokenKind::Namespace>();
  17. context.decl_name_stack().PushScopeAndStartName();
  18. // Push a pattern block to handle parameters of the namespace declaration.
  19. // TODO: Disallow these in parse, instead of check, so we don't have to do
  20. // this.
  21. context.pattern_block_stack().Push();
  22. return true;
  23. }
  24. auto HandleParseNode(Context& context, Parse::NamespaceId node_id) -> bool {
  25. auto name_context = context.decl_name_stack().FinishName(
  26. PopNameComponentWithoutParams(context, Lex::TokenKind::Namespace));
  27. auto introducer =
  28. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Namespace>();
  29. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::None);
  30. auto namespace_inst = SemIR::Namespace{
  31. context.GetSingletonType(SemIR::NamespaceType::SingletonInstId),
  32. SemIR::NameScopeId::Invalid, SemIR::InstId::Invalid};
  33. auto namespace_id =
  34. context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, namespace_inst));
  35. auto [existing_inst_id, is_poisoned] =
  36. context.decl_name_stack().LookupOrAddName(name_context, namespace_id,
  37. SemIR::AccessKind::Public);
  38. if (is_poisoned) {
  39. context.DiagnosePoisonedName(namespace_id);
  40. } else if (existing_inst_id.is_valid()) {
  41. if (auto existing =
  42. context.insts().TryGetAs<SemIR::Namespace>(existing_inst_id)) {
  43. // If there's a name conflict with a namespace, "merge" by using the
  44. // previous declaration. Otherwise, diagnose the issue.
  45. // Point at the other namespace.
  46. namespace_inst.name_scope_id = existing->name_scope_id;
  47. if (context.name_scopes()
  48. .Get(existing->name_scope_id)
  49. .is_closed_import()) {
  50. // The existing name is a package name, so this is a name conflict.
  51. context.DiagnoseDuplicateName(namespace_id, existing_inst_id);
  52. // Treat this as a local namespace name from now on to avoid further
  53. // diagnostics.
  54. context.name_scopes()
  55. .Get(existing->name_scope_id)
  56. .set_is_closed_import(false);
  57. } else if (existing->import_id.is_valid() &&
  58. !context.insts().GetLocId(existing_inst_id).is_valid()) {
  59. // When the name conflict is an imported namespace, fill the location ID
  60. // so that future diagnostics point at this declaration.
  61. context.SetNamespaceNodeId(existing_inst_id, node_id);
  62. }
  63. } else {
  64. context.DiagnoseDuplicateName(namespace_id, existing_inst_id);
  65. }
  66. }
  67. // If we weren't able to merge namespaces, add a new name scope. Note this
  68. // occurs even for duplicates where we discard the namespace, because we want
  69. // to produce a valid constant.
  70. if (!namespace_inst.name_scope_id.is_valid()) {
  71. namespace_inst.name_scope_id = context.name_scopes().Add(
  72. namespace_id, name_context.name_id_for_new_inst(),
  73. name_context.parent_scope_id);
  74. }
  75. context.ReplaceInstBeforeConstantUse(namespace_id, namespace_inst);
  76. context.decl_name_stack().PopScope();
  77. return true;
  78. }
  79. } // namespace Carbon::Check