handle_expr_category.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. auto* type = context.GetType(SemIR::GetTypeOfInstInSpecific(
  24. context.sem_ir(), context.specific_id(), inst_id));
  25. context.SetLocal(inst_id, context.builder().CreateLoad(
  26. type, context.GetValue(inst.value_id)));
  27. } break;
  28. case SemIR::ValueRepr::Pointer:
  29. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  30. break;
  31. case SemIR::ValueRepr::Custom:
  32. CARBON_FATAL("TODO: Add support for BindValue with custom value rep");
  33. }
  34. }
  35. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  36. SemIR::Temporary inst) -> void {
  37. context.FinishInit(inst.type_id, inst.storage_id, inst.init_id);
  38. context.SetLocal(inst_id, context.GetValue(inst.storage_id));
  39. }
  40. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  41. SemIR::TemporaryStorage /*inst*/) -> void {
  42. auto* type = context.GetType(SemIR::GetTypeOfInstInSpecific(
  43. context.sem_ir(), context.specific_id(), inst_id));
  44. context.SetLocal(inst_id,
  45. context.builder().CreateAlloca(type, nullptr, "temp"));
  46. }
  47. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  48. SemIR::ValueAsRef inst) -> void {
  49. CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.value_id) ==
  50. SemIR::ExprCategory::Value);
  51. CARBON_CHECK(SemIR::ValueRepr::ForType(context.sem_ir(), inst.type_id).kind ==
  52. SemIR::ValueRepr::Pointer);
  53. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  54. }
  55. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  56. SemIR::ValueOfInitializer inst) -> void {
  57. CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.init_id) ==
  58. SemIR::ExprCategory::Initializing);
  59. CARBON_CHECK(SemIR::ValueRepr::ForType(context.sem_ir(), inst.type_id).kind ==
  60. SemIR::ValueRepr::Copy);
  61. CARBON_CHECK(SemIR::InitRepr::ForType(context.sem_ir(), inst.type_id).kind ==
  62. SemIR::InitRepr::ByCopy);
  63. context.SetLocal(inst_id, context.GetValue(inst.init_id));
  64. }
  65. } // namespace Carbon::Lower