handle_expr_category.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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/file.h"
  6. namespace Carbon::Lower {
  7. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  8. SemIR::BindValue inst) -> void {
  9. switch (auto rep = SemIR::ValueRepr::ForType(context.sem_ir(), inst.type_id);
  10. rep.kind) {
  11. case SemIR::ValueRepr::Unknown:
  12. CARBON_FATAL()
  13. << "Value binding for type with incomplete value representation";
  14. case SemIR::ValueRepr::None:
  15. // Nothing should use this value, but StubRef needs a value to
  16. // propagate.
  17. // TODO: Remove this now the StubRefs are gone.
  18. context.SetLocal(inst_id,
  19. llvm::PoisonValue::get(context.GetType(inst.type_id)));
  20. break;
  21. case SemIR::ValueRepr::Copy:
  22. context.SetLocal(inst_id, context.builder().CreateLoad(
  23. context.GetType(inst.type_id),
  24. context.GetValue(inst.value_id)));
  25. break;
  26. case SemIR::ValueRepr::Pointer:
  27. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  28. break;
  29. case SemIR::ValueRepr::Custom:
  30. CARBON_FATAL() << "TODO: Add support for BindValue with custom value rep";
  31. }
  32. }
  33. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  34. SemIR::Temporary inst) -> void {
  35. context.FinishInit(inst.type_id, inst.storage_id, inst.init_id);
  36. context.SetLocal(inst_id, context.GetValue(inst.storage_id));
  37. }
  38. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  39. SemIR::TemporaryStorage inst) -> void {
  40. context.SetLocal(inst_id,
  41. context.builder().CreateAlloca(context.GetType(inst.type_id),
  42. nullptr, "temp"));
  43. }
  44. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  45. SemIR::ValueAsRef inst) -> void {
  46. CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.value_id) ==
  47. SemIR::ExprCategory::Value);
  48. CARBON_CHECK(SemIR::ValueRepr::ForType(context.sem_ir(), inst.type_id).kind ==
  49. SemIR::ValueRepr::Pointer);
  50. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  51. }
  52. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  53. SemIR::ValueOfInitializer inst) -> void {
  54. CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.init_id) ==
  55. SemIR::ExprCategory::Initializing);
  56. CARBON_CHECK(SemIR::ValueRepr::ForType(context.sem_ir(), inst.type_id).kind ==
  57. SemIR::ValueRepr::Copy);
  58. CARBON_CHECK(SemIR::InitRepr::ForType(context.sem_ir(), inst.type_id).kind ==
  59. SemIR::InitRepr::ByCopy);
  60. context.SetLocal(inst_id, context.GetValue(inst.init_id));
  61. }
  62. } // namespace Carbon::Lower