type.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 GetCppTemplateNameType(Context& context, SemIR::EntityNameId name_id,
  162. SemIR::ClangDeclId decl_id) -> SemIR::TypeId {
  163. return GetCompleteTypeImpl<SemIR::CppTemplateNameType>(context, name_id,
  164. decl_id);
  165. }
  166. auto GetFunctionType(Context& context, SemIR::FunctionId fn_id,
  167. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  168. return GetCompleteTypeImpl<SemIR::FunctionType>(context, fn_id, specific_id);
  169. }
  170. auto GetFunctionTypeWithSelfType(Context& context,
  171. SemIR::TypeInstId interface_function_type_id,
  172. SemIR::InstId self_id) -> SemIR::TypeId {
  173. return GetCompleteTypeImpl<SemIR::FunctionTypeWithSelfType>(
  174. context, interface_function_type_id, self_id);
  175. }
  176. auto GetGenericClassType(Context& context, SemIR::ClassId class_id,
  177. SemIR::SpecificId enclosing_specific_id)
  178. -> SemIR::TypeId {
  179. return GetCompleteTypeImpl<SemIR::GenericClassType>(context, class_id,
  180. enclosing_specific_id);
  181. }
  182. auto GetGenericInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  183. SemIR::SpecificId enclosing_specific_id)
  184. -> SemIR::TypeId {
  185. return GetCompleteTypeImpl<SemIR::GenericInterfaceType>(
  186. context, interface_id, enclosing_specific_id);
  187. }
  188. auto GetGenericNamedConstraintType(Context& context,
  189. SemIR::NamedConstraintId named_constraint_id,
  190. SemIR::SpecificId enclosing_specific_id)
  191. -> SemIR::TypeId {
  192. return GetCompleteTypeImpl<SemIR::GenericNamedConstraintType>(
  193. context, named_constraint_id, enclosing_specific_id);
  194. }
  195. auto GetInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  196. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  197. return GetTypeImpl<SemIR::FacetType>(
  198. context,
  199. FacetTypeFromInterface(context, interface_id, specific_id).facet_type_id);
  200. }
  201. auto GetNamedConstraintType(Context& context,
  202. SemIR::NamedConstraintId named_constraint_id,
  203. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  204. return GetTypeImpl<SemIR::FacetType>(
  205. context,
  206. FacetTypeFromNamedConstraint(context, named_constraint_id, specific_id)
  207. .facet_type_id);
  208. }
  209. auto GetFacetType(Context& context, const SemIR::FacetTypeInfo& info)
  210. -> SemIR::TypeId {
  211. return GetTypeImpl<SemIR::FacetType>(context,
  212. context.facet_types().Add(info));
  213. }
  214. auto GetFacetAccessType(Context& context, SemIR::InstId facet_value_inst_id)
  215. -> SemIR::TypeId {
  216. return GetTypeImpl<SemIR::FacetAccessType>(context, facet_value_inst_id);
  217. }
  218. auto GetPointerType(Context& context, SemIR::TypeInstId pointee_type_id)
  219. -> SemIR::TypeId {
  220. return GetCompleteTypeImpl<SemIR::PointerType>(context, pointee_type_id);
  221. }
  222. auto GetPatternType(Context& context, SemIR::TypeId scrutinee_type_id)
  223. -> SemIR::TypeId {
  224. CARBON_CHECK(!context.types().Is<SemIR::PatternType>(scrutinee_type_id),
  225. "Type is already a pattern type");
  226. if (scrutinee_type_id == SemIR::ErrorInst::TypeId) {
  227. return SemIR::ErrorInst::TypeId;
  228. }
  229. return GetTypeImpl<SemIR::PatternType>(
  230. context, context.types().GetInstId(scrutinee_type_id));
  231. }
  232. auto GetUnboundElementType(Context& context, SemIR::TypeInstId class_type_id,
  233. SemIR::TypeInstId element_type_id) -> SemIR::TypeId {
  234. return GetTypeImpl<SemIR::UnboundElementType>(context, class_type_id,
  235. element_type_id);
  236. }
  237. auto GetCanonicalFacetOrTypeValue(Context& context, SemIR::InstId inst_id)
  238. -> SemIR::InstId {
  239. auto const_inst_id = context.constant_values().GetConstantInstId(inst_id);
  240. CARBON_DCHECK(const_inst_id.has_value());
  241. if (auto access =
  242. context.insts().TryGetAs<SemIR::FacetAccessType>(const_inst_id)) {
  243. return access->facet_value_inst_id;
  244. }
  245. if (auto access =
  246. context.insts().TryGetAs<SemIR::SymbolicBindingType>(const_inst_id)) {
  247. // TODO: Look in ScopeStack with the entity_name_id to find the facet value.
  248. return access->facet_value_inst_id;
  249. }
  250. return const_inst_id;
  251. }
  252. auto GetCanonicalFacetOrTypeValue(Context& context, SemIR::ConstantId const_id)
  253. -> SemIR::ConstantId {
  254. return context.constant_values().Get(GetCanonicalFacetOrTypeValue(
  255. context, context.constant_values().GetInstId(const_id)));
  256. }
  257. auto TryGetCanonicalFacetValue(Context& context, SemIR::InstId inst_id)
  258. -> SemIR::InstId {
  259. if (context.insts().Get(inst_id).type_id() == SemIR::TypeType::TypeId) {
  260. return GetCanonicalFacetOrTypeValue(context, inst_id);
  261. }
  262. return SemIR::InstId::None;
  263. }
  264. } // namespace Carbon::Check