handle_type.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/parse/context.h"
  5. namespace Carbon::Parse {
  6. // Handles processing of a type declaration or definition after its introducer.
  7. static auto HandleTypeAfterIntroducer(Context& context,
  8. State after_params_state) -> void {
  9. auto state = context.PopState();
  10. state.state = after_params_state;
  11. context.PushState(state);
  12. context.PushState(State::DeclNameAndParamsAsOptional, state.token);
  13. }
  14. auto HandleTypeAfterIntroducerAsClass(Context& context) -> void {
  15. HandleTypeAfterIntroducer(context, State::TypeAfterParamsAsClass);
  16. }
  17. auto HandleTypeAfterIntroducerAsInterface(Context& context) -> void {
  18. HandleTypeAfterIntroducer(context, State::TypeAfterParamsAsInterface);
  19. }
  20. auto HandleTypeAfterIntroducerAsNamedConstraint(Context& context) -> void {
  21. HandleTypeAfterIntroducer(context, State::TypeAfterParamsAsNamedConstraint);
  22. }
  23. // Handles processing after params, deciding whether it's a declaration or
  24. // definition.
  25. static auto HandleTypeAfterParams(Context& context, NodeKind decl_kind,
  26. NodeKind definition_start_kind,
  27. State definition_finish_state) -> void {
  28. auto state = context.PopState();
  29. if (state.has_error) {
  30. context.RecoverFromDeclError(state, decl_kind,
  31. /*skip_past_likely_end=*/true);
  32. return;
  33. }
  34. if (auto semi = context.ConsumeIf(Lex::TokenKind::Semi)) {
  35. context.AddNode(decl_kind, *semi, state.subtree_start, state.has_error);
  36. return;
  37. }
  38. if (!context.PositionIs(Lex::TokenKind::OpenCurlyBrace)) {
  39. context.EmitExpectedDeclSemiOrDefinition(
  40. context.tokens().GetKind(state.token));
  41. context.RecoverFromDeclError(state, decl_kind,
  42. /*skip_past_likely_end=*/true);
  43. return;
  44. }
  45. state.state = definition_finish_state;
  46. context.PushState(state);
  47. context.PushState(State::DeclScopeLoop);
  48. context.AddNode(definition_start_kind, context.Consume(), state.subtree_start,
  49. state.has_error);
  50. }
  51. auto HandleTypeAfterParamsAsClass(Context& context) -> void {
  52. HandleTypeAfterParams(context, NodeKind::ClassDecl,
  53. NodeKind::ClassDefinitionStart,
  54. State::TypeDefinitionFinishAsClass);
  55. }
  56. auto HandleTypeAfterParamsAsInterface(Context& context) -> void {
  57. HandleTypeAfterParams(context, NodeKind::InterfaceDecl,
  58. NodeKind::InterfaceDefinitionStart,
  59. State::TypeDefinitionFinishAsInterface);
  60. }
  61. auto HandleTypeAfterParamsAsNamedConstraint(Context& context) -> void {
  62. HandleTypeAfterParams(context, NodeKind::NamedConstraintDecl,
  63. NodeKind::NamedConstraintDefinitionStart,
  64. State::TypeDefinitionFinishAsNamedConstraint);
  65. }
  66. // Handles parsing after the declaration scope of a type.
  67. static auto HandleTypeDefinitionFinish(Context& context,
  68. NodeKind definition_kind) -> void {
  69. auto state = context.PopState();
  70. context.AddNode(definition_kind, context.Consume(), state.subtree_start,
  71. state.has_error);
  72. }
  73. auto HandleTypeDefinitionFinishAsClass(Context& context) -> void {
  74. HandleTypeDefinitionFinish(context, NodeKind::ClassDefinition);
  75. }
  76. auto HandleTypeDefinitionFinishAsInterface(Context& context) -> void {
  77. HandleTypeDefinitionFinish(context, NodeKind::InterfaceDefinition);
  78. }
  79. auto HandleTypeDefinitionFinishAsNamedConstraint(Context& context) -> void {
  80. HandleTypeDefinitionFinish(context, NodeKind::NamedConstraintDefinition);
  81. }
  82. } // namespace Carbon::Parse