check.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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/base/value_store.h"
  8. #include "toolchain/check/context.h"
  9. #include "toolchain/diagnostics/diagnostic_emitter.h"
  10. #include "toolchain/lex/token_kind.h"
  11. #include "toolchain/parse/tree.h"
  12. #include "toolchain/parse/tree_node_location_translator.h"
  13. #include "toolchain/sem_ir/file.h"
  14. #include "toolchain/sem_ir/typed_insts.h"
  15. namespace Carbon::Check {
  16. struct UnitInfo {
  17. // A given import within the file, with its destination.
  18. struct Import {
  19. Parse::Tree::PackagingNames names;
  20. UnitInfo* unit_info;
  21. };
  22. // A file's imports corresponding to a single package, for the map.
  23. struct PackageImports {
  24. // Use the constructor so that the SmallVector is only constructed
  25. // as-needed.
  26. explicit PackageImports(Parse::NodeId node) : node(node) {}
  27. // The first `import` directive in the file, which declared the package's
  28. // identifier (even if the import failed). Used for associating diagnostics
  29. // not specific to a single import.
  30. Parse::NodeId node;
  31. // Whether there's an import that failed to load.
  32. bool has_load_error = false;
  33. // The list of valid imports.
  34. llvm::SmallVector<Import> imports;
  35. };
  36. explicit UnitInfo(Unit& unit)
  37. : unit(&unit),
  38. translator(unit.tokens, unit.tokens->source().filename(),
  39. unit.parse_tree),
  40. err_tracker(*unit.consumer),
  41. emitter(translator, err_tracker) {}
  42. Unit* unit;
  43. // Emitter information.
  44. Parse::NodeLocationTranslator translator;
  45. ErrorTrackingDiagnosticConsumer err_tracker;
  46. DiagnosticEmitter<Parse::NodeId> emitter;
  47. // A map of package names to outgoing imports. If the
  48. // import's target isn't available, the unit will be nullptr to assist with
  49. // name lookup. Invalid imports (for example, `import Main;`) aren't added
  50. // because they won't add identifiers to name lookup.
  51. llvm::DenseMap<IdentifierId, PackageImports> package_imports_map;
  52. // The remaining number of imports which must be checked before this unit can
  53. // be processed.
  54. int32_t imports_remaining = 0;
  55. // A list of incoming imports. This will be empty for `impl` files, because
  56. // imports only touch `api` files.
  57. llvm::SmallVector<UnitInfo*> incoming_imports;
  58. };
  59. // Add imports to the root block.
  60. static auto AddImports(Context& context, UnitInfo& unit_info) -> void {
  61. for (auto& [package_id, package_imports] : unit_info.package_imports_map) {
  62. llvm::SmallVector<const SemIR::File*> sem_irs;
  63. for (auto import : package_imports.imports) {
  64. sem_irs.push_back(&**import.unit_info->unit->sem_ir);
  65. }
  66. context.AddPackageImports(package_imports.node, package_id, sem_irs,
  67. package_imports.has_load_error);
  68. }
  69. }
  70. // Loops over all nodes in the tree. On some errors, this may return early,
  71. // for example if an unrecoverable state is encountered.
  72. static auto ProcessParseNodes(Context& context,
  73. ErrorTrackingDiagnosticConsumer& err_tracker)
  74. -> bool {
  75. for (auto parse_node : context.parse_tree().postorder()) {
  76. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  77. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  78. switch (auto parse_kind = context.parse_tree().node_kind(parse_node)) {
  79. #define CARBON_PARSE_NODE_KIND(Name) \
  80. case Parse::NodeKind::Name: { \
  81. if (!Check::Handle##Name(context, parse_node)) { \
  82. CARBON_CHECK(err_tracker.seen_error()) \
  83. << "Handle" #Name " returned false without printing a diagnostic"; \
  84. return false; \
  85. } \
  86. break; \
  87. }
  88. #include "toolchain/parse/node_kind.def"
  89. }
  90. }
  91. return true;
  92. }
  93. // Produces and checks the IR for the provided Parse::Tree.
  94. // TODO: Both valid and invalid imports should be recorded on the SemIR. Invalid
  95. // imports should suppress errors where it makes sense.
  96. static auto CheckParseTree(const SemIR::File& builtin_ir, UnitInfo& unit_info,
  97. llvm::raw_ostream* vlog_stream) -> void {
  98. unit_info.unit->sem_ir->emplace(
  99. *unit_info.unit->value_stores,
  100. unit_info.unit->tokens->source().filename().str(), &builtin_ir);
  101. // For ease-of-access.
  102. SemIR::File& sem_ir = **unit_info.unit->sem_ir;
  103. Context context(*unit_info.unit->tokens, unit_info.emitter,
  104. *unit_info.unit->parse_tree, sem_ir, vlog_stream);
  105. PrettyStackTraceFunction context_dumper(
  106. [&](llvm::raw_ostream& output) { context.PrintForStackDump(output); });
  107. // Add a block for the file.
  108. context.inst_block_stack().Push();
  109. // Define the package scope, with an instruction for `package` expressions to
  110. // reference.
  111. auto package_scope = context.name_scopes().Add();
  112. auto package_inst = context.AddInst(SemIR::Namespace{
  113. Parse::NodeId::Invalid,
  114. context.GetBuiltinType(SemIR::BuiltinKind::NamespaceType),
  115. package_scope});
  116. CARBON_CHECK(package_inst == SemIR::InstId::PackageNamespace);
  117. context.PushScope(SemIR::InstId::Invalid, package_scope);
  118. AddImports(context, unit_info);
  119. if (!ProcessParseNodes(context, unit_info.err_tracker)) {
  120. context.sem_ir().set_has_errors(true);
  121. return;
  122. }
  123. // Pop information for the file-level scope.
  124. sem_ir.set_top_inst_block_id(context.inst_block_stack().Pop());
  125. context.PopScope();
  126. context.VerifyOnFinish();
  127. sem_ir.set_has_errors(unit_info.err_tracker.seen_error());
  128. #ifndef NDEBUG
  129. if (auto verify = sem_ir.Verify(); !verify.ok()) {
  130. CARBON_FATAL() << sem_ir << "Built invalid semantics IR: " << verify.error()
  131. << "\n";
  132. }
  133. #endif
  134. }
  135. // The package and library names, used as map keys.
  136. using ImportKey = std::pair<llvm::StringRef, llvm::StringRef>;
  137. // Returns a key form of the package object. file_package_id is only used for
  138. // imports, not the main package directive; as a consequence, it will be invalid
  139. // for the main package directive.
  140. static auto GetImportKey(UnitInfo& unit_info, IdentifierId file_package_id,
  141. Parse::Tree::PackagingNames names) -> ImportKey {
  142. auto* stores = unit_info.unit->value_stores;
  143. llvm::StringRef package_name =
  144. names.package_id.is_valid() ? stores->identifiers().Get(names.package_id)
  145. : file_package_id.is_valid() ? stores->identifiers().Get(file_package_id)
  146. : "";
  147. llvm::StringRef library_name =
  148. names.library_id.is_valid()
  149. ? stores->string_literals().Get(names.library_id)
  150. : "";
  151. return {package_name, library_name};
  152. }
  153. static constexpr llvm::StringLiteral ExplicitMainName = "Main";
  154. // Marks an import as required on both the source and target file.
  155. //
  156. // The ID comparisons between the import and unit are okay because they both
  157. // come from the same file.
  158. static auto TrackImport(
  159. llvm::DenseMap<ImportKey, UnitInfo*>& api_map,
  160. llvm::DenseMap<ImportKey, Parse::NodeId>* explicit_import_map,
  161. UnitInfo& unit_info, Parse::Tree::PackagingNames import) -> void {
  162. const auto& packaging = unit_info.unit->parse_tree->packaging_directive();
  163. IdentifierId file_package_id =
  164. packaging ? packaging->names.package_id : IdentifierId::Invalid;
  165. auto import_key = GetImportKey(unit_info, file_package_id, import);
  166. // True if the import has `Main` as the package name, even if it comes from
  167. // the file's packaging (diagnostics may differentiate).
  168. bool is_explicit_main = import_key.first == ExplicitMainName;
  169. // Explicit imports need more validation than implicit ones. We try to do
  170. // these in an order of imports that should be removed, followed by imports
  171. // that might be valid with syntax fixes.
  172. if (explicit_import_map) {
  173. // Diagnose redundant imports.
  174. if (auto [insert_it, success] =
  175. explicit_import_map->insert({import_key, import.node});
  176. !success) {
  177. CARBON_DIAGNOSTIC(RepeatedImport, Error,
  178. "Library imported more than once.");
  179. CARBON_DIAGNOSTIC(FirstImported, Note, "First import here.");
  180. unit_info.emitter.Build(import.node, RepeatedImport)
  181. .Note(insert_it->second, FirstImported)
  182. .Emit();
  183. return;
  184. }
  185. // True if the file's package is implicitly `Main` (by omitting an explicit
  186. // package name).
  187. bool is_file_implicit_main =
  188. !packaging || !packaging->names.package_id.is_valid();
  189. // True if the import is using implicit "current package" syntax (by
  190. // omitting an explicit package name).
  191. bool is_import_implicit_current_package = !import.package_id.is_valid();
  192. // True if the import is using `default` library syntax.
  193. bool is_import_default_library = !import.library_id.is_valid();
  194. // True if the import and file point at the same package, even by
  195. // incorrectly specifying the current package name to `import`.
  196. bool is_same_package = is_import_implicit_current_package ||
  197. import.package_id == file_package_id;
  198. // True if the import points at the same library as the file's library.
  199. bool is_same_library =
  200. is_same_package &&
  201. (packaging ? import.library_id == packaging->names.library_id
  202. : is_import_default_library);
  203. // Diagnose explicit imports of the same library, whether from `api` or
  204. // `impl`.
  205. if (is_same_library) {
  206. CARBON_DIAGNOSTIC(ExplicitImportApi, Error,
  207. "Explicit import of `api` from `impl` file is "
  208. "redundant with implicit import.");
  209. CARBON_DIAGNOSTIC(ImportSelf, Error, "File cannot import itself.");
  210. bool is_impl =
  211. !packaging || packaging->api_or_impl == Parse::Tree::ApiOrImpl::Impl;
  212. unit_info.emitter.Emit(import.node,
  213. is_impl ? ExplicitImportApi : ImportSelf);
  214. return;
  215. }
  216. // Diagnose explicit imports of `Main//default`. There is no `api` for it.
  217. // This lets other diagnostics handle explicit `Main` package naming.
  218. if (is_file_implicit_main && is_import_implicit_current_package &&
  219. is_import_default_library) {
  220. CARBON_DIAGNOSTIC(ImportMainDefaultLibrary, Error,
  221. "Cannot import `Main//default`.");
  222. unit_info.emitter.Emit(import.node, ImportMainDefaultLibrary);
  223. return;
  224. }
  225. if (!is_import_implicit_current_package) {
  226. // Diagnose explicit imports of the same package that use the package
  227. // name.
  228. if (is_same_package || (is_file_implicit_main && is_explicit_main)) {
  229. CARBON_DIAGNOSTIC(
  230. ImportCurrentPackageByName, Error,
  231. "Imports from the current package must omit the package name.");
  232. unit_info.emitter.Emit(import.node, ImportCurrentPackageByName);
  233. return;
  234. }
  235. // Diagnose explicit imports from `Main`.
  236. if (is_explicit_main) {
  237. CARBON_DIAGNOSTIC(ImportMainPackage, Error,
  238. "Cannot import `Main` from other packages.");
  239. unit_info.emitter.Emit(import.node, ImportMainPackage);
  240. return;
  241. }
  242. }
  243. } else if (is_explicit_main) {
  244. // An implicit import with an explicit `Main` occurs when a `package` rule
  245. // has bad syntax, which will have been diagnosed when building the API map.
  246. // As a consequence, we return silently.
  247. return;
  248. }
  249. // Get the package imports.
  250. auto package_imports_it =
  251. unit_info.package_imports_map.try_emplace(import.package_id, import.node)
  252. .first;
  253. if (auto api = api_map.find(import_key); api != api_map.end()) {
  254. // Add references between the file and imported api.
  255. package_imports_it->second.imports.push_back({import, api->second});
  256. ++unit_info.imports_remaining;
  257. api->second->incoming_imports.push_back(&unit_info);
  258. } else {
  259. // The imported api is missing.
  260. package_imports_it->second.has_load_error = true;
  261. CARBON_DIAGNOSTIC(LibraryApiNotFound, Error,
  262. "Corresponding API not found.");
  263. CARBON_DIAGNOSTIC(ImportNotFound, Error, "Imported API not found.");
  264. unit_info.emitter.Emit(
  265. import.node, explicit_import_map ? ImportNotFound : LibraryApiNotFound);
  266. }
  267. }
  268. // Builds a map of `api` files which might be imported. Also diagnoses issues
  269. // related to the packaging because the strings are loaded as part of getting
  270. // the ImportKey (which we then do for `impl` files too).
  271. static auto BuildApiMapAndDiagnosePackaging(
  272. llvm::SmallVector<UnitInfo, 0>& unit_infos)
  273. -> llvm::DenseMap<ImportKey, UnitInfo*> {
  274. llvm::DenseMap<ImportKey, UnitInfo*> api_map;
  275. for (auto& unit_info : unit_infos) {
  276. const auto& packaging = unit_info.unit->parse_tree->packaging_directive();
  277. // An import key formed from the `package` or `library` directive. Or, for
  278. // Main//default, a placeholder key.
  279. auto import_key = packaging ? GetImportKey(unit_info, IdentifierId::Invalid,
  280. packaging->names)
  281. // Construct a boring key for Main//default.
  282. : ImportKey{"", ""};
  283. // Diagnose explicit `Main` uses before they become marked as possible
  284. // APIs.
  285. if (import_key.first == ExplicitMainName) {
  286. CARBON_DIAGNOSTIC(ExplicitMainPackage, Error,
  287. "`Main//default` must omit `package` directive.");
  288. CARBON_DIAGNOSTIC(ExplicitMainLibrary, Error,
  289. "Use `library` directive in `Main` package libraries.");
  290. unit_info.emitter.Emit(packaging->names.node, import_key.second.empty()
  291. ? ExplicitMainPackage
  292. : ExplicitMainLibrary);
  293. continue;
  294. }
  295. bool is_impl =
  296. packaging && packaging->api_or_impl == Parse::Tree::ApiOrImpl::Impl;
  297. // Add to the `api` map and diagnose duplicates. This occurs before the
  298. // file extension check because we might emit both diagnostics in situation
  299. // where the user forgets (or has syntax errors with) a package line
  300. // multiple times.
  301. if (!is_impl) {
  302. auto [entry, success] = api_map.insert({import_key, &unit_info});
  303. if (!success) {
  304. llvm::StringRef prev_filename =
  305. entry->second->unit->tokens->source().filename();
  306. if (packaging) {
  307. CARBON_DIAGNOSTIC(DuplicateLibraryApi, Error,
  308. "Library's API previously provided by `{0}`.",
  309. std::string);
  310. unit_info.emitter.Emit(packaging->names.node, DuplicateLibraryApi,
  311. prev_filename.str());
  312. } else {
  313. CARBON_DIAGNOSTIC(DuplicateMainApi, Error,
  314. "Main//default previously provided by `{0}`.",
  315. std::string);
  316. // Use the invalid node because there's no node to associate with.
  317. unit_info.emitter.Emit(Parse::NodeId::Invalid, DuplicateMainApi,
  318. prev_filename.str());
  319. }
  320. }
  321. }
  322. // Validate file extensions. Note imports rely the packaging directive, not
  323. // the extension. If the input is not a regular file, for example because it
  324. // is stdin, no filename checking is performed.
  325. if (unit_info.unit->tokens->source().is_regular_file()) {
  326. auto filename = unit_info.unit->tokens->source().filename();
  327. static constexpr llvm::StringLiteral ApiExt = ".carbon";
  328. static constexpr llvm::StringLiteral ImplExt = ".impl.carbon";
  329. bool is_api_with_impl_ext = !is_impl && filename.ends_with(ImplExt);
  330. auto want_ext = is_impl ? ImplExt : ApiExt;
  331. if (is_api_with_impl_ext || !filename.ends_with(want_ext)) {
  332. CARBON_DIAGNOSTIC(IncorrectExtension, Error,
  333. "File extension of `{0}` required for `{1}`.",
  334. llvm::StringLiteral, Lex::TokenKind);
  335. auto diag = unit_info.emitter.Build(
  336. packaging ? packaging->names.node : Parse::NodeId::Invalid,
  337. IncorrectExtension, want_ext,
  338. is_impl ? Lex::TokenKind::Impl : Lex::TokenKind::Api);
  339. if (is_api_with_impl_ext) {
  340. CARBON_DIAGNOSTIC(IncorrectExtensionImplNote, Note,
  341. "File extension of `{0}` only allowed for `{1}`.",
  342. llvm::StringLiteral, Lex::TokenKind);
  343. diag.Note(Parse::NodeId::Invalid, IncorrectExtensionImplNote, ImplExt,
  344. Lex::TokenKind::Impl);
  345. }
  346. diag.Emit();
  347. }
  348. }
  349. }
  350. return api_map;
  351. }
  352. auto CheckParseTrees(const SemIR::File& builtin_ir,
  353. llvm::MutableArrayRef<Unit> units,
  354. llvm::raw_ostream* vlog_stream) -> void {
  355. // Prepare diagnostic emitters in case we run into issues during package
  356. // checking.
  357. //
  358. // UnitInfo is big due to its SmallVectors, so we default to 0 on the stack.
  359. llvm::SmallVector<UnitInfo, 0> unit_infos;
  360. unit_infos.reserve(units.size());
  361. for (auto& unit : units) {
  362. unit_infos.emplace_back(unit);
  363. }
  364. llvm::DenseMap<ImportKey, UnitInfo*> api_map =
  365. BuildApiMapAndDiagnosePackaging(unit_infos);
  366. // Mark down imports for all files.
  367. llvm::SmallVector<UnitInfo*> ready_to_check;
  368. ready_to_check.reserve(units.size());
  369. for (auto& unit_info : unit_infos) {
  370. if (const auto& packaging =
  371. unit_info.unit->parse_tree->packaging_directive()) {
  372. if (packaging->api_or_impl == Parse::Tree::ApiOrImpl::Impl) {
  373. // An `impl` has an implicit import of its `api`.
  374. TrackImport(api_map, nullptr, unit_info, packaging->names);
  375. }
  376. }
  377. llvm::DenseMap<ImportKey, Parse::NodeId> explicit_import_map;
  378. for (const auto& import : unit_info.unit->parse_tree->imports()) {
  379. TrackImport(api_map, &explicit_import_map, unit_info, import);
  380. }
  381. // If there were no imports, mark the file as ready to check for below.
  382. if (unit_info.imports_remaining == 0) {
  383. ready_to_check.push_back(&unit_info);
  384. }
  385. }
  386. // Check everything with no dependencies. Earlier entries with dependencies
  387. // will be checked as soon as all their dependencies have been checked.
  388. for (int check_index = 0;
  389. check_index < static_cast<int>(ready_to_check.size()); ++check_index) {
  390. auto* unit_info = ready_to_check[check_index];
  391. CheckParseTree(builtin_ir, *unit_info, vlog_stream);
  392. for (auto* incoming_import : unit_info->incoming_imports) {
  393. --incoming_import->imports_remaining;
  394. if (incoming_import->imports_remaining == 0) {
  395. ready_to_check.push_back(incoming_import);
  396. }
  397. }
  398. }
  399. // If there are still units with remaining imports, it means there's a
  400. // dependency loop.
  401. if (ready_to_check.size() < unit_infos.size()) {
  402. // Go through units and mask out unevaluated imports. This breaks everything
  403. // associated with a loop equivalently, whether it's part of it or depending
  404. // on a part of it.
  405. // TODO: Better identify cycles, maybe try to untangle them.
  406. for (auto& unit_info : unit_infos) {
  407. if (unit_info.imports_remaining > 0) {
  408. for (auto& [package_id, package_imports] :
  409. unit_info.package_imports_map) {
  410. for (auto* import_it = package_imports.imports.begin();
  411. import_it != package_imports.imports.end();) {
  412. if (*import_it->unit_info->unit->sem_ir) {
  413. // The import is checked, so continue.
  414. ++import_it;
  415. } else {
  416. // The import hasn't been checked, indicating a cycle.
  417. CARBON_DIAGNOSTIC(ImportCycleDetected, Error,
  418. "Import cannot be used due to a cycle. Cycle "
  419. "must be fixed to import.");
  420. unit_info.emitter.Emit(import_it->names.node,
  421. ImportCycleDetected);
  422. // Make this look the same as an import which wasn't found.
  423. package_imports.has_load_error = true;
  424. import_it = package_imports.imports.erase(import_it);
  425. }
  426. }
  427. }
  428. }
  429. }
  430. // Check the remaining file contents, which are probably broken due to
  431. // incomplete imports.
  432. for (auto& unit_info : unit_infos) {
  433. if (unit_info.imports_remaining > 0) {
  434. CheckParseTree(builtin_ir, unit_info, vlog_stream);
  435. }
  436. }
  437. }
  438. }
  439. } // namespace Carbon::Check