handle_call_expression.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 "llvm/ADT/ScopeExit.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/convert.h"
  7. #include "toolchain/sem_ir/inst.h"
  8. namespace Carbon::Check {
  9. auto HandleCallExpression(Context& context, Parse::Node parse_node) -> bool {
  10. // Process the final explicit call argument now, but leave the arguments
  11. // block on the stack until the end of this function.
  12. context.ParamOrArgEndNoPop(Parse::NodeKind::CallExpressionStart);
  13. auto discard_args_block = llvm::make_scope_exit(
  14. [&] { context.params_or_args_stack().PopAndDiscard(); });
  15. auto [call_expr_parse_node, callee_id] =
  16. context.node_stack()
  17. .PopWithParseNode<Parse::NodeKind::CallExpressionStart>();
  18. auto diagnose_not_callable = [&, call_expr_parse_node = call_expr_parse_node,
  19. callee_id = callee_id] {
  20. auto callee_type_id = context.insts().Get(callee_id).type_id();
  21. if (callee_type_id != SemIR::TypeId::Error) {
  22. CARBON_DIAGNOSTIC(CallToNonCallable, Error,
  23. "Value of type `{0}` is not callable.", std::string);
  24. context.emitter().Emit(
  25. call_expr_parse_node, CallToNonCallable,
  26. context.sem_ir().StringifyType(callee_type_id, true));
  27. }
  28. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  29. return true;
  30. };
  31. // For a method call, pick out the `self` value.
  32. auto function_callee_id = callee_id;
  33. SemIR::InstId self_id = SemIR::InstId::Invalid;
  34. if (auto bound_method =
  35. context.insts().Get(callee_id).TryAs<SemIR::BoundMethod>()) {
  36. self_id = bound_method->object_id;
  37. function_callee_id = bound_method->function_id;
  38. }
  39. // Identify the function we're calling.
  40. auto function_decl_id = context.GetConstantValue(function_callee_id);
  41. if (!function_decl_id.is_valid()) {
  42. return diagnose_not_callable();
  43. }
  44. auto function_decl =
  45. context.insts().Get(function_decl_id).TryAs<SemIR::FunctionDeclaration>();
  46. if (!function_decl) {
  47. return diagnose_not_callable();
  48. }
  49. auto function_id = function_decl->function_id;
  50. const auto& callable = context.functions().Get(function_id);
  51. // For functions with an implicit return type, the return type is the empty
  52. // tuple type.
  53. SemIR::TypeId type_id = callable.return_type_id;
  54. if (!type_id.is_valid()) {
  55. type_id = context.CanonicalizeTupleType(call_expr_parse_node, {});
  56. }
  57. // If there is a return slot, build storage for the result.
  58. SemIR::InstId return_storage_id = SemIR::InstId::Invalid;
  59. if (callable.return_slot_id.is_valid()) {
  60. // Tentatively put storage for a temporary in the function's return slot.
  61. // This will be replaced if necessary when we perform initialization.
  62. return_storage_id = context.AddInst(
  63. SemIR::TemporaryStorage{call_expr_parse_node, callable.return_type_id});
  64. }
  65. // Convert the arguments to match the parameters.
  66. auto converted_args_id =
  67. ConvertCallArgs(context, call_expr_parse_node, self_id,
  68. context.params_or_args_stack().PeekCurrentBlockContents(),
  69. return_storage_id, function_decl->parse_node,
  70. callable.implicit_param_refs_id, callable.param_refs_id);
  71. auto call_inst_id = context.AddInst(
  72. SemIR::Call{call_expr_parse_node, type_id, callee_id, converted_args_id});
  73. context.node_stack().Push(parse_node, call_inst_id);
  74. return true;
  75. }
  76. auto HandleCallExpressionComma(Context& context, Parse::Node /*parse_node*/)
  77. -> bool {
  78. context.ParamOrArgComma();
  79. return true;
  80. }
  81. auto HandleCallExpressionStart(Context& context, Parse::Node parse_node)
  82. -> bool {
  83. auto name_id = context.node_stack().PopExpression();
  84. context.node_stack().Push(parse_node, name_id);
  85. context.ParamOrArgStart();
  86. return true;
  87. }
  88. } // namespace Carbon::Check