handle_struct.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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::StructCommaId /*node_id*/)
  32. -> bool {
  33. context.param_and_arg_refs_stack().ApplyComma();
  34. return true;
  35. }
  36. auto HandleParseNode(Context& context, Parse::StructFieldId node_id) -> bool {
  37. auto value_inst_id = context.node_stack().PopExpr();
  38. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  39. // Store the name for the type.
  40. context.args_type_info_stack().AddInstId(
  41. context.AddInstInNoBlock<SemIR::StructTypeField>(
  42. name_node,
  43. {.name_id = name_id,
  44. .field_type_id = context.insts().Get(value_inst_id).type_id()}));
  45. // Push the value back on the stack as an argument.
  46. context.node_stack().Push(node_id, value_inst_id);
  47. return true;
  48. }
  49. auto HandleParseNode(Context& context, Parse::StructTypeFieldId node_id)
  50. -> bool {
  51. auto [type_node, type_id] = context.node_stack().PopExprWithNodeId();
  52. SemIR::TypeId cast_type_id = ExprAsType(context, type_node, type_id).type_id;
  53. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  54. auto inst_id = context.AddInst<SemIR::StructTypeField>(
  55. name_node, {.name_id = name_id, .field_type_id = cast_type_id});
  56. context.node_stack().Push(node_id, inst_id);
  57. return true;
  58. }
  59. static auto DiagnoseDuplicateNames(Context& context,
  60. SemIR::InstBlockId type_block_id,
  61. bool is_struct_type_literal) -> bool {
  62. auto& sem_ir = context.sem_ir();
  63. auto fields = sem_ir.inst_blocks().Get(type_block_id);
  64. Map<SemIR::NameId, SemIR::InstId> names;
  65. auto& insts = sem_ir.insts();
  66. for (SemIR::InstId field_inst_id : fields) {
  67. auto field_inst = insts.GetAs<SemIR::StructTypeField>(field_inst_id);
  68. auto result = names.Insert(field_inst.name_id, field_inst_id);
  69. if (!result.is_inserted()) {
  70. CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
  71. "duplicated field name `{1}` in "
  72. "{0:struct type literal|struct literal}",
  73. BoolAsSelect, SemIR::NameId);
  74. CARBON_DIAGNOSTIC(StructNamePrevious, Note,
  75. "field with the same name here");
  76. context.emitter()
  77. .Build(field_inst_id, StructNameDuplicate, is_struct_type_literal,
  78. field_inst.name_id)
  79. .Note(result.value(), StructNamePrevious)
  80. .Emit();
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. auto HandleParseNode(Context& context, Parse::StructLiteralId node_id) -> bool {
  87. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  88. Parse::NodeKind::StructLiteralStart);
  89. context.scope_stack().Pop();
  90. context.node_stack()
  91. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructLiteralStart>();
  92. auto type_block_id = context.args_type_info_stack().Pop();
  93. if (DiagnoseDuplicateNames(context, type_block_id,
  94. /*is_struct_type_literal=*/false)) {
  95. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  96. return true;
  97. }
  98. auto type_id = context.GetStructType(type_block_id);
  99. auto value_id = context.AddInst<SemIR::StructLiteral>(
  100. node_id, {.type_id = type_id, .elements_id = refs_id});
  101. context.node_stack().Push(node_id, value_id);
  102. return true;
  103. }
  104. auto HandleParseNode(Context& context, Parse::StructTypeLiteralId node_id)
  105. -> bool {
  106. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  107. Parse::NodeKind::StructTypeLiteralStart);
  108. context.scope_stack().Pop();
  109. context.node_stack()
  110. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructTypeLiteralStart>();
  111. CARBON_CHECK(refs_id != SemIR::InstBlockId::Empty,
  112. "{{}} is handled by StructLiteral.");
  113. if (DiagnoseDuplicateNames(context, refs_id,
  114. /*is_struct_type_literal=*/true)) {
  115. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  116. return true;
  117. }
  118. context.AddInstAndPush<SemIR::StructType>(
  119. node_id, {.type_id = SemIR::TypeId::TypeType, .fields_id = refs_id});
  120. return true;
  121. }
  122. } // namespace Carbon::Check