type.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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, SemIRLoc loc, SemIR::IntType result)
  11. -> 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, 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, 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, SemIRLoc loc,
  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, CompileTimeFloatBitWidth);
  50. return false;
  51. }
  52. // Enforces that a float type has a valid bit width.
  53. auto ValidateFloatType(Context& context, SemIRLoc loc, SemIR::FloatType result)
  54. -> 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, 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::SingletonTypeId, 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 GetSingletonType(Context& context, SemIR::InstId singleton_id)
  96. -> SemIR::TypeId {
  97. CARBON_CHECK(SemIR::IsSingletonInstId(singleton_id));
  98. auto type_id = context.types().GetTypeIdForTypeInstId(singleton_id);
  99. // To keep client code simpler, complete builtin types before returning them.
  100. CompleteTypeOrCheckFail(context, type_id);
  101. return type_id;
  102. }
  103. auto GetClassType(Context& context, SemIR::ClassId class_id,
  104. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  105. return GetTypeImpl<SemIR::ClassType>(context, class_id, specific_id);
  106. }
  107. auto GetFunctionType(Context& context, SemIR::FunctionId fn_id,
  108. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  109. return GetCompleteTypeImpl<SemIR::FunctionType>(context, fn_id, specific_id);
  110. }
  111. auto GetFunctionTypeWithSelfType(Context& context,
  112. SemIR::InstId interface_function_type_id,
  113. SemIR::InstId self_id) -> SemIR::TypeId {
  114. return GetCompleteTypeImpl<SemIR::FunctionTypeWithSelfType>(
  115. context, interface_function_type_id, self_id);
  116. }
  117. auto GetGenericClassType(Context& context, SemIR::ClassId class_id,
  118. SemIR::SpecificId enclosing_specific_id)
  119. -> SemIR::TypeId {
  120. return GetCompleteTypeImpl<SemIR::GenericClassType>(context, class_id,
  121. enclosing_specific_id);
  122. }
  123. auto GetGenericInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  124. SemIR::SpecificId enclosing_specific_id)
  125. -> SemIR::TypeId {
  126. return GetCompleteTypeImpl<SemIR::GenericInterfaceType>(
  127. context, interface_id, enclosing_specific_id);
  128. }
  129. auto GetInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  130. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  131. return GetTypeImpl<SemIR::FacetType>(
  132. context,
  133. FacetTypeFromInterface(context, interface_id, specific_id).facet_type_id);
  134. }
  135. auto GetPointerType(Context& context, SemIR::InstId pointee_type_id)
  136. -> SemIR::TypeId {
  137. return GetTypeImpl<SemIR::PointerType>(context, pointee_type_id);
  138. }
  139. auto GetUnboundElementType(Context& context, SemIR::TypeInstId class_type_id,
  140. SemIR::TypeInstId element_type_id) -> SemIR::TypeId {
  141. return GetTypeImpl<SemIR::UnboundElementType>(context, class_type_id,
  142. element_type_id);
  143. }
  144. } // namespace Carbon::Check