handle_struct.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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().Push();
  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().Push();
  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. SemIR::InstId cast_type_inst_id =
  60. ExprAsType(context, type_node, type_id).inst_id;
  61. // Get the name while leaving it on the stack.
  62. auto name_id = context.node_stack().Peek<Parse::NodeCategory::MemberName>();
  63. context.struct_type_fields_stack().AppendToTop(
  64. {.name_id = name_id, .type_inst_id = cast_type_inst_id});
  65. return true;
  66. }
  67. // Diagnoses and returns true if there's a duplicate name.
  68. static auto DiagnoseDuplicateNames(
  69. Context& context, llvm::ArrayRef<Parse::NodeId> field_name_nodes,
  70. llvm::ArrayRef<SemIR::StructTypeField> fields, bool is_struct_type_literal)
  71. -> bool {
  72. Map<SemIR::NameId, Parse::NodeId> names;
  73. for (auto [field_name_node, field] : llvm::zip(field_name_nodes, fields)) {
  74. auto result = names.Insert(field.name_id, field_name_node);
  75. if (!result.is_inserted()) {
  76. CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
  77. "duplicated field name `{1}` in "
  78. "{0:struct type literal|struct literal}",
  79. Diagnostics::BoolAsSelect, SemIR::NameId);
  80. CARBON_DIAGNOSTIC(StructNamePrevious, Note,
  81. "field with the same name here");
  82. context.emitter()
  83. .Build(result.value(), StructNameDuplicate, is_struct_type_literal,
  84. field.name_id)
  85. .Note(field_name_node, StructNamePrevious)
  86. .Emit();
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. // Pops the names of each field from the stack. These will have been left while
  93. // handling struct fields.
  94. static auto PopFieldNameNodes(Context& context, size_t field_count)
  95. -> llvm::SmallVector<Parse::NodeId> {
  96. llvm::SmallVector<Parse::NodeId> nodes;
  97. nodes.reserve(field_count);
  98. while (true) {
  99. auto [name_node, _] =
  100. context.node_stack().PopWithNodeIdIf<Parse::NodeCategory::MemberName>();
  101. if (name_node.has_value()) {
  102. nodes.push_back(name_node);
  103. } else {
  104. break;
  105. }
  106. }
  107. CARBON_CHECK(nodes.size() == field_count, "Found {0} names, expected {1}",
  108. nodes.size(), field_count);
  109. return nodes;
  110. }
  111. auto HandleParseNode(Context& context, Parse::StructLiteralId node_id) -> bool {
  112. if (!context.node_stack().PeekIs(Parse::NodeCategory::MemberName)) {
  113. // Remove the last parameter from the node stack before collecting names.
  114. context.param_and_arg_refs_stack().EndNoPop(
  115. Parse::NodeKind::StructLiteralStart);
  116. }
  117. auto fields = context.struct_type_fields_stack().PeekArray();
  118. llvm::SmallVector<Parse::NodeId> field_name_nodes =
  119. PopFieldNameNodes(context, fields.size());
  120. auto elements_id = context.param_and_arg_refs_stack().EndAndPop(
  121. Parse::NodeKind::StructLiteralStart);
  122. context.scope_stack().Pop();
  123. context.node_stack()
  124. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructLiteralStart>();
  125. if (DiagnoseDuplicateNames(context, field_name_nodes, fields,
  126. /*is_struct_type_literal=*/false)) {
  127. context.node_stack().Push(node_id, SemIR::ErrorInst::SingletonInstId);
  128. } else {
  129. auto type_id = GetStructType(
  130. context, context.struct_type_fields().AddCanonical(fields));
  131. auto value_id = AddInst<SemIR::StructLiteral>(
  132. context, node_id, {.type_id = type_id, .elements_id = elements_id});
  133. context.node_stack().Push(node_id, value_id);
  134. }
  135. context.struct_type_fields_stack().PopArray();
  136. return true;
  137. }
  138. auto HandleParseNode(Context& context, Parse::StructTypeLiteralId node_id)
  139. -> bool {
  140. auto fields = context.struct_type_fields_stack().PeekArray();
  141. llvm::SmallVector<Parse::NodeId> field_name_nodes =
  142. PopFieldNameNodes(context, fields.size());
  143. context.scope_stack().Pop();
  144. context.node_stack()
  145. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructTypeLiteralStart>();
  146. if (DiagnoseDuplicateNames(context, field_name_nodes, fields,
  147. /*is_struct_type_literal=*/true)) {
  148. context.node_stack().Push(node_id, SemIR::ErrorInst::SingletonInstId);
  149. } else {
  150. auto fields_id = context.struct_type_fields().AddCanonical(fields);
  151. AddInstAndPush<SemIR::StructType>(
  152. context, node_id,
  153. {.type_id = SemIR::TypeType::SingletonTypeId, .fields_id = fields_id});
  154. }
  155. context.struct_type_fields_stack().PopArray();
  156. return true;
  157. }
  158. } // namespace Carbon::Check