handle_namespace.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. return true;
  19. }
  20. auto HandleParseNode(Context& context, Parse::NamespaceId node_id) -> bool {
  21. auto name_context = context.decl_name_stack().FinishName(
  22. PopNameComponentWithoutParams(context, Lex::TokenKind::Namespace));
  23. auto introducer =
  24. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Namespace>();
  25. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::None);
  26. auto namespace_inst = SemIR::Namespace{
  27. context.GetBuiltinType(SemIR::BuiltinInstKind::NamespaceType),
  28. SemIR::NameScopeId::Invalid, SemIR::InstId::Invalid};
  29. auto namespace_id =
  30. context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, namespace_inst));
  31. auto existing_inst_id = context.decl_name_stack().LookupOrAddName(
  32. name_context, namespace_id, SemIR::AccessKind::Public);
  33. if (existing_inst_id.is_valid()) {
  34. // If there's a name conflict with a namespace, "merge" by using the
  35. // previous declaration. Otherwise, diagnose the issue.
  36. if (auto existing =
  37. context.insts().TryGetAs<SemIR::Namespace>(existing_inst_id)) {
  38. // Point at the other namespace.
  39. namespace_inst.name_scope_id = existing->name_scope_id;
  40. if (context.name_scopes().Get(existing->name_scope_id).is_closed_import) {
  41. // The existing name is a package name, so this is a name conflict.
  42. context.DiagnoseDuplicateName(namespace_id, existing_inst_id);
  43. // Treat this as a local namespace name from now on to avoid further
  44. // diagnostics.
  45. context.name_scopes().Get(existing->name_scope_id).is_closed_import =
  46. false;
  47. } else if (existing->import_id.is_valid() &&
  48. !context.insts().GetLocId(existing_inst_id).is_valid()) {
  49. // When the name conflict is an imported namespace, fill the location ID
  50. // so that future diagnostics point at this declaration.
  51. context.SetNamespaceNodeId(existing_inst_id, node_id);
  52. }
  53. } else {
  54. context.DiagnoseDuplicateName(namespace_id, existing_inst_id);
  55. }
  56. }
  57. // If we weren't able to merge namespaces, add a new name scope. Note this
  58. // occurs even for duplicates where we discard the namespace, because we want
  59. // to produce a valid constant.
  60. if (!namespace_inst.name_scope_id.is_valid()) {
  61. namespace_inst.name_scope_id = context.name_scopes().Add(
  62. namespace_id, name_context.name_id_for_new_inst(),
  63. name_context.parent_scope_id);
  64. }
  65. context.ReplaceInstBeforeConstantUse(namespace_id, namespace_inst);
  66. context.decl_name_stack().PopScope();
  67. return true;
  68. }
  69. } // namespace Carbon::Check