Files
carbon-lang/toolchain/check/check.h
T
Boaz Brickner 852d0191a9 Add support for importing C++ inline functions (#5427)
This requires:
* Making `FunctionDecl` mutable since generating code
(`HandleTopLevelDecl()`) requires a mutable declaration and since we
manually add `used` attribute to force code generation.
* Passing the file system to `Lower` since it's needed by Clang code
generation.
* Creating an internal Clang LLVM module and link it against the Carbon
LLVM module.

Demo:

```c++
// hello_world.h

extern int puts;
inline void hello_world() {
  ((int (*)(const char*))&puts)("hello world");
}
```

```carbon
// main.carbon

library "Main";

import Cpp library "hello_world.h";

fn Run() -> i32 {
  Cpp.hello_world();
  return 0;
}
```

```shell
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link main.o --output=demo
$ ./demo
hello world
```

Based on https://github.com/carbon-language/carbon-lang/pull/5406.

Part of #5405.
2025-05-21 07:02:05 +00:00

44 lines
1.5 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_CHECK_H_
#define CARBON_TOOLCHAIN_CHECK_CHECK_H_
#include "common/ostream.h"
#include "toolchain/base/shared_value_stores.h"
#include "toolchain/base/timings.h"
#include "toolchain/check/diagnostic_emitter.h"
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/parse/tree_and_subtrees.h"
#include "toolchain/sem_ir/file.h"
namespace Carbon::Check {
// Checking information that's tracked per file.
struct Unit {
Diagnostics::Consumer* consumer;
SharedValueStores* value_stores;
// The `timings` may be null if nothing is to be recorded.
Timings* timings;
// The unit's SemIR, provided as empty and filled in by CheckParseTrees.
SemIR::File* sem_ir;
// The Clang AST owned by `CompileSubcommand`.
std::unique_ptr<clang::ASTUnit>* cpp_ast = nullptr;
};
// Checks a group of parse trees. This will use imports to decide the order of
// checking.
auto CheckParseTrees(
llvm::MutableArrayRef<Unit> units,
llvm::ArrayRef<Parse::GetTreeAndSubtreesFn> tree_and_subtrees_getters,
bool prelude_import, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> fs,
llvm::StringRef target, llvm::raw_ostream* vlog_stream, bool fuzzing)
-> void;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_CHECK_H_