handle_expr_category.cpp 4.4 KB

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