function.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. // Checks that `new_function` has the same parameter types and return type as
  25. // `prev_function`, or if `prev_function_id` is specified, a specific version of
  26. // `prev_function`. Prints a suitable diagnostic and returns false if not.
  27. //
  28. // `check_syntax` is false if the redeclaration can be called via a thunk with
  29. // implicit conversions from the original declaration.
  30. // `check_self` is false if the self declaration does not have to match (for
  31. // instance in impls of virtual functions).
  32. auto CheckFunctionTypeMatches(Context& context,
  33. const SemIR::Function& new_function,
  34. const SemIR::Function& prev_function,
  35. SemIR::SpecificId prev_specific_id,
  36. bool check_syntax, bool check_self) -> bool;
  37. inline auto CheckFunctionTypeMatches(Context& context,
  38. const SemIR::Function& new_function,
  39. const SemIR::Function& prev_function)
  40. -> bool {
  41. return CheckFunctionTypeMatches(context, new_function, prev_function,
  42. SemIR::SpecificId::None,
  43. /*check_syntax=*/true, /*check_self=*/true);
  44. }
  45. // Checks that the return type of the specified function is complete, issuing an
  46. // error if not. This computes the return slot usage for the function if
  47. // necessary, and returns information about how the function returns its return
  48. // value.
  49. auto CheckFunctionReturnType(Context& context, SemIR::LocId loc_id,
  50. const SemIR::Function& function,
  51. SemIR::SpecificId specific_id)
  52. -> SemIR::ReturnTypeInfo;
  53. } // namespace Carbon::Check
  54. #endif // CARBON_TOOLCHAIN_CHECK_FUNCTION_H_