check_unit.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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/check_unit.h"
  5. #include <string>
  6. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  7. #include "llvm/ADT/StringRef.h"
  8. #include "llvm/Support/VirtualFileSystem.h"
  9. #include "toolchain/base/kind_switch.h"
  10. #include "toolchain/base/pretty_stack_trace_function.h"
  11. #include "toolchain/check/generic.h"
  12. #include "toolchain/check/handle.h"
  13. #include "toolchain/check/impl.h"
  14. #include "toolchain/check/import.h"
  15. #include "toolchain/check/import_cpp.h"
  16. #include "toolchain/check/import_ref.h"
  17. #include "toolchain/check/node_id_traversal.h"
  18. namespace Carbon::Check {
  19. // Returns the number of imported IRs, to assist in Context construction.
  20. static auto GetImportedIRCount(UnitAndImports* unit_and_imports) -> int {
  21. int count = 0;
  22. for (auto& package_imports : unit_and_imports->package_imports) {
  23. count += package_imports.imports.size();
  24. }
  25. if (!unit_and_imports->api_for_impl) {
  26. // Leave an empty slot for ImportIRId::ApiForImpl.
  27. ++count;
  28. }
  29. return count;
  30. }
  31. CheckUnit::CheckUnit(UnitAndImports* unit_and_imports, int total_ir_count,
  32. llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
  33. llvm::raw_ostream* vlog_stream)
  34. : unit_and_imports_(unit_and_imports),
  35. total_ir_count_(total_ir_count),
  36. fs_(std::move(fs)),
  37. vlog_stream_(vlog_stream),
  38. emitter_(*unit_and_imports_->unit->sem_ir_converter,
  39. unit_and_imports_->err_tracker),
  40. context_(&emitter_, unit_and_imports_->unit->get_parse_tree_and_subtrees,
  41. unit_and_imports_->unit->sem_ir,
  42. GetImportedIRCount(unit_and_imports), total_ir_count,
  43. vlog_stream) {}
  44. auto CheckUnit::Run() -> void {
  45. Timings::ScopedTiming timing(unit_and_imports_->unit->timings, "check");
  46. // We can safely mark this as checked at the start.
  47. unit_and_imports_->is_checked = true;
  48. PrettyStackTraceFunction context_dumper(
  49. [&](llvm::raw_ostream& output) { context_.PrintForStackDump(output); });
  50. // Add a block for the file.
  51. context_.inst_block_stack().Push();
  52. InitPackageScopeAndImports();
  53. // Eagerly import the impls declared in the api file to prepare to redeclare
  54. // them.
  55. ImportImplsFromApiFile(context_);
  56. if (!ProcessNodeIds()) {
  57. context_.sem_ir().set_has_errors(true);
  58. return;
  59. }
  60. CheckRequiredDefinitions();
  61. context_.Finalize();
  62. context_.VerifyOnFinish();
  63. context_.sem_ir().set_has_errors(unit_and_imports_->err_tracker.seen_error());
  64. #ifndef NDEBUG
  65. if (auto verify = context_.sem_ir().Verify(); !verify.ok()) {
  66. CARBON_FATAL("{0}Built invalid semantics IR: {1}\n", context_.sem_ir(),
  67. verify.error());
  68. }
  69. #endif
  70. }
  71. auto CheckUnit::InitPackageScopeAndImports() -> void {
  72. // Importing makes many namespaces, so only canonicalize the type once.
  73. auto namespace_type_id =
  74. context_.GetSingletonType(SemIR::NamespaceType::SingletonInstId);
  75. // Define the package scope, with an instruction for `package` expressions to
  76. // reference.
  77. auto package_scope_id = context_.name_scopes().Add(
  78. SemIR::Namespace::PackageInstId, SemIR::NameId::PackageNamespace,
  79. SemIR::NameScopeId::Invalid);
  80. CARBON_CHECK(package_scope_id == SemIR::NameScopeId::Package);
  81. auto package_inst_id = context_.AddInst<SemIR::Namespace>(
  82. Parse::NodeId::Invalid, {.type_id = namespace_type_id,
  83. .name_scope_id = SemIR::NameScopeId::Package,
  84. .import_id = SemIR::InstId::Invalid});
  85. CARBON_CHECK(package_inst_id == SemIR::Namespace::PackageInstId);
  86. // If there is an implicit `api` import, set it first so that it uses the
  87. // ImportIRId::ApiForImpl when processed for imports.
  88. if (unit_and_imports_->api_for_impl) {
  89. const auto& names = context_.parse_tree().packaging_decl()->names;
  90. auto import_decl_id = context_.AddInst<SemIR::ImportDecl>(
  91. names.node_id,
  92. {.package_id = SemIR::NameId::ForIdentifier(names.package_id)});
  93. SetApiImportIR(context_,
  94. {.decl_id = import_decl_id,
  95. .is_export = false,
  96. .sem_ir = unit_and_imports_->api_for_impl->unit->sem_ir});
  97. } else {
  98. SetApiImportIR(context_,
  99. {.decl_id = SemIR::InstId::Invalid, .sem_ir = nullptr});
  100. }
  101. // Add import instructions for everything directly imported. Implicit imports
  102. // are handled separately.
  103. for (auto& package_imports : unit_and_imports_->package_imports) {
  104. CARBON_CHECK(!package_imports.import_decl_id.is_valid());
  105. package_imports.import_decl_id = context_.AddInst<SemIR::ImportDecl>(
  106. package_imports.node_id, {.package_id = SemIR::NameId::ForIdentifier(
  107. package_imports.package_id)});
  108. }
  109. // Process the imports.
  110. if (unit_and_imports_->api_for_impl) {
  111. ImportApiFile(context_, namespace_type_id,
  112. *unit_and_imports_->api_for_impl->unit->sem_ir);
  113. }
  114. ImportCurrentPackage(package_inst_id, namespace_type_id);
  115. CARBON_CHECK(context_.scope_stack().PeekIndex() == ScopeIndex::Package);
  116. ImportOtherPackages(namespace_type_id);
  117. ImportCppPackages();
  118. }
  119. auto CheckUnit::CollectDirectImports(
  120. llvm::SmallVector<SemIR::ImportIR>& results,
  121. llvm::MutableArrayRef<int> ir_to_result_index, SemIR::InstId import_decl_id,
  122. const PackageImports& imports, bool is_local) -> void {
  123. for (const auto& import : imports.imports) {
  124. const auto& direct_ir = *import.unit_info->unit->sem_ir;
  125. auto& index = ir_to_result_index[direct_ir.check_ir_id().index];
  126. if (index != -1) {
  127. // This should only happen when doing API imports for an implementation
  128. // file. Don't change the entry; is_export doesn't matter.
  129. continue;
  130. }
  131. index = results.size();
  132. results.push_back({.decl_id = import_decl_id,
  133. // Only tag exports in API files, ignoring the value in
  134. // implementation files.
  135. .is_export = is_local && import.names.is_export,
  136. .sem_ir = &direct_ir});
  137. }
  138. }
  139. auto CheckUnit::CollectTransitiveImports(SemIR::InstId import_decl_id,
  140. const PackageImports* local_imports,
  141. const PackageImports* api_imports)
  142. -> llvm::SmallVector<SemIR::ImportIR> {
  143. llvm::SmallVector<SemIR::ImportIR> results;
  144. // Track whether an IR was imported in full, including `export import`. This
  145. // distinguishes from IRs that are indirectly added without all names being
  146. // exported to this IR.
  147. llvm::SmallVector<int> ir_to_result_index(total_ir_count_, -1);
  148. // First add direct imports. This means that if an entity is imported both
  149. // directly and indirectly, the import path will reflect the direct import.
  150. if (local_imports) {
  151. CollectDirectImports(results, ir_to_result_index, import_decl_id,
  152. *local_imports,
  153. /*is_local=*/true);
  154. }
  155. if (api_imports) {
  156. CollectDirectImports(results, ir_to_result_index, import_decl_id,
  157. *api_imports,
  158. /*is_local=*/false);
  159. }
  160. // Loop through direct imports for any indirect exports. The underlying vector
  161. // is appended during iteration, so take the size first.
  162. const int direct_imports = results.size();
  163. for (int direct_index : llvm::seq(direct_imports)) {
  164. bool is_export = results[direct_index].is_export;
  165. for (const auto& indirect_ir :
  166. results[direct_index].sem_ir->import_irs().array_ref()) {
  167. if (!indirect_ir.is_export) {
  168. continue;
  169. }
  170. auto& indirect_index =
  171. ir_to_result_index[indirect_ir.sem_ir->check_ir_id().index];
  172. if (indirect_index == -1) {
  173. indirect_index = results.size();
  174. // TODO: In the case of a recursive `export import`, this only points at
  175. // the outermost import. May want something that better reflects the
  176. // recursion.
  177. results.push_back({.decl_id = results[direct_index].decl_id,
  178. .is_export = is_export,
  179. .sem_ir = indirect_ir.sem_ir});
  180. } else if (is_export) {
  181. results[indirect_index].is_export = true;
  182. }
  183. }
  184. }
  185. return results;
  186. }
  187. auto CheckUnit::ImportCurrentPackage(SemIR::InstId package_inst_id,
  188. SemIR::TypeId namespace_type_id) -> void {
  189. // Add imports from the current package.
  190. auto import_map_lookup =
  191. unit_and_imports_->package_imports_map.Lookup(IdentifierId::Invalid);
  192. if (!import_map_lookup) {
  193. // Push the scope; there are no names to add.
  194. context_.scope_stack().Push(package_inst_id, SemIR::NameScopeId::Package);
  195. return;
  196. }
  197. PackageImports& self_import =
  198. unit_and_imports_->package_imports[import_map_lookup.value()];
  199. if (self_import.has_load_error) {
  200. context_.name_scopes().Get(SemIR::NameScopeId::Package).set_has_error();
  201. }
  202. ImportLibrariesFromCurrentPackage(
  203. context_, namespace_type_id,
  204. CollectTransitiveImports(self_import.import_decl_id, &self_import,
  205. /*api_imports=*/nullptr));
  206. context_.scope_stack().Push(
  207. package_inst_id, SemIR::NameScopeId::Package, SemIR::SpecificId::Invalid,
  208. context_.name_scopes().Get(SemIR::NameScopeId::Package).has_error());
  209. }
  210. auto CheckUnit::ImportOtherPackages(SemIR::TypeId namespace_type_id) -> void {
  211. // api_imports_list is initially the size of the current file's imports,
  212. // including for API files, for simplicity in iteration. It's only really used
  213. // when processing an implementation file, in order to combine the API file
  214. // imports.
  215. //
  216. // For packages imported by the API file, the IdentifierId is the package name
  217. // and the index is into the API's import list. Otherwise, the initial
  218. // {Invalid, -1} state remains.
  219. llvm::SmallVector<std::pair<IdentifierId, int32_t>> api_imports_list;
  220. api_imports_list.resize(unit_and_imports_->package_imports.size(),
  221. {IdentifierId::Invalid, -1});
  222. // When there's an API file, add the mapping to api_imports_list.
  223. if (unit_and_imports_->api_for_impl) {
  224. const auto& api_identifiers =
  225. unit_and_imports_->api_for_impl->unit->value_stores->identifiers();
  226. auto& impl_identifiers =
  227. unit_and_imports_->unit->value_stores->identifiers();
  228. for (auto [api_imports_index, api_imports] :
  229. llvm::enumerate(unit_and_imports_->api_for_impl->package_imports)) {
  230. // Skip the current package.
  231. if (!api_imports.package_id.is_valid()) {
  232. continue;
  233. }
  234. // Translate the package ID from the API file to the implementation file.
  235. auto impl_package_id =
  236. impl_identifiers.Add(api_identifiers.Get(api_imports.package_id));
  237. if (auto lookup =
  238. unit_and_imports_->package_imports_map.Lookup(impl_package_id)) {
  239. // On a hit, replace the entry to unify the API and implementation
  240. // imports.
  241. api_imports_list[lookup.value()] = {impl_package_id, api_imports_index};
  242. } else {
  243. // On a miss, add the package as API-only.
  244. api_imports_list.push_back({impl_package_id, api_imports_index});
  245. }
  246. }
  247. }
  248. for (auto [i, api_imports_entry] : llvm::enumerate(api_imports_list)) {
  249. // These variables are updated after figuring out which imports are present.
  250. auto import_decl_id = SemIR::InstId::Invalid;
  251. IdentifierId package_id = IdentifierId::Invalid;
  252. bool has_load_error = false;
  253. // Identify the local package imports if present.
  254. PackageImports* local_imports = nullptr;
  255. if (i < unit_and_imports_->package_imports.size()) {
  256. local_imports = &unit_and_imports_->package_imports[i];
  257. if (!local_imports->package_id.is_valid()) {
  258. // Skip the current package.
  259. continue;
  260. }
  261. import_decl_id = local_imports->import_decl_id;
  262. package_id = local_imports->package_id;
  263. has_load_error |= local_imports->has_load_error;
  264. }
  265. // Identify the API package imports if present.
  266. PackageImports* api_imports = nullptr;
  267. if (api_imports_entry.second != -1) {
  268. api_imports = &unit_and_imports_->api_for_impl
  269. ->package_imports[api_imports_entry.second];
  270. if (local_imports) {
  271. CARBON_CHECK(package_id == api_imports_entry.first);
  272. } else {
  273. auto import_ir_inst_id = context_.import_ir_insts().Add(
  274. {.ir_id = SemIR::ImportIRId::ApiForImpl,
  275. .inst_id = api_imports->import_decl_id});
  276. import_decl_id =
  277. context_.AddInst(context_.MakeImportedLocAndInst<SemIR::ImportDecl>(
  278. import_ir_inst_id, {.package_id = SemIR::NameId::ForIdentifier(
  279. api_imports_entry.first)}));
  280. package_id = api_imports_entry.first;
  281. }
  282. has_load_error |= api_imports->has_load_error;
  283. }
  284. // Do the actual import.
  285. ImportLibrariesFromOtherPackage(
  286. context_, namespace_type_id, import_decl_id, package_id,
  287. CollectTransitiveImports(import_decl_id, local_imports, api_imports),
  288. has_load_error);
  289. }
  290. }
  291. auto CheckUnit::ImportCppPackages() -> void {
  292. const auto& imports = unit_and_imports_->cpp_imports;
  293. if (imports.empty()) {
  294. return;
  295. }
  296. if (imports.size() >= 2) {
  297. context_.TODO(imports[1].node_id,
  298. "multiple Cpp imports are not yet supported");
  299. return;
  300. }
  301. const auto& import = imports.front();
  302. llvm::StringRef filename =
  303. unit_and_imports_->unit->value_stores->string_literal_values().Get(
  304. import.library_id);
  305. // TODO: Pass the import location so that diagnostics would point to it.
  306. auto source_buffer = SourceBuffer::MakeFromFile(
  307. *fs_, filename, unit_and_imports_->err_tracker);
  308. if (!source_buffer) {
  309. return;
  310. }
  311. ImportCppFile(context_, import.node_id, fs_, source_buffer->filename(),
  312. source_buffer->text());
  313. }
  314. // Loops over all nodes in the tree. On some errors, this may return early,
  315. // for example if an unrecoverable state is encountered.
  316. // NOLINTNEXTLINE(readability-function-size)
  317. auto CheckUnit::ProcessNodeIds() -> bool {
  318. NodeIdTraversal traversal(context_, vlog_stream_);
  319. Parse::NodeId node_id = Parse::NodeId::Invalid;
  320. // On crash, report which token we were handling.
  321. PrettyStackTraceFunction node_dumper([&](llvm::raw_ostream& output) {
  322. auto converted = unit_and_imports_->unit->node_converter->ConvertLoc(
  323. node_id, [](DiagnosticLoc, const DiagnosticBase<>&) {});
  324. converted.loc.FormatLocation(output);
  325. output << ": checking " << context_.parse_tree().node_kind(node_id) << "\n";
  326. // Crash output has a tab indent; try to indent slightly past that.
  327. converted.loc.FormatSnippet(output, /*indent=*/10);
  328. });
  329. while (auto maybe_node_id = traversal.Next()) {
  330. node_id = *maybe_node_id;
  331. unit_and_imports_->unit->sem_ir_converter->AdvanceToken(
  332. context_.parse_tree().node_token(node_id));
  333. if (context_.parse_tree().node_has_error(node_id)) {
  334. context_.TODO(node_id, "handle invalid parse trees in `check`");
  335. return false;
  336. }
  337. bool result;
  338. auto parse_kind = context_.parse_tree().node_kind(node_id);
  339. switch (parse_kind) {
  340. #define CARBON_PARSE_NODE_KIND(Name) \
  341. case Parse::NodeKind::Name: { \
  342. result = HandleParseNode(context_, Parse::Name##Id(node_id)); \
  343. break; \
  344. }
  345. #include "toolchain/parse/node_kind.def"
  346. }
  347. if (!result) {
  348. CARBON_CHECK(
  349. unit_and_imports_->err_tracker.seen_error(),
  350. "HandleParseNode for `{0}` returned false without diagnosing.",
  351. parse_kind);
  352. return false;
  353. }
  354. traversal.Handle(parse_kind);
  355. }
  356. return true;
  357. }
  358. auto CheckUnit::CheckRequiredDefinitions() -> void {
  359. CARBON_DIAGNOSTIC(MissingDefinitionInImpl, Error,
  360. "no definition found for declaration in impl file");
  361. // Note that more required definitions can be added during this loop.
  362. for (size_t i = 0; i != context_.definitions_required().size(); ++i) {
  363. SemIR::InstId decl_inst_id = context_.definitions_required()[i];
  364. SemIR::Inst decl_inst = context_.insts().Get(decl_inst_id);
  365. CARBON_KIND_SWITCH(context_.insts().Get(decl_inst_id)) {
  366. case CARBON_KIND(SemIR::ClassDecl class_decl): {
  367. if (!context_.classes().Get(class_decl.class_id).is_defined()) {
  368. emitter_.Emit(decl_inst_id, MissingDefinitionInImpl);
  369. }
  370. break;
  371. }
  372. case CARBON_KIND(SemIR::FunctionDecl function_decl): {
  373. if (context_.functions().Get(function_decl.function_id).definition_id ==
  374. SemIR::InstId::Invalid) {
  375. emitter_.Emit(decl_inst_id, MissingDefinitionInImpl);
  376. }
  377. break;
  378. }
  379. case CARBON_KIND(SemIR::ImplDecl impl_decl): {
  380. auto& impl = context_.impls().Get(impl_decl.impl_id);
  381. if (!impl.is_defined()) {
  382. FillImplWitnessWithErrors(context_, impl);
  383. CARBON_DIAGNOSTIC(ImplMissingDefinition, Error,
  384. "impl declared but not defined");
  385. emitter_.Emit(decl_inst_id, ImplMissingDefinition);
  386. }
  387. break;
  388. }
  389. case SemIR::InterfaceDecl::Kind: {
  390. // TODO: Handle `interface` as well, once we can test it without
  391. // triggering
  392. // https://github.com/carbon-language/carbon-lang/issues/4071.
  393. CARBON_FATAL("TODO: Support interfaces in DiagnoseMissingDefinitions");
  394. }
  395. case CARBON_KIND(SemIR::SpecificFunction specific_function): {
  396. // TODO: Track a location for the use. In general we may want to track a
  397. // list of enclosing locations if this was used from a generic.
  398. SemIRLoc use_loc = decl_inst_id;
  399. if (!ResolveSpecificDefinition(context_, use_loc,
  400. specific_function.specific_id)) {
  401. CARBON_DIAGNOSTIC(MissingGenericFunctionDefinition, Error,
  402. "use of undefined generic function");
  403. CARBON_DIAGNOSTIC(MissingGenericFunctionDefinitionHere, Note,
  404. "generic function declared here");
  405. auto generic_decl_id =
  406. context_.generics()
  407. .Get(context_.specifics()
  408. .Get(specific_function.specific_id)
  409. .generic_id)
  410. .decl_id;
  411. emitter_.Build(decl_inst_id, MissingGenericFunctionDefinition)
  412. .Note(generic_decl_id, MissingGenericFunctionDefinitionHere)
  413. .Emit();
  414. }
  415. break;
  416. }
  417. default: {
  418. CARBON_FATAL("Unexpected inst in definitions_required: {0}", decl_inst);
  419. }
  420. }
  421. }
  422. }
  423. } // namespace Carbon::Check