function.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 CalleeFunction::Print(llvm::raw_ostream& out) const -> void {
  13. out << "{";
  14. CARBON_KIND_SWITCH(info) {
  15. case CARBON_KIND(SemIR::CalleeFunction::Function fn): {
  16. out << "Function{function_id: " << fn.function_id
  17. << ", enclosing_specific_id: " << fn.enclosing_specific_id
  18. << ", resolved_specific_id: " << fn.resolved_specific_id
  19. << ", self_type_id: " << fn.self_type_id
  20. << ", self_id: " << fn.self_id << "}";
  21. break;
  22. }
  23. case CARBON_KIND(SemIR::CalleeFunction::CppOverloadSet overload): {
  24. out << "CppOverload{cpp_overload_set_id: " << overload.cpp_overload_set_id
  25. << ", self_id: " << overload.self_id << "}";
  26. break;
  27. }
  28. case CARBON_KIND(SemIR::CalleeFunction::Error _): {
  29. out << "Error{}";
  30. break;
  31. }
  32. case CARBON_KIND(SemIR::CalleeFunction::NonFunction _): {
  33. out << "Other{}";
  34. break;
  35. }
  36. }
  37. out << "}";
  38. }
  39. auto GetCalleeFunction(const File& sem_ir, InstId callee_id,
  40. SpecificId specific_id) -> CalleeFunction {
  41. CalleeFunction::Function fn = {.function_id = FunctionId::None,
  42. .enclosing_specific_id = SpecificId::None,
  43. .resolved_specific_id = SpecificId::None,
  44. .self_type_id = InstId::None,
  45. .self_id = InstId::None};
  46. if (auto bound_method = sem_ir.insts().TryGetAs<BoundMethod>(callee_id)) {
  47. fn.self_id = bound_method->object_id;
  48. callee_id = bound_method->function_decl_id;
  49. }
  50. if (specific_id.has_value()) {
  51. callee_id = sem_ir.constant_values().GetInstIdIfValid(
  52. GetConstantValueInSpecific(sem_ir, specific_id, callee_id));
  53. CARBON_CHECK(callee_id.has_value(),
  54. "Invalid callee id in a specific context");
  55. }
  56. if (auto specific_function =
  57. sem_ir.insts().TryGetAs<SpecificFunction>(callee_id)) {
  58. fn.resolved_specific_id = specific_function->specific_id;
  59. callee_id = specific_function->callee_id;
  60. }
  61. // Identify the function we're calling by its type.
  62. auto val_id = sem_ir.constant_values().GetConstantInstId(callee_id);
  63. if (!val_id.has_value()) {
  64. return {.info = CalleeFunction::NonFunction()};
  65. }
  66. auto fn_type_inst =
  67. sem_ir.types().GetAsInst(sem_ir.insts().Get(val_id).type_id());
  68. if (auto cpp_overload_set_type = fn_type_inst.TryAs<CppOverloadSetType>()) {
  69. CARBON_CHECK(!fn.resolved_specific_id.has_value(),
  70. "Only `SpecificFunction` will be resolved, not C++ overloads");
  71. return {.info = CalleeFunction::CppOverloadSet{
  72. .cpp_overload_set_id = cpp_overload_set_type->overload_set_id,
  73. .self_id = fn.self_id}};
  74. }
  75. if (auto impl_fn_type = fn_type_inst.TryAs<FunctionTypeWithSelfType>()) {
  76. // Combine the associated function's `Self` with the interface function
  77. // data.
  78. fn.self_type_id = impl_fn_type->self_id;
  79. fn_type_inst = sem_ir.insts().Get(impl_fn_type->interface_function_type_id);
  80. }
  81. auto fn_type = fn_type_inst.TryAs<FunctionType>();
  82. if (!fn_type) {
  83. if (fn_type_inst.Is<ErrorInst>()) {
  84. return {.info = CalleeFunction::Error()};
  85. }
  86. return {.info = CalleeFunction::NonFunction()};
  87. }
  88. fn.function_id = fn_type->function_id;
  89. fn.enclosing_specific_id = fn_type->specific_id;
  90. return {.info = fn};
  91. }
  92. auto GetCalleeFunctionAsFunction(const File& sem_ir, InstId callee_id,
  93. SpecificId specific_id)
  94. -> CalleeFunction::Function {
  95. return std::get<CalleeFunction::Function>(
  96. GetCalleeFunction(sem_ir, callee_id, specific_id).info);
  97. }
  98. auto DecomposeVirtualFunction(const File& sem_ir, InstId fn_decl_id,
  99. SpecificId base_class_specific_id)
  100. -> DecomposedVirtualFunction {
  101. // Remap the base's vtable entry to the appropriate constant usable in
  102. // the context of the derived class (for the specific for the base
  103. // class, for instance).
  104. auto fn_decl_const_id =
  105. GetConstantValueInSpecific(sem_ir, base_class_specific_id, fn_decl_id);
  106. fn_decl_id = sem_ir.constant_values().GetInstId(fn_decl_const_id);
  107. auto specific_id = SpecificId::None;
  108. auto callee_id = fn_decl_id;
  109. if (auto specific_function =
  110. sem_ir.insts().TryGetAs<SpecificFunction>(fn_decl_id)) {
  111. specific_id = specific_function->specific_id;
  112. callee_id = specific_function->callee_id;
  113. }
  114. // Identify the function we're calling by its type.
  115. auto fn_type_inst =
  116. sem_ir.types().GetAsInst(sem_ir.insts().Get(callee_id).type_id());
  117. return {.fn_decl_id = fn_decl_id,
  118. .fn_decl_const_id = fn_decl_const_id,
  119. .function_id = fn_type_inst.As<FunctionType>().function_id,
  120. .specific_id = specific_id};
  121. }
  122. auto Function::GetParamPatternInfoFromPatternId(const File& sem_ir,
  123. InstId pattern_id)
  124. -> std::optional<ParamPatternInfo> {
  125. auto inst_id = pattern_id;
  126. auto inst = sem_ir.insts().Get(inst_id);
  127. sem_ir.insts().TryUnwrap(inst, inst_id, &AddrPattern::inner_id);
  128. auto [var_pattern, var_pattern_id] =
  129. sem_ir.insts().TryUnwrap(inst, inst_id, &VarPattern::subpattern_id);
  130. auto [param_pattern, param_pattern_id] =
  131. sem_ir.insts().TryUnwrap(inst, inst_id, &AnyParamPattern::subpattern_id);
  132. if (!param_pattern) {
  133. return std::nullopt;
  134. }
  135. auto binding_pattern = inst.As<AnyBindingPattern>();
  136. return {{.inst_id = param_pattern_id,
  137. .inst = *param_pattern,
  138. .entity_name_id = binding_pattern.entity_name_id,
  139. .var_pattern_id = var_pattern_id}};
  140. }
  141. auto Function::GetDeclaredReturnType(const File& file,
  142. SpecificId specific_id) const -> TypeId {
  143. if (!return_slot_pattern_id.has_value()) {
  144. return TypeId::None;
  145. }
  146. return ExtractScrutineeType(
  147. file, GetTypeOfInstInSpecific(file, specific_id, return_slot_pattern_id));
  148. }
  149. } // namespace Carbon::SemIR