type.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/type_completion.h"
  7. namespace Carbon::Check {
  8. // Gets or forms a type_id for a type, given the instruction kind and arguments.
  9. template <typename InstT, typename... EachArgT>
  10. static auto GetTypeImpl(Context& context, EachArgT... each_arg)
  11. -> SemIR::TypeId {
  12. // TODO: Remove inst_id parameter from TryEvalInst.
  13. InstT inst = {SemIR::TypeType::SingletonTypeId, each_arg...};
  14. return context.types().GetTypeIdForTypeConstantId(
  15. TryEvalInst(context, SemIR::InstId::None, inst));
  16. }
  17. auto FacetTypeFromInterface(Context& context, SemIR::InterfaceId interface_id,
  18. SemIR::SpecificId specific_id) -> SemIR::FacetType {
  19. SemIR::FacetTypeId facet_type_id = context.facet_types().Add(
  20. SemIR::FacetTypeInfo{.impls_constraints = {{interface_id, specific_id}},
  21. .other_requirements = false});
  22. return {.type_id = SemIR::TypeType::SingletonTypeId,
  23. .facet_type_id = facet_type_id};
  24. }
  25. // Gets or forms a type_id for a type, given the instruction kind and arguments,
  26. // and completes the type. This should only be used when type completion cannot
  27. // fail.
  28. template <typename InstT, typename... EachArgT>
  29. static auto GetCompleteTypeImpl(Context& context, EachArgT... each_arg)
  30. -> SemIR::TypeId {
  31. auto type_id = GetTypeImpl<InstT>(context, each_arg...);
  32. CompleteTypeOrCheckFail(context, type_id);
  33. return type_id;
  34. }
  35. auto GetStructType(Context& context, SemIR::StructTypeFieldsId fields_id)
  36. -> SemIR::TypeId {
  37. return GetTypeImpl<SemIR::StructType>(context, fields_id);
  38. }
  39. auto GetTupleType(Context& context, llvm::ArrayRef<SemIR::TypeId> type_ids)
  40. -> SemIR::TypeId {
  41. return GetTypeImpl<SemIR::TupleType>(
  42. context, context.type_blocks().AddCanonical(type_ids));
  43. }
  44. auto GetAssociatedEntityType(Context& context, SemIR::TypeId interface_type_id)
  45. -> SemIR::TypeId {
  46. return GetTypeImpl<SemIR::AssociatedEntityType>(context, interface_type_id);
  47. }
  48. auto GetSingletonType(Context& context, SemIR::InstId singleton_id)
  49. -> SemIR::TypeId {
  50. CARBON_CHECK(SemIR::IsSingletonInstId(singleton_id));
  51. auto type_id = context.types().GetTypeIdForTypeInstId(singleton_id);
  52. // To keep client code simpler, complete builtin types before returning them.
  53. CompleteTypeOrCheckFail(context, type_id);
  54. return type_id;
  55. }
  56. auto GetClassType(Context& context, SemIR::ClassId class_id,
  57. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  58. return GetTypeImpl<SemIR::ClassType>(context, class_id, specific_id);
  59. }
  60. auto GetFunctionType(Context& context, SemIR::FunctionId fn_id,
  61. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  62. return GetCompleteTypeImpl<SemIR::FunctionType>(context, fn_id, specific_id);
  63. }
  64. auto GetFunctionTypeWithSelfType(Context& context,
  65. SemIR::InstId interface_function_type_id,
  66. SemIR::InstId self_id) -> SemIR::TypeId {
  67. return GetCompleteTypeImpl<SemIR::FunctionTypeWithSelfType>(
  68. context, interface_function_type_id, self_id);
  69. }
  70. auto GetGenericClassType(Context& context, SemIR::ClassId class_id,
  71. SemIR::SpecificId enclosing_specific_id)
  72. -> SemIR::TypeId {
  73. return GetCompleteTypeImpl<SemIR::GenericClassType>(context, class_id,
  74. enclosing_specific_id);
  75. }
  76. auto GetGenericInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  77. SemIR::SpecificId enclosing_specific_id)
  78. -> SemIR::TypeId {
  79. return GetCompleteTypeImpl<SemIR::GenericInterfaceType>(
  80. context, interface_id, enclosing_specific_id);
  81. }
  82. auto GetInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  83. SemIR::SpecificId specific_id) -> SemIR::TypeId {
  84. return GetTypeImpl<SemIR::FacetType>(
  85. context,
  86. FacetTypeFromInterface(context, interface_id, specific_id).facet_type_id);
  87. }
  88. auto GetPointerType(Context& context, SemIR::TypeId pointee_type_id)
  89. -> SemIR::TypeId {
  90. return GetTypeImpl<SemIR::PointerType>(context, pointee_type_id);
  91. }
  92. auto GetUnboundElementType(Context& context, SemIR::TypeId class_type_id,
  93. SemIR::TypeId element_type_id) -> SemIR::TypeId {
  94. return GetTypeImpl<SemIR::UnboundElementType>(context, class_type_id,
  95. element_type_id);
  96. }
  97. } // namespace Carbon::Check