semantics_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/semantics/semantics_context.h"
  5. namespace Carbon::Check {
  6. // Build a FunctionDeclaration describing the signature of a function. This
  7. // handles the common logic shared by function declaration syntax and function
  8. // definition syntax.
  9. static auto BuildFunctionDeclaration(Context& context)
  10. -> std::pair<SemIR::FunctionId, SemIR::NodeId> {
  11. SemIR::TypeId return_type_id = SemIR::TypeId::Invalid;
  12. if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
  13. ParseNodeKind::ReturnType) {
  14. return_type_id = context.node_stack().Pop<ParseNodeKind::ReturnType>();
  15. }
  16. SemIR::NodeBlockId param_refs_id =
  17. context.node_stack().Pop<ParseNodeKind::ParameterList>();
  18. auto name_context = context.declaration_name_stack().Pop();
  19. auto fn_node = context.node_stack()
  20. .PopForSoloParseNode<ParseNodeKind::FunctionIntroducer>();
  21. // TODO: Support out-of-line definitions, which will have a resolved
  22. // name_context. Right now, those become errors in AddNameToLookup.
  23. // Add the callable.
  24. auto function_id = context.semantics_ir().AddFunction(
  25. {.name_id = name_context.state ==
  26. DeclarationNameStack::NameContext::State::Unresolved
  27. ? name_context.unresolved_name_id
  28. : SemIR::StringId(SemIR::StringId::InvalidIndex),
  29. .param_refs_id = param_refs_id,
  30. .return_type_id = return_type_id,
  31. .body_block_ids = {}});
  32. auto decl_id = context.AddNode(
  33. SemIR::Node::FunctionDeclaration::Make(fn_node, function_id));
  34. context.declaration_name_stack().AddNameToLookup(name_context, decl_id);
  35. return {function_id, decl_id};
  36. }
  37. auto HandleFunctionDeclaration(Context& context, ParseTree::Node /*parse_node*/)
  38. -> bool {
  39. BuildFunctionDeclaration(context);
  40. return true;
  41. }
  42. auto HandleFunctionDefinition(Context& context, ParseTree::Node parse_node)
  43. -> bool {
  44. SemIR::FunctionId function_id =
  45. context.node_stack().Pop<ParseNodeKind::FunctionDefinitionStart>();
  46. // If the `}` of the function is reachable, reject if we need a return value
  47. // and otherwise add an implicit `return;`.
  48. if (context.is_current_position_reachable()) {
  49. if (context.semantics_ir()
  50. .GetFunction(function_id)
  51. .return_type_id.is_valid()) {
  52. CARBON_DIAGNOSTIC(
  53. MissingReturnStatement, Error,
  54. "Missing `return` at end of function with declared return type.");
  55. context.emitter().Emit(parse_node, MissingReturnStatement);
  56. } else {
  57. context.AddNode(SemIR::Node::Return::Make(parse_node));
  58. }
  59. }
  60. context.PopScope();
  61. context.node_block_stack().Pop();
  62. context.return_scope_stack().pop_back();
  63. return true;
  64. }
  65. auto HandleFunctionDefinitionStart(Context& context, ParseTree::Node parse_node)
  66. -> bool {
  67. // Process the declaration portion of the function.
  68. auto [function_id, decl_id] = BuildFunctionDeclaration(context);
  69. const auto& function = context.semantics_ir().GetFunction(function_id);
  70. // Create the function scope and the entry block.
  71. context.return_scope_stack().push_back(decl_id);
  72. context.node_block_stack().Push();
  73. context.PushScope();
  74. context.AddCurrentCodeBlockToFunction();
  75. // Bring the parameters into scope.
  76. for (auto ref_id :
  77. context.semantics_ir().GetNodeBlock(function.param_refs_id)) {
  78. auto ref = context.semantics_ir().GetNode(ref_id);
  79. auto name_id = ref.GetAsParameter();
  80. context.AddNameToLookup(ref.parse_node(), name_id, ref_id);
  81. }
  82. context.node_stack().Push(parse_node, function_id);
  83. return true;
  84. }
  85. auto HandleFunctionIntroducer(Context& context, ParseTree::Node parse_node)
  86. -> bool {
  87. // Push the bracketing node.
  88. context.node_stack().Push(parse_node);
  89. // A name should always follow.
  90. context.declaration_name_stack().Push();
  91. return true;
  92. }
  93. auto HandleReturnType(Context& context, ParseTree::Node parse_node) -> bool {
  94. // Propagate the type expression.
  95. auto [type_parse_node, type_node_id] =
  96. context.node_stack().PopExpressionWithParseNode();
  97. auto cast_node_id = context.ExpressionAsType(type_parse_node, type_node_id);
  98. context.node_stack().Push(parse_node, cast_node_id);
  99. return true;
  100. }
  101. } // namespace Carbon::Check