handle_call_expression.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/check/convert.h"
  6. #include "toolchain/sem_ir/node.h"
  7. namespace Carbon::Check {
  8. auto HandleCallExpression(Context& context, Parse::Node parse_node) -> bool {
  9. // Process the final explicit call argument, but leave the arguments block on
  10. // the stack until we add the return slot argument.
  11. context.ParamOrArgEndNoPop(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. auto temp_id = context.AddNode(SemIR::Node::TemporaryStorage::Make(
  37. call_expr_parse_node, callable.return_type_id));
  38. context.ParamOrArgSave(temp_id);
  39. }
  40. // Convert the arguments to match the parameters.
  41. auto refs_id = context.ParamOrArgPop();
  42. if (!ConvertCallArgs(context, call_expr_parse_node, refs_id,
  43. name_node.parse_node(), callable.param_refs_id,
  44. callable.return_slot_id.is_valid())) {
  45. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
  46. return true;
  47. }
  48. auto call_node_id = context.AddNode(SemIR::Node::Call::Make(
  49. call_expr_parse_node, type_id, refs_id, function_id));
  50. context.node_stack().Push(parse_node, call_node_id);
  51. return true;
  52. }
  53. auto HandleCallExpressionComma(Context& context, Parse::Node /*parse_node*/)
  54. -> bool {
  55. context.ParamOrArgComma();
  56. return true;
  57. }
  58. auto HandleCallExpressionStart(Context& context, Parse::Node parse_node)
  59. -> bool {
  60. auto name_id = context.node_stack().PopExpression();
  61. context.node_stack().Push(parse_node, name_id);
  62. context.ParamOrArgStart();
  63. return true;
  64. }
  65. } // namespace Carbon::Check