handle_alias.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/handle.h"
  6. #include "toolchain/check/modifiers.h"
  7. #include "toolchain/check/name_component.h"
  8. #include "toolchain/parse/node_ids.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. #include "toolchain/sem_ir/typed_insts.h"
  11. namespace Carbon::Check {
  12. auto HandleParseNode(Context& context, Parse::AliasIntroducerId /*node_id*/)
  13. -> bool {
  14. context.decl_introducer_state_stack().Push<Lex::TokenKind::Alias>();
  15. context.decl_name_stack().PushScopeAndStartName();
  16. // Push a pattern block to handle parameters of the alias declaration.
  17. // TODO: Disallow these in parse, instead of check, so we don't have to do
  18. // this.
  19. context.pattern_block_stack().Push();
  20. return true;
  21. }
  22. auto HandleParseNode(Context& /*context*/,
  23. Parse::AliasInitializerId /*node_id*/) -> bool {
  24. return true;
  25. }
  26. auto HandleParseNode(Context& context, Parse::AliasId /*node_id*/) -> bool {
  27. auto [expr_node, expr_id] = context.node_stack().PopExprWithNodeId();
  28. auto name_context = context.decl_name_stack().FinishName(
  29. PopNameComponentWithoutParams(context, Lex::TokenKind::Alias));
  30. auto introducer =
  31. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Alias>();
  32. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Access);
  33. auto entity_name_id = context.entity_names().Add(
  34. {.name_id = name_context.name_id_for_new_inst(),
  35. .parent_scope_id = name_context.parent_scope_id,
  36. .bind_index = SemIR::CompileTimeBindIndex::Invalid});
  37. auto alias_type_id = SemIR::TypeId::Invalid;
  38. auto alias_value_id = SemIR::InstId::Invalid;
  39. if (expr_id.is_builtin()) {
  40. // Type (`bool`) and value (`false`) literals provided by the builtin
  41. // structure should be turned into name references.
  42. // TODO: Look into handling `false`, this doesn't do it right now because it
  43. // sees a value instruction instead of a builtin.
  44. alias_type_id = context.insts().Get(expr_id).type_id();
  45. alias_value_id = expr_id;
  46. } else if (auto inst = context.insts().TryGetAs<SemIR::NameRef>(expr_id)) {
  47. // Pass through name references, albeit changing the name in use.
  48. alias_type_id = inst->type_id;
  49. alias_value_id = inst->value_id;
  50. } else {
  51. CARBON_DIAGNOSTIC(AliasRequiresNameRef, Error,
  52. "alias initializer must be a name reference");
  53. context.emitter().Emit(expr_node, AliasRequiresNameRef);
  54. alias_type_id = SemIR::ErrorInst::SingletonTypeId;
  55. alias_value_id = SemIR::ErrorInst::SingletonInstId;
  56. }
  57. auto alias_id = context.AddInst<SemIR::BindAlias>(
  58. name_context.loc_id, {.type_id = alias_type_id,
  59. .entity_name_id = entity_name_id,
  60. .value_id = alias_value_id});
  61. // Add the name of the binding to the current scope.
  62. context.decl_name_stack().PopScope();
  63. context.decl_name_stack().AddNameOrDiagnoseDuplicate(
  64. name_context, alias_id, introducer.modifier_set.GetAccessKind());
  65. return true;
  66. }
  67. } // namespace Carbon::Check