prelude.cpp 1.2 KB

123456789101112131415161718192021222324252627282930
  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 "explorer/syntax/prelude.h"
  5. #include "explorer/syntax/parse.h"
  6. namespace Carbon {
  7. // Adds the Carbon prelude to `declarations`.
  8. void AddPrelude(llvm::vfs::FileSystem& fs, std::string_view prelude_file_name,
  9. Nonnull<Arena*> arena,
  10. std::vector<Nonnull<Declaration*>>* declarations,
  11. int* num_prelude_declarations) {
  12. ErrorOr<AST> parse_result =
  13. Parse(fs, arena, prelude_file_name, FileKind::Prelude, false);
  14. if (!parse_result.ok()) {
  15. // Try again with tracing, to help diagnose the problem.
  16. ErrorOr<AST> trace_parse_result =
  17. Parse(fs, arena, prelude_file_name, FileKind::Prelude, true);
  18. CARBON_FATAL("Failed to parse prelude:\n{0}", trace_parse_result.error());
  19. }
  20. const auto& prelude = *parse_result;
  21. declarations->insert(declarations->begin(), prelude.declarations.begin(),
  22. prelude.declarations.end());
  23. *num_prelude_declarations = prelude.declarations.size();
  24. }
  25. } // namespace Carbon