check.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 "toolchain/base/pretty_stack_trace_function.h"
  7. #include "toolchain/check/context.h"
  8. #include "toolchain/check/diagnostic_helpers.h"
  9. #include "toolchain/check/import.h"
  10. #include "toolchain/diagnostics/diagnostic_emitter.h"
  11. #include "toolchain/lex/token_kind.h"
  12. #include "toolchain/parse/node_ids.h"
  13. #include "toolchain/parse/tree.h"
  14. #include "toolchain/parse/tree_node_diagnostic_converter.h"
  15. #include "toolchain/sem_ir/file.h"
  16. #include "toolchain/sem_ir/ids.h"
  17. #include "toolchain/sem_ir/typed_insts.h"
  18. namespace Carbon::Check {
  19. // Parse node handlers. Returns false for unrecoverable errors.
  20. #define CARBON_PARSE_NODE_KIND(Name) \
  21. auto Handle##Name(Context& context, Parse::Name##Id node_id) -> bool;
  22. #include "toolchain/parse/node_kind.def"
  23. // Handles the transformation of a SemIRLocation to a DiagnosticLocation.
  24. //
  25. // TODO: Move this to diagnostic_helpers.cpp.
  26. class SemIRDiagnosticConverter : public DiagnosticConverter<SemIRLocation> {
  27. public:
  28. explicit SemIRDiagnosticConverter(
  29. const llvm::DenseMap<const SemIR::File*, Parse::NodeLocationConverter*>*
  30. node_converters,
  31. const SemIR::File* sem_ir)
  32. : node_converters_(node_converters), sem_ir_(sem_ir) {}
  33. auto ConvertLocation(SemIRLocation loc, ContextFnT context_fn) const
  34. -> DiagnosticLocation override {
  35. // Parse nodes always refer to the current IR.
  36. if (!loc.is_inst_id) {
  37. return ConvertLocationInFile(sem_ir_, loc.node_location, context_fn);
  38. }
  39. const auto* cursor_ir = sem_ir_;
  40. auto cursor_inst_id = loc.inst_id;
  41. while (true) {
  42. // If the parse node is valid, use it for the location.
  43. if (auto node_id = cursor_ir->insts().GetNodeId(cursor_inst_id);
  44. node_id.is_valid()) {
  45. return ConvertLocationInFile(cursor_ir, node_id, context_fn);
  46. }
  47. // If the parse node was invalid, recurse through import references when
  48. // possible.
  49. if (auto import_ref = cursor_ir->insts().TryGetAs<SemIR::AnyImportRef>(
  50. cursor_inst_id)) {
  51. const auto& import_ir = cursor_ir->import_irs().Get(import_ref->ir_id);
  52. auto context_loc =
  53. ConvertLocationInFile(cursor_ir, import_ir.node_id, context_fn);
  54. CARBON_DIAGNOSTIC(InImport, Note, "In import.");
  55. context_fn(context_loc, InImport);
  56. cursor_ir = import_ir.sem_ir;
  57. cursor_inst_id = import_ref->inst_id;
  58. continue;
  59. }
  60. // If a namespace has an instruction for an import, switch to looking at
  61. // it.
  62. if (auto ns =
  63. cursor_ir->insts().TryGetAs<SemIR::Namespace>(cursor_inst_id)) {
  64. if (ns->import_id.is_valid()) {
  65. cursor_inst_id = ns->import_id;
  66. continue;
  67. }
  68. }
  69. // Invalid parse node but not an import; just nothing to point at.
  70. return ConvertLocationInFile(cursor_ir, Parse::NodeId::Invalid,
  71. context_fn);
  72. }
  73. }
  74. auto ConvertArg(llvm::Any arg) const -> llvm::Any override {
  75. if (auto* name_id = llvm::any_cast<SemIR::NameId>(&arg)) {
  76. return sem_ir_->names().GetFormatted(*name_id).str();
  77. }
  78. if (auto* type_id = llvm::any_cast<SemIR::TypeId>(&arg)) {
  79. return sem_ir_->StringifyType(*type_id);
  80. }
  81. if (auto* typed_int = llvm::any_cast<TypedInt>(&arg)) {
  82. return llvm::APSInt(typed_int->value,
  83. !sem_ir_->types().IsSignedInt(typed_int->type));
  84. }
  85. return DiagnosticConverter<SemIRLocation>::ConvertArg(arg);
  86. }
  87. private:
  88. auto ConvertLocationInFile(const SemIR::File* sem_ir,
  89. Parse::NodeLocation node_location,
  90. ContextFnT context_fn) const
  91. -> DiagnosticLocation {
  92. auto it = node_converters_->find(sem_ir);
  93. CARBON_CHECK(it != node_converters_->end());
  94. return it->second->ConvertLocation(node_location, context_fn);
  95. }
  96. const llvm::DenseMap<const SemIR::File*, Parse::NodeLocationConverter*>*
  97. node_converters_;
  98. const SemIR::File* sem_ir_;
  99. };
  100. struct UnitInfo {
  101. // A given import within the file, with its destination.
  102. struct Import {
  103. Parse::Tree::PackagingNames names;
  104. UnitInfo* unit_info;
  105. };
  106. // A file's imports corresponding to a single package, for the map.
  107. struct PackageImports {
  108. // Use the constructor so that the SmallVector is only constructed
  109. // as-needed.
  110. explicit PackageImports(Parse::ImportDirectiveId node_id)
  111. : node_id(node_id) {}
  112. // The first `import` directive in the file, which declared the package's
  113. // identifier (even if the import failed). Used for associating diagnostics
  114. // not specific to a single import.
  115. Parse::ImportDirectiveId node_id;
  116. // Whether there's an import that failed to load.
  117. bool has_load_error = false;
  118. // The list of valid imports.
  119. llvm::SmallVector<Import> imports;
  120. };
  121. explicit UnitInfo(Unit& unit)
  122. : unit(&unit),
  123. converter(unit.tokens, unit.tokens->source().filename(),
  124. unit.parse_tree),
  125. err_tracker(*unit.consumer),
  126. emitter(converter, err_tracker) {}
  127. Unit* unit;
  128. // Emitter information.
  129. Parse::NodeLocationConverter converter;
  130. ErrorTrackingDiagnosticConsumer err_tracker;
  131. DiagnosticEmitter<Parse::NodeLocation> emitter;
  132. // A map of package names to outgoing imports. If the
  133. // import's target isn't available, the unit will be nullptr to assist with
  134. // name lookup. Invalid imports (for example, `import Main;`) aren't added
  135. // because they won't add identifiers to name lookup.
  136. llvm::DenseMap<IdentifierId, PackageImports> package_imports_map;
  137. // The remaining number of imports which must be checked before this unit can
  138. // be processed.
  139. int32_t imports_remaining = 0;
  140. // A list of incoming imports. This will be empty for `impl` files, because
  141. // imports only touch `api` files.
  142. llvm::SmallVector<UnitInfo*> incoming_imports;
  143. };
  144. // Add imports to the root block.
  145. static auto InitPackageScopeAndImports(Context& context, UnitInfo& unit_info)
  146. -> void {
  147. // First create the constant values map for all imported IRs. We'll populate
  148. // these with mappings for namespaces as we go.
  149. size_t num_irs = context.import_irs().size();
  150. for (auto& [_, package_imports] : unit_info.package_imports_map) {
  151. num_irs += package_imports.imports.size();
  152. }
  153. context.import_ir_constant_values().resize(
  154. num_irs, SemIR::ConstantValueStore(SemIR::ConstantId::Invalid));
  155. // Importing makes many namespaces, so only canonicalize the type once.
  156. auto namespace_type_id =
  157. context.GetBuiltinType(SemIR::BuiltinKind::NamespaceType);
  158. // Define the package scope, with an instruction for `package` expressions to
  159. // reference.
  160. auto package_scope_id = context.name_scopes().Add(
  161. SemIR::InstId::PackageNamespace, SemIR::NameId::PackageNamespace,
  162. SemIR::NameScopeId::Invalid);
  163. CARBON_CHECK(package_scope_id == SemIR::NameScopeId::Package);
  164. auto package_inst_id = context.AddInst(
  165. {Parse::NodeId::Invalid,
  166. SemIR::Namespace{namespace_type_id, SemIR::NameScopeId::Package,
  167. SemIR::InstId::Invalid}});
  168. CARBON_CHECK(package_inst_id == SemIR::InstId::PackageNamespace);
  169. // Add imports from the current package.
  170. auto self_import = unit_info.package_imports_map.find(IdentifierId::Invalid);
  171. if (self_import != unit_info.package_imports_map.end()) {
  172. bool error_in_import = self_import->second.has_load_error;
  173. for (const auto& import : self_import->second.imports) {
  174. const auto& import_sem_ir = **import.unit_info->unit->sem_ir;
  175. ImportLibraryFromCurrentPackage(context, namespace_type_id,
  176. import.names.node_id, import_sem_ir);
  177. error_in_import |= import_sem_ir.name_scopes()
  178. .Get(SemIR::NameScopeId::Package)
  179. .has_error;
  180. }
  181. // If an import of the current package caused an error for the imported
  182. // file, it transitively affects the current file too.
  183. if (error_in_import) {
  184. context.name_scopes().Get(SemIR::NameScopeId::Package).has_error = true;
  185. }
  186. context.scope_stack().Push(package_inst_id, SemIR::NameScopeId::Package,
  187. error_in_import);
  188. } else {
  189. // Push the scope; there are no names to add.
  190. context.scope_stack().Push(package_inst_id, SemIR::NameScopeId::Package);
  191. }
  192. CARBON_CHECK(context.scope_stack().PeekIndex() == ScopeIndex::Package);
  193. for (auto& [package_id, package_imports] : unit_info.package_imports_map) {
  194. if (!package_id.is_valid()) {
  195. // Current package is handled above.
  196. continue;
  197. }
  198. llvm::SmallVector<SemIR::ImportIR> import_irs;
  199. for (auto import : package_imports.imports) {
  200. import_irs.push_back({.node_id = import.names.node_id,
  201. .sem_ir = &**import.unit_info->unit->sem_ir});
  202. }
  203. ImportLibrariesFromOtherPackage(context, namespace_type_id,
  204. package_imports.node_id, package_id,
  205. import_irs, package_imports.has_load_error);
  206. }
  207. CARBON_CHECK(context.import_irs().size() == num_irs)
  208. << "Created an unexpected number of IRs";
  209. }
  210. // Loops over all nodes in the tree. On some errors, this may return early,
  211. // for example if an unrecoverable state is encountered.
  212. // NOLINTNEXTLINE(readability-function-size)
  213. static auto ProcessNodeIds(Context& context,
  214. ErrorTrackingDiagnosticConsumer& err_tracker)
  215. -> bool {
  216. for (auto node_id : context.parse_tree().postorder()) {
  217. switch (auto parse_kind = context.parse_tree().node_kind(node_id)) {
  218. #define CARBON_PARSE_NODE_KIND(Name) \
  219. case Parse::NodeKind::Name: { \
  220. if (!Check::Handle##Name(context, Parse::Name##Id(node_id))) { \
  221. CARBON_CHECK(err_tracker.seen_error()) \
  222. << "Handle" #Name " returned false without printing a diagnostic"; \
  223. return false; \
  224. } \
  225. break; \
  226. }
  227. #include "toolchain/parse/node_kind.def"
  228. }
  229. }
  230. return true;
  231. }
  232. // Produces and checks the IR for the provided Parse::Tree.
  233. static auto CheckParseTree(
  234. llvm::DenseMap<const SemIR::File*, Parse::NodeLocationConverter*>*
  235. node_converters,
  236. const SemIR::File& builtin_ir, UnitInfo& unit_info,
  237. llvm::raw_ostream* vlog_stream) -> void {
  238. unit_info.unit->sem_ir->emplace(
  239. *unit_info.unit->value_stores,
  240. unit_info.unit->tokens->source().filename().str(), &builtin_ir);
  241. // For ease-of-access.
  242. SemIR::File& sem_ir = **unit_info.unit->sem_ir;
  243. CARBON_CHECK(node_converters->insert({&sem_ir, &unit_info.converter}).second);
  244. SemIRDiagnosticConverter converter(node_converters, &sem_ir);
  245. Context::DiagnosticEmitter emitter(converter, unit_info.err_tracker);
  246. Context context(*unit_info.unit->tokens, emitter, *unit_info.unit->parse_tree,
  247. sem_ir, vlog_stream);
  248. PrettyStackTraceFunction context_dumper(
  249. [&](llvm::raw_ostream& output) { context.PrintForStackDump(output); });
  250. // Add a block for the file.
  251. context.inst_block_stack().Push();
  252. InitPackageScopeAndImports(context, unit_info);
  253. if (!ProcessNodeIds(context, unit_info.err_tracker)) {
  254. context.sem_ir().set_has_errors(true);
  255. return;
  256. }
  257. // Pop information for the file-level scope.
  258. sem_ir.set_top_inst_block_id(context.inst_block_stack().Pop());
  259. context.scope_stack().Pop();
  260. context.FinalizeExports();
  261. context.FinalizeGlobalInit();
  262. context.VerifyOnFinish();
  263. sem_ir.set_has_errors(unit_info.err_tracker.seen_error());
  264. #ifndef NDEBUG
  265. if (auto verify = sem_ir.Verify(); !verify.ok()) {
  266. CARBON_FATAL() << sem_ir << "Built invalid semantics IR: " << verify.error()
  267. << "\n";
  268. }
  269. #endif
  270. }
  271. // The package and library names, used as map keys.
  272. using ImportKey = std::pair<llvm::StringRef, llvm::StringRef>;
  273. // Returns a key form of the package object. file_package_id is only used for
  274. // imports, not the main package directive; as a consequence, it will be invalid
  275. // for the main package directive.
  276. static auto GetImportKey(UnitInfo& unit_info, IdentifierId file_package_id,
  277. Parse::Tree::PackagingNames names) -> ImportKey {
  278. auto* stores = unit_info.unit->value_stores;
  279. llvm::StringRef package_name =
  280. names.package_id.is_valid() ? stores->identifiers().Get(names.package_id)
  281. : file_package_id.is_valid() ? stores->identifiers().Get(file_package_id)
  282. : "";
  283. llvm::StringRef library_name =
  284. names.library_id.is_valid()
  285. ? stores->string_literal_values().Get(names.library_id)
  286. : "";
  287. return {package_name, library_name};
  288. }
  289. static constexpr llvm::StringLiteral ExplicitMainName = "Main";
  290. // Marks an import as required on both the source and target file.
  291. //
  292. // The ID comparisons between the import and unit are okay because they both
  293. // come from the same file.
  294. static auto TrackImport(
  295. llvm::DenseMap<ImportKey, UnitInfo*>& api_map,
  296. llvm::DenseMap<ImportKey, Parse::NodeId>* explicit_import_map,
  297. UnitInfo& unit_info, Parse::Tree::PackagingNames import) -> void {
  298. const auto& packaging = unit_info.unit->parse_tree->packaging_directive();
  299. IdentifierId file_package_id =
  300. packaging ? packaging->names.package_id : IdentifierId::Invalid;
  301. auto import_key = GetImportKey(unit_info, file_package_id, import);
  302. // True if the import has `Main` as the package name, even if it comes from
  303. // the file's packaging (diagnostics may differentiate).
  304. bool is_explicit_main = import_key.first == ExplicitMainName;
  305. // Explicit imports need more validation than implicit ones. We try to do
  306. // these in an order of imports that should be removed, followed by imports
  307. // that might be valid with syntax fixes.
  308. if (explicit_import_map) {
  309. // Diagnose redundant imports.
  310. if (auto [insert_it, success] =
  311. explicit_import_map->insert({import_key, import.node_id});
  312. !success) {
  313. CARBON_DIAGNOSTIC(RepeatedImport, Error,
  314. "Library imported more than once.");
  315. CARBON_DIAGNOSTIC(FirstImported, Note, "First import here.");
  316. unit_info.emitter.Build(import.node_id, RepeatedImport)
  317. .Note(insert_it->second, FirstImported)
  318. .Emit();
  319. return;
  320. }
  321. // True if the file's package is implicitly `Main` (by omitting an explicit
  322. // package name).
  323. bool is_file_implicit_main =
  324. !packaging || !packaging->names.package_id.is_valid();
  325. // True if the import is using implicit "current package" syntax (by
  326. // omitting an explicit package name).
  327. bool is_import_implicit_current_package = !import.package_id.is_valid();
  328. // True if the import is using `default` library syntax.
  329. bool is_import_default_library = !import.library_id.is_valid();
  330. // True if the import and file point at the same package, even by
  331. // incorrectly specifying the current package name to `import`.
  332. bool is_same_package = is_import_implicit_current_package ||
  333. import.package_id == file_package_id;
  334. // True if the import points at the same library as the file's library.
  335. bool is_same_library =
  336. is_same_package &&
  337. (packaging ? import.library_id == packaging->names.library_id
  338. : is_import_default_library);
  339. // Diagnose explicit imports of the same library, whether from `api` or
  340. // `impl`.
  341. if (is_same_library) {
  342. CARBON_DIAGNOSTIC(ExplicitImportApi, Error,
  343. "Explicit import of `api` from `impl` file is "
  344. "redundant with implicit import.");
  345. CARBON_DIAGNOSTIC(ImportSelf, Error, "File cannot import itself.");
  346. bool is_impl =
  347. !packaging || packaging->api_or_impl == Parse::Tree::ApiOrImpl::Impl;
  348. unit_info.emitter.Emit(import.node_id,
  349. is_impl ? ExplicitImportApi : ImportSelf);
  350. return;
  351. }
  352. // Diagnose explicit imports of `Main//default`. There is no `api` for it.
  353. // This lets other diagnostics handle explicit `Main` package naming.
  354. if (is_file_implicit_main && is_import_implicit_current_package &&
  355. is_import_default_library) {
  356. CARBON_DIAGNOSTIC(ImportMainDefaultLibrary, Error,
  357. "Cannot import `Main//default`.");
  358. unit_info.emitter.Emit(import.node_id, ImportMainDefaultLibrary);
  359. return;
  360. }
  361. if (!is_import_implicit_current_package) {
  362. // Diagnose explicit imports of the same package that use the package
  363. // name.
  364. if (is_same_package || (is_file_implicit_main && is_explicit_main)) {
  365. CARBON_DIAGNOSTIC(
  366. ImportCurrentPackageByName, Error,
  367. "Imports from the current package must omit the package name.");
  368. unit_info.emitter.Emit(import.node_id, ImportCurrentPackageByName);
  369. return;
  370. }
  371. // Diagnose explicit imports from `Main`.
  372. if (is_explicit_main) {
  373. CARBON_DIAGNOSTIC(ImportMainPackage, Error,
  374. "Cannot import `Main` from other packages.");
  375. unit_info.emitter.Emit(import.node_id, ImportMainPackage);
  376. return;
  377. }
  378. }
  379. } else if (is_explicit_main) {
  380. // An implicit import with an explicit `Main` occurs when a `package` rule
  381. // has bad syntax, which will have been diagnosed when building the API map.
  382. // As a consequence, we return silently.
  383. return;
  384. }
  385. // Get the package imports.
  386. auto package_imports_it = unit_info.package_imports_map
  387. .try_emplace(import.package_id, import.node_id)
  388. .first;
  389. if (auto api = api_map.find(import_key); api != api_map.end()) {
  390. // Add references between the file and imported api.
  391. package_imports_it->second.imports.push_back({import, api->second});
  392. ++unit_info.imports_remaining;
  393. api->second->incoming_imports.push_back(&unit_info);
  394. } else {
  395. // The imported api is missing.
  396. package_imports_it->second.has_load_error = true;
  397. CARBON_DIAGNOSTIC(LibraryApiNotFound, Error,
  398. "Corresponding API not found.");
  399. CARBON_DIAGNOSTIC(ImportNotFound, Error, "Imported API not found.");
  400. unit_info.emitter.Emit(import.node_id, explicit_import_map
  401. ? ImportNotFound
  402. : LibraryApiNotFound);
  403. }
  404. }
  405. // Builds a map of `api` files which might be imported. Also diagnoses issues
  406. // related to the packaging because the strings are loaded as part of getting
  407. // the ImportKey (which we then do for `impl` files too).
  408. static auto BuildApiMapAndDiagnosePackaging(
  409. llvm::SmallVector<UnitInfo, 0>& unit_infos)
  410. -> llvm::DenseMap<ImportKey, UnitInfo*> {
  411. llvm::DenseMap<ImportKey, UnitInfo*> api_map;
  412. for (auto& unit_info : unit_infos) {
  413. const auto& packaging = unit_info.unit->parse_tree->packaging_directive();
  414. // An import key formed from the `package` or `library` directive. Or, for
  415. // Main//default, a placeholder key.
  416. auto import_key = packaging ? GetImportKey(unit_info, IdentifierId::Invalid,
  417. packaging->names)
  418. // Construct a boring key for Main//default.
  419. : ImportKey{"", ""};
  420. // Diagnose explicit `Main` uses before they become marked as possible
  421. // APIs.
  422. if (import_key.first == ExplicitMainName) {
  423. CARBON_DIAGNOSTIC(ExplicitMainPackage, Error,
  424. "`Main//default` must omit `package` directive.");
  425. CARBON_DIAGNOSTIC(ExplicitMainLibrary, Error,
  426. "Use `library` directive in `Main` package libraries.");
  427. unit_info.emitter.Emit(packaging->names.node_id,
  428. import_key.second.empty() ? ExplicitMainPackage
  429. : ExplicitMainLibrary);
  430. continue;
  431. }
  432. bool is_impl =
  433. packaging && packaging->api_or_impl == Parse::Tree::ApiOrImpl::Impl;
  434. // Add to the `api` map and diagnose duplicates. This occurs before the
  435. // file extension check because we might emit both diagnostics in situations
  436. // where the user forgets (or has syntax errors with) a package line
  437. // multiple times.
  438. if (!is_impl) {
  439. auto [entry, success] = api_map.insert({import_key, &unit_info});
  440. if (!success) {
  441. llvm::StringRef prev_filename =
  442. entry->second->unit->tokens->source().filename();
  443. if (packaging) {
  444. CARBON_DIAGNOSTIC(DuplicateLibraryApi, Error,
  445. "Library's API previously provided by `{0}`.",
  446. std::string);
  447. unit_info.emitter.Emit(packaging->names.node_id, DuplicateLibraryApi,
  448. prev_filename.str());
  449. } else {
  450. CARBON_DIAGNOSTIC(DuplicateMainApi, Error,
  451. "Main//default previously provided by `{0}`.",
  452. std::string);
  453. // Use the invalid node because there's no node to associate with.
  454. unit_info.emitter.Emit(Parse::NodeId::Invalid, DuplicateMainApi,
  455. prev_filename.str());
  456. }
  457. }
  458. }
  459. // Validate file extensions. Note imports rely the packaging directive, not
  460. // the extension. If the input is not a regular file, for example because it
  461. // is stdin, no filename checking is performed.
  462. if (unit_info.unit->tokens->source().is_regular_file()) {
  463. auto filename = unit_info.unit->tokens->source().filename();
  464. static constexpr llvm::StringLiteral ApiExt = ".carbon";
  465. static constexpr llvm::StringLiteral ImplExt = ".impl.carbon";
  466. bool is_api_with_impl_ext = !is_impl && filename.ends_with(ImplExt);
  467. auto want_ext = is_impl ? ImplExt : ApiExt;
  468. if (is_api_with_impl_ext || !filename.ends_with(want_ext)) {
  469. CARBON_DIAGNOSTIC(IncorrectExtension, Error,
  470. "File extension of `{0}` required for `{1}`.",
  471. llvm::StringLiteral, Lex::TokenKind);
  472. auto diag = unit_info.emitter.Build(
  473. packaging ? packaging->names.node_id : Parse::NodeId::Invalid,
  474. IncorrectExtension, want_ext,
  475. is_impl ? Lex::TokenKind::Impl : Lex::TokenKind::Api);
  476. if (is_api_with_impl_ext) {
  477. CARBON_DIAGNOSTIC(IncorrectExtensionImplNote, Note,
  478. "File extension of `{0}` only allowed for `{1}`.",
  479. llvm::StringLiteral, Lex::TokenKind);
  480. diag.Note(Parse::NodeId::Invalid, IncorrectExtensionImplNote, ImplExt,
  481. Lex::TokenKind::Impl);
  482. }
  483. diag.Emit();
  484. }
  485. }
  486. }
  487. return api_map;
  488. }
  489. auto CheckParseTrees(const SemIR::File& builtin_ir,
  490. llvm::MutableArrayRef<Unit> units,
  491. llvm::raw_ostream* vlog_stream) -> void {
  492. // Prepare diagnostic emitters in case we run into issues during package
  493. // checking.
  494. //
  495. // UnitInfo is big due to its SmallVectors, so we default to 0 on the stack.
  496. llvm::SmallVector<UnitInfo, 0> unit_infos;
  497. unit_infos.reserve(units.size());
  498. for (auto& unit : units) {
  499. unit_infos.emplace_back(unit);
  500. }
  501. llvm::DenseMap<ImportKey, UnitInfo*> api_map =
  502. BuildApiMapAndDiagnosePackaging(unit_infos);
  503. // Mark down imports for all files.
  504. llvm::SmallVector<UnitInfo*> ready_to_check;
  505. ready_to_check.reserve(units.size());
  506. for (auto& unit_info : unit_infos) {
  507. if (const auto& packaging =
  508. unit_info.unit->parse_tree->packaging_directive()) {
  509. if (packaging->api_or_impl == Parse::Tree::ApiOrImpl::Impl) {
  510. // An `impl` has an implicit import of its `api`.
  511. auto implicit_names = packaging->names;
  512. implicit_names.package_id = IdentifierId::Invalid;
  513. TrackImport(api_map, nullptr, unit_info, implicit_names);
  514. }
  515. }
  516. llvm::DenseMap<ImportKey, Parse::NodeId> explicit_import_map;
  517. for (const auto& import : unit_info.unit->parse_tree->imports()) {
  518. TrackImport(api_map, &explicit_import_map, unit_info, import);
  519. }
  520. // If there were no imports, mark the file as ready to check for below.
  521. if (unit_info.imports_remaining == 0) {
  522. ready_to_check.push_back(&unit_info);
  523. }
  524. }
  525. llvm::DenseMap<const SemIR::File*, Parse::NodeLocationConverter*>
  526. node_converters;
  527. // Check everything with no dependencies. Earlier entries with dependencies
  528. // will be checked as soon as all their dependencies have been checked.
  529. for (int check_index = 0;
  530. check_index < static_cast<int>(ready_to_check.size()); ++check_index) {
  531. auto* unit_info = ready_to_check[check_index];
  532. CheckParseTree(&node_converters, builtin_ir, *unit_info, vlog_stream);
  533. for (auto* incoming_import : unit_info->incoming_imports) {
  534. --incoming_import->imports_remaining;
  535. if (incoming_import->imports_remaining == 0) {
  536. ready_to_check.push_back(incoming_import);
  537. }
  538. }
  539. }
  540. // If there are still units with remaining imports, it means there's a
  541. // dependency loop.
  542. if (ready_to_check.size() < unit_infos.size()) {
  543. // Go through units and mask out unevaluated imports. This breaks everything
  544. // associated with a loop equivalently, whether it's part of it or depending
  545. // on a part of it.
  546. // TODO: Better identify cycles, maybe try to untangle them.
  547. for (auto& unit_info : unit_infos) {
  548. if (unit_info.imports_remaining > 0) {
  549. for (auto& [package_id, package_imports] :
  550. unit_info.package_imports_map) {
  551. for (auto* import_it = package_imports.imports.begin();
  552. import_it != package_imports.imports.end();) {
  553. if (*import_it->unit_info->unit->sem_ir) {
  554. // The import is checked, so continue.
  555. ++import_it;
  556. } else {
  557. // The import hasn't been checked, indicating a cycle.
  558. CARBON_DIAGNOSTIC(ImportCycleDetected, Error,
  559. "Import cannot be used due to a cycle. Cycle "
  560. "must be fixed to import.");
  561. unit_info.emitter.Emit(import_it->names.node_id,
  562. ImportCycleDetected);
  563. // Make this look the same as an import which wasn't found.
  564. package_imports.has_load_error = true;
  565. import_it = package_imports.imports.erase(import_it);
  566. }
  567. }
  568. }
  569. }
  570. }
  571. // Check the remaining file contents, which are probably broken due to
  572. // incomplete imports.
  573. for (auto& unit_info : unit_infos) {
  574. if (unit_info.imports_remaining > 0) {
  575. CheckParseTree(&node_converters, builtin_ir, unit_info, vlog_stream);
  576. }
  577. }
  578. }
  579. }
  580. } // namespace Carbon::Check