handle_function.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/check/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. // TODO: This contains the IR block for the parameters and return type. At
  12. // present, it's just loose, but it's not strictly required for parameter
  13. // refs; we should either stop constructing it completely or, if it turns out
  14. // to be needed, store it. Note, the underlying issue is that the LLVM IR has
  15. // nowhere clear to emit, so changing storage would require addressing that
  16. // problem. For comparison with function calls, the IR needs to be emitted
  17. // prior to the call.
  18. context.node_block_stack().Pop();
  19. auto return_type_id = SemIR::TypeId::Invalid;
  20. auto return_slot_id = SemIR::NodeId::Invalid;
  21. if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
  22. Parse::NodeKind::ReturnType) {
  23. return_slot_id = context.node_stack().Pop<Parse::NodeKind::ReturnType>();
  24. return_type_id = context.semantics_ir().GetNode(return_slot_id).type_id();
  25. // The function only has a return slot if it uses in-place initialization.
  26. if (!SemIR::GetInitializingRepresentation(context.semantics_ir(),
  27. return_type_id)
  28. .has_return_slot()) {
  29. return_slot_id = SemIR::NodeId::Invalid;
  30. }
  31. }
  32. SemIR::NodeBlockId param_refs_id =
  33. context.node_stack().Pop<Parse::NodeKind::ParameterList>();
  34. auto name_context = context.declaration_name_stack().Pop();
  35. auto fn_node =
  36. context.node_stack()
  37. .PopForSoloParseNode<Parse::NodeKind::FunctionIntroducer>();
  38. // TODO: Support out-of-line definitions, which will have a resolved
  39. // name_context. Right now, those become errors in AddNameToLookup.
  40. // Add the callable.
  41. auto function_id = context.semantics_ir().AddFunction(
  42. {.name_id = name_context.state ==
  43. DeclarationNameStack::NameContext::State::Unresolved
  44. ? name_context.unresolved_name_id
  45. : SemIR::StringId(SemIR::StringId::InvalidIndex),
  46. .param_refs_id = param_refs_id,
  47. .return_type_id = return_type_id,
  48. .return_slot_id = return_slot_id,
  49. .body_block_ids = {}});
  50. auto decl_id = context.AddNode(
  51. SemIR::Node::FunctionDeclaration::Make(fn_node, function_id));
  52. context.declaration_name_stack().AddNameToLookup(name_context, decl_id);
  53. return {function_id, decl_id};
  54. }
  55. auto HandleFunctionDeclaration(Context& context, Parse::Node /*parse_node*/)
  56. -> bool {
  57. BuildFunctionDeclaration(context);
  58. return true;
  59. }
  60. auto HandleFunctionDefinition(Context& context, Parse::Node parse_node)
  61. -> bool {
  62. SemIR::FunctionId function_id =
  63. context.node_stack().Pop<Parse::NodeKind::FunctionDefinitionStart>();
  64. // If the `}` of the function is reachable, reject if we need a return value
  65. // and otherwise add an implicit `return;`.
  66. if (context.is_current_position_reachable()) {
  67. if (context.semantics_ir()
  68. .GetFunction(function_id)
  69. .return_type_id.is_valid()) {
  70. CARBON_DIAGNOSTIC(
  71. MissingReturnStatement, Error,
  72. "Missing `return` at end of function with declared return type.");
  73. context.emitter().Emit(parse_node, MissingReturnStatement);
  74. } else {
  75. context.AddNode(SemIR::Node::Return::Make(parse_node));
  76. }
  77. }
  78. context.PopScope();
  79. context.node_block_stack().Pop();
  80. context.return_scope_stack().pop_back();
  81. return true;
  82. }
  83. auto HandleFunctionDefinitionStart(Context& context, Parse::Node parse_node)
  84. -> bool {
  85. // Process the declaration portion of the function.
  86. auto [function_id, decl_id] = BuildFunctionDeclaration(context);
  87. const auto& function = context.semantics_ir().GetFunction(function_id);
  88. // Create the function scope and the entry block.
  89. context.return_scope_stack().push_back(decl_id);
  90. context.node_block_stack().Push();
  91. context.PushScope();
  92. context.AddCurrentCodeBlockToFunction();
  93. // Bring the parameters into scope.
  94. for (auto ref_id :
  95. context.semantics_ir().GetNodeBlock(function.param_refs_id)) {
  96. auto ref = context.semantics_ir().GetNode(ref_id);
  97. auto name_id = ref.GetAsParameter();
  98. context.AddNameToLookup(ref.parse_node(), name_id, ref_id);
  99. }
  100. context.node_stack().Push(parse_node, function_id);
  101. return true;
  102. }
  103. auto HandleFunctionIntroducer(Context& context, Parse::Node parse_node)
  104. -> bool {
  105. // Create a node block to hold the nodes created as part of the function
  106. // signature, such as parameter and return types.
  107. context.node_block_stack().Push();
  108. // Push the bracketing node.
  109. context.node_stack().Push(parse_node);
  110. // A name should always follow.
  111. context.declaration_name_stack().Push();
  112. return true;
  113. }
  114. auto HandleReturnType(Context& context, Parse::Node parse_node) -> bool {
  115. // Propagate the type expression.
  116. auto [type_parse_node, type_node_id] =
  117. context.node_stack().PopExpressionWithParseNode();
  118. auto type_id = context.ExpressionAsType(type_parse_node, type_node_id);
  119. // TODO: Use a dedicated node rather than VarStorage here.
  120. context.AddNodeAndPush(
  121. parse_node,
  122. SemIR::Node::VarStorage::Make(
  123. parse_node, type_id, context.semantics_ir().AddString("return")));
  124. return true;
  125. }
  126. } // namespace Carbon::Check