type.h 4.9 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. #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 const-qualified version of a type.
  33. auto GetConstType(Context& context, SemIR::TypeInstId inner_type_id)
  34. -> SemIR::TypeId;
  35. // Gets a class type.
  36. auto GetClassType(Context& context, SemIR::ClassId class_id,
  37. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  38. // Gets a function type. The returned type will be complete.
  39. auto GetFunctionType(Context& context, SemIR::FunctionId fn_id,
  40. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  41. auto GetVtableType(Context& context, SemIR::VtableId vtable_id)
  42. -> SemIR::TypeId;
  43. // Gets the type of an associated function with the `Self` parameter bound to
  44. // a particular value. The returned type will be complete.
  45. auto GetFunctionTypeWithSelfType(Context& context,
  46. SemIR::TypeInstId interface_function_type_id,
  47. SemIR::InstId self_id) -> SemIR::TypeId;
  48. // Gets a generic class type, which is the type of a name of a generic class,
  49. // such as the type of `Vector` given `class Vector(T:! type)`. The returned
  50. // type will be complete.
  51. auto GetGenericClassType(Context& context, SemIR::ClassId class_id,
  52. SemIR::SpecificId enclosing_specific_id)
  53. -> SemIR::TypeId;
  54. // Gets a generic interface type, which is the type of a name of a generic
  55. // interface, such as the type of `AddWith` given
  56. // `interface AddWith(T:! type)`. The returned type will be complete.
  57. auto GetGenericInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  58. SemIR::SpecificId enclosing_specific_id)
  59. -> SemIR::TypeId;
  60. // Gets the facet type corresponding to a particular interface.
  61. auto GetInterfaceType(Context& context, SemIR::InterfaceId interface_id,
  62. SemIR::SpecificId specific_id) -> SemIR::TypeId;
  63. // Returns a pointer type whose pointee type is `pointee_type_id`.
  64. auto GetPointerType(Context& context, SemIR::TypeInstId pointee_type_id)
  65. -> SemIR::TypeId;
  66. // Returns a struct type with the given fields.
  67. auto GetStructType(Context& context, SemIR::StructTypeFieldsId fields_id)
  68. -> SemIR::TypeId;
  69. // Returns a tuple type with the given element types.
  70. auto GetTupleType(Context& context, llvm::ArrayRef<SemIR::InstId> type_inst_ids)
  71. -> SemIR::TypeId;
  72. // Returns a pattern type with the given scrutinee type.
  73. auto GetPatternType(Context& context, SemIR::TypeId scrutinee_type_id)
  74. -> SemIR::TypeId;
  75. // Returns an unbound element type.
  76. auto GetUnboundElementType(Context& context, SemIR::TypeInstId class_type_id,
  77. SemIR::TypeInstId element_type_id) -> SemIR::TypeId;
  78. // Convert a facet value or type value instruction to a canonical facet or type
  79. // value instruction.
  80. //
  81. // Type values are already canonical and are returned unchanged, except for
  82. // `FacetAccessType` which is unwrapped to find the facet value it refers to.
  83. //
  84. // For facet values, unwraps `FacetValue` instructions to get to an underlying
  85. // canonical type instruction.
  86. auto GetCanonicalizedFacetOrTypeValue(Context& context, SemIR::InstId inst_id)
  87. -> SemIR::InstId;
  88. auto GetCanonicalizedFacetOrTypeValue(Context& context,
  89. SemIR::ConstantId const_id)
  90. -> SemIR::ConstantId;
  91. } // namespace Carbon::Check
  92. #endif // CARBON_TOOLCHAIN_CHECK_TYPE_H_