class.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/class.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/eval.h"
  7. #include "toolchain/check/function.h"
  8. #include "toolchain/check/import_ref.h"
  9. #include "toolchain/check/inst.h"
  10. #include "toolchain/check/type.h"
  11. namespace Carbon::Check {
  12. auto TryGetAsClass(Context& context, SemIR::TypeId type_id) -> SemIR::Class* {
  13. auto class_type = context.types().TryGetAs<SemIR::ClassType>(type_id);
  14. if (!class_type) {
  15. return nullptr;
  16. }
  17. return &context.classes().Get(class_type->class_id);
  18. }
  19. auto SetClassSelfType(Context& context, SemIR::ClassId class_id) -> void {
  20. auto& class_info = context.classes().Get(class_id);
  21. auto specific_id = context.generics().GetSelfSpecific(class_info.generic_id);
  22. class_info.self_type_id = GetClassType(context, class_id, specific_id);
  23. }
  24. auto StartClassDefinition(Context& context, SemIR::Class& class_info,
  25. SemIR::InstId definition_id) -> void {
  26. // Track that this declaration is the definition.
  27. CARBON_CHECK(!class_info.has_definition_started());
  28. class_info.definition_id = definition_id;
  29. class_info.scope_id = context.name_scopes().Add(
  30. definition_id, SemIR::NameId::None, class_info.parent_scope_id);
  31. // Introduce `Self`.
  32. context.name_scopes().AddRequiredName(
  33. class_info.scope_id, SemIR::NameId::SelfType,
  34. context.types().GetInstId(class_info.self_type_id));
  35. }
  36. // Checks that the specified finished adapter definition is valid and builds and
  37. // returns a corresponding complete type witness instruction.
  38. static auto CheckCompleteAdapterClassType(
  39. Context& context, Parse::NodeId node_id, SemIR::ClassId class_id,
  40. llvm::ArrayRef<SemIR::InstId> field_decls,
  41. llvm::ArrayRef<SemIR::InstId> body) -> SemIR::InstId {
  42. const auto& class_info = context.classes().Get(class_id);
  43. if (class_info.base_id.has_value()) {
  44. CARBON_DIAGNOSTIC(AdaptWithBase, Error, "adapter with base class");
  45. CARBON_DIAGNOSTIC(AdaptWithBaseHere, Note, "`base` declaration is here");
  46. context.emitter()
  47. .Build(class_info.adapt_id, AdaptWithBase)
  48. .Note(class_info.base_id, AdaptWithBaseHere)
  49. .Emit();
  50. return SemIR::ErrorInst::SingletonInstId;
  51. }
  52. if (!field_decls.empty()) {
  53. CARBON_DIAGNOSTIC(AdaptWithFields, Error, "adapter with fields");
  54. CARBON_DIAGNOSTIC(AdaptWithFieldHere, Note,
  55. "first field declaration is here");
  56. context.emitter()
  57. .Build(class_info.adapt_id, AdaptWithFields)
  58. .Note(field_decls.front(), AdaptWithFieldHere)
  59. .Emit();
  60. return SemIR::ErrorInst::SingletonInstId;
  61. }
  62. for (auto inst_id : body) {
  63. if (auto function_decl =
  64. context.insts().TryGetAs<SemIR::FunctionDecl>(inst_id)) {
  65. auto& function = context.functions().Get(function_decl->function_id);
  66. if (function.virtual_modifier ==
  67. SemIR::Function::VirtualModifier::Virtual) {
  68. CARBON_DIAGNOSTIC(AdaptWithVirtual, Error,
  69. "adapter with virtual function");
  70. CARBON_DIAGNOSTIC(AdaptWithVirtualHere, Note,
  71. "first virtual function declaration is here");
  72. context.emitter()
  73. .Build(class_info.adapt_id, AdaptWithVirtual)
  74. .Note(inst_id, AdaptWithVirtualHere)
  75. .Emit();
  76. return SemIR::ErrorInst::SingletonInstId;
  77. }
  78. }
  79. }
  80. // The object representation of the adapter is the object representation
  81. // of the adapted type.
  82. auto adapted_type_id =
  83. class_info.GetAdaptedType(context.sem_ir(), SemIR::SpecificId::None);
  84. auto object_repr_id = context.types().GetObjectRepr(adapted_type_id);
  85. return AddInst<SemIR::CompleteTypeWitness>(
  86. context, node_id,
  87. {.type_id =
  88. GetSingletonType(context, SemIR::WitnessType::SingletonInstId),
  89. .object_repr_id = object_repr_id});
  90. }
  91. static auto AddStructTypeFields(
  92. Context& context,
  93. llvm::SmallVector<SemIR::StructTypeField>& struct_type_fields,
  94. llvm::ArrayRef<SemIR::InstId> field_decls) -> SemIR::StructTypeFieldsId {
  95. for (auto field_decl_id : field_decls) {
  96. auto field_decl = context.insts().GetAs<SemIR::FieldDecl>(field_decl_id);
  97. field_decl.index =
  98. SemIR::ElementIndex{static_cast<int>(struct_type_fields.size())};
  99. ReplaceInstPreservingConstantValue(context, field_decl_id, field_decl);
  100. if (field_decl.type_id == SemIR::ErrorInst::SingletonTypeId) {
  101. struct_type_fields.push_back(
  102. {.name_id = field_decl.name_id,
  103. .type_id = SemIR::ErrorInst::SingletonTypeId});
  104. continue;
  105. }
  106. auto unbound_element_type =
  107. context.sem_ir().types().GetAs<SemIR::UnboundElementType>(
  108. field_decl.type_id);
  109. struct_type_fields.push_back(
  110. {.name_id = field_decl.name_id,
  111. .type_id = context.types().GetTypeIdForTypeInstId(
  112. unbound_element_type.element_type_inst_id)});
  113. }
  114. auto fields_id =
  115. context.struct_type_fields().AddCanonical(struct_type_fields);
  116. return fields_id;
  117. }
  118. // Builds and returns a vtable for the current class. Assumes that the virtual
  119. // functions for the class are listed as the top element of the `vtable_stack`.
  120. static auto BuildVtable(Context& context, Parse::NodeId node_id,
  121. SemIR::InstId base_vtable_id,
  122. llvm::ArrayRef<SemIR::InstId> vtable_contents)
  123. -> SemIR::InstId {
  124. llvm::SmallVector<SemIR::InstId> vtable;
  125. if (base_vtable_id.has_value()) {
  126. LoadImportRef(context, base_vtable_id);
  127. auto canonical_base_vtable_id =
  128. context.constant_values().GetConstantInstId(base_vtable_id);
  129. if (canonical_base_vtable_id == SemIR::ErrorInst::SingletonInstId) {
  130. return SemIR::ErrorInst::SingletonInstId;
  131. }
  132. auto base_vtable_inst_block = context.inst_blocks().Get(
  133. context.insts()
  134. .GetAs<SemIR::Vtable>(canonical_base_vtable_id)
  135. .virtual_functions_id);
  136. // TODO: Avoid quadratic search. Perhaps build a map from `NameId` to the
  137. // elements of the top of `vtable_stack`.
  138. for (auto fn_decl_id : base_vtable_inst_block) {
  139. auto fn_decl = GetCalleeFunction(context.sem_ir(), fn_decl_id);
  140. const auto& fn = context.functions().Get(fn_decl.function_id);
  141. for (auto override_fn_decl_id : vtable_contents) {
  142. auto override_fn_decl =
  143. context.insts().GetAs<SemIR::FunctionDecl>(override_fn_decl_id);
  144. const auto& override_fn =
  145. context.functions().Get(override_fn_decl.function_id);
  146. if (override_fn.virtual_modifier ==
  147. SemIR::FunctionFields::VirtualModifier::Impl &&
  148. override_fn.name_id == fn.name_id) {
  149. // TODO: Support generic base classes, rather than passing
  150. // `SpecificId::None`.
  151. CheckFunctionTypeMatches(context, override_fn, fn,
  152. SemIR::SpecificId::None,
  153. /*check_syntax=*/false,
  154. /*check_self=*/false);
  155. fn_decl_id = override_fn_decl_id;
  156. }
  157. }
  158. vtable.push_back(fn_decl_id);
  159. }
  160. }
  161. for (auto inst_id : vtable_contents) {
  162. auto fn_decl = context.insts().GetAs<SemIR::FunctionDecl>(inst_id);
  163. const auto& fn = context.functions().Get(fn_decl.function_id);
  164. if (fn.virtual_modifier != SemIR::FunctionFields::VirtualModifier::Impl) {
  165. vtable.push_back(inst_id);
  166. }
  167. }
  168. return AddInst<SemIR::Vtable>(
  169. context, node_id,
  170. {.type_id = GetSingletonType(context, SemIR::VtableType::SingletonInstId),
  171. .virtual_functions_id = context.inst_blocks().Add(vtable)});
  172. }
  173. // Checks that the specified finished class definition is valid and builds and
  174. // returns a corresponding complete type witness instruction.
  175. static auto CheckCompleteClassType(
  176. Context& context, Parse::NodeId node_id, SemIR::ClassId class_id,
  177. llvm::ArrayRef<SemIR::InstId> field_decls,
  178. llvm::ArrayRef<SemIR::InstId> vtable_contents,
  179. llvm::ArrayRef<SemIR::InstId> body) -> SemIR::InstId {
  180. auto& class_info = context.classes().Get(class_id);
  181. if (class_info.adapt_id.has_value()) {
  182. return CheckCompleteAdapterClassType(context, node_id, class_id,
  183. field_decls, body);
  184. }
  185. bool defining_vptr = class_info.is_dynamic;
  186. auto base_type_id =
  187. class_info.GetBaseType(context.sem_ir(), SemIR::SpecificId::None);
  188. SemIR::Class* base_class_info = nullptr;
  189. if (base_type_id.has_value()) {
  190. // TODO: If the base class is template dependent, we will need to decide
  191. // whether to add a vptr as part of instantiation.
  192. base_class_info = TryGetAsClass(context, base_type_id);
  193. if (base_class_info && base_class_info->is_dynamic) {
  194. defining_vptr = false;
  195. }
  196. }
  197. llvm::SmallVector<SemIR::StructTypeField> struct_type_fields;
  198. struct_type_fields.reserve(defining_vptr + class_info.base_id.has_value() +
  199. field_decls.size());
  200. if (defining_vptr) {
  201. struct_type_fields.push_back(
  202. {.name_id = SemIR::NameId::Vptr,
  203. .type_id =
  204. GetPointerType(context, SemIR::VtableType::SingletonInstId)});
  205. }
  206. if (base_type_id.has_value()) {
  207. auto base_decl = context.insts().GetAs<SemIR::BaseDecl>(class_info.base_id);
  208. base_decl.index =
  209. SemIR::ElementIndex{static_cast<int>(struct_type_fields.size())};
  210. ReplaceInstPreservingConstantValue(context, class_info.base_id, base_decl);
  211. struct_type_fields.push_back(
  212. {.name_id = SemIR::NameId::Base, .type_id = base_type_id});
  213. }
  214. if (class_info.is_dynamic) {
  215. class_info.vtable_id = BuildVtable(
  216. context, node_id,
  217. defining_vptr ? SemIR::InstId::None : base_class_info->vtable_id,
  218. vtable_contents);
  219. }
  220. return AddInst<SemIR::CompleteTypeWitness>(
  221. context, node_id,
  222. {.type_id =
  223. GetSingletonType(context, SemIR::WitnessType::SingletonInstId),
  224. .object_repr_id = GetStructType(
  225. context,
  226. AddStructTypeFields(context, struct_type_fields, field_decls))});
  227. }
  228. auto ComputeClassObjectRepr(Context& context, Parse::NodeId node_id,
  229. SemIR::ClassId class_id,
  230. llvm::ArrayRef<SemIR::InstId> field_decls,
  231. llvm::ArrayRef<SemIR::InstId> vtable_contents,
  232. llvm::ArrayRef<SemIR::InstId> body) -> void {
  233. auto complete_type_witness_id = CheckCompleteClassType(
  234. context, node_id, class_id, field_decls, vtable_contents, body);
  235. auto& class_info = context.classes().Get(class_id);
  236. class_info.complete_type_witness_id = complete_type_witness_id;
  237. }
  238. } // namespace Carbon::Check