type_completion.h 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. MakeDiagnosticBuilderFn 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, MakeDiagnosticBuilderFn diagnoser)
  35. -> 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, MakeDiagnosticBuilderFn diagnoser,
  41. MakeDiagnosticBuilderFn abstract_diagnoser) -> bool;
  42. // Like `RequireCompleteType`, but only for facet types. If it uses some
  43. // incomplete interface, diagnoses the problem and returns `None`.
  44. auto RequireCompleteFacetType(Context& context, SemIR::TypeId type_id,
  45. SemIR::LocId loc_id,
  46. const SemIR::FacetType& facet_type,
  47. MakeDiagnosticBuilderFn diagnoser)
  48. -> SemIR::CompleteFacetTypeId;
  49. // Returns the type `type_id` if it is a complete type, or produces an
  50. // incomplete type error and returns an error type. This is a convenience
  51. // wrapper around `RequireCompleteType`.
  52. auto AsCompleteType(Context& context, SemIR::TypeId type_id,
  53. SemIR::LocId loc_id, MakeDiagnosticBuilderFn diagnoser)
  54. -> SemIR::TypeId;
  55. // Returns the type `type_id` if it is a concrete type, or produces an
  56. // incomplete or abstract type error and returns an error type. This is a
  57. // convenience wrapper around `RequireConcreteType`.
  58. auto AsConcreteType(Context& context, SemIR::TypeId type_id,
  59. SemIR::LocId loc_id, MakeDiagnosticBuilderFn diagnoser,
  60. MakeDiagnosticBuilderFn abstract_diagnoser)
  61. -> SemIR::TypeId;
  62. // Adds a note to a diagnostic explaining that a class is incomplete.
  63. auto NoteIncompleteClass(Context& context, SemIR::ClassId class_id,
  64. DiagnosticBuilder& builder) -> void;
  65. // Adds a note to a diagnostic explaining that an interface is not defined.
  66. auto NoteUndefinedInterface(Context& context, SemIR::InterfaceId interface_id,
  67. DiagnosticBuilder& builder) -> void;
  68. } // namespace Carbon::Check
  69. #endif // CARBON_TOOLCHAIN_CHECK_TYPE_COMPLETION_H_