handle_struct.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 HandleStructTypeLiteralStart(Context& context,
  10. 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 HandleStructLiteralStart(Context& context,
  18. Parse::StructLiteralStartId node_id) -> 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 HandleStructFieldDesignator(Context& context,
  26. Parse::StructFieldDesignatorId /*node_id*/)
  27. -> bool {
  28. // This leaves the designated name on top because the `.` isn't interesting.
  29. CARBON_CHECK(context.node_stack().PeekIsName());
  30. return true;
  31. }
  32. auto HandleStructComma(Context& context, Parse::StructCommaId /*node_id*/)
  33. -> bool {
  34. context.param_and_arg_refs_stack().ApplyComma();
  35. return true;
  36. }
  37. auto HandleStructField(Context& context, Parse::StructFieldId node_id) -> bool {
  38. auto value_inst_id = context.node_stack().PopExpr();
  39. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  40. // Store the name for the type.
  41. context.args_type_info_stack().AddInstId(
  42. context.AddInstInNoBlock<SemIR::StructTypeField>(
  43. name_node,
  44. {.name_id = name_id,
  45. .field_type_id = context.insts().Get(value_inst_id).type_id()}));
  46. // Push the value back on the stack as an argument.
  47. context.node_stack().Push(node_id, value_inst_id);
  48. return true;
  49. }
  50. auto HandleStructTypeField(Context& context, Parse::StructTypeFieldId node_id)
  51. -> bool {
  52. auto [type_node, type_id] = context.node_stack().PopExprWithNodeId();
  53. SemIR::TypeId cast_type_id = ExprAsType(context, type_node, type_id);
  54. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  55. auto inst_id = context.AddInst<SemIR::StructTypeField>(
  56. name_node, {.name_id = name_id, .field_type_id = cast_type_id});
  57. context.node_stack().Push(node_id, inst_id);
  58. return true;
  59. }
  60. static auto DiagnoseDuplicateNames(Context& context,
  61. SemIR::InstBlockId type_block_id,
  62. llvm::StringRef construct) -> bool {
  63. auto& sem_ir = context.sem_ir();
  64. auto fields = sem_ir.inst_blocks().Get(type_block_id);
  65. Map<SemIR::NameId, SemIR::InstId> names;
  66. auto& insts = sem_ir.insts();
  67. for (SemIR::InstId field_inst_id : fields) {
  68. auto field_inst = insts.GetAs<SemIR::StructTypeField>(field_inst_id);
  69. auto result = names.Insert(field_inst.name_id, field_inst_id);
  70. if (!result.is_inserted()) {
  71. CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
  72. "Duplicated field name `{1}` in {0}.", std::string,
  73. SemIR::NameId);
  74. CARBON_DIAGNOSTIC(StructNamePrevious, Note,
  75. "Field with the same name here.");
  76. context.emitter()
  77. .Build(field_inst_id, StructNameDuplicate, construct.str(),
  78. field_inst.name_id)
  79. .Note(result.value(), StructNamePrevious)
  80. .Emit();
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. auto HandleStructLiteral(Context& context, Parse::StructLiteralId node_id)
  87. -> bool {
  88. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  89. Parse::NodeKind::StructLiteralStart);
  90. context.scope_stack().Pop();
  91. context.node_stack()
  92. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructLiteralStart>();
  93. auto type_block_id = context.args_type_info_stack().Pop();
  94. if (DiagnoseDuplicateNames(context, type_block_id, "struct literal")) {
  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 HandleStructTypeLiteral(Context& context,
  105. Parse::StructTypeLiteralId node_id) -> 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, "struct type literal")) {
  114. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  115. return true;
  116. }
  117. context.AddInstAndPush<SemIR::StructType>(
  118. node_id, {.type_id = SemIR::TypeId::TypeType, .fields_id = refs_id});
  119. return true;
  120. }
  121. } // namespace Carbon::Check