type.cpp 4.2 KB

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