convert.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 initializer for the object denoted by `init_id`.
  44. Initializer,
  45. // Convert to an initializer for the object denoted by `init_id`,
  46. // including a final destination store if needed.
  47. FullInitializer,
  48. Last = FullInitializer
  49. };
  50. // The kind of the target for this conversion.
  51. Kind kind;
  52. // The target type for the conversion.
  53. SemIR::TypeId type_id;
  54. // For an initializer, the object being initialized.
  55. SemIR::InstId init_id = SemIR::InstId::None;
  56. // For an initializer, a block of pending instructions that are needed to
  57. // form the value of `init_id`, and that can be discarded if no
  58. // initialization is needed.
  59. PendingBlock* init_block = nullptr;
  60. // Whether failure of conversion is an error and is diagnosed to the user.
  61. // When looking for a possible conversion but with graceful fallback, diagnose
  62. // should be false.
  63. bool diagnose = true;
  64. // Are we converting this value into an initializer for an object?
  65. auto is_initializer() const -> bool {
  66. return kind == Initializer || kind == FullInitializer;
  67. }
  68. // Is this some kind of explicit `as` conversion?
  69. auto is_explicit_as() const -> bool {
  70. return kind == ExplicitAs || kind == ExplicitUnsafeAs;
  71. }
  72. };
  73. // Convert a value to another type and expression category.
  74. // TODO: The `vtable_id` parameter is too much of a special case here, and
  75. // should be removed - once partial classes are implemented, the vtable pointer
  76. // initialization will be done not in this conversion, but during initialization
  77. // of the object of non-partial class type from the object of partial class
  78. // type.
  79. auto Convert(Context& context, SemIR::LocId loc_id, SemIR::InstId expr_id,
  80. ConversionTarget target,
  81. SemIR::ClassType* vtable_class_type = nullptr) -> SemIR::InstId;
  82. // Performs initialization of `target_id` from `value_id`. Returns the
  83. // possibly-converted initializing expression, which should be assigned to the
  84. // target using a suitable node for the kind of initialization.
  85. auto Initialize(Context& context, SemIR::LocId loc_id, SemIR::InstId target_id,
  86. SemIR::InstId value_id) -> SemIR::InstId;
  87. // Convert the given expression to a value expression of the same type.
  88. auto ConvertToValueExpr(Context& context, SemIR::InstId expr_id)
  89. -> SemIR::InstId;
  90. // Convert the given expression to a value or reference expression of the same
  91. // type.
  92. auto ConvertToValueOrRefExpr(Context& context, SemIR::InstId expr_id)
  93. -> SemIR::InstId;
  94. // Converts `expr_id` to a value expression of type `type_id`.
  95. auto ConvertToValueOfType(Context& context, SemIR::LocId loc_id,
  96. SemIR::InstId expr_id, SemIR::TypeId type_id)
  97. -> SemIR::InstId;
  98. // Convert the given expression to a value or reference expression of the given
  99. // type.
  100. auto ConvertToValueOrRefOfType(Context& context, SemIR::LocId loc_id,
  101. SemIR::InstId expr_id, SemIR::TypeId type_id)
  102. -> SemIR::InstId;
  103. // Attempted to convert `expr_id` to a value expression of type `type_id`, with
  104. // graceful failure, which does not result in diagnostics. An ErrorInst
  105. // instruction is still returned on failure.
  106. auto TryConvertToValueOfType(Context& context, SemIR::LocId loc_id,
  107. SemIR::InstId expr_id, SemIR::TypeId type_id)
  108. -> SemIR::InstId;
  109. // Converts `value_id` to a value expression of type `bool`.
  110. auto ConvertToBoolValue(Context& context, SemIR::LocId loc_id,
  111. SemIR::InstId value_id) -> SemIR::InstId;
  112. // Converts `value_id` to type `type_id` for an `as` expression.
  113. auto ConvertForExplicitAs(Context& context, Parse::NodeId as_node,
  114. SemIR::InstId value_id, SemIR::TypeId type_id,
  115. bool unsafe) -> SemIR::InstId;
  116. // Implicitly converts a set of arguments to match the parameter types in a
  117. // function call. Returns a block containing the converted implicit and explicit
  118. // argument values for runtime parameters. `is_operator_syntax` indicates that
  119. // this call was generated from an operator rather than from function call
  120. // syntax, so arguments to `ref` parameters aren't required to have `ref` tags.
  121. auto ConvertCallArgs(Context& context, SemIR::LocId call_loc_id,
  122. SemIR::InstId self_id,
  123. llvm::ArrayRef<SemIR::InstId> arg_refs,
  124. SemIR::InstId return_slot_arg_id,
  125. const SemIR::Function& callee,
  126. SemIR::SpecificId callee_specific_id,
  127. bool is_operator_syntax) -> SemIR::InstBlockId;
  128. // A type that has been converted for use as a type expression.
  129. struct TypeExpr {
  130. static const TypeExpr None;
  131. // Returns a TypeExpr describing a type with no associated spelling or type
  132. // sugar.
  133. static auto ForUnsugared(Context& context, SemIR::TypeId type_id) -> TypeExpr;
  134. // The converted expression of type `type`, or `ErrorInst::InstId`.
  135. SemIR::TypeInstId inst_id;
  136. // The corresponding type, or `ErrorInst::TypeId`.
  137. SemIR::TypeId type_id;
  138. };
  139. inline constexpr TypeExpr TypeExpr::None = {.inst_id = SemIR::TypeInstId::None,
  140. .type_id = SemIR::TypeId::None};
  141. // Converts an expression for use as a type.
  142. //
  143. // If `diagnose` is true, errors are diagnosed to the user. Set it to false when
  144. // looking to see if a conversion is possible but with graceful fallback.
  145. //
  146. // TODO: Most of the callers of this function discard the `inst_id` and lose
  147. // track of the conversion. In most cases we should be retaining that as the
  148. // operand of some downstream instruction.
  149. auto ExprAsType(Context& context, SemIR::LocId loc_id, SemIR::InstId value_id,
  150. bool diagnose = true) -> TypeExpr;
  151. // Converts an expression for use as a form. If the expression is a type
  152. // expression, it is interpreted as an initializing form.
  153. auto ExprAsReturnForm(Context& context, SemIR::LocId loc_id,
  154. SemIR::InstId value_id) -> Context::FormExpr;
  155. // Handles an expression whose result value is unused.
  156. auto DiscardExpr(Context& context, SemIR::InstId expr_id) -> void;
  157. } // namespace Carbon::Check
  158. #endif // CARBON_TOOLCHAIN_CHECK_CONVERT_H_