handle_expr_category.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. switch (auto rep = SemIR::ValueRepr::ForType(context.sem_ir(), inst.type_id);
  11. rep.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_id)));
  21. break;
  22. case SemIR::ValueRepr::Copy:
  23. context.SetLocal(inst_id, context.builder().CreateLoad(
  24. context.GetType(inst.type_id),
  25. 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::Temporary inst) -> void {
  36. context.FinishInit(inst.type_id, inst.storage_id, inst.init_id);
  37. context.SetLocal(inst_id, context.GetValue(inst.storage_id));
  38. }
  39. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  40. SemIR::TemporaryStorage inst) -> void {
  41. context.SetLocal(inst_id,
  42. context.builder().CreateAlloca(context.GetType(inst.type_id),
  43. nullptr, "temp"));
  44. }
  45. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  46. SemIR::ValueAsRef inst) -> void {
  47. CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.value_id) ==
  48. SemIR::ExprCategory::Value);
  49. CARBON_CHECK(SemIR::ValueRepr::ForType(context.sem_ir(), inst.type_id).kind ==
  50. SemIR::ValueRepr::Pointer);
  51. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  52. }
  53. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  54. SemIR::ValueOfInitializer inst) -> void {
  55. CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.init_id) ==
  56. SemIR::ExprCategory::Initializing);
  57. CARBON_CHECK(SemIR::ValueRepr::ForType(context.sem_ir(), inst.type_id).kind ==
  58. SemIR::ValueRepr::Copy);
  59. CARBON_CHECK(SemIR::InitRepr::ForType(context.sem_ir(), inst.type_id).kind ==
  60. SemIR::InitRepr::ByCopy);
  61. context.SetLocal(inst_id, context.GetValue(inst.init_id));
  62. }
  63. } // namespace Carbon::Lower