type.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 C++ template name type. The returned type will be complete.
  44. auto GetCppTemplateNameType(Context& context, SemIR::EntityNameId name_id,
  45. SemIR::ClangDeclId decl_id) -> SemIR::TypeId;
  46. // Gets a function type. The returned type will be complete.
  47. auto GetFunctionType(Context& context, SemIR::FunctionId fn_id,
  48. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  49. auto GetVtableType(Context& context, SemIR::VtableId vtable_id)
  50. -> SemIR::TypeId;
  51. // Gets the type of an associated function with the `Self` parameter bound to
  52. // a particular value. The returned type will be complete.
  53. auto GetFunctionTypeWithSelfType(Context& context,
  54. SemIR::TypeInstId interface_function_type_id,
  55. SemIR::InstId self_id) -> SemIR::TypeId;
  56. // Gets a generic class type, which is the type of a name of a generic class,
  57. // such as the type of `Vector` given `class Vector(T:! type)`. The returned
  58. // type will be complete.
  59. auto GetGenericClassType(Context& context, SemIR::ClassId class_id,
  60. SemIR::SpecificId enclosing_specific_id)
  61. -> SemIR::TypeId;
  62. // Gets a generic interface type, which is the type of a name of a generic
  63. // interface, such as the type of `AddWith` given
  64. // `interface AddWith(T:! type)`. The returned type will be complete.
  65. auto GetGenericInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  66. SemIR::SpecificId enclosing_specific_id)
  67. -> SemIR::TypeId;
  68. // Gets a generic named constraint type, which is the type of a name of a
  69. // generic named constraint, such as the type of `AddWith` given `constraint
  70. // AddWith(T:! type)`. The returned type will be complete.
  71. auto GetGenericNamedConstraintType(Context& context,
  72. SemIR::NamedConstraintId named_constraint_id,
  73. SemIR::SpecificId enclosing_specific_id)
  74. -> SemIR::TypeId;
  75. // Gets the facet type corresponding to a particular interface.
  76. auto GetInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  77. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  78. // Gets the facet type corresponding to a particular named constraint.
  79. auto GetNamedConstraintType(Context& context,
  80. SemIR::NamedConstraintId named_constraint_id,
  81. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  82. // Gets the facet type for the given `info`.
  83. auto GetFacetType(Context& context, const SemIR::FacetTypeInfo& info)
  84. -> SemIR::TypeId;
  85. // Gets the type contained within the given facet value.
  86. auto GetFacetAccessType(Context& context, SemIR::InstId facet_value_inst_id)
  87. -> SemIR::TypeId;
  88. // Returns a pointer type whose pointee type is `pointee_type_id`. The returned
  89. // type will be complete.
  90. auto GetPointerType(Context& context, SemIR::TypeInstId pointee_type_id)
  91. -> SemIR::TypeId;
  92. // Returns a struct type with the given fields.
  93. auto GetStructType(Context& context, SemIR::StructTypeFieldsId fields_id)
  94. -> SemIR::TypeId;
  95. // Returns a tuple type with the given element types.
  96. auto GetTupleType(Context& context, llvm::ArrayRef<SemIR::InstId> type_inst_ids)
  97. -> SemIR::TypeId;
  98. // Returns a pattern type with the given scrutinee type.
  99. auto GetPatternType(Context& context, SemIR::TypeId scrutinee_type_id)
  100. -> SemIR::TypeId;
  101. // Returns the type component of the given form value.
  102. auto GetTypeComponent(Context& context, SemIR::InstId form_inst_id)
  103. -> SemIR::TypeId;
  104. // Returns an unbound element type.
  105. auto GetUnboundElementType(Context& context, SemIR::TypeInstId class_type_id,
  106. SemIR::TypeInstId element_type_id) -> SemIR::TypeId;
  107. // Given a facet value or a type value, get the canonical facet value if
  108. // possible, or return the canonical value of the input type expression if it
  109. // has no canonical facet value.
  110. //
  111. // A facet value can be appear in two ways: as a facet value of type
  112. // `FacetType`, or through an `as type` conversion which has type `TypeType` but
  113. // still refers to the original facet value. While both have canonical values of
  114. // their own, in cases that want to work with the facet value when possible,
  115. // this collapses the two cases back together by undoing the `as type`
  116. // conversion.
  117. //
  118. // This extra canonicalization step is important for constant comparison of
  119. // facet values, when the `as type` conversion is not required to compare as a
  120. // different value.
  121. //
  122. // For type expressions other than `<facet value> as type`, the canonical type
  123. // value is returned.
  124. auto GetCanonicalFacetOrTypeValue(Context& context, SemIR::InstId inst_id)
  125. -> SemIR::InstId;
  126. auto GetCanonicalFacetOrTypeValue(Context& context, SemIR::ConstantId const_id)
  127. -> SemIR::ConstantId;
  128. // If `inst_id` is a type value which wraps a facet value, return that canonical
  129. // facet value. Otherwise, return None.
  130. //
  131. // In particular, this returns None for non-canonical instructions if no
  132. // transformation was needed to return a facet value, to preserve source
  133. // locations in the caller.
  134. auto TryGetCanonicalFacetValue(Context& context, SemIR::InstId inst_id)
  135. -> SemIR::InstId;
  136. } // namespace Carbon::Check
  137. #endif // CARBON_TOOLCHAIN_CHECK_TYPE_H_