handle_struct.cpp 4.9 KB

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