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/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().saw_access_modifier,
  28. "access modifier");
  29. }
  30. context.decl_state_stack().Pop(DeclState::Alias);
  31. auto bind_name_id = context.bind_names().Add(
  32. {.name_id = name_context.name_id_for_new_inst(),
  33. .enclosing_scope_id = name_context.enclosing_scope_id_for_new_inst()});
  34. auto alias_id = SemIR::InstId::Invalid;
  35. if (expr_id.is_builtin()) {
  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_id = context.AddInst(
  41. {name_context.parse_node,
  42. SemIR::BindAlias{context.insts().Get(expr_id).type_id(), bind_name_id,
  43. expr_id}});
  44. } else if (auto inst = context.insts().TryGetAs<SemIR::NameRef>(expr_id)) {
  45. // Pass through name references, albeit changing the name in use.
  46. alias_id = context.AddInst(
  47. {name_context.parse_node,
  48. SemIR::BindAlias{inst->type_id, bind_name_id, inst->value_id}});
  49. } else {
  50. CARBON_DIAGNOSTIC(AliasRequiresNameRef, Error,
  51. "Alias initializer must be a name reference.");
  52. context.emitter().Emit(expr_node, AliasRequiresNameRef);
  53. alias_id =
  54. context.AddInst({name_context.parse_node,
  55. SemIR::BindAlias{SemIR::TypeId::Error, bind_name_id,
  56. SemIR::InstId::BuiltinError}});
  57. }
  58. // Add the name of the binding to the current scope.
  59. context.decl_name_stack().PopScope();
  60. context.decl_name_stack().AddNameToLookup(name_context, alias_id);
  61. return true;
  62. }
  63. } // namespace Carbon::Check