semantics_handle_function.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. } else {
  16. // Canonicalize the empty tuple for the implicit return.
  17. context.CanonicalizeType(SemanticsNodeId::BuiltinEmptyTupleType);
  18. }
  19. SemanticsNodeBlockId param_refs_id =
  20. context.node_stack().Pop<ParseNodeKind::ParameterList>();
  21. auto name_context = context.declaration_name_stack().Pop();
  22. auto fn_node = context.node_stack()
  23. .PopForSoloParseNode<ParseNodeKind::FunctionIntroducer>();
  24. // TODO: Support out-of-line definitions, which will have a resolved
  25. // name_context. Right now, those become errors in AddNameToLookup.
  26. // Add the callable.
  27. auto function_id = context.semantics_ir().AddFunction(
  28. {.name_id = name_context.unresolved_name_id,
  29. .param_refs_id = param_refs_id,
  30. .return_type_id = return_type_id,
  31. .body_block_ids = {}});
  32. auto decl_id = context.AddNode(
  33. SemanticsNode::FunctionDeclaration::Make(fn_node, function_id));
  34. context.declaration_name_stack().AddNameToLookup(name_context, decl_id);
  35. return {function_id, decl_id};
  36. }
  37. auto SemanticsHandleFunctionDeclaration(SemanticsContext& context,
  38. ParseTree::Node /*parse_node*/)
  39. -> bool {
  40. BuildFunctionDeclaration(context);
  41. return true;
  42. }
  43. auto SemanticsHandleFunctionDefinition(SemanticsContext& context,
  44. ParseTree::Node parse_node) -> bool {
  45. SemanticsFunctionId function_id =
  46. context.node_stack().Pop<ParseNodeKind::FunctionDefinitionStart>();
  47. // If the `}` of the function is reachable, reject if we need a return value
  48. // and otherwise add an implicit `return;`.
  49. if (context.is_current_position_reachable()) {
  50. if (context.semantics_ir()
  51. .GetFunction(function_id)
  52. .return_type_id.is_valid()) {
  53. CARBON_DIAGNOSTIC(
  54. MissingReturnStatement, Error,
  55. "Missing `return` at end of function with declared return type.");
  56. context.emitter().Emit(parse_node, MissingReturnStatement);
  57. } else {
  58. context.AddNode(SemanticsNode::Return::Make(parse_node));
  59. }
  60. }
  61. context.PopScope();
  62. context.node_block_stack().Pop();
  63. context.return_scope_stack().pop_back();
  64. return true;
  65. }
  66. auto SemanticsHandleFunctionDefinitionStart(SemanticsContext& context,
  67. ParseTree::Node parse_node)
  68. -> bool {
  69. // Process the declaration portion of the function.
  70. auto [function_id, decl_id] = BuildFunctionDeclaration(context);
  71. const auto& function = context.semantics_ir().GetFunction(function_id);
  72. // Create the function scope and the entry block.
  73. context.return_scope_stack().push_back(decl_id);
  74. context.node_block_stack().Push();
  75. context.PushScope();
  76. context.AddCurrentCodeBlockToFunction();
  77. // Bring the parameters into scope.
  78. for (auto ref_id :
  79. context.semantics_ir().GetNodeBlock(function.param_refs_id)) {
  80. auto ref = context.semantics_ir().GetNode(ref_id);
  81. auto [name_id, target_id] = ref.GetAsBindName();
  82. context.AddNameToLookup(ref.parse_node(), name_id, target_id);
  83. }
  84. context.node_stack().Push(parse_node, function_id);
  85. return true;
  86. }
  87. auto SemanticsHandleFunctionIntroducer(SemanticsContext& context,
  88. ParseTree::Node parse_node) -> bool {
  89. // Push the bracketing node.
  90. context.node_stack().Push(parse_node);
  91. // A name should always follow.
  92. context.declaration_name_stack().Push();
  93. return true;
  94. }
  95. auto SemanticsHandleReturnType(SemanticsContext& context,
  96. ParseTree::Node parse_node) -> bool {
  97. // Propagate the type expression.
  98. auto [type_parse_node, type_node_id] =
  99. context.node_stack().PopExpressionWithParseNode();
  100. auto cast_node_id = context.ExpressionAsType(type_parse_node, type_node_id);
  101. context.node_stack().Push(parse_node, cast_node_id);
  102. return true;
  103. }
  104. } // namespace Carbon