function.h 6.5 KB

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