type.h 5.1 KB

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