type.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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_H_
  5. #define CARBON_TOOLCHAIN_CHECK_TYPE_H_
  6. #include "llvm/ADT/ArrayRef.h"
  7. #include "toolchain/check/context.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::Check {
  10. // Enforces that an integer type has a valid bit width.
  11. auto ValidateIntType(Context& context, SemIR::LocId loc_id,
  12. SemIR::IntType result) -> bool;
  13. // Enforces that a float type has a valid bit width. If the `float_kind` field
  14. // is `None`, sets it to a suitable kind for the bit width.
  15. auto ValidateFloatTypeAndSetKind(Context& context, SemIR::LocId loc_id,
  16. SemIR::FloatType& result) -> bool;
  17. // Gets the type to use for an unbound associated entity declared in this
  18. // interface. For example, this is the type of `I.T` after
  19. // `interface I { let T:! type; }`. The name of the interface is used for
  20. // diagnostics.
  21. // TODO: Should we use a different type for each such entity, or the same type
  22. // for all associated entities?
  23. auto GetAssociatedEntityType(Context& context, SemIR::InterfaceId interface_id,
  24. SemIR::SpecificId interface_specific_id)
  25. -> SemIR::TypeId;
  26. // Gets a singleton type. The returned type will be complete. Requires that
  27. // `singleton_id` is already validated to be a singleton.
  28. auto GetSingletonType(Context& context, SemIR::TypeInstId singleton_id)
  29. -> SemIR::TypeId;
  30. // Gets a const-qualified version of a type.
  31. auto GetConstType(Context& context, SemIR::TypeInstId inner_type_id)
  32. -> SemIR::TypeId;
  33. // Gets a qualified version of a type.
  34. auto GetQualifiedType(Context& context, SemIR::TypeId type_id,
  35. SemIR::TypeQualifiers quals) -> SemIR::TypeId;
  36. // Gets a class type.
  37. auto GetClassType(Context& context, SemIR::ClassId class_id,
  38. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  39. // Gets a C++ overload set type. The returned type will be complete.
  40. auto GetCppOverloadSetType(Context& context,
  41. SemIR::CppOverloadSetId overload_set_id,
  42. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  43. // Gets a function type. The returned type will be complete.
  44. auto GetFunctionType(Context& context, SemIR::FunctionId fn_id,
  45. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  46. auto GetVtableType(Context& context, SemIR::VtableId vtable_id)
  47. -> SemIR::TypeId;
  48. // Gets the type of an associated function with the `Self` parameter bound to
  49. // a particular value. The returned type will be complete.
  50. auto GetFunctionTypeWithSelfType(Context& context,
  51. SemIR::TypeInstId interface_function_type_id,
  52. SemIR::InstId self_id) -> SemIR::TypeId;
  53. // Gets a generic class type, which is the type of a name of a generic class,
  54. // such as the type of `Vector` given `class Vector(T:! type)`. The returned
  55. // type will be complete.
  56. auto GetGenericClassType(Context& context, SemIR::ClassId class_id,
  57. SemIR::SpecificId enclosing_specific_id)
  58. -> SemIR::TypeId;
  59. // Gets a generic interface type, which is the type of a name of a generic
  60. // interface, such as the type of `AddWith` given
  61. // `interface AddWith(T:! type)`. The returned type will be complete.
  62. auto GetGenericInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  63. SemIR::SpecificId enclosing_specific_id)
  64. -> SemIR::TypeId;
  65. // Gets a generic named constraint type, which is the type of a name of a
  66. // generic named constraint, such as the type of `AddWith` given `constraint
  67. // AddWith(T:! type)`. The returned type will be complete.
  68. auto GetGenericNamedConstraintType(Context& context,
  69. SemIR::NamedConstraintId named_constraint_id,
  70. SemIR::SpecificId enclosing_specific_id)
  71. -> SemIR::TypeId;
  72. // Gets the facet type corresponding to a particular interface.
  73. auto GetInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  74. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  75. // Gets the facet type corresponding to a particular named constraint.
  76. auto GetNamedConstraintType(Context& context,
  77. SemIR::NamedConstraintId named_constraint_id,
  78. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  79. // Gets the facet type for the given `info`.
  80. auto GetFacetType(Context& context, const SemIR::FacetTypeInfo& info)
  81. -> SemIR::TypeId;
  82. // Returns a pointer type whose pointee type is `pointee_type_id`. The returned
  83. // type will be complete.
  84. auto GetPointerType(Context& context, SemIR::TypeInstId pointee_type_id)
  85. -> SemIR::TypeId;
  86. // Returns a struct type with the given fields.
  87. auto GetStructType(Context& context, SemIR::StructTypeFieldsId fields_id)
  88. -> SemIR::TypeId;
  89. // Returns a tuple type with the given element types.
  90. auto GetTupleType(Context& context, llvm::ArrayRef<SemIR::InstId> type_inst_ids)
  91. -> SemIR::TypeId;
  92. // Returns a pattern type with the given scrutinee type.
  93. auto GetPatternType(Context& context, SemIR::TypeId scrutinee_type_id)
  94. -> SemIR::TypeId;
  95. // Returns an unbound element type.
  96. auto GetUnboundElementType(Context& context, SemIR::TypeInstId class_type_id,
  97. SemIR::TypeInstId element_type_id) -> SemIR::TypeId;
  98. // Given a facet value or a type value, get the canonical facet value if
  99. // possible, or return the canonical value of the input type expression if it
  100. // has no canonical facet value.
  101. //
  102. // A facet value can be appear in two ways: as a facet value of type
  103. // `FacetType`, or through an `as type` conversion which has type `TypeType` but
  104. // still refers to the original facet value. While both have canonical values of
  105. // their own, in cases that want to work with the facet value when possible,
  106. // this collapses the two cases back together by undoing the `as type`
  107. // conversion.
  108. //
  109. // This extra canonicalization step is important for constant comparison of
  110. // facet values, when the `as type` conversion is not required to compare as a
  111. // different value.
  112. //
  113. // For type expressions other than `<facet value> as type`, the canonical type
  114. // value is returned.
  115. auto GetCanonicalFacetOrTypeValue(Context& context, SemIR::InstId inst_id)
  116. -> SemIR::InstId;
  117. auto GetCanonicalFacetOrTypeValue(Context& context, SemIR::ConstantId const_id)
  118. -> SemIR::ConstantId;
  119. // If `inst_id` is a type value which wraps a facet value, return that canonical
  120. // facet value. Otherwise, return None.
  121. //
  122. // In particular, this returns None for non-canonical instructions if no
  123. // transformation was needed to return a facet value, to preserve source
  124. // locations in the caller.
  125. auto TryGetCanonicalFacetValue(Context& context, SemIR::InstId inst_id)
  126. -> SemIR::InstId;
  127. } // namespace Carbon::Check
  128. #endif // CARBON_TOOLCHAIN_CHECK_TYPE_H_