function.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. .specific_id = SpecificId::Invalid,
  12. .self_id = InstId::Invalid,
  13. .is_error = false};
  14. if (auto bound_method = sem_ir.insts().TryGetAs<BoundMethod>(callee_id)) {
  15. result.self_id = bound_method->object_id;
  16. callee_id = bound_method->function_id;
  17. }
  18. // Identify the function we're calling.
  19. auto val_id = sem_ir.constant_values().GetConstantInstId(callee_id);
  20. if (!val_id.is_valid()) {
  21. return result;
  22. }
  23. auto val_inst = sem_ir.insts().Get(val_id);
  24. auto struct_val = val_inst.TryAs<StructValue>();
  25. if (!struct_val) {
  26. result.is_error = val_inst.type_id() == SemIR::TypeId::Error;
  27. return result;
  28. }
  29. auto fn_type = sem_ir.types().TryGetAs<FunctionType>(struct_val->type_id);
  30. if (!fn_type) {
  31. return result;
  32. }
  33. result.function_id = fn_type->function_id;
  34. result.specific_id = fn_type->specific_id;
  35. return result;
  36. }
  37. auto Function::GetDeclaredReturnType(const File& file,
  38. SpecificId specific_id) const -> TypeId {
  39. if (!return_storage_id.is_valid()) {
  40. return TypeId::Invalid;
  41. }
  42. return GetTypeInSpecific(file, specific_id,
  43. file.insts().Get(return_storage_id).type_id());
  44. }
  45. } // namespace Carbon::SemIR