44
55#include " toolchain/check/check_unit.h"
66
7+ #include < string>
8+
9+ #include " llvm/ADT/IntrusiveRefCntPtr.h"
10+ #include " llvm/ADT/StringRef.h"
11+ #include " llvm/Support/VirtualFileSystem.h"
712#include " toolchain/base/kind_switch.h"
813#include " toolchain/base/pretty_stack_trace_function.h"
914#include " toolchain/check/generic.h"
1015#include " toolchain/check/handle.h"
1116#include " toolchain/check/impl.h"
1217#include " toolchain/check/import.h"
18+ #include " toolchain/check/import_cpp.h"
1319#include " toolchain/check/import_ref.h"
1420#include " toolchain/check/node_id_traversal.h"
1521
@@ -29,9 +35,11 @@ static auto GetImportedIRCount(UnitAndImports* unit_and_imports) -> int {
2935}
3036
3137CheckUnit::CheckUnit (UnitAndImports* unit_and_imports, int total_ir_count,
38+ llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
3239 llvm::raw_ostream* vlog_stream)
3340 : unit_and_imports_(unit_and_imports),
3441 total_ir_count_ (total_ir_count),
42+ fs_(fs),
3543 vlog_stream_(vlog_stream),
3644 emitter_(*unit_and_imports_->unit->sem_ir_converter,
3745 unit_and_imports_->err_tracker),
@@ -130,6 +138,7 @@ auto CheckUnit::InitPackageScopeAndImports() -> void {
130138 ImportCurrentPackage (package_inst_id, namespace_type_id);
131139 CARBON_CHECK (context_.scope_stack ().PeekIndex () == ScopeIndex::Package);
132140 ImportOtherPackages (namespace_type_id);
141+ ImportCppPackages ();
133142}
134143
135144auto CheckUnit::CollectDirectImports (
@@ -325,6 +334,37 @@ auto CheckUnit::ImportOtherPackages(SemIR::TypeId namespace_type_id) -> void {
325334 }
326335}
327336
337+ auto CheckUnit::ImportCppPackages () -> void {
338+ for (const auto & import : unit_and_imports_->cpp_imports ) {
339+ llvm::StringRef cpp_file_path =
340+ unit_and_imports_->unit ->value_stores ->string_literal_values ().Get (
341+ import .library_id );
342+
343+ auto file = fs_->openFileForRead (cpp_file_path);
344+ if (!file) {
345+ CARBON_DIAGNOSTIC (CppInteropFileNotFound, Error,
346+ " file '{0}' couldn't be opened for reading: {1}" ,
347+ std::string, std::string);
348+ emitter_.Emit (import .node_id , CppInteropFileNotFound, cpp_file_path.str (),
349+ file.getError ().message ());
350+ continue ;
351+ }
352+
353+ llvm::vfs::File& file_ref = *file.get ();
354+ auto buffer = file_ref.getBuffer (cpp_file_path);
355+ if (!buffer) {
356+ CARBON_DIAGNOSTIC (CppInteropFailedAccessingFileBuffer, Error,
357+ " failed accessing buffer of file '{0}': {1}" ,
358+ std::string, std::string);
359+ emitter_.Emit (import .node_id , CppInteropFailedAccessingFileBuffer,
360+ cpp_file_path.str (), buffer.getError ().message ());
361+ continue ;
362+ }
363+ ImportCppFile (context_, import .node_id , cpp_file_path,
364+ buffer.get ()->getBuffer ());
365+ }
366+ }
367+
328368// Loops over all nodes in the tree. On some errors, this may return early,
329369// for example if an unrecoverable state is encountered.
330370// NOLINTNEXTLINE(readability-function-size)
0 commit comments