function.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. }
  34. // Identify the function we're calling by its type.
  35. auto val_id = sem_ir.constant_values().GetConstantInstId(callee_id);
  36. if (!val_id.has_value()) {
  37. return CalleeNonFunction();
  38. }
  39. auto fn_type_inst =
  40. sem_ir.types().GetAsInst(sem_ir.insts().Get(val_id).type_id());
  41. if (auto cpp_overload_set_type = fn_type_inst.TryAs<CppOverloadSetType>()) {
  42. CARBON_CHECK(!fn.resolved_specific_id.has_value(),
  43. "Only `SpecificFunction` will be resolved, not C++ overloads");
  44. return CalleeCppOverloadSet{
  45. .cpp_overload_set_id = cpp_overload_set_type->overload_set_id,
  46. .self_id = fn.self_id};
  47. }
  48. if (auto impl_fn_type = fn_type_inst.TryAs<FunctionTypeWithSelfType>()) {
  49. // Combine the associated function's `Self` with the interface function
  50. // data.
  51. fn.self_type_id = impl_fn_type->self_id;
  52. fn_type_inst = sem_ir.insts().Get(impl_fn_type->interface_function_type_id);
  53. }
  54. auto fn_type = fn_type_inst.TryAs<FunctionType>();
  55. if (!fn_type) {
  56. if (fn_type_inst.Is<ErrorInst>()) {
  57. return CalleeError();
  58. }
  59. return CalleeNonFunction();
  60. }
  61. fn.function_id = fn_type->function_id;
  62. fn.enclosing_specific_id = fn_type->specific_id;
  63. return fn;
  64. }
  65. auto GetCalleeAsFunction(const File& sem_ir, InstId callee_id,
  66. SpecificId specific_id) -> CalleeFunction {
  67. return std::get<CalleeFunction>(GetCallee(sem_ir, callee_id, specific_id));
  68. }
  69. auto DecomposeVirtualFunction(const File& sem_ir, InstId fn_decl_id,
  70. SpecificId base_class_specific_id)
  71. -> DecomposedVirtualFunction {
  72. // Remap the base's vtable entry to the appropriate constant usable in
  73. // the context of the derived class (for the specific for the base
  74. // class, for instance).
  75. auto fn_decl_const_id =
  76. GetConstantValueInSpecific(sem_ir, base_class_specific_id, fn_decl_id);
  77. fn_decl_id = sem_ir.constant_values().GetInstId(fn_decl_const_id);
  78. auto specific_id = SpecificId::None;
  79. auto callee_id = fn_decl_id;
  80. if (auto specific_function =
  81. sem_ir.insts().TryGetAs<SpecificFunction>(fn_decl_id)) {
  82. specific_id = specific_function->specific_id;
  83. callee_id = specific_function->callee_id;
  84. }
  85. // Identify the function we're calling by its type.
  86. auto fn_type_inst =
  87. sem_ir.types().GetAsInst(sem_ir.insts().Get(callee_id).type_id());
  88. return {.fn_decl_id = fn_decl_id,
  89. .fn_decl_const_id = fn_decl_const_id,
  90. .function_id = fn_type_inst.As<FunctionType>().function_id,
  91. .specific_id = specific_id};
  92. }
  93. auto Function::GetParamPatternInfoFromPatternId(const File& sem_ir,
  94. InstId pattern_id)
  95. -> std::optional<ParamPatternInfo> {
  96. auto inst_id = pattern_id;
  97. auto inst = sem_ir.insts().Get(inst_id);
  98. sem_ir.insts().TryUnwrap(inst, inst_id, &VarPattern::subpattern_id);
  99. auto [param_pattern, param_pattern_id] =
  100. sem_ir.insts().TryUnwrap(inst, inst_id, &AnyParamPattern::subpattern_id);
  101. if (!param_pattern) {
  102. return std::nullopt;
  103. }
  104. auto binding_pattern = inst.As<AnyBindingPattern>();
  105. return {{.inst_id = param_pattern_id,
  106. .inst = *param_pattern,
  107. .entity_name_id = binding_pattern.entity_name_id}};
  108. }
  109. auto Function::GetDeclaredReturnType(const File& file,
  110. SpecificId specific_id) const -> TypeId {
  111. if (!return_slot_pattern_id.has_value()) {
  112. return TypeId::None;
  113. }
  114. return ExtractScrutineeType(
  115. file, GetTypeOfInstInSpecific(file, specific_id, return_slot_pattern_id));
  116. }
  117. } // namespace Carbon::SemIR