convert.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/parse/tree.h"
  9. #include "toolchain/sem_ir/node.h"
  10. namespace Carbon::Check {
  11. // Description of the target of a conversion.
  12. struct ConversionTarget {
  13. enum Kind {
  14. // Convert to a value of type `type`.
  15. Value,
  16. // Convert to either a value or a reference of type `type`.
  17. ValueOrReference,
  18. // The result of the conversion is discarded. It can't be an initializing
  19. // expression, but can be anything else.
  20. Discarded,
  21. // Convert to an initializer for the object denoted by `init_id`.
  22. Initializer,
  23. // Convert to an initializer for the object denoted by `init_id`,
  24. // including a final destination store if needed.
  25. FullInitializer,
  26. Last = FullInitializer
  27. };
  28. // The kind of the target for this conversion.
  29. Kind kind;
  30. // The target type for the conversion.
  31. SemIR::TypeId type_id;
  32. // For an initializer, the object being initialized.
  33. SemIR::NodeId init_id = SemIR::NodeId::Invalid;
  34. // For an initializer, a block of pending instructions that are needed to
  35. // form the value of `target_id`, and that can be discarded if no
  36. // initialization is needed.
  37. PendingBlock* init_block = nullptr;
  38. // Are we converting this value into an initializer for an object?
  39. bool is_initializer() const {
  40. return kind == Initializer || kind == FullInitializer;
  41. }
  42. };
  43. // Convert a value to another type and expression category.
  44. auto Convert(Context& context, Parse::Node parse_node, SemIR::NodeId value_id,
  45. ConversionTarget target) -> SemIR::NodeId;
  46. // Performs initialization of `target_id` from `value_id`. Returns the
  47. // possibly-converted initializing expression, which should be assigned to the
  48. // target using a suitable node for the kind of initialization.
  49. auto Initialize(Context& context, Parse::Node parse_node,
  50. SemIR::NodeId target_id, SemIR::NodeId value_id)
  51. -> SemIR::NodeId;
  52. // Convert the given expression to a value expression of the same type.
  53. auto ConvertToValueExpression(Context& context, SemIR::NodeId expr_id)
  54. -> SemIR::NodeId;
  55. // Convert the given expression to a value or reference expression of the same
  56. // type.
  57. auto ConvertToValueOrReferenceExpression(Context& context,
  58. SemIR::NodeId expr_id)
  59. -> SemIR::NodeId;
  60. // Converts `value_id` to a value expression of type `type_id`.
  61. auto ConvertToValueOfType(Context& context, Parse::Node parse_node,
  62. SemIR::NodeId value_id, SemIR::TypeId type_id)
  63. -> SemIR::NodeId;
  64. // Converts `value_id` to a value expression of type `bool`.
  65. auto ConvertToBoolValue(Context& context, Parse::Node parse_node,
  66. SemIR::NodeId value_id) -> SemIR::NodeId;
  67. // Implicitly converts a set of arguments to match the parameter types in a
  68. // function call.
  69. auto ConvertCallArgs(Context& context, Parse::Node call_parse_node,
  70. SemIR::NodeBlockId arg_refs_id,
  71. Parse::Node param_parse_node,
  72. SemIR::NodeBlockId param_refs_id, bool has_return_slot)
  73. -> bool;
  74. // Converts an expression for use as a type.
  75. auto ExpressionAsType(Context& context, Parse::Node parse_node,
  76. SemIR::NodeId value_id) -> SemIR::TypeId;
  77. } // namespace Carbon::Check
  78. #endif // CARBON_TOOLCHAIN_CHECK_CONVERT_H_