semantics_handle_function.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // Merges code block children up under the FunctionDefinitionStart.
  13. while (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) !=
  14. ParseNodeKind::FunctionDefinitionStart) {
  15. context.node_stack().PopAndIgnore();
  16. }
  17. auto function_id = context.node_stack().Pop<SemanticsFunctionId>(
  18. ParseNodeKind::FunctionDefinitionStart);
  19. context.return_scope_stack().pop_back();
  20. context.PopScope();
  21. auto body_id = context.node_block_stack().Pop();
  22. auto& function = context.semantics_ir().GetFunction(function_id);
  23. CARBON_CHECK(!function.body_id.is_valid())
  24. << "Already have function body: " << function.body_id;
  25. function.body_id = body_id;
  26. return true;
  27. }
  28. auto SemanticsHandleFunctionDefinitionStart(SemanticsContext& context,
  29. ParseTree::Node parse_node)
  30. -> bool {
  31. SemanticsTypeId return_type_id = SemanticsTypeId::Invalid;
  32. if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
  33. ParseNodeKind::ReturnType) {
  34. return_type_id =
  35. context.node_stack().Pop<SemanticsTypeId>(ParseNodeKind::ReturnType);
  36. } else {
  37. // Canonicalize the empty tuple for the implicit return.
  38. context.CanonicalizeType(SemanticsNodeId::BuiltinEmptyTupleType);
  39. }
  40. auto param_refs_id = context.node_stack().Pop<SemanticsNodeBlockId>(
  41. ParseNodeKind::ParameterList);
  42. auto name_node =
  43. context.node_stack().PopForSoloParseNode(ParseNodeKind::DeclaredName);
  44. auto fn_node = context.node_stack().PopForSoloParseNode(
  45. ParseNodeKind::FunctionIntroducer);
  46. auto name_str = context.parse_tree().GetNodeText(name_node);
  47. auto name_id = context.semantics_ir().AddString(name_str);
  48. // Add the callable, but with an invalid body for now. The body ID is only
  49. // finalized on function completion.
  50. auto function_id = context.semantics_ir().AddFunction(
  51. {.name_id = name_id,
  52. .param_refs_id = param_refs_id,
  53. .return_type_id = return_type_id,
  54. .body_id = SemanticsNodeBlockId::Invalid});
  55. auto decl_id = context.AddNode(
  56. SemanticsNode::FunctionDeclaration::Make(fn_node, function_id));
  57. context.AddNameToLookup(name_node, name_id, decl_id);
  58. context.node_block_stack().Push();
  59. context.PushScope();
  60. for (auto ref_id : context.semantics_ir().GetNodeBlock(param_refs_id)) {
  61. auto ref = context.semantics_ir().GetNode(ref_id);
  62. auto [name_id, target_id] = ref.GetAsBindName();
  63. context.AddNameToLookup(ref.parse_node(), name_id, target_id);
  64. }
  65. context.return_scope_stack().push_back(decl_id);
  66. context.node_stack().Push(parse_node, function_id);
  67. return true;
  68. }
  69. auto SemanticsHandleFunctionIntroducer(SemanticsContext& context,
  70. ParseTree::Node parse_node) -> bool {
  71. // No action, just a bracketing node.
  72. context.node_stack().Push(parse_node);
  73. return true;
  74. }
  75. } // namespace Carbon