class.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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/convert.h"
  7. #include "toolchain/check/eval.h"
  8. #include "toolchain/check/function.h"
  9. #include "toolchain/check/generic.h"
  10. #include "toolchain/check/impl.h"
  11. #include "toolchain/check/import_ref.h"
  12. #include "toolchain/check/inst.h"
  13. #include "toolchain/check/name_lookup.h"
  14. #include "toolchain/check/name_ref.h"
  15. #include "toolchain/check/pattern.h"
  16. #include "toolchain/check/pattern_match.h"
  17. #include "toolchain/check/type.h"
  18. #include "toolchain/parse/node_ids.h"
  19. #include "toolchain/sem_ir/builtin_function_kind.h"
  20. #include "toolchain/sem_ir/function.h"
  21. #include "toolchain/sem_ir/ids.h"
  22. #include "toolchain/sem_ir/typed_insts.h"
  23. namespace Carbon::Check {
  24. auto SetClassSelfType(Context& context, SemIR::ClassId class_id) -> void {
  25. auto& class_info = context.classes().Get(class_id);
  26. auto specific_id = context.generics().GetSelfSpecific(class_info.generic_id);
  27. class_info.self_type_id = GetClassType(context, class_id, specific_id);
  28. }
  29. auto StartClassDefinition(Context& context, SemIR::Class& class_info,
  30. SemIR::InstId definition_id) -> void {
  31. // Track that this declaration is the definition.
  32. CARBON_CHECK(!class_info.has_definition_started());
  33. class_info.definition_id = definition_id;
  34. class_info.scope_id = context.name_scopes().Add(
  35. definition_id, SemIR::NameId::None, class_info.parent_scope_id);
  36. // Introduce `Self`.
  37. context.name_scopes().AddRequiredName(
  38. class_info.scope_id, SemIR::NameId::SelfType,
  39. context.types().GetTypeInstId(class_info.self_type_id));
  40. }
  41. // Checks that the specified finished adapter definition is valid and builds and
  42. // returns a corresponding complete type witness instruction.
  43. static auto CheckCompleteAdapterClassType(
  44. Context& context, Parse::NodeId node_id, SemIR::ClassId class_id,
  45. llvm::ArrayRef<SemIR::InstId> field_decls,
  46. llvm::ArrayRef<SemIR::InstId> body) -> SemIR::InstId {
  47. const auto& class_info = context.classes().Get(class_id);
  48. if (class_info.base_id.has_value()) {
  49. CARBON_DIAGNOSTIC(AdaptWithBase, Error, "adapter with base class");
  50. CARBON_DIAGNOSTIC(AdaptWithBaseHere, Note, "`base` declaration is here");
  51. context.emitter()
  52. .Build(class_info.adapt_id, AdaptWithBase)
  53. .Note(class_info.base_id, AdaptWithBaseHere)
  54. .Emit();
  55. return SemIR::ErrorInst::InstId;
  56. }
  57. if (!field_decls.empty()) {
  58. CARBON_DIAGNOSTIC(AdaptWithFields, Error, "adapter with fields");
  59. CARBON_DIAGNOSTIC(AdaptWithFieldHere, Note,
  60. "first field declaration is here");
  61. context.emitter()
  62. .Build(class_info.adapt_id, AdaptWithFields)
  63. .Note(field_decls.front(), AdaptWithFieldHere)
  64. .Emit();
  65. return SemIR::ErrorInst::InstId;
  66. }
  67. for (auto inst_id : body) {
  68. if (auto function_decl =
  69. context.insts().TryGetAs<SemIR::FunctionDecl>(inst_id)) {
  70. auto& function = context.functions().Get(function_decl->function_id);
  71. if (function.virtual_modifier ==
  72. SemIR::Function::VirtualModifier::Virtual) {
  73. CARBON_DIAGNOSTIC(AdaptWithVirtual, Error,
  74. "adapter with virtual function");
  75. CARBON_DIAGNOSTIC(AdaptWithVirtualHere, Note,
  76. "first virtual function declaration is here");
  77. context.emitter()
  78. .Build(class_info.adapt_id, AdaptWithVirtual)
  79. .Note(inst_id, AdaptWithVirtualHere)
  80. .Emit();
  81. return SemIR::ErrorInst::InstId;
  82. }
  83. }
  84. }
  85. // The object representation of the adapter is the object representation
  86. // of the adapted type.
  87. auto adapted_type_id =
  88. class_info.GetAdaptedType(context.sem_ir(), SemIR::SpecificId::None);
  89. auto object_repr_id = context.types().GetObjectRepr(adapted_type_id);
  90. return AddInst<SemIR::CompleteTypeWitness>(
  91. context, node_id,
  92. {.type_id = GetSingletonType(context, SemIR::WitnessType::TypeInstId),
  93. // TODO: Use InstId from the adapt declaration.
  94. .object_repr_type_inst_id =
  95. context.types().GetTypeInstId(object_repr_id)});
  96. }
  97. static auto AddStructTypeFields(
  98. Context& context,
  99. llvm::SmallVector<SemIR::StructTypeField>& struct_type_fields,
  100. llvm::ArrayRef<SemIR::InstId> field_decls) -> SemIR::StructTypeFieldsId {
  101. for (auto field_decl_id : field_decls) {
  102. auto field_decl = context.insts().GetAs<SemIR::FieldDecl>(field_decl_id);
  103. field_decl.index =
  104. SemIR::ElementIndex{static_cast<int>(struct_type_fields.size())};
  105. ReplaceInstPreservingConstantValue(context, field_decl_id, field_decl);
  106. if (field_decl.type_id == SemIR::ErrorInst::TypeId) {
  107. struct_type_fields.push_back(
  108. {.name_id = field_decl.name_id,
  109. .type_inst_id = SemIR::ErrorInst::TypeInstId});
  110. continue;
  111. }
  112. auto unbound_element_type =
  113. context.sem_ir().types().GetAs<SemIR::UnboundElementType>(
  114. field_decl.type_id);
  115. struct_type_fields.push_back(
  116. {.name_id = field_decl.name_id,
  117. .type_inst_id = unbound_element_type.element_type_inst_id});
  118. }
  119. auto fields_id =
  120. context.struct_type_fields().AddCanonical(struct_type_fields);
  121. return fields_id;
  122. }
  123. // Builds and returns a vtable for the current class. Assumes that the virtual
  124. // functions for the class are listed as the top element of the `vtable_stack`.
  125. static auto BuildVtable(Context& context, Parse::ClassDefinitionId node_id,
  126. SemIR::ClassId class_id,
  127. std::optional<SemIR::ClassType> base_class_type,
  128. llvm::ArrayRef<SemIR::InstId> vtable_contents)
  129. -> SemIR::VtableId {
  130. auto base_vtable_id = SemIR::VtableId::None;
  131. auto base_class_specific_id = SemIR::SpecificId::None;
  132. // Get some base class/type/specific info.
  133. if (base_class_type) {
  134. auto& base_class_info = context.classes().Get(base_class_type->class_id);
  135. auto base_vtable_decl_inst_id = base_class_info.vtable_decl_id;
  136. if (base_vtable_decl_inst_id.has_value()) {
  137. LoadImportRef(context, base_vtable_decl_inst_id);
  138. auto canonical_base_vtable_inst_id =
  139. context.constant_values().GetConstantInstId(base_vtable_decl_inst_id);
  140. const auto& base_vtable_decl_inst =
  141. context.insts().GetAs<SemIR::VtableDecl>(
  142. canonical_base_vtable_inst_id);
  143. base_vtable_id = base_vtable_decl_inst.vtable_id;
  144. base_class_specific_id = base_class_type->specific_id;
  145. }
  146. }
  147. const auto& class_info = context.classes().Get(class_id);
  148. auto class_generic_id = class_info.generic_id;
  149. // Wrap vtable entries in SpecificFunctions as needed/in generic classes.
  150. auto build_specific_function =
  151. [&](SemIR::InstId fn_decl_id) -> SemIR::InstId {
  152. if (!class_generic_id.has_value()) {
  153. return fn_decl_id;
  154. }
  155. const auto& fn_decl =
  156. context.insts().GetAs<SemIR::FunctionDecl>(fn_decl_id);
  157. const auto& function = context.functions().Get(fn_decl.function_id);
  158. return GetOrAddInst<SemIR::SpecificFunction>(
  159. context, node_id,
  160. {.type_id =
  161. GetSingletonType(context, SemIR::SpecificFunctionType::TypeInstId),
  162. .callee_id = fn_decl_id,
  163. .specific_id =
  164. context.generics().GetSelfSpecific(function.generic_id)});
  165. };
  166. llvm::SmallVector<SemIR::InstId> vtable;
  167. Set<SemIR::FunctionId> implemented_impls;
  168. if (base_vtable_id.has_value()) {
  169. auto base_vtable_inst_block = context.inst_blocks().Get(
  170. context.vtables().Get(base_vtable_id).virtual_functions_id);
  171. // TODO: Avoid quadratic search. Perhaps build a map from `NameId` to the
  172. // elements of the top of `vtable_stack`.
  173. for (auto base_vtable_entry_id : base_vtable_inst_block) {
  174. auto [derived_vtable_entry_id, derived_vtable_entry_const_id, fn_id,
  175. specific_id] =
  176. DecomposeVirtualFunction(context.sem_ir(), base_vtable_entry_id,
  177. base_class_specific_id);
  178. const auto& fn = context.sem_ir().functions().Get(fn_id);
  179. const auto* i = llvm::find_if(
  180. vtable_contents, [&](SemIR::InstId override_fn_decl_id) -> bool {
  181. const auto& override_fn = context.functions().Get(
  182. context.insts()
  183. .GetAs<SemIR::FunctionDecl>(override_fn_decl_id)
  184. .function_id);
  185. return override_fn.virtual_modifier ==
  186. SemIR::FunctionFields::VirtualModifier::Override &&
  187. override_fn.name_id == fn.name_id;
  188. });
  189. if (i != vtable_contents.end()) {
  190. auto override_fn_id =
  191. context.insts().GetAs<SemIR::FunctionDecl>(*i).function_id;
  192. implemented_impls.Insert(override_fn_id);
  193. auto& override_fn = context.functions().Get(override_fn_id);
  194. CheckFunctionTypeMatches(context, override_fn, fn, specific_id,
  195. /*check_syntax=*/false,
  196. /*check_self=*/false);
  197. derived_vtable_entry_id = build_specific_function(*i);
  198. override_fn.virtual_index = vtable.size();
  199. CARBON_CHECK(override_fn.virtual_index == fn.virtual_index);
  200. } else if (auto base_vtable_specific_function =
  201. context.insts().TryGetAs<SemIR::SpecificFunction>(
  202. derived_vtable_entry_id)) {
  203. if (derived_vtable_entry_const_id.is_symbolic()) {
  204. // Create a new instruction here that is otherwise identical to
  205. // `derived_vtable_entry_id` but is dependent within the derived
  206. // class. This ensures we can `GetConstantValueInSpecific` for it
  207. // with the derived class's specific (when forming further derived
  208. // classes, lowering the vtable, etc).
  209. derived_vtable_entry_id = GetOrAddInst<SemIR::SpecificFunction>(
  210. context, node_id,
  211. {.type_id = GetSingletonType(
  212. context, SemIR::SpecificFunctionType::TypeInstId),
  213. .callee_id = base_vtable_specific_function->callee_id,
  214. .specific_id = base_vtable_specific_function->specific_id});
  215. }
  216. }
  217. vtable.push_back(derived_vtable_entry_id);
  218. }
  219. }
  220. for (auto inst_id : vtable_contents) {
  221. auto fn_decl = context.insts().GetAs<SemIR::FunctionDecl>(inst_id);
  222. auto& fn = context.functions().Get(fn_decl.function_id);
  223. if (fn.virtual_modifier !=
  224. SemIR::FunctionFields::VirtualModifier::Override) {
  225. fn.virtual_index = vtable.size();
  226. vtable.push_back(build_specific_function(inst_id));
  227. } else if (!implemented_impls.Lookup(fn_decl.function_id)) {
  228. CARBON_DIAGNOSTIC(OverrideWithoutVirtualInBase, Error,
  229. "override without compatible virtual in base class");
  230. context.emitter().Emit(SemIR::LocId(inst_id),
  231. OverrideWithoutVirtualInBase);
  232. }
  233. }
  234. return context.vtables().Add(
  235. {{.class_id = class_id,
  236. .virtual_functions_id = context.inst_blocks().Add(vtable)}});
  237. }
  238. // Checks that the specified finished class definition is valid and builds and
  239. // returns a corresponding complete type witness instruction.
  240. static auto CheckCompleteClassType(
  241. Context& context, Parse::ClassDefinitionId node_id, SemIR::ClassId class_id,
  242. llvm::ArrayRef<SemIR::InstId> field_decls,
  243. llvm::ArrayRef<SemIR::InstId> vtable_contents,
  244. llvm::ArrayRef<SemIR::InstId> body) -> SemIR::InstId {
  245. auto& class_info = context.classes().Get(class_id);
  246. if (class_info.adapt_id.has_value()) {
  247. return CheckCompleteAdapterClassType(context, node_id, class_id,
  248. field_decls, body);
  249. }
  250. bool defining_vptr = class_info.is_dynamic;
  251. auto base_type_id =
  252. class_info.GetBaseType(context.sem_ir(), SemIR::SpecificId::None);
  253. // TODO: Use InstId from base declaration.
  254. auto base_type_inst_id = context.types().GetTypeInstId(base_type_id);
  255. std::optional<SemIR::ClassType> base_class_type;
  256. if (base_type_id.has_value()) {
  257. // TODO: If the base class is template dependent, we will need to decide
  258. // whether to add a vptr as part of instantiation.
  259. base_class_type = context.types().TryGetAs<SemIR::ClassType>(base_type_id);
  260. if (base_class_type &&
  261. context.classes().Get(base_class_type->class_id).is_dynamic) {
  262. defining_vptr = false;
  263. }
  264. }
  265. llvm::SmallVector<SemIR::StructTypeField> struct_type_fields;
  266. struct_type_fields.reserve(defining_vptr + class_info.base_id.has_value() +
  267. field_decls.size());
  268. if (defining_vptr) {
  269. struct_type_fields.push_back(
  270. {.name_id = SemIR::NameId::Vptr,
  271. .type_inst_id = context.types().GetTypeInstId(
  272. GetPointerType(context, SemIR::VtableType::TypeInstId))});
  273. }
  274. if (base_type_id.has_value()) {
  275. auto base_decl = context.insts().GetAs<SemIR::BaseDecl>(class_info.base_id);
  276. base_decl.index =
  277. SemIR::ElementIndex{static_cast<int>(struct_type_fields.size())};
  278. ReplaceInstPreservingConstantValue(context, class_info.base_id, base_decl);
  279. struct_type_fields.push_back(
  280. {.name_id = SemIR::NameId::Base, .type_inst_id = base_type_inst_id});
  281. }
  282. if (class_info.is_dynamic) {
  283. auto vtable_id = BuildVtable(context, node_id, class_id, base_class_type,
  284. vtable_contents);
  285. auto vptr_type_id = GetPointerType(context, SemIR::VtableType::TypeInstId);
  286. class_info.vtable_decl_id = AddInst<SemIR::VtableDecl>(
  287. context, node_id, {.type_id = vptr_type_id, .vtable_id = vtable_id});
  288. }
  289. auto struct_type_id = GetStructType(
  290. context, AddStructTypeFields(context, struct_type_fields, field_decls));
  291. return AddInst<SemIR::CompleteTypeWitness>(
  292. context, node_id,
  293. {.type_id = GetSingletonType(context, SemIR::WitnessType::TypeInstId),
  294. .object_repr_type_inst_id =
  295. context.types().GetTypeInstId(struct_type_id)});
  296. }
  297. auto ComputeClassObjectRepr(Context& context, Parse::ClassDefinitionId node_id,
  298. SemIR::ClassId class_id,
  299. llvm::ArrayRef<SemIR::InstId> field_decls,
  300. llvm::ArrayRef<SemIR::InstId> vtable_contents,
  301. llvm::ArrayRef<SemIR::InstId> body) -> void {
  302. auto complete_type_witness_id = CheckCompleteClassType(
  303. context, node_id, class_id, field_decls, vtable_contents, body);
  304. auto& class_info = context.classes().Get(class_id);
  305. class_info.complete_type_witness_id = complete_type_witness_id;
  306. }
  307. } // namespace Carbon::Check