handle_struct.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/diagnostics/format_providers.h"
  9. namespace Carbon::Check {
  10. auto HandleParseNode(Context& context, Parse::StructTypeLiteralStartId node_id)
  11. -> bool {
  12. context.scope_stack().Push();
  13. context.node_stack().Push(node_id);
  14. context.param_and_arg_refs_stack().Push();
  15. return true;
  16. }
  17. auto HandleParseNode(Context& context, Parse::StructLiteralStartId node_id)
  18. -> bool {
  19. context.scope_stack().Push();
  20. context.node_stack().Push(node_id);
  21. context.args_type_info_stack().Push();
  22. context.param_and_arg_refs_stack().Push();
  23. return true;
  24. }
  25. auto HandleParseNode(Context& context,
  26. Parse::StructFieldDesignatorId /*node_id*/) -> bool {
  27. // This leaves the designated name on top because the `.` isn't interesting.
  28. CARBON_CHECK(context.node_stack().PeekIs<SemIR::NameId>());
  29. return true;
  30. }
  31. auto HandleParseNode(Context& context, Parse::StructLiteralCommaId /*node_id*/)
  32. -> bool {
  33. context.param_and_arg_refs_stack().ApplyComma();
  34. return true;
  35. }
  36. auto HandleParseNode(Context& context,
  37. Parse::StructTypeLiteralCommaId /*node_id*/) -> bool {
  38. context.param_and_arg_refs_stack().ApplyComma();
  39. return true;
  40. }
  41. auto HandleParseNode(Context& context, Parse::StructLiteralFieldId node_id)
  42. -> bool {
  43. auto value_inst_id = context.node_stack().PopExpr();
  44. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  45. // Store the name for the type.
  46. context.args_type_info_stack().AddInstId(
  47. context.AddInstInNoBlock<SemIR::StructTypeField>(
  48. name_node,
  49. {.name_id = name_id,
  50. .field_type_id = context.insts().Get(value_inst_id).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, Parse::StructTypeLiteralFieldId node_id)
  56. -> 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. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  60. auto inst_id = context.AddInst<SemIR::StructTypeField>(
  61. name_node, {.name_id = name_id, .field_type_id = cast_type_id});
  62. context.node_stack().Push(node_id, inst_id);
  63. return true;
  64. }
  65. static auto DiagnoseDuplicateNames(Context& context,
  66. SemIR::InstBlockId type_block_id,
  67. bool is_struct_type_literal) -> bool {
  68. auto& sem_ir = context.sem_ir();
  69. auto fields = sem_ir.inst_blocks().Get(type_block_id);
  70. Map<SemIR::NameId, SemIR::InstId> names;
  71. auto& insts = sem_ir.insts();
  72. for (SemIR::InstId field_inst_id : fields) {
  73. auto field_inst = insts.GetAs<SemIR::StructTypeField>(field_inst_id);
  74. auto result = names.Insert(field_inst.name_id, field_inst_id);
  75. if (!result.is_inserted()) {
  76. CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
  77. "duplicated field name `{1}` in "
  78. "{0:struct type literal|struct literal}",
  79. BoolAsSelect, SemIR::NameId);
  80. CARBON_DIAGNOSTIC(StructNamePrevious, Note,
  81. "field with the same name here");
  82. context.emitter()
  83. .Build(field_inst_id, StructNameDuplicate, is_struct_type_literal,
  84. field_inst.name_id)
  85. .Note(result.value(), StructNamePrevious)
  86. .Emit();
  87. return true;
  88. }
  89. }
  90. return false;
  91. }
  92. auto HandleParseNode(Context& context, Parse::StructLiteralId node_id) -> bool {
  93. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  94. Parse::NodeKind::StructLiteralStart);
  95. context.scope_stack().Pop();
  96. context.node_stack()
  97. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructLiteralStart>();
  98. auto type_block_id = context.args_type_info_stack().Pop();
  99. if (DiagnoseDuplicateNames(context, type_block_id,
  100. /*is_struct_type_literal=*/false)) {
  101. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  102. return true;
  103. }
  104. auto type_id = context.GetStructType(type_block_id);
  105. auto value_id = context.AddInst<SemIR::StructLiteral>(
  106. node_id, {.type_id = type_id, .elements_id = refs_id});
  107. context.node_stack().Push(node_id, value_id);
  108. return true;
  109. }
  110. auto HandleParseNode(Context& context, Parse::StructTypeLiteralId node_id)
  111. -> bool {
  112. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  113. Parse::NodeKind::StructTypeLiteralStart);
  114. context.scope_stack().Pop();
  115. context.node_stack()
  116. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructTypeLiteralStart>();
  117. CARBON_CHECK(refs_id != SemIR::InstBlockId::Empty,
  118. "{{}} is handled by StructLiteral.");
  119. if (DiagnoseDuplicateNames(context, refs_id,
  120. /*is_struct_type_literal=*/true)) {
  121. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  122. return true;
  123. }
  124. context.AddInstAndPush<SemIR::StructType>(
  125. node_id, {.type_id = SemIR::TypeId::TypeType, .fields_id = refs_id});
  126. return true;
  127. }
  128. } // namespace Carbon::Check