convert.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_CONVERT_H_
  5. #define CARBON_TOOLCHAIN_CHECK_CONVERT_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/pending_block.h"
  8. #include "toolchain/sem_ir/entity_with_params_base.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::Check {
  11. // Description of the target of a conversion.
  12. struct ConversionTarget {
  13. enum Kind : int8_t {
  14. // Convert to a value of type `type_id`.
  15. Value,
  16. // Convert to either a value or a reference of type `type_id`.
  17. ValueOrRef,
  18. // Convert to a durable reference of type `type_id`.
  19. DurableRef,
  20. // Convert to a reference, suitable for binding to a reference parameter.
  21. // This allows both durable and ephemeral references. The restriction that
  22. // only a `ref self` parameter can bind to an ephemeral reference is
  23. // enforced separately when handling `ref` tags on call arguments.
  24. RefParam,
  25. // Equivalent to RefParam, except that the source expression is not required
  26. // to be marked with a `ref` tag, such as an argument to a `ref self`
  27. // parameter or an operator operand.
  28. UnmarkedRefParam,
  29. // Convert to a reference of type `type_id`, for use as the argument to a
  30. // C++ thunk.
  31. CppThunkRef,
  32. // Convert for an explicit `as` cast. This allows any expression category
  33. // as the result, and uses the `As` interface instead of the `ImplicitAs`
  34. // interface.
  35. ExplicitAs,
  36. // Convert for an explicit `unsafe as` cast. This allows any expression
  37. // category as the result, and uses the `UnsafeAs` interface instead of the
  38. // `As` or `ImplicitAs` interface.
  39. ExplicitUnsafeAs,
  40. // The result of the conversion is discarded. It can't be an initializing
  41. // expression, but can be anything else.
  42. Discarded,
  43. // Convert to an initializing expression, which a subsequent operation (such
  44. // as `InitializeFrom` or `Temporary`) can use to initialize `storage_id`.
  45. // `storage_id` is only used if `type_id` has an in-place initializing
  46. // representation; otherwise, `storage_id` can be `None`, and the resulting
  47. // initializing expression can be used to initialize any object of the
  48. // appropriate type.
  49. Initializer,
  50. // Convert to an initializing expression, and use it to initialize
  51. // `storage_id` (which must not be `None`).
  52. FullInitializer,
  53. Last = FullInitializer
  54. };
  55. // The kind of the target for this conversion.
  56. Kind kind;
  57. // The target type for the conversion.
  58. SemIR::TypeId type_id;
  59. // The storage being initialized, if any.
  60. SemIR::InstId storage_id = SemIR::InstId::None;
  61. // For an initializer, a block of pending instructions that `storage_id`
  62. // depends on, and that can be discarded if `storage_id` is not accessed.
  63. PendingBlock* storage_access_block = nullptr;
  64. // Whether failure of conversion is an error and is diagnosed to the user.
  65. // When looking for a possible conversion but with graceful fallback, diagnose
  66. // should be false.
  67. bool diagnose = true;
  68. // Are we converting this value into an initializer for an object?
  69. auto is_initializer() const -> bool {
  70. return kind == Initializer || kind == FullInitializer;
  71. }
  72. // Is this some kind of explicit `as` conversion?
  73. auto is_explicit_as() const -> bool {
  74. return kind == ExplicitAs || kind == ExplicitUnsafeAs;
  75. }
  76. };
  77. // Convert a value to another type and expression category.
  78. // TODO: The `vtable_id` parameter is too much of a special case here, and
  79. // should be removed - once partial classes are implemented, the vtable pointer
  80. // initialization will be done not in this conversion, but during initialization
  81. // of the object of non-partial class type from the object of partial class
  82. // type.
  83. auto Convert(Context& context, SemIR::LocId loc_id, SemIR::InstId expr_id,
  84. ConversionTarget target,
  85. SemIR::ClassType* vtable_class_type = nullptr) -> SemIR::InstId;
  86. // Converts `value_id` to an initializing expression of the type of
  87. // `storage_id`, and returns the possibly-converted initializing expression. If
  88. // initialization is in-place, `storage_id` is used as the in-place storage;
  89. // otherwise it is used only to determine the target type. The caller is
  90. // responsible for assigning the returned initializing expression to the target
  91. // using a suitable node for the kind of initialization.
  92. //
  93. // TODO: Consider making the target type a separate parameter, and making
  94. // storage_id optional.
  95. auto Initialize(Context& context, SemIR::LocId loc_id, SemIR::InstId storage_id,
  96. SemIR::InstId value_id) -> SemIR::InstId;
  97. // Convert the given expression to a value expression of the same type.
  98. auto ConvertToValueExpr(Context& context, SemIR::InstId expr_id)
  99. -> SemIR::InstId;
  100. // Convert the given expression to a value or reference expression of the same
  101. // type.
  102. auto ConvertToValueOrRefExpr(Context& context, SemIR::InstId expr_id)
  103. -> SemIR::InstId;
  104. // Converts `expr_id` to a value expression of type `type_id`.
  105. auto ConvertToValueOfType(Context& context, SemIR::LocId loc_id,
  106. SemIR::InstId expr_id, SemIR::TypeId type_id)
  107. -> SemIR::InstId;
  108. // Convert the given expression to a value or reference expression of the given
  109. // type.
  110. auto ConvertToValueOrRefOfType(Context& context, SemIR::LocId loc_id,
  111. SemIR::InstId expr_id, SemIR::TypeId type_id)
  112. -> SemIR::InstId;
  113. // Attempted to convert `expr_id` to a value expression of type `type_id`, with
  114. // graceful failure, which does not result in diagnostics. An ErrorInst
  115. // instruction is still returned on failure.
  116. auto TryConvertToValueOfType(Context& context, SemIR::LocId loc_id,
  117. SemIR::InstId expr_id, SemIR::TypeId type_id)
  118. -> SemIR::InstId;
  119. // Converts `value_id` to a value expression of type `bool`.
  120. auto ConvertToBoolValue(Context& context, SemIR::LocId loc_id,
  121. SemIR::InstId value_id) -> SemIR::InstId;
  122. // Converts `value_id` to type `type_id` for an `as` expression.
  123. auto ConvertForExplicitAs(Context& context, Parse::NodeId as_node,
  124. SemIR::InstId value_id, SemIR::TypeId type_id,
  125. bool unsafe) -> SemIR::InstId;
  126. // Implicitly converts a set of arguments to match the parameter types in a
  127. // function call. Returns a block containing the converted implicit and explicit
  128. // argument values for runtime parameters. `is_operator_syntax` indicates that
  129. // this call was generated from an operator rather than from function call
  130. // syntax, so arguments to `ref` parameters aren't required to have `ref` tags.
  131. auto ConvertCallArgs(Context& context, SemIR::LocId call_loc_id,
  132. SemIR::InstId self_id,
  133. llvm::ArrayRef<SemIR::InstId> arg_refs,
  134. llvm::ArrayRef<SemIR::InstId> return_arg_ids,
  135. const SemIR::Function& callee,
  136. SemIR::SpecificId callee_specific_id,
  137. bool is_operator_syntax) -> SemIR::InstBlockId;
  138. // A type that has been converted for use as a type expression.
  139. struct TypeExpr {
  140. static const TypeExpr None;
  141. // Returns a TypeExpr describing a type with no associated spelling or type
  142. // sugar.
  143. static auto ForUnsugared(Context& context, SemIR::TypeId type_id) -> TypeExpr;
  144. // The converted expression of type `type`, or `ErrorInst::InstId`.
  145. SemIR::TypeInstId inst_id;
  146. // The corresponding type, or `ErrorInst::TypeId`.
  147. SemIR::TypeId type_id;
  148. };
  149. inline constexpr TypeExpr TypeExpr::None = {.inst_id = SemIR::TypeInstId::None,
  150. .type_id = SemIR::TypeId::None};
  151. // Converts an expression for use as a type.
  152. //
  153. // If `diagnose` is true, errors are diagnosed to the user. Set it to false when
  154. // looking to see if a conversion is possible but with graceful fallback.
  155. //
  156. // TODO: Most of the callers of this function discard the `inst_id` and lose
  157. // track of the conversion. In most cases we should be retaining that as the
  158. // operand of some downstream instruction.
  159. auto ExprAsType(Context& context, SemIR::LocId loc_id, SemIR::InstId value_id,
  160. bool diagnose = true) -> TypeExpr;
  161. // Converts an expression for use as a form. If the expression is a type
  162. // expression, it is interpreted as an initializing form.
  163. auto ExprAsReturnForm(Context& context, SemIR::LocId loc_id,
  164. SemIR::InstId value_id) -> Context::FormExpr;
  165. // Handles an expression whose result value is unused.
  166. auto DiscardExpr(Context& context, SemIR::InstId expr_id) -> void;
  167. } // namespace Carbon::Check
  168. #endif // CARBON_TOOLCHAIN_CHECK_CONVERT_H_