type.cpp 11 KB

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