Files
carbon-lang/explorer/syntax/prelude.cpp
T
Richard SmithandJon Ross-Perkins ea60e4f491 Track the file kind on SourceLocation rather than computing it from the file name. (#2999)
This will be more correct if a user file is named prelude.carbon, and
reduces the explorer runtime by about 10% by removing a string
comparison from the check for whether trace output is enabled.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2023-07-19 23:07:53 +00:00

31 lines
1.1 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "explorer/syntax/prelude.h"
#include "explorer/syntax/parse.h"
namespace Carbon {
// Adds the Carbon prelude to `declarations`.
void AddPrelude(std::string_view prelude_file_name, Nonnull<Arena*> arena,
std::vector<Nonnull<Declaration*>>* declarations,
int* num_prelude_declarations) {
ErrorOr<AST> parse_result =
Parse(arena, prelude_file_name, FileKind::Prelude, false);
if (!parse_result.ok()) {
// Try again with tracing, to help diagnose the problem.
ErrorOr<AST> trace_parse_result =
Parse(arena, prelude_file_name, FileKind::Prelude, true);
CARBON_FATAL() << "Failed to parse prelude:\n"
<< trace_parse_result.error();
}
const auto& prelude = *parse_result;
declarations->insert(declarations->begin(), prelude.declarations.begin(),
prelude.declarations.end());
*num_prelude_declarations = prelude.declarations.size();
}
} // namespace Carbon