handle_namespace.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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_state.h"
  6. #include "toolchain/check/modifiers.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. #include "toolchain/sem_ir/inst.h"
  9. namespace Carbon::Check {
  10. auto HandleNamespaceStart(Context& context,
  11. Parse::NamespaceStartId /*parse_node*/) -> bool {
  12. // Optional modifiers and the name follow.
  13. context.decl_state_stack().Push(DeclState::Namespace);
  14. context.decl_name_stack().PushScopeAndStartName();
  15. return true;
  16. }
  17. auto HandleNamespace(Context& context, Parse::NamespaceId parse_node) -> bool {
  18. auto name_context = context.decl_name_stack().FinishName();
  19. LimitModifiersOnDecl(context, KeywordModifierSet::None,
  20. Lex::TokenKind::Namespace);
  21. auto namespace_inst = SemIR::Namespace{
  22. context.GetBuiltinType(SemIR::BuiltinKind::NamespaceType),
  23. name_context.name_id_for_new_inst(), SemIR::NameScopeId::Invalid};
  24. auto namespace_id = context.AddInst({parse_node, namespace_inst});
  25. namespace_inst.name_scope_id = context.name_scopes().Add(
  26. namespace_id, name_context.enclosing_scope_id_for_new_inst());
  27. context.insts().Set(namespace_id, namespace_inst);
  28. context.decl_name_stack().AddNameToLookup(name_context, namespace_id);
  29. context.decl_name_stack().PopScope();
  30. context.decl_state_stack().Pop(DeclState::Namespace);
  31. return true;
  32. }
  33. } // namespace Carbon::Check