handle_expr_category.cpp 3.2 KB

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