semantics_handle_function.cpp 4.4 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 {
  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(SemanticsContext& context)
  10. -> std::pair<SemanticsFunctionId, SemanticsNodeId> {
  11. SemanticsTypeId return_type_id = SemanticsTypeId::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. SemanticsNodeBlockId 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.unresolved_name_id,
  26. .param_refs_id = param_refs_id,
  27. .return_type_id = return_type_id,
  28. .body_block_ids = {}});
  29. auto decl_id = context.AddNode(
  30. SemanticsNode::FunctionDeclaration::Make(fn_node, function_id));
  31. context.declaration_name_stack().AddNameToLookup(name_context, decl_id);
  32. return {function_id, decl_id};
  33. }
  34. auto SemanticsHandleFunctionDeclaration(SemanticsContext& context,
  35. ParseTree::Node /*parse_node*/)
  36. -> bool {
  37. BuildFunctionDeclaration(context);
  38. return true;
  39. }
  40. auto SemanticsHandleFunctionDefinition(SemanticsContext& context,
  41. ParseTree::Node parse_node) -> bool {
  42. SemanticsFunctionId function_id =
  43. context.node_stack().Pop<ParseNodeKind::FunctionDefinitionStart>();
  44. // If the `}` of the function is reachable, reject if we need a return value
  45. // and otherwise add an implicit `return;`.
  46. if (context.is_current_position_reachable()) {
  47. if (context.semantics_ir()
  48. .GetFunction(function_id)
  49. .return_type_id.is_valid()) {
  50. CARBON_DIAGNOSTIC(
  51. MissingReturnStatement, Error,
  52. "Missing `return` at end of function with declared return type.");
  53. context.emitter().Emit(parse_node, MissingReturnStatement);
  54. } else {
  55. context.AddNode(SemanticsNode::Return::Make(parse_node));
  56. }
  57. }
  58. context.PopScope();
  59. context.node_block_stack().Pop();
  60. context.return_scope_stack().pop_back();
  61. return true;
  62. }
  63. auto SemanticsHandleFunctionDefinitionStart(SemanticsContext& context,
  64. ParseTree::Node parse_node)
  65. -> bool {
  66. // Process the declaration portion of the function.
  67. auto [function_id, decl_id] = BuildFunctionDeclaration(context);
  68. const auto& function = context.semantics_ir().GetFunction(function_id);
  69. // Create the function scope and the entry block.
  70. context.return_scope_stack().push_back(decl_id);
  71. context.node_block_stack().Push();
  72. context.PushScope();
  73. context.AddCurrentCodeBlockToFunction();
  74. // Bring the parameters into scope.
  75. for (auto ref_id :
  76. context.semantics_ir().GetNodeBlock(function.param_refs_id)) {
  77. auto ref = context.semantics_ir().GetNode(ref_id);
  78. auto [name_id, target_id] = ref.GetAsBindName();
  79. context.AddNameToLookup(ref.parse_node(), name_id, target_id);
  80. }
  81. context.node_stack().Push(parse_node, function_id);
  82. return true;
  83. }
  84. auto SemanticsHandleFunctionIntroducer(SemanticsContext& context,
  85. ParseTree::Node parse_node) -> bool {
  86. // Push the bracketing node.
  87. context.node_stack().Push(parse_node);
  88. // A name should always follow.
  89. context.declaration_name_stack().Push();
  90. return true;
  91. }
  92. auto SemanticsHandleReturnType(SemanticsContext& context,
  93. 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