parser_handle_function.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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/parser/parser_context.h"
  5. namespace Carbon::Parse {
  6. auto HandleFunctionIntroducer(Context& context) -> void {
  7. auto state = context.PopState();
  8. context.AddLeafNode(NodeKind::FunctionIntroducer, context.Consume());
  9. state.state = State::FunctionAfterParameters;
  10. context.PushState(state);
  11. context.PushState(State::DeclarationNameAndParamsAsRequired, state.token);
  12. }
  13. auto HandleFunctionAfterParameters(Context& context) -> void {
  14. auto state = context.PopState();
  15. // Regardless of whether there's a return type, we'll finish the signature.
  16. state.state = State::FunctionSignatureFinish;
  17. context.PushState(state);
  18. // If there is a return type, parse the expression before adding the return
  19. // type nod.e
  20. if (context.PositionIs(TokenKind::MinusGreater)) {
  21. context.PushState(State::FunctionReturnTypeFinish);
  22. ++context.position();
  23. context.PushStateForExpression(PrecedenceGroup::ForType());
  24. }
  25. }
  26. auto HandleFunctionReturnTypeFinish(Context& context) -> void {
  27. auto state = context.PopState();
  28. context.AddNode(NodeKind::ReturnType, state.token, state.subtree_start,
  29. state.has_error);
  30. }
  31. auto HandleFunctionSignatureFinish(Context& context) -> void {
  32. auto state = context.PopState();
  33. switch (context.PositionKind()) {
  34. case TokenKind::Semi: {
  35. context.AddNode(NodeKind::FunctionDeclaration, context.Consume(),
  36. state.subtree_start, state.has_error);
  37. break;
  38. }
  39. case TokenKind::OpenCurlyBrace: {
  40. if (auto decl_context = context.GetDeclarationContext();
  41. decl_context == Context::DeclarationContext::Interface ||
  42. decl_context == Context::DeclarationContext::NamedConstraint) {
  43. CARBON_DIAGNOSTIC(
  44. MethodImplNotAllowed, Error,
  45. "Method implementations are not allowed in interfaces.");
  46. context.emitter().Emit(*context.position(), MethodImplNotAllowed);
  47. context.RecoverFromDeclarationError(state,
  48. NodeKind::FunctionDeclaration,
  49. /*skip_past_likely_end=*/true);
  50. break;
  51. }
  52. context.AddNode(NodeKind::FunctionDefinitionStart, context.Consume(),
  53. state.subtree_start, state.has_error);
  54. // Any error is recorded on the FunctionDefinitionStart.
  55. state.has_error = false;
  56. state.state = State::FunctionDefinitionFinish;
  57. context.PushState(state);
  58. context.PushState(State::StatementScopeLoop);
  59. break;
  60. }
  61. default: {
  62. if (!state.has_error) {
  63. context.EmitExpectedDeclarationSemiOrDefinition(TokenKind::Fn);
  64. }
  65. // Only need to skip if we've not already found a new line.
  66. bool skip_past_likely_end =
  67. context.tokens().GetLine(*context.position()) ==
  68. context.tokens().GetLine(state.token);
  69. context.RecoverFromDeclarationError(state, NodeKind::FunctionDeclaration,
  70. skip_past_likely_end);
  71. break;
  72. }
  73. }
  74. }
  75. auto HandleFunctionDefinitionFinish(Context& context) -> void {
  76. auto state = context.PopState();
  77. context.AddNode(NodeKind::FunctionDefinition, context.Consume(),
  78. state.subtree_start, state.has_error);
  79. }
  80. } // namespace Carbon::Parse