handle_alias.cpp 3.2 KB

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