function.h 3.7 KB

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