prelude.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031
  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"
  19. << trace_parse_result.error();
  20. }
  21. const auto& prelude = *parse_result;
  22. declarations->insert(declarations->begin(), prelude.declarations.begin(),
  23. prelude.declarations.end());
  24. *num_prelude_declarations = prelude.declarations.size();
  25. }
  26. } // namespace Carbon