function.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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::Invalid,
  11. .enclosing_specific_id = SpecificId::Invalid,
  12. .resolved_specific_id = SpecificId::Invalid,
  13. .self_id = InstId::Invalid,
  14. .is_error = false};
  15. if (auto specific_function =
  16. sem_ir.insts().TryGetAs<SpecificFunction>(callee_id)) {
  17. result.resolved_specific_id = specific_function->specific_id;
  18. callee_id = specific_function->callee_id;
  19. }
  20. if (auto bound_method = sem_ir.insts().TryGetAs<BoundMethod>(callee_id)) {
  21. result.self_id = bound_method->object_id;
  22. callee_id = bound_method->function_id;
  23. }
  24. // Identify the function we're calling.
  25. auto val_id = sem_ir.constant_values().GetConstantInstId(callee_id);
  26. if (!val_id.is_valid()) {
  27. return result;
  28. }
  29. auto val_inst = sem_ir.insts().Get(val_id);
  30. auto struct_val = val_inst.TryAs<StructValue>();
  31. if (!struct_val) {
  32. result.is_error = val_inst.type_id() == SemIR::TypeId::Error;
  33. return result;
  34. }
  35. auto fn_type = sem_ir.types().TryGetAs<FunctionType>(struct_val->type_id);
  36. if (!fn_type) {
  37. return result;
  38. }
  39. result.function_id = fn_type->function_id;
  40. result.enclosing_specific_id = fn_type->specific_id;
  41. return result;
  42. }
  43. auto Function::ParamPatternInfo::GetNameId(const File& sem_ir) -> NameId {
  44. return sem_ir.entity_names().Get(entity_name_id).name_id;
  45. }
  46. auto Function::GetParamPatternInfoFromPatternId(const File& sem_ir,
  47. InstId pattern_id)
  48. -> ParamPatternInfo {
  49. auto inst_id = pattern_id;
  50. auto inst = sem_ir.insts().Get(inst_id);
  51. if (auto addr_pattern = inst.TryAs<SemIR::AddrPattern>()) {
  52. inst_id = addr_pattern->inner_id;
  53. inst = sem_ir.insts().Get(inst_id);
  54. }
  55. auto param_pattern_id = inst_id;
  56. auto param_pattern_inst = inst.As<SemIR::ParamPattern>();
  57. inst_id = param_pattern_inst.subpattern_id;
  58. inst = sem_ir.insts().Get(inst_id);
  59. auto binding_pattern = inst.As<AnyBindingPattern>();
  60. return {.inst_id = param_pattern_id,
  61. .inst = param_pattern_inst,
  62. .entity_name_id = binding_pattern.entity_name_id};
  63. }
  64. auto Function::GetParamFromParamRefId(const File& sem_ir, InstId param_ref_id)
  65. -> ParamInfo {
  66. auto ref = sem_ir.insts().Get(param_ref_id);
  67. auto bind_name = ref.TryAs<AnyBindName>();
  68. if (bind_name) {
  69. param_ref_id = bind_name->value_id;
  70. ref = sem_ir.insts().Get(param_ref_id);
  71. } else {
  72. CARBON_FATAL();
  73. }
  74. return {param_ref_id, ref.As<Param>(), bind_name};
  75. }
  76. auto Function::ParamInfo::GetNameId(const File& sem_ir) -> NameId {
  77. if (bind_name) {
  78. return sem_ir.entity_names().Get(bind_name->entity_name_id).name_id;
  79. } else {
  80. return NameId::Invalid;
  81. }
  82. }
  83. auto Function::GetDeclaredReturnType(const File& file,
  84. SpecificId specific_id) const -> TypeId {
  85. if (!return_storage_id.is_valid()) {
  86. return TypeId::Invalid;
  87. }
  88. return GetTypeInSpecific(file, specific_id,
  89. file.insts().Get(return_storage_id).type_id());
  90. }
  91. } // namespace Carbon::SemIR