type.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #include "toolchain/sem_ir/facet_type_info.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::Check {
  11. auto ValidateIntType(Context& context, SemIR::LocId loc_id,
  12. SemIR::IntType result) -> bool {
  13. auto bit_width =
  14. context.insts().TryGetAs<SemIR::IntValue>(result.bit_width_id);
  15. if (!bit_width) {
  16. // Symbolic bit width.
  17. return true;
  18. }
  19. const auto& bit_width_val = context.ints().Get(bit_width->int_id);
  20. if (bit_width_val.isZero() ||
  21. (context.types().IsSignedInt(bit_width->type_id) &&
  22. bit_width_val.isNegative())) {
  23. CARBON_DIAGNOSTIC(IntWidthNotPositive, Error,
  24. "integer type width of {0} is not positive", TypedInt);
  25. context.emitter().Emit(
  26. loc_id, IntWidthNotPositive,
  27. {.type = bit_width->type_id, .value = bit_width_val});
  28. return false;
  29. }
  30. if (bit_width_val.ugt(IntStore::MaxIntWidth)) {
  31. CARBON_DIAGNOSTIC(IntWidthTooLarge, Error,
  32. "integer type width of {0} is greater than the "
  33. "maximum supported width of {1}",
  34. TypedInt, int);
  35. context.emitter().Emit(loc_id, IntWidthTooLarge,
  36. {.type = bit_width->type_id, .value = bit_width_val},
  37. IntStore::MaxIntWidth);
  38. return false;
  39. }
  40. return true;
  41. }
  42. auto ValidateFloatTypeAndSetKind(Context& context, SemIR::LocId loc_id,
  43. SemIR::FloatType& result) -> bool {
  44. // Get the bit width value.
  45. auto bit_width_inst =
  46. context.insts().TryGetAs<SemIR::IntValue>(result.bit_width_id);
  47. if (!bit_width_inst) {
  48. // Symbolic bit width. Defer checking until we have a concrete value.
  49. return true;
  50. }
  51. auto bit_width = context.ints().Get(bit_width_inst->int_id);
  52. // If no kind is specified, infer kind from width.
  53. if (!result.float_kind.has_value()) {
  54. switch (bit_width.getLimitedValue()) {
  55. case 16:
  56. result.float_kind = SemIR::FloatKind::Binary16;
  57. break;
  58. case 32:
  59. result.float_kind = SemIR::FloatKind::Binary32;
  60. break;
  61. case 64:
  62. result.float_kind = SemIR::FloatKind::Binary64;
  63. break;
  64. case 128:
  65. result.float_kind = SemIR::FloatKind::Binary128;
  66. break;
  67. default:
  68. CARBON_DIAGNOSTIC(CompileTimeFloatBitWidth, Error,
  69. "unsupported floating-point bit width {0}", TypedInt);
  70. context.emitter().Emit(loc_id, CompileTimeFloatBitWidth,
  71. TypedInt(bit_width_inst->type_id, bit_width));
  72. return false;
  73. }
  74. }
  75. if (llvm::APFloat::semanticsSizeInBits(result.float_kind.Semantics()) !=
  76. bit_width) {
  77. // This can't currently happen because we don't provide any way to set the
  78. // float kind other than through the bit width.
  79. // TODO: Add a float_type.make builtin that takes a float kind, and add a
  80. // diagnostic here if the size is wrong.
  81. context.TODO(loc_id, "wrong size for float type");
  82. return false;
  83. }
  84. // TODO: Diagnose if the floating-point type is not supported on this target?
  85. return true;
  86. }
  87. // Gets or forms a type_id for a type, given the instruction kind and arguments.
  88. template <typename InstT, typename... EachArgT>
  89. static auto GetTypeImpl(Context& context, EachArgT... each_arg)
  90. -> SemIR::TypeId {
  91. InstT inst = {SemIR::TypeType::TypeId, each_arg...};
  92. return context.types().GetTypeIdForTypeConstantId(TryEvalInst(context, inst));
  93. }
  94. // Gets or forms a type_id for a type, given the instruction kind and arguments,
  95. // and completes the type. This should only be used when type completion cannot
  96. // fail.
  97. template <typename InstT, typename... EachArgT>
  98. static auto GetCompleteTypeImpl(Context& context, EachArgT... each_arg)
  99. -> SemIR::TypeId {
  100. auto type_id = GetTypeImpl<InstT>(context, each_arg...);
  101. CompleteTypeOrCheckFail(context, type_id);
  102. return type_id;
  103. }
  104. auto GetStructType(Context& context, SemIR::StructTypeFieldsId fields_id)
  105. -> SemIR::TypeId {
  106. return GetTypeImpl<SemIR::StructType>(context, fields_id);
  107. }
  108. auto GetTupleType(Context& context, llvm::ArrayRef<SemIR::InstId> type_inst_ids)
  109. -> SemIR::TypeId {
  110. return GetTypeImpl<SemIR::TupleType>(
  111. context, context.inst_blocks().AddCanonical(type_inst_ids));
  112. }
  113. auto GetAssociatedEntityType(Context& context, SemIR::InterfaceId interface_id,
  114. SemIR::SpecificId interface_specific_id)
  115. -> SemIR::TypeId {
  116. return GetTypeImpl<SemIR::AssociatedEntityType>(context, interface_id,
  117. interface_specific_id);
  118. }
  119. auto GetConstType(Context& context, SemIR::TypeInstId inner_type_id)
  120. -> SemIR::TypeId {
  121. return GetTypeImpl<SemIR::ConstType>(context, inner_type_id);
  122. }
  123. auto GetQualifiedType(Context& context, SemIR::TypeId type_id,
  124. SemIR::TypeQualifiers quals) -> SemIR::TypeId {
  125. if (HasTypeQualifier(quals, SemIR::TypeQualifiers::Const)) {
  126. type_id = GetConstType(context, context.types().GetInstId(type_id));
  127. quals &= ~SemIR::TypeQualifiers::Const;
  128. }
  129. if (HasTypeQualifier(quals, SemIR::TypeQualifiers::MaybeUnformed)) {
  130. type_id = GetTypeImpl<SemIR::MaybeUnformedType>(
  131. context, context.types().GetInstId(type_id));
  132. quals &= ~SemIR::TypeQualifiers::MaybeUnformed;
  133. }
  134. if (HasTypeQualifier(quals, SemIR::TypeQualifiers::Partial)) {
  135. type_id = GetTypeImpl<SemIR::PartialType>(
  136. context, context.types().GetInstId(type_id));
  137. quals &= ~SemIR::TypeQualifiers::Partial;
  138. }
  139. CARBON_CHECK(quals == SemIR::TypeQualifiers::None);
  140. return type_id;
  141. }
  142. auto GetSingletonType(Context& context, SemIR::TypeInstId singleton_id)
  143. -> SemIR::TypeId {
  144. CARBON_CHECK(SemIR::IsSingletonInstId(singleton_id));
  145. auto type_id = context.types().GetTypeIdForTypeInstId(singleton_id);
  146. // To keep client code simpler, complete builtin types before returning them.
  147. CompleteTypeOrCheckFail(context, type_id);
  148. return type_id;
  149. }
  150. auto GetClassType(Context& context, SemIR::ClassId class_id,
  151. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  152. return GetTypeImpl<SemIR::ClassType>(context, class_id, specific_id);
  153. }
  154. auto GetFunctionType(Context& context, SemIR::FunctionId fn_id,
  155. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  156. return GetCompleteTypeImpl<SemIR::FunctionType>(context, fn_id, specific_id);
  157. }
  158. auto GetFunctionTypeWithSelfType(Context& context,
  159. SemIR::TypeInstId interface_function_type_id,
  160. SemIR::InstId self_id) -> SemIR::TypeId {
  161. return GetCompleteTypeImpl<SemIR::FunctionTypeWithSelfType>(
  162. context, interface_function_type_id, self_id);
  163. }
  164. auto GetGenericClassType(Context& context, SemIR::ClassId class_id,
  165. SemIR::SpecificId enclosing_specific_id)
  166. -> SemIR::TypeId {
  167. return GetCompleteTypeImpl<SemIR::GenericClassType>(context, class_id,
  168. enclosing_specific_id);
  169. }
  170. auto GetGenericInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  171. SemIR::SpecificId enclosing_specific_id)
  172. -> SemIR::TypeId {
  173. return GetCompleteTypeImpl<SemIR::GenericInterfaceType>(
  174. context, interface_id, enclosing_specific_id);
  175. }
  176. auto GetInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  177. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  178. return GetTypeImpl<SemIR::FacetType>(
  179. context,
  180. FacetTypeFromInterface(context, interface_id, specific_id).facet_type_id);
  181. }
  182. auto GetFacetType(Context& context, const SemIR::FacetTypeInfo& info)
  183. -> SemIR::TypeId {
  184. return GetTypeImpl<SemIR::FacetType>(context,
  185. context.facet_types().Add(info));
  186. }
  187. auto GetPointerType(Context& context, SemIR::TypeInstId pointee_type_id)
  188. -> SemIR::TypeId {
  189. return GetTypeImpl<SemIR::PointerType>(context, pointee_type_id);
  190. }
  191. auto GetPatternType(Context& context, SemIR::TypeId scrutinee_type_id)
  192. -> SemIR::TypeId {
  193. CARBON_CHECK(!context.types().Is<SemIR::PatternType>(scrutinee_type_id),
  194. "Type is already a pattern type");
  195. if (scrutinee_type_id == SemIR::ErrorInst::TypeId) {
  196. return SemIR::ErrorInst::TypeId;
  197. }
  198. return GetTypeImpl<SemIR::PatternType>(
  199. context, context.types().GetInstId(scrutinee_type_id));
  200. }
  201. auto GetUnboundElementType(Context& context, SemIR::TypeInstId class_type_id,
  202. SemIR::TypeInstId element_type_id) -> SemIR::TypeId {
  203. return GetTypeImpl<SemIR::UnboundElementType>(context, class_type_id,
  204. element_type_id);
  205. }
  206. auto GetCanonicalizedFacetOrTypeValue(Context& context, SemIR::InstId inst_id)
  207. -> SemIR::InstId {
  208. // We can have FacetAccessType of a FacetValue, and a FacetValue of a
  209. // FacetAccessType, but they don't nest indefinitely.
  210. if (auto access = context.insts().TryGetAs<SemIR::FacetAccessType>(inst_id)) {
  211. inst_id = access->facet_value_inst_id;
  212. }
  213. if (auto value = context.insts().TryGetAs<SemIR::FacetValue>(inst_id)) {
  214. inst_id = value->type_inst_id;
  215. if (auto access =
  216. context.insts().TryGetAs<SemIR::FacetAccessType>(inst_id)) {
  217. inst_id = access->facet_value_inst_id;
  218. }
  219. }
  220. CARBON_CHECK(!context.insts().Is<SemIR::FacetAccessType>(inst_id));
  221. CARBON_CHECK(!context.insts().Is<SemIR::FacetValue>(inst_id));
  222. return context.constant_values().GetConstantInstId(inst_id);
  223. }
  224. auto GetCanonicalizedFacetOrTypeValue(Context& context,
  225. SemIR::ConstantId const_id)
  226. -> SemIR::ConstantId {
  227. return context.constant_values().Get(GetCanonicalizedFacetOrTypeValue(
  228. context, context.constant_values().GetInstId(const_id)));
  229. }
  230. } // namespace Carbon::Check