handle_choice.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 =
  48. SemIR::ClassDecl{.type_id = SemIR::TypeType::SingletonTypeId,
  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 = context.types().GetTypeIdForTypeConstantId(
  95. TryEvalInst(context, SemIR::InstId::None,
  96. SemIR::ClassType{.type_id = SemIR::TypeType::SingletonTypeId,
  97. .class_id = class_decl.class_id,
  98. .specific_id = self_specific_id}));
  99. mut_class.self_type_id = self_type_id;
  100. // Enter the choice scope.
  101. context.scope_stack().Push(class_decl_id, class_info.scope_id,
  102. self_specific_id);
  103. // Checking the binding pattern for an alternative requires a non-empty stack.
  104. // We reuse the Choice token even though we're now checking an alternative
  105. // inside the Choice, since there's no better token to use.
  106. //
  107. // TODO: The token here is _not_ `Choice` though, we shouldn't need to use
  108. // that here. Either remove the need for a token or find a token (a new
  109. // introducer?) for the alternative to name.
  110. context.decl_introducer_state_stack().Push<Lex::TokenKind::Choice>();
  111. StartGenericDefinition(context);
  112. context.name_scopes().AddRequiredName(
  113. class_info.scope_id, SemIR::NameId::SelfType,
  114. context.types().GetInstId(self_type_id));
  115. // Mark the beginning of the choice body.
  116. context.node_stack().Push(node_id, class_decl.class_id);
  117. CARBON_CHECK(context.choice_deferred_bindings().empty(),
  118. "Alternatives left behind in choice_deferred_bindings: {0}",
  119. context.choice_deferred_bindings().size());
  120. return true;
  121. }
  122. static auto AddChoiceAlternative(
  123. Context& context, Parse::NodeIdOneOf<Parse::ChoiceAlternativeListCommaId,
  124. Parse::ChoiceDefinitionId>
  125. node_id) -> void {
  126. // Note, there is nothing like a ChoiceAlternativeIntroducer node, so no parse
  127. // node to pop here.
  128. auto name_component = PopNameComponent(context);
  129. if (name_component.param_patterns_id == SemIR::InstBlockId::Empty) {
  130. // Treat an empty parameter list the same as no parameter list.
  131. //
  132. // TODO: The current design suggests that we want Foo() to result in a
  133. // member function `ChoiceType.Foo()`, and `Foo` to result in a member
  134. // constant `ChoiceType.Foo`, but that only one of the two is allowed in a
  135. // single choice type. See
  136. // https://github.com/carbon-language/carbon-lang/blob/trunk/docs/design/sum_types.md#user-defined-sum-types.
  137. // For now they are not treated differently and both resolve to a member
  138. // constant.
  139. context.TODO(name_component.params_loc_id,
  140. "empty parameter list should make a member function");
  141. name_component.param_patterns_id = SemIR::InstBlockId::None;
  142. }
  143. if (name_component.param_patterns_id.has_value()) {
  144. context.TODO(name_component.params_loc_id,
  145. "choice alternatives with parameters are not yet supported");
  146. return;
  147. }
  148. context.choice_deferred_bindings().push_back({node_id, name_component});
  149. }
  150. // Info about the Choice type, used to construct each alternative member of the
  151. // class representing the Choice.
  152. struct ChoiceInfo {
  153. // The `Self` type.
  154. SemIR::TypeId self_type_id;
  155. // The scope of the class for adding the alternatives to.
  156. SemIR::NameScopeId name_scope_id;
  157. // A struct type with the same fields as `Self`. Used to construct `Self`.
  158. SemIR::TypeId self_struct_type_id;
  159. // The type of the discriminant value.
  160. SemIR::TypeId discriminant_type_id;
  161. int num_alternative_bits;
  162. };
  163. // Builds a `let` binding for an alternative without parameters as a member of
  164. // the resulting class for the Choice definition. If the alternative was `Alt`
  165. // then the binding will be like:
  166. // ```
  167. // let Alt: ChoiceType = <ChoiceType with Alt selected>;
  168. // ```
  169. static auto MakeLetBinding(Context& context, const ChoiceInfo& choice_info,
  170. int alternative_index,
  171. const Context::ChoiceDeferredBinding& binding)
  172. -> void {
  173. SemIR::InstId discriminant_value_id = [&] {
  174. if (choice_info.num_alternative_bits == 0) {
  175. return AddInst(context, binding.node_id,
  176. SemIR::TupleLiteral{
  177. .type_id = GetTupleType(context, {}),
  178. .elements_id = SemIR::InstBlockId::Empty,
  179. });
  180. } else {
  181. return MakeIntLiteral(context, binding.node_id,
  182. context.ints().Add(alternative_index));
  183. }
  184. }();
  185. discriminant_value_id =
  186. ConvertToValueOfType(context, binding.node_id, discriminant_value_id,
  187. choice_info.discriminant_type_id);
  188. auto self_value_id = ConvertToValueOfType(
  189. context, binding.node_id,
  190. AddInst(context, binding.node_id,
  191. SemIR::StructLiteral{
  192. .type_id = choice_info.self_struct_type_id,
  193. .elements_id =
  194. [&] {
  195. context.inst_block_stack().Push();
  196. context.inst_block_stack().AddInstId(
  197. discriminant_value_id);
  198. return context.inst_block_stack().Pop();
  199. }(),
  200. }),
  201. choice_info.self_type_id);
  202. auto entity_name_id = context.entity_names().Add(
  203. {.name_id = binding.name_component.name_id,
  204. .parent_scope_id = choice_info.name_scope_id});
  205. auto bind_name_id = AddInst(context, binding.node_id,
  206. SemIR::BindName{
  207. .type_id = choice_info.self_type_id,
  208. .entity_name_id = entity_name_id,
  209. .value_id = self_value_id,
  210. });
  211. context.name_scopes()
  212. .Get(choice_info.name_scope_id)
  213. .AddRequired({.name_id = binding.name_component.name_id,
  214. .result = SemIR::ScopeLookupResult::MakeFound(
  215. bind_name_id, SemIR::AccessKind::Public)});
  216. }
  217. auto HandleParseNode(Context& context, Parse::ChoiceDefinitionId node_id)
  218. -> bool {
  219. // The last alternative may optionally not have a comma after it, in which
  220. // case we get here after the last alternative.
  221. if (!context.node_stack().PeekIs(Parse::NodeKind::ChoiceDefinitionStart)) {
  222. AddChoiceAlternative(context, node_id);
  223. }
  224. auto class_id =
  225. context.node_stack().Pop<Parse::NodeKind::ChoiceDefinitionStart>();
  226. int num_alternatives = context.choice_deferred_bindings().size();
  227. int num_alternative_bits = [&] {
  228. if (num_alternatives > 1) {
  229. return static_cast<int>(ceil(log2(num_alternatives)));
  230. } else {
  231. return 0;
  232. }
  233. }();
  234. SemIR::TypeId discriminant_type_id = [&] {
  235. if (num_alternative_bits == 0) {
  236. // Even though there's no bits needed, we add an empty field. We want to
  237. // prevent constructing the Choice from an empty struct literal instead of
  238. // going through an alternative. And in the case there is no alternative,
  239. // then there's no way to construct the Choice (which can be a useful
  240. // type).
  241. //
  242. // TODO: Find a way to produce a better diagnostic, and not require an
  243. // empty field.
  244. return GetTupleType(context, {});
  245. } else {
  246. return MakeIntType(context, node_id, SemIR::IntKind::Unsigned,
  247. context.ints().Add(num_alternative_bits));
  248. }
  249. }();
  250. llvm::SmallVector<SemIR::StructTypeField, 1> struct_type_fields;
  251. struct_type_fields.push_back({
  252. .name_id = SemIR::NameId::ChoiceDiscriminant,
  253. .type_id = discriminant_type_id,
  254. });
  255. auto fields_id =
  256. context.struct_type_fields().AddCanonical(struct_type_fields);
  257. auto choice_witness_id =
  258. AddInst(context, node_id,
  259. SemIR::CompleteTypeWitness{
  260. .type_id = GetSingletonType(
  261. context, SemIR::WitnessType::SingletonInstId),
  262. .object_repr_id = GetStructType(context, fields_id)});
  263. // Note: avoid storing a reference to the returned Class, since it may be
  264. // invalidated by other type constructions.
  265. context.classes().Get(class_id).complete_type_witness_id = choice_witness_id;
  266. auto self_type_id = context.classes().Get(class_id).self_type_id;
  267. auto name_scope_id = context.classes().Get(class_id).scope_id;
  268. auto self_struct_type_id = GetStructType(
  269. context, context.struct_type_fields().AddCanonical(struct_type_fields));
  270. for (auto [i, deferred_binding] :
  271. llvm::enumerate(context.choice_deferred_bindings())) {
  272. MakeLetBinding(context,
  273. ChoiceInfo{.self_type_id = self_type_id,
  274. .name_scope_id = name_scope_id,
  275. .self_struct_type_id = self_struct_type_id,
  276. .discriminant_type_id = discriminant_type_id,
  277. .num_alternative_bits = num_alternative_bits},
  278. i, deferred_binding);
  279. }
  280. // The scopes and blocks for the choice itself.
  281. context.inst_block_stack().Pop();
  282. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Choice>();
  283. context.scope_stack().Pop();
  284. context.decl_name_stack().PopScope();
  285. FinishGenericDefinition(context, context.classes().Get(class_id).generic_id);
  286. context.choice_deferred_bindings().clear();
  287. return true;
  288. }
  289. auto HandleParseNode(Context& context,
  290. Parse::ChoiceAlternativeListCommaId node_id) -> bool {
  291. AddChoiceAlternative(context, node_id);
  292. return true;
  293. }
  294. } // namespace Carbon::Check