function.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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) -> CalleeFunction {
  10. CalleeFunction result = {.function_id = FunctionId::None,
  11. .enclosing_specific_id = SpecificId::None,
  12. .resolved_specific_id = SpecificId::None,
  13. .self_type_id = InstId::None,
  14. .self_id = InstId::None,
  15. .is_error = false};
  16. if (auto specific_function =
  17. sem_ir.insts().TryGetAs<SpecificFunction>(callee_id)) {
  18. result.resolved_specific_id = specific_function->specific_id;
  19. callee_id = specific_function->callee_id;
  20. }
  21. if (auto bound_method = sem_ir.insts().TryGetAs<BoundMethod>(callee_id)) {
  22. result.self_id = bound_method->object_id;
  23. callee_id = bound_method->function_decl_id;
  24. }
  25. // Identify the function we're calling by its type.
  26. auto val_id = sem_ir.constant_values().GetConstantInstId(callee_id);
  27. if (!val_id.has_value()) {
  28. return result;
  29. }
  30. auto fn_type_inst =
  31. sem_ir.types().GetAsInst(sem_ir.insts().Get(val_id).type_id());
  32. if (auto impl_fn_type = fn_type_inst.TryAs<FunctionTypeWithSelfType>()) {
  33. // Combine the associated function's `Self` with the interface function
  34. // data.
  35. result.self_type_id = impl_fn_type->self_id;
  36. fn_type_inst = sem_ir.insts().Get(impl_fn_type->interface_function_type_id);
  37. }
  38. auto fn_type = fn_type_inst.TryAs<FunctionType>();
  39. if (!fn_type) {
  40. result.is_error = fn_type_inst.Is<SemIR::ErrorInst>();
  41. return result;
  42. }
  43. result.function_id = fn_type->function_id;
  44. result.enclosing_specific_id = fn_type->specific_id;
  45. return result;
  46. }
  47. auto Function::ParamPatternInfo::GetNameId(const File& sem_ir) -> NameId {
  48. return sem_ir.entity_names().Get(entity_name_id).name_id;
  49. }
  50. auto Function::GetParamPatternInfoFromPatternId(const File& sem_ir,
  51. InstId pattern_id)
  52. -> ParamPatternInfo {
  53. auto inst_id = pattern_id;
  54. auto inst = sem_ir.insts().Get(inst_id);
  55. if (auto addr_pattern = inst.TryAs<SemIR::AddrPattern>()) {
  56. inst_id = addr_pattern->inner_id;
  57. inst = sem_ir.insts().Get(inst_id);
  58. }
  59. auto param_pattern_id = inst_id;
  60. auto param_pattern_inst = inst.As<SemIR::AnyParamPattern>();
  61. inst_id = param_pattern_inst.subpattern_id;
  62. inst = sem_ir.insts().Get(inst_id);
  63. auto binding_pattern = inst.As<AnyBindingPattern>();
  64. return {.inst_id = param_pattern_id,
  65. .inst = param_pattern_inst,
  66. .entity_name_id = binding_pattern.entity_name_id};
  67. }
  68. auto Function::GetNameFromPatternId(const File& sem_ir, InstId pattern_id)
  69. -> SemIR::NameId {
  70. auto inst_id = pattern_id;
  71. auto inst = sem_ir.insts().Get(inst_id);
  72. if (auto addr_pattern = inst.TryAs<SemIR::AddrPattern>()) {
  73. inst_id = addr_pattern->inner_id;
  74. inst = sem_ir.insts().Get(inst_id);
  75. }
  76. if (inst_id == SemIR::ErrorInst::SingletonInstId) {
  77. return SemIR::NameId::None;
  78. }
  79. auto param_pattern_inst = inst.As<SemIR::AnyParamPattern>();
  80. inst_id = param_pattern_inst.subpattern_id;
  81. inst = sem_ir.insts().Get(inst_id);
  82. if (inst.Is<ReturnSlotPattern>()) {
  83. return SemIR::NameId::ReturnSlot;
  84. }
  85. auto binding_pattern = inst.As<AnyBindingPattern>();
  86. return sem_ir.entity_names().Get(binding_pattern.entity_name_id).name_id;
  87. }
  88. auto Function::GetDeclaredReturnType(const File& file,
  89. SpecificId specific_id) const -> TypeId {
  90. if (!return_slot_pattern_id.has_value()) {
  91. return TypeId::None;
  92. }
  93. return GetTypeInSpecific(file, specific_id,
  94. file.insts().Get(return_slot_pattern_id).type_id());
  95. }
  96. } // namespace Carbon::SemIR