handle_function.cpp 5.6 KB

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