handle_function.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. } else if (context.PositionIs(Lex::TokenKind::MinusGreaterQuestion)) {
  23. context.PushState(StateKind::FunctionReturnFormFinish);
  24. context.ConsumeAndDiscard();
  25. context.PushStateForExpr(PrecedenceGroup::ForType());
  26. }
  27. }
  28. auto HandleFunctionReturnTypeFinish(Context& context) -> void {
  29. auto state = context.PopState();
  30. context.AddNode(NodeKind::ReturnType, state.token, state.has_error);
  31. }
  32. auto HandleFunctionReturnFormFinish(Context& context) -> void {
  33. auto state = context.PopState();
  34. context.AddNode(NodeKind::ReturnForm, state.token, state.has_error);
  35. }
  36. auto HandleFunctionSignatureFinish(Context& context) -> void {
  37. auto state = context.PopState();
  38. switch (context.PositionKind()) {
  39. case Lex::TokenKind::Semi: {
  40. context.AddNode(NodeKind::FunctionDecl, context.Consume(),
  41. state.has_error);
  42. break;
  43. }
  44. case Lex::TokenKind::OpenCurlyBrace: {
  45. context.AddFunctionDefinitionStart(context.Consume(), state.has_error);
  46. // Any error is recorded on the FunctionDefinitionStart.
  47. state.has_error = false;
  48. context.PushState(state, StateKind::FunctionDefinitionFinish);
  49. context.PushState(StateKind::StatementScopeLoop);
  50. break;
  51. }
  52. case Lex::TokenKind::EqualGreater: {
  53. context.AddFunctionDefinitionStart(context.Consume(), state.has_error);
  54. context.AddLeafNode(NodeKind::TerseBodyArrow, *(context.position() - 1));
  55. context.PushState(state, StateKind::FunctionTerseBodyFinish);
  56. context.PushStateForExpr(PrecedenceGroup::ForTopLevelExpr());
  57. break;
  58. }
  59. case Lex::TokenKind::Equal: {
  60. context.AddNode(NodeKind::BuiltinFunctionDefinitionStart,
  61. context.Consume(), state.has_error);
  62. if (!context.ConsumeAndAddLeafNodeIf(Lex::TokenKind::StringLiteral,
  63. NodeKind::BuiltinName)) {
  64. CARBON_DIAGNOSTIC(ExpectedBuiltinName, Error,
  65. "expected builtin function name after `=`");
  66. context.emitter().Emit(*context.position(), ExpectedBuiltinName);
  67. state.has_error = true;
  68. }
  69. auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
  70. if (!semi && !state.has_error) {
  71. context.DiagnoseExpectedDeclSemi(context.tokens().GetKind(state.token));
  72. state.has_error = true;
  73. }
  74. if (state.has_error) {
  75. context.RecoverFromDeclError(state, NodeKind::BuiltinFunctionDefinition,
  76. /*skip_past_likely_end=*/true);
  77. } else {
  78. context.AddNode(NodeKind::BuiltinFunctionDefinition, *semi,
  79. state.has_error);
  80. }
  81. break;
  82. }
  83. default: {
  84. if (!state.has_error) {
  85. context.DiagnoseExpectedDeclSemiOrDefinition(Lex::TokenKind::Fn);
  86. }
  87. // Only need to skip if we've not already found a new line.
  88. bool skip_past_likely_end =
  89. context.tokens().GetLine(*context.position()) ==
  90. context.tokens().GetLine(state.token);
  91. context.RecoverFromDeclError(state, NodeKind::FunctionDecl,
  92. skip_past_likely_end);
  93. break;
  94. }
  95. }
  96. }
  97. auto HandleFunctionDefinitionFinish(Context& context) -> void {
  98. auto state = context.PopState();
  99. context.AddFunctionDefinition(context.Consume(), state.has_error);
  100. }
  101. auto HandleFunctionTerseBodyFinish(Context& context) -> void {
  102. auto state = context.PopState();
  103. auto semi = context.ConsumeIf(Lex::TokenKind::Semi);
  104. if (!semi && !state.has_error) {
  105. context.DiagnoseExpectedDeclSemi(Lex::TokenKind::Fn);
  106. state.has_error = true;
  107. }
  108. context.AddFunctionTerseDefinition(semi ? *semi : *(context.position() - 1),
  109. state.has_error);
  110. }
  111. } // namespace Carbon::Parse