function.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. #ifndef CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_
  6. #include "toolchain/sem_ir/builtin_function_kind.h"
  7. #include "toolchain/sem_ir/entity_with_params_base.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. #include "toolchain/sem_ir/typed_insts.h"
  10. namespace Carbon::SemIR {
  11. // Function-specific fields.
  12. struct FunctionFields {
  13. // The following members always have values, and do not change throughout the
  14. // lifetime of the function.
  15. // The storage for the return value, which is a reference expression whose
  16. // type is the return type of the function. This may or may not be used by the
  17. // function, depending on whether the return type needs a return slot, but is
  18. // always present if the function has a declared return type.
  19. InstId return_storage_id;
  20. // The following member is set on the first call to the function, or at the
  21. // point where the function is defined.
  22. // The following members are set at the end of a builtin function definition.
  23. // If this is a builtin function, the corresponding builtin kind.
  24. BuiltinFunctionKind builtin_function_kind = BuiltinFunctionKind::None;
  25. // The following members are accumulated throughout the function definition.
  26. // A list of the statically reachable code blocks in the body of the
  27. // function, in lexical order. The first block is the entry block. This will
  28. // be empty for declarations that don't have a visible definition.
  29. llvm::SmallVector<InstBlockId> body_block_ids = {};
  30. };
  31. // A function. See EntityWithParamsBase regarding the inheritance here.
  32. struct Function : public EntityWithParamsBase,
  33. public FunctionFields,
  34. public Printable<Function> {
  35. auto Print(llvm::raw_ostream& out) const -> void {
  36. out << "{";
  37. PrintBaseFields(out);
  38. if (return_storage_id.is_valid()) {
  39. out << ", return_storage: " << return_storage_id;
  40. }
  41. if (!body_block_ids.empty()) {
  42. out << llvm::formatv(
  43. ", body: [{0}]",
  44. llvm::make_range(body_block_ids.begin(), body_block_ids.end()));
  45. }
  46. out << "}";
  47. }
  48. // Given a parameter reference instruction from `param_refs_id` or
  49. // `implicit_param_refs_id`, returns the corresponding `Param` instruction
  50. // and its ID.
  51. static auto GetParamFromParamRefId(const File& sem_ir, InstId param_ref_id)
  52. -> std::pair<InstId, Param>;
  53. // Gets the declared return type for a specific version of this function, or
  54. // the canonical return type for the original declaration no specific is
  55. // specified. Returns `Invalid` if no return type was specified, in which
  56. // case the effective return type is an empty tuple.
  57. auto GetDeclaredReturnType(const File& file,
  58. SpecificId specific_id = SpecificId::Invalid) const
  59. -> TypeId;
  60. };
  61. class File;
  62. struct CalleeFunction {
  63. // The function. Invalid if not a function.
  64. SemIR::FunctionId function_id;
  65. // The specific that contains the function.
  66. SemIR::SpecificId specific_id;
  67. // The bound `self` parameter. Invalid if not a method.
  68. SemIR::InstId self_id;
  69. // True if an error instruction was found.
  70. bool is_error;
  71. };
  72. // Returns information for the function corresponding to callee_id.
  73. auto GetCalleeFunction(const File& sem_ir, InstId callee_id) -> CalleeFunction;
  74. } // namespace Carbon::SemIR
  75. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_