function.h 3.9 KB

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