handle_expression_category.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 HandleBindValue(FunctionContext& context, SemIR::InstId inst_id,
  8. SemIR::BindValue inst) -> void {
  9. switch (auto rep =
  10. SemIR::GetValueRepresentation(context.sem_ir(), inst.type_id);
  11. rep.kind) {
  12. case SemIR::ValueRepresentation::Unknown:
  13. CARBON_FATAL()
  14. << "Value binding for type with incomplete value representation";
  15. case SemIR::ValueRepresentation::None:
  16. // Nothing should use this value, but StubReference needs a value to
  17. // propagate.
  18. // TODO: Remove this now the StubReferences are gone.
  19. context.SetLocal(inst_id,
  20. llvm::PoisonValue::get(context.GetType(inst.type_id)));
  21. break;
  22. case SemIR::ValueRepresentation::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::ValueRepresentation::Pointer:
  28. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  29. break;
  30. case SemIR::ValueRepresentation::Custom:
  31. CARBON_FATAL() << "TODO: Add support for BindValue with custom value rep";
  32. }
  33. }
  34. auto HandleTemporary(FunctionContext& context, SemIR::InstId inst_id,
  35. SemIR::Temporary inst) -> void {
  36. context.FinishInitialization(inst.type_id, inst.storage_id, inst.init_id);
  37. context.SetLocal(inst_id, context.GetValue(inst.storage_id));
  38. }
  39. auto HandleTemporaryStorage(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 HandleValueAsReference(FunctionContext& context, SemIR::InstId inst_id,
  46. SemIR::ValueAsReference inst) -> void {
  47. CARBON_CHECK(SemIR::GetExpressionCategory(context.sem_ir(), inst.value_id) ==
  48. SemIR::ExpressionCategory::Value);
  49. CARBON_CHECK(
  50. SemIR::GetValueRepresentation(context.sem_ir(), inst.type_id).kind ==
  51. SemIR::ValueRepresentation::Pointer);
  52. context.SetLocal(inst_id, context.GetValue(inst.value_id));
  53. }
  54. auto HandleValueOfInitializer(FunctionContext& context, SemIR::InstId inst_id,
  55. SemIR::ValueOfInitializer inst) -> void {
  56. CARBON_CHECK(SemIR::GetExpressionCategory(context.sem_ir(), inst.init_id) ==
  57. SemIR::ExpressionCategory::Initializing);
  58. CARBON_CHECK(
  59. SemIR::GetValueRepresentation(context.sem_ir(), inst.type_id).kind ==
  60. SemIR::ValueRepresentation::Copy);
  61. CARBON_CHECK(
  62. SemIR::GetInitializingRepresentation(context.sem_ir(), inst.type_id)
  63. .kind == SemIR::InitializingRepresentation::ByCopy);
  64. context.SetLocal(inst_id, context.GetValue(inst.init_id));
  65. }
  66. } // namespace Carbon::Lower