check_unit.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #ifndef CARBON_TOOLCHAIN_CHECK_CHECK_UNIT_H_
  5. #define CARBON_TOOLCHAIN_CHECK_CHECK_UNIT_H_
  6. #include "common/map.h"
  7. #include "llvm/ADT/SmallVector.h"
  8. #include "toolchain/check/check.h"
  9. #include "toolchain/check/context.h"
  10. #include "toolchain/check/diagnostic_emitter.h"
  11. #include "toolchain/parse/node_ids.h"
  12. #include "toolchain/sem_ir/ids.h"
  13. namespace Carbon::Check {
  14. struct UnitAndImports;
  15. // A file's imports corresponding to a single package, for
  16. // `UnitAndImports::package_imports`.
  17. struct PackageImports {
  18. // A given import within the file, with its destination.
  19. struct Import {
  20. Parse::Tree::PackagingNames names;
  21. UnitAndImports* unit_info;
  22. };
  23. // Use the constructor so that the SmallVector is only constructed
  24. // as-needed.
  25. explicit PackageImports(PackageNameId package_id,
  26. Parse::AnyPackagingDeclId node_id)
  27. : package_id(package_id), node_id(node_id) {}
  28. // The identifier of the imported package.
  29. PackageNameId package_id;
  30. // The first `package` or `import` declaration in the file, which declared the
  31. // package's identifier (even if the import failed). Used for associating
  32. // diagnostics not specific to a single import.
  33. Parse::AnyPackagingDeclId node_id;
  34. // The associated `import` instruction. Has a value after a file is checked.
  35. SemIR::InstId import_decl_id = SemIR::InstId::None;
  36. // Whether there's an import that failed to load.
  37. bool has_load_error = false;
  38. // The list of valid imports.
  39. llvm::SmallVector<Import> imports;
  40. };
  41. // Contains information accumulated while checking a `Unit` (primarily import
  42. // information), in addition to the `Unit` itself.
  43. struct UnitAndImports {
  44. // Converts a `NodeId` to a diagnostic location for `UnitAndImports`.
  45. class NodeEmitter : public Diagnostics::Emitter<Parse::NodeId> {
  46. public:
  47. explicit NodeEmitter(Diagnostics::Consumer* consumer,
  48. Parse::GetTreeAndSubtreesFn tree_and_subtrees_getter)
  49. : Emitter(consumer),
  50. tree_and_subtrees_getter_(tree_and_subtrees_getter) {}
  51. protected:
  52. auto ConvertLoc(Parse::NodeId node_id, ContextFnT /*context_fn*/) const
  53. -> Diagnostics::ConvertedLoc override {
  54. return tree_and_subtrees_getter_().NodeToDiagnosticLoc(
  55. node_id, /*token_only=*/false);
  56. }
  57. private:
  58. Parse::GetTreeAndSubtreesFn tree_and_subtrees_getter_;
  59. };
  60. explicit UnitAndImports(Unit* unit,
  61. Parse::GetTreeAndSubtreesFn tree_and_subtrees_getter)
  62. : unit(unit),
  63. err_tracker(*unit->consumer),
  64. emitter(&err_tracker, tree_and_subtrees_getter) {}
  65. auto parse_tree() -> const Parse::Tree& { return unit->sem_ir->parse_tree(); }
  66. auto source() -> const SourceBuffer& {
  67. return parse_tree().tokens().source();
  68. }
  69. Unit* unit;
  70. // Emitter information.
  71. Diagnostics::ErrorTrackingConsumer err_tracker;
  72. NodeEmitter emitter;
  73. // List of the outgoing imports. If a package includes unavailable library
  74. // imports, it has an entry with has_load_error set. Invalid imports (for
  75. // example, `import Main;`) aren't added because they won't add identifiers to
  76. // name lookup.
  77. llvm::SmallVector<PackageImports> package_imports;
  78. // A map of the package names to the outgoing imports above.
  79. Map<PackageNameId, int32_t> package_imports_map;
  80. // List of the `import Cpp` imports.
  81. llvm::SmallVector<Parse::Tree::PackagingNames> cpp_import_names;
  82. // The remaining number of imports which must be checked before this unit can
  83. // be processed.
  84. int32_t imports_remaining = 0;
  85. // A list of incoming imports. This will be empty for `impl` files, because
  86. // imports only touch `api` files.
  87. llvm::SmallVector<UnitAndImports*> incoming_imports;
  88. // The corresponding `api` unit if this is an `impl` file. The entry should
  89. // also be in the corresponding `PackageImports`.
  90. UnitAndImports* api_for_impl = nullptr;
  91. // Whether the unit has been checked.
  92. bool is_checked = false;
  93. };
  94. // Handles checking of a single unit. Requires that all dependencies have been
  95. // checked.
  96. //
  97. // This mainly splits out the single-unit logic from the higher level cross-unit
  98. // logic in check.cpp.
  99. class CheckUnit {
  100. public:
  101. explicit CheckUnit(
  102. UnitAndImports* unit_and_imports,
  103. llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters,
  104. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  105. llvm::StringRef target, llvm::raw_ostream* vlog_stream);
  106. // Produces and checks the IR for the provided unit.
  107. auto Run() -> void;
  108. private:
  109. // Add imports to the root block.
  110. auto InitPackageScopeAndImports() -> void;
  111. // Collects direct imports, for CollectTransitiveImports.
  112. auto CollectDirectImports(llvm::SmallVector<SemIR::ImportIR>& results,
  113. llvm::MutableArrayRef<int> ir_to_result_index,
  114. SemIR::InstId import_decl_id,
  115. const PackageImports& imports, bool is_local)
  116. -> void;
  117. // Collects transitive imports, handling deduplication. These will be unified
  118. // between local_imports and api_imports.
  119. auto CollectTransitiveImports(SemIR::InstId import_decl_id,
  120. const PackageImports* local_imports,
  121. const PackageImports* api_imports)
  122. -> llvm::SmallVector<SemIR::ImportIR>;
  123. // Imports the current package.
  124. auto ImportCurrentPackage(SemIR::InstId package_inst_id,
  125. SemIR::TypeId namespace_type_id) -> void;
  126. // Imports all other Carbon packages (excluding the current package).
  127. auto ImportOtherPackages(SemIR::TypeId namespace_type_id) -> void;
  128. // Checks that each required declaration is available. This applies for
  129. // declarations that should exist in an owning library, for which an extern
  130. // declaration exists that assigns ownership to the current API.
  131. auto CheckRequiredDeclarations() -> void;
  132. // Checks that each required definition is available. If the definition can be
  133. // generated by resolving a specific, does so, otherwise emits a diagnostic
  134. // for each declaration in context.definitions_required_by_decl() and
  135. // context.definitions_required_by_use that doesn't have a definition.
  136. auto CheckRequiredDefinitions() -> void;
  137. // Re-run every impl lookup with a concrete result and make sure they find the
  138. // same witnesses.
  139. auto CheckPoisonedConcreteImplLookupQueries() -> void;
  140. // Look for `impl` declarations that are invalid.
  141. auto CheckImpls() -> void;
  142. // Does work after processing the parse tree, such as finishing the IR and
  143. // checking for missing contents.
  144. auto FinishRun() -> void;
  145. // Loops over all nodes in the tree. On some errors, this may return early,
  146. // for example if an unrecoverable state is encountered.
  147. // NOLINTNEXTLINE(readability-function-size)
  148. auto ProcessNodeIds() -> bool;
  149. UnitAndImports* unit_and_imports_;
  150. Parse::GetTreeAndSubtreesFn tree_and_subtrees_getter_;
  151. // The number of IRs being checked in total.
  152. int total_ir_count_;
  153. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs_;
  154. llvm::StringRef target_;
  155. DiagnosticEmitter emitter_;
  156. Context context_;
  157. };
  158. } // namespace Carbon::Check
  159. #endif // CARBON_TOOLCHAIN_CHECK_CHECK_UNIT_H_