handle_export.cpp 3.5 KB

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