function.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/sem_ir/function.h"
  5. #include <optional>
  6. #include "toolchain/base/kind_switch.h"
  7. #include "toolchain/sem_ir/file.h"
  8. #include "toolchain/sem_ir/generic.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. #include "toolchain/sem_ir/typed_insts.h"
  11. namespace Carbon::SemIR {
  12. auto GetCallee(const File& sem_ir, InstId callee_id,
  13. SpecificId caller_specific_id) -> Callee {
  14. CalleeFunction fn = {.function_id = FunctionId::None,
  15. .enclosing_specific_id = SpecificId::None,
  16. .resolved_specific_id = SpecificId::None,
  17. .self_type_id = InstId::None,
  18. .self_id = InstId::None};
  19. if (auto bound_method = sem_ir.insts().TryGetAs<BoundMethod>(callee_id)) {
  20. fn.self_id = bound_method->object_id;
  21. callee_id = bound_method->function_decl_id;
  22. }
  23. if (caller_specific_id.has_value()) {
  24. callee_id = sem_ir.constant_values().GetInstIdIfValid(
  25. GetConstantValueInSpecific(sem_ir, caller_specific_id, callee_id));
  26. CARBON_CHECK(callee_id.has_value(),
  27. "Invalid callee id in a specific context");
  28. }
  29. if (auto specific_function =
  30. sem_ir.insts().TryGetAs<SpecificFunction>(callee_id)) {
  31. fn.resolved_specific_id = specific_function->specific_id;
  32. callee_id = specific_function->callee_id;
  33. } else if (auto specific_impl_function =
  34. sem_ir.insts().TryGetAs<SpecificImplFunction>(callee_id)) {
  35. fn.resolved_specific_id = specific_impl_function->specific_id;
  36. callee_id = specific_impl_function->callee_id;
  37. }
  38. // Identify the function we're calling by its type.
  39. auto val_id = sem_ir.constant_values().GetConstantInstId(callee_id);
  40. if (!val_id.has_value()) {
  41. return CalleeNonFunction();
  42. }
  43. auto fn_type_inst =
  44. sem_ir.types().GetAsInst(sem_ir.insts().Get(val_id).type_id());
  45. if (auto cpp_overload_set_type = fn_type_inst.TryAs<CppOverloadSetType>()) {
  46. CARBON_CHECK(!fn.resolved_specific_id.has_value(),
  47. "Only `SpecificFunction` will be resolved, not C++ overloads");
  48. return CalleeCppOverloadSet{
  49. .cpp_overload_set_id = cpp_overload_set_type->overload_set_id,
  50. .self_id = fn.self_id};
  51. }
  52. if (auto impl_fn_type = fn_type_inst.TryAs<FunctionTypeWithSelfType>()) {
  53. // Combine the associated function's `Self` with the interface function
  54. // data.
  55. fn.self_type_id = impl_fn_type->self_id;
  56. fn_type_inst = sem_ir.insts().Get(impl_fn_type->interface_function_type_id);
  57. }
  58. auto fn_type = fn_type_inst.TryAs<FunctionType>();
  59. if (!fn_type) {
  60. if (fn_type_inst.Is<ErrorInst>()) {
  61. return CalleeError();
  62. }
  63. return CalleeNonFunction();
  64. }
  65. fn.function_id = fn_type->function_id;
  66. fn.enclosing_specific_id = fn_type->specific_id;
  67. return fn;
  68. }
  69. auto GetCalleeAsFunction(const File& sem_ir, InstId callee_id,
  70. SpecificId caller_specific_id) -> CalleeFunction {
  71. return std::get<CalleeFunction>(
  72. GetCallee(sem_ir, callee_id, caller_specific_id));
  73. }
  74. auto DecomposeVirtualFunction(const File& sem_ir, InstId fn_decl_id,
  75. SpecificId base_class_specific_id)
  76. -> DecomposedVirtualFunction {
  77. // Remap the base's vtable entry to the appropriate constant usable in
  78. // the context of the derived class (for the specific for the base
  79. // class, for instance).
  80. auto fn_decl_const_id =
  81. GetConstantValueInSpecific(sem_ir, base_class_specific_id, fn_decl_id);
  82. fn_decl_id = sem_ir.constant_values().GetInstId(fn_decl_const_id);
  83. auto specific_id = SpecificId::None;
  84. auto callee_id = fn_decl_id;
  85. if (auto specific_function =
  86. sem_ir.insts().TryGetAs<SpecificFunction>(fn_decl_id)) {
  87. specific_id = specific_function->specific_id;
  88. callee_id = specific_function->callee_id;
  89. }
  90. // Identify the function we're calling by its type.
  91. auto fn_type_inst =
  92. sem_ir.types().GetAsInst(sem_ir.insts().Get(callee_id).type_id());
  93. return {.fn_decl_id = fn_decl_id,
  94. .fn_decl_const_id = fn_decl_const_id,
  95. .function_id = fn_type_inst.As<FunctionType>().function_id,
  96. .specific_id = specific_id};
  97. }
  98. auto Function::GetDeclaredReturnType(const File& file,
  99. SpecificId specific_id) const -> TypeId {
  100. if (!return_type_inst_id.has_value()) {
  101. return TypeId::None;
  102. }
  103. return file.types().GetTypeIdForTypeConstantId(
  104. GetConstantValueInSpecific(file, specific_id, return_type_inst_id));
  105. }
  106. auto Function::GetDeclaredReturnForm(const File& file,
  107. SpecificId specific_id) const -> InstId {
  108. if (!return_form_inst_id.has_value()) {
  109. // Treat as equivalent to `-> ()`.
  110. return InstId::None;
  111. }
  112. auto return_form_constant_id =
  113. GetConstantValueInSpecific(file, specific_id, return_form_inst_id);
  114. return file.constant_values().GetInstId(return_form_constant_id);
  115. }
  116. } // namespace Carbon::SemIR