handle_expr_category.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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::AcquireValue 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 AcquireValue with custom value rep");
  35. }
  36. }
  37. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  38. SemIR::MarkInPlaceInit inst) -> void {
  39. context.SetLocal(inst_id, context.GetValue(inst.dest_id));
  40. }
  41. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  42. SemIR::Temporary inst) -> void {
  43. if (SemIR::GetExprCategory(context.sem_ir(), inst.init_id) !=
  44. SemIR::ExprCategory::InPlaceInitializing) {
  45. context.InitializeStorage(context.GetTypeIdOfInst(inst_id), inst.storage_id,
  46. inst.init_id);
  47. }
  48. context.SetLocal(inst_id, context.GetValue(inst.storage_id));
  49. }
  50. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  51. SemIR::TemporaryStorage /*inst*/) -> void {
  52. context.SetLocal(
  53. inst_id, context.CreateAlloca(context.GetTypeOfInst(inst_id), "temp"));
  54. }
  55. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  56. SemIR::ValueAsRef inst) -> void {
  57. CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.value_id) ==
  58. SemIR::ExprCategory::Value);
  59. auto inst_type = context.GetTypeIdOfInst(inst_id);
  60. auto value_repr = context.GetValueRepr(inst_type);
  61. CARBON_CHECK(value_repr.repr.kind == SemIR::ValueRepr::Pointer);
  62. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  63. }
  64. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  65. SemIR::ValueOfInitializer inst) -> void {
  66. CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.init_id) ==
  67. SemIR::ExprCategory::ReprInitializing);
  68. auto inst_type = context.GetTypeIdOfInst(inst_id);
  69. auto value_repr = context.GetValueRepr(inst_type);
  70. auto init_repr = context.GetInitRepr(inst_type);
  71. CARBON_CHECK(value_repr.repr.kind == SemIR::ValueRepr::Copy);
  72. CARBON_CHECK(init_repr.kind == SemIR::InitRepr::ByCopy);
  73. context.SetLocal(inst_id, context.GetValue(inst.init_id));
  74. }
  75. } // namespace Carbon::Lower