semantics_handle_function.cpp 5.0 KB

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