import.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/import.h"
  5. #include "common/check.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/parse/node_ids.h"
  8. #include "toolchain/sem_ir/file.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. #include "toolchain/sem_ir/inst.h"
  11. #include "toolchain/sem_ir/typed_insts.h"
  12. #include "toolchain/sem_ir/value_stores.h"
  13. namespace Carbon::Check {
  14. // Returns name information for the entity, corresponding to IDs in the import
  15. // IR rather than the current IR. May return Invalid for a TODO.
  16. static auto GetImportName(Parse::NodeId parse_node, Context& context,
  17. const SemIR::File& import_sem_ir,
  18. SemIR::Inst import_inst)
  19. -> std::pair<SemIR::NameId, SemIR::NameScopeId> {
  20. switch (import_inst.kind()) {
  21. case SemIR::InstKind::BindName: {
  22. const auto& bind_name = import_sem_ir.bind_names().Get(
  23. import_inst.As<SemIR::BindName>().bind_name_id);
  24. return {bind_name.name_id, bind_name.enclosing_scope_id};
  25. }
  26. case SemIR::InstKind::FunctionDecl: {
  27. const auto& function = import_sem_ir.functions().Get(
  28. import_inst.As<SemIR::FunctionDecl>().function_id);
  29. return {function.name_id, function.enclosing_scope_id};
  30. }
  31. case SemIR::InstKind::Namespace: {
  32. auto namespace_inst = import_inst.As<SemIR::Namespace>();
  33. const auto& scope =
  34. import_sem_ir.name_scopes().Get(namespace_inst.name_scope_id);
  35. return {namespace_inst.name_id, scope.enclosing_scope_id};
  36. }
  37. default:
  38. context.TODO(parse_node, (llvm::Twine("Support GetImportName of ") +
  39. import_inst.kind().name())
  40. .str());
  41. return {SemIR::NameId::Invalid, SemIR::NameScopeId::Invalid};
  42. }
  43. }
  44. // Translate the name to the current IR. It will usually be an identifier, but
  45. // could also be a builtin name ID which is equivalent cross-IR.
  46. static auto CopyNameFromImportIR(Context& context,
  47. const SemIR::File& import_sem_ir,
  48. SemIR::NameId import_name_id) {
  49. if (auto import_identifier_id = import_name_id.AsIdentifierId();
  50. import_identifier_id.is_valid()) {
  51. auto name = import_sem_ir.identifiers().Get(import_identifier_id);
  52. return SemIR::NameId::ForIdentifier(context.identifiers().Add(name));
  53. }
  54. return import_name_id;
  55. }
  56. static auto CacheCopiedNamespace(
  57. llvm::DenseMap<SemIR::NameScopeId, SemIR::NameScopeId>& copied_namespaces,
  58. SemIR::NameScopeId import_scope_id, SemIR::NameScopeId to_scope_id)
  59. -> void {
  60. auto [it, success] = copied_namespaces.insert({import_scope_id, to_scope_id});
  61. CARBON_CHECK(success || it->second == to_scope_id)
  62. << "Copy result for namespace changed from " << import_scope_id << " to "
  63. << to_scope_id;
  64. }
  65. // Copies a namespace from the import IR, returning its ID. This may diagnose
  66. // name conflicts, but that won't change the result because namespaces supersede
  67. // other names in conflicts.
  68. static auto CopySingleNameScopeFromImportIR(
  69. Context& context,
  70. llvm::DenseMap<SemIR::NameScopeId, SemIR::NameScopeId>& copied_namespaces,
  71. SemIR::NameScopeId import_scope_id, SemIR::NameScopeId enclosing_scope_id,
  72. SemIR::NameId name_id, SemIR::TypeId namespace_type_id)
  73. -> SemIR::NameScopeId {
  74. auto& scope = context.name_scopes().Get(enclosing_scope_id);
  75. auto [it, success] = scope.names.insert({name_id, SemIR::InstId::Invalid});
  76. if (!success) {
  77. if (auto namespace_inst =
  78. context.insts().TryGetAs<SemIR::Namespace>(it->second)) {
  79. // Namespaces are open, so we can append to the existing one even if it
  80. // comes from a different file.
  81. CacheCopiedNamespace(copied_namespaces, import_scope_id,
  82. namespace_inst->name_scope_id);
  83. return namespace_inst->name_scope_id;
  84. }
  85. }
  86. // Produce the namespace for the entry. Use the invalid node because there's
  87. // no node to associate with.
  88. auto namespace_inst =
  89. SemIR::Namespace{namespace_type_id, name_id, SemIR::NameScopeId::Invalid};
  90. auto namespace_id =
  91. context.AddPlaceholderInst({Parse::NodeId::Invalid, namespace_inst});
  92. namespace_inst.name_scope_id =
  93. context.name_scopes().Add(namespace_id, enclosing_scope_id);
  94. context.ReplaceInstBeforeConstantUse(
  95. namespace_id, {Parse::NodeId::Invalid, namespace_inst});
  96. // Diagnose if there's a name conflict, but still produce the namespace to
  97. // supersede the name conflict in order to avoid repeat diagnostics.
  98. if (!success) {
  99. context.DiagnoseDuplicateName(namespace_id, it->second);
  100. }
  101. it->second = namespace_id;
  102. CacheCopiedNamespace(copied_namespaces, import_scope_id,
  103. namespace_inst.name_scope_id);
  104. return namespace_inst.name_scope_id;
  105. }
  106. // Copies enclosing name scopes from the import IR. Handles the parent
  107. // traversal. Returns the NameScope corresponding to the copied
  108. // import_enclosing_scope_id.
  109. static auto CopyEnclosingNameScopesFromImportIR(
  110. Context& context, SemIR::TypeId namespace_type_id,
  111. const SemIR::File& import_sem_ir,
  112. SemIR::NameScopeId import_enclosing_scope_id,
  113. llvm::DenseMap<SemIR::NameScopeId, SemIR::NameScopeId>& copied_namespaces)
  114. -> SemIR::NameScopeId {
  115. // Package-level names don't need work.
  116. if (import_enclosing_scope_id == SemIR::NameScopeId::Package) {
  117. return import_enclosing_scope_id;
  118. }
  119. // The scope to add namespaces to. Note this may change while looking at
  120. // enclosing scopes, if we encounter a namespace that's already added.
  121. auto scope_cursor = SemIR::NameScopeId::Package;
  122. // Build a stack of enclosing namespace names, with innermost first.
  123. llvm::SmallVector<std::pair<SemIR::NameScopeId, SemIR::NameId>>
  124. new_namespaces;
  125. while (import_enclosing_scope_id != SemIR::NameScopeId::Package) {
  126. // If the namespace was already copied, reuse the results.
  127. if (auto it = copied_namespaces.find(import_enclosing_scope_id);
  128. it != copied_namespaces.end()) {
  129. // We inject names at the provided scope, and don't need to keep
  130. // traversing parents.
  131. scope_cursor = it->second;
  132. break;
  133. }
  134. // The namespace hasn't been copied yet, so add it to our list.
  135. const auto& scope =
  136. import_sem_ir.name_scopes().Get(import_enclosing_scope_id);
  137. auto scope_inst =
  138. import_sem_ir.insts().GetAs<SemIR::Namespace>(scope.inst_id);
  139. new_namespaces.push_back({scope_inst.name_scope_id, scope_inst.name_id});
  140. import_enclosing_scope_id = scope.enclosing_scope_id;
  141. }
  142. // Add enclosing namespace names, starting with the outermost.
  143. for (auto import_namespace : llvm::reverse(new_namespaces)) {
  144. auto name_id =
  145. CopyNameFromImportIR(context, import_sem_ir, import_namespace.second);
  146. scope_cursor = CopySingleNameScopeFromImportIR(
  147. context, copied_namespaces, import_namespace.first, scope_cursor,
  148. name_id, namespace_type_id);
  149. }
  150. return scope_cursor;
  151. }
  152. auto Import(Context& context, SemIR::TypeId namespace_type_id,
  153. Parse::NodeId import_node, const SemIR::File& import_sem_ir)
  154. -> void {
  155. auto ir_id = context.sem_ir().cross_ref_irs().Add(&import_sem_ir);
  156. for (const auto import_inst_id :
  157. import_sem_ir.inst_blocks().Get(SemIR::InstBlockId::Exports)) {
  158. auto import_inst = import_sem_ir.insts().Get(import_inst_id);
  159. auto [import_name_id, import_enclosing_scope_id] =
  160. GetImportName(import_node, context, import_sem_ir, import_inst);
  161. // TODO: This should only be invalid when GetImportName for an inst
  162. // isn't yet implemented. Long-term this should be removed.
  163. if (!import_name_id.is_valid()) {
  164. continue;
  165. }
  166. llvm::DenseMap<SemIR::NameScopeId, SemIR::NameScopeId> copied_namespaces;
  167. auto name_id = CopyNameFromImportIR(context, import_sem_ir, import_name_id);
  168. SemIR::NameScopeId enclosing_scope_id = CopyEnclosingNameScopesFromImportIR(
  169. context, namespace_type_id, import_sem_ir, import_enclosing_scope_id,
  170. copied_namespaces);
  171. if (auto import_namespace_inst = import_inst.TryAs<SemIR::Namespace>()) {
  172. // Namespaces are always imported because they're essential for
  173. // qualifiers, and the type is simple.
  174. CopySingleNameScopeFromImportIR(
  175. context, copied_namespaces, import_namespace_inst->name_scope_id,
  176. enclosing_scope_id, name_id, namespace_type_id);
  177. } else {
  178. // Leave a placeholder that the inst comes from the other IR.
  179. auto target_id = context.AddPlaceholderInst(
  180. {SemIR::LazyImportRef{.ir_id = ir_id, .inst_id = import_inst_id}});
  181. // TODO: When importing from other packages, the scope's names should
  182. // be changed to allow for ambiguous names. When importing from the
  183. // current package, as is currently being done, we should issue a
  184. // diagnostic on conflicts.
  185. auto [it, success] = context.name_scopes()
  186. .Get(enclosing_scope_id)
  187. .names.insert({name_id, target_id});
  188. if (!success) {
  189. context.DiagnoseDuplicateName(target_id, it->second);
  190. }
  191. }
  192. }
  193. }
  194. } // namespace Carbon::Check