handle_call_expression.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/check/context.h"
  5. #include "toolchain/sem_ir/node.h"
  6. namespace Carbon::Check {
  7. auto HandleCallExpression(Context& context, Parse::Node parse_node) -> bool {
  8. // Process the final explicit call argument, but leave the arguments block on
  9. // the stack until we add the return slot argument.
  10. context.ParamOrArgEndNoPop(
  11. /*for_args=*/true, Parse::NodeKind::CallExpressionStart);
  12. // TODO: Convert to call expression.
  13. auto [call_expr_parse_node, name_id] =
  14. context.node_stack()
  15. .PopWithParseNode<Parse::NodeKind::CallExpressionStart>();
  16. auto name_node = context.semantics_ir().GetNode(name_id);
  17. if (name_node.kind() != SemIR::NodeKind::FunctionDeclaration) {
  18. // TODO: Work on error.
  19. context.TODO(parse_node, "Not a callable name");
  20. context.node_stack().Push(parse_node, name_id);
  21. context.ParamOrArgPop();
  22. return true;
  23. }
  24. auto function_id = name_node.GetAsFunctionDeclaration();
  25. const auto& callable = context.semantics_ir().GetFunction(function_id);
  26. // For functions with an implicit return type, the return type is the empty
  27. // tuple type.
  28. SemIR::TypeId type_id = callable.return_type_id;
  29. if (!type_id.is_valid()) {
  30. type_id = context.CanonicalizeTupleType(call_expr_parse_node, {});
  31. }
  32. // If there is a return slot, add a corresponding argument.
  33. if (callable.return_slot_id.is_valid()) {
  34. // Tentatively put storage for a temporary in the function's return slot.
  35. // This will be replaced if necessary when we perform initialization.
  36. context.AddNodeAndPush(parse_node,
  37. SemIR::Node::TemporaryStorage::Make(
  38. call_expr_parse_node, callable.return_type_id));
  39. // TODO: We pass for_args=false here because we don't want a StubReference.
  40. // We should remove the StubReferences for arguments and the corresponding
  41. // `for_args` parameter. They didn't turn out to be used for anything.
  42. context.ParamOrArgSave(/*for_args=*/false);
  43. }
  44. // Convert the arguments to match the parameters.
  45. auto refs_id = context.ParamOrArgPop();
  46. if (!context.ImplicitAsForArgs(call_expr_parse_node, refs_id,
  47. name_node.parse_node(), callable.param_refs_id,
  48. callable.return_slot_id.is_valid())) {
  49. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
  50. return true;
  51. }
  52. auto call_node_id = context.AddNode(SemIR::Node::Call::Make(
  53. call_expr_parse_node, type_id, refs_id, function_id));
  54. context.node_stack().Push(parse_node, call_node_id);
  55. return true;
  56. }
  57. auto HandleCallExpressionComma(Context& context, Parse::Node /*parse_node*/)
  58. -> bool {
  59. context.ParamOrArgComma(/*for_args=*/true);
  60. return true;
  61. }
  62. auto HandleCallExpressionStart(Context& context, Parse::Node parse_node)
  63. -> bool {
  64. auto name_id = context.node_stack().PopExpression();
  65. context.node_stack().Push(parse_node, name_id);
  66. context.ParamOrArgStart();
  67. return true;
  68. }
  69. } // namespace Carbon::Check