handle_export.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Exporting uses the decl name primarily for lookup, so treat poisoning the
  34. // same as "not found".
  35. auto inst_id =
  36. name_context.state == DeclNameStack::NameContext::State::Poisoned
  37. ? SemIR::InstId::None
  38. : name_context.prev_inst_id();
  39. if (!inst_id.has_value()) {
  40. DiagnoseNameNotFound(context, node_id, name_context.name_id_for_new_inst());
  41. return true;
  42. }
  43. auto inst = context.insts().Get(inst_id);
  44. if (inst.Is<SemIR::ExportDecl>()) {
  45. CARBON_DIAGNOSTIC(ExportRedundant, Warning,
  46. "`export` matches previous `export`");
  47. CARBON_DIAGNOSTIC(ExportPrevious, Note, "previous `export` here");
  48. context.emitter()
  49. .Build(node_id, ExportRedundant)
  50. // Use the location of the export itself, not the exported instruction.
  51. .Note(context.insts().GetLocId(inst_id), ExportPrevious)
  52. .Emit();
  53. return true;
  54. }
  55. auto import_ref = context.insts().TryGetAs<SemIR::ImportRefLoaded>(inst_id);
  56. if (!import_ref) {
  57. CARBON_DIAGNOSTIC(ExportNotImportedEntity, Error,
  58. "only imported entities are valid for `export`");
  59. CARBON_DIAGNOSTIC(ExportNotImportedEntitySource, Note,
  60. "name is declared here");
  61. context.emitter()
  62. .Build(node_id, ExportNotImportedEntity)
  63. .Note(inst_id, ExportNotImportedEntitySource)
  64. .Emit();
  65. return true;
  66. }
  67. auto export_id =
  68. AddInst<SemIR::ExportDecl>(context, node_id,
  69. {.type_id = import_ref->type_id,
  70. .entity_name_id = import_ref->entity_name_id,
  71. .value_id = inst_id});
  72. context.exports().push_back(export_id);
  73. // Replace the ImportRef in name lookup, both for the above duplicate
  74. // diagnostic and so that cross-package imports can find it easily.
  75. auto entity_name = context.entity_names().Get(import_ref->entity_name_id);
  76. auto& parent_scope = context.name_scopes().Get(entity_name.parent_scope_id);
  77. auto& scope_result =
  78. parent_scope.GetEntry(*parent_scope.Lookup(entity_name.name_id)).result;
  79. CARBON_CHECK(scope_result.target_inst_id() == inst_id);
  80. scope_result = SemIR::ScopeLookupResult::MakeFound(
  81. export_id, scope_result.access_kind());
  82. return true;
  83. }
  84. } // namespace Carbon::Check