handle_alias.cpp 3.0 KB

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