semantics_handle_function.cpp 4.6 KB

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