convert.h 4.4 KB

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