handle_alias.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/generic.h"
  6. #include "toolchain/check/handle.h"
  7. #include "toolchain/check/inst.h"
  8. #include "toolchain/check/modifiers.h"
  9. #include "toolchain/check/name_component.h"
  10. #include "toolchain/parse/node_ids.h"
  11. #include "toolchain/sem_ir/ids.h"
  12. #include "toolchain/sem_ir/typed_insts.h"
  13. namespace Carbon::Check {
  14. auto HandleParseNode(Context& context, Parse::AliasIntroducerId /*node_id*/)
  15. -> bool {
  16. // Aliases can't be generic, but we might have parsed a generic parameter in
  17. // their name, so enter a generic scope just in case.
  18. StartGenericDecl(context);
  19. // Optional modifiers and the name follow.
  20. context.decl_introducer_state_stack().Push<Lex::TokenKind::Alias>();
  21. context.decl_name_stack().PushScopeAndStartName();
  22. return true;
  23. }
  24. auto HandleParseNode(Context& /*context*/,
  25. Parse::AliasInitializerId /*node_id*/) -> bool {
  26. return true;
  27. }
  28. auto HandleParseNode(Context& context, Parse::AliasId /*node_id*/) -> bool {
  29. auto [expr_node, expr_id] = context.node_stack().PopExprWithNodeId();
  30. auto name_context = context.decl_name_stack().FinishName(
  31. PopNameComponentWithoutParams(context, Lex::TokenKind::Alias));
  32. DiscardGenericDecl(context);
  33. auto introducer =
  34. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Alias>();
  35. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::Access);
  36. auto entity_name_id = context.entity_names().Add(
  37. {.name_id = name_context.name_id_for_new_inst(),
  38. .parent_scope_id = name_context.parent_scope_id});
  39. auto alias_type_id = SemIR::TypeId::None;
  40. auto alias_value_id = SemIR::InstId::None;
  41. if (auto inst = context.insts().TryGetAs<SemIR::NameRef>(expr_id)) {
  42. // Pass through name references, albeit changing the name in use.
  43. alias_type_id = inst->type_id;
  44. alias_value_id = inst->value_id;
  45. } else if (auto inst =
  46. context.insts().TryGetAs<SemIR::TypeLiteral>(expr_id)) {
  47. // Treat type literals such as `type` or `bool` like name references.
  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::TypeId;
  55. alias_value_id = SemIR::ErrorInst::InstId;
  56. }
  57. auto alias_id =
  58. AddInst<SemIR::AliasBinding>(context, name_context.loc_id,
  59. {.type_id = alias_type_id,
  60. .entity_name_id = entity_name_id,
  61. .value_id = alias_value_id});
  62. // Add the name of the binding to the current scope.
  63. context.decl_name_stack().PopScope();
  64. context.decl_name_stack().AddNameOrDiagnose(
  65. name_context, alias_id, introducer.modifier_set.GetAccessKind());
  66. return true;
  67. }
  68. } // namespace Carbon::Check