type_completion.h 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // Returns true for types that have an object representation that may be used as
  37. // a return type or variable type. Returns true for all facet types, since their
  38. // representation is always the same and is never considered abstract.
  39. // Otherwise, this is like `RequireCompleteType`, but also require the type to
  40. // not be abstract. If it is, `abstract_diagnoser` is used to diagnose the
  41. // problem, and this function returns false.
  42. //
  43. // Note: class types are abstract if marked using the `abstract` keyword; tuple
  44. // and struct types are abstract if any element is abstract.
  45. auto RequireConcreteType(Context& context, SemIR::TypeId type_id,
  46. SemIR::LocId loc_id, MakeDiagnosticBuilderFn diagnoser,
  47. MakeDiagnosticBuilderFn abstract_diagnoser) -> bool;
  48. // Like `RequireCompleteType`, but only for facet types. If it uses some
  49. // incomplete interface, diagnoses the problem and returns `None`.
  50. auto RequireCompleteFacetType(Context& context, SemIR::TypeId type_id,
  51. SemIR::LocId loc_id,
  52. const SemIR::FacetType& facet_type,
  53. MakeDiagnosticBuilderFn diagnoser)
  54. -> SemIR::CompleteFacetTypeId;
  55. // Returns the type `type_id` if it is a complete type, or produces an
  56. // incomplete type error and returns an error type. This is a convenience
  57. // wrapper around `RequireCompleteType`.
  58. auto AsCompleteType(Context& context, SemIR::TypeId type_id,
  59. SemIR::LocId loc_id, MakeDiagnosticBuilderFn diagnoser)
  60. -> SemIR::TypeId;
  61. // Returns the type `type_id` if it is a concrete type, or produces an
  62. // incomplete or abstract type error and returns an error type. This is a
  63. // convenience wrapper around `RequireConcreteType`.
  64. auto AsConcreteType(Context& context, SemIR::TypeId type_id,
  65. SemIR::LocId loc_id, MakeDiagnosticBuilderFn diagnoser,
  66. MakeDiagnosticBuilderFn abstract_diagnoser)
  67. -> SemIR::TypeId;
  68. // Adds a note to a diagnostic explaining that a class is incomplete.
  69. auto NoteIncompleteClass(Context& context, SemIR::ClassId class_id,
  70. DiagnosticBuilder& builder) -> void;
  71. // Adds a note to a diagnostic explaining that an interface is not defined.
  72. auto NoteUndefinedInterface(Context& context, SemIR::InterfaceId interface_id,
  73. DiagnosticBuilder& builder) -> void;
  74. } // namespace Carbon::Check
  75. #endif // CARBON_TOOLCHAIN_CHECK_TYPE_COMPLETION_H_