type.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 GetTypeComponent(Context& context, SemIR::InstId form_inst_id)
  125. -> SemIR::TypeId {
  126. return GetTypeImpl<SemIR::TypeComponentOf>(context, form_inst_id);
  127. }
  128. auto GetQualifiedType(Context& context, SemIR::TypeId type_id,
  129. SemIR::TypeQualifiers quals) -> SemIR::TypeId {
  130. if (quals.HasAnyOf(SemIR::TypeQualifiers::Const)) {
  131. type_id = GetConstType(context, context.types().GetTypeInstId(type_id));
  132. quals.Remove(SemIR::TypeQualifiers::Const);
  133. }
  134. if (quals.HasAnyOf(SemIR::TypeQualifiers::MaybeUnformed)) {
  135. type_id = GetTypeImpl<SemIR::MaybeUnformedType>(
  136. context, context.types().GetTypeInstId(type_id));
  137. quals.Remove(SemIR::TypeQualifiers::MaybeUnformed);
  138. }
  139. if (quals.HasAnyOf(SemIR::TypeQualifiers::Partial)) {
  140. type_id = GetTypeImpl<SemIR::PartialType>(
  141. context, context.types().GetTypeInstId(type_id));
  142. quals.Remove(SemIR::TypeQualifiers::Partial);
  143. }
  144. CARBON_CHECK(quals == SemIR::TypeQualifiers::None);
  145. return type_id;
  146. }
  147. auto GetSingletonType(Context& context, SemIR::TypeInstId singleton_id)
  148. -> SemIR::TypeId {
  149. CARBON_CHECK(SemIR::IsSingletonInstId(singleton_id));
  150. auto type_id = context.types().GetTypeIdForTypeInstId(singleton_id);
  151. // To keep client code simpler, complete builtin types before returning them.
  152. CompleteTypeOrCheckFail(context, type_id);
  153. return type_id;
  154. }
  155. auto GetClassType(Context& context, SemIR::ClassId class_id,
  156. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  157. return GetTypeImpl<SemIR::ClassType>(context, class_id, specific_id);
  158. }
  159. auto GetCppOverloadSetType(Context& context,
  160. SemIR::CppOverloadSetId overload_set_id,
  161. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  162. return GetCompleteTypeImpl<SemIR::CppOverloadSetType>(
  163. context, overload_set_id, specific_id);
  164. }
  165. auto GetCppTemplateNameType(Context& context, SemIR::EntityNameId name_id,
  166. SemIR::ClangDeclId decl_id) -> SemIR::TypeId {
  167. return GetCompleteTypeImpl<SemIR::CppTemplateNameType>(context, name_id,
  168. decl_id);
  169. }
  170. auto GetFunctionType(Context& context, SemIR::FunctionId fn_id,
  171. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  172. return GetCompleteTypeImpl<SemIR::FunctionType>(context, fn_id, specific_id);
  173. }
  174. auto GetFunctionTypeWithSelfType(Context& context,
  175. SemIR::TypeInstId interface_function_type_id,
  176. SemIR::InstId self_id) -> SemIR::TypeId {
  177. return GetCompleteTypeImpl<SemIR::FunctionTypeWithSelfType>(
  178. context, interface_function_type_id, self_id);
  179. }
  180. auto GetGenericClassType(Context& context, SemIR::ClassId class_id,
  181. SemIR::SpecificId enclosing_specific_id)
  182. -> SemIR::TypeId {
  183. return GetCompleteTypeImpl<SemIR::GenericClassType>(context, class_id,
  184. enclosing_specific_id);
  185. }
  186. auto GetGenericInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  187. SemIR::SpecificId enclosing_specific_id)
  188. -> SemIR::TypeId {
  189. return GetCompleteTypeImpl<SemIR::GenericInterfaceType>(
  190. context, interface_id, enclosing_specific_id);
  191. }
  192. auto GetGenericNamedConstraintType(Context& context,
  193. SemIR::NamedConstraintId named_constraint_id,
  194. SemIR::SpecificId enclosing_specific_id)
  195. -> SemIR::TypeId {
  196. return GetCompleteTypeImpl<SemIR::GenericNamedConstraintType>(
  197. context, named_constraint_id, enclosing_specific_id);
  198. }
  199. auto GetInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  200. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  201. return GetTypeImpl<SemIR::FacetType>(
  202. context,
  203. FacetTypeFromInterface(context, interface_id, specific_id).facet_type_id);
  204. }
  205. auto GetNamedConstraintType(Context& context,
  206. SemIR::NamedConstraintId named_constraint_id,
  207. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  208. return GetTypeImpl<SemIR::FacetType>(
  209. context,
  210. FacetTypeFromNamedConstraint(context, named_constraint_id, specific_id)
  211. .facet_type_id);
  212. }
  213. auto GetFacetType(Context& context, const SemIR::FacetTypeInfo& info)
  214. -> SemIR::TypeId {
  215. return GetTypeImpl<SemIR::FacetType>(context,
  216. context.facet_types().Add(info));
  217. }
  218. auto GetFacetAccessType(Context& context, SemIR::InstId facet_value_inst_id)
  219. -> SemIR::TypeId {
  220. return GetTypeImpl<SemIR::FacetAccessType>(context, facet_value_inst_id);
  221. }
  222. auto GetPointerType(Context& context, SemIR::TypeInstId pointee_type_id)
  223. -> SemIR::TypeId {
  224. return GetCompleteTypeImpl<SemIR::PointerType>(context, pointee_type_id);
  225. }
  226. auto GetPatternType(Context& context, SemIR::TypeId scrutinee_type_id)
  227. -> SemIR::TypeId {
  228. CARBON_CHECK(!context.types().Is<SemIR::PatternType>(scrutinee_type_id),
  229. "Type is already a pattern type");
  230. if (scrutinee_type_id == SemIR::ErrorInst::TypeId) {
  231. return SemIR::ErrorInst::TypeId;
  232. }
  233. return GetTypeImpl<SemIR::PatternType>(
  234. context, context.types().GetTypeInstId(scrutinee_type_id));
  235. }
  236. auto GetUnboundElementType(Context& context, SemIR::TypeInstId class_type_id,
  237. SemIR::TypeInstId element_type_id) -> SemIR::TypeId {
  238. return GetTypeImpl<SemIR::UnboundElementType>(context, class_type_id,
  239. element_type_id);
  240. }
  241. auto GetCanonicalFacetOrTypeValue(Context& context, SemIR::InstId inst_id)
  242. -> SemIR::InstId {
  243. auto const_inst_id = context.constant_values().GetConstantInstId(inst_id);
  244. CARBON_DCHECK(const_inst_id.has_value());
  245. if (auto access =
  246. context.insts().TryGetAs<SemIR::FacetAccessType>(const_inst_id)) {
  247. return access->facet_value_inst_id;
  248. }
  249. return const_inst_id;
  250. }
  251. auto GetCanonicalFacetOrTypeValue(Context& context, SemIR::ConstantId const_id)
  252. -> SemIR::ConstantId {
  253. return context.constant_values().Get(GetCanonicalFacetOrTypeValue(
  254. context, context.constant_values().GetInstId(const_id)));
  255. }
  256. auto TryGetCanonicalFacetValue(Context& context, SemIR::InstId inst_id)
  257. -> SemIR::InstId {
  258. if (context.insts().Get(inst_id).type_id() == SemIR::TypeType::TypeId) {
  259. return GetCanonicalFacetOrTypeValue(context, inst_id);
  260. }
  261. return SemIR::InstId::None;
  262. }
  263. } // namespace Carbon::Check