handle_namespace.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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, Parse::NodeId /*parse_node*/)
  11. -> 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::NodeId 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. parse_node, context.GetBuiltinType(SemIR::BuiltinKind::NamespaceType),
  23. SemIR::NameScopeId::Invalid};
  24. auto namespace_id = context.AddInst(namespace_inst);
  25. namespace_inst.name_scope_id = context.name_scopes().Add(namespace_id);
  26. context.insts().Set(namespace_id, namespace_inst);
  27. context.decl_name_stack().AddNameToLookup(name_context, namespace_id);
  28. context.decl_name_stack().PopScope();
  29. context.decl_state_stack().Pop(DeclState::Namespace);
  30. return true;
  31. }
  32. } // namespace Carbon::Check