mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
Add support for nullptr literals in macros (#6426)
Adding support for macros that evaluate to nullptr literal.
Demo:
```c++
// macros.h
void foo(int a[2]);
#define MyNullPtr nullptr
```
```c++
// macros.cpp
void foo(int a[2]) {
if (!a) {
printf("array a is nullptr\n");
return;
}
printf("a[0] = %d \n", a[0]);
}
```
```c++
// main.carbon
library "Main";
import Cpp library "macros.h";
fn Run() -> i32 {
Cpp.foo(Cpp.MyNullPtr);
return 0;
}
```
```
$ clang -c macros.cpp;
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link macros.o main.o \--output=demo_carbon
$ ./demo_carbon
array a is nullptr
```
Part of #6303
This commit is contained in:
@@ -2264,6 +2264,10 @@ static auto MapConstant(Context& context, SemIR::LocId loc_id,
|
||||
MakeStringLiteral(context, Parse::StringLiteralId::None, string_id);
|
||||
context.imports().push_back(inst_id);
|
||||
return inst_id;
|
||||
} else if (isa<clang::CXXNullPtrLiteralExpr>(expr)) {
|
||||
auto type_id = MapNullptrType(context, loc_id).type_id;
|
||||
return GetOrAddInst<SemIR::UninitializedValue>(context, SemIR::LocId::None,
|
||||
{.type_id = type_id});
|
||||
}
|
||||
|
||||
SemIR::TypeId type_id = MapType(context, loc_id, expr->getType()).type_id;
|
||||
|
||||
Reference in New Issue
Block a user