function.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/ids.h"
  8. #include "toolchain/sem_ir/typed_insts.h"
  9. namespace Carbon::SemIR {
  10. // A function.
  11. struct Function : public Printable<Function> {
  12. auto Print(llvm::raw_ostream& out) const -> void {
  13. out << "{name: " << name_id << ", enclosing_scope: " << enclosing_scope_id
  14. << ", param_refs: " << param_refs_id;
  15. if (return_type_id.is_valid()) {
  16. out << ", return_type: " << return_type_id;
  17. }
  18. if (return_slot_id.is_valid()) {
  19. out << ", return_slot: " << return_slot_id;
  20. }
  21. if (!body_block_ids.empty()) {
  22. out << llvm::formatv(
  23. ", body: [{0}]",
  24. llvm::make_range(body_block_ids.begin(), body_block_ids.end()));
  25. }
  26. out << "}";
  27. }
  28. // Given a parameter reference instruction from `param_refs_id` or
  29. // `implicit_param_refs_id`, returns the corresponding `Param` instruction
  30. // and its ID.
  31. static auto GetParamFromParamRefId(const File& sem_ir, InstId param_ref_id)
  32. -> std::pair<InstId, Param>;
  33. // The following members always have values, and do not change throughout the
  34. // lifetime of the function.
  35. // The function name.
  36. NameId name_id;
  37. // The enclosing scope.
  38. NameScopeId enclosing_scope_id;
  39. // The first declaration of the function. This is a FunctionDecl.
  40. InstId decl_id;
  41. // A block containing a single reference instruction per implicit parameter.
  42. InstBlockId implicit_param_refs_id;
  43. // A block containing a single reference instruction per parameter.
  44. InstBlockId param_refs_id;
  45. // The return type. This will be invalid if the return type wasn't specified.
  46. TypeId return_type_id;
  47. // The storage for the return value, which is a reference expression whose
  48. // type is the return type of the function. Will be invalid if the function
  49. // doesn't have a return slot. If this is valid, a call to the function is
  50. // expected to have an additional final argument corresponding to the return
  51. // slot.
  52. InstId return_slot_id;
  53. // Whether the declaration is extern.
  54. bool is_extern;
  55. // The following members are set at the end of a builtin function definition.
  56. // If this is a builtin function, the corresponding builtin kind.
  57. BuiltinFunctionKind builtin_kind = BuiltinFunctionKind::None;
  58. // The following members are set at the `{` of the function definition.
  59. // The definition, if the function has been defined or is currently being
  60. // defined. This is a FunctionDecl.
  61. InstId definition_id = InstId::Invalid;
  62. // The following members are accumulated throughout the function definition.
  63. // A list of the statically reachable code blocks in the body of the
  64. // function, in lexical order. The first block is the entry block. This will
  65. // be empty for declarations that don't have a visible definition.
  66. llvm::SmallVector<InstBlockId> body_block_ids = {};
  67. };
  68. } // namespace Carbon::SemIR
  69. #endif // CARBON_TOOLCHAIN_SEM_IR_FUNCTION_H_