handle_choice.cpp 13 KB

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