type_completion.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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,
  20. SemIR::LocId loc_id,
  21. MakeDiagnosticBuilderFn diagnoser = nullptr) -> bool;
  22. // Completes the type `type_id`. CHECK-fails if it can't be completed.
  23. auto CompleteTypeOrCheckFail(Context& context, SemIR::TypeId type_id) -> void;
  24. // Like `TryToCompleteType`, but for cases where it is an error for the type
  25. // to be incomplete.
  26. //
  27. // If the type is not complete, `diagnoser` is invoked to diagnose the issue,
  28. // if a `diagnoser` is provided. The builder it returns will be annotated to
  29. // describe the reason why the type is not complete.
  30. //
  31. // `diagnoser` should build an error diagnostic. If `type_id` is dependent,
  32. // the completeness of the type will be enforced during monomorphization, and
  33. // `loc_id` is used as the location for a diagnostic produced at that time.
  34. auto RequireCompleteType(Context& context, SemIR::TypeId type_id,
  35. SemIR::LocId loc_id, MakeDiagnosticBuilderFn diagnoser)
  36. -> bool;
  37. // Returns true for types that have an object representation that may be used as
  38. // a return type or variable type. Returns true for all facet types, since their
  39. // representation is always the same and is never considered abstract.
  40. // Otherwise, this is like `RequireCompleteType`, but also require the type to
  41. // not be abstract. If it is, `abstract_diagnoser` is used to diagnose the
  42. // problem, and this function returns false.
  43. //
  44. // Note: class types are abstract if marked using the `abstract` keyword; tuple
  45. // and struct types are abstract if any element is abstract.
  46. auto RequireConcreteType(Context& context, SemIR::TypeId type_id,
  47. SemIR::LocId loc_id, MakeDiagnosticBuilderFn diagnoser,
  48. MakeDiagnosticBuilderFn abstract_diagnoser) -> bool;
  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. // Requires the named constraints in the facet type to be complete, so that the
  63. // set of interfaces the facet type requires is known. Diagnoses an error and
  64. // returns None if any named constraint is not complete.
  65. auto RequireIdentifiedFacetType(Context& context,
  66. const SemIR::FacetType& facet_type,
  67. MakeDiagnosticBuilderFn diagnoser)
  68. -> SemIR::IdentifiedFacetTypeId;
  69. // Adds a note to a diagnostic explaining that a class is incomplete.
  70. auto NoteIncompleteClass(Context& context, SemIR::ClassId class_id,
  71. DiagnosticBuilder& builder) -> void;
  72. // Adds a note to a diagnostic explaining that an interface is not defined.
  73. auto NoteIncompleteInterface(Context& context, SemIR::InterfaceId interface_id,
  74. DiagnosticBuilder& builder) -> void;
  75. } // namespace Carbon::Check
  76. #endif // CARBON_TOOLCHAIN_CHECK_TYPE_COMPLETION_H_