handle_function.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. 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::DeclNameAndParamsAsRequired, 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(Lex::TokenKind::MinusGreater)) {
  21. context.PushState(State::FunctionReturnTypeFinish);
  22. ++context.position();
  23. context.PushStateForExpr(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 Lex::TokenKind::Semi: {
  35. context.AddNode(NodeKind::FunctionDecl, context.Consume(),
  36. state.subtree_start, state.has_error);
  37. break;
  38. }
  39. case Lex::TokenKind::OpenCurlyBrace: {
  40. if (auto decl_context = context.GetDeclContext();
  41. decl_context == Context::DeclContext::Interface ||
  42. decl_context == Context::DeclContext::NamedConstraint) {
  43. CARBON_DIAGNOSTIC(
  44. MethodImplNotAllowed, Error,
  45. "Method implementations are not allowed in interfaces.");
  46. context.emitter().Emit(*context.position(), MethodImplNotAllowed);
  47. context.RecoverFromDeclError(state, NodeKind::FunctionDecl,
  48. /*skip_past_likely_end=*/true);
  49. break;
  50. }
  51. context.AddNode(NodeKind::FunctionDefinitionStart, context.Consume(),
  52. state.subtree_start, state.has_error);
  53. // Any error is recorded on the FunctionDefinitionStart.
  54. state.has_error = false;
  55. state.state = State::FunctionDefinitionFinish;
  56. context.PushState(state);
  57. context.PushState(State::StatementScopeLoop);
  58. break;
  59. }
  60. default: {
  61. if (!state.has_error) {
  62. context.EmitExpectedDeclSemiOrDefinition(Lex::TokenKind::Fn);
  63. }
  64. // Only need to skip if we've not already found a new line.
  65. bool skip_past_likely_end =
  66. context.tokens().GetLine(*context.position()) ==
  67. context.tokens().GetLine(state.token);
  68. context.RecoverFromDeclError(state, NodeKind::FunctionDecl,
  69. skip_past_likely_end);
  70. break;
  71. }
  72. }
  73. }
  74. auto HandleFunctionDefinitionFinish(Context& context) -> void {
  75. auto state = context.PopState();
  76. context.AddNode(NodeKind::FunctionDefinition, context.Consume(),
  77. state.subtree_start, state.has_error);
  78. }
  79. } // namespace Carbon::Parse