function.cpp 5.4 KB

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