handle_struct.cpp 5.0 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 HandleStructLiteralOrStructTypeLiteralStart(
  8. Context& context, Parse::StructLiteralOrStructTypeLiteralStartId node_id)
  9. -> bool {
  10. context.scope_stack().Push();
  11. context.node_stack().Push(node_id);
  12. // At this point we aren't sure whether this will be a value or type literal,
  13. // so we push onto args irrespective. It just won't be used for a type
  14. // literal.
  15. context.args_type_info_stack().Push();
  16. context.param_and_arg_refs_stack().Push();
  17. return true;
  18. }
  19. auto HandleStructFieldDesignator(Context& context,
  20. Parse::StructFieldDesignatorId /*node_id*/)
  21. -> bool {
  22. // This leaves the designated name on top because the `.` isn't interesting.
  23. CARBON_CHECK(context.node_stack().PeekIsName());
  24. return true;
  25. }
  26. auto HandleStructComma(Context& context, Parse::StructCommaId /*node_id*/)
  27. -> bool {
  28. context.param_and_arg_refs_stack().ApplyComma();
  29. return true;
  30. }
  31. auto HandleStructFieldValue(Context& context, Parse::StructFieldValueId node_id)
  32. -> bool {
  33. auto value_inst_id = context.node_stack().PopExpr();
  34. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  35. // Store the name for the type.
  36. context.args_type_info_stack().AddInstId(context.AddInstInNoBlock(
  37. {name_node, SemIR::StructTypeField{
  38. name_id, context.insts().Get(value_inst_id).type_id()}}));
  39. // Push the value back on the stack as an argument.
  40. context.node_stack().Push(node_id, value_inst_id);
  41. return true;
  42. }
  43. auto HandleStructFieldType(Context& context, Parse::StructFieldTypeId node_id)
  44. -> bool {
  45. auto [type_node, type_id] = context.node_stack().PopExprWithNodeId();
  46. SemIR::TypeId cast_type_id = ExprAsType(context, type_node, type_id);
  47. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  48. auto inst_id = context.AddInst(
  49. {name_node, SemIR::StructTypeField{name_id, cast_type_id}});
  50. context.node_stack().Push(node_id, inst_id);
  51. return true;
  52. }
  53. static auto DiagnoseDuplicateNames(Context& context,
  54. SemIR::InstBlockId type_block_id,
  55. llvm::StringRef construct) -> bool {
  56. auto& sem_ir = context.sem_ir();
  57. auto fields = sem_ir.inst_blocks().Get(type_block_id);
  58. llvm::SmallDenseMap<SemIR::NameId, SemIR::InstId> names;
  59. auto& insts = sem_ir.insts();
  60. for (SemIR::InstId field_inst_id : fields) {
  61. auto field_inst = insts.GetAs<SemIR::StructTypeField>(field_inst_id);
  62. auto [it, added] = names.insert({field_inst.name_id, field_inst_id});
  63. if (!added) {
  64. CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
  65. "Duplicated field name `{1}` in {0}.", std::string,
  66. SemIR::NameId);
  67. CARBON_DIAGNOSTIC(StructNamePrevious, Note,
  68. "Field with the same name here.");
  69. context.emitter()
  70. .Build(field_inst_id, StructNameDuplicate, construct.str(),
  71. field_inst.name_id)
  72. .Note(it->second, StructNamePrevious)
  73. .Emit();
  74. return true;
  75. }
  76. }
  77. return false;
  78. }
  79. auto HandleStructLiteral(Context& context, Parse::StructLiteralId node_id)
  80. -> bool {
  81. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  82. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  83. context.scope_stack().Pop();
  84. context.node_stack()
  85. .PopAndDiscardSoloNodeId<
  86. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  87. auto type_block_id = context.args_type_info_stack().Pop();
  88. if (DiagnoseDuplicateNames(context, type_block_id, "struct literal")) {
  89. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  90. return true;
  91. }
  92. auto type_id = context.GetStructType(type_block_id);
  93. auto value_id =
  94. context.AddInst({node_id, SemIR::StructLiteral{type_id, refs_id}});
  95. context.node_stack().Push(node_id, value_id);
  96. return true;
  97. }
  98. auto HandleStructTypeLiteral(Context& context,
  99. Parse::StructTypeLiteralId node_id) -> bool {
  100. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  101. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  102. context.scope_stack().Pop();
  103. context.node_stack()
  104. .PopAndDiscardSoloNodeId<
  105. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  106. // This is only used for value literals.
  107. context.args_type_info_stack().Pop();
  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(
  115. {node_id, SemIR::StructType{SemIR::TypeId::TypeType, refs_id}});
  116. return true;
  117. }
  118. } // namespace Carbon::Check