handle_alias.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/modifiers.h"
  6. #include "toolchain/parse/node_ids.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. #include "toolchain/sem_ir/typed_insts.h"
  9. namespace Carbon::Check {
  10. auto HandleAliasIntroducer(Context& context,
  11. Parse::AliasIntroducerId /*parse_node*/) -> bool {
  12. context.decl_state_stack().Push(DeclState::Alias);
  13. context.decl_name_stack().PushScopeAndStartName();
  14. return true;
  15. }
  16. auto HandleAliasInitializer(Context& /*context*/,
  17. Parse::AliasInitializerId /*parse_node*/) -> bool {
  18. return true;
  19. }
  20. auto HandleAlias(Context& context, Parse::AliasId /*parse_node*/) -> bool {
  21. auto [expr_node, expr_id] = context.node_stack().PopExprWithParseNode();
  22. auto name_context = context.decl_name_stack().FinishName();
  23. LimitModifiersOnDecl(context, KeywordModifierSet::Access,
  24. Lex::TokenKind::Alias);
  25. auto modifiers = context.decl_state_stack().innermost().modifier_set;
  26. if (!!(modifiers & KeywordModifierSet::Access)) {
  27. context.TODO(context.decl_state_stack().innermost().modifier_node_id(
  28. ModifierOrder::Access),
  29. "access modifier");
  30. }
  31. context.decl_state_stack().Pop(DeclState::Alias);
  32. auto bind_name_id = context.bind_names().Add(
  33. {.name_id = name_context.name_id_for_new_inst(),
  34. .enclosing_scope_id = name_context.enclosing_scope_id_for_new_inst()});
  35. auto alias_id = SemIR::InstId::Invalid;
  36. if (expr_id.is_builtin()) {
  37. // Type (`bool`) and value (`false`) literals provided by the builtin
  38. // structure should be turned into name references.
  39. // TODO: Look into handling `false`, this doesn't do it right now because it
  40. // sees a value instruction instead of a builtin.
  41. alias_id = context.AddInst(
  42. {name_context.parse_node,
  43. SemIR::BindAlias{context.insts().Get(expr_id).type_id(), bind_name_id,
  44. expr_id}});
  45. } else if (auto inst = context.insts().TryGetAs<SemIR::NameRef>(expr_id)) {
  46. // Pass through name references, albeit changing the name in use.
  47. alias_id = context.AddInst(
  48. {name_context.parse_node,
  49. SemIR::BindAlias{inst->type_id, bind_name_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_id =
  55. context.AddInst({name_context.parse_node,
  56. SemIR::BindAlias{SemIR::TypeId::Error, bind_name_id,
  57. SemIR::InstId::BuiltinError}});
  58. }
  59. // Add the name of the binding to the current scope.
  60. context.decl_name_stack().PopScope();
  61. context.decl_name_stack().AddNameToLookup(name_context, alias_id);
  62. return true;
  63. }
  64. } // namespace Carbon::Check