function.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 an instruction from `param_patterns_id` or
  54. // `implicit_param_patterns_id`, returns a `ParamPatternInfo` value with the
  55. // corresponding instruction, its ID, and the entity_name_id of the underlying
  56. // binding pattern.
  57. struct ParamPatternInfo {
  58. InstId inst_id;
  59. ParamPattern inst;
  60. EntityNameId entity_name_id;
  61. auto GetNameId(const File& sem_ir) -> NameId;
  62. };
  63. static auto GetParamPatternInfoFromPatternId(const File& sem_ir,
  64. InstId param_pattern_id)
  65. -> ParamPatternInfo;
  66. // Given a parameter reference instruction from `param_refs_id` or
  67. // `implicit_param_refs_id`, returns a `ParamInfo` value with the
  68. // corresponding instruction, its ID, and the name binding, if present.
  69. struct ParamInfo {
  70. InstId inst_id;
  71. Param inst;
  72. std::optional<AnyBindName> bind_name;
  73. // Gets the name from `bind_name`. Returns invalid if that is not present.
  74. auto GetNameId(const File& sem_ir) -> NameId;
  75. };
  76. static auto GetParamFromParamRefId(const File& sem_ir, InstId param_ref_id)
  77. -> ParamInfo;
  78. // Gets the declared return type for a specific version of this function, or
  79. // the canonical return type for the original declaration no specific is
  80. // specified. Returns `Invalid` if no return type was specified, in which
  81. // case the effective return type is an empty tuple.
  82. auto GetDeclaredReturnType(const File& file,
  83. SpecificId specific_id = SpecificId::Invalid) const
  84. -> TypeId;
  85. };
  86. class File;
  87. struct CalleeFunction {
  88. // The function. Invalid if not a function.
  89. SemIR::FunctionId function_id;
  90. // The specific that contains the function.
  91. SemIR::SpecificId enclosing_specific_id;
  92. // The specific for the callee itself, in a resolved call.
  93. SemIR::SpecificId resolved_specific_id;
  94. // The bound `self` parameter. Invalid if not a method.
  95. SemIR::InstId self_id;
  96. // True if an error instruction was found.
  97. bool is_error;
  98. };
  99. // Returns information for the function corresponding to callee_id.
  100. auto GetCalleeFunction(const File& sem_ir, InstId callee_id) -> CalleeFunction;
  101. } // namespace Carbon::SemIR
  102. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_