handle_class.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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/lex/token_kind.h"
  6. namespace Carbon::Check {
  7. auto HandleClassIntroducer(Context& context, Parse::Node parse_node) -> bool {
  8. // Create an instruction block to hold the instructions created as part of the
  9. // class signature, such as generic parameters.
  10. context.inst_block_stack().Push();
  11. // Push the bracketing node.
  12. context.node_stack().Push(parse_node);
  13. // A name should always follow.
  14. context.declaration_name_stack().PushScopeAndStartName();
  15. return true;
  16. }
  17. static auto BuildClassDeclaration(Context& context)
  18. -> std::tuple<SemIR::ClassId, SemIR::InstId> {
  19. auto name_context = context.declaration_name_stack().FinishName();
  20. auto class_keyword =
  21. context.node_stack()
  22. .PopForSoloParseNode<Parse::NodeKind::ClassIntroducer>();
  23. auto decl_block_id = context.inst_block_stack().Pop();
  24. // Add the class declaration.
  25. auto class_decl = SemIR::ClassDeclaration{
  26. class_keyword, SemIR::ClassId::Invalid, decl_block_id};
  27. auto class_decl_id = context.AddInst(class_decl);
  28. // Check whether this is a redeclaration.
  29. auto existing_id = context.declaration_name_stack().LookupOrAddName(
  30. name_context, class_decl_id);
  31. if (existing_id.is_valid()) {
  32. if (auto existing_class_decl =
  33. context.insts().Get(existing_id).TryAs<SemIR::ClassDeclaration>()) {
  34. // This is a redeclaration of an existing class.
  35. class_decl.class_id = existing_class_decl->class_id;
  36. } else {
  37. // This is a redeclaration of something other than a class.
  38. context.DiagnoseDuplicateName(name_context.parse_node, existing_id);
  39. }
  40. }
  41. // Create a new class if this isn't a valid redeclaration.
  42. if (!class_decl.class_id.is_valid()) {
  43. // TODO: If this is an invalid redeclaration of a non-class entity or there
  44. // was an error in the qualifier, we will have lost track of the class name
  45. // here. We should keep track of it even if the name is invalid.
  46. class_decl.class_id = context.classes().Add(
  47. {.name_id = name_context.state ==
  48. DeclarationNameStack::NameContext::State::Unresolved
  49. ? name_context.unresolved_name_id
  50. : SemIR::NameId::Invalid,
  51. // `.self_type_id` depends on `class_id`, so is set below.
  52. .self_type_id = SemIR::TypeId::Invalid,
  53. .declaration_id = class_decl_id});
  54. // Build the `Self` type.
  55. auto& class_info = context.classes().Get(class_decl.class_id);
  56. class_info.self_type_id =
  57. context.CanonicalizeType(context.AddInst(SemIR::ClassType{
  58. class_keyword, context.GetBuiltinType(SemIR::BuiltinKind::TypeType),
  59. class_decl.class_id}));
  60. }
  61. // Write the class ID into the ClassDeclaration.
  62. context.insts().Set(class_decl_id, class_decl);
  63. return {class_decl.class_id, class_decl_id};
  64. }
  65. auto HandleClassDeclaration(Context& context, Parse::Node /*parse_node*/)
  66. -> bool {
  67. BuildClassDeclaration(context);
  68. context.declaration_name_stack().PopScope();
  69. return true;
  70. }
  71. auto HandleClassDefinitionStart(Context& context, Parse::Node parse_node)
  72. -> bool {
  73. auto [class_id, class_decl_id] = BuildClassDeclaration(context);
  74. auto& class_info = context.classes().Get(class_id);
  75. // Track that this declaration is the definition.
  76. if (class_info.definition_id.is_valid()) {
  77. CARBON_DIAGNOSTIC(ClassRedefinition, Error, "Redefinition of class {0}.",
  78. llvm::StringRef);
  79. CARBON_DIAGNOSTIC(ClassPreviousDefinition, Note,
  80. "Previous definition was here.");
  81. context.emitter()
  82. .Build(parse_node, ClassRedefinition,
  83. context.names().GetFormatted(class_info.name_id))
  84. .Note(context.insts().Get(class_info.definition_id).parse_node(),
  85. ClassPreviousDefinition)
  86. .Emit();
  87. } else {
  88. class_info.definition_id = class_decl_id;
  89. class_info.scope_id = context.name_scopes().Add();
  90. }
  91. // Enter the class scope.
  92. context.PushScope(class_decl_id, class_info.scope_id);
  93. // Introduce `Self`.
  94. context.AddNameToLookup(
  95. parse_node, SemIR::NameId::SelfType,
  96. context.sem_ir().GetTypeAllowBuiltinTypes(class_info.self_type_id));
  97. context.inst_block_stack().Push();
  98. context.node_stack().Push(parse_node, class_id);
  99. context.args_type_info_stack().Push();
  100. // TODO: Handle the case where there's control flow in the class body. For
  101. // example:
  102. //
  103. // class C {
  104. // var v: if true then i32 else f64;
  105. // }
  106. //
  107. // We may need to track a list of instruction blocks here, as we do for a
  108. // function.
  109. class_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  110. return true;
  111. }
  112. auto HandleClassDefinition(Context& context, Parse::Node parse_node) -> bool {
  113. auto fields_id = context.args_type_info_stack().Pop();
  114. auto class_id =
  115. context.node_stack().Pop<Parse::NodeKind::ClassDefinitionStart>();
  116. context.inst_block_stack().Pop();
  117. context.PopScope();
  118. context.declaration_name_stack().PopScope();
  119. // The class type is now fully defined.
  120. auto& class_info = context.classes().Get(class_id);
  121. class_info.object_representation_id =
  122. context.CanonicalizeStructType(parse_node, fields_id);
  123. return true;
  124. }
  125. } // namespace Carbon::Check