handle_function.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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::EqualGreater: {
  45. context.AddFunctionDefinitionStart(context.Consume(), state.has_error);
  46. context.AddLeafNode(NodeKind::TerseBodyArrow, *(context.position() - 1));
  47. context.PushState(state, StateKind::FunctionTerseBodyFinish);
  48. context.PushStateForExpr(PrecedenceGroup::ForTopLevelExpr());
  49. break;
  50. }
  51. case Lex::TokenKind::Equal: {
  52. context.AddNode(NodeKind::BuiltinFunctionDefinitionStart,
  53. context.Consume(), state.has_error);
  54. if (!context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::StringLiteral,
  55. NodeKind::BuiltinName)) {
  56. CARBON_DIAGNOSTIC(ExpectedBuiltinName, Error,
  57. "expected builtin function name after `=`");
  58. context.emitter().Emit(*context.position(), ExpectedBuiltinName);
  59. state.has_error = true;
  60. }
  61. auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
  62. if (!semi && !state.has_error) {
  63. context.DiagnoseExpectedDeclSemi(context.tokens().GetKind(state.token));
  64. state.has_error = true;
  65. }
  66. if (state.has_error) {
  67. context.RecoverFromDeclError(state, NodeKind::BuiltinFunctionDefinition,
  68. /*skip_past_likely_end=*/true);
  69. } else {
  70. context.AddNode(NodeKind::BuiltinFunctionDefinition, *semi,
  71. state.has_error);
  72. }
  73. break;
  74. }
  75. default: {
  76. if (!state.has_error) {
  77. context.DiagnoseExpectedDeclSemiOrDefinition(Lex::TokenKind::Fn);
  78. }
  79. // Only need to skip if we've not already found a new line.
  80. bool skip_past_likely_end =
  81. context.tokens().GetLine(*context.position()) ==
  82. context.tokens().GetLine(state.token);
  83. context.RecoverFromDeclError(state, NodeKind::FunctionDecl,
  84. skip_past_likely_end);
  85. break;
  86. }
  87. }
  88. }
  89. auto HandleFunctionDefinitionFinish(Context& context) -> void {
  90. auto state = context.PopState();
  91. context.AddFunctionDefinition(context.Consume(), state.has_error);
  92. }
  93. auto HandleFunctionTerseBodyFinish(Context& context) -> void {
  94. auto state = context.PopState();
  95. auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
  96. if (!semi && !state.has_error) {
  97. context.DiagnoseExpectedDeclSemi(Lex::TokenKind::Fn);
  98. state.has_error = true;
  99. }
  100. context.AddFunctionTerseDefinition(semi ? *semi : *(context.position() - 1),
  101. state.has_error);
  102. }
  103. } // namespace Carbon::Parse