Add support for boolean literals in macros (#6418)

Adding support for macros with boolean literals.

Demo:

```c++
// main.carbon

library "Main";

import Core library "io";

import Cpp inline '''
  #define M_TRUE true
''';

fn Run() -> i32 {
  let a: bool = Cpp.M_TRUE;
  if (a) {
    Core.Print(1);
  } else {
    Core.Print(0);
  }
  return 0;
}
```

```
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link main.o \--output=demo_carbon
$ ./demo_carbon
1
```

Part of #6303
This commit is contained in:
Ivana Ivanovska
2025-11-24 10:40:57 +00:00
committed by GitHub
parent 201e408252
commit 093700b274
4 changed files with 239 additions and 7 deletions
+5
View File
@@ -71,6 +71,11 @@ auto TryEvaluateMacroToConstant(Context& context, SemIR::LocId loc_id,
clang::APValue ap_value = evaluated_result.Val;
switch (ap_value.getKind()) {
case clang::APValue::Int:
if (result_expr->getType()->isBooleanType()) {
return clang::CXXBoolLiteralExpr::Create(
sema.getASTContext(), ap_value.getInt().getBoolValue(),
result_expr->getType(), result_expr->getExprLoc());
}
return clang::IntegerLiteral::Create(
sema.getASTContext(), ap_value.getInt(), result_expr->getType(),
result_expr->getExprLoc());