handle_alias.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 /*node_id*/) -> 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 /*node_id*/) -> bool {
  18. return true;
  19. }
  20. auto HandleAlias(Context& context, Parse::AliasId /*node_id*/) -> bool {
  21. auto [expr_node, expr_id] = context.node_stack().PopExprWithNodeId();
  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. .bind_index = SemIR::CompileTimeBindIndex::Invalid});
  36. auto alias_id = SemIR::InstId::Invalid;
  37. if (expr_id.is_builtin()) {
  38. // Type (`bool`) and value (`false`) literals provided by the builtin
  39. // structure should be turned into name references.
  40. // TODO: Look into handling `false`, this doesn't do it right now because it
  41. // sees a value instruction instead of a builtin.
  42. alias_id = context.AddInst(
  43. {name_context.loc_id,
  44. SemIR::BindAlias{context.insts().Get(expr_id).type_id(), bind_name_id,
  45. 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_id = context.AddInst(
  49. {name_context.loc_id,
  50. SemIR::BindAlias{inst->type_id, bind_name_id, inst->value_id}});
  51. } else {
  52. CARBON_DIAGNOSTIC(AliasRequiresNameRef, Error,
  53. "Alias initializer must be a name reference.");
  54. context.emitter().Emit(expr_node, AliasRequiresNameRef);
  55. alias_id =
  56. context.AddInst({name_context.loc_id,
  57. SemIR::BindAlias{SemIR::TypeId::Error, bind_name_id,
  58. SemIR::InstId::BuiltinError}});
  59. }
  60. // Add the name of the binding to the current scope.
  61. context.decl_name_stack().PopScope();
  62. context.decl_name_stack().AddNameOrDiagnoseDuplicate(name_context, alias_id);
  63. return true;
  64. }
  65. } // namespace Carbon::Check