check.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  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.h"
  5. #include "common/check.h"
  6. #include "common/map.h"
  7. #include "toolchain/base/kind_switch.h"
  8. #include "toolchain/base/pretty_stack_trace_function.h"
  9. #include "toolchain/check/context.h"
  10. #include "toolchain/check/diagnostic_helpers.h"
  11. #include "toolchain/check/generic.h"
  12. #include "toolchain/check/handle.h"
  13. #include "toolchain/check/import.h"
  14. #include "toolchain/check/import_ref.h"
  15. #include "toolchain/check/node_id_traversal.h"
  16. #include "toolchain/check/sem_ir_diagnostic_converter.h"
  17. #include "toolchain/diagnostics/diagnostic.h"
  18. #include "toolchain/diagnostics/format_providers.h"
  19. #include "toolchain/lex/token_kind.h"
  20. #include "toolchain/parse/node_ids.h"
  21. #include "toolchain/parse/tree.h"
  22. #include "toolchain/parse/tree_node_diagnostic_converter.h"
  23. #include "toolchain/sem_ir/file.h"
  24. #include "toolchain/sem_ir/ids.h"
  25. #include "toolchain/sem_ir/typed_insts.h"
  26. namespace Carbon::Check {
  27. namespace {
  28. struct UnitInfo {
  29. // A given import within the file, with its destination.
  30. struct Import {
  31. Parse::Tree::PackagingNames names;
  32. UnitInfo* unit_info;
  33. };
  34. // A file's imports corresponding to a single package, for the map.
  35. struct PackageImports {
  36. // Use the constructor so that the SmallVector is only constructed
  37. // as-needed.
  38. explicit PackageImports(IdentifierId package_id,
  39. Parse::ImportDeclId node_id)
  40. : package_id(package_id), node_id(node_id) {}
  41. // The identifier of the imported package.
  42. IdentifierId package_id;
  43. // The first `import` declaration in the file, which declared the package's
  44. // identifier (even if the import failed). Used for associating diagnostics
  45. // not specific to a single import.
  46. Parse::ImportDeclId node_id;
  47. // The associated `import` instruction. Only valid once a file is checked.
  48. SemIR::InstId import_decl_id = SemIR::InstId::Invalid;
  49. // Whether there's an import that failed to load.
  50. bool has_load_error = false;
  51. // The list of valid imports.
  52. llvm::SmallVector<Import> imports;
  53. };
  54. explicit UnitInfo(SemIR::CheckIRId check_ir_id, Unit& unit)
  55. : check_ir_id(check_ir_id),
  56. unit(&unit),
  57. err_tracker(*unit.consumer),
  58. emitter(*unit.node_converter, err_tracker) {}
  59. auto parse_tree() -> const Parse::Tree& { return unit->sem_ir->parse_tree(); }
  60. auto source() -> const SourceBuffer& {
  61. return parse_tree().tokens().source();
  62. }
  63. SemIR::CheckIRId check_ir_id;
  64. Unit* unit;
  65. // Emitter information.
  66. ErrorTrackingDiagnosticConsumer err_tracker;
  67. DiagnosticEmitter<Parse::NodeLoc> emitter;
  68. // List of the outgoing imports. If a package includes unavailable library
  69. // imports, it has an entry with has_load_error set. Invalid imports (for
  70. // example, `import Main;`) aren't added because they won't add identifiers to
  71. // name lookup.
  72. llvm::SmallVector<PackageImports> package_imports;
  73. // A map of the package names to the outgoing imports above.
  74. Map<IdentifierId, int32_t> package_imports_map;
  75. // The remaining number of imports which must be checked before this unit can
  76. // be processed.
  77. int32_t imports_remaining = 0;
  78. // A list of incoming imports. This will be empty for `impl` files, because
  79. // imports only touch `api` files.
  80. llvm::SmallVector<UnitInfo*> incoming_imports;
  81. // The corresponding `api` unit if this is an `impl` file. The entry should
  82. // also be in the corresponding `PackageImports`.
  83. UnitInfo* api_for_impl = nullptr;
  84. // Whether the unit has been checked.
  85. bool is_checked = false;
  86. };
  87. } // namespace
  88. // Collects direct imports, for CollectTransitiveImports.
  89. static auto CollectDirectImports(llvm::SmallVector<SemIR::ImportIR>& results,
  90. llvm::MutableArrayRef<int> ir_to_result_index,
  91. SemIR::InstId import_decl_id,
  92. const UnitInfo::PackageImports& imports,
  93. bool is_local) -> void {
  94. for (const auto& import : imports.imports) {
  95. const auto& direct_ir = *import.unit_info->unit->sem_ir;
  96. auto& index = ir_to_result_index[direct_ir.check_ir_id().index];
  97. if (index != -1) {
  98. // This should only happen when doing API imports for an implementation
  99. // file. Don't change the entry; is_export doesn't matter.
  100. continue;
  101. }
  102. index = results.size();
  103. results.push_back({.decl_id = import_decl_id,
  104. // Only tag exports in API files, ignoring the value in
  105. // implementation files.
  106. .is_export = is_local && import.names.is_export,
  107. .sem_ir = &direct_ir});
  108. }
  109. }
  110. // Collects transitive imports, handling deduplication. These will be unified
  111. // between local_imports and api_imports.
  112. static auto CollectTransitiveImports(
  113. SemIR::InstId import_decl_id, const UnitInfo::PackageImports* local_imports,
  114. const UnitInfo::PackageImports* api_imports, int total_ir_count)
  115. -> llvm::SmallVector<SemIR::ImportIR> {
  116. llvm::SmallVector<SemIR::ImportIR> results;
  117. // Track whether an IR was imported in full, including `export import`. This
  118. // distinguishes from IRs that are indirectly added without all names being
  119. // exported to this IR.
  120. llvm::SmallVector<int> ir_to_result_index(total_ir_count, -1);
  121. // First add direct imports. This means that if an entity is imported both
  122. // directly and indirectly, the import path will reflect the direct import.
  123. if (local_imports) {
  124. CollectDirectImports(results, ir_to_result_index, import_decl_id,
  125. *local_imports,
  126. /*is_local=*/true);
  127. }
  128. if (api_imports) {
  129. CollectDirectImports(results, ir_to_result_index, import_decl_id,
  130. *api_imports,
  131. /*is_local=*/false);
  132. }
  133. // Loop through direct imports for any indirect exports. The underlying vector
  134. // is appended during iteration, so take the size first.
  135. const int direct_imports = results.size();
  136. for (int direct_index : llvm::seq(direct_imports)) {
  137. bool is_export = results[direct_index].is_export;
  138. for (const auto& indirect_ir :
  139. results[direct_index].sem_ir->import_irs().array_ref()) {
  140. if (!indirect_ir.is_export) {
  141. continue;
  142. }
  143. auto& indirect_index =
  144. ir_to_result_index[indirect_ir.sem_ir->check_ir_id().index];
  145. if (indirect_index == -1) {
  146. indirect_index = results.size();
  147. // TODO: In the case of a recursive `export import`, this only points at
  148. // the outermost import. May want something that better reflects the
  149. // recursion.
  150. results.push_back({.decl_id = results[direct_index].decl_id,
  151. .is_export = is_export,
  152. .sem_ir = indirect_ir.sem_ir});
  153. } else if (is_export) {
  154. results[indirect_index].is_export = true;
  155. }
  156. }
  157. }
  158. return results;
  159. }
  160. // Imports the current package.
  161. static auto ImportCurrentPackage(Context& context, UnitInfo& unit_info,
  162. int total_ir_count,
  163. SemIR::InstId package_inst_id,
  164. SemIR::TypeId namespace_type_id) -> void {
  165. // Add imports from the current package.
  166. auto import_map_lookup =
  167. unit_info.package_imports_map.Lookup(IdentifierId::Invalid);
  168. if (!import_map_lookup) {
  169. // Push the scope; there are no names to add.
  170. context.scope_stack().Push(package_inst_id, SemIR::NameScopeId::Package);
  171. return;
  172. }
  173. UnitInfo::PackageImports& self_import =
  174. unit_info.package_imports[import_map_lookup.value()];
  175. if (self_import.has_load_error) {
  176. context.name_scopes().Get(SemIR::NameScopeId::Package).set_has_error();
  177. }
  178. ImportLibrariesFromCurrentPackage(
  179. context, namespace_type_id,
  180. CollectTransitiveImports(self_import.import_decl_id, &self_import,
  181. /*api_imports=*/nullptr, total_ir_count));
  182. context.scope_stack().Push(
  183. package_inst_id, SemIR::NameScopeId::Package, SemIR::SpecificId::Invalid,
  184. context.name_scopes().Get(SemIR::NameScopeId::Package).has_error());
  185. }
  186. // Imports all other packages (excluding the current package).
  187. static auto ImportOtherPackages(Context& context, UnitInfo& unit_info,
  188. int total_ir_count,
  189. SemIR::TypeId namespace_type_id) -> void {
  190. // api_imports_list is initially the size of the current file's imports,
  191. // including for API files, for simplicity in iteration. It's only really used
  192. // when processing an implementation file, in order to combine the API file
  193. // imports.
  194. //
  195. // For packages imported by the API file, the IdentifierId is the package name
  196. // and the index is into the API's import list. Otherwise, the initial
  197. // {Invalid, -1} state remains.
  198. llvm::SmallVector<std::pair<IdentifierId, int32_t>> api_imports_list;
  199. api_imports_list.resize(unit_info.package_imports.size(),
  200. {IdentifierId::Invalid, -1});
  201. // When there's an API file, add the mapping to api_imports_list.
  202. if (unit_info.api_for_impl) {
  203. const auto& api_identifiers =
  204. unit_info.api_for_impl->unit->value_stores->identifiers();
  205. auto& impl_identifiers = unit_info.unit->value_stores->identifiers();
  206. for (auto [api_imports_index, api_imports] :
  207. llvm::enumerate(unit_info.api_for_impl->package_imports)) {
  208. // Skip the current package.
  209. if (!api_imports.package_id.is_valid()) {
  210. continue;
  211. }
  212. // Translate the package ID from the API file to the implementation file.
  213. auto impl_package_id =
  214. impl_identifiers.Add(api_identifiers.Get(api_imports.package_id));
  215. if (auto lookup = unit_info.package_imports_map.Lookup(impl_package_id)) {
  216. // On a hit, replace the entry to unify the API and implementation
  217. // imports.
  218. api_imports_list[lookup.value()] = {impl_package_id, api_imports_index};
  219. } else {
  220. // On a miss, add the package as API-only.
  221. api_imports_list.push_back({impl_package_id, api_imports_index});
  222. }
  223. }
  224. }
  225. for (auto [i, api_imports_entry] : llvm::enumerate(api_imports_list)) {
  226. // These variables are updated after figuring out which imports are present.
  227. auto import_decl_id = SemIR::InstId::Invalid;
  228. IdentifierId package_id = IdentifierId::Invalid;
  229. bool has_load_error = false;
  230. // Identify the local package imports if present.
  231. UnitInfo::PackageImports* local_imports = nullptr;
  232. if (i < unit_info.package_imports.size()) {
  233. local_imports = &unit_info.package_imports[i];
  234. if (!local_imports->package_id.is_valid()) {
  235. // Skip the current package.
  236. continue;
  237. }
  238. import_decl_id = local_imports->import_decl_id;
  239. package_id = local_imports->package_id;
  240. has_load_error |= local_imports->has_load_error;
  241. }
  242. // Identify the API package imports if present.
  243. UnitInfo::PackageImports* api_imports = nullptr;
  244. if (api_imports_entry.second != -1) {
  245. api_imports =
  246. &unit_info.api_for_impl->package_imports[api_imports_entry.second];
  247. if (local_imports) {
  248. CARBON_CHECK(package_id == api_imports_entry.first);
  249. } else {
  250. auto import_ir_inst_id = context.import_ir_insts().Add(
  251. {.ir_id = SemIR::ImportIRId::ApiForImpl,
  252. .inst_id = api_imports->import_decl_id});
  253. import_decl_id =
  254. context.AddInst(context.MakeImportedLocAndInst<SemIR::ImportDecl>(
  255. import_ir_inst_id, {.package_id = SemIR::NameId::ForIdentifier(
  256. api_imports_entry.first)}));
  257. package_id = api_imports_entry.first;
  258. }
  259. has_load_error |= api_imports->has_load_error;
  260. }
  261. // Do the actual import.
  262. ImportLibrariesFromOtherPackage(
  263. context, namespace_type_id, import_decl_id, package_id,
  264. CollectTransitiveImports(import_decl_id, local_imports, api_imports,
  265. total_ir_count),
  266. has_load_error);
  267. }
  268. }
  269. // Add imports to the root block.
  270. static auto InitPackageScopeAndImports(Context& context, UnitInfo& unit_info,
  271. int total_ir_count) -> void {
  272. // First create the constant values map for all imported IRs. We'll populate
  273. // these with mappings for namespaces as we go.
  274. size_t num_irs = 0;
  275. for (auto& package_imports : unit_info.package_imports) {
  276. num_irs += package_imports.imports.size();
  277. }
  278. if (!unit_info.api_for_impl) {
  279. // Leave an empty slot for ImportIRId::ApiForImpl.
  280. ++num_irs;
  281. }
  282. context.import_irs().Reserve(num_irs);
  283. context.import_ir_constant_values().reserve(num_irs);
  284. context.SetTotalIRCount(total_ir_count);
  285. // Importing makes many namespaces, so only canonicalize the type once.
  286. auto namespace_type_id =
  287. context.GetSingletonType(SemIR::NamespaceType::SingletonInstId);
  288. // Define the package scope, with an instruction for `package` expressions to
  289. // reference.
  290. auto package_scope_id = context.name_scopes().Add(
  291. SemIR::Namespace::PackageInstId, SemIR::NameId::PackageNamespace,
  292. SemIR::NameScopeId::Invalid);
  293. CARBON_CHECK(package_scope_id == SemIR::NameScopeId::Package);
  294. auto package_inst_id = context.AddInst<SemIR::Namespace>(
  295. Parse::NodeId::Invalid, {.type_id = namespace_type_id,
  296. .name_scope_id = SemIR::NameScopeId::Package,
  297. .import_id = SemIR::InstId::Invalid});
  298. CARBON_CHECK(package_inst_id == SemIR::Namespace::PackageInstId);
  299. // If there is an implicit `api` import, set it first so that it uses the
  300. // ImportIRId::ApiForImpl when processed for imports.
  301. if (unit_info.api_for_impl) {
  302. const auto& names = context.parse_tree().packaging_decl()->names;
  303. auto import_decl_id = context.AddInst<SemIR::ImportDecl>(
  304. names.node_id,
  305. {.package_id = SemIR::NameId::ForIdentifier(names.package_id)});
  306. SetApiImportIR(context, {.decl_id = import_decl_id,
  307. .is_export = false,
  308. .sem_ir = unit_info.api_for_impl->unit->sem_ir});
  309. } else {
  310. SetApiImportIR(context,
  311. {.decl_id = SemIR::InstId::Invalid, .sem_ir = nullptr});
  312. }
  313. // Add import instructions for everything directly imported. Implicit imports
  314. // are handled separately.
  315. for (auto& package_imports : unit_info.package_imports) {
  316. CARBON_CHECK(!package_imports.import_decl_id.is_valid());
  317. package_imports.import_decl_id = context.AddInst<SemIR::ImportDecl>(
  318. package_imports.node_id, {.package_id = SemIR::NameId::ForIdentifier(
  319. package_imports.package_id)});
  320. }
  321. // Process the imports.
  322. if (unit_info.api_for_impl) {
  323. ImportApiFile(context, namespace_type_id,
  324. *unit_info.api_for_impl->unit->sem_ir);
  325. }
  326. ImportCurrentPackage(context, unit_info, total_ir_count, package_inst_id,
  327. namespace_type_id);
  328. CARBON_CHECK(context.scope_stack().PeekIndex() == ScopeIndex::Package);
  329. ImportOtherPackages(context, unit_info, total_ir_count, namespace_type_id);
  330. }
  331. // Checks that each required definition is available. If the definition can be
  332. // generated by resolving a specific, does so, otherwise emits a diagnostic for
  333. // each declaration in context.definitions_required() that doesn't have a
  334. // definition.
  335. static auto CheckRequiredDefinitions(Context& context,
  336. Context::DiagnosticEmitter& emitter)
  337. -> void {
  338. CARBON_DIAGNOSTIC(MissingDefinitionInImpl, Error,
  339. "no definition found for declaration in impl file");
  340. // Note that more required definitions can be added during this loop.
  341. for (size_t i = 0; i != context.definitions_required().size(); ++i) {
  342. SemIR::InstId decl_inst_id = context.definitions_required()[i];
  343. SemIR::Inst decl_inst = context.insts().Get(decl_inst_id);
  344. CARBON_KIND_SWITCH(context.insts().Get(decl_inst_id)) {
  345. case CARBON_KIND(SemIR::ClassDecl class_decl): {
  346. if (!context.classes().Get(class_decl.class_id).is_defined()) {
  347. emitter.Emit(decl_inst_id, MissingDefinitionInImpl);
  348. }
  349. break;
  350. }
  351. case CARBON_KIND(SemIR::FunctionDecl function_decl): {
  352. if (context.functions().Get(function_decl.function_id).definition_id ==
  353. SemIR::InstId::Invalid) {
  354. emitter.Emit(decl_inst_id, MissingDefinitionInImpl);
  355. }
  356. break;
  357. }
  358. case CARBON_KIND(SemIR::ImplDecl impl_decl): {
  359. if (!context.impls().Get(impl_decl.impl_id).is_defined()) {
  360. emitter.Emit(decl_inst_id, MissingDefinitionInImpl);
  361. }
  362. break;
  363. }
  364. case SemIR::InterfaceDecl::Kind: {
  365. // TODO: Handle `interface` as well, once we can test it without
  366. // triggering
  367. // https://github.com/carbon-language/carbon-lang/issues/4071.
  368. CARBON_FATAL("TODO: Support interfaces in DiagnoseMissingDefinitions");
  369. }
  370. case CARBON_KIND(SemIR::SpecificFunction specific_function): {
  371. if (!ResolveSpecificDefinition(context,
  372. specific_function.specific_id)) {
  373. CARBON_DIAGNOSTIC(MissingGenericFunctionDefinition, Error,
  374. "use of undefined generic function");
  375. CARBON_DIAGNOSTIC(MissingGenericFunctionDefinitionHere, Note,
  376. "generic function declared here");
  377. auto generic_decl_id =
  378. context.generics()
  379. .Get(context.specifics()
  380. .Get(specific_function.specific_id)
  381. .generic_id)
  382. .decl_id;
  383. emitter.Build(decl_inst_id, MissingGenericFunctionDefinition)
  384. .Note(generic_decl_id, MissingGenericFunctionDefinitionHere)
  385. .Emit();
  386. }
  387. break;
  388. }
  389. default: {
  390. CARBON_FATAL("Unexpected inst in definitions_required: {0}", decl_inst);
  391. }
  392. }
  393. }
  394. }
  395. // Loops over all nodes in the tree. On some errors, this may return early,
  396. // for example if an unrecoverable state is encountered.
  397. // NOLINTNEXTLINE(readability-function-size)
  398. static auto ProcessNodeIds(Context& context, llvm::raw_ostream* vlog_stream,
  399. ErrorTrackingDiagnosticConsumer& err_tracker,
  400. Parse::NodeLocConverter& converter) -> bool {
  401. NodeIdTraversal traversal(context, vlog_stream);
  402. Parse::NodeId node_id = Parse::NodeId::Invalid;
  403. // On crash, report which token we were handling.
  404. PrettyStackTraceFunction node_dumper([&](llvm::raw_ostream& output) {
  405. auto loc = converter.ConvertLoc(
  406. node_id, [](DiagnosticLoc, const DiagnosticBase<>&) {});
  407. loc.FormatLocation(output);
  408. output << ": checking " << context.parse_tree().node_kind(node_id) << "\n";
  409. // Crash output has a tab indent; try to indent slightly past that.
  410. loc.FormatSnippet(output, /*indent=*/10);
  411. });
  412. while (auto maybe_node_id = traversal.Next()) {
  413. node_id = *maybe_node_id;
  414. auto parse_kind = context.parse_tree().node_kind(node_id);
  415. switch (parse_kind) {
  416. #define CARBON_PARSE_NODE_KIND(Name) \
  417. case Parse::NodeKind::Name: { \
  418. if (!HandleParseNode(context, Parse::Name##Id(node_id))) { \
  419. CARBON_CHECK(err_tracker.seen_error(), \
  420. "Handle" #Name \
  421. " returned false without printing a diagnostic"); \
  422. return false; \
  423. } \
  424. break; \
  425. }
  426. #include "toolchain/parse/node_kind.def"
  427. }
  428. traversal.Handle(parse_kind);
  429. }
  430. return true;
  431. }
  432. // Produces and checks the IR for the provided Parse::Tree.
  433. static auto CheckParseTree(UnitInfo& unit_info, int total_ir_count,
  434. llvm::raw_ostream* vlog_stream) -> void {
  435. Timings::ScopedTiming timing(unit_info.unit->timings, "check");
  436. // We can safely mark this as checked at the start.
  437. unit_info.is_checked = true;
  438. SemIR::File* sem_ir = unit_info.unit->sem_ir;
  439. Context::DiagnosticEmitter emitter(*unit_info.unit->sem_ir_converter,
  440. unit_info.err_tracker);
  441. Context context(&emitter, unit_info.unit->get_parse_tree_and_subtrees, sem_ir,
  442. vlog_stream);
  443. PrettyStackTraceFunction context_dumper(
  444. [&](llvm::raw_ostream& output) { context.PrintForStackDump(output); });
  445. // Add a block for the file.
  446. context.inst_block_stack().Push();
  447. InitPackageScopeAndImports(context, unit_info, total_ir_count);
  448. // Eagerly import the impls declared in the api file to prepare to redeclare
  449. // them.
  450. ImportImplsFromApiFile(context);
  451. if (!ProcessNodeIds(context, vlog_stream, unit_info.err_tracker,
  452. *unit_info.unit->node_converter)) {
  453. context.sem_ir().set_has_errors(true);
  454. return;
  455. }
  456. CheckRequiredDefinitions(context, emitter);
  457. context.Finalize();
  458. context.VerifyOnFinish();
  459. sem_ir->set_has_errors(unit_info.err_tracker.seen_error());
  460. #ifndef NDEBUG
  461. if (auto verify = sem_ir->Verify(); !verify.ok()) {
  462. CARBON_FATAL("{0}Built invalid semantics IR: {1}\n", *sem_ir,
  463. verify.error());
  464. }
  465. #endif
  466. }
  467. // The package and library names, used as map keys.
  468. using ImportKey = std::pair<llvm::StringRef, llvm::StringRef>;
  469. // Returns a key form of the package object. file_package_id is only used for
  470. // imports, not the main package declaration; as a consequence, it will be
  471. // invalid for the main package declaration.
  472. static auto GetImportKey(UnitInfo& unit_info, IdentifierId file_package_id,
  473. Parse::Tree::PackagingNames names) -> ImportKey {
  474. auto* stores = unit_info.unit->value_stores;
  475. llvm::StringRef package_name =
  476. names.package_id.is_valid() ? stores->identifiers().Get(names.package_id)
  477. : file_package_id.is_valid() ? stores->identifiers().Get(file_package_id)
  478. : "";
  479. llvm::StringRef library_name =
  480. names.library_id.is_valid()
  481. ? stores->string_literal_values().Get(names.library_id)
  482. : "";
  483. return {package_name, library_name};
  484. }
  485. static constexpr llvm::StringLiteral CppPackageName = "Cpp";
  486. static constexpr llvm::StringLiteral MainPackageName = "Main";
  487. static auto RenderImportKey(ImportKey import_key) -> std::string {
  488. if (import_key.first.empty()) {
  489. import_key.first = MainPackageName;
  490. }
  491. if (import_key.second.empty()) {
  492. return import_key.first.str();
  493. }
  494. return llvm::formatv("{0}//{1}", import_key.first, import_key.second).str();
  495. }
  496. // Marks an import as required on both the source and target file.
  497. //
  498. // The ID comparisons between the import and unit are okay because they both
  499. // come from the same file.
  500. static auto TrackImport(Map<ImportKey, UnitInfo*>& api_map,
  501. Map<ImportKey, Parse::NodeId>* explicit_import_map,
  502. UnitInfo& unit_info, Parse::Tree::PackagingNames import)
  503. -> void {
  504. const auto& packaging = unit_info.parse_tree().packaging_decl();
  505. IdentifierId file_package_id =
  506. packaging ? packaging->names.package_id : IdentifierId::Invalid;
  507. auto import_key = GetImportKey(unit_info, file_package_id, import);
  508. // True if the import has `Main` as the package name, even if it comes from
  509. // the file's packaging (diagnostics may differentiate).
  510. bool is_explicit_main = import_key.first == MainPackageName;
  511. // Explicit imports need more validation than implicit ones. We try to do
  512. // these in an order of imports that should be removed, followed by imports
  513. // that might be valid with syntax fixes.
  514. if (explicit_import_map) {
  515. // Diagnose redundant imports.
  516. if (auto insert_result =
  517. explicit_import_map->Insert(import_key, import.node_id);
  518. !insert_result.is_inserted()) {
  519. CARBON_DIAGNOSTIC(RepeatedImport, Error,
  520. "library imported more than once");
  521. CARBON_DIAGNOSTIC(FirstImported, Note, "first import here");
  522. unit_info.emitter.Build(import.node_id, RepeatedImport)
  523. .Note(insert_result.value(), FirstImported)
  524. .Emit();
  525. return;
  526. }
  527. // True if the file's package is implicitly `Main` (by omitting an explicit
  528. // package name).
  529. bool is_file_implicit_main =
  530. !packaging || !packaging->names.package_id.is_valid();
  531. // True if the import is using implicit "current package" syntax (by
  532. // omitting an explicit package name).
  533. bool is_import_implicit_current_package = !import.package_id.is_valid();
  534. // True if the import is using `default` library syntax.
  535. bool is_import_default_library = !import.library_id.is_valid();
  536. // True if the import and file point at the same package, even by
  537. // incorrectly specifying the current package name to `import`.
  538. bool is_same_package = is_import_implicit_current_package ||
  539. import.package_id == file_package_id;
  540. // True if the import points at the same library as the file's library.
  541. bool is_same_library =
  542. is_same_package &&
  543. (packaging ? import.library_id == packaging->names.library_id
  544. : is_import_default_library);
  545. // Diagnose explicit imports of the same library, whether from `api` or
  546. // `impl`.
  547. if (is_same_library) {
  548. CARBON_DIAGNOSTIC(ExplicitImportApi, Error,
  549. "explicit import of `api` from `impl` file is "
  550. "redundant with implicit import");
  551. CARBON_DIAGNOSTIC(ImportSelf, Error, "file cannot import itself");
  552. bool is_impl = !packaging || packaging->is_impl;
  553. unit_info.emitter.Emit(import.node_id,
  554. is_impl ? ExplicitImportApi : ImportSelf);
  555. return;
  556. }
  557. // Diagnose explicit imports of `Main//default`. There is no `api` for it.
  558. // This lets other diagnostics handle explicit `Main` package naming.
  559. if (is_file_implicit_main && is_import_implicit_current_package &&
  560. is_import_default_library) {
  561. CARBON_DIAGNOSTIC(ImportMainDefaultLibrary, Error,
  562. "cannot import `Main//default`");
  563. unit_info.emitter.Emit(import.node_id, ImportMainDefaultLibrary);
  564. return;
  565. }
  566. if (!is_import_implicit_current_package) {
  567. // Diagnose explicit imports of the same package that use the package
  568. // name.
  569. if (is_same_package || (is_file_implicit_main && is_explicit_main)) {
  570. CARBON_DIAGNOSTIC(
  571. ImportCurrentPackageByName, Error,
  572. "imports from the current package must omit the package name");
  573. unit_info.emitter.Emit(import.node_id, ImportCurrentPackageByName);
  574. return;
  575. }
  576. // Diagnose explicit imports from `Main`.
  577. if (is_explicit_main) {
  578. CARBON_DIAGNOSTIC(ImportMainPackage, Error,
  579. "cannot import `Main` from other packages");
  580. unit_info.emitter.Emit(import.node_id, ImportMainPackage);
  581. return;
  582. }
  583. }
  584. } else if (is_explicit_main) {
  585. // An implicit import with an explicit `Main` occurs when a `package` rule
  586. // has bad syntax, which will have been diagnosed when building the API map.
  587. // As a consequence, we return silently.
  588. return;
  589. }
  590. // Get the package imports, or create them if this is the first.
  591. auto create_imports = [&]() -> int32_t {
  592. int32_t index = unit_info.package_imports.size();
  593. unit_info.package_imports.push_back(
  594. UnitInfo::PackageImports(import.package_id, import.node_id));
  595. return index;
  596. };
  597. auto insert_result =
  598. unit_info.package_imports_map.Insert(import.package_id, create_imports);
  599. UnitInfo::PackageImports& package_imports =
  600. unit_info.package_imports[insert_result.value()];
  601. if (auto api_lookup = api_map.Lookup(import_key)) {
  602. // Add references between the file and imported api.
  603. UnitInfo* api = api_lookup.value();
  604. package_imports.imports.push_back({import, api});
  605. ++unit_info.imports_remaining;
  606. api->incoming_imports.push_back(&unit_info);
  607. // If this is the implicit import, note we have it.
  608. if (!explicit_import_map) {
  609. CARBON_CHECK(!unit_info.api_for_impl);
  610. unit_info.api_for_impl = api;
  611. }
  612. } else {
  613. // The imported api is missing.
  614. package_imports.has_load_error = true;
  615. if (!explicit_import_map && import_key.first == CppPackageName) {
  616. // Don't diagnose the implicit import in `impl package Cpp`, because we'll
  617. // have diagnosed the use of `Cpp` in the declaration.
  618. return;
  619. }
  620. CARBON_DIAGNOSTIC(LibraryApiNotFound, Error,
  621. "corresponding API for '{0}' not found", std::string);
  622. CARBON_DIAGNOSTIC(ImportNotFound, Error, "imported API '{0}' not found",
  623. std::string);
  624. unit_info.emitter.Emit(
  625. import.node_id,
  626. explicit_import_map ? ImportNotFound : LibraryApiNotFound,
  627. RenderImportKey(import_key));
  628. }
  629. }
  630. // Builds a map of `api` files which might be imported. Also diagnoses issues
  631. // related to the packaging because the strings are loaded as part of getting
  632. // the ImportKey (which we then do for `impl` files too).
  633. static auto BuildApiMapAndDiagnosePackaging(
  634. llvm::MutableArrayRef<UnitInfo> unit_infos) -> Map<ImportKey, UnitInfo*> {
  635. Map<ImportKey, UnitInfo*> api_map;
  636. for (auto& unit_info : unit_infos) {
  637. const auto& packaging = unit_info.parse_tree().packaging_decl();
  638. // An import key formed from the `package` or `library` declaration. Or, for
  639. // Main//default, a placeholder key.
  640. auto import_key = packaging ? GetImportKey(unit_info, IdentifierId::Invalid,
  641. packaging->names)
  642. // Construct a boring key for Main//default.
  643. : ImportKey{"", ""};
  644. // Diagnose restricted package names before they become marked as possible
  645. // APIs.
  646. if (import_key.first == MainPackageName) {
  647. CARBON_DIAGNOSTIC(ExplicitMainPackage, Error,
  648. "`Main//default` must omit `package` declaration");
  649. CARBON_DIAGNOSTIC(
  650. ExplicitMainLibrary, Error,
  651. "use `library` declaration in `Main` package libraries");
  652. unit_info.emitter.Emit(packaging->names.node_id,
  653. import_key.second.empty() ? ExplicitMainPackage
  654. : ExplicitMainLibrary);
  655. continue;
  656. } else if (import_key.first == CppPackageName) {
  657. CARBON_DIAGNOSTIC(CppPackageDeclaration, Error,
  658. "`Cpp` cannot be used by a `package` declaration");
  659. unit_info.emitter.Emit(packaging->names.node_id, CppPackageDeclaration);
  660. continue;
  661. }
  662. bool is_impl = packaging && packaging->is_impl;
  663. // Add to the `api` map and diagnose duplicates. This occurs before the
  664. // file extension check because we might emit both diagnostics in situations
  665. // where the user forgets (or has syntax errors with) a package line
  666. // multiple times.
  667. if (!is_impl) {
  668. auto insert_result = api_map.Insert(import_key, &unit_info);
  669. if (!insert_result.is_inserted()) {
  670. llvm::StringRef prev_filename =
  671. insert_result.value()->source().filename();
  672. if (packaging) {
  673. CARBON_DIAGNOSTIC(DuplicateLibraryApi, Error,
  674. "library's API previously provided by `{0}`",
  675. std::string);
  676. unit_info.emitter.Emit(packaging->names.node_id, DuplicateLibraryApi,
  677. prev_filename.str());
  678. } else {
  679. CARBON_DIAGNOSTIC(DuplicateMainApi, Error,
  680. "`Main//default` previously provided by `{0}`",
  681. std::string);
  682. // Use the invalid node because there's no node to associate with.
  683. unit_info.emitter.Emit(Parse::NodeId::Invalid, DuplicateMainApi,
  684. prev_filename.str());
  685. }
  686. }
  687. }
  688. // Validate file extensions. Note imports rely the packaging declaration,
  689. // not the extension. If the input is not a regular file, for example
  690. // because it is stdin, no filename checking is performed.
  691. if (unit_info.source().is_regular_file()) {
  692. auto filename = unit_info.source().filename();
  693. static constexpr llvm::StringLiteral ApiExt = ".carbon";
  694. static constexpr llvm::StringLiteral ImplExt = ".impl.carbon";
  695. bool is_api_with_impl_ext = !is_impl && filename.ends_with(ImplExt);
  696. auto want_ext = is_impl ? ImplExt : ApiExt;
  697. if (is_api_with_impl_ext || !filename.ends_with(want_ext)) {
  698. CARBON_DIAGNOSTIC(
  699. IncorrectExtension, Error,
  700. "file extension of `{0:.impl|}.carbon` required for {0:`impl`|api}",
  701. BoolAsSelect);
  702. auto diag = unit_info.emitter.Build(
  703. packaging ? packaging->names.node_id : Parse::NodeId::Invalid,
  704. IncorrectExtension, is_impl);
  705. if (is_api_with_impl_ext) {
  706. CARBON_DIAGNOSTIC(
  707. IncorrectExtensionImplNote, Note,
  708. "file extension of `.impl.carbon` only allowed for `impl`");
  709. diag.Note(Parse::NodeId::Invalid, IncorrectExtensionImplNote);
  710. }
  711. diag.Emit();
  712. }
  713. }
  714. }
  715. return api_map;
  716. }
  717. auto CheckParseTrees(llvm::MutableArrayRef<Unit> units, bool prelude_import,
  718. llvm::raw_ostream* vlog_stream) -> void {
  719. // UnitInfo is big due to its SmallVectors, so we default to 0 on the
  720. // stack.
  721. llvm::SmallVector<UnitInfo, 0> unit_infos;
  722. unit_infos.reserve(units.size());
  723. for (auto [i, unit] : llvm::enumerate(units)) {
  724. unit_infos.emplace_back(SemIR::CheckIRId(i), unit);
  725. }
  726. Map<ImportKey, UnitInfo*> api_map =
  727. BuildApiMapAndDiagnosePackaging(unit_infos);
  728. // Mark down imports for all files.
  729. llvm::SmallVector<UnitInfo*> ready_to_check;
  730. ready_to_check.reserve(units.size());
  731. for (auto& unit_info : unit_infos) {
  732. const auto& packaging = unit_info.parse_tree().packaging_decl();
  733. if (packaging && packaging->is_impl) {
  734. // An `impl` has an implicit import of its `api`.
  735. auto implicit_names = packaging->names;
  736. implicit_names.package_id = IdentifierId::Invalid;
  737. TrackImport(api_map, nullptr, unit_info, implicit_names);
  738. }
  739. Map<ImportKey, Parse::NodeId> explicit_import_map;
  740. // Add the prelude import. It's added to explicit_import_map so that it can
  741. // conflict with an explicit import of the prelude.
  742. IdentifierId core_ident_id =
  743. unit_info.unit->value_stores->identifiers().Add("Core");
  744. if (prelude_import &&
  745. !(packaging && packaging->names.package_id == core_ident_id)) {
  746. auto prelude_id =
  747. unit_info.unit->value_stores->string_literal_values().Add("prelude");
  748. TrackImport(api_map, &explicit_import_map, unit_info,
  749. {.node_id = Parse::InvalidNodeId(),
  750. .package_id = core_ident_id,
  751. .library_id = prelude_id});
  752. }
  753. for (const auto& import : unit_info.parse_tree().imports()) {
  754. TrackImport(api_map, &explicit_import_map, unit_info, import);
  755. }
  756. // If there were no imports, mark the file as ready to check for below.
  757. if (unit_info.imports_remaining == 0) {
  758. ready_to_check.push_back(&unit_info);
  759. }
  760. }
  761. // Check everything with no dependencies. Earlier entries with dependencies
  762. // will be checked as soon as all their dependencies have been checked.
  763. for (int check_index = 0;
  764. check_index < static_cast<int>(ready_to_check.size()); ++check_index) {
  765. auto* unit_info = ready_to_check[check_index];
  766. CheckParseTree(*unit_info, units.size(), vlog_stream);
  767. for (auto* incoming_import : unit_info->incoming_imports) {
  768. --incoming_import->imports_remaining;
  769. if (incoming_import->imports_remaining == 0) {
  770. ready_to_check.push_back(incoming_import);
  771. }
  772. }
  773. }
  774. // If there are still units with remaining imports, it means there's a
  775. // dependency loop.
  776. if (ready_to_check.size() < unit_infos.size()) {
  777. // Go through units and mask out unevaluated imports. This breaks everything
  778. // associated with a loop equivalently, whether it's part of it or depending
  779. // on a part of it.
  780. // TODO: Better identify cycles, maybe try to untangle them.
  781. for (auto& unit_info : unit_infos) {
  782. if (unit_info.imports_remaining > 0) {
  783. for (auto& package_imports : unit_info.package_imports) {
  784. for (auto* import_it = package_imports.imports.begin();
  785. import_it != package_imports.imports.end();) {
  786. if (import_it->unit_info->is_checked) {
  787. // The import is checked, so continue.
  788. ++import_it;
  789. } else {
  790. // The import hasn't been checked, indicating a cycle.
  791. CARBON_DIAGNOSTIC(ImportCycleDetected, Error,
  792. "import cannot be used due to a cycle; cycle "
  793. "must be fixed to import");
  794. unit_info.emitter.Emit(import_it->names.node_id,
  795. ImportCycleDetected);
  796. // Make this look the same as an import which wasn't found.
  797. package_imports.has_load_error = true;
  798. if (unit_info.api_for_impl == import_it->unit_info) {
  799. unit_info.api_for_impl = nullptr;
  800. }
  801. import_it = package_imports.imports.erase(import_it);
  802. }
  803. }
  804. }
  805. }
  806. }
  807. // Check the remaining file contents, which are probably broken due to
  808. // incomplete imports.
  809. for (auto& unit_info : unit_infos) {
  810. if (unit_info.imports_remaining > 0) {
  811. CheckParseTree(unit_info, units.size(), vlog_stream);
  812. }
  813. }
  814. }
  815. }
  816. } // namespace Carbon::Check