function.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. if (auto addr_pattern = inst.TryAs<AddrPattern>()) {
  61. inst_id = addr_pattern->inner_id;
  62. inst = sem_ir.insts().Get(inst_id);
  63. }
  64. auto param_pattern_inst = inst.TryAs<AnyParamPattern>();
  65. if (!param_pattern_inst) {
  66. return std::nullopt;
  67. }
  68. auto param_pattern_id = inst_id;
  69. inst_id = param_pattern_inst->subpattern_id;
  70. inst = sem_ir.insts().Get(inst_id);
  71. auto binding_pattern = inst.As<AnyBindingPattern>();
  72. return {{.inst_id = param_pattern_id,
  73. .inst = *param_pattern_inst,
  74. .entity_name_id = binding_pattern.entity_name_id}};
  75. }
  76. auto Function::GetDeclaredReturnType(const File& file,
  77. SpecificId specific_id) const -> TypeId {
  78. if (!return_slot_pattern_id.has_value()) {
  79. return TypeId::None;
  80. }
  81. return GetTypeOfInstInSpecific(file, specific_id, return_slot_pattern_id);
  82. }
  83. } // namespace Carbon::SemIR