handle_struct.cpp 4.9 KB

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