handle_function.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include "toolchain/parse/handle.h"
  6. namespace Carbon::Parse {
  7. auto HandleFunctionIntroducer(Context& context) -> void {
  8. auto state = context.PopState();
  9. context.PushState(state, StateKind::FunctionAfterParams);
  10. context.PushState(StateKind::DeclNameAndParams, state.token);
  11. }
  12. auto HandleFunctionAfterParams(Context& context) -> void {
  13. auto state = context.PopState();
  14. // Regardless of whether there's a return type, we'll finish the signature.
  15. context.PushState(state, StateKind::FunctionSignatureFinish);
  16. // If there is a return type, parse the expression before adding the return
  17. // type node.
  18. if (context.PositionIs(Lex::TokenKind::MinusGreater)) {
  19. context.PushState(StateKind::FunctionReturnTypeFinish);
  20. context.ConsumeAndDiscard();
  21. context.PushStateForExpr(PrecedenceGroup::ForType());
  22. }
  23. }
  24. auto HandleFunctionReturnTypeFinish(Context& context) -> void {
  25. auto state = context.PopState();
  26. context.AddNode(NodeKind::ReturnType, state.token, state.has_error);
  27. }
  28. auto HandleFunctionSignatureFinish(Context& context) -> void {
  29. auto state = context.PopState();
  30. switch (context.PositionKind()) {
  31. case Lex::TokenKind::Semi: {
  32. context.AddNode(NodeKind::FunctionDecl, context.Consume(),
  33. state.has_error);
  34. break;
  35. }
  36. case Lex::TokenKind::OpenCurlyBrace: {
  37. context.AddFunctionDefinitionStart(context.Consume(), state.has_error);
  38. // Any error is recorded on the FunctionDefinitionStart.
  39. state.has_error = false;
  40. context.PushState(state, StateKind::FunctionDefinitionFinish);
  41. context.PushState(StateKind::StatementScopeLoop);
  42. break;
  43. }
  44. case Lex::TokenKind::Equal: {
  45. context.AddNode(NodeKind::BuiltinFunctionDefinitionStart,
  46. context.Consume(), state.has_error);
  47. if (!context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::StringLiteral,
  48. NodeKind::BuiltinName)) {
  49. CARBON_DIAGNOSTIC(ExpectedBuiltinName, Error,
  50. "expected builtin function name after `=`");
  51. context.emitter().Emit(*context.position(), ExpectedBuiltinName);
  52. state.has_error = true;
  53. }
  54. auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
  55. if (!semi && !state.has_error) {
  56. context.DiagnoseExpectedDeclSemi(context.tokens().GetKind(state.token));
  57. state.has_error = true;
  58. }
  59. if (state.has_error) {
  60. context.RecoverFromDeclError(state, NodeKind::BuiltinFunctionDefinition,
  61. /*skip_past_likely_end=*/true);
  62. } else {
  63. context.AddNode(NodeKind::BuiltinFunctionDefinition, *semi,
  64. state.has_error);
  65. }
  66. break;
  67. }
  68. default: {
  69. if (!state.has_error) {
  70. context.DiagnoseExpectedDeclSemiOrDefinition(Lex::TokenKind::Fn);
  71. }
  72. // Only need to skip if we've not already found a new line.
  73. bool skip_past_likely_end =
  74. context.tokens().GetLine(*context.position()) ==
  75. context.tokens().GetLine(state.token);
  76. context.RecoverFromDeclError(state, NodeKind::FunctionDecl,
  77. skip_past_likely_end);
  78. break;
  79. }
  80. }
  81. }
  82. auto HandleFunctionDefinitionFinish(Context& context) -> void {
  83. auto state = context.PopState();
  84. context.AddFunctionDefinition(context.Consume(), state.has_error);
  85. }
  86. } // namespace Carbon::Parse