type.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #include "toolchain/check/type.h"
  5. #include "toolchain/check/eval.h"
  6. #include "toolchain/check/facet_type.h"
  7. #include "toolchain/check/type_completion.h"
  8. namespace Carbon::Check {
  9. // Enforces that an integer type has a valid bit width.
  10. auto ValidateIntType(Context& context, SemIR::LocId loc_id,
  11. SemIR::IntType result) -> bool {
  12. auto bit_width =
  13. context.insts().TryGetAs<SemIR::IntValue>(result.bit_width_id);
  14. if (!bit_width) {
  15. // Symbolic bit width.
  16. return true;
  17. }
  18. const auto& bit_width_val = context.ints().Get(bit_width->int_id);
  19. if (bit_width_val.isZero() ||
  20. (context.types().IsSignedInt(bit_width->type_id) &&
  21. bit_width_val.isNegative())) {
  22. CARBON_DIAGNOSTIC(IntWidthNotPositive, Error,
  23. "integer type width of {0} is not positive", TypedInt);
  24. context.emitter().Emit(
  25. loc_id, IntWidthNotPositive,
  26. {.type = bit_width->type_id, .value = bit_width_val});
  27. return false;
  28. }
  29. if (bit_width_val.ugt(IntStore::MaxIntWidth)) {
  30. CARBON_DIAGNOSTIC(IntWidthTooLarge, Error,
  31. "integer type width of {0} is greater than the "
  32. "maximum supported width of {1}",
  33. TypedInt, int);
  34. context.emitter().Emit(loc_id, IntWidthTooLarge,
  35. {.type = bit_width->type_id, .value = bit_width_val},
  36. IntStore::MaxIntWidth);
  37. return false;
  38. }
  39. return true;
  40. }
  41. // Enforces that the bit width is 64 for a float.
  42. auto ValidateFloatBitWidth(Context& context, SemIR::LocId loc_id,
  43. SemIR::InstId inst_id) -> bool {
  44. auto inst = context.insts().GetAs<SemIR::IntValue>(inst_id);
  45. if (context.ints().Get(inst.int_id) == 64) {
  46. return true;
  47. }
  48. CARBON_DIAGNOSTIC(CompileTimeFloatBitWidth, Error, "bit width must be 64");
  49. context.emitter().Emit(loc_id, CompileTimeFloatBitWidth);
  50. return false;
  51. }
  52. // Enforces that a float type has a valid bit width.
  53. auto ValidateFloatType(Context& context, SemIR::LocId loc_id,
  54. SemIR::FloatType result) -> bool {
  55. auto bit_width =
  56. context.insts().TryGetAs<SemIR::IntValue>(result.bit_width_id);
  57. if (!bit_width) {
  58. // Symbolic bit width.
  59. return true;
  60. }
  61. return ValidateFloatBitWidth(context, loc_id, result.bit_width_id);
  62. }
  63. // Gets or forms a type_id for a type, given the instruction kind and arguments.
  64. template <typename InstT, typename... EachArgT>
  65. static auto GetTypeImpl(Context& context, EachArgT... each_arg)
  66. -> SemIR::TypeId {
  67. InstT inst = {SemIR::TypeType::TypeId, each_arg...};
  68. return context.types().GetTypeIdForTypeConstantId(TryEvalInst(context, inst));
  69. }
  70. // Gets or forms a type_id for a type, given the instruction kind and arguments,
  71. // and completes the type. This should only be used when type completion cannot
  72. // fail.
  73. template <typename InstT, typename... EachArgT>
  74. static auto GetCompleteTypeImpl(Context& context, EachArgT... each_arg)
  75. -> SemIR::TypeId {
  76. auto type_id = GetTypeImpl<InstT>(context, each_arg...);
  77. CompleteTypeOrCheckFail(context, type_id);
  78. return type_id;
  79. }
  80. auto GetStructType(Context& context, SemIR::StructTypeFieldsId fields_id)
  81. -> SemIR::TypeId {
  82. return GetTypeImpl<SemIR::StructType>(context, fields_id);
  83. }
  84. auto GetTupleType(Context& context, llvm::ArrayRef<SemIR::InstId> type_inst_ids)
  85. -> SemIR::TypeId {
  86. return GetTypeImpl<SemIR::TupleType>(
  87. context, context.inst_blocks().AddCanonical(type_inst_ids));
  88. }
  89. auto GetAssociatedEntityType(Context& context, SemIR::InterfaceId interface_id,
  90. SemIR::SpecificId interface_specific_id)
  91. -> SemIR::TypeId {
  92. return GetTypeImpl<SemIR::AssociatedEntityType>(context, interface_id,
  93. interface_specific_id);
  94. }
  95. auto GetConstType(Context& context, SemIR::TypeInstId inner_type_id)
  96. -> SemIR::TypeId {
  97. return GetTypeImpl<SemIR::ConstType>(context, inner_type_id);
  98. }
  99. auto GetSingletonType(Context& context, SemIR::TypeInstId singleton_id)
  100. -> SemIR::TypeId {
  101. CARBON_CHECK(SemIR::IsSingletonInstId(singleton_id));
  102. auto type_id = context.types().GetTypeIdForTypeInstId(singleton_id);
  103. // To keep client code simpler, complete builtin types before returning them.
  104. CompleteTypeOrCheckFail(context, type_id);
  105. return type_id;
  106. }
  107. auto GetClassType(Context& context, SemIR::ClassId class_id,
  108. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  109. return GetTypeImpl<SemIR::ClassType>(context, class_id, specific_id);
  110. }
  111. auto GetFunctionType(Context& context, SemIR::FunctionId fn_id,
  112. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  113. return GetCompleteTypeImpl<SemIR::FunctionType>(context, fn_id, specific_id);
  114. }
  115. auto GetFunctionTypeWithSelfType(Context& context,
  116. SemIR::TypeInstId interface_function_type_id,
  117. SemIR::InstId self_id) -> SemIR::TypeId {
  118. return GetCompleteTypeImpl<SemIR::FunctionTypeWithSelfType>(
  119. context, interface_function_type_id, self_id);
  120. }
  121. auto GetGenericClassType(Context& context, SemIR::ClassId class_id,
  122. SemIR::SpecificId enclosing_specific_id)
  123. -> SemIR::TypeId {
  124. return GetCompleteTypeImpl<SemIR::GenericClassType>(context, class_id,
  125. enclosing_specific_id);
  126. }
  127. auto GetGenericInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  128. SemIR::SpecificId enclosing_specific_id)
  129. -> SemIR::TypeId {
  130. return GetCompleteTypeImpl<SemIR::GenericInterfaceType>(
  131. context, interface_id, enclosing_specific_id);
  132. }
  133. auto GetInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  134. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  135. return GetTypeImpl<SemIR::FacetType>(
  136. context,
  137. FacetTypeFromInterface(context, interface_id, specific_id).facet_type_id);
  138. }
  139. auto GetPointerType(Context& context, SemIR::TypeInstId pointee_type_id)
  140. -> SemIR::TypeId {
  141. return GetTypeImpl<SemIR::PointerType>(context, pointee_type_id);
  142. }
  143. auto GetPatternType(Context& context, SemIR::TypeId scrutinee_type_id)
  144. -> SemIR::TypeId {
  145. CARBON_CHECK(!context.types().Is<SemIR::PatternType>(scrutinee_type_id),
  146. "Type is already a pattern type");
  147. if (scrutinee_type_id == SemIR::ErrorInst::TypeId) {
  148. return SemIR::ErrorInst::TypeId;
  149. }
  150. return GetTypeImpl<SemIR::PatternType>(
  151. context, context.types().GetInstId(scrutinee_type_id));
  152. }
  153. auto GetUnboundElementType(Context& context, SemIR::TypeInstId class_type_id,
  154. SemIR::TypeInstId element_type_id) -> SemIR::TypeId {
  155. return GetTypeImpl<SemIR::UnboundElementType>(context, class_type_id,
  156. element_type_id);
  157. }
  158. auto GetCanonicalizedFacetOrTypeValue(Context& context, SemIR::InstId inst_id)
  159. -> SemIR::InstId {
  160. // We can have FacetAccessType of a FacetValue, and a FacetValue of a
  161. // FacetAccessType, but they don't nest indefinitely.
  162. if (auto access = context.insts().TryGetAs<SemIR::FacetAccessType>(inst_id)) {
  163. inst_id = access->facet_value_inst_id;
  164. }
  165. if (auto value = context.insts().TryGetAs<SemIR::FacetValue>(inst_id)) {
  166. inst_id = value->type_inst_id;
  167. if (auto access =
  168. context.insts().TryGetAs<SemIR::FacetAccessType>(inst_id)) {
  169. inst_id = access->facet_value_inst_id;
  170. }
  171. }
  172. CARBON_CHECK(!context.insts().Is<SemIR::FacetAccessType>(inst_id));
  173. CARBON_CHECK(!context.insts().Is<SemIR::FacetValue>(inst_id));
  174. return context.constant_values().GetConstantInstId(inst_id);
  175. }
  176. auto GetCanonicalizedFacetOrTypeValue(Context& context,
  177. SemIR::ConstantId const_id)
  178. -> SemIR::ConstantId {
  179. return context.constant_values().Get(GetCanonicalizedFacetOrTypeValue(
  180. context, context.constant_values().GetInstId(const_id)));
  181. }
  182. } // namespace Carbon::Check