function.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "toolchain/sem_ir/file.h"
  6. #include "toolchain/sem_ir/generic.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::SemIR {
  9. auto GetCalleeFunction(const File& sem_ir, InstId callee_id,
  10. SpecificId specific_id) -> CalleeFunction {
  11. CalleeFunction result = {.function_id = FunctionId::None,
  12. .enclosing_specific_id = SpecificId::None,
  13. .resolved_specific_id = SpecificId::None,
  14. .self_type_id = InstId::None,
  15. .self_id = InstId::None,
  16. .is_error = false};
  17. if (auto bound_method = sem_ir.insts().TryGetAs<BoundMethod>(callee_id)) {
  18. result.self_id = bound_method->object_id;
  19. callee_id = bound_method->function_decl_id;
  20. }
  21. if (specific_id.has_value()) {
  22. callee_id = sem_ir.constant_values().GetInstIdIfValid(
  23. GetConstantValueInSpecific(sem_ir, specific_id, callee_id));
  24. CARBON_CHECK(callee_id.has_value(),
  25. "Invalid callee id in a specific context");
  26. }
  27. if (auto specific_function =
  28. sem_ir.insts().TryGetAs<SpecificFunction>(callee_id)) {
  29. result.resolved_specific_id = specific_function->specific_id;
  30. callee_id = specific_function->callee_id;
  31. }
  32. // Identify the function we're calling by its type.
  33. auto val_id = sem_ir.constant_values().GetConstantInstId(callee_id);
  34. if (!val_id.has_value()) {
  35. return result;
  36. }
  37. auto fn_type_inst =
  38. sem_ir.types().GetAsInst(sem_ir.insts().Get(val_id).type_id());
  39. if (auto impl_fn_type = fn_type_inst.TryAs<FunctionTypeWithSelfType>()) {
  40. // Combine the associated function's `Self` with the interface function
  41. // data.
  42. result.self_type_id = impl_fn_type->self_id;
  43. fn_type_inst = sem_ir.insts().Get(impl_fn_type->interface_function_type_id);
  44. }
  45. auto fn_type = fn_type_inst.TryAs<FunctionType>();
  46. if (!fn_type) {
  47. result.is_error = fn_type_inst.Is<SemIR::ErrorInst>();
  48. return result;
  49. }
  50. result.function_id = fn_type->function_id;
  51. result.enclosing_specific_id = fn_type->specific_id;
  52. return result;
  53. }
  54. auto Function::GetParamPatternInfoFromPatternId(const File& sem_ir,
  55. InstId pattern_id)
  56. -> std::optional<ParamPatternInfo> {
  57. auto inst_id = pattern_id;
  58. auto inst = sem_ir.insts().Get(inst_id);
  59. if (auto addr_pattern = inst.TryAs<SemIR::AddrPattern>()) {
  60. inst_id = addr_pattern->inner_id;
  61. inst = sem_ir.insts().Get(inst_id);
  62. }
  63. auto param_pattern_inst = inst.TryAs<SemIR::AnyParamPattern>();
  64. if (!param_pattern_inst) {
  65. return std::nullopt;
  66. }
  67. auto param_pattern_id = inst_id;
  68. inst_id = param_pattern_inst->subpattern_id;
  69. inst = sem_ir.insts().Get(inst_id);
  70. auto binding_pattern = inst.As<AnyBindingPattern>();
  71. return {{.inst_id = param_pattern_id,
  72. .inst = *param_pattern_inst,
  73. .entity_name_id = binding_pattern.entity_name_id}};
  74. }
  75. auto Function::GetDeclaredReturnType(const File& file,
  76. SpecificId specific_id) const -> TypeId {
  77. if (!return_slot_pattern_id.has_value()) {
  78. return TypeId::None;
  79. }
  80. return GetTypeOfInstInSpecific(file, specific_id, return_slot_pattern_id);
  81. }
  82. } // namespace Carbon::SemIR