handle_export.cpp 3.2 KB

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