handle_function.cpp 3.7 KB

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