type.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #ifndef CARBON_TOOLCHAIN_CHECK_TYPE_H_
  5. #define CARBON_TOOLCHAIN_CHECK_TYPE_H_
  6. #include "llvm/ADT/ArrayRef.h"
  7. #include "toolchain/check/context.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::Check {
  10. // Enforces that an integer type has a valid bit width.
  11. auto ValidateIntType(Context& context, SemIR::LocId loc_id,
  12. SemIR::IntType result) -> bool;
  13. // Enforces that the bit width is 64 for a float.
  14. auto ValidateFloatBitWidth(Context& context, SemIR::LocId loc_id,
  15. SemIR::InstId inst_id) -> bool;
  16. // Enforces that a float type has a valid bit width.
  17. auto ValidateFloatType(Context& context, SemIR::LocId loc_id,
  18. SemIR::FloatType result) -> bool;
  19. // Gets the type to use for an unbound associated entity declared in this
  20. // interface. For example, this is the type of `I.T` after
  21. // `interface I { let T:! type; }`. The name of the interface is used for
  22. // diagnostics.
  23. // TODO: Should we use a different type for each such entity, or the same type
  24. // for all associated entities?
  25. auto GetAssociatedEntityType(Context& context, SemIR::InterfaceId interface_id,
  26. SemIR::SpecificId interface_specific_id)
  27. -> SemIR::TypeId;
  28. // Gets a singleton type. The returned type will be complete. Requires that
  29. // `singleton_id` is already validated to be a singleton.
  30. auto GetSingletonType(Context& context, SemIR::TypeInstId singleton_id)
  31. -> SemIR::TypeId;
  32. // Gets a class type.
  33. auto GetClassType(Context& context, SemIR::ClassId class_id,
  34. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  35. // Gets a function type. The returned type will be complete.
  36. auto GetFunctionType(Context& context, SemIR::FunctionId fn_id,
  37. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  38. auto GetVtableType(Context& context, SemIR::VtableId vtable_id)
  39. -> SemIR::TypeId;
  40. // Gets the type of an associated function with the `Self` parameter bound to
  41. // a particular value. The returned type will be complete.
  42. auto GetFunctionTypeWithSelfType(Context& context,
  43. SemIR::TypeInstId interface_function_type_id,
  44. SemIR::InstId self_id) -> SemIR::TypeId;
  45. // Gets a generic class type, which is the type of a name of a generic class,
  46. // such as the type of `Vector` given `class Vector(T:! type)`. The returned
  47. // type will be complete.
  48. auto GetGenericClassType(Context& context, SemIR::ClassId class_id,
  49. SemIR::SpecificId enclosing_specific_id)
  50. -> SemIR::TypeId;
  51. // Gets a generic interface type, which is the type of a name of a generic
  52. // interface, such as the type of `AddWith` given
  53. // `interface AddWith(T:! type)`. The returned type will be complete.
  54. auto GetGenericInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  55. SemIR::SpecificId enclosing_specific_id)
  56. -> SemIR::TypeId;
  57. // Gets the facet type corresponding to a particular interface.
  58. auto GetInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  59. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  60. // Returns a pointer type whose pointee type is `pointee_type_id`.
  61. auto GetPointerType(Context& context, SemIR::TypeInstId pointee_type_id)
  62. -> SemIR::TypeId;
  63. // Returns a struct type with the given fields.
  64. auto GetStructType(Context& context, SemIR::StructTypeFieldsId fields_id)
  65. -> SemIR::TypeId;
  66. // Returns a tuple type with the given element types.
  67. auto GetTupleType(Context& context, llvm::ArrayRef<SemIR::InstId> type_inst_ids)
  68. -> SemIR::TypeId;
  69. // Returns a pattern type with the given scrutinee type.
  70. auto GetPatternType(Context& context, SemIR::TypeId scrutinee_type_id)
  71. -> SemIR::TypeId;
  72. // Returns an unbound element type.
  73. auto GetUnboundElementType(Context& context, SemIR::TypeInstId class_type_id,
  74. SemIR::TypeInstId element_type_id) -> SemIR::TypeId;
  75. // Convert a facet value or type value instruction to a canonical facet or type
  76. // value instruction.
  77. //
  78. // Type values are already canonical and are returned unchanged, except for
  79. // `FacetAccessType` which is unwrapped to find the facet value it refers to.
  80. //
  81. // For facet values, unwraps `FacetValue` instructions to get to an underlying
  82. // canonical type instruction.
  83. auto GetCanonicalizedFacetOrTypeValue(Context& context, SemIR::InstId inst_id)
  84. -> SemIR::InstId;
  85. auto GetCanonicalizedFacetOrTypeValue(Context& context,
  86. SemIR::ConstantId const_id)
  87. -> SemIR::ConstantId;
  88. } // namespace Carbon::Check
  89. #endif // CARBON_TOOLCHAIN_CHECK_TYPE_H_