convert.h 9.8 KB

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