handle_impl.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. namespace Carbon::Check {
  6. auto HandleImplIntroducer(Context& context, Parse::ImplIntroducerId parse_node)
  7. -> bool {
  8. // Create an instruction block to hold the instructions created for the type
  9. // and interface.
  10. context.inst_block_stack().Push();
  11. // Push the bracketing node.
  12. context.node_stack().Push(parse_node);
  13. // Optional modifiers follow.
  14. context.decl_state_stack().Push(DeclState::Impl);
  15. // Create a scope for implicit parameters. We may not use it, but it's simpler
  16. // to create it unconditionally than to track whether it exists.
  17. context.PushScope();
  18. return true;
  19. }
  20. auto HandleImplForall(Context& context, Parse::ImplForallId parse_node)
  21. -> bool {
  22. auto params_id =
  23. context.node_stack().Pop<Parse::NodeKind::ImplicitParamList>();
  24. context.node_stack().Push(parse_node, params_id);
  25. return true;
  26. }
  27. auto HandleImplAs(Context& /*context*/, Parse::ImplAsId /*parse_node*/)
  28. -> bool {
  29. return true;
  30. }
  31. static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId /*parse_node*/)
  32. -> SemIR::InstId {
  33. auto interface_id = context.node_stack().PopExpr();
  34. auto self_id = SemIR::InstId::Invalid;
  35. if (!context.node_stack().PeekIs<Parse::NodeKind::ImplForall>() &&
  36. !context.node_stack().PeekIs<Parse::NodeKind::ImplIntroducer>()) {
  37. self_id = context.node_stack().PopExpr();
  38. }
  39. auto params_id = context.node_stack().PopIf<Parse::NodeKind::ImplForall>();
  40. auto decl_block_id = context.inst_block_stack().Pop();
  41. context.node_stack()
  42. .PopAndDiscardSoloParseNode<Parse::NodeKind::ImplIntroducer>();
  43. // TODO: Build an `Impl` object.
  44. static_cast<void>(decl_block_id);
  45. static_cast<void>(params_id);
  46. static_cast<void>(self_id);
  47. static_cast<void>(interface_id);
  48. return SemIR::InstId::Invalid;
  49. }
  50. auto HandleImplDecl(Context& context, Parse::ImplDeclId parse_node) -> bool {
  51. BuildImplDecl(context, parse_node);
  52. context.PopScope();
  53. return true;
  54. }
  55. auto HandleImplDefinitionStart(Context& context,
  56. Parse::ImplDefinitionStartId parse_node)
  57. -> bool {
  58. auto impl_decl_id = BuildImplDecl(context, parse_node);
  59. auto enclosing_scope_id = SemIR::NameScopeId::Invalid;
  60. auto scope_id = context.name_scopes().Add(
  61. impl_decl_id, SemIR::NameId::Invalid, enclosing_scope_id);
  62. context.PushScope(impl_decl_id, scope_id);
  63. return true;
  64. }
  65. auto HandleImplDefinition(Context& context,
  66. Parse::ImplDefinitionId /*parse_node*/) -> bool {
  67. context.PopScope();
  68. context.PopScope();
  69. return true;
  70. }
  71. } // namespace Carbon::Check