mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Export Carbon global variables and static vars to C++ (#7298)
Add `ExportVarToCpp`. This checks the `clang_decls` mapping and returns an existing decl if found. Otherwise, it creates a new `VarDecl` and adds it to the `clang_decls` mapping. When lowering, in `FileContext::BuildGlobalVariableDecl`, the `clang_decls` mapping is used to lookup an existing `llvm::GlobalVariable` for the instruction. If found, use that rather than creating a new one to avoid an unwanted second definition in the llvm IR.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
// 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-FILE: toolchain/testing/testdata/min_prelude/full.carbon
|
||||
// EXTRA-ARGS: --clang-arg=--std=c++20
|
||||
//
|
||||
// AUTOUPDATE
|
||||
// TIP: To test this file alone, run:
|
||||
// TIP: bazel test //toolchain/testing:file_test --test_arg=--file_tests=toolchain/check/testdata/interop/cpp/var/import/constexpr.carbon
|
||||
// TIP: To dump output, run:
|
||||
// TIP: bazel run //toolchain/testing:file_test -- --dump_output --file_tests=toolchain/check/testdata/interop/cpp/var/import/constexpr.carbon
|
||||
|
||||
// --- bool.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
import Cpp inline '''
|
||||
constexpr bool b = true;
|
||||
''';
|
||||
|
||||
class C(B:! bool) {}
|
||||
fn F() -> C(Cpp.b);
|
||||
let x: C(true) = F();
|
||||
|
||||
// --- int.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
import Cpp inline '''
|
||||
constexpr int i = 123;
|
||||
''';
|
||||
|
||||
class C(I:! i32) {}
|
||||
fn F() -> C(Cpp.i);
|
||||
let x: C(123) = F();
|
||||
|
||||
// --- float.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
import Cpp inline '''
|
||||
constexpr float flt = 123.5;
|
||||
''';
|
||||
|
||||
class C(V:! f32) {}
|
||||
fn F() -> C(Cpp.flt);
|
||||
let x: C(123.5) = F();
|
||||
|
||||
// --- pointer.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
import Cpp inline '''
|
||||
int i = 123;
|
||||
constexpr int* _Nonnull ptr = &i;
|
||||
''';
|
||||
|
||||
class C(V:! i32*) {}
|
||||
fn F() -> C(Cpp.ptr);
|
||||
let x: C(&Cpp.i) = F();
|
||||
Reference in New Issue
Block a user