check_unit.h 6.5 KB

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