prelude.cpp 1.1 KB

12345678910111213141516171819202122232425262728
  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(std::string_view prelude_file_name, Nonnull<Arena*> arena,
  9. std::vector<Nonnull<Declaration*>>* declarations,
  10. int* num_prelude_declarations) {
  11. ErrorOr<AST> parse_result = Parse(arena, prelude_file_name, false);
  12. if (!parse_result.ok()) {
  13. // Try again with tracing, to help diagnose the problem.
  14. ErrorOr<AST> trace_parse_result = Parse(arena, prelude_file_name, true);
  15. CARBON_FATAL() << "Failed to parse prelude:\n"
  16. << trace_parse_result.error();
  17. }
  18. const auto& prelude = *parse_result;
  19. declarations->insert(declarations->begin(), prelude.declarations.begin(),
  20. prelude.declarations.end());
  21. *num_prelude_declarations = prelude.declarations.size();
  22. }
  23. } // namespace Carbon