type_completion.h 5.2 KB

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