prelude.cpp 1.1 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(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 =
  12. Parse(arena, prelude_file_name, FileKind::Prelude, false);
  13. if (!parse_result.ok()) {
  14. // Try again with tracing, to help diagnose the problem.
  15. ErrorOr<AST> trace_parse_result =
  16. Parse(arena, prelude_file_name, FileKind::Prelude, true);
  17. CARBON_FATAL() << "Failed to parse prelude:\n"
  18. << 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