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
+9 -3
View File
@@ -2244,9 +2244,8 @@ static auto IsIncompleteClass(Context& context, SemIR::NameScopeId scope_id)
context.classes().Get(class_decl->class_id).self_type_id);
}
// Maps a Clang literal expression to a Carbon constant. Currently supports
// only integer and floating-point literals.
// TODO: Add support for the other constant types for which a C++ to Carbon type
// Maps a Clang literal expression to a Carbon constant.
// TODO: Add support for all constant types for which a C++ to Carbon type
// mapping exists.
static auto MapConstant(Context& context, SemIR::LocId loc_id,
clang::Expr* expr) -> SemIR::InstId {
@@ -2286,6 +2285,13 @@ static auto MapConstant(Context& context, SemIR::LocId loc_id,
context,
MakeImportedLocIdAndInst<SemIR::IntValue>(
context, imported_loc_id, {.type_id = type_id, .int_id = int_id}));
} else if (auto* bool_literal = dyn_cast<clang::CXXBoolLiteralExpr>(expr)) {
inst_id = AddInstInNoBlock(
context,
MakeImportedLocIdAndInst<SemIR::BoolLiteral>(
context, imported_loc_id,
{.type_id = type_id,
.value = SemIR::BoolValue::From(bool_literal->getValue())}));
} else if (auto* float_literal = dyn_cast<clang::FloatingLiteral>(expr)) {
FloatId float_id = context.floats().Add(float_literal->getValue());
inst_id = AddInstInNoBlock(context,