type_completion.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_TYPE_COMPLETION_H_
  5. #define CARBON_TOOLCHAIN_CHECK_TYPE_COMPLETION_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/diagnostic_helpers.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::Check {
  10. // Attempts to complete the type `type_id`. Returns `true` if the type is
  11. // complete, or `false` if it could not be completed. A complete type has
  12. // known object and value representations. Returns `true` if the type is
  13. // symbolic.
  14. //
  15. // Avoid calling this where possible, as it can lead to coherence issues.
  16. // However, it's important that we use it during monomorphization, where we
  17. // don't want to trigger a request for more monomorphization.
  18. // TODO: Remove the other call to this function.
  19. auto TryToCompleteType(Context& context, SemIR::TypeId type_id, SemIRLoc loc,
  20. Context::BuildDiagnosticFn diagnoser = nullptr) -> bool;
  21. // Completes the type `type_id`. CHECK-fails if it can't be completed.
  22. auto CompleteTypeOrCheckFail(Context& context, SemIR::TypeId type_id) -> void;
  23. // Like `TryToCompleteType`, but for cases where it is an error for the type
  24. // to be incomplete.
  25. //
  26. // If the type is not complete, `diagnoser` is invoked to diagnose the issue,
  27. // if a `diagnoser` is provided. The builder it returns will be annotated to
  28. // describe the reason why the type is not complete.
  29. //
  30. // `diagnoser` should build an error diagnostic. If `type_id` is dependent,
  31. // the completeness of the type will be enforced during monomorphization, and
  32. // `loc_id` is used as the location for a diagnostic produced at that time.
  33. auto RequireCompleteType(Context& context, SemIR::TypeId type_id,
  34. SemIR::LocId loc_id,
  35. Context::BuildDiagnosticFn diagnoser) -> bool;
  36. // Like `RequireCompleteType`, but also require the type to not be an abstract
  37. // class type. If it is, `abstract_diagnoser` is used to diagnose the problem,
  38. // and this function returns false.
  39. auto RequireConcreteType(Context& context, SemIR::TypeId type_id,
  40. SemIR::LocId loc_id,
  41. Context::BuildDiagnosticFn diagnoser,
  42. Context::BuildDiagnosticFn abstract_diagnoser) -> bool;
  43. // Like `RequireCompleteType`, but also require the type to be defined. A
  44. // defined type has known members. If the type is not defined, `diagnoser` is
  45. // used to diagnose the problem, and this function returns false.
  46. //
  47. // This is the same as `RequireCompleteType` except for facet types, which are
  48. // complete before they are fully defined.
  49. auto RequireDefinedType(Context& context, SemIR::TypeId type_id,
  50. SemIR::LocId loc_id,
  51. Context::BuildDiagnosticFn diagnoser) -> bool;
  52. // Returns the type `type_id` if it is a complete type, or produces an
  53. // incomplete type error and returns an error type. This is a convenience
  54. // wrapper around `RequireCompleteType`.
  55. auto AsCompleteType(Context& context, SemIR::TypeId type_id,
  56. SemIR::LocId loc_id, Context::BuildDiagnosticFn diagnoser)
  57. -> SemIR::TypeId;
  58. // Returns the type `type_id` if it is a concrete type, or produces an
  59. // incomplete or abstract type error and returns an error type. This is a
  60. // convenience wrapper around `RequireConcreteType`.
  61. auto AsConcreteType(Context& context, SemIR::TypeId type_id,
  62. SemIR::LocId loc_id, Context::BuildDiagnosticFn diagnoser,
  63. Context::BuildDiagnosticFn abstract_diagnoser)
  64. -> SemIR::TypeId;
  65. } // namespace Carbon::Check
  66. #endif // CARBON_TOOLCHAIN_CHECK_TYPE_COMPLETION_H_