mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-01 22:02:45 +01:00
And generally replace "node" by "inst" in the code and "instruction" in comments. --------- Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
72 lines
2.9 KiB
C++
72 lines
2.9 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 "common/check.h"
|
|
|
|
#include "toolchain/base/pretty_stack_trace_function.h"
|
|
#include "toolchain/check/context.h"
|
|
#include "toolchain/parse/tree_node_location_translator.h"
|
|
#include "toolchain/sem_ir/file.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto CheckParseTree(SharedValueStores& value_stores,
|
|
const SemIR::File& builtin_ir,
|
|
const Lex::TokenizedBuffer& tokens,
|
|
const Parse::Tree& parse_tree, DiagnosticConsumer& consumer,
|
|
llvm::raw_ostream* vlog_stream) -> SemIR::File {
|
|
auto sem_ir = SemIR::File(value_stores, tokens.filename().str(), &builtin_ir);
|
|
|
|
Parse::NodeLocationTranslator translator(&tokens, &parse_tree);
|
|
ErrorTrackingDiagnosticConsumer err_tracker(consumer);
|
|
DiagnosticEmitter<Parse::Node> emitter(translator, err_tracker);
|
|
|
|
Check::Context context(tokens, emitter, parse_tree, sem_ir, vlog_stream);
|
|
PrettyStackTraceFunction context_dumper(
|
|
[&](llvm::raw_ostream& output) { context.PrintForStackDump(output); });
|
|
|
|
// Add a block for the Parse::Tree.
|
|
context.inst_block_stack().Push();
|
|
context.PushScope();
|
|
|
|
// Loops over all nodes in the tree. On some errors, this may return early,
|
|
// for example if an unrecoverable state is encountered.
|
|
for (auto parse_node : parse_tree.postorder()) {
|
|
// clang warns on unhandled enum values; clang-tidy is incorrect here.
|
|
// NOLINTNEXTLINE(bugprone-switch-missing-default-case)
|
|
switch (auto parse_kind = parse_tree.node_kind(parse_node)) {
|
|
#define CARBON_PARSE_NODE_KIND(Name) \
|
|
case Parse::NodeKind::Name: { \
|
|
if (!Check::Handle##Name(context, parse_node)) { \
|
|
CARBON_CHECK(err_tracker.seen_error()) \
|
|
<< "Handle" #Name " returned false without printing a diagnostic"; \
|
|
sem_ir.set_has_errors(true); \
|
|
return sem_ir; \
|
|
} \
|
|
break; \
|
|
}
|
|
#include "toolchain/parse/node_kind.def"
|
|
}
|
|
}
|
|
|
|
// Pop information for the file-level scope.
|
|
sem_ir.set_top_inst_block_id(context.inst_block_stack().Pop());
|
|
context.PopScope();
|
|
|
|
context.VerifyOnFinish();
|
|
|
|
sem_ir.set_has_errors(err_tracker.seen_error());
|
|
|
|
#ifndef NDEBUG
|
|
if (auto verify = sem_ir.Verify(); !verify.ok()) {
|
|
CARBON_FATAL() << sem_ir << "Built invalid semantics IR: " << verify.error()
|
|
<< "\n";
|
|
}
|
|
#endif
|
|
|
|
return sem_ir;
|
|
}
|
|
|
|
} // namespace Carbon::Check
|