semantics_handle_function.cpp 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 {
  6. auto SemanticsHandleFunctionDeclaration(SemanticsContext& context,
  7. ParseTree::Node parse_node) -> bool {
  8. return context.TODO(parse_node, "HandleFunctionDeclaration");
  9. }
  10. auto SemanticsHandleFunctionDefinition(SemanticsContext& context,
  11. ParseTree::Node parse_node) -> bool {
  12. auto function_id = context.node_stack().Pop<SemanticsFunctionId>(
  13. ParseNodeKind::FunctionDefinitionStart);
  14. // If the `}` of the function is reachable, reject if we need a return value
  15. // and otherwise add an implicit `return;`.
  16. if (context.is_current_position_reachable()) {
  17. if (context.semantics_ir()
  18. .GetFunction(function_id)
  19. .return_type_id.is_valid()) {
  20. CARBON_DIAGNOSTIC(
  21. MissingReturnStatement, Error,
  22. "Missing `return` at end of function with declared return type.");
  23. context.emitter().Emit(parse_node, MissingReturnStatement);
  24. } else {
  25. context.AddNode(SemanticsNode::Return::Make(parse_node));
  26. }
  27. }
  28. context.return_scope_stack().pop_back();
  29. context.PopScope();
  30. context.node_block_stack().Pop();
  31. return true;
  32. }
  33. auto SemanticsHandleFunctionDefinitionStart(SemanticsContext& context,
  34. ParseTree::Node parse_node)
  35. -> bool {
  36. SemanticsTypeId return_type_id = SemanticsTypeId::Invalid;
  37. if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
  38. ParseNodeKind::ReturnType) {
  39. return_type_id =
  40. context.node_stack().Pop<SemanticsTypeId>(ParseNodeKind::ReturnType);
  41. } else {
  42. // Canonicalize the empty tuple for the implicit return.
  43. context.CanonicalizeType(SemanticsNodeId::BuiltinEmptyTupleType);
  44. }
  45. auto param_refs_id = context.node_stack().Pop<SemanticsNodeBlockId>(
  46. ParseNodeKind::ParameterList);
  47. auto name_context = context.PopDeclarationName();
  48. auto fn_node = context.node_stack().PopForSoloParseNode(
  49. ParseNodeKind::FunctionIntroducer);
  50. // Create the entry block.
  51. auto outer_block = context.node_block_stack().PeekForAdd();
  52. context.node_block_stack().Push();
  53. // TODO: Support out-of-line definitions, which will have a resolved
  54. // name_context. Right now, those become errors in AddNameToLookup.
  55. // Add the callable.
  56. auto function_id = context.semantics_ir().AddFunction(
  57. {.name_id = name_context.unresolved_name_id,
  58. .param_refs_id = param_refs_id,
  59. .return_type_id = return_type_id,
  60. .body_block_ids = {context.node_block_stack().PeekForAdd()}});
  61. auto decl_id = context.AddNodeToBlock(
  62. outer_block,
  63. SemanticsNode::FunctionDeclaration::Make(fn_node, function_id));
  64. context.AddNameToLookup(name_context, decl_id);
  65. context.PushScope();
  66. for (auto ref_id : context.semantics_ir().GetNodeBlock(param_refs_id)) {
  67. auto ref = context.semantics_ir().GetNode(ref_id);
  68. auto [name_id, target_id] = ref.GetAsBindName();
  69. context.AddNameToLookup(ref.parse_node(), name_id, target_id);
  70. }
  71. context.return_scope_stack().push_back(decl_id);
  72. context.node_stack().Push(parse_node, function_id);
  73. return true;
  74. }
  75. auto SemanticsHandleFunctionIntroducer(SemanticsContext& context,
  76. ParseTree::Node parse_node) -> bool {
  77. // Push the bracketing node.
  78. context.node_stack().Push(parse_node);
  79. // A name should always follow.
  80. context.PushDeclarationName();
  81. return true;
  82. }
  83. } // namespace Carbon