handle_function.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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, State::FunctionAfterParams);
  10. context.PushState(State::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, State::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(State::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.subtree_start,
  27. state.has_error);
  28. }
  29. auto HandleFunctionSignatureFinish(Context& context) -> void {
  30. auto state = context.PopState();
  31. switch (context.PositionKind()) {
  32. case Lex::TokenKind::Semi: {
  33. context.AddNode(NodeKind::FunctionDecl, context.Consume(),
  34. state.subtree_start, state.has_error);
  35. break;
  36. }
  37. case Lex::TokenKind::OpenCurlyBrace: {
  38. context.AddFunctionDefinitionStart(context.Consume(), state.subtree_start,
  39. state.has_error);
  40. // Any error is recorded on the FunctionDefinitionStart.
  41. state.has_error = false;
  42. context.PushState(state, State::FunctionDefinitionFinish);
  43. context.PushState(State::StatementScopeLoop);
  44. break;
  45. }
  46. case Lex::TokenKind::Equal: {
  47. context.AddNode(NodeKind::BuiltinFunctionDefinitionStart,
  48. context.Consume(), state.subtree_start, state.has_error);
  49. if (!context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::StringLiteral,
  50. NodeKind::BuiltinName)) {
  51. CARBON_DIAGNOSTIC(ExpectedBuiltinName, Error,
  52. "Expected builtin function name after `=`.");
  53. context.emitter().Emit(*context.position(), ExpectedBuiltinName);
  54. state.has_error = true;
  55. }
  56. auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
  57. if (!semi && !state.has_error) {
  58. context.DiagnoseExpectedDeclSemi(context.tokens().GetKind(state.token));
  59. state.has_error = true;
  60. }
  61. if (state.has_error) {
  62. context.RecoverFromDeclError(state, NodeKind::BuiltinFunctionDefinition,
  63. /*skip_past_likely_end=*/true);
  64. } else {
  65. context.AddNode(NodeKind::BuiltinFunctionDefinition, *semi,
  66. state.subtree_start, state.has_error);
  67. }
  68. break;
  69. }
  70. default: {
  71. if (!state.has_error) {
  72. context.DiagnoseExpectedDeclSemiOrDefinition(Lex::TokenKind::Fn);
  73. }
  74. // Only need to skip if we've not already found a new line.
  75. bool skip_past_likely_end =
  76. context.tokens().GetLine(*context.position()) ==
  77. context.tokens().GetLine(state.token);
  78. context.RecoverFromDeclError(state, NodeKind::FunctionDecl,
  79. skip_past_likely_end);
  80. break;
  81. }
  82. }
  83. }
  84. auto HandleFunctionDefinitionFinish(Context& context) -> void {
  85. auto state = context.PopState();
  86. context.AddFunctionDefinition(context.Consume(), state.subtree_start,
  87. state.has_error);
  88. }
  89. } // namespace Carbon::Parse