function.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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, SpecificId specific_id)
  13. -> 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 (specific_id.has_value()) {
  24. callee_id = sem_ir.constant_values().GetInstIdIfValid(
  25. GetConstantValueInSpecific(sem_ir, 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 specific_id) -> CalleeFunction {
  71. return std::get<CalleeFunction>(GetCallee(sem_ir, callee_id, specific_id));
  72. }
  73. auto DecomposeVirtualFunction(const File& sem_ir, InstId fn_decl_id,
  74. SpecificId base_class_specific_id)
  75. -> DecomposedVirtualFunction {
  76. // Remap the base's vtable entry to the appropriate constant usable in
  77. // the context of the derived class (for the specific for the base
  78. // class, for instance).
  79. auto fn_decl_const_id =
  80. GetConstantValueInSpecific(sem_ir, base_class_specific_id, fn_decl_id);
  81. fn_decl_id = sem_ir.constant_values().GetInstId(fn_decl_const_id);
  82. auto specific_id = SpecificId::None;
  83. auto callee_id = fn_decl_id;
  84. if (auto specific_function =
  85. sem_ir.insts().TryGetAs<SpecificFunction>(fn_decl_id)) {
  86. specific_id = specific_function->specific_id;
  87. callee_id = specific_function->callee_id;
  88. }
  89. // Identify the function we're calling by its type.
  90. auto fn_type_inst =
  91. sem_ir.types().GetAsInst(sem_ir.insts().Get(callee_id).type_id());
  92. return {.fn_decl_id = fn_decl_id,
  93. .fn_decl_const_id = fn_decl_const_id,
  94. .function_id = fn_type_inst.As<FunctionType>().function_id,
  95. .specific_id = specific_id};
  96. }
  97. auto Function::GetParamPatternInfoFromPatternId(const File& sem_ir,
  98. InstId pattern_id)
  99. -> std::optional<ParamPatternInfo> {
  100. auto inst_id = pattern_id;
  101. auto inst = sem_ir.insts().Get(inst_id);
  102. sem_ir.insts().TryUnwrap(inst, inst_id, &VarPattern::subpattern_id);
  103. auto [param_pattern, param_pattern_id] =
  104. sem_ir.insts().TryUnwrap(inst, inst_id, &AnyParamPattern::subpattern_id);
  105. if (!param_pattern) {
  106. return std::nullopt;
  107. }
  108. auto binding_pattern = inst.As<AnyBindingPattern>();
  109. return {{.inst_id = param_pattern_id,
  110. .inst = *param_pattern,
  111. .entity_name_id = binding_pattern.entity_name_id}};
  112. }
  113. auto Function::GetDeclaredReturnType(const File& file,
  114. SpecificId specific_id) const -> TypeId {
  115. if (!return_type_inst_id.has_value()) {
  116. return TypeId::None;
  117. }
  118. return file.types().GetTypeIdForTypeConstantId(
  119. GetConstantValueInSpecific(file, specific_id, return_type_inst_id));
  120. }
  121. } // namespace Carbon::SemIR