function.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // State saved for a function definition that has been suspended after
  13. // processing its declaration and before processing its body. This is used for
  14. // inline method handling.
  15. //
  16. // This type is large, so moves of this type should be avoided.
  17. struct SuspendedFunction : public MoveOnly<SuspendedFunction> {
  18. // The function that was declared.
  19. SemIR::FunctionId function_id;
  20. // The instruction ID of the FunctionDecl instruction.
  21. SemIR::InstId decl_id;
  22. // The declaration name information of the function. This includes the scope
  23. // information, such as parameter names.
  24. DeclNameStack::SuspendedName saved_name_state;
  25. };
  26. // Returns the ID of the self parameter pattern, or None.
  27. // TODO: Do this during initial traversal of implicit params.
  28. auto FindSelfPattern(Context& context,
  29. SemIR::InstBlockId implicit_param_patterns_id)
  30. -> SemIR::InstId;
  31. // Checks that `new_function` has the same return type as `prev_function`, or if
  32. // `prev_function_id` is specified, a specific version of `prev_function`.
  33. // Prints a suitable diagnostic and returns false if not. Never checks for a
  34. // syntactic match.
  35. auto CheckFunctionReturnTypeMatches(Context& context,
  36. const SemIR::Function& new_function,
  37. const SemIR::Function& prev_function,
  38. SemIR::SpecificId prev_specific_id,
  39. bool diagnose = true) -> bool;
  40. // Checks that `new_function` has the same parameter types and return type as
  41. // `prev_function`, or if `prev_function_id` is specified, a specific version of
  42. // `prev_function`. Prints a suitable diagnostic and returns false if not.
  43. //
  44. // `check_syntax` is false if the redeclaration can be called via a thunk with
  45. // implicit conversions from the original declaration.
  46. // `check_self` is false if the self declaration does not have to match (for
  47. // instance in impls of virtual functions).
  48. auto CheckFunctionTypeMatches(Context& context,
  49. const SemIR::Function& new_function,
  50. const SemIR::Function& prev_function,
  51. SemIR::SpecificId prev_specific_id,
  52. bool check_syntax, bool check_self,
  53. bool diagnose = true) -> bool;
  54. inline auto CheckFunctionTypeMatches(Context& context,
  55. const SemIR::Function& new_function,
  56. const SemIR::Function& prev_function)
  57. -> bool {
  58. return CheckFunctionTypeMatches(context, new_function, prev_function,
  59. SemIR::SpecificId::None,
  60. /*check_syntax=*/true, /*check_self=*/true);
  61. }
  62. // Checks that the return type of the specified function is complete, issuing an
  63. // error if not. This computes the return slot usage for the function if
  64. // necessary, and returns information about how the function returns its return
  65. // value.
  66. auto CheckFunctionReturnType(Context& context, SemIR::LocId loc_id,
  67. const SemIR::Function& function,
  68. SemIR::SpecificId specific_id)
  69. -> SemIR::ReturnTypeInfo;
  70. // Checks that a function declaration's signature is suitable to support a
  71. // function definition. This requires the parameter types to be complete and the
  72. // return type to be concrete.
  73. auto CheckFunctionDefinitionSignature(Context& context,
  74. SemIR::FunctionId function_id) -> void;
  75. } // namespace Carbon::Check
  76. #endif // CARBON_TOOLCHAIN_CHECK_FUNCTION_H_