handle_export.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/decl_name_stack.h"
  6. #include "toolchain/check/handle.h"
  7. #include "toolchain/check/modifiers.h"
  8. #include "toolchain/check/name_component.h"
  9. #include "toolchain/parse/typed_nodes.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::ExportIntroducerId /*node_id*/)
  14. -> bool {
  15. context.decl_introducer_state_stack().Push<Lex::TokenKind::Export>();
  16. // TODO: Probably need to update DeclNameStack to restrict to only namespaces.
  17. context.decl_name_stack().PushScopeAndStartName();
  18. return true;
  19. }
  20. auto HandleParseNode(Context& context, Parse::ExportDeclId node_id) -> bool {
  21. auto name_context = context.decl_name_stack().FinishName(
  22. PopNameComponentWithoutParams(context, Lex::TokenKind::Export));
  23. context.decl_name_stack().PopScope();
  24. auto introducer =
  25. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Export>();
  26. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::None);
  27. if (name_context.state == DeclNameStack::NameContext::State::Error) {
  28. // Should already be diagnosed.
  29. return true;
  30. }
  31. auto inst_id = name_context.prev_inst_id();
  32. if (!inst_id.has_value()) {
  33. context.DiagnoseNameNotFound(node_id, name_context.name_id_for_new_inst());
  34. return true;
  35. }
  36. auto inst = context.insts().Get(inst_id);
  37. if (inst.Is<SemIR::ExportDecl>()) {
  38. CARBON_DIAGNOSTIC(ExportRedundant, Warning,
  39. "`export` matches previous `export`");
  40. CARBON_DIAGNOSTIC(ExportPrevious, Note, "previous `export` here");
  41. context.emitter()
  42. .Build(node_id, ExportRedundant)
  43. // Use the location of the export itself, not the exported instruction.
  44. .Note(context.insts().GetLocId(inst_id), ExportPrevious)
  45. .Emit();
  46. return true;
  47. }
  48. auto import_ref = context.insts().TryGetAs<SemIR::ImportRefLoaded>(inst_id);
  49. if (!import_ref) {
  50. CARBON_DIAGNOSTIC(ExportNotImportedEntity, Error,
  51. "only imported entities are valid for `export`");
  52. CARBON_DIAGNOSTIC(ExportNotImportedEntitySource, Note,
  53. "name is declared here");
  54. context.emitter()
  55. .Build(node_id, ExportNotImportedEntity)
  56. .Note(inst_id, ExportNotImportedEntitySource)
  57. .Emit();
  58. return true;
  59. }
  60. auto export_id = context.AddInst<SemIR::ExportDecl>(
  61. node_id, {.type_id = import_ref->type_id,
  62. .entity_name_id = import_ref->entity_name_id,
  63. .value_id = inst_id});
  64. context.AddExport(export_id);
  65. // Replace the ImportRef in name lookup, both for the above duplicate
  66. // diagnostic and so that cross-package imports can find it easily.
  67. auto entity_name = context.entity_names().Get(import_ref->entity_name_id);
  68. auto& parent_scope = context.name_scopes().Get(entity_name.parent_scope_id);
  69. auto& scope_result =
  70. parent_scope.GetEntry(*parent_scope.Lookup(entity_name.name_id)).result;
  71. CARBON_CHECK(scope_result.target_inst_id() == inst_id);
  72. scope_result = SemIR::ScopeLookupResult::MakeFound(
  73. export_id, scope_result.access_kind());
  74. return true;
  75. }
  76. } // namespace Carbon::Check