function.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/sem_ir/file.h"
  7. #include "toolchain/sem_ir/generic.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. #include "toolchain/sem_ir/typed_insts.h"
  10. namespace Carbon::SemIR {
  11. auto GetCalleeFunction(const File& sem_ir, InstId callee_id,
  12. SpecificId specific_id) -> CalleeFunction {
  13. CalleeFunction result = {.function_id = FunctionId::None,
  14. .cpp_overload_set_id = CppOverloadSetId::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. .is_error = false};
  20. if (auto bound_method = sem_ir.insts().TryGetAs<BoundMethod>(callee_id)) {
  21. result.self_id = bound_method->object_id;
  22. callee_id = bound_method->function_decl_id;
  23. }
  24. if (specific_id.has_value()) {
  25. callee_id = sem_ir.constant_values().GetInstIdIfValid(
  26. GetConstantValueInSpecific(sem_ir, specific_id, callee_id));
  27. CARBON_CHECK(callee_id.has_value(),
  28. "Invalid callee id in a specific context");
  29. }
  30. if (auto specific_function =
  31. sem_ir.insts().TryGetAs<SpecificFunction>(callee_id)) {
  32. result.resolved_specific_id = specific_function->specific_id;
  33. callee_id = specific_function->callee_id;
  34. }
  35. // Identify the function we're calling by its type.
  36. auto val_id = sem_ir.constant_values().GetConstantInstId(callee_id);
  37. if (!val_id.has_value()) {
  38. return result;
  39. }
  40. auto fn_type_inst =
  41. sem_ir.types().GetAsInst(sem_ir.insts().Get(val_id).type_id());
  42. if (auto cpp_overload_set_type = fn_type_inst.TryAs<CppOverloadSetType>()) {
  43. result.cpp_overload_set_id = cpp_overload_set_type->overload_set_id;
  44. return result;
  45. }
  46. if (auto impl_fn_type = fn_type_inst.TryAs<FunctionTypeWithSelfType>()) {
  47. // Combine the associated function's `Self` with the interface function
  48. // data.
  49. result.self_type_id = impl_fn_type->self_id;
  50. fn_type_inst = sem_ir.insts().Get(impl_fn_type->interface_function_type_id);
  51. }
  52. auto fn_type = fn_type_inst.TryAs<FunctionType>();
  53. if (!fn_type) {
  54. result.is_error = fn_type_inst.Is<ErrorInst>();
  55. return result;
  56. }
  57. result.function_id = fn_type->function_id;
  58. result.enclosing_specific_id = fn_type->specific_id;
  59. return result;
  60. }
  61. auto DecomposeVirtualFunction(const File& sem_ir, InstId fn_decl_id,
  62. SpecificId base_class_specific_id)
  63. -> DecomposedVirtualFunction {
  64. // Remap the base's vtable entry to the appropriate constant usable in
  65. // the context of the derived class (for the specific for the base
  66. // class, for instance).
  67. auto fn_decl_const_id =
  68. GetConstantValueInSpecific(sem_ir, base_class_specific_id, fn_decl_id);
  69. fn_decl_id = sem_ir.constant_values().GetInstId(fn_decl_const_id);
  70. auto specific_id = SpecificId::None;
  71. auto callee_id = fn_decl_id;
  72. if (auto specific_function =
  73. sem_ir.insts().TryGetAs<SpecificFunction>(fn_decl_id)) {
  74. specific_id = specific_function->specific_id;
  75. callee_id = specific_function->callee_id;
  76. }
  77. // Identify the function we're calling by its type.
  78. auto fn_type_inst =
  79. sem_ir.types().GetAsInst(sem_ir.insts().Get(callee_id).type_id());
  80. return {.fn_decl_id = fn_decl_id,
  81. .fn_decl_const_id = fn_decl_const_id,
  82. .function_id = fn_type_inst.As<FunctionType>().function_id,
  83. .specific_id = specific_id};
  84. }
  85. auto Function::GetParamPatternInfoFromPatternId(const File& sem_ir,
  86. InstId pattern_id)
  87. -> std::optional<ParamPatternInfo> {
  88. auto inst_id = pattern_id;
  89. auto inst = sem_ir.insts().Get(inst_id);
  90. sem_ir.insts().TryUnwrap(inst, inst_id, &AddrPattern::inner_id);
  91. auto [var_pattern, var_pattern_id] =
  92. sem_ir.insts().TryUnwrap(inst, inst_id, &VarPattern::subpattern_id);
  93. auto [param_pattern, param_pattern_id] =
  94. sem_ir.insts().TryUnwrap(inst, inst_id, &AnyParamPattern::subpattern_id);
  95. if (!param_pattern) {
  96. return std::nullopt;
  97. }
  98. auto binding_pattern = inst.As<AnyBindingPattern>();
  99. return {{.inst_id = param_pattern_id,
  100. .inst = *param_pattern,
  101. .entity_name_id = binding_pattern.entity_name_id,
  102. .var_pattern_id = var_pattern_id}};
  103. }
  104. auto Function::GetDeclaredReturnType(const File& file,
  105. SpecificId specific_id) const -> TypeId {
  106. if (!return_slot_pattern_id.has_value()) {
  107. return TypeId::None;
  108. }
  109. return ExtractScrutineeType(
  110. file, GetTypeOfInstInSpecific(file, specific_id, return_slot_pattern_id));
  111. }
  112. } // namespace Carbon::SemIR