convert.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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`.
  15. Value,
  16. // Convert to either a value or a reference of type `type`.
  17. ValueOrRef,
  18. // Convert for an explicit `as` cast. This allows any expression category
  19. // as the result, and uses the `As` interface instead of the `ImplicitAs`
  20. // interface.
  21. // TODO: Use of an interface for conversions is not yet supported.
  22. ExplicitAs,
  23. // The result of the conversion is discarded. It can't be an initializing
  24. // expression, but can be anything else.
  25. Discarded,
  26. // Convert to an initializer for the object denoted by `init_id`.
  27. Initializer,
  28. // Convert to an initializer for the object denoted by `init_id`,
  29. // including a final destination store if needed.
  30. FullInitializer,
  31. Last = FullInitializer
  32. };
  33. // The kind of the target for this conversion.
  34. Kind kind;
  35. // The target type for the conversion.
  36. SemIR::TypeId type_id;
  37. // For an initializer, the object being initialized.
  38. SemIR::InstId init_id = SemIR::InstId::Invalid;
  39. // For an initializer, a block of pending instructions that are needed to
  40. // form the value of `init_id`, and that can be discarded if no
  41. // initialization is needed.
  42. PendingBlock* init_block = nullptr;
  43. // Are we converting this value into an initializer for an object?
  44. auto is_initializer() const -> bool {
  45. return kind == Initializer || kind == FullInitializer;
  46. }
  47. };
  48. // Convert a value to another type and expression category.
  49. auto Convert(Context& context, SemIR::LocId loc_id, SemIR::InstId expr_id,
  50. ConversionTarget target) -> SemIR::InstId;
  51. // Performs initialization of `target_id` from `value_id`. Returns the
  52. // possibly-converted initializing expression, which should be assigned to the
  53. // target using a suitable node for the kind of initialization.
  54. auto Initialize(Context& context, SemIR::LocId loc_id, SemIR::InstId target_id,
  55. SemIR::InstId value_id) -> SemIR::InstId;
  56. // Convert the given expression to a value expression of the same type.
  57. auto ConvertToValueExpr(Context& context, SemIR::InstId expr_id)
  58. -> SemIR::InstId;
  59. // Convert the given expression to a value or reference expression of the same
  60. // type.
  61. auto ConvertToValueOrRefExpr(Context& context, SemIR::InstId expr_id)
  62. -> SemIR::InstId;
  63. // Converts `expr_id` to a value expression of type `type_id`.
  64. auto ConvertToValueOfType(Context& context, SemIR::LocId loc_id,
  65. SemIR::InstId expr_id, SemIR::TypeId type_id)
  66. -> SemIR::InstId;
  67. // Convert the given expression to a value or reference expression of the given
  68. // type.
  69. auto ConvertToValueOrRefOfType(Context& context, SemIR::LocId loc_id,
  70. SemIR::InstId expr_id, SemIR::TypeId type_id)
  71. -> SemIR::InstId;
  72. // Converts `value_id` to a value expression of type `bool`.
  73. auto ConvertToBoolValue(Context& context, SemIR::LocId loc_id,
  74. SemIR::InstId value_id) -> SemIR::InstId;
  75. // Converts `value_id` to type `type_id` for an `as` expression.
  76. auto ConvertForExplicitAs(Context& context, Parse::NodeId as_node,
  77. SemIR::InstId value_id, SemIR::TypeId type_id)
  78. -> SemIR::InstId;
  79. // Information about the parameters of a callee. This information is extracted
  80. // from the EntityWithParamsBase before calling ConvertCallArgs, because
  81. // conversion can trigger importing of more entities, which can invalidate the
  82. // reference to the callee.
  83. struct CalleeParamsInfo {
  84. explicit CalleeParamsInfo(const SemIR::EntityWithParamsBase& callee)
  85. : callee_loc(callee.latest_decl_id()),
  86. implicit_param_refs_id(callee.implicit_param_refs_id),
  87. param_refs_id(callee.param_refs_id) {}
  88. // The location of the callee to use in diagnostics.
  89. SemIRLoc callee_loc;
  90. // The implicit parameters of the callee.
  91. SemIR::InstBlockId implicit_param_refs_id;
  92. // The explicit parameters of the callee.
  93. SemIR::InstBlockId param_refs_id;
  94. };
  95. // Implicitly converts a set of arguments to match the parameter types in a
  96. // function call. Returns a block containing the converted implicit and explicit
  97. // argument values for runtime parameters.
  98. auto ConvertCallArgs(Context& context, SemIR::LocId call_loc_id,
  99. SemIR::InstId self_id,
  100. llvm::ArrayRef<SemIR::InstId> arg_refs,
  101. SemIR::InstId return_storage_id,
  102. const CalleeParamsInfo& callee,
  103. SemIR::SpecificId callee_specific_id)
  104. -> SemIR::InstBlockId;
  105. // Converts an expression for use as a type.
  106. auto ExprAsType(Context& context, SemIR::LocId loc_id, SemIR::InstId value_id)
  107. -> SemIR::TypeId;
  108. } // namespace Carbon::Check
  109. #endif // CARBON_TOOLCHAIN_CHECK_CONVERT_H_