handle_export.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 HandleExportIntroducer(Context& context,
  14. Parse::ExportIntroducerId /*node_id*/) -> 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 HandleExportDecl(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.is_valid()) {
  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. .bind_name_id = import_ref->bind_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 bind_name = context.bind_names().Get(import_ref->bind_name_id);
  68. auto& names = context.name_scopes().Get(bind_name.parent_scope_id).names;
  69. auto it = names.find(bind_name.name_id);
  70. CARBON_CHECK(it->second == inst_id);
  71. it->second = export_id;
  72. return true;
  73. }
  74. } // namespace Carbon::Check