handle_export.cpp 3.4 KB

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