handle_export.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // The parser supports patterns after `export`, so we need a pattern block
  19. // to handle them.
  20. context.pattern_block_stack().Push();
  21. return true;
  22. }
  23. auto HandleParseNode(Context& context, Parse::ExportDeclId node_id) -> bool {
  24. auto name_context = context.decl_name_stack().FinishName(
  25. PopNameComponentWithoutParams(context, Lex::TokenKind::Export));
  26. context.decl_name_stack().PopScope();
  27. auto introducer =
  28. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Export>();
  29. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::None);
  30. if (name_context.state == DeclNameStack::NameContext::State::Error) {
  31. // Should already be diagnosed.
  32. return true;
  33. }
  34. auto inst_id = name_context.prev_inst_id();
  35. if (!inst_id.is_valid()) {
  36. context.DiagnoseNameNotFound(node_id, name_context.name_id_for_new_inst());
  37. return true;
  38. }
  39. auto inst = context.insts().Get(inst_id);
  40. if (inst.Is<SemIR::ExportDecl>()) {
  41. CARBON_DIAGNOSTIC(ExportRedundant, Warning,
  42. "`export` matches previous `export`");
  43. CARBON_DIAGNOSTIC(ExportPrevious, Note, "previous `export` here");
  44. context.emitter()
  45. .Build(node_id, ExportRedundant)
  46. // Use the location of the export itself, not the exported instruction.
  47. .Note(context.insts().GetLocId(inst_id), ExportPrevious)
  48. .Emit();
  49. return true;
  50. }
  51. auto import_ref = context.insts().TryGetAs<SemIR::ImportRefLoaded>(inst_id);
  52. if (!import_ref) {
  53. CARBON_DIAGNOSTIC(ExportNotImportedEntity, Error,
  54. "only imported entities are valid for `export`");
  55. CARBON_DIAGNOSTIC(ExportNotImportedEntitySource, Note,
  56. "name is declared here");
  57. context.emitter()
  58. .Build(node_id, ExportNotImportedEntity)
  59. .Note(inst_id, ExportNotImportedEntitySource)
  60. .Emit();
  61. return true;
  62. }
  63. auto export_id = context.AddInst<SemIR::ExportDecl>(
  64. node_id, {.type_id = import_ref->type_id,
  65. .entity_name_id = import_ref->entity_name_id,
  66. .value_id = inst_id});
  67. context.AddExport(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_inst_id =
  73. parent_scope.GetEntry(*parent_scope.Lookup(entity_name.name_id)).inst_id;
  74. CARBON_CHECK(scope_inst_id == inst_id);
  75. scope_inst_id = export_id;
  76. return true;
  77. }
  78. } // namespace Carbon::Check