handle_struct.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "common/map.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/convert.h"
  7. #include "toolchain/check/handle.h"
  8. #include "toolchain/check/inst.h"
  9. #include "toolchain/check/type.h"
  10. #include "toolchain/diagnostics/format_providers.h"
  11. namespace Carbon::Check {
  12. auto HandleParseNode(Context& context, Parse::StructLiteralStartId node_id)
  13. -> bool {
  14. context.scope_stack().PushForSameRegion();
  15. context.node_stack().Push(node_id);
  16. context.struct_type_fields_stack().PushArray();
  17. context.param_and_arg_refs_stack().Push();
  18. return true;
  19. }
  20. auto HandleParseNode(Context& context, Parse::StructTypeLiteralStartId node_id)
  21. -> bool {
  22. context.scope_stack().PushForSameRegion();
  23. context.node_stack().Push(node_id);
  24. context.struct_type_fields_stack().PushArray();
  25. return true;
  26. }
  27. auto HandleParseNode(Context& context,
  28. Parse::StructFieldDesignatorId /*node_id*/) -> bool {
  29. // This leaves the designated name on top because the `.` isn't interesting.
  30. CARBON_CHECK(context.node_stack().PeekIs<SemIR::NameId>());
  31. return true;
  32. }
  33. auto HandleParseNode(Context& context, Parse::StructLiteralCommaId /*node_id*/)
  34. -> bool {
  35. context.param_and_arg_refs_stack().ApplyComma();
  36. return true;
  37. }
  38. auto HandleParseNode(Context& /*context*/,
  39. Parse::StructTypeLiteralCommaId /*node_id*/) -> bool {
  40. return true;
  41. }
  42. auto HandleParseNode(Context& context, Parse::StructLiteralFieldId node_id)
  43. -> bool {
  44. auto value_inst_id = context.node_stack().PopExpr();
  45. // Get the name while leaving it on the stack.
  46. auto name_id = context.node_stack().Peek<Parse::NodeCategory::MemberName>();
  47. // Store the name for the type.
  48. auto value_type_inst_id =
  49. context.types().GetInstId(context.insts().Get(value_inst_id).type_id());
  50. context.struct_type_fields_stack().AppendToTop(
  51. {.name_id = name_id, .type_inst_id = value_type_inst_id});
  52. // Push the value back on the stack as an argument.
  53. context.node_stack().Push(node_id, value_inst_id);
  54. return true;
  55. }
  56. auto HandleParseNode(Context& context,
  57. Parse::StructTypeLiteralFieldId /*node_id*/) -> bool {
  58. auto [type_node, type_id] = context.node_stack().PopExprWithNodeId();
  59. auto cast_type_inst_id = ExprAsType(context, type_node, type_id).inst_id;
  60. // Get the name while leaving it on the stack.
  61. auto name_id = context.node_stack().Peek<Parse::NodeCategory::MemberName>();
  62. context.struct_type_fields_stack().AppendToTop(
  63. {.name_id = name_id, .type_inst_id = cast_type_inst_id});
  64. return true;
  65. }
  66. // Diagnoses and returns true if there's a duplicate name.
  67. static auto DiagnoseDuplicateNames(
  68. Context& context, llvm::ArrayRef<Parse::NodeId> field_name_nodes,
  69. llvm::ArrayRef<SemIR::StructTypeField> fields, bool is_struct_type_literal)
  70. -> bool {
  71. Map<SemIR::NameId, Parse::NodeId> names;
  72. for (auto [field_name_node, field] : llvm::zip(field_name_nodes, fields)) {
  73. auto result = names.Insert(field.name_id, field_name_node);
  74. if (!result.is_inserted()) {
  75. CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
  76. "duplicated field name `{1}` in "
  77. "{0:struct type literal|struct literal}",
  78. Diagnostics::BoolAsSelect, SemIR::NameId);
  79. CARBON_DIAGNOSTIC(StructNamePrevious, Note,
  80. "field with the same name here");
  81. context.emitter()
  82. .Build(result.value(), StructNameDuplicate, is_struct_type_literal,
  83. field.name_id)
  84. .Note(field_name_node, StructNamePrevious)
  85. .Emit();
  86. return true;
  87. }
  88. }
  89. return false;
  90. }
  91. // Pops the names of each field from the stack. These will have been left while
  92. // handling struct fields.
  93. static auto PopFieldNameNodes(Context& context, size_t field_count)
  94. -> llvm::SmallVector<Parse::NodeId> {
  95. llvm::SmallVector<Parse::NodeId> nodes;
  96. nodes.reserve(field_count);
  97. while (true) {
  98. auto [name_node, _] =
  99. context.node_stack().PopWithNodeIdIf<Parse::NodeCategory::MemberName>();
  100. if (name_node.has_value()) {
  101. nodes.push_back(name_node);
  102. } else {
  103. break;
  104. }
  105. }
  106. CARBON_CHECK(nodes.size() == field_count, "Found {0} names, expected {1}",
  107. nodes.size(), field_count);
  108. return nodes;
  109. }
  110. auto HandleParseNode(Context& context, Parse::StructLiteralId node_id) -> bool {
  111. if (!context.node_stack().PeekIs(Parse::NodeCategory::MemberName)) {
  112. // Remove the last parameter from the node stack before collecting names.
  113. context.param_and_arg_refs_stack().EndNoPop(
  114. Parse::NodeKind::StructLiteralStart);
  115. }
  116. auto fields = context.struct_type_fields_stack().PeekArray();
  117. llvm::SmallVector<Parse::NodeId> field_name_nodes =
  118. PopFieldNameNodes(context, fields.size());
  119. auto elements_id = context.param_and_arg_refs_stack().EndAndPop(
  120. Parse::NodeKind::StructLiteralStart);
  121. context.scope_stack().Pop();
  122. context.node_stack()
  123. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructLiteralStart>();
  124. if (DiagnoseDuplicateNames(context, field_name_nodes, fields,
  125. /*is_struct_type_literal=*/false)) {
  126. context.node_stack().Push(node_id, SemIR::ErrorInst::InstId);
  127. } else {
  128. auto type_id = GetStructType(
  129. context, context.struct_type_fields().AddCanonical(fields));
  130. auto value_id = AddInst<SemIR::StructLiteral>(
  131. context, node_id, {.type_id = type_id, .elements_id = elements_id});
  132. context.node_stack().Push(node_id, value_id);
  133. }
  134. context.struct_type_fields_stack().PopArray();
  135. return true;
  136. }
  137. auto HandleParseNode(Context& context, Parse::StructTypeLiteralId node_id)
  138. -> bool {
  139. auto fields = context.struct_type_fields_stack().PeekArray();
  140. llvm::SmallVector<Parse::NodeId> field_name_nodes =
  141. PopFieldNameNodes(context, fields.size());
  142. context.scope_stack().Pop();
  143. context.node_stack()
  144. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructTypeLiteralStart>();
  145. if (DiagnoseDuplicateNames(context, field_name_nodes, fields,
  146. /*is_struct_type_literal=*/true)) {
  147. context.node_stack().Push(node_id, SemIR::ErrorInst::InstId);
  148. } else {
  149. auto fields_id = context.struct_type_fields().AddCanonical(fields);
  150. AddInstAndPush<SemIR::StructType>(
  151. context, node_id,
  152. {.type_id = SemIR::TypeType::TypeId, .fields_id = fields_id});
  153. }
  154. context.struct_type_fields_stack().PopArray();
  155. return true;
  156. }
  157. } // namespace Carbon::Check