handle_namespace.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  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/inst.h"
  8. namespace Carbon::Check {
  9. auto HandleNamespaceStart(Context& context, Parse::NodeId parse_node) -> bool {
  10. // Optional modifiers and the name follow.
  11. context.decl_state_stack().Push(DeclState::Namespace, parse_node);
  12. context.decl_name_stack().PushScopeAndStartName();
  13. return true;
  14. }
  15. auto HandleNamespace(Context& context, Parse::NodeId /*parse_node*/) -> bool {
  16. auto name_context = context.decl_name_stack().FinishName();
  17. auto first_node = context.decl_state_stack().innermost().first_node;
  18. LimitModifiersOnDecl(context, KeywordModifierSet::None,
  19. Lex::TokenKind::Namespace);
  20. auto namespace_id = context.AddInst(SemIR::Namespace{
  21. first_node, context.GetBuiltinType(SemIR::BuiltinKind::NamespaceType),
  22. context.name_scopes().Add()});
  23. context.decl_name_stack().AddNameToLookup(name_context, namespace_id);
  24. context.decl_name_stack().PopScope();
  25. context.decl_state_stack().Pop(DeclState::Namespace);
  26. return true;
  27. }
  28. } // namespace Carbon::Check