handle_class.cpp 5.0 KB

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