type_completion.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, bool diagnose = false) -> 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. //
  34. // Note that in general, the code that creates an inst is responsible for
  35. // enforcing any type completeness requirements associated with that inst; it
  36. // should not rely on its downstream consumers to do so.
  37. auto RequireCompleteType(Context& context, SemIR::TypeId type_id,
  38. SemIR::LocId loc_id,
  39. DiagnosticContextFn diagnostic_context) -> bool;
  40. // Returns whether the type is complete and concrete.
  41. //
  42. // Avoid calling this where possible, as it can lead to coherence issues.
  43. // Usually you want a diagnostic when it's not, so call RequireConcreteType.
  44. //
  45. // The `type_id` must be complete, so `TryToCompleteType` must have already been
  46. // called, or this function may crash.
  47. auto TryIsConcreteType(Context& context, SemIR::TypeId type_id,
  48. SemIR::LocId loc_id) -> bool;
  49. // Returns true for types that are complete and that have an object
  50. // representation that may be used as a return type or variable type.
  51. //
  52. // The `complete_type_diagnostic_context` is used to contextualize diagnostics
  53. // in checking that the type is complete. The `concrete_type_diagnostic_context`
  54. // is used to contextualize diagnostics in checking that the type is concrete.
  55. //
  56. // Note: class types are abstract if marked using the `abstract` keyword; tuple
  57. // and struct types are abstract if any element is abstract.
  58. auto RequireConcreteType(Context& context, SemIR::TypeId type_id,
  59. SemIR::LocId loc_id,
  60. DiagnosticContextFn complete_type_diagnostic_context,
  61. DiagnosticContextFn concrete_type_diagnostic_context)
  62. -> bool;
  63. // Performs `RequireIdentifiedFacetType`, but it is not an error if the facet
  64. // type is not identified.
  65. //
  66. // If `allow_partially_identified` is true, the facet type may be partially
  67. // identified, which can contain interfaces from a named constraint that is in
  68. // the middle of being defined.
  69. auto TryToIdentifyFacetType(Context& context, SemIR::LocId loc_id,
  70. SemIR::ConstantId self_const_id,
  71. const SemIR::FacetType& facet_type,
  72. bool allow_partially_identified)
  73. -> SemIR::IdentifiedFacetTypeId;
  74. // Requires the named constraints in the facet type to be complete, so that the
  75. // set of interfaces the facet type requires is known. The `self_const_id` is a
  76. // type or facet type expression that is the self that the FacetType is
  77. // constraining. Produces a set of interfaces that must be implemented for a set
  78. // of types, most of them for the `self_const_id`. Diagnoses an error and
  79. // returns None if any error is found.
  80. //
  81. // The `self_const_id` is converted to the canonical facet value (if it's a
  82. // facet-value-as-type, the as-type is stripped off), and this is visible in the
  83. // resulting IdentifiedFacetType. Comparing the `self_const_id` against the
  84. // output self values requires the caller to also canonicalize the
  85. // `self_const_id`.
  86. //
  87. // TODO: Remove `diagnose` and split into `TryIdentifyFacetType`.
  88. auto RequireIdentifiedFacetType(Context& context, SemIR::LocId loc_id,
  89. SemIR::ConstantId self_const_id,
  90. const SemIR::FacetType& facet_type,
  91. DiagnosticContextFn diagnostic_context,
  92. bool diagnose = true)
  93. -> SemIR::IdentifiedFacetTypeId;
  94. // Emits an error diagnostic explaining that a class is incomplete.
  95. //
  96. // The caller must ensure a Context message will be provided to point to the
  97. // failing operation that requires a complete class.
  98. auto DiagnoseIncompleteClass(Context& context, SemIR::ClassId class_id) -> void;
  99. // Emits an error diagnostic explaining that an interface is not defined.
  100. //
  101. // The caller must ensure a Context message will be provided to point to the
  102. // failing operation that requires a complete interface.
  103. auto DiagnoseIncompleteInterface(Context& context,
  104. SemIR::InterfaceId interface_id) -> void;
  105. // Adds a note to a diagnostic explaining that a class is abstract.
  106. //
  107. // The caller must ensure a Context message will be provided to point to the
  108. // failing operation that requires a concrete class.
  109. auto DiagnoseAbstractClass(Context& context, SemIR::ClassId class_id,
  110. bool direct_use) -> void;
  111. } // namespace Carbon::Check
  112. #endif // CARBON_TOOLCHAIN_CHECK_TYPE_COMPLETION_H_