handle_expr_category.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "toolchain/lower/function_context.h"
  5. #include "toolchain/sem_ir/expr_info.h"
  6. #include "toolchain/sem_ir/file.h"
  7. namespace Carbon::Lower {
  8. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  9. SemIR::BindValue inst) -> void {
  10. auto inst_type = context.GetTypeIdOfInst(inst_id);
  11. switch (context.GetValueRepr(inst_type).repr.kind) {
  12. case SemIR::ValueRepr::Unknown:
  13. CARBON_FATAL(
  14. "Value binding for type with incomplete value representation");
  15. case SemIR::ValueRepr::None:
  16. // Nothing should use this value, but StubRef needs a value to
  17. // propagate.
  18. // TODO: Remove this now the StubRefs are gone.
  19. context.SetLocal(inst_id,
  20. llvm::PoisonValue::get(context.GetType(inst_type)));
  21. break;
  22. case SemIR::ValueRepr::Copy:
  23. context.SetLocal(
  24. inst_id,
  25. context.LoadObject(inst_type, context.GetValue(inst.value_id)));
  26. break;
  27. case SemIR::ValueRepr::Pointer:
  28. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  29. break;
  30. case SemIR::ValueRepr::Custom:
  31. CARBON_FATAL("TODO: Add support for BindValue with custom value rep");
  32. }
  33. }
  34. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  35. SemIR::InPlaceInit inst) -> void {
  36. auto type = context.GetTypeIdOfInst(inst_id);
  37. auto* value = context.GetValue(inst.dest_id);
  38. // If the initializing representation is by-value, and the value
  39. // representation is by-copy, then we need to load from the storage. Otherwise
  40. // we want a pointer to the result.
  41. switch (context.GetInitRepr(type).kind) {
  42. case SemIR::InitRepr::None:
  43. case SemIR::InitRepr::InPlace:
  44. break;
  45. case SemIR::InitRepr::ByCopy:
  46. switch (context.GetValueRepr(type).repr.kind) {
  47. case SemIR::ValueRepr::Unknown:
  48. CARBON_FATAL("Unexpected incomplete type");
  49. case SemIR::ValueRepr::None:
  50. case SemIR::ValueRepr::Pointer:
  51. break;
  52. case SemIR::ValueRepr::Copy:
  53. value = context.builder().CreateLoad(context.GetType(type), value);
  54. break;
  55. case SemIR::ValueRepr::Custom:
  56. CARBON_FATAL(
  57. "TODO: Add support for InPlaceInit with custom value rep");
  58. }
  59. break;
  60. case SemIR::InitRepr::Incomplete:
  61. CARBON_FATAL("Unexpected incomplete type");
  62. }
  63. context.SetLocal(inst_id, value);
  64. }
  65. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  66. SemIR::Temporary inst) -> void {
  67. context.FinishInit(context.GetTypeIdOfInst(inst_id), inst.storage_id,
  68. inst.init_id);
  69. context.SetLocal(inst_id, context.GetValue(inst.storage_id));
  70. }
  71. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  72. SemIR::TemporaryStorage /*inst*/) -> void {
  73. context.SetLocal(
  74. inst_id, context.CreateAlloca(context.GetTypeOfInst(inst_id), "temp"));
  75. }
  76. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  77. SemIR::ValueAsRef inst) -> void {
  78. CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.value_id) ==
  79. SemIR::ExprCategory::Value);
  80. auto inst_type = context.GetTypeIdOfInst(inst_id);
  81. auto value_repr = context.GetValueRepr(inst_type);
  82. CARBON_CHECK(value_repr.repr.kind == SemIR::ValueRepr::Pointer);
  83. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  84. }
  85. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  86. SemIR::ValueOfInitializer inst) -> void {
  87. CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.init_id) ==
  88. SemIR::ExprCategory::Initializing);
  89. auto inst_type = context.GetTypeIdOfInst(inst_id);
  90. auto value_repr = context.GetValueRepr(inst_type);
  91. auto init_repr = context.GetInitRepr(inst_type);
  92. CARBON_CHECK(value_repr.repr.kind == SemIR::ValueRepr::Copy);
  93. CARBON_CHECK(init_repr.kind == SemIR::InitRepr::ByCopy);
  94. context.SetLocal(inst_id, context.GetValue(inst.init_id));
  95. }
  96. } // namespace Carbon::Lower