function.h 3.5 KB

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