function.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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::GetParamFromParamRefId(const File& sem_ir, InstId param_ref_id)
  44. -> ParamInfo {
  45. auto ref = sem_ir.insts().Get(param_ref_id);
  46. if (auto addr_pattern = ref.TryAs<AddrPattern>()) {
  47. param_ref_id = addr_pattern->inner_id;
  48. ref = sem_ir.insts().Get(param_ref_id);
  49. }
  50. auto bind_name = ref.TryAs<AnyBindName>();
  51. if (bind_name) {
  52. param_ref_id = bind_name->value_id;
  53. ref = sem_ir.insts().Get(param_ref_id);
  54. }
  55. return {param_ref_id, ref.As<Param>(), bind_name};
  56. }
  57. auto Function::ParamInfo::GetNameId(const File& sem_ir) -> NameId {
  58. if (bind_name) {
  59. return sem_ir.entity_names().Get(bind_name->entity_name_id).name_id;
  60. } else {
  61. return NameId::Invalid;
  62. }
  63. }
  64. auto Function::GetDeclaredReturnType(const File& file,
  65. SpecificId specific_id) const -> TypeId {
  66. if (!return_storage_id.is_valid()) {
  67. return TypeId::Invalid;
  68. }
  69. return GetTypeInSpecific(file, specific_id,
  70. file.insts().Get(return_storage_id).type_id());
  71. }
  72. } // namespace Carbon::SemIR