function.cpp 4.0 KB

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