check.cpp 28 KB

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