import.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. // Creates a namespace. The type ID is builtin, and reused to avoid duplicative
  57. // canonicalization.
  58. static auto AddNamespace(Context& context,
  59. SemIR::NameScopeId enclosing_scope_id,
  60. SemIR::NameId name_id, SemIR::TypeId namespace_type_id)
  61. -> std::pair<SemIR::InstId, SemIR::NameScopeId> {
  62. // Use the invalid node because there's no node to associate with.
  63. auto inst =
  64. SemIR::Namespace{namespace_type_id, name_id, SemIR::NameScopeId::Invalid};
  65. auto id = context.AddPlaceholderInst({Parse::NodeId::Invalid, inst});
  66. inst.name_scope_id = context.name_scopes().Add(id, enclosing_scope_id);
  67. context.ReplaceInstBeforeConstantUse(id, {Parse::NodeId::Invalid, inst});
  68. return {id, inst.name_scope_id};
  69. }
  70. static auto CacheCopiedNamespace(
  71. llvm::DenseMap<SemIR::NameScopeId, SemIR::NameScopeId>& copied_namespaces,
  72. SemIR::NameScopeId import_scope_id, SemIR::NameScopeId to_scope_id)
  73. -> void {
  74. auto [it, success] = copied_namespaces.insert({import_scope_id, to_scope_id});
  75. CARBON_CHECK(success || it->second == to_scope_id)
  76. << "Copy result for namespace changed from " << import_scope_id << " to "
  77. << to_scope_id;
  78. }
  79. // Copies enclosing name scopes from the import IR. Handles the parent
  80. // traversal. Returns the NameScope corresponding to the copied
  81. // import_enclosing_scope_id.
  82. static auto CopyEnclosingNameScopeFromImportIR(
  83. Context& context, SemIR::TypeId namespace_type_id,
  84. const SemIR::File& import_sem_ir,
  85. SemIR::NameScopeId import_enclosing_scope_id,
  86. llvm::DenseMap<SemIR::NameScopeId, SemIR::NameScopeId>& copied_namespaces)
  87. -> SemIR::NameScopeId {
  88. // Package-level names don't need work.
  89. if (import_enclosing_scope_id == SemIR::NameScopeId::Package) {
  90. return import_enclosing_scope_id;
  91. }
  92. // The scope to add namespaces to. Note this may change while looking at
  93. // enclosing scopes, if we encounter a namespace that's already added.
  94. auto scope_cursor = SemIR::NameScopeId::Package;
  95. // Build a stack of enclosing namespace names, with innermost first.
  96. llvm::SmallVector<std::pair<SemIR::NameScopeId, SemIR::NameId>>
  97. new_namespaces;
  98. while (import_enclosing_scope_id != SemIR::NameScopeId::Package) {
  99. // If the namespace was already copied, reuse the results.
  100. if (auto it = copied_namespaces.find(import_enclosing_scope_id);
  101. it != copied_namespaces.end()) {
  102. // We inject names at the provided scope, and don't need to keep
  103. // traversing parents.
  104. scope_cursor = it->second;
  105. break;
  106. }
  107. // The namespace hasn't been copied yet, so add it to our list.
  108. const auto& scope =
  109. import_sem_ir.name_scopes().Get(import_enclosing_scope_id);
  110. auto scope_inst =
  111. import_sem_ir.insts().GetAs<SemIR::Namespace>(scope.inst_id);
  112. new_namespaces.push_back({scope_inst.name_scope_id, scope_inst.name_id});
  113. import_enclosing_scope_id = scope.enclosing_scope_id;
  114. }
  115. // Add enclosing namespace names, starting with the outermost.
  116. for (auto import_namespace : llvm::reverse(new_namespaces)) {
  117. auto name_id =
  118. CopyNameFromImportIR(context, import_sem_ir, import_namespace.second);
  119. auto& scope = context.name_scopes().Get(scope_cursor);
  120. auto [it, success] = scope.names.insert({name_id, SemIR::InstId::Invalid});
  121. if (!success) {
  122. auto inst = context.insts().Get(it->second);
  123. if (auto namespace_inst = inst.TryAs<SemIR::Namespace>()) {
  124. // Namespaces are open, so we can append to the existing one even if it
  125. // comes from a different file.
  126. scope_cursor = namespace_inst->name_scope_id;
  127. CacheCopiedNamespace(copied_namespaces, import_namespace.first,
  128. scope_cursor);
  129. continue;
  130. }
  131. // Produce a diagnostic, but still produce the namespace to supersede the
  132. // name conflict in order to avoid repeat diagnostics.
  133. // TODO: Produce a diagnostic.
  134. }
  135. // Produce the namespace for the entry.
  136. auto [namespace_id, name_scope_id] =
  137. AddNamespace(context, scope_cursor, name_id, namespace_type_id);
  138. it->second = namespace_id;
  139. scope_cursor = name_scope_id;
  140. CacheCopiedNamespace(copied_namespaces, import_namespace.first,
  141. scope_cursor);
  142. }
  143. return scope_cursor;
  144. }
  145. auto Import(Context& context, SemIR::TypeId namespace_type_id,
  146. Parse::NodeId import_node, const SemIR::File& import_sem_ir)
  147. -> void {
  148. auto ir_id = context.sem_ir().cross_ref_irs().Add(&import_sem_ir);
  149. for (const auto import_inst_id :
  150. import_sem_ir.inst_blocks().Get(SemIR::InstBlockId::Exports)) {
  151. auto import_inst = import_sem_ir.insts().Get(import_inst_id);
  152. auto [import_name_id, import_enclosing_scope_id] =
  153. GetImportName(import_node, context, import_sem_ir, import_inst);
  154. // TODO: This should only be invalid when GetImportName for an inst
  155. // isn't yet implemented. Long-term this should be removed.
  156. if (!import_name_id.is_valid()) {
  157. continue;
  158. }
  159. llvm::DenseMap<SemIR::NameScopeId, SemIR::NameScopeId> copied_namespaces;
  160. auto name_id = CopyNameFromImportIR(context, import_sem_ir, import_name_id);
  161. SemIR::NameScopeId enclosing_scope_id = CopyEnclosingNameScopeFromImportIR(
  162. context, namespace_type_id, import_sem_ir, import_enclosing_scope_id,
  163. copied_namespaces);
  164. if (auto import_namespace_inst = import_inst.TryAs<SemIR::Namespace>()) {
  165. // Namespaces are always imported because they're essential for
  166. // qualifiers, and the type is simple.
  167. auto [namespace_id, name_scope_id] =
  168. AddNamespace(context, enclosing_scope_id, name_id, namespace_type_id);
  169. context.name_scopes().AddEntry(enclosing_scope_id, name_id, namespace_id);
  170. CacheCopiedNamespace(copied_namespaces,
  171. import_namespace_inst->name_scope_id, name_scope_id);
  172. } else {
  173. // Leave a placeholder that the inst comes from the other IR.
  174. auto target_id = context.AddPlaceholderInst(
  175. {Parse::NodeId::Invalid,
  176. SemIR::LazyImportRef{.ir_id = ir_id, .inst_id = import_inst_id}});
  177. // TODO: When importing from other packages, the scope's names should
  178. // be changed to allow for ambiguous names. When importing from the
  179. // current package, as is currently being done, we should issue a
  180. // diagnostic on conflicts.
  181. context.name_scopes().AddEntry(enclosing_scope_id, name_id, target_id);
  182. }
  183. }
  184. }
  185. } // namespace Carbon::Check