handle_call_expr.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 HandleCallExpr(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::CallExprStart);
  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().PopWithParseNode<Parse::NodeKind::CallExprStart>();
  17. auto diagnose_not_callable = [&, call_expr_parse_node = call_expr_parse_node,
  18. callee_id = callee_id] {
  19. auto callee_type_id = context.insts().Get(callee_id).type_id();
  20. if (callee_type_id != SemIR::TypeId::Error) {
  21. CARBON_DIAGNOSTIC(CallToNonCallable, Error,
  22. "Value of type `{0}` is not callable.", std::string);
  23. context.emitter().Emit(
  24. call_expr_parse_node, CallToNonCallable,
  25. context.sem_ir().StringifyType(callee_type_id, true));
  26. }
  27. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  28. return true;
  29. };
  30. // For a method call, pick out the `self` value.
  31. auto function_callee_id = callee_id;
  32. SemIR::InstId self_id = SemIR::InstId::Invalid;
  33. if (auto bound_method =
  34. context.insts().Get(callee_id).TryAs<SemIR::BoundMethod>()) {
  35. self_id = bound_method->object_id;
  36. function_callee_id = bound_method->function_id;
  37. }
  38. // Identify the function we're calling.
  39. auto function_decl_id = context.GetConstantValue(function_callee_id);
  40. if (!function_decl_id.is_valid()) {
  41. return diagnose_not_callable();
  42. }
  43. auto function_decl =
  44. context.insts().Get(function_decl_id).TryAs<SemIR::FunctionDecl>();
  45. if (!function_decl) {
  46. return diagnose_not_callable();
  47. }
  48. auto function_id = function_decl->function_id;
  49. const auto& callable = context.functions().Get(function_id);
  50. // For functions with an implicit return type, the return type is the empty
  51. // tuple type.
  52. SemIR::TypeId type_id = callable.return_type_id;
  53. if (!type_id.is_valid()) {
  54. type_id = context.CanonicalizeTupleType(call_expr_parse_node, {});
  55. }
  56. // If there is a return slot, build storage for the result.
  57. SemIR::InstId return_storage_id = SemIR::InstId::Invalid;
  58. if (callable.return_slot_id.is_valid()) {
  59. // Tentatively put storage for a temporary in the function's return slot.
  60. // This will be replaced if necessary when we perform initialization.
  61. return_storage_id = context.AddInst(
  62. SemIR::TemporaryStorage{call_expr_parse_node, callable.return_type_id});
  63. }
  64. // Convert the arguments to match the parameters.
  65. auto converted_args_id =
  66. ConvertCallArgs(context, call_expr_parse_node, self_id,
  67. context.params_or_args_stack().PeekCurrentBlockContents(),
  68. return_storage_id, function_decl->parse_node,
  69. callable.implicit_param_refs_id, callable.param_refs_id);
  70. auto call_inst_id = context.AddInst(
  71. SemIR::Call{call_expr_parse_node, type_id, callee_id, converted_args_id});
  72. context.node_stack().Push(parse_node, call_inst_id);
  73. return true;
  74. }
  75. auto HandleCallExprComma(Context& context, Parse::Node /*parse_node*/) -> bool {
  76. context.ParamOrArgComma();
  77. return true;
  78. }
  79. auto HandleCallExprStart(Context& context, Parse::Node parse_node) -> bool {
  80. auto name_id = context.node_stack().PopExpr();
  81. context.node_stack().Push(parse_node, name_id);
  82. context.ParamOrArgStart();
  83. return true;
  84. }
  85. } // namespace Carbon::Check