function.h 3.3 KB

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