function.cpp 1.5 KB

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