handle_struct.cpp 6.5 KB

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