handle_expr_category.cpp 3.0 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. 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 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)));
  21. break;
  22. case SemIR::ValueRepr::Copy: {
  23. auto* type = context.GetType(inst_type);
  24. context.SetLocal(inst_id, context.builder().CreateLoad(
  25. type, 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(context.GetTypeIdOfInst(inst_id), inst.storage_id,
  37. 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. context.SetLocal(
  43. inst_id, context.CreateAlloca(context.GetTypeOfInst(inst_id), "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. auto inst_type = context.GetTypeIdOfInst(inst_id);
  50. auto value_repr = context.GetValueRepr(inst_type);
  51. CARBON_CHECK(value_repr.repr.kind == SemIR::ValueRepr::Pointer);
  52. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  53. }
  54. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  55. SemIR::ValueOfInitializer inst) -> void {
  56. CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.init_id) ==
  57. SemIR::ExprCategory::Initializing);
  58. auto inst_type = context.GetTypeIdOfInst(inst_id);
  59. auto value_repr = context.GetValueRepr(inst_type);
  60. auto init_repr = context.GetInitRepr(inst_type);
  61. CARBON_CHECK(value_repr.repr.kind == SemIR::ValueRepr::Copy);
  62. CARBON_CHECK(init_repr.kind == SemIR::InitRepr::ByCopy);
  63. context.SetLocal(inst_id, context.GetValue(inst.init_id));
  64. }
  65. } // namespace Carbon::Lower