Files
carbon-lang/toolchain/check/cpp/generate_ast.h
Christopher Di Bella 57bf903ef1 Keep resolving generics and templates until they're all resolved (#7957)
`FinishAst` resolves C++ template instantiation. Template instantiation
can complete Carbon types, generate thunks, and call generic functions.
These operations can add new entries to `definitions_required_by_use()`.
Similarly, resolving any of those can generate more C++ templates that
need instantiating. This change moves `FinishAst` ahead of
`CheckRequiredDefinitions` and loops until no new Carbon definitions
have been added. This prevents later passes from crashing when
evaluating specifics on unresolved generics or uninstantiated templates.

`FinishAst` also disconnects the AST. Subsequent definitions and `impl`s
may depend on C++ interop, so the AST must remain connected until all
the Carbon end-of-file checks complete. We achieve this by decoupling
AST disconnection from finalisation.

Assisted-by: Antigravity with Gemini
Assisted-by: Antigravity with Claude Opus 5.5 Max
2026-10-26 20:52:43 +00:00

71 lines
2.6 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
#ifndef CARBON_TOOLCHAIN_CHECK_CPP_GENERATE_AST_H_
#define CARBON_TOOLCHAIN_CHECK_CPP_GENERATE_AST_H_
#include <memory>
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "toolchain/check/context.h"
#include "toolchain/parse/tree.h"
#include "toolchain/sem_ir/cpp_domain.h"
#include "toolchain/sem_ir/ids.h"
namespace clang {
class CompilerInvocation;
class CompilerInstance;
class Parser;
class ExternalSemaSource;
class CodeGenerator;
} // namespace clang
namespace Carbon::Diagnostics {
class Consumer;
} // namespace Carbon::Diagnostics
namespace Carbon::Check {
// Initializes a Clang compilation instance, which can be used to parse C++ code
// within one or more Carbon files. Returns the initialized state, or null on
// failure.
auto InitializeCppDomain(
Diagnostics::Consumer& consumer, llvm::ArrayRef<SemIR::CppInputFile> inputs,
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
llvm::LLVMContext* llvm_context,
std::shared_ptr<clang::CompilerInvocation> base_invocation)
-> std::unique_ptr<SemIR::CppDomain>;
// Generates a Clang AST for the given C++ imports and sets it as the context's
// `cpp_context` and the SemIR's `cpp_file`. Returns a bool that represents
// whether compilation was successful.
auto GenerateAst(Context& context,
llvm::ArrayRef<Parse::Tree::PackagingNames> imports,
SemIR::CppDomain& domain) -> bool;
// Injects C++ code from `inline Cpp` into the active Clang AST context.
// Returns a bool representing whether parsing was successful.
auto InjectAstFromInlineCode(Context& context, SemIR::LocId loc_id,
llvm::StringRef source_code) -> void;
// Finalizes the C++ AST fragment for the given checking context. Performs end
// of file steps such as template instantiation and warning on unused
// declarations. This may be called more than once for the same context, to
// perform template instantiations that later Carbon checking requires.
auto FinalizeCppAstFragment(Context& context) -> void;
// Disconnects C++ AST generation from the given checking context at the end of
// checking.
auto DisconnectCppAst(Context& context) -> void;
// Finalizes a C++ domain at the end of checking all files.
auto FinalizeCppDomain(SemIR::CppDomain& domain) -> void;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_CPP_GENERATE_AST_H_