handle_aggregates.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 "llvm/ADT/STLExtras.h"
  5. #include "llvm/ADT/StringRef.h"
  6. #include "llvm/ADT/Twine.h"
  7. #include "llvm/IR/Constants.h"
  8. #include "llvm/IR/Value.h"
  9. #include "toolchain/lower/aggregate.h"
  10. #include "toolchain/lower/function_context.h"
  11. #include "toolchain/sem_ir/expr_info.h"
  12. #include "toolchain/sem_ir/file.h"
  13. #include "toolchain/sem_ir/ids.h"
  14. #include "toolchain/sem_ir/inst.h"
  15. #include "toolchain/sem_ir/typed_insts.h"
  16. namespace Carbon::Lower {
  17. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  18. SemIR::ClassDecl /*inst*/) -> void {
  19. // No action to perform.
  20. }
  21. static auto GetStructFieldName(FunctionContext::TypeInFile struct_type,
  22. SemIR::ElementIndex index) -> llvm::StringRef {
  23. auto struct_type_inst = struct_type.file->types().GetAs<SemIR::AnyStructType>(
  24. struct_type.type_id);
  25. auto fields =
  26. struct_type.file->struct_type_fields().Get(struct_type_inst.fields_id);
  27. // We intentionally don't add this to the fingerprint because it's only used
  28. // as an instruction name, and so doesn't affect the semantics of the IR.
  29. return struct_type.file->names().GetIRBaseName(fields[index.index].name_id);
  30. }
  31. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  32. SemIR::ClassElementAccess inst) -> void {
  33. // Find the class that we're performing access into.
  34. auto class_type = context.GetTypeIdOfInst(inst.base_id);
  35. auto object_repr = FunctionContext::TypeInFile{
  36. .file = class_type.file,
  37. .type_id = class_type.file->types().GetObjectRepr(class_type.type_id)};
  38. // Translate the class field access into a struct access on the object
  39. // representation.
  40. context.SetLocal(inst_id, GetAggregateElement(
  41. context, inst.base_id, inst.index, inst_id,
  42. GetStructFieldName(object_repr, inst.index)));
  43. }
  44. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  45. SemIR::ClassInit inst) -> void {
  46. context.SetLocal(inst_id,
  47. EmitAggregateInitializer(context, inst_id, inst.elements_id,
  48. "class.init"));
  49. }
  50. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  51. SemIR::StructAccess inst) -> void {
  52. auto struct_type = context.GetTypeIdOfInst(inst.struct_id);
  53. context.SetLocal(inst_id, GetAggregateElement(
  54. context, inst.struct_id, inst.index, inst_id,
  55. GetStructFieldName(struct_type, inst.index)));
  56. }
  57. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  58. SemIR::StructLiteral /*inst*/) -> void {
  59. // A StructLiteral should always be converted to a StructInit or StructValue
  60. // if its value is needed.
  61. }
  62. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  63. SemIR::StructInit inst) -> void {
  64. context.SetLocal(inst_id,
  65. EmitAggregateInitializer(context, inst_id, inst.elements_id,
  66. "struct.init"));
  67. }
  68. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  69. SemIR::StructValue inst) -> void {
  70. auto type = context.GetTypeIdOfInst(inst_id);
  71. if (auto fn_type =
  72. type.file->types().TryGetAs<SemIR::FunctionType>(type.type_id)) {
  73. context.SetLocal(inst_id, context.GetFileContext(type.file).GetFunction(
  74. fn_type->function_id));
  75. return;
  76. }
  77. context.SetLocal(inst_id,
  78. EmitAggregateValueRepr(context, inst_id, inst.elements_id));
  79. }
  80. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  81. SemIR::TupleAccess inst) -> void {
  82. context.SetLocal(
  83. inst_id, GetAggregateElement(context, inst.tuple_id, inst.index, inst_id,
  84. "tuple.elem"));
  85. }
  86. auto HandleInst(FunctionContext& /*context*/, SemIR::InstId /*inst_id*/,
  87. SemIR::TupleLiteral /*inst*/) -> void {
  88. // A TupleLiteral should always be converted to a TupleInit or TupleValue if
  89. // its value is needed.
  90. }
  91. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  92. SemIR::TupleInit inst) -> void {
  93. context.SetLocal(inst_id,
  94. EmitAggregateInitializer(context, inst_id, inst.elements_id,
  95. "tuple.init"));
  96. }
  97. auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
  98. SemIR::TupleValue inst) -> void {
  99. context.SetLocal(inst_id,
  100. EmitAggregateValueRepr(context, inst_id, inst.elements_id));
  101. }
  102. } // namespace Carbon::Lower