function.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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_CHECK_FUNCTION_H_
  5. #define CARBON_TOOLCHAIN_CHECK_FUNCTION_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/decl_name_stack.h"
  8. #include "toolchain/check/subst.h"
  9. #include "toolchain/sem_ir/function.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. namespace Carbon::Check {
  12. // Returns the ID of the self parameter pattern, or None.
  13. // TODO: Do this during initial traversal of implicit params.
  14. auto FindSelfPattern(Context& context,
  15. SemIR::InstBlockId implicit_param_patterns_id)
  16. -> SemIR::InstId;
  17. // Creates suitable return patterns for the given return form, and adds them to
  18. // the current pattern block.
  19. auto AddReturnPatterns(Context& context, SemIR::LocId loc_id,
  20. Context::FormExpr form_expr) -> SemIR::InstBlockId;
  21. // Returns whether `function` is a valid declaration of `builtin_kind`.
  22. auto IsValidBuiltinDeclaration(Context& context,
  23. const SemIR::Function& function,
  24. SemIR::BuiltinFunctionKind builtin_kind) -> bool;
  25. // The signature to declare for a builtin function.
  26. struct BuiltinFunctionSignature {
  27. // The type of the implicit `[self: Self]` parameter, or `None` if there is
  28. // none.
  29. SemIR::TypeId self_type_id = SemIR::TypeId::None;
  30. // Whether `self` is a ref parameter.
  31. bool self_is_ref = true;
  32. // The types of the explicit parameters.
  33. llvm::ArrayRef<SemIR::TypeId> param_type_ids = {};
  34. // The return type, or `None` if the function doesn't declare a return type.
  35. SemIR::TypeId return_type_id = SemIR::TypeId::None;
  36. };
  37. // Creates and returns a new builtin function declaration.
  38. //
  39. // TODO: Instead of synthesizing builtin function declarations, we should
  40. // ideally declare the builtin functions in Carbon code instead.
  41. auto MakeBuiltinFunction(Context& context, SemIR::LocId loc_id,
  42. SemIR::BuiltinFunctionKind builtin_kind,
  43. SemIR::NameScopeId name_scope_id,
  44. SemIR::NameId name_id,
  45. BuiltinFunctionSignature signature) -> SemIR::InstId;
  46. // Checks that `new_function` has the same return type as `prev_function`, or if
  47. // `prev_function_id` is specified, a specific version of `prev_function`.
  48. // Prints a suitable diagnostic and returns false if not. Never checks for a
  49. // syntactic match.
  50. auto CheckFunctionReturnTypeMatches(Context& context,
  51. const SemIR::Function& new_function,
  52. const SemIR::Function& prev_function,
  53. SemIR::SpecificId prev_specific_id,
  54. bool diagnose = true) -> bool;
  55. // Checks that `new_function` has the same parameter types and return type as
  56. // `prev_function`, or if `prev_function_id` is specified, a specific version of
  57. // `prev_function`. Prints a suitable diagnostic and returns false if not.
  58. //
  59. // `check_syntax` is false if the redeclaration can be called via a thunk with
  60. // implicit conversions from the original declaration.
  61. // `check_self` is false if the self declaration does not have to match (for
  62. // instance in impls of virtual functions).
  63. auto CheckFunctionTypeMatches(Context& context,
  64. const SemIR::Function& new_function,
  65. const SemIR::Function& prev_function,
  66. SemIR::SpecificId prev_specific_id,
  67. bool check_syntax, bool check_self,
  68. bool diagnose = true) -> bool;
  69. inline auto CheckFunctionTypeMatches(Context& context,
  70. const SemIR::Function& new_function,
  71. const SemIR::Function& prev_function)
  72. -> bool {
  73. return CheckFunctionTypeMatches(context, new_function, prev_function,
  74. SemIR::SpecificId::None,
  75. /*check_syntax=*/true, /*check_self=*/true);
  76. }
  77. // Checks that the scrutinee type of `return_pattern_id` in `specific_id` is
  78. // concrete. If so, it returns that type; if not, it issues an error and returns
  79. // SemIR::ErrorInst::TypeId. `return_pattern_id` must be part of a function's
  80. // return form, or the error message will be nonsensical.
  81. auto CheckFunctionReturnPatternType(Context& context, SemIR::LocId loc_id,
  82. SemIR::InstId return_pattern_id,
  83. SemIR::SpecificId specific_id)
  84. -> SemIR::TypeId;
  85. // Checks that a function declaration's signature is suitable to support a
  86. // function definition. This requires the parameter types to be complete and the
  87. // return type to be concrete.
  88. auto CheckFunctionDefinitionSignature(Context& context,
  89. SemIR::FunctionId function_id) -> void;
  90. // Prepares for a function signature. Handles necessary stack setup. This is
  91. // used for generated functions/thunks, not user-declared functions.
  92. auto StartFunctionSignature(Context& context) -> void;
  93. // Results for `FinishFunctionSignature`.
  94. struct FinishFunctionSignatureResult {
  95. SemIR::InstBlockId pattern_block_id;
  96. SemIR::InstBlockId decl_block_id;
  97. };
  98. // Finishes signatures started by `StartFunctionSignature`.
  99. auto FinishFunctionSignature(Context& context, bool check_unused = true)
  100. -> FinishFunctionSignatureResult;
  101. // Creates a function object for the given function declaration. The caller must
  102. // add the returned `decl_id` to a block (typically the current block or
  103. // imports).
  104. auto MakeFunctionDecl(Context& context, SemIR::LocId loc_id,
  105. SemIR::InstBlockId decl_block_id, bool build_generic,
  106. bool is_definition, SemIR::Function function)
  107. -> std::pair<SemIR::InstId, SemIR::FunctionId>;
  108. // Starts a function definition. Handles necessary stack setup, creating the
  109. // function scope and entry block, and definition validation. This is used for
  110. // both generated functions/thunks and user-declared functions.
  111. auto StartFunctionDefinition(Context& context, SemIR::InstId decl_id,
  112. SemIR::FunctionId function_id) -> void;
  113. // Finishes definitions started by `StartFunctionDefinition`.
  114. auto FinishFunctionDefinition(Context& context, SemIR::FunctionId function_id)
  115. -> void;
  116. } // namespace Carbon::Check
  117. #endif // CARBON_TOOLCHAIN_CHECK_FUNCTION_H_