function.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. .enclosing_specific_id = SpecificId::None,
  15. .resolved_specific_id = SpecificId::None,
  16. .self_type_id = InstId::None,
  17. .self_id = InstId::None,
  18. .is_error = false,
  19. .is_cpp_overload_set = 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 (fn_type_inst.TryAs<CppOverloadSetType>()) {
  43. // TODO: Consider evaluating this at runtime instead of having a field.
  44. result.is_cpp_overload_set = true;
  45. return result;
  46. }
  47. if (auto impl_fn_type = fn_type_inst.TryAs<FunctionTypeWithSelfType>()) {
  48. // Combine the associated function's `Self` with the interface function
  49. // data.
  50. result.self_type_id = impl_fn_type->self_id;
  51. fn_type_inst = sem_ir.insts().Get(impl_fn_type->interface_function_type_id);
  52. }
  53. auto fn_type = fn_type_inst.TryAs<FunctionType>();
  54. if (!fn_type) {
  55. result.is_error = fn_type_inst.Is<ErrorInst>();
  56. return result;
  57. }
  58. result.function_id = fn_type->function_id;
  59. result.enclosing_specific_id = fn_type->specific_id;
  60. return result;
  61. }
  62. auto DecomposeVirtualFunction(const File& sem_ir, InstId fn_decl_id,
  63. SpecificId base_class_specific_id)
  64. -> DecomposedVirtualFunction {
  65. // Remap the base's vtable entry to the appropriate constant usable in
  66. // the context of the derived class (for the specific for the base
  67. // class, for instance).
  68. auto fn_decl_const_id =
  69. GetConstantValueInSpecific(sem_ir, base_class_specific_id, fn_decl_id);
  70. fn_decl_id = sem_ir.constant_values().GetInstId(fn_decl_const_id);
  71. auto specific_id = SemIR::SpecificId::None;
  72. auto callee_id = fn_decl_id;
  73. if (auto specific_function =
  74. sem_ir.insts().TryGetAs<SemIR::SpecificFunction>(fn_decl_id)) {
  75. specific_id = specific_function->specific_id;
  76. callee_id = specific_function->callee_id;
  77. }
  78. // Identify the function we're calling by its type.
  79. auto fn_type_inst =
  80. sem_ir.types().GetAsInst(sem_ir.insts().Get(callee_id).type_id());
  81. return {.fn_decl_id = fn_decl_id,
  82. .fn_decl_const_id = fn_decl_const_id,
  83. .function_id = fn_type_inst.As<FunctionType>().function_id,
  84. .specific_id = specific_id};
  85. }
  86. auto Function::GetParamPatternInfoFromPatternId(const File& sem_ir,
  87. InstId pattern_id)
  88. -> std::optional<ParamPatternInfo> {
  89. auto inst_id = pattern_id;
  90. auto inst = sem_ir.insts().Get(inst_id);
  91. sem_ir.insts().TryUnwrap(inst, inst_id, &AddrPattern::inner_id);
  92. auto [var_pattern, var_pattern_id] =
  93. sem_ir.insts().TryUnwrap(inst, inst_id, &VarPattern::subpattern_id);
  94. auto [param_pattern, param_pattern_id] =
  95. sem_ir.insts().TryUnwrap(inst, inst_id, &AnyParamPattern::subpattern_id);
  96. if (!param_pattern) {
  97. return std::nullopt;
  98. }
  99. auto binding_pattern = inst.As<AnyBindingPattern>();
  100. return {{.inst_id = param_pattern_id,
  101. .inst = *param_pattern,
  102. .entity_name_id = binding_pattern.entity_name_id,
  103. .var_pattern_id = var_pattern_id}};
  104. }
  105. auto Function::GetDeclaredReturnType(const File& file,
  106. SpecificId specific_id) const -> TypeId {
  107. if (!return_slot_pattern_id.has_value()) {
  108. return TypeId::None;
  109. }
  110. return ExtractScrutineeType(
  111. file, GetTypeOfInstInSpecific(file, specific_id, return_slot_pattern_id));
  112. }
  113. } // namespace Carbon::SemIR