handle_choice.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #include "toolchain/check/decl_name_stack.h"
  7. #include "toolchain/check/eval.h"
  8. #include "toolchain/check/generic.h"
  9. #include "toolchain/check/handle.h"
  10. #include "toolchain/check/inst.h"
  11. #include "toolchain/check/literal.h"
  12. #include "toolchain/check/name_component.h"
  13. #include "toolchain/check/type.h"
  14. #include "toolchain/check/unused.h"
  15. #include "toolchain/diagnostics/diagnostic.h"
  16. #include "toolchain/lex/token_kind.h"
  17. #include "toolchain/sem_ir/ids.h"
  18. #include "toolchain/sem_ir/inst.h"
  19. #include "toolchain/sem_ir/name_scope.h"
  20. #include "toolchain/sem_ir/typed_insts.h"
  21. namespace Carbon::Check {
  22. auto HandleParseNode(Context& context, Parse::ChoiceIntroducerId node_id)
  23. -> bool {
  24. // This choice is potentially generic.
  25. StartGenericDecl(context);
  26. // Create an instruction block to hold the instructions created as part of the
  27. // choice signature, such as generic parameters.
  28. context.inst_block_stack().Push();
  29. // There's no modifiers on a choice, but this informs how to typecheck any
  30. // generic binding pattern.
  31. context.decl_introducer_state_stack().Push<Lex::TokenKind::Choice>();
  32. // Push the bracketing node.
  33. context.node_stack().Push(node_id);
  34. // The choice's name follows.
  35. context.decl_name_stack().PushScopeAndStartName();
  36. return true;
  37. }
  38. auto HandleParseNode(Context& context, Parse::ChoiceDefinitionStartId node_id)
  39. -> bool {
  40. auto name = PopNameComponent(context);
  41. auto name_context = context.decl_name_stack().FinishName(name);
  42. context.node_stack()
  43. .PopAndDiscardSoloNodeId<Parse::NodeKind::ChoiceIntroducer>();
  44. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Choice>();
  45. auto decl_block_id = context.inst_block_stack().Pop();
  46. // Choices create a ClassId, since they ultimately turn into a class with
  47. // methods and some builtin impls.
  48. auto class_decl = SemIR::ClassDecl{.type_id = SemIR::TypeType::TypeId,
  49. .class_id = SemIR::ClassId::None,
  50. .decl_block_id = decl_block_id};
  51. auto class_decl_id = AddPlaceholderInst(context, node_id, class_decl);
  52. context.decl_name_stack().AddNameOrDiagnose(name_context, class_decl_id,
  53. SemIR::AccessKind::Public);
  54. // An inst block for the body of the choice.
  55. context.inst_block_stack().Push();
  56. auto body_block_id = context.inst_block_stack().PeekOrAdd();
  57. SemIR::Class class_info = {
  58. name_context.MakeEntityWithParamsBase(name, class_decl_id,
  59. /*is_extern=*/false,
  60. SemIR::LibraryNameId::None),
  61. {// `.self_type_id` depends on the ClassType, so is set below.
  62. .self_type_id = SemIR::TypeId::None,
  63. .inheritance_kind = SemIR::ClassFields::Final,
  64. // TODO: Handle the case where there's control flow in the alternatives.
  65. // For example:
  66. //
  67. // choice C {
  68. // Alt(x: if true then i32 else f64),
  69. // }
  70. //
  71. // We may need to track a list of instruction blocks here, as we do for a
  72. // function.
  73. .body_block_id = body_block_id}};
  74. // This call finishes the GenericDecl, after which we can use the `Self`
  75. // specific.
  76. class_info.generic_id = BuildGenericDecl(context, class_decl_id);
  77. auto self_specific_id =
  78. context.generics().GetSelfSpecific(class_info.generic_id);
  79. class_info.definition_id = class_decl_id;
  80. class_info.scope_id = context.name_scopes().Add(
  81. class_decl_id, SemIR::NameId::None, class_info.parent_scope_id);
  82. class_decl.class_id = context.classes().Add(class_info);
  83. if (class_info.has_parameters()) {
  84. class_decl.type_id = GetGenericClassType(
  85. context, class_decl.class_id, context.scope_stack().PeekSpecificId());
  86. }
  87. ReplaceInstBeforeConstantUse(context, class_decl_id, class_decl);
  88. // We had to construct the `ClassId` from `Class` in order to build the `Self`
  89. // type below. But it needs to be written back to the `Class` in the
  90. // ValueStore, not the local variable. This gives a mutable reference to the
  91. // `Class` in the ValueStore.
  92. SemIR::Class& mut_class = context.classes().Get(class_decl.class_id);
  93. // Build the `Self` type using the resulting type constant.
  94. auto self_type_id =
  95. GetClassType(context, class_decl.class_id, self_specific_id);
  96. mut_class.self_type_id = self_type_id;
  97. // Enter the choice scope.
  98. context.scope_stack().PushForEntity(class_decl_id, class_info.scope_id,
  99. self_specific_id);
  100. // Checking the binding pattern for an alternative requires a non-empty stack.
  101. // We reuse the Choice token even though we're now checking an alternative
  102. // inside the Choice, since there's no better token to use.
  103. //
  104. // TODO: The token here is _not_ `Choice` though, we shouldn't need to use
  105. // that here. Either remove the need for a token or find a token (a new
  106. // introducer?) for the alternative to name.
  107. context.decl_introducer_state_stack().Push<Lex::TokenKind::Choice>();
  108. StartGenericDefinition(context, class_info.generic_id);
  109. context.name_scopes().AddRequiredName(
  110. class_info.scope_id, SemIR::NameId::SelfType,
  111. context.types().GetTypeInstId(self_type_id));
  112. // Mark the beginning of the choice body.
  113. context.node_stack().Push(node_id, class_decl.class_id);
  114. CARBON_CHECK(context.choice_deferred_bindings().empty(),
  115. "Alternatives left behind in choice_deferred_bindings: {0}",
  116. context.choice_deferred_bindings().size());
  117. return true;
  118. }
  119. static auto AddChoiceAlternative(
  120. Context& context, Parse::NodeIdOneOf<Parse::ChoiceAlternativeListCommaId,
  121. Parse::ChoiceDefinitionId>
  122. node_id) -> void {
  123. // Note, there is nothing like a ChoiceAlternativeIntroducer node, so no parse
  124. // node to pop here.
  125. auto name_component = PopNameComponent(context);
  126. if (name_component.param_patterns_id == SemIR::InstBlockId::Empty) {
  127. // Treat an empty parameter list the same as no parameter list.
  128. //
  129. // TODO: The current design suggests that we want Foo() to result in a
  130. // member function `ChoiceType.Foo()`, and `Foo` to result in a member
  131. // constant `ChoiceType.Foo`, but that only one of the two is allowed in a
  132. // single choice type. See
  133. // https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/sum_types.md#user-defined-sum-types.
  134. // For now they are not treated differently and both resolve to a member
  135. // constant.
  136. context.TODO(name_component.params_loc_id,
  137. "empty parameter list should make a member function");
  138. name_component.param_patterns_id = SemIR::InstBlockId::None;
  139. }
  140. if (name_component.param_patterns_id.has_value()) {
  141. context.TODO(name_component.params_loc_id,
  142. "choice alternatives with parameters are not yet supported");
  143. return;
  144. }
  145. context.choice_deferred_bindings().push_back({node_id, name_component});
  146. }
  147. // Info about the Choice type, used to construct each alternative member of the
  148. // class representing the Choice.
  149. struct ChoiceInfo {
  150. // The `Self` type.
  151. SemIR::TypeId self_type_id;
  152. // The scope of the class for adding the alternatives to.
  153. SemIR::NameScopeId name_scope_id;
  154. // A struct type with the same fields as `Self`. Used to construct `Self`.
  155. SemIR::TypeId self_struct_type_id;
  156. // The type of the discriminant value.
  157. SemIR::TypeId discriminant_type_id;
  158. int num_alternative_bits;
  159. };
  160. // Builds a `let` binding for an alternative without parameters as a member of
  161. // the resulting class for the Choice definition. If the alternative was `Alt`
  162. // then the binding will be like:
  163. // ```
  164. // let Alt: ChoiceType = <ChoiceType with Alt selected>;
  165. // ```
  166. static auto MakeLetBinding(Context& context, const ChoiceInfo& choice_info,
  167. int alternative_index,
  168. const Context::ChoiceDeferredBinding& binding)
  169. -> void {
  170. SemIR::InstId discriminant_value_id = [&] {
  171. if (choice_info.num_alternative_bits == 0) {
  172. return AddInst(context, binding.node_id,
  173. SemIR::TupleLiteral{
  174. .type_id = GetTupleType(context, {}),
  175. .elements_id = SemIR::InstBlockId::Empty,
  176. });
  177. } else {
  178. return MakeIntLiteral(context, binding.node_id,
  179. context.ints().Add(alternative_index));
  180. }
  181. }();
  182. discriminant_value_id =
  183. ConvertToValueOfType(context, binding.node_id, discriminant_value_id,
  184. choice_info.discriminant_type_id);
  185. auto self_value_id = ConvertToValueOfType(
  186. context, binding.node_id,
  187. AddInst(context, binding.node_id,
  188. SemIR::StructLiteral{
  189. .type_id = choice_info.self_struct_type_id,
  190. .elements_id =
  191. [&] {
  192. context.inst_block_stack().Push();
  193. context.inst_block_stack().AddInstId(
  194. discriminant_value_id);
  195. return context.inst_block_stack().Pop();
  196. }(),
  197. }),
  198. choice_info.self_type_id);
  199. auto entity_name_id = context.entity_names().Add(
  200. {.name_id = binding.name_component.name_id,
  201. .parent_scope_id = choice_info.name_scope_id});
  202. auto bind_name_id = AddInst(context, binding.node_id,
  203. SemIR::ValueBinding{
  204. .type_id = choice_info.self_type_id,
  205. .entity_name_id = entity_name_id,
  206. .value_id = self_value_id,
  207. });
  208. context.name_scopes()
  209. .Get(choice_info.name_scope_id)
  210. .AddRequired({.name_id = binding.name_component.name_id,
  211. .result = SemIR::ScopeLookupResult::MakeFound(
  212. bind_name_id, SemIR::AccessKind::Public)});
  213. }
  214. auto HandleParseNode(Context& context, Parse::ChoiceDefinitionId node_id)
  215. -> bool {
  216. // The last alternative may optionally not have a comma after it, in which
  217. // case we get here after the last alternative.
  218. if (!context.node_stack().PeekIs(Parse::NodeKind::ChoiceDefinitionStart)) {
  219. AddChoiceAlternative(context, node_id);
  220. }
  221. auto class_id =
  222. context.node_stack().Pop<Parse::NodeKind::ChoiceDefinitionStart>();
  223. int num_alternatives = context.choice_deferred_bindings().size();
  224. int num_alternative_bits = [&] {
  225. if (num_alternatives > 1) {
  226. return static_cast<int>(ceil(log2(num_alternatives)));
  227. } else {
  228. return 0;
  229. }
  230. }();
  231. SemIR::TypeId discriminant_type_id = [&] {
  232. if (num_alternative_bits == 0) {
  233. // Even though there's no bits needed, we add an empty field. We want to
  234. // prevent constructing the Choice from an empty struct literal instead of
  235. // going through an alternative. And in the case there is no alternative,
  236. // then there's no way to construct the Choice (which can be a useful
  237. // type).
  238. //
  239. // TODO: Find a way to produce a better diagnostic, and not require an
  240. // empty field.
  241. return GetTupleType(context, {});
  242. } else {
  243. return MakeIntType(context, node_id, SemIR::IntKind::Unsigned,
  244. context.ints().Add(num_alternative_bits));
  245. }
  246. }();
  247. llvm::SmallVector<SemIR::StructTypeField, 1> struct_type_fields;
  248. struct_type_fields.push_back({
  249. .name_id = SemIR::NameId::ChoiceDiscriminant,
  250. .type_inst_id = context.types().GetTypeInstId(discriminant_type_id),
  251. });
  252. auto fields_id =
  253. context.struct_type_fields().AddCanonical(struct_type_fields);
  254. auto choice_witness_id = AddInst(
  255. context, node_id,
  256. SemIR::CompleteTypeWitness{
  257. .type_id = GetSingletonType(context, SemIR::WitnessType::TypeInstId),
  258. .object_repr_type_inst_id = context.types().GetTypeInstId(
  259. GetStructType(context, fields_id))});
  260. auto& class_info = context.classes().Get(class_id);
  261. class_info.complete_type_witness_id = choice_witness_id;
  262. auto self_struct_type_id = GetStructType(
  263. context, context.struct_type_fields().AddCanonical(struct_type_fields));
  264. for (auto [i, deferred_binding] :
  265. llvm::enumerate(context.choice_deferred_bindings())) {
  266. // TODO: This requires the class to be complete, but we've not yet called
  267. // `FinishGenericDefinition`, so we can't use it as a complete type yet. But
  268. // this also potentially adds things to the generic definition, so we can't
  269. // call `FinsihGenericDefinition` before this call, either.
  270. MakeLetBinding(context,
  271. ChoiceInfo{.self_type_id = class_info.self_type_id,
  272. .name_scope_id = class_info.scope_id,
  273. .self_struct_type_id = self_struct_type_id,
  274. .discriminant_type_id = discriminant_type_id,
  275. .num_alternative_bits = num_alternative_bits},
  276. i, deferred_binding);
  277. }
  278. // The scopes and blocks for the choice itself.
  279. context.inst_block_stack().Pop();
  280. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Choice>();
  281. context.scope_stack().Pop(/*check_unused=*/true);
  282. context.decl_name_stack().PopScope();
  283. FinishGenericDefinition(context, class_info.generic_id);
  284. context.choice_deferred_bindings().clear();
  285. return true;
  286. }
  287. auto HandleParseNode(Context& context,
  288. Parse::ChoiceAlternativeListCommaId node_id) -> bool {
  289. AddChoiceAlternative(context, node_id);
  290. return true;
  291. }
  292. } // namespace Carbon::Check