function.cpp 3.3 KB

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