handle_export.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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/generic.h"
  7. #include "toolchain/check/handle.h"
  8. #include "toolchain/check/inst.h"
  9. #include "toolchain/check/modifiers.h"
  10. #include "toolchain/check/name_component.h"
  11. #include "toolchain/check/name_lookup.h"
  12. #include "toolchain/parse/typed_nodes.h"
  13. #include "toolchain/sem_ir/ids.h"
  14. #include "toolchain/sem_ir/typed_insts.h"
  15. namespace Carbon::Check {
  16. auto HandleParseNode(Context& context, Parse::ExportIntroducerId /*node_id*/)
  17. -> bool {
  18. // Export declarations can't be generic, but we might have parsed a generic
  19. // parameter in their name, so enter a generic scope just in case.
  20. StartGenericDecl(context);
  21. context.decl_introducer_state_stack().Push<Lex::TokenKind::Export>();
  22. // TODO: Probably need to update DeclNameStack to restrict to only namespaces.
  23. context.decl_name_stack().PushScopeAndStartName();
  24. return true;
  25. }
  26. auto HandleParseNode(Context& context, Parse::ExportDeclId node_id) -> bool {
  27. auto name_context = context.decl_name_stack().FinishName(
  28. PopNameComponentWithoutParams(context, Lex::TokenKind::Export));
  29. DiscardGenericDecl(context);
  30. context.decl_name_stack().PopScope();
  31. auto introducer =
  32. context.decl_introducer_state_stack().Pop<Lex::TokenKind::Export>();
  33. LimitModifiersOnDecl(context, introducer, KeywordModifierSet::None);
  34. if (name_context.state == DeclNameStack::NameContext::State::Error) {
  35. // Should already be diagnosed.
  36. return true;
  37. }
  38. // Exporting uses the decl name primarily for lookup, so treat poisoning the
  39. // same as "not found".
  40. auto inst_id =
  41. name_context.state == DeclNameStack::NameContext::State::Poisoned
  42. ? SemIR::InstId::None
  43. : name_context.prev_inst_id();
  44. if (!inst_id.has_value()) {
  45. DiagnoseNameNotFound(context, node_id, name_context.name_id_for_new_inst());
  46. return true;
  47. }
  48. auto inst = context.insts().Get(inst_id);
  49. if (inst.Is<SemIR::ExportDecl>()) {
  50. CARBON_DIAGNOSTIC(ExportRedundant, Warning,
  51. "`export` matches previous `export`");
  52. CARBON_DIAGNOSTIC(ExportPrevious, Note, "previous `export` here");
  53. context.emitter()
  54. .Build(node_id, ExportRedundant)
  55. // Use the location of the export itself, not the exported instruction.
  56. //
  57. // TODO: This construction of a LocId that does not just contain the
  58. // InstId prevents GetAbsoluteNodeIdImpl() from seeing the `ExportDecl`
  59. // instruction, which prevents it from chasing through it to the entity
  60. // being exported. It might be nice to make this more explicit.
  61. .Note(context.insts().GetCanonicalLocId(inst_id), ExportPrevious)
  62. .Emit();
  63. return true;
  64. }
  65. auto import_ref = context.insts().TryGetAs<SemIR::ImportRefLoaded>(inst_id);
  66. if (!import_ref) {
  67. CARBON_DIAGNOSTIC(ExportNotImportedEntity, Error,
  68. "only imported entities are valid for `export`");
  69. CARBON_DIAGNOSTIC(ExportNotImportedEntitySource, Note,
  70. "name is declared here");
  71. context.emitter()
  72. .Build(node_id, ExportNotImportedEntity)
  73. .Note(inst_id, ExportNotImportedEntitySource)
  74. .Emit();
  75. return true;
  76. }
  77. auto export_id =
  78. AddInst<SemIR::ExportDecl>(context, node_id,
  79. {.type_id = import_ref->type_id,
  80. .entity_name_id = import_ref->entity_name_id,
  81. .value_id = inst_id});
  82. context.exports().push_back(export_id);
  83. // Replace the ImportRef in name lookup, both for the above duplicate
  84. // diagnostic and so that cross-package imports can find it easily.
  85. auto entity_name = context.entity_names().Get(import_ref->entity_name_id);
  86. auto& parent_scope = context.name_scopes().Get(entity_name.parent_scope_id);
  87. auto& scope_result =
  88. parent_scope.GetEntry(*parent_scope.Lookup(entity_name.name_id)).result;
  89. CARBON_CHECK(scope_result.target_inst_id() == inst_id);
  90. scope_result = SemIR::ScopeLookupResult::MakeFound(
  91. export_id, scope_result.access_kind());
  92. return true;
  93. }
  94. } // namespace Carbon::Check