Add support for using C++ double type in imported function declarations (#5868)

Carbon only supports f64, so only double can be mapped.

https://github.com/carbon-language/carbon-lang/blob/30f0ddab71bda71f8789080962b1fe8a5938e327/toolchain/check/type.cpp#L54

C++ Interop Demo:

```c++
// hello_world.h

auto hello_world(double x) -> void;
```

```c++
// hello_world.cpp

#include "hello_world.h"

#include <cstdio>

auto hello_world(double x) -> void {
  printf("double: %f\n", x);
}
```

```carbon
// main.carbon

library "Main";

import Cpp library "hello_world.h";

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

```shell
$ clang -c hello_world.cpp
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link hello_world.o main.o --output=demo
$ $ ./demo
double: 0.250000
```

Before this change:

```shell
$ bazel-bin/toolchain/carbon compile main.carbon
main.carbon:8:3: error: semantics TODO: `Unsupported: parameter type: double`
  Cpp.hello_world(0.25);
  ^~~~~~~~~~~~~~~
main.carbon:8:3: note: in `Cpp` name lookup for `hello_world`
  Cpp.hello_world(0.25);
  ^~~~~~~~~~~~~~~
```

Part of #5263.
This commit is contained in:
Boaz Brickner
2025-07-30 07:13:59 +00:00
committed by GitHub
parent ef475d8197
commit f0cff612eb
5 changed files with 117 additions and 19 deletions
+9
View File
@@ -794,6 +794,15 @@ static auto MapBuiltinType(Context& context, clang::QualType qual_type,
return MakeIntType(context, context.ints().Add(width), is_signed);
}
// TODO: Handle integer types that map to named aliases.
} else if (type.isDoubleType()) {
// TODO: Handle other floating point types when Carbon supports fN where N
// != 64.
CARBON_CHECK(ast_context.getTypeSize(qual_type) == 64);
CARBON_CHECK(ast_context.hasSameType(qual_type, ast_context.DoubleTy));
return ExprAsType(
context, Parse::NodeId::None,
MakeFloatTypeLiteral(context, Parse::NodeId::None,
SemIR::FloatKind::None, context.ints().Add(64)));
}
return {.inst_id = SemIR::TypeInstId::None, .type_id = SemIR::TypeId::None};