convert.h 7.3 KB

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